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.moc"
28 #include <kfontchooser.h>
29 #include <kfontdialog.h>
30 #include <kcolordialog.h>
34 #include <QPushButton>
36 #include <QVBoxLayout>
37 #include <QHBoxLayout>
40 FontColourChooser::FontColourChooser(QWidget
*parent
,
41 const QStringList
&fontList
, const QString
& frameLabel
, bool fg
, bool defaultFont
, int visibleListSize
)
46 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
47 topLayout
->setMargin(0);
48 topLayout
->setSpacing(KDialog::spacingHint());
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
);
66 KHBox
* box
= new KHBox(page
); // to group widgets for QWhatsThis text
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
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();
94 QHBoxLayout
* layout
= new QHBoxLayout();
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
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()
120 mDefaultFont
->setChecked(true);
123 void FontColourChooser::setFont(const QFont
& font
, bool onlyFixed
)
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
162 return mFgColourButton
->color();
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
)
185 mFgColourButton
->setColor(colour
);
186 mFontChooser
->setColor(colour
);
190 void FontColourChooser::setReadOnly(bool ro
)
196 mFgColourButton
->setReadOnly(ro
);
197 mBgColourButton
->setReadOnly(ro
);
198 mDefaultFont
->setReadOnly(ro
);
202 bool FontColourChooser::eventFilter(QObject
*, QEvent
* e
)
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
222 void FontColourChooser::slotDefaultFontToggled(bool on
)
224 mFontChooser
->setEnabled(!on
);