From ede9e93b52265edc7751771c1dc65df366d2159d Mon Sep 17 00:00:00 2001 From: Huy Pham <42948410+hpham@users.noreply.github.com> Date: Mon, 3 Feb 2020 09:10:46 -0800 Subject: [PATCH] add a menu option for opening recent files, hard code some example xml files for testing --- daemon/core/gui/menuaction.py | 11 +++++++---- daemon/core/gui/menubar.py | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/daemon/core/gui/menuaction.py b/daemon/core/gui/menuaction.py index 6f7e5371..870bb239 100644 --- a/daemon/core/gui/menuaction.py +++ b/daemon/core/gui/menuaction.py @@ -89,12 +89,15 @@ class MenuAction: title="Open", filetypes=(("XML Files", "*.xml"), ("All Files", "*")), ) - if file_path: - self.app.core.xml_file = file_path - self.app.core.xml_dir = str(os.path.dirname(file_path)) + self.open_xml_task(file_path) + + def open_xml_task(self, filename): + if filename: + self.app.core.xml_file = filename + self.app.core.xml_dir = str(os.path.dirname(filename)) self.prompt_save_running_session() self.app.statusbar.progress_bar.start(5) - task = BackgroundTask(self.app, self.app.core.open_xml, args=(file_path,)) + task = BackgroundTask(self.app, self.app.core.open_xml, args=(filename,)) task.start() def gui_preferences(self): diff --git a/daemon/core/gui/menubar.py b/daemon/core/gui/menubar.py index afaf2b1a..83852f29 100644 --- a/daemon/core/gui/menubar.py +++ b/daemon/core/gui/menubar.py @@ -1,3 +1,5 @@ +import logging +import os import tkinter as tk from functools import partial from typing import TYPE_CHECKING @@ -55,6 +57,17 @@ class Menubar(tk.Menu): menu.add_command(label="Save", accelerator="Ctrl+S", command=self.save) menu.add_command(label="Reload", underline=0, state=tk.DISABLED) self.app.bind_all("", self.save) + sample_xmls = [ + "sample1.xml", + "/home/ncs/.coretk/xmls/sample1.xml", + "sample1.xml", + ] + recent = tk.Menu(menu) + for i in sample_xmls: + recent.add_command(label=i, command=partial(self.open_recent_files, i)) + menu.add_cascade(label="Recent files", menu=recent) + + # menu.add_command(label="Recent files") menu.add_separator() menu.add_command(label="Export Python script...", state=tk.DISABLED) menu.add_command(label="Execute XML or Python script...", state=tk.DISABLED) @@ -409,6 +422,13 @@ class Menubar(tk.Menu): menu.add_command(label="About", command=self.menuaction.show_about) self.add_cascade(label="Help", menu=menu) + def open_recent_files(self, filename: str): + if os.path.isfile(filename): + logging.debug("Open recent file %s", filename) + self.menuaction.open_xml_task(filename) + else: + logging.warning("File does not exist %s", filename) + def save(self): xml_file = self.app.core.xml_file if xml_file: