Skip to content

Commit 7203654

Browse files
committed
Open from directory: add cancelable progress bar
1 parent bde4f73 commit 7203654

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

cdl/core/gui/panel/base.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -855,14 +855,15 @@ def set_current_object_title(self, title: str) -> None:
855855
self.objview.update_item(obj.uuid)
856856

857857
def __load_from_file(
858-
self, filename: str, create_group: bool = True
858+
self, filename: str, create_group: bool = True, add_objects: bool = True
859859
) -> list[SignalObj] | list[ImageObj]:
860860
"""Open objects from file (signal/image), add them to DataLab and return them.
861861
862862
Args:
863863
filename: file name
864864
create_group: if True, create a new group if more than one object is loaded.
865865
Defaults to True. If False, all objects are added to the current group.
866+
add_objects: if True, add objects to the panel. Defaults to True.
866867
867868
Returns:
868869
New object or list of new objects
@@ -875,7 +876,8 @@ def __load_from_file(
875876
group_id = self.add_group(osp.basename(filename)).uuid
876877
for obj in objs:
877878
obj.metadata["source"] = filename
878-
self.add_object(obj, group_id=group_id, set_current=obj is objs[-1])
879+
if add_objects:
880+
self.add_object(obj, group_id=group_id, set_current=obj is objs[-1])
879881
self.selection_changed()
880882
return objs
881883

@@ -907,29 +909,44 @@ def load_from_directory(self, directory: str | None = None) -> list[TypeObj]:
907909
directory = getexistingdirectory(self, _("Open"), basedir)
908910
if not directory:
909911
return []
912+
folders = [
913+
path
914+
for path in glob.glob(osp.join(directory, "**"), recursive=True)
915+
if osp.isdir(path) and len(os.listdir(path)) > 0
916+
]
910917
objs = []
911-
# Iterate over all subfolders in the directory:
912-
for path in glob.glob(osp.join(directory, "**"), recursive=True):
913-
if osp.isdir(path) and len(os.listdir(path)) > 0:
918+
with create_progress_bar(
919+
self, _("Scanning directory"), max_=len(folders) - 1
920+
) as progress:
921+
# Iterate over all subfolders in the directory:
922+
for i_path, path in enumerate(folders):
923+
progress.setValue(i_path + 1)
924+
if progress.wasCanceled():
925+
break
914926
path = osp.normpath(path)
915-
self.add_group(osp.basename(path), select=True)
916927
fnames = [
917928
osp.join(path, fname)
918929
for fname in os.listdir(path)
919930
if osp.isfile(osp.join(path, fname))
920931
]
921932
new_objs = self.load_from_files(
922-
fnames, create_group=False, ignore_errors=True
933+
fnames,
934+
create_group=False,
935+
add_objects=False,
936+
ignore_errors=True,
923937
)
924-
objs += new_objs
925-
if len(new_objs) == 0:
926-
self.remove_object(force=True) # Remove empty group
938+
if new_objs:
939+
objs += new_objs
940+
grp = self.add_group(osp.basename(path))
941+
for obj in new_objs:
942+
self.add_object(obj, group_id=grp.uuid, set_current=False)
927943
return objs
928944

929945
def load_from_files(
930946
self,
931947
filenames: list[str] | None = None,
932948
create_group: bool = False,
949+
add_objects: bool = True,
933950
ignore_errors: bool = False,
934951
) -> list[TypeObj]:
935952
"""Open objects from file (signals/images), add them to DataLab and return them.
@@ -939,6 +956,7 @@ def load_from_files(
939956
create_group: if True, create a new group if more than one object is loaded
940957
for a single file. Defaults to False: all objects are added to the current
941958
group.
959+
add_objects: if True, add objects to the panel. Defaults to True.
942960
ignore_errors: if True, ignore errors when loading files. Defaults to False.
943961
944962
Returns:
@@ -956,7 +974,9 @@ def load_from_files(
956974
with qt_try_loadsave_file(self.parent(), filename, "load"):
957975
Conf.main.base_dir.set(filename)
958976
try:
959-
objs += self.__load_from_file(filename, create_group=create_group)
977+
objs += self.__load_from_file(
978+
filename, create_group=create_group, add_objects=add_objects
979+
)
960980
except Exception as exc: # pylint: disable=broad-except
961981
if ignore_errors:
962982
# Ignore unknown file types

cdl/locale/fr/LC_MESSAGES/cdl.po

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
msgid ""
55
msgstr ""
66
"Project-Id-Version: PACKAGE VERSION\n"
7-
"POT-Creation-Date: 2025-03-30 15:59+0200\n"
7+
"POT-Creation-Date: 2025-04-08 10:41+0200\n"
88
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
99
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1010
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -1960,6 +1960,9 @@ msgstr "Renommer le groupe"
19601960
msgid "Adding objects to workspace"
19611961
msgstr "Ajout d'objets à l'espace de travail"
19621962

1963+
msgid "Scanning directory"
1964+
msgstr "Analyse du répertoire"
1965+
19631966
msgid "Save as"
19641967
msgstr "Enregistrer sous"
19651968

doc/locale/fr/LC_MESSAGES/api/core.gui/panel.po

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: DataLab \n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2025-04-08 10:22+0200\n"
11+
"POT-Creation-Date: 2025-04-08 10:41+0200\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language: fr\n"
@@ -346,6 +346,9 @@ msgstr ""
346346
msgid "if True, create a new group if more than one object is loaded for a single file. Defaults to False: all objects are added to the current group."
347347
msgstr ""
348348

349+
msgid "if True, add objects to the panel. Defaults to True."
350+
msgstr ""
351+
349352
msgid "if True, ignore errors when loading files. Defaults to False."
350353
msgstr ""
351354

0 commit comments

Comments
 (0)