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