/[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 18 by bh, Mon Sep 3 16:25:09 2001 UTC revision 1056 by frank, Tue May 27 11:29:46 2003 UTC
# Line 1  Line 1 
1  # Copyright (C) 2001 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  import sys, os  __ThubanVersion__ = "0.2" #"$THUBAN_0_2$"
17    #__BuildDate__ = "$Date$"
18    
19    import os
20    
21  from wxPython.wx import *  from wxPython.wx import *
22    from wxPython.wx import __version__ as wxPython_version
23    
24    from wxPython.lib.dialogs import wxMultipleChoiceDialog
25    
26  import Thuban  import Thuban
27  from Thuban.Model.session import Session  import Thuban.version
28  from Thuban.Model.map import Map  
29  from Thuban.Model.layer import Layer  from Thuban import _
30    from Thuban.Model.session import create_empty_session
31    from Thuban.Model.layer import Layer, RasterLayer
32  from Thuban.Model.color import Color  from Thuban.Model.color import Color
33  from Thuban.Model.proj import Projection  from Thuban.Model.proj import Projection
34    
# Line 27  import view Line 36  import view
36  import tree  import tree
37  import proj4dialog  import proj4dialog
38  import tableview, identifyview  import tableview, identifyview
39    from Thuban.UI.classifier import Classifier
40    import legend
41    from menu import Menu
42    
43    from context import Context
44    from command import registry, Command, ToolCommand
45    from messages import LAYER_SELECTED, SHAPES_SELECTED, VIEW_POSITION
46    
47    from Thuban.UI.dock import DockFrame
48    from Thuban.UI.join import JoinDialog
49    
50  import main  import resource
 from command import registry, Command  
51    
52    import projdialog
53    
 # the directory where the toolbar icons are stored  
 bitmapdir = os.path.join(Thuban.__path__[0], os.pardir, "Resources", "Bitmaps")  
 bitmapext = ".xpm"  
54    
55    
56  class MainWindow(wxFrame):  class MainWindow(DockFrame):
57    
58      def __init__(self, parent, ID):      # Some messages that can be subscribed/unsubscribed directly through
59          wxFrame.__init__(self, parent, ID, 'Thuban',      # the MapCanvas come in fact from other objects. This is a map to
60                           wxDefaultPosition, wxSize(400, 300))      # map those messages to the names of the instance variables they
61        # actually come from. This delegation is implemented in the
62        # Subscribe and unsubscribed methods
63        delegated_messages = {LAYER_SELECTED: "canvas",
64                              SHAPES_SELECTED: "canvas"}
65    
66        # Methods delegated to some instance variables. The delegation is
67        # implemented in the __getattr__ method.
68        delegated_methods = {"SelectLayer": "canvas",
69                             "SelectShapes": "canvas",
70                             "SelectedShapes": "canvas",
71                             }
72    
73        def __init__(self, parent, ID, title, application, interactor,
74                     initial_message = None, size = wxSize(-1, -1)):
75            DockFrame.__init__(self, parent, ID, title, wxDefaultPosition, size)
76            #wxFrame.__init__(self, parent, ID, title, wxDefaultPosition, size)
77    
78            self.application = application
79    
80          self.CreateStatusBar()          self.CreateStatusBar()
81          self.SetStatusText("This is the wxPython-based "          if initial_message:
82                        "Graphical User Interface for exploring geographic data")              self.SetStatusText(initial_message)
83    
84          self.identify_view = None          self.identify_view = None
85    
86          self.init_ids()          self.init_ids()
87    
88          menuBar = wxMenuBar()          # creat the menubar from the main_menu description
89            self.SetMenuBar(self.build_menu_bar(main_menu))
         menu = wxMenu()  
         menuBar.Append(menu, "&File");  
         for name in ["new_session", "open_session", None,  
                      "save_session", "save_session_as", None,  
                      "exit"]:  
             self.add_menu_command(menu, name)  
   
         menu = wxMenu()  
         menuBar.Append(menu, "&Map");  
         for name in ["map_projection",  
                      None,  
                      "map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool",  
                      "map_identify_tool", "map_label_tool",  
                      None,  
                      "map_full_extent",  
                      None,  
                      "map_print"]:  
             self.add_menu_command(menu, name)  
   
         menu = wxMenu()  
         menuBar.Append(menu, "&Layer");  
         for name in ["layer_add", "layer_remove",  
                      None,  
                      "layer_fill_color", "layer_transparent_fill",  
                      "layer_ourline_color", "layer_no_outline",  
                      None,  
                      "layer_raise", "layer_lower",  
                      None,  
                      "layer_show", "layer_hide",  
                      None,  
                      "layer_show_table"]:  
             self.add_menu_command(menu, name)  
   
         menu = wxMenu()  
         menuBar.Append(menu, "&Help");  
         self.add_menu_command(menu, "help_about")  
   
         self.SetMenuBar(menuBar)  
90    
91          # toolbar          # Similarly, create the toolbar from main_toolbar
92          toolbar = self.CreateToolBar(wxTB_3DBUTTONS)          toolbar = self.build_toolbar(main_toolbar)
         for name in ["map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool",  
                      "map_identify_tool", "map_label_tool"]:  
             self.add_toolbar_command(toolbar, name)  
93          # call Realize to make sure that the tools appear.          # call Realize to make sure that the tools appear.
94          toolbar.Realize()          toolbar.Realize()
95    
96    
97          # Create the map canvas          # Create the map canvas
98          canvas = view.MapCanvas(self, -1)          canvas = view.MapCanvas(self, -1)
99            canvas.Subscribe(VIEW_POSITION, self.view_position_changed)
100            canvas.Subscribe(SHAPES_SELECTED, self.identify_view_on_demand)
101          self.canvas = canvas          self.canvas = canvas
102    
103            self.SetMainWindow(self.canvas)
104    
105            self.SetAutoLayout(True)
106    
107            self.init_dialogs()
108    
109          EVT_CLOSE(self, self.OnClose)          EVT_CLOSE(self, self.OnClose)
110    
111        def Subscribe(self, channel, *args):
112            """Subscribe a function to a message channel.
113    
114            If channel is one of the delegated messages call the appropriate
115            object's Subscribe method. Otherwise do nothing.
116            """
117            if channel in self.delegated_messages:
118                object = getattr(self, self.delegated_messages[channel])
119                object.Subscribe(channel, *args)
120            else:
121                print "Trying to subscribe to unsupported channel %s" % channel
122    
123        def Unsubscribe(self, channel, *args):
124            """Unsubscribe a function from a message channel.
125    
126            If channel is one of the delegated messages call the appropriate
127            object's Unsubscribe method. Otherwise do nothing.
128            """
129            if channel in self.delegated_messages:
130                object = getattr(self, self.delegated_messages[channel])
131                object.Unsubscribe(channel, *args)
132    
133        def __getattr__(self, attr):
134            """If attr is one of the delegated methods return that method
135    
136            Otherwise raise AttributeError.
137            """
138            if attr in self.delegated_methods:
139                return getattr(getattr(self, self.delegated_methods[attr]), attr)
140            raise AttributeError(attr)
141    
142      def init_ids(self):      def init_ids(self):
143          """Initialize the ids"""          """Initialize the ids"""
144          self.current_id = 6000          self.current_id = 6000
145          self.id_to_name = {}          self.id_to_name = {}
146          self.name_to_id = {}          self.name_to_id = {}
147            self.events_bound = {}
148    
149      def get_id(self, name):      def get_id(self, name):
150          """Return the wxWindows id for the command named name.          """Return the wxWindows id for the command named name.
# Line 123  class MainWindow(wxFrame): Line 157  class MainWindow(wxFrame):
157              self.name_to_id[name] = ID              self.name_to_id[name] = ID
158              self.id_to_name[ID] = name              self.id_to_name[ID] = name
159          return ID          return ID
160            
161        def bind_command_events(self, command, ID):
162            """Bind the necessary events for the given command and ID"""
163            if not self.events_bound.has_key(ID):
164                # the events haven't been bound yet
165                EVT_MENU(self, ID, self.invoke_command)
166                if command.IsDynamic():
167                    EVT_UPDATE_UI(self, ID, self.update_command_ui)
168    
169        def build_menu_bar(self, menudesc):
170            """Build and return the menu bar from the menu description"""
171            menu_bar = wxMenuBar()
172    
173            for item in menudesc.items:
174                # here the items must all be Menu instances themselves
175                menu_bar.Append(self.build_menu(item), item.title)
176    
177            return menu_bar
178    
179        def build_menu(self, menudesc):
180            """Return a wxMenu built from the menu description menudesc"""
181            wxmenu = wxMenu()
182            last = None
183            for item in menudesc.items:
184                if item is None:
185                    # a separator. Only add one if the last item was not a
186                    # separator
187                    if last is not None:
188                        wxmenu.AppendSeparator()
189                elif isinstance(item, Menu):
190                    # a submenu
191                    wxmenu.AppendMenu(wxNewId(), item.title, self.build_menu(item))
192                else:
193                    # must the name the name of a command
194                    self.add_menu_command(wxmenu, item)
195                last = item
196            return wxmenu
197    
198        def build_toolbar(self, toolbardesc):
199            """Build and return the main toolbar window from a toolbar description
200    
201            The parameter should be an instance of the Menu class but it
202            should not contain submenus.
203            """
204            toolbar = self.CreateToolBar(wxTB_3DBUTTONS)
205    
206            # set the size of the tools' bitmaps. Not needed on wxGTK, but
207            # on Windows, although it doesn't work very well there. It seems
208            # that only 16x16 icons are really supported on windows.
209            # We probably shouldn't hardwire the bitmap size here.
210            toolbar.SetToolBitmapSize(wxSize(24, 24))
211    
212            for item in toolbardesc.items:
213                if item is None:
214                    toolbar.AddSeparator()
215                else:
216                    # assume it's a string.
217                    self.add_toolbar_command(toolbar, item)
218    
219            return toolbar
220    
221      def add_menu_command(self, menu, name):      def add_menu_command(self, menu, name):
222          """Add the command with name name to the menu menu.          """Add the command with name name to the menu menu.
223    
# Line 137  class MainWindow(wxFrame): Line 231  class MainWindow(wxFrame):
231                  ID = self.get_id(name)                  ID = self.get_id(name)
232                  menu.Append(ID, command.Title(), command.HelpText(),                  menu.Append(ID, command.Title(), command.HelpText(),
233                              command.IsCheckCommand())                              command.IsCheckCommand())
234                  EVT_MENU(self, ID, self.invoke_command)                  self.bind_command_events(command, ID)
                 if command.IsDynamic():  
                     EVT_UPDATE_UI(self, ID, self.update_command_ui)  
235              else:              else:
236                  print "Unknown command %s" % name                  print _("Unknown command %s") % name
237    
238      def add_toolbar_command(self, toolbar, name):      def add_toolbar_command(self, toolbar, name):
239          """Add the command with name name to the toolbar toolbar.          """Add the command with name name to the toolbar toolbar.
# Line 156  class MainWindow(wxFrame): Line 248  class MainWindow(wxFrame):
248              command = registry.Command(name)              command = registry.Command(name)
249              if command is not None:              if command is not None:
250                  ID = self.get_id(name)                  ID = self.get_id(name)
251                  filename = os.path.join(bitmapdir, command.Icon()) + bitmapext                  bitmap = resource.GetBitmapResource(command.Icon(),
252                  bitmap = wxBitmap(filename, wxBITMAP_TYPE_XPM)                                                      wxBITMAP_TYPE_XPM)
253                  toolbar.AddTool(ID, bitmap,                  toolbar.AddTool(ID, bitmap,
254                                  shortHelpString = command.HelpText(),                                  shortHelpString = command.HelpText(),
255                                  isToggle = command.IsCheckCommand())                                  isToggle = command.IsCheckCommand())
256                    self.bind_command_events(command, ID)
257              else:              else:
258                  print "Unknown command %s" % name                  print _("Unknown command %s") % name
259    
260        def Context(self):
261            """Return the context object for a command invoked from this window
262            """
263            return Context(self.application, self.application.Session(), self)
264    
265      def invoke_command(self, event):      def invoke_command(self, event):
266          name = self.id_to_name.get(event.GetId())          name = self.id_to_name.get(event.GetId())
267          if name is not None:          if name is not None:
268              command = registry.Command(name)              command = registry.Command(name)
269              command.Execute(self)              command.Execute(self.Context())
270          else:          else:
271              print "Unknown command ID %d" % event.GetId()              print _("Unknown command ID %d") % event.GetId()
272    
273      def update_command_ui(self, event):      def update_command_ui(self, event):
274          #print "update_command_ui", self.id_to_name[event.GetId()]          #print "update_command_ui", self.id_to_name[event.GetId()]
275            context = self.Context()
276          command = registry.Command(self.id_to_name[event.GetId()])          command = registry.Command(self.id_to_name[event.GetId()])
277          if command is not None:          if command is not None:
278              event.Enable(command.Sensitive(self))              sensitive = command.Sensitive(context)
279              event.SetText(command.DynText(self))              event.Enable(sensitive)
280                if command.IsTool() and not sensitive and command.Checked(context):
281                    # When a checked tool command is disabled deselect all
282                    # tools. Otherwise the tool would remain active but it
283                    # might lead to errors if the tools stays active. This
284                    # problem occurred in GREAT-ER and this fixes it, but
285                    # it's not clear to me whether this is really the best
286                    # way to do it (BH, 20021206).
287                    self.canvas.SelectTool(None)
288                event.SetText(command.DynText(context))
289              if command.IsCheckCommand():              if command.IsCheckCommand():
290                  event.Check(command.Checked(self))                      event.Check(command.Checked(context))
291    
292        def RunMessageBox(self, title, text, flags = wxOK | wxICON_INFORMATION):
293            """Run a modal message box with the given text, title and flags
294            and return the result"""
295            dlg = wxMessageDialog(self, text, title, flags)
296            dlg.CenterOnParent()
297            result = dlg.ShowModal()
298            dlg.Destroy()
299            return result
300    
301        def init_dialogs(self):
302            """Initialize the dialog handling"""
303            # The mainwindow maintains a dict mapping names to open
304            # non-modal dialogs. The dialogs are put into this dict when
305            # they're created and removed when they're closed
306            self.dialogs = {}
307    
308        def add_dialog(self, name, dialog):
309            if self.dialogs.has_key(name):
310                raise RuntimeError(_("The Dialog named %s is already open") % name)
311            self.dialogs[name] = dialog
312    
313        def dialog_open(self, name):
314            return self.dialogs.has_key(name)
315    
316        def remove_dialog(self, name):
317            del self.dialogs[name]
318    
319        def get_open_dialog(self, name):
320            return self.dialogs.get(name)
321    
322        def view_position_changed(self):
323            pos = self.canvas.CurrentPosition()
324            if pos is not None:
325                text = "(%10.10g, %10.10g)" % pos
326            else:
327                text = ""
328            self.set_position_text(text)
329    
330        def set_position_text(self, text):
331            """Set the statusbar text showing the current position.
332    
333            By default the text is shown in field 0 of the status bar.
334            Override this method in derived classes to put it into a
335            different field of the statusbar.
336            """
337            self.SetStatusText(text)
338    
339        def save_modified_session(self, can_veto = 1):
340            """If the current session has been modified, ask the user
341            whether to save it and do so if requested. Return the outcome of
342            the dialog (either wxID_OK, wxID_CANCEL or wxID_NO). If the
343            dialog wasn't run return wxID_NO.
344    
345            If the can_veto parameter is true (default) the dialog includes
346            a cancel button, otherwise not.
347            """
348            if self.application.session.WasModified():
349                flags = wxYES_NO | wxICON_QUESTION
350                if can_veto:
351                    flags = flags | wxCANCEL
352                result = self.RunMessageBox(_("Exit"),
353                                            _("The session has been modified."
354                                             " Do you want to save it?"),
355                                            flags)
356                if result == wxID_YES:
357                    self.SaveSession()
358            else:
359                result = wxID_NO
360            return result
361    
362        def prepare_new_session(self):
363            for d in self.dialogs.values():
364                if not isinstance(d, tree.SessionTreeView):
365                    d.Close()
366    
367      def NewSession(self):      def NewSession(self):
368          session = Session("")          if self.save_modified_session() != wxID_CANCEL:
369          session.AddMap(Map(""))              self.prepare_new_session()
370          main.app.SetSession(session)              self.application.SetSession(create_empty_session())
371    
372      def OpenSession(self):      def OpenSession(self):
373          dlg = wxFileDialog(self, "Select a session file", ".", "",          if self.save_modified_session() != wxID_CANCEL:
374                             "*.session", wxOPEN)              dlg = wxFileDialog(self, _("Open Session"), ".", "",
375          if dlg.ShowModal() == wxID_OK:                                 "Thuban Session File (*.thuban)|*.thuban",
376              main.app.OpenSession(dlg.GetPath())                                 wxOPEN)
377          dlg.Destroy()              if dlg.ShowModal() == wxID_OK:
378                    self.prepare_new_session()
379                    self.application.OpenSession(dlg.GetPath())
380                dlg.Destroy()
381    
382      def SaveSession(self):      def SaveSession(self):
383          main.app.SaveSession()          if self.application.session.filename == None:
384                self.SaveSessionAs()
385            else:
386                self.application.SaveSession()
387    
388      def SaveSessionAs(self):      def SaveSessionAs(self):
389          dlg = wxFileDialog(self, "Enter a filename for session", ".", "",          dlg = wxFileDialog(self, _("Save Session As"), ".", "",
390                             "*.session", wxOPEN)                             "Thuban Session File (*.thuban)|*.thuban",
391                               wxSAVE|wxOVERWRITE_PROMPT)
392          if dlg.ShowModal() == wxID_OK:          if dlg.ShowModal() == wxID_OK:
393              main.app.session.SetFilename(dlg.GetPath())              self.application.session.SetFilename(dlg.GetPath())
394              main.app.SaveSession()              self.application.SaveSession()
395          dlg.Destroy()          dlg.Destroy()
396    
397      def Exit(self):      def Exit(self):
398          self.Close(false)          self.Close(False)
399    
400      def OnClose(self, event):      def OnClose(self, event):
401          veto = 0          result = self.save_modified_session(can_veto = event.CanVeto())
402          if main.app.session.WasModified():          if result == wxID_CANCEL:
             flags = wxYES_NO | wxICON_QUESTION  
             if event.CanVeto():  
                 flags = flags | wxCANCEL  
             dlg = wxMessageDialog(self,  
                                   ("The session has been modified."  
                                    " Do you want to save it?"),  
                                   "Exit", flags)  
             result = dlg.ShowModal()  
             dlg.Destroy()  
             if result == wxID_YES:  
                 self.SaveSession()  
             elif result == wxID_CANCEL:  
                 veto = 1  
   
         if veto:  
403              event.Veto()              event.Veto()
404          else:          else:
405                # FIXME: it would be better to tie the unsubscription to
406                # wx's destroy event, but that isn't implemented for wxGTK
407                # yet.
408                self.canvas.Unsubscribe(VIEW_POSITION, self.view_position_changed)
409                DockFrame.OnClose(self, event)
410                for dlg in self.dialogs.values():
411                    dlg.Destroy()
412                self.canvas.Destroy()
413              self.Destroy()              self.Destroy()
414    
415      def SetMap(self, map):      def SetMap(self, map):
416          self.canvas.SetMap(map)          self.canvas.SetMap(map)
417            self.__SetTitle(map.Title())
418    
419            dialog = self.FindRegisteredDock("legend")
420            if dialog is not None:
421                dialog.GetPanel().SetMap(self.Map())
422    
423        def Map(self):
424            """Return the map displayed by this mainwindow"""
425    
426            return self.canvas.Map()
427    
428        def ToggleSessionTree(self):
429            """If the session tree is shown close it otherwise create a new tree"""
430            name = "session_tree"
431            dialog = self.get_open_dialog(name)
432            if dialog is None:
433                dialog = tree.SessionTreeView(self, self.application, name)
434                self.add_dialog(name, dialog)
435                dialog.Show(True)
436            else:
437                dialog.Close()
438    
439        def SessionTreeShown(self):
440            """Return true iff the session tree is currently shown"""
441            return self.get_open_dialog("session_tree") is not None
442    
443      def About(self):      def About(self):
444          dlg = wxMessageDialog(self,          self.RunMessageBox(_("About"),
445                                ("Thuban is a program for\n"                             _("Thuban %s\n"
446                                 "exploring geographic data.\n"                              #"Build Date: %s\n"
447                                 "Copyright (C) 2001 Intevation GmbH.\n"                              "using:\n"
448                                 "Thuban is licensed under the GPL"),                              "  %s\n"
449                                "About", wxOK | wxICON_INFORMATION)                              "  %s\n\n"
450          dlg.ShowModal()                              "Thuban is a program for\n"
451          dlg.Destroy()                              "exploring geographic data.\n"
452                                "Copyright (C) 2001-2003 Intevation GmbH.\n"
453                                "Thuban is licensed under the GNU GPL"
454                                % (Thuban.version.longversion,
455                                   "wxPython %s" % wxPython_version,
456                                   "Python %d.%d.%d" % sys.version_info[:3]
457                                  )),
458    #                           % __ThubanVersion__), #__BuildDate__)),
459                               wxOK | wxICON_INFORMATION)
460    
461      def AddLayer(self):      def AddLayer(self):
462          dlg = wxFileDialog(self, "Select a session file", ".", "", "*.*",          dlg = wxFileDialog(self, _("Select a data file"), ".", "", "*.*",
463                             wxOPEN)                             wxOPEN)
464          if dlg.ShowModal() == wxID_OK:          if dlg.ShowModal() == wxID_OK:
465              filename = dlg.GetPath()              filename = dlg.GetPath()
466              title = os.path.splitext(os.path.basename(filename))[0]              title = os.path.splitext(os.path.basename(filename))[0]
             layer = Layer(title, filename)  
467              map = self.canvas.Map()              map = self.canvas.Map()
468              has_layers = map.HasLayers()              has_layers = map.HasLayers()
469              map.AddLayer(layer)              try:
470              if not has_layers:                  store = self.application.Session().OpenShapefile(filename)
471                  # if we're adding a layer to an empty map, for the new              except IOError:
472                  # map to the window                  # the layer couldn't be opened
473                  self.canvas.FitMapToWindow()                  self.RunMessageBox(_("Add Layer"),
474                                       _("Can't open the file '%s'.") % filename)
475                else:
476                    layer = Layer(title, store)
477                    map.AddLayer(layer)
478                    if not has_layers:
479                        # if we're adding a layer to an empty map, fit the
480                        # new map to the window
481                        self.canvas.FitMapToWindow()
482            dlg.Destroy()
483    
484        def AddRasterLayer(self):
485            dlg = wxFileDialog(self, _("Select an image file"), ".", "", "*.*",
486                               wxOPEN)
487            if dlg.ShowModal() == wxID_OK:
488                filename = dlg.GetPath()
489                title = os.path.splitext(os.path.basename(filename))[0]
490                map = self.canvas.Map()
491                has_layers = map.HasLayers()
492                try:
493                    layer = RasterLayer(title, filename)
494                except IOError:
495                    # the layer couldn't be opened
496                    self.RunMessageBox(_("Add Image Layer"),
497                                       _("Can't open the file '%s'.") % filename)
498                else:
499                    map.AddLayer(layer)
500                    if not has_layers:
501                        # if we're adding a layer to an empty map, fit the
502                        # new map to the window
503                        self.canvas.FitMapToWindow()
504          dlg.Destroy()          dlg.Destroy()
505    
506      def RemoveLayer(self):      def RemoveLayer(self):
507          layer = self.current_layer()          layer = self.current_layer()
508          if layer is not None:          if layer is not None:
509              self.canvas.Map().RemoveLayer(layer)              self.canvas.Map().RemoveLayer(layer)
510          else:  
511              dlg = wxMessageDialog(self, "No layer is selected.\n",      def CanRemoveLayer(self):
512                                    "Remove layer", wxOK | wxICON_INFORMATION)          """Return true if the currently selected layer can be deleted.
513              dlg.ShowModal()  
514              dlg.Destroy()          If no layer is selected return False.
515    
516            The return value of this method determines whether the remove
517            layer command is sensitive in menu.
518            """
519            layer = self.current_layer()
520            if layer is not None:
521                return self.canvas.Map().CanRemoveLayer(layer)
522            return False
523    
524      def RaiseLayer(self):      def RaiseLayer(self):
525          layer = self.current_layer()          layer = self.current_layer()
526          if layer is not None:          if layer is not None:
527              self.canvas.Map().RaiseLayer(layer)              self.canvas.Map().RaiseLayer(layer)
528            
529      def LowerLayer(self):      def LowerLayer(self):
530          layer = self.current_layer()          layer = self.current_layer()
531          if layer is not None:          if layer is not None:
# Line 283  class MainWindow(wxFrame): Line 536  class MainWindow(wxFrame):
536    
537          If no layer is selected, return None          If no layer is selected, return None
538          """          """
539          tree = main.app.tree.tree          return self.canvas.SelectedLayer()
         layer = tree.GetPyData(tree.GetSelection())  
         if isinstance(layer, Layer):  
             return layer  
         return None  
540    
541      def has_selected_layer(self):      def has_selected_layer(self):
542          """Return true if a layer is currently selected"""          """Return true if a layer is currently selected"""
543          tree = main.app.tree.tree          return self.canvas.HasSelectedLayer()
         layer = tree.GetPyData(tree.GetSelection())  
         return isinstance(layer, Layer)  
544    
545      def choose_color(self):      def has_selected_shapes(self):
546          """Run the color selection dialog and return the selected color.          """Return true if a shape is currently selected"""
547            return self.canvas.HasSelectedShapes()
         If the user cancels, return None.  
         """  
         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):  
         layer = self.current_layer()  
         if layer is not None:  
             layer.SetFill(None)  
   
     def LayerOutlineColor(self):  
         layer = self.current_layer()  
         if layer is not None:  
             color = self.choose_color()  
             if color is not None:  
                 layer.SetStroke(color)  
   
     def LayerNoOutline(self):  
         layer = self.current_layer()  
         if layer is not None:  
             layer.SetStroke(None)  
548    
549      def HideLayer(self):      def HideLayer(self):
550          layer = self.current_layer()          layer = self.current_layer()
# Line 348  class MainWindow(wxFrame): Line 559  class MainWindow(wxFrame):
559      def LayerShowTable(self):      def LayerShowTable(self):
560          layer = self.current_layer()          layer = self.current_layer()
561          if layer is not None:          if layer is not None:
562              tv = tableview.TableFrame(self, layer.table)              table = layer.table
563              tv.Show(true)              name = "table_view" + str(id(table))
564                dialog = self.get_open_dialog(name)
565                if dialog is None:
566                    dialog = tableview.LayerTableFrame(self, name,
567                                             _("Layer Table: %s") % layer.Title(),
568                                             layer, table)
569                    self.add_dialog(name, dialog)
570                    dialog.Show(True)
571                else:
572                    # FIXME: bring dialog to front here
573                    pass
574    
575        def MapProjection(self):
576    
577            name = "map_projection"
578            dialog = self.get_open_dialog(name)
579    
580            if dialog is None:
581                map = self.canvas.Map()
582                dialog = projdialog.ProjFrame(self, name,
583                         _("Map Projection: %s") % map.Title(), map)
584                self.add_dialog(name, dialog)
585                dialog.Show()
586            dialog.Raise()
587    
588        def LayerProjection(self):
589    
590            layer = self.current_layer()
591    
592            name = "layer_projection" + str(id(layer))
593            dialog = self.get_open_dialog(name)
594    
595            if dialog is None:
596                map = self.canvas.Map()
597                dialog = projdialog.ProjFrame(self, name,
598                         _("Layer Projection: %s") % layer.Title(), layer)
599                self.add_dialog(name, dialog)
600                dialog.Show()
601            dialog.Raise()
602    
603        def LayerEditProperties(self):
604    
605            #
606            # the menu option for this should only be available if there
607            # is a current layer, so we don't need to check if the
608            # current layer is None
609            #
610    
611            layer = self.current_layer()
612            self.OpenLayerProperties(layer)
613    
614      def Projection(self):      def OpenLayerProperties(self, layer, group = None):
615          map = self.canvas.Map()          name = "layer_properties" + str(id(layer))
616          proj = map.projection          dialog = self.get_open_dialog(name)
617          if proj is None:  
618              proj4Dlg = proj4dialog.Proj4Dialog(NULL, None)          if dialog is None:
619                dialog = Classifier(self, name, layer, group)
620                self.add_dialog(name, dialog)
621                dialog.Show()
622            dialog.Raise()
623    
624        def LayerJoinTable(self):
625            print "LayerJoinTable: Not implemented."
626    
627        def LayerUnjoinTable(self):
628            print "LayerUnjoinTable: Not implemented."
629    
630        def ShowLegend(self):
631            if not self.LegendShown():
632                self.ToggleLegend()
633    
634        def ToggleLegend(self):
635            """Show the legend if it's not shown otherwise hide it again"""
636            name = "legend"
637            dialog = self.FindRegisteredDock(name)
638    
639            if dialog is None:
640                dialog = self.CreateDock(name, -1, _("Legend"), wxLAYOUT_LEFT)
641                legend.LegendPanel(dialog, None, self)
642                dialog.Dock()
643                dialog.GetPanel().SetMap(self.Map())
644                dialog.Show()
645          else:          else:
646              proj4Dlg = proj4dialog.Proj4Dialog(NULL, map.projection.params)              dialog.Show(not dialog.IsShown())
647          if proj4Dlg.ShowModal() == wxID_OK:  
648              params = proj4Dlg.GetParams()      def LegendShown(self):
649              if params is not None:          """Return true iff the legend is currently open"""
650                  proj = Projection(params)          dialog = self.FindRegisteredDock("legend")
651            return dialog is not None and dialog.IsShown()
652    
653        def TableOpen(self):
654            dlg = wxFileDialog(self, _("Open Table"), ".", "",
655                               _("DBF Files (*.dbf)") + "|*.dbf|" +
656                               #_("CSV Files (*.csv)") + "|*.csv|" +
657                               _("All Files (*.*)") + "|*.*",
658                               wxOPEN)
659            if dlg.ShowModal() == wxID_OK:
660                filename = dlg.GetPath()
661                dlg.Destroy()
662                try:
663                    table = self.application.session.OpenTableFile(filename)
664                except IOError:
665                    # the layer couldn't be opened
666                    self.RunMessageBox(_("Open Table"),
667                                       _("Can't open the file '%s'.") % filename)
668              else:              else:
669                  proj = None                  self.ShowTableView(table)
670              map.SetProjection(proj)  
671          proj4Dlg.Destroy()      def TableClose(self):
672            print "TableClose: not implemented"
673    
674        def TableShow(self):
675            """Offer a multi-selection dialog for tables to be displayed
676    
677            The windows for the selected tables are opened or brought to
678            the front.
679            """
680            tables = self.application.session.Tables()
681    
682            dlg = wxMultipleChoiceDialog(self, _("Pick the table to show:"),
683                                         _("Show Table"),
684                                         [t.Title() for t in tables],
685                                         size = (400,300), style = wxRESIZE_BORDER)
686            if (dlg.ShowModal() == wxID_OK):
687                for i in dlg.GetValue():
688                    # XXX: if the table belongs to a layer, open a
689                    # LayerTableFrame instead of QueryTableFrame
690                    self.ShowTableView(tables[i])
691    
692        def TableJoin(self):
693            dlg = JoinDialog(self, _("Join Tables"), self.application.session)
694            dlg.ShowModal()
695    
696        def ShowTableView(self, table):
697            """Open a table view for the table and optionally"""
698            name = "table_view%d" % id(table)
699            dialog = self.get_open_dialog(name)
700            if dialog is None:
701                dialog = tableview.QueryTableFrame(self, name,
702                                                   _("Table: %s") % table.Title(),
703                                                   table)
704                self.add_dialog(name, dialog)
705                dialog.Show(True)
706            # FIXME: else bring dialog to front
707    
708      def ZoomInTool(self):      def ZoomInTool(self):
709          self.canvas.ZoomInTool()          self.canvas.ZoomInTool()
# Line 377  class MainWindow(wxFrame): Line 715  class MainWindow(wxFrame):
715          self.canvas.PanTool()          self.canvas.PanTool()
716    
717      def IdentifyTool(self):      def IdentifyTool(self):
         if self.identify_view is None:  
             self.identify_view = identifyview.IdentifyView(self, main.app)  
             self.identify_view.Show(true)  
718          self.canvas.IdentifyTool()          self.canvas.IdentifyTool()
719            self.identify_view_on_demand(None, None)
720    
721      def LabelTool(self):      def LabelTool(self):
722          self.canvas.LabelTool()          self.canvas.LabelTool()
# Line 388  class MainWindow(wxFrame): Line 724  class MainWindow(wxFrame):
724      def FullExtent(self):      def FullExtent(self):
725          self.canvas.FitMapToWindow()          self.canvas.FitMapToWindow()
726    
727        def FullLayerExtent(self):
728            self.canvas.FitLayerToWindow(self.current_layer())
729    
730        def FullSelectionExtent(self):
731            self.canvas.FitSelectedToWindow()
732    
733        def ExportMap(self):
734            self.canvas.Export()
735    
736      def PrintMap(self):      def PrintMap(self):
737          self.canvas.Print()          self.canvas.Print()
738    
739        def RenameMap(self):
740            dlg = wxTextEntryDialog(self, "Map Title: ", "Rename Map",
741                                    self.Map().Title())
742            if dlg.ShowModal() == wxID_OK:
743                title = dlg.GetValue()
744                if title != "":
745                    self.Map().SetTitle(title)
746                    self.__SetTitle(title)
747    
748            dlg.Destroy()
749    
750        def identify_view_on_demand(self, layer, shapes):
751            """Subscribed to the canvas' SHAPES_SELECTED message
752    
753            If the current tool is the identify tool, at least one shape is
754            selected and the identify dialog is not shown, show the dialog.
755            """
756            # If the selection has become empty we don't need to do
757            # anything. Otherwise it could happen that the dialog was popped
758            # up when the selection became empty, e.g. when a new selection
759            # is opened while the identify tool is active and dialog had
760            # been closed
761            if not shapes:
762                return
763    
764            name = "identify_view"
765            if self.canvas.CurrentTool() == "IdentifyTool":
766                if not self.dialog_open(name):
767                    dialog = identifyview.IdentifyView(self, name)
768                    self.add_dialog(name, dialog)
769                    dialog.Show(True)
770                else:
771                    # FIXME: bring dialog to front?
772                    pass
773    
774        def __SetTitle(self, title):
775            self.SetTitle("Thuban - " + title)
776    
777  #  #
778  # Define all the commands available in the main window  # Define all the commands available in the main window
# Line 399  class MainWindow(wxFrame): Line 781  class MainWindow(wxFrame):
781    
782  # Helper functions to define common command implementations  # Helper functions to define common command implementations
783  def call_method(context, methodname, *args):  def call_method(context, methodname, *args):
784      """Call the context's method methodname with args *args"""      """Call the mainwindow's method methodname with args *args"""
785      apply(getattr(context, methodname), args)      apply(getattr(context.mainwindow, methodname), args)
786    
787  def _method_command(name, title, method, helptext = "", sensitive = None):  def _method_command(name, title, method, helptext = "",
788      """Add a command implemented by a method of the context object"""                      icon = "", sensitive = None, checked = None):
789        """Add a command implemented by a method of the mainwindow object"""
790      registry.Add(Command(name, title, call_method, args=(method,),      registry.Add(Command(name, title, call_method, args=(method,),
791                           helptext = helptext, sensitive = sensitive))                           helptext = helptext, icon = icon,
792                             sensitive = sensitive, checked = checked))
793    
794    def make_check_current_tool(toolname):
795        """Return a function that tests if the currently active tool is toolname
796    
797        The returned function can be called with the context and returns
798        true iff the currently active tool's name is toolname. It's directly
799        usable as the 'checked' callback of a command.
800        """
801        def check_current_tool(context, name=toolname):
802            return context.mainwindow.canvas.CurrentTool() == name
803        return check_current_tool
804    
805  def _tool_command(name, title, method, toolname, helptext = "",  def _tool_command(name, title, method, toolname, helptext = "",
806                    icon = ""):                    icon = "", sensitive = None):
807      """Add a tool command"""      """Add a tool command"""
808      def check_current_tool(context, name=toolname):      registry.Add(ToolCommand(name, title, call_method, args=(method,),
809          return context.canvas.CurrentTool() == name                               helptext = helptext, icon = icon,
810      registry.Add(Command(name, title, call_method, args=(method,),                               checked = make_check_current_tool(toolname),
811                           helptext = helptext, icon = icon,                               sensitive = sensitive))
                          checked = check_current_tool))  
812    
813  def _has_selected_layer(context):  def _has_selected_layer(context):
814      """Return true if a layer is selected in the context"""      """Return true if a layer is selected in the context"""
815      return context.has_selected_layer()      return context.mainwindow.has_selected_layer()
816    
817    def _has_selected_shapes(context):
818        """Return true if a layer is selected in the context"""
819        return context.mainwindow.has_selected_shapes()
820    
821    def _can_remove_layer(context):
822        return context.mainwindow.CanRemoveLayer()
823    
824    def _has_tree_window_shown(context):
825        """Return true if the tree window is shown"""
826        return context.mainwindow.SessionTreeShown()
827    
828    def _has_visible_map(context):
829        """Return true iff theres a visible map in the mainwindow.
830    
831        A visible map is a map with at least one visible layer."""
832        map = context.mainwindow.Map()
833        if map is not None:
834            for layer in map.Layers():
835                if layer.Visible():
836                    return 1
837        return 0
838    
839    def _has_legend_shown(context):
840        """Return true if the legend window is shown"""
841        return context.mainwindow.LegendShown()
842    
843    
844  # File menu  # File menu
845  _method_command("new_session", "&New Session", "NewSession")  _method_command("new_session", _("&New Session"), "NewSession")
846  _method_command("open_session", "&Open Session", "OpenSession")  _method_command("open_session", _("&Open Session..."), "OpenSession")
847  _method_command("save_session", "&Save Session", "SaveSession")  _method_command("save_session", _("&Save Session"), "SaveSession")
848  _method_command("save_session_as", "Save Session &As", "SaveSessionAs")  _method_command("save_session_as", _("Save Session &As..."), "SaveSessionAs")
849  _method_command("exit", "&Exit", "Exit")  _method_command("toggle_session_tree", _("Session &Tree"), "ToggleSessionTree",
850                    checked = _has_tree_window_shown)
851    _method_command("toggle_legend", _("Legend"), "ToggleLegend",
852                    checked = _has_legend_shown)
853    _method_command("exit", _("E&xit"), "Exit")
854    
855  # Help menu  # Help menu
856  _method_command("help_about", "&About", "About")  _method_command("help_about", _("&About..."), "About")
857    
858    
859  # Map menu  # Map menu
860  _method_command("map_projection", "Pro&jection", "Projection")  _method_command("map_projection", _("Pro&jection..."), "MapProjection")
861    
862  _tool_command("map_zoom_in_tool", "&Zoom in", "ZoomInTool", "ZoomInTool",  _tool_command("map_zoom_in_tool", _("&Zoom in"), "ZoomInTool", "ZoomInTool",
863                helptext = "Switch to map-mode 'zoom-in'", icon = "zoom_in")                helptext = _("Switch to map-mode 'zoom-in'"), icon = "zoom_in",
864  _tool_command("map_zoom_out_tool", "Zoom &out", "ZoomOutTool", "ZoomOutTool",                sensitive = _has_visible_map)
865                helptext = "Switch to map-mode 'zoom-out'", icon = "zoom_out")  _tool_command("map_zoom_out_tool", _("Zoom &out"), "ZoomOutTool", "ZoomOutTool",
866  _tool_command("map_pan_tool", "&Pan", "PanTool", "PanTool",                helptext = _("Switch to map-mode 'zoom-out'"), icon = "zoom_out",
867                helptext = "Switch to map-mode 'pan'", icon = "pan")                sensitive = _has_visible_map)
868  _tool_command("map_identify_tool", "&Identify", "IdentifyTool", "IdentifyTool",  _tool_command("map_pan_tool", _("&Pan"), "PanTool", "PanTool",
869                helptext = "Switch to map-mode 'identify'", icon = "identify")                helptext = _("Switch to map-mode 'pan'"), icon = "pan",
870  _tool_command("map_label_tool", "&Label", "LabelTool", "LabelTool",                sensitive = _has_visible_map)
871                helptext = "Add/Remove labels", icon = "label")  _tool_command("map_identify_tool", _("&Identify"), "IdentifyTool",
872  _method_command("map_full_extent", "&Full extent", "FullExtent")                "IdentifyTool",
873  _method_command("map_print", "Prin&t", "PrintMap", helptext = "Print the map")                helptext = _("Switch to map-mode 'identify'"), icon = "identify",
874                  sensitive = _has_visible_map)
875    _tool_command("map_label_tool", _("&Label"), "LabelTool", "LabelTool",
876                  helptext = _("Add/Remove labels"), icon = "label",
877                  sensitive = _has_visible_map)
878    _method_command("map_full_extent", _("&Full extent"), "FullExtent",
879                   helptext = _("Full Extent"), icon = "fullextent",
880                  sensitive = _has_visible_map)
881    _method_command("layer_full_extent", _("&Full layer extent"), "FullLayerExtent",
882                   helptext = _("Full Layer Extent"), icon = "fulllayerextent",
883                  sensitive = _has_selected_layer)
884    _method_command("selected_full_extent", _("&Full selection extent"), "FullSelectionExtent",
885                   helptext = _("Full Selection Extent"), icon = "fullselextent",
886                  sensitive = _has_selected_shapes)
887    _method_command("map_export", _("E&xport"), "ExportMap",
888                        helptext = _("Export the map to file"))
889    _method_command("map_print", _("Prin&t"), "PrintMap",
890                    helptext = _("Print the map"))
891    _method_command("map_rename", _("&Rename..."), "RenameMap",
892                    helptext = _("Rename the map"))
893    _method_command("layer_add", _("&Add Layer..."), "AddLayer",
894                    helptext = _("Add a new layer to active map"))
895    _method_command("rasterlayer_add", _("&Add Image Layer..."), "AddRasterLayer",
896                    helptext = _("Add a new image layer to active map"))
897    _method_command("layer_remove", _("&Remove Layer"), "RemoveLayer",
898                    helptext = _("Remove selected layer(s)"),
899                    sensitive = _can_remove_layer)
900    
901  # Layer menu  # Layer menu
902  _method_command("layer_add", "&Add", "AddLayer",  _method_command("layer_projection", _("Pro&jection..."), "LayerProjection",
                 helptext = "Add a new layer to active map")  
 _method_command("layer_remove", "&Remove", "RemoveLayer",  
                 helptext = "Remove selected layer(s)",  
903                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
904  _method_command("layer_fill_color", "&Fill Color", "LayerFillColor",  _method_command("layer_raise", _("&Raise"), "RaiseLayer",
905                  helptext = "Set the fill color of selected layer(s)",                  helptext = _("Raise selected layer(s)"),
906                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
907  _method_command("layer_transparent_fill", "&Transparent Fill",  _method_command("layer_lower", _("&Lower"), "LowerLayer",
908                  "LayerTransparentFill",                  helptext = _("Lower selected layer(s)"),
                 helptext = "Do not fill the selected layer(s)",  
909                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
910  _method_command("layer_ourline_color", "&Outline Color", "LayerOutlineColor",  _method_command("layer_show", _("&Show"), "ShowLayer",
911                  helptext = "Set the outline color of selected layer(s)",                  helptext = _("Make selected layer(s) visible"),
912                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
913  _method_command("layer_no_outline", "&No Outline", "LayerNoOutline",  _method_command("layer_hide", _("&Hide"), "HideLayer",
914                  helptext = "Do not draw the outline of the selected layer(s)",                  helptext = _("Make selected layer(s) unvisible"),
915                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
916  _method_command("layer_raise", "&Raise", "RaiseLayer",  _method_command("layer_show_table", _("Show Ta&ble"), "LayerShowTable",
917                  helptext = "Raise selected layer(s)",                  helptext = _("Show the selected layer's table"),
918                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
919  _method_command("layer_lower", "&Lower", "LowerLayer",  _method_command("layer_properties", _("&Properties..."), "LayerEditProperties",
                 helptext = "Lower selected layer(s)",  
920                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
921  _method_command("layer_show", "&Show", "ShowLayer",  _method_command("layer_jointable", _("&Join Table..."), "LayerJoinTable",
                 helptext = "Make selected layer(s) visible",  
922                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
923  _method_command("layer_hide", "&Hide", "HideLayer",  _method_command("layer_unjointable", _("&Unjoin Table..."), "LayerUnjoinTable",
                 helptext = "Make selected layer(s) unvisible",  
                 sensitive = _has_selected_layer)  
 _method_command("layer_show_table", "Show Ta&ble", "LayerShowTable",  
                 helptext = "Show the selected layer's table",  
924                  sensitive = _has_selected_layer)                  sensitive = _has_selected_layer)
925    
926    # Table menu
927    _method_command("table_open", _("&Open..."), "TableOpen")
928    _method_command("table_close", _("&Close"), "TableClose")
929    _method_command("table_show", _("&Show"), "TableShow")
930    _method_command("table_join", _("&Join..."), "TableJoin")
931    
932    #  Export only under Windows ...
933    map_menu = ["layer_add", "rasterlayer_add", "layer_remove", "map_rename",
934                            None,
935                            "map_projection",
936                            None,
937                            "map_zoom_in_tool", "map_zoom_out_tool",
938                            "map_pan_tool",
939                            "map_full_extent",
940                            "layer_full_extent",
941                            "selected_full_extent",
942                            None,
943                            "map_identify_tool", "map_label_tool",
944                            None,
945                            "toggle_legend",
946                            None]
947    if wxPlatform == '__WXMSW__':
948        map_menu.append("map_export")
949    map_menu.append("map_print")
950    
951    # the menu structure
952    main_menu = Menu("<main>", "<main>",
953                     [Menu("file", _("&File"),
954                           ["new_session", "open_session", None,
955                            "save_session", "save_session_as", None,
956                            "toggle_session_tree", None,
957                            "exit"]),
958                      Menu("map", _("&Map"), map_menu),
959                      Menu("layer", _("&Layer"),
960                            ["layer_raise", "layer_lower",
961                            None,
962                            "layer_show", "layer_hide",
963                            None,
964                            "layer_projection",
965                            None,
966                            "layer_show_table",
967                            "layer_jointable",
968                            "layer_unjointable",
969                            None,
970                            "layer_properties"]),
971                      Menu("table", _("&Table"),
972                           ["table_open", "table_close",
973                           None,
974                           "table_show",
975                           None,
976                           "table_join"]),
977                      Menu("help", _("&Help"),
978                           ["help_about"])])
979    
980    # the main toolbar
981    
982    main_toolbar = Menu("<toolbar>", "<toolbar>",
983                        ["map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool",
984                         "map_full_extent",
985                         "layer_full_extent",
986                         "selected_full_extent",
987                         None,
988                         "map_identify_tool", "map_label_tool"])

Legend:
Removed from v.18  
changed lines
  Added in v.1056

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26