-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
executable file
·164 lines (135 loc) · 4.79 KB
/
config.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
__all__ = ["ConfigUI", "Config"]
from PySide.QtCore import Slot, Signal, QCoreApplication, QSettings
from PySide.QtGui import QWidget
from configUI import Ui_config
version = "0.1.0"
duree_defaut = "14"
class ConfigUI(QWidget):
# signaux
u"""Signal envoyé quand la duree a été modifiée"""
majDuree = Signal()
CONFIG_TEXT = 0
CONFIG_INT = 1
CONFIG_BOOL = 2
CONFIG_PWD = 3
"""Classe chargée de l'interface de configuration de l'application"""
def __init__(self, parent=None):
super(ConfigUI, self).__init__(parent)
self.__conf = Config.getInstance()
self.__createWidgets()
def setMap(self):
# mapping nom -> (contrôle, callback de setting)
self.__map = {
"signature": (# name
self._ui.leSignature, # widget
ConfigUI.CONFIG_TEXT
),
"duree": (
self._ui.sbDuree,
ConfigUI.CONFIG_INT
),
"email": (
self._ui.leEmail,
ConfigUI.CONFIG_TEXT
),
"serveur": (
self._ui.leServeur,
ConfigUI.CONFIG_TEXT
),
"port": (
self._ui.sbPort,
ConfigUI.CONFIG_INT
),
"securite": (
self._ui.cbSecurite,
ConfigUI.CONFIG_PWD
),
"login": (
self._ui.leLogin,
ConfigUI.CONFIG_TEXT
),
"password": (
self._ui.lePassword,
ConfigUI.CONFIG_TEXT
),
}
def __createWidgets(self):
"""Créée les widgets de l'interface graphique"""
self._ui = Ui_config()
self._ui.setupUi(self)
self.setMap()
self._ui.wMail.setVisible(False)
for key in self.__map.keys():
widget = self.__map[key][0]
widget.setProperty("name", key)
if self.__map[key][1] == ConfigUI.CONFIG_TEXT:
widget.textChanged.connect(self.valueChanged)
if not self.__conf[key]:
try:
self.__conf[key] = self.__map[key][2]
except:
self.__conf[key] = ""
widget.setText(self.__conf[key])
elif self.__map[key][1] == ConfigUI.CONFIG_INT:
self.__map[key][0].valueChanged.connect(self.valueChanged)
self._ui.leEmail.setText(self.__conf["email"])
self._ui.leSignature.setText(self.__conf["signature"])
if not self.__conf["duree"]:
self.__conf["duree"] = duree_defaut
self._ui.sbDuree.setValue(int(self.__conf["duree"]))
self._ui.leServeur.setText(self.__conf["serveur"])
self._ui.cbMail.stateChanged.connect(self._ui.wMail.setVisible)
@Slot()
def valueChanged(self, value):
"""Enregistre la valeur modifiée, dans la propriété correspondante"""
key = self.sender().property("name")
self.__conf[key] = value
if key == "duree" or key == "signature":
self.majDuree.emit()
class Config():
"""Classe chargée de la lecture/écriture/sauvegarde de la configuration"""
_instance = None
_path = "private/config"
def __init__(self):
assert self._instance == None
Config._instance = self
QCoreApplication.setOrganizationName("nicolas.carrier")
QCoreApplication.setApplicationVersion(version)
QCoreApplication.setApplicationName("gem")
self.__settings = QSettings()
@staticmethod
def getInstance():
if Config._instance == None:
Config._instance = Config()
return Config._instance
def keys(self):
return self.__settings.allKeys()
def __getitem__(self, key):
"""Méthode magique sous-jacent à l'opérateur [] en lecture"""
if key in self.keys():
return self.__settings.value(key)
else:
return ""
def __setitem__(self, key, value):
"""Méthode magique sous-jacent à l'opérateur [] en écriture"""
self.__settings.setValue(key, value)
def __delitem__(self, key):
"""Méthode magique sous-jacent à l'opérateur del"""
self.__settings.remove(key)
if __name__ == "__main__":
"""Main de test"""
import sys
from PySide.QtGui import QApplication
from PySide.QtCore import QLibraryInfo, QLocale, QTranslator
app = QApplication(sys.argv)
locale = QLocale.system().name()
translator = QTranslator()
translator.load("qt_" + locale,
QLibraryInfo.location(QLibraryInfo.TranslationsPath))
app.installTranslator(translator)
ui = ConfigUI()
ui.show()
ret = app.exec_()
sys.exit(ret)