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 Thuban import _ |
19 |
|
20 |
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 |
A Session consists of arbitrary numbers of maps, tables and extensions |
30 |
|
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 |
EXTENSIONS_CHANGED -- Extensions were added, removed. |
40 |
|
41 |
LAYERS_CHANGED -- Same as the map's event of the same name. |
42 |
It's simply resent from the session to make |
43 |
subscriptions easier. |
44 |
|
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 |
""" |
52 |
|
53 |
# message channels that have to be forwarded from maps contained in |
54 |
# the session. |
55 |
forwarded_channels = ( |
56 |
# generic channels |
57 |
CHANGED, |
58 |
|
59 |
# map specific channels |
60 |
MAP_PROJECTION_CHANGED, |
61 |
LAYERS_CHANGED, |
62 |
|
63 |
# layer channels forwarded by the map |
64 |
LAYER_PROJECTION_CHANGED, |
65 |
LAYER_LEGEND_CHANGED, |
66 |
LAYER_VISIBILITY_CHANGED, |
67 |
|
68 |
# channels forwarded by an extension |
69 |
EXTENSION_CHANGED, |
70 |
EXTENSION_OBJECTS_CHANGED) |
71 |
|
72 |
def __init__(self, title): |
73 |
TitledObject.__init__(self, title) |
74 |
Modifiable.__init__(self) |
75 |
self.filename = None |
76 |
self.maps = [] |
77 |
self.tables = [] |
78 |
self.extensions = [] |
79 |
|
80 |
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 |
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 |
for channel in self.forwarded_channels: |
103 |
map.Subscribe(channel, self.forward, channel) |
104 |
self.changed(MAPS_CHANGED) |
105 |
|
106 |
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 |
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 |
def Destroy(self): |
126 |
for map in self.maps: |
127 |
map.Destroy() |
128 |
self.maps = [] |
129 |
self.tables = [] |
130 |
Modifiable.Destroy(self) |
131 |
|
132 |
def forward(self, *args): |
133 |
"""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 |
if len(args) > 1: |
141 |
args = (args[-1],) + args[:-1] |
142 |
apply(self.issue, args) |
143 |
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 |
|
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 |
def TreeInfo(self): |
165 |
items = [] |
166 |
if self.filename is None: |
167 |
items.append(_("Filename:")) |
168 |
else: |
169 |
items.append(_("Filename: %s") % self.filename) |
170 |
|
171 |
if self.WasModified(): |
172 |
items.append(_("Modified")) |
173 |
else: |
174 |
items.append(_("Unmodified")) |
175 |
|
176 |
items.extend(self.maps) |
177 |
items.extend(self.extensions) |
178 |
|
179 |
return (_("Session: %s") % self.title, items) |
180 |
|
181 |
|
182 |
def create_empty_session(): |
183 |
"""Return an empty session useful as a starting point""" |
184 |
import os |
185 |
session = Session(_('unnamed session')) |
186 |
session.SetFilename(None) |
187 |
session.AddMap(Map(_('unnamed map'))) |
188 |
session.UnsetModified() |
189 |
return session |