From e7d6ad2595ed20021823e2693789cddc0fcbeab4 Mon Sep 17 00:00:00 2001 From: Robert Jordens Date: Sun, 31 Jul 2016 17:24:08 +0200 Subject: [PATCH] browser: cleanup dir/file restore, closes #527 --- artiq/browser/files.py | 23 +++++++++++++++-------- artiq/frontend/artiq_browser.py | 15 +++++++-------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/artiq/browser/files.py b/artiq/browser/files.py index 1e6a061ce..1a45c7938 100644 --- a/artiq/browser/files.py +++ b/artiq/browser/files.py @@ -120,7 +120,7 @@ class FilesDock(QtWidgets.QDockWidget): dataset_changed = QtCore.pyqtSignal(str) metadata_changed = QtCore.pyqtSignal(dict) - def __init__(self, datasets, browse_root="", restore_selection=True): + def __init__(self, datasets, browse_root=""): QtWidgets.QDockWidget.__init__(self, "Files") self.setObjectName("Files") self.setFeatures(self.DockWidgetMovable | self.DockWidgetFloatable) @@ -161,8 +161,6 @@ class FilesDock(QtWidgets.QDockWidget): self.rl.activated.connect(self.list_activated) self.splitter.addWidget(self.rl) - self.restore_selection = restore_selection - def tree_current_changed(self, current, previous): idx = self.rt.model().mapToSource(current) self.rl.setRootIndex(idx) @@ -213,9 +211,11 @@ class FilesDock(QtWidgets.QDockWidget): def select_dir(self, path): if not os.path.exists(path): + logger.warning("directory does not exist %s", path) return idx = self.model.index(path) if not idx.isValid(): + logger.warning("directory invalid %s", path) return self.rl.setRootIndex(idx) @@ -237,22 +237,29 @@ class FilesDock(QtWidgets.QDockWidget): def select_file(self, path): if not os.path.exists(path): + logger.warning("file does not exist %s", path) return self.select_dir(os.path.dirname(path)) idx = self.model.index(path) if not idx.isValid(): + logger.warning("file invalid %s", path) return self.rl.setCurrentIndex(idx) def save_state(self): - return { + state = { "dir": self.model.filePath(self.rl.rootIndex()), - "file": self.model.filePath(self.rl.currentIndex()), "splitter": bytes(self.splitter.saveState()), } + idx = self.rl.currentIndex() + if idx.isValid(): + state["file"] = self.model.filePath(idx) + else: + state["file"] = None + return state def restore_state(self, state): - if self.restore_selection: - self.select_dir(state["dir"]) - self.select_file(state["file"]) self.splitter.restoreState(QtCore.QByteArray(state["splitter"])) + self.select_dir(state["dir"]) + if state["file"] is not None: + self.select_file(state["file"]) diff --git a/artiq/frontend/artiq_browser.py b/artiq/frontend/artiq_browser.py index a11a39b1c..10fc0df85 100755 --- a/artiq/frontend/artiq_browser.py +++ b/artiq/frontend/artiq_browser.py @@ -43,7 +43,7 @@ def get_argparser(): class Browser(QtWidgets.QMainWindow): - def __init__(self, smgr, datasets_sub, browse_root, restore_selection, + def __init__(self, smgr, datasets_sub, browse_root, master_host, master_port): QtWidgets.QMainWindow.__init__(self) smgr.register(self) @@ -68,8 +68,7 @@ class Browser(QtWidgets.QMainWindow): QtCore.Qt.ScrollBarAsNeeded) self.setCentralWidget(self.experiments) - self.files = files.FilesDock(datasets_sub, browse_root, - restore_selection) + self.files = files.FilesDock(datasets_sub, browse_root) smgr.register(self.files) self.files.dataset_activated.connect( @@ -146,13 +145,9 @@ def main(): smgr = state.StateManager(args.db_file) browser = Browser(smgr, datasets_sub, args.browse_root, - args.select is not None, args.server, - args.port) + args.server, args.port) widget_log_handler.callback = browser.log.append_message - if args.select is not None: - browser.files.select(args.select) - if os.name == "nt": # HACK: show the main window before creating applets. # Otherwise, the windows of those applets that are in detached @@ -161,6 +156,10 @@ def main(): smgr.load() smgr.start() atexit_register_coroutine(smgr.stop) + + if args.select is not None: + browser.files.select(args.select) + browser.show() loop.run_until_complete(browser.exit_request.wait())