kalarm

fontcolour.cpp

00001 /*
00002  *  fontcolour.cpp  -  font and colour chooser widget
00003  *  Program:  kalarm
00004  *  Copyright (C) 2001 - 2003, 2005 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().lineSpacing()/2);
00059     }
00060     if (fg)
00061     {
00062         QBoxLayout* layout = new QHBoxLayout(topLayout);
00063         QHBox* box = new QHBox(page);    // to group widgets for QWhatsThis text
00064         box->setSpacing(KDialog::spacingHint());
00065         layout->addWidget(box);
00066 
00067         QLabel* label = new QLabel(i18n("&Foreground color:"), box);
00068         label->setMinimumSize(label->sizeHint());
00069         mFgColourButton = new ColourCombo(box);
00070         mFgColourButton->setMinimumSize(mFgColourButton->sizeHint());
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         layout->addStretch();
00075     }
00076 
00077     QBoxLayout* layout = new QHBoxLayout(topLayout);
00078     QHBox* box = new QHBox(page);    // to group widgets for QWhatsThis text
00079     box->setSpacing(KDialog::spacingHint());
00080     layout->addWidget(box);
00081 
00082     QLabel* label = new QLabel(i18n("&Background color:"), box);
00083     label->setMinimumSize(label->sizeHint());
00084     mBgColourButton = new ColourCombo(box);
00085     mBgColourButton->setMinimumSize(mBgColourButton->sizeHint());
00086     connect(mBgColourButton, SIGNAL(activated(const QString&)), SLOT(setSampleColour()));
00087     label->setBuddy(mBgColourButton);
00088     QWhatsThis::add(box, i18n("Select the alarm message background color"));
00089     layout->addStretch();
00090 
00091     if (editColours)
00092     {
00093         layout = new QHBoxLayout(topLayout);
00094         QPushButton* button = new QPushButton(i18n("Add Co&lor..."), page);
00095         button->setFixedSize(button->sizeHint());
00096         connect(button, SIGNAL(clicked()), SLOT(slotAddColour()));
00097         QWhatsThis::add(button, i18n("Choose a new color to add to the color selection list."));
00098         layout->addWidget(button);
00099 
00100         mRemoveColourButton = new QPushButton(i18n("&Remove Color"), page);
00101         mRemoveColourButton->setFixedSize(mRemoveColourButton->sizeHint());
00102         connect(mRemoveColourButton, SIGNAL(clicked()), SLOT(slotRemoveColour()));
00103         QWhatsThis::add(mRemoveColourButton,
00104               i18n("Remove the color currently shown in the background color chooser, from the color selection list."));
00105         layout->addWidget(mRemoveColourButton);
00106     }
00107 
00108     if (defaultFont)
00109     {
00110         layout = new QHBoxLayout(topLayout);
00111         mDefaultFont = new CheckBox(i18n("Use &default font"), page);
00112         mDefaultFont->setMinimumSize(mDefaultFont->sizeHint());
00113         connect(mDefaultFont, SIGNAL(toggled(bool)), SLOT(slotDefaultFontToggled(bool)));
00114         QWhatsThis::add(mDefaultFont,
00115               i18n("Check to use the default font current at the time the alarm is displayed."));
00116         layout->addWidget(mDefaultFont);
00117         layout->addWidget(new QWidget(page));    // left adjust the widget
00118     }
00119     else
00120         mDefaultFont = 0;
00121 
00122     mFontChooser = new KFontChooser(page, name, onlyFixed, fontList, false, visibleListSize);
00123     topLayout->addWidget(mFontChooser);
00124 
00125     slotDefaultFontToggled(false);
00126 }
00127 
00128 void FontColourChooser::setDefaultFont()
00129 {
00130     if (mDefaultFont)
00131         mDefaultFont->setChecked(true);
00132 }
00133 
00134 void FontColourChooser::setFont(const QFont& font, bool onlyFixed)
00135 {
00136     if (mDefaultFont)
00137         mDefaultFont->setChecked(false);
00138     mFontChooser->setFont(font, onlyFixed);
00139 }
00140 
00141 bool FontColourChooser::defaultFont() const
00142 {
00143     return mDefaultFont ? mDefaultFont->isChecked() : false;
00144 }
00145 
00146 QFont FontColourChooser::font() const
00147 {
00148     return (mDefaultFont && mDefaultFont->isChecked()) ? QFont() : mFontChooser->font();
00149 }
00150 
00151 void FontColourChooser::setBgColour(const QColor& colour)
00152 {
00153     mBgColourButton->setColor(colour);
00154     mFontChooser->setBackgroundColor(colour);
00155 }
00156 
00157 void FontColourChooser::setSampleColour()
00158 {
00159     QColor bg = mBgColourButton->color();
00160     mFontChooser->setBackgroundColor(bg);
00161     QColor fg = fgColour();
00162     mFontChooser->setColor(fg);
00163     if (mRemoveColourButton)
00164         mRemoveColourButton->setEnabled(!mBgColourButton->isCustomColour());   // no deletion of custom colour
00165 }
00166 
00167 QColor FontColourChooser::bgColour() const
00168 {
00169     return mBgColourButton->color();
00170 }
00171 
00172 QColor FontColourChooser::fgColour() const
00173 {
00174     if (mFgColourButton)
00175         return mFgColourButton->color();
00176     else
00177     {
00178         QColor bg = mBgColourButton->color();
00179         QPalette pal(bg, bg);
00180         return pal.color(QPalette::Active, QColorGroup::Text);
00181     }
00182 }
00183 
00184 QString FontColourChooser::sampleText() const
00185 {
00186     return mFontChooser->sampleText();
00187 }
00188 
00189 void FontColourChooser::setSampleText(const QString& text)
00190 {
00191     mFontChooser->setSampleText(text);
00192 }
00193 
00194 void FontColourChooser::setFgColour(const QColor& colour)
00195 {
00196     if (mFgColourButton)
00197     {
00198         mFgColourButton->setColor(colour);
00199         mFontChooser->setColor(colour);
00200     }
00201 }
00202 
00203 void FontColourChooser::setReadOnly(bool ro)
00204 {
00205     if (ro != mReadOnly)
00206     {
00207         mReadOnly = ro;
00208         if (mFgColourButton)
00209             mFgColourButton->setReadOnly(ro);
00210         mBgColourButton->setReadOnly(ro);
00211         mDefaultFont->setReadOnly(ro);
00212     }
00213 }
00214 
00215 void FontColourChooser::slotDefaultFontToggled(bool on)
00216 {
00217     mFontChooser->setEnabled(!on);
00218 }
00219 
00220 void FontColourChooser::setColours(const ColourList& colours)
00221 {
00222     mColourList = colours;
00223     mBgColourButton->setColours(mColourList);
00224     mFontChooser->setBackgroundColor(mBgColourButton->color());
00225 }
00226 
00227 void FontColourChooser::slotAddColour()
00228 {
00229     QColor colour;
00230     if (KColorDialog::getColor(colour, this) == QDialog::Accepted)
00231     {
00232         mColourList.insert(colour);
00233         mBgColourButton->setColours(mColourList);
00234     }
00235 }
00236 
00237 void FontColourChooser::slotRemoveColour()
00238 {
00239     if (!mBgColourButton->isCustomColour())
00240     {
00241         mColourList.remove(mBgColourButton->color());
00242         mBgColourButton->setColours(mColourList);
00243     }
00244 }
00245 
KDE Home | KDE Accessibility Home | Description of Access Keys