ochess/src/base_tab/BaseGameTab.cpp

131 lines
3.8 KiB
C++
Raw Normal View History

2022-12-23 16:49:33 +01:00
#include "BaseGameTab.hpp"
#include "AppendGameDialog.hpp"
#include <wx/filename.h>
2022-12-24 12:46:59 +01:00
wxDEFINE_EVENT(OPEN_GAME_EVENT, wxCommandEvent);
2022-12-24 22:30:20 +01:00
BaseGameTab::BaseGameTab(wxFrame *parent, std::string base_file, TabInfos *main_tab)
2022-12-23 16:49:33 +01:00
: TabBase_TabGames(parent), base_file(base_file),
2022-12-24 22:30:20 +01:00
base(NULL),main_tab(main_tab) {
2022-12-23 16:49:33 +01:00
2022-12-25 15:26:16 +01:00
glm=new GameListManager(game_list);
2022-12-23 16:49:33 +01:00
this->Bind(wxEVT_BUTTON, &BaseGameTab::OnDelete, this, ID_DELETE_BUTTON);
this->Bind(wxEVT_BUTTON, &BaseGameTab::OnSave, this, ID_SAVE_BUTTON);
this->Bind(wxEVT_BUTTON, &BaseGameTab::OnExport, this, ID_EXPORT_BUTTON);
this->Bind(wxEVT_LIST_ITEM_ACTIVATED, &BaseGameTab::OnOpenGame, this, wxID_ANY);
this->Bind(wxEVT_BUTTON, &BaseGameTab::OnImport, this, ID_IMPORT_BUTTON);
current_base->SetLabel(base_file);
LoadFile();
}
void BaseGameTab::OnImport(wxCommandEvent &event) {
AppendGameDialog *dia = new AppendGameDialog(this, base);
dia->ShowModal();
2022-12-25 15:51:00 +01:00
glm->Clear();
2022-12-23 16:49:33 +01:00
deleted.clear();
edited.clear();
LoadFile();
}
void BaseGameTab::OnDelete(wxCommandEvent &event) {
2022-12-25 17:30:22 +01:00
for(auto i: glm->GetSelectedItems()){
deleted.push_back(glm->GetItemGameId(i));
glm->MarkItemAsDeleted(i);
2022-12-23 16:49:33 +01:00
}
}
void BaseGameTab::OnSave(wxCommandEvent &event) {
std::vector<std::shared_ptr<GameBase>> new_games_bases;
2022-12-24 22:30:20 +01:00
// 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
2022-12-23 16:49:33 +01:00
std::vector<std::shared_ptr<Game>> new_games;
new_games.insert(
2022-12-24 22:30:20 +01:00
new_games.end(), edited_games.begin(),
edited_games.end()); // Add edited game (since they are also deleted)
2022-12-23 16:49:33 +01:00
base->Save(deleted, new_games_bases, new_games);
2022-12-24 22:30:20 +01:00
2022-12-25 09:57:34 +01:00
// Close all opened games in this database
2022-12-24 22:30:20 +01:00
wxCommandEvent closeLinkedTabEvent(CLOSE_LINKED_TAB, GetId());
closeLinkedTabEvent.SetClientData(main_tab);
ProcessEvent(closeLinkedTabEvent);
2022-12-25 15:51:00 +01:00
glm->Clear();
2022-12-23 16:49:33 +01:00
edited.clear();
deleted.clear();
LoadFile();
}
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) {
2022-12-25 09:57:34 +01:00
if(edited.find(id) != edited.end()){
// TODO: Focus on the game tab and if close reopen it
wxLogDebug("Already opened!");
}
else {
wxLogDebug("Open game");
edited[id]=*g;
deleted.push_back(id);
2022-12-25 17:30:22 +01:00
glm->MarkItemAsOpen(event.GetIndex());
2022-12-25 09:57:34 +01:00
wxCommandEvent openGameEvent(OPEN_GAME_EVENT, GetId());
openGameEvent.SetEventObject(this);
openGameEvent.SetClientData(g);
ProcessEvent(openGameEvent);
}
2022-12-23 16:49:33 +01:00
}
}
void BaseGameTab::ApplyPreferences() {}
void BaseGameTab::OnExport(wxCommandEvent &event) {
wxFileDialog openFileDialog(this, _("Export database"), "", "",
"Database files (*.pgn)|*.pgn",
wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
if (openFileDialog.ShowModal() != wxID_CANCEL) {
std::string path = openFileDialog.GetPath().ToStdString();
wxFileName file(base_file);
wxString ext = file.GetExt().Lower();
GameBase *base;
if (ext == "pgn") {
base = new PGNGameBase(path);
base->Export(this->base);
delete base;
}
}
}
void BaseGameTab::LoadFile() {
wxFileName file(base_file);
wxString ext = file.GetExt().Lower();
if (ext == "pgn") {
base = std::shared_ptr<GameBase>(new PGNGameBase(base_file));
SetLabel(file.GetName() + "(PGN)");
}
if (base != NULL) {
while (base->NextGame()) {
2022-12-25 15:26:16 +01:00
glm->AddGame(
base->GetTag("White"),
base->GetTag("Black"),
base->GetTag("Event"),
base->GetTag("Round"),
base->GetTag("Result"),
base->GetTag("ECO"));
2022-12-23 16:49:33 +01:00
}
}
wxCommandEvent event(REFRESH_TAB_TITLE, GetId());
event.SetEventObject(this);
ProcessEvent(event);
}