Plugins/WizardPlugins/ColorDialogWizard/ColorDialogWizardDialog.py

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

eric ide

mercurial