/[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 517 by jonathan, Tue Mar 11 17:28:39 2003 UTC revision 622 by bh, Mon Apr 7 10:54:32 2003 UTC
# Line 1  Line 1 
1  # Copyright (C) 2001, 2002 by Intevation GmbH  # Copyright (C) 2001, 2002, 2003 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]>
# Line 31  import tree Line 31  import tree
31  import proj4dialog  import proj4dialog
32  import tableview, identifyview  import tableview, identifyview
33  import classifier  import classifier
34    import legend
35  from menu import Menu  from menu import Menu
36    
37  from context import Context  from context import Context
38  from command import registry, Command, ToolCommand  from command import registry, Command, ToolCommand
39  from messages import SELECTED_SHAPE, VIEW_POSITION  from messages import LAYER_SELECTED, SHAPES_SELECTED, VIEW_POSITION, DOCKABLE_DOCKED, DOCKABLE_UNDOCKED, DOCKABLE_CLOSED
40    
41    from Thuban.UI.dock import DockableWindow, DockFrame, DockPanel
42    
43    
44  # the directory where the toolbar icons are stored  # the directory where the toolbar icons are stored
45  bitmapdir = os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Bitmaps")  bitmapdir = os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Bitmaps")
46  bitmapext = ".xpm"  bitmapext = ".xpm"
47    
48    ID_WINDOW_LEGEND = 4001
49    ID_WINDOW_CANVAS = 4002
50    
51    
52  class MainWindow(wxFrame):  class MainWindow(DockFrame):
53    
54        # Some messages that can be subscribed/unsubscribed directly through
55        # the MapCanvas come in fact from other objects. This is a map to
56        # map those messages to the names of the instance variables they
57        # actually come from. This delegation is implemented in the
58        # Subscribe and unsubscribed methods
59        delegated_messages = {LAYER_SELECTED: "canvas",
60                              SHAPES_SELECTED: "canvas"}
61    
62        # Methods delegated to some instance variables. The delegation is
63        # implemented in the __getattr__ method.
64        delegated_methods = {"SelectLayer": "canvas",
65                             "SelectShapes": "canvas",
66                             }
67    
68      def __init__(self, parent, ID, title, application, interactor,      def __init__(self, parent, ID, title, application, interactor,
69                   initial_message = None, size = wxSize(-1, -1)):                   initial_message = None, size = wxSize(-1, -1)):
70          wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, size)          DockFrame.__init__(self, parent, ID, title, wxDefaultPosition, size)
71            #wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, size)
72    
73          self.application = application          self.application = application
         self.interactor = interactor  
74    
75          self.CreateStatusBar()          self.CreateStatusBar()
76          if initial_message:          if initial_message:
# Line 68  class MainWindow(wxFrame): Line 88  class MainWindow(wxFrame):
88          # call Realize to make sure that the tools appear.          # call Realize to make sure that the tools appear.
89          toolbar.Realize()          toolbar.Realize()
90    
91    
92          # Create the map canvas          # Create the map canvas
93          canvas = view.MapCanvas(self, -1, interactor)          canvas = view.MapCanvas(self, -1)
94          canvas.Subscribe(VIEW_POSITION, self.view_position_changed)          canvas.Subscribe(VIEW_POSITION, self.view_position_changed)
95            canvas.Subscribe(SHAPES_SELECTED, self.identify_view_on_demand)
96          self.canvas = canvas          self.canvas = canvas
97    
98            self.SetMainWindow(self.canvas)
99    
100            self.SetAutoLayout(True)
101    
102          self.init_dialogs()          self.init_dialogs()
103    
104          interactor.Subscribe(SELECTED_SHAPE, self.identify_view_on_demand)          EVT_CLOSE(self, self._OnClose)
105    
106          EVT_CLOSE(self, self.OnClose)      def Subscribe(self, channel, *args):
107            """Subscribe a function to a message channel.
108    
109            If channel is one of the delegated messages call the appropriate
110            object's Subscribe method. Otherwise do nothing.
111            """
112            if channel in self.delegated_messages:
113                object = getattr(self, self.delegated_messages[channel])
114                object.Subscribe(channel, *args)
115            else:
116                print "Trying to subscribe to unsupported channel %s" % channel
117    
118        def Unsubscribe(self, channel, *args):
119            """Unsubscribe a function from a message channel.
120    
121            If channel is one of the delegated messages call the appropriate
122            object's Unsubscribe method. Otherwise do nothing.
123            """
124            if channel in self.delegated_messages:
125                object = getattr(self, self.delegated_messages[channel])
126                object.Unsubscribe(channel, *args)
127    
128        def __getattr__(self, attr):
129            """If attr is one of the delegated methods return that method
130    
131            Otherwise raise AttributeError.
132            """
133            if attr in self.delegated_methods:
134                return getattr(getattr(self, self.delegated_methods[attr]), attr)
135            raise AttributeError(attr)
136    
137      def init_ids(self):      def init_ids(self):
138          """Initialize the ids"""          """Initialize the ids"""
# Line 332  class MainWindow(wxFrame): Line 387  class MainWindow(wxFrame):
387          dlg.Destroy()          dlg.Destroy()
388    
389      def Exit(self):      def Exit(self):
390          self.Close(false)          self.Close(False)
391    
392      def OnClose(self, event):      def _OnClose(self, event):
393          result = self.save_modified_session(can_veto = event.CanVeto())          result = self.save_modified_session(can_veto = event.CanVeto())
394          if result == wxID_CANCEL:          if result == wxID_CANCEL:
395              event.Veto()              event.Veto()
# Line 343  class MainWindow(wxFrame): Line 398  class MainWindow(wxFrame):
398              # wx's destroy event, but that isn't implemented for wxGTK              # wx's destroy event, but that isn't implemented for wxGTK
399              # yet.              # yet.
400              self.canvas.Unsubscribe(VIEW_POSITION, self.view_position_changed)              self.canvas.Unsubscribe(VIEW_POSITION, self.view_position_changed)
401                DockFrame._OnClose(self, event)
402              self.Destroy()              self.Destroy()
403    
404      def SetMap(self, map):      def SetMap(self, map):
405          self.canvas.SetMap(map)          self.canvas.SetMap(map)
406            #self.legendPanel.SetMap(map)
407    
408      def Map(self):      def Map(self):
409          """Return the map displayed by this mainwindow"""          """Return the map displayed by this mainwindow"""
410    
411            # sanity check
412            #assert(self.canvas.Map() is self.legendPanel.GetMap())
413    
414          return self.canvas.Map()          return self.canvas.Map()
415    
416      def ShowSessionTree(self):      def ToggleSessionTree(self):
417            """If the session tree is shown close it otherwise create a new tree"""
418          name = "session_tree"          name = "session_tree"
419          dialog = self.get_open_dialog(name)          dialog = self.get_open_dialog(name)
420          if dialog is None:          if dialog is None:
# Line 360  class MainWindow(wxFrame): Line 422  class MainWindow(wxFrame):
422              self.add_dialog(name, dialog)              self.add_dialog(name, dialog)
423              dialog.Show(True)              dialog.Show(True)
424          else:          else:
425              # FIXME: bring dialog to front here              dialog.Close()
             pass  
426    
427        def SessionTreeShown(self):
428            """Return true iff the session tree is currently shown"""
429            return self.get_open_dialog("session_tree") is not None
430    
431      def About(self):      def About(self):
432          self.RunMessageBox(_("About"),          self.RunMessageBox(_("About"),
# Line 393  class MainWindow(wxFrame): Line 457  class MainWindow(wxFrame):
457                                     _("Can't open the file '%s'.") % filename)                                     _("Can't open the file '%s'.") % filename)
458              else:              else:
459                  if not has_layers:                  if not has_layers:
460                      # if we're adding a layer to an empty map, for the                      # if we're adding a layer to an empty map, fit the
461                      # new map to the window                      # new map to the window
462                      self.canvas.FitMapToWindow()                      self.canvas.FitMapToWindow()
463          dlg.Destroy()          dlg.Destroy()
# Line 406  class MainWindow(wxFrame): Line 470  class MainWindow(wxFrame):
470      def CanRemoveLayer(self):      def CanRemoveLayer(self):
471          """Return true if the currently selected layer can be deleted.          """Return true if the currently selected layer can be deleted.
472    
473          If no layer is selected return false.          If no layer is selected return False.
474    
475          The return value of this method determines whether the remove          The return value of this method determines whether the remove
476          layer command is sensitive in menu.          layer command is sensitive in menu.
# Line 414  class MainWindow(wxFrame): Line 478  class MainWindow(wxFrame):
478          layer = self.current_layer()          layer = self.current_layer()
479          if layer is not None:          if layer is not None:
480              return self.canvas.Map().CanRemoveLayer(layer)              return self.canvas.Map().CanRemoveLayer(layer)
481          return 0          return False
482    
483      def RaiseLayer(self):      def RaiseLayer(self):
484          layer = self.current_layer()          layer = self.current_layer()
# Line 431  class MainWindow(wxFrame): Line 495  class MainWindow(wxFrame):
495    
496          If no layer is selected, return None          If no layer is selected, return None
497          """          """
498          return self.interactor.SelectedLayer()          return self.canvas.SelectedLayer()
499    
500      def has_selected_layer(self):      def has_selected_layer(self):
501          """Return true if a layer is currently selected"""          """Return true if a layer is currently selected"""
502          return self.interactor.HasSelectedLayer()          return self.canvas.HasSelectedLayer()
503    
504      def choose_color(self):      def choose_color(self):
505          """Run the color selection dialog and return the selected color.          """Run the color selection dialog and return the selected color.
# Line 463  class MainWindow(wxFrame): Line 527  class MainWindow(wxFrame):
527      def LayerTransparentFill(self):      def LayerTransparentFill(self):
528          layer = self.current_layer()          layer = self.current_layer()
529          if layer is not None:          if layer is not None:
530              layer.GetClassification().SetDefaultFill(Color.None)              layer.GetClassification().SetDefaultFill(Color.Transparent)
531    
532      def LayerOutlineColor(self):      def LayerOutlineColor(self):
533          layer = self.current_layer()          layer = self.current_layer()
# Line 475  class MainWindow(wxFrame): Line 539  class MainWindow(wxFrame):
539      def LayerNoOutline(self):      def LayerNoOutline(self):
540          layer = self.current_layer()          layer = self.current_layer()
541          if layer is not None:          if layer is not None:
542              layer.GetClassification().SetDefaultLineColor(Color.None)              layer.GetClassification().SetDefaultLineColor(Color.Transparent)
543    
544      def HideLayer(self):      def HideLayer(self):
545          layer = self.current_layer()          layer = self.current_layer()
# Line 494  class MainWindow(wxFrame): Line 558  class MainWindow(wxFrame):
558              name = "table_view" + str(id(table))              name = "table_view" + str(id(table))
559              dialog = self.get_open_dialog(name)              dialog = self.get_open_dialog(name)
560              if dialog is None:              if dialog is None:
561                  dialog = tableview.LayerTableFrame(self, self.interactor, name,                  dialog = tableview.LayerTableFrame(self, name,
562                                                     _("Table: %s") % layer.Title(),                                                 _("Table: %s") % layer.Title(),
563                                                     layer, table)                                                     layer, table)
564                  self.add_dialog(name, dialog)                  self.add_dialog(name, dialog)
565                  dialog.Show(true)                  dialog.Show(true)
# Line 529  class MainWindow(wxFrame): Line 593  class MainWindow(wxFrame):
593          #          #
594    
595          layer = self.current_layer()          layer = self.current_layer()
596            self.OpenClassifier(layer)
597    
598        def OpenClassifier(self, layer, group = None):
599          name = "classifier" + str(id(layer))          name = "classifier" + str(id(layer))
600          dialog = self.get_open_dialog(name)          dialog = self.get_open_dialog(name)
601    
602          if dialog is None:          if dialog is None:
603              dialog = classifier.Classifier(self, self.interactor,              dialog = classifier.Classifier(self, name, layer, group)
                                            name, self.current_layer())  
604              self.add_dialog(name, dialog)              self.add_dialog(name, dialog)
605              dialog.Show()              dialog.Show()
606            dialog.Raise()
607    
608    
609        def ShowLegend(self):
610            if not self.LegendShown():
611                self.ToggleLegend()
612    
613        def ToggleLegend(self):
614            """Show the legend if it's not shown otherwise hide it again"""
615            name = "legend"
616            dialog = self.FindRegisteredDock(name)
617    
618            if dialog is None:
619                title = "Legend: %s" % self.Map().Title()
620                dialog = self.CreateDock(name, -1, title, wxLAYOUT_LEFT)
621                legend.LegendPanel(dialog, None, self)
622                dialog.Dock()
623                dialog.GetPanel().SetMap(self.Map())
624                dialog.Show()
625            else:
626                dialog.Show(not dialog.IsShown())
627    
628        def LegendShown(self):
629            """Return true iff the legend is currently open"""
630            dialog = self.FindRegisteredDock("legend")
631            return dialog is not None and dialog.IsShown()
632    
633      def ZoomInTool(self):      def ZoomInTool(self):
634          self.canvas.ZoomInTool()          self.canvas.ZoomInTool()
# Line 560  class MainWindow(wxFrame): Line 652  class MainWindow(wxFrame):
652      def PrintMap(self):      def PrintMap(self):
653          self.canvas.Print()          self.canvas.Print()
654    
655      def identify_view_on_demand(self, layer, shape):      def identify_view_on_demand(self, layer, shapes):
656          name = "identify_view"          name = "identify_view"
657          if self.canvas.CurrentTool() == "IdentifyTool":          if self.canvas.CurrentTool() == "IdentifyTool":
658              if not self.dialog_open(name):              if not self.dialog_open(name):
659                  dialog = identifyview.IdentifyView(self, self.interactor, name)                  dialog = identifyview.IdentifyView(self, name)
660                  self.add_dialog(name, dialog)                  self.add_dialog(name, dialog)
661                  dialog.Show(true)                  dialog.Show(True)
662              else:              else:
663                  # FIXME: bring dialog to front?                  # FIXME: bring dialog to front?
664                  pass                  pass
# Line 582  def call_method(context, methodname, *ar Line 674  def call_method(context, methodname, *ar
674      apply(getattr(context.mainwindow, methodname), args)      apply(getattr(context.mainwindow, methodname), args)
675    
676  def _method_command(name, title, method, helptext = "",  def _method_command(name, title, method, helptext = "",
677                      icon = "", sensitive = None):                      icon = "", sensitive = None, checked = None):
678      """Add a command implemented by a method of the mainwindow object"""      """Add a command implemented by a method of the mainwindow object"""
679      registry.Add(Command(name, title, call_method, args=(method,),      registry.Add(Command(name, title, call_method, args=(method,),
680                           helptext = helptext, icon = icon,                           helptext = helptext, icon = icon,
681                           sensitive = sensitive))                           sensitive = sensitive, checked = checked))
682    
683  def make_check_current_tool(toolname):  def make_check_current_tool(toolname):
684      """Return a function that tests if the currently active tool is toolname      """Return a function that tests if the currently active tool is toolname
# Line 616  def _can_remove_layer(context): Line 708  def _can_remove_layer(context):
708    
709  def _has_tree_window_shown(context):  def _has_tree_window_shown(context):
710      """Return true if the tree window is shown"""      """Return true if the tree window is shown"""
711      return context.mainwindow.get_open_dialog("session_tree") is None      return context.mainwindow.SessionTreeShown()
712    
713  def _has_visible_map(context):  def _has_visible_map(context):
714      """Return true iff theres a visible map in the mainwindow.      """Return true iff theres a visible map in the mainwindow.
# Line 629  def _has_visible_map(context): Line 721  def _has_visible_map(context):
721                  return 1                  return 1
722      return 0      return 0
723    
724    def _has_legend_shown(context):
725        """Return true if the legend window is shown"""
726        return context.mainwindow.LegendShown()
727    
728    
729  # File menu  # File menu
730  _method_command("new_session", _("&New Session"), "NewSession")  _method_command("new_session", _("&New Session"), "NewSession")
731  _method_command("open_session", _("&Open Session"), "OpenSession")  _method_command("open_session", _("&Open Session"), "OpenSession")
732  _method_command("save_session", _("&Save Session"), "SaveSession")  _method_command("save_session", _("&Save Session"), "SaveSession")
733  _method_command("save_session_as", _("Save Session &As"), "SaveSessionAs")  _method_command("save_session_as", _("Save Session &As"), "SaveSessionAs")
734  _method_command("show_session_tree", _("Show Session &Tree"), "ShowSessionTree",  _method_command("toggle_session_tree", _("Session &Tree"), "ToggleSessionTree",
735                  sensitive = _has_tree_window_shown)                  checked = _has_tree_window_shown)
736    _method_command("toggle_legend", _("Legend"), "ToggleLegend",
737                    checked = _has_legend_shown)
738  _method_command("exit", _("E&xit"), "Exit")  _method_command("exit", _("E&xit"), "Exit")
739    
740  # Help menu  # Help menu
# Line 702  _method_command("layer_hide", _("&Hide") Line 800  _method_command("layer_hide", _("&Hide")
800  _method_command("layer_show_table", _("Show Ta&ble"), "LayerShowTable",  _method_command("layer_show_table", _("Show Ta&ble"), "LayerShowTable",
801                  helptext = _("Show the selected layer's table"),                  helptext = _("Show the selected layer's table"),
802                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
   
803  _method_command("layer_classifier", _("Classify"), "Classify",  _method_command("layer_classifier", _("Classify"), "Classify",
804                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
805    
# Line 711  main_menu = Menu("<main>", "<main>", Line 808  main_menu = Menu("<main>", "<main>",
808                   [Menu("file", _("&File"),                   [Menu("file", _("&File"),
809                         ["new_session", "open_session", None,                         ["new_session", "open_session", None,
810                          "save_session", "save_session_as", None,                          "save_session", "save_session_as", None,
811                          "show_session_tree", None,                          "toggle_session_tree",
812                            "toggle_legend", None,
813                          "exit"]),                          "exit"]),
814                    Menu("map", _("&Map"),                    Menu("map", _("&Map"),
815                         ["layer_add", "layer_remove",                         ["layer_add", "layer_remove",

Legend:
Removed from v.517  
changed lines
  Added in v.622

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26