Skip to content

Commit

Permalink
Add fix for #648 with error message during import
Browse files Browse the repository at this point in the history
This fixes #648 by adding a command tracker queue. When the queue is not empty, closing the dialog is prevented to ensure the completion callback can still access UI elements.
  • Loading branch information
ww2406 committed Oct 13, 2024
1 parent 03675b0 commit 0c316b9
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions gui/wxpython/modules/import_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"""

import os
from collections import deque

from pathlib import Path

Expand Down Expand Up @@ -60,6 +61,8 @@ def __init__(

self.commandId = -1 # id of running command

self._commands_running = deque()

wx.Dialog.__init__(
self, parent, id, title, style=style, name="MultiImportDialog"
)
Expand Down Expand Up @@ -122,16 +125,17 @@ def __init__(
# buttons
#
# cancel
self._DEFAULT_CLOSE_BTN_TEXT = "Close dialog"
self.btn_close = CloseButton(parent=self.panel)
self.btn_close.SetToolTip(_("Close dialog"))
self.btn_close.SetToolTip(_(self._DEFAULT_CLOSE_BTN_TEXT))
self.btn_close.Bind(wx.EVT_BUTTON, self.OnClose)
# run
self.btn_run = Button(parent=self.panel, id=wx.ID_OK, label=_("&Import"))
self.btn_run.SetToolTip(_("Import selected layers"))
self.btn_run.SetDefault()
self.btn_run.Bind(wx.EVT_BUTTON, self.OnRun)

self.Bind(wx.EVT_CLOSE, lambda evt: self.Destroy())
self.Bind(wx.EVT_CLOSE, self._handleCloseEvent)

self.notebook = GNotebook(parent=self, style=globalvar.FNPageDStyle)

Expand Down Expand Up @@ -270,6 +274,30 @@ def _validateOutputMapName(self):
return False
return True

def _handleCloseEvent(self, *args, **kwargs):
if len(self._commands_running) > 0:
# prevent dialog close while async commands are running
return
self.Destroy()

def _addToCommandQueue(self):
self._commands_running.append(True)

# disable the dialog close button
self.btn_close.SetToolTip(_("Dialog cannot be closed while command is running."))
self.btn_close.Disable()

def _removeFromCommandQueue(self):
try:
self._commands_running.pop()
except IndexError:
pass
finally:
if len(self._commands_running) == 0:
# enable the dialog close button
self.btn_close.Enable()
self.btn_close.SetToolTip(self._DEFAULT_CLOSE_BTN_TEXT)

def OnClose(self, event=None):
"""Close dialog"""
self.Close()
Expand Down Expand Up @@ -519,6 +547,7 @@ def OnRun(self, event):
):
cmd.append("--overwrite")

self._addToCommandQueue()
# run in Layer Manager
self._giface.RunCmd(
cmd, onDone=self.OnCmdDone, userData=userData, addLayer=False
Expand All @@ -527,9 +556,11 @@ def OnRun(self, event):
def OnCmdDone(self, event):
"""Load layers and close if required"""
if not hasattr(self, "AddLayers"):
self._removeFromCommandQueue()
return

self.AddLayers(event.returncode, event.cmd, event.userData)
self._removeFromCommandQueue()

if event.returncode == 0 and self.closeOnFinish.IsChecked():
self.Close()
Expand Down Expand Up @@ -670,6 +701,7 @@ def OnRun(self, event):
):
cmd.append("--overwrite")

self._addToCommandQueue()
# run in Layer Manager
self._giface.RunCmd(
cmd, onDone=self.OnCmdDone, userData=userData, addLayer=False
Expand All @@ -678,10 +710,13 @@ def OnRun(self, event):
def OnCmdDone(self, event):
"""Load layers and close if required"""
if not hasattr(self, "AddLayers"):
self._removeFromCommandQueue()
return

self.AddLayers(event.returncode, event.cmd, event.userData)

self._removeFromCommandQueue()

if self.popOGR:
os.environ.pop("GRASS_VECTOR_OGR")

Expand Down Expand Up @@ -880,16 +915,20 @@ def OnRun(self, event):
):
cmd.append("--overwrite")

self._commands_running.append(True)
# run in Layer Manager
self._giface.RunCmd(cmd, onDone=self.OnCmdDone, addLayer=False)

def OnCmdDone(self, event):
"""Load layers and close if required"""
if not hasattr(self, "AddLayers"):
self._removeFromCommandQueue()
return

self.AddLayers(event.returncode, event.cmd)

self._removeFromCommandQueue()

if self.closeOnFinish.IsChecked():
self.Close()

Expand Down

0 comments on commit 0c316b9

Please sign in to comment.