kalarm

fontcolour.cpp
1 /*
2  * fontcolour.cpp - font and colour chooser widget
3  * Program: kalarm
4  * Copyright © 2001-2003,2005,2008 by David Jarvie <software@astrojar.org.uk>
5  *
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.
10  *
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.
15  *
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.
19  */
20 
21 #include <tqobjectlist.h>
22 #include <tqwidget.h>
23 #include <tqgroupbox.h>
24 #include <tqpushbutton.h>
25 #include <tqhbox.h>
26 #include <tqlabel.h>
27 #include <tqlayout.h>
28 #include <tqwhatsthis.h>
29 
30 #include <tdeglobal.h>
31 #include <tdelocale.h>
32 #include <kcolordialog.h>
33 
34 #include "kalarmapp.h"
35 #include "preferences.h"
36 #include "colourcombo.h"
37 #include "checkbox.h"
38 #include "fontcolour.moc"
39 
40 
41 FontColourChooser::FontColourChooser(TQWidget *parent, const char *name,
42  bool onlyFixed, const TQStringList &fontList,
43  const TQString& frameLabel, bool editColours, bool fg, bool defaultFont,
44  int visibleListSize)
45  : TQWidget(parent, name),
46  mFgColourButton(0),
47  mRemoveColourButton(0),
48  mColourList(Preferences::messageColours()),
49  mReadOnly(false)
50 {
51  TQVBoxLayout* topLayout = new TQVBoxLayout(this, 0, KDialog::spacingHint());
52  TQWidget* page = this;
53  if (!frameLabel.isNull())
54  {
55  page = new TQGroupBox(frameLabel, this);
56  topLayout->addWidget(page);
57  topLayout = new TQVBoxLayout(page, KDialog::marginHint(), KDialog::spacingHint());
58  topLayout->addSpacing(fontMetrics().height() - KDialog::marginHint() + KDialog::spacingHint());
59  }
60  TQHBoxLayout* hlayout = new TQHBoxLayout(topLayout);
61  TQVBoxLayout* colourLayout = new TQVBoxLayout(hlayout);
62  if (fg)
63  {
64  TQHBox* box = new TQHBox(page); // to group widgets for TQWhatsThis text
65  box->setSpacing(KDialog::spacingHint()/2);
66  colourLayout->addWidget(box);
67 
68  TQLabel* label = new TQLabel(i18n("&Foreground color:"), box);
69  box->setStretchFactor(new TQWidget(box), 0);
70  mFgColourButton = new ColourCombo(box);
71  connect(mFgColourButton, TQ_SIGNAL(activated(const TQString&)), TQ_SLOT(setSampleColour()));
72  label->setBuddy(mFgColourButton);
73  TQWhatsThis::add(box, i18n("Select the alarm message foreground color"));
74  }
75 
76  TQHBox* box = new TQHBox(page); // to group widgets for TQWhatsThis text
77  box->setSpacing(KDialog::spacingHint()/2);
78  colourLayout->addWidget(box);
79 
80  TQLabel* label = new TQLabel(i18n("&Background color:"), box);
81  box->setStretchFactor(new TQWidget(box), 0);
82  mBgColourButton = new ColourCombo(box);
83  connect(mBgColourButton, TQ_SIGNAL(activated(const TQString&)), TQ_SLOT(setSampleColour()));
84  label->setBuddy(mBgColourButton);
85  TQWhatsThis::add(box, i18n("Select the alarm message background color"));
86  hlayout->addStretch();
87 
88  if (editColours)
89  {
90  TQHBoxLayout* layout = new TQHBoxLayout(topLayout);
91  TQPushButton* button = new TQPushButton(i18n("Add Co&lor..."), page);
92  button->setFixedSize(button->sizeHint());
93  connect(button, TQ_SIGNAL(clicked()), TQ_SLOT(slotAddColour()));
94  TQWhatsThis::add(button, i18n("Choose a new color to add to the color selection list."));
95  layout->addWidget(button);
96 
97  mRemoveColourButton = new TQPushButton(i18n("&Remove Color"), page);
98  mRemoveColourButton->setFixedSize(mRemoveColourButton->sizeHint());
99  connect(mRemoveColourButton, TQ_SIGNAL(clicked()), TQ_SLOT(slotRemoveColour()));
100  TQWhatsThis::add(mRemoveColourButton,
101  i18n("Remove the color currently shown in the background color chooser, from the color selection list."));
102  layout->addWidget(mRemoveColourButton);
103  }
104 
105  if (defaultFont)
106  {
107  TQHBoxLayout* layout = new TQHBoxLayout(topLayout);
108  mDefaultFont = new CheckBox(i18n("Use &default font"), page);
109  mDefaultFont->setMinimumSize(mDefaultFont->sizeHint());
110  connect(mDefaultFont, TQ_SIGNAL(toggled(bool)), TQ_SLOT(slotDefaultFontToggled(bool)));
111  TQWhatsThis::add(mDefaultFont,
112  i18n("Check to use the default font current at the time the alarm is displayed."));
113  layout->addWidget(mDefaultFont);
114  layout->addWidget(new TQWidget(page)); // left adjust the widget
115  }
116  else
117  mDefaultFont = 0;
118 
119  mFontChooser = new TDEFontChooser(page, name, onlyFixed, fontList, false, visibleListSize);
120  mFontChooser->installEventFilter(this); // for read-only mode
121  const TQObjectList* kids = mFontChooser->queryList();
122  for (TQObjectList::ConstIterator it = kids->constBegin(); it != kids->constEnd(); ++it)
123  (*it)->installEventFilter(this);
124  topLayout->addWidget(mFontChooser);
125 
126  slotDefaultFontToggled(false);
127 }
128 
129 void FontColourChooser::setDefaultFont()
130 {
131  if (mDefaultFont)
132  mDefaultFont->setChecked(true);
133 }
134 
135 void FontColourChooser::setFont(const TQFont& font, bool onlyFixed)
136 {
137  if (mDefaultFont)
138  mDefaultFont->setChecked(false);
139  mFontChooser->setFont(font, onlyFixed);
140 }
141 
142 bool FontColourChooser::defaultFont() const
143 {
144  return mDefaultFont ? mDefaultFont->isChecked() : false;
145 }
146 
147 TQFont FontColourChooser::font() const
148 {
149  return (mDefaultFont && mDefaultFont->isChecked()) ? TQFont() : mFontChooser->font();
150 }
151 
152 void FontColourChooser::setBgColour(const TQColor& colour)
153 {
154  mBgColourButton->setColor(colour);
155  mFontChooser->setBackgroundColor(colour);
156 }
157 
158 void FontColourChooser::setSampleColour()
159 {
160  TQColor bg = mBgColourButton->color();
161  mFontChooser->setBackgroundColor(bg);
162  TQColor fg = fgColour();
163  mFontChooser->setColor(fg);
164  if (mRemoveColourButton)
165  mRemoveColourButton->setEnabled(!mBgColourButton->isCustomColour()); // no deletion of custom colour
166 }
167 
168 TQColor FontColourChooser::bgColour() const
169 {
170  return mBgColourButton->color();
171 }
172 
173 TQColor FontColourChooser::fgColour() const
174 {
175  if (mFgColourButton)
176  return mFgColourButton->color();
177  else
178  {
179  TQColor bg = mBgColourButton->color();
180  TQPalette pal(bg, bg);
181  return pal.color(TQPalette::Active, TQColorGroup::Text);
182  }
183 }
184 
185 TQString FontColourChooser::sampleText() const
186 {
187  return mFontChooser->sampleText();
188 }
189 
190 void FontColourChooser::setSampleText(const TQString& text)
191 {
192  mFontChooser->setSampleText(text);
193 }
194 
195 void FontColourChooser::setFgColour(const TQColor& colour)
196 {
197  if (mFgColourButton)
198  {
199  mFgColourButton->setColor(colour);
200  mFontChooser->setColor(colour);
201  }
202 }
203 
204 void FontColourChooser::setReadOnly(bool ro)
205 {
206  if (ro != mReadOnly)
207  {
208  mReadOnly = ro;
209  if (mFgColourButton)
210  mFgColourButton->setReadOnly(ro);
211  mBgColourButton->setReadOnly(ro);
212  mDefaultFont->setReadOnly(ro);
213  }
214 }
215 
216 bool FontColourChooser::eventFilter(TQObject*, TQEvent* e)
217 {
218  if (mReadOnly)
219  {
220  switch (e->type())
221  {
222  case TQEvent::MouseButtonPress:
223  case TQEvent::MouseButtonRelease:
224  case TQEvent::MouseButtonDblClick:
225  case TQEvent::KeyPress:
226  case TQEvent::KeyRelease:
227  return true; // prevent the event being handled
228  default:
229  break;
230  }
231  }
232  return false;
233 }
234 
235 void FontColourChooser::slotDefaultFontToggled(bool on)
236 {
237  mFontChooser->setEnabled(!on);
238 }
239 
240 void FontColourChooser::setColours(const ColourList& colours)
241 {
242  mColourList = colours;
243  mBgColourButton->setColours(mColourList);
244  mFontChooser->setBackgroundColor(mBgColourButton->color());
245 }
246 
247 void FontColourChooser::slotAddColour()
248 {
249  TQColor colour;
250  if (KColorDialog::getColor(colour, this) == TQDialog::Accepted)
251  {
252  mColourList.insert(colour);
253  mBgColourButton->setColours(mColourList);
254  }
255 }
256 
257 void FontColourChooser::slotRemoveColour()
258 {
259  if (!mBgColourButton->isCustomColour())
260  {
261  mColourList.remove(mBgColourButton->color());
262  mBgColourButton->setColours(mColourList);
263  }
264 }
265 
the KAlarm application object