1/*
2 SPDX-FileCopyrightText: 1996 Bernd Johannes Wuebben <wuebben@kde.org>
3 SPDX-FileCopyrightText: 1999 Preston Brown <pbrown@kde.org>
4 SPDX-FileCopyrightText: 1999 Mario Weilguni <mweilguni@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "kfontchooserdialog.h"
10
11#include <QDialogButtonBox>
12#include <QPointer>
13#include <QVBoxLayout>
14
15class KFontChooserDialogPrivate
16{
17public:
18 KFontChooser *m_fontChooser = nullptr;
19};
20
21KFontChooserDialog::KFontChooserDialog(const KFontChooser::DisplayFlags &flags, QWidget *parent)
22 : QDialog(parent)
23 , d(new KFontChooserDialogPrivate)
24{
25 setWindowTitle(tr(s: "Select Font", c: "@title:window"));
26 d->m_fontChooser = new KFontChooser(flags, this);
27 d->m_fontChooser->setMinVisibleItems(8);
28 d->m_fontChooser->setObjectName(QStringLiteral("fontChooser"));
29
30 connect(sender: d->m_fontChooser, signal: &KFontChooser::fontSelected, context: this, slot: &KFontChooserDialog::fontSelected);
31
32 QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
33 QVBoxLayout *mainLayout = new QVBoxLayout(this);
34 mainLayout->addWidget(d->m_fontChooser);
35 mainLayout->addWidget(buttonBox);
36
37 connect(sender: buttonBox, signal: &QDialogButtonBox::accepted, context: this, slot: &QDialog::accept);
38 connect(sender: buttonBox, signal: &QDialogButtonBox::rejected, context: this, slot: &QDialog::reject);
39}
40
41KFontChooserDialog::~KFontChooserDialog() = default;
42
43void KFontChooserDialog::setFont(const QFont &font, bool onlyFixed)
44{
45 d->m_fontChooser->setFont(font, onlyFixed);
46}
47
48QFont KFontChooserDialog::font() const
49{
50 return d->m_fontChooser->font();
51}
52
53// If the styleName property is set for a QFont, using setBold(true) would
54// lead to Qt using an "emboldended"/synthetic font style instead of using
55// the bold style provided by the font itself; the latter looks better than
56// the former, also accorgin to upstream, the styleName property is useful
57// for fancy font styles, so there is no point in setting it for "Regular"
58// fonts. For more details see:
59// https://bugreports.qt.io/browse/QTBUG-63792
60// https://bugs.kde.org/show_bug.cgi?id=378523
61static void stripRegularStyleName(QFont &font)
62{
63 if (font.weight() == QFont::Normal //
64 && (font.styleName() == QLatin1String("Regular") //
65 || font.styleName() == QLatin1String("Normal") //
66 || font.styleName() == QLatin1String("Book") //
67 || font.styleName() == QLatin1String("Roman"))) {
68 font.setStyleName(QString());
69 }
70}
71
72// static
73int KFontChooserDialog::getFontDiff(QFont &theFont, KFontChooser::FontDiffFlags &diffFlags, const KFontChooser::DisplayFlags &flags, QWidget *parent)
74{
75 QPointer<KFontChooserDialog> dialog = new KFontChooserDialog(flags | KFontChooser::ShowDifferences, parent);
76 dialog->setObjectName(QStringLiteral("Font Selector"));
77 dialog->setFont(font: theFont, onlyFixed: flags & KFontChooser::FixedFontsOnly);
78
79 const int result = dialog->exec();
80 if (result == Accepted) {
81 theFont = dialog->d->m_fontChooser->font();
82 diffFlags = dialog->d->m_fontChooser->fontDiffFlags();
83 stripRegularStyleName(font&: theFont);
84 }
85 delete dialog;
86 return result;
87}
88
89// static
90int KFontChooserDialog::getFont(QFont &theFont, const KFontChooser::DisplayFlags &flags, QWidget *parent)
91{
92 QPointer<KFontChooserDialog> dialog = new KFontChooserDialog(flags, parent);
93 dialog->setObjectName(QStringLiteral("Font Selector"));
94 dialog->setFont(font: theFont, onlyFixed: flags & KFontChooser::FixedFontsOnly);
95
96 const int result = dialog->exec();
97 if (result == Accepted) {
98 theFont = dialog->d->m_fontChooser->font();
99 stripRegularStyleName(font&: theFont);
100 }
101 delete dialog;
102 return result;
103}
104
105#include "moc_kfontchooserdialog.cpp"
106

source code of kwidgetsaddons/src/kfontchooserdialog.cpp