/[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 2040 by bh, Tue Dec 23 11:37:04 2003 UTC revision 2529 by russell, Thu Jan 20 17:55:23 2005 UTC
# Line 1  Line 1 
1  # Copyright (C) 2001, 2002, 2003 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]>
# Line 35  from Thuban.UI.multiplechoicedialog impo Line 35  from Thuban.UI.multiplechoicedialog impo
35  import view  import view
36  import tree  import tree
37  import tableview, identifyview  import tableview, identifyview
 from Thuban.UI.classifier import Classifier  
38  import legend  import legend
39  from menu import Menu  from menu import Menu
40    
# Line 53  import Thuban.Model.resource Line 52  import Thuban.Model.resource
52    
53  import projdialog  import projdialog
54    
55    from Thuban.Lib.classmapper import ClassMapper
56    
57    layer_properties_dialogs = ClassMapper()
58    
59  class MainWindow(DockFrame):  class MainWindow(DockFrame):
60    
# Line 338  class MainWindow(DockFrame): Line 340  class MainWindow(DockFrame):
340              text = "(%10.10g, %10.10g)" % pos              text = "(%10.10g, %10.10g)" % pos
341          else:          else:
342              text = ""              text = ""
343                map = self.canvas.Map()
344                for layer in map.layers:
345                    bbox = layer.LatLongBoundingBox()
346                    if bbox:
347                        left, bottom, right, top = bbox
348                        if not (-180 <= left <= 180 and
349                            -180 <= right <= 180 and
350                            -90 <= top <= 90 and
351                            -90 <= bottom <= 90):
352                            text = ("Select '"+layer.title+"' and pick a " +
353                                "projection using Layer/Projection...")
354                            break
355    
356          self.set_position_text(text)          self.set_position_text(text)
357    
358      def set_position_text(self, text):      def set_position_text(self, text):
# Line 349  class MainWindow(DockFrame): Line 364  class MainWindow(DockFrame):
364          """          """
365          self.SetStatusText(text)          self.SetStatusText(text)
366    
367        def OpenOrRaiseDialog(self, name, dialog_class, *args, **kw):
368            """
369            Open or raise a dialog.
370    
371            If a dialog with the denoted name does already exist it is
372            raised.  Otherwise a new dialog, an instance of dialog_class,
373            is created, inserted into the main list and displayed.
374            """
375            dialog = self.get_open_dialog(name)
376    
377            if dialog is None:
378                dialog = dialog_class(self, name, *args, **kw)
379                self.add_dialog(name, dialog)
380                dialog.Show(True)
381            else:
382                dialog.Raise()
383                              
384      def save_modified_session(self, can_veto = 1):      def save_modified_session(self, can_veto = 1):
385          """If the current session has been modified, ask the user          """If the current session has been modified, ask the user
386          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
# Line 378  class MainWindow(DockFrame): Line 410  class MainWindow(DockFrame):
410    
411      def OpenSession(self):      def OpenSession(self):
412          if self.save_modified_session() != wxID_CANCEL:          if self.save_modified_session() != wxID_CANCEL:
413              dlg = wxFileDialog(self, _("Open Session"), ".", "",              dlg = wxFileDialog(self, _("Open Session"),
414                                   self.application.Path("data"), "",
415                                 "Thuban Session File (*.thuban)|*.thuban",                                 "Thuban Session File (*.thuban)|*.thuban",
416                                 wxOPEN)                                 wxOPEN)
417              if dlg.ShowModal() == wxID_OK:              if dlg.ShowModal() == wxID_OK:
418                  self.application.OpenSession(dlg.GetPath(),                  self.application.OpenSession(dlg.GetPath(),
419                                               self.run_db_param_dialog)                                               self.run_db_param_dialog)
420                    self.application.SetPath("data", dlg.GetPath())
421              dlg.Destroy()              dlg.Destroy()
422    
423      def run_db_param_dialog(self, parameters, message):      def run_db_param_dialog(self, parameters, message):
# Line 398  class MainWindow(DockFrame): Line 432  class MainWindow(DockFrame):
432              self.application.SaveSession()              self.application.SaveSession()
433    
434      def SaveSessionAs(self):      def SaveSessionAs(self):
435          dlg = wxFileDialog(self, _("Save Session As"), ".", "",          dlg = wxFileDialog(self, _("Save Session As"),
436                               self.application.Path("data"), "",
437                             "Thuban Session File (*.thuban)|*.thuban",                             "Thuban Session File (*.thuban)|*.thuban",
438                             wxSAVE|wxOVERWRITE_PROMPT)                             wxSAVE|wxOVERWRITE_PROMPT)
439          if dlg.ShowModal() == wxID_OK:          if dlg.ShowModal() == wxID_OK:
440              self.application.session.SetFilename(dlg.GetPath())              self.application.session.SetFilename(dlg.GetPath())
441              self.application.SaveSession()              self.application.SaveSession()
442                self.application.SetPath("data",dlg.GetPath())
443          dlg.Destroy()          dlg.Destroy()
444    
445      def Exit(self):      def Exit(self):
# Line 468  class MainWindow(DockFrame): Line 504  class MainWindow(DockFrame):
504          dialog.Raise()          dialog.Raise()
505    
506      def AddLayer(self):      def AddLayer(self):
507          dlg = wxFileDialog(self, _("Select one or more data files"), ".", "",          dlg = wxFileDialog(self, _("Select one or more data files"),
508                               self.application.Path("data"), "",
509                             _("Shapefiles (*.shp)") + "|*.shp;*.SHP|" +                             _("Shapefiles (*.shp)") + "|*.shp;*.SHP|" +
510                             _("All Files (*.*)") + "|*.*",                             _("All Files (*.*)") + "|*.*",
511                             wxOPEN | wxMULTIPLE)                             wxOPEN | wxMULTIPLE)
# Line 491  class MainWindow(DockFrame): Line 528  class MainWindow(DockFrame):
528                          # if we're adding a layer to an empty map, fit the                          # if we're adding a layer to an empty map, fit the
529                          # new map to the window                          # new map to the window
530                          self.canvas.FitMapToWindow()                          self.canvas.FitMapToWindow()
531                        self.application.SetPath("data",filename)
532          dlg.Destroy()          dlg.Destroy()
533    
534      def AddRasterLayer(self):      def AddRasterLayer(self):
535          dlg = wxFileDialog(self, _("Select an image file"), ".", "", "*.*",          dlg = wxFileDialog(self, _("Select an image file"),
536                               self.application.Path("data"), "", "*.*",
537                             wxOPEN)                             wxOPEN)
538          if dlg.ShowModal() == wxID_OK:          if dlg.ShowModal() == wxID_OK:
539              filename = dlg.GetPath()              filename = dlg.GetPath()
# Line 513  class MainWindow(DockFrame): Line 552  class MainWindow(DockFrame):
552                      # if we're adding a layer to an empty map, fit the                      # if we're adding a layer to an empty map, fit the
553                      # new map to the window                      # new map to the window
554                      self.canvas.FitMapToWindow()                      self.canvas.FitMapToWindow()
555                    self.application.SetPath("data", filename)
556          dlg.Destroy()          dlg.Destroy()
557    
558      def AddDBLayer(self):      def AddDBLayer(self):
# Line 521  class MainWindow(DockFrame): Line 561  class MainWindow(DockFrame):
561          dlg = ChooseDBTableDialog(self, self.application.Session())          dlg = ChooseDBTableDialog(self, self.application.Session())
562    
563          if dlg.ShowModal() == wxID_OK:          if dlg.ShowModal() == wxID_OK:
564              dbconn, dbtable = dlg.GetTable()              dbconn, dbtable, id_column, geo_column = dlg.GetTable()
565              try:              try:
566                  title = str(dbtable)                  title = str(dbtable)
567    
568                  # Chose the correct Interface for the database type                  # Chose the correct Interface for the database type
569                  store = PostGISShapeStore(dbconn, dbtable)                  store = session.OpenDBShapeStore(dbconn, dbtable,
570                  session.AddShapeStore(store)                                                   id_column = id_column,
571                                                     geometry_column = geo_column)
572                  layer = Layer(title, store)                  layer = Layer(title, store)
573              except:              except:
574                  # Some error occured while initializing the layer                  # Some error occured while initializing the layer
575                  self.RunMessageBox(_("Add Layer from database"),                  self.RunMessageBox(_("Add Layer from database"),
576                                     _("Can't open the database table '%s'")                                     _("Can't open the database table '%s'")
577                                     % dbtable)                                     % dbtable)
578                    return
579    
580              map = self.canvas.Map()              map = self.canvas.Map()
581    
# Line 562  class MainWindow(DockFrame): Line 604  class MainWindow(DockFrame):
604              return self.canvas.Map().CanRemoveLayer(layer)              return self.canvas.Map().CanRemoveLayer(layer)
605          return False          return False
606    
607        def LayerToTop(self):
608            layer = self.current_layer()
609            if layer is not None:
610                self.canvas.Map().MoveLayerToTop(layer)
611    
612      def RaiseLayer(self):      def RaiseLayer(self):
613          layer = self.current_layer()          layer = self.current_layer()
614          if layer is not None:          if layer is not None:
# Line 572  class MainWindow(DockFrame): Line 619  class MainWindow(DockFrame):
619          if layer is not None:          if layer is not None:
620              self.canvas.Map().LowerLayer(layer)              self.canvas.Map().LowerLayer(layer)
621    
622        def LayerToBottom(self):
623            layer = self.current_layer()
624            if layer is not None:
625                self.canvas.Map().MoveLayerToBottom(layer)
626    
627      def current_layer(self):      def current_layer(self):
628          """Return the currently selected layer.          """Return the currently selected layer.
629    
# Line 594  class MainWindow(DockFrame): Line 646  class MainWindow(DockFrame):
646      def HideLayer(self):      def HideLayer(self):
647          layer = self.current_layer()          layer = self.current_layer()
648          if layer is not None:          if layer is not None:
649              layer.SetVisible(0)              layer.SetVisible(False)
650    
651      def ShowLayer(self):      def ShowLayer(self):
652          layer = self.current_layer()          layer = self.current_layer()
653          if layer is not None:          if layer is not None:
654              layer.SetVisible(1)              layer.SetVisible(True)
655    
656        def ToggleLayerVisibility(self):
657            layer = self.current_layer()
658            layer.SetVisible(not layer.Visible())
659    
660      def DuplicateLayer(self):      def DuplicateLayer(self):
661          """Ceate a new layer above the selected layer with the same shapestore          """Ceate a new layer above the selected layer with the same shapestore
# Line 610  class MainWindow(DockFrame): Line 666  class MainWindow(DockFrame):
666                                layer.ShapeStore(),                                layer.ShapeStore(),
667                                projection = layer.GetProjection())                                projection = layer.GetProjection())
668              new_classification = copy.deepcopy(layer.GetClassification())              new_classification = copy.deepcopy(layer.GetClassification())
669                new_layer.SetClassificationColumn(
670                        layer.GetClassificationColumn())
671              new_layer.SetClassification(new_classification)              new_layer.SetClassification(new_classification)
672              self.Map().AddLayer(new_layer)              self.Map().AddLayer(new_layer)
673    
# Line 619  class MainWindow(DockFrame): Line 677  class MainWindow(DockFrame):
677          return layer is not None and hasattr(layer, "ShapeStore")          return layer is not None and hasattr(layer, "ShapeStore")
678    
679      def LayerShowTable(self):      def LayerShowTable(self):
680            """
681            Present a TableView Window for the current layer.
682            In case the window is already open, bring it to the front.
683            In case, there is no active layer, do nothing.
684            In case, the layer has no ShapeStore, do nothing.
685            """
686          layer = self.current_layer()          layer = self.current_layer()
687          if layer is not None:          if layer is not None:
688                if not hasattr(layer, "ShapeStore"):
689                    return
690              table = layer.ShapeStore().Table()              table = layer.ShapeStore().Table()
691              name = "table_view" + str(id(table))              name = "table_view" + str(id(table))
692              dialog = self.get_open_dialog(name)              dialog = self.get_open_dialog(name)
# Line 631  class MainWindow(DockFrame): Line 697  class MainWindow(DockFrame):
697                  self.add_dialog(name, dialog)                  self.add_dialog(name, dialog)
698                  dialog.Show(True)                  dialog.Show(True)
699              else:              else:
700                  # FIXME: bring dialog to front here                  dialog.Raise()
                 pass  
701    
702      def MapProjection(self):      def MapProjection(self):
703    
# Line 674  class MainWindow(DockFrame): Line 739  class MainWindow(DockFrame):
739          self.OpenLayerProperties(layer)          self.OpenLayerProperties(layer)
740    
741      def OpenLayerProperties(self, layer, group = None):      def OpenLayerProperties(self, layer, group = None):
742          name = "layer_properties" + str(id(layer))          """
743          dialog = self.get_open_dialog(name)          Open or raise the properties dialog.
744    
745          if dialog is None:          This method opens or raises the properties dialog for the
746              dialog = Classifier(self, name, self.Map(), layer, group)          currently selected layer if one is defined for this layer
747              self.add_dialog(name, dialog)          type.
748              dialog.Show()          """
749          dialog.Raise()          dialog_class = layer_properties_dialogs.get(layer)
750    
751            if dialog_class is not None:
752                name = "layer_properties" + str(id(layer))
753                self.OpenOrRaiseDialog(name, dialog_class, layer, group = group)
754    
755      def LayerJoinTable(self):      def LayerJoinTable(self):
756          layer = self.canvas.SelectedLayer()          layer = self.canvas.SelectedLayer()
# Line 722  class MainWindow(DockFrame): Line 791  class MainWindow(DockFrame):
791          return dialog is not None and dialog.IsShown()          return dialog is not None and dialog.IsShown()
792    
793      def TableOpen(self):      def TableOpen(self):
794          dlg = wxFileDialog(self, _("Open Table"), ".", "",          dlg = wxFileDialog(self, _("Open Table"),
795                               self.application.Path("data"), "",
796                             _("DBF Files (*.dbf)") + "|*.dbf|" +                             _("DBF Files (*.dbf)") + "|*.dbf|" +
797                             #_("CSV Files (*.csv)") + "|*.csv|" +                             #_("CSV Files (*.csv)") + "|*.csv|" +
798                             _("All Files (*.*)") + "|*.*",                             _("All Files (*.*)") + "|*.*",
# Line 738  class MainWindow(DockFrame): Line 808  class MainWindow(DockFrame):
808                                     _("Can't open the file '%s'.") % filename)                                     _("Can't open the file '%s'.") % filename)
809              else:              else:
810                  self.ShowTableView(table)                  self.ShowTableView(table)
811                    self.application.SetPath("data",filename)
812    
813      def TableClose(self):      def TableClose(self):
814          tables = self.application.session.UnreferencedTables()          tables = self.application.session.UnreferencedTables()
# Line 975  def _has_selected_layer(context): Line 1046  def _has_selected_layer(context):
1046      """Return true if a layer is selected in the context"""      """Return true if a layer is selected in the context"""
1047      return context.mainwindow.has_selected_layer()      return context.mainwindow.has_selected_layer()
1048    
1049    def _has_selected_layer_visible(context):
1050        """Return true if a layer is selected in the context which is
1051        visible."""
1052        if context.mainwindow.has_selected_layer():
1053            layer = context.mainwindow.current_layer()
1054            if layer.Visible(): return True
1055        return False
1056    
1057  def _has_selected_shape_layer(context):  def _has_selected_shape_layer(context):
1058      """Return true if a shape layer is selected in the context"""      """Return true if a shape layer is selected in the context"""
1059      return context.mainwindow.has_selected_shape_layer()      return context.mainwindow.has_selected_shape_layer()
# Line 998  def _has_visible_map(context): Line 1077  def _has_visible_map(context):
1077      if map is not None:      if map is not None:
1078          for layer in map.Layers():          for layer in map.Layers():
1079              if layer.Visible():              if layer.Visible():
1080                  return 1                  return True
1081      return 0      return False
1082    
1083  def _has_legend_shown(context):  def _has_legend_shown(context):
1084      """Return true if the legend window is shown"""      """Return true if the legend window is shown"""
# Line 1123  _method_command("layer_jointable", _("&J Line 1202  _method_command("layer_jointable", _("&J
1202                  sensitive = _has_selected_shape_layer,                  sensitive = _has_selected_shape_layer,
1203                  helptext = _("Join and attach a table to the selected layer"))                  helptext = _("Join and attach a table to the selected layer"))
1204    
1205    # further layer methods:
1206    _method_command("layer_to_top", _("&Top"), "LayerToTop",
1207                    helptext = _("Put selected layer to the top"),
1208                    sensitive = _has_selected_layer)
1209    _method_command("layer_to_bottom", _("&Bottom"), "LayerToBottom",
1210                    helptext = _("Put selected layer to the bottom"),
1211                    sensitive = _has_selected_layer)
1212    _method_command("layer_visibility", _("&Visible"), "ToggleLayerVisibility",
1213                    checked = _has_selected_layer_visible,
1214                    helptext = _("Toggle visibility of selected layer"),
1215                    sensitive = _has_selected_layer)
1216    
1217  def _can_unjoin(context):  def _can_unjoin(context):
1218      """Return whether the Layer/Unjoin command can be executed.      """Return whether the Layer/Unjoin command can be executed.
1219    

Legend:
Removed from v.2040  
changed lines
  Added in v.2529

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26