kalarm/lib

colourcombo.cpp
1 /*
2  * colourcombo.cpp - colour selection combo box
3  * Program: kalarm
4  * Copyright (c) 2001 - 2003, 2005 by David Jarvie <software@astrojar.org.uk>
5  *
6  * Some code taken from tdelibs/tdeui/kcolorcombo.cpp in the KDE libraries:
7  * Copyright (C) 1997 Martin Jones (mjones@kde.org)
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23 
24 #include <tqpainter.h>
25 
26 #include <tdelocale.h>
27 #include <kcolordialog.h>
28 
29 #include "kalarm/preferences.h"
30 #include "colourcombo.moc"
31 
32 
33 ColourCombo::ColourCombo(TQWidget* parent, const char* name, const TQColor& defaultColour)
34  : TQComboBox(parent, name),
35  mColourList(Preferences::messageColours()),
36  mSelectedColour(defaultColour),
37  mCustomColour(255, 255, 255),
38  mReadOnly(false),
39  mDisabled(false)
40 {
41  addColours();
42  connect(this, TQ_SIGNAL(activated(int)), TQ_SLOT(slotActivated(int)));
43  connect(this, TQ_SIGNAL(highlighted(int)), TQ_SLOT(slotHighlighted(int)));
44  Preferences::connect(TQ_SIGNAL(preferencesChanged()), this, TQ_SLOT(slotPreferencesChanged()));
45 }
46 
47 void ColourCombo::setColour(const TQColor& colour)
48 {
49  mSelectedColour = colour;
50  addColours();
51 }
52 
53 /******************************************************************************
54 * Set a new colour selection.
55 */
56 void ColourCombo::setColours(const ColourList& colours)
57 {
58  mColourList = colours;
59  if (mSelectedColour != mCustomColour
60  && !mColourList.contains(mSelectedColour))
61  {
62  // The current colour has been deleted
63  mSelectedColour = mColourList.count() ? mColourList.first() : mCustomColour;
64  }
65  addColours();
66 }
67 
68 /******************************************************************************
69 * Called when the user changes the preference settings.
70 * If the colour list has changed, update the colours displayed.
71 */
72 void ColourCombo::slotPreferencesChanged()
73 {
74  const ColourList& prefColours = Preferences::messageColours();
75  if (prefColours != mColourList)
76  setColours(prefColours); // update the display with the new colours
77 }
78 
79 /******************************************************************************
80 * Enable or disable the control.
81 * If it is disabled, its colour is set to the dialog background colour.
82 */
83 void ColourCombo::setEnabled(bool enable)
84 {
85  if (enable && mDisabled)
86  {
87  mDisabled = false;
88  setColour(mSelectedColour);
89  }
90  else if (!enable && !mDisabled)
91  {
92  mSelectedColour = color();
93  int end = count();
94  if (end > 1)
95  {
96  // Add a dialog background colour item
97  TQPixmap pm = *pixmap(1);
98  pm.fill(paletteBackgroundColor());
99  insertItem(pm);
100  setCurrentItem(end);
101  }
102  mDisabled = true;
103  }
104  TQComboBox::setEnabled(enable);
105 }
106 
107 void ColourCombo::slotActivated(int index)
108 {
109  if (index)
110  mSelectedColour = mColourList[index - 1];
111  else
112  {
113  if (KColorDialog::getColor(mCustomColour, this) == TQDialog::Accepted)
114  {
115  TQRect rect;
116  drawCustomItem(rect, false);
117  }
118  mSelectedColour = mCustomColour;
119  }
120  emit activated(mSelectedColour);
121 }
122 
123 void ColourCombo::slotHighlighted(int index)
124 {
125  mSelectedColour = index ? mColourList[index - 1] : mCustomColour;
126  emit highlighted(mSelectedColour);
127 }
128 
129 /******************************************************************************
130 * Initialise the items in the combo box to one for each colour in the list.
131 */
132 void ColourCombo::addColours()
133 {
134  clear();
135 
136  for (ColourList::const_iterator it = mColourList.begin(); ; ++it)
137  {
138  if (it == mColourList.end())
139  {
140  mCustomColour = mSelectedColour;
141  break;
142  }
143  if (mSelectedColour == *it)
144  break;
145  }
146 
147  TQRect rect;
148  drawCustomItem(rect, true);
149 
150  TQPainter painter;
151  TQPixmap pixmap(rect.width(), rect.height());
152  int i = 1;
153  for (ColourList::const_iterator it = mColourList.begin(); it != mColourList.end(); ++i, ++it)
154  {
155  painter.begin(&pixmap);
156  TQBrush brush(*it);
157  painter.fillRect(rect, brush);
158  painter.end();
159 
160  insertItem(pixmap);
161  pixmap.detach();
162 
163  if (*it == mSelectedColour.rgb())
164  setCurrentItem(i);
165  }
166 }
167 
168 void ColourCombo::drawCustomItem(TQRect& rect, bool insert)
169 {
170  TQPen pen;
171  if (tqGray(mCustomColour.rgb()) < 128)
172  pen.setColor(TQt::white);
173  else
174  pen.setColor(TQt::black);
175 
176  TQPainter painter;
177  TQFontMetrics fm = TQFontMetrics(painter.font());
178  rect.setRect(0, 0, width(), fm.height() + 4);
179  TQPixmap pixmap(rect.width(), rect.height());
180 
181  painter.begin(&pixmap);
182  TQBrush brush(mCustomColour);
183  painter.fillRect(rect, brush);
184  painter.setPen(pen);
185  painter.drawText(2, fm.ascent() + 2, i18n("Custom..."));
186  painter.end();
187 
188  if (insert)
189  insertItem(pixmap);
190  else
191  changeItem(pixmap, 0);
192  pixmap.detach();
193 }
194 
196 {
197  mReadOnly = ro;
198 }
199 
200 void ColourCombo::resizeEvent(TQResizeEvent* re)
201 {
202  TQComboBox::resizeEvent(re);
203  addColours();
204 }
205 
206 void ColourCombo::mousePressEvent(TQMouseEvent* e)
207 {
208  if (mReadOnly)
209  {
210  // Swallow up the event if it's the left button
211  if (e->button() == TQt::LeftButton)
212  return;
213  }
214  TQComboBox::mousePressEvent(e);
215 }
216 
217 void ColourCombo::mouseReleaseEvent(TQMouseEvent* e)
218 {
219  if (!mReadOnly)
220  TQComboBox::mouseReleaseEvent(e);
221 }
222 
223 void ColourCombo::mouseMoveEvent(TQMouseEvent* e)
224 {
225  if (!mReadOnly)
226  TQComboBox::mouseMoveEvent(e);
227 }
228 
229 void ColourCombo::keyPressEvent(TQKeyEvent* e)
230 {
231  if (!mReadOnly || e->key() == TQt::Key_Escape)
232  TQComboBox::keyPressEvent(e);
233 }
234 
235 void ColourCombo::keyReleaseEvent(TQKeyEvent* e)
236 {
237  if (!mReadOnly)
238  TQComboBox::keyReleaseEvent(e);
239 }
void activated(const TQColor &)
Signal emitted when a new colour has been selected.
void setColours(const ColourList &list)
Initialises the list of colours to list.
Definition: colourcombo.cpp:56
TQColor colour() const
Returns the selected colour.
Definition: colourcombo.h:58
TQColor color() const
Returns the selected colour.
Definition: colourcombo.h:56
virtual void setEnabled(bool enabled)
Enables or disables the widget.
Definition: colourcombo.cpp:83
virtual void setReadOnly(bool readOnly)
Sets whether the combo box can be changed by the user.
void highlighted(const TQColor &)
Signal emitted when a new colour has been highlighted.
void setColour(const TQColor &c)
Sets the selected colour to c.
Definition: colourcombo.cpp:47
ColourCombo(TQWidget *parent=0, const char *name=0, const TQColor &defaultColour=0xFFFFFF)
Constructor.
Definition: colourcombo.cpp:33
Represents a sorted list of colours.
Definition: colourlist.h:40
size_type contains(const TQColor &c) const
Returns true if the list contains the colour c.
Definition: colourlist.h:86
size_type count() const
Returns the number of colours in the list.
Definition: colourlist.h:74
const_iterator begin() const
Returns an iterator pointing to the first colour in the list.
Definition: colourlist.h:78
const_iterator end() const
Returns an iterator pointing past the last colour in the list.
Definition: colourlist.h:80
TQColor first() const
Returns the first colour in the list.
Definition: colourlist.h:100