KWidgetsAddons

kfontsizeaction.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 1999 Reginald Stadlbauer <[email protected]>
4  SPDX-FileCopyrightText: 1999 Simon Hausmann <[email protected]>
5  SPDX-FileCopyrightText: 2000 Nicolas Hadacek <[email protected]>
6  SPDX-FileCopyrightText: 2000 Kurt Granroth <[email protected]>
7  SPDX-FileCopyrightText: 2000 Michael Koch <[email protected]>
8  SPDX-FileCopyrightText: 2001 Holger Freyther <[email protected]>
9  SPDX-FileCopyrightText: 2002 Ellis Whitehead <[email protected]>
10  SPDX-FileCopyrightText: 2002 Joseph Wenninger <[email protected]>
11  SPDX-FileCopyrightText: 2003 Andras Mantia <[email protected]>
12  SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <[email protected]>
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 
25 class KFontSizeActionPrivate : public KSelectActionPrivate
26 {
27  Q_DECLARE_PUBLIC(KFontSizeAction)
28 
29 public:
30  KFontSizeActionPrivate(KFontSizeAction *qq)
31  : KSelectActionPrivate(qq)
32  {
33  }
34 
35  void init();
36 };
37 
38 // BEGIN KFontSizeAction
39 KFontSizeAction::KFontSizeAction(QObject *parent)
40  : KSelectAction(*new KFontSizeActionPrivate(this), parent)
41 {
43 
44  d->init();
45 }
46 
47 KFontSizeAction::KFontSizeAction(const QString &text, QObject *parent)
48  : KSelectAction(*new KFontSizeActionPrivate(this), parent)
49 {
51 
52  setText(text);
53  d->init();
54 }
55 
56 KFontSizeAction::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 
66 KFontSizeAction::~KFontSizeAction() = default;
67 
68 void KFontSizeActionPrivate::init()
69 {
70  Q_Q(KFontSizeAction);
71 
72  q->setEditable(true);
73  QFontDatabase fontDB;
74  const QList<int> sizes = fontDB.standardSizes();
75  QStringList lst;
76  lst.reserve(sizes.count());
77  for (QList<int>::ConstIterator it = sizes.begin(), total = sizes.end(); it != total; ++it) {
78  lst.append(QString::number(*it));
79  }
80 
81  q->setItems(lst);
82 }
83 
84 void KFontSizeAction::setFontSize(int size)
85 {
86  if (size == fontSize()) {
87  const QString test = QString::number(size);
88  const auto actions = this->actions();
89  for (QAction *action : actions) {
90  if (action->text() == test) {
92  return;
93  }
94  }
95  }
96 
97  if (size < 1) {
98  qCWarning(KWidgetsAddonsLog) << "KFontSizeAction: Size " << size << " is out of range";
99  return;
100  }
101 
102  QAction *a = action(QString::number(size));
103  if (!a) {
104  // Insert at the correct position in the list (to keep sorting)
105  QList<int> lst;
106  // Convert to list of ints
107  QStringListIterator itemsIt(items());
108  while (itemsIt.hasNext()) {
109  lst.append(itemsIt.next().toInt());
110  }
111  // New size
112  lst.append(size);
113  // Sort the list
114  std::sort(lst.begin(), lst.end());
115  clear();
116  for (int it : std::as_const(lst)) {
117  QAction *const action = addAction(QString::number(it));
118  if (it == size) {
120  }
121  }
122 
123  } else {
124  setCurrentAction(a);
125  }
126 }
127 
128 int KFontSizeAction::fontSize() const
129 {
130  return currentText().toInt();
131 }
132 
134 {
135  Q_EMIT fontSizeChanged(action->text().toInt());
137 }
138 
139 #include "moc_kfontsizeaction.cpp"
void append(const T &value)
QString number(int n, int base)
Q_EMITQ_EMIT
virtual void actionTriggered(QAction *action)
This function is called whenever an action from the selections is triggered.
void actionTriggered(QAction *action) override
This function is called whenever an action from the selections is triggered.
int count(const T &value) const const
QAction * action(int index) const
Returns the action at index, if one exists.
QList< int > standardSizes()
QList< QAction * > actions() const
Returns the list of selectable actions.
void reserve(int alloc)
KGuiItem test()
Returns the 'Test' gui item.
void clear()
Remove and delete all the items in this action.
int toInt(bool *ok, int base) const const
void init(KXmlGuiWindow *window, KgDifficulty *difficulty=nullptr)
void addAction(QAction *action)
Add action to the list of selectable actions.
bool setCurrentAction(QAction *action)
Sets the currently checked item.
QList::iterator begin()
Action for selecting one of several items.
Definition: kselectaction.h:44
QList::iterator end()
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Sep 21 2023 04:03:40 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.