1 |
# Copyright (C) 2001, 2002 by Intevation GmbH |
2 |
# Authors: |
3 |
# Jan-Oliver Wagner <[email protected]> |
4 |
# Bernhard Herzog <[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 |
""" |
10 |
Thuban's application object. |
11 |
""" |
12 |
|
13 |
__version__ = "$Revision$" |
14 |
|
15 |
import sys, os |
16 |
import traceback |
17 |
|
18 |
from wxPython.wx import * |
19 |
|
20 |
from Thuban.Lib.connector import Publisher |
21 |
|
22 |
from Thuban.Model.session import create_empty_session |
23 |
from Thuban.Model.save import save_session |
24 |
from Thuban.Model.load import load_session |
25 |
from Thuban.Model.messages import MAPS_CHANGED |
26 |
|
27 |
import view |
28 |
import tree |
29 |
from interactor import Interactor |
30 |
import mainwindow |
31 |
|
32 |
from messages import SESSION_CHANGED |
33 |
|
34 |
|
35 |
|
36 |
class ThubanApplication(wxApp, Publisher): |
37 |
|
38 |
""" |
39 |
Thuban's application class. |
40 |
|
41 |
All wxWindows programs have to have an instance of an application |
42 |
class derived from wxApp. In Thuban the application class holds |
43 |
references to the main window, the session and the interactor. |
44 |
""" |
45 |
|
46 |
def OnInit(self): |
47 |
self.read_startup_files() |
48 |
self.interactor = Interactor(None) |
49 |
top = self.CreateMainWindow() |
50 |
top.Show(true) |
51 |
self.top = top |
52 |
self.SetTopWindow(top) |
53 |
self.session = None |
54 |
self.create_session() |
55 |
return true |
56 |
|
57 |
def read_startup_files(self): |
58 |
"""Read the startup files.""" |
59 |
# for now the startup file is ~/.thuban/thubanstart.py |
60 |
dir =os.path.expanduser("~/.thuban") |
61 |
if os.path.isdir(dir): |
62 |
sys.path.append(dir) |
63 |
try: |
64 |
import thubanstart |
65 |
except ImportError: |
66 |
tb = sys.exc_info()[2] |
67 |
try: |
68 |
if tb.tb_next is not None: |
69 |
# The ImportError exception was raised from |
70 |
# inside the thubanstart module. |
71 |
sys.stderr.write("Cannot import the thubanstart" |
72 |
"module\n") |
73 |
traceback.print_exc(None, sys.stderr) |
74 |
else: |
75 |
# There's no thubanstart module. |
76 |
sys.stderr.write("No thubanstart module available\n") |
77 |
finally: |
78 |
# make sure we delete the traceback object, |
79 |
# otherwise there's be circular references involving |
80 |
# the current stack frame |
81 |
del tb |
82 |
except: |
83 |
sys.stderr.write("Cannot import the thubanstart module\n") |
84 |
traceback.print_exc(None, sys.stderr) |
85 |
else: |
86 |
# There's no .thuban directory |
87 |
sys.stderr.write("No ~/.thuban directory\n") |
88 |
|
89 |
def CreateMainWindow(self): |
90 |
"""Create and return the main window for the application. |
91 |
|
92 |
Override this in subclasses to instantiate the Thuban mainwindow |
93 |
with different parameters or to use a different class for the |
94 |
main window. |
95 |
|
96 |
when this method is called by OnInit self.interactor (to be used |
97 |
for the interactor argument of the standard Thuban main window |
98 |
class) has already been instantiated. |
99 |
""" |
100 |
msg = ("This is the wxPython-based Graphical User Interface" |
101 |
" for exploring geographic data") |
102 |
return mainwindow.MainWindow(NULL, -1, "Thuban", self, self.interactor, |
103 |
initial_message = msg) |
104 |
|
105 |
def Session(self): |
106 |
"""Return the application's session object""" |
107 |
return self.session |
108 |
|
109 |
def SetSession(self, session): |
110 |
"""Make session the new session. |
111 |
|
112 |
Issue SESSION_CHANGED after self.session has become the new |
113 |
session. After the session has been assigned call |
114 |
self.subscribe_session() with the new session and |
115 |
self.unsubscribe_session with the old one. |
116 |
""" |
117 |
oldsession = self.session |
118 |
self.session = session |
119 |
self.subscribe_session(self.session) |
120 |
self.issue(SESSION_CHANGED) |
121 |
self.interactor.SetSession(session) |
122 |
self.maps_changed() |
123 |
if oldsession is not None: |
124 |
self.unsubscribe_session(oldsession) |
125 |
oldsession.Destroy() |
126 |
|
127 |
def subscribe_session(self, session): |
128 |
"""Subscribe to some of the sessions channels. |
129 |
|
130 |
Extend this method in derived classes if you need additional |
131 |
channels. |
132 |
""" |
133 |
session.Subscribe(MAPS_CHANGED, self.maps_changed) |
134 |
|
135 |
def unsubscribe_session(self, session): |
136 |
"""Unsubscribe from the sessions channels. |
137 |
|
138 |
Extend this method in derived classes if you subscribed to |
139 |
additional channels in subscribe_session(). |
140 |
""" |
141 |
session.Unsubscribe(MAPS_CHANGED, self.maps_changed) |
142 |
|
143 |
def create_session(self): |
144 |
"""Create a default session. |
145 |
|
146 |
Override this method in derived classes to instantiate the |
147 |
session differently or to use a different session class. Don't |
148 |
subscribe to channels here yet. Do that in the |
149 |
subscribe_session() method. |
150 |
""" |
151 |
self.SetSession(create_empty_session()) |
152 |
|
153 |
def OpenSession(self, filename): |
154 |
session = load_session(filename) |
155 |
session.SetFilename(filename) |
156 |
session.UnsetModified() |
157 |
self.SetSession(session) |
158 |
|
159 |
def SaveSession(self): |
160 |
save_session(self.session, self.session.filename) |
161 |
|
162 |
def maps_changed(self, *args): |
163 |
if self.session.HasMaps(): |
164 |
self.top.SetMap(self.session.Maps()[0]) |
165 |
else: |
166 |
self.top.SetMap(None) |