1 |
# Copyright (c) 2001, 2002 by Intevation GmbH |
# Copyright (c) 2001, 2002, 2003 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
4 |
# Jan-Oliver Wagner <[email protected]> |
# Jan-Oliver Wagner <[email protected]> |
8 |
|
|
9 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
10 |
|
|
|
from Thuban.Lib.connector import Publisher |
|
|
|
|
11 |
from messages import MAPS_CHANGED, EXTENSIONS_CHANGED, FILENAME_CHANGED, \ |
from messages import MAPS_CHANGED, EXTENSIONS_CHANGED, FILENAME_CHANGED, \ |
12 |
LAYERS_CHANGED, MAP_PROJECTION_CHANGED, \ |
MAP_LAYERS_CHANGED, MAP_PROJECTION_CHANGED, \ |
13 |
LAYER_LEGEND_CHANGED, LAYER_PROJECTION_CHANGED, LAYER_VISIBILITY_CHANGED, \ |
LAYER_CHANGED, LAYER_PROJECTION_CHANGED, LAYER_VISIBILITY_CHANGED,\ |
14 |
EXTENSION_CHANGED, EXTENSION_OBJECTS_CHANGED |
EXTENSION_CHANGED, EXTENSION_OBJECTS_CHANGED, CHANGED |
15 |
|
|
16 |
from base import TitledObject, Modifiable |
from Thuban import _ |
17 |
|
|
18 |
|
from base import TitledObject, Modifiable |
19 |
from map import Map |
from map import Map |
20 |
|
from data import ShapefileStore |
21 |
|
|
22 |
|
|
23 |
class Session(TitledObject, Modifiable): |
class Session(TitledObject, Modifiable): |
36 |
|
|
37 |
EXTENSIONS_CHANGED -- Extensions were added, removed. |
EXTENSIONS_CHANGED -- Extensions were added, removed. |
38 |
|
|
39 |
LAYERS_CHANGED -- Same as the map's event of the same name. |
MAP_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 |
# message channels that have to be forwarded from maps contained in |
52 |
# the session. |
# the session. |
53 |
forwarded_channels = ( |
forwarded_channels = ( |
54 |
|
# generic channels |
55 |
|
CHANGED, |
56 |
|
|
57 |
# map specific channels |
# map specific channels |
58 |
MAP_PROJECTION_CHANGED, |
MAP_PROJECTION_CHANGED, |
59 |
LAYERS_CHANGED, |
MAP_LAYERS_CHANGED, |
60 |
|
|
61 |
# layer channels forwarded by the map |
# layer channels forwarded by the map |
62 |
LAYER_PROJECTION_CHANGED, |
LAYER_PROJECTION_CHANGED, |
63 |
LAYER_LEGEND_CHANGED, |
LAYER_CHANGED, |
64 |
LAYER_VISIBILITY_CHANGED, |
LAYER_VISIBILITY_CHANGED, |
65 |
|
|
66 |
# channels forwarded by an extension |
# channels forwarded by an extension |
75 |
self.tables = [] |
self.tables = [] |
76 |
self.extensions = [] |
self.extensions = [] |
77 |
|
|
78 |
|
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 |
def SetFilename(self, filename): |
def SetFilename(self, filename): |
89 |
self.filename = filename |
self.filename = filename |
90 |
self.changed(FILENAME_CHANGED) |
self.changed(FILENAME_CHANGED) |
101 |
map.Subscribe(channel, self.forward, channel) |
map.Subscribe(channel, self.forward, channel) |
102 |
self.changed(MAPS_CHANGED) |
self.changed(MAPS_CHANGED) |
103 |
|
|
104 |
|
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 |
def Extensions(self): |
def Extensions(self): |
112 |
return self.extensions |
return self.extensions |
113 |
|
|
120 |
extension.Subscribe(channel, self.forward, channel) |
extension.Subscribe(channel, self.forward, channel) |
121 |
self.changed(EXTENSIONS_CHANGED) |
self.changed(EXTENSIONS_CHANGED) |
122 |
|
|
123 |
|
def OpenShapefile(self, filename): |
124 |
|
"""Return a shapefile store object for the data in the given file""" |
125 |
|
return ShapefileStore(self, filename) |
126 |
|
|
127 |
def Destroy(self): |
def Destroy(self): |
128 |
for map in self.maps: |
for map in self.maps: |
129 |
map.Destroy() |
map.Destroy() |
130 |
self.maps = [] |
self.maps = [] |
131 |
self.tables = [] |
self.tables = [] |
132 |
Publisher.Destroy(self) |
Modifiable.Destroy(self) |
133 |
|
|
134 |
def forward(self, *args): |
def forward(self, *args): |
135 |
"""Reissue events""" |
"""Reissue events. |
136 |
|
|
137 |
|
If the channel the event is forwarded to is a changed-channel |
138 |
|
that is not the CHANGED channel issue CHANGED as well. An |
139 |
|
channel is considered to be a changed-channel if it's name ends |
140 |
|
with 'CHANGED'. |
141 |
|
""" |
142 |
if len(args) > 1: |
if len(args) > 1: |
143 |
args = (args[-1],) + args[:-1] |
args = (args[-1],) + args[:-1] |
144 |
apply(self.issue, args) |
apply(self.issue, args) |
145 |
|
channel = args[0] |
146 |
|
# It's a bit of a kludge to rely on the channel name for this. |
147 |
|
if channel.endswith("CHANGED") and channel != CHANGED: |
148 |
|
self.issue(CHANGED, self) |
149 |
|
|
150 |
def WasModified(self): |
def WasModified(self): |
151 |
"""Return true if the session or one of the maps was modified""" |
"""Return true if the session or one of the maps was modified""" |
166 |
def TreeInfo(self): |
def TreeInfo(self): |
167 |
items = [] |
items = [] |
168 |
if self.filename is None: |
if self.filename is None: |
169 |
items.append("Filename:") |
items.append(_("Filename:")) |
170 |
else: |
else: |
171 |
items.append("Filename: %s" % self.filename) |
items.append(_("Filename: %s") % self.filename) |
172 |
|
|
173 |
if self.WasModified(): |
if self.WasModified(): |
174 |
items.append("Modified") |
items.append(_("Modified")) |
175 |
else: |
else: |
176 |
items.append("Unmodified") |
items.append(_("Unmodified")) |
177 |
|
|
178 |
items.extend(self.maps) |
items.extend(self.maps) |
179 |
items.extend(self.extensions) |
items.extend(self.extensions) |
180 |
|
|
181 |
return ("Session: %s" % self.title, items) |
return (_("Session: %s") % self.title, items) |
182 |
|
|
183 |
|
|
184 |
def create_empty_session(): |
def create_empty_session(): |
185 |
"""Return an empty session useful as a starting point""" |
"""Return an empty session useful as a starting point""" |
186 |
import os |
import os |
187 |
session = Session('unnamed session') |
session = Session(_('unnamed session')) |
188 |
session.SetFilename(None) |
session.SetFilename(None) |
189 |
session.AddMap(Map('unnamed map')) |
session.AddMap(Map(_('unnamed map'))) |
190 |
session.UnsetModified() |
session.UnsetModified() |
191 |
return session |
return session |