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

Source Code for Module Pmv.visionCommands

  1  ######################################################################### 
  2  # 
  3  # Date: Aug 2001 Authors: Michel Sanner, Daniel Stoffler 
  4  # 
  5  #    sanner@scripps.edu 
  6  #    stoffler@scripps.edu 
  7  # 
  8  #       The Scripps Research Institute (TSRI) 
  9  #       Molecular Graphics Lab 
 10  #       La Jolla, CA 92037, USA 
 11  # 
 12  # Copyright: Michel Sanner, Daniel Stoffler and TSRI 
 13  # 
 14  # Revision: Guillaume Vareille 
 15  # 
 16  ######################################################################### 
 17  # 
 18  #$Header: /opt/cvs/python/packages/share1.5/Pmv/visionCommands.py,v 1.52 2007/06/18 21:15:04 gillet Exp $ 
 19  # 
 20  #$Id: visionCommands.py,v 1.52 2007/06/18 21:15:04 gillet Exp $ 
 21  # 
 22  # 
 23  import Tkinter 
 24  import sys 
 25   
 26  from mglutil.gui.InputForm.Tk.gui import InputFormDescr, InputForm 
 27  from Pmv.mvCommand import MVCommand, MVCommandGUI 
 28  from Pmv.VisionInterface.PmvNodes import PmvNode, PmvMolecule, PmvViewer, \ 
 29       PmvSetNode 
 30  #from Pmv.selectionCommands import sets__ 
 31  from Vision.VPE import VisualProgramingEnvironment, NodeLibrary 
 32  from ViewerFramework.VFCommand import CommandGUI # this adds ok/cancel to form 
 33   
 34   
35 -class VisionCommand(MVCommand):
36
37 - def __init__(self, func=None):
38 MVCommand.__init__(self, func) 39 self.root = None 40 self.lib = None # will point to pmvlib 41 self.ed = None # VisualProgrammingEnvironment object
42 43
44 - def onExitFromViewer(self):
45 if self.ed is None: 46 return 47 if len(self.ed.networks): 48 self.vf.log("##") 49 self.vf.log("## SAVING Vision Networks") 50 self.vf.log("##") 51 self.vf.log("self.browseCommands('visionCommands', commands=('vision',))") 52 # no need to call the vision command because it has ben called 53 # and loged int he Pmv session already 54 #self.vf.log("self.vision()") 55 self.vf.log("from NetworkEditor.net import Network") 56 self.vf.log("self.vision.ed.deleteNetwork(self.vision.ed.networks['Network 0'])") 57 self.vf.log("pmv = self") 58 self.vf.log("self = pmv.vision.ed") 59 60 from NetworkEditor.macros import MacroNetwork 61 for netname, net in self.ed.networks.items(): 62 if isinstance(net, MacroNetwork): 63 continue 64 65 # do not create any code if no nodes are present in this net 66 if len(net.nodes) == 0: 67 continue 68 69 self.vf.log("##") 70 self.vf.log("## Networks %s"%netname) 71 self.vf.log("##") 72 73 if net._modified: 74 self.vf.log("masterNet = Network('%s')"%netname) 75 self.vf.log("self.addNetwork(masterNet)") 76 self.vf.log("masterNet.buildIcons()") 77 self.vf.log("self.setNetwork(masterNet)") 78 netsrc = net.getNetworkCreationSourceCode() 79 for line in netsrc: 80 self.vf.log(line) 81 else: 82 if net.filename: 83 self.vf.log("self.loadNetwork('%s')"%net.filename) 84 else: 85 print 'WARNING: visionCommand: onExit: network %s has no filename'%netname 86 self.vf.log("##") 87 self.vf.log("## Networks %s End"%netname) 88 self.vf.log("##\n\n") 89 self.root.withdraw() 90 91 self.vf.log("self = pmv") 92 self.exit_cb()
93 94
95 - def exit_cb(self):
96 """the original exit_cb() in simpleNE.py destroys the root, which 97 we cannot do here. Hence, we need to re-implement the exit_cb() here 98 """ 99 ################# 100 # PART I: this is the exit_cb() from Vision/VPE.py: 101 ################# 102 103 # we add attributes to node libraries. Upon exit, we clear these 104 # attributes: 105 for lib in self.ed.libraries.values(): 106 self.ed.deleteLibrary(lib.name) 107 108 109 ################# 110 # PART II: this is the exit_cb() from NetworkEditor/simpleNE.py: 111 ################# 112 from NetworkEditor.macros import MacroNetwork 113 for net in self.ed.networks.values(): 114 if not isinstance(net, MacroNetwork): 115 self.ed.deleteNetwork(net) 116 self.root = None
117 118
119 - def onAddCmdToViewer(self):
120 pass
121
122 - def interactiveExit_cb(self, event=None):
123 from tkMessageBox import askyesno 124 msg = askyesno("Quit","Are you sure you want to quit PMV?",parent = self.root) 125 if msg == True: 126 self.exit_cb() 127 self.vf.GUI.quit_cb()
128
129 - def onAddNewCmd(self, cmd):
130 # # we fire all Pmv nodes to publish the newly loaded command 131 # if self.ed: 132 # for net in self.ed.networks.values(): 133 # for node in net.nodes: 134 # if isinstance(node, PmvNode): 135 # node.schedule_cb() 136 137 # also, load command to export Pmv Sets to Vision 138 self.vf.browseCommands( 139 'visionCommands', 140 commands=['exportSets'], package='Pmv', topCommand=0)
141 142
143 - def doit(self):
144 if self.root is None: 145 self.root = Tkinter.Toplevel() 146 self.root.withdraw() 147 148 # the vision font must be decided in vision or _visionrc, not here anymore 149 # if 'changeFont' in self.vf.commands.keys(): 150 # currentFont = self.vf.changeFont.getCurrentFont() 151 # self.ed = VisualProgramingEnvironment( 152 # master=self.root, font=currentFont, withShell=0) 153 # else: 154 self.ed = VisualProgramingEnvironment(master=self.root, 155 withShell=0) 156 157 self.root.protocol("WM_DELETE_WINDOW", self.hide) 158 self.ed.sourceFile(resourceFile='_visionrc') 159 filemenu = self.ed.menuButtons['File'].menu 160 filemenu.entryconfig(filemenu.index('Quit'),command = self.hide) 161 # let Vision know which PMV instance runs it 162 self.ed.vf = self.vf 163 164 # add handle to Pmv library instance 165 from Pmv.VisionInterface.PmvNodes import pmvlib 166 self.lib = pmvlib 167 168 # initialize the API between PMV and Vision 169 self.vf.visionAPI.setEditor(self.ed) 170 self.vf.visionAPI.setLibrary(self.lib) 171 172 # add Pmv library to Vision 173 self.ed.addLibraryInstance(pmvlib, 'Pmv.VisionInterface.PmvNodes', 174 'pmvlib') 175 176 # add Standard library to Vision 177 from Vision.StandardNodes import stdlib 178 self.ed.addLibraryInstance(stdlib, 'Vision.StandardNodes', 179 'stdlib') 180 181 # add MolKit library to Vision if needed 182 if len(self.vf.Mols): 183 from MolKit.VisionInterface.MolKitNodes import molkitlib 184 self.ed.addLibraryInstance( 185 molkitlib, 'MolKit.VisionInterface.MolKitNodes', 'molkitlib') 186 187 # add molecule nodes to Pmv library if molecules were loaded in 188 # Pmv before Vision was started 189 for obj, name, kw in self.vf.visionAPI.objects: 190 self.vf.visionAPI.addNodeToLibrary(obj, name, kw) 191 192 # is ARTK running? if so, we need to add the library and nodes 193 if hasattr(self.vf, 'art') and self.vf.art.visionAPI is not None: 194 visionAPI = self.vf.art.visionAPI 195 visionAPI.setEditor(self.ed) 196 from ARViewer.VisionInterface.ARNodes import artlib 197 visionAPI.setLibrary(artlib) 198 visionAPI.ed.addLibraryInstance( 199 artlib, 'ARTK.VisionInterface.ARNodes', 'artlib') 200 # add nodes to ARTK library 201 for obj, name, kw in visionAPI.objects: 202 visionAPI.addNodeToLibrary(obj, name, kw) 203 204 # and add link to ARViewer 205 self.ed.art = self.vf.art 206 207 elif self.vf.GUI.toolbarCheckbuttons['Vision']['Variable'].get() == 0 and self.root: 208 self.root.withdraw() 209 elif self.vf.GUI.toolbarCheckbuttons['Vision']['Variable'].get() == 1 and self.root: 210 self.root.deiconify() 211 212 ################################################################## 213 # FIXME: 214 # Workaround: currently, Vision would crash on some Windows2000 and SGI 215 # when running multi-threaded. We turn MT off by default 216 ################################################################## 217 #if sys.platform == 'win32' or sys.platform == 'irix646': 218 self.ed.configure(withThreads=0)
219
220 - def show(self, event=None):
221 if self.root is None: 222 self.doitWrapper() 223 else: 224 self.root.deiconify() 225 self.vf.GUI.toolbarCheckbuttons['Vision']['Variable'].set(1)
226 227
228 - def hide(self, event=None):
229 if self.root: 230 self.root.withdraw() 231 self.vf.GUI.toolbarCheckbuttons['Vision']['Variable'].set(0)
232 233
234 - def guiCallback(self):
235 self.doitWrapper()
236 237
238 - def __call__(self, **kw):
239 """None <- vision(**kw) starts the visual programming environment""" 240 apply( self.doitWrapper, (), kw )
241 242 243 244 VisionCommandGUI = MVCommandGUI() 245 msg = 'show/Hide the visual programming environment Vision' 246 from moleculeViewer import ICONPATH 247 VisionCommandGUI.addToolBar('Vision', icon1='vision.png', balloonhelp=msg, 248 icon_dir=ICONPATH, index=5) 249 250 251
252 -class ExportSets(MVCommand):
253 """Command to export PMV sets (sets defined through the selection command) 254 into Vision. Each set is exported as a Vision node, added to the Pmv Library 255 into the category Molecules.""" 256 257
258 - def __init__(self):
259 self.form = None 260 MVCommand.__init__(self)
261 262
263 - def onAddNewCmd(self, cmd):
264 # Vision command needs to be loaded 265 if not hasattr(self, 'vision'): 266 self.vf.browseCommands( 267 'visionCommands', 268 commands=['vision'], package='Pmv', topCommand=0)
269 270
271 - def doit(self, sets):
272 if not len(sets.keys()): 273 return 274 275 # has Vision been instanciated? 276 if self.vf.vision.root is None: 277 self.vf.vision.doit() 278 279 # pass set instance and string representation of set to Vision 280 lib = self.vf.vision.lib 281 for name, set in sets.items(): 282 found = 0 283 for node in lib.libraryDescr['Molecules']['nodes']: 284 if node.name == name: # set already defined 285 found = 1 286 break 287 288 if not found: 289 selString = set[0].full_name() 290 lib.addNode(PmvSetNode, name, 'Molecules', 291 args=(set[0],selString), 292 kw={'constrkw': 293 {'set':'self.vf.sets["%s"]'%name, 294 'selString':'selString'} })
295 296
297 - def __call__(self, sets, *args, **kw):
298 """export sets ( sets )""" 299 apply( self.doitWrapper, (sets)+args, kw)
300 301
302 - def guiCallback(self):
303 #if not sets__.keys(): 304 if not self.vf.sets.keys(): 305 self.warningMsg("No sets defined!", title="%s WARNING: "%self.name) 306 return 307 # FIXME: this is a HACK! we have to re-create the form every time 308 # since more sets might have been added 309 if hasattr(self, 'cmdForms') and len(self.cmdForms.keys()) and \ 310 self.cmdForms.has_key('Select Sets'): 311 self.cmdForms['Select Sets'].destroy() 312 form = InputForm(self.vf.GUI.ROOT, None, 313 self.buildFormDescr('Select Sets')) 314 self.cmdForms['Select Sets'] = form 315 316 result = self.showForm('Select Sets') 317 if result and len(result.keys()): 318 sets = {} 319 for k, v in result.items(): 320 if v == 1: 321 sets[k] = self.vf.sets[k] 322 #sets[k] = sets__[k] 323 if len(sets.keys()): 324 apply( self.doitWrapper, (sets,), {} )
325 326
327 - def buildFormDescr(self, formName):
328 #from Pmv.selectionCommands import sets__ 329 #if not sets__.keys(): 330 if not self.vf.sets.keys(): 331 self.warningMsg("No sets defined!") 332 return 333 334 ifd = InputFormDescr(title = 'Select Sets') 335 336 gridrow = 0 337 #for k in sets__.keys(): 338 for k in self.vf.sets.keys(): 339 ifd.append({'name': k, 340 'widgetType':Tkinter.Checkbutton, 341 'wcfg':{ 'text': k, 342 'variable': Tkinter.IntVar(), 343 'command': None}, 344 'gridcfg':{'sticky':Tkinter.W,'row':gridrow,'column':0, 345 'columnspan':2}, 346 }) 347 gridrow = gridrow + 1 348 return ifd
349 350 351 352 ExportSetsGUI = CommandGUI() # adds ok/cancel 353 ExportSetsGUI.addMenuCommand('menuRoot', 'Select', 'Add to Vision', 354 cascadeName='Export Sets') 355 356 commandList = [ 357 {'name':'vision', 'cmd':VisionCommand(), 'gui':VisionCommandGUI}, 358 {'name':'exportSets', 'cmd':ExportSets(), 'gui':ExportSetsGUI}, 359 ] 360
361 -def initModule(viewer):
362 for dict in commandList: 363 viewer.addCommand(dict['cmd'], dict['name'], dict['gui'])
364