next up previous contents
Next: Support for adapter instantiation Up: Module adapters Previous: Class UserClassAdapter   Contents

Class StaticContainerAdapter

This class can be used to bound a set of widgets to a property that is a container, like a tuple, a list or a map, or in general a class that implements __getitem__ and __setitem__ methods.

From the other hand, the set of widgets can be a list provided by the user, or a container widget like a Box, a Notebook, etc. Widgets will be linked by their position when the property is list-like, or by their names or instances when the property is map-like.

This class supports only properties that are static containers, i.e. those containers that do not change their length dynamically. If the container grows up in length, no change will occur in the view-side.

This class derives from class UserClassAdapter.

Widget connection
Different than Adapter's method, connect_widget accepts sets.

def connect_widget(self, widget,
                   getters=None, setters=None, 
                   signals=None, arg=None)

widget
is either a container widget, or a list of widgets.
getters
optional function or list or a map of functions used to ``read'' the widget(s). Each function receives a widget instance.
setters
optional function or list or a map of functions used to ``write'' the widget(s). Each function receives a widget instance and value for setting.

signal
can be None, a signal name, or a list or a map of signal names.

arg
Optional argument that is passed to each signal handler. It will be used when connecting the signal(s).

When maps are used, keys can be widgets or widget names. The length of the possible lists or maps must be lesser or equal to the number of widgets that will be connected.

update_model(idx=None)
Updates the value of property at given index. If idx is None, all controlled indices will be updated. This method should be called directly by the user in very unusual conditions.

update_widget(idx=None)
Forces the widget at given index to be updated from the property value. If index is not given, all controlled widgets will be updated. This method should be called directly by the user when the property is not observable, or in very unusual conditions.

Since things got a bit convoluted here, some examples can help to understand how this kind of adapter can be used.

Suppose you have a glade file containing a button and a HBox called "hbox" containing a text entry, a label and a SpinButton.

Figure: StaticContainerAdapter at work
Image adap2

The view is simply:

class MyView (View):
    def __init__(self, ctrl):
        View.__init__(self, ctrl, "adapters.glade", "window")
        return
    pass # end of class

The model contains a tuple of three integers that we want to connect to the widgets into the HBox. When the button is clicked, one of the three integers is randomly incremented.

class MyModel (Model):
    __properties__ = {
       'box' : [0,1,2]
       }
    pass # end of class

The controller handles the button click signal:

import random
class MyCtrl (Controller):
    def on_button_clicked(self, button):
        self.model.box[random.randint(0,2)] += 1
        return
    pass # end of class

If typically construction of adapters occurs into method register_adapters for the sake of simplicity in this example instantiation of the adapter is located in the main launching code:

m = MyModel()
c = MyCtrl(m)
v = MyView(c)

a = StaticContainerAdapter(m, "box")
a.connect_widget(v["hbox"])

gtk.main()

Adaption of widgets occur by their position into the "hbox" container.

Second example makes use of an explicit list of widgets, and exploits also parameter setters to customize the way the label "lbl" shows its value.

m = MyModel()
c = MyCtrl(m)
v = MyView(c)

a1 = StaticContainerAdapter(m, "box")
a1.connect_widget(map(lambda x: v[x], "en lbl sb".split()), 
                  setters = {'lbl': lambda w, v: 
                     w.set_markup("<big>Val: <b>%d</b></big>" % v)})

gtk.main()

Figure 11: Customized setter for the label
Image adap3

Finally, instead of being a tuple, the observable property can be also a map, whose keys are widget names.

class MyModel (Model):
    __properties__ = {
        'box' : { 'en'  : 0,
                  'lbl' : 1,
                  'sb'  : 2 }
        }
    pass # end of class

in this case bounding between widgets and values into the property in the model is carried out by looking at names, and not position.


next up previous contents
Next: Support for adapter instantiation Up: Module adapters Previous: Class UserClassAdapter   Contents
Roberto Cavada 2008-08-26