diff options
Diffstat (limited to 'src/designer/src/lib/uilib/properties.cpp')
-rw-r--r-- | src/designer/src/lib/uilib/properties.cpp | 164 |
1 files changed, 89 insertions, 75 deletions
diff --git a/src/designer/src/lib/uilib/properties.cpp b/src/designer/src/lib/uilib/properties.cpp index d7ba8cef1..872e8975f 100644 --- a/src/designer/src/lib/uilib/properties.cpp +++ b/src/designer/src/lib/uilib/properties.cpp @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2020 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the Qt Designer of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2020 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "properties_p.h" #include "ui4_p.h" @@ -53,21 +17,17 @@ #include <QtWidgets/qframe.h> #include <QtWidgets/qabstractscrollarea.h> +#include <limits> + QT_BEGIN_NAMESPACE +using namespace Qt::StringLiterals; + #ifdef QFORMINTERNAL_NAMESPACE namespace QFormInternal { #endif -static inline void fixEnum(QString &s) -{ - int qualifierIndex = s.lastIndexOf(QLatin1Char(':')); - if (qualifierIndex == -1) - qualifierIndex = s.lastIndexOf(QLatin1Char('.')); - if (qualifierIndex != -1) - s.remove(0, qualifierIndex + 1); -} // Convert complex DOM types with the help of QAbstractFormBuilder QVariant domPropertyToVariant(QAbstractFormBuilder *afb,const QMetaObject *meta,const DomProperty *p) { @@ -107,29 +67,44 @@ QVariant domPropertyToVariant(QAbstractFormBuilder *afb,const QMetaObject *meta, const QMetaEnum e = meta->property(index).enumerator(); Q_ASSERT(e.isFlag() == true); - return QVariant(e.keysToValue(p->elementSet().toUtf8())); + bool ok{}; + QVariant result(e.keysToValue(p->elementSet().toUtf8().constData(), &ok)); + if (!ok) { + uiLibWarning(QCoreApplication::translate("QFormBuilder", + "The value \"%1\" of the set-type property %2 could not be read."). + arg(p->attributeName(), p->elementSet())); + return {}; + } + return result; } case DomProperty::Enum: { const QByteArray pname = p->attributeName().toUtf8(); const int index = meta->indexOfProperty(pname); - QString enumValue = p->elementEnum(); + const auto &enumValue = p->elementEnum(); // Triggers in case of objects in Designer like Spacer/Line for which properties // are serialized using language introspection. On preview, however, these objects are // emulated by hacks in the formbuilder (size policy/orientation) - fixEnum(enumValue); if (index == -1) { // ### special-casing for Line (QFrame) -- fix for 4.2. Jambi hack for enumerations if (!qstrcmp(meta->className(), "QFrame") && (pname == QByteArray("orientation"))) { - return QVariant(enumValue == QFormBuilderStrings::instance().horizontalPostFix ? QFrame::HLine : QFrame::VLine); + return QVariant(enumValue.endsWith("Horizontal"_L1) ? QFrame::HLine : QFrame::VLine); } uiLibWarning(QCoreApplication::translate("QFormBuilder", "The enumeration-type property %1 could not be read.").arg(p->attributeName())); return QVariant(); } const QMetaEnum e = meta->property(index).enumerator(); - return QVariant(e.keyToValue(enumValue.toUtf8())); + bool ok{}; + QVariant result(e.keyToValue(enumValue.toUtf8().constData(), &ok)); + if (!ok) { + uiLibWarning(QCoreApplication::translate("QFormBuilder", + "The value \"%1\" of the enum-type property %2 could not be read."). + arg(p->attributeName(), enumValue)); + return {}; + } + return result; } case DomProperty::Brush: return QVariant::fromValue(afb->setupBrush(p->elementBrush())); @@ -145,13 +120,22 @@ QVariant domPropertyToVariant(QAbstractFormBuilder *afb,const QMetaObject *meta, return domPropertyToVariant(p); } +// Convert a legacy Qt 4 integer font weight to the closes enumeration value + +static inline QMetaEnum fontWeightMetaEnum() +{ + const QMetaEnum result = metaEnum<QAbstractFormBuilderGadget>("fontWeight"); + Q_ASSERT(result.isValid()); + return result; +} + // Convert simple DOM types QVariant domPropertyToVariant(const DomProperty *p) { // requires non-const virtual nameToIcon, etc. switch(p->kind()) { case DomProperty::Bool: - return QVariant(p->elementBool() == QFormBuilderStrings::instance().trueValue); + return QVariant(p->elementBool() == "true"_L1); case DomProperty::Cstring: return QVariant(p->elementCstring().toUtf8()); @@ -230,8 +214,6 @@ QVariant domPropertyToVariant(const DomProperty *p) f.setPointSize(font->elementPointSize()); if (font->hasElementItalic()) f.setItalic(font->elementItalic()); - if (font->hasElementBold()) - f.setBold(font->elementBold()); if (font->hasElementUnderline()) f.setUnderline(font->elementUnderline()); if (font->hasElementStrikeOut()) @@ -244,6 +226,19 @@ QVariant domPropertyToVariant(const DomProperty *p) f.setStyleStrategy(enumKeyOfObjectToValue<QAbstractFormBuilderGadget, QFont::StyleStrategy>("styleStrategy", font->elementStyleStrategy().toLatin1().constData())); } + if (font->hasElementHintingPreference()) { + f.setHintingPreference(enumKeyOfObjectToValue<QAbstractFormBuilderGadget, QFont::HintingPreference>("hintingPreference", + font->elementHintingPreference().toLatin1().constData())); + } + + if (font->hasElementFontWeight()) { + f.setWeight(enumKeyOfObjectToValue<QAbstractFormBuilderGadget, QFont::Weight>( + "fontWeight", + font->elementFontWeight().toLatin1().constData())); + } else if (font->hasElementBold()) { + f.setBold(font->elementBold()); + } + return QVariant::fromValue(f); } @@ -282,8 +277,8 @@ QVariant domPropertyToVariant(const DomProperty *p) const DomLocale *locale = p->elementLocale(); return QVariant::fromValue(QLocale(enumKeyOfObjectToValue<QAbstractFormBuilderGadget, QLocale::Language>("language", locale->attributeLanguage().toLatin1().constData()), - enumKeyOfObjectToValue<QAbstractFormBuilderGadget, QLocale::Country>("country", - locale->attributeCountry().toLatin1().constData()))); + enumKeyOfObjectToValue<QAbstractFormBuilderGadget, QLocale::Territory>("country", + locale->attributeCountry().toLatin1().constData()))); } case DomProperty::SizePolicy: { const DomSizePolicy *sizep = p->elementSizePolicy(); @@ -330,7 +325,7 @@ static bool applySimpleProperty(const QVariant &v, bool translateString, DomProp DomString *str = new DomString(); str->setText(v.toString()); if (!translateString) - str->setAttributeNotr(QStringLiteral("true")); + str->setAttributeNotr(u"true"_s); dom_prop->setElementString(str); } return true; @@ -360,7 +355,7 @@ static bool applySimpleProperty(const QVariant &v, bool translateString, DomProp return true; case QMetaType::Bool: - dom_prop->setElementBool(v.toBool() ? QFormBuilderStrings::instance().trueValue : QFormBuilderStrings::instance().falseValue); + dom_prop->setElementBool(v.toBool() ? "true"_L1 : "false"_L1); return true; case QMetaType::QChar: { @@ -446,9 +441,23 @@ static bool applySimpleProperty(const QVariant &v, bool translateString, DomProp DomFont *fnt = new DomFont(); const QFont font = qvariant_cast<QFont>(v); const uint mask = font.resolveMask(); - if (mask & QFont::WeightResolved) - fnt->setElementBold(font.bold()); - if (mask & QFont::FamilyResolved) + if (mask & QFont::WeightResolved) { + switch (font.weight()) { + case QFont::Normal: + fnt->setElementBold(false); + break; + case QFont::Bold: + fnt->setElementBold(true); + break; + default: { + const QMetaEnum me = fontWeightMetaEnum(); + const QString ws = QLatin1StringView(me.valueToKey(font.weight())); + fnt->setElementFontWeight(ws); + } + break; + } + } + if ((mask & (QFont::FamilyResolved | QFont::FamiliesResolved)) != 0) fnt->setElementFamily(font.family()); if (mask & QFont::StyleResolved) fnt->setElementItalic(font.italic()); @@ -462,8 +471,13 @@ static bool applySimpleProperty(const QVariant &v, bool translateString, DomProp fnt->setElementKerning(font.kerning()); if (mask & QFont::StyleStrategyResolved) { const QMetaEnum styleStrategy_enum = metaEnum<QAbstractFormBuilderGadget>("styleStrategy"); - fnt->setElementStyleStrategy(QLatin1String(styleStrategy_enum.valueToKey(font.styleStrategy()))); + fnt->setElementStyleStrategy(QLatin1StringView(styleStrategy_enum.valueToKey(font.styleStrategy()))); + } + if (mask & QFont::HintingPreferenceResolved) { + const QMetaEnum hintingPreference_enum = metaEnum<QAbstractFormBuilderGadget>("hintingPreference"); + fnt->setElementHintingPreference(QLatin1StringView(hintingPreference_enum.valueToKey(font.hintingPreference()))); } + dom_prop->setElementFont(fnt); } return true; @@ -471,7 +485,7 @@ static bool applySimpleProperty(const QVariant &v, bool translateString, DomProp #if QT_CONFIG(cursor) case QMetaType::QCursor: { const QMetaEnum cursorShape_enum = metaEnum<QAbstractFormBuilderGadget>("cursorShape"); - dom_prop->setElementCursorShape(QLatin1String(cursorShape_enum.valueToKey(qvariant_cast<QCursor>(v).shape()))); + dom_prop->setElementCursorShape(QLatin1StringView(cursorShape_enum.valueToKey(qvariant_cast<QCursor>(v).shape()))); } return true; #endif @@ -488,10 +502,10 @@ static bool applySimpleProperty(const QVariant &v, bool translateString, DomProp const QLocale locale = qvariant_cast<QLocale>(v); const QMetaEnum language_enum = metaEnum<QAbstractFormBuilderGadget>("language"); - const QMetaEnum country_enum = metaEnum<QAbstractFormBuilderGadget>("country"); + const QMetaEnum territory_enum = metaEnum<QAbstractFormBuilderGadget>("country"); - dom->setAttributeLanguage(QLatin1String(language_enum.valueToKey(locale.language()))); - dom->setAttributeCountry(QLatin1String(country_enum.valueToKey(locale.country()))); + dom->setAttributeLanguage(QLatin1StringView(language_enum.valueToKey(locale.language()))); + dom->setAttributeCountry(QLatin1StringView(territory_enum.valueToKey(locale.territory()))); dom_prop->setElementLocale(dom); } @@ -506,8 +520,8 @@ static bool applySimpleProperty(const QVariant &v, bool translateString, DomProp const QMetaEnum sizeType_enum = metaEnum<QAbstractFormBuilderGadget>("sizeType"); - dom->setAttributeHSizeType(QLatin1String(sizeType_enum.valueToKey(sizePolicy.horizontalPolicy()))); - dom->setAttributeVSizeType(QLatin1String(sizeType_enum.valueToKey(sizePolicy.verticalPolicy()))); + dom->setAttributeHSizeType(QLatin1StringView(sizeType_enum.valueToKey(sizePolicy.horizontalPolicy()))); + dom->setAttributeVSizeType(QLatin1StringView(sizeType_enum.valueToKey(sizePolicy.verticalPolicy()))); dom_prop->setElementSizePolicy(dom); } @@ -580,7 +594,7 @@ static bool applySimpleProperty(const QVariant &v, bool translateString, DomProp static QString msgCannotWriteProperty(const QString &pname, const QVariant &v) { return QCoreApplication::translate("QFormBuilder", "The property %1 could not be written. The type %2 is not supported yet."). - arg(pname).arg(QLatin1String(v.typeName())); + arg(pname).arg(QLatin1StringView(v.typeName())); } @@ -595,10 +609,9 @@ static bool isOfType(const QMetaObject *what, const QMetaObject *type) static bool isTranslatable(const QString &pname, const QVariant &v, const QMetaObject *meta) { - const QFormBuilderStrings &strings = QFormBuilderStrings::instance(); - if (pname == strings.objectNameProperty) + if (pname == "objectName"_L1) return false; - if (pname == strings.styleSheetProperty && v.metaType().id() == QMetaType::QString + if (pname == "styleSheet"_L1 && v.metaType().id() == QMetaType::QString && isOfType(meta, &QWidget::staticMetaObject)) { return false; } @@ -610,8 +623,6 @@ static bool isTranslatable(const QString &pname, const QVariant &v, const QMetaO DomProperty *variantToDomProperty(QAbstractFormBuilder *afb, const QMetaObject *meta, const QString &pname, const QVariant &v) { - const QFormBuilderStrings &strings = QFormBuilderStrings::instance(); - DomProperty *dom_prop = new DomProperty(); dom_prop->setAttributeName(pname); @@ -626,8 +637,11 @@ DomProperty *variantToDomProperty(QAbstractFormBuilder *afb, const QMetaObject * dom_prop->setElementEnum(QString::fromLatin1(e.valueToKey(v.toInt()))); return dom_prop; } - if (!meta_property.hasStdCppSet() || (isOfType(meta, &QAbstractScrollArea::staticMetaObject) && pname == strings.cursorProperty)) + if (!meta_property.hasStdCppSet() + || (isOfType(meta, &QAbstractScrollArea::staticMetaObject) + && pname == "cursor"_L1)) { dom_prop->setAttributeStdset(0); + } } // Try simple properties |