Commit 2919cac9 authored by Yan's avatar Yan
Browse files

Added Open Recents functionality, minor cleanup

* Recent file list is now stored in settings
* flake8 cleanup
* tried better thread handling
parent a7893c0a
...@@ -91,7 +91,11 @@ def load_file(parent, augCanvas, update, settings, loadthread, filename=None): ...@@ -91,7 +91,11 @@ def load_file(parent, augCanvas, update, settings, loadthread, filename=None):
augCanvas.filename = filename augCanvas.filename = filename
parent.setWindowTitle("Prasopes - {}".format( parent.setWindowTitle("Prasopes - {}".format(
os.path.basename(filename))) os.path.basename(filename)))
oldrecents = settings.value("recents")
oldrecents.remove(filename) if filename in oldrecents else None
settings.setValue("recents", [filename, *oldrecents])
update.signal.emit() update.signal.emit()
loadthread.quit()
error.signal.connect(lambda: QtWidgets.QMessageBox.critical( error.signal.connect(lambda: QtWidgets.QMessageBox.critical(
parent, errormsg[0], errormsg[1])) parent, errormsg[0], errormsg[1]))
loadthread.run = runfnc loadthread.run = runfnc
...@@ -143,6 +147,22 @@ def update_spectrum(augCanvas, config): ...@@ -143,6 +147,22 @@ def update_spectrum(augCanvas, config):
augCanvas.draw() augCanvas.draw()
def update_recents(rcm, main_window, augCanvas, update, config, loadthread):
rcm.clear()
# Actions need to be stored somewhere. Otherwise they end up in garbage.
rcm.actionCache = []
for i, j in enumerate(config.value("recents"), start=1):
rcm.actionCache.append(QtWidgets.QAction("&{}. {}".format(i, j), None))
rcm.actionCache[-1].triggered.connect(lambda _, fn=j: load_file(
main_window, augCanvas, update, config, loadthread, filename=fn))
rcm.actionCache.append(rcm.addSeparator())
rcm.actionCache.append(QtWidgets.QAction("Clear Recents", None))
[rcm.actionCache[-1].triggered.connect(i) for i in (
lambda: config.setValue("recents", ""), lambda: update_recents(
rcm, main_window, augCanvas, update, config, loadthread))]
rcm.addActions(rcm.actionCache)
def dropped(event, parent, augCanvas, update, config, loadthread): def dropped(event, parent, augCanvas, update, config, loadthread):
dropurl = event.mimeData().urls()[0].toLocalFile() dropurl = event.mimeData().urls()[0].toLocalFile()
load_file(parent, augCanvas, update, config, loadthread, filename=dropurl) load_file(parent, augCanvas, update, config, loadthread, filename=dropurl)
...@@ -217,7 +237,7 @@ def main(): ...@@ -217,7 +237,7 @@ def main():
zce_logger = logging.getLogger('zceLogger') zce_logger = logging.getLogger('zceLogger')
logging.basicConfig() logging.basicConfig()
# p_logger.setLevel("WARN") # p_logger.setLevel("WARN")
# p_logger.setLevel("DEBUG") p_logger.setLevel("DEBUG")
# drl_logger.setLevel("INFO") # drl_logger.setLevel("INFO")
drl_logger.setLevel("DEBUG") drl_logger.setLevel("DEBUG")
zce_logger.setLevel("DEBUG") zce_logger.setLevel("DEBUG")
...@@ -299,9 +319,16 @@ def main(): ...@@ -299,9 +319,16 @@ def main():
predictform.editingFinished.connect(lambda: predictmz( predictform.editingFinished.connect(lambda: predictmz(
predictform, augCanvas)) predictform, augCanvas))
recents_menu = QtWidgets.QMenu('Open &Recent', main_window)
update_recents(recents_menu, main_window, augCanvas,
update, config, loadthread)
update.signal.connect(lambda: update_recents(
recents_menu, main_window, augCanvas, update, config, loadthread))
file_menu = QtWidgets.QMenu('&File', main_window) file_menu = QtWidgets.QMenu('&File', main_window)
main_window.menuBar().addMenu(file_menu) main_window.menuBar().addMenu(file_menu)
file_menu.addAction(openact) file_menu.addAction(openact)
file_menu.addMenu(recents_menu)
file_menu.addAction(exportact) file_menu.addAction(exportact)
file_menu.addSeparator() file_menu.addSeparator()
file_menu.addAction(printact) file_menu.addAction(printact)
......
...@@ -19,7 +19,8 @@ def settings(): ...@@ -19,7 +19,8 @@ def settings():
("imggen/xinch", 10), ("imggen/xinch", 10),
("imggen/yinch", 4), ("imggen/yinch", 4),
("imggen/dpi", 300), ("imggen/dpi", 300),
("imggen/xtics", 5)} ("imggen/xtics", 5),
("recents", "")}
[settings.setValue(*i) [settings.setValue(*i)
for i in defvals if not settings.contains(i[0])] for i in defvals if not settings.contains(i[0])]
return settings return settings
......
...@@ -59,7 +59,8 @@ def consoleDockWidget(localvars, actionname, cfval): ...@@ -59,7 +59,8 @@ def consoleDockWidget(localvars, actionname, cfval):
console = InteractiveConsole(locals=loc) console = InteractiveConsole(locals=loc)
text = inp.text() text = inp.text()
inp.setText("") inp.setText("")
with contextlib.redirect_stdout(stream), contextlib.redirect_stderr(stream): with contextlib.redirect_stdout(stream),\
contextlib.redirect_stderr(stream):
outp.append(">>>"+text) outp.append(">>>"+text)
console.runcode(text) console.runcode(text)
outtext = stream.getvalue() outtext = stream.getvalue()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment