Update db management

This commit is contained in:
Loic Guegan 2022-12-24 22:30:20 +01:00
parent 418b2e7f60
commit d298c59206
5 changed files with 42 additions and 13 deletions

View file

@ -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<std::shared_ptr<GameBase>> new_games_bases;
// Build edited games vector
std::vector<std::shared_ptr<Game>> 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<std::shared_ptr<Game>> 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<Game> *g = new std::shared_ptr<Game>(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());

View file

@ -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<GameBase> base;
std::vector<std::uint32_t> deleted;
std::vector<std::shared_ptr<Game>> edited;
std::unordered_map<long, std::shared_ptr<Game>> 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();

View file

@ -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);