48
53
label = _('Return to previous zoom')),
49
54
'zoomMenu' : MetaIcon(img = 'zoom-more',
50
55
label = _('Various zoom options'),
51
desc = _('Zoom to computational, default, saved region, ...')),
56
desc = _('Zoom to default or saved region, save to named region, ...')),
52
57
'zoomExtent' : MetaIcon(img = 'zoom-extent',
53
58
label = _('Zoom to selected map layer(s)')),
59
'zoomRegion' : MetaIcon(img = 'zoom-region',
60
label = _('Zoom to computational region extent')),
54
61
'pan' : MetaIcon(img = 'pan',
56
63
desc = _('Drag with mouse to pan')),
57
64
'saveFile' : MetaIcon(img = 'map-export',
58
label = _('Save display to graphic file')),
65
label = _('Save display to file')),
59
66
'print' : MetaIcon(img = 'print',
60
67
label = _('Print display')),
61
68
'font' : MetaIcon(img = 'font',
80
87
class BaseToolbar(wx.ToolBar):
81
"""!Abstract toolbar class"""
82
def __init__(self, parent):
88
"""Abstract toolbar class.
90
Following code shows how to create new basic toolbar:
93
class MyToolbar(BaseToolbar):
94
def __init__(self, parent):
95
BaseToolbar.__init__(self, parent)
96
self.InitToolbar(self._toolbarData())
99
def _toolbarData(self):
100
return self._getToolbarData((("help", Icons["help"],
105
def __init__(self, parent, toolSwitcher=None, style=wx.NO_BORDER|wx.TB_HORIZONTAL):
83
106
self.parent = parent
84
wx.ToolBar.__init__(self, parent = self.parent, id = wx.ID_ANY)
88
self.Bind(wx.EVT_TOOL, self.OnTool)
107
wx.ToolBar.__init__(self, parent=self.parent, id=wx.ID_ANY,
90
112
self.SetToolBitmapSize(globalvar.toolbarSize)
114
self.toolSwitcher = toolSwitcher
92
117
def InitToolbar(self, toolData):
93
"""!Initialize toolbar, add tools to the toolbar
118
"""Initialize toolbar, add tools to the toolbar
95
120
for tool in toolData:
96
121
self.CreateTool(*tool)
98
123
self._data = toolData
100
125
def _toolbarData(self):
101
"""!Toolbar data (virtual)"""
126
"""Toolbar data (virtual)"""
104
129
def CreateTool(self, label, bitmap, kind,
105
130
shortHelp, longHelp, handler, pos = -1):
106
"""!Add tool to the toolbar
131
"""Add tool to the toolbar
108
@param pos if -1 add tool, if > 0 insert at given pos
133
:param pos: if -1 add tool, if > 0 insert at given pos
111
136
bmpDisabled = wx.NullBitmap
114
139
tool = vars(self)[label] = wx.NewId()
115
140
Debug.msg(3, "CreateTool(): tool=%d, label=%s bitmap=%s" % \
116
141
(tool, label, bitmap))
122
147
toolWin = self.InsertLabelTool(pos, tool, label, bitmap,
123
148
bmpDisabled, kind,
124
149
shortHelp, longHelp)
150
self.handlers[tool] = handler
125
151
self.Bind(wx.EVT_TOOL, handler, toolWin)
152
self.Bind(wx.EVT_TOOL, self.OnTool, toolWin)
126
153
else: # separator
127
154
self.AddSeparator()
131
158
def EnableLongHelp(self, enable = True):
132
"""!Enable/disable long help
159
"""Enable/disable long help
134
@param enable True for enable otherwise disable
161
:param enable: True for enable otherwise disable
136
163
for tool in self._data:
137
164
if tool[0] == '': # separator
141
168
self.SetToolLongHelp(vars(self)[tool[0]], tool[4])
143
170
self.SetToolLongHelp(vars(self)[tool[0]], "")
145
172
def OnTool(self, event):
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)
156
# deselect previously selected tool
157
id = self.action.get('id', -1)
158
if id != event.GetId():
159
self.ToggleTool(self.action['id'], False)
161
self.ToggleTool(self.action['id'], True)
163
self.action['id'] = event.GetId()
168
self.ToggleTool(self.action['id'], True)
170
def GetAction(self, type = 'desc'):
171
"""!Get current action info"""
172
return self.action.get(type, '')
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', '') }
175
if self.toolSwitcher:
176
Debug.msg(3, "BaseToolbar.OnTool(): id = %s" % event.GetId())
177
self.toolSwitcher.ToolChanged(event.GetId())
180
def SelectTool(self, id):
181
self.ToggleTool(id, True)
182
self.toolSwitcher.ToolChanged(id)
184
self.handlers[id](event=None)
186
def SelectDefault(self):
187
"""Select default tool"""
188
self.SelectTool(self._default)
181
190
def FixSize(self, width):
182
"""!Fix toolbar width on Windows
191
"""Fix toolbar width on Windows
184
@todo Determine why combobox causes problems here
194
Determine why combobox causes problems here
186
196
if platform.system() == 'Windows':
187
197
size = self.GetBestSize()
188
198
self.SetSize((size[0] + width, size[1]))
190
200
def Enable(self, tool, enable = True):
191
"""!Enable defined tool
201
"""Enable/Disable defined tool
194
@param enable True to enable otherwise disable tool
204
:param enable: True to enable otherwise disable tool
197
207
id = getattr(self, tool)
198
208
except AttributeError:
209
# TODO: test everything that this is not raised
210
# this error was ignored for a long time
211
raise AttributeError("Toolbar does not have a tool %s." % tool)
201
214
self.EnableTool(id, enable)
216
def EnableAll(self, enable = True):
217
"""Enable/Disable all tools
219
:param enable: True to enable otherwise disable tool
221
for item in self._toolbarData():
224
self.Enable(item[0], enable)
203
226
def _getToolbarData(self, data):
207
230
for args in data:
230
253
self.PopupMenu(menu)
256
def CreateSelectionButton(self, tooltip = _("Select graphics tool")):
257
"""Add button to toolbar for selection of graphics drawing mode.
259
Button must be custom (not toolbar tool) to set smaller width.
261
arrowPath = os.path.join(IMGDIR, 'small_down_arrow.png')
262
if os.path.isfile(arrowPath) and os.path.getsize(arrowPath):
263
bitmap = wx.Bitmap(name = arrowPath)
265
bitmap = wx.ArtProvider.GetBitmap(id = wx.ART_MISSING_IMAGE, client = wx.ART_TOOLBAR)
266
button = wx.BitmapButton(parent=self, id=wx.ID_ANY, size=((-1, self.GetToolSize()[1])),
267
bitmap=bitmap, style=wx.NO_BORDER)
268
button.SetToolTipString(tooltip)
273
"""Class handling switching tools in toolbar and custom toggle buttons."""
275
self._groups = defaultdict(lambda: defaultdict(list))
276
self._toolsGroups = defaultdict(list)
278
# emitted when tool is changed
279
self.toggleToolChanged = Signal('ToolSwitcher.toggleToolChanged')
281
def AddToolToGroup(self, group, toolbar, tool):
282
"""Adds tool from toolbar to group of exclusive tools.
284
:param group: name of group (e.g. 'mouseUse')
285
:param toolbar: instance of toolbar
286
:param tool: id of a tool from the toolbar
288
self._groups[group][toolbar].append(tool)
289
self._toolsGroups[tool].append(group)
291
def AddCustomToolToGroup(self, group, btnId, toggleHandler):
292
"""Adds custom tool from to group of exclusive tools (some toggle button).
294
:param group: name of group (e.g. 'mouseUse')
295
:param btnId: id of a tool (typically button)
296
:param toggleHandler: handler to be called to switch the button
298
self._groups[group]['custom'].append((btnId, toggleHandler))
299
self._toolsGroups[btnId].append(group)
301
def RemoveCustomToolFromGroup(self, tool):
302
"""Removes custom tool from group.
304
:param tool: id of the button
306
if not tool in self._toolsGroups:
308
for group in self._toolsGroups[tool]:
309
self._groups[group]['custom'] = \
310
[(bid, hdlr) for (bid, hdlr)
311
in self._groups[group]['custom'] if bid != tool]
313
def RemoveToolbarFromGroup(self, group, toolbar):
314
"""Removes toolbar from group.
316
Before toolbar is destroyed, it must be removed from group, too.
317
Otherwise we can expect some DeadObject errors.
319
:param group: name of group (e.g. 'mouseUse')
320
:param toolbar: instance of toolbar
322
for tb in self._groups[group]:
324
del self._groups[group][tb]
327
def IsToolInGroup(self, tool, group):
328
"""Checks whether a tool is in a specified group.
331
:param group: name of group (e.g. 'mouseUse')
333
for group in self._toolsGroups[tool]:
334
for tb in self._groups[group]:
335
if tb.FindById(tool):
339
def ToolChanged(self, tool):
340
"""When any tool/button is pressed, other tools from group must be unchecked.
342
:param tool: id of a tool/button
344
for group in self._toolsGroups[tool]:
345
for tb in self._groups[group]:
347
for btnId, handler in self._groups[group][tb]:
351
for tl in self._groups[group][tb]:
352
if tb.FindById(tl): # check if still exists
354
tb.ToggleTool(tl, False)
356
tb.ToggleTool(tool, True)
358
self.toggleToolChanged.emit(id=tool)