1
# -*- coding: utf-8 -*-
3
from PyQt4.QtGui import (QLineEdit, QIcon, QHBoxLayout, QColor, QPushButton,
5
from PyQt4.QtCore import SIGNAL, QSize, QString, pyqtSignature, pyqtProperty
8
class ColorButton(QPushButton):
10
Color choosing push button
12
__pyqtSignals__ = ("colorChanged(QColor)",)
14
def __init__(self, parent=None):
15
QPushButton.__init__(self, parent)
16
self.setFixedSize(20, 20)
17
self.setIconSize(QSize(12, 12))
18
self.connect(self, SIGNAL("clicked()"), self.choose_color)
19
self._color = QColor()
21
def choose_color(self):
22
rgba, valid = QColorDialog.getRgba(self._color.rgba(),
25
color = QColor.fromRgba(rgba)
31
@pyqtSignature("QColor")
32
def set_color(self, color):
33
if color != self._color:
35
self.emit(SIGNAL("colorChanged(QColor)"), self._color)
36
pixmap = QPixmap(self.iconSize())
38
self.setIcon(QIcon(pixmap))
40
color = pyqtProperty("QColor", get_color, set_color)
43
def text_to_qcolor(text):
45
Create a QColor from specified string
46
Avoid warning from Qt when an invalid QColor is instantiated
49
if isinstance(text, QString):
51
if not isinstance(text, (unicode, str)):
53
if text.startswith('#') and len(text)==7:
54
correct = '#0123456789abcdef'
56
if char.lower() not in correct:
58
elif text not in list(QColor.colorNames()):
60
color.setNamedColor(text)
64
class ColorLayout(QHBoxLayout):
65
"""Color-specialized QLineEdit layout"""
66
def __init__(self, color, parent=None):
67
QHBoxLayout.__init__(self)
68
assert isinstance(color, QColor)
69
self.lineedit = QLineEdit(color.name(), parent)
70
self.connect(self.lineedit, SIGNAL("textChanged(QString)"),
72
self.addWidget(self.lineedit)
73
self.colorbtn = ColorButton(parent)
74
self.colorbtn.color = color
75
self.connect(self.colorbtn, SIGNAL("colorChanged(QColor)"),
77
self.addWidget(self.colorbtn)
79
def update_color(self, text):
80
color = text_to_qcolor(text)
82
self.colorbtn.color = color
84
def update_text(self, color):
85
self.lineedit.setText(color.name())
88
return self.lineedit.text()