mirror of
https://gitlab.com/manzerbredes/ochess.git
synced 2025-04-07 02:26:29 +02:00
143 lines
4.1 KiB
C++
143 lines
4.1 KiB
C++
#include "BaseGameTab.hpp"
|
|
#include <wx/filename.h>
|
|
|
|
wxDEFINE_EVENT(OPEN_GAME_EVENT, wxCommandEvent);
|
|
|
|
|
|
BaseGameTab::BaseGameTab(wxFrame *parent, std::shared_ptr<GameBase> base, TabInfos *main_tab)
|
|
: TabBase_TabGames(parent), main_tab(main_tab),base(base) {
|
|
|
|
glm=std::make_shared<GameListManager>(game_list);
|
|
Reset(base);
|
|
|
|
|
|
this->Bind(wxEVT_BUTTON, &BaseGameTab::OnDelete, this, ID_DELETE_BUTTON);
|
|
this->Bind(wxEVT_LIST_ITEM_ACTIVATED, &BaseGameTab::OnOpenGame, this, wxID_ANY);
|
|
this->Bind(wxEVT_BUTTON, &BaseGameTab::OnApplyFilter, this, ID_APPLY_FILTER_BUTTON);
|
|
this->Bind(wxEVT_TEXT_ENTER, &BaseGameTab::OnApplyFilter, this, ID_SEARCH_TERMS);
|
|
|
|
search_terms->SetHint("e.g: Paul Morphy");
|
|
}
|
|
|
|
void BaseGameTab::OnApplyFilter(wxCommandEvent &event){
|
|
wxString terms=search_terms->GetValue();
|
|
if(terms.length()>0){
|
|
glm->Filter(terms.ToStdString());
|
|
} else {
|
|
glm->ClearFilter();
|
|
}
|
|
}
|
|
|
|
void BaseGameTab::OnImport(wxCommandEvent &event) {
|
|
// AppendGameDialog *dia = new AppendGameDialog(this, base);
|
|
// dia->ShowModal();
|
|
// glm->Clear();
|
|
// deleted.clear();
|
|
// edited.clear();
|
|
// LoadFile();
|
|
}
|
|
|
|
void BaseGameTab::OnDelete(wxCommandEvent &event) {
|
|
for(auto i: glm->GetSelectedItems()){
|
|
deleted.push_back(glm->GetItemGameId(i));
|
|
glm->MarkItemAsDeleted(i);
|
|
}
|
|
}
|
|
|
|
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_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);
|
|
|
|
// glm->Clear();
|
|
// 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));
|
|
wxLogDebug("kjkj");
|
|
|
|
if (g != NULL) {
|
|
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);
|
|
glm->MarkItemAsOpen(event.GetIndex());
|
|
wxCommandEvent openGameEvent(OPEN_GAME_EVENT, GetId());
|
|
openGameEvent.SetEventObject(this);
|
|
openGameEvent.SetClientData(g);
|
|
ProcessEvent(openGameEvent);
|
|
}
|
|
}
|
|
}
|
|
|
|
std::vector<std::shared_ptr<Game>> BaseGameTab::GetEditedGames(){
|
|
std::vector<std::shared_ptr<Game>> games;
|
|
for(auto it = edited.begin(); it != edited.end(); it++){
|
|
games.push_back(it->second);
|
|
}
|
|
return(games);
|
|
}
|
|
|
|
void BaseGameTab::Reset(std::shared_ptr<GameBase> base){
|
|
glm->Clear();
|
|
edited.clear();
|
|
deleted.clear();
|
|
|
|
// Load all games (for now :)
|
|
SHOW_DIALOG_BUSY("Loading database...");
|
|
this->base=base;
|
|
if (base != NULL) {
|
|
while (base->NextGame()) {
|
|
glm->AddGame(
|
|
base->GetTag("White"),
|
|
base->GetTag("Black"),
|
|
base->GetTag("Event"),
|
|
base->GetTag("Round"),
|
|
base->GetTag("Result"),
|
|
base->GetTag("ECO"));
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
// }
|
|
// }
|
|
}
|
|
|
|
|