11 |
from Thuban.Lib.connector import Publisher |
from Thuban.Lib.connector import Publisher |
12 |
|
|
13 |
from messages import MAPS_CHANGED, EXTENSIONS_CHANGED, FILENAME_CHANGED, \ |
from messages import MAPS_CHANGED, EXTENSIONS_CHANGED, FILENAME_CHANGED, \ |
14 |
LAYERS_CHANGED, MAP_PROJECTION_CHANGED, \ |
MAP_LAYERS_CHANGED, MAP_PROJECTION_CHANGED, \ |
15 |
LAYER_LEGEND_CHANGED, LAYER_PROJECTION_CHANGED, LAYER_VISIBILITY_CHANGED, \ |
LAYER_CHANGED, LAYER_PROJECTION_CHANGED, LAYER_VISIBILITY_CHANGED,\ |
16 |
EXTENSION_CHANGED, EXTENSION_OBJECTS_CHANGED |
EXTENSION_CHANGED, EXTENSION_OBJECTS_CHANGED, CHANGED |
17 |
|
|
18 |
|
from Thuban import _ |
19 |
|
|
20 |
from base import TitledObject, Modifiable |
from base import TitledObject, Modifiable |
21 |
|
|
38 |
|
|
39 |
EXTENSIONS_CHANGED -- Extensions were added, removed. |
EXTENSIONS_CHANGED -- Extensions were added, removed. |
40 |
|
|
41 |
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. |
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 |
# message channels that have to be forwarded from maps contained in |
54 |
# the session. |
# the session. |
55 |
forwarded_channels = ( |
forwarded_channels = ( |
56 |
|
# generic channels |
57 |
|
CHANGED, |
58 |
|
|
59 |
# map specific channels |
# map specific channels |
60 |
MAP_PROJECTION_CHANGED, |
MAP_PROJECTION_CHANGED, |
61 |
LAYERS_CHANGED, |
MAP_LAYERS_CHANGED, |
62 |
|
|
63 |
# layer channels forwarded by the map |
# layer channels forwarded by the map |
64 |
LAYER_PROJECTION_CHANGED, |
LAYER_PROJECTION_CHANGED, |
65 |
LAYER_LEGEND_CHANGED, |
LAYER_CHANGED, |
66 |
LAYER_VISIBILITY_CHANGED, |
LAYER_VISIBILITY_CHANGED, |
67 |
|
|
68 |
# channels forwarded by an extension |
# channels forwarded by an extension |
77 |
self.tables = [] |
self.tables = [] |
78 |
self.extensions = [] |
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 |
92 |
self.changed(FILENAME_CHANGED) |
self.changed(FILENAME_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): |
def Extensions(self): |
114 |
return self.extensions |
return self.extensions |
115 |
|
|
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""" |
164 |
def TreeInfo(self): |
def TreeInfo(self): |
165 |
items = [] |
items = [] |
166 |
if self.filename is None: |
if self.filename is None: |
167 |
items.append("Filename:") |
items.append(_("Filename:")) |
168 |
else: |
else: |
169 |
items.append("Filename: %s" % self.filename) |
items.append(_("Filename: %s") % self.filename) |
170 |
|
|
171 |
if self.WasModified(): |
if self.WasModified(): |
172 |
items.append("Modified") |
items.append(_("Modified")) |
173 |
else: |
else: |
174 |
items.append("Unmodified") |
items.append(_("Unmodified")) |
175 |
|
|
176 |
items.extend(self.maps) |
items.extend(self.maps) |
177 |
items.extend(self.extensions) |
items.extend(self.extensions) |
178 |
|
|
179 |
return ("Session: %s" % self.title, items) |
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 |
import os |
import os |
185 |
session = Session('unnamed session') |
session = Session(_('unnamed session')) |
186 |
session.SetFilename(None) |
session.SetFilename(None) |
187 |
session.AddMap(Map('unnamed map')) |
session.AddMap(Map(_('unnamed map'))) |
188 |
session.UnsetModified() |
session.UnsetModified() |
189 |
return session |
return session |