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 |
17 |
|
18 |
from base import TitledObject, Modifiable |
19 |
|
20 |
from map import Map |
21 |
|
22 |
|
23 |
class Session(TitledObject, Modifiable): |
24 |
|
25 |
"""A complete session. |
26 |
|
27 |
A Session consists of arbitrary numbers of maps, tables and extensions |
28 |
|
29 |
Session objects send the following events: |
30 |
|
31 |
TITLE_CHANGED -- The title has changed. Parameters: the session. |
32 |
|
33 |
FILENAME_CHANGED -- The filename has changed. No parameters. |
34 |
|
35 |
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. |
40 |
It's simply resent from the session to make |
41 |
subscriptions easier. |
42 |
""" |
43 |
|
44 |
# message channels that have to be forwarded from maps contained in |
45 |
# the session. |
46 |
forwarded_channels = ( |
47 |
# map specific channels |
48 |
MAP_PROJECTION_CHANGED, |
49 |
LAYERS_CHANGED, |
50 |
|
51 |
# layer channels forwarded by the map |
52 |
LAYER_PROJECTION_CHANGED, |
53 |
LAYER_LEGEND_CHANGED, |
54 |
LAYER_VISIBILITY_CHANGED, |
55 |
|
56 |
# channels forwarded by an extension |
57 |
EXTENSION_CHANGED, |
58 |
EXTENSION_OBJECTS_CHANGED) |
59 |
|
60 |
def __init__(self, title): |
61 |
TitledObject.__init__(self, title) |
62 |
Modifiable.__init__(self) |
63 |
self.filename = None |
64 |
self.maps = [] |
65 |
self.tables = [] |
66 |
self.extensions = [] |
67 |
|
68 |
def SetFilename(self, filename): |
69 |
self.filename = filename |
70 |
self.changed(FILENAME_CHANGED) |
71 |
|
72 |
def Maps(self): |
73 |
return self.maps |
74 |
|
75 |
def HasMaps(self): |
76 |
return len(self.maps) > 0 |
77 |
|
78 |
def AddMap(self, map): |
79 |
self.maps.append(map) |
80 |
for channel in self.forwarded_channels: |
81 |
map.Subscribe(channel, self.forward, channel) |
82 |
self.changed(MAPS_CHANGED) |
83 |
|
84 |
def Extensions(self): |
85 |
return self.extensions |
86 |
|
87 |
def HasExtensions(self): |
88 |
return len(self.extensions) > 0 |
89 |
|
90 |
def AddExtension(self, extension): |
91 |
self.extensions.append(extension) |
92 |
for channel in self.forwarded_channels: |
93 |
extension.Subscribe(channel, self.forward, channel) |
94 |
self.changed(EXTENSIONS_CHANGED) |
95 |
|
96 |
def Destroy(self): |
97 |
for map in self.maps: |
98 |
map.Destroy() |
99 |
self.maps = [] |
100 |
self.tables = [] |
101 |
Publisher.Destroy(self) |
102 |
|
103 |
def forward(self, *args): |
104 |
"""Reissue events""" |
105 |
if len(args) > 1: |
106 |
args = (args[-1],) + args[:-1] |
107 |
apply(self.issue, args) |
108 |
|
109 |
def WasModified(self): |
110 |
"""Return true if the session or one of the maps was modified""" |
111 |
if self.modified: |
112 |
return 1 |
113 |
else: |
114 |
for map in self.maps: |
115 |
if map.WasModified(): |
116 |
return 1 |
117 |
return 0 |
118 |
|
119 |
def UnsetModified(self): |
120 |
"""Unset the modified flag of the session and the maps""" |
121 |
Modifiable.UnsetModified(self) |
122 |
for map in self.maps: |
123 |
map.UnsetModified() |
124 |
|
125 |
def TreeInfo(self): |
126 |
items = [] |
127 |
if self.filename is None: |
128 |
items.append("Filename:") |
129 |
else: |
130 |
items.append("Filename: %s" % self.filename) |
131 |
|
132 |
if self.WasModified(): |
133 |
items.append("Modified") |
134 |
else: |
135 |
items.append("Unmodified") |
136 |
|
137 |
items.extend(self.maps) |
138 |
items.extend(self.extensions) |
139 |
|
140 |
return ("Session: %s" % self.title, items) |
141 |
|
142 |
|
143 |
def create_empty_session(): |
144 |
"""Return an empty session useful as a starting point""" |
145 |
import os |
146 |
session = Session('unnamed session') |
147 |
session.SetFilename(None) |
148 |
session.AddMap(Map('unnamed map')) |
149 |
session.UnsetModified() |
150 |
return session |