The concept of local folders doesn't apply any more.
[kdepim.git] / kalarm / fontcolour.cpp
blobb5dbaf76ef109979650c1cc33a94ad6f11265b8a
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.moc"
27 #include <kglobal.h>
28 #include <kfontchooser.h>
29 #include <kfontdialog.h>
30 #include <kcolordialog.h>
31 #include <khbox.h>
33 #include <QGroupBox>
34 #include <QPushButton>
35 #include <QLabel>
36 #include <QVBoxLayout>
37 #include <QHBoxLayout>
40 FontColourChooser::FontColourChooser(QWidget *parent,
41 const QStringList &fontList, const QString& frameLabel, bool fg, bool defaultFont, int visibleListSize)
42 : QWidget(parent),
43 mFgColourButton(0),
44 mReadOnly(false)
46 QVBoxLayout* topLayout = new QVBoxLayout(this);
47 topLayout->setMargin(0);
48 topLayout->setSpacing(KDialog::spacingHint());
49 QWidget* page = this;
50 if (!frameLabel.isNull())
52 page = new QGroupBox(frameLabel, this);
53 topLayout->addWidget(page);
54 topLayout = new QVBoxLayout(page);
55 topLayout->setMargin(KDialog::marginHint());
56 topLayout->setSpacing(KDialog::spacingHint());
58 QHBoxLayout* hlayout = new QHBoxLayout();
59 hlayout->setMargin(0);
60 topLayout->addLayout(hlayout);
61 QVBoxLayout* colourLayout = new QVBoxLayout();
62 colourLayout->setMargin(0);
63 hlayout->addLayout(colourLayout);
64 if (fg)
66 KHBox* box = new KHBox(page); // to group widgets for QWhatsThis text
67 box->setMargin(0);
68 box->setSpacing(KDialog::spacingHint()/2);
69 colourLayout->addWidget(box);
71 QLabel* label = new QLabel(i18nc("@label:listbox", "Foreground color:"), box);
72 box->setStretchFactor(new QWidget(box), 0);
73 mFgColourButton = new ColourButton(box);
74 connect(mFgColourButton, SIGNAL(changed(QColor)), SLOT(setSampleColour()));
75 label->setBuddy(mFgColourButton);
76 box->setWhatsThis(i18nc("@info:whatsthis", "Select the alarm message foreground color"));
79 KHBox* box = new KHBox(page); // to group widgets for QWhatsThis text
80 box->setMargin(0);
81 box->setSpacing(KDialog::spacingHint()/2);
82 colourLayout->addWidget(box);
84 QLabel* label = new QLabel(i18nc("@label:listbox", "Background color:"), box);
85 box->setStretchFactor(new QWidget(box), 0);
86 mBgColourButton = new ColourButton(box);
87 connect(mBgColourButton, SIGNAL(changed(QColor)), SLOT(setSampleColour()));
88 label->setBuddy(mBgColourButton);
89 box->setWhatsThis(i18nc("@info:whatsthis", "Select the alarm message background color"));
90 hlayout->addStretch();
92 if (defaultFont)
94 QHBoxLayout* layout = new QHBoxLayout();
95 layout->setMargin(0);
96 topLayout->addLayout(layout);
97 mDefaultFont = new CheckBox(i18nc("@option:check", "Use default font"), page);
98 mDefaultFont->setMinimumSize(mDefaultFont->sizeHint());
99 connect(mDefaultFont, SIGNAL(toggled(bool)), SLOT(slotDefaultFontToggled(bool)));
100 mDefaultFont->setWhatsThis(i18nc("@info:whatsthis", "Check to use the default font current at the time the alarm is displayed."));
101 layout->addWidget(mDefaultFont);
102 layout->addWidget(new QWidget(page)); // left adjust the widget
104 else
105 mDefaultFont = 0;
107 mFontChooser = new KFontChooser(page, KFontChooser::DisplayFrame, fontList, visibleListSize);
108 mFontChooser->installEventFilter(this); // for read-only mode
109 QList<QWidget*> kids = mFontChooser->findChildren<QWidget*>();
110 for (int i = 0, end = kids.count(); i < end; ++i)
111 kids[i]->installEventFilter(this);
112 topLayout->addWidget(mFontChooser);
114 slotDefaultFontToggled(false);
117 void FontColourChooser::setDefaultFont()
119 if (mDefaultFont)
120 mDefaultFont->setChecked(true);
123 void FontColourChooser::setFont(const QFont& font, bool onlyFixed)
125 if (mDefaultFont)
126 mDefaultFont->setChecked(false);
127 mFontChooser->setFont(font, onlyFixed);
130 bool FontColourChooser::defaultFont() const
132 return mDefaultFont ? mDefaultFont->isChecked() : false;
135 QFont FontColourChooser::font() const
137 return (mDefaultFont && mDefaultFont->isChecked()) ? QFont() : mFontChooser->font();
140 void FontColourChooser::setBgColour(const QColor& colour)
142 mBgColourButton->setColor(colour);
143 mFontChooser->setBackgroundColor(colour);
146 void FontColourChooser::setSampleColour()
148 QColor bg = mBgColourButton->color();
149 mFontChooser->setBackgroundColor(bg);
150 QColor fg = fgColour();
151 mFontChooser->setColor(fg);
154 QColor FontColourChooser::bgColour() const
156 return mBgColourButton->color();
159 QColor FontColourChooser::fgColour() const
161 if (mFgColourButton)
162 return mFgColourButton->color();
163 else
165 QColor bg = mBgColourButton->color();
166 QPalette pal(bg, bg);
167 return pal.color(QPalette::Active, QPalette::Text);
171 QString FontColourChooser::sampleText() const
173 return mFontChooser->sampleText();
176 void FontColourChooser::setSampleText(const QString& text)
178 mFontChooser->setSampleText(text);
181 void FontColourChooser::setFgColour(const QColor& colour)
183 if (mFgColourButton)
185 mFgColourButton->setColor(colour);
186 mFontChooser->setColor(colour);
190 void FontColourChooser::setReadOnly(bool ro)
192 if (ro != mReadOnly)
194 mReadOnly = ro;
195 if (mFgColourButton)
196 mFgColourButton->setReadOnly(ro);
197 mBgColourButton->setReadOnly(ro);
198 mDefaultFont->setReadOnly(ro);
202 bool FontColourChooser::eventFilter(QObject*, QEvent* e)
204 if (mReadOnly)
206 switch (e->type())
208 case QEvent::MouseMove:
209 case QEvent::MouseButtonPress:
210 case QEvent::MouseButtonRelease:
211 case QEvent::MouseButtonDblClick:
212 case QEvent::KeyPress:
213 case QEvent::KeyRelease:
214 return true; // prevent the event being handled
215 default:
216 break;
219 return false;
222 void FontColourChooser::slotDefaultFontToggled(bool on)
224 mFontChooser->setEnabled(!on);
227 // vim: et sw=4: