/[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 346 by bh, Wed Oct 23 14:45:28 2002 UTC revision 1094 by bh, Wed May 28 18:59:46 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]>
5    # Frank Koormann <[email protected]>
6  #  #
7  # This program is free software under the GPL (>=v2)  # This program is free software under the GPL (>=v2)
8  # Read the file COPYING coming with Thuban for details.  # Read the file COPYING coming with Thuban for details.
# Line 12  The main window Line 13  The main window
13    
14  __version__ = "$Revision$"  __version__ = "$Revision$"
15    
16    __ThubanVersion__ = "0.2" #"$THUBAN_0_2$"
17    #__BuildDate__ = "$Date$"
18    
19  import os  import os
20    import copy
21    
22  from wxPython.wx import *  from wxPython.wx import *
23    from wxPython.wx import __version__ as wxPython_version
24    
25    from wxPython.lib.dialogs import wxMultipleChoiceDialog
26    
27  import Thuban  import Thuban
28    import Thuban.version
29    
30    from Thuban import _
31  from Thuban.Model.session import create_empty_session  from Thuban.Model.session import create_empty_session
32  from Thuban.Model.layer import Layer  from Thuban.Model.layer import Layer, RasterLayer
 from Thuban.Model.color import Color  
 from Thuban.Model.proj import Projection  
33    
34  import view  import view
35  import tree  import tree
 import proj4dialog  
36  import tableview, identifyview  import tableview, identifyview
37    from Thuban.UI.classifier import Classifier
38    import legend
39  from menu import Menu  from menu import Menu
40    
41  from context import Context  from context import Context
42  from command import registry, Command  from command import registry, Command, ToolCommand
43  from messages import SELECTED_SHAPE, VIEW_POSITION  from messages import LAYER_SELECTED, SHAPES_SELECTED, VIEW_POSITION
44    
45    from Thuban.UI.dock import DockFrame
46    from Thuban.UI.join import JoinDialog
47    
48    import resource
49    
50    import projdialog
51    
 # the directory where the toolbar icons are stored  
 bitmapdir = os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Bitmaps")  
 bitmapext = ".xpm"  
52    
53    
54  class MainWindow(wxFrame):  class MainWindow(DockFrame):
55    
56        # Some messages that can be subscribed/unsubscribed directly through
57        # the MapCanvas come in fact from other objects. This is a map to
58        # map those messages to the names of the instance variables they
59        # actually come from. This delegation is implemented in the
60        # Subscribe and unsubscribed methods
61        delegated_messages = {LAYER_SELECTED: "canvas",
62                              SHAPES_SELECTED: "canvas"}
63    
64        # Methods delegated to some instance variables. The delegation is
65        # implemented in the __getattr__ method.
66        delegated_methods = {"SelectLayer": "canvas",
67                             "SelectShapes": "canvas",
68                             "SelectedLayer": "canvas",
69                             "SelectedShapes": "canvas",
70                             }
71    
72      def __init__(self, parent, ID, title, application, interactor,      def __init__(self, parent, ID, title, application, interactor,
73                   initial_message = None, size = wxSize(-1, -1)):                   initial_message = None, size = wxSize(-1, -1)):
74          wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, size)          DockFrame.__init__(self, parent, ID, title, wxDefaultPosition, size)
75            #wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, size)
76    
77          self.application = application          self.application = application
         self.interactor = interactor  
78    
79          self.CreateStatusBar()          self.CreateStatusBar()
80          if initial_message:          if initial_message:
# Line 63  class MainWindow(wxFrame): Line 92  class MainWindow(wxFrame):
92          # call Realize to make sure that the tools appear.          # call Realize to make sure that the tools appear.
93          toolbar.Realize()          toolbar.Realize()
94    
95    
96          # Create the map canvas          # Create the map canvas
97          canvas = view.MapCanvas(self, -1, interactor)          canvas = view.MapCanvas(self, -1)
98          canvas.Subscribe(VIEW_POSITION, self.view_position_changed)          canvas.Subscribe(VIEW_POSITION, self.view_position_changed)
99            canvas.Subscribe(SHAPES_SELECTED, self.identify_view_on_demand)
100          self.canvas = canvas          self.canvas = canvas
101    
102          self.init_dialogs()          self.SetMainWindow(self.canvas)
103    
104          interactor.Subscribe(SELECTED_SHAPE, self.identify_view_on_demand)          self.SetAutoLayout(True)
105    
106            self.init_dialogs()
107    
108          EVT_CLOSE(self, self.OnClose)          EVT_CLOSE(self, self.OnClose)
109    
110        def Subscribe(self, channel, *args):
111            """Subscribe a function to a message channel.
112    
113            If channel is one of the delegated messages call the appropriate
114            object's Subscribe method. Otherwise do nothing.
115            """
116            if channel in self.delegated_messages:
117                object = getattr(self, self.delegated_messages[channel])
118                object.Subscribe(channel, *args)
119            else:
120                print "Trying to subscribe to unsupported channel %s" % channel
121    
122        def Unsubscribe(self, channel, *args):
123            """Unsubscribe a function from a message channel.
124    
125            If channel is one of the delegated messages call the appropriate
126            object's Unsubscribe method. Otherwise do nothing.
127            """
128            if channel in self.delegated_messages:
129                object = getattr(self, self.delegated_messages[channel])
130                object.Unsubscribe(channel, *args)
131    
132        def __getattr__(self, attr):
133            """If attr is one of the delegated methods return that method
134    
135            Otherwise raise AttributeError.
136            """
137            if attr in self.delegated_methods:
138                return getattr(getattr(self, self.delegated_methods[attr]), attr)
139            raise AttributeError(attr)
140    
141      def init_ids(self):      def init_ids(self):
142          """Initialize the ids"""          """Initialize the ids"""
143          self.current_id = 6000          self.current_id = 6000
# Line 168  class MainWindow(wxFrame): Line 232  class MainWindow(wxFrame):
232                              command.IsCheckCommand())                              command.IsCheckCommand())
233                  self.bind_command_events(command, ID)                  self.bind_command_events(command, ID)
234              else:              else:
235                  print "Unknown command %s" % name                  print _("Unknown command %s") % name
236    
237      def add_toolbar_command(self, toolbar, name):      def add_toolbar_command(self, toolbar, name):
238          """Add the command with name name to the toolbar toolbar.          """Add the command with name name to the toolbar toolbar.
# Line 183  class MainWindow(wxFrame): Line 247  class MainWindow(wxFrame):
247              command = registry.Command(name)              command = registry.Command(name)
248              if command is not None:              if command is not None:
249                  ID = self.get_id(name)                  ID = self.get_id(name)
250                  filename = os.path.join(bitmapdir, command.Icon()) + bitmapext                  bitmap = resource.GetBitmapResource(command.Icon(),
251                  bitmap = wxBitmap(filename, wxBITMAP_TYPE_XPM)                                                      wxBITMAP_TYPE_XPM)
252                  toolbar.AddTool(ID, bitmap,                  toolbar.AddTool(ID, bitmap,
253                                  shortHelpString = command.HelpText(),                                  shortHelpString = command.HelpText(),
254                                  isToggle = command.IsCheckCommand())                                  isToggle = command.IsCheckCommand())
255                  self.bind_command_events(command, ID)                  self.bind_command_events(command, ID)
256              else:              else:
257                  print "Unknown command %s" % name                  print _("Unknown command %s") % name
258    
259      def Context(self):      def Context(self):
260          """Return the context object for a command invoked from this window          """Return the context object for a command invoked from this window
# Line 203  class MainWindow(wxFrame): Line 267  class MainWindow(wxFrame):
267              command = registry.Command(name)              command = registry.Command(name)
268              command.Execute(self.Context())              command.Execute(self.Context())
269          else:          else:
270              print "Unknown command ID %d" % event.GetId()              print _("Unknown command ID %d") % event.GetId()
271    
272      def update_command_ui(self, event):      def update_command_ui(self, event):
273          #print "update_command_ui", self.id_to_name[event.GetId()]          #print "update_command_ui", self.id_to_name[event.GetId()]
274          context = self.Context()          context = self.Context()
275          command = registry.Command(self.id_to_name[event.GetId()])          command = registry.Command(self.id_to_name[event.GetId()])
276          if command is not None:          if command is not None:
277              event.Enable(command.Sensitive(context))              sensitive = command.Sensitive(context)
278                event.Enable(sensitive)
279                if command.IsTool() and not sensitive and command.Checked(context):
280                    # When a checked tool command is disabled deselect all
281                    # tools. Otherwise the tool would remain active but it
282                    # might lead to errors if the tools stays active. This
283                    # problem occurred in GREAT-ER and this fixes it, but
284                    # it's not clear to me whether this is really the best
285                    # way to do it (BH, 20021206).
286                    self.canvas.SelectTool(None)
287              event.SetText(command.DynText(context))              event.SetText(command.DynText(context))
288              if command.IsCheckCommand():              if command.IsCheckCommand():
289                  event.Check(command.Checked(context))                      event.Check(command.Checked(context))
290    
291      def RunMessageBox(self, title, text, flags = wxOK | wxICON_INFORMATION):      def RunMessageBox(self, title, text, flags = wxOK | wxICON_INFORMATION):
292          """Run a modal message box with the given text, title and flags          """Run a modal message box with the given text, title and flags
# Line 233  class MainWindow(wxFrame): Line 306  class MainWindow(wxFrame):
306    
307      def add_dialog(self, name, dialog):      def add_dialog(self, name, dialog):
308          if self.dialogs.has_key(name):          if self.dialogs.has_key(name):
309              raise RuntimeError("The Dialog named %s is already open" % name)              raise RuntimeError(_("The Dialog named %s is already open") % name)
310          self.dialogs[name] = dialog          self.dialogs[name] = dialog
311    
312      def dialog_open(self, name):      def dialog_open(self, name):
# Line 275  class MainWindow(wxFrame): Line 348  class MainWindow(wxFrame):
348              flags = wxYES_NO | wxICON_QUESTION              flags = wxYES_NO | wxICON_QUESTION
349              if can_veto:              if can_veto:
350                  flags = flags | wxCANCEL                  flags = flags | wxCANCEL
351              result = self.RunMessageBox("Exit",              result = self.RunMessageBox(_("Exit"),
352                                          ("The session has been modified."                                          _("The session has been modified."
353                                           " Do you want to save it?"),                                           " Do you want to save it?"),
354                                          flags)                                          flags)
355              if result == wxID_YES:              if result == wxID_YES:
# Line 285  class MainWindow(wxFrame): Line 358  class MainWindow(wxFrame):
358              result = wxID_NO              result = wxID_NO
359          return result          return result
360    
361        def prepare_new_session(self):
362            for d in self.dialogs.values():
363                if not isinstance(d, tree.SessionTreeView):
364                    d.Close()
365    
366      def NewSession(self):      def NewSession(self):
367          self.save_modified_session()          if self.save_modified_session() != wxID_CANCEL:
368          self.application.SetSession(create_empty_session())              self.prepare_new_session()
369                self.application.SetSession(create_empty_session())
370    
371      def OpenSession(self):      def OpenSession(self):
372          self.save_modified_session()          if self.save_modified_session() != wxID_CANCEL:
373          dlg = wxFileDialog(self, "Select a session file", ".", "",              dlg = wxFileDialog(self, _("Open Session"), ".", "",
374                             "*.thuban", wxOPEN)                                 "Thuban Session File (*.thuban)|*.thuban",
375          if dlg.ShowModal() == wxID_OK:                                 wxOPEN)
376              self.application.OpenSession(dlg.GetPath())              if dlg.ShowModal() == wxID_OK:
377          dlg.Destroy()                  self.prepare_new_session()
378                    self.application.OpenSession(dlg.GetPath())
379                dlg.Destroy()
380    
381      def SaveSession(self):      def SaveSession(self):
382          if self.application.session.filename == None:          if self.application.session.filename == None:
383              self.SaveSessionAs()              self.SaveSessionAs()
384          self.application.SaveSession()          else:
385                self.application.SaveSession()
386    
387      def SaveSessionAs(self):      def SaveSessionAs(self):
388          dlg = wxFileDialog(self, "Enter a filename for session", ".", "",          dlg = wxFileDialog(self, _("Save Session As"), ".", "",
389                             "*.thuban", wxOPEN)                             "Thuban Session File (*.thuban)|*.thuban",
390                               wxSAVE|wxOVERWRITE_PROMPT)
391          if dlg.ShowModal() == wxID_OK:          if dlg.ShowModal() == wxID_OK:
392              self.application.session.SetFilename(dlg.GetPath())              self.application.session.SetFilename(dlg.GetPath())
393              self.application.SaveSession()              self.application.SaveSession()
394          dlg.Destroy()          dlg.Destroy()
395    
396      def Exit(self):      def Exit(self):
397          self.Close(false)          self.Close(False)
398    
399      def OnClose(self, event):      def OnClose(self, event):
400          result = self.save_modified_session(can_veto = event.CanVeto())          result = self.save_modified_session(can_veto = event.CanVeto())
# Line 322  class MainWindow(wxFrame): Line 405  class MainWindow(wxFrame):
405              # wx's destroy event, but that isn't implemented for wxGTK              # wx's destroy event, but that isn't implemented for wxGTK
406              # yet.              # yet.
407              self.canvas.Unsubscribe(VIEW_POSITION, self.view_position_changed)              self.canvas.Unsubscribe(VIEW_POSITION, self.view_position_changed)
408                DockFrame.OnClose(self, event)
409                for dlg in self.dialogs.values():
410                    dlg.Destroy()
411                self.canvas.Destroy()
412              self.Destroy()              self.Destroy()
413    
414      def SetMap(self, map):      def SetMap(self, map):
415          self.canvas.SetMap(map)          self.canvas.SetMap(map)
416            self.__SetTitle(map.Title())
417    
418            dialog = self.FindRegisteredDock("legend")
419            if dialog is not None:
420                dialog.GetPanel().SetMap(self.Map())
421    
422      def Map(self):      def Map(self):
423          """Return the map displayed by this mainwindow"""          """Return the map displayed by this mainwindow"""
424    
425          return self.canvas.Map()          return self.canvas.Map()
426    
427      def ShowSessionTree(self):      def ToggleSessionTree(self):
428            """If the session tree is shown close it otherwise create a new tree"""
429          name = "session_tree"          name = "session_tree"
430          dialog = self.get_open_dialog(name)          dialog = self.get_open_dialog(name)
431          if dialog is None:          if dialog is None:
432              dialog = tree.SessionTreeView(self, self.application, name)              dialog = tree.SessionTreeView(self, self.application, name)
433              self.add_dialog(name, dialog)              self.add_dialog(name, dialog)
434              dialog.Show(true)              dialog.Show(True)
435          else:          else:
436              # FIXME: bring dialog to front here              dialog.Close()
437              pass  
438        def SessionTreeShown(self):
439            """Return true iff the session tree is currently shown"""
440            return self.get_open_dialog("session_tree") is not None
441    
442      def About(self):      def About(self):
443          self.RunMessageBox("About",          self.RunMessageBox(_("About"),
444                             ("Thuban is a program for\n"                             _("Thuban %s\n"
445                                #"Build Date: %s\n"
446                                "using:\n"
447                                "  %s\n"
448                                "  %s\n\n"
449                                "Thuban is a program for\n"
450                              "exploring geographic data.\n"                              "exploring geographic data.\n"
451                              "Copyright (C) 2001, 2002 Intevation GmbH.\n"                              "Copyright (C) 2001-2003 Intevation GmbH.\n"
452                              "Thuban is licensed under the GPL"),                              "Thuban is licensed under the GNU GPL"
453                                % (Thuban.version.longversion,
454                                   "wxPython %s" % wxPython_version,
455                                   "Python %d.%d.%d" % sys.version_info[:3]
456                                  )),
457    #                           % __ThubanVersion__), #__BuildDate__)),
458                             wxOK | wxICON_INFORMATION)                             wxOK | wxICON_INFORMATION)
459    
460      def AddLayer(self):      def AddLayer(self):
461          dlg = wxFileDialog(self, "Select a data file", ".", "", "*.*",          dlg = wxFileDialog(self, _("Select a data file"), ".", "", "*.*",
462                             wxOPEN)                             wxOPEN)
463          if dlg.ShowModal() == wxID_OK:          if dlg.ShowModal() == wxID_OK:
464              filename = dlg.GetPath()              filename = dlg.GetPath()
465              title = os.path.splitext(os.path.basename(filename))[0]              title = os.path.splitext(os.path.basename(filename))[0]
             layer = Layer(title, filename)  
466              map = self.canvas.Map()              map = self.canvas.Map()
467              has_layers = map.HasLayers()              has_layers = map.HasLayers()
468              try:              try:
469                    store = self.application.Session().OpenShapefile(filename)
470                except IOError:
471                    # the layer couldn't be opened
472                    self.RunMessageBox(_("Add Layer"),
473                                       _("Can't open the file '%s'.") % filename)
474                else:
475                    layer = Layer(title, store)
476                  map.AddLayer(layer)                  map.AddLayer(layer)
477                    if not has_layers:
478                        # if we're adding a layer to an empty map, fit the
479                        # new map to the window
480                        self.canvas.FitMapToWindow()
481            dlg.Destroy()
482    
483        def AddRasterLayer(self):
484            dlg = wxFileDialog(self, _("Select an image file"), ".", "", "*.*",
485                               wxOPEN)
486            if dlg.ShowModal() == wxID_OK:
487                filename = dlg.GetPath()
488                title = os.path.splitext(os.path.basename(filename))[0]
489                map = self.canvas.Map()
490                has_layers = map.HasLayers()
491                try:
492                    layer = RasterLayer(title, filename)
493              except IOError:              except IOError:
494                  # the layer couldn't be opened                  # the layer couldn't be opened
495                  self.RunMessageBox("Add Layer",                  self.RunMessageBox(_("Add Image Layer"),
496                                     "Can't open the file '%s'." % filename)                                     _("Can't open the file '%s'.") % filename)
497              else:              else:
498                    map.AddLayer(layer)
499                  if not has_layers:                  if not has_layers:
500                      # if we're adding a layer to an empty map, for the                      # if we're adding a layer to an empty map, fit the
501                      # new map to the window                      # new map to the window
502                      self.canvas.FitMapToWindow()                      self.canvas.FitMapToWindow()
503          dlg.Destroy()          dlg.Destroy()
# Line 380  class MainWindow(wxFrame): Line 510  class MainWindow(wxFrame):
510      def CanRemoveLayer(self):      def CanRemoveLayer(self):
511          """Return true if the currently selected layer can be deleted.          """Return true if the currently selected layer can be deleted.
512    
513          If no layer is selected return false.          If no layer is selected return False.
514    
515          The return value of this method determines whether the remove          The return value of this method determines whether the remove
516          layer command is sensitive in menu.          layer command is sensitive in menu.
# Line 388  class MainWindow(wxFrame): Line 518  class MainWindow(wxFrame):
518          layer = self.current_layer()          layer = self.current_layer()
519          if layer is not None:          if layer is not None:
520              return self.canvas.Map().CanRemoveLayer(layer)              return self.canvas.Map().CanRemoveLayer(layer)
521          return 0          return False
522    
523      def RaiseLayer(self):      def RaiseLayer(self):
524          layer = self.current_layer()          layer = self.current_layer()
# Line 405  class MainWindow(wxFrame): Line 535  class MainWindow(wxFrame):
535    
536          If no layer is selected, return None          If no layer is selected, return None
537          """          """
538          return self.interactor.SelectedLayer()          return self.canvas.SelectedLayer()
539    
540      def has_selected_layer(self):      def has_selected_layer(self):
541          """Return true if a layer is currently selected"""          """Return true if a layer is currently selected"""
542          return self.interactor.HasSelectedLayer()          return self.canvas.HasSelectedLayer()
543    
544      def choose_color(self):      def has_selected_shapes(self):
545          """Run the color selection dialog and return the selected color.          """Return true if a shape is currently selected"""
546            return self.canvas.HasSelectedShapes()
547    
548          If the user cancels, return None.      def HideLayer(self):
         """  
         dlg = wxColourDialog(self)  
         color = None  
         if dlg.ShowModal() == wxID_OK:  
             data = dlg.GetColourData()  
             wxc = data.GetColour()  
             color = Color(wxc.Red() / 255.0,  
                           wxc.Green() / 255.0,  
                           wxc.Blue() / 255.0)  
         dlg.Destroy()  
         return color  
   
     def LayerFillColor(self):  
         layer = self.current_layer()  
         if layer is not None:  
             color = self.choose_color()  
             if color is not None:  
                 layer.SetFill(color)  
   
     def LayerTransparentFill(self):  
549          layer = self.current_layer()          layer = self.current_layer()
550          if layer is not None:          if layer is not None:
551              layer.SetFill(None)              layer.SetVisible(0)
552    
553      def LayerOutlineColor(self):      def ShowLayer(self):
554          layer = self.current_layer()          layer = self.current_layer()
555          if layer is not None:          if layer is not None:
556              color = self.choose_color()              layer.SetVisible(1)
             if color is not None:  
                 layer.SetStroke(color)  
557    
558      def LayerNoOutline(self):      def DuplicateLayer(self):
559            """Ceate a new layer above the selected layer with the same shapestore
560            """
561          layer = self.current_layer()          layer = self.current_layer()
562          if layer is not None:          if layer is not None and hasattr(layer, "ShapeStore"):
563              layer.SetStroke(None)              new_layer = Layer(_("Copy of `%s'") % layer.Title(),
564                                  layer.ShapeStore(),
565                                  projection = layer.GetProjection())
566                new_classification = copy.deepcopy(layer.GetClassification())
567                new_layer.SetClassification(new_classification)
568                self.Map().AddLayer(new_layer)
569    
570      def HideLayer(self):      def CanDuplicateLayer(self):
571          layer = self.current_layer()          """Return whether the DuplicateLayer method can create a duplicate"""
         if layer is not None:  
             layer.SetVisible(0)  
           
     def ShowLayer(self):  
572          layer = self.current_layer()          layer = self.current_layer()
573          if layer is not None:          return layer is not None and hasattr(layer, "ShapeStore")
             layer.SetVisible(1)  
574    
575      def LayerShowTable(self):      def LayerShowTable(self):
576          layer = self.current_layer()          layer = self.current_layer()
# Line 468  class MainWindow(wxFrame): Line 579  class MainWindow(wxFrame):
579              name = "table_view" + str(id(table))              name = "table_view" + str(id(table))
580              dialog = self.get_open_dialog(name)              dialog = self.get_open_dialog(name)
581              if dialog is None:              if dialog is None:
582                  dialog = tableview.LayerTableFrame(self, self.interactor, name,                  dialog = tableview.LayerTableFrame(self, name,
583                                                     "Table: %s" % layer.Title(),                                           _("Layer Table: %s") % layer.Title(),
584                                                     layer, table)                                           layer, table)
585                  self.add_dialog(name, dialog)                  self.add_dialog(name, dialog)
586                  dialog.Show(true)                  dialog.Show(True)
587              else:              else:
588                  # FIXME: bring dialog to front here                  # FIXME: bring dialog to front here
589                  pass                  pass
590    
591      def Projection(self):      def MapProjection(self):
592          map = self.canvas.Map()  
593          proj = map.projection          name = "map_projection"
594          if proj is None:          dialog = self.get_open_dialog(name)
595              proj4Dlg = proj4dialog.Proj4Dialog(NULL, None, map.BoundingBox())  
596            if dialog is None:
597                map = self.canvas.Map()
598                dialog = projdialog.ProjFrame(self, name,
599                         _("Map Projection: %s") % map.Title(), map)
600                self.add_dialog(name, dialog)
601                dialog.Show()
602            dialog.Raise()
603    
604        def LayerProjection(self):
605    
606            layer = self.current_layer()
607    
608            name = "layer_projection" + str(id(layer))
609            dialog = self.get_open_dialog(name)
610    
611            if dialog is None:
612                map = self.canvas.Map()
613                dialog = projdialog.ProjFrame(self, name,
614                         _("Layer Projection: %s") % layer.Title(), layer)
615                self.add_dialog(name, dialog)
616                dialog.Show()
617            dialog.Raise()
618    
619        def LayerEditProperties(self):
620    
621            #
622            # the menu option for this should only be available if there
623            # is a current layer, so we don't need to check if the
624            # current layer is None
625            #
626    
627            layer = self.current_layer()
628            self.OpenLayerProperties(layer)
629    
630        def OpenLayerProperties(self, layer, group = None):
631            name = "layer_properties" + str(id(layer))
632            dialog = self.get_open_dialog(name)
633    
634            if dialog is None:
635                dialog = Classifier(self, name, layer, group)
636                self.add_dialog(name, dialog)
637                dialog.Show()
638            dialog.Raise()
639    
640        def LayerJoinTable(self):
641            layer = self.canvas.SelectedLayer()
642            if layer is not None:
643                dlg = JoinDialog(self, _("Join Layer with Table"),
644                                 self.application.session,
645                                 layer = layer)
646                dlg.ShowModal()
647    
648        def LayerUnjoinTable(self):
649            layer = self.canvas.SelectedLayer()
650            if layer is not None:
651                orig_store = layer.ShapeStore().OrigShapeStore()
652                if orig_store:
653                    layer.SetShapeStore(orig_store)
654    
655        def ShowLegend(self):
656            if not self.LegendShown():
657                self.ToggleLegend()
658    
659        def ToggleLegend(self):
660            """Show the legend if it's not shown otherwise hide it again"""
661            name = "legend"
662            dialog = self.FindRegisteredDock(name)
663    
664            if dialog is None:
665                dialog = self.CreateDock(name, -1, _("Legend"), wxLAYOUT_LEFT)
666                legend.LegendPanel(dialog, None, self)
667                dialog.Dock()
668                dialog.GetPanel().SetMap(self.Map())
669                dialog.Show()
670          else:          else:
671              proj4Dlg = proj4dialog.Proj4Dialog(NULL, map.projection.params,              dialog.Show(not dialog.IsShown())
672                                                 map.BoundingBox())  
673          if proj4Dlg.ShowModal() == wxID_OK:      def LegendShown(self):
674              params = proj4Dlg.GetParams()          """Return true iff the legend is currently open"""
675              if params is not None:          dialog = self.FindRegisteredDock("legend")
676                  proj = Projection(params)          return dialog is not None and dialog.IsShown()
677    
678        def TableOpen(self):
679            dlg = wxFileDialog(self, _("Open Table"), ".", "",
680                               _("DBF Files (*.dbf)") + "|*.dbf|" +
681                               #_("CSV Files (*.csv)") + "|*.csv|" +
682                               _("All Files (*.*)") + "|*.*",
683                               wxOPEN)
684            if dlg.ShowModal() == wxID_OK:
685                filename = dlg.GetPath()
686                dlg.Destroy()
687                try:
688                    table = self.application.session.OpenTableFile(filename)
689                except IOError:
690                    # the layer couldn't be opened
691                    self.RunMessageBox(_("Open Table"),
692                                       _("Can't open the file '%s'.") % filename)
693              else:              else:
694                  proj = None                  self.ShowTableView(table)
695              map.SetProjection(proj)  
696          proj4Dlg.Destroy()      def TableClose(self):
697            tables = self.application.session.UnreferencedTables()
698    
699            lst = [(t.Title(), t) for t in tables]
700            lst.sort()
701            titles = [i[0] for i in lst]
702            dlg = wxMultipleChoiceDialog(self, _("Pick the tables to close:"),
703                                         _("Close Table"), titles,
704                                         size = (400, 300),
705                                         style = wxDEFAULT_DIALOG_STYLE |
706                                                 wxRESIZE_BORDER)
707            if dlg.ShowModal() == wxID_OK:
708                for i in dlg.GetValue():
709                    self.application.session.RemoveTable(lst[i][1])
710    
711    
712        def TableShow(self):
713            """Offer a multi-selection dialog for tables to be displayed
714    
715            The windows for the selected tables are opened or brought to
716            the front.
717            """
718            tables = self.application.session.Tables()
719    
720            lst = [(t.Title(), t) for t in tables]
721            lst.sort()
722            titles = [i[0] for i in lst]
723            dlg = wxMultipleChoiceDialog(self, _("Pick the table to show:"),
724                                         _("Show Table"), titles,
725                                         size = (400,300),
726                                         style = wxDEFAULT_DIALOG_STYLE |
727                                                 wxRESIZE_BORDER)
728            if (dlg.ShowModal() == wxID_OK):
729                for i in dlg.GetValue():
730                    # XXX: if the table belongs to a layer, open a
731                    # LayerTableFrame instead of QueryTableFrame
732                    self.ShowTableView(lst[i][1])
733    
734        def TableJoin(self):
735            dlg = JoinDialog(self, _("Join Tables"), self.application.session)
736            dlg.ShowModal()
737    
738        def ShowTableView(self, table):
739            """Open a table view for the table and optionally"""
740            name = "table_view%d" % id(table)
741            dialog = self.get_open_dialog(name)
742            if dialog is None:
743                dialog = tableview.QueryTableFrame(self, name,
744                                                   _("Table: %s") % table.Title(),
745                                                   table)
746                self.add_dialog(name, dialog)
747                dialog.Show(True)
748            # FIXME: else bring dialog to front
749    
750      def ZoomInTool(self):      def ZoomInTool(self):
751          self.canvas.ZoomInTool()          self.canvas.ZoomInTool()
# Line 513  class MainWindow(wxFrame): Line 766  class MainWindow(wxFrame):
766      def FullExtent(self):      def FullExtent(self):
767          self.canvas.FitMapToWindow()          self.canvas.FitMapToWindow()
768    
769        def FullLayerExtent(self):
770            self.canvas.FitLayerToWindow(self.current_layer())
771    
772        def FullSelectionExtent(self):
773            self.canvas.FitSelectedToWindow()
774    
775        def ExportMap(self):
776            self.canvas.Export()
777    
778      def PrintMap(self):      def PrintMap(self):
779          self.canvas.Print()          self.canvas.Print()
780    
781      def identify_view_on_demand(self, layer, shape):      def RenameMap(self):
782            dlg = wxTextEntryDialog(self, "Map Title: ", "Rename Map",
783                                    self.Map().Title())
784            if dlg.ShowModal() == wxID_OK:
785                title = dlg.GetValue()
786                if title != "":
787                    self.Map().SetTitle(title)
788                    self.__SetTitle(title)
789    
790            dlg.Destroy()
791    
792        def identify_view_on_demand(self, layer, shapes):
793            """Subscribed to the canvas' SHAPES_SELECTED message
794    
795            If the current tool is the identify tool, at least one shape is
796            selected and the identify dialog is not shown, show the dialog.
797            """
798            # If the selection has become empty we don't need to do
799            # anything. Otherwise it could happen that the dialog was popped
800            # up when the selection became empty, e.g. when a new selection
801            # is opened while the identify tool is active and dialog had
802            # been closed
803            if not shapes:
804                return
805    
806          name = "identify_view"          name = "identify_view"
807          if self.canvas.CurrentTool() == "IdentifyTool":          if self.canvas.CurrentTool() == "IdentifyTool":
808              if not self.dialog_open(name):              if not self.dialog_open(name):
809                  dialog = identifyview.IdentifyView(self, self.interactor, name)                  dialog = identifyview.IdentifyView(self, name)
810                  self.add_dialog(name, dialog)                  self.add_dialog(name, dialog)
811                  dialog.Show(true)                  dialog.Show(True)
812              else:              else:
813                  # FIXME: bring dialog to front?                  # FIXME: bring dialog to front?
814                  pass                  pass
815    
816        def __SetTitle(self, title):
817            self.SetTitle("Thuban - " + title)
818    
819  #  #
820  # Define all the commands available in the main window  # Define all the commands available in the main window
821  #  #
# Line 538  def call_method(context, methodname, *ar Line 827  def call_method(context, methodname, *ar
827      apply(getattr(context.mainwindow, methodname), args)      apply(getattr(context.mainwindow, methodname), args)
828    
829  def _method_command(name, title, method, helptext = "",  def _method_command(name, title, method, helptext = "",
830                      icon = "", sensitive = None):                      icon = "", sensitive = None, checked = None):
831      """Add a command implemented by a method of the mainwindow object"""      """Add a command implemented by a method of the mainwindow object"""
832      registry.Add(Command(name, title, call_method, args=(method,),      registry.Add(Command(name, title, call_method, args=(method,),
833                           helptext = helptext, icon = icon,                           helptext = helptext, icon = icon,
834                           sensitive = sensitive))                           sensitive = sensitive, checked = checked))
835    
836  def make_check_current_tool(toolname):  def make_check_current_tool(toolname):
837      """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 558  def make_check_current_tool(toolname): Line 847  def make_check_current_tool(toolname):
847  def _tool_command(name, title, method, toolname, helptext = "",  def _tool_command(name, title, method, toolname, helptext = "",
848                    icon = "", sensitive = None):                    icon = "", sensitive = None):
849      """Add a tool command"""      """Add a tool command"""
850      registry.Add(Command(name, title, call_method, args=(method,),      registry.Add(ToolCommand(name, title, call_method, args=(method,),
851                           helptext = helptext, icon = icon,                               helptext = helptext, icon = icon,
852                           checked = make_check_current_tool(toolname),                               checked = make_check_current_tool(toolname),
853                           sensitive = sensitive))                               sensitive = sensitive))
854    
855  def _has_selected_layer(context):  def _has_selected_layer(context):
856      """Return true if a layer is selected in the context"""      """Return true if a layer is selected in the context"""
857      return context.mainwindow.has_selected_layer()      return context.mainwindow.has_selected_layer()
858    
859    def _has_selected_shapes(context):
860        """Return true if a layer is selected in the context"""
861        return context.mainwindow.has_selected_shapes()
862    
863  def _can_remove_layer(context):  def _can_remove_layer(context):
864      return context.mainwindow.CanRemoveLayer()      return context.mainwindow.CanRemoveLayer()
865    
866  def _has_tree_window_shown(context):  def _has_tree_window_shown(context):
867      """Return true if the tree window is shown"""      """Return true if the tree window is shown"""
868      return context.mainwindow.get_open_dialog("session_tree") is None      return context.mainwindow.SessionTreeShown()
869    
870  def _has_visible_map(context):  def _has_visible_map(context):
871      """Return true iff theres a visible map in the mainwindow.      """Return true iff theres a visible map in the mainwindow.
# Line 585  def _has_visible_map(context): Line 878  def _has_visible_map(context):
878                  return 1                  return 1
879      return 0      return 0
880    
881    def _has_legend_shown(context):
882        """Return true if the legend window is shown"""
883        return context.mainwindow.LegendShown()
884    
885    
886  # File menu  # File menu
887  _method_command("new_session", "&New Session", "NewSession")  _method_command("new_session", _("&New Session"), "NewSession")
888  _method_command("open_session", "&Open Session", "OpenSession")  _method_command("open_session", _("&Open Session..."), "OpenSession")
889  _method_command("save_session", "&Save Session", "SaveSession")  _method_command("save_session", _("&Save Session"), "SaveSession")
890  _method_command("save_session_as", "Save Session &As", "SaveSessionAs")  _method_command("save_session_as", _("Save Session &As..."), "SaveSessionAs")
891  _method_command("show_session_tree", "Show Session &Tree", "ShowSessionTree",  _method_command("toggle_session_tree", _("Session &Tree"), "ToggleSessionTree",
892                  sensitive = _has_tree_window_shown)                  checked = _has_tree_window_shown)
893  _method_command("exit", "E&xit", "Exit")  _method_command("toggle_legend", _("Legend"), "ToggleLegend",
894                    checked = _has_legend_shown)
895    _method_command("exit", _("E&xit"), "Exit")
896    
897  # Help menu  # Help menu
898  _method_command("help_about", "&About", "About")  _method_command("help_about", _("&About..."), "About")
899    
900    
901  # Map menu  # Map menu
902  _method_command("map_projection", "Pro&jection", "Projection")  _method_command("map_projection", _("Pro&jection..."), "MapProjection")
903    
904  _tool_command("map_zoom_in_tool", "&Zoom in", "ZoomInTool", "ZoomInTool",  _tool_command("map_zoom_in_tool", _("&Zoom in"), "ZoomInTool", "ZoomInTool",
905                helptext = "Switch to map-mode 'zoom-in'", icon = "zoom_in",                helptext = _("Switch to map-mode 'zoom-in'"), icon = "zoom_in",
906                sensitive = _has_visible_map)                sensitive = _has_visible_map)
907  _tool_command("map_zoom_out_tool", "Zoom &out", "ZoomOutTool", "ZoomOutTool",  _tool_command("map_zoom_out_tool", _("Zoom &out"), "ZoomOutTool", "ZoomOutTool",
908                helptext = "Switch to map-mode 'zoom-out'", icon = "zoom_out",                helptext = _("Switch to map-mode 'zoom-out'"), icon = "zoom_out",
909                sensitive = _has_visible_map)                sensitive = _has_visible_map)
910  _tool_command("map_pan_tool", "&Pan", "PanTool", "PanTool",  _tool_command("map_pan_tool", _("&Pan"), "PanTool", "PanTool",
911                helptext = "Switch to map-mode 'pan'", icon = "pan",                helptext = _("Switch to map-mode 'pan'"), icon = "pan",
912                sensitive = _has_visible_map)                sensitive = _has_visible_map)
913  _tool_command("map_identify_tool", "&Identify", "IdentifyTool", "IdentifyTool",  _tool_command("map_identify_tool", _("&Identify"), "IdentifyTool",
914                helptext = "Switch to map-mode 'identify'", icon = "identify",                "IdentifyTool",
915                  helptext = _("Switch to map-mode 'identify'"), icon = "identify",
916                sensitive = _has_visible_map)                sensitive = _has_visible_map)
917  _tool_command("map_label_tool", "&Label", "LabelTool", "LabelTool",  _tool_command("map_label_tool", _("&Label"), "LabelTool", "LabelTool",
918                helptext = "Add/Remove labels", icon = "label",                helptext = _("Add/Remove labels"), icon = "label",
919                sensitive = _has_visible_map)                sensitive = _has_visible_map)
920  _method_command("map_full_extent", "&Full extent", "FullExtent",  _method_command("map_full_extent", _("&Full extent"), "FullExtent",
921                 helptext = "Full Extent", icon = "fullextent",                 helptext = _("Full Extent"), icon = "fullextent",
922                sensitive = _has_visible_map)                sensitive = _has_visible_map)
923  _method_command("map_print", "Prin&t", "PrintMap", helptext = "Print the map")  _method_command("layer_full_extent", _("&Full layer extent"), "FullLayerExtent",
924                   helptext = _("Full Layer Extent"), icon = "fulllayerextent",
925                  sensitive = _has_selected_layer)
926    _method_command("selected_full_extent", _("&Full selection extent"), "FullSelectionExtent",
927                   helptext = _("Full Selection Extent"), icon = "fullselextent",
928                  sensitive = _has_selected_shapes)
929    _method_command("map_export", _("E&xport"), "ExportMap",
930                        helptext = _("Export the map to file"))
931    _method_command("map_print", _("Prin&t"), "PrintMap",
932                    helptext = _("Print the map"))
933    _method_command("map_rename", _("&Rename..."), "RenameMap",
934                    helptext = _("Rename the map"))
935    _method_command("layer_add", _("&Add Layer..."), "AddLayer",
936                    helptext = _("Add a new layer to active map"))
937    _method_command("rasterlayer_add", _("&Add Image Layer..."), "AddRasterLayer",
938                    helptext = _("Add a new image layer to active map"))
939    _method_command("layer_remove", _("&Remove Layer"), "RemoveLayer",
940                    helptext = _("Remove selected layer(s)"),
941                    sensitive = _can_remove_layer)
942    
943  # Layer menu  # Layer menu
944  _method_command("layer_add", "&Add Layer", "AddLayer",  _method_command("layer_projection", _("Pro&jection..."), "LayerProjection",
                 helptext = "Add a new layer to active map")  
 _method_command("layer_remove", "&Remove Layer", "RemoveLayer",  
                 helptext = "Remove selected layer(s)",  
                 sensitive = _can_remove_layer)  
 _method_command("layer_fill_color", "&Fill Color", "LayerFillColor",  
                 helptext = "Set the fill color of selected layer(s)",  
                 sensitive = _has_selected_layer)  
 _method_command("layer_transparent_fill", "&Transparent Fill",  
                 "LayerTransparentFill",  
                 helptext = "Do not fill the selected layer(s)",  
945                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
946  _method_command("layer_outline_color", "&Outline Color", "LayerOutlineColor",  _method_command("layer_duplicate", _("&Duplicate"), "DuplicateLayer",
947                  helptext = "Set the outline color of selected layer(s)",                  helptext = _("Duplicate selected layer(s)"),
948              sensitive = lambda context: context.mainwindow.CanDuplicateLayer())
949    _method_command("layer_raise", _("&Raise"), "RaiseLayer",
950                    helptext = _("Raise selected layer(s)"),
951                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
952  _method_command("layer_no_outline", "&No Outline", "LayerNoOutline",  _method_command("layer_lower", _("&Lower"), "LowerLayer",
953                  helptext = "Do not draw the outline of the selected layer(s)",                  helptext = _("Lower selected layer(s)"),
954                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
955  _method_command("layer_raise", "&Raise", "RaiseLayer",  _method_command("layer_show", _("&Show"), "ShowLayer",
956                  helptext = "Raise selected layer(s)",                  helptext = _("Make selected layer(s) visible"),
957                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
958  _method_command("layer_lower", "&Lower", "LowerLayer",  _method_command("layer_hide", _("&Hide"), "HideLayer",
959                  helptext = "Lower selected layer(s)",                  helptext = _("Make selected layer(s) unvisible"),
960                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
961  _method_command("layer_show", "&Show", "ShowLayer",  _method_command("layer_show_table", _("Show Ta&ble"), "LayerShowTable",
962                  helptext = "Make selected layer(s) visible",                  helptext = _("Show the selected layer's table"),
963                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
964  _method_command("layer_hide", "&Hide", "HideLayer",  _method_command("layer_properties", _("&Properties..."), "LayerEditProperties",
                 helptext = "Make selected layer(s) unvisible",  
965                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
966  _method_command("layer_show_table", "Show Ta&ble", "LayerShowTable",  _method_command("layer_jointable", _("&Join Table..."), "LayerJoinTable",
                 helptext = "Show the selected layer's table",  
967                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
968    
969    def _can_unjoin(context):
970        """Return whether the Layer/Unjoin command can be executed.
971    
972        This is the case if a layer is selected and that layer has a
973        shapestore that has an original shapestore.
974        """
975        layer = context.mainwindow.SelectedLayer()
976        if layer is None:
977            return 0
978        getstore = getattr(layer, "ShapeStore", None)
979        if getstore is not None:
980            return getstore().OrigShapeStore() is not None
981        else:
982            return 0
983    _method_command("layer_unjointable", _("&Unjoin Table..."), "LayerUnjoinTable",
984                    sensitive = _can_unjoin)
985    
986    # Table menu
987    _method_command("table_open", _("&Open..."), "TableOpen")
988    _method_command("table_close", _("&Close"), "TableClose",
989           sensitive = lambda context: bool(context.session.UnreferencedTables()))
990    _method_command("table_show", _("&Show"), "TableShow")
991    _method_command("table_join", _("&Join..."), "TableJoin")
992    
993    #  Export only under Windows ...
994    map_menu = ["layer_add", "rasterlayer_add", "layer_remove", "map_rename",
995                            None,
996                            "map_projection",
997                            None,
998                            "map_zoom_in_tool", "map_zoom_out_tool",
999                            "map_pan_tool",
1000                            "map_full_extent",
1001                            "layer_full_extent",
1002                            "selected_full_extent",
1003                            None,
1004                            "map_identify_tool", "map_label_tool",
1005                            None,
1006                            "toggle_legend",
1007                            None]
1008    if wxPlatform == '__WXMSW__':
1009        map_menu.append("map_export")
1010    map_menu.append("map_print")
1011    
1012  # the menu structure  # the menu structure
1013  main_menu = Menu("<main>", "<main>",  main_menu = Menu("<main>", "<main>",
1014                   [Menu("file", "&File",                   [Menu("file", _("&File"),
1015                         ["new_session", "open_session", None,                         ["new_session", "open_session", None,
1016                          "save_session", "save_session_as", None,                          "save_session", "save_session_as", None,
1017                          "show_session_tree", None,                          "toggle_session_tree", None,
1018                          "exit"]),                          "exit"]),
1019                    Menu("map", "&Map",                    Menu("map", _("&Map"), map_menu),
1020                         ["layer_add", "layer_remove",                    Menu("layer", _("&Layer"),
1021                          None,                          ["layer_raise", "layer_lower",
                         "map_projection",  
1022                          None,                          None,
1023                          "map_zoom_in_tool", "map_zoom_out_tool",                          "layer_show", "layer_hide",
                         "map_pan_tool", "map_identify_tool", "map_label_tool",  
                         None,  
                         "map_full_extent",  
1024                          None,                          None,
1025                          "map_print"]),                          "layer_duplicate",
                   Menu("layer", "&Layer",  
                        ["layer_fill_color", "layer_transparent_fill",  
                         "layer_outline_color", "layer_no_outline",  
1026                          None,                          None,
1027                          "layer_raise", "layer_lower",                          "layer_projection",
1028                          None,                          None,
1029                          "layer_show", "layer_hide",                          "layer_show_table",
1030                            "layer_jointable",
1031                            "layer_unjointable",
1032                          None,                          None,
1033                          "layer_show_table"]),                          "layer_properties"]),
1034                    Menu("help", "&Help",                    Menu("table", _("&Table"),
1035                           ["table_open", "table_close",
1036                           None,
1037                           "table_show",
1038                           None,
1039                           "table_join"]),
1040                      Menu("help", _("&Help"),
1041                         ["help_about"])])                         ["help_about"])])
1042    
1043  # the main toolbar  # the main toolbar
1044    
1045  main_toolbar = Menu("<toolbar>", "<toolbar>",  main_toolbar = Menu("<toolbar>", "<toolbar>",
1046                      ["map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool",                      ["map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool",
1047                       "map_identify_tool", "map_label_tool", "map_full_extent"])                       "map_full_extent",
1048                         "layer_full_extent",
1049                         "selected_full_extent",
1050                         None,
1051                         "map_identify_tool", "map_label_tool"])

Legend:
Removed from v.346  
changed lines
  Added in v.1094

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26