Debug database tab

This commit is contained in:
Loic Guegan 2022-12-26 12:51:48 +01:00
parent 2bd85f53bc
commit 1d78e106ad
15 changed files with 315 additions and 528 deletions

View file

@ -5,23 +5,19 @@
wxDEFINE_EVENT(OPEN_GAME_EVENT, wxCommandEvent);
BaseGameTab::BaseGameTab(wxFrame *parent, std::string base_file, TabInfos *main_tab)
: TabBase_TabGames(parent), base_file(base_file),
base(NULL),main_tab(main_tab) {
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);
glm=new GameListManager(game_list);
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);
this->Bind(wxEVT_BUTTON, &BaseGameTab::OnApplyFilter, this, ID_APPLY_FILTER_BUTTON);
this->Bind(wxEVT_TEXT_ENTER, &BaseGameTab::OnApplyFilter, this, ID_SEARCH_TERMS);
current_base->SetLabel(base_file);
search_terms->SetHint("e.g: Paul Morphy");
LoadFile();
}
void BaseGameTab::OnApplyFilter(wxCommandEvent &event){
@ -34,12 +30,12 @@ void BaseGameTab::OnApplyFilter(wxCommandEvent &event){
}
void BaseGameTab::OnImport(wxCommandEvent &event) {
AppendGameDialog *dia = new AppendGameDialog(this, base);
dia->ShowModal();
glm->Clear();
deleted.clear();
edited.clear();
LoadFile();
// AppendGameDialog *dia = new AppendGameDialog(this, base);
// dia->ShowModal();
// glm->Clear();
// deleted.clear();
// edited.clear();
// LoadFile();
}
void BaseGameTab::OnDelete(wxCommandEvent &event) {
@ -50,35 +46,37 @@ void BaseGameTab::OnDelete(wxCommandEvent &event) {
}
void BaseGameTab::OnSave(wxCommandEvent &event) {
std::vector<std::shared_ptr<GameBase>> new_games_bases;
// 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);
}
// // 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);
// // 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);
// // 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();
// 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
@ -97,33 +95,21 @@ void BaseGameTab::OnOpenGame(wxListEvent &event) {
}
}
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;
}
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::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)");
}
void BaseGameTab::Reset(std::shared_ptr<GameBase> base){
glm->Clear();
edited.clear();
deleted.clear();
// Load all games (for now :)
this->base=base;
if (base != NULL) {
while (base->NextGame()) {
glm->AddGame(
@ -135,8 +121,23 @@ void BaseGameTab::LoadFile() {
base->GetTag("ECO"));
}
}
wxCommandEvent event(REFRESH_TAB_TITLE, GetId());
event.SetEventObject(this);
ProcessEvent(event);
}
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;
// }
// }
}

View file

@ -5,29 +5,28 @@
// 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::unordered_map<long, std::shared_ptr<Game>> edited;
std::string base_file;
TabInfos *main_tab;
GameListManager *glm;
public:
BaseGameTab(wxFrame *parent, std::string base_file, TabInfos *main_tab);
~BaseGameTab() {delete(glm);};
std::shared_ptr<GameListManager> glm;
void ApplyPreferences();
void LoadFile();
BaseGameTab(wxFrame *parent, std::shared_ptr<GameBase> base, TabInfos *main_tab);
void Reset(std::shared_ptr<GameBase> base);
void OnDelete(wxCommandEvent &event);
void OnSave(wxCommandEvent &event);
void OnExport(wxCommandEvent &event);
void OnOpenGame(wxListEvent &event);
void OnImport(wxCommandEvent &event);
void OnApplyFilter(wxCommandEvent &event);
std::vector<std::shared_ptr<Game>> GetEditedGames();
std::vector<std::uint32_t> GetDeletedGameIds() {return(deleted);};
std::shared_ptr<Game> GetGame() { return (std::shared_ptr<Game>(NULL)); }
std::shared_ptr<GameBase> GetBase() { return (std::shared_ptr<GameBase>(base)); };
};

View file

@ -1,9 +1,25 @@
#include "BaseManageTab.hpp"
#define ADD_INFO(text) {informations->WriteText(text);informations->WriteText("\n");}
BaseManageTab::BaseManageTab(wxFrame *parent):
TabBase_TabManage(parent)
BaseManageTab::BaseManageTab(wxFrame *parent, std::shared_ptr<GameBase> db, std::shared_ptr<GameListManager> glm):
TabBase_TabManage(parent), glm(glm), base(db)
{
RefreshInformations();
}
void BaseManageTab::RefreshInformations(){
informations->Clear();
wxFileName base_path(base->GetFilePath());
ADD_INFO("Database Path: "+base_path.GetFullPath());
ADD_INFO("File Size: "+base_path.GetHumanReadableSize());
ADD_INFO("Last Modified: "+base_path.GetModificationTime().Format());
ADD_INFO("Database Format: "+base->GetFormat());
ADD_INFO("Total Number of Games: "+std::to_string(glm->rows.size()));
}
void BaseManageTab::Reset(std::shared_ptr<GameBase> db) {
this->base=db;
RefreshInformations();
}

View file

@ -1,10 +1,15 @@
#include "ochess.hpp"
#include "GameListManager.hpp"
#include "gamebase/GameBase.hpp"
class BaseManageTab : public TabBase_TabManage {
/// @brief Never free the following pointer in that class
std::shared_ptr<GameListManager> glm;
std::shared_ptr<GameBase> base;
public:
BaseManageTab(wxFrame *parent);
BaseManageTab(wxFrame *parent, std::shared_ptr<GameBase> db, std::shared_ptr<GameListManager> glm);
void RefreshInformations();
void Reset(std::shared_ptr<GameBase> db);
};

View file

@ -3,34 +3,68 @@
#include <wx/filename.h>
BaseTab::BaseTab(wxFrame *parent, std::string base_file)
: TabBase(parent), TabInfos(TabInfos::BASE){
: TabBase(parent), TabInfos(TabInfos::BASE), base_file(base_file){
// First open the database
OpenDatabase(base_file);
// Games tab
games_tab=new BaseGameTab((wxFrame *)notebook,base_file,this);
games_tab=new BaseGameTab((wxFrame *)notebook,base,this);
notebook->AddPage(games_tab, "Games list",true); // true for selecting the tab
// Import tab
import_tab=new BaseImportTab((wxFrame *)notebook,this);
notebook->AddPage(import_tab, "Import games");
// Manage tab
manage_tab=new BaseManageTab((wxFrame *)notebook);
manage_tab=new BaseManageTab((wxFrame *)notebook, base, games_tab->glm);
notebook->AddPage(manage_tab, "Manage database");
RefreshLabel();
this->Bind(OPEN_GAME_EVENT, &BaseTab::OnNewGame, this, wxID_ANY);
// Refresh dynamic elements of the database (tab title, available db for import etc.)
Refresh();
// Bindings
this->Bind(OPEN_GAME_EVENT, &BaseTab::OnOpenGame, this, wxID_ANY);
this->Bind(wxEVT_BUTTON, &BaseTab::OnSave, this, ID_SAVE_BUTTON);
}
void BaseTab::OnNewGame(wxCommandEvent &event){
void BaseTab::OnOpenGame(wxCommandEvent &event){
std::shared_ptr<Game> *g = (std::shared_ptr<Game>*)event.GetClientData();
this->game=*g;
// Ask MainFrame to open a new game
// TODO: Simplify that is, use wxWidget main app to do it
wxCommandEvent newGameEvent(NEW_GAME_EVENT, GetId());
newGameEvent.SetEventObject(this);
newGameEvent.SetClientData((TabInfos*)this);
ProcessEvent(newGameEvent);
}
void BaseTab::ApplyPreferences() {}
void BaseTab::Refresh(){
import_tab->RefreshImportLists();
SetLabel(wxFileName(base->GetFilePath()).GetName()+" [DB]"); // Propagated to MainWindow tab title automatically by wxWidget
}
void BaseTab::RefreshLabel(){
SetLabel("Database XX");
void BaseTab::OpenDatabase(std::string dbpath) {
wxFileName file(dbpath);
wxString ext = file.GetExt().Lower();
if (ext == "pgn") {
base.reset();
base = std::shared_ptr<GameBase>(new PGNGameBase(dbpath));
}
}
void BaseTab::OnSave(wxCommandEvent &event) {
std::vector<std::shared_ptr<GameBase>> dummy_empty_base;
base->Save(games_tab->GetDeletedGameIds(), dummy_empty_base, games_tab->GetEditedGames());
// Close all opened games in this database
wxCommandEvent closeLinkedTabEvent(CLOSE_LINKED_TAB, GetId());
closeLinkedTabEvent.SetClientData((TabInfos*)this);
ProcessEvent(closeLinkedTabEvent);
// Reopen the saved database
OpenDatabase(base_file);
games_tab->Reset(base);
manage_tab->Reset(base);
}

View file

@ -7,21 +7,27 @@
wxDECLARE_EVENT(NEW_GAME_EVENT, wxCommandEvent);
class BaseTab : public TabBase, public TabInfos {
/// @brief The opened database
std::shared_ptr<GameBase> base;
/// @brief The last opened game
std::shared_ptr<Game> game;
/// All sub tabs
BaseGameTab *games_tab;
BaseImportTab *import_tab;
BaseManageTab *manage_tab;
void OnNewGame(wxCommandEvent &event);
std::string base_file;
void OnOpenGame(wxCommandEvent &event);
void OnSave(wxCommandEvent &event);
void OpenDatabase(std::string dbpath);
public:
BaseTab(wxFrame *parent, std::string base_file);
void ApplyPreferences();
void RefreshLabel();
void Refresh();
std::shared_ptr<Game> GetGame() { return (std::shared_ptr<Game>(game)); }
std::shared_ptr<GameBase> GetBase() { return (std::shared_ptr<GameBase>(base)); };
void Refresh() {import_tab->RefreshImportLists();};
};

View file

@ -41,6 +41,7 @@ void GameListManager::Clear(){
deleted_games.clear();
opened_games.clear();
rows.clear();
game_counter=0;
}
void GameListManager::ClearDisplayedRow(){

View file

@ -22,14 +22,15 @@ typedef struct Item {
} RType;
class GameListManager {
wxListCtrl *game_list;
long game_counter;
std::vector<RType> rows;
wxListCtrl *game_list;
std::vector<long> deleted_games, opened_games;
void DisplayRow(long id);
void ClearDisplayedRow();
public:
std::vector<RType> rows;
GameListManager(wxListCtrl *game_list);
void AddGame(CType White,CType Black,CType Event,CType Round, CType Result, CType Eco);
void Clear();

View file

@ -14,6 +14,8 @@ public:
virtual bool NextGame() = 0;
virtual std::string GetTag(std::string tag) = 0;
virtual void Reset() = 0;
virtual std::string GetFormat() = 0;
virtual std::string GetFilePath() = 0;
/**
* @brief Save the given base into current base format (export)
*

View file

@ -18,6 +18,8 @@ public:
std::vector<std::shared_ptr<Game>> new_games);
void Reset();
void Export(std::shared_ptr<GameBase> base);
std::string GetFormat() {return("PGN");};
std::string GetFilePath() {return(file);};
static std::string GetMovesPGN(HalfMove *m, bool needDots);
static std::string GetPGN(std::shared_ptr<Game> g);
};