/[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 318 - (hide annotations)
Fri Sep 13 14:21:12 2002 UTC (22 years, 5 months ago) by bh
Original Path: trunk/thuban/Thuban/Model/session.py
File MIME type: text/x-python
File size: 5714 byte(s)
(Session.forwarded_channels): Forward
the CHANGED channel too.

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 bh 128 LAYERS_CHANGED, MAP_PROJECTION_CHANGED, \
15 bh 232 LAYER_LEGEND_CHANGED, LAYER_PROJECTION_CHANGED, LAYER_VISIBILITY_CHANGED,\
16     EXTENSION_CHANGED, EXTENSION_OBJECTS_CHANGED, CHANGED
17 bh 6
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 jan 197 A Session consists of arbitrary numbers of maps, tables and extensions
28 bh 6
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 jan 197 EXTENSIONS_CHANGED -- Extensions were added, removed.
38    
39 bh 6 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 bh 232
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 bh 6 """
50    
51 bh 128 # message channels that have to be forwarded from maps contained in
52     # the session.
53     forwarded_channels = (
54 bh 318 # generic channels
55     CHANGED,
56    
57 bh 128 # map specific channels
58     MAP_PROJECTION_CHANGED,
59     LAYERS_CHANGED,
60    
61     # layer channels forwarded by the map
62     LAYER_PROJECTION_CHANGED,
63     LAYER_LEGEND_CHANGED,
64 jan 197 LAYER_VISIBILITY_CHANGED,
65 bh 128
66 jan 197 # channels forwarded by an extension
67     EXTENSION_CHANGED,
68     EXTENSION_OBJECTS_CHANGED)
69    
70 bh 6 def __init__(self, title):
71     TitledObject.__init__(self, title)
72     Modifiable.__init__(self)
73     self.filename = None
74     self.maps = []
75     self.tables = []
76 jan 197 self.extensions = []
77 bh 6
78 bh 232 def changed(self, channel = None, *args):
79     """Like the inherited version but issue a CHANGED message as well.
80    
81     The CHANGED message is only issued if channel given is a
82     different channel than CHANGED.
83     """
84     Modifiable.changed(self, channel, *args)
85     if channel != CHANGED:
86     self.issue(CHANGED, self)
87    
88 bh 6 def SetFilename(self, filename):
89     self.filename = filename
90     self.changed(FILENAME_CHANGED)
91    
92     def Maps(self):
93     return self.maps
94    
95     def HasMaps(self):
96     return len(self.maps) > 0
97    
98     def AddMap(self, map):
99     self.maps.append(map)
100 bh 128 for channel in self.forwarded_channels:
101 bh 6 map.Subscribe(channel, self.forward, channel)
102     self.changed(MAPS_CHANGED)
103    
104 bh 241 def RemoveMap(self, map):
105     for channel in self.forwarded_channels:
106     map.Unsubscribe(channel, self.forward, channel)
107     self.maps.remove(map)
108     self.changed(MAPS_CHANGED)
109     map.Destroy()
110    
111 jan 197 def Extensions(self):
112     return self.extensions
113    
114     def HasExtensions(self):
115     return len(self.extensions) > 0
116    
117     def AddExtension(self, extension):
118     self.extensions.append(extension)
119     for channel in self.forwarded_channels:
120     extension.Subscribe(channel, self.forward, channel)
121     self.changed(EXTENSIONS_CHANGED)
122    
123 bh 6 def Destroy(self):
124     for map in self.maps:
125     map.Destroy()
126     self.maps = []
127     self.tables = []
128 bh 250 Modifiable.Destroy(self)
129 bh 6
130     def forward(self, *args):
131 bh 232 """Reissue events.
132    
133     If the channel the event is forwarded to is a changed-channel
134     that is not the CHANGED channel issue CHANGED as well. An
135     channel is considered to be a changed-channel if it's name ends
136     with 'CHANGED'.
137     """
138 bh 6 if len(args) > 1:
139     args = (args[-1],) + args[:-1]
140     apply(self.issue, args)
141 bh 232 channel = args[0]
142     # It's a bit of a kludge to rely on the channel name for this.
143     if channel.endswith("CHANGED") and channel != CHANGED:
144     self.issue(CHANGED, self)
145 bh 6
146     def WasModified(self):
147     """Return true if the session or one of the maps was modified"""
148     if self.modified:
149     return 1
150     else:
151     for map in self.maps:
152     if map.WasModified():
153     return 1
154     return 0
155    
156     def UnsetModified(self):
157     """Unset the modified flag of the session and the maps"""
158     Modifiable.UnsetModified(self)
159     for map in self.maps:
160     map.UnsetModified()
161    
162 bh 217 def TreeInfo(self):
163     items = []
164     if self.filename is None:
165     items.append("Filename:")
166     else:
167     items.append("Filename: %s" % self.filename)
168 bh 6
169 bh 217 if self.WasModified():
170     items.append("Modified")
171     else:
172     items.append("Unmodified")
173 bh 6
174 bh 217 items.extend(self.maps)
175     items.extend(self.extensions)
176    
177     return ("Session: %s" % self.title, items)
178    
179    
180 bh 6 def create_empty_session():
181     """Return an empty session useful as a starting point"""
182 jan 103 import os
183 bh 6 session = Session('unnamed session')
184 jan 103 session.SetFilename(None)
185 bh 6 session.AddMap(Map('unnamed map'))
186 bh 56 session.UnsetModified()
187 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