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. """
29 __doc__
= "A MVP framework. Contains base classes for views and presenters."
32 """Base class for widget-based view objects."""
33 def __init__(self
, widget
, model
= None, presenter
= None):
36 self
._presenter
= presenter
40 if presenter
is not None:
44 self
._connect
_signals
(widget
)
46 def _connect_signals(self
, widget
):
49 def _initialize(self
):
50 """Called from the constructor after the instance variables have
51 been set and the corresponding initialization functions have been
52 called, but before connecting gtk signals."""
56 """Called when the model is set."""
59 def _presenter_set(self
):
60 """Called when the presenter is set."""
66 def set_model(self
, model
):
69 # don't call model_set if model is None. This prevents objects
70 # relying on model_set to fail (or gtk throwing a warning when
71 # model is not a gtk.TreeModel).
73 model
= property(get_model
, set_model
, None, doc
="The model object.")
75 def get_presenter(self
):
76 return self
._presenter
78 def set_presenter(self
, presenter
):
79 self
._presenter
= presenter
81 presenter
= property(get_presenter
, set_presenter
, None,
82 "The presenter object.")
84 class WidgetView(View
):
85 def __init__(self
, widget
, model
=None, presenter
=None):
86 View
.__init
__(self
, widget
, model
, presenter
)
88 def _connect_signals(self
, widget
):
89 xml
= glade
.get_widget_tree(widget
)
91 for key
in dir(self
.__class
__):
93 nameFuncMap
[key
[1:]] = getattr(self
, key
)
94 xml
.signal_autoconnect(nameFuncMap
)
96 class GladeView(View
):
97 def __init__(self
, widget
, model
=None, presenter
=None):
98 View
.__init
__(self
, widget
, model
, presenter
)
100 def _connect_signals(self
, widget
):
102 for key
in dir(self
.__class
__):
103 if key
[:4] == '_on_':
104 nameFuncMap
[key
[1:]] = getattr(self
, key
)
105 widget
.signal_autoconnect(nameFuncMap
)
107 class BasicPresenter(object, Event
.SignalEmitter
):
108 """Base class for basic (non-composite) presenters."""
109 def __init__(self
, model
= None, view
= None):
110 Event
.SignalEmitter
.__init
__(self
)
115 if model
is not None:
119 view
.presenter
= self
124 def _initialize(self
):
127 def _model_set(self
):
136 def set_view(self
, view
):
138 view
.presenter
= self
139 view
.model
= self
._model
142 view
= property(get_view
, set_view
, None,
143 """The view component. Setting this als sets the view
144 object's presenter and model attributes.""")
149 def set_model(self
, model
):
151 if self
._view
is not None:
152 self
._view
.model
= model
155 model
= property(get_model
, set_model
, None,
156 """The model component. Setting this also sets the view
157 object's (if defined) model attribute.""")
160 def get_widget_tree(widget
):
161 gladexml
= glade
.XML(utils
.find_glade_file(), widget
, gettext
.textdomain())
162 return glade
.get_widget_tree(gladexml
.get_widget(widget
))