KWidgetsAddons

kfontsizeaction.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 1999 Reginald Stadlbauer <[email protected]>
3  (C) 1999 Simon Hausmann <[email protected]>
4  (C) 2000 Nicolas Hadacek <[email protected]>
5  (C) 2000 Kurt Granroth <[email protected]>
6  (C) 2000 Michael Koch <[email protected]>
7  (C) 2001 Holger Freyther <[email protected]>
8  (C) 2002 Ellis Whitehead <[email protected]>
9  (C) 2002 Joseph Wenninger <[email protected]>
10  (C) 2003 Andras Mantia <[email protected]>
11  (C) 2005-2006 Hamish Rodda <[email protected]>
12 
13  This library is free software; you can redistribute it and/or
14  modify it under the terms of the GNU Library General Public
15  License version 2 as published by the Free Software Foundation.
16 
17  This library is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  Library General Public License for more details.
21 
22  You should have received a copy of the GNU Library General Public License
23  along with this library; see the file COPYING.LIB. If not, write to
24  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25  Boston, MA 02110-1301, USA.
26 */
27 
28 #include "kfontsizeaction.h"
29 
30 #include "loggingcategory.h"
31 
32 #include <QFontDatabase>
33 
34 class Q_DECL_HIDDEN KFontSizeAction::Private
35 {
36 public:
37  Private(KFontSizeAction *parent)
38  : q(parent)
39  {
40  }
41 
42  void init();
43 
44  KFontSizeAction *q;
45 };
46 
47 // BEGIN KFontSizeAction
48 KFontSizeAction::KFontSizeAction(QObject *parent)
49  : KSelectAction(parent),
50  d(new Private(this))
51 {
52  d->init();
53 }
54 
55 KFontSizeAction::KFontSizeAction(const QString &text, QObject *parent)
56  : KSelectAction(text, parent),
57  d(new Private(this))
58 {
59  d->init();
60 }
61 
62 KFontSizeAction::KFontSizeAction(const QIcon &icon, const QString &text, QObject *parent)
63  : KSelectAction(icon, text, parent),
64  d(new Private(this))
65 {
66  d->init();
67 }
68 
69 KFontSizeAction::~KFontSizeAction()
70 {
71  delete d;
72 }
73 
74 void KFontSizeAction::Private::init()
75 {
76  q->setEditable(true);
77  QFontDatabase fontDB;
78  const QList<int> sizes = fontDB.standardSizes();
79  QStringList lst;
80  lst.reserve(sizes.count());
81  for (QList<int>::ConstIterator it = sizes.begin(), total = sizes.end(); it != total; ++it) {
82  lst.append(QString::number(*it));
83  }
84 
85  q->setItems(lst);
86 }
87 
88 void KFontSizeAction::setFontSize(int size)
89 {
90  if (size == fontSize()) {
91  const QString test = QString::number(size);
92  const auto actions = this->actions();
93  for (QAction *action : actions) {
94  if (action->text() == test) {
95  setCurrentAction(action);
96  return;
97  }
98  }
99  }
100 
101  if (size < 1) {
102  qCWarning(KWidgetsAddonsLog) << "KFontSizeAction: Size " << size << " is out of range";
103  return;
104  }
105 
106  QAction *a = action(QString::number(size));
107  if (!a) {
108  // Insert at the correct position in the list (to keep sorting)
109  QList<int> lst;
110  // Convert to list of ints
111  QStringListIterator itemsIt(items());
112  while (itemsIt.hasNext()) {
113  lst.append(itemsIt.next().toInt());
114  }
115  // New size
116  lst.append(size);
117  // Sort the list
118  std::sort(lst.begin(), lst.end());
119  clear();
120  for (int it : qAsConst(lst)) {
121  QAction *const action = addAction(QString::number(it));
122  if (it == size) {
123  setCurrentAction(action);
124  }
125  }
126 
127  } else {
128  setCurrentAction(a);
129  }
130 }
131 
132 int KFontSizeAction::fontSize() const
133 {
134  return currentText().toInt();
135 }
136 
138 {
139  emit fontSizeChanged(action->text().toInt());
141 }
142 
QList< int > standardSizes()
An action to allow changing of the font size.
void addAction(QAction *action)
void reserve(int alloc)
QAction * clear(const QObject *recvr, const char *slot, QObject *parent)
QUICKADDONS_EXPORT void init()
Action for selecting one of several items.
Definition: kselectaction.h:54
QSize size() const const
KGuiItem test()
Returns the &#39;Test&#39; gui item.
QString number(int n, int base)
int count(const T &value) const const
void append(const T &value)
QList::iterator end()
void actionTriggered(QAction *action) override
This function is called whenever an action from the selections is triggered.
virtual void actionTriggered(QAction *action)
This function is called whenever an action from the selections is triggered.
QList< QAction * > actions() const const
QList::iterator begin()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun May 24 2020 22:50:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.