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               (C) 2007 Clarence Dang <dang@kde.org>
0013               (C) 2014 Dan Leinir Turthra Jensen <admin@leinir.dk>
0014 
0015     This library is free software; you can redistribute it and/or
0016     modify it under the terms of the GNU Library General Public
0017     License version 2 as published by the Free Software Foundation.
0018 
0019     This library is distributed in the hope that it will be useful,
0020     but WITHOUT ANY WARRANTY; without even the implied warranty of
0021     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0022     Library General Public License for more details.
0023 
0024     You should have received a copy of the GNU Library General Public License
0025     along with this library; see the file COPYING.LIB.  If not, write to
0026     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0027     Boston, MA 02110-1301, USA.
0028 */
0029 
0030 // This is a minorly modified version of the KFontAction class. It exists
0031 // entirely because there's a hang bug on windows at the moment.
0032 
0033 #include "FontFamilyAction.h"
0034 
0035 #include "TextShapeDebug.h"
0036 
0037 #include <QToolBar>
0038 
0039 #include <QIcon>
0040 #include <klocalizedstring.h>
0041 #include <kfontchooser.h>
0042 #include <KoFontComboBox.h>
0043 
0044 class KoFontFamilyAction::KoFontFamilyActionPrivate
0045 {
0046     public:
0047         KoFontFamilyActionPrivate(KoFontFamilyAction *parent)
0048             : q(parent),
0049               settingFont(0)
0050         {
0051         }
0052 
0053         void _ko_slotFontChanged(const QFont &font)
0054         {
0055             debugTextShape << "KoFontComboBox - slotFontChanged("
0056                         << font.family() << ") settingFont=" << settingFont;
0057             if (settingFont)
0058                 return;
0059 
0060             q->setFont(font.family());
0061             q->triggered(font.family());
0062 
0063             debugTextShape << "\tslotFontChanged done";
0064         }
0065 
0066 
0067         KoFontFamilyAction *q;
0068         int settingFont;
0069 };
0070 
0071 KoFontFamilyAction::KoFontFamilyAction(uint fontListCriteria, QObject *parent)
0072   : KSelectAction(parent), d(new KoFontFamilyActionPrivate(this))
0073 {
0074     QStringList list;
0075     KFontChooser::getFontList( list, fontListCriteria );
0076     KSelectAction::setItems( list );
0077     setEditable( true );
0078 }
0079 
0080 KoFontFamilyAction::KoFontFamilyAction(QObject *parent)
0081   : KSelectAction(parent), d(new KoFontFamilyActionPrivate(this))
0082 {
0083     QStringList list;
0084     KFontChooser::getFontList( list, 0 );
0085     KSelectAction::setItems( list );
0086     setEditable( true );
0087 }
0088 
0089 KoFontFamilyAction::KoFontFamilyAction(const QString & text, QObject *parent)
0090   : KSelectAction(text, parent), d(new KoFontFamilyActionPrivate(this))
0091 {
0092     QStringList list;
0093     KFontChooser::getFontList( list, 0 );
0094     KSelectAction::setItems( list );
0095     setEditable( true );
0096 }
0097 
0098 KoFontFamilyAction::KoFontFamilyAction(const QIcon &icon, const QString &text, QObject *parent)
0099   : KSelectAction(icon, text, parent), d(new KoFontFamilyActionPrivate(this))
0100 {
0101     QStringList list;
0102     KFontChooser::getFontList( list, 0 );
0103     KSelectAction::setItems( list );
0104     setEditable( true );
0105 }
0106 
0107 KoFontFamilyAction::~KoFontFamilyAction()
0108 {
0109     delete d;
0110 }
0111 
0112 QString KoFontFamilyAction::font() const
0113 {
0114     return currentText();
0115 }
0116 
0117 QWidget* KoFontFamilyAction::createWidget(QWidget* parent)
0118 {
0119     debugTextShape << "KoFontFamilyAction::createWidget()";
0120 // silence unclear warning from original kfontaction.acpp
0121 // #ifdef __GNUC__
0122 // #warning FIXME: items need to be converted
0123 // #endif
0124     // This is the visual element on the screen.  This method overrides
0125     // the KSelectAction one, preventing KSelectAction from creating its
0126     // regular KComboBox.
0127     KoFontComboBox *cb = new KoFontComboBox( parent );
0128     //cb->setFontList(items());
0129 
0130     debugTextShape << "\tset=" << font();
0131     // Do this before connecting the signal so that nothing will fire.
0132     cb->setCurrentFont( QFont( font().toLower() ) );
0133     debugTextShape << "\tspit back=" << cb->currentFont().family();
0134 
0135     connect( cb, SIGNAL(currentFontChanged(QFont)), SLOT(_ko_slotFontChanged(QFont)) );
0136     cb->setMinimumWidth( cb->sizeHint().width() );
0137     return cb;
0138 }
0139 
0140 /*
0141  * Maintenance note: Keep in sync with KFontComboBox::setCurrentFont()
0142  */
0143 void KoFontFamilyAction::setFont( const QString &family )
0144 {
0145     debugTextShape << "KoFontFamilyAction::setFont(" << family << ")";
0146 
0147     // Suppress triggered(QString) signal and prevent recursive call to ourself.
0148     d->settingFont++;
0149 
0150     foreach(QWidget *w, createdWidgets())
0151     {
0152         KoFontComboBox *cb = qobject_cast<KoFontComboBox *>(w);
0153         debugTextShape << "\tw=" << w << "cb=" << cb;
0154 
0155         if(!cb) continue;
0156 
0157         cb->setCurrentFont(QFont(family.toLower()));
0158         debugTextShape << "\t\tw spit back=" << cb->currentFont().family();
0159     }
0160 
0161     d->settingFont--;
0162 
0163     debugTextShape << "\tcalling setCurrentAction()";
0164 
0165     QString lowerName = family.toLower();
0166     if (setCurrentAction(lowerName, Qt::CaseInsensitive))
0167        return;
0168 
0169     int i = lowerName.indexOf(" [");
0170     if (i > -1)
0171     {
0172        lowerName = lowerName.left(i);
0173        i = 0;
0174        if (setCurrentAction(lowerName, Qt::CaseInsensitive))
0175           return;
0176     }
0177 
0178     lowerName += " [";
0179     if (setCurrentAction(lowerName, Qt::CaseInsensitive))
0180       return;
0181 
0182     // TODO: Inconsistent state if KFontComboBox::setCurrentFont() succeeded
0183     //       but setCurrentAction() did not and vice-versa.
0184     debugTextShape << "Font not found " << family.toLower();
0185 }
0186 
0187 // have to include this because of Q_PRIVATE_SLOT
0188 #include "moc_FontFamilyAction.cpp"