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
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
47 : QWidget(parent)
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 setFocusProxy(d->m_button);
60 setFocusPolicy(d->m_button->focusPolicy());
61
62 layout->addWidget(d->m_sampleLabel, 1);
63 layout->addWidget(d->m_button);
64
65 connect(d->m_button, &QPushButton::clicked, this, [this] {
66 d->buttonClicked();
67 });
68
69 d->displaySampleText();
70 d->setToolTip();
71
72 d->m_sampleLabel->installEventFilter(this);
73}
74
75KFontRequester::~KFontRequester() = default;
76
77QFont KFontRequester::font() const
78{
79 return d->m_selFont;
80}
81
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
98{
99 return d->m_sampleLabel;
100}
101
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(d->m_selFont);
114}
115
117{
118 d->m_sampleText = text;
119 d->displaySampleText();
120}
121
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()) {
134 auto *e = static_cast<QMouseEvent *>(event);
135
136 if (e->button() == Qt::LeftButton && rect().contains(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{
156
157 const int result = KFontChooserDialog::getFont(m_selFont, flags, q->parentWidget());
158
159 if (result == QDialog::Accepted) {
160 displaySampleText();
161 Q_EMIT q->fontSelected(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(m_selFont.family());
176 m_sampleLabel->setText(QStringLiteral("%1 %2").arg(family).arg(size));
177 } else {
178 m_sampleLabel->setText(m_sampleText);
179 }
180}
181
182void KFontRequesterPrivate::setToolTip()
183{
184 m_button->setToolTip(tr("Choose font…", "@info:tooltip"));
185
186 m_sampleLabel->setToolTip(QString());
187 m_sampleLabel->setWhatsThis(QString());
188
189 if (m_title.isNull()) {
190 m_sampleLabel->setToolTip(tr("Preview of the selected font", "@info:tooltip"));
191 m_sampleLabel->setWhatsThis(
192 tr("This is a preview of the selected font. You can change it"
193 " by clicking the \"Choose Font...\" button.",
194 "@info:whatsthis"));
195 } else {
196 m_sampleLabel->setToolTip(tr("Preview of the \"%1\" font", "@info:tooltip").arg(m_title));
197 m_sampleLabel->setWhatsThis(tr("This is a preview of the \"%1\" font. You can change it"
198 " by clicking the \"Choose Font...\" button.",
199 "@info:whatsthis")
200 .arg(m_title));
201 }
202}
203
204#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.
@ FixedFontsOnly
Only show monospaced/fixed-width fonts, excluding proportional fonts, (the checkbox to toggle showing...
@ NoDisplayFlags
No flags set.
This class provides a widget with a lineedit and a button, which invokes a font dialog (KFontChooserD...
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
QString family() const const
int pixelSize() const const
qreal pointSizeF() const const
QIcon fromTheme(const QString &name)
void setText(const QString &)
void addWidget(QWidget *w)
void setContentsMargins(const QMargins &margins)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
virtual bool eventFilter(QObject *watched, QEvent *event)
bool isEmpty() const const
bool isNull() const const
LeftButton
virtual bool event(QEvent *event) override
void setFocusPolicy(Qt::FocusPolicy policy)
void setFont(const QFont &)
QLayout * layout() const const
QWidget * parentWidget() const const
void setFocusProxy(QWidget *w)
void setToolTip(const QString &)
void setWhatsThis(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:58:18 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.