#!/usr/bin/env python
###########################################################################
#
# Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
#
# PyQt port in November 2010 by Hans-Peter Jansen <hpj@urpla.net>
#
###########################################################################

import sys

# This is only needed for Python v2 but is harmless for Python v3.
import sip
sip.setapi('QString', 2)    # converts QStrings to unicode objects internally
sip.setapi('QVariant', 2)   # converts QVariants to the basic data types internally

from PyQt4 import QtCore, QtGui

class ColorListEditor(QtGui.QComboBox):
    def __init__(self, widget = None):
        super(ColorListEditor, self).__init__(widget)
        self.populateList()

    def getColor(self):
        color = self.itemData(self.currentIndex(), QtCore.Qt.DecorationRole)
        print "ColorListEditor.getColor(%s)" % color.name()
        return color

    def setColor(self, color):
        print "ColorListEditor.setColor(%s)" % color.name()
        self.setCurrentIndex(self.findData(color, int(QtCore.Qt.DecorationRole)))

    color = QtCore.pyqtProperty(QtGui.QColor, getColor, setColor, user = True)

    def populateList(self):
        for i, colorName in enumerate(QtGui.QColor.colorNames()):
            color = QtGui.QColor(colorName)
            self.insertItem(i, colorName)
            self.setItemData(i, color, QtCore.Qt.DecorationRole)


class ColorListItemEditorCreator(QtGui.QItemEditorCreatorBase):
    def createWidget(self, parent):
        return ColorListEditor(parent)

    def valuePropertyName(self):
        return "color"


class Window(QtGui.QWidget):
    def __init__(self, parent = None):
        super(Window, self).__init__(parent)
        factory = QtGui.QItemEditorFactory()
        factory.registerEditor(QtCore.QVariant.Color, ColorListItemEditorCreator())
        QtGui.QItemEditorFactory.setDefaultFactory(factory)
        self.createGUI()

    def createGUI(self):
        tableData = [
            (self.tr("Alice"), QtGui.QColor("aliceblue")),
            (self.tr("Neptun"), QtGui.QColor("aquamarine")),
            (self.tr("Ferdinand"), QtGui.QColor("springgreen")),
        ]
        table = QtGui.QTableWidget(3, 2)
        table.setHorizontalHeaderLabels([self.tr("Name"), self.tr("Hair Color")])
        table.verticalHeader().setVisible(False)
        table.resize(150, 50)
        for i, t in enumerate(tableData):
            name, color = t
            nameItem = QtGui.QTableWidgetItem(name)
            colorItem = QtGui.QTableWidgetItem()
            colorItem.setData(QtCore.Qt.DisplayRole, color)
            table.setItem(i, 0, nameItem)
            table.setItem(i, 1, colorItem)
        table.resizeColumnToContents(0)
        table.horizontalHeader().setStretchLastSection(True)
        layout = QtGui.QGridLayout()
        layout.addWidget(table, 0, 0)
        self.setLayout(layout)
        self.setWindowTitle(self.tr("Color Editor Factory"))


if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec_())
