pygpibtoolkit/qt4/qpreferenceseditor.py

Thu, 21 Aug 2008 19:10:57 +0200

author
David Douard <david.douard@logilab.fr>
date
Thu, 21 Aug 2008 19:10:57 +0200
changeset 66
2a97995628a3
parent 42
a947d80b1afd
child 78
b73a9d9e45ec
permissions
-rw-r--r--

added copyright header to every python file

66
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 42
diff changeset
1 # This program is free software; you can redistribute it and/or modify it under
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 42
diff changeset
2 # the terms of the GNU General Public License as published by the Free Software
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 42
diff changeset
3 # Foundation; either version 2 of the License, or (at your option) any later
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 42
diff changeset
4 # version.
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 42
diff changeset
5 #
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 42
diff changeset
6 # This program is distributed in the hope that it will be useful, but WITHOUT
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 42
diff changeset
7 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 42
diff changeset
8 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 42
diff changeset
9 #
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 42
diff changeset
10 # You should have received a copy of the GNU General Public License along with
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 42
diff changeset
11 # this program; if not, write to the Free Software Foundation, Inc.,
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 42
diff changeset
12 # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 42
diff changeset
13 """ Copyright (c) 2007-2008 David Douard (Paris, FRANCE).
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 42
diff changeset
14 http://www.logilab.org/project/pygpibtoolkit -- mailto:david.douard@logilab.fr
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 42
diff changeset
15 """
2a97995628a3 added copyright header to every python file
David Douard <david.douard@logilab.fr>
parents: 42
diff changeset
16
40
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
17 import os
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
18 from PyQt4 import QtCore, QtGui, uic
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
19 from PyQt4.QtCore import Qt, SIGNAL
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
20 import sip
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
21 from qpreferences import fromVariant
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
22 from qpreferences import UnicodeItem
42
a947d80b1afd make q3562 app beginning to work; can now display state & trace datablock with some options.
David Douard <david.douard@logilab.fr>
parents: 40
diff changeset
23 from pygpibtoolkit.tools import AbstractRegister
40
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
24
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
25 class WidgetMetaclass(sip.wrappertype):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
26 _widgets = {}
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
27 def __init__(cls, name, bases, dct):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
28 # called at class creation
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
29 super(WidgetMetaclass, cls).__init__(name, bases, dct)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
30 if name != "BaseWidget":
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
31 WidgetRegister().add(cls)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
32
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
33 class BaseWidget(QtGui.QWidget):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
34 __metaclass__ = WidgetMetaclass
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
35 _filter = None
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
36
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
37 class WidgetRegister(AbstractRegister):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
38 _registered_type = BaseWidget
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
39 getWidget = AbstractRegister.get_class
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
40
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
41 form_class, base_class = uic.loadUiType(os.path.join(os.path.dirname(__file__), "qpreferences_dialog.ui"))
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
42
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
43 class ItemValidator(QtGui.QValidator):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
44 def __init__(self, parent, item):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
45 QtGui.QValidator.__init__(self, parent)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
46 self._item = item
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
47
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
48 def validate(self, value, pos):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
49 value = unicode(value)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
50 if value.strip() == "":
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
51 return (self.Intermediate, pos)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
52 if self._item.validate(value):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
53 return (self.Acceptable, pos)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
54 return (self.Invalid, pos)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
55
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
56 class BaseEditor(BaseWidget):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
57 """
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
58 Basic editor for preference items. Use a QLineEdit with no
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
59 validation or so...
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
60 """
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
61 _accepts = "UnicodeItem"
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
62 def __init__(self, parent, item):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
63 BaseWidget.__init__(self, parent)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
64 self._item = item
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
65 self.setupUI()
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
66
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
67 def setValue(self, value):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
68 self._editor.setText(unicode(value))
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
69
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
70 def getValue(self):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
71 return unicode(self._editor.text())
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
72
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
73 def setupUI(self):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
74 self._editor = QtGui.QLineEdit(self)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
75 self._validator = ItemValidator(self, self._item)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
76 self._editor.setValidator(self._validator)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
77 l = QtGui.QHBoxLayout(self)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
78 l.setContentsMargins(0,0,0,0)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
79 l.addWidget(self._editor, 1)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
80 self.setFocusProxy(self._editor)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
81
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
82 class IntEditor(BaseEditor):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
83 _accepts = "IntItem"
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
84 def setupUI(self):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
85 self._editor = QtGui.QSpinBox(self)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
86 self._editor.setMinimum(self._item._min)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
87 self._editor.setMaximum(self._item._max)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
88 l = QtGui.QHBoxLayout(self)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
89 l.setContentsMargins(0,0,0,0)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
90 l.addWidget(self._editor, 1)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
91 self.setFocusProxy(self._editor)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
92
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
93 def setValue(self, value):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
94 self._editor.setValue(int(value))
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
95
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
96 def getValue(self):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
97 return self._editor.value()
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
98
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
99 class ColorEditor(BaseEditor):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
100 _accepts = "ColorItem"
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
101 def setupUI(self):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
102 self._editor_pix = QtGui.QPixmap(40,30)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
103 self._editor_pix.fill(QtGui.QColor('white'))
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
104
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
105 self._editor_btn = QtGui.QPushButton("")
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
106 self._editor_btn.setFlat(True)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
107 self._editor_btn.setFocusPolicy(Qt.NoFocus)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
108 self._editor_btn.setIcon(QtGui.QIcon(self._editor_pix))
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
109
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
110 self._editor_edt = QtGui.QLineEdit()
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
111 self._editor_edt.setInputMask(r"\#HHHHHHhh")
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
112 fm = QtGui.QApplication.fontMetrics()
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
113 w = fm.width("#FFFFFFFF ")
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
114 self._editor_edt.setMaximumWidth(w)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
115
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
116
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
117 l = QtGui.QHBoxLayout(self)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
118 l.setContentsMargins(0,0,0,0)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
119 l.addWidget(self._editor_edt)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
120 l.addWidget(self._editor_btn)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
121 l.addStretch(1)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
122 self.setFocusProxy(self._editor_edt)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
123 assert self.connect(self._editor_btn, SIGNAL("pressed()"),
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
124 self.chooseColor)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
125 assert self.connect(self._editor_edt, SIGNAL("editingFinished()"),
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
126 self.colorEdited)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
127
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
128 def setValue(self, value):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
129 if isinstance(value, tuple):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
130 color = self._item._type(*value)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
131 elif isinstance(value, self._item._type):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
132 color = value
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
133 elif isinstance(value, long):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
134 color = self._item._type(value)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
135 alpha = value >> 24
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
136 color.setAlpha(alpha)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
137 else:
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
138 color = self._item._type(value)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
139
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
140 rgba = color.getRgb()
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
141 colorname = ("#"+"%02X"*4)%rgba
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
142 self._rgba = rgba
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
143 self._editor_pix.fill(color)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
144 self._editor_btn.setIcon(QtGui.QIcon(self._editor_pix))
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
145 self._editor_edt.setText(colorname)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
146
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
147 def getValue(self):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
148 return self._item._type(*self._rgba)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
149
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
150 def colorEdited(self):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
151 val = unicode(self._editor_edt.text())
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
152 if len(val) == 7:
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
153 val += "FF" # miss alpha channel
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
154 val = val[1:]
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
155 val = [val[2*i:2*i+2] for i in range(len(val)/2)]
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
156 val = [int(x, 16) for x in val]
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
157
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
158 self._rgba = tuple(val)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
159 self.setValue(self._rgba)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
160
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
161
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
162 def chooseColor(self):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
163 newcolor, ok = QtGui.QColorDialog.getRgba(self.getValue().rgba(), self)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
164 if ok:
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
165 self.setValue(newcolor)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
166
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
167 class PreferencesEditor(QtGui.QDialog, form_class):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
168 def __init__(self, preferences, parent=None):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
169 QtGui.QDialog.__init__(self, parent)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
170 self.setupUi(self)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
171 self._prefs = preferences
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
172 self.buildUI()
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
173
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
174 def buildUI(self):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
175 mainw = self.centralTab
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
176 for i in range(mainw.count()):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
177 mainw.removeTab(0)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
178
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
179 eds = {}
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
180 self._editors = eds
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
181
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
182 wr = WidgetRegister()
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
183 if len(self._prefs.groups)>1:
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
184 for group in self._prefs.groups:
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
185 if group is None:
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
186 continue
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
187 w = QtGui.QWidget(mainw)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
188 mainw.addTab(w, group)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
189 g = QtGui.QGridLayout(w)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
190 g.setVerticalSpacing(2)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
191 for i, k in enumerate(self._prefs.keys(group)):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
192 name = self._prefs.getName(k)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
193 item = self._prefs.getItem(k)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
194 if not name:
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
195 name = k
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
196 l = QtGui.QLabel(name, w)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
197 g.addWidget(l, i, 0)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
198 if self._prefs.getDescription(k):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
199 l.setToolTip(self._prefs.getDescription(k))
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
200 wcls = wr.getWidget(item)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
201 e = wcls(w, item)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
202 eds[k] = e
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
203 g.addWidget(e, i, 1)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
204 val = self._prefs.getPref(k)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
205 if val is None:
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
206 val = ''
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
207 e.setValue(val)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
208
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
209 # add blank space
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
210 g.addWidget(QtGui.QWidget(w), i+1, 0)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
211 g.setRowStretch(i+1,1)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
212 g.setColumnStretch(1,1)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
213
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
214 def accept(self):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
215 p=self._prefs
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
216 for k in self._editors:
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
217 newval = self._editors[k].getValue()
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
218 p.setPref(k, newval)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
219 return QtGui.QDialog.accept(self)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
220
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
221 if __name__ == '__main__':
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
222 from qpreferences import AbstractPreferences, UnicodeItem, IntItem, BaseItem
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
223 from qpreferences import ColorItem
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
224 class TestPreferences(AbstractPreferences):
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
225 ORGANISATION="Logilab"
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
226 APPLICATION="test_qpref_editor"
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
227
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
228 device = UnicodeItem('/dev/ttyUSB0', name="the device",
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
229 group="GPIB settings")
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
230 address = IntItem(5, name="address",
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
231 description="GPIB address of the plotter",
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
232 group="GPIB settings",
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
233 min=0, max=16)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
234 other = UnicodeItem('toto', name="other stuff",
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
235 group="General")
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
236 color = ColorItem(default='red',name="Colour",
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
237 group="General")
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
238 _pos = BaseItem(None)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
239 _size = BaseItem(None)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
240 _appState = BaseItem(None)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
241
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
242 a = QtGui.QApplication([])
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
243
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
244 prefs = TestPreferences()
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
245 w = PreferencesEditor(prefs)
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
246 w.show()
1bbea188a7e5 refactoring: moved everything of the library to a "pygpibtoolkit" module.
David Douard <david.douard@logilab.fr>
parents:
diff changeset
247 a.exec_()

mercurial