File indexing completed on 2024-04-28 15:32:03

0001 /*
0002     SPDX-FileCopyrightText: 1996 Bernd Johannes Wuebben <wuebben@kde.org>
0003     SPDX-FileCopyrightText: 1999 Preston Brown <pbrown@kde.org>
0004     SPDX-FileCopyrightText: 1999 Mario Weilguni <mweilguni@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "kfontchooserdialog.h"
0010 
0011 #include <QDialogButtonBox>
0012 #include <QPointer>
0013 #include <QVBoxLayout>
0014 
0015 class KFontChooserDialogPrivate
0016 {
0017 public:
0018     KFontChooser *m_fontChooser = nullptr;
0019 };
0020 
0021 KFontChooserDialog::KFontChooserDialog(const KFontChooser::DisplayFlags &flags, QWidget *parent)
0022     : QDialog(parent)
0023     , d(new KFontChooserDialogPrivate)
0024 {
0025     setWindowTitle(tr("Select Font", "@title:window"));
0026     d->m_fontChooser = new KFontChooser(flags, this);
0027     d->m_fontChooser->setMinVisibleItems(8);
0028     d->m_fontChooser->setObjectName(QStringLiteral("fontChooser"));
0029 
0030     connect(d->m_fontChooser, &KFontChooser::fontSelected, this, &KFontChooserDialog::fontSelected);
0031 
0032     QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
0033     QVBoxLayout *mainLayout = new QVBoxLayout(this);
0034     mainLayout->addWidget(d->m_fontChooser);
0035     mainLayout->addWidget(buttonBox);
0036 
0037     connect(buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
0038     connect(buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
0039 }
0040 
0041 KFontChooserDialog::~KFontChooserDialog() = default;
0042 
0043 void KFontChooserDialog::setFont(const QFont &font, bool onlyFixed)
0044 {
0045     d->m_fontChooser->setFont(font, onlyFixed);
0046 }
0047 
0048 QFont KFontChooserDialog::font() const
0049 {
0050     return d->m_fontChooser->font();
0051 }
0052 
0053 // If the styleName property is set for a QFont, using setBold(true) would
0054 // lead to Qt using an "emboldended"/synthetic font style instead of using
0055 // the bold style provided by the font itself; the latter looks better than
0056 // the former, also accorgin to upstream, the styleName property is useful
0057 // for fancy font styles, so there is no point in setting it for "Regular"
0058 // fonts. For more details see:
0059 // https://bugreports.qt.io/browse/QTBUG-63792
0060 // https://bugs.kde.org/show_bug.cgi?id=378523
0061 static void stripRegularStyleName(QFont &font)
0062 {
0063     if (font.weight() == QFont::Normal //
0064         && (font.styleName() == QLatin1String("Regular") //
0065             || font.styleName() == QLatin1String("Normal") //
0066             || font.styleName() == QLatin1String("Book") //
0067             || font.styleName() == QLatin1String("Roman"))) {
0068         font.setStyleName(QString());
0069     }
0070 }
0071 
0072 // static
0073 int KFontChooserDialog::getFontDiff(QFont &theFont, KFontChooser::FontDiffFlags &diffFlags, const KFontChooser::DisplayFlags &flags, QWidget *parent)
0074 {
0075     QPointer<KFontChooserDialog> dialog = new KFontChooserDialog(flags | KFontChooser::ShowDifferences, parent);
0076     dialog->setObjectName(QStringLiteral("Font Selector"));
0077     dialog->setFont(theFont, flags & KFontChooser::FixedFontsOnly);
0078 
0079     const int result = dialog->exec();
0080     if (result == Accepted) {
0081         theFont = dialog->d->m_fontChooser->font();
0082         diffFlags = dialog->d->m_fontChooser->fontDiffFlags();
0083         stripRegularStyleName(theFont);
0084     }
0085     delete dialog;
0086     return result;
0087 }
0088 
0089 // static
0090 int KFontChooserDialog::getFont(QFont &theFont, const KFontChooser::DisplayFlags &flags, QWidget *parent)
0091 {
0092     QPointer<KFontChooserDialog> dialog = new KFontChooserDialog(flags, parent);
0093     dialog->setObjectName(QStringLiteral("Font Selector"));
0094     dialog->setFont(theFont, flags & KFontChooser::FixedFontsOnly);
0095 
0096     const int result = dialog->exec();
0097     if (result == Accepted) {
0098         theFont = dialog->d->m_fontChooser->font();
0099         stripRegularStyleName(theFont);
0100     }
0101     delete dialog;
0102     return result;
0103 }
0104 
0105 #include "moc_kfontchooserdialog.cpp"