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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 310 by bh, Tue Sep 10 16:45:22 2002 UTC revision 469 by jonathan, Wed Mar 5 18:19:25 2003 UTC
# Line 17  import os Line 17  import os
17  from wxPython.wx import *  from wxPython.wx import *
18    
19  import Thuban  import Thuban
20    from Thuban import _
21  from Thuban.Model.session import create_empty_session  from Thuban.Model.session import create_empty_session
22  from Thuban.Model.layer import Layer  from Thuban.Model.layer import Layer
23  from Thuban.Model.color import Color  from Thuban.Model.color import Color
# Line 26  import view Line 27  import view
27  import tree  import tree
28  import proj4dialog  import proj4dialog
29  import tableview, identifyview  import tableview, identifyview
30    import classifier
31  from menu import Menu  from menu import Menu
32    
33  from context import Context  from context import Context
34  from command import registry, Command  from command import registry, Command, ToolCommand
35  from messages import SELECTED_SHAPE, VIEW_POSITION  from messages import SELECTED_SHAPE, VIEW_POSITION
36    
37    
# Line 112  class MainWindow(wxFrame): Line 114  class MainWindow(wxFrame):
114          return menu_bar          return menu_bar
115    
116      def build_menu(self, menudesc):      def build_menu(self, menudesc):
117          """Build and return a wxMenu from a menudescription"""          """Return a wxMenu built from the menu description menudesc"""
118          wxmenu = wxMenu()          wxmenu = wxMenu()
119          last = None          last = None
120          for item in menudesc.items:          for item in menudesc.items:
             # here the items must all be Menu instances themselves  
121              if item is None:              if item is None:
122                  # a separator. Only add one if the last item was not a                  # a separator. Only add one if the last item was not a
123                  # separator                  # separator
# Line 169  class MainWindow(wxFrame): Line 170  class MainWindow(wxFrame):
170                              command.IsCheckCommand())                              command.IsCheckCommand())
171                  self.bind_command_events(command, ID)                  self.bind_command_events(command, ID)
172              else:              else:
173                  print "Unknown command %s" % name                  print _("Unknown command %s") % name
174    
175      def add_toolbar_command(self, toolbar, name):      def add_toolbar_command(self, toolbar, name):
176          """Add the command with name name to the toolbar toolbar.          """Add the command with name name to the toolbar toolbar.
# Line 191  class MainWindow(wxFrame): Line 192  class MainWindow(wxFrame):
192                                  isToggle = command.IsCheckCommand())                                  isToggle = command.IsCheckCommand())
193                  self.bind_command_events(command, ID)                  self.bind_command_events(command, ID)
194              else:              else:
195                  print "Unknown command %s" % name                  print _("Unknown command %s") % name
196    
197      def Context(self):      def Context(self):
198          """Return the context object for a command invoked from this window          """Return the context object for a command invoked from this window
# Line 204  class MainWindow(wxFrame): Line 205  class MainWindow(wxFrame):
205              command = registry.Command(name)              command = registry.Command(name)
206              command.Execute(self.Context())              command.Execute(self.Context())
207          else:          else:
208              print "Unknown command ID %d" % event.GetId()              print _("Unknown command ID %d") % event.GetId()
209    
210      def update_command_ui(self, event):      def update_command_ui(self, event):
211          #print "update_command_ui", self.id_to_name[event.GetId()]          #print "update_command_ui", self.id_to_name[event.GetId()]
212          context = self.Context()          context = self.Context()
213          command = registry.Command(self.id_to_name[event.GetId()])          command = registry.Command(self.id_to_name[event.GetId()])
214          if command is not None:          if command is not None:
215              event.Enable(command.Sensitive(context))              sensitive = command.Sensitive(context)
216                event.Enable(sensitive)
217                if command.IsTool() and not sensitive and command.Checked(context):
218                    # When a checked tool command is disabled deselect all
219                    # tools. Otherwise the tool would remain active but it
220                    # might lead to errors if the tools stays active. This
221                    # problem occurred in GREAT-ER and this fixes it, but
222                    # it's not clear to me whether this is really the best
223                    # way to do it (BH, 20021206).
224                    self.canvas.SelectTool(None)
225              event.SetText(command.DynText(context))              event.SetText(command.DynText(context))
226              if command.IsCheckCommand():              if command.IsCheckCommand():
227                  event.Check(command.Checked(context))                      event.Check(command.Checked(context))
228    
229      def RunMessageBox(self, title, text, flags = wxOK | wxICON_INFORMATION):      def RunMessageBox(self, title, text, flags = wxOK | wxICON_INFORMATION):
230          """Run a modal message box with the given text, title and flags          """Run a modal message box with the given text, title and flags
231          and return the result"""          and return the result"""
232          dlg = wxMessageDialog(self, text, title, flags)          dlg = wxMessageDialog(self, text, title, flags)
233            dlg.CenterOnParent()
234          result = dlg.ShowModal()          result = dlg.ShowModal()
235          dlg.Destroy()          dlg.Destroy()
236          return result          return result
# Line 233  class MainWindow(wxFrame): Line 244  class MainWindow(wxFrame):
244    
245      def add_dialog(self, name, dialog):      def add_dialog(self, name, dialog):
246          if self.dialogs.has_key(name):          if self.dialogs.has_key(name):
247              raise RuntimeError("The Dialog named %s is already open" % name)              raise RuntimeError(_("The Dialog named %s is already open") % name)
248          self.dialogs[name] = dialog          self.dialogs[name] = dialog
249    
250      def dialog_open(self, name):      def dialog_open(self, name):
# Line 251  class MainWindow(wxFrame): Line 262  class MainWindow(wxFrame):
262              text = "(%10.10g, %10.10g)" % pos              text = "(%10.10g, %10.10g)" % pos
263          else:          else:
264              text = ""              text = ""
265            self.set_position_text(text)
266    
267        def set_position_text(self, text):
268            """Set the statusbar text showing the current position.
269    
270            By default the text is shown in field 0 of the status bar.
271            Override this method in derived classes to put it into a
272            different field of the statusbar.
273            """
274          self.SetStatusText(text)          self.SetStatusText(text)
275    
276      def save_modified_session(self, can_veto = 1):      def save_modified_session(self, can_veto = 1):
# Line 266  class MainWindow(wxFrame): Line 286  class MainWindow(wxFrame):
286              flags = wxYES_NO | wxICON_QUESTION              flags = wxYES_NO | wxICON_QUESTION
287              if can_veto:              if can_veto:
288                  flags = flags | wxCANCEL                  flags = flags | wxCANCEL
289              result = self.RunMessageBox("Exit",              result = self.RunMessageBox(_("Exit"),
290                                          ("The session has been modified."                                          _("The session has been modified."
291                                           " Do you want to save it?"),                                           " Do you want to save it?"),
292                                          flags)                                          flags)
293              if result == wxID_YES:              if result == wxID_YES:
# Line 282  class MainWindow(wxFrame): Line 302  class MainWindow(wxFrame):
302    
303      def OpenSession(self):      def OpenSession(self):
304          self.save_modified_session()          self.save_modified_session()
305          dlg = wxFileDialog(self, "Select a session file", ".", "",          dlg = wxFileDialog(self, _("Open Session"), ".", "",
306                             "*.thuban", wxOPEN)                             "*.thuban", wxOPEN)
307          if dlg.ShowModal() == wxID_OK:          if dlg.ShowModal() == wxID_OK:
308              self.application.OpenSession(dlg.GetPath())              self.application.OpenSession(dlg.GetPath())
# Line 294  class MainWindow(wxFrame): Line 314  class MainWindow(wxFrame):
314          self.application.SaveSession()          self.application.SaveSession()
315    
316      def SaveSessionAs(self):      def SaveSessionAs(self):
317          dlg = wxFileDialog(self, "Enter a filename for session", ".", "",          dlg = wxFileDialog(self, _("Save Session As"), ".", "",
318                             "*.thuban", wxOPEN)                             "*.thuban", wxOPEN)
319          if dlg.ShowModal() == wxID_OK:          if dlg.ShowModal() == wxID_OK:
320              self.application.session.SetFilename(dlg.GetPath())              self.application.session.SetFilename(dlg.GetPath())
# Line 334  class MainWindow(wxFrame): Line 354  class MainWindow(wxFrame):
354              pass              pass
355    
356      def About(self):      def About(self):
357          self.RunMessageBox("About",          self.RunMessageBox(_("About"),
358                             ("Thuban is a program for\n"                             _("Thuban is a program for\n"
359                              "exploring geographic data.\n"                              "exploring geographic data.\n"
360                              "Copyright (C) 2001 Intevation GmbH.\n"                              "Copyright (C) 2001-2003 Intevation GmbH.\n"
361                              "Thuban is licensed under the GPL"),                              "Thuban is licensed under the GPL"),
362                             wxOK | wxICON_INFORMATION)                             wxOK | wxICON_INFORMATION)
363    
364      def AddLayer(self):      def AddLayer(self):
365          dlg = wxFileDialog(self, "Select a data file", ".", "", "*.*",          dlg = wxFileDialog(self, _("Select a data file"), ".", "", "*.*",
366                             wxOPEN)                             wxOPEN)
367          if dlg.ShowModal() == wxID_OK:          if dlg.ShowModal() == wxID_OK:
368              filename = dlg.GetPath()              filename = dlg.GetPath()
# Line 354  class MainWindow(wxFrame): Line 374  class MainWindow(wxFrame):
374                  map.AddLayer(layer)                  map.AddLayer(layer)
375              except IOError:              except IOError:
376                  # the layer couldn't be opened                  # the layer couldn't be opened
377                  self.RunMessageBox("Add Layer",                  self.RunMessageBox(_("Add Layer"),
378                                     "Can't open the file '%s'." % filename)                                     _("Can't open the file '%s'.") % filename)
379              else:              else:
380                  if not has_layers:                  if not has_layers:
381                      # if we're adding a layer to an empty map, for the                      # if we're adding a layer to an empty map, for the
# Line 423  class MainWindow(wxFrame): Line 443  class MainWindow(wxFrame):
443          if layer is not None:          if layer is not None:
444              color = self.choose_color()              color = self.choose_color()
445              if color is not None:              if color is not None:
446                  layer.SetFill(color)                  layer.GetClassification().SetDefaultFill(color)
447    
448      def LayerTransparentFill(self):      def LayerTransparentFill(self):
449          layer = self.current_layer()          layer = self.current_layer()
450          if layer is not None:          if layer is not None:
451              layer.SetFill(None)              layer.GetClassification().SetDefaultFill(Color.None)
452    
453      def LayerOutlineColor(self):      def LayerOutlineColor(self):
454          layer = self.current_layer()          layer = self.current_layer()
455          if layer is not None:          if layer is not None:
456              color = self.choose_color()              color = self.choose_color()
457              if color is not None:              if color is not None:
458                  layer.SetStroke(color)                  layer.GetClassification().SetDefaultLineColor(color)
459    
460      def LayerNoOutline(self):      def LayerNoOutline(self):
461          layer = self.current_layer()          layer = self.current_layer()
462          if layer is not None:          if layer is not None:
463              layer.SetStroke(None)              layer.GetClassification().SetDefaultLineColor(Color.None)
464    
465      def HideLayer(self):      def HideLayer(self):
466          layer = self.current_layer()          layer = self.current_layer()
# Line 460  class MainWindow(wxFrame): Line 480  class MainWindow(wxFrame):
480              dialog = self.get_open_dialog(name)              dialog = self.get_open_dialog(name)
481              if dialog is None:              if dialog is None:
482                  dialog = tableview.LayerTableFrame(self, self.interactor, name,                  dialog = tableview.LayerTableFrame(self, self.interactor, name,
483                                                     "Table: %s" % layer.Title(),                                                     _("Table: %s") % layer.Title(),
484                                                     layer, table)                                                     layer, table)
485                  self.add_dialog(name, dialog)                  self.add_dialog(name, dialog)
486                  dialog.Show(true)                  dialog.Show(true)
# Line 485  class MainWindow(wxFrame): Line 505  class MainWindow(wxFrame):
505              map.SetProjection(proj)              map.SetProjection(proj)
506          proj4Dlg.Destroy()          proj4Dlg.Destroy()
507    
508        def Classify(self):
509            classifyDlg = classifier.Classifier(NULL, self.current_layer())
510    
511            classifyDlg.ShowModal()
512            classifyDlg.Destroy()
513    
514      def ZoomInTool(self):      def ZoomInTool(self):
515          self.canvas.ZoomInTool()          self.canvas.ZoomInTool()
516    
# Line 549  def make_check_current_tool(toolname): Line 575  def make_check_current_tool(toolname):
575  def _tool_command(name, title, method, toolname, helptext = "",  def _tool_command(name, title, method, toolname, helptext = "",
576                    icon = "", sensitive = None):                    icon = "", sensitive = None):
577      """Add a tool command"""      """Add a tool command"""
578      registry.Add(Command(name, title, call_method, args=(method,),      registry.Add(ToolCommand(name, title, call_method, args=(method,),
579                           helptext = helptext, icon = icon,                               helptext = helptext, icon = icon,
580                           checked = make_check_current_tool(toolname),                               checked = make_check_current_tool(toolname),
581                           sensitive = sensitive))                               sensitive = sensitive))
582    
583  def _has_selected_layer(context):  def _has_selected_layer(context):
584      """Return true if a layer is selected in the context"""      """Return true if a layer is selected in the context"""
# Line 578  def _has_visible_map(context): Line 604  def _has_visible_map(context):
604    
605    
606  # File menu  # File menu
607  _method_command("new_session", "&New Session", "NewSession")  _method_command("new_session", _("&New Session"), "NewSession")
608  _method_command("open_session", "&Open Session", "OpenSession")  _method_command("open_session", _("&Open Session"), "OpenSession")
609  _method_command("save_session", "&Save Session", "SaveSession")  _method_command("save_session", _("&Save Session"), "SaveSession")
610  _method_command("save_session_as", "Save Session &As", "SaveSessionAs")  _method_command("save_session_as", _("Save Session &As"), "SaveSessionAs")
611  _method_command("show_session_tree", "Show Session &Tree", "ShowSessionTree",  _method_command("show_session_tree", _("Show Session &Tree"), "ShowSessionTree",
612                  sensitive = _has_tree_window_shown)                  sensitive = _has_tree_window_shown)
613  _method_command("exit", "&Exit", "Exit")  _method_command("exit", _("E&xit"), "Exit")
614    
615  # Help menu  # Help menu
616  _method_command("help_about", "&About", "About")  _method_command("help_about", _("&About"), "About")
617    
618    
619  # Map menu  # Map menu
620  _method_command("map_projection", "Pro&jection", "Projection")  _method_command("map_projection", _("Pro&jection"), "Projection")
621    
622  _tool_command("map_zoom_in_tool", "&Zoom in", "ZoomInTool", "ZoomInTool",  _tool_command("map_zoom_in_tool", _("&Zoom in"), "ZoomInTool", "ZoomInTool",
623                helptext = "Switch to map-mode 'zoom-in'", icon = "zoom_in",                helptext = _("Switch to map-mode 'zoom-in'"), icon = "zoom_in",
624                sensitive = _has_visible_map)                sensitive = _has_visible_map)
625  _tool_command("map_zoom_out_tool", "Zoom &out", "ZoomOutTool", "ZoomOutTool",  _tool_command("map_zoom_out_tool", _("Zoom &out"), "ZoomOutTool", "ZoomOutTool",
626                helptext = "Switch to map-mode 'zoom-out'", icon = "zoom_out",                helptext = _("Switch to map-mode 'zoom-out'"), icon = "zoom_out",
627                sensitive = _has_visible_map)                sensitive = _has_visible_map)
628  _tool_command("map_pan_tool", "&Pan", "PanTool", "PanTool",  _tool_command("map_pan_tool", _("&Pan"), "PanTool", "PanTool",
629                helptext = "Switch to map-mode 'pan'", icon = "pan",                helptext = _("Switch to map-mode 'pan'"), icon = "pan",
630                sensitive = _has_visible_map)                sensitive = _has_visible_map)
631  _tool_command("map_identify_tool", "&Identify", "IdentifyTool", "IdentifyTool",  _tool_command("map_identify_tool", _("&Identify"), "IdentifyTool",
632                helptext = "Switch to map-mode 'identify'", icon = "identify",                "IdentifyTool",
633                  helptext = _("Switch to map-mode 'identify'"), icon = "identify",
634                sensitive = _has_visible_map)                sensitive = _has_visible_map)
635  _tool_command("map_label_tool", "&Label", "LabelTool", "LabelTool",  _tool_command("map_label_tool", _("&Label"), "LabelTool", "LabelTool",
636                helptext = "Add/Remove labels", icon = "label",                helptext = _("Add/Remove labels"), icon = "label",
637                sensitive = _has_visible_map)                sensitive = _has_visible_map)
638  _method_command("map_full_extent", "&Full extent", "FullExtent",  _method_command("map_full_extent", _("&Full extent"), "FullExtent",
639                 helptext = "Full Extent", icon = "fullextent",                 helptext = _("Full Extent"), icon = "fullextent",
640                sensitive = _has_visible_map)                sensitive = _has_visible_map)
641  _method_command("map_print", "Prin&t", "PrintMap", helptext = "Print the map")  _method_command("map_print", _("Prin&t"), "PrintMap",
642                    helptext = _("Print the map"))
643    
644  # Layer menu  # Layer menu
645  _method_command("layer_add", "&Add Layer", "AddLayer",  _method_command("layer_add", _("&Add Layer"), "AddLayer",
646                  helptext = "Add a new layer to active map")                  helptext = _("Add a new layer to active map"))
647  _method_command("layer_remove", "&Remove Layer", "RemoveLayer",  _method_command("layer_remove", _("&Remove Layer"), "RemoveLayer",
648                  helptext = "Remove selected layer(s)",                  helptext = _("Remove selected layer(s)"),
649                  sensitive = _can_remove_layer)                  sensitive = _can_remove_layer)
650  _method_command("layer_fill_color", "&Fill Color", "LayerFillColor",  _method_command("layer_fill_color", _("&Fill Color"), "LayerFillColor",
651                  helptext = "Set the fill color of selected layer(s)",                  helptext = _("Set the fill color of selected layer(s)"),
652                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
653  _method_command("layer_transparent_fill", "&Transparent Fill",  _method_command("layer_transparent_fill", _("&Transparent Fill"),
654                  "LayerTransparentFill",                  "LayerTransparentFill",
655                  helptext = "Do not fill the selected layer(s)",                  helptext = _("Do not fill the selected layer(s)"),
656                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
657  _method_command("layer_outline_color", "&Outline Color", "LayerOutlineColor",  _method_command("layer_outline_color", _("&Outline Color"), "LayerOutlineColor",
658                  helptext = "Set the outline color of selected layer(s)",                  helptext = _("Set the outline color of selected layer(s)"),
659                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
660  _method_command("layer_no_outline", "&No Outline", "LayerNoOutline",  _method_command("layer_no_outline", _("&No Outline"), "LayerNoOutline",
661                  helptext = "Do not draw the outline of the selected layer(s)",                  helptext= _("Do not draw the outline of the selected layer(s)"),
662                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
663  _method_command("layer_raise", "&Raise", "RaiseLayer",  _method_command("layer_raise", _("&Raise"), "RaiseLayer",
664                  helptext = "Raise selected layer(s)",                  helptext = _("Raise selected layer(s)"),
665                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
666  _method_command("layer_lower", "&Lower", "LowerLayer",  _method_command("layer_lower", _("&Lower"), "LowerLayer",
667                  helptext = "Lower selected layer(s)",                  helptext = _("Lower selected layer(s)"),
668                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
669  _method_command("layer_show", "&Show", "ShowLayer",  _method_command("layer_show", _("&Show"), "ShowLayer",
670                  helptext = "Make selected layer(s) visible",                  helptext = _("Make selected layer(s) visible"),
671                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
672  _method_command("layer_hide", "&Hide", "HideLayer",  _method_command("layer_hide", _("&Hide"), "HideLayer",
673                  helptext = "Make selected layer(s) unvisible",                  helptext = _("Make selected layer(s) unvisible"),
674                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
675  _method_command("layer_show_table", "Show Ta&ble", "LayerShowTable",  _method_command("layer_show_table", _("Show Ta&ble"), "LayerShowTable",
676                  helptext = "Show the selected layer's table",                  helptext = _("Show the selected layer's table"),
677                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
678    
679    _method_command("layer_classifier", _("Classify"), "Classify",
680                    sensitive = _has_selected_layer)
681    
682  # the menu structure  # the menu structure
683  main_menu = Menu("<main>", "<main>",  main_menu = Menu("<main>", "<main>",
684                   [Menu("file", "&File",                   [Menu("file", _("&File"),
685                         ["new_session", "open_session", None,                         ["new_session", "open_session", None,
686                          "save_session", "save_session_as", None,                          "save_session", "save_session_as", None,
687                          "show_session_tree", None,                          "show_session_tree", None,
688                          "exit"]),                          "exit"]),
689                    Menu("map", "&Map",                    Menu("map", _("&Map"),
690                         ["layer_add", "layer_remove",                         ["layer_add", "layer_remove",
691                          None,                          None,
692                          "map_projection",                          "map_projection",
# Line 667  main_menu = Menu("<main>", "<main>", Line 697  main_menu = Menu("<main>", "<main>",
697                          "map_full_extent",                          "map_full_extent",
698                          None,                          None,
699                          "map_print"]),                          "map_print"]),
700                    Menu("layer", "&Layer",                    Menu("layer", _("&Layer"),
701                         ["layer_fill_color", "layer_transparent_fill",                         ["layer_fill_color", "layer_transparent_fill",
702                          "layer_outline_color", "layer_no_outline",                          "layer_outline_color", "layer_no_outline",
703                          None,                          None,
# Line 675  main_menu = Menu("<main>", "<main>", Line 705  main_menu = Menu("<main>", "<main>",
705                          None,                          None,
706                          "layer_show", "layer_hide",                          "layer_show", "layer_hide",
707                          None,                          None,
708                          "layer_show_table"]),                          "layer_show_table",
709                    Menu("help", "&Help",                          None,
710                            "layer_classifier"]),
711                      Menu("help", _("&Help"),
712                         ["help_about"])])                         ["help_about"])])
713    
714  # the main toolbar  # the main toolbar
715    
716  main_toolbar = Menu("<toolbar>", "<toolbar>",  main_toolbar = Menu("<toolbar>", "<toolbar>",
717                      ["map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool",                      ["map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool",
718                       "map_identify_tool", "map_label_tool", "map_full_extent"])                       "map_full_extent", None,
719                         "map_identify_tool", "map_label_tool"])

Legend:
Removed from v.310  
changed lines
  Added in v.469

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26