kalarm

fontcolour.cpp

00001 /*
00002  *  fontcolour.cpp  -  font and colour chooser widget
00003  *  Program:  kalarm
00004  *  Copyright © 2001-2003,2005,2008 by David Jarvie <software@astrojar.org.uk>
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License along
00017  *  with this program; if not, write to the Free Software Foundation, Inc.,
00018  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #include <qobjectlist.h>
00022 #include <qwidget.h>
00023 #include <qgroupbox.h>
00024 #include <qpushbutton.h>
00025 #include <qhbox.h>
00026 #include <qlabel.h>
00027 #include <qlayout.h>
00028 #include <qwhatsthis.h>
00029 
00030 #include <kglobal.h>
00031 #include <klocale.h>
00032 #include <kcolordialog.h>
00033 
00034 #include "kalarmapp.h"
00035 #include "preferences.h"
00036 #include "colourcombo.h"
00037 #include "checkbox.h"
00038 #include "fontcolour.moc"
00039 
00040 
00041 FontColourChooser::FontColourChooser(QWidget *parent, const char *name,
00042           bool onlyFixed, const QStringList &fontList,
00043           const QString& frameLabel, bool editColours, bool fg, bool defaultFont,
00044           int visibleListSize)
00045     : QWidget(parent, name),
00046       mFgColourButton(0),
00047       mRemoveColourButton(0),
00048       mColourList(Preferences::messageColours()),
00049       mReadOnly(false)
00050 {
00051     QVBoxLayout* topLayout = new QVBoxLayout(this, 0, KDialog::spacingHint());
00052     QWidget* page = this;
00053     if (!frameLabel.isNull())
00054     {
00055         page = new QGroupBox(frameLabel, this);
00056         topLayout->addWidget(page);
00057         topLayout = new QVBoxLayout(page, KDialog::marginHint(), KDialog::spacingHint());
00058         topLayout->addSpacing(fontMetrics().height() - KDialog::marginHint() + KDialog::spacingHint());
00059     }
00060     QHBoxLayout* hlayout = new QHBoxLayout(topLayout);
00061     QVBoxLayout* colourLayout = new QVBoxLayout(hlayout);
00062     if (fg)
00063     {
00064         QHBox* box = new QHBox(page);    // to group widgets for QWhatsThis text
00065         box->setSpacing(KDialog::spacingHint()/2);
00066         colourLayout->addWidget(box);
00067 
00068         QLabel* label = new QLabel(i18n("&Foreground color:"), box);
00069         box->setStretchFactor(new QWidget(box), 0);
00070         mFgColourButton = new ColourCombo(box);
00071         connect(mFgColourButton, SIGNAL(activated(const QString&)), SLOT(setSampleColour()));
00072         label->setBuddy(mFgColourButton);
00073         QWhatsThis::add(box, i18n("Select the alarm message foreground color"));
00074     }
00075 
00076     QHBox* box = new QHBox(page);    // to group widgets for QWhatsThis text
00077     box->setSpacing(KDialog::spacingHint()/2);
00078     colourLayout->addWidget(box);
00079 
00080     QLabel* label = new QLabel(i18n("&Background color:"), box);
00081     box->setStretchFactor(new QWidget(box), 0);
00082     mBgColourButton = new ColourCombo(box);
00083     connect(mBgColourButton, SIGNAL(activated(const QString&)), SLOT(setSampleColour()));
00084     label->setBuddy(mBgColourButton);
00085     QWhatsThis::add(box, i18n("Select the alarm message background color"));
00086     hlayout->addStretch();
00087 
00088     if (editColours)
00089     {
00090         QHBoxLayout* layout = new QHBoxLayout(topLayout);
00091         QPushButton* button = new QPushButton(i18n("Add Co&lor..."), page);
00092         button->setFixedSize(button->sizeHint());
00093         connect(button, SIGNAL(clicked()), SLOT(slotAddColour()));
00094         QWhatsThis::add(button, i18n("Choose a new color to add to the color selection list."));
00095         layout->addWidget(button);
00096 
00097         mRemoveColourButton = new QPushButton(i18n("&Remove Color"), page);
00098         mRemoveColourButton->setFixedSize(mRemoveColourButton->sizeHint());
00099         connect(mRemoveColourButton, SIGNAL(clicked()), SLOT(slotRemoveColour()));
00100         QWhatsThis::add(mRemoveColourButton,
00101               i18n("Remove the color currently shown in the background color chooser, from the color selection list."));
00102         layout->addWidget(mRemoveColourButton);
00103     }
00104 
00105     if (defaultFont)
00106     {
00107         QHBoxLayout* layout = new QHBoxLayout(topLayout);
00108         mDefaultFont = new CheckBox(i18n("Use &default font"), page);
00109         mDefaultFont->setMinimumSize(mDefaultFont->sizeHint());
00110         connect(mDefaultFont, SIGNAL(toggled(bool)), SLOT(slotDefaultFontToggled(bool)));
00111         QWhatsThis::add(mDefaultFont,
00112               i18n("Check to use the default font current at the time the alarm is displayed."));
00113         layout->addWidget(mDefaultFont);
00114         layout->addWidget(new QWidget(page));    // left adjust the widget
00115     }
00116     else
00117         mDefaultFont = 0;
00118 
00119     mFontChooser = new KFontChooser(page, name, onlyFixed, fontList, false, visibleListSize);
00120     mFontChooser->installEventFilter(this);   // for read-only mode
00121     const QObjectList* kids = mFontChooser->queryList();
00122     for (QObjectList::ConstIterator it = kids->constBegin();  it != kids->constEnd();  ++it)
00123         (*it)->installEventFilter(this);
00124     topLayout->addWidget(mFontChooser);
00125 
00126     slotDefaultFontToggled(false);
00127 }
00128 
00129 void FontColourChooser::setDefaultFont()
00130 {
00131     if (mDefaultFont)
00132         mDefaultFont->setChecked(true);
00133 }
00134 
00135 void FontColourChooser::setFont(const QFont& font, bool onlyFixed)
00136 {
00137     if (mDefaultFont)
00138         mDefaultFont->setChecked(false);
00139     mFontChooser->setFont(font, onlyFixed);
00140 }
00141 
00142 bool FontColourChooser::defaultFont() const
00143 {
00144     return mDefaultFont ? mDefaultFont->isChecked() : false;
00145 }
00146 
00147 QFont FontColourChooser::font() const
00148 {
00149     return (mDefaultFont && mDefaultFont->isChecked()) ? QFont() : mFontChooser->font();
00150 }
00151 
00152 void FontColourChooser::setBgColour(const QColor& colour)
00153 {
00154     mBgColourButton->setColor(colour);
00155     mFontChooser->setBackgroundColor(colour);
00156 }
00157 
00158 void FontColourChooser::setSampleColour()
00159 {
00160     QColor bg = mBgColourButton->color();
00161     mFontChooser->setBackgroundColor(bg);
00162     QColor fg = fgColour();
00163     mFontChooser->setColor(fg);
00164     if (mRemoveColourButton)
00165         mRemoveColourButton->setEnabled(!mBgColourButton->isCustomColour());   // no deletion of custom colour
00166 }
00167 
00168 QColor FontColourChooser::bgColour() const
00169 {
00170     return mBgColourButton->color();
00171 }
00172 
00173 QColor FontColourChooser::fgColour() const
00174 {
00175     if (mFgColourButton)
00176         return mFgColourButton->color();
00177     else
00178     {
00179         QColor bg = mBgColourButton->color();
00180         QPalette pal(bg, bg);
00181         return pal.color(QPalette::Active, QColorGroup::Text);
00182     }
00183 }
00184 
00185 QString FontColourChooser::sampleText() const
00186 {
00187     return mFontChooser->sampleText();
00188 }
00189 
00190 void FontColourChooser::setSampleText(const QString& text)
00191 {
00192     mFontChooser->setSampleText(text);
00193 }
00194 
00195 void FontColourChooser::setFgColour(const QColor& colour)
00196 {
00197     if (mFgColourButton)
00198     {
00199         mFgColourButton->setColor(colour);
00200         mFontChooser->setColor(colour);
00201     }
00202 }
00203 
00204 void FontColourChooser::setReadOnly(bool ro)
00205 {
00206     if (ro != mReadOnly)
00207     {
00208         mReadOnly = ro;
00209         if (mFgColourButton)
00210             mFgColourButton->setReadOnly(ro);
00211         mBgColourButton->setReadOnly(ro);
00212         mDefaultFont->setReadOnly(ro);
00213     }
00214 }
00215 
00216 bool FontColourChooser::eventFilter(QObject*, QEvent* e)
00217 {
00218     if (mReadOnly)
00219     {
00220         switch (e->type())
00221         {
00222             case QEvent::MouseButtonPress:
00223             case QEvent::MouseButtonRelease:
00224             case QEvent::MouseButtonDblClick:
00225             case QEvent::KeyPress:
00226             case QEvent::KeyRelease:
00227                 return true;   // prevent the event being handled
00228             default:
00229                 break;
00230         }
00231     }
00232     return false;
00233 }
00234 
00235 void FontColourChooser::slotDefaultFontToggled(bool on)
00236 {
00237     mFontChooser->setEnabled(!on);
00238 }
00239 
00240 void FontColourChooser::setColours(const ColourList& colours)
00241 {
00242     mColourList = colours;
00243     mBgColourButton->setColours(mColourList);
00244     mFontChooser->setBackgroundColor(mBgColourButton->color());
00245 }
00246 
00247 void FontColourChooser::slotAddColour()
00248 {
00249     QColor colour;
00250     if (KColorDialog::getColor(colour, this) == QDialog::Accepted)
00251     {
00252         mColourList.insert(colour);
00253         mBgColourButton->setColours(mColourList);
00254     }
00255 }
00256 
00257 void FontColourChooser::slotRemoveColour()
00258 {
00259     if (!mBgColourButton->isCustomColour())
00260     {
00261         mColourList.remove(mBgColourButton->color());
00262         mBgColourButton->setColours(mColourList);
00263     }
00264 }
00265