File indexing completed on 2025-04-27 03:58:23
0001 /* ============================================================ 0002 * 0003 * This file is a part of digiKam project 0004 * https://www.digikam.org 0005 * 0006 * Date : 2008-12-23 0007 * Description : a widget to select between system font or a custom font. 0008 * 0009 * SPDX-FileCopyrightText: 2008-2024 by Gilles Caulier <caulier dot gilles at gmail dot com> 0010 * 0011 * SPDX-License-Identifier: GPL-2.0-or-later 0012 * 0013 * ============================================================ */ 0014 0015 #include "dfontselect.h" 0016 0017 // Qt includes 0018 0019 #include <QLabel> 0020 #include <QEvent> 0021 #include <QPushButton> 0022 #include <QFontDatabase> 0023 #include <QApplication> 0024 #include <QStyle> 0025 #include <QComboBox> 0026 #include <QFontDialog> 0027 0028 // KDE includes 0029 0030 #include <klocalizedstring.h> 0031 0032 // Local includes 0033 0034 #include "dexpanderbox.h" 0035 0036 namespace Digikam 0037 { 0038 0039 class Q_DECL_HIDDEN DFontSelect::Private 0040 { 0041 public: 0042 0043 explicit Private() 0044 : space (nullptr), 0045 label (nullptr), 0046 desc (nullptr), 0047 chooseFontButton(nullptr), 0048 modeCombo (nullptr), 0049 mode (DFontSelect::SystemFont) 0050 { 0051 } 0052 0053 QWidget* space; 0054 QLabel* label; 0055 DAdjustableLabel* desc; 0056 0057 QFont font; 0058 0059 QPushButton* chooseFontButton; 0060 0061 QComboBox* modeCombo; 0062 0063 DFontSelect::FontMode mode; 0064 }; 0065 0066 DFontSelect::DFontSelect(const QString& text, QWidget* const parent) 0067 : DHBox(parent), 0068 d (new Private) 0069 { 0070 d->label = new QLabel(this); 0071 d->label->setText(text); 0072 d->space = new QWidget(this); 0073 0074 if (text.isEmpty()) 0075 { 0076 d->label->hide(); 0077 d->space->hide(); 0078 } 0079 0080 d->modeCombo = new QComboBox(this); 0081 d->modeCombo->addItem(i18n("System Font")); 0082 d->modeCombo->addItem(i18n("Custom Font")); 0083 0084 d->chooseFontButton = new QPushButton(i18n("Choose..."), this); 0085 d->desc = new DAdjustableLabel(this); 0086 0087 setSpacing(qMin(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing), 0088 QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing))); 0089 setContentsMargins(QMargins()); 0090 setStretchFactor(d->space, 10); 0091 0092 connect(d->modeCombo, SIGNAL(activated(int)), 0093 this, SLOT(slotChangeMode(int))); 0094 0095 connect(d->chooseFontButton, SIGNAL(clicked()), 0096 this, SLOT(slotOpenFontDialog())); 0097 0098 slotChangeMode(d->modeCombo->currentIndex()); 0099 } 0100 0101 DFontSelect::~DFontSelect() 0102 { 0103 delete d; 0104 } 0105 0106 void DFontSelect::setMode(FontMode mode) 0107 { 0108 d->mode = mode; 0109 d->modeCombo->setCurrentIndex(d->mode); 0110 d->desc->setAdjustedText(QString::fromLatin1("%1 - %2").arg(font().family()).arg(font().pointSize())); 0111 d->chooseFontButton->setEnabled(d->mode == CustomFont); 0112 } 0113 0114 DFontSelect::FontMode DFontSelect::mode() const 0115 { 0116 return d->mode; 0117 } 0118 0119 QFont DFontSelect::font() const 0120 { 0121 return (d->mode == CustomFont) ? d->font 0122 : QFontDatabase::systemFont(QFontDatabase::GeneralFont); 0123 } 0124 0125 void DFontSelect::setFont(const QFont& font) 0126 { 0127 d->font = font; 0128 0129 if (d->font == QFontDatabase::systemFont(QFontDatabase::GeneralFont)) 0130 { 0131 setMode(SystemFont); 0132 } 0133 else 0134 { 0135 setMode(CustomFont); 0136 } 0137 } 0138 0139 bool DFontSelect::event(QEvent* e) 0140 { 0141 if (e->type() == QEvent::Polish) 0142 { 0143 d->modeCombo->setFont(font()); 0144 } 0145 0146 return DHBox::event(e); 0147 } 0148 0149 void DFontSelect::slotOpenFontDialog() 0150 { 0151 bool ok = false; 0152 QFont f = QFontDialog::getFont(&ok, font(), this); 0153 0154 if (ok) 0155 { 0156 setFont(f); 0157 Q_EMIT signalFontChanged(); 0158 } 0159 } 0160 0161 void DFontSelect::slotChangeMode(int index) 0162 { 0163 setMode((index == CustomFont) ? CustomFont : SystemFont); 0164 Q_EMIT signalFontChanged(); 0165 } 0166 0167 } // namespace Digikam 0168 0169 #include "moc_dfontselect.cpp"