colorComboBox.cpp 1.06 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
	colorNames <<
		"red" <<
27
		"green" <<
Pierre Kraemer's avatar
Pierre Kraemer committed
28 29
		"blue" <<
		"cyan "<<
30
		"magenta" <<
Pierre Kraemer's avatar
Pierre Kraemer committed
31
		"yellow" <<
32 33 34
		"gray" <<
		"white" <<
		"black";
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

	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);
}