3 Module that provides a Model-View-Presenter framework for use with Straw
5 __copyright__
= "Copyright (c) 2002-2005 Free Software Foundation, Inc."
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
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. """
26 __doc__
= "A MVP framework. Contains base classes for views and presenters."
29 """Base class for widget-based view objects."""
30 def __init__(self
, widget
, model
= None, presenter
= None):
33 self
._presenter
= presenter
37 if presenter
is not None:
41 self
._connect
_signals
(widget
)
43 def _connect_signals(self
, widget
):
46 def _initialize(self
):
47 """Called from the constructor after the instance variables have
48 been set and the corresponding initialization functions have been
49 called, but before connecting gtk signals."""
53 """Called when the model is set."""
56 def _presenter_set(self
):
57 """Called when the presenter is set."""
63 def set_model(self
, model
):
66 # don't call model_set if model is None. This prevents objects
67 # relying on model_set to fail (or gtk throwing a warning when
68 # model is not a gtk.TreeModel).
70 model
= property(get_model
, set_model
, None, doc
="The model object.")
72 def get_presenter(self
):
73 return self
._presenter
75 def set_presenter(self
, presenter
):
76 self
._presenter
= presenter
78 presenter
= property(get_presenter
, set_presenter
, None,
79 "The presenter object.")
81 class WidgetView(View
):
82 def __init__(self
, widget
, model
=None, presenter
=None):
83 View
.__init
__(self
, widget
, model
, presenter
)
85 def _connect_signals(self
, widget
):
86 xml
= glade
.get_widget_tree(widget
)
88 for key
in dir(self
.__class
__):
90 nameFuncMap
[key
[1:]] = getattr(self
, key
)
91 xml
.signal_autoconnect(nameFuncMap
)
93 class GladeView(View
):
94 def __init__(self
, widget
, model
=None, presenter
=None):
95 View
.__init
__(self
, widget
, model
, presenter
)
97 def _connect_signals(self
, widget
):
99 for key
in dir(self
.__class
__):
100 if key
[:4] == '_on_':
101 nameFuncMap
[key
[1:]] = getattr(self
, key
)
102 widget
.signal_autoconnect(nameFuncMap
)
104 class BasicPresenter(object):
105 """Base class for basic (non-composite) presenters."""
106 def __init__(self
, model
= None, view
= None):
111 if model
is not None:
115 view
.presenter
= self
120 def _initialize(self
):
123 def _model_set(self
):
132 def set_view(self
, view
):
134 view
.presenter
= self
135 view
.model
= self
._model
138 view
= property(get_view
, set_view
, None,
139 """The view component. Setting this also sets the view
140 object's presenter and model attributes.""")
145 def set_model(self
, model
):
147 if self
._view
is not None:
148 self
._view
.model
= model
151 model
= property(get_model
, set_model
, None,
152 """The model component. Setting this also sets the view
153 object's (if defined) model attribute.""")