1 |
bh |
6 |
# Copyright (c) 2001 by Intevation GmbH |
2 |
|
|
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with Thuban for details. |
7 |
|
|
|
8 |
|
|
__version__ = "$Revision$" |
9 |
|
|
|
10 |
|
|
from Thuban.Lib.connector import Publisher |
11 |
|
|
|
12 |
|
|
from messages import MAPS_CHANGED, LAYERS_CHANGED, MAP_PROJECTION_CHANGED, \ |
13 |
|
|
LAYER_LEGEND_CHANGED, FILENAME_CHANGED |
14 |
|
|
|
15 |
|
|
from base import TitledObject, Modifiable |
16 |
|
|
|
17 |
|
|
from map import Map |
18 |
|
|
|
19 |
|
|
|
20 |
|
|
class Session(TitledObject, Modifiable): |
21 |
|
|
|
22 |
|
|
"""A complete session. |
23 |
|
|
|
24 |
|
|
A Session consists of arbitrary numbers of maps and tables |
25 |
|
|
|
26 |
|
|
Session objects send the following events: |
27 |
|
|
|
28 |
|
|
TITLE_CHANGED -- The title has changed. Parameters: the session. |
29 |
|
|
|
30 |
|
|
FILENAME_CHANGED -- The filename has changed. No parameters. |
31 |
|
|
|
32 |
|
|
MAPS_CHANGED -- Maps were added, removed. |
33 |
|
|
|
34 |
|
|
LAYERS_CHANGED -- Same as the map's event of the same name. |
35 |
|
|
It's simply resent from the session to make |
36 |
|
|
subscriptions easier. |
37 |
|
|
""" |
38 |
|
|
|
39 |
|
|
def __init__(self, title): |
40 |
|
|
TitledObject.__init__(self, title) |
41 |
|
|
Modifiable.__init__(self) |
42 |
|
|
self.filename = None |
43 |
|
|
self.maps = [] |
44 |
|
|
self.tables = [] |
45 |
|
|
|
46 |
|
|
def SetFilename(self, filename): |
47 |
|
|
self.filename = filename |
48 |
|
|
self.changed(FILENAME_CHANGED) |
49 |
|
|
|
50 |
|
|
def Maps(self): |
51 |
|
|
return self.maps |
52 |
|
|
|
53 |
|
|
def HasMaps(self): |
54 |
|
|
return len(self.maps) > 0 |
55 |
|
|
|
56 |
|
|
def AddMap(self, map): |
57 |
|
|
self.maps.append(map) |
58 |
|
|
for channel in (LAYERS_CHANGED, MAP_PROJECTION_CHANGED, |
59 |
|
|
LAYER_LEGEND_CHANGED): |
60 |
|
|
map.Subscribe(channel, self.forward, channel) |
61 |
|
|
self.changed(MAPS_CHANGED) |
62 |
|
|
|
63 |
|
|
def Destroy(self): |
64 |
|
|
for map in self.maps: |
65 |
|
|
map.Destroy() |
66 |
|
|
self.maps = [] |
67 |
|
|
self.tables = [] |
68 |
|
|
Publisher.Destroy(self) |
69 |
|
|
|
70 |
|
|
def forward(self, *args): |
71 |
|
|
"""Reissue events""" |
72 |
|
|
if len(args) > 1: |
73 |
|
|
args = (args[-1],) + args[:-1] |
74 |
|
|
apply(self.issue, args) |
75 |
|
|
|
76 |
|
|
def WasModified(self): |
77 |
|
|
"""Return true if the session or one of the maps was modified""" |
78 |
|
|
if self.modified: |
79 |
|
|
return 1 |
80 |
|
|
else: |
81 |
|
|
for map in self.maps: |
82 |
|
|
if map.WasModified(): |
83 |
|
|
return 1 |
84 |
|
|
return 0 |
85 |
|
|
|
86 |
|
|
def UnsetModified(self): |
87 |
|
|
"""Unset the modified flag of the session and the maps""" |
88 |
|
|
Modifiable.UnsetModified(self) |
89 |
|
|
for map in self.maps: |
90 |
|
|
map.UnsetModified() |
91 |
|
|
|
92 |
|
|
|
93 |
|
|
|
94 |
|
|
def create_empty_session(): |
95 |
|
|
"""Return an empty session useful as a starting point""" |
96 |
|
|
session = Session('unnamed session') |
97 |
|
|
session.SetFilename('unnamed.session') |
98 |
|
|
session.AddMap(Map('unnamed map')) |
99 |
bh |
56 |
session.UnsetModified() |
100 |
bh |
6 |
return session |