File indexing completed on 2024-04-28 03:59:06

0001 /*
0002     SPDX-FileCopyrightText: 2003 Nadeem Hasan <nhasan@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "kfontrequester.h"
0008 #include "fonthelpers_p.h"
0009 
0010 #include <KFontChooserDialog>
0011 
0012 #include <QCoreApplication>
0013 #include <QEvent>
0014 #include <QFontDatabase>
0015 #include <QFontInfo>
0016 #include <QHBoxLayout>
0017 #include <QLabel>
0018 #include <QMouseEvent>
0019 #include <QPushButton>
0020 
0021 #include <cmath>
0022 
0023 class KFontRequesterPrivate
0024 {
0025     Q_DECLARE_TR_FUNCTIONS(KFontRequester)
0026 
0027 public:
0028     KFontRequesterPrivate(KFontRequester *qq)
0029         : q(qq)
0030     {
0031     }
0032 
0033     void displaySampleText();
0034     void setToolTip();
0035 
0036     void buttonClicked();
0037 
0038     KFontRequester *q;
0039     bool m_onlyFixed;
0040     QString m_sampleText, m_title;
0041     QLabel *m_sampleLabel = nullptr;
0042     QPushButton *m_button = nullptr;
0043     QFont m_selFont;
0044 };
0045 
0046 KFontRequester::KFontRequester(QWidget *parent, bool onlyFixed)
0047     : QWidget(parent)
0048     , d(new KFontRequesterPrivate(this))
0049 {
0050     d->m_onlyFixed = onlyFixed;
0051 
0052     QHBoxLayout *layout = new QHBoxLayout(this);
0053     layout->setContentsMargins(0, 0, 0, 0);
0054 
0055     d->m_sampleLabel = new QLabel(this);
0056     d->m_button = new QPushButton(QIcon::fromTheme(QStringLiteral("document-edit")), QString(), this);
0057 
0058     d->m_sampleLabel->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken);
0059     setFocusProxy(d->m_button);
0060     setFocusPolicy(d->m_button->focusPolicy());
0061 
0062     layout->addWidget(d->m_sampleLabel, 1);
0063     layout->addWidget(d->m_button);
0064 
0065     connect(d->m_button, &QPushButton::clicked, this, [this] {
0066         d->buttonClicked();
0067     });
0068 
0069     d->displaySampleText();
0070     d->setToolTip();
0071 
0072     d->m_sampleLabel->installEventFilter(this);
0073 }
0074 
0075 KFontRequester::~KFontRequester() = default;
0076 
0077 QFont KFontRequester::font() const
0078 {
0079     return d->m_selFont;
0080 }
0081 
0082 bool KFontRequester::isFixedOnly() const
0083 {
0084     return d->m_onlyFixed;
0085 }
0086 
0087 QString KFontRequester::sampleText() const
0088 {
0089     return d->m_sampleText;
0090 }
0091 
0092 QString KFontRequester::title() const
0093 {
0094     return d->m_title;
0095 }
0096 
0097 QLabel *KFontRequester::label() const
0098 {
0099     return d->m_sampleLabel;
0100 }
0101 
0102 QPushButton *KFontRequester::button() const
0103 {
0104     return d->m_button;
0105 }
0106 
0107 void KFontRequester::setFont(const QFont &font, bool onlyFixed)
0108 {
0109     d->m_selFont = font;
0110     d->m_onlyFixed = onlyFixed;
0111 
0112     d->displaySampleText();
0113     Q_EMIT fontSelected(d->m_selFont);
0114 }
0115 
0116 void KFontRequester::setSampleText(const QString &text)
0117 {
0118     d->m_sampleText = text;
0119     d->displaySampleText();
0120 }
0121 
0122 void KFontRequester::setTitle(const QString &title)
0123 {
0124     d->m_title = title;
0125     d->setToolTip();
0126 }
0127 
0128 bool KFontRequester::eventFilter(QObject *watched, QEvent *event)
0129 {
0130     if (watched == d->m_sampleLabel) {
0131         switch (event->type()) {
0132         case QEvent::MouseButtonPress:
0133         case QEvent::MouseButtonRelease: {
0134             auto *e = static_cast<QMouseEvent *>(event);
0135 
0136             if (e->button() == Qt::LeftButton && rect().contains(e->pos())) {
0137                 if (e->type() == QEvent::MouseButtonRelease) {
0138                     d->buttonClicked();
0139                 }
0140                 e->accept();
0141                 return true;
0142             }
0143             break;
0144         }
0145         default:
0146             break;
0147         }
0148     }
0149 
0150     return QWidget::eventFilter(watched, event);
0151 }
0152 
0153 void KFontRequesterPrivate::buttonClicked()
0154 {
0155     KFontChooser::DisplayFlags flags = m_onlyFixed ? KFontChooser::FixedFontsOnly : KFontChooser::NoDisplayFlags;
0156 
0157     const int result = KFontChooserDialog::getFont(m_selFont, flags, q->parentWidget());
0158 
0159     if (result == QDialog::Accepted) {
0160         displaySampleText();
0161         Q_EMIT q->fontSelected(m_selFont);
0162     }
0163 }
0164 
0165 void KFontRequesterPrivate::displaySampleText()
0166 {
0167     m_sampleLabel->setFont(m_selFont);
0168 
0169     qreal size = m_selFont.pointSizeF();
0170     if (size == -1) {
0171         size = m_selFont.pixelSize();
0172     }
0173 
0174     if (m_sampleText.isEmpty()) {
0175         QString family = translateFontName(m_selFont.family());
0176         m_sampleLabel->setText(QStringLiteral("%1 %2").arg(family).arg(size));
0177     } else {
0178         m_sampleLabel->setText(m_sampleText);
0179     }
0180 }
0181 
0182 void KFontRequesterPrivate::setToolTip()
0183 {
0184     m_button->setToolTip(tr("Choose font...", "@info:tooltip"));
0185 
0186     m_sampleLabel->setToolTip(QString());
0187     m_sampleLabel->setWhatsThis(QString());
0188 
0189     if (m_title.isNull()) {
0190         m_sampleLabel->setToolTip(tr("Preview of the selected font", "@info:tooltip"));
0191         m_sampleLabel->setWhatsThis(
0192             tr("This is a preview of the selected font. You can change it"
0193                " by clicking the \"Choose Font...\" button.",
0194                "@info:whatsthis"));
0195     } else {
0196         m_sampleLabel->setToolTip(tr("Preview of the \"%1\" font", "@info:tooltip").arg(m_title));
0197         m_sampleLabel->setWhatsThis(tr("This is a preview of the \"%1\" font. You can change it"
0198                                        " by clicking the \"Choose Font...\" button.",
0199                                        "@info:whatsthis")
0200                                         .arg(m_title));
0201     }
0202 }
0203 
0204 #include "moc_kfontrequester.cpp"