removed feed title, refresh, and subscriptions label on top of the main view
[straw.git] / src / lib / MVP.py
blobcabc1f1d0735fc98197b071fa524b535e482f407
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 gettext
22 import pygtk
23 pygtk.require('2.0')
24 from gtk import glade
25 import Event
26 import utils
29 __doc__ = "A MVP framework. Contains base classes for views and presenters."
31 class View(object):
32 """Base class for widget-based view objects."""
33 def __init__(self, widget, model = None, presenter = None):
34 self._widget = widget
35 self._model = model
36 self._presenter = presenter
38 if model is not None:
39 self._model_set()
40 if presenter is not None:
41 self._presenter_set()
43 self._initialize()
44 self._connect_signals(widget)
46 def _connect_signals(self, widget):
47 raise NotImplemented
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."""
53 pass
55 def _model_set(self):
56 """Called when the model is set."""
57 pass
59 def _presenter_set(self):
60 """Called when the presenter is set."""
61 pass
63 def get_model(self):
64 return self._model
66 def set_model(self, model):
67 self._model = model
68 if 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).
72 self._model_set()
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
80 self._presenter_set()
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)
90 nameFuncMap = {}
91 for key in dir(self.__class__):
92 if key[:4] == '_on_':
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):
101 nameFuncMap = {}
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)
112 self._model = model
113 self._view = view
115 if model is not None:
116 self._model_set()
117 if view is not None:
118 view.model = model
119 view.presenter = self
120 self._view_set()
122 self._initialize()
124 def _initialize(self):
125 pass
127 def _model_set(self):
128 pass
130 def _view_set(self):
131 pass
133 def get_view(self):
134 return self._view
136 def set_view(self, view):
137 self._view = view
138 view.presenter = self
139 view.model = self._model
140 self._view_set()
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.""")
146 def get_model(self):
147 return self._model
149 def set_model(self, model):
150 self._model = model
151 if self._view is not None:
152 self._view.model = model
153 self._model_set()
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))