Skip to content

Commit

Permalink
Show progress dialog only when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
probonopd committed Feb 12, 2025
1 parent c92937b commit b8191f3
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions fileops.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def __init__(self, parent):
self.progress_dialog.setWindowModality(QtCore.Qt.WindowModality.WindowModal)
self.progress_dialog.setAutoClose(True)
self.progress_dialog.setAutoReset(True)
self.show_window_timer = QtCore.QTimer(self.parent)
self.show_window_timer.timeout.connect(self.show_progress_dialog)
self.show_window_timer.setSingleShot(True)
self.operation_finished = False

def cancel(self):
if hasattr(self, "op_thread"):
Expand All @@ -23,9 +27,12 @@ def cancel(self):
def showError(self, message: str):
# Close the progress dialog if it's visible; if it hasn't been shown yet,
# this will be effectively a no-op.
self.show_window_timer.timeout.disconnect()
if self.progress_dialog.isVisible():
self.progress_dialog.close()

self.show_window_timer.timeout.disconnect()

# Create and execute an error message box.
err_box = QtWidgets.QMessageBox(self.parent)
err_box.setIcon(QtWidgets.QMessageBox.Icon.Critical)
Expand Down Expand Up @@ -56,18 +63,30 @@ def run(self, operations, op_type):

# At this point no errors, so show the progress dialog.
self.progress_dialog.setValue(0)
self.progress_dialog.show()
self.operation_finished = False
self.show_window_timer.start(1000)

self.op_thread = FileOperationThread(operations, op_type, total_size)
self.op_thread.progress.connect(self.progress_dialog.setValue)
# Connect error signal to our method; this ensures that the progress dialog closes
# before or as the error dialog appears.
self.op_thread.error.connect(self.showError)
self.op_thread.finished.connect(lambda: (self.progress_dialog.close(),
self.parent.refresh_view() if hasattr(self.parent, "refresh_view") else None))
self.op_thread.finished.connect(self.operation_finished_slot)
self.progress_dialog.canceled.connect(self.op_thread.cancel)
self.op_thread.start()

def show_progress_dialog(self):
# Only show the progress dialog if the operation hasn't finished yet
# and if the current progress is still less than 30%.
if not self.operation_finished and self.progress_dialog.value() < 30:
self.progress_dialog.show()

def operation_finished_slot(self):
self.operation_finished = True
self.progress_dialog.close()
self.show_window_timer.timeout.disconnect()
self.parent.refresh_view() if hasattr(self.parent, "refresh_view") else None


class FileOperationThread(QtCore.QThread):
progress = QtCore.pyqtSignal(int)
Expand Down

0 comments on commit b8191f3

Please sign in to comment.