diff options
author | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2023-11-01 13:36:04 +0100 |
---|---|---|
committer | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2023-11-05 21:16:31 +0100 |
commit | c96ef6aee69e754b621d99d44b48e9ad1f5baf51 (patch) | |
tree | 88a09393ac7e616f329e8fcf807c80ead1f18a6b | |
parent | 737ed178033e6dd3358fbcc87db300ab21d65b32 (diff) |
QFontCombobox: misc cleanup
Cleanup QFontCombobox:
- use pmf-style connect
- use range-based for loop
Change-Id: I9ec87fbca2b0e6d52f0a4cbae07cc0624dc0f6bc
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
-rw-r--r-- | src/widgets/widgets/qfontcombobox.cpp | 56 | ||||
-rw-r--r-- | src/widgets/widgets/qfontcombobox.h | 2 |
2 files changed, 30 insertions, 28 deletions
diff --git a/src/widgets/widgets/qfontcombobox.cpp b/src/widgets/widgets/qfontcombobox.cpp index 0083a5ecea..c80687c7ed 100644 --- a/src/widgets/widgets/qfontcombobox.cpp +++ b/src/widgets/widgets/qfontcombobox.cpp @@ -13,6 +13,8 @@ #include <private/qcombobox_p.h> #include <qdebug.h> +#include <array> + QT_BEGIN_NAMESPACE using namespace Qt::StringLiterals; @@ -148,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) }; @@ -282,7 +283,7 @@ QSize QFontFamilyDelegate::sizeHint(const QStyleOptionViewItem &option, } -void QFontComboBoxPrivate::_q_updateModel() +void QFontComboBoxPrivate::updateModel() { Q_Q(QFontComboBox); @@ -298,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() + " ["_L1)) + 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 @@ -328,7 +328,7 @@ 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()) { @@ -338,7 +338,7 @@ void QFontComboBoxPrivate::_q_updateModel() #endif } - if (list.isEmpty()) { + if (result.isEmpty()) { if (currentFont != QFont()) { currentFont = QFont(); emit q->currentFontChanged(currentFont); @@ -349,7 +349,7 @@ void QFontComboBoxPrivate::_q_updateModel() } -void QFontComboBoxPrivate::_q_currentChanged(const QString &text) +void QFontComboBoxPrivate::currentChanged(const QString &text) { Q_Q(QFontComboBox); const QStringList families = currentFont.families(); @@ -410,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), + }; } @@ -423,6 +424,9 @@ QFontComboBox::QFontComboBox(QWidget *parent) */ QFontComboBox::~QFontComboBox() { + Q_D(const QFontComboBox); + for (const QMetaObject::Connection &connection : d->connections) + QObject::disconnect(connection); } /*! @@ -441,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 @@ -477,7 +481,7 @@ void QFontComboBox::setFontFilters(FontFilters filters) { Q_D(QFontComboBox); d->filters = filters; - d->_q_updateModel(); + d->updateModel(); } QFontComboBox::FontFilters QFontComboBox::fontFilters() const @@ -503,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); } } diff --git a/src/widgets/widgets/qfontcombobox.h b/src/widgets/widgets/qfontcombobox.h index 8c4bad2ae0..9c5432b332 100644 --- a/src/widgets/widgets/qfontcombobox.h +++ b/src/widgets/widgets/qfontcombobox.h @@ -65,8 +65,6 @@ protected: private: Q_DISABLE_COPY(QFontComboBox) Q_DECLARE_PRIVATE(QFontComboBox) - Q_PRIVATE_SLOT(d_func(), void _q_currentChanged(const QString &)) - Q_PRIVATE_SLOT(d_func(), void _q_updateModel()) }; Q_DECLARE_OPERATORS_FOR_FLAGS(QFontComboBox::FontFilters) |