File indexing completed on 2024-12-22 04:16:40
0001 /* This file is part of the KDE libraries 0002 SPDX-FileCopyrightText: 1999 Reginald Stadlbauer <reggie@kde.org> 0003 SPDX-FileCopyrightText: 1999 Simon Hausmann <hausmann@kde.org> 0004 SPDX-FileCopyrightText: 2000 Nicolas Hadacek <haadcek@kde.org> 0005 SPDX-FileCopyrightText: 2000 Kurt Granroth <granroth@kde.org> 0006 SPDX-FileCopyrightText: 2000 Michael Koch <koch@kde.org> 0007 SPDX-FileCopyrightText: 2001 Holger Freyther <freyther@kde.org> 0008 SPDX-FileCopyrightText: 2002 Ellis Whitehead <ellis@kde.org> 0009 SPDX-FileCopyrightText: 2002 Joseph Wenninger <jowenn@kde.org> 0010 SPDX-FileCopyrightText: 2003 Andras Mantia <amantia@kde.org> 0011 SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <rodda@kde.org> 0012 0013 SPDX-License-Identifier: LGPL-2.0-only 0014 */ 0015 0016 #include <QFontDatabase> 0017 #include <QToolBar> 0018 #include <QToolButton> 0019 0020 #include <QDebug> 0021 #include <klocalizedstring.h> 0022 #include <QIcon> 0023 0024 #include "FontSizeAction.h" 0025 0026 QString format(qreal v) 0027 { 0028 static const QString f("%1"); 0029 static const QString e; 0030 static const QRegExp r("\\.?0+$"); 0031 return f.arg(v, 0, 'f').replace(r, e); 0032 } 0033 0034 class FontSizeAction::Private 0035 { 0036 public: 0037 Private(FontSizeAction *parent) 0038 : q(parent) 0039 { 0040 } 0041 0042 void init(); 0043 0044 FontSizeAction *q; 0045 }; 0046 0047 // BEGIN FontSizeAction 0048 FontSizeAction::FontSizeAction(QObject *parent) 0049 : KSelectAction(parent) 0050 , d(new Private(this)) 0051 { 0052 d->init(); 0053 } 0054 0055 FontSizeAction::FontSizeAction(const QString &text, QObject *parent) 0056 : KSelectAction(text, parent) 0057 , d(new Private(this)) 0058 { 0059 d->init(); 0060 } 0061 0062 FontSizeAction::FontSizeAction(const QIcon &icon, const QString &text, QObject *parent) 0063 : KSelectAction(icon, text, parent) 0064 , d(new Private(this)) 0065 { 0066 d->init(); 0067 } 0068 0069 FontSizeAction::~FontSizeAction() 0070 { 0071 delete d; 0072 } 0073 0074 void FontSizeAction::Private::init() 0075 { 0076 q->setEditable(true); 0077 QFontDatabase fontDB; 0078 const QList<int> sizes = fontDB.standardSizes(); 0079 QStringList lst; 0080 for (QList<int>::ConstIterator it = sizes.begin(); it != sizes.end(); ++it) { 0081 lst.append(format(*it)); 0082 } 0083 q->setItems(lst); 0084 } 0085 0086 void FontSizeAction::setFontSize(qreal size) 0087 { 0088 if (size == fontSize()) { 0089 const QString test = format(size); 0090 Q_FOREACH (QAction *action, actions()) { 0091 if (action->text() == test) { 0092 setCurrentAction(action); 0093 return; 0094 } 0095 } 0096 } 0097 0098 if (size < 1) { 0099 qWarning() << "FontSizeAction: Size " << size << " is out of range"; 0100 return; 0101 } 0102 0103 QAction *a = action(format(size)); 0104 if (!a) { 0105 // Insert at the correct position in the list (to keep sorting) 0106 QList<qreal> lst; 0107 // Convert to list of qreals 0108 QStringListIterator itemsIt(items()); 0109 QStringList debug_lst = items(); 0110 0111 while (itemsIt.hasNext()) { 0112 lst.append(itemsIt.next().toDouble()); 0113 } 0114 //add the new size 0115 lst.append(size); 0116 0117 //remove actions 0118 clear(); 0119 0120 // Sort the list 0121 std::sort(lst.begin(), lst.end()); 0122 Q_FOREACH (qreal it, lst) { 0123 QAction *const action = addAction(format(it)); 0124 if (it == size) { 0125 setCurrentAction(action); 0126 } 0127 } 0128 0129 } else { 0130 setCurrentAction(a); 0131 } 0132 } 0133 0134 qreal FontSizeAction::fontSize() const 0135 { 0136 return currentText().toDouble(); 0137 } 0138 0139 void FontSizeAction::actionTriggered(QAction *action) 0140 { 0141 emit fontSizeChanged(action->text().toDouble()); 0142 KSelectAction::actionTriggered(action); 0143 }