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 super(ControlBase, self).__init__() 31 self._help = helptext 32 self._value = defaultValue 33 self._form = None # Qt widget 34 self._parent = None # Parent window 35 self._label = label # Label 36 self._popupMenu = None 37 38 self.initForm()
39
40 - def initForm(self):
41 """ 42 Load Control and initiate the events 43 """ 44 control_path = tools.getFileInSameDirectory(__file__, "textInput.ui") 45 self._form = uic.loadUi(control_path) 46 self.form.label.setText(self._label) 47 self.form.lineEdit.setText(self._value) 48 self.form.setToolTip(self.help)
49 50 # self.form.lineEdit.editingFinished.connect(self.finishEditing) 51
52 - def __repr__(self): return str(self._value)
53 54 ########################################################################## 55 ############ Funcions #################################################### 56 ########################################################################## 57
58 - def load(self, data):
59 """ 60 Load a value from the dict variable 61 @param data: dictionary with the value of the Control 62 """ 63 if 'value' in data: 64 self.value = data['value']
65
66 - def save(self, data):
67 """ 68 Save a value to dict variable 69 @param data: dictionary with to where the value of the Control will be added 70 """ 71 if self.value: 72 data['value'] = self.value
73
74 - def show(self):
75 """ 76 Show the control 77 """ 78 self.form.show()
79
80 - def hide(self):
81 """ 82 Hide the control 83 """ 84 self.form.hide()
85
86 - def addPopupMenuOption(self, label, functionAction=None, key=None):
87 """ 88 Add an option to the Control popup menu 89 @param label: label of the option. 90 @param functionAction: function called when the option is selected. 91 @param key: shortcut key 92 """ 93 if not self._popupMenu: 94 self.form.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) 95 self.form.customContextMenuRequested.connect(self.__openPopupMenu) 96 self._popupMenu = QtGui.QMenu() 97 self._popupMenu.aboutToShow.connect( 98 self.aboutToShowContextMenuEvent) 99 100 if label == "-": 101 return self._popupMenu.addSeparator() 102 else: 103 action = QtGui.QAction(label, self.form) 104 if key != None: 105 action.setShortcut(QtGui.QKeySequence(key)) 106 if functionAction: 107 action.triggered.connect(functionAction) 108 self._popupMenu.addAction(action) 109 return action
110
111 - def addPopupSubMenuOption(self, label, options, keys={}):
112 """ 113 Add submenu options to the Control popup menu 114 @param label: submenu label of the option. 115 @param options: dictionary representing the submenu. ex: { 'Example': event function, ... }. 116 @param keys: shortcut keys. ex: { 'Example': shortcut key, ... }. 117 """ 118 if not self._popupMenu: 119 self.form.setContextMenuPolicy(QtCore.Qt.CustomContextMenu) 120 self.form.customContextMenuRequested.connect(self.__openPopupMenu) 121 self._popupMenu = QtGui.QMenu() 122 self._popupMenu.aboutToShow.connect( 123 self.aboutToShowContextMenuEvent) 124 125 submenu = QtGui.QMenu(label, self._popupMenu) 126 for text, func in options.items(): 127 if text == "-": 128 submenu.addSeparator() 129 else: 130 action = QtGui.QAction(text, self.form) 131 if text in keys: 132 action.setShortcut(QtGui.QKeySequence(keys[text])) 133 134 if func: 135 action.triggered.connect(func) 136 submenu.addAction(action) 137 self._popupMenu.addMenu(submenu)
138 139 ########################################################################## 140 ############ Events ###################################################### 141 ########################################################################## 142
143 - def changed(self):
144 """ 145 Function called when ever the Control value is changed 146 """ 147 pass
148
150 """ 151 Function called before open the Control popup menu 152 """ 153 pass
154
155 - def __openPopupMenu(self, position):
156 if self._popupMenu: 157 self._popupMenu.exec_(self.form.mapToGlobal(position))
158 159 ########################################################################## 160 ############ Properties ################################################## 161 ########################################################################## 162 163 ########################################################################## 164 # Set the Control enabled or disabled 165 166 @property
167 - def enabled(self):
168 return self.form.isEnabled()
169 170 @enabled.setter
171 - def enabled(self, value):
172 """@type value: boolean""" 173 self.form.setEnabled(value)
174 175 ########################################################################## 176 # Return or update the value of the Control 177 178 @property
179 - def value(self):
180 return self._value
181 182 @value.setter
183 - def value(self, value):
184 """ 185 This property return and set what the control should manage or store. 186 @type value: string 187 """ 188 oldvalue = self._value 189 self._value = value 190 if oldvalue != value: 191 self.changed()
192 193 @property
194 - def name(self):
195 return self.form.objectName()
196 197 @name.setter
198 - def name(self, value):
199 """ 200 This property return and set the name of the control 201 @type value: string 202 """ 203 self.form.setObjectName(value)
204 205 ########################################################################## 206 # Return or update the label of the Control 207 208 @property
209 - def label(self):
210 return self._label
211 212 @label.setter
213 - def label(self, value):
214 """ 215 Label of the control, if applies 216 @type value: string 217 """ 218 self._label = value
219 220 ########################################################################## 221 # Return the QT widget 222 223 @property
224 - def form(self):
225 """ 226 Returns the Widget of the control. 227 This property will be deprecated in a future version. 228 """ 229 return self._form
230 231 ########################################################################## 232 # Parent window 233 234 @property
235 - def parent(self): return self._parent
236 237 @parent.setter
238 - def parent(self, value):
239 """ 240 Returns or set the parent basewidget where the Control is 241 @type value: BaseWidget 242 """ 243 self._parent = value
244 245 246 @property
247 - def help(self): return self._help if self._help else ''
248