Package pyforms :: Package gui :: Package Controls :: Module ControlBase
[hide private]
[frames] | no frames]

Source Code for Module pyforms.gui.Controls.ControlBase

  1  # !/usr/bin/python 
  2  # -*- coding: utf-8 -*- 
  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" 
19 20 21 -class ControlBase(object):
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 # Qt widget 33 self._parent = None # Parent window 34 self._label = label # Label 35 self._popupMenu = None 36 37 self.initForm()
38
39 - def initForm(self):
40 """ 41 Load Control and initiate the events 42 """ 43 control_path = tools.getFileInSameDirectory(__file__, "textInput.ui") 44 self._form = uic.loadUi(control_path) 45 self.form.label.setText(self._label) 46 self.form.lineEdit.setText(self._value) 47 self.form.setToolTip(self.help)
48 49 # self.form.lineEdit.editingFinished.connect(self.finishEditing) 50
51 - def __repr__(self): return str(self._value)
52 53 ########################################################################## 54 ############ Funcions #################################################### 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
73 - def show(self):
74 """ 75 Show the control 76 """ 77 self.form.show()
78
79 - def hide(self):
80 """ 81 Hide the control 82 """ 83 self.form.hide()
84
85 - def addPopupMenuOption(self, label, functionAction=None, key=None):
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
110 - def addPopupSubMenuOption(self, label, options, keys={}):
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 ############ Events ###################################################### 140 ########################################################################## 141
142 - def changed(self):
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
154 - def __openPopupMenu(self, position):
155 if self._popupMenu: 156 self._popupMenu.exec_(self.form.mapToGlobal(position))
157 158 ########################################################################## 159 ############ Properties ################################################## 160 ########################################################################## 161 162 ########################################################################## 163 # Set the Control enabled or disabled 164 165 @property
166 - def enabled(self):
167 return self.form.isEnabled()
168 169 @enabled.setter
170 - def enabled(self, value):
171 """@type value: boolean""" 172 self.form.setEnabled(value)
173 174 ########################################################################## 175 # Return or update the value of the Control 176 177 @property
178 - def value(self):
179 return self._value
180 181 @value.setter
182 - def value(self, value):
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
193 - def name(self):
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 # Return or update the label of the Control 206 207 @property
208 - def label(self):
209 return self._label
210 211 @label.setter
212 - def label(self, value):
213 """ 214 Label of the control, if applies 215 @type value: string 216 """ 217 self._label = value
218 219 ########################################################################## 220 # Return the QT widget 221 222 @property
223 - def form(self):
224 """ 225 Returns the Widget of the control. 226 This property will be deprecated in a future version. 227 """ 228 return self._form
229 230 ########################################################################## 231 # Parent window 232 233 @property
234 - def parent(self): return self._parent
235 236 @parent.setter
237 - def parent(self, value):
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