kdeui: replace KFontComboBox with QFontComboBox

one less class to maintain with substitute provided by Katie

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2024-03-31 01:28:12 +02:00
parent e1c7b6739b
commit bb11a3e5f4
8 changed files with 10 additions and 525 deletions

View file

@ -122,7 +122,6 @@ install(
KFloatValidator
KFontAction
KFontChooser
KFontComboBox
KFontDialog
KFontRequester
KFontSizeAction

View file

@ -1 +0,0 @@
#include "../kfontcombobox.h"

View file

@ -111,7 +111,6 @@ set(kdeui_LIB_SRCS
findreplace/kreplacedialog.cpp
fonts/fonthelpers.cpp
fonts/kfontchooser.cpp
fonts/kfontcombobox.cpp
fonts/kfontdialog.cpp
fonts/kfontrequester.cpp
icons/kiconeffect.cpp
@ -400,7 +399,6 @@ install(
findreplace/kfinddialog.h
findreplace/kreplacedialog.h
fonts/kfontchooser.h
fonts/kfontcombobox.h
fonts/kfontdialog.h
fonts/kfontrequester.h
icons/kiconeffect.h

View file

@ -28,14 +28,15 @@
#include "kfontaction.h"
#include <QtGui/QToolBar>
#include <QToolBar>
#include <QFontComboBox>
#include <QStringListModel>
#include <kdebug.h>
#include <kfontdialog.h>
#include <kicon.h>
#include <klocale.h>
#include <kfontchooser.h>
#include <kfontcombobox.h>
class KFontAction::KFontActionPrivate
{
@ -48,7 +49,7 @@ class KFontAction::KFontActionPrivate
void _k_slotFontChanged(const QFont &font)
{
kDebug(129) << "KFontComboBox - slotFontChanged("
kDebug(129) << "KFontAction - slotFontChanged("
<< font.family() << ") settingFont=" << settingFont;
if (settingFont)
return;
@ -117,8 +118,10 @@ QWidget* KFontAction::createWidget(QWidget* parent)
// This is the visual element on the screen. This method overrides
// the KSelectAction one, preventing KSelectAction from creating its
// regular KComboBox.
KFontComboBox *cb = new KFontComboBox( parent );
cb->setFontList(items());
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.
@ -130,9 +133,6 @@ QWidget* KFontAction::createWidget(QWidget* parent)
return cb;
}
/*
* Maintenance note: Keep in sync with KFontComboBox::setCurrentFont()
*/
void KFontAction::setFont( const QString &family )
{
kDebug(129) << "KFontAction::setFont(" << family << ")";
@ -142,7 +142,7 @@ void KFontAction::setFont( const QString &family )
foreach(QWidget *w, createdWidgets())
{
KFontComboBox *cb = qobject_cast<KFontComboBox *>(w);
QFontComboBox *cb = qobject_cast<QFontComboBox *>(w);
kDebug(129) << "\tw=" << w << "cb=" << cb;
if(!cb) continue;
@ -171,7 +171,7 @@ void KFontAction::setFont( const QString &family )
if (setCurrentAction(lowerName, Qt::CaseInsensitive))
return;
// TODO: Inconsistent state if KFontComboBox::setCurrentFont() succeeded
// TODO: Inconsistent state if QFontComboBox::setCurrentFont() succeeded
// but setCurrentAction() did not and vice-versa.
kDebug(129) << "Font not found " << family.toLower();
}

View file

@ -125,7 +125,6 @@ void KConfigDialogManager::initMaps()
// KDE
s_changedMap->insert( "KComboBox", SIGNAL(activated(int)));
s_changedMap->insert( "KFontComboBox", SIGNAL(activated(int)));
s_changedMap->insert( "KFontRequester", SIGNAL(fontSelected(QFont)));
s_changedMap->insert( "KFontChooser", SIGNAL(fontSelected(QFont)));
s_changedMap->insert( "KHistoryCombo", SIGNAL(activated(int)));

View file

@ -1,371 +0,0 @@
/* This file is part of the KDE libraries
Copyright (C) 2008 Chusslove Illich <caslav.ilic@gmx.net>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
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 "kfontcombobox.h"
#include "fonthelpers_p.h"
#include "kdebug.h"
#include "klocale.h"
#include "kcolorscheme.h"
#include "kglobalsettings.h"
#include "kfontchooser.h"
#include "kcompletion.h"
#include "kiconloader.h"
#include <QEvent>
#include <QListView>
#include <QFontDatabase>
#include <QIcon>
#include <QAbstractItemDelegate>
#include <QStringListModel>
#include <QPainter>
#include <QPen>
#include <QList>
#include <QHash>
#include <QScrollBar>
static QString alphabetSample ()
{
return i18nc("short",
// i18n: A shorter version of the alphabet test phrase translated in
// another message. It is displayed in the dropdown list of font previews
// (the font selection combo box), so keep it under the length equivalent
// to 60 or so proportional Latin characters.
"The Quick Brown Fox Jumps Over The Lazy Dog");
}
class KFontFamilyDelegate : public QAbstractItemDelegate
{
Q_OBJECT
public:
explicit KFontFamilyDelegate (QObject *parent);
void paint (QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QSize sizeHint (const QStyleOptionViewItem &option,
const QModelIndex &index) const;
QIcon truetype;
QIcon bitmap;
double sizeFactFamily;
double sizeFactSample;
QHash<QString, QString> fontFamilyTrMap;
QFontDatabase fontdb;
};
KFontFamilyDelegate::KFontFamilyDelegate (QObject *parent)
: QAbstractItemDelegate(parent)
{
truetype = SmallIcon(QString::fromLatin1("application-x-font-ttf"));
bitmap = SmallIcon(QString::fromLatin1("application-x-font-bdf"));
// Font size factors for family name and text sample in font previes,
// multiplies normal font size.
sizeFactFamily = 1.0;
sizeFactSample = 1.0; // better leave at 1, so that user can relate sizes to default
}
void KFontFamilyDelegate::paint (QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QBrush sampleBrush;
if (option.state & QStyle::State_Selected) {
painter->save();
painter->setBrush(option.palette.highlight());
painter->setPen(Qt::NoPen);
painter->drawRect(option.rect);
painter->setPen(QPen(option.palette.highlightedText(), 0));
sampleBrush = option.palette.highlightedText();
} else {
sampleBrush = KColorScheme(QPalette::Normal).foreground(KColorScheme::InactiveText);
}
QFont baseFont = KGlobalSettings::generalFont();
QString trFontFamily = index.data(Qt::DisplayRole).toString();
QString fontFamily = fontFamilyTrMap[trFontFamily];
// Choose and paint an icon according to the font type, scalable or bitmat.
const QIcon *icon = &bitmap;
if (fontdb.isScalable(fontFamily)) {
icon = &truetype;
}
QRect r = option.rect;
icon->paint(painter, r, Qt::AlignLeft|Qt::AlignTop);
// Claim space taken up by the icon.
QSize actualSize = icon->actualSize(r.size());
if (option.direction == Qt::RightToLeft) {
r.setRight(r.right() - actualSize.width() - 4);
} else {
r.setLeft(r.left() + actualSize.width() + 4);
}
// Draw the font family.
QFont oldPainterFont = painter->font();
QFont familyFont = baseFont;
familyFont.setPointSizeF(familyFont.pointSizeF() * sizeFactFamily);
painter->setFont(familyFont);
painter->drawText(r, Qt::AlignTop|Qt::AlignLeading|Qt::TextSingleLine, trFontFamily);
// Claim space taken up by the font family name.
int h = painter->fontMetrics().lineSpacing();
r.setTop(r.top() + h);
// Show text sample in user's language if the writing system is supported,
// otherwise show a collage of generic script samples provided by Qt.
// If the font does not report what it supports, assume all.
QString sample = alphabetSample();
QFont sampleFont;
sampleFont.setFamily(fontFamily);
sampleFont.setPointSizeF(sampleFont.pointSizeF() * sizeFactSample);
painter->setFont(sampleFont);
QPen oldPen = painter->pen();
painter->setPen(sampleBrush.color());
painter->drawText(r, Qt::AlignTop|Qt::AlignLeading|Qt::TextSingleLine, sample);
painter->setFont(oldPainterFont);
painter->setPen(oldPen);
if (option.state & QStyle::State_Selected) {
painter->restore();
}
}
QSize KFontFamilyDelegate::sizeHint (const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
Q_UNUSED(option);
QFont baseFont = KGlobalSettings::generalFont();
QString trFontFamily = index.data(Qt::DisplayRole).toString();
QString fontFamily = fontFamilyTrMap[trFontFamily];
QFont familyFont = baseFont;
familyFont.setPointSizeF(familyFont.pointSizeF() * sizeFactFamily);
QFontMetrics familyMetrics(familyFont);
QFont sampleFont = baseFont;
sampleFont.setFamily(fontFamily);
sampleFont.setPointSizeF(sampleFont.pointSizeF() * sizeFactSample);
QFontMetrics sampleMetrics(sampleFont);
QString sample = alphabetSample();
// Only the hight matters here, the width is mandated by KFontComboBox::event()
return QSize(qMax(familyMetrics.width(trFontFamily), sampleMetrics.width(sample)),
qRound(familyMetrics.lineSpacing() + sampleMetrics.lineSpacing() * 1.2));
}
class KFontComboBoxPrivate
{
public:
KFontComboBoxPrivate (KFontComboBox *parent);
void updateDatabase ();
void updateIndexToFont ();
void _k_currentFontChanged (int index);
KFontComboBox *k;
QFont currentFont;
bool onlyFixed;
bool signalsAllowed;
KFontFamilyDelegate *delegate;
QStringListModel *model;
QStringList fontList;
};
KFontComboBoxPrivate::KFontComboBoxPrivate (KFontComboBox *parent)
: k(parent),
currentFont(KGlobalSettings::generalFont()),
onlyFixed(false),
signalsAllowed(true)
{
}
void KFontComboBoxPrivate::updateDatabase ()
{
QStringList fontFamilies = fontList;
if (fontList.isEmpty()) {
KFontChooser::getFontList(fontFamilies,
onlyFixed ? KFontChooser::FixedWidthFonts : 0);
}
// Translate font families for the list model.
delegate->fontFamilyTrMap.clear();
QStringList trFontFamilies =
translateFontNameList(fontFamilies, &(delegate->fontFamilyTrMap));
// Add families to the list model and completion.
model->setStringList(trFontFamilies);
KCompletion *completion = k->completionObject();
if (completion) {
completion->setItems(trFontFamilies);
completion->setIgnoreCase(true);
}
}
void KFontComboBoxPrivate::updateIndexToFont ()
{
// QFontDatabase necessary to return the family with proper casing.
QFontDatabase fdb;
QString selectedFontFamily = fdb.font(currentFont.family(), currentFont.styleName(), currentFont.pointSize()).family();
QString trSelectedFontFamily = translateFontName(selectedFontFamily);
const QStringList trFontFamilies = model->stringList();
if (!trFontFamilies.count()) {
return;
}
// Match the font's family with an item in the list.
int index = 0;
foreach (const QString &trFontFamily, trFontFamilies) {
if (trSelectedFontFamily == trFontFamily) {
break;
}
++index;
}
if (index == trFontFamilies.count()) {
// If no family matched, change font to first on the list.
index = 0;
currentFont = QFont(delegate->fontFamilyTrMap[trFontFamilies[0]]);
emit k->currentFontChanged(currentFont);
}
// Set the new list item.
signalsAllowed = false;
k->setCurrentIndex(index);
signalsAllowed = true;
}
void KFontComboBoxPrivate::_k_currentFontChanged (int index)
{
if (!signalsAllowed) {
return;
}
QString trFontFamily = k->itemText(index);
QString fontFamily = delegate->fontFamilyTrMap[trFontFamily];
if (!fontFamily.isEmpty()) {
currentFont = QFont(fontFamily);
emit k->currentFontChanged(currentFont);
} else {
// Unknown font family given. Just remove from the list.
// This should not happen, as adding arbitrary font names is prevented.
QStringList lst = model->stringList();
lst.removeAll(trFontFamily);
model->setStringList(lst);
}
}
KFontComboBox::KFontComboBox (QWidget *parent)
: KComboBox(true, parent), d(new KFontComboBoxPrivate(this))
{
// Inputing arbitrary font names does not make sense.
setEditable(false);
// Special list item painter showing font previews and its list model.
d->delegate = new KFontFamilyDelegate(this);
setItemDelegate(d->delegate);
d->model = new QStringListModel(this);
setModel(d->model);
// Set current font when a new family has been chosen in the combo.
connect(this, SIGNAL(currentIndexChanged(int)),
this, SLOT(_k_currentFontChanged(int)));
// Initialize font selection and list of available fonts.
d->updateDatabase();
d->updateIndexToFont();
}
KFontComboBox::~KFontComboBox ()
{
delete d;
}
void KFontComboBox::setOnlyFixed (bool onlyFixed)
{
if (onlyFixed != d->onlyFixed) {
d->onlyFixed = onlyFixed;
d->updateDatabase();
}
}
void KFontComboBox::setFontList (const QStringList &fontList)
{
if (fontList != d->fontList) {
d->fontList = fontList;
d->updateDatabase();
}
}
QFont KFontComboBox::currentFont () const
{
return d->currentFont;
}
void KFontComboBox::setCurrentFont (const QFont &font)
{
if (font != d->currentFont) {
d->currentFont = font;
emit currentFontChanged(d->currentFont);
d->updateIndexToFont();
}
}
bool KFontComboBox::event (QEvent *e)
{
if (e->type() == QEvent::Resize) {
QListView *lview = qobject_cast<QListView*>(view());
if (lview) {
QString sample = alphabetSample();
// Limit text sample length to avoid too wide list view.
if (sample.length() > 60) {
sample = sample.left(57) + "...";
}
QFont approxFont = KGlobalSettings::generalFont();
approxFont.setPointSizeF(approxFont.pointSizeF()
* d->delegate->sizeFactSample);
int widgetWidth = width();
int sampleWidth = QFontMetrics(approxFont).width(sample);
sampleWidth = qRound(sampleWidth * 1.1); // extra for wider fonts
int iconWidth = d->delegate->truetype.actualSize(size()).width();
int vsbarWidth = 0;
if (lview->verticalScrollBar()) {
vsbarWidth = lview->verticalScrollBar()->width();
}
lview->window()->setFixedWidth( qMax(widgetWidth, sampleWidth)
+ iconWidth + vsbarWidth);
}
}
return KComboBox::event(e);
}
QSize KFontComboBox::sizeHint() const
{
QSize sz = KComboBox::sizeHint();
QFontMetrics fm(KGlobalSettings::generalFont());
sz.setWidth(fm.width("m") * 14);
return sz;
}
#include "kfontcombobox.moc"
#include "moc_kfontcombobox.moc"

View file

@ -1,133 +0,0 @@
/* This file is part of the KDE libraries
Copyright (C) 2008 Chusslove Illich <caslav.ilic@gmx.net>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
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.
*/
#ifndef KFONTCOMBOBOX_P_H
#define KFONTCOMBOBOX_P_H
#include <kdeui_export.h>
#include <kcombobox.h>
class KFontComboBoxPrivate;
/**
* @short A lightweight font selection widget.
*
* A combobox to select the font from. Lightweight counterpart to KFontChooser,
* for situations where only the font family should be selected, while the
* font style and size are handled by other means. Like in KFontChooser,
* this widget will show the font previews in the unrolled dropdown list.
*
* @note The class is similar to QFontComboBox, but more tightly integrated
* with KDE desktop. Use it instead of QFontComboBox by default in KDE code.
*
* \image html kfontcombobox.png "KDE Font Combo Box"
*
* @author Chusslove Illich \<caslav.ilic@gmx.net\>
*
* @see KFontAction
* @see KFontChooser
*
* @since 4.1
*/
class KDEUI_EXPORT KFontComboBox : public KComboBox
{
Q_OBJECT
Q_PROPERTY(QFont currentFont READ currentFont WRITE setCurrentFont NOTIFY currentFontChanged USER true)
public:
/**
* Constructor.
*
* @param parent the parent widget
*/
explicit KFontComboBox (QWidget *parent = 0);
/**
* Toggle selectable fonts to be only those of fixed width or all.
*
* @param onlyFixed only fixed width fonts when @p true,
* all fonts when @p false
*/
void setOnlyFixed (bool onlyFixed);
/**
* Set selectable fonts to be only those present in the list.
*
* @param fontList a list of fonts as returned by QFontDatabase::families() or
* QFontChooser::getFontList(). If this is empty (default), then the list
* of fonts is constructed according to the @p onlyFixed setting.
* @since 4.9.2
*/
void setFontList (const QStringList &fontList);
/**
* Destructor.
*/
virtual ~KFontComboBox ();
/**
* The font currently selected from the list.
*
* @return the selected font
*/
QFont currentFont () const;
/**
* The recommended size of the widget.
* Reimplemented to make the recommended width independent
* of the particular fonts installed.
*
* @return recommended size
*/
virtual QSize sizeHint() const;
public Q_SLOTS:
/**
* Set the font to show as selected in the combobox.
*
* @param font the new font
*/
void setCurrentFont (const QFont &font);
Q_SIGNALS:
/**
* Emitted when a new font has been selected,
* either through user input or by setFont().
*
* @param font the new font
*/
void currentFontChanged (const QFont &font);
protected:
bool event (QEvent *e);
private:
friend class KFontComboBoxPrivate;
KFontComboBoxPrivate * const d;
Q_DISABLE_COPY(KFontComboBox)
Q_PRIVATE_SLOT(d, void _k_currentFontChanged (int))
};
#endif

View file

@ -105,12 +105,6 @@ ToolTip=Font Chooser (KDE)
WhatsThis=A font type, size and style selection widget complete with preview
Group=Input (KDE)
[KFontComboBox]
IncludeFile=kfontcombobox.h
ToolTip=Font Combo Box (KDE)
WhatsThis=A combo box for font selection with language-specific previews
Group=Input (KDE)
[KFontRequester]
IncludeFile=kfontrequester.h
ToolTip=Font Requester (KDE)