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