summaryrefslogtreecommitdiff
path: root/winSettings.py
blob: e6f3f297b8d6d8d29486923a960bfa19d372e3eb (plain)
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
from PyQt4 import QtGui, QtCore
import sys

from misc import *
import plugins


class winSettings(QtGui.QWidget):
    btnSave=None
    btnClose=None
    lstPlugins=None

    winMain=None
    settings = None


    def __init__(self, winMain, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.settings = QtCore.QSettings(ORGNAME, APPNAME)
        self.winMain=winMain

        self.btnSave=Button('save all', self.onBtnSaveClick)
        self.btnClose=Button('close', self.onBtnCloseClick)

        self.lstPlugins=QtGui.QListWidget(self)
        layoutWin=QtGui.QVBoxLayout()
        layoutButtons=QtGui.QHBoxLayout()

        tabWidget=QtGui.QTabWidget(parent)
        self.setLayout(layoutWin)

        tabWidget.addTab(self.lstPlugins, "plugins")
        for k,entry in plugins.listPlugins().iteritems():
            plugin=entry[plugins.PLUGIN_INSTANCE]
            if plugin:
                tabWidget.addTab(plugin.getSettingsWidget(), plugin.getName())
        self.fillList()

        layoutWin.addWidget(tabWidget)
        layoutWin.addLayout(layoutButtons)

        layoutButtons.addStretch()
        layoutButtons.addWidget(self.btnSave)
        layoutButtons.addWidget(self.btnClose)

        self.connect(self.lstPlugins, QtCore.SIGNAL('itemChanged (QListWidgetItem*)'), self.onlstPluginItemChanged)

        self.setWindowIcon(QtGui.QIcon(appIcon))
        self.setWindowTitle('Settings')
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.center()
        self.resize(800,400)
        doEvents()

    def fillList(self):
        self.lstPlugins.clear()
        for k,entry in plugins.listPlugins().iteritems():
            plugin=entry[plugins.PLUGIN_INSTANCE]
            if plugin:
                if entry[plugins.PLUGIN_MSG]:
                    item=QtGui.QListWidgetItem("%s\t%s"%(entry[plugins.PLUGIN_CLASS], entry[plugins.PLUGIN_MSG]))
                    item.setCheckState(QtCore.Qt.Unchecked)
                    item.setTextColor(QtCore.Qt.red)
                else:
                    item=QtGui.QListWidgetItem("%s\t%s"%(entry[plugins.PLUGIN_CLASS], plugin.getInfo()))
                    if plugin.isLoaded():
                        item.setCheckState(QtCore.Qt.Checked)
                    else:
                        item.setCheckState(QtCore.Qt.Unchecked)

                    if self.settings.value(plugin.getName() + '/load') == None:
                        # load new plugins by default
                        item.setTextColor(QtCore.Qt.blue)
                        self.settings.setValue(plugin.getName() + '/load', QtCore.QVariant(True))

            else:
                item=QtGui.QListWidgetItem("%s\t%s"%(entry[plugins.PLUGIN_CLASS], entry[plugins.PLUGIN_MSG]))
                item.setCheckState(QtCore.Qt.Unchecked)
                item.setTextColor(QtCore.Qt.red)
            self.lstPlugins.addItem(item)


    def center(self):
        screen = QtGui.QDesktopWidget().screenGeometry()
        size = self.geometry()
        self.move((screen.width()-size.width())/2, (screen.height()-size.height())/2+100)

    def onBtnSaveClick(self):
        map(lambda entry: entry[plugins.PLUGIN_INSTANCE] and entry[plugins.PLUGIN_INSTANCE].saveSettings(), plugins.listPlugins().values())

    def onBtnCloseClick(self):
         self.close()
    def onlstPluginItemChanged(self, item):
        # check here if we have to load or unload the plugin!
        toload = (item.checkState() == QtCore.Qt.Checked)
        className=str(item.text()[0:str(item.text()).find('\t')])
        if toload:
            # refresh the plugin file
            plugin=plugins.loadPlugin(className, self.winMain)
            if plugin:
                plugin.load()
            self.fillList()
            self.winMain.restoreLayout()
        else:
            plugin=plugins.getPlugin(className)
            if plugin:
                plugin.unload()
        if plugin:
            self.settings.setValue(plugin.getName() + '/load', QtCore.QVariant(toload))
    def closeEvent(self, event):
        map(lambda entry: entry[plugins.PLUGIN_INSTANCE] and entry[plugins.PLUGIN_INSTANCE].resetSettingCache(), plugins.listPlugins().values())
        self.winMain.wSettings=None