From f0adbd7b6b03708a8cd520cdece64567605dc291 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20M=C3=BCller?= Date: Thu, 30 Nov 2023 10:34:48 +0100 Subject: [PATCH] ref: minor UI improvements --- mpl_data_cast/gui/__main__.py | 20 ++++++++--------- mpl_data_cast/gui/main.py | 7 ++---- mpl_data_cast/gui/preferences.py | 11 ++++----- mpl_data_cast/gui/preferences.ui | 26 +++------------------- mpl_data_cast/gui/widget_tree.py | 38 +++++++++++++++++++++++--------- 5 files changed, 48 insertions(+), 54 deletions(-) diff --git a/mpl_data_cast/gui/__main__.py b/mpl_data_cast/gui/__main__.py index 5994e0d..5976a28 100644 --- a/mpl_data_cast/gui/__main__.py +++ b/mpl_data_cast/gui/__main__.py @@ -1,19 +1,17 @@ def main(): - import os - import pkg_resources + from importlib import resources import sys + from PyQt6 import QtWidgets, QtCore, QtGui - from PyQt6.QtWidgets import QApplication + from .main import MPLDataCast - app = QApplication(sys.argv) - imdir = pkg_resources.resource_filename("mpl_data_cast", "img") + app = QtWidgets.QApplication(sys.argv) + ref_ico = resources.files("mpl_data_cast.gui.img") / "mpldc_icon.png" + with resources.as_file(ref_ico) as path_icon: + app.setWindowIcon(QtGui.QIcon(str(path_icon))) - from PyQt6 import QtGui - from mpl_data_cast.gui import MPLDataCast - - # Set Application Icon - icon_path = os.path.join(imdir, "mpldc_icon.png") - app.setWindowIcon(QtGui.QIcon(icon_path)) + # Use dots as decimal separators + QtCore.QLocale.setDefault(QtCore.QLocale(QtCore.QLocale.c())) window = MPLDataCast() # noqa: F841 diff --git a/mpl_data_cast/gui/main.py b/mpl_data_cast/gui/main.py index 1a244f2..39152f4 100644 --- a/mpl_data_cast/gui/main.py +++ b/mpl_data_cast/gui/main.py @@ -83,11 +83,7 @@ def on_action_preferences(self): dlg = preferences.Preferences(self) dlg.setWindowTitle("MPL-Data-Cast Preferences") dlg.exec() - # update maximum tree depth - self.widget_output.tree_depth_limit = int(self.settings.value( - "main/tree_depth_limit", 8)) - self.widget_input.tree_depth_limit = int(self.settings.value( - "main/tree_depth_limit", 8)) + # set default output path self.widget_output.path = self.settings.value("main/output_path", pathlib.Path.home()) @@ -147,6 +143,7 @@ def on_task_transfer(self) -> None: nb_files += 1 with Callback(self, nb_files) as path_callback: result = rp.cast(path_callback=path_callback) + self.widget_output.trigger_recount_objects() if result["success"]: self.progressBar.setValue(100) QtWidgets.QMessageBox.information(self, "Transfer completed", diff --git a/mpl_data_cast/gui/preferences.py b/mpl_data_cast/gui/preferences.py index daae666..5a58830 100644 --- a/mpl_data_cast/gui/preferences.py +++ b/mpl_data_cast/gui/preferences.py @@ -1,5 +1,5 @@ +from importlib import resources import pathlib -import pkg_resources import warnings from PyQt6 import uic, QtCore, QtWidgets @@ -13,9 +13,11 @@ class Preferences(QtWidgets.QDialog): def __init__(self, parent, *args, **kwargs): QtWidgets.QWidget.__init__(self, parent=parent, *args, **kwargs) - path_ui = pkg_resources.resource_filename( - "mpl_data_cast.gui", "preferences.ui") - uic.loadUi(path_ui, self) + + ref_ui = resources.files("mpl_data_cast.gui") / "preferences.ui" + with resources.as_file(ref_ui) as path_ui: + uic.loadUi(path_ui, self) + self.settings = QtCore.QSettings() self.parent = parent @@ -23,7 +25,6 @@ def __init__(self, parent, *args, **kwargs): self.config_pairs = [ ["main/output_path", self.lineEdit_output_path, pathlib.Path.home()], - ["main/tree_depth_limit", self.spinBox_tree_depth_limit, 3], ] self.reload() diff --git a/mpl_data_cast/gui/preferences.ui b/mpl_data_cast/gui/preferences.ui index a23cccf..de939ab 100644 --- a/mpl_data_cast/gui/preferences.ui +++ b/mpl_data_cast/gui/preferences.ui @@ -35,10 +35,10 @@ - - + + - Maximum depth of file tree to display: + Select directory @@ -52,26 +52,6 @@ - - - - Select directory - - - - - - - 1 - - - 24 - - - 10 - - - diff --git a/mpl_data_cast/gui/widget_tree.py b/mpl_data_cast/gui/widget_tree.py index 3d65a9a..19705dd 100644 --- a/mpl_data_cast/gui/widget_tree.py +++ b/mpl_data_cast/gui/widget_tree.py @@ -20,8 +20,18 @@ def __init__(self, *args, **kwargs): self.num_objects = 0 self.size_objects = 0 self.must_break = False + self.abort_current_count = False self.is_counting = False self.has_counted = False + self.lock = threading.Lock() + + def reset(self): + with self.lock: + self.abort_current_count = True + self.num_objects = 0 + self.size_objects = 0 + self.has_counted = False + self.is_counting = False def run(self): recipe = self.recipe @@ -62,23 +72,29 @@ def run(self): if (self.must_break or recipe != self.recipe or path != self.path): self.num_objects = 0 + self.size_objects = 0 break try: item = next(tree_iterator) except StopIteration: + self.has_counted = True break except BaseException: # Windows might encounter PermissionError. pass else: - self.num_objects += 1 - try: - self.size_objects += sum( - [it.stat().st_size for it in item]) - except BaseException: - pass - self.is_counting = False - self.has_counted = True + with self.lock: + # check before incrementing + if self.abort_current_count: + self.abort_current_count = False + break + self.num_objects += 1 + try: + self.size_objects += sum( + [it.stat().st_size for it in item]) + except BaseException: + pass + self.is_counting = False time.sleep(0.5) @@ -121,8 +137,6 @@ def __init__(self, self.groupBox.setTitle(self.which.capitalize()) self.pushButton_dir.setText(f"Select {self.which} directory") self.settings = QtCore.QSettings() - self.tree_depth_limit = int(self.settings.value( - "main/tree_depth_limit", 3)) self.pushButton_dir.clicked.connect( self.on_tree_browse_button) @@ -209,6 +223,10 @@ def on_update_object_count(self): label = f"{objects} objects ({size_str})" self.label_objects.setText(label) + @QtCore.pyqtSlot() + def trigger_recount_objects(self): + self.tree_counter.reset() + def human_size(bt, units=None): """Return a human-eadable string representation of bytes """