/[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 188 by bh, Tue May 28 12:38:45 2002 UTC revision 222 by bh, Thu Jul 18 13:03:08 2002 UTC
# Line 29  import tableview, identifyview Line 29  import tableview, identifyview
29  from menu import Menu  from menu import Menu
30    
31  import main  import main
32    from context import Context
33  from command import registry, Command  from command import registry, Command
34  from messages import SELECTED_SHAPE, VIEW_POSITION  from messages import SELECTED_SHAPE, VIEW_POSITION
35    
# Line 54  class MainWindow(wxFrame): Line 55  class MainWindow(wxFrame):
55    
56          self.init_ids()          self.init_ids()
57    
58          #main_menu.print_menu()          # creat the menubar from the main_menu description
59          self.SetMenuBar(self.build_menu_bar(main_menu))          self.SetMenuBar(self.build_menu_bar(main_menu))
60    
61          # toolbar          # Similarly, create the toolbar from main_toolbar
62          toolbar = self.CreateToolBar(wxTB_3DBUTTONS)          toolbar = self.build_toolbar(main_toolbar)
   
         # set the size of the tools' bitmaps. Not needed on wxGTK, but  
         # on Windows. We probably shouldn't hardwire the bitmap size  
         # here  
         toolbar.SetToolBitmapSize(wxSize(24, 24))  
   
         for name in ["map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool",  
                      "map_identify_tool", "map_label_tool", "map_full_extent"]:  
             self.add_toolbar_command(toolbar, name)  
63          # call Realize to make sure that the tools appear.          # call Realize to make sure that the tools appear.
64          toolbar.Realize()          toolbar.Realize()
65    
# Line 87  class MainWindow(wxFrame): Line 79  class MainWindow(wxFrame):
79          self.current_id = 6000          self.current_id = 6000
80          self.id_to_name = {}          self.id_to_name = {}
81          self.name_to_id = {}          self.name_to_id = {}
82            self.events_bound = {}
83    
84      def get_id(self, name):      def get_id(self, name):
85          """Return the wxWindows id for the command named name.          """Return the wxWindows id for the command named name.
# Line 100  class MainWindow(wxFrame): Line 93  class MainWindow(wxFrame):
93              self.id_to_name[ID] = name              self.id_to_name[ID] = name
94          return ID          return ID
95    
96        def bind_command_events(self, command, ID):
97            """Bind the necessary events for the given command and ID"""
98            if not self.events_bound.has_key(ID):
99                # the events haven't been bound yet
100                EVT_MENU(self, ID, self.invoke_command)
101                if command.IsDynamic():
102                    EVT_UPDATE_UI(self, ID, self.update_command_ui)
103    
104      def build_menu_bar(self, menudesc):      def build_menu_bar(self, menudesc):
105          """Build and return the menu bar from the menu description"""          """Build and return the menu bar from the menu description"""
106          menu_bar = wxMenuBar()          menu_bar = wxMenuBar()
# Line 130  class MainWindow(wxFrame): Line 131  class MainWindow(wxFrame):
131              last = item              last = item
132          return wxmenu          return wxmenu
133    
134        def build_toolbar(self, toolbardesc):
135            """Build and return the main toolbar window from a toolbar description
136    
137            The parameter should be an instance of the Menu class but it
138            should not contain submenus.
139            """
140            toolbar = self.CreateToolBar(wxTB_3DBUTTONS)
141    
142            # set the size of the tools' bitmaps. Not needed on wxGTK, but
143            # on Windows, although it doesn't work very well there. It seems
144            # that only 16x16 icons are really supported on windows.
145            # We probably shouldn't hardwire the bitmap size here.
146            toolbar.SetToolBitmapSize(wxSize(24, 24))
147    
148            for item in toolbardesc.items:
149                if item is None:
150                    toolbar.AddSeparator()
151                else:
152                    # assume it's a string.
153                    self.add_toolbar_command(toolbar, item)
154    
155            return toolbar
156    
157      def add_menu_command(self, menu, name):      def add_menu_command(self, menu, name):
158          """Add the command with name name to the menu menu.          """Add the command with name name to the menu menu.
159    
# Line 143  class MainWindow(wxFrame): Line 167  class MainWindow(wxFrame):
167                  ID = self.get_id(name)                  ID = self.get_id(name)
168                  menu.Append(ID, command.Title(), command.HelpText(),                  menu.Append(ID, command.Title(), command.HelpText(),
169                              command.IsCheckCommand())                              command.IsCheckCommand())
170                  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)  
171              else:              else:
172                  print "Unknown command %s" % name                  print "Unknown command %s" % name
173    
# Line 167  class MainWindow(wxFrame): Line 189  class MainWindow(wxFrame):
189                  toolbar.AddTool(ID, bitmap,                  toolbar.AddTool(ID, bitmap,
190                                  shortHelpString = command.HelpText(),                                  shortHelpString = command.HelpText(),
191                                  isToggle = command.IsCheckCommand())                                  isToggle = command.IsCheckCommand())
192                    self.bind_command_events(command, ID)
193              else:              else:
194                  print "Unknown command %s" % name                  print "Unknown command %s" % name
195    
# Line 174  class MainWindow(wxFrame): Line 197  class MainWindow(wxFrame):
197          name = self.id_to_name.get(event.GetId())          name = self.id_to_name.get(event.GetId())
198          if name is not None:          if name is not None:
199              command = registry.Command(name)              command = registry.Command(name)
200              command.Execute(self)              command.Execute(Context(main.app, main.app.Session(), self))
201          else:          else:
202              print "Unknown command ID %d" % event.GetId()              print "Unknown command ID %d" % event.GetId()
203    
204      def update_command_ui(self, event):      def update_command_ui(self, event):
205          #print "update_command_ui", self.id_to_name[event.GetId()]          #print "update_command_ui", self.id_to_name[event.GetId()]
206            context = Context(main.app, main.app.Session(), self)
207          command = registry.Command(self.id_to_name[event.GetId()])          command = registry.Command(self.id_to_name[event.GetId()])
208          if command is not None:          if command is not None:
209              event.Enable(command.Sensitive(self))              event.Enable(command.Sensitive(context))
210              event.SetText(command.DynText(self))              event.SetText(command.DynText(context))
211              if command.IsCheckCommand():              if command.IsCheckCommand():
212                  event.Check(command.Checked(self))                  event.Check(command.Checked(context))
213    
214      def RunMessageBox(self, title, text, flags = wxOK | wxICON_INFORMATION):      def RunMessageBox(self, title, text, flags = wxOK | wxICON_INFORMATION):
215          """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 335  class MainWindow(wxFrame): Line 359  class MainWindow(wxFrame):
359          layer = self.current_layer()          layer = self.current_layer()
360          if layer is not None:          if layer is not None:
361              self.canvas.Map().RaiseLayer(layer)              self.canvas.Map().RaiseLayer(layer)
362            
363      def LowerLayer(self):      def LowerLayer(self):
364          layer = self.current_layer()          layer = self.current_layer()
365          if layer is not None:          if layer is not None:
# Line 475  class MainWindow(wxFrame): Line 499  class MainWindow(wxFrame):
499    
500  # Helper functions to define common command implementations  # Helper functions to define common command implementations
501  def call_method(context, methodname, *args):  def call_method(context, methodname, *args):
502      """Call the context's method methodname with args *args"""      """Call the mainwindow's method methodname with args *args"""
503      apply(getattr(context, methodname), args)      apply(getattr(context.mainwindow, methodname), args)
504    
505  def _method_command(name, title, method, helptext = "",  def _method_command(name, title, method, helptext = "",
506                      icon = "", sensitive = None):                      icon = "", sensitive = None):
507      """Add a command implemented by a method of the context object"""      """Add a command implemented by a method of the mainwindow object"""
508      registry.Add(Command(name, title, call_method, args=(method,),      registry.Add(Command(name, title, call_method, args=(method,),
509                           helptext = helptext, icon = icon,                           helptext = helptext, icon = icon,
510                           sensitive = sensitive))                           sensitive = sensitive))
# Line 489  def _tool_command(name, title, method, t Line 513  def _tool_command(name, title, method, t
513                    icon = ""):                    icon = ""):
514      """Add a tool command"""      """Add a tool command"""
515      def check_current_tool(context, name=toolname):      def check_current_tool(context, name=toolname):
516          return context.canvas.CurrentTool() == name          return context.mainwindow.canvas.CurrentTool() == name
517      registry.Add(Command(name, title, call_method, args=(method,),      registry.Add(Command(name, title, call_method, args=(method,),
518                           helptext = helptext, icon = icon,                           helptext = helptext, icon = icon,
519                           checked = check_current_tool))                           checked = check_current_tool))
520    
521  def _has_selected_layer(context):  def _has_selected_layer(context):
522      """Return true if a layer is selected in the context"""      """Return true if a layer is selected in the context"""
523      return context.has_selected_layer()      return context.mainwindow.has_selected_layer()
524    
525  # File menu  # File menu
526  _method_command("new_session", "&New Session", "NewSession")  _method_command("new_session", "&New Session", "NewSession")
# Line 592  main_menu = Menu("<main>", "<main>", Line 616  main_menu = Menu("<main>", "<main>",
616                          "layer_show_table"]),                          "layer_show_table"]),
617                    Menu("help", "&Help",                    Menu("help", "&Help",
618                         ["help_about"])])                         ["help_about"])])
619    
620    # the main toolbar
621    
622    main_toolbar = Menu("<toolbar>", "<toolbar>",
623                        ["map_zoom_in_tool", "map_zoom_out_tool", "map_pan_tool",
624                         "map_identify_tool", "map_label_tool", "map_full_extent"])

Legend:
Removed from v.188  
changed lines
  Added in v.222

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26