|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2009 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a palette widget for the icon editor. |
|
8 """ |
|
9 |
|
10 from PyQt4.QtCore import * |
|
11 from PyQt4.QtGui import * |
|
12 |
|
13 class IconEditorPalette(QWidget): |
|
14 """ |
|
15 Class implementing a palette widget for the icon editor. |
|
16 |
|
17 @signal colorSelected(QColor) emitted after a new color has been selected |
|
18 """ |
|
19 def __init__(self, parent = None): |
|
20 """ |
|
21 Constructor |
|
22 |
|
23 @param parent reference to the parent widget (QWidget) |
|
24 """ |
|
25 QWidget.__init__(self, parent) |
|
26 |
|
27 if self.layoutDirection == Qt.Horizontal: |
|
28 direction = QBoxLayout.LeftToRight |
|
29 else: |
|
30 direction = QBoxLayout.TopToBottom |
|
31 self.__layout = QBoxLayout(direction, self) |
|
32 self.setLayout(self.__layout) |
|
33 |
|
34 self.__preview = QLabel(self) |
|
35 self.__preview.setFrameStyle(QFrame.Panel | QFrame.Sunken) |
|
36 self.__preview.setFixedHeight(64) |
|
37 self.__preview.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) |
|
38 self.__preview.setWhatsThis(self.trUtf8( |
|
39 """<b>Preview</b>""" |
|
40 """<p>This is a 1:1 preview of the current icon.</p>""" |
|
41 )) |
|
42 self.__layout.addWidget(self.__preview) |
|
43 |
|
44 self.__color = QLabel(self) |
|
45 self.__color.setFrameStyle(QFrame.Panel | QFrame.Sunken) |
|
46 self.__color.setFixedHeight(24) |
|
47 self.__color.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) |
|
48 self.__color.setWhatsThis(self.trUtf8( |
|
49 """<b>Current Color</b>""" |
|
50 """<p>This is the currently selected color used for drawing.</p>""" |
|
51 )) |
|
52 self.__layout.addWidget(self.__color) |
|
53 |
|
54 self.__colorTxt = QLabel(self) |
|
55 self.__colorTxt.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter) |
|
56 self.__colorTxt.setWhatsThis(self.trUtf8( |
|
57 """<b>Current Color Value</b>""" |
|
58 """<p>This is the currently selected color value used for drawing.</p>""" |
|
59 )) |
|
60 self.__layout.addWidget(self.__colorTxt) |
|
61 |
|
62 self.__colorButton = QPushButton(self.trUtf8("Select Color"), self) |
|
63 self.__colorButton.setWhatsThis(self.trUtf8( |
|
64 """<b>Select Color</b>""" |
|
65 """<p>Select the current drawing color via a color selection dialog.</p>""" |
|
66 )) |
|
67 self.connect(self.__colorButton, SIGNAL("clicked()"), self.__selectColor) |
|
68 self.__layout.addWidget(self.__colorButton) |
|
69 |
|
70 self.__colorAlpha = QSpinBox(self) |
|
71 self.__colorAlpha.setRange(0, 255) |
|
72 self.__colorAlpha.setWhatsThis(self.trUtf8( |
|
73 """<b>Select alpha channel value</b>""" |
|
74 """<p>Select the value for the alpha channel of the current color.</p>""" |
|
75 )) |
|
76 self.__layout.addWidget(self.__colorAlpha) |
|
77 self.connect(self.__colorAlpha, SIGNAL("valueChanged(int)"), |
|
78 self.__alphaChanged) |
|
79 |
|
80 spacer = QSpacerItem(10, 10, QSizePolicy.Minimum, QSizePolicy.Expanding) |
|
81 self.__layout.addItem(spacer) |
|
82 |
|
83 def previewChanged(self, pixmap): |
|
84 """ |
|
85 Public slot to update the preview. |
|
86 """ |
|
87 self.__preview.setPixmap(pixmap) |
|
88 |
|
89 def colorChanged(self, color): |
|
90 """ |
|
91 Public slot to update the color preview. |
|
92 """ |
|
93 self.__currentColor = color |
|
94 self.__currentAlpha = color.alpha() |
|
95 |
|
96 pm = QPixmap(90, 18) |
|
97 pm.fill(color) |
|
98 self.__color.setPixmap(pm) |
|
99 |
|
100 self.__colorTxt.setText( |
|
101 "%d, %d, %d, %d" % (color.red(), color.green(), color.blue(), color.alpha())) |
|
102 |
|
103 self.__colorAlpha.setValue(self.__currentAlpha) |
|
104 |
|
105 def __selectColor(self): |
|
106 """ |
|
107 Private slot to select a new drawing color. |
|
108 """ |
|
109 col = QColorDialog.getColor(self.__currentColor) |
|
110 col.setAlpha(self.__currentAlpha) |
|
111 |
|
112 if col.isValid(): |
|
113 self.emit(SIGNAL("colorSelected(QColor)"), col) |
|
114 |
|
115 def __alphaChanged(self, val): |
|
116 """ |
|
117 Private slot to track changes of the alpha channel. |
|
118 |
|
119 @param val value of the alpha channel |
|
120 """ |
|
121 if val != self.__currentAlpha: |
|
122 col = QColor(self.__currentColor) |
|
123 col.setAlpha(val) |
|
124 self.emit(SIGNAL("colorSelected(QColor)"), col) |