1 |
# Copyright (C) 2001, 2002, 2003 by Intevation GmbH |
# Copyright (C) 2001-2005 by Intevation GmbH |
2 |
# Authors: |
# Authors: |
3 |
# Jan-Oliver Wagner <[email protected]> |
# Jan-Oliver Wagner <[email protected]> |
4 |
# Bernhard Herzog <[email protected]> |
# Bernhard Herzog <[email protected]> |
17 |
|
|
18 |
import traceback |
import traceback |
19 |
|
|
20 |
from wxPython.wx import * |
import wx |
|
from wxPython.lib.dialogs import wxScrolledMessageDialog |
|
21 |
|
|
22 |
from Thuban.Lib.connector import Publisher |
from Thuban.Lib.connector import Publisher |
23 |
from Thuban.Lib.fileutil import get_application_dir |
from Thuban.Lib.fileutil import get_application_dir |
30 |
from Thuban.Model.layer import RasterLayer |
from Thuban.Model.layer import RasterLayer |
31 |
import Thuban.Model.resource |
import Thuban.Model.resource |
32 |
|
|
33 |
|
from extensionregistry import ext_registry |
34 |
|
|
35 |
import view |
import view |
36 |
import tree |
import tree |
37 |
import mainwindow |
import mainwindow |
38 |
|
import dbdialog |
39 |
|
import altpathdialog |
40 |
|
import exceptiondialog |
41 |
|
|
42 |
from messages import SESSION_REPLACED |
from messages import SESSION_REPLACED |
43 |
|
|
44 |
|
|
45 |
class ThubanApplication(wxApp, Publisher): |
class ThubanApplication(wx.App, Publisher): |
46 |
|
|
47 |
""" |
""" |
48 |
Thuban's application class. |
Thuban's application class. |
54 |
|
|
55 |
def OnInit(self): |
def OnInit(self): |
56 |
sys.excepthook = self.ShowExceptionDialog |
sys.excepthook = self.ShowExceptionDialog |
57 |
|
|
58 |
|
# Initialize instance variables before trying to create any |
59 |
|
# windows. Creating windows can start an event loop if |
60 |
|
# e.g. message boxes are popped up for some reason, and event |
61 |
|
# handlers, especially EVT_UPDATE_UI may want to access things |
62 |
|
# from the application. |
63 |
|
|
64 |
|
# Defaults for the directories used in file dialogs |
65 |
|
self.path={"data":".", "projection":".", "alt_path":""} |
66 |
|
|
67 |
|
self.session = None |
68 |
|
self.top = None |
69 |
|
self.create_session() |
70 |
|
|
71 |
|
# Create an optional splash screen and then the mainwindow |
72 |
self.splash = self.splash_screen() |
self.splash = self.splash_screen() |
73 |
if self.splash is not None: |
if self.splash is not None: |
74 |
self.splash.Show() |
self.splash.Show() |
75 |
self.read_startup_files() |
self.read_startup_files() |
76 |
|
self.init_extensions() |
77 |
self.top = self.CreateMainWindow() |
self.top = self.CreateMainWindow() |
78 |
|
# The session was alredy created above and we need to get the |
79 |
|
# map into the mainwindow. maps_changed does that. |
80 |
|
self.maps_changed() |
81 |
self.SetTopWindow(self.top) |
self.SetTopWindow(self.top) |
82 |
if self.splash is None: |
if self.splash is None: |
83 |
self.ShowMainWindow() |
self.ShowMainWindow() |
84 |
self.session = None |
|
|
self.create_session() |
|
85 |
return True |
return True |
86 |
|
|
87 |
def OnExit(self): |
def OnExit(self): |
125 |
# There's no .thuban directory |
# There's no .thuban directory |
126 |
sys.stderr.write(_("No ~/.thuban directory\n")) |
sys.stderr.write(_("No ~/.thuban directory\n")) |
127 |
|
|
128 |
|
def init_extensions(self): |
129 |
|
"""Call initialization callbacks for all registered extensions.""" |
130 |
|
for ext in ext_registry: |
131 |
|
ext.init_ext() |
132 |
|
|
133 |
def splash_screen(self): |
def splash_screen(self): |
134 |
"""Create and return a splash screen. |
"""Create and return a splash screen. |
135 |
|
|
165 |
""" |
""" |
166 |
msg = (_("This is the wxPython-based Graphical User Interface" |
msg = (_("This is the wxPython-based Graphical User Interface" |
167 |
" for exploring geographic data")) |
" for exploring geographic data")) |
168 |
return mainwindow.MainWindow(NULL, -1, "Thuban", self, None, |
return mainwindow.MainWindow(None, -1, "Thuban", self, None, |
169 |
initial_message = msg, |
initial_message = msg, |
170 |
size = (600, 400)) |
size = (600, 400)) |
171 |
|
|
190 |
self.unsubscribe_session(oldsession) |
self.unsubscribe_session(oldsession) |
191 |
oldsession.Destroy() |
oldsession.Destroy() |
192 |
|
|
193 |
|
def SetPath(self, group, filename): |
194 |
|
"""Store the application's default path for file dialogs extracted |
195 |
|
from a given filename. |
196 |
|
""" |
197 |
|
self.path[group] = os.path.dirname( filename ) |
198 |
|
|
199 |
|
def Path(self, group): |
200 |
|
"""Return the application's default path for file dialogs.""" |
201 |
|
return self.path[group] |
202 |
|
|
203 |
def subscribe_session(self, session): |
def subscribe_session(self, session): |
204 |
"""Subscribe to some of the sessions channels. |
"""Subscribe to some of the sessions channels. |
205 |
|
|
226 |
""" |
""" |
227 |
self.SetSession(create_empty_session()) |
self.SetSession(create_empty_session()) |
228 |
|
|
229 |
def OpenSession(self, filename, db_connection_callback = None): |
def OpenSession(self, filename, db_connection_callback = None, |
230 |
|
shapefile_callback = None): |
231 |
"""Open the session in the file named filename""" |
"""Open the session in the file named filename""" |
232 |
# Make sure we deal with an absolute pathname. Otherwise we can |
# Make sure we deal with an absolute pathname. Otherwise we can |
233 |
# get problems when saving because the saving code expects an |
# get problems when saving because the saving code expects an |
234 |
# absolute directory name |
# absolute directory name |
235 |
filename = os.path.abspath(filename) |
filename = os.path.abspath(filename) |
236 |
|
if db_connection_callback is None: |
237 |
|
db_connection_callback = self.run_db_param_dialog |
238 |
|
if shapefile_callback is None: |
239 |
|
shapefile_callback = self.run_alt_path_dialog |
240 |
try: |
try: |
241 |
session = load_session(filename, |
session = load_session(filename, |
242 |
db_connection_callback=db_connection_callback) |
db_connection_callback=db_connection_callback, |
243 |
|
shapefile_callback=shapefile_callback) |
244 |
except LoadCancelled: |
except LoadCancelled: |
245 |
return |
return |
246 |
session.SetFilename(filename) |
session.SetFilename(filename) |
254 |
msg = _("The current session contains Image layers,\n" |
msg = _("The current session contains Image layers,\n" |
255 |
"but the GDAL library is not available to " |
"but the GDAL library is not available to " |
256 |
"draw them.") |
"draw them.") |
257 |
dlg = wx.wxMessageDialog(None, |
dlg = wx.MessageDialog(None, |
258 |
msg, |
msg, |
259 |
_("Library not available"), |
_("Library not available"), |
260 |
wx.wxOK | wx.wxICON_INFORMATION) |
wx.OK | wx.ICON_INFORMATION) |
261 |
print msg |
print msg |
262 |
dlg.ShowModal() |
dlg.ShowModal() |
263 |
dlg.Destroy() |
dlg.Destroy() |
264 |
break |
break |
265 |
|
|
266 |
|
def run_db_param_dialog(self, parameters, message): |
267 |
|
"""Implementation of the db_connection_callback for loading sessions""" |
268 |
|
dlg = dbdialog.DBDialog(None, _("DB Connection Parameters"), |
269 |
|
parameters, message) |
270 |
|
return dlg.RunDialog() |
271 |
|
|
272 |
|
# run_alt_path_dialog: Raise a dialog to ask for an alternative path |
273 |
|
# if the shapefile couldn't be found. |
274 |
|
# TODO: |
275 |
|
# - Store a list of already used alternative paths and return these |
276 |
|
# iteratively (using a generator) |
277 |
|
# - How do we interact with the user to tell him we used a different |
278 |
|
# shapefile (location), mode "check"? The current approach with the |
279 |
|
# file dialog is not that comfortable. |
280 |
|
# |
281 |
|
def run_alt_path_dialog(self, filename, mode = None, second_try = 0): |
282 |
|
"""Implemetation of the shapefile_callback while loading sessions. |
283 |
|
|
284 |
|
This implements two modes: |
285 |
|
- search: Search for an alternative path. If available from a |
286 |
|
list of alrady known paths, else interactivly by file dialog. |
287 |
|
Currently the "second_try" is important since else the user might |
288 |
|
be caught in a loop. |
289 |
|
- check: Ask the user for confirmation, if a path from list has |
290 |
|
been found successful. |
291 |
|
|
292 |
|
Returns: |
293 |
|
- fname: The full path to the (shape) file. |
294 |
|
- from_list: Flags if the path was taken from list or entered |
295 |
|
manually. |
296 |
|
""" |
297 |
|
|
298 |
|
if mode == "search": |
299 |
|
if self.Path('alt_path') == "" or second_try: |
300 |
|
dlg = altpathdialog.AltPathFileDialog(filename) |
301 |
|
fname = dlg.RunDialog() |
302 |
|
if fname is not None: |
303 |
|
self.SetPath('alt_path', fname) |
304 |
|
from_list = 0 |
305 |
|
else: |
306 |
|
fname = os.path.join(self.Path('alt_path'), |
307 |
|
os.path.basename(filename)) |
308 |
|
from_list = 1 |
309 |
|
elif mode == "check": |
310 |
|
dlg = altpathdialog.AltPathConfirmDialog(filename) |
311 |
|
fname = dlg.RunDialog() |
312 |
|
if fname is not None: |
313 |
|
self.SetPath('alt_path', fname) |
314 |
|
from_list = 0 |
315 |
|
else: |
316 |
|
fname = None |
317 |
|
from_list = 0 |
318 |
|
return fname, from_list |
319 |
|
|
320 |
|
|
321 |
def SaveSession(self): |
def SaveSession(self): |
322 |
save_session(self.session, self.session.filename) |
save_session(self.session, self.session.filename) |
323 |
|
|
324 |
def maps_changed(self, *args): |
def maps_changed(self, *args): |
325 |
if self.session.HasMaps(): |
"""Subscribed to the session's MAPS_CHANGED messages. |
326 |
self.top.SetMap(self.session.Maps()[0]) |
|
327 |
else: |
Set the toplevel window's map to the map in the session. This is |
328 |
self.top.SetMap(None) |
done by calling the window's SetMap method with the map as |
329 |
|
argument. If the session doesn't have any maps None is used |
330 |
|
instead. |
331 |
|
|
332 |
|
Currently Thuban can only really handle at most one map in a |
333 |
|
sessions so the first map in the session's list of maps as |
334 |
|
returned by the Maps method is used. |
335 |
|
""" |
336 |
|
# The mainwindow may not have been created yet, so check whether |
337 |
|
# it has been created before calling any of its methods |
338 |
|
if self.top is not None: |
339 |
|
if self.session.HasMaps(): |
340 |
|
self.top.SetMap(self.session.Maps()[0]) |
341 |
|
else: |
342 |
|
self.top.SetMap(None) |
343 |
|
|
344 |
in_exception_dialog = 0 # flag: are we already inside the exception dialog? |
in_exception_dialog = 0 # flag: are we already inside the exception dialog? |
345 |
|
|
355 |
if self.in_exception_dialog: |
if self.in_exception_dialog: |
356 |
return |
return |
357 |
self.in_exception_dialog = 1 |
self.in_exception_dialog = 1 |
358 |
while wxIsBusy(): |
while wx.IsBusy(): |
359 |
wxEndBusyCursor() # reset the mouse cursor |
wx.EndBusyCursor() # reset the mouse cursor |
360 |
|
|
361 |
try: |
try: |
362 |
lines = traceback.format_exception(exc_type, exc_value, |
lines = traceback.format_exception(exc_type, exc_value, |
363 |
exc_traceback) |
exc_traceback) |
364 |
message = "An unhandled exception occurred:\n%s\n" % exc_value+\ |
message = _("An unhandled exception occurred:\n%s\n" |
365 |
"(please report to " \ |
"(please report to" |
366 |
"http://thuban.intevation.org/bugtracker.html)"\ |
" http://thuban.intevation.org/bugtracker.html)" |
367 |
"\n\n\n"+\ |
"\n\n%s") % (exc_value, "".join(lines)) |
|
"".join(lines) |
|
368 |
print message |
print message |
369 |
|
|
370 |
# We don't use an explicit parent here because this method might |
# We don't use an explicit parent here because this method might |
371 |
# be called in circumstances where the main window doesn't exist |
# be called in circumstances where the main window doesn't exist |
372 |
# anymore. |
# anymore. |
373 |
dlg = wxScrolledMessageDialog(None, message, |
exceptiondialog.run_exception_dialog(None, message) |
|
"Thuban: Internal Error") |
|
|
dlg.ShowModal() |
|
|
dlg.Destroy() |
|
374 |
|
|
375 |
finally: |
finally: |
376 |
self.in_exception_dialog = 0 |
self.in_exception_dialog = 0 |