• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

KDEUI

  • sources
  • kde-4.12
  • kdelibs
  • kdeui
  • colors
kcolorcombo.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 1997 Martin Jones (mjones@kde.org)
3  Copyright (C) 2007 Pino Toscano (pino@kde.org)
4  Copyright (c) 2007 David Jarvie (software@astrojar.org.uk)
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License as published by the Free Software Foundation; either
9  version 2 of the License, or (at your option) any later version.
10 
11  This library 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 GNU
14  Library General Public License for more details.
15 
16  You should have received a copy of the GNU Library General Public License
17  along with this library; see the file COPYING.LIB. If not, write to
18  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  Boston, MA 02110-1301, USA.
20 */
21 
22 #include "kcolorcombo.h"
23 
24 #include <QtGui/QAbstractItemDelegate>
25 #include <QtGui/QApplication>
26 #include <QtGui/QStylePainter>
27 
28 #include <klocale.h>
29 
30 #include "kcolordialog.h"
31 
32 class KColorComboDelegate : public QAbstractItemDelegate
33 {
34  public:
35  enum ItemRoles {
36  ColorRole = Qt::UserRole + 1
37  };
38 
39  enum LayoutMetrics {
40  FrameMargin = 3
41  };
42 
43  KColorComboDelegate(QObject *parent = 0);
44  virtual ~KColorComboDelegate();
45 
46  virtual void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
47  virtual QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
48 };
49 
50 static QBrush k_colorcombodelegate_brush(const QModelIndex &index, int role)
51 {
52  QBrush brush;
53  QVariant v = index.data(role);
54  if (v.type() == QVariant::Brush) {
55  brush = v.value<QBrush>();
56  } else if (v.type() == QVariant::Color) {
57  brush = QBrush(v.value<QColor>());
58  }
59  return brush;
60 }
61 
62 KColorComboDelegate::KColorComboDelegate(QObject *parent)
63  : QAbstractItemDelegate(parent)
64 {
65 }
66 
67 KColorComboDelegate::~KColorComboDelegate()
68 {
69 }
70 
71 void KColorComboDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
72 {
73  // background
74  QColor innercolor(Qt::white);
75  bool isSelected = (option.state & QStyle::State_Selected);
76  bool paletteBrush = (k_colorcombodelegate_brush(index, Qt::BackgroundRole).style() == Qt::NoBrush);
77  if (isSelected) {
78  innercolor = option.palette.color(QPalette::Highlight);
79  } else {
80  innercolor = option.palette.color(QPalette::Base);
81  }
82  // highlight selected item
83  QStyleOptionViewItemV4 opt(option);
84  opt.showDecorationSelected = true;
85  QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
86  style->drawPrimitive(QStyle::PE_PanelItemViewItem, &opt, painter, opt.widget);
87  QRect innerrect = option.rect.adjusted(FrameMargin, FrameMargin, -FrameMargin, -FrameMargin);
88  // inner color
89  QVariant cv = index.data(ColorRole);
90  if (cv.type() == QVariant::Color) {
91  QColor tmpcolor = cv.value<QColor>();
92  if (tmpcolor.isValid()) {
93  innercolor = tmpcolor;
94  paletteBrush = false;
95  painter->setPen(Qt::transparent);
96  painter->setBrush(innercolor);
97  QPainter::RenderHints tmpHint = painter->renderHints();
98  painter->setRenderHint(QPainter::Antialiasing);
99  painter->drawRoundedRect(innerrect, 2, 2);
100  painter->setRenderHints(tmpHint);
101  painter->setBrush(Qt::NoBrush);
102  }
103  }
104  // text
105  QVariant tv = index.data(Qt::DisplayRole);
106  if (tv.type() == QVariant::String) {
107  QString text = tv.toString();
108  QColor textColor;
109  if (paletteBrush) {
110  if (isSelected) {
111  textColor = option.palette.color(QPalette::HighlightedText);
112  } else {
113  textColor = option.palette.color(QPalette::Text);
114  }
115  } else {
116  int unused, v;
117  innercolor.getHsv(&unused, &unused, &v);
118  if (v > 128) {
119  textColor = Qt::black;
120  } else {
121  textColor = Qt::white;
122  }
123  }
124  painter->setPen(textColor);
125  painter->drawText(innerrect.adjusted(1, 1, -1, -1), text);
126  }
127 }
128 
129 QSize KColorComboDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
130 {
131  Q_UNUSED(index)
132 
133  // the width does not matter, as the view will always use the maximum width available
134  return QSize(100, option.fontMetrics.height() + 2 * FrameMargin);
135 }
136 
137 
138 static const uchar standardPalette[][4] = {
139  { 255, 255, 255 }, // white
140  { 192, 192, 192 }, // light gray
141  { 160, 160, 160 }, // gray
142  { 128, 128, 128 }, // dark gray
143  { 0, 0, 0 }, // black
144 
145  { 255, 128, 128 }, //light red
146  { 255, 192, 128 }, //light orange
147  { 255, 255, 128 }, //light yellow
148  { 128, 255, 128 }, //light green
149  { 128, 255, 255 }, //cyan blue
150  { 128, 128, 255 }, //light blue
151  { 255, 128, 255 }, //light violet
152  { 255, 0, 0 }, //red
153  { 255, 128, 0 }, //orange
154  { 255, 255, 0 }, //yellow
155  { 0, 255, 0 }, //green
156  { 0, 255, 255 }, //light blue
157  { 0, 0, 255 }, //blue
158  { 255, 0, 255 }, //violet
159  { 128, 0, 0 }, //dark red
160  { 128, 64, 0 }, //dark orange
161  { 128, 128, 0 }, //dark yellow
162  { 0, 128, 0 }, //dark green
163  { 0, 128, 128 }, //dark light blue
164  { 0, 0, 128 }, //dark blue
165  { 128, 0, 128 } //dark violet
166 };
167 
168 #define STANDARD_PALETTE_SIZE (int(sizeof(standardPalette) / sizeof(*standardPalette)))
169 
170 static inline QColor standardColor(int i)
171 {
172  const uchar *entry = standardPalette[i];
173  return QColor(entry[0], entry[1], entry[2]);
174 }
175 
176 class KColorComboPrivate
177 {
178  public:
179  KColorComboPrivate(KColorCombo *qq);
180 
181  void addColors();
182  void setCustomColor(const QColor &color, bool lookupInPresets = true);
183 
184  // slots
185  void _k_slotActivated(int index);
186  void _k_slotHighlighted(int index);
187 
188  KColorCombo *q;
189  QList<QColor> colorList;
190  QColor customColor;
191  QColor internalcolor;
192 };
193 
194 KColorComboPrivate::KColorComboPrivate(KColorCombo *qq)
195  : q(qq), customColor(Qt::white)
196 {
197 }
198 
199 void KColorComboPrivate::setCustomColor(const QColor &color, bool lookupInPresets)
200 {
201  if (lookupInPresets) {
202  if (colorList.isEmpty()) {
203  for (int i = 0; i < STANDARD_PALETTE_SIZE; ++i) {
204  if (standardColor(i) == color) {
205  q->setCurrentIndex(i + 1);
206  internalcolor = color;
207  return;
208  }
209  }
210  } else {
211  int i = colorList.indexOf(color);
212  if (i >= 0) {
213  q->setCurrentIndex(i + 1);
214  internalcolor = color;
215  return;
216  }
217  }
218  }
219 
220  internalcolor = color;
221  customColor = color;
222  q->setItemData(0, customColor, KColorComboDelegate::ColorRole);
223 }
224 
225 
226 KColorCombo::KColorCombo( QWidget *parent )
227  : QComboBox(parent), d(new KColorComboPrivate(this))
228 {
229  setItemDelegate(new KColorComboDelegate(this));
230  d->addColors();
231 
232  connect(this, SIGNAL(activated(int)), SLOT(_k_slotActivated(int)));
233  connect(this, SIGNAL(highlighted(int)), SLOT(_k_slotHighlighted(int)));
234 
235  // select the white color
236  setCurrentIndex(1);
237  d->_k_slotActivated(1);
238 
239  setMaxVisibleItems(13);
240 }
241 
242 
243 KColorCombo::~KColorCombo()
244 {
245  delete d;
246 }
247 
248 void KColorCombo::setColors( const QList<QColor> &colors )
249 {
250  clear();
251  d->colorList = colors;
252  d->addColors();
253 }
254 
255 QList<QColor> KColorCombo::colors() const
256 {
257  if (d->colorList.isEmpty()) {
258  QList<QColor> list;
259  for (int i = 0; i < STANDARD_PALETTE_SIZE; ++i) {
260  list += standardColor(i);
261  }
262  return list;
263  } else {
264  return d->colorList;
265  }
266 }
267 
271 void KColorCombo::setColor( const QColor &col )
272 {
273  if (!col.isValid()) {
274  return;
275  }
276 
277  if (count() == 0) {
278  d->addColors();
279  }
280 
281  d->setCustomColor(col, true);
282 }
283 
284 
288 QColor KColorCombo::color() const {
289  return d->internalcolor;
290 }
291 
292 bool KColorCombo::isCustomColor() const
293 {
294  return d->internalcolor == d->customColor;
295 }
296 
297 void KColorCombo::paintEvent(QPaintEvent *event)
298 {
299  Q_UNUSED(event)
300  QStylePainter painter(this);
301  painter.setPen(palette().color(QPalette::Text));
302 
303  QStyleOptionComboBox opt;
304  initStyleOption(&opt);
305  painter.drawComplexControl(QStyle::CC_ComboBox, opt);
306 
307  QRect frame = style()->subControlRect(QStyle::CC_ComboBox, &opt, QStyle::SC_ComboBoxEditField, this);
308  painter.setRenderHint(QPainter::Antialiasing);
309  painter.setPen(Qt::transparent);
310  painter.setBrush(QBrush(d->internalcolor));
311  painter.drawRoundedRect(frame.adjusted(1, 1, -1, -1), 2, 2);
312 }
313 
317 void KColorCombo::showEmptyList()
318 {
319  clear();
320 }
321 
322 void KColorComboPrivate::_k_slotActivated(int index)
323 {
324  if (index == 0) {
325  if (KColorDialog::getColor(customColor, q) == QDialog::Accepted) {
326  setCustomColor(customColor, false);
327  }
328  } else if (colorList.isEmpty()) {
329  internalcolor = standardColor(index - 1);
330  } else {
331  internalcolor = colorList[index - 1];
332  }
333 
334  emit q->activated(internalcolor);
335 }
336 
337 void KColorComboPrivate::_k_slotHighlighted(int index)
338 {
339  if (index == 0) {
340  internalcolor = customColor;
341  } else if (colorList.isEmpty()) {
342  internalcolor = standardColor(index - 1);
343  } else {
344  internalcolor = colorList[index - 1];
345  }
346 
347  emit q->highlighted(internalcolor);
348 }
349 
350 void KColorComboPrivate::addColors()
351 {
352  q->addItem(i18nc("Custom color", "Custom..."));
353 
354  if (colorList.isEmpty()) {
355  for (int i = 0; i < STANDARD_PALETTE_SIZE; ++i) {
356  q->addItem(QString());
357  q->setItemData(i + 1, standardColor(i), KColorComboDelegate::ColorRole);
358  }
359  } else {
360  for (int i = 0, count = colorList.count(); i < count; ++i) {
361  q->addItem(QString());
362  q->setItemData(i + 1, colorList[i], KColorComboDelegate::ColorRole);
363  }
364  }
365 }
366 
367 #include "kcolorcombo.moc"
QVariant
QColor
KColorCombo::color
QColor color() const
Returns the currently selected color.
KColorCombo::highlighted
void highlighted(const QColor &col)
Emitted when a new item has been highlighted.
KColorCombo::KColorCombo
KColorCombo(QWidget *parent=0)
Constructs a color combo box.
Definition: kcolorcombo.cpp:226
QWidget
QString
QObject
klocale.h
i18nc
QString i18nc(const char *ctxt, const char *text)
KColorCombo::setColors
void setColors(const QList< QColor > &colors)
Set a custom list of colors to choose from, in place of the standard list.
Definition: kcolorcombo.cpp:248
KColorCombo::~KColorCombo
~KColorCombo()
Definition: kcolorcombo.cpp:243
KColorCombo
Combobox for colors.
Definition: kcolorcombo.h:45
QComboBox
KStandardAction::clear
KAction * clear(const QObject *recvr, const char *slot, QObject *parent)
Clear the content of the focus widget.
Definition: kstandardaction.cpp:314
KColorCombo::setColor
void setColor(const QColor &col)
Selects the color col.
Definition: kcolorcombo.cpp:271
k_colorcombodelegate_brush
static QBrush k_colorcombodelegate_brush(const QModelIndex &index, int role)
Definition: kcolorcombo.cpp:50
STANDARD_PALETTE_SIZE
#define STANDARD_PALETTE_SIZE
Definition: kcolorcombo.cpp:168
KColorCombo::activated
void activated(const QColor &col)
Emitted when a new color box has been selected.
kcolorcombo.h
KColorCombo::isCustomColor
bool isCustomColor() const
Find whether the currently selected color is a custom color selected using a color dialog...
Definition: kcolorcombo.cpp:292
KColorCombo::colors
QList< QColor > colors() const
Return the list of colors available for selection.
kcolordialog.h
standardColor
static QColor standardColor(int i)
Definition: kcolorcombo.cpp:170
QRect
standardPalette
static const uchar standardPalette[][4]
Definition: kcolorcombo.cpp:138
KColorCombo::showEmptyList
void showEmptyList()
Clear the color list and don't show it, till the next setColor() call.
Definition: kcolorcombo.cpp:317
QSize
KColorDialog::getColor
static int getColor(QColor &theColor, QWidget *parent=0L)
Creates a modal color dialog, let the user choose a color, and returns when the dialog is closed...
Definition: kcolordialog.cpp:1492
KColorCombo::paintEvent
virtual void paintEvent(QPaintEvent *event)
Definition: kcolorcombo.cpp:297
QList< QColor >
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:14 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal