Changeset 687 for moxy


Ignore:
Timestamp:
Nov 30, 2011 6:04:49 PM (14 years ago)
Author:
jemian
Message:

same problem, GUI fails when trying to set widget background color

File:
1 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified moxy/trunk/src/test/gui.py

    r686 r687  
    2323
    2424import epics
     25import threading
    2526
    2627from enthought.traits.api import HasTraits, String, List, Bool, Generic, \
    27     Float, Dict
     28    Float, Dict, Color
    2829from enthought.traits.ui.api import Item, UItem, UReadonly, View, StatusItem, \
    2930    Action, Handler, VGrid, Label
     
    3637
    3738
    38 class ActionHandler(Handler):
     39class ActionHandler(Handler, threading.Thread):
    3940    '''implements controls for PvMail GUI application'''
     41   
     42    def __init__(self):
     43        threading.Thread.__init__(self)
    4044   
    4145    def _findGui(self, uinfo):
     
    5458        gui.SetStatus('pressed <Run>, starting CA monitors')
    5559       
    56         kw = {'gui': gui}
    57         for name in ('prj:m1', 'prj:m2'):
    58             for field in ('VAL', 'RBV'):
    59                 pvname = name+"."+field
    60                 if pvname not in channel:
    61                     channel[pvname] = ch = epics.PV(pvname)
    62                     callback_index[pvname] = ch.add_callback(self.ca_callback, **kw)
     60        kw = {'gui': gui}       # needed for processing in the callback
     61        for axis in ('x', 'y'):
     62            for field in ('VAL', 'RBV', 'DMOV'):
     63                ref = gui.axes[axis][field]
     64                pvname = ref['pvname']
     65                ref['channel'] = ch = epics.PV(pvname)
     66                ref['cb_index'] = ch.add_callback(self.ca_callback, **kw)
     67                gui.axes[pvname] = {'axis': axis, 'field': field}
     68                if field == 'DMOV':
     69                    for editor in uinfo.ui._editors:
     70                        if editor.name == axis + "_VAL":
     71                            ref['VAL_editor'] = editor
     72                        if editor.name == axis + "_RBV":
     73                            ref['RBV_editor'] = editor
    6374   
    6475    def do_stop(self, uinfo):
     
    7384        gui.SetStatus('pressed <Stop>')
    7485       
    75         for pvname in channel.keys():
    76             channel[pvname].remove_callback( callback_index[pvname] )
    77             del callback_index[pvname]
    78             del channel[pvname]
     86        for axis in ('x', 'y'):
     87            for field in ('VAL', 'RBV', 'DMOV'):
     88                ref = gui.axes[axis][field]
     89                if ref['cb_index']:
     90                    ref['channel'].remove_callback( ref['cb_index'] )
     91                    ref['cb_index'] = None
     92                    ref['channel'] = None
     93                    if field == 'DMOV':
     94                        ref['VAL_editor'] = None
     95                        ref['RBV_editor'] = None
    7996   
    8097    def ca_callback(self, **kw):
     
    83100        '''
    84101        pvname = kw['pvname']
     102        value = kw['value']
    85103        gui = kw['gui']
    86         value = kw['value']
    87 
    88         if pvname == "prj:m1.VAL":
    89             gui.x_VAL = value
    90         elif pvname == "prj:m1.RBV":
    91             gui.x_RBV = value
    92         elif pvname == "prj:m2.VAL":
    93             gui.y_VAL = value
    94         elif pvname == "prj:m2.RBV":
    95             gui.y_RBV = value
     104        axis = gui.axes[pvname]['axis']
     105        field = gui.axes[pvname]['field']
     106       
     107        # can this be more efficient?
     108        if axis == 'x':
     109            if field == 'VAL':
     110                gui.x_VAL = value
     111            elif field == "RBV":
     112                gui.x_RBV = value
     113            elif field == "DMOV":
     114                # set the background color based on moving status
     115                self.setDmovColor(gui, axis, field, value)
     116        elif axis == 'y':
     117            if field == 'VAL':
     118                gui.y_VAL = value
     119            elif field == "RBV":
     120                gui.y_RBV = value
     121            elif field == "DMOV":
     122                self.setDmovColor(gui, axis, field, value)
     123       
     124    def setDmovColor(self, gui, axis, field, value):
     125        '''
     126        set the background color of the VAL and RBV widgets based on DMOV
     127        '''
     128        dmov_spec = gui.axes[axis][field]
     129        for ed_field in ('VAL', 'RBV'):
     130            editor = dmov_spec[ed_field + "_editor"]
     131            #color_old = editor.control.GetBackgroundColour()
     132            if value == 0:  # moving
     133                color_new = gui.pale_green
     134            else:
     135                if ed_field == 'VAL':
     136                    color_new = gui.val_default_color
     137                elif ed_field == 'RBV':
     138                    color_new = gui.rbv_default_color
     139            # This command crashes things!
     140            #editor.control.SetBackgroundColour(color_new)
     141            # TODO: how to set the color?
    96142
    97143
     
    106152    y_VAL = Float
    107153   
     154    color_fmt = "#" + "%02x"*3
     155    pale_green = Color((179, 250, 142))
     156    rbv_default_color = Color((220, 218, 213))
     157    val_default_color = Color((255, 255, 255))
     158    some_default_color = Color((200, 191, 140))
     159    boa_default_color = Color((237, 233, 227))
     160   
     161    trait_color = Color
     162   
     163    axes = {
     164            'x': {
     165                  'name': 'x',
     166                  'VAL': {
     167                          'pvname' : 'prj:m1.VAL',
     168                          'channel' : None,
     169                          'cb_index': None,
     170                          },
     171                  'RBV': {
     172                          'pvname' : 'prj:m1.RBV',
     173                          'channel' : None,
     174                          'cb_index': None,
     175                          },
     176                  'DMOV': {
     177                          'pvname' : 'prj:m1.DMOV',
     178                          'channel' : None,
     179                          'cb_index': None,
     180                          'VAL_editor': None,
     181                          'RBV_editor': None,
     182                          },
     183                  },
     184            'y': {
     185                  'name': 'y',
     186                  'VAL': {
     187                          'pvname' : 'prj:m2.VAL',
     188                          'channel' : None,
     189                          'cb_index': None,
     190                          },
     191                  'RBV': {
     192                          'pvname' : 'prj:m2.RBV',
     193                          'channel' : None,
     194                          'cb_index': None,
     195                          },
     196                  'DMOV': {
     197                          'pvname' : 'prj:m2.DMOV',
     198                          'channel' : None,
     199                          'cb_index': None,
     200                          'VAL_editor': None,
     201                          'RBV_editor': None,
     202                          },
     203                  },
     204            }
     205   
    108206    actionRun = Action(name = "Run",
    109207                       desc = "start monitoring PVs",
     
    117215    monitors = []       # list of active CA monitors
    118216   
     217    vgrid = VGrid(
     218        Label('field'),             Label('X axis'),        Label('Y axis'),
     219        Label('readback (RBV)'),    UReadonly('x_RBV'),     UReadonly('y_RBV'),
     220        Label('target (VAL)'),      UItem('x_VAL'),         UItem('y_VAL'),
     221        columns = 3,
     222        show_border=True,
     223    )
     224   
    119225    view = View(
    120                 VGrid(
    121                         Label('field'),
    122                         Label('X axis'),
    123                         Label('Y axis'),
    124                         Label('readback (RBV)'),
    125                         UReadonly('x_RBV'),
    126                         UReadonly('y_RBV'),
    127                         Label('target (VAL)'),
    128                         UItem('x_VAL'),
    129                         UItem('y_VAL'),
    130                         columns = 3,
    131                         show_border=True,
    132                 ),
     226                vgrid,
    133227                title="moxy prototype GUI",
    134228                width=500,
Note: See TracChangeset for help on using the changeset viewer.