From d298c59206ccb8c3e511fad9884e1cb0bd98b793 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Sat, 24 Dec 2022 22:30:20 +0100 Subject: [PATCH] Update db management --- src/MainWindow.cpp | 21 ++++++++++++++++----- src/MainWindow.hpp | 2 ++ src/base_tab/BaseGameTab.cpp | 24 +++++++++++++++++++----- src/base_tab/BaseGameTab.hpp | 6 ++++-- src/base_tab/BaseTab.cpp | 2 +- 5 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 9ab8d00..deb2b8b 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -9,6 +9,8 @@ wxDEFINE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent); wxDEFINE_EVENT(NEW_GAME_EVENT, wxCommandEvent); wxDEFINE_EVENT(CLOSE_TAB_EVENT, wxCommandEvent); wxDEFINE_EVENT(REFRESH_ENGINE_LIST, wxCommandEvent); +wxDEFINE_EVENT(CLOSE_LINKED_TAB, wxCommandEvent); + /// ---------- MainWindow ---------- @@ -50,6 +52,7 @@ MainWindow::MainWindow() Bind(CLOSE_TAB_EVENT, &MainWindow::OnCloseTabEvent, this, wxID_ANY); Bind(wxEVT_MENU, &MainWindow::OnMenuItemClick, this, wxID_ANY); Bind(REFRESH_ENGINE_LIST, &MainWindow::OnRefreshEngineList, this, wxID_ANY); + Bind(CLOSE_LINKED_TAB, &MainWindow::OnCloseTabLinkedTo, this, wxID_ANY); // Add new game tab by default NewGame(std::shared_ptr(new Game())); @@ -69,6 +72,11 @@ void MainWindow::OnCloseTabEvent(wxCommandEvent &event) { notebook->DeletePage(notebook->GetSelection()); } +void MainWindow::OnCloseTabLinkedTo(wxCommandEvent &event){ + TabInfos *infos=(TabInfos*)event.GetClientData(); + CloseTabLinkedTo(infos->id); +} + void MainWindow::OnMenuItemClick(wxCommandEvent &event) { std::uint32_t id = event.GetId(); if (id == wxID_EXIT) { @@ -132,8 +140,7 @@ void MainWindow::NewEngine() { try { engine = new uciadapter::UCI(path); EngineTab *bt = new EngineTab((wxWindow *)notebook, engine, path); - notebook->AddPage(bt, bt->GetLabel()); - notebook->SetSelection(notebook->GetPageIndex(bt)); + AddPage(bt,bt); } catch (...) { SHOW_DIALOG_ERROR("Could not communicate with the engine"); } @@ -173,11 +180,16 @@ void MainWindow::OnClose(wxCloseEvent &e) { } void MainWindow::CloseTabLinkedTo(long id){ - for(int i=0;iGetPageCount();i++){ + int i=0; + while(iGetPageCount()){ wxWindow *page=notebook->GetPage(i); TabInfos* infos=(TabInfos*)page->GetClientData(); if(infos->is_linked && infos->linked_id==id){ notebook->DeletePage(i); + i=0; // Restart to page 0 since notebook updated + } + else { + i++; } } } @@ -190,8 +202,7 @@ void MainWindow::OpenFile() { std::string path = openFileDialog.GetPath().ToStdString(); // Test base tab BaseTab *bt = new BaseTab((wxFrame *)notebook, path); - notebook->AddPage(bt, bt->GetLabel()); - notebook->SetSelection(notebook->GetPageIndex(bt)); + AddPage(bt,bt); } } diff --git a/src/MainWindow.hpp b/src/MainWindow.hpp index 1562a9d..f705173 100644 --- a/src/MainWindow.hpp +++ b/src/MainWindow.hpp @@ -12,6 +12,7 @@ wxDECLARE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent); wxDECLARE_EVENT(NEW_GAME_EVENT, wxCommandEvent); wxDECLARE_EVENT(CLOSE_TAB_EVENT, wxCommandEvent); wxDECLARE_EVENT(REFRESH_ENGINE_LIST, wxCommandEvent); +wxDECLARE_EVENT(CLOSE_LINKED_TAB, wxCommandEvent); class MainWindow : public MainFrame { wxPreferencesEditor *prefsEditor; @@ -29,6 +30,7 @@ class MainWindow : public MainFrame { void OnCloseTabEvent(wxCommandEvent &event); void OnRefreshEngineList(wxCommandEvent &event); void OnMenuItemClick(wxCommandEvent &event); + void OnCloseTabLinkedTo(wxCommandEvent &event); void AddPage(wxWindow* window, TabInfos* infos); void CloseTabLinkedTo(long id); public: diff --git a/src/base_tab/BaseGameTab.cpp b/src/base_tab/BaseGameTab.cpp index dafa71a..ae652cc 100644 --- a/src/base_tab/BaseGameTab.cpp +++ b/src/base_tab/BaseGameTab.cpp @@ -5,9 +5,9 @@ wxDEFINE_EVENT(OPEN_GAME_EVENT, wxCommandEvent); -BaseGameTab::BaseGameTab(wxFrame *parent, std::string base_file) +BaseGameTab::BaseGameTab(wxFrame *parent, std::string base_file, TabInfos *main_tab) : TabBase_TabGames(parent), base_file(base_file), - base(NULL) { + base(NULL),main_tab(main_tab) { game_list->InsertColumn(0, L"id", wxLIST_FORMAT_LEFT, 50); game_list->InsertColumn(1, L"White", wxLIST_FORMAT_LEFT, 200); @@ -56,11 +56,25 @@ void BaseGameTab::OnDelete(wxCommandEvent &event) { void BaseGameTab::OnSave(wxCommandEvent &event) { std::vector> new_games_bases; + + // Build edited games vector + std::vector> edited_games; + for (auto itr = edited.begin(); itr != edited.end(); itr++) { + edited_games.push_back(itr->second); + } + + // Combine new_games and edited games std::vector> new_games; new_games.insert( - new_games.end(), edited.begin(), - edited.end()); // Add edited game (since they are also deleted) + new_games.end(), edited_games.begin(), + edited_games.end()); // Add edited game (since they are also deleted) base->Save(deleted, new_games_bases, new_games); + + // CLose all opened games in this database + wxCommandEvent closeLinkedTabEvent(CLOSE_LINKED_TAB, GetId()); + closeLinkedTabEvent.SetClientData(main_tab); + ProcessEvent(closeLinkedTabEvent); + game_list->DeleteAllItems(); edited.clear(); deleted.clear(); @@ -72,7 +86,7 @@ void BaseGameTab::OnOpenGame(wxListEvent &event) { long id = std::stoi(event.GetItem().GetText().ToStdString()); std::shared_ptr *g = new std::shared_ptr(base->GetGame(id)); if (g != NULL) { - edited.push_back(*g); + edited[id]=*g; deleted.push_back(id); game_list->SetItemBackgroundColour(event.GetIndex(), *wxGREEN); wxCommandEvent openGameEvent(OPEN_GAME_EVENT, GetId()); diff --git a/src/base_tab/BaseGameTab.hpp b/src/base_tab/BaseGameTab.hpp index f93903c..4c338cf 100644 --- a/src/base_tab/BaseGameTab.hpp +++ b/src/base_tab/BaseGameTab.hpp @@ -6,15 +6,17 @@ // Foreign events wxDECLARE_EVENT(OPEN_GAME_EVENT, wxCommandEvent); wxDECLARE_EVENT(REFRESH_TAB_TITLE, wxCommandEvent); +wxDECLARE_EVENT(CLOSE_LINKED_TAB, wxCommandEvent); class BaseGameTab : public TabBase_TabGames { std::shared_ptr base; std::vector deleted; - std::vector> edited; + std::unordered_map> edited; std::string base_file; + TabInfos *main_tab; public: - BaseGameTab(wxFrame *parent, std::string base_file); + BaseGameTab(wxFrame *parent, std::string base_file, TabInfos *main_tab); void ApplyPreferences(); void LoadFile(); diff --git a/src/base_tab/BaseTab.cpp b/src/base_tab/BaseTab.cpp index a2b7207..06774b4 100644 --- a/src/base_tab/BaseTab.cpp +++ b/src/base_tab/BaseTab.cpp @@ -6,7 +6,7 @@ BaseTab::BaseTab(wxFrame *parent, std::string base_file) : TabBase(parent), TabInfos(TabInfos::BASE){ // Games tab - games_tab=new BaseGameTab((wxFrame *)notebook,base_file); + games_tab=new BaseGameTab((wxFrame *)notebook,base_file,this); notebook->AddPage(games_tab, "Games list",true); // true for selecting the tab // Import tab import_tab=new BaseImportTab((wxFrame *)notebook);