/[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 1140 by jan, Fri Jun 6 11:18:51 2003 UTC revision 1644 by bh, Mon Aug 25 12:44:55 2003 UTC
# Line 12  The main window Line 12  The main window
12  """  """
13    
14  __version__ = "$Revision$"  __version__ = "$Revision$"
15    # $Source$
16  __ThubanVersion__ = "0.2" #"$THUBAN_0_2$"  # $Id$
 #__BuildDate__ = "$Date$"  
17    
18  import os  import os
19  import copy  import copy
# Line 22  import copy Line 21  import copy
21  from wxPython.wx import *  from wxPython.wx import *
22  from wxPython.wx import __version__ as wxPython_version  from wxPython.wx import __version__ as wxPython_version
23    
 from wxPython.lib.dialogs import wxMultipleChoiceDialog  
   
24  import Thuban  import Thuban
25  import Thuban.version  import Thuban.version
26    
27  from Thuban import _  from Thuban import _
28  from Thuban.Model.session import create_empty_session  from Thuban.Model.session import create_empty_session
29  from Thuban.Model.layer import Layer, RasterLayer  from Thuban.Model.layer import Layer, RasterLayer
30    from Thuban.Model.postgisdb import PostGISShapeStore, has_postgis_support
31    # XXX: replace this by
32    # from wxPython.lib.dialogs import wxMultipleChoiceDialog
33    # when Thuban does not support wxPython 2.4.0 any more.
34    from Thuban.UI.multiplechoicedialog import wxMultipleChoiceDialog
35    
36  import view  import view
37  import tree  import tree
# Line 40  from menu import Menu Line 42  from menu import Menu
42    
43  from context import Context  from context import Context
44  from command import registry, Command, ToolCommand  from command import registry, Command, ToolCommand
45  from messages import LAYER_SELECTED, SHAPES_SELECTED, VIEW_POSITION  from messages import LAYER_SELECTED, SHAPES_SELECTED, VIEW_POSITION, \
46         MAP_REPLACED
47    from about import About
48    
49  from Thuban.UI.dock import DockFrame  from Thuban.UI.dock import DockFrame
50  from Thuban.UI.join import JoinDialog  from Thuban.UI.join import JoinDialog
51    from Thuban.UI.dbdialog import DBFrame, ChooseDBTableDialog
52  import resource  import resource
53    import Thuban.Model.resource
54    
55  import projdialog  import projdialog
56    
57    
   
58  class MainWindow(DockFrame):  class MainWindow(DockFrame):
59    
60      # Some messages that can be subscribed/unsubscribed directly through      # Some messages that can be subscribed/unsubscribed directly through
# Line 59  class MainWindow(DockFrame): Line 63  class MainWindow(DockFrame):
63      # actually come from. This delegation is implemented in the      # actually come from. This delegation is implemented in the
64      # Subscribe and unsubscribed methods      # Subscribe and unsubscribed methods
65      delegated_messages = {LAYER_SELECTED: "canvas",      delegated_messages = {LAYER_SELECTED: "canvas",
66                            SHAPES_SELECTED: "canvas"}                            SHAPES_SELECTED: "canvas",
67                              MAP_REPLACED: "canvas"}
68    
69      # Methods delegated to some instance variables. The delegation is      # Methods delegated to some instance variables. The delegation is
70      # implemented in the __getattr__ method.      # implemented in the __getattr__ method.
# Line 105  class MainWindow(DockFrame): Line 110  class MainWindow(DockFrame):
110    
111          self.init_dialogs()          self.init_dialogs()
112    
113            self.ShowLegend()
114    
115          EVT_CLOSE(self, self.OnClose)          EVT_CLOSE(self, self.OnClose)
116    
117      def Subscribe(self, channel, *args):      def Subscribe(self, channel, *args):
# Line 358  class MainWindow(DockFrame): Line 365  class MainWindow(DockFrame):
365              result = wxID_NO              result = wxID_NO
366          return result          return result
367    
     def prepare_new_session(self):  
         for d in self.dialogs.values():  
             if not isinstance(d, tree.SessionTreeView):  
                 d.Close()  
   
368      def NewSession(self):      def NewSession(self):
369          if self.save_modified_session() != wxID_CANCEL:          if self.save_modified_session() != wxID_CANCEL:
             self.prepare_new_session()  
370              self.application.SetSession(create_empty_session())              self.application.SetSession(create_empty_session())
371    
372      def OpenSession(self):      def OpenSession(self):
# Line 374  class MainWindow(DockFrame): Line 375  class MainWindow(DockFrame):
375                                 "Thuban Session File (*.thuban)|*.thuban",                                 "Thuban Session File (*.thuban)|*.thuban",
376                                 wxOPEN)                                 wxOPEN)
377              if dlg.ShowModal() == wxID_OK:              if dlg.ShowModal() == wxID_OK:
                 self.prepare_new_session()  
378                  self.application.OpenSession(dlg.GetPath())                  self.application.OpenSession(dlg.GetPath())
379              dlg.Destroy()              dlg.Destroy()
380    
# Line 440  class MainWindow(DockFrame): Line 440  class MainWindow(DockFrame):
440          return self.get_open_dialog("session_tree") is not None          return self.get_open_dialog("session_tree") is not None
441    
442      def About(self):      def About(self):
443          self.RunMessageBox(_("About"),          dlg = About(self)
444                             _("Thuban %s\n"          dlg.ShowModal()
445                              #"Build Date: %s\n"          dlg.Destroy()
446                              "using:\n"  
447                              "  %s\n"      def DatabaseManagement(self):
448                              "  %s\n\n"          name = "dbmanagement"
449                              "Thuban is a program for\n"          dialog = self.get_open_dialog(name)
450                              "exploring geographic data.\n"          if dialog is None:
451                              "Copyright (C) 2001-2003 Intevation GmbH.\n"              map = self.canvas.Map()
452                              "Thuban is licensed under the GNU GPL"              dialog = DBFrame(self, name, self.application.Session())
453                              % (Thuban.version.longversion,              self.add_dialog(name, dialog)
454                                 "wxPython %s" % wxPython_version,              dialog.Show()
455                                 "Python %d.%d.%d" % sys.version_info[:3]          dialog.Raise()
                               )),  
 #                           % __ThubanVersion__), #__BuildDate__)),  
                            wxOK | wxICON_INFORMATION)  
456    
457      def AddLayer(self):      def AddLayer(self):
458          dlg = wxFileDialog(self, _("Select a data file"), ".", "", "*.*",          dlg = wxFileDialog(self, _("Select one or more data files"), ".", "",
459                             wxOPEN)                             _("Shapefiles (*.shp)") + "|*.shp|" +
460                               _("All Files (*.*)") + "|*.*",
461                               wxOPEN | wxMULTIPLE)
462          if dlg.ShowModal() == wxID_OK:          if dlg.ShowModal() == wxID_OK:
463              filename = dlg.GetPath()              filenames = dlg.GetPaths()
464              title = os.path.splitext(os.path.basename(filename))[0]              for filename in filenames:
465              map = self.canvas.Map()                  title = os.path.splitext(os.path.basename(filename))[0]
466              has_layers = map.HasLayers()                  map = self.canvas.Map()
467              try:                  has_layers = map.HasLayers()
468                  store = self.application.Session().OpenShapefile(filename)                  try:
469              except IOError:                      store = self.application.Session().OpenShapefile(filename)
470                  # the layer couldn't be opened                  except IOError:
471                  self.RunMessageBox(_("Add Layer"),                      # the layer couldn't be opened
472                                     _("Can't open the file '%s'.") % filename)                      self.RunMessageBox(_("Add Layer"),
473              else:                                         _("Can't open the file '%s'.")%filename)
474                  layer = Layer(title, store)                  else:
475                  map.AddLayer(layer)                      layer = Layer(title, store)
476                  if not has_layers:                      map.AddLayer(layer)
477                      # if we're adding a layer to an empty map, fit the                      if not has_layers:
478                      # new map to the window                          # if we're adding a layer to an empty map, fit the
479                      self.canvas.FitMapToWindow()                          # new map to the window
480                            self.canvas.FitMapToWindow()
481          dlg.Destroy()          dlg.Destroy()
482    
483      def AddRasterLayer(self):      def AddRasterLayer(self):
# Line 502  class MainWindow(DockFrame): Line 502  class MainWindow(DockFrame):
502                      self.canvas.FitMapToWindow()                      self.canvas.FitMapToWindow()
503          dlg.Destroy()          dlg.Destroy()
504    
505        def AddDBLayer(self):
506            """Add a layer read from a database"""
507            session = self.application.Session()
508            dlg = ChooseDBTableDialog(self.application.Session(), self,-1, "")
509    
510            if dlg.ShowModal() == wxID_OK:
511                dbconn, dbtable = dlg.GetTable()
512                try:
513                    title = str(dbtable)
514    
515                    # Chose the correct Interface for the database type
516                    store = PostGISShapeStore(dbconn, dbtable)
517                    session.AddShapeStore(store)
518                    layer = Layer(title, store)
519                except:
520                    # Some error occured while initializing the layer
521                    self.RunMessageBox(_("Add Layer from database"),
522                                       _("Can't open the database table '%s'")
523                                       % dbtable)
524    
525                map = self.canvas.Map()
526    
527                has_layers = map.HasLayers()
528                map.AddLayer(layer)
529                if not has_layers:
530                    self.canvas.FitMapToWindow()
531    
532            dlg.Destroy()
533    
534      def RemoveLayer(self):      def RemoveLayer(self):
535          layer = self.current_layer()          layer = self.current_layer()
536          if layer is not None:          if layer is not None:
# Line 575  class MainWindow(DockFrame): Line 604  class MainWindow(DockFrame):
604      def LayerShowTable(self):      def LayerShowTable(self):
605          layer = self.current_layer()          layer = self.current_layer()
606          if layer is not None:          if layer is not None:
607              table = layer.table              table = layer.ShapeStore().Table()
608              name = "table_view" + str(id(table))              name = "table_view" + str(id(table))
609              dialog = self.get_open_dialog(name)              dialog = self.get_open_dialog(name)
610              if dialog is None:              if dialog is None:
# Line 632  class MainWindow(DockFrame): Line 661  class MainWindow(DockFrame):
661          dialog = self.get_open_dialog(name)          dialog = self.get_open_dialog(name)
662    
663          if dialog is None:          if dialog is None:
664              dialog = Classifier(self, name, layer, group)              dialog = Classifier(self, name, self.Map(), layer, group)
665              self.add_dialog(name, dialog)              self.add_dialog(name, dialog)
666              dialog.Show()              dialog.Show()
667          dialog.Raise()          dialog.Raise()
# Line 670  class MainWindow(DockFrame): Line 699  class MainWindow(DockFrame):
699          else:          else:
700              dialog.Show(not dialog.IsShown())              dialog.Show(not dialog.IsShown())
701    
         self.canvas.FitMapToWindow()  
   
702      def LegendShown(self):      def LegendShown(self):
703          """Return true iff the legend is currently open"""          """Return true iff the legend is currently open"""
704          dialog = self.FindRegisteredDock("legend")          dialog = self.FindRegisteredDock("legend")
# Line 747  class MainWindow(DockFrame): Line 774  class MainWindow(DockFrame):
774                                                 table)                                                 table)
775              self.add_dialog(name, dialog)              self.add_dialog(name, dialog)
776              dialog.Show(True)              dialog.Show(True)
777          # FIXME: else bring dialog to front          dialog.Raise()
778    
779      def TableRename(self):      def TableRename(self):
780          """Let the user rename a table"""          """Let the user rename a table"""
# Line 936  def _has_legend_shown(context): Line 963  def _has_legend_shown(context):
963      """Return true if the legend window is shown"""      """Return true if the legend window is shown"""
964      return context.mainwindow.LegendShown()      return context.mainwindow.LegendShown()
965    
966    def _has_gdal_support(context):
967        """Return True if the GDAL is available"""
968        return Thuban.Model.resource.has_gdal_support()
969    
970    def _has_dbconnections(context):
971        """Return whether the the session has database connections"""
972        return context.session.HasDBConnections()
973    
974    def _has_postgis_support(context):
975        return has_postgis_support()
976    
977    
978  # File menu  # File menu
979  _method_command("new_session", _("&New Session"), "NewSession",  _method_command("new_session", _("&New Session"), "NewSession",
# Line 952  _method_command("toggle_session_tree", _ Line 990  _method_command("toggle_session_tree", _
990  _method_command("toggle_legend", _("Legend"), "ToggleLegend",  _method_command("toggle_legend", _("Legend"), "ToggleLegend",
991                  checked = _has_legend_shown,                  checked = _has_legend_shown,
992                  helptext = _("Toggle Legend on/off"))                  helptext = _("Toggle Legend on/off"))
993    _method_command("database_management", _("&Database Connections..."),
994                    "DatabaseManagement",
995                    sensitive = _has_postgis_support)
996  _method_command("exit", _("E&xit"), "Exit",  _method_command("exit", _("E&xit"), "Exit",
997                  helptext = _("Finish working with Thuban"))                  helptext = _("Finish working with Thuban"))
998    
# Line 999  _method_command("map_rename", _("&Rename Line 1040  _method_command("map_rename", _("&Rename
1040  _method_command("layer_add", _("&Add Layer..."), "AddLayer",  _method_command("layer_add", _("&Add Layer..."), "AddLayer",
1041                  helptext = _("Add a new layer to the map"))                  helptext = _("Add a new layer to the map"))
1042  _method_command("rasterlayer_add", _("&Add Image Layer..."), "AddRasterLayer",  _method_command("rasterlayer_add", _("&Add Image Layer..."), "AddRasterLayer",
1043                  helptext = _("Add a new image layer to the map"))                  helptext = _("Add a new image layer to the map"),
1044                    sensitive = _has_gdal_support)
1045    _method_command("layer_add_db", _("Add &Database Layer..."), "AddDBLayer",
1046                    helptext = _("Add a new database layer to active map"),
1047                    sensitive = _has_dbconnections)
1048  _method_command("layer_remove", _("&Remove Layer"), "RemoveLayer",  _method_command("layer_remove", _("&Remove Layer"), "RemoveLayer",
1049                  helptext = _("Remove selected layer"),                  helptext = _("Remove selected layer"),
1050                  sensitive = _can_remove_layer)                  sensitive = _can_remove_layer)
# Line 1075  _method_command("table_join", _("&Join.. Line 1120  _method_command("table_join", _("&Join..
1120                  helptext = _("Join two tables creating a new one"))                  helptext = _("Join two tables creating a new one"))
1121    
1122  #  Export only under Windows ...  #  Export only under Windows ...
1123  map_menu = ["layer_add", "rasterlayer_add", "layer_remove", "map_rename",  map_menu = ["layer_add", "layer_add_db", "rasterlayer_add", "layer_remove",
1124                          None,                          None,
1125                            "map_rename",
1126                          "map_projection",                          "map_projection",
1127                          None,                          None,
1128                          "map_zoom_in_tool", "map_zoom_out_tool",                          "map_zoom_in_tool", "map_zoom_out_tool",
1129                          "map_pan_tool",                          "map_pan_tool",
1130                          "map_full_extent",                          "map_full_extent",
1131                          "layer_full_extent",                          "layer_full_extent",
1132                          "selected_full_extent",                          "selected_full_extent",
1133                          None,                          None,
# Line 1098  main_menu = Menu("<main>", "<main>", Line 1144  main_menu = Menu("<main>", "<main>",
1144                   [Menu("file", _("&File"),                   [Menu("file", _("&File"),
1145                         ["new_session", "open_session", None,                         ["new_session", "open_session", None,
1146                          "save_session", "save_session_as", None,                          "save_session", "save_session_as", None,
1147                            "database_management", None,
1148                          "toggle_session_tree", None,                          "toggle_session_tree", None,
1149                          "exit"]),                          "exit"]),
1150                    Menu("map", _("&Map"), map_menu),                    Menu("map", _("&Map"), map_menu),
# Line 1133  main_toolbar = Menu("<toolbar>", "<toolb Line 1180  main_toolbar = Menu("<toolbar>", "<toolb
1180                       "selected_full_extent",                       "selected_full_extent",
1181                       None,                       None,
1182                       "map_identify_tool", "map_label_tool"])                       "map_identify_tool", "map_label_tool"])
1183    

Legend:
Removed from v.1140  
changed lines
  Added in v.1644

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26