plotter/qpreferences.py

changeset 39
8becd52c2171
parent 38
73bd2fe81005
child 40
1bbea188a7e5
equal deleted inserted replaced
38:73bd2fe81005 39:8becd52c2171
1 import os
2 from PyQt4 import QtCore, QtGui, uic
3 from tools import AbstractRegister
4
5 def fromVariant(v):
6 _cvrts = {0: lambda x:None,
7 1: lambda x:x.toBool(),
8 2: lambda x:x.toInt()[0],
9 6: lambda x:x.toDouble()[0],
10 10: lambda x:unicode(x.toString()),
11 12: lambda x:x.toByteArray(),
12 21: lambda x:x.toSize(),
13 22: lambda x:x.toSizeF(),
14 25: lambda x:x.toPoint(),
15 26: lambda x:x.toPointF(),
16 64: lambda x:QtGui.QFont(x),
17 67: lambda x:QtGui.QColor(x),
18 }
19 t = v.userType()
20 return _cvrts[t](v)
21
22 class PreferenceMetaclass(type):
23 _widgets = {}
24 def __init__(cls, name, bases, dct):
25 # called at class creation
26 super(type, cls).__init__(name, bases, dct)
27 if name != "BaseItem":
28 ItemRegister().add(cls)
29
30 class BaseItem(object):
31 #__metaclass__ = PreferenceMetaclass
32 _id = 0
33 def __init__(self, default=None, name=None, description=None, group=None):
34 self._default = default
35 self._id = BaseItem._id
36 self._name = name
37 self._description = description
38 self._group = group
39 BaseItem._id += 1
40
41 def validate(self, value):
42 return True
43
44 def __get__(self, obj, cls):
45 if obj is None: #when called from the class, return the Item itself
46 return self
47 try:
48 return obj.getPref(self._id)
49 except Exception, e:
50 return None
51
52 def __set__(self, obj, value):
53 obj.setPref(self._id, value)
54
55 class ItemRegister(AbstractRegister):
56 _registered_type = BaseItem
57 getItem = AbstractRegister.get_class
58
59 class PointItem(BaseItem):
60 _type = QtCore.QPoint
61
62 class SizeItem(BaseItem):
63 _type = QtCore.QSize
64
65 class ByteArrayItem(BaseItem):
66 _type = QtCore.QByteArray
67
68 class UnicodeItem(BaseItem):
69 _type = unicode
70 def validate(self, value):
71 return isinstance(value, basestring)
72
73 class IntItem(BaseItem):
74 _type = int
75 def __init__(self, default=None, name=None, description=None, group=None, min=None, max=None):
76 BaseItem.__init__(self, default, name, description, group)
77 self._min = min
78 self._max = max
79
80 def validate(self, value):
81 try:
82 value = self._type(value)
83 except:
84 return False
85 if self._min is not None and value<self._min:
86 return False
87 if self._max is not None and value>self._max:
88 return False
89 return True
90
91 class ColorItem(BaseItem):
92 _type = QtGui.QColor
93
94 def validate(self, value):
95 try:
96 self._type(value)
97 return True
98 except:
99 return False
100
101 class AbstractPreferences(QtCore.QObject):
102 def __init__(self):
103 QtCore.QObject.__init__(self)
104 self._settings = QtCore.QSettings(QtCore.QSettings.UserScope,
105 self.ORGANISATION, self.APPLICATION)
106 self._prefs = {}
107 self.groups = []
108 keys = []
109 for k in dir(self.__class__):
110 item = self.getItem(k)
111 if isinstance(item, BaseItem):
112 keys.append((k,item))
113 keys.sort(key=lambda x: x[1]._id)
114 for k, item in keys:
115 self._prefs[item._id] = k
116 if item._group not in self.groups:
117 self.groups.append(item._group)
118
119 def getItem(self, key):
120 return getattr(self.__class__, key)
121 #return self._prefs.get(key, None)
122
123 def getPref(self, key):
124 key = self._prefs.get(key, key)
125 default = getattr(self.__class__, key)._default
126 if default is not None:
127 default = QtCore.QVariant(default)
128 else:
129 default = QtCore.QVariant()
130 val = fromVariant(self._settings.value(key, default))
131 return val
132
133 def setPref(self, key, value):
134 key = self._prefs.get(key, key)
135 self._settings.setValue(key, QtCore.QVariant(value))
136
137 def keys(self, group=None):
138 return [k for k in self._prefs.values() if not k.startswith('_') and self.getItem(k)._group == group]
139
140 def getName(self, key):
141 item = getattr(self.__class__, key)
142 return item._name
143
144 def getDescription(self, key):
145 item = getattr(self.__class__, key)
146 return item._description
147
148 def __getitem__(self, key):
149 return getattr(self, key)
150

mercurial