1
2
3
4 """ Control Base
5
6 """
7
8 from PyQt4 import uic, QtGui, QtCore
9 import pyforms.Utils.tools as tools
10
11 __author__ = "Ricardo Ribeiro"
12 __copyright__ = ""
13 __credits__ = "Ricardo Ribeiro"
14 __license__ = "MIT"
15 __version__ = "0.0"
16 __maintainer__ = ["Ricardo Ribeiro", "Carlos Mão de Ferro"]
17 __email__ = ["ricardojvr at gmail.com", "cajomferro at gmail.com"]
18 __status__ = "Development"
22 """
23 This class represents the most basic control that can exist
24 A Control is a Widget or a group of widgets that can be reused from application to application
25
26 @undocumented: __repr__
27 """
28
29 - def __init__(self, label='', defaultValue='', helptext=''):
30 self._help = helptext
31 self._value = defaultValue
32 self._form = None
33 self._parent = None
34 self._label = label
35 self._popupMenu = None
36
37 self.initForm()
38
48
49
50
52
53
54
55
56
57 - def load(self, data):
58 """
59 Load a value from the dict variable
60 @param data: dictionary with the value of the Control
61 """
62 if 'value' in data:
63 self.value = data['value']
64
65 - def save(self, data):
66 """
67 Save a value to dict variable
68 @param data: dictionary with to where the value of the Control will be added
69 """
70 if self.value:
71 data['value'] = self.value
72
74 """
75 Show the control
76 """
77 self.form.show()
78
80 """
81 Hide the control
82 """
83 self.form.hide()
84
86 """
87 Add an option to the Control popup menu
88 @param label: label of the option.
89 @param functionAction: function called when the option is selected.
90 @param key: shortcut key
91 """
92 if not self._popupMenu:
93 self.form.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
94 self.form.customContextMenuRequested.connect(self.__openPopupMenu)
95 self._popupMenu = QtGui.QMenu()
96 self._popupMenu.aboutToShow.connect(
97 self.aboutToShowContextMenuEvent)
98
99 if label == "-":
100 return self._popupMenu.addSeparator()
101 else:
102 action = QtGui.QAction(label, self.form)
103 if key != None:
104 action.setShortcut(QtGui.QKeySequence(key))
105 if functionAction:
106 action.triggered.connect(functionAction)
107 self._popupMenu.addAction(action)
108 return action
109
111 """
112 Add submenu options to the Control popup menu
113 @param label: submenu label of the option.
114 @param options: dictionary representing the submenu. ex: { 'Example': event function, ... }.
115 @param keys: shortcut keys. ex: { 'Example': shortcut key, ... }.
116 """
117 if not self._popupMenu:
118 self.form.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
119 self.form.customContextMenuRequested.connect(self.__openPopupMenu)
120 self._popupMenu = QtGui.QMenu()
121 self._popupMenu.aboutToShow.connect(
122 self.aboutToShowContextMenuEvent)
123
124 submenu = QtGui.QMenu(label, self._popupMenu)
125 for text, func in options.items():
126 if text == "-":
127 submenu.addSeparator()
128 else:
129 action = QtGui.QAction(text, self.form)
130 if text in keys:
131 action.setShortcut(QtGui.QKeySequence(keys[text]))
132
133 if func:
134 action.triggered.connect(func)
135 submenu.addAction(action)
136 self._popupMenu.addMenu(submenu)
137
138
139
140
141
143 """
144 Function called when ever the Control value is changed
145 """
146 pass
147
149 """
150 Function called before open the Control popup menu
151 """
152 pass
153
155 if self._popupMenu:
156 self._popupMenu.exec_(self.form.mapToGlobal(position))
157
158
159
160
161
162
163
164
165 @property
167 return self.form.isEnabled()
168
169 @enabled.setter
171 """@type value: boolean"""
172 self.form.setEnabled(value)
173
174
175
176
177 @property
180
181 @value.setter
183 """
184 This property return and set what the control should manage or store.
185 @type value: string
186 """
187 oldvalue = self._value
188 self._value = value
189 if oldvalue != value:
190 self.changed()
191
192 @property
194 return self.form.objectName()
195
196 @name.setter
197 - def name(self, value):
198 """
199 This property return and set the name of the control
200 @type value: string
201 """
202 self.form.setObjectName(value)
203
204
205
206
207 @property
210
211 @label.setter
213 """
214 Label of the control, if applies
215 @type value: string
216 """
217 self._label = value
218
219
220
221
222 @property
229
230
231
232
233 @property
234 - def parent(self): return self._parent
235
236 @parent.setter
238 """
239 Returns or set the parent basewidget where the Control is
240 @type value: BaseWidget
241 """
242 self._parent = value
243
244
245 @property
246 - def help(self): return self._help if self._help else ''
247