File indexing completed on 2024-04-28 03:59:06

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     const QList<int> sizes = QFontDatabase::standardSizes();
0074     QStringList lst;
0075     lst.reserve(sizes.count());
0076     for (QList<int>::ConstIterator it = sizes.begin(), total = sizes.end(); it != total; ++it) {
0077         lst.append(QString::number(*it));
0078     }
0079 
0080     q->setItems(lst);
0081 }
0082 
0083 void KFontSizeAction::setFontSize(int size)
0084 {
0085     if (size == fontSize()) {
0086         const QString test = QString::number(size);
0087         const auto actions = this->actions();
0088         for (QAction *action : actions) {
0089             if (action->text() == test) {
0090                 setCurrentAction(action);
0091                 return;
0092             }
0093         }
0094     }
0095 
0096     if (size < 1) {
0097         qCWarning(KWidgetsAddonsLog) << "KFontSizeAction: Size " << size << " is out of range";
0098         return;
0099     }
0100 
0101     QAction *a = action(QString::number(size));
0102     if (!a) {
0103         // Insert at the correct position in the list (to keep sorting)
0104         QList<int> lst;
0105         // Convert to list of ints
0106         QStringListIterator itemsIt(items());
0107         while (itemsIt.hasNext()) {
0108             lst.append(itemsIt.next().toInt());
0109         }
0110         // New size
0111         lst.append(size);
0112         // Sort the list
0113         std::sort(lst.begin(), lst.end());
0114         clear();
0115         for (int it : std::as_const(lst)) {
0116             QAction *const action = addAction(QString::number(it));
0117             if (it == size) {
0118                 setCurrentAction(action);
0119             }
0120         }
0121 
0122     } else {
0123         setCurrentAction(a);
0124     }
0125 }
0126 
0127 int KFontSizeAction::fontSize() const
0128 {
0129     return currentText().toInt();
0130 }
0131 
0132 void KFontSizeAction::slotActionTriggered(QAction *action)
0133 {
0134     Q_EMIT fontSizeChanged(action->text().toInt());
0135     KSelectAction::slotActionTriggered(action);
0136 }
0137 
0138 #include "moc_kfontsizeaction.cpp"