24#include <QtGui/QAbstractItemDelegate>
25#include <QtGui/QApplication>
26#include <QtGui/QStylePainter>
32class KColorComboDelegate :
public QAbstractItemDelegate
36 ColorRole = Qt::UserRole + 1
43 KColorComboDelegate(
QObject *parent = 0);
44 virtual ~KColorComboDelegate();
46 virtual void paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index)
const;
47 virtual QSize sizeHint(
const QStyleOptionViewItem &option,
const QModelIndex &index)
const;
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>());
62KColorComboDelegate::KColorComboDelegate(
QObject *parent)
63 : QAbstractItemDelegate(parent)
67KColorComboDelegate::~KColorComboDelegate()
71void KColorComboDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index)
const
74 QColor innercolor(Qt::white);
75 bool isSelected = (option.state & QStyle::State_Selected);
78 innercolor = option.palette.color(QPalette::Highlight);
80 innercolor = option.palette.color(QPalette::Base);
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);
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;
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);
105 QVariant tv = index.data(Qt::DisplayRole);
106 if (tv.type() == QVariant::String) {
107 QString text = tv.toString();
111 textColor = option.palette.color(QPalette::HighlightedText);
113 textColor = option.palette.color(QPalette::Text);
117 innercolor.getHsv(&unused, &unused, &v);
119 textColor = Qt::black;
121 textColor = Qt::white;
124 painter->setPen(textColor);
125 painter->drawText(innerrect.adjusted(1, 1, -1, -1), text);
129QSize KColorComboDelegate::sizeHint(
const QStyleOptionViewItem &option,
const QModelIndex &index)
const
134 return QSize(100, option.fontMetrics.height() + 2 * FrameMargin);
168#define STANDARD_PALETTE_SIZE (int(sizeof(standardPalette) / sizeof(*standardPalette)))
173 return QColor(entry[0], entry[1], entry[2]);
176class KColorComboPrivate
182 void setCustomColor(
const QColor &color,
bool lookupInPresets =
true);
185 void _k_slotActivated(
int index);
186 void _k_slotHighlighted(
int index);
191 QColor internalcolor;
194KColorComboPrivate::KColorComboPrivate(
KColorCombo *qq)
195 : q(qq), customColor(Qt::white)
199void KColorComboPrivate::setCustomColor(
const QColor &color,
bool lookupInPresets)
201 if (lookupInPresets) {
202 if (colorList.isEmpty()) {
205 q->setCurrentIndex(i + 1);
206 internalcolor = color;
211 int i = colorList.indexOf(color);
213 q->setCurrentIndex(i + 1);
214 internalcolor = color;
220 internalcolor = color;
222 q->setItemData(0, customColor, KColorComboDelegate::ColorRole);
227 :
QComboBox(parent), d(new KColorComboPrivate(this))
229 setItemDelegate(
new KColorComboDelegate(
this));
232 connect(
this, SIGNAL(
activated(
int)), SLOT(_k_slotActivated(
int)));
233 connect(
this, SIGNAL(
highlighted(
int)), SLOT(_k_slotHighlighted(
int)));
237 d->_k_slotActivated(1);
239 setMaxVisibleItems(13);
251 d->colorList = colors;
257 if (d->colorList.isEmpty()) {
273 if (!col.isValid()) {
281 d->setCustomColor(col,
true);
289 return d->internalcolor;
294 return d->internalcolor == d->customColor;
300 QStylePainter painter(
this);
301 painter.setPen(palette().color(QPalette::Text));
303 QStyleOptionComboBox opt;
304 initStyleOption(&opt);
305 painter.drawComplexControl(QStyle::CC_ComboBox, opt);
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);
322void KColorComboPrivate::_k_slotActivated(
int index)
326 setCustomColor(customColor,
false);
328 }
else if (colorList.isEmpty()) {
331 internalcolor = colorList[index - 1];
334 emit q->activated(internalcolor);
337void KColorComboPrivate::_k_slotHighlighted(
int index)
340 internalcolor = customColor;
341 }
else if (colorList.isEmpty()) {
344 internalcolor = colorList[index - 1];
347 emit q->highlighted(internalcolor);
350void KColorComboPrivate::addColors()
352 q->addItem(
i18nc(
"Custom color",
"Custom..."));
354 if (colorList.isEmpty()) {
356 q->addItem(QString());
357 q->setItemData(i + 1,
standardColor(i), KColorComboDelegate::ColorRole);
360 for (
int i = 0, count = colorList.count(); i < count; ++i) {
361 q->addItem(QString());
362 q->setItemData(i + 1, colorList[i], KColorComboDelegate::ColorRole);
367#include "kcolorcombo.moc"
void setColors(const QList< QColor > &colors)
Set a custom list of colors to choose from, in place of the standard list.
void showEmptyList()
Clear the color list and don't show it, till the next setColor() call.
virtual void paintEvent(QPaintEvent *event)
KColorCombo(QWidget *parent=0)
Constructs a color combo box.
void activated(const QColor &col)
Emitted when a new color box has been selected.
void setColor(const QColor &col)
Selects the color col.
bool isCustomColor() const
Find whether the currently selected color is a custom color selected using a color dialog.
void highlighted(const QColor &col)
Emitted when a new item has been highlighted.
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.
#define STANDARD_PALETTE_SIZE
static QColor standardColor(int i)
static QBrush k_colorcombodelegate_brush(const QModelIndex &index, int role)
static const uchar standardPalette[][4]
QString i18nc(const char *ctxt, const char *text)