Package vqt :: Module application
[hide private]
[frames] | no frames]

Source Code for Module vqt.application

  1  import os 
  2  import json 
  3   
  4  from PyQt4 import QtCore, QtGui 
  5   
  6  import vqt.cli as vq_cli 
  7  import vqt.main as vq_main 
  8  import vqt.saveable as vq_save 
  9  import vqt.hotkeys as vq_hotkeys 
 10  import vqt.menubuilder as vq_menu 
 11   
12 -class VQDockWidget(vq_hotkeys.HotKeyMixin, QtGui.QDockWidget):
13
14 - def __init__(self, parent):
15 QtGui.QDockWidget.__init__(self, parent) 16 vq_hotkeys.HotKeyMixin.__init__(self) 17 self.addHotKey('ctrl+enter', 'mem:undockmaximize') 18 self.addHotKeyTarget('mem:undockmaximize', self._hotkey_undock_maximize) 19 self.setAllowedAreas(QtCore.Qt.AllDockWidgetAreas)
20
21 - def vqSaveState(self, settings, name):
22 wid = self.widget() 23 if isinstance(wid, vq_save.SaveableWidget): 24 return wid.vqSaveState(settings, name)
25
26 - def vqRestoreState(self, settings, name):
27 wid = self.widget() 28 if isinstance(wid, vq_save.SaveableWidget): 29 return wid.vqRestoreState(settings, name)
30
31 - def setWidget(self, widget):
32 # If he sets his window title, we want to... 33 self.setWindowTitle(widget.windowTitle()) 34 widget.setWindowTitle = self.setWindowTitle 35 QtGui.QDockWidget.setWidget(self, widget)
36
37 - def closeEvent(self, event):
38 39 self.hide() 40 41 w = self.widget() 42 w.setParent(None) 43 w.close() 44 45 self.parent().vqRemoveDockWidget(self) 46 47 event.accept()
48
49 - def _hotkey_undock_maximize(self):
50 # if docked, undock 51 if not self.isFloating(): 52 self.setFloating(1) 53 # if not maximized, maximize 54 if not self.isMaximized(): 55 self.showMaximized() 56 else: 57 # else dock 58 self.showNormal() 59 self.setFloating(False) 60 else: 61 # else dock 62 self.showNormal() 63 self.setFloating(False) 64 65 self.show() 66 self.raise_()
67 68 import vqt.hotkeys as vq_hotkey 69
70 -class VQMainCmdWindow(vq_hotkey.HotKeyMixin, QtGui.QMainWindow):
71 ''' 72 A base class for application window's to inherit from. 73 ''' 74 75 __cli_widget_class__ = vq_cli.VQCli 76
77 - def __init__(self, appname, cmd):
78 79 QtGui.QMainWindow.__init__(self) 80 vq_hotkey.HotKeyMixin.__init__(self) 81 82 self._vq_appname = appname 83 self._vq_dockwidgets = [] 84 85 self._vq_settings = QtCore.QSettings('invisigoth', application=appname, parent=self) 86 self._vq_histfile = os.path.join( os.path.expanduser('~'), '.%s_history' % appname) 87 88 self._dock_classes = {} 89 90 self.vqInitDockWidgetClasses() 91 92 self._vq_mbar = vq_menu.VQMenuBar() 93 self.setMenuBar(self._vq_mbar) 94 95 # AnimatedDocks, AllowNestedDocks, AllowTabbedDocks, ForceTabbedDocks, VerticalTabs 96 self.setDockOptions(self.AnimatedDocks | self.AllowTabbedDocks) 97 98 self._vq_cli = self.__cli_widget_class__(cmd) 99 self._vq_cli.input.loadHistory(self._vq_histfile) 100 self._vq_cli.sigCliQuit.connect( self.close ) 101 102 self.setCentralWidget(self._vq_cli) 103 self.vqRestoreGuiSettings(self._vq_settings)
104
105 - def vqAddMenuField(self, fname, callback, args=()):
106 self._vq_mbar.addField(fname, callback, args=args)
107
108 - def vqAddDynMenu(self, fname, callback):
109 self._vq_mbar.addDynMenu(fname, callback)
110
111 - def vqInitDockWidgetClasses(self):
112 # apps can over-ride 113 pass
114
115 - def vqAddDockWidgetClass(self, cls, args=()):
116 self._dock_classes[cls.__name__] = (cls,args)
117
118 - def vqBuildDockWidget(self, clsname, floating=False, area=QtCore.Qt.TopDockWidgetArea):
119 res = self._dock_classes.get(clsname) 120 if res == None: 121 print('vqBuildDockWidget Failed For: %s' % clsname) 122 return 123 cls, args = res 124 obj = cls(*args) 125 return self.vqDockWidget(obj, area, floating=floating), obj
126
127 - def vqRestoreGuiSettings(self, settings):
128 129 dwcls = settings.value('DockClasses') 130 if not dwcls.isNull(): 131 132 for i, clsname in enumerate(dwcls.toStringList()): 133 name = 'VQDockWidget%d' % i 134 try: 135 tup = self.vqBuildDockWidget(str(clsname), floating=True) 136 if tup != None: 137 d, obj = tup 138 d.setObjectName(name) 139 d.vqRestoreState(settings,name) 140 d.show() 141 except Exception, e: 142 print('Error Building: %s: %s' % (clsname,e)) 143 144 # Once dock widgets are loaded, we can restoreState 145 state = settings.value('DockState') 146 if not state.isNull(): 147 self.restoreState(state.toByteArray()) 148 149 geom = settings.value('DockGeometry') 150 if not geom.isNull(): 151 self.restoreGeometry(geom.toByteArray()) 152 153 # Just get all the resize activities done... 154 vq_main.eatevents() 155 for w in self.vqGetDockWidgets(): 156 w.show() 157 158 return True
159
160 - def vqSaveGuiSettings(self, settings):
161 162 dock_classes = [] 163 164 # Enumerate the current dock windows and set 165 # their names by their list order... 166 for i, w in enumerate(self.vqGetDockWidgets()): 167 widget = w.widget() 168 dock_classes.append(widget.__class__.__name__) 169 name = 'VQDockWidget%d' % i 170 w.setObjectName(name) 171 w.vqSaveState(settings,name) 172 173 settings.setValue('DockClasses', dock_classes) 174 settings.setValue('DockGeometry', self.saveGeometry()) 175 settings.setValue('DockState', self.saveState())
176
177 - def closeEvent(self, event):
178 self.vqSaveGuiSettings(self._vq_settings) 179 self._vq_cli.input.saveHistory(self._vq_histfile) 180 QtGui.QMainWindow.closeEvent(self, event)
181
182 - def vqGetDockWidgets(self):
183 return list(self._vq_dockwidgets)
184
185 - def vqClearDockWidgets(self):
186 for wid in self.vqGetDockWidgets(): 187 wid.close()
188
189 - def vqRemoveDockWidget(self, widget):
190 self._vq_dockwidgets.remove(widget) 191 self.removeDockWidget(widget)
192
193 - def vqDockWidget(self, widget, area=QtCore.Qt.TopDockWidgetArea, floating=False):
194 d = VQDockWidget(self) 195 d.setWidget(widget) 196 d.setFloating(floating) 197 self.addDockWidget(area, d) 198 self._vq_dockwidgets.append(d) 199 self.restoreDockWidget(d) 200 d.show() 201 return d
202