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