GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gui_core/toolbars.py
Go to the documentation of this file.
1 """!
2 @package gui_core.toolbars
3 
4 @brief Base classes toolbar widgets
5 
6 Classes:
7  - toolbars::BaseToolbar
8 
9 (C) 2007-2011 by the GRASS Development Team
10 
11 This program is free software under the GNU General Public License
12 (>=v2). Read the file COPYING that comes with GRASS for details.
13 
14 @author Michael Barton
15 @author Jachym Cepicky
16 @author Martin Landa <landa.martin gmail.com>
17 """
18 
19 import os
20 import sys
21 import platform
22 
23 import wx
24 
25 from core import globalvar
26 from core.debug import Debug
27 from icons.icon import MetaIcon
28 
29 BaseIcons = {
30  'display' : MetaIcon(img = 'show',
31  label = _('Display map'),
32  desc = _('Re-render modified map layers only')),
33  'render' : MetaIcon(img = 'layer-redraw',
34  label = _('Render map'),
35  desc = _('Force re-rendering all map layers')),
36  'erase' : MetaIcon(img = 'erase',
37  label = _('Erase display'),
38  desc = _('Erase display canvas with given background color')),
39  'pointer' : MetaIcon(img = 'pointer',
40  label = _('Pointer')),
41  'zoomIn' : MetaIcon(img = 'zoom-in',
42  label = _('Zoom in'),
43  desc = _('Drag or click mouse to zoom')),
44  'zoomOut' : MetaIcon(img = 'zoom-out',
45  label = _('Zoom out'),
46  desc = _('Drag or click mouse to unzoom')),
47  'zoomBack' : MetaIcon(img = 'zoom-last',
48  label = _('Return to previous zoom')),
49  'zoomMenu' : MetaIcon(img = 'zoom-more',
50  label = _('Various zoom options'),
51  desc = _('Zoom to computational, default, saved region, ...')),
52  'zoomExtent' : MetaIcon(img = 'zoom-extent',
53  label = _('Zoom to selected map layer(s)')),
54  'pan' : MetaIcon(img = 'pan',
55  label = _('Pan'),
56  desc = _('Drag with mouse to pan')),
57  'saveFile' : MetaIcon(img = 'map-export',
58  label = _('Save display to graphic file')),
59  'print' : MetaIcon(img = 'print',
60  label = _('Print display')),
61  'font' : MetaIcon(img = 'font',
62  label = _('Select font')),
63  'help' : MetaIcon(img = 'help',
64  label = _('Show manual')),
65  'quit' : MetaIcon(img = 'quit',
66  label = _('Quit')),
67  'addRast' : MetaIcon(img = 'layer-raster-add',
68  label = _('Add raster map layer')),
69  'addVect' : MetaIcon(img = 'layer-vector-add',
70  label = _('Add vector map layer')),
71  'overlay' : MetaIcon(img = 'overlay-add',
72  label = _('Add map elements'),
73  desc = _('Overlay elements like scale and legend onto map')),
74  'histogramD' : MetaIcon(img = 'layer-raster-histogram',
75  label = _('Create histogram with d.histogram')),
76  'settings' : MetaIcon(img = 'settings',
77  label = _("Settings")),
78  }
79 
80 class BaseToolbar(wx.ToolBar):
81  """!Abstract toolbar class"""
82  def __init__(self, parent):
83  self.parent = parent
84  wx.ToolBar.__init__(self, parent = self.parent, id = wx.ID_ANY)
85 
86  self.action = dict()
87 
88  self.Bind(wx.EVT_TOOL, self.OnTool)
89 
90  self.SetToolBitmapSize(globalvar.toolbarSize)
91 
92  def InitToolbar(self, toolData):
93  """!Initialize toolbar, add tools to the toolbar
94  """
95  for tool in toolData:
96  self.CreateTool(*tool)
97 
98  self._data = toolData
99 
100  def _toolbarData(self):
101  """!Toolbar data (virtual)"""
102  return None
103 
104  def CreateTool(self, label, bitmap, kind,
105  shortHelp, longHelp, handler, pos = -1):
106  """!Add tool to the toolbar
107 
108  @param pos if -1 add tool, if > 0 insert at given pos
109  @return id of tool
110  """
111  bmpDisabled = wx.NullBitmap
112  tool = -1
113  if label:
114  tool = vars(self)[label] = wx.NewId()
115  Debug.msg(3, "CreateTool(): tool=%d, label=%s bitmap=%s" % \
116  (tool, label, bitmap))
117  if pos < 0:
118  toolWin = self.AddLabelTool(tool, label, bitmap,
119  bmpDisabled, kind,
120  shortHelp, longHelp)
121  else:
122  toolWin = self.InsertLabelTool(pos, tool, label, bitmap,
123  bmpDisabled, kind,
124  shortHelp, longHelp)
125  self.Bind(wx.EVT_TOOL, handler, toolWin)
126  else: # separator
127  self.AddSeparator()
128 
129  return tool
130 
131  def EnableLongHelp(self, enable = True):
132  """!Enable/disable long help
133 
134  @param enable True for enable otherwise disable
135  """
136  for tool in self._data:
137  if tool[0] == '': # separator
138  continue
139 
140  if enable:
141  self.SetToolLongHelp(vars(self)[tool[0]], tool[4])
142  else:
143  self.SetToolLongHelp(vars(self)[tool[0]], "")
144 
145  def OnTool(self, event):
146  """!Tool selected
147  """
148 
149  if hasattr(self.parent, 'toolbars'):
150  if self.parent.GetToolbar('vdigit'):
151  # update vdigit toolbar (unselect currently selected tool)
152  id = self.parent.toolbars['vdigit'].GetAction(type = 'id')
153  self.parent.toolbars['vdigit'].ToggleTool(id, False)
154 
155  if event:
156  # deselect previously selected tool
157  id = self.action.get('id', -1)
158  if id != event.GetId():
159  self.ToggleTool(self.action['id'], False)
160  else:
161  self.ToggleTool(self.action['id'], True)
162 
163  self.action['id'] = event.GetId()
164 
165  event.Skip()
166  else:
167  # initialize toolbar
168  self.ToggleTool(self.action['id'], True)
169 
170  def GetAction(self, type = 'desc'):
171  """!Get current action info"""
172  return self.action.get(type, '')
173 
174  def SelectDefault(self, event):
175  """!Select default tool"""
176  self.ToggleTool(self.defaultAction['id'], True)
177  self.defaultAction['bind'](event)
178  self.action = { 'id' : self.defaultAction['id'],
179  'desc' : self.defaultAction.get('desc', '') }
180 
181  def FixSize(self, width):
182  """!Fix toolbar width on Windows
183 
184  @todo Determine why combobox causes problems here
185  """
186  if platform.system() == 'Windows':
187  size = self.GetBestSize()
188  self.SetSize((size[0] + width, size[1]))
189 
190  def Enable(self, tool, enable = True):
191  """!Enable defined tool
192 
193  @param tool name
194  @param enable True to enable otherwise disable tool
195  """
196  try:
197  id = getattr(self, tool)
198  except AttributeError:
199  return
200 
201  self.EnableTool(id, enable)
202 
203  def _getToolbarData(self, data):
204  """!Define tool
205  """
206  retData = list()
207  for args in data:
208  retData.append(self._defineTool(*args))
209  return retData
210 
211  def _defineTool(self, name = None, icon = None, handler = None, item = wx.ITEM_NORMAL, pos = -1):
212  """!Define tool
213  """
214  if name:
215  return (name, icon.GetBitmap(),
216  item, icon.GetLabel(), icon.GetDesc(),
217  handler, pos)
218  return ("", "", "", "", "", "") # separator
219 
220  def _onMenu(self, data):
221  """!Toolbar pop-up menu"""
222  menu = wx.Menu()
223 
224  for icon, handler in data:
225  item = wx.MenuItem(menu, wx.ID_ANY, icon.GetLabel())
226  item.SetBitmap(icon.GetBitmap(self.parent.iconsize))
227  menu.AppendItem(item)
228  self.Bind(wx.EVT_MENU, handler, item)
229 
230  self.PopupMenu(menu)
231  menu.Destroy()
def Enable
Enable defined tool.
def EnableLongHelp
Enable/disable long help.
def SelectDefault
Select default tool.
wxGUI debugging
def OnTool
Tool selected.
def InitToolbar
Initialize toolbar, add tools to the toolbar.
def _defineTool
Define tool.
def CreateTool
Add tool to the toolbar.
Abstract toolbar class.
def FixSize
Fix toolbar width on Windows.
def GetAction
Get current action info.