### Navigation

- index
- modules |
- next |
- previous |
- GTK MVC 1.99.1 documentation »

# Adapters

class `gtkmvc.adapters.``Adapter`

class `gtkmvc.adapters.``UserClassAdapter`

class `gtkmvc.adapters.``RoUserClassAdapter`

class `gtkmvc.adapters.``StaticContainerAdapter`

  These are shortcuts to classes from modules in this package.

## Overwriting properties

class `gtkmvc.adapters.basic.``Adapter`(model, prop_name, prop_read=None, prop_write=None, value_error=None, spurious=False, prop_cast=True)

  Bases: `gtkmvc.observer.Observer`

  Creates a new adapter that handles setting of value of a model single model property when a corresponding widgets set is changed and viceversa when the property is also observable.

  This class handles only assignments to properties. For other kinds of setting (e.g. user-defined classes used as observable properties, containers, etc.) use other types of Adapters derived from this class.

  prop_name is the model’s property name (as a string). It is possible to use a dotted notation to identify a property contained into a hierarchy of models. For example ‘a.b.c’ identifies property ‘c’ into model ‘b’ inside model ‘a’, where model ‘a’ is an attribute of given top level model. Last name must be an observable or non-observable attribute, and previous names (if specified) must all refer to instances of class Model. First name from the left must be the name of a model instance inside the given model.

  prop_{write,read} are two optional functions that apply custom modifications to the value of the property before setting and reading it. Both take a value and must return a transformed value whose type must be compatible with the type of the property.

  value_error can be a function (or a method) to be called when a ValueError exception occurs while trying to set a wrong value for the property inside the model. The function will receive: the adapter, the property name and the value coming from the widget that offended the model.

  spurious controls if the adapter should receive spurious changes from the model (see spuriousness in class Observer for further information).

  `connect_widget`(wid, getter=None, setter=None, signal=None, arg=None, update=True)

    Called when the widget is instantiated, and the adapter is ready to connect the widget and the property inside the observed model. arg is the (optional) argument that will be passed when connecting the signal.

    getter and setter are the (optional) methods used for reading and writing the widget’s value. When not specified, default getter and setter will be guessed by looking at the widget type the adapter will be connected with. Guessing is carried out by querying information specified into module ‘adapters.default’.

    Finally, if update is false, the widget will not be updated

  `get_property_name`()

    Returns the property the adapter is conected to

  `get_widget`()

    Returns the widget the adapter is conected to

  `update_model`()

    Forces the property to be updated from the value hold by the widget. This method should be called directly by the user in very unusual conditions.

  `update_widget`()

    Forces the widget to be updated from the property value. This method should be called directly by the user when the property is not observable, or in very unusual conditions.

## Mutating properties

class `gtkmvc.adapters.basic.``UserClassAdapter`(model, prop_name, getter, setter, prop_read=None, prop_write=None, value_error=None, spurious=False)

  Bases: `gtkmvc.adapters.basic.Adapter`

  This class handles the communication between a widget and a class instance (possibly observable) that is a property inside the model. The value to be shown is taken and stored by using a getter and a setter. getter and setter can be: names of user class methods, bound or unbound methods of the user class, or a function that will receive the user class instance and possible arguments whose number depends on whether it is a getter or a setter.

## Modifying immutable properties

class `gtkmvc.adapters.basic.``RoUserClassAdapter`(model, prop_name, getter, setter, prop_read=None, prop_write=None, value_error=None, spurious=False)

  Bases: `gtkmvc.adapters.basic.UserClassAdapter`

  This class is for Read-Only user classes. RO classes are those whose setting methods do not change the instance, but return a new instance that has been changed accordingly to the setters semantics. An example is python datetime class, whose replace method does not change the instance it is invoked on, but returns a new datetime instance.

  This class is likely to be used very rarely.

## Single Adapter, multiple widgets

class `gtkmvc.adapters.containers.``StaticContainerAdapter`(model, prop_name, prop_read=None, prop_write=None, value_error=None, spurious=False)

  Bases: `gtkmvc.adapters.basic.UserClassAdapter`

  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 name 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.

  `connect_widget`(wid, getters=None, setters=None, signals=None, arg=None)

    Called when the widget is instantiated, and the adapter is ready to connect the widgets inside it (if a container) or each widget if wid is a list of widgets. getters and setters can be None, a function or a list or a map of functions. signals can be None, a signal name, or a list or a map of signal names. 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.

## Connecting automatically

`gtkmvc.adapters.default.``add_adapter`(widget_class, signal_name, getter, setter, value_type)

  This function can be used to extend at runtime the set of default adapters. If given widget class which is being added is already in the default set, it will be substituted by the new one until the next removal (see remove_adapter).

`gtkmvc.adapters.default.``remove_adapter`(widget_class)

  Removes the given widget class information from the default set of adapters.

  If widget_class had been previously added by using add_adapter, the added adapter will be removed, restoring possibly previusly existing adapter(s). Notice that this function will remove only one adapter about given wiget_class (the first found in order), even if many are currently stored.

  Returns True if one adapter was removed, False if no adapter was removed.

`gtkmvc.adapters.default.``search_adapter_info`(wid)

  Given a widget returns the default tuple found in __def_adapter

### Navigation

- index
- modules |
- next |
- previous |
- GTK MVC 1.99.1 documentation »