kdelibs/kdeui/actions/kfontaction.cpp
Ivailo Monev 45674c0c4e kdeui: replace KFontDialog and KFontChooser with QFontDialog
requires the following commit from the Katie repo:
97fea30784dfdc41a6599ea31219d0b366d8efaa

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
2024-03-31 19:01:45 +03:00

163 lines
5 KiB
C++

/* This file is part of the KDE libraries
Copyright (C) 1999 Reginald Stadlbauer <reggie@kde.org>
(C) 1999 Simon Hausmann <hausmann@kde.org>
(C) 2000 Nicolas Hadacek <haadcek@kde.org>
(C) 2000 Kurt Granroth <granroth@kde.org>
(C) 2000 Michael Koch <koch@kde.org>
(C) 2001 Holger Freyther <freyther@kde.org>
(C) 2002 Ellis Whitehead <ellis@kde.org>
(C) 2002 Joseph Wenninger <jowenn@kde.org>
(C) 2003 Andras Mantia <amantia@kde.org>
(C) 2005-2006 Hamish Rodda <rodda@kde.org>
(C) 2007 Clarence Dang <dang@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "kfontaction.h"
#include <QToolBar>
#include <QFontDatabase>
#include <QFontComboBox>
#include <QStringListModel>
#include <kdebug.h>
#include <kicon.h>
#include <klocale.h>
class KFontAction::KFontActionPrivate
{
public:
KFontActionPrivate(KFontAction *parent)
: q(parent),
settingFont(0)
{
}
void _k_slotFontChanged(const QFont &font)
{
kDebug(129) << "KFontAction - slotFontChanged("
<< font.family() << ") settingFont=" << settingFont;
if (settingFont)
return;
q->setFont(font.family());
q->triggered(font.family());
kDebug(129) << "\tslotFontChanged done";
}
KFontAction *q;
int settingFont;
};
KFontAction::KFontAction(QObject *parent)
: KSelectAction(parent), d(new KFontActionPrivate(this))
{
KSelectAction::setItems( QFontDatabase().families() );
setEditable( true );
}
KFontAction::KFontAction(const QString & text, QObject *parent)
: KSelectAction(text, parent), d(new KFontActionPrivate(this))
{
KSelectAction::setItems( QFontDatabase().families() );
setEditable( true );
}
KFontAction::KFontAction(const KIcon &icon, const QString &text, QObject *parent)
: KSelectAction(icon, text, parent), d(new KFontActionPrivate(this))
{
KSelectAction::setItems( QFontDatabase().families() );
setEditable( true );
}
KFontAction::~KFontAction()
{
delete d;
}
QString KFontAction::font() const
{
return currentText();
}
QWidget* KFontAction::createWidget(QWidget* parent)
{
kDebug(129) << "KFontAction::createWidget()";
#warning FIXME: items need to be converted
// This is the visual element on the screen. This method overrides
// the KSelectAction one, preventing KSelectAction from creating its
// regular KComboBox.
QFontComboBox *cb = new QFontComboBox( parent );
QStringListModel *cbmodel = qobject_cast<QStringListModel*>(cb->model());
Q_ASSERT(cbmodel);
cbmodel->setStringList(items());
kDebug(129) << "\tset=" << font();
// Do this before connecting the signal so that nothing will fire.
cb->setCurrentFont( QFont( font().toLower() ) );
kDebug(129) << "\tspit back=" << cb->currentFont().family();
connect( cb, SIGNAL(currentFontChanged(QFont)), SLOT(_k_slotFontChanged(QFont)) );
cb->setMinimumWidth( cb->sizeHint().width() );
return cb;
}
void KFontAction::setFont( const QString &family )
{
kDebug(129) << "KFontAction::setFont(" << family << ")";
// Suppress triggered(QString) signal and prevent recursive call to ourself.
d->settingFont++;
foreach(QWidget *w, createdWidgets())
{
QFontComboBox *cb = qobject_cast<QFontComboBox *>(w);
kDebug(129) << "\tw=" << w << "cb=" << cb;
if(!cb) continue;
cb->setCurrentFont(QFont(family.toLower()));
kDebug(129) << "\t\tw spit back=" << cb->currentFont().family();
}
d->settingFont--;
kDebug(129) << "\tcalling setCurrentAction()";
QString lowerName = family.toLower();
if (setCurrentAction(lowerName, Qt::CaseInsensitive))
return;
int i = lowerName.indexOf(" [");
if (i > -1)
{
lowerName = lowerName.left(i);
if (setCurrentAction(lowerName, Qt::CaseInsensitive))
return;
}
lowerName += " [";
if (setCurrentAction(lowerName, Qt::CaseInsensitive))
return;
// TODO: Inconsistent state if QFontComboBox::setCurrentFont() succeeded
// but setCurrentAction() did not and vice-versa.
kDebug(129) << "Font not found " << family.toLower();
}
#include "moc_kfontaction.cpp"