1 /*
2     SPDX-FileCopyrightText: 2003 Nadeem Hasan <nhasan@kde.org>
3 
4     SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "kfontrequester.h"
8 #include "fonthelpers_p.h"
9 
10 #include <KFontChooserDialog>
11 
12 #include <QCoreApplication>
13 #include <QFontDatabase>
14 #include <QFontInfo>
15 #include <QHBoxLayout>
16 #include <QLabel>
17 #include <QPushButton>
18 
19 #include <cmath>
20 
21 class KFontRequesterPrivate
22 {
23     Q_DECLARE_TR_FUNCTIONS(KFontRequester)
24 
25 public:
KFontRequesterPrivate(KFontRequester * qq)26     KFontRequesterPrivate(KFontRequester *qq)
27         : q(qq)
28     {
29     }
30 
31     void displaySampleText();
32     void setToolTip();
33 
34     void buttonClicked();
35 
36     KFontRequester *q;
37     bool m_onlyFixed;
38     QString m_sampleText, m_title;
39     QLabel *m_sampleLabel = nullptr;
40     QPushButton *m_button = nullptr;
41     QFont m_selFont;
42 };
43 
KFontRequester(QWidget * parent,bool onlyFixed)44 KFontRequester::KFontRequester(QWidget *parent, bool onlyFixed)
45     : QWidget(parent)
46     , d(new KFontRequesterPrivate(this))
47 {
48     d->m_onlyFixed = onlyFixed;
49 
50     QHBoxLayout *layout = new QHBoxLayout(this);
51     layout->setContentsMargins(0, 0, 0, 0);
52 
53     d->m_sampleLabel = new QLabel(this);
54     d->m_button = new QPushButton(QIcon::fromTheme(QStringLiteral("document-edit")), QString(), this);
55 
56     d->m_sampleLabel->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
57     setFocusProxy(d->m_button);
58     setFocusPolicy(d->m_button->focusPolicy());
59 
60     layout->addWidget(d->m_sampleLabel, 1);
61     layout->addWidget(d->m_button);
62 
63     connect(d->m_button, &QPushButton::clicked, this, [this] {
64         d->buttonClicked();
65     });
66 
67     d->displaySampleText();
68     d->setToolTip();
69 }
70 
71 KFontRequester::~KFontRequester() = default;
72 
font() const73 QFont KFontRequester::font() const
74 {
75     return d->m_selFont;
76 }
77 
isFixedOnly() const78 bool KFontRequester::isFixedOnly() const
79 {
80     return d->m_onlyFixed;
81 }
82 
sampleText() const83 QString KFontRequester::sampleText() const
84 {
85     return d->m_sampleText;
86 }
87 
title() const88 QString KFontRequester::title() const
89 {
90     return d->m_title;
91 }
92 
label() const93 QLabel *KFontRequester::label() const
94 {
95     return d->m_sampleLabel;
96 }
97 
button() const98 QPushButton *KFontRequester::button() const
99 {
100     return d->m_button;
101 }
102 
setFont(const QFont & font,bool onlyFixed)103 void KFontRequester::setFont(const QFont &font, bool onlyFixed)
104 {
105     d->m_selFont = font;
106     d->m_onlyFixed = onlyFixed;
107 
108     d->displaySampleText();
109     Q_EMIT fontSelected(d->m_selFont);
110 }
111 
setSampleText(const QString & text)112 void KFontRequester::setSampleText(const QString &text)
113 {
114     d->m_sampleText = text;
115     d->displaySampleText();
116 }
117 
setTitle(const QString & title)118 void KFontRequester::setTitle(const QString &title)
119 {
120     d->m_title = title;
121     d->setToolTip();
122 }
123 
buttonClicked()124 void KFontRequesterPrivate::buttonClicked()
125 {
126     KFontChooser::DisplayFlags flags = m_onlyFixed ? KFontChooser::FixedFontsOnly : KFontChooser::NoDisplayFlags;
127 
128     const int result = KFontChooserDialog::getFont(m_selFont, flags, q->parentWidget());
129 
130     if (result == QDialog::Accepted) {
131         displaySampleText();
132         Q_EMIT q->fontSelected(m_selFont);
133     }
134 }
135 
displaySampleText()136 void KFontRequesterPrivate::displaySampleText()
137 {
138     m_sampleLabel->setFont(m_selFont);
139 
140     qreal size = m_selFont.pointSizeF();
141     if (size == -1) {
142         size = m_selFont.pixelSize();
143     }
144 
145     if (m_sampleText.isEmpty()) {
146         QString family = translateFontName(m_selFont.family());
147         m_sampleLabel->setText(QStringLiteral("%1 %2").arg(family).arg(size));
148     } else {
149         m_sampleLabel->setText(m_sampleText);
150     }
151 }
152 
setToolTip()153 void KFontRequesterPrivate::setToolTip()
154 {
155     m_button->setToolTip(tr("Choose font...", "@info:tooltip"));
156 
157     m_sampleLabel->setToolTip(QString());
158     m_sampleLabel->setWhatsThis(QString());
159 
160     if (m_title.isNull()) {
161         m_sampleLabel->setToolTip(tr("Preview of the selected font", "@info:tooltip"));
162         m_sampleLabel->setWhatsThis(
163             tr("This is a preview of the selected font. You can change it"
164                " by clicking the \"Choose Font...\" button.",
165                "@info:whatsthis"));
166     } else {
167         m_sampleLabel->setToolTip(tr("Preview of the \"%1\" font", "@info:tooltip").arg(m_title));
168         m_sampleLabel->setWhatsThis(tr("This is a preview of the \"%1\" font. You can change it"
169                                        " by clicking the \"Choose Font...\" button.",
170                                        "@info:whatsthis")
171                                         .arg(m_title));
172     }
173 }
174 
175 #include "moc_kfontrequester.cpp"
176