1 |
# Copyright (C) 2001, 2002 by Intevation GmbH |
# Copyright (C) 2001, 2002, 2003, 2004 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]> |
5 |
|
# Frank Koormann <[email protected]> |
6 |
# |
# |
7 |
# This program is free software under the GPL (>=v2) |
# This program is free software under the GPL (>=v2) |
8 |
# Read the file COPYING coming with Thuban for details. |
# Read the file COPYING coming with Thuban for details. |
12 |
""" |
""" |
13 |
|
|
14 |
__version__ = "$Revision$" |
__version__ = "$Revision$" |
15 |
|
# $Source$ |
16 |
|
# $Id$ |
17 |
|
|
18 |
import os |
import os |
19 |
|
import copy |
20 |
|
|
21 |
from wxPython.wx import * |
from wxPython.wx import * |
22 |
|
|
23 |
import Thuban |
import Thuban |
24 |
|
|
25 |
|
from Thuban import _ |
26 |
|
from Thuban.Model.messages import TITLE_CHANGED |
27 |
from Thuban.Model.session import create_empty_session |
from Thuban.Model.session import create_empty_session |
28 |
from Thuban.Model.layer import Layer |
from Thuban.Model.layer import Layer, RasterLayer |
29 |
from Thuban.Model.color import Color |
from Thuban.Model.postgisdb import PostGISShapeStore, has_postgis_support |
30 |
from Thuban.Model.proj import Projection |
# XXX: replace this by |
31 |
|
# from wxPython.lib.dialogs import wxMultipleChoiceDialog |
32 |
|
# when Thuban does not support wxPython 2.4.0 any more. |
33 |
|
from Thuban.UI.multiplechoicedialog import wxMultipleChoiceDialog |
34 |
|
|
35 |
import view |
import view |
36 |
import tree |
import tree |
|
import proj4dialog |
|
37 |
import tableview, identifyview |
import tableview, identifyview |
38 |
|
import legend |
39 |
from menu import Menu |
from menu import Menu |
40 |
|
|
41 |
from context import Context |
from context import Context |
42 |
from command import registry, Command |
from command import registry, Command, ToolCommand |
43 |
from messages import SELECTED_SHAPE, VIEW_POSITION |
from messages import LAYER_SELECTED, SHAPES_SELECTED, VIEW_POSITION, \ |
44 |
|
MAP_REPLACED |
45 |
|
from about import About |
46 |
# the directory where the toolbar icons are stored |
|
47 |
bitmapdir = os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Bitmaps") |
from Thuban.UI.dock import DockFrame |
48 |
bitmapext = ".xpm" |
from Thuban.UI.join import JoinDialog |
49 |
|
from Thuban.UI.dbdialog import DBFrame, DBDialog, ChooseDBTableDialog |
50 |
|
import resource |
51 |
class MainWindow(wxFrame): |
import Thuban.Model.resource |
52 |
|
|
53 |
def __init__(self, parent, ID, application, interactor): |
import projdialog |
54 |
wxFrame.__init__(self, parent, ID, 'Thuban', |
|
55 |
wxDefaultPosition, wxSize(400, 300)) |
from Thuban.Lib.classmapper import ClassMapper |
56 |
|
|
57 |
|
layer_properties_dialogs = ClassMapper() |
58 |
|
|
59 |
|
class MainWindow(DockFrame): |
60 |
|
|
61 |
|
# Some messages that can be subscribed/unsubscribed directly through |
62 |
|
# the MapCanvas come in fact from other objects. This is a map to |
63 |
|
# map those messages to the names of the instance variables they |
64 |
|
# actually come from. This delegation is implemented in the |
65 |
|
# Subscribe and unsubscribed methods |
66 |
|
delegated_messages = {LAYER_SELECTED: "canvas", |
67 |
|
SHAPES_SELECTED: "canvas", |
68 |
|
MAP_REPLACED: "canvas"} |
69 |
|
|
70 |
|
# Methods delegated to some instance variables. The delegation is |
71 |
|
# implemented in the __getattr__ method. |
72 |
|
delegated_methods = {"SelectLayer": "canvas", |
73 |
|
"SelectShapes": "canvas", |
74 |
|
"SelectedLayer": "canvas", |
75 |
|
"SelectedShapes": "canvas", |
76 |
|
} |
77 |
|
|
78 |
|
def __init__(self, parent, ID, title, application, interactor, |
79 |
|
initial_message = None, size = wxSize(-1, -1)): |
80 |
|
DockFrame.__init__(self, parent, ID, title, wxDefaultPosition, size) |
81 |
|
#wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, size) |
82 |
|
|
83 |
self.application = application |
self.application = application |
|
self.interactor = interactor |
|
84 |
|
|
85 |
self.CreateStatusBar() |
self.CreateStatusBar() |
86 |
self.SetStatusText("This is the wxPython-based " |
if initial_message: |
87 |
"Graphical User Interface for exploring geographic data") |
self.SetStatusText(initial_message) |
88 |
|
|
89 |
self.identify_view = None |
self.identify_view = None |
90 |
|
|
98 |
# call Realize to make sure that the tools appear. |
# call Realize to make sure that the tools appear. |
99 |
toolbar.Realize() |
toolbar.Realize() |
100 |
|
|
101 |
|
|
102 |
# Create the map canvas |
# Create the map canvas |
103 |
canvas = view.MapCanvas(self, -1, interactor) |
canvas = view.MapCanvas(self, -1) |
104 |
canvas.Subscribe(VIEW_POSITION, self.view_position_changed) |
canvas.Subscribe(VIEW_POSITION, self.view_position_changed) |
105 |
|
canvas.Subscribe(SHAPES_SELECTED, self.identify_view_on_demand) |
106 |
self.canvas = canvas |
self.canvas = canvas |
107 |
|
self.canvas.Subscribe(TITLE_CHANGED, self.title_changed) |
108 |
|
|
109 |
|
self.SetMainWindow(self.canvas) |
110 |
|
|
111 |
|
self.SetAutoLayout(True) |
112 |
|
|
113 |
self.init_dialogs() |
self.init_dialogs() |
114 |
|
|
115 |
interactor.Subscribe(SELECTED_SHAPE, self.identify_view_on_demand) |
self.ShowLegend() |
116 |
|
|
117 |
EVT_CLOSE(self, self.OnClose) |
EVT_CLOSE(self, self.OnClose) |
118 |
|
|
119 |
|
def Subscribe(self, channel, *args): |
120 |
|
"""Subscribe a function to a message channel. |
121 |
|
|
122 |
|
If channel is one of the delegated messages call the appropriate |
123 |
|
object's Subscribe method. Otherwise do nothing. |
124 |
|
""" |
125 |
|
if channel in self.delegated_messages: |
126 |
|
object = getattr(self, self.delegated_messages[channel]) |
127 |
|
object.Subscribe(channel, *args) |
128 |
|
else: |
129 |
|
print "Trying to subscribe to unsupported channel %s" % channel |
130 |
|
|
131 |
|
def Unsubscribe(self, channel, *args): |
132 |
|
"""Unsubscribe a function from a message channel. |
133 |
|
|
134 |
|
If channel is one of the delegated messages call the appropriate |
135 |
|
object's Unsubscribe method. Otherwise do nothing. |
136 |
|
""" |
137 |
|
if channel in self.delegated_messages: |
138 |
|
object = getattr(self, self.delegated_messages[channel]) |
139 |
|
try: |
140 |
|
object.Unsubscribe(channel, *args) |
141 |
|
except wxPyDeadObjectError: |
142 |
|
# The object was a wxObject and has already been |
143 |
|
# destroyed. Hopefully it has unsubscribed all its |
144 |
|
# subscribers already so that it's OK if we do nothing |
145 |
|
# here |
146 |
|
pass |
147 |
|
|
148 |
|
def __getattr__(self, attr): |
149 |
|
"""If attr is one of the delegated methods return that method |
150 |
|
|
151 |
|
Otherwise raise AttributeError. |
152 |
|
""" |
153 |
|
if attr in self.delegated_methods: |
154 |
|
return getattr(getattr(self, self.delegated_methods[attr]), attr) |
155 |
|
raise AttributeError(attr) |
156 |
|
|
157 |
def init_ids(self): |
def init_ids(self): |
158 |
"""Initialize the ids""" |
"""Initialize the ids""" |
159 |
self.current_id = 6000 |
self.current_id = 6000 |
192 |
return menu_bar |
return menu_bar |
193 |
|
|
194 |
def build_menu(self, menudesc): |
def build_menu(self, menudesc): |
195 |
"""Build and return a wxMenu from a menudescription""" |
"""Return a wxMenu built from the menu description menudesc""" |
196 |
wxmenu = wxMenu() |
wxmenu = wxMenu() |
197 |
last = None |
last = None |
198 |
for item in menudesc.items: |
for item in menudesc.items: |
|
# here the items must all be Menu instances themselves |
|
199 |
if item is None: |
if item is None: |
200 |
# a separator. Only add one if the last item was not a |
# a separator. Only add one if the last item was not a |
201 |
# separator |
# separator |
248 |
command.IsCheckCommand()) |
command.IsCheckCommand()) |
249 |
self.bind_command_events(command, ID) |
self.bind_command_events(command, ID) |
250 |
else: |
else: |
251 |
print "Unknown command %s" % name |
print _("Unknown command %s") % name |
252 |
|
|
253 |
def add_toolbar_command(self, toolbar, name): |
def add_toolbar_command(self, toolbar, name): |
254 |
"""Add the command with name name to the toolbar toolbar. |
"""Add the command with name name to the toolbar toolbar. |
263 |
command = registry.Command(name) |
command = registry.Command(name) |
264 |
if command is not None: |
if command is not None: |
265 |
ID = self.get_id(name) |
ID = self.get_id(name) |
266 |
filename = os.path.join(bitmapdir, command.Icon()) + bitmapext |
bitmap = resource.GetBitmapResource(command.Icon(), |
267 |
bitmap = wxBitmap(filename, wxBITMAP_TYPE_XPM) |
wxBITMAP_TYPE_XPM) |
268 |
toolbar.AddTool(ID, bitmap, |
toolbar.AddTool(ID, bitmap, |
269 |
shortHelpString = command.HelpText(), |
shortHelpString = command.HelpText(), |
270 |
isToggle = command.IsCheckCommand()) |
isToggle = command.IsCheckCommand()) |
271 |
self.bind_command_events(command, ID) |
self.bind_command_events(command, ID) |
272 |
else: |
else: |
273 |
print "Unknown command %s" % name |
print _("Unknown command %s") % name |
274 |
|
|
275 |
|
def Context(self): |
276 |
|
"""Return the context object for a command invoked from this window |
277 |
|
""" |
278 |
|
return Context(self.application, self.application.Session(), self) |
279 |
|
|
280 |
def invoke_command(self, event): |
def invoke_command(self, event): |
281 |
name = self.id_to_name.get(event.GetId()) |
name = self.id_to_name.get(event.GetId()) |
282 |
if name is not None: |
if name is not None: |
283 |
command = registry.Command(name) |
command = registry.Command(name) |
284 |
context = Context(self.application, self.application.Session(), |
command.Execute(self.Context()) |
|
self) |
|
|
command.Execute(context) |
|
285 |
else: |
else: |
286 |
print "Unknown command ID %d" % event.GetId() |
print _("Unknown command ID %d") % event.GetId() |
287 |
|
|
288 |
def update_command_ui(self, event): |
def update_command_ui(self, event): |
289 |
#print "update_command_ui", self.id_to_name[event.GetId()] |
#print "update_command_ui", self.id_to_name[event.GetId()] |
290 |
context = Context(self.application, self.application.Session(), self) |
context = self.Context() |
291 |
command = registry.Command(self.id_to_name[event.GetId()]) |
command = registry.Command(self.id_to_name[event.GetId()]) |
292 |
if command is not None: |
if command is not None: |
293 |
event.Enable(command.Sensitive(context)) |
sensitive = command.Sensitive(context) |
294 |
|
event.Enable(sensitive) |
295 |
|
if command.IsTool() and not sensitive and command.Checked(context): |
296 |
|
# When a checked tool command is disabled deselect all |
297 |
|
# tools. Otherwise the tool would remain active but it |
298 |
|
# might lead to errors if the tools stays active. This |
299 |
|
# problem occurred in GREAT-ER and this fixes it, but |
300 |
|
# it's not clear to me whether this is really the best |
301 |
|
# way to do it (BH, 20021206). |
302 |
|
self.canvas.SelectTool(None) |
303 |
event.SetText(command.DynText(context)) |
event.SetText(command.DynText(context)) |
304 |
if command.IsCheckCommand(): |
if command.IsCheckCommand(): |
305 |
event.Check(command.Checked(context)) |
event.Check(command.Checked(context)) |
306 |
|
|
307 |
def RunMessageBox(self, title, text, flags = wxOK | wxICON_INFORMATION): |
def RunMessageBox(self, title, text, flags = wxOK | wxICON_INFORMATION): |
308 |
"""Run a modal message box with the given text, title and flags |
"""Run a modal message box with the given text, title and flags |
309 |
and return the result""" |
and return the result""" |
310 |
dlg = wxMessageDialog(self, text, title, flags) |
dlg = wxMessageDialog(self, text, title, flags) |
311 |
|
dlg.CenterOnParent() |
312 |
result = dlg.ShowModal() |
result = dlg.ShowModal() |
313 |
dlg.Destroy() |
dlg.Destroy() |
314 |
return result |
return result |
322 |
|
|
323 |
def add_dialog(self, name, dialog): |
def add_dialog(self, name, dialog): |
324 |
if self.dialogs.has_key(name): |
if self.dialogs.has_key(name): |
325 |
raise RuntimeError("The Dialog named %s is already open" % name) |
raise RuntimeError(_("The Dialog named %s is already open") % name) |
326 |
self.dialogs[name] = dialog |
self.dialogs[name] = dialog |
327 |
|
|
328 |
def dialog_open(self, name): |
def dialog_open(self, name): |
335 |
return self.dialogs.get(name) |
return self.dialogs.get(name) |
336 |
|
|
337 |
def view_position_changed(self): |
def view_position_changed(self): |
338 |
|
"""Put current position in status bar after a mouse move.""" |
339 |
pos = self.canvas.CurrentPosition() |
pos = self.canvas.CurrentPosition() |
340 |
if pos is not None: |
if pos is not None: |
341 |
text = "(%10.10g, %10.10g)" % pos |
text = "(%10.10g, %10.10g)" % pos |
342 |
else: |
else: |
343 |
text = "" |
text = "" |
344 |
|
# XXX This is a hack until we find a better place for this code. |
345 |
|
# (BER 20050120) |
346 |
|
# BH wrote (20050120): |
347 |
|
# this branch is only executed when the mouse |
348 |
|
# leaves the canvas window, so it's not that often [..] |
349 |
|
# [Here] not the right place to put this code. |
350 |
|
# I don't have a better solution at hand, |
351 |
|
# but the view_position_changed is only there to update |
352 |
|
# the current position. If other information is to |
353 |
|
# be shown in the status bar it should |
354 |
|
# be handled in a different way and |
355 |
|
# by other methods. |
356 |
|
# |
357 |
|
# The status bar widget supports some kind of stack of texts. |
358 |
|
# maybe that can be used to distinguis |
359 |
|
# between short-lived information such as the mouse position |
360 |
|
# and more permanent information such as the hint |
361 |
|
# about the projections. |
362 |
|
map = self.canvas.Map() |
363 |
|
for layer in map.layers: |
364 |
|
bbox = layer.LatLongBoundingBox() |
365 |
|
if bbox: |
366 |
|
left, bottom, right, top = bbox |
367 |
|
if not (-180 <= left <= 180 and |
368 |
|
-180 <= right <= 180 and |
369 |
|
-90 <= top <= 90 and |
370 |
|
-90 <= bottom <= 90): |
371 |
|
text = _("Select layer '%s' and pick a projection " |
372 |
|
"using Layer/Projection...") % layer.title |
373 |
|
break |
374 |
|
|
375 |
|
self.set_position_text(text) |
376 |
|
|
377 |
|
def set_position_text(self, text): |
378 |
|
"""Set the statusbar text showing the current position. |
379 |
|
|
380 |
|
By default the text is shown in field 0 of the status bar. |
381 |
|
Override this method in derived classes to put it into a |
382 |
|
different field of the statusbar. |
383 |
|
""" |
384 |
self.SetStatusText(text) |
self.SetStatusText(text) |
385 |
|
|
386 |
|
def OpenOrRaiseDialog(self, name, dialog_class, *args, **kw): |
387 |
|
""" |
388 |
|
Open or raise a dialog. |
389 |
|
|
390 |
|
If a dialog with the denoted name does already exist it is |
391 |
|
raised. Otherwise a new dialog, an instance of dialog_class, |
392 |
|
is created, inserted into the main list and displayed. |
393 |
|
""" |
394 |
|
dialog = self.get_open_dialog(name) |
395 |
|
|
396 |
|
if dialog is None: |
397 |
|
dialog = dialog_class(self, name, *args, **kw) |
398 |
|
self.add_dialog(name, dialog) |
399 |
|
dialog.Show(True) |
400 |
|
else: |
401 |
|
dialog.Raise() |
402 |
|
|
403 |
def save_modified_session(self, can_veto = 1): |
def save_modified_session(self, can_veto = 1): |
404 |
"""If the current session has been modified, ask the user |
"""If the current session has been modified, ask the user |
405 |
whether to save it and do so if requested. Return the outcome of |
whether to save it and do so if requested. Return the outcome of |
413 |
flags = wxYES_NO | wxICON_QUESTION |
flags = wxYES_NO | wxICON_QUESTION |
414 |
if can_veto: |
if can_veto: |
415 |
flags = flags | wxCANCEL |
flags = flags | wxCANCEL |
416 |
result = self.RunMessageBox("Exit", |
result = self.RunMessageBox(_("Exit"), |
417 |
("The session has been modified." |
_("The session has been modified." |
418 |
" Do you want to save it?"), |
" Do you want to save it?"), |
419 |
flags) |
flags) |
420 |
if result == wxID_YES: |
if result == wxID_YES: |
424 |
return result |
return result |
425 |
|
|
426 |
def NewSession(self): |
def NewSession(self): |
427 |
self.save_modified_session() |
if self.save_modified_session() != wxID_CANCEL: |
428 |
self.application.SetSession(create_empty_session()) |
self.application.SetSession(create_empty_session()) |
429 |
|
|
430 |
def OpenSession(self): |
def OpenSession(self): |
431 |
self.save_modified_session() |
if self.save_modified_session() != wxID_CANCEL: |
432 |
dlg = wxFileDialog(self, "Select a session file", ".", "", |
dlg = wxFileDialog(self, _("Open Session"), |
433 |
"*.thuban", wxOPEN) |
self.application.Path("data"), "", |
434 |
if dlg.ShowModal() == wxID_OK: |
"Thuban Session File (*.thuban)|*.thuban", |
435 |
self.application.OpenSession(dlg.GetPath()) |
wxOPEN) |
436 |
dlg.Destroy() |
if dlg.ShowModal() == wxID_OK: |
437 |
|
self.application.OpenSession(dlg.GetPath(), |
438 |
|
self.run_db_param_dialog) |
439 |
|
self.application.SetPath("data", dlg.GetPath()) |
440 |
|
dlg.Destroy() |
441 |
|
|
442 |
|
def run_db_param_dialog(self, parameters, message): |
443 |
|
dlg = DBDialog(self, _("DB Connection Parameters"), parameters, |
444 |
|
message) |
445 |
|
return dlg.RunDialog() |
446 |
|
|
447 |
def SaveSession(self): |
def SaveSession(self): |
448 |
if self.application.session.filename == None: |
if self.application.session.filename == None: |
449 |
self.SaveSessionAs() |
self.SaveSessionAs() |
450 |
self.application.SaveSession() |
else: |
451 |
|
self.application.SaveSession() |
452 |
|
|
453 |
def SaveSessionAs(self): |
def SaveSessionAs(self): |
454 |
dlg = wxFileDialog(self, "Enter a filename for session", ".", "", |
dlg = wxFileDialog(self, _("Save Session As"), |
455 |
"*.thuban", wxOPEN) |
self.application.Path("data"), "", |
456 |
|
"Thuban Session File (*.thuban)|*.thuban", |
457 |
|
wxSAVE|wxOVERWRITE_PROMPT) |
458 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
459 |
self.application.session.SetFilename(dlg.GetPath()) |
self.application.session.SetFilename(dlg.GetPath()) |
460 |
self.application.SaveSession() |
self.application.SaveSession() |
461 |
|
self.application.SetPath("data",dlg.GetPath()) |
462 |
dlg.Destroy() |
dlg.Destroy() |
463 |
|
|
464 |
def Exit(self): |
def Exit(self): |
465 |
self.Close(false) |
self.Close(False) |
466 |
|
|
467 |
def OnClose(self, event): |
def OnClose(self, event): |
468 |
result = self.save_modified_session(can_veto = event.CanVeto()) |
result = self.save_modified_session(can_veto = event.CanVeto()) |
469 |
if result == wxID_CANCEL: |
if result == wxID_CANCEL: |
470 |
event.Veto() |
event.Veto() |
471 |
else: |
else: |
472 |
|
# FIXME: it would be better to tie the unsubscription to |
473 |
|
# wx's destroy event, but that isn't implemented for wxGTK |
474 |
|
# yet. |
475 |
|
self.canvas.Unsubscribe(VIEW_POSITION, self.view_position_changed) |
476 |
|
DockFrame.OnClose(self, event) |
477 |
|
for dlg in self.dialogs.values(): |
478 |
|
dlg.Destroy() |
479 |
|
self.canvas.Destroy() |
480 |
self.Destroy() |
self.Destroy() |
481 |
|
|
482 |
def SetMap(self, map): |
def SetMap(self, map): |
483 |
self.canvas.SetMap(map) |
self.canvas.SetMap(map) |
484 |
|
self.update_title() |
485 |
|
|
486 |
def ShowSessionTree(self): |
dialog = self.FindRegisteredDock("legend") |
487 |
|
if dialog is not None: |
488 |
|
dialog.GetPanel().SetMap(self.Map()) |
489 |
|
|
490 |
|
def Map(self): |
491 |
|
"""Return the map displayed by this mainwindow""" |
492 |
|
|
493 |
|
return self.canvas.Map() |
494 |
|
|
495 |
|
def ToggleSessionTree(self): |
496 |
|
"""If the session tree is shown close it otherwise create a new tree""" |
497 |
name = "session_tree" |
name = "session_tree" |
498 |
dialog = self.get_open_dialog(name) |
dialog = self.get_open_dialog(name) |
499 |
if dialog is None: |
if dialog is None: |
500 |
dialog = tree.SessionTreeView(self, self.application, name) |
dialog = tree.SessionTreeView(self, self.application, name) |
501 |
self.add_dialog(name, dialog) |
self.add_dialog(name, dialog) |
502 |
dialog.Show(true) |
dialog.Show(True) |
503 |
else: |
else: |
504 |
# FIXME: bring dialog to front here |
dialog.Close() |
505 |
pass |
|
506 |
|
def SessionTreeShown(self): |
507 |
|
"""Return true iff the session tree is currently shown""" |
508 |
|
return self.get_open_dialog("session_tree") is not None |
509 |
|
|
510 |
def About(self): |
def About(self): |
511 |
self.RunMessageBox("About", |
dlg = About(self) |
512 |
("Thuban is a program for\n" |
dlg.ShowModal() |
513 |
"exploring geographic data.\n" |
dlg.Destroy() |
514 |
"Copyright (C) 2001 Intevation GmbH.\n" |
|
515 |
"Thuban is licensed under the GPL"), |
def DatabaseManagement(self): |
516 |
wxOK | wxICON_INFORMATION) |
name = "dbmanagement" |
517 |
|
dialog = self.get_open_dialog(name) |
518 |
|
if dialog is None: |
519 |
|
map = self.canvas.Map() |
520 |
|
dialog = DBFrame(self, name, self.application.Session()) |
521 |
|
self.add_dialog(name, dialog) |
522 |
|
dialog.Show() |
523 |
|
dialog.Raise() |
524 |
|
|
525 |
def AddLayer(self): |
def AddLayer(self): |
526 |
dlg = wxFileDialog(self, "Select a data file", ".", "", "*.*", |
dlg = wxFileDialog(self, _("Select one or more data files"), |
527 |
|
self.application.Path("data"), "", |
528 |
|
_("Shapefiles (*.shp)") + "|*.shp;*.SHP|" + |
529 |
|
_("All Files (*.*)") + "|*.*", |
530 |
|
wxOPEN | wxMULTIPLE) |
531 |
|
if dlg.ShowModal() == wxID_OK: |
532 |
|
filenames = dlg.GetPaths() |
533 |
|
for filename in filenames: |
534 |
|
title = os.path.splitext(os.path.basename(filename))[0] |
535 |
|
map = self.canvas.Map() |
536 |
|
has_layers = map.HasLayers() |
537 |
|
try: |
538 |
|
store = self.application.Session().OpenShapefile(filename) |
539 |
|
except IOError: |
540 |
|
# the layer couldn't be opened |
541 |
|
self.RunMessageBox(_("Add Layer"), |
542 |
|
_("Can't open the file '%s'.")%filename) |
543 |
|
else: |
544 |
|
layer = Layer(title, store) |
545 |
|
map.AddLayer(layer) |
546 |
|
if not has_layers: |
547 |
|
# if we're adding a layer to an empty map, fit the |
548 |
|
# new map to the window |
549 |
|
self.canvas.FitMapToWindow() |
550 |
|
self.application.SetPath("data",filename) |
551 |
|
dlg.Destroy() |
552 |
|
|
553 |
|
def AddRasterLayer(self): |
554 |
|
dlg = wxFileDialog(self, _("Select an image file"), |
555 |
|
self.application.Path("data"), "", "*.*", |
556 |
wxOPEN) |
wxOPEN) |
557 |
if dlg.ShowModal() == wxID_OK: |
if dlg.ShowModal() == wxID_OK: |
558 |
filename = dlg.GetPath() |
filename = dlg.GetPath() |
559 |
title = os.path.splitext(os.path.basename(filename))[0] |
title = os.path.splitext(os.path.basename(filename))[0] |
|
layer = Layer(title, filename) |
|
560 |
map = self.canvas.Map() |
map = self.canvas.Map() |
561 |
has_layers = map.HasLayers() |
has_layers = map.HasLayers() |
562 |
try: |
try: |
563 |
map.AddLayer(layer) |
layer = RasterLayer(title, filename) |
564 |
except IOError: |
except IOError: |
565 |
# the layer couldn't be opened |
# the layer couldn't be opened |
566 |
self.RunMessageBox("Add Layer", |
self.RunMessageBox(_("Add Image Layer"), |
567 |
"Can't open the file '%s'." % filename) |
_("Can't open the file '%s'.") % filename) |
568 |
else: |
else: |
569 |
|
map.AddLayer(layer) |
570 |
if not has_layers: |
if not has_layers: |
571 |
# if we're adding a layer to an empty map, for the |
# if we're adding a layer to an empty map, fit the |
572 |
# new map to the window |
# new map to the window |
573 |
self.canvas.FitMapToWindow() |
self.canvas.FitMapToWindow() |
574 |
|
self.application.SetPath("data", filename) |
575 |
|
dlg.Destroy() |
576 |
|
|
577 |
|
def AddDBLayer(self): |
578 |
|
"""Add a layer read from a database""" |
579 |
|
session = self.application.Session() |
580 |
|
dlg = ChooseDBTableDialog(self, self.application.Session()) |
581 |
|
|
582 |
|
if dlg.ShowModal() == wxID_OK: |
583 |
|
dbconn, dbtable, id_column, geo_column = dlg.GetTable() |
584 |
|
try: |
585 |
|
title = str(dbtable) |
586 |
|
|
587 |
|
# Chose the correct Interface for the database type |
588 |
|
store = session.OpenDBShapeStore(dbconn, dbtable, |
589 |
|
id_column = id_column, |
590 |
|
geometry_column = geo_column) |
591 |
|
layer = Layer(title, store) |
592 |
|
except: |
593 |
|
# Some error occured while initializing the layer |
594 |
|
self.RunMessageBox(_("Add Layer from database"), |
595 |
|
_("Can't open the database table '%s'") |
596 |
|
% dbtable) |
597 |
|
return |
598 |
|
|
599 |
|
map = self.canvas.Map() |
600 |
|
|
601 |
|
has_layers = map.HasLayers() |
602 |
|
map.AddLayer(layer) |
603 |
|
if not has_layers: |
604 |
|
self.canvas.FitMapToWindow() |
605 |
|
|
606 |
dlg.Destroy() |
dlg.Destroy() |
607 |
|
|
608 |
def RemoveLayer(self): |
def RemoveLayer(self): |
610 |
if layer is not None: |
if layer is not None: |
611 |
self.canvas.Map().RemoveLayer(layer) |
self.canvas.Map().RemoveLayer(layer) |
612 |
|
|
613 |
|
def CanRemoveLayer(self): |
614 |
|
"""Return true if the currently selected layer can be deleted. |
615 |
|
|
616 |
|
If no layer is selected return False. |
617 |
|
|
618 |
|
The return value of this method determines whether the remove |
619 |
|
layer command is sensitive in menu. |
620 |
|
""" |
621 |
|
layer = self.current_layer() |
622 |
|
if layer is not None: |
623 |
|
return self.canvas.Map().CanRemoveLayer(layer) |
624 |
|
return False |
625 |
|
|
626 |
|
def LayerToTop(self): |
627 |
|
layer = self.current_layer() |
628 |
|
if layer is not None: |
629 |
|
self.canvas.Map().MoveLayerToTop(layer) |
630 |
|
|
631 |
def RaiseLayer(self): |
def RaiseLayer(self): |
632 |
layer = self.current_layer() |
layer = self.current_layer() |
633 |
if layer is not None: |
if layer is not None: |
638 |
if layer is not None: |
if layer is not None: |
639 |
self.canvas.Map().LowerLayer(layer) |
self.canvas.Map().LowerLayer(layer) |
640 |
|
|
641 |
|
def LayerToBottom(self): |
642 |
|
layer = self.current_layer() |
643 |
|
if layer is not None: |
644 |
|
self.canvas.Map().MoveLayerToBottom(layer) |
645 |
|
|
646 |
def current_layer(self): |
def current_layer(self): |
647 |
"""Return the currently selected layer. |
"""Return the currently selected layer. |
648 |
|
|
649 |
If no layer is selected, return None |
If no layer is selected, return None |
650 |
""" |
""" |
651 |
return self.interactor.SelectedLayer() |
return self.canvas.SelectedLayer() |
652 |
|
|
653 |
def has_selected_layer(self): |
def has_selected_layer(self): |
654 |
"""Return true if a layer is currently selected""" |
"""Return true if a layer is currently selected""" |
655 |
return self.interactor.HasSelectedLayer() |
return self.canvas.HasSelectedLayer() |
656 |
|
|
657 |
def choose_color(self): |
def has_selected_shape_layer(self): |
658 |
"""Run the color selection dialog and return the selected color. |
"""Return true if a shape layer is currently selected""" |
659 |
|
return isinstance(self.current_layer(), Layer) |
660 |
|
|
661 |
|
def has_selected_shapes(self): |
662 |
|
"""Return true if a shape is currently selected""" |
663 |
|
return self.canvas.HasSelectedShapes() |
664 |
|
|
665 |
If the user cancels, return None. |
def HideLayer(self): |
|
""" |
|
|
dlg = wxColourDialog(self) |
|
|
color = None |
|
|
if dlg.ShowModal() == wxID_OK: |
|
|
data = dlg.GetColourData() |
|
|
wxc = data.GetColour() |
|
|
color = Color(wxc.Red() / 255.0, |
|
|
wxc.Green() / 255.0, |
|
|
wxc.Blue() / 255.0) |
|
|
dlg.Destroy() |
|
|
return color |
|
|
|
|
|
def LayerFillColor(self): |
|
666 |
layer = self.current_layer() |
layer = self.current_layer() |
667 |
if layer is not None: |
if layer is not None: |
668 |
color = self.choose_color() |
layer.SetVisible(False) |
|
if color is not None: |
|
|
layer.SetFill(color) |
|
669 |
|
|
670 |
def LayerTransparentFill(self): |
def ShowLayer(self): |
671 |
layer = self.current_layer() |
layer = self.current_layer() |
672 |
if layer is not None: |
if layer is not None: |
673 |
layer.SetFill(None) |
layer.SetVisible(True) |
674 |
|
|
675 |
def LayerOutlineColor(self): |
def ToggleLayerVisibility(self): |
676 |
layer = self.current_layer() |
layer = self.current_layer() |
677 |
if layer is not None: |
layer.SetVisible(not layer.Visible()) |
|
color = self.choose_color() |
|
|
if color is not None: |
|
|
layer.SetStroke(color) |
|
678 |
|
|
679 |
def LayerNoOutline(self): |
def DuplicateLayer(self): |
680 |
|
"""Ceate a new layer above the selected layer with the same shapestore |
681 |
|
""" |
682 |
layer = self.current_layer() |
layer = self.current_layer() |
683 |
if layer is not None: |
if layer is not None and hasattr(layer, "ShapeStore"): |
684 |
layer.SetStroke(None) |
new_layer = Layer(_("Copy of `%s'") % layer.Title(), |
685 |
|
layer.ShapeStore(), |
686 |
|
projection = layer.GetProjection()) |
687 |
|
new_classification = copy.deepcopy(layer.GetClassification()) |
688 |
|
new_layer.SetClassificationColumn( |
689 |
|
layer.GetClassificationColumn()) |
690 |
|
new_layer.SetClassification(new_classification) |
691 |
|
self.Map().AddLayer(new_layer) |
692 |
|
|
693 |
def HideLayer(self): |
def CanDuplicateLayer(self): |
694 |
|
"""Return whether the DuplicateLayer method can create a duplicate""" |
695 |
layer = self.current_layer() |
layer = self.current_layer() |
696 |
if layer is not None: |
return layer is not None and hasattr(layer, "ShapeStore") |
|
layer.SetVisible(0) |
|
|
|
|
|
def ShowLayer(self): |
|
|
layer = self.current_layer() |
|
|
if layer is not None: |
|
|
layer.SetVisible(1) |
|
697 |
|
|
698 |
def LayerShowTable(self): |
def LayerShowTable(self): |
699 |
|
""" |
700 |
|
Present a TableView Window for the current layer. |
701 |
|
In case the window is already open, bring it to the front. |
702 |
|
In case, there is no active layer, do nothing. |
703 |
|
In case, the layer has no ShapeStore, do nothing. |
704 |
|
""" |
705 |
layer = self.current_layer() |
layer = self.current_layer() |
706 |
if layer is not None: |
if layer is not None: |
707 |
table = layer.table |
if not hasattr(layer, "ShapeStore"): |
708 |
|
return |
709 |
|
table = layer.ShapeStore().Table() |
710 |
name = "table_view" + str(id(table)) |
name = "table_view" + str(id(table)) |
711 |
dialog = self.get_open_dialog(name) |
dialog = self.get_open_dialog(name) |
712 |
if dialog is None: |
if dialog is None: |
713 |
dialog = tableview.TableFrame(self, self.interactor, name, |
dialog = tableview.LayerTableFrame(self, name, |
714 |
"Table: %s" % layer.Title(), |
_("Layer Table: %s") % layer.Title(), |
715 |
layer, table) |
layer, table) |
716 |
self.add_dialog(name, dialog) |
self.add_dialog(name, dialog) |
717 |
dialog.Show(true) |
dialog.Show(True) |
718 |
else: |
else: |
719 |
# FIXME: bring dialog to front here |
dialog.Raise() |
720 |
pass |
|
721 |
|
def MapProjection(self): |
722 |
|
|
723 |
|
name = "map_projection" |
724 |
|
dialog = self.get_open_dialog(name) |
725 |
|
|
726 |
|
if dialog is None: |
727 |
|
map = self.canvas.Map() |
728 |
|
dialog = projdialog.ProjFrame(self, name, |
729 |
|
_("Map Projection: %s") % map.Title(), map) |
730 |
|
self.add_dialog(name, dialog) |
731 |
|
dialog.Show() |
732 |
|
dialog.Raise() |
733 |
|
|
734 |
def Projection(self): |
def LayerProjection(self): |
735 |
map = self.canvas.Map() |
|
736 |
proj = map.projection |
layer = self.current_layer() |
737 |
if proj is None: |
|
738 |
proj4Dlg = proj4dialog.Proj4Dialog(NULL, None, map.BoundingBox()) |
name = "layer_projection" + str(id(layer)) |
739 |
else: |
dialog = self.get_open_dialog(name) |
740 |
proj4Dlg = proj4dialog.Proj4Dialog(NULL, map.projection.params, |
|
741 |
map.BoundingBox()) |
if dialog is None: |
742 |
if proj4Dlg.ShowModal() == wxID_OK: |
map = self.canvas.Map() |
743 |
params = proj4Dlg.GetParams() |
dialog = projdialog.ProjFrame(self, name, |
744 |
if params is not None: |
_("Layer Projection: %s") % layer.Title(), layer) |
745 |
proj = Projection(params) |
self.add_dialog(name, dialog) |
746 |
|
dialog.Show() |
747 |
|
dialog.Raise() |
748 |
|
|
749 |
|
def LayerEditProperties(self): |
750 |
|
|
751 |
|
# |
752 |
|
# the menu option for this should only be available if there |
753 |
|
# is a current layer, so we don't need to check if the |
754 |
|
# current layer is None |
755 |
|
# |
756 |
|
|
757 |
|
layer = self.current_layer() |
758 |
|
self.OpenLayerProperties(layer) |
759 |
|
|
760 |
|
def OpenLayerProperties(self, layer, group = None): |
761 |
|
""" |
762 |
|
Open or raise the properties dialog. |
763 |
|
|
764 |
|
This method opens or raises the properties dialog for the |
765 |
|
currently selected layer if one is defined for this layer |
766 |
|
type. |
767 |
|
""" |
768 |
|
dialog_class = layer_properties_dialogs.get(layer) |
769 |
|
|
770 |
|
if dialog_class is not None: |
771 |
|
name = "layer_properties" + str(id(layer)) |
772 |
|
self.OpenOrRaiseDialog(name, dialog_class, layer, group = group) |
773 |
|
|
774 |
|
def LayerJoinTable(self): |
775 |
|
layer = self.canvas.SelectedLayer() |
776 |
|
if layer is not None: |
777 |
|
dlg = JoinDialog(self, _("Join Layer with Table"), |
778 |
|
self.application.session, |
779 |
|
layer = layer) |
780 |
|
dlg.ShowModal() |
781 |
|
|
782 |
|
def LayerUnjoinTable(self): |
783 |
|
layer = self.canvas.SelectedLayer() |
784 |
|
if layer is not None: |
785 |
|
orig_store = layer.ShapeStore().OrigShapeStore() |
786 |
|
if orig_store: |
787 |
|
layer.SetShapeStore(orig_store) |
788 |
|
|
789 |
|
def ShowLegend(self): |
790 |
|
if not self.LegendShown(): |
791 |
|
self.ToggleLegend() |
792 |
|
|
793 |
|
def ToggleLegend(self): |
794 |
|
"""Show the legend if it's not shown otherwise hide it again""" |
795 |
|
name = "legend" |
796 |
|
dialog = self.FindRegisteredDock(name) |
797 |
|
|
798 |
|
if dialog is None: |
799 |
|
dialog = self.CreateDock(name, -1, _("Legend"), wxLAYOUT_LEFT) |
800 |
|
legend.LegendPanel(dialog, None, self) |
801 |
|
dialog.Dock() |
802 |
|
dialog.GetPanel().SetMap(self.Map()) |
803 |
|
dialog.Show() |
804 |
|
else: |
805 |
|
dialog.Show(not dialog.IsShown()) |
806 |
|
|
807 |
|
def LegendShown(self): |
808 |
|
"""Return true iff the legend is currently open""" |
809 |
|
dialog = self.FindRegisteredDock("legend") |
810 |
|
return dialog is not None and dialog.IsShown() |
811 |
|
|
812 |
|
def TableOpen(self): |
813 |
|
dlg = wxFileDialog(self, _("Open Table"), |
814 |
|
self.application.Path("data"), "", |
815 |
|
_("DBF Files (*.dbf)") + "|*.dbf|" + |
816 |
|
#_("CSV Files (*.csv)") + "|*.csv|" + |
817 |
|
_("All Files (*.*)") + "|*.*", |
818 |
|
wxOPEN) |
819 |
|
if dlg.ShowModal() == wxID_OK: |
820 |
|
filename = dlg.GetPath() |
821 |
|
dlg.Destroy() |
822 |
|
try: |
823 |
|
table = self.application.session.OpenTableFile(filename) |
824 |
|
except IOError: |
825 |
|
# the layer couldn't be opened |
826 |
|
self.RunMessageBox(_("Open Table"), |
827 |
|
_("Can't open the file '%s'.") % filename) |
828 |
else: |
else: |
829 |
proj = None |
self.ShowTableView(table) |
830 |
map.SetProjection(proj) |
self.application.SetPath("data",filename) |
831 |
proj4Dlg.Destroy() |
|
832 |
|
def TableClose(self): |
833 |
|
tables = self.application.session.UnreferencedTables() |
834 |
|
|
835 |
|
lst = [(t.Title(), t) for t in tables] |
836 |
|
lst.sort() |
837 |
|
titles = [i[0] for i in lst] |
838 |
|
dlg = wxMultipleChoiceDialog(self, _("Pick the tables to close:"), |
839 |
|
_("Close Table"), titles, |
840 |
|
size = (400, 300), |
841 |
|
style = wxDEFAULT_DIALOG_STYLE | |
842 |
|
wxRESIZE_BORDER) |
843 |
|
if dlg.ShowModal() == wxID_OK: |
844 |
|
for i in dlg.GetValue(): |
845 |
|
self.application.session.RemoveTable(lst[i][1]) |
846 |
|
|
847 |
|
|
848 |
|
def TableShow(self): |
849 |
|
"""Offer a multi-selection dialog for tables to be displayed |
850 |
|
|
851 |
|
The windows for the selected tables are opened or brought to |
852 |
|
the front. |
853 |
|
""" |
854 |
|
tables = self.application.session.Tables() |
855 |
|
|
856 |
|
lst = [(t.Title(), t) for t in tables] |
857 |
|
lst.sort() |
858 |
|
titles = [i[0] for i in lst] |
859 |
|
dlg = wxMultipleChoiceDialog(self, _("Pick the table to show:"), |
860 |
|
_("Show Table"), titles, |
861 |
|
size = (400,300), |
862 |
|
style = wxDEFAULT_DIALOG_STYLE | |
863 |
|
wxRESIZE_BORDER) |
864 |
|
if (dlg.ShowModal() == wxID_OK): |
865 |
|
for i in dlg.GetValue(): |
866 |
|
# XXX: if the table belongs to a layer, open a |
867 |
|
# LayerTableFrame instead of QueryTableFrame |
868 |
|
self.ShowTableView(lst[i][1]) |
869 |
|
|
870 |
|
def TableJoin(self): |
871 |
|
dlg = JoinDialog(self, _("Join Tables"), self.application.session) |
872 |
|
dlg.ShowModal() |
873 |
|
|
874 |
|
def ShowTableView(self, table): |
875 |
|
"""Open a table view for the table and optionally""" |
876 |
|
name = "table_view%d" % id(table) |
877 |
|
dialog = self.get_open_dialog(name) |
878 |
|
if dialog is None: |
879 |
|
dialog = tableview.QueryTableFrame(self, name, |
880 |
|
_("Table: %s") % table.Title(), |
881 |
|
table) |
882 |
|
self.add_dialog(name, dialog) |
883 |
|
dialog.Show(True) |
884 |
|
dialog.Raise() |
885 |
|
|
886 |
|
def TableRename(self): |
887 |
|
"""Let the user rename a table""" |
888 |
|
|
889 |
|
# First, let the user select a table |
890 |
|
tables = self.application.session.Tables() |
891 |
|
lst = [(t.Title(), t) for t in tables] |
892 |
|
lst.sort() |
893 |
|
titles = [i[0] for i in lst] |
894 |
|
dlg = wxMultipleChoiceDialog(self, _("Pick the table to rename:"), |
895 |
|
_("Rename Table"), titles, |
896 |
|
size = (400,300), |
897 |
|
style = wxDEFAULT_DIALOG_STYLE | |
898 |
|
wxRESIZE_BORDER) |
899 |
|
if (dlg.ShowModal() == wxID_OK): |
900 |
|
to_rename = [lst[i][1] for i in dlg.GetValue()] |
901 |
|
dlg.Destroy() |
902 |
|
else: |
903 |
|
to_rename = [] |
904 |
|
|
905 |
|
# Second, let the user rename the layers |
906 |
|
for table in to_rename: |
907 |
|
dlg = wxTextEntryDialog(self, _("Table Title:"), _("Rename Table"), |
908 |
|
table.Title()) |
909 |
|
try: |
910 |
|
if dlg.ShowModal() == wxID_OK: |
911 |
|
title = dlg.GetValue() |
912 |
|
if title != "": |
913 |
|
table.SetTitle(title) |
914 |
|
|
915 |
|
# Make sure the session is marked as modified. |
916 |
|
# FIXME: This should be handled automatically, |
917 |
|
# but that requires more changes to the tables |
918 |
|
# than I have time for currently. |
919 |
|
self.application.session.changed() |
920 |
|
finally: |
921 |
|
dlg.Destroy() |
922 |
|
|
923 |
|
|
924 |
def ZoomInTool(self): |
def ZoomInTool(self): |
925 |
self.canvas.ZoomInTool() |
self.canvas.ZoomInTool() |
940 |
def FullExtent(self): |
def FullExtent(self): |
941 |
self.canvas.FitMapToWindow() |
self.canvas.FitMapToWindow() |
942 |
|
|
943 |
|
def FullLayerExtent(self): |
944 |
|
self.canvas.FitLayerToWindow(self.current_layer()) |
945 |
|
|
946 |
|
def FullSelectionExtent(self): |
947 |
|
self.canvas.FitSelectedToWindow() |
948 |
|
|
949 |
|
def ExportMap(self): |
950 |
|
self.canvas.Export() |
951 |
|
|
952 |
def PrintMap(self): |
def PrintMap(self): |
953 |
self.canvas.Print() |
self.canvas.Print() |
954 |
|
|
955 |
def identify_view_on_demand(self, layer, shape): |
def RenameMap(self): |
956 |
|
dlg = wxTextEntryDialog(self, _("Map Title:"), _("Rename Map"), |
957 |
|
self.Map().Title()) |
958 |
|
if dlg.ShowModal() == wxID_OK: |
959 |
|
title = dlg.GetValue() |
960 |
|
if title != "": |
961 |
|
self.Map().SetTitle(title) |
962 |
|
|
963 |
|
dlg.Destroy() |
964 |
|
|
965 |
|
def RenameLayer(self): |
966 |
|
"""Let the user rename the currently selected layer""" |
967 |
|
layer = self.current_layer() |
968 |
|
if layer is not None: |
969 |
|
dlg = wxTextEntryDialog(self, _("Layer Title:"), _("Rename Layer"), |
970 |
|
layer.Title()) |
971 |
|
try: |
972 |
|
if dlg.ShowModal() == wxID_OK: |
973 |
|
title = dlg.GetValue() |
974 |
|
if title != "": |
975 |
|
layer.SetTitle(title) |
976 |
|
finally: |
977 |
|
dlg.Destroy() |
978 |
|
|
979 |
|
def identify_view_on_demand(self, layer, shapes): |
980 |
|
"""Subscribed to the canvas' SHAPES_SELECTED message |
981 |
|
|
982 |
|
If the current tool is the identify tool, at least one shape is |
983 |
|
selected and the identify dialog is not shown, show the dialog. |
984 |
|
""" |
985 |
|
# If the selection has become empty we don't need to do |
986 |
|
# anything. Otherwise it could happen that the dialog was popped |
987 |
|
# up when the selection became empty, e.g. when a new selection |
988 |
|
# is opened while the identify tool is active and dialog had |
989 |
|
# been closed |
990 |
|
if not shapes: |
991 |
|
return |
992 |
|
|
993 |
name = "identify_view" |
name = "identify_view" |
994 |
if self.canvas.CurrentTool() == "IdentifyTool": |
if self.canvas.CurrentTool() == "IdentifyTool": |
995 |
if not self.dialog_open(name): |
if not self.dialog_open(name): |
996 |
dialog = identifyview.IdentifyView(self, self.interactor, name) |
dialog = identifyview.IdentifyView(self, name) |
997 |
self.add_dialog(name, dialog) |
self.add_dialog(name, dialog) |
998 |
dialog.Show(true) |
dialog.Show(True) |
999 |
else: |
else: |
1000 |
# FIXME: bring dialog to front? |
# FIXME: bring dialog to front? |
1001 |
pass |
pass |
1002 |
|
|
1003 |
|
def title_changed(self, map): |
1004 |
|
"""Subscribed to the canvas' TITLE_CHANGED messages""" |
1005 |
|
self.update_title() |
1006 |
|
|
1007 |
|
def update_title(self): |
1008 |
|
"""Update the window's title according to it's current state. |
1009 |
|
|
1010 |
|
In this default implementation the title is 'Thuban - ' followed |
1011 |
|
by the map's title or simply 'Thuban' if there is not map. |
1012 |
|
Derived classes should override this method to get different |
1013 |
|
titles. |
1014 |
|
|
1015 |
|
This method is called automatically by other methods when the |
1016 |
|
title may have to change. For the methods implemented in this |
1017 |
|
class this usually only means that a different map has been set |
1018 |
|
or the current map's title has changed. |
1019 |
|
""" |
1020 |
|
map = self.Map() |
1021 |
|
if map is not None: |
1022 |
|
title = _("Thuban - %s") % (map.Title(),) |
1023 |
|
else: |
1024 |
|
title = _("Thuban") |
1025 |
|
self.SetTitle(title) |
1026 |
|
|
1027 |
|
|
1028 |
# |
# |
1029 |
# Define all the commands available in the main window |
# Define all the commands available in the main window |
1030 |
# |
# |
1036 |
apply(getattr(context.mainwindow, methodname), args) |
apply(getattr(context.mainwindow, methodname), args) |
1037 |
|
|
1038 |
def _method_command(name, title, method, helptext = "", |
def _method_command(name, title, method, helptext = "", |
1039 |
icon = "", sensitive = None): |
icon = "", sensitive = None, checked = None): |
1040 |
"""Add a command implemented by a method of the mainwindow object""" |
"""Add a command implemented by a method of the mainwindow object""" |
1041 |
registry.Add(Command(name, title, call_method, args=(method,), |
registry.Add(Command(name, title, call_method, args=(method,), |
1042 |
helptext = helptext, icon = icon, |
helptext = helptext, icon = icon, |
1043 |
sensitive = sensitive)) |
sensitive = sensitive, checked = checked)) |
1044 |
|
|
1045 |
def _tool_command(name, title, method, toolname, helptext = "", |
def make_check_current_tool(toolname): |
1046 |
icon = ""): |
"""Return a function that tests if the currently active tool is toolname |
1047 |
"""Add a tool command""" |
|
1048 |
|
The returned function can be called with the context and returns |
1049 |
|
true iff the currently active tool's name is toolname. It's directly |
1050 |
|
usable as the 'checked' callback of a command. |
1051 |
|
""" |
1052 |
def check_current_tool(context, name=toolname): |
def check_current_tool(context, name=toolname): |
1053 |
return context.mainwindow.canvas.CurrentTool() == name |
return context.mainwindow.canvas.CurrentTool() == name |
1054 |
registry.Add(Command(name, title, call_method, args=(method,), |
return check_current_tool |
1055 |
helptext = helptext, icon = icon, |
|
1056 |
checked = check_current_tool)) |
def _tool_command(name, title, method, toolname, helptext = "", |
1057 |
|
icon = "", sensitive = None): |
1058 |
|
"""Add a tool command""" |
1059 |
|
registry.Add(ToolCommand(name, title, call_method, args=(method,), |
1060 |
|
helptext = helptext, icon = icon, |
1061 |
|
checked = make_check_current_tool(toolname), |
1062 |
|
sensitive = sensitive)) |
1063 |
|
|
1064 |
def _has_selected_layer(context): |
def _has_selected_layer(context): |
1065 |
"""Return true if a layer is selected in the context""" |
"""Return true if a layer is selected in the context""" |
1066 |
return context.mainwindow.has_selected_layer() |
return context.mainwindow.has_selected_layer() |
1067 |
|
|
1068 |
|
def _has_selected_layer_visible(context): |
1069 |
|
"""Return true if a layer is selected in the context which is |
1070 |
|
visible.""" |
1071 |
|
if context.mainwindow.has_selected_layer(): |
1072 |
|
layer = context.mainwindow.current_layer() |
1073 |
|
if layer.Visible(): return True |
1074 |
|
return False |
1075 |
|
|
1076 |
|
def _has_selected_shape_layer(context): |
1077 |
|
"""Return true if a shape layer is selected in the context""" |
1078 |
|
return context.mainwindow.has_selected_shape_layer() |
1079 |
|
|
1080 |
|
def _has_selected_shapes(context): |
1081 |
|
"""Return true if a layer is selected in the context""" |
1082 |
|
return context.mainwindow.has_selected_shapes() |
1083 |
|
|
1084 |
|
def _can_remove_layer(context): |
1085 |
|
return context.mainwindow.CanRemoveLayer() |
1086 |
|
|
1087 |
|
def _has_tree_window_shown(context): |
1088 |
|
"""Return true if the tree window is shown""" |
1089 |
|
return context.mainwindow.SessionTreeShown() |
1090 |
|
|
1091 |
|
def _has_visible_map(context): |
1092 |
|
"""Return true iff theres a visible map in the mainwindow. |
1093 |
|
|
1094 |
|
A visible map is a map with at least one visible layer.""" |
1095 |
|
map = context.mainwindow.Map() |
1096 |
|
if map is not None: |
1097 |
|
for layer in map.Layers(): |
1098 |
|
if layer.Visible(): |
1099 |
|
return True |
1100 |
|
return False |
1101 |
|
|
1102 |
|
def _has_legend_shown(context): |
1103 |
|
"""Return true if the legend window is shown""" |
1104 |
|
return context.mainwindow.LegendShown() |
1105 |
|
|
1106 |
|
def _has_gdal_support(context): |
1107 |
|
"""Return True if the GDAL is available""" |
1108 |
|
return Thuban.Model.resource.has_gdal_support() |
1109 |
|
|
1110 |
|
def _has_dbconnections(context): |
1111 |
|
"""Return whether the the session has database connections""" |
1112 |
|
return context.session.HasDBConnections() |
1113 |
|
|
1114 |
|
def _has_postgis_support(context): |
1115 |
|
return has_postgis_support() |
1116 |
|
|
1117 |
|
|
1118 |
# File menu |
# File menu |
1119 |
_method_command("new_session", "&New Session", "NewSession") |
_method_command("new_session", _("&New Session"), "NewSession", |
1120 |
_method_command("open_session", "&Open Session", "OpenSession") |
helptext = _("Start a new session")) |
1121 |
_method_command("save_session", "&Save Session", "SaveSession") |
_method_command("open_session", _("&Open Session..."), "OpenSession", |
1122 |
_method_command("save_session_as", "Save Session &As", "SaveSessionAs") |
helptext = _("Open a session file")) |
1123 |
_method_command("show_session_tree", "Show Session &Tree", "ShowSessionTree") |
_method_command("save_session", _("&Save Session"), "SaveSession", |
1124 |
_method_command("exit", "&Exit", "Exit") |
helptext =_("Save this session to the file it was opened from")) |
1125 |
|
_method_command("save_session_as", _("Save Session &As..."), "SaveSessionAs", |
1126 |
|
helptext = _("Save this session to a new file")) |
1127 |
|
_method_command("toggle_session_tree", _("Session &Tree"), "ToggleSessionTree", |
1128 |
|
checked = _has_tree_window_shown, |
1129 |
|
helptext = _("Toggle on/off the session tree analysis window")) |
1130 |
|
_method_command("toggle_legend", _("Legend"), "ToggleLegend", |
1131 |
|
checked = _has_legend_shown, |
1132 |
|
helptext = _("Toggle Legend on/off")) |
1133 |
|
_method_command("database_management", _("&Database Connections..."), |
1134 |
|
"DatabaseManagement", |
1135 |
|
sensitive = _has_postgis_support) |
1136 |
|
_method_command("exit", _("E&xit"), "Exit", |
1137 |
|
helptext = _("Finish working with Thuban")) |
1138 |
|
|
1139 |
# Help menu |
# Help menu |
1140 |
_method_command("help_about", "&About", "About") |
_method_command("help_about", _("&About..."), "About", |
1141 |
|
helptext = _("Info about Thuban authors, version and modules")) |
1142 |
|
|
1143 |
|
|
1144 |
# Map menu |
# Map menu |
1145 |
_method_command("map_projection", "Pro&jection", "Projection") |
_method_command("map_projection", _("Pro&jection..."), "MapProjection", |
1146 |
|
helptext = _("Set or change the map projection")) |
1147 |
|
|
1148 |
_tool_command("map_zoom_in_tool", "&Zoom in", "ZoomInTool", "ZoomInTool", |
_tool_command("map_zoom_in_tool", _("&Zoom in"), "ZoomInTool", "ZoomInTool", |
1149 |
helptext = "Switch to map-mode 'zoom-in'", icon = "zoom_in") |
helptext = _("Switch to map-mode 'zoom-in'"), icon = "zoom_in", |
1150 |
_tool_command("map_zoom_out_tool", "Zoom &out", "ZoomOutTool", "ZoomOutTool", |
sensitive = _has_visible_map) |
1151 |
helptext = "Switch to map-mode 'zoom-out'", icon = "zoom_out") |
_tool_command("map_zoom_out_tool", _("Zoom &out"), "ZoomOutTool", "ZoomOutTool", |
1152 |
_tool_command("map_pan_tool", "&Pan", "PanTool", "PanTool", |
helptext = _("Switch to map-mode 'zoom-out'"), icon = "zoom_out", |
1153 |
helptext = "Switch to map-mode 'pan'", icon = "pan") |
sensitive = _has_visible_map) |
1154 |
_tool_command("map_identify_tool", "&Identify", "IdentifyTool", "IdentifyTool", |
_tool_command("map_pan_tool", _("&Pan"), "PanTool", "PanTool", |
1155 |
helptext = "Switch to map-mode 'identify'", icon = "identify") |
helptext = _("Switch to map-mode 'pan'"), icon = "pan", |
1156 |
_tool_command("map_label_tool", "&Label", "LabelTool", "LabelTool", |
sensitive = _has_visible_map) |
1157 |
helptext = "Add/Remove labels", icon = "label") |
_tool_command("map_identify_tool", _("&Identify"), "IdentifyTool", |
1158 |
_method_command("map_full_extent", "&Full extent", "FullExtent", |
"IdentifyTool", |
1159 |
helptext = "Full Extent", icon = "fullextent") |
helptext = _("Switch to map-mode 'identify'"), icon = "identify", |
1160 |
_method_command("map_print", "Prin&t", "PrintMap", helptext = "Print the map") |
sensitive = _has_visible_map) |
1161 |
|
_tool_command("map_label_tool", _("&Label"), "LabelTool", "LabelTool", |
1162 |
|
helptext = _("Add/Remove labels"), icon = "label", |
1163 |
|
sensitive = _has_visible_map) |
1164 |
|
_method_command("map_full_extent", _("&Full extent"), "FullExtent", |
1165 |
|
helptext = _("Zoom to the full map extent"), icon = "fullextent", |
1166 |
|
sensitive = _has_visible_map) |
1167 |
|
_method_command("layer_full_extent", _("&Full layer extent"), "FullLayerExtent", |
1168 |
|
helptext = _("Zoom to the full layer extent"), |
1169 |
|
icon = "fulllayerextent", sensitive = _has_selected_layer) |
1170 |
|
_method_command("selected_full_extent", _("&Full selection extent"), |
1171 |
|
"FullSelectionExtent", |
1172 |
|
helptext = _("Zoom to the full selection extent"), |
1173 |
|
icon = "fullselextent", sensitive = _has_selected_shapes) |
1174 |
|
_method_command("map_export", _("E&xport"), "ExportMap", |
1175 |
|
helptext = _("Export the map to file")) |
1176 |
|
_method_command("map_print", _("Prin&t"), "PrintMap", |
1177 |
|
helptext = _("Print the map")) |
1178 |
|
_method_command("map_rename", _("&Rename..."), "RenameMap", |
1179 |
|
helptext = _("Rename the map")) |
1180 |
|
_method_command("layer_add", _("&Add Layer..."), "AddLayer", |
1181 |
|
helptext = _("Add a new layer to the map")) |
1182 |
|
_method_command("rasterlayer_add", _("&Add Image Layer..."), "AddRasterLayer", |
1183 |
|
helptext = _("Add a new image layer to the map"), |
1184 |
|
sensitive = _has_gdal_support) |
1185 |
|
_method_command("layer_add_db", _("Add &Database Layer..."), "AddDBLayer", |
1186 |
|
helptext = _("Add a new database layer to active map"), |
1187 |
|
sensitive = _has_dbconnections) |
1188 |
|
_method_command("layer_remove", _("&Remove Layer"), "RemoveLayer", |
1189 |
|
helptext = _("Remove selected layer"), |
1190 |
|
sensitive = _can_remove_layer) |
1191 |
|
|
1192 |
# Layer menu |
# Layer menu |
1193 |
_method_command("layer_add", "&Add Layer", "AddLayer", |
_method_command("layer_projection", _("Pro&jection..."), "LayerProjection", |
1194 |
helptext = "Add a new layer to active map") |
sensitive = _has_selected_layer, |
1195 |
_method_command("layer_remove", "&Remove Layer", "RemoveLayer", |
helptext = _("Specify projection for selected layer")) |
1196 |
helptext = "Remove selected layer(s)", |
_method_command("layer_duplicate", _("&Duplicate"), "DuplicateLayer", |
1197 |
sensitive = _has_selected_layer) |
helptext = _("Duplicate selected layer"), |
1198 |
_method_command("layer_fill_color", "&Fill Color", "LayerFillColor", |
sensitive = lambda context: context.mainwindow.CanDuplicateLayer()) |
1199 |
helptext = "Set the fill color of selected layer(s)", |
_method_command("layer_rename", _("Re&name ..."), "RenameLayer", |
1200 |
|
helptext = _("Rename selected layer"), |
1201 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_layer) |
1202 |
_method_command("layer_transparent_fill", "&Transparent Fill", |
_method_command("layer_raise", _("&Raise"), "RaiseLayer", |
1203 |
"LayerTransparentFill", |
helptext = _("Raise selected layer"), |
|
helptext = "Do not fill the selected layer(s)", |
|
1204 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_layer) |
1205 |
_method_command("layer_outline_color", "&Outline Color", "LayerOutlineColor", |
_method_command("layer_lower", _("&Lower"), "LowerLayer", |
1206 |
helptext = "Set the outline color of selected layer(s)", |
helptext = _("Lower selected layer"), |
1207 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_layer) |
1208 |
_method_command("layer_no_outline", "&No Outline", "LayerNoOutline", |
_method_command("layer_show", _("&Show"), "ShowLayer", |
1209 |
helptext = "Do not draw the outline of the selected layer(s)", |
helptext = _("Make selected layer visible"), |
1210 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_layer) |
1211 |
_method_command("layer_raise", "&Raise", "RaiseLayer", |
_method_command("layer_hide", _("&Hide"), "HideLayer", |
1212 |
helptext = "Raise selected layer(s)", |
helptext = _("Make selected layer unvisible"), |
1213 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_layer) |
1214 |
_method_command("layer_lower", "&Lower", "LowerLayer", |
_method_command("layer_show_table", _("Show Ta&ble"), "LayerShowTable", |
1215 |
helptext = "Lower selected layer(s)", |
helptext = _("Show the selected layer's table"), |
1216 |
|
sensitive = _has_selected_shape_layer) |
1217 |
|
_method_command("layer_properties", _("&Properties..."), "LayerEditProperties", |
1218 |
|
sensitive = _has_selected_layer, |
1219 |
|
helptext = _("Edit the properties of the selected layer")) |
1220 |
|
_method_command("layer_jointable", _("&Join Table..."), "LayerJoinTable", |
1221 |
|
sensitive = _has_selected_shape_layer, |
1222 |
|
helptext = _("Join and attach a table to the selected layer")) |
1223 |
|
|
1224 |
|
# further layer methods: |
1225 |
|
_method_command("layer_to_top", _("&Top"), "LayerToTop", |
1226 |
|
helptext = _("Put selected layer to the top"), |
1227 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_layer) |
1228 |
_method_command("layer_show", "&Show", "ShowLayer", |
_method_command("layer_to_bottom", _("&Bottom"), "LayerToBottom", |
1229 |
helptext = "Make selected layer(s) visible", |
helptext = _("Put selected layer to the bottom"), |
1230 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_layer) |
1231 |
_method_command("layer_hide", "&Hide", "HideLayer", |
_method_command("layer_visibility", _("&Visible"), "ToggleLayerVisibility", |
1232 |
helptext = "Make selected layer(s) unvisible", |
checked = _has_selected_layer_visible, |
1233 |
sensitive = _has_selected_layer) |
helptext = _("Toggle visibility of selected layer"), |
|
_method_command("layer_show_table", "Show Ta&ble", "LayerShowTable", |
|
|
helptext = "Show the selected layer's table", |
|
1234 |
sensitive = _has_selected_layer) |
sensitive = _has_selected_layer) |
1235 |
|
|
1236 |
|
def _can_unjoin(context): |
1237 |
|
"""Return whether the Layer/Unjoin command can be executed. |
1238 |
|
|
1239 |
# the menu structure |
This is the case if a layer is selected and that layer has a |
1240 |
main_menu = Menu("<main>", "<main>", |
shapestore that has an original shapestore. |
1241 |
[Menu("file", "&File", |
""" |
1242 |
["new_session", "open_session", None, |
layer = context.mainwindow.SelectedLayer() |
1243 |
"save_session", "save_session_as", None, |
if layer is None: |
1244 |
"show_session_tree", None, |
return 0 |
1245 |
"exit"]), |
getstore = getattr(layer, "ShapeStore", None) |
1246 |
Menu("map", "&Map", |
if getstore is not None: |
1247 |
["layer_add", "layer_remove", |
return getstore().OrigShapeStore() is not None |
1248 |
|
else: |
1249 |
|
return 0 |
1250 |
|
_method_command("layer_unjointable", _("&Unjoin Table..."), "LayerUnjoinTable", |
1251 |
|
sensitive = _can_unjoin, |
1252 |
|
helptext = _("Undo the last join operation")) |
1253 |
|
|
1254 |
|
|
1255 |
|
def _has_tables(context): |
1256 |
|
return bool(context.session.Tables()) |
1257 |
|
|
1258 |
|
# Table menu |
1259 |
|
_method_command("table_open", _("&Open..."), "TableOpen", |
1260 |
|
helptext = _("Open a DBF-table from a file")) |
1261 |
|
_method_command("table_close", _("&Close..."), "TableClose", |
1262 |
|
sensitive = lambda context: bool(context.session.UnreferencedTables()), |
1263 |
|
helptext = _("Close one or more tables from a list")) |
1264 |
|
_method_command("table_rename", _("&Rename..."), "TableRename", |
1265 |
|
sensitive = _has_tables, |
1266 |
|
helptext = _("Rename one or more tables")) |
1267 |
|
_method_command("table_show", _("&Show..."), "TableShow", |
1268 |
|
sensitive = _has_tables, |
1269 |
|
helptext = _("Show one or more tables in a dialog")) |
1270 |
|
_method_command("table_join", _("&Join..."), "TableJoin", |
1271 |
|
sensitive = _has_tables, |
1272 |
|
helptext = _("Join two tables creating a new one")) |
1273 |
|
|
1274 |
|
# Export only under Windows ... |
1275 |
|
map_menu = ["layer_add", "layer_add_db", "rasterlayer_add", "layer_remove", |
1276 |
None, |
None, |
1277 |
|
"map_rename", |
1278 |
"map_projection", |
"map_projection", |
1279 |
None, |
None, |
1280 |
"map_zoom_in_tool", "map_zoom_out_tool", |
"map_zoom_in_tool", "map_zoom_out_tool", |
1281 |
"map_pan_tool", "map_identify_tool", "map_label_tool", |
"map_pan_tool", |
|
None, |
|
1282 |
"map_full_extent", |
"map_full_extent", |
1283 |
|
"layer_full_extent", |
1284 |
|
"selected_full_extent", |
1285 |
|
None, |
1286 |
|
"map_identify_tool", "map_label_tool", |
1287 |
None, |
None, |
1288 |
"map_print"]), |
"toggle_legend", |
1289 |
Menu("layer", "&Layer", |
None] |
1290 |
["layer_fill_color", "layer_transparent_fill", |
if wxPlatform == '__WXMSW__': |
1291 |
"layer_outline_color", "layer_no_outline", |
map_menu.append("map_export") |
1292 |
|
map_menu.append("map_print") |
1293 |
|
|
1294 |
|
# the menu structure |
1295 |
|
main_menu = Menu("<main>", "<main>", |
1296 |
|
[Menu("file", _("&File"), |
1297 |
|
["new_session", "open_session", None, |
1298 |
|
"save_session", "save_session_as", None, |
1299 |
|
"database_management", None, |
1300 |
|
"toggle_session_tree", None, |
1301 |
|
"exit"]), |
1302 |
|
Menu("map", _("&Map"), map_menu), |
1303 |
|
Menu("layer", _("&Layer"), |
1304 |
|
["layer_rename", "layer_duplicate", |
1305 |
None, |
None, |
1306 |
"layer_raise", "layer_lower", |
"layer_raise", "layer_lower", |
1307 |
None, |
None, |
1308 |
"layer_show", "layer_hide", |
"layer_show", "layer_hide", |
1309 |
None, |
None, |
1310 |
"layer_show_table"]), |
"layer_projection", |
1311 |
Menu("help", "&Help", |
None, |
1312 |
|
"layer_show_table", |
1313 |
|
"layer_jointable", |
1314 |
|
"layer_unjointable", |
1315 |
|
None, |
1316 |
|
"layer_properties"]), |
1317 |
|
Menu("table", _("&Table"), |
1318 |
|
["table_open", "table_close", "table_rename", |
1319 |
|
None, |
1320 |
|
"table_show", |
1321 |
|
None, |
1322 |
|
"table_join"]), |
1323 |
|
Menu("help", _("&Help"), |
1324 |
["help_about"])]) |
["help_about"])]) |
1325 |
|
|
1326 |
# the main toolbar |
# the main toolbar |
1327 |
|
|
1328 |
main_toolbar = Menu("<toolbar>", "<toolbar>", |
main_toolbar = Menu("<toolbar>", "<toolbar>", |
1329 |
["map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool", |
["map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool", |
1330 |
"map_identify_tool", "map_label_tool", "map_full_extent"]) |
"map_full_extent", |
1331 |
|
"layer_full_extent", |
1332 |
|
"selected_full_extent", |
1333 |
|
None, |
1334 |
|
"map_identify_tool", "map_label_tool"]) |
1335 |
|
|