Skip to content

Commit

Permalink
add Open Recent to gui
Browse files Browse the repository at this point in the history
  • Loading branch information
zeptofine committed Oct 7, 2023
1 parent 83bd9a3 commit f7fe8c7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ env.bak/
venv.bak/

filedb.arrow
config.json
config.json
imdataset_creator/gui/.recent_files
53 changes: 46 additions & 7 deletions imdataset_creator/gui/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from PySide6 import QtWidgets
from PySide6.QtCore import Qt, Signal, Slot
from PySide6.QtGui import QAction, QKeySequence, QShortcut
from PySide6.QtWidgets import QFileDialog, QMainWindow, QSplitter, QStatusBar, QToolBar, QWidget
from PySide6.QtWidgets import QFileDialog, QMainWindow, QSplitter, QStatusBar, QToolBar, QWidget, QMenu
from rich import print as rprint

from ..configs import MainConfig
Expand All @@ -33,6 +33,16 @@
RECENT_FILES_PATH = PROGRAM_ORIGIN / ".recent_files"


def get_recent_files():
if RECENT_FILES_PATH.exists():
return RECENT_FILES_PATH.read_text().splitlines()
return []


def save_recent_files(files):
RECENT_FILES_PATH.write_text("\n".join(files))


class InputList(FlowList):
items: list[InputView]

Expand Down Expand Up @@ -144,8 +154,13 @@ def __init__(self, cfg_path=Path("config.json")):
filemenu.addAction(save_as_action)
filemenu.addAction(reload_action)

self.recents_menu = QMenu("Open Recent", self)
self.recent_files = []
filemenu.addMenu(self.recents_menu)

editmenu = menu.addMenu("Edit")
editmenu.addAction(clear_action)

# (get_producers := QAction("get producers", self)).triggered.connect(self.gather_producers)
# (get_rules := QAction("get rules", self)).triggered.connect(self.gather_rules)
# (get_builder := QAction("get builder", self)).triggered.connect(self.create_builder)
Expand Down Expand Up @@ -188,15 +203,20 @@ def save_config_as(self):
def load_config(self):
with self.cfg_path.open("r") as f:
self.from_cfg(MainConfig(json.load(f)))
self.update_recents()

@Slot()
def open_config(self):
file = QFileDialog.getOpenFileName(
self,
"Select cfg path",
str(self.cfg_path.parent),
)[0]
def open_config(self, s: str = ""):
if not s:
file = QFileDialog.getOpenFileName(
self,
"Select cfg path",
str(self.cfg_path.parent),
)[0]
else:
file = s
if file:
print(f"Opening {file}")
self.cfg_path = Path(file)
self.load_config()

Expand All @@ -207,6 +227,20 @@ def clear(self):
self.rulelist.empty()
self.outputlist.empty()

@Slot()
def update_recents(self):
recents = get_recent_files()
if (txt := str(self.cfg_path.resolve())) not in recents:
recents.insert(0, txt)
else:
recents.remove(txt)
recents.insert(0, txt)
save_recent_files(recents[:10])

self.recents_menu.clear()
for file in recents:
self.recents_menu.addAction(file).triggered.connect(lambda: self.open_config(file))

@catch_loading
@Slot(dict)
def from_cfg(self, cfg: MainConfig):
Expand Down Expand Up @@ -267,6 +301,11 @@ def main():
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--cfg_path")
args = parser.parse_args()

# check all recent files exist

save_recent_files([file for file in get_recent_files() if os.path.exists(file)])

app = QtWidgets.QApplication([])
central_window = Window(Path(args.cfg_path)) if args.cfg_path else Window()
central_window.show()
Expand Down

0 comments on commit f7fe8c7

Please sign in to comment.