|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2003 - 2021 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the color dialog wizard dialog. |
|
8 """ |
|
9 |
|
10 import os |
|
11 |
|
12 from PyQt5.QtCore import pyqtSlot |
|
13 from PyQt5.QtGui import QColor |
|
14 from PyQt5.QtWidgets import QColorDialog, QDialog, QDialogButtonBox |
|
15 |
|
16 from E5Gui import E5MessageBox |
|
17 |
|
18 from .Ui_ColorDialogWizardDialog import Ui_ColorDialogWizardDialog |
|
19 |
|
20 |
|
21 class ColorDialogWizardDialog(QDialog, Ui_ColorDialogWizardDialog): |
|
22 """ |
|
23 Class implementing the color dialog wizard dialog. |
|
24 |
|
25 It displays a dialog for entering the parameters |
|
26 for the QColorDialog code generator. |
|
27 """ |
|
28 def __init__(self, parent=None): |
|
29 """ |
|
30 Constructor |
|
31 |
|
32 @param parent parent widget (QWidget) |
|
33 """ |
|
34 super().__init__(parent) |
|
35 self.setupUi(self) |
|
36 |
|
37 self.bTest = self.buttonBox.addButton( |
|
38 self.tr("Test"), QDialogButtonBox.ButtonRole.ActionRole) |
|
39 |
|
40 msh = self.minimumSizeHint() |
|
41 self.resize(max(self.width(), msh.width()), msh.height()) |
|
42 |
|
43 def on_buttonBox_clicked(self, button): |
|
44 """ |
|
45 Private slot called by a button of the button box clicked. |
|
46 |
|
47 @param button button that was clicked (QAbstractButton) |
|
48 """ |
|
49 if button == self.bTest: |
|
50 self.on_bTest_clicked() |
|
51 |
|
52 @pyqtSlot() |
|
53 def on_bTest_clicked(self): |
|
54 """ |
|
55 Private method to test the selected options. |
|
56 """ |
|
57 if self.rColor.isChecked(): |
|
58 if not self.eColor.currentText(): |
|
59 QColorDialog.getColor() |
|
60 else: |
|
61 coStr = self.eColor.currentText() |
|
62 if coStr.startswith('#'): |
|
63 coStr = "QColor('{0}')".format(coStr) |
|
64 else: |
|
65 coStr = "QColor({0})".format(coStr) |
|
66 try: |
|
67 exec('from PyQt5.QtCore import Qt;' # secok |
|
68 ' QColorDialog.getColor({0}, None, "{1}")'.format( |
|
69 coStr, self.eTitle.text())) |
|
70 except Exception: |
|
71 E5MessageBox.critical( |
|
72 self, |
|
73 self.tr("QColorDialog Wizard Error"), |
|
74 self.tr( |
|
75 """<p>The color <b>{0}</b> is not valid.</p>""") |
|
76 .format(coStr)) |
|
77 |
|
78 elif self.rRGBA.isChecked(): |
|
79 QColorDialog.getColor( |
|
80 QColor(self.sRed.value(), self.sGreen.value(), |
|
81 self.sBlue.value(), self.sAlpha.value()), |
|
82 None, self.eTitle.text(), |
|
83 QColorDialog.ColorDialogOptions( |
|
84 QColorDialog.ColorDialogOption.ShowAlphaChannel)) |
|
85 |
|
86 def on_eRGB_textChanged(self, text): |
|
87 """ |
|
88 Private slot to handle the textChanged signal of eRGB. |
|
89 |
|
90 @param text the new text (string) |
|
91 """ |
|
92 if not text: |
|
93 self.sRed.setEnabled(True) |
|
94 self.sGreen.setEnabled(True) |
|
95 self.sBlue.setEnabled(True) |
|
96 self.sAlpha.setEnabled(True) |
|
97 self.bTest.setEnabled(True) |
|
98 else: |
|
99 self.sRed.setEnabled(False) |
|
100 self.sGreen.setEnabled(False) |
|
101 self.sBlue.setEnabled(False) |
|
102 self.sAlpha.setEnabled(False) |
|
103 self.bTest.setEnabled(False) |
|
104 |
|
105 def on_eColor_editTextChanged(self, text): |
|
106 """ |
|
107 Private slot to handle the editTextChanged signal of eColor. |
|
108 |
|
109 @param text the new text (string) |
|
110 """ |
|
111 if not text or text.startswith('Qt.') or text.startswith('#'): |
|
112 self.bTest.setEnabled(True) |
|
113 else: |
|
114 self.bTest.setEnabled(False) |
|
115 |
|
116 def getCode(self, indLevel, indString): |
|
117 """ |
|
118 Public method to get the source code. |
|
119 |
|
120 @param indLevel indentation level (int) |
|
121 @param indString string used for indentation (space or tab) (string) |
|
122 @return generated code (string) |
|
123 """ |
|
124 # calculate our indentation level and the indentation string |
|
125 il = indLevel + 1 |
|
126 istring = il * indString |
|
127 estring = os.linesep + indLevel * indString |
|
128 |
|
129 # now generate the code |
|
130 if self.parentSelf.isChecked(): |
|
131 parent = "self" |
|
132 elif self.parentNone.isChecked(): |
|
133 parent = "None" |
|
134 elif self.parentOther.isChecked(): |
|
135 parent = self.parentEdit.text() |
|
136 if parent == "": |
|
137 parent = "None" |
|
138 |
|
139 resvar = self.eResultVar.text() |
|
140 if not resvar: |
|
141 resvar = "color" |
|
142 code = '{0} = QColorDialog.'.format(resvar) |
|
143 if self.rColor.isChecked(): |
|
144 code += 'getColor({0}'.format(os.linesep) |
|
145 if self.eColor.currentText(): |
|
146 col = self.eColor.currentText() |
|
147 if col.startswith('#'): |
|
148 code += '{0}QColor("{1}"),{2}'.format( |
|
149 istring, col, os.linesep) |
|
150 else: |
|
151 code += '{0}QColor({1}),{2}'.format( |
|
152 istring, col, os.linesep) |
|
153 else: |
|
154 code += '{0}QColor(Qt.GlobalColor.white),{1}'.format( |
|
155 istring, os.linesep) |
|
156 code += '{0}{1},{2}'.format(istring, parent, os.linesep) |
|
157 code += '{0}self.tr("{1}"),{2}'.format( |
|
158 istring, self.eTitle.text(), os.linesep) |
|
159 code += ( |
|
160 '{0}QColorDialog.ColorDialogOptions(' |
|
161 'QColorDialog.ColorDialogOption.ShowAlphaChannel)' |
|
162 .format(istring) |
|
163 ) |
|
164 code += '){0}'.format(estring) |
|
165 elif self.rRGBA.isChecked(): |
|
166 code += 'getColor({0}'.format(os.linesep) |
|
167 if not self.eRGB.text(): |
|
168 code += '{0}QColor({1:d}, {2:d}, {3:d}, {4:d}),{5}'.format( |
|
169 istring, self.sRed.value(), self.sGreen.value(), |
|
170 self.sBlue.value(), self.sAlpha.value(), os.linesep) |
|
171 else: |
|
172 code += '{0}{1},{2}'.format( |
|
173 istring, self.eRGB.text(), os.linesep) |
|
174 code += '{0}{1},{2}'.format(istring, parent, os.linesep) |
|
175 code += '{0}self.tr("{1}"),{2}'.format( |
|
176 istring, self.eTitle.text(), os.linesep) |
|
177 code += ( |
|
178 '{0}QColorDialog.ColorDialogOptions(' |
|
179 'QColorDialog.ColorDialogOption.ShowAlphaChannel)' |
|
180 .format(istring) |
|
181 ) |
|
182 code += '){0}'.format(estring) |
|
183 |
|
184 return code |