1 |
# Copyright (c) 2001 by Intevation GmbH |
# Copyright (c) 2001, 2002 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
|
# Jan-Oliver Wagner <[email protected]> |
5 |
# |
# |
6 |
# This program is free software under the GPL (>=v2) |
# This program is free software under the GPL (>=v2) |
7 |
# Read the file COPYING coming with Thuban for details. |
# Read the file COPYING coming with Thuban for details. |
10 |
|
|
11 |
from Thuban.Lib.connector import Publisher |
from Thuban.Lib.connector import Publisher |
12 |
|
|
13 |
from messages import MAPS_CHANGED, LAYERS_CHANGED, MAP_PROJECTION_CHANGED, \ |
from messages import MAPS_CHANGED, EXTENSIONS_CHANGED, FILENAME_CHANGED, \ |
14 |
LAYER_LEGEND_CHANGED, FILENAME_CHANGED |
MAP_LAYERS_CHANGED, MAP_PROJECTION_CHANGED, \ |
15 |
|
LAYER_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 |
from base import TitledObject, Modifiable |
21 |
|
|
26 |
|
|
27 |
"""A complete session. |
"""A complete session. |
28 |
|
|
29 |
A Session consists of arbitrary numbers of maps and tables |
A Session consists of arbitrary numbers of maps, tables and extensions |
30 |
|
|
31 |
Session objects send the following events: |
Session objects send the following events: |
32 |
|
|
36 |
|
|
37 |
MAPS_CHANGED -- Maps were added, removed. |
MAPS_CHANGED -- Maps were added, removed. |
38 |
|
|
39 |
LAYERS_CHANGED -- Same as the map's event of the same name. |
EXTENSIONS_CHANGED -- Extensions were added, removed. |
40 |
|
|
41 |
|
MAP_LAYERS_CHANGED -- Same as the map's event of the same name. |
42 |
It's simply resent from the session to make |
It's simply resent from the session to make |
43 |
subscriptions easier. |
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 |
|
MAP_LAYERS_CHANGED, |
62 |
|
|
63 |
|
# layer channels forwarded by the map |
64 |
|
LAYER_PROJECTION_CHANGED, |
65 |
|
LAYER_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): |
def __init__(self, title): |
73 |
TitledObject.__init__(self, title) |
TitledObject.__init__(self, title) |
74 |
Modifiable.__init__(self) |
Modifiable.__init__(self) |
75 |
self.filename = None |
self.filename = None |
76 |
self.maps = [] |
self.maps = [] |
77 |
self.tables = [] |
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): |
def SetFilename(self, filename): |
91 |
self.filename = filename |
self.filename = filename |
99 |
|
|
100 |
def AddMap(self, map): |
def AddMap(self, map): |
101 |
self.maps.append(map) |
self.maps.append(map) |
102 |
for channel in (LAYERS_CHANGED, MAP_PROJECTION_CHANGED, |
for channel in self.forwarded_channels: |
|
LAYER_LEGEND_CHANGED): |
|
103 |
map.Subscribe(channel, self.forward, channel) |
map.Subscribe(channel, self.forward, channel) |
104 |
self.changed(MAPS_CHANGED) |
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): |
def Destroy(self): |
126 |
for map in self.maps: |
for map in self.maps: |
127 |
map.Destroy() |
map.Destroy() |
128 |
self.maps = [] |
self.maps = [] |
129 |
self.tables = [] |
self.tables = [] |
130 |
Publisher.Destroy(self) |
Modifiable.Destroy(self) |
131 |
|
|
132 |
def forward(self, *args): |
def forward(self, *args): |
133 |
"""Reissue events""" |
"""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: |
if len(args) > 1: |
141 |
args = (args[-1],) + args[:-1] |
args = (args[-1],) + args[:-1] |
142 |
apply(self.issue, args) |
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): |
def WasModified(self): |
149 |
"""Return true if the session or one of the maps was modified""" |
"""Return true if the session or one of the maps was modified""" |
161 |
for map in self.maps: |
for map in self.maps: |
162 |
map.UnsetModified() |
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(): |
def create_empty_session(): |
183 |
"""Return an empty session useful as a starting point""" |
"""Return an empty session useful as a starting point""" |
184 |
session = Session('unnamed session') |
import os |
185 |
session.SetFilename('unnamed.session') |
session = Session(_('unnamed session')) |
186 |
session.AddMap(Map('unnamed map')) |
session.SetFilename(None) |
187 |
|
session.AddMap(Map(_('unnamed map'))) |
188 |
session.UnsetModified() |
session.UnsetModified() |
189 |
return session |
return session |