1- from PySide6 .QtWidgets import QMessageBox
1+ from PySide6 .QtWidgets import QMessageBox , QMenu
2+ from PySide6 .QtCore import QObject , Signal , QSettings
3+ from PySide6 .QtGui import QAction
24
35
46def prompt_overwrite_or_append (controller ):
@@ -17,4 +19,52 @@ def prompt_overwrite_or_append(controller):
1719 elif msg_box .clickedButton () == overwrite_button :
1820 return "overwrite"
1921 elif msg_box .clickedButton () == append_button :
20- return "append"
22+ return "append"
23+
24+
25+ class RecentFilesManager (QObject ):
26+ """Manage a list of recent files."""
27+ open_file = Signal (str ) # Signal to open a file
28+
29+ def __init__ (self , max_files = 10 ):
30+ super ().__init__ ()
31+ self .max_files = max_files
32+ # TODO: link together with other settings, i.e. move settings to mc
33+ self .settings = QSettings ("PEtab_GUI" , "PEtab_GUI" )
34+ self .recent_files = self .load_recent_files ()
35+ self .tool_bar_menu = QMenu ("Recent Files" )
36+ self .update_tool_bar_menu ()
37+
38+ def add_file (self , file_path ):
39+ """Add a file to the recent files list."""
40+ if file_path in self .recent_files :
41+ self .recent_files .remove (file_path )
42+ self .recent_files .insert (0 , file_path )
43+ self .recent_files = self .recent_files [:self .max_files ]
44+ self .save_recent_files ()
45+ self .update_tool_bar_menu ()
46+
47+ def load_recent_files (self ):
48+ """Load recent files from settings."""
49+ return self .settings .value ("recent_files" , [])
50+
51+ def save_recent_files (self ):
52+ """Save recent files to settings."""
53+ self .settings .setValue ("recent_files" , self .recent_files )
54+
55+ def update_tool_bar_menu (self ):
56+ """Create a menu for the tool bar."""
57+ self .tool_bar_menu .clear ()
58+ for idx , file_path in enumerate (self .recent_files ):
59+ action = QAction (file_path , self .tool_bar_menu )
60+ action .triggered .connect (lambda : self .open_file .emit (file_path ))
61+ self .tool_bar_menu .addAction (action )
62+ self .tool_bar_menu .addSeparator ()
63+ clear_action = QAction ("Clear Recent Files" , self .tool_bar_menu )
64+ clear_action .triggered .connect (self .clear_recent_files )
65+
66+ def clear_recent_files (self ):
67+ """Clear the recent files list."""
68+ self .recent_files = []
69+ self .save_recent_files ()
70+ self .update_tool_bar_menu ()
0 commit comments