"""
:filename: CmdBttnPlugin.py
:author: roar@tordivel.no
:requirements: Scorpion 9.0.0.494 or higher
Scorpion Plugin for user configurable panel with buttons to
send commands to Scorpion
::
1.0.0.6, 11nov2015, RL: modified for autodoc
1.0.0.5, 10jan2013, RL:
using SpeedButtons
1.0.0.4, 10aug2012, RL:
removed accessControl
halign and valign replaces hcenter and vcenter
1.0.0.3, 06aug2012, RL:
added vcenter and hcenter
1.0.0.2, 24dec2011, TV:
enable both in Service and Settings
standard header - add versionlabel
1.0.0.1, 29sep2011, RL:
created
"""
__version__ = '1.0.0.6'
import os
from Scorpion import GetControlByHandle,SpbDialog,PluginChanged,ExecuteCmd,GetStringValue
[docs]class CmdBttnPlugin(object):
def __init__(self,cntr,name):
self.name=name #name of this instance
self.cntr=cntr #the container control
self.cntr.onResize=self.cntrResize #hook up to resize event to position controls
self.init() #initial state
self.addBttn('Configure','command','params') #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.cmds=[] #list of commands
self.params=[] #list of commands
self.glyphs=[] #list of all glyph names if any
self.desc=self.cntr.addControl('CaptionPanel') #add description label
self.desc.align=1 #top
self.desc.caption='Command 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
self.bttnWidth=75 #default button width
self.bttnHeight=25 #default button height
self.bttnLayout=2 #default glyph top
self.bttnFlat=False #default not flat buttons
[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,cmd,params,glyph=None):
'''
add a button to the plugin
'''
top=6
if self.desc.visible: top+=self.desc.bottom
bttn=self.cntr.addControl('SpeedButton',6+len(self.bttns)*self.bttnWidth,top)
bttn.width=self.bttnWidth
bttn.height=self.bttnHeight
bttn.layout=self.bttnLayout
bttn.flat=self.bttnFlat
bttn.tag=len(self.bttns) #current button index
bttn.showHint=True
bttn.onClick=self.bttnClick
if caption:bttn.caption=caption #set button caption
else: bttn.caption=''
if glyph:bttn.glyphName=os.path.join(GetStringValue('System.Profile'),glyph)
self.bttns.append(bttn) #append bttn
if cmd:self.cmds.append(cmd) #append bttn command
else: self.cmds.append('')
if params:self.params.append(params) #append bttn command porameters
else: self.params.append('')
if glyph:self.glyphs.append(glyph) #append bttn glyph to list
else: self.glyphs.append('')
return bttn
[docs] def getConfig(self):
'''
returns plugin configuration as xml
'''
from SPB import CreateSpb
spb=CreateSpb()
spb.setText('type',self.__class__.__name__)
spb.setInt('version',2)
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)
spb.setInt('bttn.width',self.bttnWidth)
spb.setInt('bttn.height',self.bttnHeight)
spb.setInt('bttn.layout',self.bttnLayout)
spb.setBool('bttn.flat',self.bttnFlat)
for i in range(len(self.bttns)):
spb.setText('button%d.caption'%(i+1),self.bttns[i].caption)
spb.setText('button%d.command'%(i+1),self.cmds[i])
spb.setText('button%d.params'%(i+1),self.params[i])
spb.setText('button%d.glyphName'%(i+1),self.glyphs[i])
spb.setInt('button%d.numGlyphs'%(i+1),self.bttns[i].numGlyphs)
spb.setInt('button%d.groupIndex'%(i+1),self.bttns[i].groupIndex)
spb.setBool('button%d.allowAllUp'%(i+1),self.bttns[i].allowAllUp)
spb.setText('button%d.hint'%(i+1),self.bttns[i].hint)
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') in [1,2]:
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('bttn.width'):self.bttnWidth=spb.getInt('bttn.width')
if spb.isEntry('bttn.height'):self.bttnHeight=spb.getInt('bttn.height')
if spb.isEntry('bttn.layout'):self.bttnLayout=spb.getInt('bttn.layout')
if spb.isEntry('bttn.flat'):self.bttnFlat=spb.getBool('bttn.flat')
if spb.isEntry('count'):
for i in range(spb.getInt('count')):
bttn=self.addBttn(spb.getText('button%d.caption'%(i+1)),spb.getText('button%d.command'%(i+1)),spb.getText('button%d.params'%(i+1)),spb.getText('button%d.glyphName'%(i+1)))
if spb.isEntry('button%d.hint'%(i+1)):bttn.hint=spb.getText('button%d.hint'%(i+1))
if spb.isEntry('button%d.numGlyphs'%(i+1)):bttn.numGlyphs=spb.getInt('button%d.numGlyphs'%(i+1))
if spb.isEntry('button%d.groupIndex'%(i+1)):bttn.groupIndex=spb.getInt('button%d.groupIndex'%(i+1))
if spb.isEntry('button%d.allowAllUp'%(i+1)):bttn.allowAllUp=spb.getBool('button%d.allowAllUp'%(i+1))
self.positionControls()
[docs] def cntrResize(self,sender,args):
'''
reposition controls
'''
self.positionControls()
[docs] def bttnClick(self,sender,args):
'''
callback when user clicks a button
'''
if sender in self.bttns:
ExecuteCmd(self.cmds[sender.tag],self.params[sender.tag])
[docs]def CreatePlugin(hWnd, name=''):
'''
Scorpion Plugin Stub - Required
'''
cntr=GetControlByHandle(hWnd)
return CmdBttnPlugin(cntr,name)