mirror of
https://gitlab.com/manzerbredes/ochess.git
synced 2025-04-07 02:26:29 +02:00
112 lines
No EOL
3.7 KiB
C++
112 lines
No EOL
3.7 KiB
C++
#include "BaseTab.hpp"
|
|
#include <wx/filename.h>
|
|
|
|
BaseTab::BaseTab(wxFrame *parent, std::string base_file)
|
|
: BasePanelBF(parent), base_file(base_file), TabInfos(TabInfos::BASE),
|
|
base(NULL) {
|
|
|
|
game_list->InsertColumn(0, L"id", wxLIST_FORMAT_LEFT, 50);
|
|
game_list->InsertColumn(1, L"White", wxLIST_FORMAT_LEFT, 200);
|
|
game_list->InsertColumn(2, L"Black", wxLIST_FORMAT_LEFT, 200);
|
|
game_list->InsertColumn(3, L"Event", wxLIST_FORMAT_LEFT, 150);
|
|
game_list->InsertColumn(4, L"Round", wxLIST_FORMAT_LEFT, 100);
|
|
game_list->InsertColumn(5, L"Result", wxLIST_FORMAT_LEFT, 200);
|
|
game_list->InsertColumn(6, L"ECO", wxLIST_FORMAT_LEFT, 200);
|
|
|
|
this->Bind(wxEVT_BUTTON, &BaseTab::OnDelete, this, ID_DELETE_BUTTON);
|
|
this->Bind(wxEVT_BUTTON, &BaseTab::OnSave, this, ID_SAVE_BUTTON);
|
|
this->Bind(wxEVT_BUTTON, &BaseTab::OnExport, this, ID_EXPORT_BUTTON);
|
|
this->Bind(wxEVT_LIST_ITEM_ACTIVATED, &BaseTab::OnOpenGame, this, wxID_ANY);
|
|
current_base->SetLabel(base_file);
|
|
LoadFile();
|
|
}
|
|
|
|
void BaseTab::OnDelete(wxCommandEvent &event) {
|
|
long selected =
|
|
game_list->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
|
|
if (selected >= 0) {
|
|
wxListItem listItem;
|
|
listItem.m_itemId = selected; // sets row
|
|
listItem.m_col = 0; // sets column
|
|
game_list->GetItem(listItem); // gets item
|
|
deleted.push_back(std::stoi(listItem.GetText().ToStdString()));
|
|
for (std::uint32_t &i : deleted) {
|
|
wxLogDebug("%d", i);
|
|
}
|
|
game_list->SetItemBackgroundColour(selected, *wxRED);
|
|
}
|
|
}
|
|
|
|
void BaseTab::OnSave(wxCommandEvent &event) {
|
|
std::vector<GameBase *> new_games_bases;
|
|
std::vector<Game *> new_games;
|
|
new_games.insert(
|
|
new_games.end(), edited.begin(),
|
|
edited.end()); // Add edited game (since they are also deleted)
|
|
base->Save(deleted, new_games_bases, new_games);
|
|
game_list->DeleteAllItems();
|
|
deleted.clear();
|
|
LoadFile();
|
|
}
|
|
|
|
void BaseTab::OnOpenGame(wxListEvent &event) {
|
|
wxLogDebug("Open!");
|
|
long id = std::stoi(event.GetItem().GetText().ToStdString());
|
|
Game *g = base->GetGame(id);
|
|
if (g != NULL) {
|
|
edited.push_back(g);
|
|
deleted.push_back(id);
|
|
game_list->SetItemBackgroundColour(event.GetIndex(), *wxGREEN);
|
|
wxCommandEvent newGameEvent(NEW_GAME_EVENT, GetId());
|
|
newGameEvent.SetEventObject(this);
|
|
newGameEvent.SetClientData(g);
|
|
ProcessEvent(newGameEvent);
|
|
}
|
|
}
|
|
|
|
void BaseTab::ApplyPreferences() {}
|
|
|
|
void BaseTab::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->Save(this->base);
|
|
delete base;
|
|
}
|
|
}
|
|
}
|
|
|
|
void BaseTab::LoadFile() {
|
|
wxFileName file(base_file);
|
|
wxString ext = file.GetExt().Lower();
|
|
if (ext == "pgn") {
|
|
base = new PGNGameBase(base_file);
|
|
SetLabel(file.GetName() + "(PGN)");
|
|
}
|
|
|
|
if (base != NULL) {
|
|
long id = 0;
|
|
while (base->NextGame()) {
|
|
long index =
|
|
game_list->InsertItem(0, std::to_string(id)); // want this for col. 1
|
|
game_list->SetItem(index, 1, base->GetTag("White"));
|
|
game_list->SetItem(index, 2, base->GetTag("Black"));
|
|
game_list->SetItem(index, 3, base->GetTag("Event"));
|
|
game_list->SetItem(index, 4, base->GetTag("Round"));
|
|
game_list->SetItem(index, 5, base->GetTag("Result"));
|
|
game_list->SetItem(index, 6, base->GetTag("ECO"));
|
|
id++;
|
|
}
|
|
}
|
|
|
|
wxCommandEvent event(REFRESH_TAB_TITLE, GetId());
|
|
event.SetEventObject(this);
|
|
ProcessEvent(event);
|
|
} |