diff --git a/docs/pics/kfontchooserdialog.png b/docs/pics/kfontchooserdialog.png new file mode 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 GIT binary patch literal 0 Hc$@ + Copyright (c) 1999 Preston Brown + Copyright (c) 1999 Mario Weilguni + + 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 K_FONT_CHOOSER_DIALOG_H +#define K_FONT_CHOOSER_DIALOG_H + +#include + +#include + +class QFont; +class QStringList; + +class KFontChooserDialogPrivate; + +/** + * @short A font selection dialog. + * + * The KFontChooserDialog provides a dialog for interactive font selection. + * It is basically a thin wrapper around the KFontChooser widget (the latter + * can also be used standalone). In most cases, the simplest use of this + * class is the static method KFontChooserDialog::getFont(), which shows + * the dialog, allows the user to select a font, and returns when the + * dialog is closed. + * + * Features offered by KFontChooserDialog/KFontChooser: + * - The ability to set decimal font sizes (e.g. "12.1") + * - When selecting an initial font, if the styleName property of that font + * isn't set, the dialog will try and select the correct font style from the + * styles list + * - The ability to set multiple fonts at once, for an example of this functionality + * see "Adjust All Fonts" in the Fonts KCM in Systemsettings; and you can change + * the font family, style or size separately + * - Discarding the styleName property when closing the dialog for "Regular" font + * styles, since it doesn't make sense to set that property for such fonts and + * this allows setBold(true) to work correctly for more details see: + * https://bugreports.qt.io/browse/QTBUG-63792 + * https://bugs.kde.org/show_bug.cgi?id=378523 + * + * Example: + * + * \code + * QFont myFont; + * int result = KFontChooserDialog::getFont(myFont); + * if (result == QDialog::Accepted) { + * ... + * } + * \endcode + * + * \image html kfontchooserdialog.png "KDE Font Dialog" + * + * @author Preston Brown , Bernd Wuebben + * + * @since 5.69 + */ +class KWIDGETSADDONS_EXPORT KFontChooserDialog : public QDialog +{ + Q_OBJECT + +public: + /** + * Constructs a font selection dialog. + * + * @param flags Defines how the font chooser is displayed. + * @see KFontChooser::DisplayFlags + * @param parent The parent widget of the dialog, if any. + */ + explicit KFontChooserDialog(const KFontChooser::DisplayFlags &flags = KFontChooser::NoDisplayFlags, + QWidget *parent = nullptr); + + ~KFontChooserDialog(); + + /** + * Sets the currently selected font in the dialog. + * + * @param font The font to select + * @param onlyFixed If true, the font list will show only fixed width (monospace) + * fonts, otherwise all available fonts are shown + */ + void setFont(const QFont &font, bool onlyFixed = false); + + /** + * @return The currently selected font in the dialog. + */ + QFont font() const; + + /** + * Creates a modal font dialog, lets the user choose a font, and returns when + * the dialog is closed. + * + * @param theFont a reference to the font to write the chosen font into. + * @param flags Defines how the font chooser is displayed. + * @see KFontChooser::DisplayFlags + * @param parent Parent widget of the dialog; the dialog will be centered relative to it. + * @return QDialog::result(). + */ + static int getFont(QFont &theFont, + const KFontChooser::DisplayFlags &flags = KFontChooser::NoDisplayFlags, + QWidget *parent = nullptr); + + /** + * Creates a modal font difference dialog, lets the user choose a selection + * of changes that should be made to a set of fonts, and returns when the + * dialog is closed. Useful for choosing slight adjustments to the font set + * when the user would otherwise have to manually edit a number of fonts. + * + * @param theFont a reference to the font to write the chosen font into. + * @param flags Defines how the font chooser is displayed. + * @see KFontChooser::DisplayFlags + * @param diffFlags a reference to the int into which the chosen + * difference selection bitmask should be written. + * Check the returned bitmask like: + * \code + * if ( diffFlags & KFontChooser::FontDiffFamily ) { + * [...] + * } + * if ( diffFlags & KFontChooser::FontDiffStyle ) { + * [...] + * } + * if ( diffFlags & KFontChooser::FontDiffSize ) { + * [...] + * } + * \endcode + * @param parent Parent widget of the dialog; the dialog will be centered relative to it. + * + * @returns QDialog::result(). + */ + static int getFontDiff(QFont &theFont, + KFontChooser::FontDiffFlags &diffFlags, + const KFontChooser::DisplayFlags &flags = KFontChooser::NoDisplayFlags, + QWidget *parent = nullptr); + +Q_SIGNALS: + /** + * Emitted whenever the currently selected font changes. + * Connect to this to monitor the font as it is selected if you are + * not running modal. + */ + void fontSelected(const QFont &font); + +private: + KFontChooserDialogPrivate *const d; + + Q_DISABLE_COPY(KFontChooserDialog) +}; + +#endif diff --git a/src/kfontchooserdialog.cpp b/src/kfontchooserdialog.cpp new file mode 100644 --- /dev/null +++ b/src/kfontchooserdialog.cpp @@ -0,0 +1,123 @@ +/* +Copyright (C) 1996 Bernd Johannes Wuebben +Copyright (c) 1999 Preston Brown +Copyright (c) 1999 Mario Weilguni + +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 "kfontchooserdialog.h" + +#include +#include +#include + +class KFontChooserDialogPrivate +{ +public: + KFontChooserDialogPrivate() + : chooser(nullptr) + { + } + + KFontChooser *chooser; +}; + +KFontChooserDialog::KFontChooserDialog(const KFontChooser::DisplayFlags &flags, QWidget *parent) + : QDialog(parent), + d(new KFontChooserDialogPrivate) +{ + setWindowTitle(tr("Select Font")); + d->chooser = new KFontChooser(this, flags, QStringList(), 8, nullptr); + d->chooser->setObjectName(QStringLiteral("fontChooser")); + + connect(d->chooser, &KFontChooser::fontSelected, this, &KFontChooserDialog::fontSelected); + + QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); + QVBoxLayout *mainLayout = new QVBoxLayout; + setLayout(mainLayout); + mainLayout->addWidget(d->chooser); + mainLayout->addWidget(buttonBox); + + connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept); + connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject); +} + +KFontChooserDialog::~KFontChooserDialog() +{ + delete d; +} + +void KFontChooserDialog::setFont(const QFont &font, bool onlyFixed) +{ + d->chooser->setFont(font, onlyFixed); +} + +QFont KFontChooserDialog::font() const +{ + return d->chooser->font(); +} + +// If the styleName property is set for a QFont, using setBold(true) would +// lead to Qt using an "emboldended"/synthetic font style instead of using +// the bold style provided by the font itself; the latter looks better than +// the former, also accorgin to upstream, the styleName property is useful +// for fancy font styles, so there is no point in setting it for "Regular" +// fonts. For more details see: +// https://bugreports.qt.io/browse/QTBUG-63792 +// https://bugs.kde.org/show_bug.cgi?id=378523 +static void stripRegularStyleName(QFont &font) +{ + if (font.weight() == QFont::Normal + && (font.styleName() == QLatin1String("Regular") + || font.styleName() == QLatin1String("Normal") + || font.styleName() == QLatin1String("Book") + || font.styleName() == QLatin1String("Roman"))) { + font.setStyleName(QString()); + } +} + +// static +int KFontChooserDialog::getFontDiff(QFont &theFont, KFontChooser::FontDiffFlags &diffFlags, + const KFontChooser::DisplayFlags &flags, QWidget *parent) +{ + KFontChooserDialog dlg(flags | KFontChooser::ShowDifferences, parent); + dlg.setObjectName(QStringLiteral("Font Selector")); + dlg.setFont(theFont, flags & KFontChooser::FixedFontsOnly); + + const int result = dlg.exec(); + if (result == Accepted) { + theFont = dlg.d->chooser->font(); + diffFlags = dlg.d->chooser->fontDiffFlags(); + stripRegularStyleName(theFont); + } + return result; +} + +// static +int KFontChooserDialog::getFont(QFont &theFont, const KFontChooser::DisplayFlags &flags, QWidget *parent) +{ + KFontChooserDialog dlg(flags, parent); + dlg.setObjectName(QStringLiteral("Font Selector")); + dlg.setFont(theFont, flags & KFontChooser::FixedFontsOnly); + + const int result = dlg.exec(); + if (result == Accepted) { + theFont = dlg.d->chooser->font(); + stripRegularStyleName(theFont); + } + return result; +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -25,6 +25,7 @@ kseparatortest ksqueezedtextlabeltest ktitlewidgettest + kfontchooserdialogtest kfontrequestertest kpassworddialogtest keditlistwidgettest diff --git a/tests/kfontchooserdialogtest.cpp b/tests/kfontchooserdialogtest.cpp new file mode 100644 --- /dev/null +++ b/tests/kfontchooserdialogtest.cpp @@ -0,0 +1,45 @@ +/* + Copyright (C) 1996 Bernd Johannes Wuebben + wuebben@math.cornell.edu + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +*/ + +#include "kfontchooserdialog.h" + +#include +#include + +int main(int argc, char **argv) +{ + QApplication::setApplicationName(QStringLiteral("KFontChooserDialogTest")); + + QApplication app(argc, argv); + + app.setFont(QFont(QStringLiteral("DejaVu Sans"), 11)); + + // QFont font = QFont("Times",18,QFont::Bold); + + QFont font; + int nRet = KFontChooserDialog::getFont(font); + qDebug() << font.toString(); + + KFontChooser::FontDiffFlags diffFlags; + nRet = KFontChooserDialog::getFontDiff(font, diffFlags); + qDebug() << font.toString(); + + return nRet; +}