mirror of
				https://github.com/SoftFever/OrcaSlicer.git
				synced 2025-10-30 12:11:15 -06:00 
			
		
		
		
	#2616 - Added Recent projects item to File menu
This commit is contained in:
		
							parent
							
								
									cc70c8dff9
								
							
						
					
					
						commit
						de88db5918
					
				
					 6 changed files with 98 additions and 3 deletions
				
			
		|  | @ -229,6 +229,33 @@ std::string AppConfig::get_last_dir() const | |||
|     return std::string(); | ||||
| } | ||||
| 
 | ||||
| std::vector<std::string> AppConfig::get_recent_projects() const | ||||
| { | ||||
|     std::vector<std::string> ret; | ||||
|     const auto it = m_storage.find("recent_projects"); | ||||
|     if (it != m_storage.end()) | ||||
|     { | ||||
|         for (const std::map<std::string, std::string>::value_type& item : it->second) | ||||
|         { | ||||
|             ret.push_back(item.second); | ||||
|         } | ||||
|     } | ||||
|     return ret; | ||||
| } | ||||
| 
 | ||||
| void AppConfig::set_recent_projects(const std::vector<std::string>& recent_projects) | ||||
| { | ||||
|     auto it = m_storage.find("recent_projects"); | ||||
|     if (it == m_storage.end()) | ||||
|         it = m_storage.insert(std::map<std::string, std::map<std::string, std::string>>::value_type("recent_projects", std::map<std::string, std::string>())).first; | ||||
| 
 | ||||
|     it->second.clear(); | ||||
|     for (unsigned int i = 0; i < (unsigned int)recent_projects.size(); ++i) | ||||
|     { | ||||
|         it->second[std::to_string(i + 1)] = recent_projects[i]; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void AppConfig::update_config_dir(const std::string &dir) | ||||
| { | ||||
|     this->set("recent", "config_directory", dir); | ||||
|  |  | |||
|  | @ -122,6 +122,9 @@ public: | |||
| 	// Does the config file exist?
 | ||||
| 	static bool 		exists(); | ||||
| 
 | ||||
|     std::vector<std::string> get_recent_projects() const; | ||||
|     void set_recent_projects(const std::vector<std::string>& recent_projects); | ||||
| 
 | ||||
| private: | ||||
| 	// Map of section, name -> value
 | ||||
| 	std::map<std::string, std::map<std::string, std::string>> 	m_storage; | ||||
|  |  | |||
|  | @ -35,6 +35,7 @@ namespace GUI { | |||
| MainFrame::MainFrame() : | ||||
| DPIFrame(NULL, wxID_ANY, "", wxDefaultPosition, wxDefaultSize, wxDEFAULT_FRAME_STYLE, "mainframe"), | ||||
|     m_printhost_queue_dlg(new PrintHostQueueDialog(this)) | ||||
|     , m_recent_projects(9) | ||||
| { | ||||
|     // Fonts were created by the DPIFrame constructor for the monitor, on which the window opened.
 | ||||
|     wxGetApp().update_fonts(this); | ||||
|  | @ -383,6 +384,40 @@ void MainFrame::init_menubar() | |||
|         append_menu_item(fileMenu, wxID_ANY, _(L("&Open Project")) + dots + "\tCtrl+O", _(L("Open a project file")), | ||||
|             [this](wxCommandEvent&) { if (m_plater) m_plater->load_project(); }, menu_icon("open"), nullptr, | ||||
|             [this](){return m_plater != nullptr; }, this); | ||||
| 
 | ||||
|         wxMenu* recent_projects_menu = new wxMenu(); | ||||
|         wxMenuItem* recent_projects_submenu = append_submenu(fileMenu, recent_projects_menu, wxID_ANY, _(L("Recent projects")), ""); | ||||
|         m_recent_projects.UseMenu(recent_projects_menu); | ||||
|         Bind(wxEVT_MENU, [this](wxCommandEvent& evt) { | ||||
|             size_t file_id = evt.GetId() - wxID_FILE1; | ||||
|             wxString filename = m_recent_projects.GetHistoryFile(file_id); | ||||
|             if (wxFileExists(filename)) | ||||
|                 m_plater->load_project(filename); | ||||
|             else | ||||
|             { | ||||
|                 wxMessageDialog msg(this, _(L("The selected project is no more available")), _(L("Error"))); | ||||
|                 msg.ShowModal(); | ||||
| 
 | ||||
|                 m_recent_projects.RemoveFileFromHistory(file_id); | ||||
|                 std::vector<std::string> recent_projects; | ||||
|                 size_t count = m_recent_projects.GetCount(); | ||||
|                 for (size_t i = 0; i < count; ++i) | ||||
|                 { | ||||
|                     recent_projects.push_back(into_u8(m_recent_projects.GetHistoryFile(i))); | ||||
|                 } | ||||
|                 wxGetApp().app_config->set_recent_projects(recent_projects); | ||||
|                 wxGetApp().app_config->save(); | ||||
|             } | ||||
|             }, wxID_FILE1, wxID_FILE9); | ||||
| 
 | ||||
|         std::vector<std::string> recent_projects = wxGetApp().app_config->get_recent_projects(); | ||||
|         for (const std::string& project : recent_projects) | ||||
|         { | ||||
|             m_recent_projects.AddFileToHistory(from_u8(project)); | ||||
|         } | ||||
| 
 | ||||
|         Bind(wxEVT_UPDATE_UI, [this](wxUpdateUIEvent& evt) { evt.Enable(m_recent_projects.GetCount() > 0); }, recent_projects_submenu->GetId()); | ||||
| 
 | ||||
|         append_menu_item(fileMenu, wxID_ANY, _(L("&Save Project")) + "\tCtrl+S", _(L("Save current project file")), | ||||
|             [this](wxCommandEvent&) { if (m_plater) m_plater->export_3mf(into_path(m_plater->get_project_filename(".3mf"))); }, menu_icon("save"), nullptr, | ||||
|             [this](){return m_plater != nullptr && can_save(); }, this); | ||||
|  | @ -1046,6 +1081,23 @@ void MainFrame::on_config_changed(DynamicPrintConfig* config) const | |||
|         m_plater->on_config_change(*config); // propagate config change events to the plater
 | ||||
| } | ||||
| 
 | ||||
| void MainFrame::add_to_recent_projects(const wxString& filename) | ||||
| { | ||||
|     if (wxFileExists(filename)) | ||||
|     { | ||||
|         m_recent_projects.AddFileToHistory(filename); | ||||
|         std::vector<std::string> recent_projects; | ||||
|         size_t count = m_recent_projects.GetCount(); | ||||
|         for (size_t i = 0; i < count; ++i) | ||||
|         { | ||||
|             recent_projects.push_back(into_u8(m_recent_projects.GetHistoryFile(i))); | ||||
|         } | ||||
|         wxGetApp().app_config->set_recent_projects(recent_projects); | ||||
|         wxGetApp().app_config->save(); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| //
 | ||||
| // Called after the Preferences dialog is closed and the program settings are saved.
 | ||||
| // Update the UI based on the current preferences.
 | ||||
| void MainFrame::update_ui_from_settings() | ||||
|  |  | |||
|  | @ -6,6 +6,7 @@ | |||
| #include <wx/frame.h> | ||||
| #include <wx/settings.h> | ||||
| #include <wx/string.h> | ||||
| #include <wx/filehistory.h> | ||||
| 
 | ||||
| #include <string> | ||||
| #include <map> | ||||
|  | @ -84,6 +85,8 @@ class MainFrame : public DPIFrame | |||
|     // vector of a MenuBar items changeable in respect to printer technology 
 | ||||
|     std::vector<wxMenuItem*> m_changeable_menu_items; | ||||
| 
 | ||||
|     wxFileHistory m_recent_projects; | ||||
| 
 | ||||
| protected: | ||||
|     virtual void on_dpi_changed(const wxRect &suggested_rect); | ||||
| 
 | ||||
|  | @ -121,6 +124,8 @@ public: | |||
|     // Propagate changed configuration from the Tab to the Platter and save changes to the AppConfig
 | ||||
|     void        on_config_changed(DynamicPrintConfig* cfg) const ; | ||||
| 
 | ||||
|     void        add_to_recent_projects(const wxString& filename); | ||||
| 
 | ||||
|     PrintHostQueueDialog* printhost_queue_dlg() { return m_printhost_queue_dlg; } | ||||
| 
 | ||||
|     Plater*             m_plater { nullptr }; | ||||
|  |  | |||
|  | @ -3277,6 +3277,9 @@ void Plater::priv::set_project_filename(const wxString& filename) | |||
| 
 | ||||
|     m_project_filename = from_path(full_path); | ||||
|     wxGetApp().mainframe->update_title(); | ||||
| 
 | ||||
|     if (!filename.empty()) | ||||
|         wxGetApp().mainframe->add_to_recent_projects(filename); | ||||
| } | ||||
| 
 | ||||
| bool Plater::priv::init_common_menu(wxMenu* menu, const bool is_part/* = false*/) | ||||
|  | @ -3590,15 +3593,19 @@ void Plater::load_project() | |||
| { | ||||
|     wxString input_file; | ||||
|     wxGetApp().load_project(this, input_file); | ||||
|     load_project(input_file); | ||||
| } | ||||
| 
 | ||||
|     if (input_file.empty()) | ||||
| void Plater::load_project(const wxString& filename) | ||||
| { | ||||
|     if (filename.empty()) | ||||
|         return; | ||||
| 
 | ||||
|     p->reset(); | ||||
|     p->set_project_filename(input_file); | ||||
|     p->set_project_filename(filename); | ||||
| 
 | ||||
|     std::vector<fs::path> input_paths; | ||||
|     input_paths.push_back(into_path(input_file)); | ||||
|     input_paths.push_back(into_path(filename)); | ||||
|     load_files(input_paths); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -138,6 +138,7 @@ public: | |||
| 
 | ||||
|     void new_project(); | ||||
|     void load_project(); | ||||
|     void load_project(const wxString& filename); | ||||
|     void add_model(); | ||||
|     void extract_config_from_project(); | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Enrico Turri
						Enrico Turri