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

Source Code for Module Pmv.interactiveCommands

  1  ############################################################################# 
  2  # 
  3  # Author: Michel F. SANNER 
  4  # 
  5  # Copyright: M. Sanner TSRI 2000 
  6  # 
  7  ############################################################################# 
  8   
  9  # 
 10  # $Header: /opt/cvs/python/packages/share1.5/Pmv/interactiveCommands.py,v 1.29 2007/01/26 01:37:41 vareille Exp $ 
 11  # 
 12  # $Id: interactiveCommands.py,v 1.29 2007/01/26 01:37:41 vareille Exp $ 
 13  # 
 14   
 15  from MolKit.molecule import Atom, Molecule 
 16  from MolKit.protein import Protein, Residue, Chain 
 17  from MolKit.tree import TreeNode 
 18  from Pmv.mvCommand import MVCommand, MVICOM 
 19  from Pmv.picker import AtomPicker 
 20  from ViewerFramework.VFCommand import CommandGUI, ICOM, ActiveObject, Command 
 21  ##  from ViewerFramework.gui import InputFormDescr 
 22  from mglutil.gui.InputForm.Tk.gui import InputFormDescr 
 23  import Tkinter, Pmw, types, string, os 
 24   
 25   
26 -class MVSetInteractiveCmd(MVCommand):
27 28
29 - def getLevelName(self, level ):
30 if level==Atom: level = 'Atom' 31 elif level==Residue: level = 'Residue' 32 elif level==Chain: level = 'Chain' 33 elif level==Molecule or isinstance(newValue,Protein): 34 level = 'Molecule' 35 return level
36 37
38 - def setupUndoBefore(self, cmd, modifier=None):
39 cmdDict = self.vf.ICmdCaller.commands.value 40 cmd = cmdDict[modifier] 41 self.addUndoCall( (cmd, modifier), {}, self.name )
42 43
44 - def doit(self, cmd, modifier=None):
45 icom = self.vf.ICmdCaller 46 icom.setCommands(cmd, modifier)
47 48
49 - def __call__(self, cmd, modifier=None, **kw):
50 """None <- setICOM(cmd, modifier, **kw) 51 cmd: command 52 modifier: name of the modifier ('Shift_L', 'Control_L', 'Alt_L' ...) 53 """ 54 if cmd is not None: 55 assert isinstance(cmd, ICOM) 56 kw['modifier'] = modifier 57 apply( self.doitWrapper, (cmd, ), kw )
58 59
60 - def command_cb(self, val):
61 cmd = self.vf.commands[val] 62 apply( self.doitWrapper, (cmd, None), {} )
63 64
65 - def level_cb(self, val):
66 apply( self.vf.setIcomLevel, (eval(val),), {} )
67 68 69
70 -def getICOMBar(viewer):
71 # selection command use buttons in a selectionBar 72 # this function creates the bar if it does not exist yet else returns it 73 if not viewer.GUI.menuBars.has_key('icomBar'): 74 ## viewer.GUI.addMenuBar('icomBar', 75 ## {'borderwidth':2, 'relief':'raised'}, 76 ## {'side':'top', 'expand':1, 'fill':'x'}) 77 viewer.GUI.addMenuBar('icomBar', 78 {'borderframe':0, 'horizscrollbar_width':7, 79 'vscrollmode':'none', 'frame_relief':'groove', 80 'frame_borderwidth':2,}, 81 # {'frame_borderwidth':2, 82 # 'frame_relief':'raised', 83 # 'horizscrollbar_width':7, 84 # 'vscrollmode':'none', 85 # 'usehullsize':1, 'hull_width':300, 86 # 'hull_height':53}, 87 {'side':'top', 'expand':1, 'fill':'x'}) 88 return viewer.GUI.menuBars['icomBar']
89 90 91 from DejaVu.colorTool import TkColor 92 93 from mglutil.gui.BasicWidgets.Tk.customizedWidgets import kbComboBox 94 95
96 -class MVIcomGUI(MVCommand):
97 98
99 - def updateICOMWidget(self, command, modifier):
100 if command is None: name='None' 101 else: name = command.name 102 if modifier==None: 103 self.NoModWidget.selectitem(name) 104 elif modifier=='Shift_L': 105 self.ShiftWidget.selectitem(name) 106 elif modifier=='Control_L': 107 self.CtrlWidget.selectitem(name) 108 elif modifier=='Alt_L': 109 self.AltWidget.selectitem(name)
110 111
112 - def onAddNewCmd(self, cmd):
113 if not self.vf.hasGui: return 114 names = ['None'] + self.getICOMNames() 115 #self.NoModWidget.setitems(names) 116 self.NoModWidget.setlist(names) 117 #self.ShiftWidget.setitems(names) 118 self.ShiftWidget.setlist(names) 119 #self.CtrlWidget.setitems(names) 120 self.CtrlWidget.setlist(names) 121 #self.AltWidget.setitems(names) 122 self.AltWidget.setlist(names)
123 124
125 - def getICOMNames(self):
126 names = filter( lambda x, klass=ICOM: isinstance(x, klass), 127 self.vf.commands.values() ) 128 names = map( lambda x: x.name, names ) 129 names.sort() 130 return names
131 132
133 - def onAddCmdToViewer(self):
134 if not self.vf.hasGui: 135 return 136 137 bar = getICOMBar(self.vf) 138 if isinstance(bar, Pmw.ScrolledFrame): 139 barFrame = bar.interior() 140 else: 141 barFrame = bar 142 self.vf.ICmdCaller.commands.AddCallback(self.updateICOMWidget) 143 144 # create level radio buttons 145 options = ['Atom', 'Residue', 'Chain', 'Molecule' ] 146 col = self.vf.ICmdCaller.levelColors 147 self.LevelOption = Pmw.OptionMenu(barFrame, 148 label_text = 'picking level:', 149 labelpos = 'w', 150 initialitem = options[0], 151 command = self.setLevel_cb, 152 items = options, 153 menubutton_pady=0, 154 ) 155 self.LevelOption.pack(side = 'left', anchor = 'nw', 156 expand = 1, padx = 2, pady = 1) 157 158 for o in options: 159 c = (col[o][0]/1.5,col[o][1]/1.5,col[o][2]/1.5) 160 self.LevelOption._menu.entryconfigure( 161 self.LevelOption._menu.index(o), 162 background = TkColor(c), 163 activebackground = TkColor(col[o])) 164 165 c = (col[options[0]][0]/1.5,col[options[0]][1]/1.5,col[options[0]][2]/1.5) 166 self.LevelOption._menubutton.configure( 167 background = TkColor(c), 168 activebackground = TkColor(col[options[0]])) 169 170 bar = getICOMBar(self.vf) 171 if isinstance(bar, Pmw.ScrolledFrame): 172 barFrame = bar.interior() 173 else: 174 barFrame = bar 175 #self.Label = Tkinter.Label(barFrame, text='PCOM: ') 176 #self.Label.pack(side='left') 177 178 # get the sorted list of ICOMS 179 names = ['None'] + self.getICOMNames() 180 181 # create modifier dropDown combo box 182 #self.NoModWidget = Pmw.OptionMenu(barFrame, labelpos = 'w', 183 # command = self.NoMod_cb, items = names ) 184 def comboBoxValidatorNoMod(event=None): 185 return event in self.NoModWidget._list.get()
186 self.NoModWidget = Pmw.ComboBox( barFrame, 187 #label_text = 'NoMod:', 188 labelpos = 'w', 189 entryfield_value='None', 190 selectioncommand=self.NoMod_cb, 191 scrolledlist_items=names, 192 history = False, 193 #entryfield_entry_width=12, 194 ) 195 self.NoModWidget.configure(entryfield_validate=comboBoxValidatorNoMod) 196 self.NoModWidget.pack(side = 'left', anchor = 'nw', 197 expand = 1, padx = 2, pady = 1) 198 199 cmdDict = self.vf.ICmdCaller.commands.value 200 cmd=cmdDict[None] 201 if cmd is None: name = 'None' 202 else: name = cmd.name 203 self.NoModWidget.selectitem(name) 204 205 #self.ShiftWidget = Pmw.OptionMenu(barFrame, 206 # label_text = 'Shift:', labelpos = 'w', 207 # command = self.Shift_cb, items = names) 208 def comboBoxValidatorShift(event=None): 209 return event in self.ShiftWidget._list.get()
210 self.ShiftWidget = Pmw.ComboBox( barFrame, 211 label_text = 'Shift:', 212 labelpos = 'w', 213 entryfield_value='None', 214 selectioncommand=self.Shift_cb, 215 scrolledlist_items=names, 216 history = False, 217 #entryfield_entry_width=12, 218 ) 219 self.ShiftWidget.configure(entryfield_validate=comboBoxValidatorShift) 220 self.ShiftWidget.pack(side = 'left', anchor = 'nw', 221 expand = 1, padx = 2, pady = 1) 222 cmd = cmdDict['Shift_L'] 223 if cmd is None: name = 'None' 224 else: name = cmd.name 225 self.ShiftWidget.selectitem(name) 226 227 #self.CtrlWidget = Pmw.OptionMenu(barFrame, 228 # label_text = 'Ctrl:', labelpos = 'w', 229 # command = self.Ctrl_cb, items = names) 230 def comboBoxValidatorCtrl(event=None): 231 return event in self.CtrlWidget._list.get() 232 self.CtrlWidget = Pmw.ComboBox( barFrame, 233 label_text = 'Ctrl:', 234 labelpos = 'w', 235 entryfield_value='None', 236 selectioncommand=self.Ctrl_cb, 237 scrolledlist_items=names, 238 history = False, 239 #entryfield_entry_width=12, 240 ) 241 self.CtrlWidget.configure(entryfield_validate=comboBoxValidatorCtrl) 242 self.CtrlWidget.pack(side = 'left', anchor = 'nw', 243 expand = 1, padx = 2, pady = 1) 244 cmd = cmdDict['Control_L'] 245 if cmd is None: name = 'None' 246 else: name = cmd.name 247 self.CtrlWidget.selectitem(name) 248 249 #self.AltWidget = Pmw.OptionMenu(barFrame, 250 # label_text = 'Alt:', labelpos = 'w', 251 # command = self.Alt_cb, items = names) 252 def comboBoxValidatorAlt(event=None): 253 return event in self.AltWidget._list.get() 254 self.AltWidget = Pmw.ComboBox( barFrame, 255 label_text = 'Alt:', 256 labelpos = 'w', 257 entryfield_value='None', 258 selectioncommand=self.Alt_cb, 259 scrolledlist_items=names, 260 history = False, 261 #entryfield_entry_width=12, 262 ) 263 self.AltWidget.configure(entryfield_validate=comboBoxValidatorAlt) 264 self.AltWidget.pack(side = 'left', anchor = 'nw', 265 expand = 1, padx = 2, pady = 1) 266 cmd = cmdDict['Alt_L'] 267 if cmd is None: name = 'None' 268 else: name = cmd.name 269 self.AltWidget.selectitem(name) 270 self.bar = bar 271 self.bar.pack_forget() 272 273
274 - def guiCallback(self, event=None):
275 self.vf.ICOMbar.bar.pack() 276 if self.vf.GUI.toolbarCheckbuttons['PCOM']['Variable'].get(): 277 self.vf.ICOMbar.bar.pack(fill='x') 278 self.vf.GUI.ROOT.update() 279 w = self.vf.GUI.ROOT.winfo_width() 280 h = self.vf.GUI.ROOT.winfo_reqheight() 281 h1 = self.vf.GUI.ROOT.winfo_height() 282 if h1 > h: 283 h = h1 284 self.vf.GUI.ROOT.geometry('%dx%d' % ( w, h) ) 285 else: 286 self.vf.ICOMbar.bar.pack_forget()
287 288
289 - def NoMod_cb(self, cmdName):
290 if isinstance(cmdName, Tkinter.Event): 291 cmdName = cmdName.widget.get(cmdName.widget.curselection()) 292 if cmdName=='None' or cmdName is None: cmd=None 293 else: cmd = self.vf.commands[cmdName] 294 self.vf.setICOM(cmd, None)
295 296
297 - def Shift_cb(self, cmdName):
298 if isinstance(cmdName, Tkinter.Event): 299 cmdName = cmdName.widget.get(cmdName.widget.curselection()) 300 if cmdName=='None' or cmdName is None: cmd=None 301 else: cmd = self.vf.commands[cmdName] 302 self.vf.setICOM(cmd, 'Shift_L')
303 304
305 - def Ctrl_cb(self, cmdName):
306 if isinstance(cmdName, Tkinter.Event): 307 cmdName = cmdName.widget.get(cmdName.widget.curselection()) 308 if cmdName=='None' or cmdName is None: cmd=None 309 else: cmd = self.vf.commands[cmdName] 310 self.vf.setICOM(cmd, 'Control_L')
311 312
313 - def Alt_cb(self, cmdName):
314 if isinstance(cmdName, Tkinter.Event): 315 cmdName = cmdName.widget.get(cmdName.widget.curselection()) 316 if cmdName=='None' or cmdName is None: cmd=None 317 else: cmd = self.vf.commands[cmdName] 318 self.vf.setICOM(cmd, 'Alt_L')
319 320
321 - def setLevel_cb(self, level):
322 col = self.vf.ICmdCaller.levelColors 323 c = (col[level][0]/1.5,col[level][1]/1.5,col[level][2]/1.5) 324 self.LevelOption._menubutton.configure( 325 background = TkColor(c), 326 activebackground = TkColor(col[level])) 327 if level=='Atom': 328 self.vf.setIcomLevel(Atom) 329 elif level=='Residue': 330 self.vf.setIcomLevel(Residue) 331 elif level=='Chain': 332 self.vf.setIcomLevel(Chain) 333 elif level=='Molecule': 334 self.vf.setIcomLevel(Molecule)
335 336 PCOMGUI = CommandGUI() 337 from moleculeViewer import ICONPATH 338 PCOMGUI.addToolBar('PCOM', icon1 = 'mouse.gif', icon_dir=ICONPATH, 339 balloonhelp = 'Picking COMmands', index = 0)
340 -class MVBindCmdToKey(MVCommand):
341
342 - def __init__(self, func=None):
343 MVCommand.__init__(self, func=None) 344 otherKeys = ['Escape', 'F1', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 345 'F9', 'Tab', 'space', 'Return', 'Insert', 'Delete', 346 'Home', 'End', 'Prior', 'Next', 'Up', 'Down', 'Left', 347 'Right', 'Caps_Lock', 'Num_Lock', 'BackSpace'] 348 349 self.bindings = {} 350 for key in string.letters+string.digits: 351 self.bindings[key] = {'None':None, 'Shift_L':None,\ 352 'Control_L':None, 'Alt_L':None, 353 'Shift_R':None, 'Control_R':None, 354 'Alt_R':None } 355 for key in otherKeys: 356 self.bindings[key] = {'None':None, 'Shift_L':None,\ 357 'Control_L':None, 'Alt_L':None, 358 'Shift_R':None, 'Control_R':None, 359 'Alt_R':None }
360 361
362 - def onAddCmdToViewer(self):
363 if self.vf.hasGui: 364 self.vf.GUI.addCameraCallback('<KeyRelease>', 365 self.processRelease_cb)
366
367 - def processRelease_cb(self, event):
368 if not self.bindings.has_key(event.keysym): 369 return 370 dict = self.bindings[event.keysym] 371 mod = self.vf.GUI.VIEWER.getModifier() 372 c = dict[mod] 373 if c: 374 apply( c[0], c[1], c[2] )
375 376
377 - def setupUndoBefore(self, key, modifier, cmd, args, kw):
378 if not self.bindings.has_key(key): return 379 c = self.bindings[key][modifier] 380 if c: cmd = c 381 else: c = None 382 self.addUndoCall( (key, modifier, cmd, args, kw), {}, self.name )
383 384
385 - def doit(self, key, modifier, cmd, args=(), kw={}):
386 assert isinstance(cmd, Command) 387 assert modifier in ['None', 'Shift_L', 'Control_L', 'Alt_L', 388 'Shift_R', 'Control_R', 'Alt_R'] 389 assert key in self.bindings.keys() 390 if cmd is None: cmd='None' 391 self.bindings[key][modifier] = (cmd, args, kw)
392 393
394 - def __call__(self, key, modifier, cmd, args=(), kwa={}, **kw):
395 """None <- bindCmdToKey(key, modifier, cmd, args=(), kwa={}, **kw) 396 key: name of the key 397 modifier: name of the modifier ('Shift_L', 'Control_L', 'Alt_L' ...) 398 cmd: name of the command which wanted to be bound to the key""" 399 400 apply( self.doitWrapper, (key, modifier, cmd, args, kwa), kw)
401 402 403 404 commandList = [ 405 {'name':'ICOMbar', 'cmd':MVIcomGUI(), 'gui':PCOMGUI}, 406 {'name':'setICOM', 'cmd':MVSetInteractiveCmd(), 'gui':None}, 407 408 {'name':'bindCmdToKey', 'cmd':MVBindCmdToKey(), 'gui':None}, 409 ] 410
411 -def initModule(viewer):
412 for dict in commandList: 413 viewer.addCommand(dict['cmd'], dict['name'], dict['gui'])
414