SVN_SILENT made messages (after extraction)
[kdepim.git] / kalarm / fontcolour.cpp
blob68d54ee57eb6e3f2cbe26763ef90107919937571
1 /*
2 * fontcolour.cpp - font and colour chooser widget
3 * Program: kalarm
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"
24 #include "checkbox.h"
25 #include "fontcolour.h"
27 #include <kfontchooser.h>
29 #include <QGroupBox>
30 #include <QPushButton>
31 #include <QLabel>
32 #include <QVBoxLayout>
33 #include <QHBoxLayout>
34 #include <QStyle>
37 FontColourChooser::FontColourChooser(QWidget* parent, const QStringList& fontList,
38 const QString& frameLabel, bool fg, bool defaultFont, int visibleListSize)
39 : QWidget(parent),
40 mFgColourButton(Q_NULLPTR),
41 mReadOnly(false)
43 QVBoxLayout* topLayout = new QVBoxLayout(this);
44 topLayout->setSpacing(style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing));
45 QWidget* page = this;
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);
60 if (fg)
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();
94 if (defaultFont)
96 QHBoxLayout* layout = new QHBoxLayout();
97 layout->setMargin(0);
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
106 else
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()
121 if (mDefaultFont)
122 mDefaultFont->setChecked(true);
125 void FontColourChooser::setFont(const QFont& font, bool onlyFixed)
127 if (mDefaultFont)
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
163 if (mFgColourButton)
164 return mFgColourButton->color();
165 else
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)
185 if (mFgColourButton)
187 mFgColourButton->setColor(colour);
188 mFontChooser->setColor(colour);
192 void FontColourChooser::setReadOnly(bool ro)
194 if (ro != mReadOnly)
196 mReadOnly = ro;
197 if (mFgColourButton)
198 mFgColourButton->setReadOnly(ro);
199 mBgColourButton->setReadOnly(ro);
200 mDefaultFont->setReadOnly(ro);
204 bool FontColourChooser::eventFilter(QObject*, QEvent* e)
206 if (mReadOnly)
208 switch (e->type())
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
217 default:
218 break;
221 return false;
224 void FontColourChooser::slotDefaultFontToggled(bool on)
226 mFontChooser->setEnabled(!on);
230 // vim: et sw=4: