File: toolbars.py

package info (click to toggle)
grass 6.4.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 104,028 kB
  • ctags: 40,409
  • sloc: ansic: 419,980; python: 63,559; tcl: 46,692; cpp: 29,791; sh: 18,564; makefile: 7,000; xml: 3,505; yacc: 561; perl: 559; lex: 480; sed: 70; objc: 7
file content (231 lines) | stat: -rw-r--r-- 8,398 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
"""!
@package gui_core.toolbars

@brief Base classes toolbar widgets

Classes:
 - toolbars::BaseToolbar

(C) 2007-2011 by the GRASS Development Team

This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.

@author Michael Barton
@author Jachym Cepicky
@author Martin Landa <landa.martin gmail.com>
"""

import os
import sys
import platform

import wx

from core               import globalvar
from core.debug         import Debug
from icons.icon         import MetaIcon

BaseIcons = {
    'display'    : MetaIcon(img = 'show',
                            label = _('Display map'),
                            desc  =  _('Re-render modified map layers only')),
    'render'     : MetaIcon(img = 'layer-redraw',
                            label = _('Render map'),
                            desc = _('Force re-rendering all map layers')),
    'erase'      : MetaIcon(img = 'erase',
                            label = _('Erase display'),
                            desc = _('Erase display canvas with given background color')),
    'pointer'    : MetaIcon(img = 'pointer',
                            label = _('Pointer')),
    'zoomIn'     : MetaIcon(img = 'zoom-in',
                            label = _('Zoom in'),
                            desc = _('Drag or click mouse to zoom')),
    'zoomOut'    : MetaIcon(img = 'zoom-out',
                            label = _('Zoom out'),
                            desc = _('Drag or click mouse to unzoom')),
    'zoomBack'   : MetaIcon(img = 'zoom-last',
                            label = _('Return to previous zoom')),
    'zoomMenu'   : MetaIcon(img = 'zoom-more',
                            label = _('Various zoom options'),
                            desc = _('Zoom to computational, default, saved region, ...')),
    'zoomExtent' : MetaIcon(img = 'zoom-extent',
                            label = _('Zoom to selected map layer(s)')),
    'pan'        : MetaIcon(img = 'pan',
                            label = _('Pan'),
                            desc = _('Drag with mouse to pan')),
    'saveFile'   : MetaIcon(img = 'map-export',
                            label = _('Save display to graphic file')),
    'print'      : MetaIcon(img = 'print',
                            label = _('Print display')),
    'font'       : MetaIcon(img = 'font',
                            label = _('Select font')),
    'help'       : MetaIcon(img = 'help',
                            label = _('Show manual')),
    'quit'       : MetaIcon(img = 'quit',
                            label = _('Quit')),
    'addRast'    : MetaIcon(img = 'layer-raster-add',
                            label = _('Add raster map layer')),
    'addVect'    : MetaIcon(img = 'layer-vector-add',
                            label = _('Add vector map layer')),
    'overlay'    : MetaIcon(img = 'overlay-add',
                            label = _('Add map elements'),
                            desc = _('Overlay elements like scale and legend onto map')),
    'histogramD' : MetaIcon(img = 'layer-raster-histogram',
                            label = _('Create histogram with d.histogram')),
    'settings'   : MetaIcon(img = 'settings',
                            label = _("Settings")),
    }
    
class BaseToolbar(wx.ToolBar):
    """!Abstract toolbar class"""
    def __init__(self, parent):
        self.parent = parent
        wx.ToolBar.__init__(self, parent = self.parent, id = wx.ID_ANY)
        
        self.action = dict()
        
        self.Bind(wx.EVT_TOOL, self.OnTool)
        
        self.SetToolBitmapSize(globalvar.toolbarSize)
        
    def InitToolbar(self, toolData):
        """!Initialize toolbar, add tools to the toolbar
        """
        for tool in toolData:
            self.CreateTool(*tool)
        
        self._data = toolData
        
    def _toolbarData(self):
        """!Toolbar data (virtual)"""
        return None
    
    def CreateTool(self, label, bitmap, kind,
                   shortHelp, longHelp, handler, pos = -1):
        """!Add tool to the toolbar
        
        @param pos if -1 add tool, if > 0 insert at given pos
        @return id of tool
        """
        bmpDisabled = wx.NullBitmap
        tool = -1
        if label: 
            tool = vars(self)[label] = wx.NewId()
            Debug.msg(3, "CreateTool(): tool=%d, label=%s bitmap=%s" % \
                          (tool, label, bitmap))
            if pos < 0:
                toolWin = self.AddLabelTool(tool, label, bitmap,
                                            bmpDisabled, kind,
                                            shortHelp, longHelp)
            else:
                toolWin = self.InsertLabelTool(pos, tool, label, bitmap,
                                            bmpDisabled, kind,
                                            shortHelp, longHelp)
            self.Bind(wx.EVT_TOOL, handler, toolWin)
        else: # separator
            self.AddSeparator()
        
        return tool
    
    def EnableLongHelp(self, enable = True):
        """!Enable/disable long help
        
        @param enable True for enable otherwise disable
        """
        for tool in self._data:
            if tool[0] == '': # separator
                continue
            
            if enable:
                self.SetToolLongHelp(vars(self)[tool[0]], tool[4])
            else:
                self.SetToolLongHelp(vars(self)[tool[0]], "")
        
    def OnTool(self, event):
        """!Tool selected
        """
        
        if hasattr(self.parent, 'toolbars'):
            if self.parent.GetToolbar('vdigit'):
                # update vdigit toolbar (unselect currently selected tool)
                id = self.parent.toolbars['vdigit'].GetAction(type = 'id')
                self.parent.toolbars['vdigit'].ToggleTool(id, False)
        
        if event:
            # deselect previously selected tool
            id = self.action.get('id', -1)
            if id != event.GetId():
                self.ToggleTool(self.action['id'], False)
            else:
                self.ToggleTool(self.action['id'], True)
            
            self.action['id'] = event.GetId()
            
            event.Skip()
        else:
            # initialize toolbar
            self.ToggleTool(self.action['id'], True)
        
    def GetAction(self, type = 'desc'):
        """!Get current action info"""
        return self.action.get(type, '')
    
    def SelectDefault(self, event):
        """!Select default tool"""
        self.ToggleTool(self.defaultAction['id'], True)
        self.defaultAction['bind'](event)
        self.action = { 'id' : self.defaultAction['id'],
                        'desc' : self.defaultAction.get('desc', '') }
        
    def FixSize(self, width):
        """!Fix toolbar width on Windows
            
        @todo Determine why combobox causes problems here
        """
        if platform.system() == 'Windows':
            size = self.GetBestSize()
            self.SetSize((size[0] + width, size[1]))

    def Enable(self, tool, enable = True):
        """!Enable defined tool
        
        @param tool name
        @param enable True to enable otherwise disable tool
        """
        try:
            id = getattr(self, tool)
        except AttributeError:
            return
        
        self.EnableTool(id, enable)
        
    def _getToolbarData(self, data):
        """!Define tool
        """
        retData = list()
        for args in data:
            retData.append(self._defineTool(*args))
        return retData

    def _defineTool(self, name = None, icon = None, handler = None, item = wx.ITEM_NORMAL, pos = -1):
        """!Define tool
        """
        if name:
            return (name, icon.GetBitmap(),
                    item, icon.GetLabel(), icon.GetDesc(),
                    handler, pos)
        return ("", "", "", "", "", "") # separator

    def _onMenu(self, data):
        """!Toolbar pop-up menu"""
        menu = wx.Menu()
        
        for icon, handler in data:
            item = wx.MenuItem(menu, wx.ID_ANY, icon.GetLabel())
            item.SetBitmap(icon.GetBitmap(self.parent.iconsize))
            menu.AppendItem(item)
            self.Bind(wx.EVT_MENU, handler, item)
        
        self.PopupMenu(menu)
        menu.Destroy()