Package Pmv :: Module dashboardCommands
[hide private]
[frames] | no frames]

Source Code for Module Pmv.dashboardCommands

  1  import Tkinter 
  2  from Pmv.mvCommand import MVCommand, MVCommandGUI 
  3   
  4  # TODO 
  5  # add spline command column 
  6  # adding the columns should be commands 
  7   
  8  from Pmv.dashboard import MolFragTreeWithButtons, MolFragNodeWithButtons, \ 
  9       moveTreeToWidget 
 10  from mglutil.gui.BasicWidgets.Tk.trees.TreeWithButtons import \ 
 11       ColumnDescriptor 
 12   
13 -class FloatDashboard(MVCommand):
14 """Command to move the dashboard widget into its own toplevel widget""" 15
16 - def onAddCmdToViewer(self):
17 if not self.vf.hasGui: return 18 19 self.vf.browseCommands('dashboardCommands', package='Pmv', log=0)
20 21
22 - def doit(self):
23 # get handle to old tree 24 oldtree = self.vf.dashboard.tree 25 26 if isinstance(oldtree.master, Tkinter.Toplevel): 27 return 28 29 # create window for new tree 30 master = Tkinter.Toplevel() 31 #master.withdraw() 32 #master.protocol('WM_DELETE_WINDOW',self.vf.dashboard.hide) 33 master.protocol('WM_DELETE_WINDOW',self.vf.dockDashboard) 34 35 tree = moveTreeToWidget(oldtree, master) 36 tree.vf = self.vf 37 tree.configure(hull_height=300) 38 # update tree eight to force vertical scroll bar to appear if needed 39 tree.updateTreeHeight() 40 self.vf.dashboard.tree = tree 41 tree.sets = self.vf.sets
42 43
44 - def destroy(self, event=None):
45 self.vf.GUI.toolbarCheckbuttons['Dashboard']['Variable'].set(0) 46 self.vf.dockDashboard(topCommand=0)
47 48 49
50 -class DockDashboard(MVCommand):
51 """Command to move the dashboard widget into the PMV GUI""" 52
53 - def onAddCmdToViewer(self):
54 if not self.vf.hasGui: return 55 56 self.vf.browseCommands('dashboardCommands', package='Pmv', log=0)
57 58
59 - def doit(self):
60 61 # get handle to old tree 62 oldtree = self.vf.dashboard.tree 63 64 # get master 65 if oldtree.master==self.vf.GUI.ROOT: 66 return 67 68 oldmaster = oldtree.master 69 tree = moveTreeToWidget(oldtree, self.vf.GUI.ROOT) 70 tree.pack(side='bottom', expand=0, fill='x') 71 oldmaster.destroy() 72 tree.vf = self.vf 73 self.vf.dashboard.tree = tree 74 tree.sets = self.vf.sets
75 76 77
78 -class DashboardSuspendRedraw(MVCommand):
79 """Command to suspend and un-suspend the tree redrawing""" 80
81 - def onAddCmdToViewer(self):
82 if not self.vf.hasGui: return 83 84 self.vf.browseCommands('dashboardCommands', package='Pmv', log=0)
85 86
87 - def doit(self, val):
88 assert val in [True, False, 0, 1] 89 self.vf.dashboard.tree.suspendRedraw = val
90 91 92
93 -class ShowDashboard(MVCommand):
94 """Command to show or hide the dashboard, can be added to _pmvrc"""
95 - def onAddCmdToViewer(self):
96 if not self.vf.hasGui: return 97 98 self.vf.browseCommands('dashboardCommands', package='Pmv', log=0)
99 100
101 - def doit(self, val):
102 #print "ShowDashboard.doit", val 103 tree = self.vf.dashboard.tree 104 if val: 105 self.vf.GUI.toolbarCheckbuttons['Dashboard']['Variable'].set(1) 106 if isinstance(tree.master, Tkinter.Toplevel): 107 self.vf.dashboard.tree.master.deiconify() 108 else: 109 self.vf.dashboard.tree.pack(side='bottom', expand=0, fill='x') 110 else: 111 self.vf.GUI.toolbarCheckbuttons['Dashboard']['Variable'].set(0) 112 if isinstance(tree.master, Tkinter.Toplevel): 113 self.vf.dashboard.tree.master.withdraw() 114 else: 115 self.vf.dashboard.tree.forget()
116 117 118
119 -class AddDashboardCmd(MVCommand):
120 """Command to add a command in a nwe column""" 121
122 - def onAddCmdToViewer(self):
123 if not self.vf.hasGui: return 124 125 self.vf.browseCommands('dashboardCommands', package='Pmv', log=0)
126 127
128 - def doit(self, descr):
129 self.vf.dashboard.addColumnDescriptor(descr)
130 131 132
133 -class Dashboard(MVCommand):
134 135 """Display a widget showing a tree representation of the molecules in the Viewer and check buttons allowing to carry out command on parts of molecules directly. 136 Certain commands such as coloring or displaying lines, CPK and S&B are implmented as mutually exclusive (i.e. like radio buttons. 137 """ 138
139 - def hide(self):
140 self.vf.showDashboard(False)
141 142
143 - def show(self):
144 self.vf.showDashboard(True)
145 146
147 - def addColumnDescriptor(self, colDescr):
148 # adds self.vf to colDescr 149 assert isinstance(colDescr,ColumnDescriptor) 150 151 colDescr.vf = self.vf 152 153 self.tree.addColumnDescriptor(colDescr)
154 155
156 - def onAddCmdToViewer(self):
157 vf = self.vf 158 159 if not vf.hasGui: return 160 161 vf.browseCommands('dashboardCommands', package='Pmv', log=0) 162 #self.hasMSMS = True 163 #try: 164 # import mslib 165 # vf.browseCommands('msmsCommands', package='Pmv', log=0) 166 #except: 167 # self.hasMSMS = False 168 169 from ViewerFramework.VF import DeleteAtomsEvent, AddAtomsEvent 170 vf.registerListener(DeleteAtomsEvent, self.handleDeleteEvents) 171 vf.registerListener(AddAtomsEvent, self.handleAddEvents) 172 173 # build the tree 174 #master = Tkinter.Toplevel() 175 #master.withdraw() 176 #master.protocol('WM_DELETE_WINDOW',self.hide) 177 178 from MolKit.molecule import MolecularSystem 179 self.system = syst = MolecularSystem ('PMV Molecules') 180 rootnode = MolFragNodeWithButtons(syst, None) 181 tree = MolFragTreeWithButtons(self.vf.GUI.ROOT, rootnode, self.vf, 182 selectionMode='multiple') 183 rootnode.expand() 184 tree.pack(side='bottom', expand=0, fill='x') 185 self.tree = tree 186 self.tree.sets = vf.sets
187 188
189 - def guiCallback(self):
190 if self.vf.GUI.toolbarCheckbuttons['Dashboard']['Variable'].get(): 191 #self.show() 192 self.vf.floatDashboard() 193 else: 194 #self.hide() 195 self.vf.dockDashboard()
196 197
198 - def onAddObjectToViewer(self, obj):
199 """ 200 Add the new molecule to the tree 201 """ 202 # we have to save .top and .parent else they become syst 203 # and it brakes Pmv 204 top = obj.top 205 parent = obj.parent 206 self.system.adopt(obj) 207 # restore .top and .parent 208 obj.top = top 209 obj.parent = parent 210 211 rootNode = self.tree.root 212 length = len(self.system.children) 213 if length==1: 214 rootNode.refresh() 215 216 rootNode.refreshChildren() 217 218 if length==1: 219 rootNode.expand() 220 221 # make scroll bar appear if needed 222 self.tree.updateTreeHeight()
223 224
225 - def onRemoveObjectFromViewer(self, obj):
226 #if obj in self.system: 227 # self.system.delete(obj) 228 if obj in self.system.molecules: 229 self.system.remove(obj) 230 self.tree.root.refreshChildren()
231 232
233 - def handleDeleteEvents(self, event):
234 """Function to update tree when molecular fragments are deleted. 235 """ 236 # we get here BEFORE The atoms are actually deleted 237 # so we collapse the tree to force its reconstruction AFTER 238 # the atoms have actualyl been deleted. 239 240 self.tree.root.refreshChildren() 241 return
242 243 ## this code does not rebuild the tree properly because the deleted objects 244 ## are still there 245 ## tree = self.tree 246 ## for obj in event.objects: 247 ## try: 248 ## parentNode = tree.objectToNode[obj.parent] 249 ## if len(obj.parent.children)==0: 250 ## parentNode.refresh() 251 ## else: 252 ## parentNode.refreshChildren(redraw=False) 253 ## except KeyError: 254 ## pass 255 ## tree.redraw() 256 257
258 - def handleAddEvents(self, event):
259 """Function to update tree when molecular fragments are added. 260 """ 261 tree = self.tree 262 for obj in event.objects: 263 try: 264 parentNode = tree.objectToNode[obj.parent] 265 if len(obj.parent.children)==1: 266 parentNode.refresh() 267 parentNode.refreshChildren(redraw=False) 268 except KeyError: 269 pass 270 tree.redraw()
271 272
273 - def onCmdRun(self, command, *args, **kw):
274 #import traceback 275 #print '#############################################################' 276 #traceback.print_stack() 277 #print 'OnRun', command, args, kw 278 # called when a Pmv command is run 279 280 # find the column for this command 281 for i,col in enumerate(self.tree.columns): 282 if command.name in col.pmvCmdsToHandle: 283 try: 284 col.onPmvCmd(command, *((i,)+args), **kw) 285 except AttributeError: 286 pass 287 return
288 289 ## tree = self.tree 290 ## column = None 291 ## for i,col in enumerate(tree.columns): 292 ## cmd, arg, opt = col.cmd 293 ## if cmd==command: 294 ## column=i 295 ## break 296 297 ## if column is None: 298 ## return # the command is not one in the dashboard 299 300 ## if col.commandType=='button': 301 ## return # only check buttons not managed upon PMV command 302 303 ## molFrag = args[0] 304 ## negate = kw['negate'] 305 ## if column<6:#command==self.vf.displayLines: 306 ## for o in molFrag: 307 ## try: 308 ## node = self.tree.objectToNode[o] 309 ## if node.chkbtval[column]==negate: 310 ## #only call if button needs to be checked 311 ## node.set(column, negate==False) 312 ## except KeyError: 313 ## #print 'Failed to find object in tree', o 314 ## pass 315 316 317 Dashboard_GUI = MVCommandGUI() 318 from moleculeViewer import ICONPATH 319 Dashboard_GUI.addToolBar('Dashboard', icon1='dashboard.png', 320 icon_dir=ICONPATH, 321 balloonhelp='Float Dashboard Widget', index=9) 322 323 commandList = [ 324 {'name':'dashboard', 'cmd':Dashboard(), 'gui':Dashboard_GUI}, 325 #{'name':'showDashboard', 'cmd':ShowDashboard(), 'gui':None}, 326 {'name':'floatDashboard', 'cmd':FloatDashboard(), 'gui':None}, 327 {'name':'dockDashboard', 'cmd':DockDashboard(), 'gui':None}, 328 {'name':'addDashboardCmd', 'cmd':AddDashboardCmd(), 'gui':None}, 329 {'name':'dashboardSuspendRedraw', 'cmd':DashboardSuspendRedraw(), 330 'gui':None}, 331 ] 332
333 -def initModule(viewer):
334 for dict in commandList: 335 viewer.addCommand(dict['cmd'], dict['name'], dict['gui'])
336