From 3263bda23b8bccb9958471be064603d958f08253 Mon Sep 17 00:00:00 2001 From: Loic Guegan Date: Mon, 26 Dec 2022 16:23:14 +0100 Subject: Improve game import in db --- src/base_tab/BaseImportTab.cpp | 74 +++++++++++++++++++++++++++++++++++++++--- src/base_tab/BaseImportTab.hpp | 16 +++++++-- src/base_tab/BaseTab.cpp | 4 ++- src/base_tab/BaseTab.hpp | 2 ++ 4 files changed, 89 insertions(+), 7 deletions(-) (limited to 'src/base_tab') diff --git a/src/base_tab/BaseImportTab.cpp b/src/base_tab/BaseImportTab.cpp index 9b2f541..6395fdf 100644 --- a/src/base_tab/BaseImportTab.cpp +++ b/src/base_tab/BaseImportTab.cpp @@ -1,14 +1,38 @@ #include "BaseImportTab.hpp" +#include +#include "gamebase/GameBase.hpp" -BaseImportTab::BaseImportTab(wxFrame *parent, TabInfos *main_tab): -TabBase_TabImport(parent), main_tab(main_tab) +BaseImportTab::BaseImportTab(wxFrame *parent, std::shared_ptr db, TabInfos *main_tab): +TabBase_TabImport(parent), main_tab(main_tab), base(db) { - glm=new GameListManager(game_list); + glm=std::make_shared(game_list); RefreshImportLists(); + RefreshPendingImports(); this->Bind(wxEVT_BUTTON, &BaseImportTab::OnLoad, this, ID_LOAD_BUTTON); + this->Bind(wxEVT_BUTTON, &BaseImportTab::OnImportGame, this, ID_IMPORT_GAME_BUTTON); + this->Bind(wxEVT_BUTTON, &BaseImportTab::OnImportSelection, this, ID_IMPORT_SELECTION); + this->Bind(wxEVT_BUTTON, &BaseImportTab::OnImportDatabase, this, ID_IMPORT_DB); + opened_db_list->SetHint("No other database open"); } +void BaseImportTab::OnImportDatabase(wxCommandEvent &event){ + if(std::find(databases_to_import.begin(), databases_to_import.end(), selected_base) == databases_to_import.end()){ + databases_to_import.push_back(selected_base); + RefreshPendingImports(); + } + else SHOW_DIALOG_INFO("Database already prepared for import"); +} + +void BaseImportTab::RefreshPendingImports(){ + int ngames=games_to_import.size(); + int ndb=databases_to_import.size(); + if(ngames+ndb>0){ + pending_imports->SetLabel(" Pending imports: "+std::to_string(ngames)+" games and "+std::to_string(ndb)+" databases"); + }else + pending_imports->SetLabel(""); +} + void BaseImportTab::RefreshImportLists(){ opened_game_list->Clear(); opened_db_list->Clear(); @@ -26,6 +50,48 @@ void BaseImportTab::RefreshImportLists(){ } } +void BaseImportTab::OnImportSelection(wxCommandEvent &event){ + long selected = -1; + while ((selected = game_list->GetNextItem(selected, wxLIST_NEXT_ALL, + wxLIST_STATE_SELECTED)) != + wxNOT_FOUND) { + games_to_import.push_back(selected_base->GetGame(glm->GetItemGameId(selected))); + } + RefreshPendingImports(); +} + +void BaseImportTab::OnImportGame(wxCommandEvent &event){ + TabInfos *game_tab=(TabInfos*)opened_game_list->GetClientData(opened_game_list->GetSelection()); + std::shared_ptr g=game_tab->GetGame(); + if(std::find(games_to_import.begin(), games_to_import.end(), g) == games_to_import.end()){ wxLogDebug("Import!"); + games_to_import.push_back(g); + RefreshPendingImports(); + } + else SHOW_DIALOG_INFO("Game already prepared for import"); +} + void BaseImportTab::OnLoad(wxCommandEvent &event){ -wxLogDebug("Load!"); + TabInfos *game_tab=(TabInfos*)opened_db_list->GetClientData(opened_db_list->GetSelection()); + selected_base.reset(); + selected_base=game_tab->GetBase(); + glm->Clear(); + + // Load all games (for now :) + selected_base->Reset(); + while (selected_base->NextGame()) { + glm->AddGame( + selected_base->GetTag("White"), + selected_base->GetTag("Black"), + selected_base->GetTag("Event"), + selected_base->GetTag("Round"), + selected_base->GetTag("Result"), + selected_base->GetTag("ECO")); + } +} + +void BaseImportTab::Reset(std::shared_ptr base){ + this->base=base; + this->games_to_import.clear(); + this->databases_to_import.clear(); + glm->Clear(); } diff --git a/src/base_tab/BaseImportTab.hpp b/src/base_tab/BaseImportTab.hpp index a301f9b..a3a19de 100644 --- a/src/base_tab/BaseImportTab.hpp +++ b/src/base_tab/BaseImportTab.hpp @@ -1,12 +1,24 @@ +#pragma once + #include "ochess.hpp" #include "GameListManager.hpp" +#include "game_tab/Game.hpp" class BaseImportTab : public TabBase_TabImport { TabInfos *main_tab; - GameListManager *glm; + std::shared_ptr glm; + std::vector> games_to_import; + std::vector> databases_to_import; + std::shared_ptr base; + std::shared_ptr selected_base; + void RefreshPendingImports(); public: - BaseImportTab(wxFrame *parent, TabInfos *main_tab); + BaseImportTab(wxFrame *parent, std::shared_ptr db, TabInfos *main_tab); void RefreshImportLists(); void OnLoad(wxCommandEvent &event); + void OnImportGame(wxCommandEvent &event); + void OnImportSelection(wxCommandEvent &event); + void OnImportDatabase(wxCommandEvent &event); + void Reset(std::shared_ptr base); }; \ No newline at end of file diff --git a/src/base_tab/BaseTab.cpp b/src/base_tab/BaseTab.cpp index 05ff907..c13caae 100644 --- a/src/base_tab/BaseTab.cpp +++ b/src/base_tab/BaseTab.cpp @@ -10,9 +10,10 @@ BaseTab::BaseTab(wxFrame *parent, std::string base_file) // Games tab games_tab=new BaseGameTab((wxFrame *)notebook,base,this); + glm=games_tab->glm; notebook->AddPage(games_tab, "Games list",true); // true for selecting the tab // Import tab - import_tab=new BaseImportTab((wxFrame *)notebook,this); + import_tab=new BaseImportTab((wxFrame *)notebook,base,this); notebook->AddPage(import_tab, "Import games"); // Manage tab manage_tab=new BaseManageTab((wxFrame *)notebook, base, games_tab->glm); @@ -67,4 +68,5 @@ void BaseTab::OnSave(wxCommandEvent &event) { OpenDatabase(base_file); games_tab->Reset(base); manage_tab->Reset(base); + import_tab->Reset(base); } \ No newline at end of file diff --git a/src/base_tab/BaseTab.hpp b/src/base_tab/BaseTab.hpp index c8efd11..5b21353 100644 --- a/src/base_tab/BaseTab.hpp +++ b/src/base_tab/BaseTab.hpp @@ -1,3 +1,4 @@ +#pragma once #include "gamebase/GameBase.hpp" #include "ochess.hpp" @@ -19,6 +20,7 @@ class BaseTab : public TabBase, public TabInfos { BaseManageTab *manage_tab; std::string base_file; + std::shared_ptr glm; void OnOpenGame(wxCommandEvent &event); void OnSave(wxCommandEvent &event); -- cgit v1.2.3