Skip to content

Commit

Permalink
wxGUI/startup: fix terminate location download dialog download thread (
Browse files Browse the repository at this point in the history
…#832) (#873)

* wxGUI/startup: don't set message wx widget string if location download dialog is destroyed
* wxGUI/core: replace set/reset stop flag for terminate thread with traces (urlretrieve runned from thread can't terminate with stop flag)
* wxGUI/startup: avoid printing annoying wx debug message on location download dialog start-up
* wxGUI/startup: change 'Download' button label to 'Abort' after start downloading, and allow terminate download thread correctly
* wxGUI/startup: handle location download dialog urlretrieve HTTPError, URLError exception
* wxGUI/startup: add string translation function
* wxGUI/startup: set location download dialog download button mnemonic label
* wxGUI/startup: fix close location download modal dialog
* wxGUI/startup: fix location download dialog 'Cancel' and 'Download' button layout
* wxGUI/startup: centre location download modal dialog
  • Loading branch information
tmszi committed Aug 4, 2020
1 parent 618acaf commit c857cf0
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 45 deletions.
48 changes: 46 additions & 2 deletions gui/wxpython/core/gthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import time

import wx
from wx.lib.newevent import NewEvent

import sys
if sys.version_info.major == 2:
Expand All @@ -27,14 +28,21 @@

from core.gconsole import EVT_CMD_DONE, wxCmdDone

wxThdTerminate, EVT_THD_TERMINATE = NewEvent()


class gThread(threading.Thread, wx.EvtHandler):
"""Thread for various backends"""
"""Thread for various backends
terminating thread:
https://www.geeksforgeeks.org/python-different-ways-to-kill-a-thread/
"""
requestId = 0

def __init__(self, requestQ=None, resultQ=None, **kwds):
wx.EvtHandler.__init__(self)
self.terminate = False
self._terminate_evt = None

threading.Thread.__init__(self, **kwds)

Expand All @@ -51,6 +59,7 @@ def __init__(self, requestQ=None, resultQ=None, **kwds):
self.setDaemon(True)

self.Bind(EVT_CMD_DONE, self.OnDone)
self.Bind(EVT_THD_TERMINATE, self.OnTerminate)
self.start()

def Run(self, *args, **kwds):
Expand Down Expand Up @@ -80,7 +89,7 @@ def SetId(self, id):
def run(self):
while True:
requestId, args, kwds = self.requestQ.get()
for key in ('callable', 'ondone', 'userdata'):
for key in ('callable', 'ondone', 'userdata', 'onterminate'):
if key in kwds:
vars()[key] = kwds[key]
del kwds[key]
Expand All @@ -93,6 +102,13 @@ def run(self):
exception = None
time.sleep(.01)

self._terminate_evt = wxThdTerminate(
onterminate=vars()['onterminate'],
kwds=kwds,
args=args,
pid=requestId,
)

if self.terminate:
return

Expand Down Expand Up @@ -123,3 +139,31 @@ def OnDone(self, event):
def Terminate(self, terminate=True):
"""Abort command(s)"""
self.terminate = terminate

def start(self):
self.__run_backup = self.run
self.run = self.__run
threading.Thread.start(self)

def __run(self):
sys.settrace(self.globaltrace)
self.__run_backup()
self.run = self.__run_backup

def globaltrace(self, frame, event, arg):
if event == 'call':
return self.localtrace
else:
return None

def localtrace(self, frame, event, arg):
if self.terminate:
if event == 'line':
# Send event
wx.PostEvent(self, self._terminate_evt)
raise SystemExit()
return self.localtrace

def OnTerminate(self, event):
if event.onterminate:
event.onterminate(event)
6 changes: 6 additions & 0 deletions gui/wxpython/gis_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@

from core import globalvar
import wx
# import adv and html before wx.App is created, otherwise
# we get annoying "Debug: Adding duplicate image handler for 'Windows bitmap file'"
# during download location dialog start up, remove when not needed
import wx.adv
import wx.html
import wx.lib.mixins.listctrl as listmix

from core.gcmd import GMessage, GError, DecodeString, RunCommand
Expand Down Expand Up @@ -766,6 +771,7 @@ def DownloadLocation(self, event):
from startup.locdownload import LocationDownloadDialog

loc_download = LocationDownloadDialog(parent=self, database=self.gisdbase)
loc_download.Centre()
loc_download.ShowModal()
location = loc_download.GetLocation()
if location:
Expand Down

0 comments on commit c857cf0

Please sign in to comment.