Updated Arabic Translation by Djihed Afifi.
[straw.git] / src / lib / MVP.py
blobcd72ca655900446036183f9aa3a31a313b0c52d1
1 """ MVP.py
3 Module that provides a Model-View-Presenter framework for use with Straw
4 """
5 __copyright__ = "Copyright (c) 2002-2005 Free Software Foundation, Inc."
6 __license__ = """
7 Straw is free software; you can redistribute it and/or modify it under the
8 terms of the GNU General Public License as published by the Free Software
9 Foundation; either version 2 of the License, or (at your option) any later
10 version.
12 Straw is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along with
17 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 Place - Suite 330, Boston, MA 02111-1307, USA. """
21 import pygtk
22 pygtk.require('2.0')
23 from gtk import glade
24 import Event
27 __doc__ = "A MVP framework. Contains base classes for views and presenters."
29 class View(object):
30 """Base class for widget-based view objects."""
31 def __init__(self, widget, model = None, presenter = None):
32 self._widget = widget
33 self._model = model
34 self._presenter = presenter
36 if model is not None:
37 self._model_set()
38 if presenter is not None:
39 self._presenter_set()
41 self._initialize()
42 self._connect_signals(widget)
44 def _connect_signals(self, widget):
45 raise NotImplemented
47 def _initialize(self):
48 """Called from the constructor after the instance variables have
49 been set and the corresponding initialization functions have been
50 called, but before connecting gtk signals."""
51 pass
53 def _model_set(self):
54 """Called when the model is set."""
55 pass
57 def _presenter_set(self):
58 """Called when the presenter is set."""
59 pass
61 def get_model(self):
62 return self._model
64 def set_model(self, model):
65 self._model = model
66 if model:
67 # don't call model_set if model is None. This prevents objects
68 # relying on model_set to fail (or gtk throwing a warning when
69 # model is not a gtk.TreeModel).
70 self._model_set()
71 model = property(get_model, set_model, None, doc="The model object.")
73 def get_presenter(self):
74 return self._presenter
76 def set_presenter(self, presenter):
77 self._presenter = presenter
78 self._presenter_set()
79 presenter = property(get_presenter, set_presenter, None,
80 "The presenter object.")
82 class WidgetView(View):
83 def __init__(self, widget, model=None, presenter=None):
84 View.__init__(self, widget, model, presenter)
86 def _connect_signals(self, widget):
87 xml = glade.get_widget_tree(widget)
88 nameFuncMap = {}
89 for key in dir(self.__class__):
90 if key[:4] == '_on_':
91 nameFuncMap[key[1:]] = getattr(self, key)
92 xml.signal_autoconnect(nameFuncMap)
94 class GladeView(View):
95 def __init__(self, widget, model=None, presenter=None):
96 View.__init__(self, widget, model, presenter)
98 def _connect_signals(self, widget):
99 nameFuncMap = {}
100 for key in dir(self.__class__):
101 if key[:4] == '_on_':
102 nameFuncMap[key[1:]] = getattr(self, key)
103 widget.signal_autoconnect(nameFuncMap)
105 class BasicPresenter(object, Event.SignalEmitter):
106 """Base class for basic (non-composite) presenters."""
107 def __init__(self, model = None, view = None):
108 Event.SignalEmitter.__init__(self)
110 self._model = model
111 self._view = view
113 if model is not None:
114 self._model_set()
115 if view is not None:
116 view.model = model
117 view.presenter = self
118 self._view_set()
120 self._initialize()
122 def _initialize(self):
123 pass
125 def _model_set(self):
126 pass
128 def _view_set(self):
129 pass
131 def get_view(self):
132 return self._view
134 def set_view(self, view):
135 self._view = view
136 view.presenter = self
137 view.model = self._model
138 self._view_set()
140 view = property(get_view, set_view, None,
141 """The view component. Setting this als sets the view
142 object's presenter and model attributes.""")
144 def get_model(self):
145 return self._model
147 def set_model(self, model):
148 self._model = model
149 if self._view is not None:
150 self._view.model = model
151 self._model_set()
153 model = property(get_model, set_model, None,
154 """The model component. Setting this also sets the view
155 object's (if defined) model attribute.""")