17 |
from wxPython.wx import * |
from wxPython.wx import * |
18 |
|
|
19 |
import Thuban |
import Thuban |
20 |
from Thuban.Model.session import Session |
from Thuban.Model.session import Session, create_empty_session |
21 |
from Thuban.Model.map import Map |
from Thuban.Model.map import Map |
22 |
from Thuban.Model.layer import Layer |
from Thuban.Model.layer import Layer |
23 |
from Thuban.Model.color import Color |
from Thuban.Model.color import Color |
30 |
|
|
31 |
import main |
import main |
32 |
from command import registry, Command |
from command import registry, Command |
33 |
|
from messages import SELECTED_SHAPE |
34 |
|
|
35 |
|
|
36 |
# the directory where the toolbar icons are stored |
# the directory where the toolbar icons are stored |
40 |
|
|
41 |
class MainWindow(wxFrame): |
class MainWindow(wxFrame): |
42 |
|
|
43 |
def __init__(self, parent, ID): |
def __init__(self, parent, ID, interactor): |
44 |
wxFrame.__init__(self, parent, ID, 'Thuban', |
wxFrame.__init__(self, parent, ID, 'Thuban', |
45 |
wxDefaultPosition, wxSize(400, 300)) |
wxDefaultPosition, wxSize(400, 300)) |
46 |
|
|
47 |
|
self.interactor = interactor |
48 |
|
|
49 |
self.CreateStatusBar() |
self.CreateStatusBar() |
50 |
self.SetStatusText("This is the wxPython-based " |
self.SetStatusText("This is the wxPython-based " |
51 |
"Graphical User Interface for exploring geographic data") |
"Graphical User Interface for exploring geographic data") |
97 |
|
|
98 |
# toolbar |
# toolbar |
99 |
toolbar = self.CreateToolBar(wxTB_3DBUTTONS) |
toolbar = self.CreateToolBar(wxTB_3DBUTTONS) |
100 |
|
|
101 |
|
# set the size of the tools' bitmaps. Not needed on wxGTK, but |
102 |
|
# on Windows. We probably shouldn't hardwire the bitmap size |
103 |
|
# here |
104 |
|
toolbar.SetToolBitmapSize(wxSize(24, 24)) |
105 |
|
|
106 |
for name in ["map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool", |
for name in ["map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool", |
107 |
"map_identify_tool", "map_label_tool"]: |
"map_identify_tool", "map_label_tool"]: |
108 |
self.add_toolbar_command(toolbar, name) |
self.add_toolbar_command(toolbar, name) |
110 |
toolbar.Realize() |
toolbar.Realize() |
111 |
|
|
112 |
# Create the map canvas |
# Create the map canvas |
113 |
canvas = view.MapCanvas(self, -1) |
canvas = view.MapCanvas(self, -1, interactor) |
114 |
self.canvas = canvas |
self.canvas = canvas |
115 |
|
|
116 |
|
self.init_dialogs() |
117 |
|
|
118 |
|
interactor.Subscribe(SELECTED_SHAPE, self.identify_view_on_demand) |
119 |
|
|
120 |
EVT_CLOSE(self, self.OnClose) |
EVT_CLOSE(self, self.OnClose) |
121 |
|
|
122 |
def init_ids(self): |
def init_ids(self): |
194 |
if command.IsCheckCommand(): |
if command.IsCheckCommand(): |
195 |
event.Check(command.Checked(self)) |
event.Check(command.Checked(self)) |
196 |
|
|
197 |
|
def RunMessageBox(self, title, text, flags = wxOK | wxICON_INFORMATION): |
198 |
|
"""Run a modla message box with the given text, title and flags |
199 |
|
and return the result""" |
200 |
|
dlg = wxMessageDialog(self, text, title, flags) |
201 |
|
result = dlg.ShowModal() |
202 |
|
dlg.Destroy() |
203 |
|
return result |
204 |
|
|
205 |
|
def init_dialogs(self): |
206 |
|
"""Initialize the dialog handling""" |
207 |
|
# The mainwindow maintains a dict mapping names to open |
208 |
|
# non-modal dialogs. The dialogs are put into this dict when |
209 |
|
# they're created and removed when they're closed |
210 |
|
self.dialogs = {} |
211 |
|
|
212 |
|
def add_dialog(self, name, dialog): |
213 |
|
if self.dialogs.has_key(name): |
214 |
|
raise RuntimeError("The Dialog named %s is already open" % name) |
215 |
|
self.dialogs[name] = dialog |
216 |
|
|
217 |
|
def dialog_open(self, name): |
218 |
|
return self.dialogs.has_key(name) |
219 |
|
|
220 |
|
def remove_dialog(self, name): |
221 |
|
del self.dialogs[name] |
222 |
|
|
223 |
|
def get_open_dialog(self, name): |
224 |
|
return self.dialogs.get(name) |
225 |
|
|
226 |
|
def save_modified_session(self, can_veto = 1): |
227 |
|
"""If the current session has been modified, ask the user |
228 |
|
whether to save it and do so if requested. Return the outcome of |
229 |
|
the dialog (either wxID_OK, wxID_CANCEL or wxID_NO). If the |
230 |
|
dialog wasn't run return wxID_NO. |
231 |
|
|
232 |
|
If the can_veto parameter is true (default) the dialog includes |
233 |
|
a cancel button, otherwise not. |
234 |
|
""" |
235 |
|
if main.app.session.WasModified(): |
236 |
|
flags = wxYES_NO | wxICON_QUESTION |
237 |
|
if can_veto: |
238 |
|
flags = flags | wxCANCEL |
239 |
|
result = self.RunMessageBox("Exit", |
240 |
|
("The session has been modified." |
241 |
|
" Do you want to save it?"), |
242 |
|
flags) |
243 |
|
if result == wxID_YES: |
244 |
|
self.SaveSession() |
245 |
|
else: |
246 |
|
result = wxID_NO |
247 |
|
return result |
248 |
|
|
249 |
def NewSession(self): |
def NewSession(self): |
250 |
session = Session("") |
self.save_modified_session() |
251 |
session.AddMap(Map("")) |
main.app.SetSession(create_empty_session()) |
|
main.app.SetSession(session) |
|
252 |
|
|
253 |
def OpenSession(self): |
def OpenSession(self): |
254 |
|
self.save_modified_session() |
255 |
dlg = wxFileDialog(self, "Select a session file", ".", "", |
dlg = wxFileDialog(self, "Select a session file", ".", "", |
256 |
"*.session", wxOPEN) |
"*.session", wxOPEN) |
257 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
273 |
self.Close(false) |
self.Close(false) |
274 |
|
|
275 |
def OnClose(self, event): |
def OnClose(self, event): |
276 |
veto = 0 |
result = self.save_modified_session(can_veto = event.CanVeto()) |
277 |
if main.app.session.WasModified(): |
if result == wxID_CANCEL: |
|
flags = wxYES_NO | wxICON_QUESTION |
|
|
if event.CanVeto(): |
|
|
flags = flags | wxCANCEL |
|
|
dlg = wxMessageDialog(self, |
|
|
("The session has been modified." |
|
|
" Do you want to save it?"), |
|
|
"Exit", flags) |
|
|
result = dlg.ShowModal() |
|
|
dlg.Destroy() |
|
|
if result == wxID_YES: |
|
|
self.SaveSession() |
|
|
elif result == wxID_CANCEL: |
|
|
veto = 1 |
|
|
|
|
|
if veto: |
|
278 |
event.Veto() |
event.Veto() |
279 |
else: |
else: |
280 |
self.Destroy() |
self.Destroy() |
282 |
def SetMap(self, map): |
def SetMap(self, map): |
283 |
self.canvas.SetMap(map) |
self.canvas.SetMap(map) |
284 |
|
|
285 |
|
def ShowSessionTree(self): |
286 |
|
name = "session_tree" |
287 |
|
dialog = self.get_open_dialog(name) |
288 |
|
if dialog is None: |
289 |
|
dialog = tree.SessionTreeView(self, main.app, name) |
290 |
|
self.add_dialog(name, dialog) |
291 |
|
dialog.Show(true) |
292 |
|
else: |
293 |
|
# FIXME: bring dialog to front here |
294 |
|
pass |
295 |
|
|
296 |
def About(self): |
def About(self): |
297 |
dlg = wxMessageDialog(self, |
self.RunMessageBox("About", |
298 |
("Thuban is a program for\n" |
("Thuban is a program for\n" |
299 |
"exploring geographic data.\n" |
"exploring geographic data.\n" |
300 |
"Copyright (C) 2001 Intevation GmbH.\n" |
"Copyright (C) 2001 Intevation GmbH.\n" |
301 |
"Thuban is licensed under the GPL"), |
"Thuban is licensed under the GPL"), |
302 |
"About", wxOK | wxICON_INFORMATION) |
wxOK | wxICON_INFORMATION) |
|
dlg.ShowModal() |
|
|
dlg.Destroy() |
|
303 |
|
|
304 |
def AddLayer(self): |
def AddLayer(self): |
305 |
dlg = wxFileDialog(self, "Select a session file", ".", "", "*.*", |
dlg = wxFileDialog(self, "Select a session file", ".", "", "*.*", |
308 |
filename = dlg.GetPath() |
filename = dlg.GetPath() |
309 |
title = os.path.splitext(os.path.basename(filename))[0] |
title = os.path.splitext(os.path.basename(filename))[0] |
310 |
layer = Layer(title, filename) |
layer = Layer(title, filename) |
311 |
self.canvas.Map().AddLayer(layer) |
map = self.canvas.Map() |
312 |
|
has_layers = map.HasLayers() |
313 |
|
try: |
314 |
|
map.AddLayer(layer) |
315 |
|
except IOError: |
316 |
|
# the layer couldn't be opened |
317 |
|
self.RunMessageBox("Add Layer", |
318 |
|
"Can't open the file '%s'." % filename) |
319 |
|
else: |
320 |
|
if not has_layers: |
321 |
|
# if we're adding a layer to an empty map, for the |
322 |
|
# new map to the window |
323 |
|
self.canvas.FitMapToWindow() |
324 |
dlg.Destroy() |
dlg.Destroy() |
325 |
|
|
326 |
def RemoveLayer(self): |
def RemoveLayer(self): |
327 |
layer = self.current_layer() |
layer = self.current_layer() |
328 |
if layer is not None: |
if layer is not None: |
329 |
self.canvas.Map().RemoveLayer(layer) |
self.canvas.Map().RemoveLayer(layer) |
|
else: |
|
|
dlg = wxMessageDialog(self, "No layer is selected.\n", |
|
|
"Remove layer", wxOK | wxICON_INFORMATION) |
|
|
dlg.ShowModal() |
|
|
dlg.Destroy() |
|
330 |
|
|
331 |
def RaiseLayer(self): |
def RaiseLayer(self): |
332 |
layer = self.current_layer() |
layer = self.current_layer() |
343 |
|
|
344 |
If no layer is selected, return None |
If no layer is selected, return None |
345 |
""" |
""" |
346 |
tree = main.app.tree.tree |
return self.interactor.SelectedLayer() |
|
layer = tree.GetPyData(tree.GetSelection()) |
|
|
if isinstance(layer, Layer): |
|
|
return layer |
|
|
return None |
|
347 |
|
|
348 |
def has_selected_layer(self): |
def has_selected_layer(self): |
349 |
"""Return true if a layer is currently selected""" |
"""Return true if a layer is currently selected""" |
350 |
tree = main.app.tree.tree |
return self.interactor.HasSelectedLayer() |
|
layer = tree.GetPyData(tree.GetSelection()) |
|
|
return isinstance(layer, Layer) |
|
351 |
|
|
352 |
def choose_color(self): |
def choose_color(self): |
353 |
"""Run the color selection dialog and return the selected color. |
"""Run the color selection dialog and return the selected color. |
402 |
def LayerShowTable(self): |
def LayerShowTable(self): |
403 |
layer = self.current_layer() |
layer = self.current_layer() |
404 |
if layer is not None: |
if layer is not None: |
405 |
tv = tableview.TableFrame(self, layer.table) |
table = layer.table |
406 |
tv.Show(true) |
name = "table_view" + str(id(table)) |
407 |
|
dialog = self.get_open_dialog(name) |
408 |
|
if dialog is None: |
409 |
|
dialog = tableview.TableFrame(self, self.interactor, name, |
410 |
|
"Table: %s" % layer.Title(), |
411 |
|
layer, table) |
412 |
|
self.add_dialog(name, dialog) |
413 |
|
dialog.Show(true) |
414 |
|
else: |
415 |
|
# FIXME: bring dialog to front here |
416 |
|
pass |
417 |
|
|
418 |
def Projection(self): |
def Projection(self): |
419 |
map = self.canvas.Map() |
map = self.canvas.Map() |
441 |
self.canvas.PanTool() |
self.canvas.PanTool() |
442 |
|
|
443 |
def IdentifyTool(self): |
def IdentifyTool(self): |
|
if self.identify_view is None: |
|
|
self.identify_view = identifyview.IdentifyView(self, main.app) |
|
|
self.identify_view.Show(true) |
|
444 |
self.canvas.IdentifyTool() |
self.canvas.IdentifyTool() |
445 |
|
self.identify_view_on_demand(None, None) |
446 |
|
|
447 |
def LabelTool(self): |
def LabelTool(self): |
448 |
self.canvas.LabelTool() |
self.canvas.LabelTool() |
453 |
def PrintMap(self): |
def PrintMap(self): |
454 |
self.canvas.Print() |
self.canvas.Print() |
455 |
|
|
456 |
|
def identify_view_on_demand(self, layer, shape): |
457 |
|
name = "identify_view" |
458 |
|
if self.canvas.CurrentTool() == "IdentifyTool": |
459 |
|
if not self.dialog_open(name): |
460 |
|
dialog = identifyview.IdentifyView(self, self.interactor, name) |
461 |
|
self.add_dialog(name, dialog) |
462 |
|
dialog.Show(true) |
463 |
|
else: |
464 |
|
# FIXME: bring dialog to front? |
465 |
|
pass |
466 |
|
|
467 |
# |
# |
468 |
# Define all the commands available in the main window |
# Define all the commands available in the main window |