forked from M-Labs/artiq
1
0
Fork 0

browser: cleanup dir/file restore, closes #527

This commit is contained in:
Robert Jördens 2016-07-31 17:24:08 +02:00
parent 92f3757c74
commit e7d6ad2595
2 changed files with 22 additions and 16 deletions

View File

@ -120,7 +120,7 @@ class FilesDock(QtWidgets.QDockWidget):
dataset_changed = QtCore.pyqtSignal(str) dataset_changed = QtCore.pyqtSignal(str)
metadata_changed = QtCore.pyqtSignal(dict) 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") QtWidgets.QDockWidget.__init__(self, "Files")
self.setObjectName("Files") self.setObjectName("Files")
self.setFeatures(self.DockWidgetMovable | self.DockWidgetFloatable) self.setFeatures(self.DockWidgetMovable | self.DockWidgetFloatable)
@ -161,8 +161,6 @@ class FilesDock(QtWidgets.QDockWidget):
self.rl.activated.connect(self.list_activated) self.rl.activated.connect(self.list_activated)
self.splitter.addWidget(self.rl) self.splitter.addWidget(self.rl)
self.restore_selection = restore_selection
def tree_current_changed(self, current, previous): def tree_current_changed(self, current, previous):
idx = self.rt.model().mapToSource(current) idx = self.rt.model().mapToSource(current)
self.rl.setRootIndex(idx) self.rl.setRootIndex(idx)
@ -213,9 +211,11 @@ class FilesDock(QtWidgets.QDockWidget):
def select_dir(self, path): def select_dir(self, path):
if not os.path.exists(path): if not os.path.exists(path):
logger.warning("directory does not exist %s", path)
return return
idx = self.model.index(path) idx = self.model.index(path)
if not idx.isValid(): if not idx.isValid():
logger.warning("directory invalid %s", path)
return return
self.rl.setRootIndex(idx) self.rl.setRootIndex(idx)
@ -237,22 +237,29 @@ class FilesDock(QtWidgets.QDockWidget):
def select_file(self, path): def select_file(self, path):
if not os.path.exists(path): if not os.path.exists(path):
logger.warning("file does not exist %s", path)
return return
self.select_dir(os.path.dirname(path)) self.select_dir(os.path.dirname(path))
idx = self.model.index(path) idx = self.model.index(path)
if not idx.isValid(): if not idx.isValid():
logger.warning("file invalid %s", path)
return return
self.rl.setCurrentIndex(idx) self.rl.setCurrentIndex(idx)
def save_state(self): def save_state(self):
return { state = {
"dir": self.model.filePath(self.rl.rootIndex()), "dir": self.model.filePath(self.rl.rootIndex()),
"file": self.model.filePath(self.rl.currentIndex()),
"splitter": bytes(self.splitter.saveState()), "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): 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.splitter.restoreState(QtCore.QByteArray(state["splitter"]))
self.select_dir(state["dir"])
if state["file"] is not None:
self.select_file(state["file"])

View File

@ -43,7 +43,7 @@ def get_argparser():
class Browser(QtWidgets.QMainWindow): 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): master_host, master_port):
QtWidgets.QMainWindow.__init__(self) QtWidgets.QMainWindow.__init__(self)
smgr.register(self) smgr.register(self)
@ -68,8 +68,7 @@ class Browser(QtWidgets.QMainWindow):
QtCore.Qt.ScrollBarAsNeeded) QtCore.Qt.ScrollBarAsNeeded)
self.setCentralWidget(self.experiments) self.setCentralWidget(self.experiments)
self.files = files.FilesDock(datasets_sub, browse_root, self.files = files.FilesDock(datasets_sub, browse_root)
restore_selection)
smgr.register(self.files) smgr.register(self.files)
self.files.dataset_activated.connect( self.files.dataset_activated.connect(
@ -146,13 +145,9 @@ def main():
smgr = state.StateManager(args.db_file) smgr = state.StateManager(args.db_file)
browser = Browser(smgr, datasets_sub, args.browse_root, browser = Browser(smgr, datasets_sub, args.browse_root,
args.select is not None, args.server, args.server, args.port)
args.port)
widget_log_handler.callback = browser.log.append_message widget_log_handler.callback = browser.log.append_message
if args.select is not None:
browser.files.select(args.select)
if os.name == "nt": if os.name == "nt":
# HACK: show the main window before creating applets. # HACK: show the main window before creating applets.
# Otherwise, the windows of those applets that are in detached # Otherwise, the windows of those applets that are in detached
@ -161,6 +156,10 @@ def main():
smgr.load() smgr.load()
smgr.start() smgr.start()
atexit_register_coroutine(smgr.stop) atexit_register_coroutine(smgr.stop)
if args.select is not None:
browser.files.select(args.select)
browser.show() browser.show()
loop.run_until_complete(browser.exit_request.wait()) loop.run_until_complete(browser.exit_request.wait())