KWidgetsAddons

kfontrequester.cpp
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
48 , d(new KFontRequesterPrivate(this))
49{
50 d->m_onlyFixed = onlyFixed;
51
52 QHBoxLayout *layout = new QHBoxLayout(this);
53 layout->setContentsMargins(0, 0, 0, 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 d->m_sampleLabel->setProperty("_breeze_force_frame", true);
60 setFocusProxy(d->m_button);
61 setFocusPolicy(d->m_button->focusPolicy());
62
63 layout->addWidget(d->m_sampleLabel, 1);
64 layout->addWidget(d->m_button);
65
66 connect(d->m_button, &QPushButton::clicked, this, [this] {
67 d->buttonClicked();
68 });
69
70 d->displaySampleText();
71 d->setToolTip();
72
73 d->m_sampleLabel->installEventFilter(this);
74}
75
76KFontRequester::~KFontRequester() = default;
77
78QFont KFontRequester::font() const
79{
80 return d->m_selFont;
81}
82
84{
85 return d->m_onlyFixed;
86}
87
88QString KFontRequester::sampleText() const
89{
90 return d->m_sampleText;
91}
92
93QString KFontRequester::title() const
94{
95 return d->m_title;
96}
97
99{
100 return d->m_sampleLabel;
101}
102
104{
105 return d->m_button;
106}
107
108void KFontRequester::setFont(const QFont &font, bool onlyFixed)
109{
110 d->m_selFont = font;
111 d->m_onlyFixed = onlyFixed;
112
113 d->displaySampleText();
114 Q_EMIT fontSelected(d->m_selFont);
115}
116
118{
119 d->m_sampleText = text;
120 d->displaySampleText();
121}
122
124{
125 d->m_title = title;
126 d->setToolTip();
127}
128
129bool KFontRequester::eventFilter(QObject *watched, QEvent *event)
130{
131 if (watched == d->m_sampleLabel) {
132 switch (event->type()) {
135 auto *e = static_cast<QMouseEvent *>(event);
136
137 if (e->button() == Qt::LeftButton && rect().contains(e->pos())) {
138 if (e->type() == QEvent::MouseButtonRelease) {
139 d->buttonClicked();
140 }
141 e->accept();
142 return true;
143 }
144 break;
145 }
146 default:
147 break;
148 }
149 }
150
151 return QWidget::eventFilter(watched, event);
152}
153
154void KFontRequesterPrivate::buttonClicked()
155{
157
158 const int result = KFontChooserDialog::getFont(m_selFont, flags, q->parentWidget());
159
160 if (result == QDialog::Accepted) {
161 displaySampleText();
162 Q_EMIT q->fontSelected(m_selFont);
163 }
164}
165
166void KFontRequesterPrivate::displaySampleText()
167{
168 m_sampleLabel->setFont(m_selFont);
169
170 qreal size = m_selFont.pointSizeF();
171 if (size == -1) {
172 size = m_selFont.pixelSize();
173 }
174
175 if (m_sampleText.isEmpty()) {
176 QString family = translateFontName(m_selFont.family());
177 m_sampleLabel->setText(QStringLiteral("%1 %2").arg(family).arg(size));
178 } else {
179 m_sampleLabel->setText(m_sampleText);
180 }
181}
182
183void KFontRequesterPrivate::setToolTip()
184{
185 m_button->setToolTip(tr("Choose font…", "@info:tooltip"));
186
187 m_sampleLabel->setToolTip(QString());
188 m_sampleLabel->setWhatsThis(QString());
189
190 if (m_title.isNull()) {
191 m_sampleLabel->setToolTip(tr("Preview of the selected font", "@info:tooltip"));
192 m_sampleLabel->setWhatsThis(
193 tr("This is a preview of the selected font. You can change it"
194 " by clicking the \"Choose Font...\" button.",
195 "@info:whatsthis"));
196 } else {
197 m_sampleLabel->setToolTip(tr("Preview of the \"%1\" font", "@info:tooltip").arg(m_title));
198 m_sampleLabel->setWhatsThis(tr("This is a preview of the \"%1\" font. You can change it"
199 " by clicking the \"Choose Font...\" button.",
200 "@info:whatsthis")
201 .arg(m_title));
202 }
203}
204
205#include "moc_kfontrequester.cpp"
static int getFont(QFont &theFont, const KFontChooser::DisplayFlags &flags=KFontChooser::NoDisplayFlags, QWidget *parent=nullptr)
Creates a modal font dialog, lets the user choose a font, and returns when the dialog is closed.
QFlags< DisplayFlag > DisplayFlags
Stores a combination of DisplayFlag values.
@ FixedFontsOnly
Only show monospaced/fixed-width fonts, excluding proportional fonts, (the checkbox to toggle showing...
@ NoDisplayFlags
No flags set.
virtual void setFont(const QFont &font, bool onlyFixed=false)
Sets the currently selected font in the requester.
bool isFixedOnly() const
QLabel * label() const
virtual void setTitle(const QString &title)
Set the title for the widget that will be used in the tooltip and what's this text.
QPushButton * button() const
void fontSelected(const QFont &font)
Emitted when a new font has been selected in the underlying dialog.
virtual void setSampleText(const QString &text)
Sets the sample text.
KFontRequester(QWidget *parent=nullptr, bool onlyFixed=false)
Constructs a font requester widget.
void clicked(bool checked)
MouseButtonPress
QIcon fromTheme(const QString &name)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool event(QEvent *e)
virtual bool eventFilter(QObject *watched, QEvent *event)
QObject * parent() const const
LeftButton
QWidget(QWidget *parent, Qt::WindowFlags f)
virtual bool event(QEvent *event) override
void setFocusPolicy(Qt::FocusPolicy policy)
QLayout * layout() const const
void setFocusProxy(QWidget *w)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Mar 28 2025 11:54:47 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.