2 * fontcolour.cpp - font and colour chooser widget
4 * Copyright © 2001-2009 by David Jarvie <djarvie@kde.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "kalarmapp.h"
22 #include "preferences.h"
23 #include "colourbutton.h"
25 #include "fontcolour.h"
27 #include <kfontchooser.h>
30 #include <QPushButton>
32 #include <QVBoxLayout>
33 #include <QHBoxLayout>
37 FontColourChooser::FontColourChooser(QWidget
* parent
, const QStringList
& fontList
,
38 const QString
& frameLabel
, bool fg
, bool defaultFont
, int visibleListSize
)
40 mFgColourButton(Q_NULLPTR
),
43 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
44 topLayout
->setSpacing(style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing
));
46 if (!frameLabel
.isNull())
48 page
= new QGroupBox(frameLabel
, this);
49 topLayout
->addWidget(page
);
50 topLayout
= new QVBoxLayout(page
);
51 topLayout
->setMargin(style()->pixelMetric(QStyle::PM_DefaultChildMargin
));
52 topLayout
->setSpacing(style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing
));
54 QHBoxLayout
* hlayout
= new QHBoxLayout();
55 hlayout
->setMargin(0);
56 topLayout
->addLayout(hlayout
);
57 QVBoxLayout
* colourLayout
= new QVBoxLayout();
58 colourLayout
->setMargin(0);
59 hlayout
->addLayout(colourLayout
);
62 QWidget
* box
= new QWidget(page
); // to group widgets for QWhatsThis text
63 colourLayout
->addWidget(box
);
64 QHBoxLayout
* boxHLayout
= new QHBoxLayout(box
);
65 boxHLayout
->setMargin(0);
66 boxHLayout
->setSpacing(style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing
) / 2);
68 QLabel
* label
= new QLabel(i18nc("@label:listbox", "Foreground color:"), box
);
69 boxHLayout
->addWidget(label
);
70 boxHLayout
->setStretchFactor(new QWidget(box
), 0);
71 mFgColourButton
= new ColourButton(box
);
72 boxHLayout
->addWidget(mFgColourButton
);
73 connect(mFgColourButton
, &ColourButton::changed
, this, &FontColourChooser::setSampleColour
);
74 label
->setBuddy(mFgColourButton
);
75 box
->setWhatsThis(i18nc("@info:whatsthis", "Select the alarm message foreground color"));
78 QWidget
* box
= new QWidget(page
); // to group widgets for QWhatsThis text
79 colourLayout
->addWidget(box
);
80 QHBoxLayout
* boxHLayout
= new QHBoxLayout(box
);
81 boxHLayout
->setMargin(0);
82 boxHLayout
->setSpacing(style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing
) / 2);
84 QLabel
* label
= new QLabel(i18nc("@label:listbox", "Background color:"), box
);
85 boxHLayout
->addWidget(label
);
86 boxHLayout
->setStretchFactor(new QWidget(box
), 0);
87 mBgColourButton
= new ColourButton(box
);
88 boxHLayout
->addWidget(mBgColourButton
);
89 connect(mBgColourButton
, &ColourButton::changed
, this, &FontColourChooser::setSampleColour
);
90 label
->setBuddy(mBgColourButton
);
91 box
->setWhatsThis(i18nc("@info:whatsthis", "Select the alarm message background color"));
92 hlayout
->addStretch();
96 QHBoxLayout
* layout
= new QHBoxLayout();
98 topLayout
->addLayout(layout
);
99 mDefaultFont
= new CheckBox(i18nc("@option:check", "Use default font"), page
);
100 mDefaultFont
->setMinimumSize(mDefaultFont
->sizeHint());
101 connect(mDefaultFont
, &CheckBox::toggled
, this, &FontColourChooser::slotDefaultFontToggled
);
102 mDefaultFont
->setWhatsThis(i18nc("@info:whatsthis", "Check to use the default font current at the time the alarm is displayed."));
103 layout
->addWidget(mDefaultFont
);
104 layout
->addWidget(new QWidget(page
)); // left adjust the widget
107 mDefaultFont
= Q_NULLPTR
;
109 mFontChooser
= new KFontChooser(page
, KFontChooser::DisplayFrame
, fontList
, visibleListSize
);
110 mFontChooser
->installEventFilter(this); // for read-only mode
111 QList
<QWidget
*> kids
= mFontChooser
->findChildren
<QWidget
*>();
112 for (int i
= 0, end
= kids
.count(); i
< end
; ++i
)
113 kids
[i
]->installEventFilter(this);
114 topLayout
->addWidget(mFontChooser
);
116 slotDefaultFontToggled(false);
119 void FontColourChooser::setDefaultFont()
122 mDefaultFont
->setChecked(true);
125 void FontColourChooser::setFont(const QFont
& font
, bool onlyFixed
)
128 mDefaultFont
->setChecked(false);
129 mFontChooser
->setFont(font
, onlyFixed
);
132 bool FontColourChooser::defaultFont() const
134 return mDefaultFont
? mDefaultFont
->isChecked() : false;
137 QFont
FontColourChooser::font() const
139 return (mDefaultFont
&& mDefaultFont
->isChecked()) ? QFont() : mFontChooser
->font();
142 void FontColourChooser::setBgColour(const QColor
& colour
)
144 mBgColourButton
->setColor(colour
);
145 mFontChooser
->setBackgroundColor(colour
);
148 void FontColourChooser::setSampleColour()
150 QColor bg
= mBgColourButton
->color();
151 mFontChooser
->setBackgroundColor(bg
);
152 QColor fg
= fgColour();
153 mFontChooser
->setColor(fg
);
156 QColor
FontColourChooser::bgColour() const
158 return mBgColourButton
->color();
161 QColor
FontColourChooser::fgColour() const
164 return mFgColourButton
->color();
167 QColor bg
= mBgColourButton
->color();
168 QPalette
pal(bg
, bg
);
169 return pal
.color(QPalette::Active
, QPalette::Text
);
173 QString
FontColourChooser::sampleText() const
175 return mFontChooser
->sampleText();
178 void FontColourChooser::setSampleText(const QString
& text
)
180 mFontChooser
->setSampleText(text
);
183 void FontColourChooser::setFgColour(const QColor
& colour
)
187 mFgColourButton
->setColor(colour
);
188 mFontChooser
->setColor(colour
);
192 void FontColourChooser::setReadOnly(bool ro
)
198 mFgColourButton
->setReadOnly(ro
);
199 mBgColourButton
->setReadOnly(ro
);
200 mDefaultFont
->setReadOnly(ro
);
204 bool FontColourChooser::eventFilter(QObject
*, QEvent
* e
)
210 case QEvent::MouseMove
:
211 case QEvent::MouseButtonPress
:
212 case QEvent::MouseButtonRelease
:
213 case QEvent::MouseButtonDblClick
:
214 case QEvent::KeyPress
:
215 case QEvent::KeyRelease
:
216 return true; // prevent the event being handled
224 void FontColourChooser::slotDefaultFontToggled(bool on
)
226 mFontChooser
->setEnabled(!on
);