colorComboBox.cpp 1.11 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <QtGui>

#include "colorComboBox.h"

ColorComboBox::ColorComboBox(QWidget *widget) : QComboBox(widget)
{
	//connect( this, SIGNAL(highlighted(int)), this, SLOT(slotHighlight(int)) );
	populateList();
}

QColor ColorComboBox::color() const
{
	return qvariant_cast<QColor>(itemData(currentIndex(), Qt::DecorationRole));
}

void ColorComboBox::setColor(QColor color)
{
	setCurrentIndex(findData(color, int(Qt::DecorationRole)));
}

void ColorComboBox::populateList()
{
	//QStringList colorNames = QColor::colorNames();
	QStringList colorNames;
Pierre Kraemer's avatar
Pierre Kraemer committed
25
26
27
28
29
30
31
32
33
34
35
36
37
	colorNames <<
		"darkGreen" <<
		"green" <<
		"gray" <<
		"red" <<
		"white" <<
		"blue" <<
		"cyan "<<
		"darkMagenta" <<
		"yellow" <<
		"darkRed" <<
		"black" <<
		"magenta";
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

	for (int i = 0; i < colorNames.size(); ++i) {
		QColor color(colorNames[i]);
		insertItem(i, colorNames[i]);
		setItemData(i, color, Qt::DecorationRole);
	}
}

void ColorComboBox::slotHighlight(int index)
{
	const QStringList colorNames = QColor::colorNames();
	QColor color(colorNames.at(index));

	QPalette palette = this->palette();
	palette.setColor(QPalette::Highlight, color);
	setPalette(palette);
}