File indexing completed on 2025-02-16 13:11:42
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 <QFontDatabase> 0014 #include <QFontInfo> 0015 #include <QHBoxLayout> 0016 #include <QLabel> 0017 #include <QPushButton> 0018 0019 #include <cmath> 0020 0021 class KFontRequesterPrivate 0022 { 0023 Q_DECLARE_TR_FUNCTIONS(KFontRequester) 0024 0025 public: 0026 KFontRequesterPrivate(KFontRequester *qq) 0027 : q(qq) 0028 { 0029 } 0030 0031 void displaySampleText(); 0032 void setToolTip(); 0033 0034 void buttonClicked(); 0035 0036 KFontRequester *q; 0037 bool m_onlyFixed; 0038 QString m_sampleText, m_title; 0039 QLabel *m_sampleLabel = nullptr; 0040 QPushButton *m_button = nullptr; 0041 QFont m_selFont; 0042 }; 0043 0044 KFontRequester::KFontRequester(QWidget *parent, bool onlyFixed) 0045 : QWidget(parent) 0046 , d(new KFontRequesterPrivate(this)) 0047 { 0048 d->m_onlyFixed = onlyFixed; 0049 0050 QHBoxLayout *layout = new QHBoxLayout(this); 0051 layout->setContentsMargins(0, 0, 0, 0); 0052 0053 d->m_sampleLabel = new QLabel(this); 0054 d->m_button = new QPushButton(QIcon::fromTheme(QStringLiteral("document-edit")), QString(), this); 0055 0056 d->m_sampleLabel->setFrameStyle(QFrame::StyledPanel | QFrame::Sunken); 0057 setFocusProxy(d->m_button); 0058 setFocusPolicy(d->m_button->focusPolicy()); 0059 0060 layout->addWidget(d->m_sampleLabel, 1); 0061 layout->addWidget(d->m_button); 0062 0063 connect(d->m_button, &QPushButton::clicked, this, [this] { 0064 d->buttonClicked(); 0065 }); 0066 0067 d->displaySampleText(); 0068 d->setToolTip(); 0069 } 0070 0071 KFontRequester::~KFontRequester() = default; 0072 0073 QFont KFontRequester::font() const 0074 { 0075 return d->m_selFont; 0076 } 0077 0078 bool KFontRequester::isFixedOnly() const 0079 { 0080 return d->m_onlyFixed; 0081 } 0082 0083 QString KFontRequester::sampleText() const 0084 { 0085 return d->m_sampleText; 0086 } 0087 0088 QString KFontRequester::title() const 0089 { 0090 return d->m_title; 0091 } 0092 0093 QLabel *KFontRequester::label() const 0094 { 0095 return d->m_sampleLabel; 0096 } 0097 0098 QPushButton *KFontRequester::button() const 0099 { 0100 return d->m_button; 0101 } 0102 0103 void KFontRequester::setFont(const QFont &font, bool onlyFixed) 0104 { 0105 d->m_selFont = font; 0106 d->m_onlyFixed = onlyFixed; 0107 0108 d->displaySampleText(); 0109 Q_EMIT fontSelected(d->m_selFont); 0110 } 0111 0112 void KFontRequester::setSampleText(const QString &text) 0113 { 0114 d->m_sampleText = text; 0115 d->displaySampleText(); 0116 } 0117 0118 void KFontRequester::setTitle(const QString &title) 0119 { 0120 d->m_title = title; 0121 d->setToolTip(); 0122 } 0123 0124 void KFontRequesterPrivate::buttonClicked() 0125 { 0126 KFontChooser::DisplayFlags flags = m_onlyFixed ? KFontChooser::FixedFontsOnly : KFontChooser::NoDisplayFlags; 0127 0128 const int result = KFontChooserDialog::getFont(m_selFont, flags, q->parentWidget()); 0129 0130 if (result == QDialog::Accepted) { 0131 displaySampleText(); 0132 Q_EMIT q->fontSelected(m_selFont); 0133 } 0134 } 0135 0136 void KFontRequesterPrivate::displaySampleText() 0137 { 0138 m_sampleLabel->setFont(m_selFont); 0139 0140 qreal size = m_selFont.pointSizeF(); 0141 if (size == -1) { 0142 size = m_selFont.pixelSize(); 0143 } 0144 0145 if (m_sampleText.isEmpty()) { 0146 QString family = translateFontName(m_selFont.family()); 0147 m_sampleLabel->setText(QStringLiteral("%1 %2").arg(family).arg(size)); 0148 } else { 0149 m_sampleLabel->setText(m_sampleText); 0150 } 0151 } 0152 0153 void KFontRequesterPrivate::setToolTip() 0154 { 0155 m_button->setToolTip(tr("Choose font...", "@info:tooltip")); 0156 0157 m_sampleLabel->setToolTip(QString()); 0158 m_sampleLabel->setWhatsThis(QString()); 0159 0160 if (m_title.isNull()) { 0161 m_sampleLabel->setToolTip(tr("Preview of the selected font", "@info:tooltip")); 0162 m_sampleLabel->setWhatsThis( 0163 tr("This is a preview of the selected font. You can change it" 0164 " by clicking the \"Choose Font...\" button.", 0165 "@info:whatsthis")); 0166 } else { 0167 m_sampleLabel->setToolTip(tr("Preview of the \"%1\" font", "@info:tooltip").arg(m_title)); 0168 m_sampleLabel->setWhatsThis(tr("This is a preview of the \"%1\" font. You can change it" 0169 " by clicking the \"Choose Font...\" button.", 0170 "@info:whatsthis") 0171 .arg(m_title)); 0172 } 0173 } 0174 0175 #include "moc_kfontrequester.cpp"