diff options
Diffstat (limited to 'src/widgets/widgets/qfontcombobox.cpp')
-rw-r--r-- | src/widgets/widgets/qfontcombobox.cpp | 126 |
1 files changed, 52 insertions, 74 deletions
diff --git a/src/widgets/widgets/qfontcombobox.cpp b/src/widgets/widgets/qfontcombobox.cpp index 4a9d510177..c80687c7ed 100644 --- a/src/widgets/widgets/qfontcombobox.cpp +++ b/src/widgets/widgets/qfontcombobox.cpp @@ -1,46 +1,11 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtWidgets module 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) 2016 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 "qfontcombobox.h" +#include <qabstractitemdelegate.h> +#include <qaccessible.h> #include <qstringlistmodel.h> -#include <qitemdelegate.h> #include <qlistview.h> #include <qpainter.h> #include <qevent.h> @@ -48,8 +13,12 @@ #include <private/qcombobox_p.h> #include <qdebug.h> +#include <array> + QT_BEGIN_NAMESPACE +using namespace Qt::StringLiterals; + static QFontDatabase::WritingSystem writingSystemFromScript(QLocale::Script script) { switch (script) { @@ -134,7 +103,7 @@ static QFontDatabase::WritingSystem writingSystemFromLocale() static QFontDatabase::WritingSystem writingSystemForFont(const QFont &font, bool *hasLatin) { - QList<QFontDatabase::WritingSystem> writingSystems = QFontDatabase::writingSystems(font.families().first()); + QList<QFontDatabase::WritingSystem> writingSystems = QFontDatabase::writingSystems(font.families().constFirst()); // qDebug() << font.families().first() << writingSystems; // this just confuses the algorithm below. Vietnamese is Latin with lots of special chars @@ -166,13 +135,13 @@ static QFontDatabase::WritingSystem writingSystemForFont(const QFont &font, bool return system; } - if (writingSystems.count() == 1 && system > QFontDatabase::Cyrillic) + if (writingSystems.size() == 1 && system > QFontDatabase::Cyrillic) return system; - if (writingSystems.count() <= 2 && system > QFontDatabase::Armenian && system < QFontDatabase::Vietnamese) + if (writingSystems.size() <= 2 && system > QFontDatabase::Armenian && system < QFontDatabase::Vietnamese) return system; - if (writingSystems.count() <= 5 && system >= QFontDatabase::SimplifiedChinese && system <= QFontDatabase::Korean) + if (writingSystems.size() <= 5 && system >= QFontDatabase::SimplifiedChinese && system <= QFontDatabase::Korean) return system; return QFontDatabase::Any; @@ -181,16 +150,15 @@ static QFontDatabase::WritingSystem writingSystemForFont(const QFont &font, bool class QFontComboBoxPrivate : public QComboBoxPrivate { public: - inline QFontComboBoxPrivate() { filters = QFontComboBox::AllFonts; } - - QFontComboBox::FontFilters filters; + QFontComboBox::FontFilters filters = QFontComboBox::AllFonts; QFont currentFont; QHash<QFontDatabase::WritingSystem, QString> sampleTextForWritingSystem; QHash<QString, QString> sampleTextForFontFamily; QHash<QString, QFont> displayFontForFontFamily; + std::array<QMetaObject::Connection, 2> connections; - void _q_updateModel(); - void _q_currentChanged(const QString &); + void updateModel(); + void currentChanged(const QString &); Q_DECLARE_PUBLIC(QFontComboBox) }; @@ -287,7 +255,7 @@ void QFontFamilyDelegate::paint(QPainter *painter, const QString sampleText = comboPrivate->sampleTextForFontFamily.value(text, comboPrivate->sampleTextForWritingSystem.value(system)); if (system != QFontDatabase::Any || !sampleText.isEmpty()) { - int w = painter->fontMetrics().horizontalAdvance(text + QLatin1String(" ")); + int w = painter->fontMetrics().horizontalAdvance(text + " "_L1); painter->setFont(font2); const QString sample = !sampleText.isEmpty() ? sampleText : QFontDatabase::writingSystemSample(system); if (option.direction == Qt::RightToLeft) @@ -315,7 +283,7 @@ QSize QFontFamilyDelegate::sizeHint(const QStyleOptionViewItem &option, } -void QFontComboBoxPrivate::_q_updateModel() +void QFontComboBoxPrivate::updateModel() { Q_Q(QFontComboBox); @@ -331,29 +299,28 @@ void QFontComboBoxPrivate::_q_updateModel() QFontFamilyDelegate *delegate = qobject_cast<QFontFamilyDelegate *>(q->view()->itemDelegate()); QFontDatabase::WritingSystem system = delegate ? delegate->writingSystem : QFontDatabase::Any; - QStringList list = QFontDatabase::families(system); + const QStringList list = QFontDatabase::families(system); QStringList result; int offset = 0; QFontInfo fi(currentFont); - for (int i = 0; i < list.size(); ++i) { - if (QFontDatabase::isPrivateFamily(list.at(i))) + for (const auto &family : list) { + if (QFontDatabase::isPrivateFamily(family)) continue; if ((filters & scalableMask) && (filters & scalableMask) != scalableMask) { - if (bool(filters & QFontComboBox::ScalableFonts) != QFontDatabase::isSmoothlyScalable(list.at(i))) + if (bool(filters & QFontComboBox::ScalableFonts) != QFontDatabase::isSmoothlyScalable(family)) continue; } if ((filters & spacingMask) && (filters & spacingMask) != spacingMask) { - if (bool(filters & QFontComboBox::MonospacedFonts) != QFontDatabase::isFixedPitch(list.at(i))) + if (bool(filters & QFontComboBox::MonospacedFonts) != QFontDatabase::isFixedPitch(family)) continue; } - result += list.at(i); - if (list.at(i) == fi.family() || list.at(i).startsWith(fi.family() + QLatin1String(" ["))) - offset = result.count() - 1; + result += family; + if (family == fi.family() || family.startsWith(fi.family() + " ["_L1)) + offset = result.size() - 1; } - list = result; //we need to block the signals so that the model doesn't emit reset //this prevents the current index from changing @@ -361,10 +328,17 @@ void QFontComboBoxPrivate::_q_updateModel() ///TODO: we should finda way to avoid blocking signals and have a real update of the model { const QSignalBlocker blocker(m); - m->setStringList(list); + m->setStringList(result); + // Since the modelReset signal is blocked the view will not emit an accessibility event + #if QT_CONFIG(accessibility) + if (QAccessible::isActive()) { + QAccessibleTableModelChangeEvent accessibleEvent(q->view(), QAccessibleTableModelChangeEvent::ModelReset); + QAccessible::updateAccessibility(&accessibleEvent); + } + #endif } - if (list.isEmpty()) { + if (result.isEmpty()) { if (currentFont != QFont()) { currentFont = QFont(); emit q->currentFontChanged(currentFont); @@ -375,10 +349,10 @@ void QFontComboBoxPrivate::_q_updateModel() } -void QFontComboBoxPrivate::_q_currentChanged(const QString &text) +void QFontComboBoxPrivate::currentChanged(const QString &text) { Q_Q(QFontComboBox); - QStringList families = currentFont.families(); + const QStringList families = currentFont.families(); if (families.isEmpty() || families.first() != text) { currentFont.setFamilies(QStringList{text}); emit q->currentFontChanged(currentFont); @@ -415,7 +389,7 @@ void QFontComboBoxPrivate::_q_currentChanged(const QString &text) \image windowsvista-fontcombobox.png Screenshot of QFontComboBox on Windows Vista - \sa QComboBox, QFont, QFontInfo, QFontMetrics, QFontDatabase, {Character Map Example} + \sa QComboBox, QFont, QFontInfo, QFontMetrics, QFontDatabase */ /*! @@ -436,11 +410,12 @@ QFontComboBox::QFontComboBox(QWidget *parent) lview->setUniformItemSizes(true); setWritingSystem(QFontDatabase::Any); - connect(this, SIGNAL(currentTextChanged(QString)), - this, SLOT(_q_currentChanged(QString))); - - connect(qApp, SIGNAL(fontDatabaseChanged()), - this, SLOT(_q_updateModel())); + d->connections = { + QObjectPrivate::connect(this, &QFontComboBox::currentTextChanged, + d, &QFontComboBoxPrivate::currentChanged), + QObjectPrivate::connect(qApp, &QGuiApplication::fontDatabaseChanged, + d, &QFontComboBoxPrivate::updateModel), + }; } @@ -449,6 +424,9 @@ QFontComboBox::QFontComboBox(QWidget *parent) */ QFontComboBox::~QFontComboBox() { + Q_D(const QFontComboBox); + for (const QMetaObject::Connection &connection : d->connections) + QObject::disconnect(connection); } /*! @@ -467,7 +445,7 @@ void QFontComboBox::setWritingSystem(QFontDatabase::WritingSystem script) QFontFamilyDelegate *delegate = qobject_cast<QFontFamilyDelegate *>(view()->itemDelegate()); if (delegate) delegate->writingSystem = script; - d->_q_updateModel(); + d->updateModel(); } QFontDatabase::WritingSystem QFontComboBox::writingSystem() const @@ -503,7 +481,7 @@ void QFontComboBox::setFontFilters(FontFilters filters) { Q_D(QFontComboBox); d->filters = filters; - d->_q_updateModel(); + d->updateModel(); } QFontComboBox::FontFilters QFontComboBox::fontFilters() const @@ -529,8 +507,8 @@ void QFontComboBox::setCurrentFont(const QFont &font) Q_D(QFontComboBox); if (font != d->currentFont) { d->currentFont = font; - d->_q_updateModel(); - if (d->currentFont == font) { //else the signal has already be emitted by _q_updateModel + d->updateModel(); + if (d->currentFont == font) { //else the signal has already be emitted by updateModel emit currentFontChanged(d->currentFont); } } @@ -567,7 +545,7 @@ QSize QFontComboBox::sizeHint() const { QSize sz = QComboBox::sizeHint(); QFontMetrics fm(font()); - sz.setWidth(fm.horizontalAdvance(QLatin1Char('m'))*14); + sz.setWidth(fm.horizontalAdvance(u'm') * 14); return sz; } |