GRASS Programmer's Manual  6.5.svn(2012)-r51648
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
gui_core/toolbars.py
Go to the documentation of this file.
00001 """!
00002 @package gui_core.toolbars
00003 
00004 @brief Base classes toolbar widgets
00005 
00006 Classes:
00007  - toolbars::BaseToolbar
00008 
00009 (C) 2007-2011 by the GRASS Development Team
00010 
00011 This program is free software under the GNU General Public License
00012 (>=v2). Read the file COPYING that comes with GRASS for details.
00013 
00014 @author Michael Barton
00015 @author Jachym Cepicky
00016 @author Martin Landa <landa.martin gmail.com>
00017 """
00018 
00019 import os
00020 import sys
00021 import platform
00022 
00023 import wx
00024 
00025 from core               import globalvar
00026 from core.debug         import Debug
00027 from icons.icon         import MetaIcon
00028 
00029 BaseIcons = {
00030     'display'    : MetaIcon(img = 'show',
00031                             label = _('Display map'),
00032                             desc  =  _('Re-render modified map layers only')),
00033     'render'     : MetaIcon(img = 'layer-redraw',
00034                             label = _('Render map'),
00035                             desc = _('Force re-rendering all map layers')),
00036     'erase'      : MetaIcon(img = 'erase',
00037                             label = _('Erase display'),
00038                             desc = _('Erase display canvas with given background color')),
00039     'pointer'    : MetaIcon(img = 'pointer',
00040                             label = _('Pointer')),
00041     'zoomIn'     : MetaIcon(img = 'zoom-in',
00042                             label = _('Zoom in'),
00043                             desc = _('Drag or click mouse to zoom')),
00044     'zoomOut'    : MetaIcon(img = 'zoom-out',
00045                             label = _('Zoom out'),
00046                             desc = _('Drag or click mouse to unzoom')),
00047     'zoomBack'   : MetaIcon(img = 'zoom-last',
00048                             label = _('Return to previous zoom')),
00049     'zoomMenu'   : MetaIcon(img = 'zoom-more',
00050                             label = _('Various zoom options'),
00051                             desc = _('Zoom to computational, default, saved region, ...')),
00052     'zoomExtent' : MetaIcon(img = 'zoom-extent',
00053                             label = _('Zoom to selected map layer(s)')),
00054     'pan'        : MetaIcon(img = 'pan',
00055                             label = _('Pan'),
00056                             desc = _('Drag with mouse to pan')),
00057     'saveFile'   : MetaIcon(img = 'map-export',
00058                             label = _('Save display to graphic file')),
00059     'print'      : MetaIcon(img = 'print',
00060                             label = _('Print display')),
00061     'font'       : MetaIcon(img = 'font',
00062                             label = _('Select font')),
00063     'help'       : MetaIcon(img = 'help',
00064                             label = _('Show manual')),
00065     'quit'       : MetaIcon(img = 'quit',
00066                             label = _('Quit')),
00067     'addRast'    : MetaIcon(img = 'layer-raster-add',
00068                             label = _('Add raster map layer')),
00069     'addVect'    : MetaIcon(img = 'layer-vector-add',
00070                             label = _('Add vector map layer')),
00071     'overlay'    : MetaIcon(img = 'overlay-add',
00072                             label = _('Add map elements'),
00073                             desc = _('Overlay elements like scale and legend onto map')),
00074     'histogramD' : MetaIcon(img = 'layer-raster-histogram',
00075                             label = _('Create histogram with d.histogram')),
00076     'settings'   : MetaIcon(img = 'settings',
00077                             label = _("Settings")),
00078     }
00079     
00080 class BaseToolbar(wx.ToolBar):
00081     """!Abstract toolbar class"""
00082     def __init__(self, parent):
00083         self.parent = parent
00084         wx.ToolBar.__init__(self, parent = self.parent, id = wx.ID_ANY)
00085         
00086         self.action = dict()
00087         
00088         self.Bind(wx.EVT_TOOL, self.OnTool)
00089         
00090         self.SetToolBitmapSize(globalvar.toolbarSize)
00091         
00092     def InitToolbar(self, toolData):
00093         """!Initialize toolbar, add tools to the toolbar
00094         """
00095         for tool in toolData:
00096             self.CreateTool(*tool)
00097         
00098         self._data = toolData
00099         
00100     def _toolbarData(self):
00101         """!Toolbar data (virtual)"""
00102         return None
00103     
00104     def CreateTool(self, label, bitmap, kind,
00105                    shortHelp, longHelp, handler, pos = -1):
00106         """!Add tool to the toolbar
00107         
00108         @param pos if -1 add tool, if > 0 insert at given pos
00109         @return id of tool
00110         """
00111         bmpDisabled = wx.NullBitmap
00112         tool = -1
00113         if label: 
00114             tool = vars(self)[label] = wx.NewId()
00115             Debug.msg(3, "CreateTool(): tool=%d, label=%s bitmap=%s" % \
00116                           (tool, label, bitmap))
00117             if pos < 0:
00118                 toolWin = self.AddLabelTool(tool, label, bitmap,
00119                                             bmpDisabled, kind,
00120                                             shortHelp, longHelp)
00121             else:
00122                 toolWin = self.InsertLabelTool(pos, tool, label, bitmap,
00123                                             bmpDisabled, kind,
00124                                             shortHelp, longHelp)
00125             self.Bind(wx.EVT_TOOL, handler, toolWin)
00126         else: # separator
00127             self.AddSeparator()
00128         
00129         return tool
00130     
00131     def EnableLongHelp(self, enable = True):
00132         """!Enable/disable long help
00133         
00134         @param enable True for enable otherwise disable
00135         """
00136         for tool in self._data:
00137             if tool[0] == '': # separator
00138                 continue
00139             
00140             if enable:
00141                 self.SetToolLongHelp(vars(self)[tool[0]], tool[4])
00142             else:
00143                 self.SetToolLongHelp(vars(self)[tool[0]], "")
00144         
00145     def OnTool(self, event):
00146         """!Tool selected
00147         """
00148         if self.parent.GetName() == "GCPFrame":
00149             return
00150         
00151         if hasattr(self.parent, 'toolbars'):
00152             if self.parent.GetToolbar('vdigit'):
00153                 # update vdigit toolbar (unselect currently selected tool)
00154                 id = self.parent.toolbars['vdigit'].GetAction(type = 'id')
00155                 self.parent.toolbars['vdigit'].ToggleTool(id, False)
00156         
00157         if event:
00158             # deselect previously selected tool
00159             id = self.action.get('id', -1)
00160             if id != event.GetId():
00161                 self.ToggleTool(self.action['id'], False)
00162             else:
00163                 self.ToggleTool(self.action['id'], True)
00164             
00165             self.action['id'] = event.GetId()
00166             
00167             event.Skip()
00168         else:
00169             # initialize toolbar
00170             self.ToggleTool(self.action['id'], True)
00171         
00172     def GetAction(self, type = 'desc'):
00173         """!Get current action info"""
00174         return self.action.get(type, '')
00175     
00176     def SelectDefault(self, event):
00177         """!Select default tool"""
00178         self.ToggleTool(self.defaultAction['id'], True)
00179         self.defaultAction['bind'](event)
00180         self.action = { 'id' : self.defaultAction['id'],
00181                         'desc' : self.defaultAction.get('desc', '') }
00182         
00183     def FixSize(self, width):
00184         """!Fix toolbar width on Windows
00185             
00186         @todo Determine why combobox causes problems here
00187         """
00188         if platform.system() == 'Windows':
00189             size = self.GetBestSize()
00190             self.SetSize((size[0] + width, size[1]))
00191 
00192     def Enable(self, tool, enable = True):
00193         """!Enable defined tool
00194         
00195         @param tool name
00196         @param enable True to enable otherwise disable tool
00197         """
00198         try:
00199             id = getattr(self, tool)
00200         except AttributeError:
00201             return
00202         
00203         self.EnableTool(id, enable)
00204         
00205     def _getToolbarData(self, data):
00206         """!Define tool
00207         """
00208         retData = list()
00209         for args in data:
00210             retData.append(self._defineTool(*args))
00211         return retData
00212 
00213     def _defineTool(self, name = None, icon = None, handler = None, item = wx.ITEM_NORMAL, pos = -1):
00214         """!Define tool
00215         """
00216         if name:
00217             return (name, icon.GetBitmap(),
00218                     item, icon.GetLabel(), icon.GetDesc(),
00219                     handler, pos)
00220         return ("", "", "", "", "", "") # separator
00221 
00222     def _onMenu(self, data):
00223         """!Toolbar pop-up menu"""
00224         menu = wx.Menu()
00225         
00226         for icon, handler in data:
00227             item = wx.MenuItem(menu, wx.ID_ANY, icon.GetLabel())
00228             item.SetBitmap(icon.GetBitmap(self.parent.iconsize))
00229             menu.AppendItem(item)
00230             self.Bind(wx.EVT_MENU, handler, item)
00231         
00232         self.PopupMenu(menu)
00233         menu.Destroy()