Skip to content

Commit 845daff

Browse files
author
lindakladivova
committed
adding pop-up menu with an item for delete command from history + refactoring
1 parent c670734 commit 845daff

File tree

8 files changed

+287
-146
lines changed

8 files changed

+287
-146
lines changed

gui/wxpython/core/gconsole.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040

4141
from grass.pydispatch.signal import Signal
4242

43+
from grass.grassdb.history import (
44+
get_current_mapset_gui_history_path,
45+
add_entry_to_history,
46+
)
47+
4348
from core import globalvar
4449
from core.gcmd import CommandThread, GError, GException
4550
from gui_core.forms import GUI
@@ -495,8 +500,16 @@ def RunCmd(
495500
Debug.msg(2, "GPrompt:RunCmd(): empty command")
496501
return
497502

498-
# update history file, command prompt history and history model
499-
self._giface.updateHistory.emit(cmd=cmd_save_to_history)
503+
# add entry to command history log
504+
history_path = get_current_mapset_gui_history_path()
505+
try:
506+
add_entry_to_history(cmd_save_to_history, history_path)
507+
except OSError as e:
508+
GError(str(e))
509+
510+
# update command prompt and history model
511+
if self._giface:
512+
self._giface.addEntryToHistory.emit(cmd=cmd_save_to_history)
500513

501514
if command[0] in globalvar.grassCmd:
502515
# send GRASS command without arguments to GUI command interface

gui/wxpython/core/giface.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,13 @@ def __init__(self):
232232
# Signal emitted when workspace is changed
233233
self.workspaceChanged = Signal("StandaloneGrassInterface.workspaceChanged")
234234

235-
# Signal emitted when history should be updated
236-
self.updateHistory = Signal("StandaloneGrassInterface.updateHistory")
235+
# Signal emitted when entry to history is added
236+
self.addEntryToHistory = Signal("StandaloneGrassInterface.addEntryToHistory")
237+
238+
# Signal emitted when entry from history is removed
239+
self.removeEntryFromHistory = Signal(
240+
"StandaloneGrassInterface.removeEntryFromHistory"
241+
)
237242

238243
# workaround, standalone grass interface should be moved to sep. file
239244
from core.gconsole import GConsole, EVT_CMD_OUTPUT, EVT_CMD_PROGRESS

gui/wxpython/gui_core/goutput.py

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from grass.grassdb.history import (
3030
read_history,
3131
create_history_file,
32-
update_history,
3332
copy_history,
3433
get_current_mapset_gui_history_path,
3534
)
@@ -149,12 +148,14 @@ def __init__(
149148
if self.giface:
150149
self.giface.currentMapsetChanged.connect(self._loadHistory)
151150

152-
if self._gcstyle == GC_PROMPT:
153-
# connect update history signal only for main Console Window
154-
self.giface.updateHistory.connect(
155-
lambda cmd: self.cmdPrompt.UpdateCmdHistory(cmd)
156-
)
157-
self.giface.updateHistory.connect(lambda cmd: self.UpdateHistory(cmd))
151+
if self._gcstyle == GC_PROMPT:
152+
# connect update history signals only for main Console Window
153+
self.giface.addEntryToHistory.connect(
154+
lambda cmd: self.cmdPrompt.AddEntryToCmdHistory(cmd)
155+
)
156+
self.giface.removeEntryFromHistory.connect(
157+
lambda index: self.cmdPrompt.RemoveEntryFromCmdHistory(index)
158+
)
158159

159160
# buttons
160161
self.btnClear = ClearButton(parent=self.panelPrompt)
@@ -448,14 +449,6 @@ def OnCmdProgress(self, event):
448449
self.progressbar.SetValue(event.value)
449450
event.Skip()
450451

451-
def UpdateHistory(self, cmd):
452-
"""Update command history"""
453-
history_path = get_current_mapset_gui_history_path()
454-
try:
455-
update_history(cmd, history_path)
456-
except OSError as e:
457-
GError(str(e))
458-
459452
def OnCmdExportHistory(self, event):
460453
"""Export the history of executed commands stored
461454
in a .wxgui_history file to a selected file."""

gui/wxpython/gui_core/prompt.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ def SetTextAndFocus(self, text):
304304
self.SetCurrentPos(pos)
305305
self.SetFocus()
306306

307-
def UpdateCmdHistory(self, cmd):
308-
"""Update command history
307+
def AddEntryToCmdHistory(self, cmd):
308+
"""Add entry to command history buffer
309309
310310
:param cmd: command given as a string
311311
"""
@@ -319,6 +319,17 @@ def UpdateCmdHistory(self, cmd):
319319
del self.cmdbuffer[0]
320320
self.cmdindex = len(self.cmdbuffer)
321321

322+
def RemoveEntryFromCmdHistory(self, index):
323+
"""Remove entry from command history buffer
324+
:param index: index of deleted command
325+
"""
326+
# remove command at the given index from history buffer
327+
if index < len(self.cmdbuffer):
328+
self.cmdbuffer.pop(index)
329+
330+
# update cmd index size
331+
self.cmdindex = len(self.cmdbuffer)
332+
322333
def EntityToComplete(self):
323334
"""Determines which part of command (flags, parameters) should
324335
be completed at current cursor position"""

gui/wxpython/history/browser.py

Lines changed: 17 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,20 @@
1313
for details.
1414
1515
@author Linda Karlovska (Kladivova) [email protected]
16+
@author Anna Petrasova (kratochanna gmail com)
17+
@author Tomas Zigo
1618
"""
1719

1820
import wx
19-
import re
20-
21-
from core import globalvar
22-
from core.gcmd import GError, GException
23-
from core.utils import (
24-
parse_mapcalc_cmd,
25-
replace_module_cmd_special_flags,
26-
split,
27-
)
28-
from gui_core.forms import GUI
29-
from gui_core.treeview import CTreeView
21+
3022
from gui_core.wrap import SearchCtrl
3123
from history.tree import HistoryBrowserTree
3224

3325
from grass.pydispatch.signal import Signal
3426

3527

3628
class HistoryBrowser(wx.Panel):
37-
"""History browser for executing the commands from history log.
29+
"""History browser panel for executing the commands from history log.
3830
3931
Signal:
4032
showNotification - attribute 'message'
@@ -52,22 +44,24 @@ def __init__(
5244
self.parent = parent
5345
self._giface = giface
5446

55-
self.showNotification = Signal("HistoryBrowser.showNotification")
56-
self.runIgnoredCmdPattern = Signal("HistoryBrowser.runIgnoredCmdPattern")
5747
wx.Panel.__init__(self, parent=parent, id=id, **kwargs)
48+
self.SetName("HistoryBrowser")
5849

59-
self._createTree()
60-
61-
self._giface.currentMapsetChanged.connect(self.UpdateHistoryModelFromScratch)
62-
self._giface.updateHistory.connect(
63-
lambda cmd: self.UpdateHistoryModelByCommand(cmd)
64-
)
50+
self.showNotification = Signal("HistoryBrowser.showNotification")
51+
self.runIgnoredCmdPattern = Signal("HistoryBrowser.runIgnoredCmdPattern")
6552

53+
# search box
6654
self.search = SearchCtrl(self)
6755
self.search.SetDescriptiveText(_("Search"))
6856
self.search.ShowCancelButton(True)
69-
self.search.Bind(wx.EVT_TEXT, lambda evt: self.Filter(evt.GetString()))
70-
self.search.Bind(wx.EVT_SEARCHCTRL_CANCEL_BTN, lambda evt: self.Filter(""))
57+
self.search.Bind(wx.EVT_TEXT, lambda evt: self.tree.Filter(evt.GetString()))
58+
self.search.Bind(wx.EVT_SEARCHCTRL_CANCEL_BTN, lambda evt: self.tree.Filter(""))
59+
60+
# tree with layers
61+
self.tree = HistoryBrowserTree(self, giface=giface)
62+
self.tree.SetToolTip(_("Double-click to run selected tool"))
63+
self.tree.showNotification.connect(self.showNotification)
64+
self.tree.runIgnoredCmdPattern.connect(self.runIgnoredCmdPattern)
7165

7266
self._layout()
7367

@@ -81,83 +75,9 @@ def _layout(self):
8175
border=5,
8276
)
8377
sizer.Add(
84-
self._tree, proportion=1, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=5
78+
self.tree, proportion=1, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=5
8579
)
8680

8781
self.SetSizerAndFit(sizer)
8882
self.SetAutoLayout(True)
8983
self.Layout()
90-
91-
def _createTree(self):
92-
"""Create tree based on the model"""
93-
self._model = HistoryBrowserTree()
94-
self._tree = self._getTreeInstance()
95-
self._tree.SetToolTip(_("Double-click to open the tool"))
96-
self._tree.selectionChanged.connect(self.OnItemSelected)
97-
self._tree.itemActivated.connect(lambda node: self.Run(node))
98-
99-
def _getTreeInstance(self):
100-
return CTreeView(model=self._model.GetModel(), parent=self)
101-
102-
def _getSelectedNode(self):
103-
selection = self._tree.GetSelected()
104-
if not selection:
105-
return None
106-
return selection[0]
107-
108-
def _refreshTree(self):
109-
self._tree.SetModel(self._model.GetModel())
110-
111-
def Filter(self, text):
112-
"""Filter history
113-
114-
:param str text: text string
115-
"""
116-
model = self._model.GetModel()
117-
if text:
118-
model = self._model.model.Filtered(key=["command"], value=text)
119-
self._tree.SetModel(model)
120-
else:
121-
self._tree.SetModel(model)
122-
123-
def UpdateHistoryModelFromScratch(self):
124-
"""Update the model from scratch and refresh the tree"""
125-
self._model.CreateModel()
126-
self._refreshTree()
127-
128-
def UpdateHistoryModelByCommand(self, cmd):
129-
"""Update the model by the command and refresh the tree"""
130-
self._model.UpdateModel(cmd)
131-
self._refreshTree()
132-
133-
def OnItemSelected(self, node):
134-
"""Item selected"""
135-
command = node.data["command"]
136-
self.showNotification.emit(message=command)
137-
138-
def Run(self, node=None):
139-
"""Parse selected history command into list and launch module dialog."""
140-
node = node or self._getSelectedNode()
141-
if node:
142-
command = node.data["command"]
143-
if (
144-
globalvar.ignoredCmdPattern
145-
and re.compile(globalvar.ignoredCmdPattern).search(command)
146-
and "--help" not in command
147-
and "--ui" not in command
148-
):
149-
self.runIgnoredCmdPattern.emit(cmd=split(command))
150-
return
151-
if re.compile(r"^r[3]?\.mapcalc").search(command):
152-
command = parse_mapcalc_cmd(command)
153-
command = replace_module_cmd_special_flags(command)
154-
lst = split(command)
155-
try:
156-
GUI(parent=self, giface=self._giface).ParseCommand(lst)
157-
except GException as e:
158-
GError(
159-
parent=self,
160-
message=str(e),
161-
caption=_("Cannot be parsed into command"),
162-
showTraceback=False,
163-
)

0 commit comments

Comments
 (0)