8 """ |
8 """ |
9 |
9 |
10 from PyQt6.QtCore import pyqtSignal, Qt |
10 from PyQt6.QtCore import pyqtSignal, Qt |
11 from PyQt6.QtGui import QColor, QPainter, QPixmap |
11 from PyQt6.QtGui import QColor, QPainter, QPixmap |
12 from PyQt6.QtWidgets import ( |
12 from PyQt6.QtWidgets import ( |
13 QWidget, QBoxLayout, QLabel, QFrame, QPushButton, QSpinBox, QGroupBox, |
13 QWidget, |
14 QVBoxLayout, QRadioButton, QSpacerItem, QSizePolicy, QColorDialog |
14 QBoxLayout, |
|
15 QLabel, |
|
16 QFrame, |
|
17 QPushButton, |
|
18 QSpinBox, |
|
19 QGroupBox, |
|
20 QVBoxLayout, |
|
21 QRadioButton, |
|
22 QSpacerItem, |
|
23 QSizePolicy, |
|
24 QColorDialog, |
15 ) |
25 ) |
16 |
26 |
17 |
27 |
18 class IconEditorPalette(QWidget): |
28 class IconEditorPalette(QWidget): |
19 """ |
29 """ |
20 Class implementing a palette widget for the icon editor. |
30 Class implementing a palette widget for the icon editor. |
21 |
31 |
22 @signal colorSelected(QColor) emitted after a new color has been selected |
32 @signal colorSelected(QColor) emitted after a new color has been selected |
23 @signal compositingChanged(QPainter.CompositionMode) emitted to signal a |
33 @signal compositingChanged(QPainter.CompositionMode) emitted to signal a |
24 change of the compositing mode |
34 change of the compositing mode |
25 """ |
35 """ |
|
36 |
26 colorSelected = pyqtSignal(QColor) |
37 colorSelected = pyqtSignal(QColor) |
27 compositingChanged = pyqtSignal(QPainter.CompositionMode) |
38 compositingChanged = pyqtSignal(QPainter.CompositionMode) |
28 |
39 |
29 def __init__(self, parent=None): |
40 def __init__(self, parent=None): |
30 """ |
41 """ |
31 Constructor |
42 Constructor |
32 |
43 |
33 @param parent reference to the parent widget (QWidget) |
44 @param parent reference to the parent widget (QWidget) |
34 """ |
45 """ |
35 super().__init__(parent) |
46 super().__init__(parent) |
36 |
47 |
37 direction = ( |
48 direction = ( |
38 QBoxLayout.Direction.LeftToRight |
49 QBoxLayout.Direction.LeftToRight |
39 if self.layoutDirection == Qt.Orientation.Horizontal else |
50 if self.layoutDirection == Qt.Orientation.Horizontal |
40 QBoxLayout.Direction.TopToBottom |
51 else QBoxLayout.Direction.TopToBottom |
41 ) |
52 ) |
42 self.__layout = QBoxLayout(direction, self) |
53 self.__layout = QBoxLayout(direction, self) |
43 self.setLayout(self.__layout) |
54 self.setLayout(self.__layout) |
44 |
55 |
45 self.__preview = QLabel(self) |
56 self.__preview = QLabel(self) |
46 self.__preview.setFrameStyle(QFrame.Shape.Panel | QFrame.Shadow.Sunken) |
57 self.__preview.setFrameStyle(QFrame.Shape.Panel | QFrame.Shadow.Sunken) |
47 self.__preview.setFixedHeight(64) |
58 self.__preview.setFixedHeight(64) |
48 self.__preview.setAlignment( |
59 self.__preview.setAlignment( |
49 Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter) |
60 Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter |
50 self.__preview.setWhatsThis(self.tr( |
61 ) |
51 """<b>Preview</b>""" |
62 self.__preview.setWhatsThis( |
52 """<p>This is a 1:1 preview of the current icon.</p>""" |
63 self.tr( |
53 )) |
64 """<b>Preview</b>""" |
|
65 """<p>This is a 1:1 preview of the current icon.</p>""" |
|
66 ) |
|
67 ) |
54 self.__layout.addWidget(self.__preview) |
68 self.__layout.addWidget(self.__preview) |
55 |
69 |
56 self.__color = QLabel(self) |
70 self.__color = QLabel(self) |
57 self.__color.setFrameStyle(QFrame.Shape.Panel | QFrame.Shadow.Sunken) |
71 self.__color.setFrameStyle(QFrame.Shape.Panel | QFrame.Shadow.Sunken) |
58 self.__color.setFixedHeight(24) |
72 self.__color.setFixedHeight(24) |
59 self.__color.setAlignment( |
73 self.__color.setAlignment( |
60 Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter) |
74 Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter |
61 self.__color.setWhatsThis(self.tr( |
75 ) |
62 """<b>Current Color</b>""" |
76 self.__color.setWhatsThis( |
63 """<p>This is the currently selected color used for drawing.</p>""" |
77 self.tr( |
64 )) |
78 """<b>Current Color</b>""" |
|
79 """<p>This is the currently selected color used for drawing.</p>""" |
|
80 ) |
|
81 ) |
65 self.__layout.addWidget(self.__color) |
82 self.__layout.addWidget(self.__color) |
66 |
83 |
67 self.__colorTxt = QLabel(self) |
84 self.__colorTxt = QLabel(self) |
68 self.__colorTxt.setAlignment( |
85 self.__colorTxt.setAlignment( |
69 Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter) |
86 Qt.AlignmentFlag.AlignHCenter | Qt.AlignmentFlag.AlignVCenter |
70 self.__colorTxt.setWhatsThis(self.tr( |
87 ) |
71 """<b>Current Color Value</b>""" |
88 self.__colorTxt.setWhatsThis( |
72 """<p>This is the currently selected color value used for""" |
89 self.tr( |
73 """ drawing.</p>""" |
90 """<b>Current Color Value</b>""" |
74 )) |
91 """<p>This is the currently selected color value used for""" |
|
92 """ drawing.</p>""" |
|
93 ) |
|
94 ) |
75 self.__layout.addWidget(self.__colorTxt) |
95 self.__layout.addWidget(self.__colorTxt) |
76 |
96 |
77 self.__colorButton = QPushButton(self.tr("Select Color"), self) |
97 self.__colorButton = QPushButton(self.tr("Select Color"), self) |
78 self.__colorButton.setWhatsThis(self.tr( |
98 self.__colorButton.setWhatsThis( |
79 """<b>Select Color</b>""" |
99 self.tr( |
80 """<p>Select the current drawing color via a color selection""" |
100 """<b>Select Color</b>""" |
81 """ dialog.</p>""" |
101 """<p>Select the current drawing color via a color selection""" |
82 )) |
102 """ dialog.</p>""" |
|
103 ) |
|
104 ) |
83 self.__colorButton.clicked.connect(self.__selectColor) |
105 self.__colorButton.clicked.connect(self.__selectColor) |
84 self.__layout.addWidget(self.__colorButton) |
106 self.__layout.addWidget(self.__colorButton) |
85 |
107 |
86 self.__colorAlpha = QSpinBox(self) |
108 self.__colorAlpha = QSpinBox(self) |
87 self.__colorAlpha.setRange(0, 255) |
109 self.__colorAlpha.setRange(0, 255) |
88 self.__colorAlpha.setWhatsThis(self.tr( |
110 self.__colorAlpha.setWhatsThis( |
89 """<b>Select alpha channel value</b>""" |
111 self.tr( |
90 """<p>Select the value for the alpha channel of the current""" |
112 """<b>Select alpha channel value</b>""" |
91 """ color.</p>""" |
113 """<p>Select the value for the alpha channel of the current""" |
92 )) |
114 """ color.</p>""" |
|
115 ) |
|
116 ) |
93 self.__layout.addWidget(self.__colorAlpha) |
117 self.__layout.addWidget(self.__colorAlpha) |
94 self.__colorAlpha.valueChanged[int].connect(self.__alphaChanged) |
118 self.__colorAlpha.valueChanged[int].connect(self.__alphaChanged) |
95 |
119 |
96 self.__compositingGroup = QGroupBox(self.tr("Compositing"), self) |
120 self.__compositingGroup = QGroupBox(self.tr("Compositing"), self) |
97 self.__compositingGroupLayout = QVBoxLayout(self.__compositingGroup) |
121 self.__compositingGroupLayout = QVBoxLayout(self.__compositingGroup) |
98 self.__compositingGroup.setLayout(self.__compositingGroupLayout) |
122 self.__compositingGroup.setLayout(self.__compositingGroupLayout) |
99 self.__sourceButton = QRadioButton(self.tr("Replace"), |
123 self.__sourceButton = QRadioButton(self.tr("Replace"), self.__compositingGroup) |
100 self.__compositingGroup) |
124 self.__sourceButton.setWhatsThis( |
101 self.__sourceButton.setWhatsThis(self.tr( |
125 self.tr( |
102 """<b>Replace</b>""" |
126 """<b>Replace</b>""" |
103 """<p>Replace the existing pixel with a new color.</p>""" |
127 """<p>Replace the existing pixel with a new color.</p>""" |
104 )) |
128 ) |
|
129 ) |
105 self.__sourceButton.clicked[bool].connect(self.__compositingChanged) |
130 self.__sourceButton.clicked[bool].connect(self.__compositingChanged) |
106 self.__compositingGroupLayout.addWidget(self.__sourceButton) |
131 self.__compositingGroupLayout.addWidget(self.__sourceButton) |
107 self.__sourceOverButton = QRadioButton(self.tr("Blend"), |
132 self.__sourceOverButton = QRadioButton( |
108 self.__compositingGroup) |
133 self.tr("Blend"), self.__compositingGroup |
109 self.__sourceOverButton.setWhatsThis(self.tr( |
134 ) |
110 """<b>Blend</b>""" |
135 self.__sourceOverButton.setWhatsThis( |
111 """<p>Blend the new color over the existing pixel.</p>""" |
136 self.tr( |
112 )) |
137 """<b>Blend</b>""" |
|
138 """<p>Blend the new color over the existing pixel.</p>""" |
|
139 ) |
|
140 ) |
113 self.__sourceOverButton.setChecked(True) |
141 self.__sourceOverButton.setChecked(True) |
114 self.__sourceOverButton.clicked[bool].connect( |
142 self.__sourceOverButton.clicked[bool].connect(self.__compositingChanged) |
115 self.__compositingChanged) |
|
116 self.__compositingGroupLayout.addWidget(self.__sourceOverButton) |
143 self.__compositingGroupLayout.addWidget(self.__sourceOverButton) |
117 self.__layout.addWidget(self.__compositingGroup) |
144 self.__layout.addWidget(self.__compositingGroup) |
118 |
145 |
119 spacer = QSpacerItem( |
146 spacer = QSpacerItem( |
120 10, 10, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Expanding) |
147 10, 10, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Expanding |
|
148 ) |
121 self.__layout.addItem(spacer) |
149 self.__layout.addItem(spacer) |
122 |
150 |
123 def previewChanged(self, pixmap): |
151 def previewChanged(self, pixmap): |
124 """ |
152 """ |
125 Public slot to update the preview. |
153 Public slot to update the preview. |
126 |
154 |
127 @param pixmap new preview pixmap (QPixmap) |
155 @param pixmap new preview pixmap (QPixmap) |
128 """ |
156 """ |
129 self.__preview.setPixmap(pixmap) |
157 self.__preview.setPixmap(pixmap) |
130 |
158 |
131 def colorChanged(self, color): |
159 def colorChanged(self, color): |
132 """ |
160 """ |
133 Public slot to update the color preview. |
161 Public slot to update the color preview. |
134 |
162 |
135 @param color new color (QColor) |
163 @param color new color (QColor) |
136 """ |
164 """ |
137 self.__currentColor = color |
165 self.__currentColor = color |
138 self.__currentAlpha = color.alpha() |
166 self.__currentAlpha = color.alpha() |
139 |
167 |
140 pm = QPixmap(90, 18) |
168 pm = QPixmap(90, 18) |
141 pm.fill(color) |
169 pm.fill(color) |
142 self.__color.setPixmap(pm) |
170 self.__color.setPixmap(pm) |
143 |
171 |
144 self.__colorTxt.setText( |
172 self.__colorTxt.setText( |
145 "{0:d}, {1:d}, {2:d}, {3:d}".format( |
173 "{0:d}, {1:d}, {2:d}, {3:d}".format( |
146 color.red(), color.green(), color.blue(), color.alpha())) |
174 color.red(), color.green(), color.blue(), color.alpha() |
147 |
175 ) |
|
176 ) |
|
177 |
148 self.__colorAlpha.setValue(self.__currentAlpha) |
178 self.__colorAlpha.setValue(self.__currentAlpha) |
149 |
179 |
150 def __selectColor(self): |
180 def __selectColor(self): |
151 """ |
181 """ |
152 Private slot to select a new drawing color. |
182 Private slot to select a new drawing color. |
153 """ |
183 """ |
154 col = QColorDialog.getColor(self.__currentColor) |
184 col = QColorDialog.getColor(self.__currentColor) |
155 col.setAlpha(self.__currentAlpha) |
185 col.setAlpha(self.__currentAlpha) |
156 |
186 |
157 if col.isValid(): |
187 if col.isValid(): |
158 self.colorSelected.emit(col) |
188 self.colorSelected.emit(col) |
159 |
189 |
160 def __alphaChanged(self, val): |
190 def __alphaChanged(self, val): |
161 """ |
191 """ |
162 Private slot to track changes of the alpha channel. |
192 Private slot to track changes of the alpha channel. |
163 |
193 |
164 @param val value of the alpha channel |
194 @param val value of the alpha channel |
165 """ |
195 """ |
166 if val != self.__currentAlpha: |
196 if val != self.__currentAlpha: |
167 col = QColor(self.__currentColor) |
197 col = QColor(self.__currentColor) |
168 col.setAlpha(val) |
198 col.setAlpha(val) |
169 self.colorSelected.emit(col) |
199 self.colorSelected.emit(col) |
170 |
200 |
171 def setCompositingMode(self, mode): |
201 def setCompositingMode(self, mode): |
172 """ |
202 """ |
173 Public method to set the compositing mode. |
203 Public method to set the compositing mode. |
174 |
204 |
175 @param mode compositing mode to set (QPainter.CompositionMode) |
205 @param mode compositing mode to set (QPainter.CompositionMode) |
176 """ |
206 """ |
177 if mode == QPainter.CompositionMode.CompositionMode_Source: |
207 if mode == QPainter.CompositionMode.CompositionMode_Source: |
178 self.__sourceButton.setChecked(True) |
208 self.__sourceButton.setChecked(True) |
179 elif mode == QPainter.CompositionMode.CompositionMode_SourceOver: |
209 elif mode == QPainter.CompositionMode.CompositionMode_SourceOver: |
180 self.__sourceOverButton.setChecked(True) |
210 self.__sourceOverButton.setChecked(True) |
181 |
211 |
182 def __compositingChanged(self, on): |
212 def __compositingChanged(self, on): |
183 """ |
213 """ |
184 Private slot to handle a change of the compositing mode. |
214 Private slot to handle a change of the compositing mode. |
185 |
215 |
186 @param on flag indicating the checked state of the compositing button |
216 @param on flag indicating the checked state of the compositing button |
187 (boolean) |
217 (boolean) |
188 """ |
218 """ |
189 if on: |
219 if on: |
190 if self.__sourceButton.isChecked(): |
220 if self.__sourceButton.isChecked(): |
191 self.compositingChanged.emit( |
221 self.compositingChanged.emit( |
192 QPainter.CompositionMode.CompositionMode_Source) |
222 QPainter.CompositionMode.CompositionMode_Source |
|
223 ) |
193 elif self.__sourceOverButton.isChecked(): |
224 elif self.__sourceOverButton.isChecked(): |
194 self.compositingChanged.emit( |
225 self.compositingChanged.emit( |
195 QPainter.CompositionMode.CompositionMode_SourceOver) |
226 QPainter.CompositionMode.CompositionMode_SourceOver |
|
227 ) |