Source code for ImageHistoryPlugin

"""
:filename:     ImageHistoryPlugin.py
:author:       roar@tordivel.no
:requirements: Scorpion 9.0.0.527 or higher

Scorpion plugin panel for showing image history list

::

  1.0.0.5, 17jan2014, roar@tordivel.no:
    updated icons
  1.0.0.4, 12sep2012, roar@tordivel.no:
    updates on afterInspect
  1.0.0.3, 28aug2012, roar@tordivel.no:
    using SpeedButton
  1.0.0.2, 30may2012, thor@tordivel.no:
	   Configure changed to Folder
  1.0.0.1, 30nov2011, roar@tordivel.no:
    initial version
"""

__version__ = '1.0.0.5'

from Scorpion import RegisterCallback,PluginChanged,PluginNotify,GetControlByHandle,GetImageBuffer,GetStatistics,ExecuteCmd,GetBoolValue,SelectDirectory,SpbDialog
import time,os.path

[docs]class ImageHistoryPlugin(object): """Scorpion GUI plugin for viewing image history"""
[docs] class HistoryItem(object): ''' single history item controls container ''' def __init__(self,owner,offset,top): self.owner=owner self.offset=offset self.lblState=self.owner.historyPanel.addControl('Label',16,top) self.lblState.font.bold=self.owner.highlightSelected and offset==0 self.pState=self.owner.historyPanel.addControl('Panel',6,self.lblState.top+self.lblState.height/2-3) self.pState.height=8 self.pState.width=8 self.pState.bevelInner=0 self.pState.bevelOuter=1 self.pState.caption='' self.update()
[docs] def update(self): ''' update controls for actual item ''' ib=GetImageBuffer() imageno=ib.getImageNo(self.owner.index+self.offset) #this items imageno state=ib.getState(imageno) if state==1:self.pState.color='white' elif state==2:self.pState.color='lime' elif state==3:self.pState.color='yellow' elif state==4:self.pState.color='red' else: self.pState.color=self.owner.historyPanel.color if state==None: self.lblState.caption=' -' else: try: t=time.strftime(self.owner.timeFormat,time.strptime(ib.getTime(imageno),"%Y%m%d_%H%M%S")) except: t=ib.getTime(imageno) self.lblState.caption='%5d %s %s'%(imageno,t,ib.getComment(imageno))
def __init__(self,cntr,name): ''' initiate plugin with default values ''' self.host=cntr #keep a copy of host so we can hook into resize self.name=name #remember name as it is used in __str__ method self.index=0 #current selected index self.historyLength=4 #no of visible items self.buttonWidth=75 #default button width self.buttonHeight=-1 #default button height, -1 equals auto height self.fontSize=8 #common font size self.timeFormat='%H:%M:%S' #default time format self.highlightSelected=False #flag for highlightening selected item self.resetCurves=False #flag for resetting curves on reset self.zeroStatistics=False #flag for zeroing statistics on reset self.resetStatistics=False #flag for resetting statistics on reset self.resetWhenRunning=False #flag for resetting while running only self.showButtons=False #master flag for visible buttons self.showUpDown=True #flag for visible up/down buttons self.showConfigure=True #flag for visible up/down buttons self.showSave=True #flag for visible save button self.showSaveAll=True #flag for visible saveAll button self.showReset=True #flag for visible reset button self.historyItems=[] #container for dynamic number of items self.verbose=0 #debug level, 0=None, 1=debug printouts self.bttns=[] #all buttons in control cntr.deleteControls() #delete previous added controls if any self.gbHistory=cntr.addControl('GroupBox'); self.gbHistory.caption='Image history' self.gbHistory.align=5 #client #create a panel holding controls for user interaction self.bttnPanel=self.gbHistory.addControl('Panel'); self.bttnPanel.width=10+2*self.buttonWidth #spacing+buttons self.bttnPanel.align=4 #right self.bttnPanel.bevelInner=0 self.bttnPanel.bevelOuter=0 self.bttnPanel.caption='' self.bttnPanel.font.size=self.fontSize #child will inherit fontSize self.bttnPanel.onResize=self.bttnPanelResize #hook up into resize event self.bUp=self.bttnPanel.addControl('SpeedButton',3,0) #top/height will be handled in resize self.bDown=self.bttnPanel.addControl('SpeedButton',3,self.bUp.bottom+2) self.bConfigure=self.bttnPanel.addControl('SpeedButton',3,self.bDown.bottom+2) self.bSave=self.bttnPanel.addControl('SpeedButton',self.bUp.right+2,0) self.bSaveAll=self.bttnPanel.addControl('SpeedButton',self.bUp.right+2,self.bSave.bottom+2) self.bReset=self.bttnPanel.addControl('SpeedButton',self.bUp.right+2,self.bSaveAll.bottom+2) self.bUp.margin=3 self.bDown.margin=3 self.bConfigure.margin=3 self.bSave.margin=3 self.bSaveAll.margin=3 self.bReset.margin=3 self.bUp.font.size=self.bttnPanel.font.size self.bDown.font.size=self.bttnPanel.font.size self.bConfigure.font.size=self.bttnPanel.font.size self.bSave.font.size=self.bttnPanel.font.size self.bSaveAll.font.size=self.bttnPanel.font.size self.bReset.font.size=self.bttnPanel.font.size self.bUp.glyphName=os.path.join(os.path.split(__file__)[0],'up_space32x16.bmp') self.bDown.glyphName=os.path.join(os.path.split(__file__)[0],'down_space32x16.bmp') self.bConfigure.glyphName=os.path.join(os.path.split(__file__)[0],'open_space32x16.bmp') self.bSave.glyphName=os.path.join(os.path.split(__file__)[0],'save_space32x16.bmp') self.bSaveAll.glyphName=os.path.join(os.path.split(__file__)[0],'save_space32x16.bmp') self.bReset.glyphName=os.path.join(os.path.split(__file__)[0],'refresh_space32x16.bmp') self.bUp.numGlyphs=2 self.bDown.numGlyphs=2 self.bConfigure.numGlyphs=2 self.bSave.numGlyphs=2 self.bSaveAll.numGlyphs=2 self.bReset.numGlyphs=2 self.bUp.caption='Up' self.bDown.caption='Down' self.bConfigure.caption='Folder' self.bSave.caption='Save' self.bSaveAll.caption='Save All' self.bReset.caption='Reset' self.bUp.hint='move up in history' self.bDown.hint='move down in history' self.bConfigure.hint='select simulation folder' self.bSave.hint='save current image' self.bSaveAll.hint='save all images' self.bReset.hint='reset history' self.bUp.showHint=True self.bDown.showHint=True self.bConfigure.showHint=True self.bSave.showHint=True self.bSaveAll.showHint=True self.bReset.showHint=True self.bUp.onClick=self.bUpClick self.bDown.onClick=self.bDownClick self.bConfigure.onClick=self.bConfigureClick self.bSave.onClick=self.bSaveClick self.bSaveAll.onClick=self.bSaveAllClick self.bReset.onClick=self.bResetClick self.historyPanel=self.gbHistory.addControl('Panel') self.historyPanel.align=5 #client, left of bttnPanel self.historyPanel.bevelInner=0 self.historyPanel.bevelOuter=0 self.historyPanel.caption='' self.createControls() #create historyItems RegisterCallback('HistoryList.New',self.historyListChanged) RegisterCallback('HistoryList.Update',self.historyListChanged) RegisterCallback('HistoryList.SelectionChange',self.historyListChanged) RegisterCallback('Actions.BeforeStart',self.actionsBeforeStart) RegisterCallback('Actions.AfterStop',self.actionsAfterStop) RegisterCallback('Actions.AfterInspect',self.actionsAfterInspect) RegisterCallback('System.AccessControlChanged',self.systemAccessControlChanged) self.bttnPanelResize(None,None) self.showControls() self.update() if self.verbose:print self,'__init__'
[docs] def __del__(self): ''' do nessesary cleanup if any ''' if self.verbose:print self,'__del__'
[docs] def __str__(self): ''' return a unique persistance name for host application storage ''' return '%s_%s'%(self.__class__.__name__,self.name.replace('.','_'))
[docs] def createControls(self): ''' create all historyitem controls ''' self.historyPanel.deleteControls() self.historyItems=[] for i in range(self.historyLength): self.historyItems.append(self.HistoryItem(self,i,4+i*self.fontSize*2))
[docs] def showControls(self): ''' show buttons due to configuration ''' self.bUp.visible=self.showUpDown self.bDown.visible=self.showUpDown self.bConfigure.visible=self.showConfigure and (GetBoolValue('System.Service') or GetBoolValue('System.Settings')) self.bSave.visible=self.showSave self.bSaveAll.visible=self.showSaveAll self.bReset.visible=self.showReset self.bttnPanel.visible=self.showButtons and (self.bUp.visible or self.bDown.visible or self.bConfigure.visible or self.bSave.visible or self.bSaveAll.visible or self.bReset.visible)
[docs] def enableControls(self): ''' enable buttons due to current selection and state ''' ib=GetImageBuffer() enable=not GetBoolValue('System.Running') self.bUp.enabled=enable and ib.currentIndex>0 self.bDown.enabled=enable and ib.currentIndex<ib.count-1 self.bConfigure.enabled=enable and (GetBoolValue('System.Service') or GetBoolValue('System.Settings')) self.bSave.enabled=ib.count>0 self.bSaveAll.enabled=ib.count>0 self.bReset.enabled=enable or self.resetWhenRunning
[docs] def update(self): ''' update controls due to imagebuffer state ''' for item in self.historyItems: item.update() self.enableControls()
[docs] def bttnPanelResize(self,sender,args): ''' resize controls ''' self.bUp.top=0 if self.buttonHeight<1: self.bUp.height=int(self.bttnPanel.clientHeight/3)-2 else: self.bUp.height=self.buttonHeight self.bUp.width=self.buttonWidth self.bDown.top=self.bUp.bottom+2 self.bDown.height=self.bUp.height self.bDown.width=self.bUp.width self.bConfigure.top=self.bDown.bottom+2 self.bConfigure.height=self.bUp.height self.bConfigure.width=self.bUp.width self.bSave.left=self.bUp.right+2 self.bSave.top=self.bUp.top self.bSave.width=self.bUp.width self.bSave.height=self.bUp.height self.bSaveAll.left=self.bSave.left self.bSaveAll.top=self.bSave.bottom+2 self.bSaveAll.width=self.bUp.width self.bSaveAll.height=self.bUp.height self.bReset.left=self.bSave.left self.bReset.top=self.bSaveAll.bottom+2 self.bReset.width=self.bUp.width self.bReset.height=self.bUp.height
[docs] def historyListChanged(self,ImageNo,Comment): ''' callback when history changes ''' if self.verbose:print 'historyListChanged',ImageNo,Comment self.index=GetImageBuffer().indexOf(ImageNo) self.update() if Comment==None:return '' #fix bug - returning None returns "None" string return Comment
[docs] def actionsAfterInspect(self): ''' ''' self.update()
[docs] def actionsBeforeStart(self): ''' disable all buttons while running ''' self.enableControls()
[docs] def actionsAfterStop(self): ''' enable all buttons while not running ''' self.enableControls()
[docs] def systemAccessControlChanged(self,settings,service): ''' show configure button when entering either service or settings if showConfigure ''' self.showControls() self.enableControls()
[docs] def bUpClick(self,sender,args): ''' scroll up ''' GetImageBuffer().currentIndex=GetImageBuffer().currentIndex-1
[docs] def bDownClick(self,sender,args): ''' scroll down ''' GetImageBuffer().currentIndex=GetImageBuffer().currentIndex+1
[docs] def bConfigureClick(self,seder,args): ib=GetImageBuffer() ok,path=SelectDirectory(ib.path) if ok: ib.path=path
[docs] def bSaveClick(self,sender,args): ''' save selected image ''' if self.verbose:print self,'bSaveClick' GetImageBuffer().save()
[docs] def bSaveAllClick(self,sender,args): ''' save all images ''' if self.verbose:print self,'bSaveAllClick' GetImageBuffer().saveAll()
[docs] def bResetClick(self,sender,args): ''' clear all history ''' self.index=0 GetImageBuffer().clear() if self.resetCurves: ExecuteCmd('curves','cmd=zero') if self.zeroStatistics: GetStatistics().zero() elif self.resetStatistics: GetStatistics().reset() self.update()
[docs] def getConfig(self): ''' returns plugin configuration as string ''' from SPB import CreateSpb spb=CreateSpb() spb.name=self.__class__.__name__ #create a spb instance, populate with the plugin configuration #and return the xml string spb.setInt('version',1) spb.setInt('historyLength',self.historyLength) spb.setInt('fontSize',self.fontSize) spb.setInt('buttonWidth',self.buttonWidth) spb.setInt('buttonHeight',self.buttonHeight) spb.setText('timeFormat',self.timeFormat) spb.setBool('highlightSelected',self.highlightSelected) spb.setBool('showButtons',self.showButtons) spb.setBool('showUpDown',self.showUpDown) spb.setBool('showConfigure',self.showConfigure) spb.setBool('showSave',self.showSave) spb.setBool('showSaveAll',self.showSaveAll) spb.setBool('showReset',self.showReset) spb.setBool('resetCurves',self.resetCurves) spb.setBool('zeroStatistics',self.zeroStatistics) spb.setBool('resetStatistics',self.resetStatistics) spb.setBool('resetWhenRunning',self.resetWhenRunning) return spb.xml
[docs] def setConfig(self,value): ''' set plugin configuration from the string 'value' ''' from SPB import CreateSpb spb=CreateSpb(value) # TODO: extract plugin configuration from the spb object if spb.name==self.__class__.__name__: if spb.getInt('version')==1: self.historyLength=spb.getInt('historyLength') self.fontSize=spb.getInt('fontSize') self.buttonWidth=spb.getInt('buttonWidth') self.buttonHeight=spb.getInt('buttonHeight') self.timeFormat=spb.getText('timeFormat') self.highlightSelected=spb.getBool('highlightSelected') if self.timeFormat==None:self.timeFormat='%Y.%m.%d %H:%M:%S' self.bttnPanel.width=10+2*self.buttonWidth self.bttnPanel.font.size=self.fontSize self.historyPanel.font.size=self.fontSize self.showButtons=spb.getBool('showButtons') self.showUpDown=spb.getBool('showUpDown') self.showConfigure=spb.getBool('showConfigure') self.showSave=spb.getBool('showSave') self.showSaveAll=spb.getBool('showSaveAll') self.showReset=spb.getBool('showReset') self.resetCurves=spb.getBool('resetCurves') self.zeroStatistics=spb.getBool('zeroStatistics') self.resetStatistics=spb.getBool('resetStatistics') self.resetWhenRunning=spb.getBool('resetWhenRunning') self.createControls() self.showControls() self.bttnPanelResize(self.bttnPanel,None) self.update()
[docs] def configure(self): ''' launch an input dialog for editing plugin ''' ok,cfg=SpbDialog('Plugin configuration',self.getConfig()) if ok: self.setConfig(cfg) PluginChanged(self)
[docs]def CreatePlugin(hWnd, name=''): ''' Scorpion Plugin Stub - Required ''' cntr=GetControlByHandle(hWnd) return ImageHistoryPlugin(cntr,name)