Browse Source

Factor out root-dir-setting

Could now be redone at runtime (in principle)
Michael Hanke 1 year ago
parent
commit
70587cf43f
3 changed files with 44 additions and 35 deletions
  1. 16 16
      datalad_gooey/app.py
  2. 26 18
      datalad_gooey/fsbrowser.py
  3. 2 1
      datalad_gooey/tests/test_fsbrowser.py

+ 16 - 16
datalad_gooey/app.py

@@ -105,21 +105,6 @@ class GooeyApp(QObject):
         # setup themeing before the first dialog goes up
         self._setup_looknfeel()
 
-        if not path:
-            # start root path given, ask user
-            path = QFileDialog.getExistingDirectory(
-                caption="Select a base directory for DataLad",
-                options=QFileDialog.ShowDirsOnly,
-            )
-            if not path:
-                # user aborted root path selection, start in HOME.
-                # HOME is a better choice than CWD in most environments
-                path = Path.home()
-
-        # set path for root item and PWD to give relative paths a reference that makes
-        # sense within the app
-        self._set_root_path(path)
-
         self._dlapi = None
         self._main_window = None
         self._cmdexec = GooeyDataladCmdExec()
@@ -128,9 +113,11 @@ class GooeyApp(QObject):
         # setup UI
         self._fsbrowser = GooeyFilesystemBrowser(
             self,
-            path,
             self.get_widget('fsBrowser'),
         )
+        # set path for root item and PWD to give relative paths a reference
+        # that makes sense within the app
+        self._set_root_path(path)
 
         # remember what backend was in use
         self._prev_ui_backend = dlui.ui.backend
@@ -261,8 +248,21 @@ class GooeyApp(QObject):
         # for a use case.
         # to make this possible, we would need to be able to adjust or reset the
         # treeview
+        if not path:
+            # start root path given, ask user
+            path = QFileDialog.getExistingDirectory(
+                caption="Select a base directory for DataLad",
+                options=QFileDialog.ShowDirsOnly,
+            )
+            if not path:
+                # user aborted root path selection, start in HOME.
+                # HOME is a better choice than CWD in most environments
+                path = Path.home()
+
         chpwd(path)
         self._path = path
+        # (re)init the browser
+        self._fsbrowser.set_root(path)
 
     @property
     def rootpath(self):

+ 26 - 18
datalad_gooey/fsbrowser.py

@@ -36,10 +36,13 @@ class GooeyFilesystemBrowser(QObject):
     item_requires_annotation = Signal(FSBrowserItem)
 
     # DONE
-    def __init__(self, app, path: Path, treewidget: QTreeWidget):
+    def __init__(self, app, treewidget: QTreeWidget):
         super().__init__()
 
         tw = treewidget
+        # disable until set_root() was called
+        tw.setDisabled(True)
+        self._tree = tw
         # TODO must setColumnNumber()
 
         self._app = app
@@ -51,26 +54,9 @@ class GooeyFilesystemBrowser(QObject):
         # established defined sorting order of the tree
         tw.sortItems(1, Qt.AscendingOrder)
 
-        # establish the root item, based on a fake lsdir result
-        # the info needed is so simple, it is not worth a command
-        # execution
-        root = FSBrowserItem.from_lsdir_result(
-            dict(
-                path=path,
-                type='dataset' if GitRepo.is_valid(path) else 'directory',
-            ),
-            parent=tw,
-        )
-        # set the tooltip to the full path, otherwise only names are shown
-        root.setToolTip(0, str(path))
-        tw.addTopLevelItem(root)
-        self._root_item = root
-
         tw.customContextMenuRequested.connect(
             self._custom_context_menu)
 
-        self._tree = tw
-
         # whenever a treeview node is expanded, add the path to the fswatcher
         tw.itemExpanded.connect(self._watch_dir)
         # and also populate it with items for contained paths
@@ -91,6 +77,28 @@ class GooeyFilesystemBrowser(QObject):
         self._app._cmdexec.results_received.connect(
             self._cmdexec_results_handler)
 
+    def set_root(self, path):
+        tw = self._tree
+        # wipe the previous state
+        tw.clear()
+        # TODO stop any pending annotations
+
+        # establish the root item, based on a fake lsdir result
+        # the info needed is so simple, it is not worth a command
+        # execution
+        root = FSBrowserItem.from_lsdir_result(
+            dict(
+                path=path,
+                type='dataset' if GitRepo.is_valid(path) else 'directory',
+            ),
+            parent=tw,
+        )
+        # set the tooltip to the full path, otherwise only names are shown
+        root.setToolTip(0, str(path))
+        tw.addTopLevelItem(root)
+        self._root_item = root
+        tw.setEnabled(True)
+
     def _populate_item(self, item):
         if item.childCount():
             return

+ 2 - 1
datalad_gooey/tests/test_fsbrowser.py

@@ -14,4 +14,5 @@ def test_GooeyFilesystemBrowser():
     class FakeApp(QWidget):
         _cmdexec = GooeyDataladCmdExec()
 
-    GooeyFilesystemBrowser(FakeApp(), Path.cwd(), QTreeWidget())
+    fsb = GooeyFilesystemBrowser(FakeApp(), QTreeWidget())
+    fsb.set_root(Path.cwd())