The code here is very similar to the code in Colors
and Color Wells and only differences from that code
will be described.
Need to override
Whereas in the Colors and Color Wells example
Call the parent-class
Save ColorOption in instance.
Register a trigger handler to monitor changes in the
backbone atom list when we're make visible. We ignore
the event argument.
Synchronize with well color.
If no handler is currently registered, register one.
Registration occurs when the
In this case, the trigger name is the same as the name
of the class of objects being monitored, "Atom".
The handler function is
The handler function is always invoked by the trigger
with three arguments:
Note that with a newly opened model, objects will just
appear in both the
The
Check through modified atoms for backbone atoms.
If any of the changed atoms is a backbone atom, call
Check whether a handler is currently registered (i.e., the
handler identifier,
Deregistration requires two arguments: the name of the
trigger and the unique handler identifier returned by
the registration call.
Set the unique handler identifier to
Define the module variable
Define
import os
import Tkinter
import chimera
from chimera.baseDialog import ModelessDialog
from chimera.tkoptions import ColorOption
import ColorWellUI
class ColorWellDialog(ModelessDialog):
title = 'Set Backbone Color'
__init__ to initialize our extra state.
def __init__(self, *args, **kw):
coloropt
was a local variable, here the coloropt variable is stored
in the instance because the trigger handler (which has access
to the instance) needs to update the color well contained in
the ColorOption. A new variable, handlerId, is created to
keep track of whether a handler is currently registered. The
handler is only created when needed. See map and unmap
below. (Note that the instance variables must be set before
calling the base __init__ method since the dialog may be mapped
during initialization, depending on which window system is used.)
self.colorOpt = None
self.handlerId = None
__init__.
apply(ModelessDialog.__init__, (self,) + args, kw)
def fillInUI(self, master):
self.coloropt = ColorOption(master, 0, 'Backbone Color', None, self._setBackboneColor, balloon='Protein backbone color')
self._updateBackboneColor()
def _updateBackboneColor(self):
for m in chimera.openModels.list(modelTypes=[chimera.Molecule]):
for a in m.atoms:
if ColorWellUI.MAINCHAIN.match(a.name):
try:
if a.color != theColor:
self.coloropt.setMultiple()
return
except NameError:
theColor = a.color
try:
self.coloropt.set(theColor)
except NameError:
self.coloropt.set(None)
def _setBackboneColor(self, coloroption):
ColorWellUI.mainchain(coloroption.get())
def map(self, *ignore):
self._updateBackboneColor()
if self.handlerId is None:
chimera.triggers object
is requested to add a handler. Registration requires
three arguments:
_handler, defined below.
And the additional argument is empty (None) -- it could
have been the ColorOption instance (coloropt) but that
is accessible via the instance. The return value from
the registration is a unique handler identifier for
the handler/argument combination. This identifier is
required for deregistering the handler.
created set and not in the modified
set, even though the newly created objects will normally have
various of their default attributes modified by later
code sections.
self.handlerId = chimera.triggers.addHandler('Atom', self._handler, None)
_handler function is the trigger handler invoked when
attributes of Atom instances change.
def _handler(self, trigger, additional, atomChanges):
for a in atomChanges.modified:
_updateBackboneColor to synchronize the well color
with backbone atom colors.
if ColorWellUI.MAINCHAIN.match(a.name):
self._updateBackboneColor()
return
unmap is called when the dialog disappears. We ignore the
event argument.
def unmap(self, *ignore):
handlerId, is not None) and
deregister it if necessary.
if self.handlerId is not None:
chimera.triggers.deleteHandler('Atom', self.handlerId)
None to indicate
that no handler is currently registered.
self.handlerId = None
dialog, which tracks the dialog instance.
It is initialized to None, and is assigned a usable value when the
dialog is created.
dialog = None
showColorWellUI, which is invoked when the Chimera
toolbar button is pressed.
def showColorWellUI():
global dialog
if dialog is not None:
dialog.enter()
return
dialog = ColorWellDialog()
dir, file = os.path.split(__file__)
icon = os.path.join(dir, 'AtomTrigger.tiff')
chimera.tkgui.app.toolbar.add(icon, showColorWellUI, 'Set Main Chain Color', None)