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 <QEvent>
14#include <QFontDatabase>
15#include <QFontInfo>
16#include <QHBoxLayout>
17#include <QLabel>
18#include <QMouseEvent>
19#include <QPushButton>
20
21#include <cmath>
22
23class KFontRequesterPrivate
24{
25 Q_DECLARE_TR_FUNCTIONS(KFontRequester)
26
27public:
28 KFontRequesterPrivate(KFontRequester *qq)
29 : q(qq)
30 {
31 }
32
33 void displaySampleText();
34 void setToolTip();
35
36 void buttonClicked();
37
38 KFontRequester *q;
39 bool m_onlyFixed;
40 QString m_sampleText, m_title;
41 QLabel *m_sampleLabel = nullptr;
42 QPushButton *m_button = nullptr;
43 QFont m_selFont;
44};
45
46KFontRequester::KFontRequester(QWidget *parent, bool onlyFixed)
47 : QWidget(parent)
48 , d(new KFontRequesterPrivate(this))
49{
50 d->m_onlyFixed = onlyFixed;
51
52 QHBoxLayout *layout = new QHBoxLayout(this);
53 layout->setContentsMargins(left: 0, top: 0, right: 0, bottom: 0);
54
55 d->m_sampleLabel = new QLabel(this);
56 d->m_button = new QPushButton(QIcon::fromTheme(QStringLiteral("document-edit")), QString(), this);
57
58 d->m_sampleLabel->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
59 setFocusProxy(d->m_button);
60 setFocusPolicy(d->m_button->focusPolicy());
61
62 layout->addWidget(d->m_sampleLabel, stretch: 1);
63 layout->addWidget(d->m_button);
64
65 connect(sender: d->m_button, signal: &QPushButton::clicked, context: this, slot: [this] {
66 d->buttonClicked();
67 });
68
69 d->displaySampleText();
70 d->setToolTip();
71
72 d->m_sampleLabel->installEventFilter(filterObj: this);
73}
74
75KFontRequester::~KFontRequester() = default;
76
77QFont KFontRequester::font() const
78{
79 return d->m_selFont;
80}
81
82bool KFontRequester::isFixedOnly() const
83{
84 return d->m_onlyFixed;
85}
86
87QString KFontRequester::sampleText() const
88{
89 return d->m_sampleText;
90}
91
92QString KFontRequester::title() const
93{
94 return d->m_title;
95}
96
97QLabel *KFontRequester::label() const
98{
99 return d->m_sampleLabel;
100}
101
102QPushButton *KFontRequester::button() const
103{
104 return d->m_button;
105}
106
107void KFontRequester::setFont(const QFont &font, bool onlyFixed)
108{
109 d->m_selFont = font;
110 d->m_onlyFixed = onlyFixed;
111
112 d->displaySampleText();
113 Q_EMIT fontSelected(font: d->m_selFont);
114}
115
116void KFontRequester::setSampleText(const QString &text)
117{
118 d->m_sampleText = text;
119 d->displaySampleText();
120}
121
122void KFontRequester::setTitle(const QString &title)
123{
124 d->m_title = title;
125 d->setToolTip();
126}
127
128bool KFontRequester::eventFilter(QObject *watched, QEvent *event)
129{
130 if (watched == d->m_sampleLabel) {
131 switch (event->type()) {
132 case QEvent::MouseButtonPress:
133 case QEvent::MouseButtonRelease: {
134 auto *e = static_cast<QMouseEvent *>(event);
135
136 if (e->button() == Qt::LeftButton && rect().contains(p: e->pos())) {
137 if (e->type() == QEvent::MouseButtonRelease) {
138 d->buttonClicked();
139 }
140 e->accept();
141 return true;
142 }
143 break;
144 }
145 default:
146 break;
147 }
148 }
149
150 return QWidget::eventFilter(watched, event);
151}
152
153void KFontRequesterPrivate::buttonClicked()
154{
155 KFontChooser::DisplayFlags flags = m_onlyFixed ? KFontChooser::FixedFontsOnly : KFontChooser::NoDisplayFlags;
156
157 const int result = KFontChooserDialog::getFont(theFont&: m_selFont, flags, parent: q->parentWidget());
158
159 if (result == QDialog::Accepted) {
160 displaySampleText();
161 Q_EMIT q->fontSelected(font: m_selFont);
162 }
163}
164
165void KFontRequesterPrivate::displaySampleText()
166{
167 m_sampleLabel->setFont(m_selFont);
168
169 qreal size = m_selFont.pointSizeF();
170 if (size == -1) {
171 size = m_selFont.pixelSize();
172 }
173
174 if (m_sampleText.isEmpty()) {
175 QString family = translateFontName(name: m_selFont.family());
176 m_sampleLabel->setText(QStringLiteral("%1 %2").arg(a: family).arg(a: size));
177 } else {
178 m_sampleLabel->setText(m_sampleText);
179 }
180}
181
182void KFontRequesterPrivate::setToolTip()
183{
184 m_button->setToolTip(tr(sourceText: "Choose font...", disambiguation: "@info:tooltip"));
185
186 m_sampleLabel->setToolTip(QString());
187 m_sampleLabel->setWhatsThis(QString());
188
189 if (m_title.isNull()) {
190 m_sampleLabel->setToolTip(tr(sourceText: "Preview of the selected font", disambiguation: "@info:tooltip"));
191 m_sampleLabel->setWhatsThis(
192 tr(sourceText: "This is a preview of the selected font. You can change it"
193 " by clicking the \"Choose Font...\" button.",
194 disambiguation: "@info:whatsthis"));
195 } else {
196 m_sampleLabel->setToolTip(tr(sourceText: "Preview of the \"%1\" font", disambiguation: "@info:tooltip").arg(a: m_title));
197 m_sampleLabel->setWhatsThis(tr(sourceText: "This is a preview of the \"%1\" font. You can change it"
198 " by clicking the \"Choose Font...\" button.",
199 disambiguation: "@info:whatsthis")
200 .arg(a: m_title));
201 }
202}
203
204#include "moc_kfontrequester.cpp"
205

source code of kwidgetsaddons/src/kfontrequester.cpp