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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 582 - (hide annotations)
Tue Apr 1 10:22:01 2003 UTC (21 years, 11 months ago) by jonathan
Original Path: trunk/thuban/Thuban/Model/session.py
File MIME type: text/x-python
File size: 5755 byte(s)
Use LAYER_CHANGED instead of LAYER_LEGEND_CHANGED.

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