File indexing completed on 2024-04-28 15:32:04

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1999 Reginald Stadlbauer <reggie@kde.org>
0004     SPDX-FileCopyrightText: 1999 Simon Hausmann <hausmann@kde.org>
0005     SPDX-FileCopyrightText: 2000 Nicolas Hadacek <haadcek@kde.org>
0006     SPDX-FileCopyrightText: 2000 Kurt Granroth <granroth@kde.org>
0007     SPDX-FileCopyrightText: 2000 Michael Koch <koch@kde.org>
0008     SPDX-FileCopyrightText: 2001 Holger Freyther <freyther@kde.org>
0009     SPDX-FileCopyrightText: 2002 Ellis Whitehead <ellis@kde.org>
0010     SPDX-FileCopyrightText: 2002 Joseph Wenninger <jowenn@kde.org>
0011     SPDX-FileCopyrightText: 2003 Andras Mantia <amantia@kde.org>
0012     SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <rodda@kde.org>
0013 
0014     SPDX-License-Identifier: LGPL-2.0-only
0015 */
0016 
0017 #include "kfontsizeaction.h"
0018 
0019 #include "kselectaction_p.h"
0020 
0021 #include "loggingcategory.h"
0022 
0023 #include <QFontDatabase>
0024 
0025 class KFontSizeActionPrivate : public KSelectActionPrivate
0026 {
0027     Q_DECLARE_PUBLIC(KFontSizeAction)
0028 
0029 public:
0030     KFontSizeActionPrivate(KFontSizeAction *qq)
0031         : KSelectActionPrivate(qq)
0032     {
0033     }
0034 
0035     void init();
0036 };
0037 
0038 // BEGIN KFontSizeAction
0039 KFontSizeAction::KFontSizeAction(QObject *parent)
0040     : KSelectAction(*new KFontSizeActionPrivate(this), parent)
0041 {
0042     Q_D(KFontSizeAction);
0043 
0044     d->init();
0045 }
0046 
0047 KFontSizeAction::KFontSizeAction(const QString &text, QObject *parent)
0048     : KSelectAction(*new KFontSizeActionPrivate(this), parent)
0049 {
0050     Q_D(KFontSizeAction);
0051 
0052     setText(text);
0053     d->init();
0054 }
0055 
0056 KFontSizeAction::KFontSizeAction(const QIcon &icon, const QString &text, QObject *parent)
0057     : KSelectAction(*new KFontSizeActionPrivate(this), parent)
0058 {
0059     Q_D(KFontSizeAction);
0060 
0061     setIcon(icon);
0062     setText(text);
0063     d->init();
0064 }
0065 
0066 KFontSizeAction::~KFontSizeAction() = default;
0067 
0068 void KFontSizeActionPrivate::init()
0069 {
0070     Q_Q(KFontSizeAction);
0071 
0072     q->setEditable(true);
0073     QFontDatabase fontDB;
0074     const QList<int> sizes = fontDB.standardSizes();
0075     QStringList lst;
0076     lst.reserve(sizes.count());
0077     for (QList<int>::ConstIterator it = sizes.begin(), total = sizes.end(); it != total; ++it) {
0078         lst.append(QString::number(*it));
0079     }
0080 
0081     q->setItems(lst);
0082 }
0083 
0084 void KFontSizeAction::setFontSize(int size)
0085 {
0086     if (size == fontSize()) {
0087         const QString test = QString::number(size);
0088         const auto actions = this->actions();
0089         for (QAction *action : actions) {
0090             if (action->text() == test) {
0091                 setCurrentAction(action);
0092                 return;
0093             }
0094         }
0095     }
0096 
0097     if (size < 1) {
0098         qCWarning(KWidgetsAddonsLog) << "KFontSizeAction: Size " << size << " is out of range";
0099         return;
0100     }
0101 
0102     QAction *a = action(QString::number(size));
0103     if (!a) {
0104         // Insert at the correct position in the list (to keep sorting)
0105         QList<int> lst;
0106         // Convert to list of ints
0107         QStringListIterator itemsIt(items());
0108         while (itemsIt.hasNext()) {
0109             lst.append(itemsIt.next().toInt());
0110         }
0111         // New size
0112         lst.append(size);
0113         // Sort the list
0114         std::sort(lst.begin(), lst.end());
0115         clear();
0116         for (int it : std::as_const(lst)) {
0117             QAction *const action = addAction(QString::number(it));
0118             if (it == size) {
0119                 setCurrentAction(action);
0120             }
0121         }
0122 
0123     } else {
0124         setCurrentAction(a);
0125     }
0126 }
0127 
0128 int KFontSizeAction::fontSize() const
0129 {
0130     return currentText().toInt();
0131 }
0132 
0133 void KFontSizeAction::actionTriggered(QAction *action)
0134 {
0135     Q_EMIT fontSizeChanged(action->text().toInt());
0136     KSelectAction::actionTriggered(action);
0137 }
0138 
0139 #include "moc_kfontsizeaction.cpp"