24 #include <QtGui/QAbstractItemDelegate>
25 #include <QtGui/QApplication>
26 #include <QtGui/QStylePainter>
32 class 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;
54 if (v.type() == QVariant::Brush) {
55 brush = v.value<QBrush>();
56 }
else if (v.type() == QVariant::Color) {
57 brush = QBrush(v.value<
QColor>());
62 KColorComboDelegate::KColorComboDelegate(
QObject *parent)
63 : QAbstractItemDelegate(parent)
67 KColorComboDelegate::~KColorComboDelegate()
71 void 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);
90 if (cv.type() == QVariant::Color) {
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) {
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);
129 QSize 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]);
176 class KColorComboPrivate
182 void setCustomColor(
const QColor &color,
bool lookupInPresets =
true);
185 void _k_slotActivated(
int index);
186 void _k_slotHighlighted(
int index);
194 KColorComboPrivate::KColorComboPrivate(
KColorCombo *qq)
195 : q(qq), customColor(Qt::white)
199 void 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);
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);
322 void KColorComboPrivate::_k_slotActivated(
int index)
326 setCustomColor(customColor,
false);
328 }
else if (colorList.isEmpty()) {
331 internalcolor = colorList[index - 1];
334 emit q->activated(internalcolor);
337 void KColorComboPrivate::_k_slotHighlighted(
int index)
340 internalcolor = customColor;
341 }
else if (colorList.isEmpty()) {
344 internalcolor = colorList[index - 1];
347 emit q->highlighted(internalcolor);
350 void KColorComboPrivate::addColors()
352 q->addItem(
i18nc(
"Custom color",
"Custom..."));
354 if (colorList.isEmpty()) {
357 q->setItemData(i + 1,
standardColor(i), KColorComboDelegate::ColorRole);
360 for (
int i = 0, count = colorList.count(); i < count; ++i) {
362 q->setItemData(i + 1, colorList[i], KColorComboDelegate::ColorRole);
367 #include "kcolorcombo.moc"
QColor color() const
Returns the currently selected color.
void highlighted(const QColor &col)
Emitted when a new item has been highlighted.
KColorCombo(QWidget *parent=0)
Constructs a color combo box.
QString i18nc(const char *ctxt, const char *text)
void setColors(const QList< QColor > &colors)
Set a custom list of colors to choose from, in place of the standard list.
KAction * clear(const QObject *recvr, const char *slot, QObject *parent)
Clear the content of the focus widget.
void setColor(const QColor &col)
Selects the color col.
static QBrush k_colorcombodelegate_brush(const QModelIndex &index, int role)
#define STANDARD_PALETTE_SIZE
void activated(const QColor &col)
Emitted when a new color box has been selected.
bool isCustomColor() const
Find whether the currently selected color is a custom color selected using a color dialog...
QList< QColor > colors() const
Return the list of colors available for selection.
static QColor standardColor(int i)
static const uchar standardPalette[][4]
void showEmptyList()
Clear the color list and don't show it, till the next setColor() call.
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...
virtual void paintEvent(QPaintEvent *event)