toolbars.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. """!
  2. @package gui_core.toolbars
  3. @brief Base classes toolbar widgets
  4. Classes:
  5. - toolbars::BaseToolbar
  6. (C) 2007-2011 by the GRASS Development Team
  7. This program is free software under the GNU General Public License
  8. (>=v2). Read the file COPYING that comes with GRASS for details.
  9. @author Michael Barton
  10. @author Jachym Cepicky
  11. @author Martin Landa <landa.martin gmail.com>
  12. """
  13. import os
  14. import sys
  15. import platform
  16. import wx
  17. from core import globalvar
  18. from core.debug import Debug
  19. class BaseToolbar(wx.ToolBar):
  20. """!Abstract toolbar class"""
  21. def __init__(self, parent):
  22. self.parent = parent
  23. wx.ToolBar.__init__(self, parent = self.parent, id = wx.ID_ANY)
  24. self.action = dict()
  25. self.Bind(wx.EVT_TOOL, self.OnTool)
  26. self.SetToolBitmapSize(globalvar.toolbarSize)
  27. def InitToolbar(self, toolData):
  28. """!Initialize toolbar, add tools to the toolbar
  29. """
  30. for tool in toolData:
  31. self.CreateTool(*tool)
  32. self._data = toolData
  33. def _toolbarData(self):
  34. """!Toolbar data (virtual)"""
  35. return None
  36. def CreateTool(self, label, bitmap, kind,
  37. shortHelp, longHelp, handler, pos = -1):
  38. """!Add tool to the toolbar
  39. @param pos if -1 add tool, if > 0 insert at given pos
  40. @return id of tool
  41. """
  42. bmpDisabled = wx.NullBitmap
  43. tool = -1
  44. if label:
  45. tool = vars(self)[label] = wx.NewId()
  46. Debug.msg(3, "CreateTool(): tool=%d, label=%s bitmap=%s" % \
  47. (tool, label, bitmap))
  48. if pos < 0:
  49. toolWin = self.AddLabelTool(tool, label, bitmap,
  50. bmpDisabled, kind,
  51. shortHelp, longHelp)
  52. else:
  53. toolWin = self.InsertLabelTool(pos, tool, label, bitmap,
  54. bmpDisabled, kind,
  55. shortHelp, longHelp)
  56. self.Bind(wx.EVT_TOOL, handler, toolWin)
  57. else: # separator
  58. self.AddSeparator()
  59. return tool
  60. def EnableLongHelp(self, enable = True):
  61. """!Enable/disable long help
  62. @param enable True for enable otherwise disable
  63. """
  64. for tool in self._data:
  65. if tool[0] == '': # separator
  66. continue
  67. if enable:
  68. self.SetToolLongHelp(vars(self)[tool[0]], tool[4])
  69. else:
  70. self.SetToolLongHelp(vars(self)[tool[0]], "")
  71. def OnTool(self, event):
  72. """!Tool selected
  73. """
  74. if self.parent.GetName() == "GCPFrame":
  75. return
  76. if hasattr(self.parent, 'toolbars'):
  77. if self.parent.GetToolbar('vdigit'):
  78. # update vdigit toolbar (unselect currently selected tool)
  79. id = self.parent.toolbars['vdigit'].GetAction(type = 'id')
  80. self.parent.toolbars['vdigit'].ToggleTool(id, False)
  81. if event:
  82. # deselect previously selected tool
  83. id = self.action.get('id', -1)
  84. if id != event.GetId():
  85. self.ToggleTool(self.action['id'], False)
  86. else:
  87. self.ToggleTool(self.action['id'], True)
  88. self.action['id'] = event.GetId()
  89. event.Skip()
  90. else:
  91. # initialize toolbar
  92. self.ToggleTool(self.action['id'], True)
  93. def GetAction(self, type = 'desc'):
  94. """!Get current action info"""
  95. return self.action.get(type, '')
  96. def SelectDefault(self, event):
  97. """!Select default tool"""
  98. self.ToggleTool(self.defaultAction['id'], True)
  99. self.defaultAction['bind'](event)
  100. self.action = { 'id' : self.defaultAction['id'],
  101. 'desc' : self.defaultAction.get('desc', '') }
  102. def FixSize(self, width):
  103. """!Fix toolbar width on Windows
  104. @todo Determine why combobox causes problems here
  105. """
  106. if platform.system() == 'Windows':
  107. size = self.GetBestSize()
  108. self.SetSize((size[0] + width, size[1]))
  109. def Enable(self, tool, enable = True):
  110. """!Enable/Disable defined tool
  111. @param tool name
  112. @param enable True to enable otherwise disable tool
  113. """
  114. try:
  115. id = getattr(self, tool)
  116. except AttributeError:
  117. return
  118. self.EnableTool(id, enable)
  119. def EnableAll(self, enable = True):
  120. """!Enable/Disable all tools
  121. @param enable True to enable otherwise disable tool
  122. """
  123. for item in self._toolbarData():
  124. if not item[0]:
  125. continue
  126. self.Enable(item[0], enable)
  127. def _getToolbarData(self, data):
  128. """!Define tool
  129. """
  130. retData = list()
  131. for args in data:
  132. retData.append(self._defineTool(*args))
  133. return retData
  134. def _defineTool(self, name = None, icon = None, handler = None, item = wx.ITEM_NORMAL, pos = -1):
  135. """!Define tool
  136. """
  137. if name:
  138. return (name, icon.GetBitmap(),
  139. item, icon.GetLabel(), icon.GetDesc(),
  140. handler, pos)
  141. return ("", "", "", "", "", "") # separator