# -*- 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))