/[thuban]/branches/WIP-pyshapelib-bramz/Thuban/UI/mainwindow.py
ViewVC logotype

Contents of /branches/WIP-pyshapelib-bramz/Thuban/UI/mainwindow.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2017 - (show annotations)
Wed Dec 3 18:43:03 2003 UTC (21 years, 3 months ago) by bh
Original Path: trunk/thuban/Thuban/UI/mainwindow.py
File MIME type: text/x-python
File size: 46559 byte(s)
(MainWindow.Unsubscribe): Deal with
publishers that are wx objects and may have been destroyed by wx
already. Fixes RT #2242.

1 # Copyright (C) 2001, 2002, 2003 by Intevation GmbH
2 # Authors:
3 # Jan-Oliver Wagner <[email protected]>
4 # Bernhard Herzog <[email protected]>
5 # Frank Koormann <[email protected]>
6 #
7 # This program is free software under the GPL (>=v2)
8 # Read the file COPYING coming with Thuban for details.
9
10 """
11 The main window
12 """
13
14 __version__ = "$Revision$"
15 # $Source$
16 # $Id$
17
18 import os
19 import copy
20
21 from wxPython.wx import *
22
23 import Thuban
24
25 from Thuban import _
26 from Thuban.Model.messages import TITLE_CHANGED
27 from Thuban.Model.session import create_empty_session
28 from Thuban.Model.layer import Layer, RasterLayer
29 from Thuban.Model.postgisdb import PostGISShapeStore, has_postgis_support
30 # 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
36 import tree
37 import tableview, identifyview
38 from Thuban.UI.classifier import Classifier
39 import legend
40 from menu import Menu
41
42 from context import Context
43 from command import registry, Command, ToolCommand
44 from messages import LAYER_SELECTED, SHAPES_SELECTED, VIEW_POSITION, \
45 MAP_REPLACED
46 from about import About
47
48 from Thuban.UI.dock import DockFrame
49 from Thuban.UI.join import JoinDialog
50 from Thuban.UI.dbdialog import DBFrame, DBDialog, ChooseDBTableDialog
51 import resource
52 import Thuban.Model.resource
53
54 import projdialog
55
56
57 class MainWindow(DockFrame):
58
59 # Some messages that can be subscribed/unsubscribed directly through
60 # the MapCanvas come in fact from other objects. This is a map to
61 # map those messages to the names of the instance variables they
62 # actually come from. This delegation is implemented in the
63 # Subscribe and unsubscribed methods
64 delegated_messages = {LAYER_SELECTED: "canvas",
65 SHAPES_SELECTED: "canvas",
66 MAP_REPLACED: "canvas"}
67
68 # Methods delegated to some instance variables. The delegation is
69 # implemented in the __getattr__ method.
70 delegated_methods = {"SelectLayer": "canvas",
71 "SelectShapes": "canvas",
72 "SelectedLayer": "canvas",
73 "SelectedShapes": "canvas",
74 }
75
76 def __init__(self, parent, ID, title, application, interactor,
77 initial_message = None, size = wxSize(-1, -1)):
78 DockFrame.__init__(self, parent, ID, title, wxDefaultPosition, size)
79 #wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, size)
80
81 self.application = application
82
83 self.CreateStatusBar()
84 if initial_message:
85 self.SetStatusText(initial_message)
86
87 self.identify_view = None
88
89 self.init_ids()
90
91 # creat the menubar from the main_menu description
92 self.SetMenuBar(self.build_menu_bar(main_menu))
93
94 # Similarly, create the toolbar from main_toolbar
95 toolbar = self.build_toolbar(main_toolbar)
96 # call Realize to make sure that the tools appear.
97 toolbar.Realize()
98
99
100 # Create the map canvas
101 canvas = view.MapCanvas(self, -1)
102 canvas.Subscribe(VIEW_POSITION, self.view_position_changed)
103 canvas.Subscribe(SHAPES_SELECTED, self.identify_view_on_demand)
104 self.canvas = canvas
105 self.canvas.Subscribe(TITLE_CHANGED, self.title_changed)
106
107 self.SetMainWindow(self.canvas)
108
109 self.SetAutoLayout(True)
110
111 self.init_dialogs()
112
113 self.ShowLegend()
114
115 EVT_CLOSE(self, self.OnClose)
116
117 def Subscribe(self, channel, *args):
118 """Subscribe a function to a message channel.
119
120 If channel is one of the delegated messages call the appropriate
121 object's Subscribe method. Otherwise do nothing.
122 """
123 if channel in self.delegated_messages:
124 object = getattr(self, self.delegated_messages[channel])
125 object.Subscribe(channel, *args)
126 else:
127 print "Trying to subscribe to unsupported channel %s" % channel
128
129 def Unsubscribe(self, channel, *args):
130 """Unsubscribe a function from a message channel.
131
132 If channel is one of the delegated messages call the appropriate
133 object's Unsubscribe method. Otherwise do nothing.
134 """
135 if channel in self.delegated_messages:
136 object = getattr(self, self.delegated_messages[channel])
137 try:
138 object.Unsubscribe(channel, *args)
139 except wxPyDeadObjectError:
140 # The object was a wxObject and has already been
141 # destroyed. Hopefully it has unsubscribed all its
142 # subscribers already so that it's OK if we do nothing
143 # here
144 pass
145
146 def __getattr__(self, attr):
147 """If attr is one of the delegated methods return that method
148
149 Otherwise raise AttributeError.
150 """
151 if attr in self.delegated_methods:
152 return getattr(getattr(self, self.delegated_methods[attr]), attr)
153 raise AttributeError(attr)
154
155 def init_ids(self):
156 """Initialize the ids"""
157 self.current_id = 6000
158 self.id_to_name = {}
159 self.name_to_id = {}
160 self.events_bound = {}
161
162 def get_id(self, name):
163 """Return the wxWindows id for the command named name.
164
165 Create a new one if there isn't one yet"""
166 ID = self.name_to_id.get(name)
167 if ID is None:
168 ID = self.current_id
169 self.current_id = self.current_id + 1
170 self.name_to_id[name] = ID
171 self.id_to_name[ID] = name
172 return ID
173
174 def bind_command_events(self, command, ID):
175 """Bind the necessary events for the given command and ID"""
176 if not self.events_bound.has_key(ID):
177 # the events haven't been bound yet
178 EVT_MENU(self, ID, self.invoke_command)
179 if command.IsDynamic():
180 EVT_UPDATE_UI(self, ID, self.update_command_ui)
181
182 def build_menu_bar(self, menudesc):
183 """Build and return the menu bar from the menu description"""
184 menu_bar = wxMenuBar()
185
186 for item in menudesc.items:
187 # here the items must all be Menu instances themselves
188 menu_bar.Append(self.build_menu(item), item.title)
189
190 return menu_bar
191
192 def build_menu(self, menudesc):
193 """Return a wxMenu built from the menu description menudesc"""
194 wxmenu = wxMenu()
195 last = None
196 for item in menudesc.items:
197 if item is None:
198 # a separator. Only add one if the last item was not a
199 # separator
200 if last is not None:
201 wxmenu.AppendSeparator()
202 elif isinstance(item, Menu):
203 # a submenu
204 wxmenu.AppendMenu(wxNewId(), item.title, self.build_menu(item))
205 else:
206 # must the name the name of a command
207 self.add_menu_command(wxmenu, item)
208 last = item
209 return wxmenu
210
211 def build_toolbar(self, toolbardesc):
212 """Build and return the main toolbar window from a toolbar description
213
214 The parameter should be an instance of the Menu class but it
215 should not contain submenus.
216 """
217 toolbar = self.CreateToolBar(wxTB_3DBUTTONS)
218
219 # set the size of the tools' bitmaps. Not needed on wxGTK, but
220 # on Windows, although it doesn't work very well there. It seems
221 # that only 16x16 icons are really supported on windows.
222 # We probably shouldn't hardwire the bitmap size here.
223 toolbar.SetToolBitmapSize(wxSize(24, 24))
224
225 for item in toolbardesc.items:
226 if item is None:
227 toolbar.AddSeparator()
228 else:
229 # assume it's a string.
230 self.add_toolbar_command(toolbar, item)
231
232 return toolbar
233
234 def add_menu_command(self, menu, name):
235 """Add the command with name name to the menu menu.
236
237 If name is None, add a separator.
238 """
239 if name is None:
240 menu.AppendSeparator()
241 else:
242 command = registry.Command(name)
243 if command is not None:
244 ID = self.get_id(name)
245 menu.Append(ID, command.Title(), command.HelpText(),
246 command.IsCheckCommand())
247 self.bind_command_events(command, ID)
248 else:
249 print _("Unknown command %s") % name
250
251 def add_toolbar_command(self, toolbar, name):
252 """Add the command with name name to the toolbar toolbar.
253
254 If name is None, add a separator.
255 """
256 # Assume that all toolbar commands are also menu commmands so
257 # that we don't have to add the event handlers here
258 if name is None:
259 toolbar.AddSeparator()
260 else:
261 command = registry.Command(name)
262 if command is not None:
263 ID = self.get_id(name)
264 bitmap = resource.GetBitmapResource(command.Icon(),
265 wxBITMAP_TYPE_XPM)
266 toolbar.AddTool(ID, bitmap,
267 shortHelpString = command.HelpText(),
268 isToggle = command.IsCheckCommand())
269 self.bind_command_events(command, ID)
270 else:
271 print _("Unknown command %s") % name
272
273 def Context(self):
274 """Return the context object for a command invoked from this window
275 """
276 return Context(self.application, self.application.Session(), self)
277
278 def invoke_command(self, event):
279 name = self.id_to_name.get(event.GetId())
280 if name is not None:
281 command = registry.Command(name)
282 command.Execute(self.Context())
283 else:
284 print _("Unknown command ID %d") % event.GetId()
285
286 def update_command_ui(self, event):
287 #print "update_command_ui", self.id_to_name[event.GetId()]
288 context = self.Context()
289 command = registry.Command(self.id_to_name[event.GetId()])
290 if command is not None:
291 sensitive = command.Sensitive(context)
292 event.Enable(sensitive)
293 if command.IsTool() and not sensitive and command.Checked(context):
294 # When a checked tool command is disabled deselect all
295 # tools. Otherwise the tool would remain active but it
296 # might lead to errors if the tools stays active. This
297 # problem occurred in GREAT-ER and this fixes it, but
298 # it's not clear to me whether this is really the best
299 # way to do it (BH, 20021206).
300 self.canvas.SelectTool(None)
301 event.SetText(command.DynText(context))
302 if command.IsCheckCommand():
303 event.Check(command.Checked(context))
304
305 def RunMessageBox(self, title, text, flags = wxOK | wxICON_INFORMATION):
306 """Run a modal message box with the given text, title and flags
307 and return the result"""
308 dlg = wxMessageDialog(self, text, title, flags)
309 dlg.CenterOnParent()
310 result = dlg.ShowModal()
311 dlg.Destroy()
312 return result
313
314 def init_dialogs(self):
315 """Initialize the dialog handling"""
316 # The mainwindow maintains a dict mapping names to open
317 # non-modal dialogs. The dialogs are put into this dict when
318 # they're created and removed when they're closed
319 self.dialogs = {}
320
321 def add_dialog(self, name, dialog):
322 if self.dialogs.has_key(name):
323 raise RuntimeError(_("The Dialog named %s is already open") % name)
324 self.dialogs[name] = dialog
325
326 def dialog_open(self, name):
327 return self.dialogs.has_key(name)
328
329 def remove_dialog(self, name):
330 del self.dialogs[name]
331
332 def get_open_dialog(self, name):
333 return self.dialogs.get(name)
334
335 def view_position_changed(self):
336 pos = self.canvas.CurrentPosition()
337 if pos is not None:
338 text = "(%10.10g, %10.10g)" % pos
339 else:
340 text = ""
341 self.set_position_text(text)
342
343 def set_position_text(self, text):
344 """Set the statusbar text showing the current position.
345
346 By default the text is shown in field 0 of the status bar.
347 Override this method in derived classes to put it into a
348 different field of the statusbar.
349 """
350 self.SetStatusText(text)
351
352 def save_modified_session(self, can_veto = 1):
353 """If the current session has been modified, ask the user
354 whether to save it and do so if requested. Return the outcome of
355 the dialog (either wxID_OK, wxID_CANCEL or wxID_NO). If the
356 dialog wasn't run return wxID_NO.
357
358 If the can_veto parameter is true (default) the dialog includes
359 a cancel button, otherwise not.
360 """
361 if self.application.session.WasModified():
362 flags = wxYES_NO | wxICON_QUESTION
363 if can_veto:
364 flags = flags | wxCANCEL
365 result = self.RunMessageBox(_("Exit"),
366 _("The session has been modified."
367 " Do you want to save it?"),
368 flags)
369 if result == wxID_YES:
370 self.SaveSession()
371 else:
372 result = wxID_NO
373 return result
374
375 def NewSession(self):
376 if self.save_modified_session() != wxID_CANCEL:
377 self.application.SetSession(create_empty_session())
378
379 def OpenSession(self):
380 if self.save_modified_session() != wxID_CANCEL:
381 dlg = wxFileDialog(self, _("Open Session"), ".", "",
382 "Thuban Session File (*.thuban)|*.thuban",
383 wxOPEN)
384 if dlg.ShowModal() == wxID_OK:
385 self.application.OpenSession(dlg.GetPath(),
386 self.run_db_param_dialog)
387 dlg.Destroy()
388
389 def run_db_param_dialog(self, parameters, message):
390 dlg = DBDialog(self, _("DB Connection Parameters"), parameters,
391 message)
392 return dlg.RunDialog()
393
394 def SaveSession(self):
395 if self.application.session.filename == None:
396 self.SaveSessionAs()
397 else:
398 self.application.SaveSession()
399
400 def SaveSessionAs(self):
401 dlg = wxFileDialog(self, _("Save Session As"), ".", "",
402 "Thuban Session File (*.thuban)|*.thuban",
403 wxSAVE|wxOVERWRITE_PROMPT)
404 if dlg.ShowModal() == wxID_OK:
405 self.application.session.SetFilename(dlg.GetPath())
406 self.application.SaveSession()
407 dlg.Destroy()
408
409 def Exit(self):
410 self.Close(False)
411
412 def OnClose(self, event):
413 result = self.save_modified_session(can_veto = event.CanVeto())
414 if result == wxID_CANCEL:
415 event.Veto()
416 else:
417 # FIXME: it would be better to tie the unsubscription to
418 # wx's destroy event, but that isn't implemented for wxGTK
419 # yet.
420 self.canvas.Unsubscribe(VIEW_POSITION, self.view_position_changed)
421 DockFrame.OnClose(self, event)
422 for dlg in self.dialogs.values():
423 dlg.Destroy()
424 self.canvas.Destroy()
425 self.Destroy()
426
427 def SetMap(self, map):
428 self.canvas.SetMap(map)
429 self.update_title()
430
431 dialog = self.FindRegisteredDock("legend")
432 if dialog is not None:
433 dialog.GetPanel().SetMap(self.Map())
434
435 def Map(self):
436 """Return the map displayed by this mainwindow"""
437
438 return self.canvas.Map()
439
440 def ToggleSessionTree(self):
441 """If the session tree is shown close it otherwise create a new tree"""
442 name = "session_tree"
443 dialog = self.get_open_dialog(name)
444 if dialog is None:
445 dialog = tree.SessionTreeView(self, self.application, name)
446 self.add_dialog(name, dialog)
447 dialog.Show(True)
448 else:
449 dialog.Close()
450
451 def SessionTreeShown(self):
452 """Return true iff the session tree is currently shown"""
453 return self.get_open_dialog("session_tree") is not None
454
455 def About(self):
456 dlg = About(self)
457 dlg.ShowModal()
458 dlg.Destroy()
459
460 def DatabaseManagement(self):
461 name = "dbmanagement"
462 dialog = self.get_open_dialog(name)
463 if dialog is None:
464 map = self.canvas.Map()
465 dialog = DBFrame(self, name, self.application.Session())
466 self.add_dialog(name, dialog)
467 dialog.Show()
468 dialog.Raise()
469
470 def AddLayer(self):
471 dlg = wxFileDialog(self, _("Select one or more data files"), ".", "",
472 _("Shapefiles (*.shp)") + "|*.shp;*.SHP|" +
473 _("All Files (*.*)") + "|*.*",
474 wxOPEN | wxMULTIPLE)
475 if dlg.ShowModal() == wxID_OK:
476 filenames = dlg.GetPaths()
477 for filename in filenames:
478 title = os.path.splitext(os.path.basename(filename))[0]
479 map = self.canvas.Map()
480 has_layers = map.HasLayers()
481 try:
482 store = self.application.Session().OpenShapefile(filename)
483 except IOError:
484 # the layer couldn't be opened
485 self.RunMessageBox(_("Add Layer"),
486 _("Can't open the file '%s'.")%filename)
487 else:
488 layer = Layer(title, store)
489 map.AddLayer(layer)
490 if not has_layers:
491 # if we're adding a layer to an empty map, fit the
492 # new map to the window
493 self.canvas.FitMapToWindow()
494 dlg.Destroy()
495
496 def AddRasterLayer(self):
497 dlg = wxFileDialog(self, _("Select an image file"), ".", "", "*.*",
498 wxOPEN)
499 if dlg.ShowModal() == wxID_OK:
500 filename = dlg.GetPath()
501 title = os.path.splitext(os.path.basename(filename))[0]
502 map = self.canvas.Map()
503 has_layers = map.HasLayers()
504 try:
505 layer = RasterLayer(title, filename)
506 except IOError:
507 # the layer couldn't be opened
508 self.RunMessageBox(_("Add Image Layer"),
509 _("Can't open the file '%s'.") % filename)
510 else:
511 map.AddLayer(layer)
512 if not has_layers:
513 # if we're adding a layer to an empty map, fit the
514 # new map to the window
515 self.canvas.FitMapToWindow()
516 dlg.Destroy()
517
518 def AddDBLayer(self):
519 """Add a layer read from a database"""
520 session = self.application.Session()
521 dlg = ChooseDBTableDialog(self, self.application.Session())
522
523 if dlg.ShowModal() == wxID_OK:
524 dbconn, dbtable = dlg.GetTable()
525 try:
526 title = str(dbtable)
527
528 # Chose the correct Interface for the database type
529 store = PostGISShapeStore(dbconn, dbtable)
530 session.AddShapeStore(store)
531 layer = Layer(title, store)
532 except:
533 # Some error occured while initializing the layer
534 self.RunMessageBox(_("Add Layer from database"),
535 _("Can't open the database table '%s'")
536 % dbtable)
537
538 map = self.canvas.Map()
539
540 has_layers = map.HasLayers()
541 map.AddLayer(layer)
542 if not has_layers:
543 self.canvas.FitMapToWindow()
544
545 dlg.Destroy()
546
547 def RemoveLayer(self):
548 layer = self.current_layer()
549 if layer is not None:
550 self.canvas.Map().RemoveLayer(layer)
551
552 def CanRemoveLayer(self):
553 """Return true if the currently selected layer can be deleted.
554
555 If no layer is selected return False.
556
557 The return value of this method determines whether the remove
558 layer command is sensitive in menu.
559 """
560 layer = self.current_layer()
561 if layer is not None:
562 return self.canvas.Map().CanRemoveLayer(layer)
563 return False
564
565 def RaiseLayer(self):
566 layer = self.current_layer()
567 if layer is not None:
568 self.canvas.Map().RaiseLayer(layer)
569
570 def LowerLayer(self):
571 layer = self.current_layer()
572 if layer is not None:
573 self.canvas.Map().LowerLayer(layer)
574
575 def current_layer(self):
576 """Return the currently selected layer.
577
578 If no layer is selected, return None
579 """
580 return self.canvas.SelectedLayer()
581
582 def has_selected_layer(self):
583 """Return true if a layer is currently selected"""
584 return self.canvas.HasSelectedLayer()
585
586 def has_selected_shapes(self):
587 """Return true if a shape is currently selected"""
588 return self.canvas.HasSelectedShapes()
589
590 def HideLayer(self):
591 layer = self.current_layer()
592 if layer is not None:
593 layer.SetVisible(0)
594
595 def ShowLayer(self):
596 layer = self.current_layer()
597 if layer is not None:
598 layer.SetVisible(1)
599
600 def DuplicateLayer(self):
601 """Ceate a new layer above the selected layer with the same shapestore
602 """
603 layer = self.current_layer()
604 if layer is not None and hasattr(layer, "ShapeStore"):
605 new_layer = Layer(_("Copy of `%s'") % layer.Title(),
606 layer.ShapeStore(),
607 projection = layer.GetProjection())
608 new_classification = copy.deepcopy(layer.GetClassification())
609 new_layer.SetClassification(new_classification)
610 self.Map().AddLayer(new_layer)
611
612 def CanDuplicateLayer(self):
613 """Return whether the DuplicateLayer method can create a duplicate"""
614 layer = self.current_layer()
615 return layer is not None and hasattr(layer, "ShapeStore")
616
617 def LayerShowTable(self):
618 layer = self.current_layer()
619 if layer is not None:
620 table = layer.ShapeStore().Table()
621 name = "table_view" + str(id(table))
622 dialog = self.get_open_dialog(name)
623 if dialog is None:
624 dialog = tableview.LayerTableFrame(self, name,
625 _("Layer Table: %s") % layer.Title(),
626 layer, table)
627 self.add_dialog(name, dialog)
628 dialog.Show(True)
629 else:
630 # FIXME: bring dialog to front here
631 pass
632
633 def MapProjection(self):
634
635 name = "map_projection"
636 dialog = self.get_open_dialog(name)
637
638 if dialog is None:
639 map = self.canvas.Map()
640 dialog = projdialog.ProjFrame(self, name,
641 _("Map Projection: %s") % map.Title(), map)
642 self.add_dialog(name, dialog)
643 dialog.Show()
644 dialog.Raise()
645
646 def LayerProjection(self):
647
648 layer = self.current_layer()
649
650 name = "layer_projection" + str(id(layer))
651 dialog = self.get_open_dialog(name)
652
653 if dialog is None:
654 map = self.canvas.Map()
655 dialog = projdialog.ProjFrame(self, name,
656 _("Layer Projection: %s") % layer.Title(), layer)
657 self.add_dialog(name, dialog)
658 dialog.Show()
659 dialog.Raise()
660
661 def LayerEditProperties(self):
662
663 #
664 # the menu option for this should only be available if there
665 # is a current layer, so we don't need to check if the
666 # current layer is None
667 #
668
669 layer = self.current_layer()
670 self.OpenLayerProperties(layer)
671
672 def OpenLayerProperties(self, layer, group = None):
673 name = "layer_properties" + str(id(layer))
674 dialog = self.get_open_dialog(name)
675
676 if dialog is None:
677 dialog = Classifier(self, name, self.Map(), layer, group)
678 self.add_dialog(name, dialog)
679 dialog.Show()
680 dialog.Raise()
681
682 def LayerJoinTable(self):
683 layer = self.canvas.SelectedLayer()
684 if layer is not None:
685 dlg = JoinDialog(self, _("Join Layer with Table"),
686 self.application.session,
687 layer = layer)
688 dlg.ShowModal()
689
690 def LayerUnjoinTable(self):
691 layer = self.canvas.SelectedLayer()
692 if layer is not None:
693 orig_store = layer.ShapeStore().OrigShapeStore()
694 if orig_store:
695 layer.SetShapeStore(orig_store)
696
697 def ShowLegend(self):
698 if not self.LegendShown():
699 self.ToggleLegend()
700
701 def ToggleLegend(self):
702 """Show the legend if it's not shown otherwise hide it again"""
703 name = "legend"
704 dialog = self.FindRegisteredDock(name)
705
706 if dialog is None:
707 dialog = self.CreateDock(name, -1, _("Legend"), wxLAYOUT_LEFT)
708 legend.LegendPanel(dialog, None, self)
709 dialog.Dock()
710 dialog.GetPanel().SetMap(self.Map())
711 dialog.Show()
712 else:
713 dialog.Show(not dialog.IsShown())
714
715 def LegendShown(self):
716 """Return true iff the legend is currently open"""
717 dialog = self.FindRegisteredDock("legend")
718 return dialog is not None and dialog.IsShown()
719
720 def TableOpen(self):
721 dlg = wxFileDialog(self, _("Open Table"), ".", "",
722 _("DBF Files (*.dbf)") + "|*.dbf|" +
723 #_("CSV Files (*.csv)") + "|*.csv|" +
724 _("All Files (*.*)") + "|*.*",
725 wxOPEN)
726 if dlg.ShowModal() == wxID_OK:
727 filename = dlg.GetPath()
728 dlg.Destroy()
729 try:
730 table = self.application.session.OpenTableFile(filename)
731 except IOError:
732 # the layer couldn't be opened
733 self.RunMessageBox(_("Open Table"),
734 _("Can't open the file '%s'.") % filename)
735 else:
736 self.ShowTableView(table)
737
738 def TableClose(self):
739 tables = self.application.session.UnreferencedTables()
740
741 lst = [(t.Title(), t) for t in tables]
742 lst.sort()
743 titles = [i[0] for i in lst]
744 dlg = wxMultipleChoiceDialog(self, _("Pick the tables to close:"),
745 _("Close Table"), titles,
746 size = (400, 300),
747 style = wxDEFAULT_DIALOG_STYLE |
748 wxRESIZE_BORDER)
749 if dlg.ShowModal() == wxID_OK:
750 for i in dlg.GetValue():
751 self.application.session.RemoveTable(lst[i][1])
752
753
754 def TableShow(self):
755 """Offer a multi-selection dialog for tables to be displayed
756
757 The windows for the selected tables are opened or brought to
758 the front.
759 """
760 tables = self.application.session.Tables()
761
762 lst = [(t.Title(), t) for t in tables]
763 lst.sort()
764 titles = [i[0] for i in lst]
765 dlg = wxMultipleChoiceDialog(self, _("Pick the table to show:"),
766 _("Show Table"), titles,
767 size = (400,300),
768 style = wxDEFAULT_DIALOG_STYLE |
769 wxRESIZE_BORDER)
770 if (dlg.ShowModal() == wxID_OK):
771 for i in dlg.GetValue():
772 # XXX: if the table belongs to a layer, open a
773 # LayerTableFrame instead of QueryTableFrame
774 self.ShowTableView(lst[i][1])
775
776 def TableJoin(self):
777 dlg = JoinDialog(self, _("Join Tables"), self.application.session)
778 dlg.ShowModal()
779
780 def ShowTableView(self, table):
781 """Open a table view for the table and optionally"""
782 name = "table_view%d" % id(table)
783 dialog = self.get_open_dialog(name)
784 if dialog is None:
785 dialog = tableview.QueryTableFrame(self, name,
786 _("Table: %s") % table.Title(),
787 table)
788 self.add_dialog(name, dialog)
789 dialog.Show(True)
790 dialog.Raise()
791
792 def TableRename(self):
793 """Let the user rename a table"""
794
795 # First, let the user select a table
796 tables = self.application.session.Tables()
797 lst = [(t.Title(), t) for t in tables]
798 lst.sort()
799 titles = [i[0] for i in lst]
800 dlg = wxMultipleChoiceDialog(self, _("Pick the table to rename:"),
801 _("Rename Table"), titles,
802 size = (400,300),
803 style = wxDEFAULT_DIALOG_STYLE |
804 wxRESIZE_BORDER)
805 if (dlg.ShowModal() == wxID_OK):
806 to_rename = [lst[i][1] for i in dlg.GetValue()]
807 dlg.Destroy()
808 else:
809 to_rename = []
810
811 # Second, let the user rename the layers
812 for table in to_rename:
813 dlg = wxTextEntryDialog(self, "Table Title: ", "Rename Table",
814 table.Title())
815 try:
816 if dlg.ShowModal() == wxID_OK:
817 title = dlg.GetValue()
818 if title != "":
819 table.SetTitle(title)
820
821 # Make sure the session is marked as modified.
822 # FIXME: This should be handled automatically,
823 # but that requires more changes to the tables
824 # than I have time for currently.
825 self.application.session.changed()
826 finally:
827 dlg.Destroy()
828
829
830 def ZoomInTool(self):
831 self.canvas.ZoomInTool()
832
833 def ZoomOutTool(self):
834 self.canvas.ZoomOutTool()
835
836 def PanTool(self):
837 self.canvas.PanTool()
838
839 def IdentifyTool(self):
840 self.canvas.IdentifyTool()
841 self.identify_view_on_demand(None, None)
842
843 def LabelTool(self):
844 self.canvas.LabelTool()
845
846 def FullExtent(self):
847 self.canvas.FitMapToWindow()
848
849 def FullLayerExtent(self):
850 self.canvas.FitLayerToWindow(self.current_layer())
851
852 def FullSelectionExtent(self):
853 self.canvas.FitSelectedToWindow()
854
855 def ExportMap(self):
856 self.canvas.Export()
857
858 def PrintMap(self):
859 self.canvas.Print()
860
861 def RenameMap(self):
862 dlg = wxTextEntryDialog(self, "Map Title: ", "Rename Map",
863 self.Map().Title())
864 if dlg.ShowModal() == wxID_OK:
865 title = dlg.GetValue()
866 if title != "":
867 self.Map().SetTitle(title)
868
869 dlg.Destroy()
870
871 def RenameLayer(self):
872 """Let the user rename the currently selected layer"""
873 layer = self.current_layer()
874 if layer is not None:
875 dlg = wxTextEntryDialog(self, "Layer Title: ", "Rename Layer",
876 layer.Title())
877 try:
878 if dlg.ShowModal() == wxID_OK:
879 title = dlg.GetValue()
880 if title != "":
881 layer.SetTitle(title)
882 finally:
883 dlg.Destroy()
884
885 def identify_view_on_demand(self, layer, shapes):
886 """Subscribed to the canvas' SHAPES_SELECTED message
887
888 If the current tool is the identify tool, at least one shape is
889 selected and the identify dialog is not shown, show the dialog.
890 """
891 # If the selection has become empty we don't need to do
892 # anything. Otherwise it could happen that the dialog was popped
893 # up when the selection became empty, e.g. when a new selection
894 # is opened while the identify tool is active and dialog had
895 # been closed
896 if not shapes:
897 return
898
899 name = "identify_view"
900 if self.canvas.CurrentTool() == "IdentifyTool":
901 if not self.dialog_open(name):
902 dialog = identifyview.IdentifyView(self, name)
903 self.add_dialog(name, dialog)
904 dialog.Show(True)
905 else:
906 # FIXME: bring dialog to front?
907 pass
908
909 def title_changed(self, map):
910 """Subscribed to the canvas' TITLE_CHANGED messages"""
911 self.update_title()
912
913 def update_title(self):
914 """Update the window's title according to it's current state.
915
916 In this default implementation the title is 'Thuban - ' followed
917 by the map's title or simply 'Thuban' if there is not map.
918 Derived classes should override this method to get different
919 titles.
920
921 This method is called automatically by other methods when the
922 title may have to change. For the methods implemented in this
923 class this usually only means that a different map has been set
924 or the current map's title has changed.
925 """
926 map = self.Map()
927 if map is not None:
928 title = _("Thuban - %s") % (map.Title(),)
929 else:
930 title = _("Thuban")
931 self.SetTitle(title)
932
933
934 #
935 # Define all the commands available in the main window
936 #
937
938
939 # Helper functions to define common command implementations
940 def call_method(context, methodname, *args):
941 """Call the mainwindow's method methodname with args *args"""
942 apply(getattr(context.mainwindow, methodname), args)
943
944 def _method_command(name, title, method, helptext = "",
945 icon = "", sensitive = None, checked = None):
946 """Add a command implemented by a method of the mainwindow object"""
947 registry.Add(Command(name, title, call_method, args=(method,),
948 helptext = helptext, icon = icon,
949 sensitive = sensitive, checked = checked))
950
951 def make_check_current_tool(toolname):
952 """Return a function that tests if the currently active tool is toolname
953
954 The returned function can be called with the context and returns
955 true iff the currently active tool's name is toolname. It's directly
956 usable as the 'checked' callback of a command.
957 """
958 def check_current_tool(context, name=toolname):
959 return context.mainwindow.canvas.CurrentTool() == name
960 return check_current_tool
961
962 def _tool_command(name, title, method, toolname, helptext = "",
963 icon = "", sensitive = None):
964 """Add a tool command"""
965 registry.Add(ToolCommand(name, title, call_method, args=(method,),
966 helptext = helptext, icon = icon,
967 checked = make_check_current_tool(toolname),
968 sensitive = sensitive))
969
970 def _has_selected_layer(context):
971 """Return true if a layer is selected in the context"""
972 return context.mainwindow.has_selected_layer()
973
974 def _has_selected_shapes(context):
975 """Return true if a layer is selected in the context"""
976 return context.mainwindow.has_selected_shapes()
977
978 def _can_remove_layer(context):
979 return context.mainwindow.CanRemoveLayer()
980
981 def _has_tree_window_shown(context):
982 """Return true if the tree window is shown"""
983 return context.mainwindow.SessionTreeShown()
984
985 def _has_visible_map(context):
986 """Return true iff theres a visible map in the mainwindow.
987
988 A visible map is a map with at least one visible layer."""
989 map = context.mainwindow.Map()
990 if map is not None:
991 for layer in map.Layers():
992 if layer.Visible():
993 return 1
994 return 0
995
996 def _has_legend_shown(context):
997 """Return true if the legend window is shown"""
998 return context.mainwindow.LegendShown()
999
1000 def _has_gdal_support(context):
1001 """Return True if the GDAL is available"""
1002 return Thuban.Model.resource.has_gdal_support()
1003
1004 def _has_dbconnections(context):
1005 """Return whether the the session has database connections"""
1006 return context.session.HasDBConnections()
1007
1008 def _has_postgis_support(context):
1009 return has_postgis_support()
1010
1011
1012 # File menu
1013 _method_command("new_session", _("&New Session"), "NewSession",
1014 helptext = _("Start a new session"))
1015 _method_command("open_session", _("&Open Session..."), "OpenSession",
1016 helptext = _("Open a session file"))
1017 _method_command("save_session", _("&Save Session"), "SaveSession",
1018 helptext =_("Save this session to the file it was opened from"))
1019 _method_command("save_session_as", _("Save Session &As..."), "SaveSessionAs",
1020 helptext = _("Save this session to a new file"))
1021 _method_command("toggle_session_tree", _("Session &Tree"), "ToggleSessionTree",
1022 checked = _has_tree_window_shown,
1023 helptext = _("Toggle on/off the session tree analysis window"))
1024 _method_command("toggle_legend", _("Legend"), "ToggleLegend",
1025 checked = _has_legend_shown,
1026 helptext = _("Toggle Legend on/off"))
1027 _method_command("database_management", _("&Database Connections..."),
1028 "DatabaseManagement",
1029 sensitive = _has_postgis_support)
1030 _method_command("exit", _("E&xit"), "Exit",
1031 helptext = _("Finish working with Thuban"))
1032
1033 # Help menu
1034 _method_command("help_about", _("&About..."), "About",
1035 helptext = _("Info about Thuban authors, version and modules"))
1036
1037
1038 # Map menu
1039 _method_command("map_projection", _("Pro&jection..."), "MapProjection",
1040 helptext = _("Set or change the map projection"))
1041
1042 _tool_command("map_zoom_in_tool", _("&Zoom in"), "ZoomInTool", "ZoomInTool",
1043 helptext = _("Switch to map-mode 'zoom-in'"), icon = "zoom_in",
1044 sensitive = _has_visible_map)
1045 _tool_command("map_zoom_out_tool", _("Zoom &out"), "ZoomOutTool", "ZoomOutTool",
1046 helptext = _("Switch to map-mode 'zoom-out'"), icon = "zoom_out",
1047 sensitive = _has_visible_map)
1048 _tool_command("map_pan_tool", _("&Pan"), "PanTool", "PanTool",
1049 helptext = _("Switch to map-mode 'pan'"), icon = "pan",
1050 sensitive = _has_visible_map)
1051 _tool_command("map_identify_tool", _("&Identify"), "IdentifyTool",
1052 "IdentifyTool",
1053 helptext = _("Switch to map-mode 'identify'"), icon = "identify",
1054 sensitive = _has_visible_map)
1055 _tool_command("map_label_tool", _("&Label"), "LabelTool", "LabelTool",
1056 helptext = _("Add/Remove labels"), icon = "label",
1057 sensitive = _has_visible_map)
1058 _method_command("map_full_extent", _("&Full extent"), "FullExtent",
1059 helptext = _("Zoom to the full map extent"), icon = "fullextent",
1060 sensitive = _has_visible_map)
1061 _method_command("layer_full_extent", _("&Full layer extent"), "FullLayerExtent",
1062 helptext = _("Zoom to the full layer extent"),
1063 icon = "fulllayerextent", sensitive = _has_selected_layer)
1064 _method_command("selected_full_extent", _("&Full selection extent"),
1065 "FullSelectionExtent",
1066 helptext = _("Zoom to the full selection extent"),
1067 icon = "fullselextent", sensitive = _has_selected_shapes)
1068 _method_command("map_export", _("E&xport"), "ExportMap",
1069 helptext = _("Export the map to file"))
1070 _method_command("map_print", _("Prin&t"), "PrintMap",
1071 helptext = _("Print the map"))
1072 _method_command("map_rename", _("&Rename..."), "RenameMap",
1073 helptext = _("Rename the map"))
1074 _method_command("layer_add", _("&Add Layer..."), "AddLayer",
1075 helptext = _("Add a new layer to the map"))
1076 _method_command("rasterlayer_add", _("&Add Image Layer..."), "AddRasterLayer",
1077 helptext = _("Add a new image layer to the map"),
1078 sensitive = _has_gdal_support)
1079 _method_command("layer_add_db", _("Add &Database Layer..."), "AddDBLayer",
1080 helptext = _("Add a new database layer to active map"),
1081 sensitive = _has_dbconnections)
1082 _method_command("layer_remove", _("&Remove Layer"), "RemoveLayer",
1083 helptext = _("Remove selected layer"),
1084 sensitive = _can_remove_layer)
1085
1086 # Layer menu
1087 _method_command("layer_projection", _("Pro&jection..."), "LayerProjection",
1088 sensitive = _has_selected_layer,
1089 helptext = _("Specify projection for selected layer"))
1090 _method_command("layer_duplicate", _("&Duplicate"), "DuplicateLayer",
1091 helptext = _("Duplicate selected layer"),
1092 sensitive = lambda context: context.mainwindow.CanDuplicateLayer())
1093 _method_command("layer_rename", _("Re&name ..."), "RenameLayer",
1094 helptext = _("Rename selected layer"),
1095 sensitive = _has_selected_layer)
1096 _method_command("layer_raise", _("&Raise"), "RaiseLayer",
1097 helptext = _("Raise selected layer"),
1098 sensitive = _has_selected_layer)
1099 _method_command("layer_lower", _("&Lower"), "LowerLayer",
1100 helptext = _("Lower selected layer"),
1101 sensitive = _has_selected_layer)
1102 _method_command("layer_show", _("&Show"), "ShowLayer",
1103 helptext = _("Make selected layer visible"),
1104 sensitive = _has_selected_layer)
1105 _method_command("layer_hide", _("&Hide"), "HideLayer",
1106 helptext = _("Make selected layer unvisible"),
1107 sensitive = _has_selected_layer)
1108 _method_command("layer_show_table", _("Show Ta&ble"), "LayerShowTable",
1109 helptext = _("Show the selected layer's table"),
1110 sensitive = _has_selected_layer)
1111 _method_command("layer_properties", _("&Properties..."), "LayerEditProperties",
1112 sensitive = _has_selected_layer,
1113 helptext = _("Edit the properties of the selected layer"))
1114 _method_command("layer_jointable", _("&Join Table..."), "LayerJoinTable",
1115 sensitive = _has_selected_layer,
1116 helptext = _("Join and attach a table to the selected layer"))
1117
1118 def _can_unjoin(context):
1119 """Return whether the Layer/Unjoin command can be executed.
1120
1121 This is the case if a layer is selected and that layer has a
1122 shapestore that has an original shapestore.
1123 """
1124 layer = context.mainwindow.SelectedLayer()
1125 if layer is None:
1126 return 0
1127 getstore = getattr(layer, "ShapeStore", None)
1128 if getstore is not None:
1129 return getstore().OrigShapeStore() is not None
1130 else:
1131 return 0
1132 _method_command("layer_unjointable", _("&Unjoin Table..."), "LayerUnjoinTable",
1133 sensitive = _can_unjoin,
1134 helptext = _("Undo the last join operation"))
1135
1136
1137 def _has_tables(context):
1138 return bool(context.session.Tables())
1139
1140 # Table menu
1141 _method_command("table_open", _("&Open..."), "TableOpen",
1142 helptext = _("Open a DBF-table from a file"))
1143 _method_command("table_close", _("&Close..."), "TableClose",
1144 sensitive = lambda context: bool(context.session.UnreferencedTables()),
1145 helptext = _("Close one or more tables from a list"))
1146 _method_command("table_rename", _("&Rename..."), "TableRename",
1147 sensitive = _has_tables,
1148 helptext = _("Rename one or more tables"))
1149 _method_command("table_show", _("&Show..."), "TableShow",
1150 sensitive = _has_tables,
1151 helptext = _("Show one or more tables in a dialog"))
1152 _method_command("table_join", _("&Join..."), "TableJoin",
1153 sensitive = _has_tables,
1154 helptext = _("Join two tables creating a new one"))
1155
1156 # Export only under Windows ...
1157 map_menu = ["layer_add", "layer_add_db", "rasterlayer_add", "layer_remove",
1158 None,
1159 "map_rename",
1160 "map_projection",
1161 None,
1162 "map_zoom_in_tool", "map_zoom_out_tool",
1163 "map_pan_tool",
1164 "map_full_extent",
1165 "layer_full_extent",
1166 "selected_full_extent",
1167 None,
1168 "map_identify_tool", "map_label_tool",
1169 None,
1170 "toggle_legend",
1171 None]
1172 if wxPlatform == '__WXMSW__':
1173 map_menu.append("map_export")
1174 map_menu.append("map_print")
1175
1176 # the menu structure
1177 main_menu = Menu("<main>", "<main>",
1178 [Menu("file", _("&File"),
1179 ["new_session", "open_session", None,
1180 "save_session", "save_session_as", None,
1181 "database_management", None,
1182 "toggle_session_tree", None,
1183 "exit"]),
1184 Menu("map", _("&Map"), map_menu),
1185 Menu("layer", _("&Layer"),
1186 ["layer_rename", "layer_duplicate",
1187 None,
1188 "layer_raise", "layer_lower",
1189 None,
1190 "layer_show", "layer_hide",
1191 None,
1192 "layer_projection",
1193 None,
1194 "layer_show_table",
1195 "layer_jointable",
1196 "layer_unjointable",
1197 None,
1198 "layer_properties"]),
1199 Menu("table", _("&Table"),
1200 ["table_open", "table_close", "table_rename",
1201 None,
1202 "table_show",
1203 None,
1204 "table_join"]),
1205 Menu("help", _("&Help"),
1206 ["help_about"])])
1207
1208 # the main toolbar
1209
1210 main_toolbar = Menu("<toolbar>", "<toolbar>",
1211 ["map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool",
1212 "map_full_extent",
1213 "layer_full_extent",
1214 "selected_full_extent",
1215 None,
1216 "map_identify_tool", "map_label_tool"])
1217

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26