| 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 | |
| 23 | class KFontRequesterPrivate |
| 24 | { |
| 25 | Q_DECLARE_TR_FUNCTIONS(KFontRequester) |
| 26 | |
| 27 | public: |
| 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 | |
| 46 | KFontRequester::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 | d->m_sampleLabel->setProperty(name: "_breeze_force_frame" , value: true); |
| 60 | setFocusProxy(d->m_button); |
| 61 | setFocusPolicy(d->m_button->focusPolicy()); |
| 62 | |
| 63 | layout->addWidget(d->m_sampleLabel, stretch: 1); |
| 64 | layout->addWidget(d->m_button); |
| 65 | |
| 66 | connect(sender: d->m_button, signal: &QPushButton::clicked, context: this, slot: [this] { |
| 67 | d->buttonClicked(); |
| 68 | }); |
| 69 | |
| 70 | d->displaySampleText(); |
| 71 | d->setToolTip(); |
| 72 | |
| 73 | d->m_sampleLabel->installEventFilter(filterObj: this); |
| 74 | } |
| 75 | |
| 76 | KFontRequester::~KFontRequester() = default; |
| 77 | |
| 78 | QFont KFontRequester::font() const |
| 79 | { |
| 80 | return d->m_selFont; |
| 81 | } |
| 82 | |
| 83 | bool KFontRequester::isFixedOnly() const |
| 84 | { |
| 85 | return d->m_onlyFixed; |
| 86 | } |
| 87 | |
| 88 | QString KFontRequester::sampleText() const |
| 89 | { |
| 90 | return d->m_sampleText; |
| 91 | } |
| 92 | |
| 93 | QString KFontRequester::title() const |
| 94 | { |
| 95 | return d->m_title; |
| 96 | } |
| 97 | |
| 98 | QLabel *KFontRequester::label() const |
| 99 | { |
| 100 | return d->m_sampleLabel; |
| 101 | } |
| 102 | |
| 103 | QPushButton *KFontRequester::button() const |
| 104 | { |
| 105 | return d->m_button; |
| 106 | } |
| 107 | |
| 108 | void 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(font: d->m_selFont); |
| 115 | } |
| 116 | |
| 117 | void KFontRequester::setSampleText(const QString &text) |
| 118 | { |
| 119 | d->m_sampleText = text; |
| 120 | d->displaySampleText(); |
| 121 | } |
| 122 | |
| 123 | void KFontRequester::setTitle(const QString &title) |
| 124 | { |
| 125 | d->m_title = title; |
| 126 | d->setToolTip(); |
| 127 | } |
| 128 | |
| 129 | bool KFontRequester::eventFilter(QObject *watched, QEvent *event) |
| 130 | { |
| 131 | if (watched == d->m_sampleLabel) { |
| 132 | switch (event->type()) { |
| 133 | case QEvent::MouseButtonPress: |
| 134 | case QEvent::MouseButtonRelease: { |
| 135 | auto *e = static_cast<QMouseEvent *>(event); |
| 136 | |
| 137 | if (e->button() == Qt::LeftButton && rect().contains(p: 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 | |
| 154 | void KFontRequesterPrivate::buttonClicked() |
| 155 | { |
| 156 | KFontChooser::DisplayFlags flags = m_onlyFixed ? KFontChooser::FixedFontsOnly : KFontChooser::NoDisplayFlags; |
| 157 | |
| 158 | const int result = KFontChooserDialog::getFont(theFont&: m_selFont, flags, parent: q->parentWidget()); |
| 159 | |
| 160 | if (result == QDialog::Accepted) { |
| 161 | displaySampleText(); |
| 162 | Q_EMIT q->fontSelected(font: m_selFont); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void 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(name: m_selFont.family()); |
| 177 | m_sampleLabel->setText(QStringLiteral("%1 %2" ).arg(a: family).arg(a: size)); |
| 178 | } else { |
| 179 | m_sampleLabel->setText(m_sampleText); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void KFontRequesterPrivate::setToolTip() |
| 184 | { |
| 185 | m_button->setToolTip(tr(sourceText: "Choose font…" , disambiguation: "@info:tooltip" )); |
| 186 | |
| 187 | m_sampleLabel->setToolTip(QString()); |
| 188 | m_sampleLabel->setWhatsThis(QString()); |
| 189 | |
| 190 | if (m_title.isNull()) { |
| 191 | m_sampleLabel->setToolTip(tr(sourceText: "Preview of the selected font" , disambiguation: "@info:tooltip" )); |
| 192 | m_sampleLabel->setWhatsThis( |
| 193 | tr(sourceText: "This is a preview of the selected font. You can change it" |
| 194 | " by clicking the \"Choose Font...\" button." , |
| 195 | disambiguation: "@info:whatsthis" )); |
| 196 | } else { |
| 197 | m_sampleLabel->setToolTip(tr(sourceText: "Preview of the \"%1\" font" , disambiguation: "@info:tooltip" ).arg(a: m_title)); |
| 198 | m_sampleLabel->setWhatsThis(tr(sourceText: "This is a preview of the \"%1\" font. You can change it" |
| 199 | " by clicking the \"Choose Font...\" button." , |
| 200 | disambiguation: "@info:whatsthis" ) |
| 201 | .arg(a: m_title)); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | #include "moc_kfontrequester.cpp" |
| 206 | |