Added debug logging around document view creation.
[straw.git] / straw / MVP.py
blobf63c4c90476502ae5d0ae6b9c4a5508655927848
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
26 __doc__ = "A MVP framework. Contains base classes for views and presenters."
28 class View(object):
29 """Base class for widget-based view objects."""
30 def __init__(self, widget, model = None, presenter = None):
31 self._widget = widget
32 self._model = model
33 self._presenter = presenter
35 if model is not None:
36 self._model_set()
37 if presenter is not None:
38 self._presenter_set()
40 self._initialize()
41 self._connect_signals(widget)
43 def _connect_signals(self, widget):
44 raise NotImplemented
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."""
50 pass
52 def _model_set(self):
53 """Called when the model is set."""
54 pass
56 def _presenter_set(self):
57 """Called when the presenter is set."""
58 pass
60 def get_model(self):
61 return self._model
63 def set_model(self, model):
64 self._model = model
65 if 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).
69 self._model_set()
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
77 self._presenter_set()
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)
87 nameFuncMap = {}
88 for key in dir(self.__class__):
89 if key[:4] == '_on_':
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):
98 nameFuncMap = {}
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):
108 self._model = model
109 self._view = view
111 if model is not None:
112 self._model_set()
113 if view is not None:
114 view.model = model
115 view.presenter = self
116 self._view_set()
118 self._initialize()
120 def _initialize(self):
121 pass
123 def _model_set(self):
124 pass
126 def _view_set(self):
127 pass
129 def get_view(self):
130 return self._view
132 def set_view(self, view):
133 self._view = view
134 view.presenter = self
135 view.model = self._model
136 self._view_set()
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.""")
142 def get_model(self):
143 return self._model
145 def set_model(self, model):
146 self._model = model
147 if self._view is not None:
148 self._view.model = model
149 self._model_set()
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.""")