KWidgetsAddons

kfontsizeaction.cpp
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{
43
44 d->init();
45}
46
47KFontSizeAction::KFontSizeAction(const QString &text, QObject *parent)
48 : KSelectAction(*new KFontSizeActionPrivate(this), parent)
49{
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{
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);
74 QStringList lst;
75 lst.reserve(sizes.count());
76 for (QList<int>::ConstIterator it = sizes.begin(), total = sizes.end(); it != total; ++it) {
77 lst.append(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) {
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(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(itemsIt.next().toInt());
109 }
110 // New size
111 lst.append(size);
112 // Sort the list
113 std::sort(lst.begin(), lst.end());
114 clear();
115 for (int it : std::as_const(lst)) {
117 if (it == size) {
119 }
120 }
121
122 } else {
124 }
125}
126
127int KFontSizeAction::fontSize() const
128{
129 return currentText().toInt();
130}
131
133{
134 Q_EMIT fontSizeChanged(action->text().toInt());
136}
137
138#include "moc_kfontsizeaction.cpp"
An action to allow changing of the font size.
void slotActionTriggered(QAction *action) override
This function is called whenever an action from the selections is triggered.
Action for selecting one of several items.
bool setCurrentAction(QAction *action)
Sets the currently checked item.
virtual void slotActionTriggered(QAction *action)
This function is called whenever an action from the selections is triggered.
void clear()
Remove and delete all the items in this action.
QList< QAction * > actions() const
Returns the list of selectable actions.
void addAction(QAction *action)
Add action to the list of selectable actions.
QAction * action(int index) const
Returns the action at index, if one exists.
KGuiItem test()
Returns the 'Test' gui item.
QList< int > standardSizes()
void append(QList< T > &&value)
iterator begin()
qsizetype count() const const
iterator end()
void reserve(qsizetype size)
Q_EMITQ_EMIT
QString number(double n, char format, int precision)
int toInt(bool *ok, int base) const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:27 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.