Skip to content

Commit fc7c1ff

Browse files
mrvisschermarc-vdm
andauthored
Multiple patches for the import Wizard (#1205)
* Wizard actually destroyed, any threads safely exited * MacOS fixes * Turn off 'Applying strategy: ...' prints for better thread stability --------- Co-authored-by: marc-vdm <m.t.van.der.meide@cml.leidenuniv.nl>
1 parent 96a60fd commit fc7c1ff

2 files changed

Lines changed: 42 additions & 9 deletions

File tree

activity_browser/bwutils/importers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def simple_automated_import(cls, filepath, db_name: str, relink: dict = None) ->
8181
obj.project_parameters, obj.database_parameters,
8282
any("parameters" in ds for ds in obj.data)
8383
])
84-
obj.apply_strategies()
84+
obj.apply_strategies(verbose=False)
8585
if any(obj.unlinked) and relink:
8686
for db, new_db in relink.items():
8787
if db == "(name missing)":

activity_browser/ui/wizards/db_import_wizard.py

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def __init__(self, parent=None):
5353
self.downloader = ABEcoinventDownloader()
5454
self.setWindowTitle("Database Import Wizard")
5555
self.setWindowModality(QtCore.Qt.ApplicationModal)
56+
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
57+
self.setWindowFlags(QtCore.Qt.Sheet)
58+
self.setOption(self.NoCancelButton, False)
5659

5760
# Construct and bind pages.
5861
self.import_type_page = ImportTypePage(self)
@@ -85,10 +88,6 @@ def __init__(self, parent=None):
8588
# db import is done when finish button becomes active
8689
self.button(QtWidgets.QWizard.FinishButton).clicked.connect(self.cleanup)
8790

88-
# thread management
89-
self.button(QtWidgets.QWizard.CancelButton).clicked.connect(self.cancel_thread)
90-
self.button(QtWidgets.QWizard.CancelButton).clicked.connect(self.cancel_extraction)
91-
9291
import_signals.connection_problem.connect(self.show_info)
9392
import_signals.import_failure.connect(self.show_info)
9493
import_signals.import_failure_detailed.connect(self.show_detailed)
@@ -104,20 +103,54 @@ def system_model(self):
104103
def update_downloader(self):
105104
self.downloader.version = self.version
106105
self.downloader.system_model = self.system_model
106+
107+
def done(self, result: int):
108+
"""
109+
Reimplementation of the QWizard.done method which is called when the wizard is done
110+
or when the user cancels.
111+
"""
112+
# indicate to the user that the click was succesful
113+
self.button(QtWidgets.QWizard.CancelButton).setDisabled(True)
114+
115+
# cancel any running tasks
116+
self.cancel_extraction()
117+
self.cancel_thread()
118+
119+
# else just call done on super()
120+
super().done(result)
107121

108122
def closeEvent(self, event):
109123
""" Close event now behaves similarly to cancel, because of self.reject.
110124
111125
This allows the import wizard to be reused, ie starts from the beginning
112126
"""
113-
self.cancel_thread()
114127
self.cancel_extraction()
128+
self.cancel_thread()
115129
event.accept()
116130

117131
def cancel_thread(self):
118-
log.info('\nDatabase import interrupted!')
119-
import_signals.cancel_sentinel = True
120-
self.cleanup()
132+
"""Cancels the worker thread initiated by the import page"""
133+
thread = self.import_page.main_worker_thread
134+
dispatcher = self.thread().eventDispatcher()
135+
136+
# show the user we're working on something within the wizard
137+
self.setCursor(QtCore.Qt.WaitCursor)
138+
139+
if thread.isRunning():
140+
# flag an abort through the sentinel
141+
import_signals.cancel_sentinel = True
142+
# make sure the import page doesn't receive any last signals by the thread
143+
import_signals.disconnect(self.import_page)
144+
# signal the thread to exit when it can do so safely
145+
thread.exit(1)
146+
147+
# block while the thread is still running
148+
while thread.isRunning():
149+
# make sure we stay responsive
150+
dispatcher.processEvents(QtCore.QEventLoop.AllEvents)
151+
152+
# return to normal
153+
self.setCursor(QtCore.Qt.ArrowCursor)
121154

122155
def cancel_extraction(self):
123156
process = getattr(self.downloader, "extraction_process", None)

0 commit comments

Comments
 (0)