1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 1999 Reginald Stadlbauer <reggie@kde.org>
4 SPDX-FileCopyrightText: 1999 Simon Hausmann <hausmann@kde.org>
5 SPDX-FileCopyrightText: 2000 Nicolas Hadacek <haadcek@kde.org>
6 SPDX-FileCopyrightText: 2000 Kurt Granroth <granroth@kde.org>
7 SPDX-FileCopyrightText: 2000 Michael Koch <koch@kde.org>
8 SPDX-FileCopyrightText: 2001 Holger Freyther <freyther@kde.org>
9 SPDX-FileCopyrightText: 2002 Ellis Whitehead <ellis@kde.org>
10 SPDX-FileCopyrightText: 2002 Joseph Wenninger <jowenn@kde.org>
11 SPDX-FileCopyrightText: 2003 Andras Mantia <amantia@kde.org>
12 SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <rodda@kde.org>
13
14 SPDX-License-Identifier: LGPL-2.0-only
15*/
16
17#include "kfontsizeaction.h"
18
19#include "kselectaction_p.h"
20
21#include "loggingcategory.h"
22
23#include <QFontDatabase>
24
25class KFontSizeActionPrivate : public KSelectActionPrivate
26{
27 Q_DECLARE_PUBLIC(KFontSizeAction)
28
29public:
30 KFontSizeActionPrivate(KFontSizeAction *qq)
31 : KSelectActionPrivate(qq)
32 {
33 }
34
35 void init();
36};
37
38// BEGIN KFontSizeAction
39KFontSizeAction::KFontSizeAction(QObject *parent)
40 : KSelectAction(*new KFontSizeActionPrivate(this), parent)
41{
42 Q_D(KFontSizeAction);
43
44 d->init();
45}
46
47KFontSizeAction::KFontSizeAction(const QString &text, QObject *parent)
48 : KSelectAction(*new KFontSizeActionPrivate(this), parent)
49{
50 Q_D(KFontSizeAction);
51
52 setText(text);
53 d->init();
54}
55
56KFontSizeAction::KFontSizeAction(const QIcon &icon, const QString &text, QObject *parent)
57 : KSelectAction(*new KFontSizeActionPrivate(this), parent)
58{
59 Q_D(KFontSizeAction);
60
61 setIcon(icon);
62 setText(text);
63 d->init();
64}
65
66KFontSizeAction::~KFontSizeAction() = default;
67
68void KFontSizeActionPrivate::init()
69{
70 Q_Q(KFontSizeAction);
71
72 q->setEditable(true);
73 const QList<int> sizes = QFontDatabase::standardSizes();
74 QStringList lst;
75 lst.reserve(asize: sizes.count());
76 for (QList<int>::ConstIterator it = sizes.begin(), total = sizes.end(); it != total; ++it) {
77 lst.append(t: QString::number(*it));
78 }
79
80 q->setItems(lst);
81}
82
83void KFontSizeAction::setFontSize(int size)
84{
85 if (size == fontSize()) {
86 const QString test = QString::number(size);
87 const auto actions = this->actions();
88 for (QAction *action : actions) {
89 if (action->text() == test) {
90 setCurrentAction(action);
91 return;
92 }
93 }
94 }
95
96 if (size < 1) {
97 qCWarning(KWidgetsAddonsLog) << "KFontSizeAction: Size " << size << " is out of range";
98 return;
99 }
100
101 QAction *a = action(text: QString::number(size));
102 if (!a) {
103 // Insert at the correct position in the list (to keep sorting)
104 QList<int> lst;
105 // Convert to list of ints
106 QStringListIterator itemsIt(items());
107 while (itemsIt.hasNext()) {
108 lst.append(t: itemsIt.next().toInt());
109 }
110 // New size
111 lst.append(t: size);
112 // Sort the list
113 std::sort(first: lst.begin(), last: lst.end());
114 clear();
115 for (int it : std::as_const(t&: lst)) {
116 QAction *const action = addAction(text: QString::number(it));
117 if (it == size) {
118 setCurrentAction(action);
119 }
120 }
121
122 } else {
123 setCurrentAction(a);
124 }
125}
126
127int KFontSizeAction::fontSize() const
128{
129 return currentText().toInt();
130}
131
132void KFontSizeAction::slotActionTriggered(QAction *action)
133{
134 Q_EMIT fontSizeChanged(action->text().toInt());
135 KSelectAction::slotActionTriggered(action);
136}
137
138#include "moc_kfontsizeaction.cpp"
139

source code of kwidgetsaddons/src/kfontsizeaction.cpp