Source code for ToolEditPlugin

"""
:filename:     ToolEditPlugin.py
:author:       roar@tordivel.no
:requirements: Scorpion 9.0.0.494 or higher

Scorpion sample plugin for hosting controls inside a PluginManager
hosted panel/page

::

  1.0.0.4, 11nov2015, RL: modified for autodoc
  1.0.0.3, 10aug2012, RL:
    removed accessControl
    halign and valign replaces hcenter and vcenter
  1.0.0.2, 06aug2012, RL:
    added hcenter and vcenter
  1.0.0.1, 29sep2011, RL:
    created
"""

__version__ = '1.0.0.4'

from Scorpion import GetControlByHandle,SpbDialog,PluginChanged,GetTool

[docs]class ToolEditPlugin(object): """ToolEditPlugin""" def __init__(self,cntr,name): self.name=name #name of this instance self.cntr=cntr #the container control self.init() #initial state self.cntr.onResize=self.cntrResize #hook up to resize event to position controls self.addBttn('Configure','tool','2','') #add default button
[docs] def __str__(self): ''' return a unique persistance name for host application storage ''' return '%s_%s'%(self.__class__.__name__,self.name)
[docs] def init(self): ''' reset all content and add the description label ''' self.cntr.deleteControls() #delete previous added controls if any self.bttns=[] #list of buttons self.tools=[] #list of tools to edit self.tabs=[] #list of index of tab to configure self.contexts=[] #list of dialog contexts/captions self.desc=self.cntr.addControl('CaptionPanel') #add description label self.desc.align=1 #top self.desc.caption='Tool configuration' self.halign=0 #bttn horizontal alignment, 0=left,1=center,2=right self.valign=0 #bttn vertical alignment, 0=top,1=center,2=bottom
[docs] def positionControls(self): ''' set button positions ''' n=len(self.bttns) if n>0: if self.halign==0: left=6 elif self.halign==1: left=int(self.cntr.clientWidth/2 - (n*self.bttns[0].width+(n-1)*6)/2) else: left=self.cntr.clientWidth-(self.bttns[0].width+6)*len(self.bttns) if self.desc.visible: voffs=self.desc.bottom else: voffs=0 if self.valign==0: top=voffs+6 elif self.valign==1: top=voffs + int((self.cntr.clientHeight-voffs)/2 - self.bttns[0].height/2) else: top=self.cntr.clientHeight-self.bttns[0].height-6 for i in range(len(self.bttns)): self.bttns[i].left=left self.bttns[i].top=top left+=self.bttns[i].width+6
[docs] def addBttn(self,caption,tool,tab,context): ''' add a button to the plugin ''' top=6 if self.desc.visible: top+=self.desc.bottom bttn=self.cntr.addControl('Button',6,top) bttn.caption=caption bttn.left+=len(self.bttns)*(bttn.width+6) #adjust left position bttn.tag=len(self.bttns) #button index bttn.onClick=self.bttnClick self.bttns.append(bttn) self.tools.append(tool) self.tabs.append(tab) self.contexts.append(context)
[docs] def getConfig(self): ''' returns plugin configuration as xml ''' from SPB import CreateSpb spb=CreateSpb() spb.setText('type',self.__class__.__name__) spb.setInt('version',1) spb.setInt('count',len(self.bttns)) spb.setInt('halign',self.halign) spb.setInt('valign',self.valign) spb.setText('desc.caption',self.desc.caption) spb.setBool('desc.visible',self.desc.visible) for i in range(len(self.bttns)): spb.setText('button%d.caption'%(i+1),self.bttns[i].caption) spb.setText('button%d.tool'%(i+1),self.tools[i]) spb.setText('button%d.tab'%(i+1),self.tabs[i]) spb.setText('button%d.context'%(i+1),self.contexts[i]) return spb.xml
[docs] def setConfig(self,value): ''' set plugin configuration from the string 'value' ''' self.init() from SPB import CreateSpb spb=CreateSpb(value) if spb.getText('type')==self.__class__.__name__: if spb.getInt('version')==1: if spb.isEntry('halign'):self.halign=spb.getInt('halign') if spb.isEntry('valign'):self.valign=spb.getInt('valign') if spb.isEntry('desc.caption'):self.desc.caption=spb.getText('desc.caption') if spb.isEntry('desc.visible'):self.desc.visible=spb.getBool('desc.visible') if spb.isEntry('count'): for i in range(spb.getInt('count')): caption=spb.getText('button%d.caption'%(i+1)) tool=spb.getText('button%d.tool'%(i+1)) tab=spb.getText('button%d.tab'%(i+1)) context=spb.getText('button%d.context'%(i+1)) if caption and tool and tab: self.addBttn(caption,tool,tab,context) else: self.addBttn('Configure','name','2','') self.positionControls()
[docs] def configure(self): ''' launch an input dialog for editing tool ''' ok,cfg=SpbDialog('Plugin configuration',self.getConfig()) if ok: self.setConfig(cfg) PluginChanged(self)
[docs] def cntrResize(self,sender,args): ''' reposition controls ''' self.positionControls()
[docs] def bttnClick(self,sender,args): ''' callback when user clicks the configure button ''' if sender in self.bttns: t=GetTool(self.tools[sender.tag]) #button tag holds the button index if t.bound: #verify valid object before edit t.edit(self.tabs[sender.tag],self.contexts[sender.tag]) #launch config dialog with specified tab only
[docs]def CreatePlugin(hWnd, name=''): ''' Scorpion Plugin Stub - Required ''' cntr=GetControlByHandle(hWnd) return ToolEditPlugin(cntr,name)