next up previous
Next: About this document ... Up: Implementation Previous: Model

Controller

Class ExampleController contains the logic of the application. The controller handles two signals and the observable property notification. Signals are the destroy event, invoked when the application quits, and the on_button1_clicked, fired when button1 is pressed.

 
from gtkmvc.controller import Controller
from gtk import mainquit

class ExampleController(Controller):
    """The only one controller. Handles the button clicked signal, and
    notifications about one observable property."""

    def __init__(self, model):
        """Contructor. model will be accessible via the member 'self.model'.
        Registration is also performed."""
        Controller.__init__(self, model)
        return

    def registerView(self, view):
        Controller.registerView(self, view) # Calls the overridden method

        # Connects the exiting signal:
        view.get_top_widget().connect("destroy", mainquit)
        return

    # Signal
    def on_button1_clicked(self, button):
        """Handles the signal clicked for button1. Changes the model."""
        self.model.set_next_message()
        return

    # Observables notifications:
    def property_message_index_change_notification(self, model, old, new):
        """The model is changed and the view must be updated"""
        msg = self.model.get_message(new)
        
        self.view['label_text'].set_text(msg)
        self.view['label_text_len'].set_text(str(len(msg)))
        return    
    pass # end of class

The destroy signal is connected when the View registers itself inside the controller, by using the method override of registerView. Method on_button1_clicked calls a method inside the model which changes a part of the state inside the model. Since that part of the state is an observable property, the associated observer (which is the controller itself) is notified of the modification, by calling method property_message_index_change_notification. This method updates the view connected to the controller.



Roberto Cavada 2004-11-16