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 <tqobjectlist.h> 00022 #include <tqwidget.h> 00023 #include <tqgroupbox.h> 00024 #include <tqpushbutton.h> 00025 #include <tqhbox.h> 00026 #include <tqlabel.h> 00027 #include <tqlayout.h> 00028 #include <tqwhatsthis.h> 00029 00030 #include <tdeglobal.h> 00031 #include <tdelocale.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(TQWidget *parent, const char *name, 00042 bool onlyFixed, const TQStringList &fontList, 00043 const TQString& frameLabel, bool editColours, bool fg, bool defaultFont, 00044 int visibleListSize) 00045 : TQWidget(parent, name), 00046 mFgColourButton(0), 00047 mRemoveColourButton(0), 00048 mColourList(Preferences::messageColours()), 00049 mReadOnly(false) 00050 { 00051 TQVBoxLayout* topLayout = new TQVBoxLayout(this, 0, KDialog::spacingHint()); 00052 TQWidget* page = this; 00053 if (!frameLabel.isNull()) 00054 { 00055 page = new TQGroupBox(frameLabel, this); 00056 topLayout->addWidget(page); 00057 topLayout = new TQVBoxLayout(page, KDialog::marginHint(), KDialog::spacingHint()); 00058 topLayout->addSpacing(fontMetrics().height() - KDialog::marginHint() + KDialog::spacingHint()); 00059 } 00060 TQHBoxLayout* hlayout = new TQHBoxLayout(topLayout); 00061 TQVBoxLayout* colourLayout = new TQVBoxLayout(hlayout); 00062 if (fg) 00063 { 00064 TQHBox* box = new TQHBox(page); // to group widgets for TQWhatsThis text 00065 box->setSpacing(KDialog::spacingHint()/2); 00066 colourLayout->addWidget(box); 00067 00068 TQLabel* label = new TQLabel(i18n("&Foreground color:"), box); 00069 box->setStretchFactor(new TQWidget(box), 0); 00070 mFgColourButton = new ColourCombo(box); 00071 connect(mFgColourButton, TQT_SIGNAL(activated(const TQString&)), TQT_SLOT(setSampleColour())); 00072 label->setBuddy(mFgColourButton); 00073 TQWhatsThis::add(box, i18n("Select the alarm message foreground color")); 00074 } 00075 00076 TQHBox* box = new TQHBox(page); // to group widgets for TQWhatsThis text 00077 box->setSpacing(KDialog::spacingHint()/2); 00078 colourLayout->addWidget(box); 00079 00080 TQLabel* label = new TQLabel(i18n("&Background color:"), box); 00081 box->setStretchFactor(new TQWidget(box), 0); 00082 mBgColourButton = new ColourCombo(box); 00083 connect(mBgColourButton, TQT_SIGNAL(activated(const TQString&)), TQT_SLOT(setSampleColour())); 00084 label->setBuddy(mBgColourButton); 00085 TQWhatsThis::add(box, i18n("Select the alarm message background color")); 00086 hlayout->addStretch(); 00087 00088 if (editColours) 00089 { 00090 TQHBoxLayout* layout = new TQHBoxLayout(topLayout); 00091 TQPushButton* button = new TQPushButton(i18n("Add Co&lor..."), page); 00092 button->setFixedSize(button->sizeHint()); 00093 connect(button, TQT_SIGNAL(clicked()), TQT_SLOT(slotAddColour())); 00094 TQWhatsThis::add(button, i18n("Choose a new color to add to the color selection list.")); 00095 layout->addWidget(button); 00096 00097 mRemoveColourButton = new TQPushButton(i18n("&Remove Color"), page); 00098 mRemoveColourButton->setFixedSize(mRemoveColourButton->sizeHint()); 00099 connect(mRemoveColourButton, TQT_SIGNAL(clicked()), TQT_SLOT(slotRemoveColour())); 00100 TQWhatsThis::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 TQHBoxLayout* layout = new TQHBoxLayout(topLayout); 00108 mDefaultFont = new CheckBox(i18n("Use &default font"), page); 00109 mDefaultFont->setMinimumSize(mDefaultFont->sizeHint()); 00110 connect(mDefaultFont, TQT_SIGNAL(toggled(bool)), TQT_SLOT(slotDefaultFontToggled(bool))); 00111 TQWhatsThis::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 TQWidget(page)); // left adjust the widget 00115 } 00116 else 00117 mDefaultFont = 0; 00118 00119 mFontChooser = new TDEFontChooser(page, name, onlyFixed, fontList, false, visibleListSize); 00120 mFontChooser->installEventFilter(this); // for read-only mode 00121 const TQObjectList* kids = mFontChooser->queryList(); 00122 for (TQObjectList::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 TQFont& 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 TQFont FontColourChooser::font() const 00148 { 00149 return (mDefaultFont && mDefaultFont->isChecked()) ? TQFont() : mFontChooser->font(); 00150 } 00151 00152 void FontColourChooser::setBgColour(const TQColor& colour) 00153 { 00154 mBgColourButton->setColor(colour); 00155 mFontChooser->setBackgroundColor(colour); 00156 } 00157 00158 void FontColourChooser::setSampleColour() 00159 { 00160 TQColor bg = mBgColourButton->color(); 00161 mFontChooser->setBackgroundColor(bg); 00162 TQColor fg = fgColour(); 00163 mFontChooser->setColor(fg); 00164 if (mRemoveColourButton) 00165 mRemoveColourButton->setEnabled(!mBgColourButton->isCustomColour()); // no deletion of custom colour 00166 } 00167 00168 TQColor FontColourChooser::bgColour() const 00169 { 00170 return mBgColourButton->color(); 00171 } 00172 00173 TQColor FontColourChooser::fgColour() const 00174 { 00175 if (mFgColourButton) 00176 return mFgColourButton->color(); 00177 else 00178 { 00179 TQColor bg = mBgColourButton->color(); 00180 TQPalette pal(bg, bg); 00181 return pal.color(TQPalette::Active, TQColorGroup::Text); 00182 } 00183 } 00184 00185 TQString FontColourChooser::sampleText() const 00186 { 00187 return mFontChooser->sampleText(); 00188 } 00189 00190 void FontColourChooser::setSampleText(const TQString& text) 00191 { 00192 mFontChooser->setSampleText(text); 00193 } 00194 00195 void FontColourChooser::setFgColour(const TQColor& 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(TQObject*, TQEvent* e) 00217 { 00218 if (mReadOnly) 00219 { 00220 switch (e->type()) 00221 { 00222 case TQEvent::MouseButtonPress: 00223 case TQEvent::MouseButtonRelease: 00224 case TQEvent::MouseButtonDblClick: 00225 case TQEvent::KeyPress: 00226 case TQEvent::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 TQColor colour; 00250 if (KColorDialog::getColor(colour, this) == TQDialog::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