1 |
# Copyright (C) 2001, 2002, 2003 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 os.path |
17 |
from tempfile import mktemp |
18 |
|
19 |
import traceback |
20 |
|
21 |
from wxPython.wx import * |
22 |
|
23 |
from Thuban.Lib.connector import Publisher |
24 |
|
25 |
from Thuban import _ |
26 |
from Thuban.Model.session import create_empty_session |
27 |
from Thuban.Model.save import save_session |
28 |
from Thuban.Model.load import load_session |
29 |
from Thuban.Model.messages import MAPS_CHANGED |
30 |
|
31 |
import view |
32 |
import tree |
33 |
import mainwindow |
34 |
|
35 |
from messages import SESSION_REPLACED |
36 |
|
37 |
|
38 |
|
39 |
class ThubanApplication(wxApp, Publisher): |
40 |
|
41 |
""" |
42 |
Thuban's application class. |
43 |
|
44 |
All wxWindows programs have to have an instance of an application |
45 |
class derived from wxApp. In Thuban the application class holds |
46 |
references to the main window and the session. |
47 |
""" |
48 |
|
49 |
def OnInit(self): |
50 |
self.splash = self.splash_screen() |
51 |
if self.splash is not None: |
52 |
self.splash.Show() |
53 |
self.read_startup_files() |
54 |
self.top = self.CreateMainWindow() |
55 |
self.SetTopWindow(self.top) |
56 |
if self.splash is None: |
57 |
self.ShowMainWindow() |
58 |
self.session = None |
59 |
self.create_session() |
60 |
return True |
61 |
|
62 |
def OnExit(self): |
63 |
"""Clean up code. |
64 |
|
65 |
Extend this in derived classes if needed. |
66 |
""" |
67 |
self.session.Destroy() |
68 |
self.session = None |
69 |
Publisher.Destroy(self) |
70 |
|
71 |
def read_startup_files(self): |
72 |
"""Read the startup files.""" |
73 |
# for now the startup file is ~/.thuban/thubanstart.py |
74 |
if os.name == 'nt': |
75 |
# This should result in something like the user directory ... |
76 |
guess = os.path.dirname(os.path.dirname(os.path.dirname(mktemp()))) |
77 |
dir = os.path.join(guess, ".thuban") |
78 |
if not os.path.isdir(dir): |
79 |
os.mkdir(dir) |
80 |
else: |
81 |
dir =os.path.expanduser("~/.thuban") |
82 |
if os.path.isdir(dir): |
83 |
sys.path.append(dir) |
84 |
try: |
85 |
import thubanstart |
86 |
except ImportError: |
87 |
tb = sys.exc_info()[2] |
88 |
try: |
89 |
if tb.tb_next is not None: |
90 |
# The ImportError exception was raised from |
91 |
# inside the thubanstart module. |
92 |
sys.stderr.write(_("Cannot import the thubanstart" |
93 |
" module\n")) |
94 |
traceback.print_exc(None, sys.stderr) |
95 |
else: |
96 |
# There's no thubanstart module. |
97 |
sys.stderr.write(_("No thubanstart module available\n")) |
98 |
finally: |
99 |
# make sure we delete the traceback object, |
100 |
# otherwise there's be circular references involving |
101 |
# the current stack frame |
102 |
del tb |
103 |
except: |
104 |
sys.stderr.write(_("Cannot import the thubanstart module\n")) |
105 |
traceback.print_exc(None, sys.stderr) |
106 |
else: |
107 |
# There's no .thuban directory |
108 |
sys.stderr.write(_("No ~/.thuban directory\n")) |
109 |
|
110 |
def splash_screen(self): |
111 |
"""Create and return a splash screen. |
112 |
|
113 |
This method is called by OnInit to determine whether the |
114 |
application should have a splashscreen. If the application |
115 |
should display a splash screen override this method in a derived |
116 |
class and have it create and return the wxSplashScreen instance. |
117 |
The implementation of this method in the derived class should |
118 |
also arranged for ShowMainWindow to be called. |
119 |
|
120 |
The default implementation simply returns None so that no splash |
121 |
screen is shown and ShowMainWindow will be called automatically. |
122 |
""" |
123 |
return None |
124 |
|
125 |
def ShowMainWindow(self): |
126 |
"""Show the main window |
127 |
|
128 |
Normally this method is automatically called by OnInit to show |
129 |
the main window. However, if the splash_screen method has |
130 |
returned a splashscreen it is expected that the derived class |
131 |
also arranges for ShowMainWindow to be called at the appropriate |
132 |
time. |
133 |
""" |
134 |
self.top.Show(True) |
135 |
|
136 |
def CreateMainWindow(self): |
137 |
"""Create and return the main window for the application. |
138 |
|
139 |
Override this in subclasses to instantiate the Thuban mainwindow |
140 |
with different parameters or to use a different class for the |
141 |
main window. |
142 |
""" |
143 |
msg = (_("This is the wxPython-based Graphical User Interface" |
144 |
" for exploring geographic data")) |
145 |
return mainwindow.MainWindow(NULL, -1, "Thuban", self, None, |
146 |
initial_message = msg, |
147 |
size = (600, 400)) |
148 |
|
149 |
def Session(self): |
150 |
"""Return the application's session object""" |
151 |
return self.session |
152 |
|
153 |
def SetSession(self, session): |
154 |
"""Make session the new session. |
155 |
|
156 |
Issue SESSION_REPLACED after self.session has become the new |
157 |
session. After the session has been assigned call |
158 |
self.subscribe_session() with the new session and |
159 |
self.unsubscribe_session with the old one. |
160 |
""" |
161 |
oldsession = self.session |
162 |
self.session = session |
163 |
self.subscribe_session(self.session) |
164 |
self.issue(SESSION_REPLACED) |
165 |
self.maps_changed() |
166 |
if oldsession is not None: |
167 |
self.unsubscribe_session(oldsession) |
168 |
oldsession.Destroy() |
169 |
|
170 |
def subscribe_session(self, session): |
171 |
"""Subscribe to some of the sessions channels. |
172 |
|
173 |
Extend this method in derived classes if you need additional |
174 |
channels. |
175 |
""" |
176 |
session.Subscribe(MAPS_CHANGED, self.maps_changed) |
177 |
|
178 |
def unsubscribe_session(self, session): |
179 |
"""Unsubscribe from the sessions channels. |
180 |
|
181 |
Extend this method in derived classes if you subscribed to |
182 |
additional channels in subscribe_session(). |
183 |
""" |
184 |
session.Unsubscribe(MAPS_CHANGED, self.maps_changed) |
185 |
|
186 |
def create_session(self): |
187 |
"""Create a default session. |
188 |
|
189 |
Override this method in derived classes to instantiate the |
190 |
session differently or to use a different session class. Don't |
191 |
subscribe to channels here yet. Do that in the |
192 |
subscribe_session() method. |
193 |
""" |
194 |
self.SetSession(create_empty_session()) |
195 |
|
196 |
def OpenSession(self, filename): |
197 |
"""Open the session in the file named filename""" |
198 |
# Make sure we deal with an absolute pathname. Otherwise we can |
199 |
# get problems when saving because the saving code expects an |
200 |
# absolute directory name |
201 |
filename = os.path.abspath(filename) |
202 |
session = load_session(filename) |
203 |
session.SetFilename(filename) |
204 |
session.UnsetModified() |
205 |
self.SetSession(session) |
206 |
|
207 |
def SaveSession(self): |
208 |
save_session(self.session, self.session.filename) |
209 |
|
210 |
def maps_changed(self, *args): |
211 |
if self.session.HasMaps(): |
212 |
self.top.SetMap(self.session.Maps()[0]) |
213 |
else: |
214 |
self.top.SetMap(None) |