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 |
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 |
from base import TitledObject, Modifiable |
19 |
|
|
24 |
|
|
25 |
"""A complete session. |
"""A complete session. |
26 |
|
|
27 |
A Session consists of arbitrary numbers of maps and tables |
A Session consists of arbitrary numbers of maps, tables and extensions |
28 |
|
|
29 |
Session objects send the following events: |
Session objects send the following events: |
30 |
|
|
34 |
|
|
35 |
MAPS_CHANGED -- Maps were added, removed. |
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. |
LAYERS_CHANGED -- Same as the map's event of the same name. |
40 |
It's simply resent from the session to make |
It's simply resent from the session to make |
41 |
subscriptions easier. |
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): |
def __init__(self, title): |
68 |
TitledObject.__init__(self, title) |
TitledObject.__init__(self, title) |
69 |
Modifiable.__init__(self) |
Modifiable.__init__(self) |
70 |
self.filename = None |
self.filename = None |
71 |
self.maps = [] |
self.maps = [] |
72 |
self.tables = [] |
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): |
def SetFilename(self, filename): |
86 |
self.filename = filename |
self.filename = filename |
94 |
|
|
95 |
def AddMap(self, map): |
def AddMap(self, map): |
96 |
self.maps.append(map) |
self.maps.append(map) |
97 |
for channel in (LAYERS_CHANGED, MAP_PROJECTION_CHANGED, |
for channel in self.forwarded_channels: |
|
LAYER_LEGEND_CHANGED): |
|
98 |
map.Subscribe(channel, self.forward, channel) |
map.Subscribe(channel, self.forward, channel) |
99 |
self.changed(MAPS_CHANGED) |
self.changed(MAPS_CHANGED) |
100 |
|
|
101 |
|
def Extensions(self): |
102 |
|
return self.extensions |
103 |
|
|
104 |
|
def HasExtensions(self): |
105 |
|
return len(self.extensions) > 0 |
106 |
|
|
107 |
|
def AddExtension(self, extension): |
108 |
|
self.extensions.append(extension) |
109 |
|
for channel in self.forwarded_channels: |
110 |
|
extension.Subscribe(channel, self.forward, channel) |
111 |
|
self.changed(EXTENSIONS_CHANGED) |
112 |
|
|
113 |
def Destroy(self): |
def Destroy(self): |
114 |
for map in self.maps: |
for map in self.maps: |
115 |
map.Destroy() |
map.Destroy() |
118 |
Publisher.Destroy(self) |
Publisher.Destroy(self) |
119 |
|
|
120 |
def forward(self, *args): |
def forward(self, *args): |
121 |
"""Reissue events""" |
"""Reissue events. |
122 |
|
|
123 |
|
If the channel the event is forwarded to is a changed-channel |
124 |
|
that is not the CHANGED channel issue CHANGED as well. An |
125 |
|
channel is considered to be a changed-channel if it's name ends |
126 |
|
with 'CHANGED'. |
127 |
|
""" |
128 |
if len(args) > 1: |
if len(args) > 1: |
129 |
args = (args[-1],) + args[:-1] |
args = (args[-1],) + args[:-1] |
130 |
apply(self.issue, args) |
apply(self.issue, args) |
131 |
|
channel = args[0] |
132 |
|
# It's a bit of a kludge to rely on the channel name for this. |
133 |
|
if channel.endswith("CHANGED") and channel != CHANGED: |
134 |
|
self.issue(CHANGED, self) |
135 |
|
|
136 |
def WasModified(self): |
def WasModified(self): |
137 |
"""Return true if the session or one of the maps was modified""" |
"""Return true if the session or one of the maps was modified""" |
149 |
for map in self.maps: |
for map in self.maps: |
150 |
map.UnsetModified() |
map.UnsetModified() |
151 |
|
|
152 |
|
def TreeInfo(self): |
153 |
|
items = [] |
154 |
|
if self.filename is None: |
155 |
|
items.append("Filename:") |
156 |
|
else: |
157 |
|
items.append("Filename: %s" % self.filename) |
158 |
|
|
159 |
|
if self.WasModified(): |
160 |
|
items.append("Modified") |
161 |
|
else: |
162 |
|
items.append("Unmodified") |
163 |
|
|
164 |
|
items.extend(self.maps) |
165 |
|
items.extend(self.extensions) |
166 |
|
|
167 |
|
return ("Session: %s" % self.title, items) |
168 |
|
|
169 |
|
|
170 |
def create_empty_session(): |
def create_empty_session(): |
171 |
"""Return an empty session useful as a starting point""" |
"""Return an empty session useful as a starting point""" |
172 |
|
import os |
173 |
session = Session('unnamed session') |
session = Session('unnamed session') |
174 |
session.SetFilename('unnamed.session') |
session.SetFilename(None) |
175 |
session.AddMap(Map('unnamed map')) |
session.AddMap(Map('unnamed map')) |
176 |
|
session.UnsetModified() |
177 |
return session |
return session |