/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/Model/session.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/Thuban/Model/session.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 250 - (show annotations)
Tue Jul 30 14:17:58 2002 UTC (22 years, 7 months ago) by bh
Original Path: trunk/thuban/Thuban/Model/session.py
File MIME type: text/x-python
File size: 5669 byte(s)
(Session.Destroy): Don't bypass the
direct base class' Destroy method.

1 # Copyright (c) 2001, 2002 by Intevation GmbH
2 # Authors:
3 # Bernhard Herzog <[email protected]>
4 # Jan-Oliver Wagner <[email protected]>
5 #
6 # This program is free software under the GPL (>=v2)
7 # Read the file COPYING coming with Thuban for details.
8
9 __version__ = "$Revision$"
10
11 from Thuban.Lib.connector import Publisher
12
13 from messages import MAPS_CHANGED, EXTENSIONS_CHANGED, FILENAME_CHANGED, \
14 LAYERS_CHANGED, MAP_PROJECTION_CHANGED, \
15 LAYER_LEGEND_CHANGED, LAYER_PROJECTION_CHANGED, LAYER_VISIBILITY_CHANGED,\
16 EXTENSION_CHANGED, EXTENSION_OBJECTS_CHANGED, CHANGED
17
18 from base import TitledObject, Modifiable
19
20 from map import Map
21
22
23 class Session(TitledObject, Modifiable):
24
25 """A complete session.
26
27 A Session consists of arbitrary numbers of maps, tables and extensions
28
29 Session objects send the following events:
30
31 TITLE_CHANGED -- The title has changed. Parameters: the session.
32
33 FILENAME_CHANGED -- The filename has changed. No parameters.
34
35 MAPS_CHANGED -- Maps were added, removed.
36
37 EXTENSIONS_CHANGED -- Extensions were added, removed.
38
39 LAYERS_CHANGED -- Same as the map's event of the same name.
40 It's simply resent from the session to make
41 subscriptions easier.
42
43 CHANGED -- Generic changed event. Parameters: the session. The
44 event is always issued when any other changed event
45 is issused. This is useful for code that needs to be
46 notified whenever something in the session has
47 changed but it's too cumbersome or error-prone to
48 subscribe to all the individual events.
49 """
50
51 # message channels that have to be forwarded from maps contained in
52 # the session.
53 forwarded_channels = (
54 # map specific channels
55 MAP_PROJECTION_CHANGED,
56 LAYERS_CHANGED,
57
58 # layer channels forwarded by the map
59 LAYER_PROJECTION_CHANGED,
60 LAYER_LEGEND_CHANGED,
61 LAYER_VISIBILITY_CHANGED,
62
63 # channels forwarded by an extension
64 EXTENSION_CHANGED,
65 EXTENSION_OBJECTS_CHANGED)
66
67 def __init__(self, title):
68 TitledObject.__init__(self, title)
69 Modifiable.__init__(self)
70 self.filename = None
71 self.maps = []
72 self.tables = []
73 self.extensions = []
74
75 def changed(self, channel = None, *args):
76 """Like the inherited version but issue a CHANGED message as well.
77
78 The CHANGED message is only issued if channel given is a
79 different channel than CHANGED.
80 """
81 Modifiable.changed(self, channel, *args)
82 if channel != CHANGED:
83 self.issue(CHANGED, self)
84
85 def SetFilename(self, filename):
86 self.filename = filename
87 self.changed(FILENAME_CHANGED)
88
89 def Maps(self):
90 return self.maps
91
92 def HasMaps(self):
93 return len(self.maps) > 0
94
95 def AddMap(self, map):
96 self.maps.append(map)
97 for channel in self.forwarded_channels:
98 map.Subscribe(channel, self.forward, channel)
99 self.changed(MAPS_CHANGED)
100
101 def RemoveMap(self, map):
102 for channel in self.forwarded_channels:
103 map.Unsubscribe(channel, self.forward, channel)
104 self.maps.remove(map)
105 self.changed(MAPS_CHANGED)
106 map.Destroy()
107
108 def Extensions(self):
109 return self.extensions
110
111 def HasExtensions(self):
112 return len(self.extensions) > 0
113
114 def AddExtension(self, extension):
115 self.extensions.append(extension)
116 for channel in self.forwarded_channels:
117 extension.Subscribe(channel, self.forward, channel)
118 self.changed(EXTENSIONS_CHANGED)
119
120 def Destroy(self):
121 for map in self.maps:
122 map.Destroy()
123 self.maps = []
124 self.tables = []
125 Modifiable.Destroy(self)
126
127 def forward(self, *args):
128 """Reissue events.
129
130 If the channel the event is forwarded to is a changed-channel
131 that is not the CHANGED channel issue CHANGED as well. An
132 channel is considered to be a changed-channel if it's name ends
133 with 'CHANGED'.
134 """
135 if len(args) > 1:
136 args = (args[-1],) + args[:-1]
137 apply(self.issue, args)
138 channel = args[0]
139 # It's a bit of a kludge to rely on the channel name for this.
140 if channel.endswith("CHANGED") and channel != CHANGED:
141 self.issue(CHANGED, self)
142
143 def WasModified(self):
144 """Return true if the session or one of the maps was modified"""
145 if self.modified:
146 return 1
147 else:
148 for map in self.maps:
149 if map.WasModified():
150 return 1
151 return 0
152
153 def UnsetModified(self):
154 """Unset the modified flag of the session and the maps"""
155 Modifiable.UnsetModified(self)
156 for map in self.maps:
157 map.UnsetModified()
158
159 def TreeInfo(self):
160 items = []
161 if self.filename is None:
162 items.append("Filename:")
163 else:
164 items.append("Filename: %s" % self.filename)
165
166 if self.WasModified():
167 items.append("Modified")
168 else:
169 items.append("Unmodified")
170
171 items.extend(self.maps)
172 items.extend(self.extensions)
173
174 return ("Session: %s" % self.title, items)
175
176
177 def create_empty_session():
178 """Return an empty session useful as a starting point"""
179 import os
180 session = Session('unnamed session')
181 session.SetFilename(None)
182 session.AddMap(Map('unnamed map'))
183 session.UnsetModified()
184 return session

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26