Warning, file /pim/kalarm/src/fontcolour.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  fontcolour.cpp  -  font and colour chooser widget
0003  *  Program:  kalarm
0004  *  SPDX-FileCopyrightText: 2001-2023 David Jarvie <djarvie@kde.org>
0005  *
0006  *  SPDX-License-Identifier: GPL-2.0-or-later
0007  */
0008 
0009 #include "fontcolour.h"
0010 
0011 #include "lib/colourbutton.h"
0012 #include "lib/checkbox.h"
0013 
0014 #include <KFontChooser>
0015 
0016 #include <QEvent>
0017 #include <QGroupBox>
0018 #include <QLabel>
0019 #include <QHBoxLayout>
0020 #include <QVBoxLayout>
0021 #include <QStyle>
0022 
0023 
0024 FontColourChooser::FontColourChooser(QWidget* parent, const QStringList& fontList,
0025            const QString& frameLabel, bool fg, bool defaultFont, int visibleListSize)
0026     : QWidget(parent)
0027 {
0028     auto topLayout = new QVBoxLayout(this);
0029     QWidget* page = this;
0030     if (!frameLabel.isNull())
0031     {
0032         topLayout->setContentsMargins(0, style()->pixelMetric(QStyle::PM_LayoutTopMargin),
0033                                       0, style()->pixelMetric(QStyle::PM_LayoutBottomMargin));
0034         page = new QGroupBox(frameLabel, this);
0035         topLayout->addWidget(page);
0036         topLayout = new QVBoxLayout(page);
0037     }
0038     else
0039         topLayout->setContentsMargins(0, 0, 0, 0);
0040     auto hlayout = new QHBoxLayout();
0041     hlayout->setContentsMargins(0, 0, 0, 0);
0042     topLayout->addLayout(hlayout);
0043     auto colourLayout = new QVBoxLayout();
0044     colourLayout->setContentsMargins(0, 0, 0, 0);
0045     hlayout->addLayout(colourLayout);
0046     if (fg)
0047     {
0048         QWidget* box = new QWidget(page);    // to group widgets for QWhatsThis text
0049         colourLayout->addWidget(box);
0050         auto boxHLayout = new QHBoxLayout(box);
0051         boxHLayout->setContentsMargins(0, 0, 0, 0);
0052 
0053         QLabel* label = new QLabel(i18nc("@label:listbox", "Foreground color:"), box);
0054         boxHLayout->addWidget(label);
0055         boxHLayout->setStretchFactor(new QWidget(box), 0);
0056         mFgColourButton = new ColourButton(box);
0057         boxHLayout->addWidget(mFgColourButton);
0058         connect(mFgColourButton, &ColourButton::changed, this, &FontColourChooser::setSampleColour);
0059         label->setBuddy(mFgColourButton);
0060         box->setWhatsThis(i18nc("@info:whatsthis", "Select the alarm message foreground color"));
0061     }
0062 
0063     QWidget* box = new QWidget(page);    // to group widgets for QWhatsThis text
0064     colourLayout->addWidget(box);
0065     auto boxHLayout = new QHBoxLayout(box);
0066     boxHLayout->setContentsMargins(0, 0, 0, 0);
0067 
0068     QLabel* label = new QLabel(i18nc("@label:listbox", "Background color:"), box);
0069     boxHLayout->addWidget(label);
0070     boxHLayout->setStretchFactor(new QWidget(box), 0);
0071     mBgColourButton = new ColourButton(box);
0072     boxHLayout->addWidget(mBgColourButton);
0073     connect(mBgColourButton, &ColourButton::changed, this, &FontColourChooser::setSampleColour);
0074     label->setBuddy(mBgColourButton);
0075     box->setWhatsThis(i18nc("@info:whatsthis", "Select the alarm message background color"));
0076     hlayout->addStretch();
0077 
0078     if (defaultFont)
0079     {
0080         auto layout = new QHBoxLayout();
0081         layout->setContentsMargins(0, 0, 0, 0);
0082         topLayout->addLayout(layout);
0083         mDefaultFont = new CheckBox(i18nc("@option:check", "Use default font"), page);
0084         mDefaultFont->setMinimumSize(mDefaultFont->sizeHint());
0085         connect(mDefaultFont, &CheckBox::toggled, this, &FontColourChooser::slotDefaultFontToggled);
0086         mDefaultFont->setWhatsThis(i18nc("@info:whatsthis", "Check to use the default font current at the time the alarm is displayed."));
0087         layout->addWidget(mDefaultFont);
0088         layout->addWidget(new QWidget(page));    // left adjust the widget
0089     }
0090 
0091     mFontChooser = new KFontChooser(KFontChooser::DisplayFrame, page);
0092     mFontChooser->setFontListItems(fontList);
0093     mFontChooser->setMinVisibleItems(visibleListSize);
0094 
0095     mFontChooser->installEventFilter(this);   // for read-only mode
0096     QList<QWidget*> kids = mFontChooser->findChildren<QWidget*>();
0097     for (int i = 0, end = kids.count();  i < end;  ++i)
0098         kids[i]->installEventFilter(this);
0099     topLayout->addWidget(mFontChooser);
0100 
0101     slotDefaultFontToggled(false);
0102 }
0103 
0104 void FontColourChooser::setFont(const QFont& font, bool defaultFont, bool onlyFixed)
0105 {
0106     if (mDefaultFont)
0107         mDefaultFont->setChecked(defaultFont);
0108     mFontChooser->setFont(font, onlyFixed);
0109 }
0110 
0111 bool FontColourChooser::defaultFont() const
0112 {
0113     return mDefaultFont ? mDefaultFont->isChecked() : false;
0114 }
0115 
0116 QFont FontColourChooser::font() const
0117 {
0118     return (mDefaultFont && mDefaultFont->isChecked()) ? QFont() : mFontChooser->font();
0119 }
0120 
0121 void FontColourChooser::setBgColour(const QColor& colour)
0122 {
0123     mBgColourButton->setColor(colour);
0124     mFontChooser->setBackgroundColor(colour);
0125 }
0126 
0127 void FontColourChooser::setSampleColour()
0128 {
0129     QColor bg = mBgColourButton->color();
0130     mFontChooser->setBackgroundColor(bg);
0131     QColor fg = fgColour();
0132     mFontChooser->setColor(fg);
0133 }
0134 
0135 QColor FontColourChooser::bgColour() const
0136 {
0137     return mBgColourButton->color();
0138 }
0139 
0140 QColor FontColourChooser::fgColour() const
0141 {
0142     if (mFgColourButton)
0143         return mFgColourButton->color();
0144     else
0145     {
0146         QColor bg = mBgColourButton->color();
0147         QPalette pal(bg, bg);
0148         return pal.color(QPalette::Active, QPalette::Text);
0149     }
0150 }
0151 
0152 QString FontColourChooser::sampleText() const
0153 {
0154     return mFontChooser->sampleText();
0155 }
0156 
0157 void FontColourChooser::setSampleText(const QString& text)
0158 {
0159     mFontChooser->setSampleText(text);
0160 }
0161 
0162 void FontColourChooser::setFgColour(const QColor& colour)
0163 {
0164     if (mFgColourButton)
0165     {
0166         mFgColourButton->setColor(colour);
0167         mFontChooser->setColor(colour);
0168     }
0169 }
0170 
0171 void FontColourChooser::setReadOnly(bool ro)
0172 {
0173     if (ro != mReadOnly)
0174     {
0175         mReadOnly = ro;
0176         if (mFgColourButton)
0177             mFgColourButton->setReadOnly(ro);
0178         mBgColourButton->setReadOnly(ro);
0179         mDefaultFont->setReadOnly(ro);
0180     }
0181 }
0182 
0183 bool FontColourChooser::eventFilter(QObject*, QEvent* e)
0184 {
0185     if (mReadOnly)
0186     {
0187         switch (e->type())
0188         {
0189             case QEvent::MouseMove:
0190             case QEvent::MouseButtonPress:
0191             case QEvent::MouseButtonRelease:
0192             case QEvent::MouseButtonDblClick:
0193             case QEvent::KeyPress:
0194             case QEvent::KeyRelease:
0195                 return true;   // prevent the event being handled
0196             default:
0197                 break;
0198         }
0199     }
0200     return false;
0201 }
0202 
0203 void FontColourChooser::slotDefaultFontToggled(bool on)
0204 {
0205     mFontChooser->setEnabled(!on);
0206 }
0207 
0208 #include "moc_fontcolour.cpp"
0209 
0210 // vim: et sw=4: