File indexing completed on 2024-05-12 16:34:57

0001 /* This file is part of the KDE libraries
0002     Copyright (C) 1999 Reginald Stadlbauer <reggie@kde.org>
0003               (C) 1999 Simon Hausmann <hausmann@kde.org>
0004               (C) 2000 Nicolas Hadacek <haadcek@kde.org>
0005               (C) 2000 Kurt Granroth <granroth@kde.org>
0006               (C) 2000 Michael Koch <koch@kde.org>
0007               (C) 2001 Holger Freyther <freyther@kde.org>
0008               (C) 2002 Ellis Whitehead <ellis@kde.org>
0009               (C) 2002 Joseph Wenninger <jowenn@kde.org>
0010               (C) 2003 Andras Mantia <amantia@kde.org>
0011               (C) 2005-2006 Hamish Rodda <rodda@kde.org>
0012 
0013     This library is free software; you can redistribute it and/or
0014     modify it under the terms of the GNU Library General Public
0015     License version 2 as published by the Free Software Foundation.
0016 
0017     This library is distributed in the hope that it will be useful,
0018     but WITHOUT ANY WARRANTY; without even the implied warranty of
0019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0020     Library General Public License for more details.
0021 
0022     You should have received a copy of the GNU Library General Public License
0023     along with this library; see the file COPYING.LIB.  If not, write to
0024     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0025     Boston, MA 02110-1301, USA.
0026 */
0027 
0028 #include <QFontDatabase>
0029 #include <QToolBar>
0030 #include <QToolButton>
0031 
0032 #include <QDebug>
0033 #include <klocalizedstring.h>
0034 #include <QIcon>
0035 
0036 #include "FontSizeAction.h"
0037 
0038 #include <algorithm>
0039 
0040 QString format(qreal v) {
0041     static const QString f("%1");
0042     static const QString e("");
0043     static const QRegExp r("\\.?0+$");
0044     return f.arg(v, 0, 'f').replace(r, e);
0045 }
0046 
0047 class FontSizeAction::Private
0048 {
0049     public:
0050         Private(FontSizeAction *parent)
0051             : q(parent)
0052         {
0053         }
0054 
0055         void init();
0056 
0057         FontSizeAction *q;
0058 };
0059 
0060 // BEGIN FontSizeAction
0061 FontSizeAction::FontSizeAction(QObject *parent)
0062   : KSelectAction(parent),
0063     d(new Private(this))
0064 {
0065   d->init();
0066 }
0067 
0068 FontSizeAction::FontSizeAction(const QString &text, QObject *parent)
0069   : KSelectAction(text, parent),
0070     d(new Private(this))
0071 {
0072   d->init();
0073 }
0074 
0075 FontSizeAction::FontSizeAction(const QIcon &icon, const QString &text, QObject *parent)
0076   : KSelectAction(icon, text, parent),
0077     d(new Private(this))
0078 {
0079   d->init();
0080 }
0081 
0082 FontSizeAction::~FontSizeAction()
0083 {
0084   delete d;
0085 }
0086 
0087 void FontSizeAction::Private::init()
0088 {
0089     q->setEditable( true );
0090     QFontDatabase fontDB;
0091     const QList<int> sizes = fontDB.standardSizes();
0092     QStringList lst;
0093     for ( QList<int>::ConstIterator it = sizes.begin(); it != sizes.end(); ++it ) {
0094         lst.append( format(*it) );
0095     }
0096     q->setItems( lst );
0097 }
0098 
0099 void FontSizeAction::setFontSize( qreal size )
0100 {
0101     if ( size == fontSize() ) {
0102         const QString test = format(size);
0103         Q_FOREACH(QAction* action, actions()) {
0104           if (action->text() == test) {
0105               setCurrentAction(action);
0106               return;
0107           }
0108         }
0109     }
0110 
0111     if ( size < 1 ) {
0112         qWarning() << "FontSizeAction: Size " << size << " is out of range";
0113         return;
0114     }
0115 
0116     QAction* a = action( format(size) );
0117     if ( !a ) {
0118         // Insert at the correct position in the list (to keep sorting)
0119         QList<qreal> lst;
0120         // Convert to list of qreals
0121         QStringListIterator itemsIt( items() );
0122     QStringList debug_lst = items();
0123 
0124         while ( itemsIt.hasNext() ) {
0125             lst.append( itemsIt.next().toDouble() );
0126         }
0127         //add the new size
0128         lst.append( size );
0129 
0130         //remove actions
0131         clear();
0132 
0133         // Sort the list
0134         std::sort( lst.begin(), lst.end() );
0135         Q_FOREACH( qreal it, lst ) {
0136             QAction* const action = addAction( format(it) );
0137             if (it == size) {
0138                 setCurrentAction(action);
0139             }
0140         }
0141 
0142     } else {
0143         setCurrentAction( a );
0144     }
0145 }
0146 
0147 qreal FontSizeAction::fontSize() const
0148 {
0149   return currentText().toDouble();
0150 }
0151 
0152 void FontSizeAction::actionTriggered( QAction* action )
0153 {
0154     emit fontSizeChanged( action->text().toDouble() );
0155     KSelectAction::actionTriggered( action );
0156 }