1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
#include "BaseGameTab.hpp"
#include "AppendGameDialog.hpp"
#include <wx/filename.h>
wxDEFINE_EVENT(OPEN_GAME_EVENT, wxCommandEvent);
BaseGameTab::BaseGameTab(wxFrame *parent, std::string base_file)
: TabBase_TabGames(parent), base_file(base_file),
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, &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();
game_list->DeleteAllItems();
deleted.clear();
edited.clear();
LoadFile();
}
void BaseGameTab::OnDelete(wxCommandEvent &event) {
long selected = -1;
while ((selected = game_list->GetNextItem(selected, wxLIST_NEXT_ALL,
wxLIST_STATE_SELECTED)) !=
wxNOT_FOUND) {
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 BaseGameTab::OnSave(wxCommandEvent &event) {
std::vector<std::shared_ptr<GameBase>> new_games_bases;
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)
base->Save(deleted, new_games_bases, new_games);
game_list->DeleteAllItems();
edited.clear();
deleted.clear();
LoadFile();
}
void BaseGameTab::OnOpenGame(wxListEvent &event) {
wxLogDebug("Open!");
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);
deleted.push_back(id);
game_list->SetItemBackgroundColour(event.GetIndex(), *wxGREEN);
wxCommandEvent openGameEvent(OPEN_GAME_EVENT, GetId());
openGameEvent.SetEventObject(this);
openGameEvent.SetClientData(g);
ProcessEvent(openGameEvent);
}
}
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) {
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);
}
|