diff options
Diffstat (limited to 'src/designer/src/components/propertyeditor/fontpropertymanager.cpp')
-rw-r--r-- | src/designer/src/components/propertyeditor/fontpropertymanager.cpp | 228 |
1 files changed, 160 insertions, 68 deletions
diff --git a/src/designer/src/components/propertyeditor/fontpropertymanager.cpp b/src/designer/src/components/propertyeditor/fontpropertymanager.cpp index b624de147..fdd33add2 100644 --- a/src/designer/src/components/propertyeditor/fontpropertymanager.cpp +++ b/src/designer/src/components/propertyeditor/fontpropertymanager.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 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:GPL-EXCEPT$ -** 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 General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** 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-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 #include "fontpropertymanager.h" #include "qtpropertymanager.h" @@ -41,21 +16,36 @@ #include <QtCore/qtextstream.h> #include <QtCore/qxmlstream.h> +#include <utility> + QT_BEGIN_NAMESPACE +using namespace Qt::StringLiterals; + namespace qdesigner_internal { + using DisambiguatedTranslation = std::pair<const char *, const char *>; + static const char *aliasingC[] = { QT_TRANSLATE_NOOP("FontPropertyManager", "PreferDefault"), QT_TRANSLATE_NOOP("FontPropertyManager", "NoAntialias"), QT_TRANSLATE_NOOP("FontPropertyManager", "PreferAntialias") }; + static const DisambiguatedTranslation hintingPreferenceC[] = { + QT_TRANSLATE_NOOP3("FontPropertyManager", "PreferDefaultHinting", "QFont::StyleStrategy combo"), + QT_TRANSLATE_NOOP3("FontPropertyManager", "PreferNoHinting", "QFont::StyleStrategy combo"), + QT_TRANSLATE_NOOP3("FontPropertyManager", "PreferVerticalHinting", "QFont::StyleStrategy combo"), + QT_TRANSLATE_NOOP3("FontPropertyManager", "PreferFullHinting", "QFont::StyleStrategy combo") + }; + FontPropertyManager::FontPropertyManager() { - const int nameCount = sizeof(aliasingC)/sizeof(const char *); - for (int i = 0; i < nameCount; i++) - m_aliasingEnumNames.push_back(QCoreApplication::translate("FontPropertyManager", aliasingC[i])); + for (const auto *a : aliasingC) + m_aliasingEnumNames.append(QCoreApplication::translate("FontPropertyManager", a)); + + for (const auto &h : hintingPreferenceC) + m_hintingPreferenceEnumNames.append(QCoreApplication::translate("FontPropertyManager", h.first, h.second)); QString errorMessage; if (!readFamilyMapping(&m_familyMappings, &errorMessage)) { @@ -69,7 +59,7 @@ namespace qdesigner_internal { ResetMap &resetMap) { if (m_createdFontProperty) { - PropertyToSubPropertiesMap::iterator it = m_propertyToFontSubProperties.find(m_createdFontProperty); + auto it = m_propertyToFontSubProperties.find(m_createdFontProperty); if (it == m_propertyToFontSubProperties.end()) it = m_propertyToFontSubProperties.insert(m_createdFontProperty, PropertyList()); const int index = it.value().size(); @@ -79,7 +69,7 @@ namespace qdesigner_internal { resetMap[property] = true; } - if (type == QVariant::Font) + if (type == QMetaType::QFont) m_createdFontProperty = property; } @@ -103,24 +93,33 @@ namespace qdesigner_internal { int type, int enumTypeId) { - if (type != QVariant::Font) + if (type != QMetaType::QFont) return; // This will cause a recursion QtVariantProperty *antialiasing = vm->addProperty(enumTypeId, QCoreApplication::translate("FontPropertyManager", "Antialiasing")); const QFont font = qvariant_cast<QFont>(vm->variantProperty(property)->value()); - antialiasing->setAttribute(QStringLiteral("enumNames"), m_aliasingEnumNames); + antialiasing->setAttribute(u"enumNames"_s, m_aliasingEnumNames); antialiasing->setValue(antialiasingToIndex(font.styleStrategy())); property->addSubProperty(antialiasing); m_propertyToAntialiasing[property] = antialiasing; m_antialiasingToProperty[antialiasing] = property; + + QtVariantProperty *hintingPreference = vm->addProperty(enumTypeId, QCoreApplication::translate("FontPropertyManager", "HintingPreference")); + hintingPreference->setAttribute(u"enumNames"_s, m_hintingPreferenceEnumNames); + hintingPreference->setValue(hintingPreferenceToIndex(font.hintingPreference())); + property->addSubProperty(hintingPreference); + + m_propertyToHintingPreference[property] = hintingPreference; + m_hintingPreferenceToProperty[hintingPreference] = property; + // Fiddle family names if (!m_familyMappings.isEmpty()) { - const PropertyToSubPropertiesMap::iterator it = m_propertyToFontSubProperties.find(m_createdFontProperty); + const auto it = m_propertyToFontSubProperties.find(m_createdFontProperty); QtVariantProperty *familyProperty = vm->variantProperty(it.value().constFirst()); - const QString enumNamesAttribute = QStringLiteral("enumNames"); + const QString enumNamesAttribute = u"enumNames"_s; QStringList plainFamilyNames = familyProperty->attributeValue(enumNamesAttribute).toStringList(); // Did someone load fonts or something? if (m_designerFamilyNames.size() != plainFamilyNames.size()) @@ -133,7 +132,7 @@ namespace qdesigner_internal { bool FontPropertyManager::uninitializeProperty(QtProperty *property) { - const PropertyToPropertyMap::iterator ait = m_propertyToAntialiasing.find(property); + const auto ait = m_propertyToAntialiasing.find(property); if (ait != m_propertyToAntialiasing.end()) { QtProperty *antialiasing = ait.value(); m_antialiasingToProperty.remove(antialiasing); @@ -141,7 +140,15 @@ namespace qdesigner_internal { delete antialiasing; } - PropertyToSubPropertiesMap::iterator sit = m_propertyToFontSubProperties.find(property); + const auto hit = m_propertyToHintingPreference.find(property); + if (hit != m_propertyToHintingPreference.end()) { + QtProperty *hintingPreference = hit.value(); + m_hintingPreferenceToProperty.remove(hintingPreference); + m_propertyToHintingPreference.erase(hit); + delete hintingPreference; + } + + const auto sit = m_propertyToFontSubProperties.find(property); if (sit == m_propertyToFontSubProperties.end()) return false; @@ -155,20 +162,30 @@ namespace qdesigner_internal { void FontPropertyManager::slotPropertyDestroyed(QtProperty *property) { removeAntialiasingProperty(property); + removeHintingPreferenceProperty(property); } void FontPropertyManager::removeAntialiasingProperty(QtProperty *property) { - const PropertyToPropertyMap::iterator ait = m_antialiasingToProperty.find(property); + const auto ait = m_antialiasingToProperty.find(property); if (ait == m_antialiasingToProperty.end()) return; m_propertyToAntialiasing[ait.value()] = 0; m_antialiasingToProperty.erase(ait); } + void FontPropertyManager::removeHintingPreferenceProperty(QtProperty *property) + { + const auto hit = m_hintingPreferenceToProperty.find(property); + if (hit == m_hintingPreferenceToProperty.end()) + return; + m_propertyToHintingPreference[hit.value()] = nullptr; + m_hintingPreferenceToProperty.erase(hit); + } + bool FontPropertyManager::resetFontSubProperty(QtVariantPropertyManager *vm, QtProperty *property) { - const PropertyToPropertyMap::iterator it = m_fontSubPropertyToProperty.find(property); + const auto it = m_fontSubPropertyToProperty.find(property); if (it == m_fontSubPropertyToProperty.end()) return false; @@ -207,31 +224,80 @@ namespace qdesigner_internal { return QFont::PreferDefault; } + int FontPropertyManager::hintingPreferenceToIndex(QFont::HintingPreference h) + { + switch (h) { + case QFont::PreferDefaultHinting: + return 0; + case QFont::PreferNoHinting: + return 1; + case QFont::PreferVerticalHinting: + return 2; + case QFont::PreferFullHinting: + return 3; + } + return 0; + } + + QFont::HintingPreference FontPropertyManager::indexToHintingPreference(int idx) + { + switch (idx) { + case 0: + return QFont::PreferDefaultHinting; + case 1: + return QFont::PreferNoHinting; + case 2: + return QFont::PreferVerticalHinting; + case 3: + return QFont::PreferFullHinting; + } + return QFont::PreferDefaultHinting; + } + unsigned FontPropertyManager::fontFlag(int idx) { switch (idx) { - case 0: return QFont::FamilyResolved; - case 1: return QFont::SizeResolved; - case 2: return QFont::WeightResolved; - case 3: return QFont::StyleResolved; - case 4: return QFont::UnderlineResolved; - case 5: return QFont::StrikeOutResolved; - case 6: return QFont::KerningResolved; - case 7: return QFont::StyleStrategyResolved; + case 0: + return QFont::FamilyResolved | QFont::FamiliesResolved; + case 1: + return QFont::SizeResolved; + case 2: + case 7: + return QFont::WeightResolved; + case 3: + return QFont::StyleResolved; + case 4: + return QFont::UnderlineResolved; + case 5: + return QFont::StrikeOutResolved; + case 6: + return QFont::KerningResolved; + case 8: + return QFont::StyleStrategyResolved; + case 9: + return QFont::HintingPreferenceResolved; } return 0; } int FontPropertyManager::valueChanged(QtVariantPropertyManager *vm, QtProperty *property, const QVariant &value) { - QtProperty *antialiasingProperty = m_antialiasingToProperty.value(property, 0); - if (!antialiasingProperty) { - if (m_propertyToFontSubProperties.contains(property)) { - updateModifiedState(property, value); - } - return DesignerPropertyManager::NoMatch; - } + if (auto *antialiasingProperty = m_antialiasingToProperty.value(property, nullptr)) + return antialiasingValueChanged(vm, antialiasingProperty, value); + + if (auto *hintingPreferenceProperty = m_hintingPreferenceToProperty.value(property, nullptr)) + return hintingPreferenceValueChanged(vm, hintingPreferenceProperty, value); + + if (m_propertyToFontSubProperties.contains(property)) + updateModifiedState(property, value); + + return DesignerPropertyManager::NoMatch; + } + int FontPropertyManager::antialiasingValueChanged(QtVariantPropertyManager *vm, + QtProperty *antialiasingProperty, + const QVariant &value) + { QtVariantProperty *fontProperty = vm->variantProperty(antialiasingProperty); const QFont::StyleStrategy newValue = indexToAntialiasing(value.toInt()); @@ -245,9 +311,26 @@ namespace qdesigner_internal { return DesignerPropertyManager::Changed; } + int FontPropertyManager::hintingPreferenceValueChanged(QtVariantPropertyManager *vm, + QtProperty *hintingPreferenceProperty, + const QVariant &value) + { + QtVariantProperty *fontProperty = vm->variantProperty(hintingPreferenceProperty); + const QFont::HintingPreference newValue = indexToHintingPreference(value.toInt()); + + QFont font = qvariant_cast<QFont>(fontProperty->value()); + const QFont::HintingPreference oldValue = font.hintingPreference(); + if (newValue == oldValue) + return DesignerPropertyManager::Unchanged; + + font.setHintingPreference(newValue); + fontProperty->setValue(QVariant::fromValue(font)); + return DesignerPropertyManager::Changed; + } + void FontPropertyManager::updateModifiedState(QtProperty *property, const QVariant &value) { - const PropertyToSubPropertiesMap::iterator it = m_propertyToFontSubProperties.find(property); + const auto it = m_propertyToFontSubProperties.find(property); if (it == m_propertyToFontSubProperties.end()) return; @@ -274,6 +357,14 @@ namespace qdesigner_internal { antialiasing->setValue(antialiasingToIndex(font.styleStrategy())); } } + + if (QtProperty *hintingPreferenceProperty = m_propertyToHintingPreference.value(property, nullptr)) { + if (auto *hintingPreference = vm->variantProperty(hintingPreferenceProperty)) { + QFont font = qvariant_cast<QFont>(value); + hintingPreference->setValue(hintingPreferenceToIndex(font.hintingPreference())); + } + } + } /* Parse a mappings file of the form: @@ -281,14 +372,14 @@ namespace qdesigner_internal { * <mapping><family>DejaVu Sans</family><display>DejaVu Sans [CE]</display></mapping> * ... which is used to display on which platforms fonts are available.*/ - static const char *rootTagC = "fontmappings"; - static const char *mappingTagC = "mapping"; - static const char *familyTagC = "family"; - static const char *displayTagC = "display"; +static constexpr auto rootTagC = "fontmappings"_L1; +static constexpr auto mappingTagC = "mapping"_L1; +static constexpr auto familyTagC = "family"_L1; +static constexpr auto displayTagC = "display"_L1; static QString msgXmlError(const QXmlStreamReader &r, const QString& fileName) { - return QString::fromUtf8("An error has been encountered at line %1 of %2: %3:").arg(r.lineNumber()).arg(fileName, r.errorString()); + return u"An error has been encountered at line %1 of %2: %3:"_s.arg(r.lineNumber()).arg(fileName, r.errorString()); } /* Switch stages when encountering a start element (state table) */ @@ -299,14 +390,14 @@ namespace qdesigner_internal { { switch (currentStage) { case ParseBeginning: - return startElement == QLatin1String(rootTagC) ? ParseWithinRoot : ParseError; + return startElement == rootTagC ? ParseWithinRoot : ParseError; case ParseWithinRoot: case ParseWithinDisplay: // Next mapping, was in <display> - return startElement == QLatin1String(mappingTagC) ? ParseWithinMapping : ParseError; + return startElement == mappingTagC ? ParseWithinMapping : ParseError; case ParseWithinMapping: - return startElement == QLatin1String(familyTagC) ? ParseWithinFamily : ParseError; + return startElement == familyTagC ? ParseWithinFamily : ParseError; case ParseWithinFamily: - return startElement == QLatin1String(displayTagC) ? ParseWithinDisplay : ParseError; + return startElement == displayTagC ? ParseWithinDisplay : ParseError; case ParseError: break; } @@ -316,10 +407,10 @@ namespace qdesigner_internal { bool FontPropertyManager::readFamilyMapping(NameMap *rc, QString *errorMessage) { rc->clear(); - const QString fileName = QStringLiteral(":/qt-project.org/propertyeditor/fontmapping.xml"); + const QString fileName = u":/qt-project.org/propertyeditor/fontmapping.xml"_s; QFile file(fileName); if (!file.open(QIODevice::ReadOnly)) { - *errorMessage = QString::fromUtf8("Unable to open %1: %2").arg(fileName, file.errorString()); + *errorMessage = "Unable to open %1: %2"_L1.arg(fileName, file.errorString()); return false; } @@ -338,7 +429,7 @@ namespace qdesigner_internal { stage = nextStage(stage, reader.name()); switch (stage) { case ParseError: - reader.raiseError(QString::fromUtf8("Unexpected element <%1>.").arg(reader.name().toString())); + reader.raiseError("Unexpected element <%1>."_L1.arg(reader.name())); *errorMessage = msgXmlError(reader, fileName); return false; case ParseWithinFamily: @@ -350,6 +441,7 @@ namespace qdesigner_internal { default: break; } + break; default: break; } |