4
@brief GRASS interface for standalone application (without layer manager)
7
- giface::StandaloneGrassInterface
9
(C) 2012-2014 by the GRASS Development Team
11
This program is free software under the GNU General Public License
12
(>=v2). Read the file COPYING that comes with GRASS for details.
14
@author Anna Kratochvilova <kratochanna gmail.com>
15
@author Vaclav Petras <wenzeslaus gmail.com>
19
from core.utils import _
21
import grass.script as grass
23
from grass.pydispatch.signal import Signal
25
# to disable Abstract class not referenced
26
#pylint: disable=R0921
30
"""Enum class for notifications suggestions.
32
Can be used for log messages, commands, warnings, errors.
33
The value is the suggestion how user should be notified
34
about the new message.
43
"""Layer is generaly usable layer object.
46
Currently without specifying the interface.
47
Current implementations only provides all attributes of existing
48
layer as used in lmgr.
53
class LayerList(object):
54
def GetSelectedLayers(self, checkedOnly=True):
55
"""Returns list of selected layers.
58
Usage of checked and selected is still subject to change.
59
Checked layers should be showed. Selected are for analyses.
60
However, this may be the same for some implementations
61
(e.g. it d.mon has all layers checked and selected).
63
raise NotImplementedError()
65
def GetSelectedLayer(self, checkedOnly=False):
66
"""Returns selected layer or None when there is no selected layer.
69
Parameter checkedOnly is here False by default. This might
70
change if we find the right way of handling unchecked layers.
72
raise NotImplementedError()
74
def AddLayer(self, ltype, name=None, checked=None,
75
opacity=1.0, cmd=None):
76
"""Adds a new layer to the layer list.
78
Launches property dialog if needed (raster, vector, etc.)
80
:param ltype: layer type (raster, vector, 3d-raster, ...)
81
:param name: layer name
82
:param checked: if True layer is checked
83
:param opacity: layer opacity level
84
:param cmd: command (given as a list)
86
raise NotImplementedError()
88
def GetLayersByName(self, name):
89
"""Returns list of layers with a given name.
91
:param name: fully qualified map name
94
if common usage is just to check the presence of layer,
95
intoroduce a new method ContainsLayerByName(name)
97
raise NotImplementedError()
99
def GetLayerByData(self, key, value):
100
"""Returns layer with specified.
103
Returns only one layer. This might change.
106
Avoid using this method, it might be removed in the future.
108
raise NotImplementedError()
111
class GrassInterface:
112
"""GrassInterface provides the functionality which should be available
113
to every GUI component.
117
The GrassInterface process is not finished.
119
def RunCmd(self, *args, **kwargs):
120
"""Executes a command.
122
raise NotImplementedError()
124
def Help(self, entry):
125
"""Shows a manual page for a given entry.
127
raise NotImplementedError()
129
def WriteLog(self, text, wrap=None, notification=Notification.HIGHLIGHT):
130
"""Writes log message.
132
raise NotImplementedError()
134
def WriteCmdLog(self, text, pid=None, notification=Notification.MAKE_VISIBLE):
135
"""Writes message related to start or end of the command.
137
raise NotImplementedError()
139
def WriteWarning(self, text):
140
"""Writes warning message for the user.
142
raise NotImplementedError()
144
def WriteError(self, text):
145
"""Writes error message for the user."""
146
raise NotImplementedError()
148
def GetLayerTree(self):
149
"""Returns LayerManager's tree GUI object.
152
Will be removed from the interface.
154
raise NotImplementedError()
156
def GetLayerList(self):
157
"""Returns a layer management object.
159
raise NotImplementedError()
161
def GetMapDisplay(self):
162
"""Returns current map display.
166
For layer related tasks use GetLayerList().
168
:return: MapFrame instance
169
:return: None when no mapdisplay open
171
raise NotImplementedError()
173
def GetAllMapDisplays(self):
174
"""Get list of all map displays.
178
Might be removed from the interface.
180
:return: list of MapFrame instances
182
raise NotImplementedError()
184
def GetMapWindow(self):
185
"""Returns current map window.
189
For layer related tasks use GetLayerList().
191
raise NotImplementedError()
193
def GetProgress(self):
194
"""Returns object which shows the progress.
198
Some implementations may not implement this method.
200
raise NotImplementedError()
203
class StandaloneGrassInterface():
204
"""@implements GrassInterface"""
207
# Signal when some map is created or updated by a module.
208
# attributes: name: map name, ltype: map type,
209
# add: if map should be added to layer tree (questionable attribute)
210
self.mapCreated = Signal('StandaloneGrassInterface.mapCreated')
212
# Signal emitted to request updating of map
213
self.updateMap = Signal('StandaloneGrassInterface.updateMap')
215
# workaround, standalone grass interface should be moved to sep. file
216
from core.gconsole import GConsole, \
217
EVT_CMD_OUTPUT, EVT_CMD_PROGRESS
219
self._gconsole = GConsole()
220
self._gconsole.Bind(EVT_CMD_PROGRESS, self._onCmdProgress)
221
self._gconsole.Bind(EVT_CMD_OUTPUT, self._onCmdOutput)
222
self._gconsole.writeLog.connect(self.WriteLog)
223
self._gconsole.writeCmdLog.connect(self.WriteCmdLog)
224
self._gconsole.writeWarning.connect(self.WriteWarning)
225
self._gconsole.writeError.connect(self.WriteError)
227
def _onCmdOutput(self, event):
228
"""Print command output"""
232
if style == 'warning':
233
self.WriteWarning(message)
234
elif style == 'error':
235
self.WriteError(message)
237
self.WriteLog(message)
240
def _onCmdProgress(self, event):
241
"""Update progress message info"""
242
grass.percent(event.value, 100, 1)
245
def RunCmd(self, command, compReg=True, skipInterface=False,
246
onDone=None, onPrepare=None, userData=None, notification=Notification.MAKE_VISIBLE):
247
self._gconsole.RunCmd(command=command, compReg=compReg,
248
skipInterface=skipInterface, onDone=onDone,
249
onPrepare=onPrepare, userData=userData, notification=notification)
251
def Help(self, entry):
252
self._gconsole.RunCmd(['g.manual', 'entry=%s' % entry])
254
def WriteLog(self, text, wrap=None,
255
notification=Notification.HIGHLIGHT):
256
self._write(grass.message, text)
258
def WriteCmdLog(self, text, pid=None, notification=Notification.MAKE_VISIBLE):
260
text = '(' + str(pid) + ') ' + text
261
self._write(grass.message, text)
263
def WriteWarning(self, text):
264
self._write(grass.warning, text)
266
def WriteError(self, text):
267
self._write(grass.error, text)
269
def _write(self, function, text):
270
orig = os.getenv("GRASS_MESSAGE_FORMAT")
271
os.environ["GRASS_MESSAGE_FORMAT"] = 'standard'
273
os.environ["GRASS_MESSAGE_FORMAT"] = orig
275
def GetLayerList(self):
276
raise NotImplementedError()
278
def GetLayerTree(self):
281
def GetMapDisplay(self):
282
"""Get current map display.
286
def GetAllMapDisplays(self):
287
"""Get list of all map displays.
291
def GetMapWindow(self):
292
raise NotImplementedError()
294
def GetProgress(self):
295
# TODO: implement some progress with same inface as gui one
296
# (probably using g.message or similarly to Write... functions)
297
raise NotImplementedError()
299
def UpdateCmdHistory(self, cmd):
300
raise NotImplementedError()