From 394cb9e08516751025bca075d5188b661f4a88da Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 6 Jan 2012 16:37:12 +0100 Subject: [PATCH] Qt Designer: Simplify code for translatable properties. Introduce base class for PropertySheetStringValue, PropertySheetKeySequenceValue, which allows for removing redundant code to set/retrieve the translation parameters to/from the DOM classes. Task-number: QTBUG-8926 Task-number: QTBUG-20440 Change-Id: I5b65fff2bb27be82d04f96cf0d66652887b1c4b7 Sanity-Review: Qt Sanity Bot Reviewed-by: Friedemann Kleint --- .../components/formeditor/qdesigner_resource.cpp | 158 ++++++++------------- src/designer/src/lib/shared/qdesigner_utils.cpp | 99 +++---------- src/designer/src/lib/shared/qdesigner_utils_p.h | 56 ++++---- 3 files changed, 108 insertions(+), 205 deletions(-) diff --git a/src/designer/src/components/formeditor/qdesigner_resource.cpp b/src/designer/src/components/formeditor/qdesigner_resource.cpp index 7d3ccfb..0b4f267 100644 --- a/src/designer/src/components/formeditor/qdesigner_resource.cpp +++ b/src/designer/src/components/formeditor/qdesigner_resource.cpp @@ -339,6 +339,34 @@ bool QDesignerResourceBuilder::isResourceType(const QVariant &value) const return false; } // ------------------------- QDesignerTextBuilder + +template // for DomString, potentially DomStringList +inline void translationParametersToDom(const PropertySheetTranslatableData &data, DomElement *e) +{ + const QString propertyComment = data.disambiguation(); + if (!propertyComment.isEmpty()) + e->setAttributeComment(propertyComment); + const QString propertyExtracomment = data.comment(); + if (!propertyExtracomment.isEmpty()) + e->setAttributeExtraComment(propertyExtracomment); + if (!data.translatable()) + e->setAttributeNotr(QStringLiteral("true")); +} + +template // for DomString, potentially DomStringList +inline void translationParametersFromDom(const DomElement *e, PropertySheetTranslatableData *data) +{ + if (e->hasAttributeComment()) + data->setDisambiguation(e->attributeComment()); + if (e->hasAttributeExtraComment()) + data->setComment(e->attributeExtraComment()); + if (e->hasAttributeNotr()) { + const QString notr = e->attributeNotr(); + const bool translatable = !(notr == QStringLiteral("true") || notr == QStringLiteral("yes")); + data->setTranslatable(translatable); + } +} + class QDesignerTextBuilder : public QTextBuilder { public: @@ -353,21 +381,12 @@ public: QVariant QDesignerTextBuilder::loadText(const DomProperty *text) const { - const DomString *str = text->elementString(); - PropertySheetStringValue strVal(str->text()); - if (str->hasAttributeComment()) { - strVal.setDisambiguation(str->attributeComment()); + if (const DomString *domString = text->elementString()) { + PropertySheetStringValue stringValue(domString->text()); + translationParametersFromDom(domString, &stringValue); + return QVariant::fromValue(stringValue); } - if (str->hasAttributeExtraComment()) { - strVal.setComment(str->attributeExtraComment()); - } - if (str->hasAttributeNotr()) { - const QString notr = str->attributeNotr(); - const bool translatable = !(notr == QStringLiteral("true") || notr == QStringLiteral("yes")); - if (!translatable) - strVal.setTranslatable(translatable); - } - return QVariant::fromValue(strVal); + return QVariant(QString()); } QVariant QDesignerTextBuilder::toNativeValue(const QVariant &value) const @@ -377,34 +396,35 @@ QVariant QDesignerTextBuilder::toNativeValue(const QVariant &value) const return value; } -DomProperty *QDesignerTextBuilder::saveText(const QVariant &value) const +static inline DomProperty *stringToDomProperty(const QString &value) { - if (!value.canConvert() && !value.canConvert()) - return 0; + DomString *domString = new DomString(); + domString->setText(value); + DomProperty *property = new DomProperty(); + property->setElementString(domString); + return property; +} +static inline DomProperty *stringToDomProperty(const QString &value, + const PropertySheetTranslatableData &translatableData) +{ + DomString *domString = new DomString(); + domString->setText(value); + translationParametersToDom(translatableData, domString); DomProperty *property = new DomProperty(); - DomString *domStr = new DomString(); + property->setElementString(domString); + return property; +} +DomProperty *QDesignerTextBuilder::saveText(const QVariant &value) const +{ if (value.canConvert()) { - PropertySheetStringValue str = qvariant_cast(value); - - domStr->setText(str.value()); - - const QString property_comment = str.disambiguation(); - if (!property_comment.isEmpty()) - domStr->setAttributeComment(property_comment); - const QString property_extraComment = str.comment(); - if (!property_extraComment.isEmpty()) - domStr->setAttributeExtraComment(property_extraComment); - const bool property_translatable = str.translatable(); - if (!property_translatable) - domStr->setAttributeNotr(QStringLiteral("true")); - } else { - domStr->setText(value.toString()); + const PropertySheetStringValue str = qvariant_cast(value); + return stringToDomProperty(str.value(), str); } - - property->setElementString(domStr); - return property; + if (value.canConvert()) + return stringToDomProperty(value.toString()); + return 0; } QDesignerResource::QDesignerResource(FormWindow *formWindow) : @@ -928,30 +948,12 @@ void QDesignerResource::applyProperties(QObject *o, const QList &p if (index != -1 && sheet->property(index).userType() == qMetaTypeId()) { const DomString *key = p->elementString(); PropertySheetKeySequenceValue keyVal(QKeySequence(key->text())); - if (key->hasAttributeComment()) - keyVal.setDisambiguation(key->attributeComment()); - if (key->hasAttributeExtraComment()) - keyVal.setComment(key->attributeExtraComment()); - if (key->hasAttributeNotr()) { - const QString notr = key->attributeNotr(); - const bool translatable = !(notr == QStringLiteral("true") || notr == QStringLiteral("yes")); - if (!translatable) - keyVal.setTranslatable(translatable); - } + translationParametersFromDom(key, &keyVal); v = QVariant::fromValue(keyVal); } else { const DomString *str = p->elementString(); PropertySheetStringValue strVal(v.toString()); - if (str->hasAttributeComment()) - strVal.setDisambiguation(str->attributeComment()); - if (str->hasAttributeExtraComment()) - strVal.setComment(str->attributeExtraComment()); - if (str->hasAttributeNotr()) { - const QString notr = str->attributeNotr(); - const bool translatable = !(notr == QStringLiteral("true") || notr == QStringLiteral("yes")); - if (!translatable) - strVal.setTranslatable(translatable); - } + translationParametersFromDom(str, &strVal); v = QVariant::fromValue(strVal); } } @@ -1340,42 +1342,6 @@ DomWidget *QDesignerResource::saveWidget(QDesignerDockWidget *dockWidget, DomWid return ui_widget; } -static void saveStringProperty(DomProperty *property, const PropertySheetStringValue &value) -{ - DomString *str = new DomString(); - str->setText(value.value()); - - const QString property_comment = value.disambiguation(); - if (!property_comment.isEmpty()) - str->setAttributeComment(property_comment); - const QString property_extraComment = value.comment(); - if (!property_extraComment.isEmpty()) - str->setAttributeExtraComment(property_extraComment); - const bool property_translatable = value.translatable(); - if (!property_translatable) - str->setAttributeNotr(QStringLiteral("true")); - - property->setElementString(str); -} - -static void saveKeySequenceProperty(DomProperty *property, const PropertySheetKeySequenceValue &value) -{ - DomString *str = new DomString(); - str->setText(value.value().toString()); - - const QString property_comment = value.disambiguation(); - if (!property_comment.isEmpty()) - str->setAttributeComment(property_comment); - const QString property_extraComment = value.comment(); - if (!property_extraComment.isEmpty()) - str->setAttributeExtraComment(property_extraComment); - const bool property_translatable = value.translatable(); - if (!property_translatable) - str->setAttributeNotr(QStringLiteral("true")); - - property->setElementString(str); -} - DomWidget *QDesignerResource::saveWidget(QTabWidget *widget, DomWidget *ui_parentWidget) { DomWidget *ui_widget = QAbstractFormBuilder::createDom(widget, ui_parentWidget, false); @@ -2028,25 +1994,21 @@ DomProperty *QDesignerResource::createProperty(QObject *object, const QString &p return applyProperStdSetAttribute(object, propertyName, p); } else if (value.canConvert()) { const PropertySheetStringValue strVal = qvariant_cast(value); - DomProperty *p = new DomProperty; + DomProperty *p = stringToDomProperty(strVal.value(), strVal); if (!hasSetter(core(), object, propertyName)) p->setAttributeStdset(0); p->setAttributeName(propertyName); - saveStringProperty(p, strVal); - return applyProperStdSetAttribute(object, propertyName, p); } else if (value.canConvert()) { const PropertySheetKeySequenceValue keyVal = qvariant_cast(value); - DomProperty *p = new DomProperty; + DomProperty *p = stringToDomProperty(keyVal.value().toString(), keyVal); if (!hasSetter(core(), object, propertyName)) p->setAttributeStdset(0); p->setAttributeName(propertyName); - saveKeySequenceProperty(p, keyVal); - return applyProperStdSetAttribute(object, propertyName, p); } diff --git a/src/designer/src/lib/shared/qdesigner_utils.cpp b/src/designer/src/lib/shared/qdesigner_utils.cpp index 2303d19..007c584 100644 --- a/src/designer/src/lib/shared/qdesigner_utils.cpp +++ b/src/designer/src/lib/shared/qdesigner_utils.cpp @@ -469,10 +469,19 @@ namespace qdesigner_internal } + PropertySheetTranslatableData::PropertySheetTranslatableData(bool translatable, const QString &disambiguation, const QString &comment) : + m_translatable(translatable), m_disambiguation(disambiguation), m_comment(comment) { } + + bool PropertySheetTranslatableData::equals(const PropertySheetTranslatableData &rhs) const + { + return m_translatable == rhs.m_translatable + && m_disambiguation == rhs.m_disambiguation + && m_comment == rhs.m_comment; + } + PropertySheetStringValue::PropertySheetStringValue(const QString &value, - bool translatable, const QString &disambiguation, const QString &comment) - : m_value(value), m_translatable(translatable), m_disambiguation(disambiguation), m_comment(comment) - { } + bool translatable, const QString &disambiguation, const QString &comment) : + PropertySheetTranslatableData(translatable, disambiguation, comment), m_value(value) {} QString PropertySheetStringValue::value() const { @@ -484,59 +493,20 @@ namespace qdesigner_internal m_value = value; } - bool PropertySheetStringValue::translatable() const - { - return m_translatable; - } - - void PropertySheetStringValue::setTranslatable(bool translatable) - { - m_translatable = translatable; - } - - QString PropertySheetStringValue::disambiguation() const - { - return m_disambiguation; - } - - void PropertySheetStringValue::setDisambiguation(const QString &disambiguation) - { - m_disambiguation = disambiguation; - } - - QString PropertySheetStringValue::comment() const - { - return m_comment; - } - - void PropertySheetStringValue::setComment(const QString &comment) - { - m_comment = comment; - } - bool PropertySheetStringValue::equals(const PropertySheetStringValue &rhs) const { - return (m_value == rhs.m_value) && (m_translatable == rhs.m_translatable) - && (m_disambiguation == rhs.m_disambiguation) && (m_comment == rhs.m_comment); + return m_value == rhs.m_value && PropertySheetTranslatableData::equals(rhs); } PropertySheetKeySequenceValue::PropertySheetKeySequenceValue(const QKeySequence &value, bool translatable, const QString &disambiguation, const QString &comment) - : m_value(value), - m_standardKey(QKeySequence::UnknownKey), - m_translatable(translatable), - m_disambiguation(disambiguation), - m_comment(comment) - { } + : PropertySheetTranslatableData(translatable, disambiguation, comment), + m_value(value), m_standardKey(QKeySequence::UnknownKey) {} PropertySheetKeySequenceValue::PropertySheetKeySequenceValue(const QKeySequence::StandardKey &standardKey, bool translatable, const QString &disambiguation, const QString &comment) - : m_value(QKeySequence(standardKey)), - m_standardKey(standardKey), - m_translatable(translatable), - m_disambiguation(disambiguation), - m_comment(comment) - { } + : PropertySheetTranslatableData(translatable, disambiguation, comment), + m_value(QKeySequence(standardKey)), m_standardKey(standardKey) {} QKeySequence PropertySheetKeySequenceValue::value() const { @@ -565,43 +535,12 @@ namespace qdesigner_internal return m_standardKey != QKeySequence::UnknownKey; } - QString PropertySheetKeySequenceValue::comment() const - { - return m_comment; - } - - void PropertySheetKeySequenceValue::setComment(const QString &comment) - { - m_comment = comment; - } - - QString PropertySheetKeySequenceValue::disambiguation() const - { - return m_disambiguation; - } - - void PropertySheetKeySequenceValue::setDisambiguation(const QString &disambiguation) - { - m_disambiguation = disambiguation; - } - - bool PropertySheetKeySequenceValue::translatable() const - { - return m_translatable; - } - - void PropertySheetKeySequenceValue::setTranslatable(bool translatable) - { - m_translatable = translatable; - } - bool PropertySheetKeySequenceValue::equals(const PropertySheetKeySequenceValue &rhs) const { - return (m_value == rhs.m_value) && (m_standardKey == rhs.m_standardKey) - && (m_translatable == rhs.m_translatable) && (m_disambiguation == rhs.m_disambiguation) && (m_comment == rhs.m_comment); + return m_value == rhs.m_value && m_standardKey == rhs.m_standardKey + && PropertySheetTranslatableData::equals(rhs); } - /* IconSubPropertyMask: Assign each icon sub-property (pixmaps for the * various states/modes and the theme) a flag bit (see QFont) so that they * can be handled individually when assigning property values to diff --git a/src/designer/src/lib/shared/qdesigner_utils_p.h b/src/designer/src/lib/shared/qdesigner_utils_p.h index 129b155..96ad21a 100644 --- a/src/designer/src/lib/shared/qdesigner_utils_p.h +++ b/src/designer/src/lib/shared/qdesigner_utils_p.h @@ -327,47 +327,59 @@ private: friend class FormWindowBase; }; +// -------------- PropertySheetTranslatableData: Base class for translatable properties. +class QDESIGNER_SHARED_EXPORT PropertySheetTranslatableData +{ +protected: + PropertySheetTranslatableData(bool translatable = true, + const QString &disambiguation = QString(), + const QString &comment = QString()); + bool equals(const PropertySheetTranslatableData &rhs) const; + +public: + bool translatable() const { return m_translatable; } + void setTranslatable(bool translatable) { m_translatable = translatable; } + QString disambiguation() const { return m_disambiguation; } + void setDisambiguation(const QString &d) { m_disambiguation = d; } + QString comment() const { return m_comment; } + void setComment(const QString &comment) { m_comment = comment; } + +private: + bool m_translatable; + QString m_disambiguation; + QString m_comment; +}; + // -------------- StringValue: Returned by the property sheet for strings -class QDESIGNER_SHARED_EXPORT PropertySheetStringValue +class QDESIGNER_SHARED_EXPORT PropertySheetStringValue : public PropertySheetTranslatableData { public: - explicit PropertySheetStringValue(const QString &value = QString(), - bool translatable = true, - const QString &disambiguation = QString(), - const QString &comment = QString()); + PropertySheetStringValue(const QString &value = QString(), bool translatable = true, + const QString &disambiguation = QString(), const QString &comment = QString()); bool operator==(const PropertySheetStringValue &other) const { return equals(other); } bool operator!=(const PropertySheetStringValue &other) const { return !equals(other); } QString value() const; void setValue(const QString &value); - bool translatable() const; - void setTranslatable(bool translatable); - QString disambiguation() const; - void setDisambiguation(const QString &disambiguation); - QString comment() const; - void setComment(const QString &comment); private: bool equals(const PropertySheetStringValue &rhs) const; QString m_value; - bool m_translatable; - QString m_disambiguation; - QString m_comment; }; // -------------- StringValue: Returned by the property sheet for strings -class QDESIGNER_SHARED_EXPORT PropertySheetKeySequenceValue +class QDESIGNER_SHARED_EXPORT PropertySheetKeySequenceValue : public PropertySheetTranslatableData { public: - explicit PropertySheetKeySequenceValue(const QKeySequence &value = QKeySequence(), + PropertySheetKeySequenceValue(const QKeySequence &value = QKeySequence(), bool translatable = true, const QString &disambiguation = QString(), const QString &comment = QString()); - explicit PropertySheetKeySequenceValue(const QKeySequence::StandardKey &standardKey, + PropertySheetKeySequenceValue(const QKeySequence::StandardKey &standardKey, bool translatable = true, const QString &disambiguation = QString(), const QString &comment = QString()); @@ -381,21 +393,11 @@ public: void setStandardKey(const QKeySequence::StandardKey &standardKey); bool isStandardKey() const; - bool translatable() const; - void setTranslatable(bool translatable); - QString disambiguation() const; - void setDisambiguation(const QString &disambiguation); - QString comment() const; - void setComment(const QString &comment); - private: bool equals(const PropertySheetKeySequenceValue &rhs) const; QKeySequence m_value; QKeySequence::StandardKey m_standardKey; - bool m_translatable; - QString m_disambiguation; - QString m_comment; }; } // namespace qdesigner_internal -- 2.7.4