19 |
|
|
20 |
from Thuban.Lib.connector import Publisher |
from Thuban.Lib.connector import Publisher |
21 |
|
|
22 |
|
from Thuban import _ |
23 |
from Thuban.Model.session import create_empty_session |
from Thuban.Model.session import create_empty_session |
24 |
from Thuban.Model.save import save_session |
from Thuban.Model.save import save_session |
25 |
from Thuban.Model.load import load_session |
from Thuban.Model.load import load_session |
26 |
|
from Thuban.Model.messages import MAPS_CHANGED |
27 |
|
|
28 |
import view |
import view |
29 |
import tree |
import tree |
47 |
def OnInit(self): |
def OnInit(self): |
48 |
self.read_startup_files() |
self.read_startup_files() |
49 |
self.interactor = Interactor(None) |
self.interactor = Interactor(None) |
50 |
top = mainwindow.MainWindow(NULL, -1, self, self.interactor) |
top = self.CreateMainWindow() |
51 |
top.Show(true) |
top.Show(true) |
52 |
self.top = top |
self.top = top |
53 |
self.SetTopWindow(top) |
self.SetTopWindow(top) |
55 |
self.create_session() |
self.create_session() |
56 |
return true |
return true |
57 |
|
|
58 |
|
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 |
def read_startup_files(self): |
def read_startup_files(self): |
77 |
"""Read the startup files.""" |
"""Read the startup files.""" |
78 |
# for now the startup file is ~/.thuban/thubanstart.py |
# for now the startup file is ~/.thuban/thubanstart.py |
87 |
if tb.tb_next is not None: |
if tb.tb_next is not None: |
88 |
# The ImportError exception was raised from |
# The ImportError exception was raised from |
89 |
# inside the thubanstart module. |
# inside the thubanstart module. |
90 |
sys.stderr.write("Cannot import the thubanstart" |
sys.stderr.write(_("Cannot import the thubanstart" |
91 |
"module\n") |
"module\n")) |
92 |
traceback.print_exc(None, sys.stderr) |
traceback.print_exc(None, sys.stderr) |
93 |
else: |
else: |
94 |
# There's no thubanstart module. |
# There's no thubanstart module. |
95 |
sys.stderr.write("No thubanstart module available\n") |
sys.stderr.write(_("No thubanstart module available\n")) |
96 |
finally: |
finally: |
97 |
# make sure we delete the traceback object, |
# make sure we delete the traceback object, |
98 |
# otherwise there's be circular references involving |
# otherwise there's be circular references involving |
99 |
# the current stack frame |
# the current stack frame |
100 |
del tb |
del tb |
101 |
except: |
except: |
102 |
sys.stderr.write("Cannot import the thubanstart module\n") |
sys.stderr.write(_("Cannot import the thubanstart module\n")) |
103 |
traceback.print_exc(None, sys.stderr) |
traceback.print_exc(None, sys.stderr) |
104 |
else: |
else: |
105 |
# There's no .thuban directory |
# There's no .thuban directory |
106 |
sys.stderr.write("No ~/.thuban directory\n") |
sys.stderr.write(_("No ~/.thuban directory\n")) |
107 |
|
|
108 |
|
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 |
|
msg = (_("This is the wxPython-based Graphical User Interface" |
120 |
|
" for exploring geographic data")) |
121 |
|
return mainwindow.MainWindow(NULL, -1, "Thuban", self, self.interactor, |
122 |
|
initial_message = msg) |
123 |
|
|
124 |
def Session(self): |
def Session(self): |
125 |
"""Return the application's session object""" |
"""Return the application's session object""" |
128 |
def SetSession(self, session): |
def SetSession(self, session): |
129 |
"""Make session the new session. |
"""Make session the new session. |
130 |
|
|
131 |
Issue SESSION_CHANGED. |
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 |
""" |
""" |
136 |
oldsession = self.session |
oldsession = self.session |
137 |
self.session = session |
self.session = session |
138 |
|
self.subscribe_session(self.session) |
139 |
self.issue(SESSION_CHANGED) |
self.issue(SESSION_CHANGED) |
140 |
self.interactor.SetSession(session) |
self.interactor.SetSession(session) |
141 |
self.set_map() |
self.maps_changed() |
142 |
if oldsession is not None: |
if oldsession is not None: |
143 |
|
self.unsubscribe_session(oldsession) |
144 |
oldsession.Destroy() |
oldsession.Destroy() |
145 |
|
|
146 |
|
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 |
def create_session(self): |
def create_session(self): |
163 |
# Create a simple default session |
"""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 |
self.SetSession(create_empty_session()) |
self.SetSession(create_empty_session()) |
171 |
|
|
172 |
def OpenSession(self, filename): |
def OpenSession(self, filename): |
178 |
def SaveSession(self): |
def SaveSession(self): |
179 |
save_session(self.session, self.session.filename) |
save_session(self.session, self.session.filename) |
180 |
|
|
181 |
def set_map(self): |
def maps_changed(self, *args): |
182 |
if self.session.HasMaps(): |
if self.session.HasMaps(): |
183 |
self.top.SetMap(self.session.Maps()[0]) |
self.top.SetMap(self.session.Maps()[0]) |
184 |
else: |
else: |