Menu

[r253]: / trunk / configsystem.py  Maximize  Restore  History

Download this file

360 lines (324 with data), 13.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# -*- coding: utf-8 -*-
import os, pickle, pluginsystem
from PyQt4 import QtCore, QtGui
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import globalvars as g
"""
bool: [type], [Text for label]
int: [type], [Text for label], [Min value], [Max value]
string: [type], [Text for label]
password: [type], [Text for label]
combo: [type], [Text for label], [Dictionary of options for QComboBox]
color: [type], [Text for label]
slider: [type], [Text for label], [Min value], [Max value], [Step]
image: [type], [Text for label]
options = (
('tab1',
(
('optionName1', ('bool', "Text...")),
('optionName2', ('bool', "Text...")),
)
),
('tab2',
(
('optionName3', ('int', "Text...", 300, None )),
('optionName4', ('int', "Text...", 0, None)),
('optionName5', ('int', "Text...", 0, 9999)),
)
),
('tab3',
(
('optionName6', ('string', 'Text...' )),
('optionName7', ('password', 'Text...' )),
)
),
('tab4',
(
('optionName8', ('combo', "Text...", ('Option 1' : 'Value 1', 'Option 2' : 'Value 2'))),
('optionName9', ('color', "Text...")),
('optionName10', ('slider', "Text...", 0, 10, 1)),
('optionName11', ('image', "Text...")),
)
)
)
"""
global config_dialog_ptr
def plugin_has_stored_prefs(config_folder):
try:
if not os.path.exists(g.plugins_config_path + config_folder + os.path.sep):
return False
elif not os.path.exists(g.plugins_config_path + config_folder + os.path.sep + "config"):
return False
else:
return True
except:
return False
def load_config(config_folder, default_config_prefs):
if plugin_has_stored_prefs(config_folder):
try:
f = open(g.plugins_config_path + config_folder + os.path.sep + "config", "r")
config_prefs = pickle.load(f)
f.close()
if sorted(default_config_prefs.keys()) == sorted(config_prefs.keys()):
return config_prefs
else:
return default_config_prefs.update(config_prefs)
except:
return default_config_prefs
else:
save_config(config_folder, default_config_prefs)
return default_config_prefs
def save_config(config_folder, config_prefs):
try:
if not os.path.exists(g.plugins_config_path + config_folder + os.path.sep):
os.makedirs(g.plugins_config_path + config_folder + os.path.sep)
if not os.path.exists(g.plugins_config_path + config_folder + os.path.sep + "config"):
f = open(g.plugins_config_path + config_folder + os.path.sep + "config", "a+")
f.close()
f = open(g.plugins_config_path + config_folder + os.path.sep + "config", "w")
pickle.dump(config_prefs, f)
f.close()
except BaseException, e:
print e
def config_dialog(config_folder, config_struct, config_prefs, parent=None):
global config_dialog_ptr
config_dialog_ptr = ConfigDialog(config_folder, config_struct, config_prefs, parent)
class ConfigDialog(QtGui.QDialog):
def __init__(self, config_folder, config_struct, config_prefs, parent=None):
QtGui.QDialog.__init__(self, parent)
self.parent = parent
self.options = config_struct
self.config_folder = config_folder
self.setWindowTitle(config_folder)
self.setObjectName("configWidget")
self.resize(600, 300)
self.verticalLayout = QtGui.QVBoxLayout(self)
self.verticalLayout.setObjectName("configVerticalLayout")
self.tabs = QtGui.QTabWidget(self)
self.tabs.setObjectName("configTabWidget")
self.tabs.setDocumentMode(True)
self.verticalLayout.addWidget(self.tabs)
self.buttonBox = QtGui.QDialogButtonBox(self)
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("configButtonBox")
self.verticalLayout.addWidget(self.buttonBox)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("accepted()"), self.accept)
QtCore.QObject.connect(self.buttonBox, QtCore.SIGNAL("rejected()"), self.reject)
QtCore.QMetaObject.connectSlotsByName(self)
self.show()
for tab in range(0, len(self.options)):
tabName = self.options[tab][0]
pagewidget = QtGui.QWidget(self)
layout=QtGui.QGridLayout(pagewidget)
pagewidget.setLayout(layout)
row=-2
for option in range(0, len(self.options[tab][1])):
optionName = self.options[tab][1][option][0]
optionOptions = self.options[tab][1][option][1]
row+=2
if optionOptions[0]=='bool':
cb=QtGui.QCheckBox(optionOptions[1])
cb.setObjectName(optionName)
cb.setChecked(config_prefs[optionName])
layout.addWidget(cb, row, 0, 1, 2)
elif optionOptions[0]=='int':
label=QtGui.QLabel(optionOptions[1])
label.setWordWrap(True)
spin=QtGui.QSpinBox()
spin.setObjectName(optionName)
if optionOptions[2] is not None:
spin.setMinimum(optionOptions[2])
else:
spin.setMinimum(-99999)
if optionOptions[3] is not None:
spin.setMaximum(optionOptions[3])
else:
spin.setMaximum(99999)
spin.setValue(config_prefs[optionName])
layout.addWidget(label, row, 0)
layout.setColumnStretch(0, 1)
layout.addWidget(spin, row, 1)
elif optionOptions[0]=='string':
label=QtGui.QLabel(optionOptions[1])
label.setWordWrap(True)
text=QtGui.QLineEdit()
text.setObjectName(optionName)
if not config_prefs[optionName] == None:
text.setText(config_prefs[optionName])
layout.addWidget(label, row, 0)
layout.addWidget(text, row, 1)
elif optionOptions[0]=='password':
label=QtGui.QLabel(optionOptions[1])
label.setWordWrap(True)
text=QtGui.QLineEdit()
text.setObjectName(optionName)
text.setEchoMode(QtGui.QLineEdit.Password)
if not config_prefs[optionName] == None:
text.setText(config_prefs[optionName])
layout.addWidget(label, row, 0)
layout.addWidget(text, row, 1)
elif optionOptions[0] == 'combo':
label = QtGui.QLabel(optionOptions[1])
label.setWordWrap(True)
cb = QtGui.QComboBox()
cb.setObjectName(optionName)
for name, value in optionOptions[2].iteritems():
cb.addItem(name, value)
proxy = QSortFilterProxyModel(cb)
proxy.setSourceModel(cb.model())
cb.model().setParent(proxy)
cb.setModel(proxy)
cb.model().sort(0)
cb.setCurrentIndex(cb.findData(config_prefs[optionName]))
layout.addWidget(label, row, 0)
layout.setColumnStretch(0, 1)
layout.addWidget(cb, row, 1)
elif optionOptions[0] == 'color':
label = QtGui.QLabel(optionOptions[1])
label.setWordWrap(True)
button = ColorButton(self)
button.setObjectName(optionName)
button.setColor(config_prefs[optionName])
layout.addWidget(label, row, 0)
layout.setColumnStretch(0, 1)
layout.addWidget(button, row, 1)
elif optionOptions[0] == 'slider':
label = QtGui.QLabel(optionOptions[1])
label.setWordWrap(True)
slider = SliderWidget(self)
slider.setObjectName(optionName)
slider.setMinimum(optionOptions[2])
slider.setMaximum(optionOptions[3])
slider.setStep(optionOptions[4])
slider.setValue(config_prefs[optionName])
layout.addWidget(label, row, 0)
layout.setColumnStretch(0, 1)
layout.addWidget(slider, row, 1)
elif optionOptions[0] == 'image':
label = QtGui.QLabel(optionOptions[1])
label.setWordWrap(True)
button = ImageButton(self)
button.setObjectName(optionName)
button.setImage(config_prefs[optionName])
layout.addWidget(label, row, 0)
layout.setColumnStretch(0, 1)
layout.addWidget(button, row, 1)
separator=QtGui.QFrame()
separator.setFrameStyle(QtGui.QFrame.HLine | QtGui.QFrame.Plain)
layout.addWidget(separator, row+1, 0, 1, 3)
self.tabs.addTab(pagewidget, tabName)
self.tabs.setCurrentIndex(0)
def accept(self):
global config_dialog_ptr
options = self.options
newOptions = "{"
for tab in range(0, len(options)):
for option in range(0, len(options[tab][1])):
optionName = options[tab][1][option][0]
optionOptions = options[tab][1][option][1]
optionType = optionOptions[0]
controls = self.tabs.widget(tab).children()
for control in controls:
if control.objectName() == optionName:
if optionType == "int":
value = str(control.value())
newOptions += "'" + optionName + "':" + value + ","
elif optionType == "string" or optionType == "password":
value = unicode(control.text())
value = value.replace('"', '\\"')
newOptions += "'" + optionName + "':'" + value + "',"
elif optionType == "bool":
if control.isChecked():
value = True
else:
value = False
newOptions += "'" + optionName + "':" + str(value) + ","
elif optionType == "combo":
data = control.itemData(control.currentIndex())
value = unicode(data.toString())
if data.type() == QVariant.String:
newOptions += "'" + optionName + "':'" + value + "',"
else:
newOptions += "'" + optionName + "':" + value + ","
elif optionType == "color":
value = str(control.getColor())
newOptions += "'" + optionName + "':'" + value + "',"
elif optionType == "slider":
value = str(control.getValue())
newOptions += "'" + optionName + "':" + value + ","
elif optionType == "image":
value = unicode(control.getImage())
newOptions += "'" + optionName + "':'" + value + "',"
newOptions += "}"
save_config(self.config_folder, eval(newOptions))
pluginsystem.trigger_event("reload_config", plugin=self.config_folder)
config_dialog_ptr = None
self.done(0)
def reject(self):
global config_dialog_ptr
config_dialog_ptr = None
self.done(0)
class ColorButton(QtGui.QPushButton):
def __init__(self, parent=None):
QtGui.QPushButton.__init__(self, parent)
self.a = QtGui.QColorDialog(self)
self.c = QtGui.QPixmap(50, 7)
self.c.fill(QtCore.Qt.black)
self.setIcon(QtGui.QIcon(self.c))
self.setIconSize(QtCore.QSize(50, 15))
self.connect(self.a, QtCore.SIGNAL("colorSelected(QColor)"), self.colorSelected)
self.connect(self, QtCore.SIGNAL("clicked()"), self.a.show)
def colorSelected(self, color):
self.c.fill(self.a.selectedColor())
self.setIcon(QtGui.QIcon(self.c))
self.setProperty("colorname", color.name())
def setColor(self, s):
self.c.fill(QColor(s))
self.setIcon(QtGui.QIcon(self.c))
self.setProperty("colorname", s)
def getColor(self):
return self.property("colorname").toString()
class SliderWidget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.setGeometry(0, 0, 200, 30)
self.setMaximumHeight(30)
self.layout = QtGui.QGridLayout(self)
self.slider = QtGui.QSlider(QtCore.Qt.Horizontal, self)
self.slider.setGeometry(0, 0, 170, 30)
self.label = QtGui.QLabel(str(self.slider.value()), self)
self.layout.addWidget(self.slider, 0, 0)
self.layout.addWidget(self.label, 0, 1)
self.layout.setColumnMinimumWidth(0, 175)
self.layout.setColumnMinimumWidth(1, 25)
self.connect(self.slider, QtCore.SIGNAL("valueChanged(int)"), self.valueChanged)
def valueChanged(self, i):
self.label.setText(str(i))
def setValue(self, i):
self.slider.setValue(i)
def getValue(self):
return self.slider.value()
def setMinimum(self, minvalue):
self.slider.setMinimum(minvalue)
def setMaximum(self, maxvalue):
self.slider.setMaximum(maxvalue)
def setStep(self, step):
self.slider.setSingleStep(step)
class ImageButton(QtGui.QPushButton):
def __init__(self, parent=None):
QtGui.QPushButton.__init__(self, parent)
self.setIcon(QtGui.QIcon())
self.setIconSize(QtCore.QSize(100, 100))
self.setMinimumHeight(100)
self.setGeometry(0, 0, 100, 100)
self.connect(self, QtCore.SIGNAL("clicked()"), self.showDialog)
def showDialog(self):
filename = QtGui.QFileDialog.getOpenFileName(self, _("Choose an image"), "", QtCore.QString(_("Image Files") + "(*.png *.jpg *.jpeg *.bmp *.gif)"))
self.setProperty("filename", filename)
self.setIcon(QtGui.QIcon(filename))
def getImage(self):
return self.property("filename").toString()
def setImage(self, filename):
self.setIcon(QtGui.QIcon(filename))