mirror of
https://gitlab.com/manzerbredes/ochess.git
synced 2025-04-30 17:07:45 +00:00
Cleaning code
This commit is contained in:
parent
d6e8d80a8f
commit
53090ab2a3
11 changed files with 87 additions and 119 deletions
|
@ -1,18 +1,13 @@
|
|||
#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) {
|
||||
BaseGameTab::BaseGameTab(wxFrame *parent, std::shared_ptr<GameBase> base)
|
||||
: TabBase_TabGames(parent),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);
|
||||
|
||||
|
@ -28,15 +23,6 @@ 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();
|
||||
}
|
||||
|
||||
void BaseGameTab::OnDelete(wxCommandEvent &event) {
|
||||
for(auto i: glm->GetSelectedItems()){
|
||||
deleted.push_back(glm->GetItemGameId(i));
|
||||
|
@ -44,56 +30,25 @@ void BaseGameTab::OnDelete(wxCommandEvent &event) {
|
|||
}
|
||||
}
|
||||
|
||||
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::shared_ptr<Game> BaseGameTab::OpenGame(long gameid, long item) {
|
||||
if(edited.find(gameid) != edited.end()){
|
||||
// TODO: Focus on the game tab and if close reopen it
|
||||
wxLogDebug("Already opened!");
|
||||
}
|
||||
else {
|
||||
std::shared_ptr<Game> g = base->GetGame(gameid);
|
||||
if(g){
|
||||
edited[gameid]=g;
|
||||
deleted.push_back(gameid);
|
||||
glm->MarkItemAsOpen(item);
|
||||
return g;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
||||
std::vector<std::shared_ptr<Game>> BaseGameTab::GetEditedGames(){
|
||||
std::vector<std::shared_ptr<Game>> games;
|
||||
for(auto it = edited.begin(); it != edited.end(); it++){
|
||||
|
@ -123,21 +78,5 @@ void BaseGameTab::Reset(std::shared_ptr<GameBase> base){
|
|||
}
|
||||
}
|
||||
|
||||
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;
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -3,30 +3,25 @@
|
|||
#include "gamebase/PGNGameBase.hpp"
|
||||
#include "GameListManager.hpp"
|
||||
|
||||
// Foreign events
|
||||
wxDECLARE_EVENT(OPEN_GAME_EVENT, wxCommandEvent);
|
||||
wxDECLARE_EVENT(CLOSE_LINKED_TAB, wxCommandEvent);
|
||||
|
||||
class BaseGameTab : public TabBase_TabGames {
|
||||
std::shared_ptr<GameBase> base;
|
||||
/// @brief Old deleted games id
|
||||
std::vector<std::uint32_t> deleted;
|
||||
/// @brief Old edited game id+object
|
||||
std::unordered_map<long, std::shared_ptr<Game>> edited;
|
||||
TabInfos *main_tab;
|
||||
|
||||
public:
|
||||
std::shared_ptr<GameListManager> glm;
|
||||
|
||||
BaseGameTab(wxFrame *parent, std::shared_ptr<GameBase> base, TabInfos *main_tab);
|
||||
BaseGameTab(wxFrame *parent, std::shared_ptr<GameBase> base);
|
||||
|
||||
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> OpenGame(long gameid, long item);
|
||||
|
||||
std::shared_ptr<Game> GetGame() { return (std::shared_ptr<Game>(NULL)); }
|
||||
std::shared_ptr<GameBase> GetBase() { return (std::shared_ptr<GameBase>(base)); };
|
||||
};
|
|
@ -8,8 +8,7 @@ BaseTab::BaseTab(wxFrame *parent, std::string base_file)
|
|||
base=OpenDatabase(base_file);
|
||||
|
||||
// Games tab
|
||||
games_tab=new BaseGameTab((wxFrame *)notebook,base,this);
|
||||
glm=games_tab->glm;
|
||||
games_tab=new BaseGameTab((wxFrame *)notebook,base);
|
||||
notebook->AddPage(games_tab, "Games list",true); // true for selecting the tab
|
||||
// Import tab
|
||||
import_tab=new BaseImportTab((wxFrame *)notebook,base,this);
|
||||
|
@ -22,21 +21,22 @@ BaseTab::BaseTab(wxFrame *parent, std::string base_file)
|
|||
Refresh();
|
||||
|
||||
// Bindings
|
||||
this->Bind(OPEN_GAME_EVENT, &BaseTab::OnOpenGame, this, wxID_ANY);
|
||||
this->Bind(wxEVT_BUTTON, &BaseTab::OnSave, this, ID_SAVE_BUTTON);
|
||||
this->Bind(wxEVT_LIST_ITEM_ACTIVATED, &BaseTab::OnOpenGame, this, ID_TABGAMES_GAME_LIST);
|
||||
}
|
||||
|
||||
|
||||
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::OnOpenGame(wxListEvent &event){
|
||||
long gameid=std::stol(event.GetItem().GetText().ToStdString());
|
||||
std::shared_ptr<Game> g = games_tab->OpenGame(gameid,event.GetIndex());
|
||||
if(g){
|
||||
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::Refresh(){
|
||||
|
|
|
@ -6,7 +6,9 @@
|
|||
#include "BaseImportTab.hpp"
|
||||
#include "BaseManageTab.hpp"
|
||||
|
||||
// Foreign events
|
||||
wxDECLARE_EVENT(NEW_GAME_EVENT, wxCommandEvent);
|
||||
wxDECLARE_EVENT(CLOSE_LINKED_TAB, wxCommandEvent);
|
||||
|
||||
class BaseTab : public TabBase, public TabInfos {
|
||||
/// @brief The opened database
|
||||
|
@ -19,10 +21,12 @@ class BaseTab : public TabBase, public TabInfos {
|
|||
BaseImportTab *import_tab;
|
||||
BaseManageTab *manage_tab;
|
||||
|
||||
/// @brief Database file path
|
||||
std::string base_file;
|
||||
std::shared_ptr<GameListManager> glm;
|
||||
|
||||
void OnOpenGame(wxCommandEvent &event);
|
||||
/// @brief Listen events from BaseImportTab
|
||||
void OnOpenGame(wxListEvent &event);
|
||||
/// @brief Listen events from BaseManageTab
|
||||
void OnSave(wxCommandEvent &event);
|
||||
|
||||
public:
|
||||
|
|
|
@ -74,6 +74,21 @@ void GameListManager::SortBy(short col){
|
|||
case 1:
|
||||
std::sort(rows.begin(),rows.end(), [&](RType a, RType b){return(a.White < b.White);});
|
||||
break;
|
||||
case 2:
|
||||
std::sort(rows.begin(),rows.end(), [&](RType a, RType b){return(a.Black < b.Black);});
|
||||
break;
|
||||
case 3:
|
||||
std::sort(rows.begin(),rows.end(), [&](RType a, RType b){return(a.Event < b.Event);});
|
||||
break;
|
||||
case 4:
|
||||
std::sort(rows.begin(),rows.end(), [&](RType a, RType b){return(a.Round < b.Round);});
|
||||
break;
|
||||
case 5:
|
||||
std::sort(rows.begin(),rows.end(), [&](RType a, RType b){return(a.Result < b.Result);});
|
||||
break;
|
||||
case 6:
|
||||
std::sort(rows.begin(),rows.end(), [&](RType a, RType b){return(a.Eco < b.Eco);});
|
||||
break;
|
||||
default:
|
||||
std::sort(rows.begin(),rows.end(), [&](RType a, RType b){return(a.id < b.id);});
|
||||
}
|
||||
|
|
|
@ -10,8 +10,10 @@
|
|||
#define BG_IMPORT(INDEX) game_list->SetItemBackgroundColour(INDEX, *wxBLUE)
|
||||
#define DISPLAY_ALL_ROWS() {for(int i=0;i<rows.size();i++){DisplayRow(i);}}
|
||||
|
||||
///@brief Column content type
|
||||
typedef std::string CType;
|
||||
|
||||
///@brief Row item content
|
||||
typedef struct Item {
|
||||
long id;
|
||||
CType White;
|
||||
|
@ -22,6 +24,10 @@ typedef struct Item {
|
|||
CType Eco;
|
||||
} RType;
|
||||
|
||||
/**
|
||||
* @brief A manager for wxListCtrl that display games
|
||||
*
|
||||
*/
|
||||
class GameListManager {
|
||||
long game_counter;
|
||||
wxListCtrl *game_list;
|
||||
|
@ -30,17 +36,25 @@ class GameListManager {
|
|||
void DisplayRow(long id);
|
||||
void ClearDisplayedRow();
|
||||
public:
|
||||
/// @brief Accessible outside (DO NOT MODIFY FROM OUTSIDE)
|
||||
std::vector<RType> rows;
|
||||
|
||||
GameListManager(wxListCtrl *game_list);
|
||||
/// @brief Add a game to the list
|
||||
long AddGame(CType White,CType Black,CType Event,CType Round, CType Result, CType Eco);
|
||||
void Clear();
|
||||
void MarkItemAsOpen(long item);
|
||||
void MarkItemAsDeleted(long item);
|
||||
void MarkItemAsImported(long item);
|
||||
/// @brief Clear the state of the GameListManager
|
||||
void Clear();
|
||||
/// @brief Return the id of the selected items
|
||||
std::vector<long> GetSelectedItems();
|
||||
/// @brief Get the game id from the item id
|
||||
long GetItemGameId(long item);
|
||||
/// @brief Filter the rows given terms
|
||||
void Filter(std::string terms);
|
||||
/// @brief Remove all filters
|
||||
void ClearFilter();
|
||||
/// @brief Sort items by the given column
|
||||
void SortBy(short col);
|
||||
};
|
|
@ -4,7 +4,7 @@
|
|||
std::shared_ptr<GameBase> OpenDatabase(const std::string &dbpath, bool createIfNotExist){
|
||||
wxFileName file(dbpath);
|
||||
wxString ext = file.GetExt().Lower();
|
||||
bool create=createIfNotExist && !file.Exists();
|
||||
bool create=(createIfNotExist && !file.Exists());
|
||||
if (ext == "pgn") {
|
||||
if(create)
|
||||
PGNGameBase::CreateDatabaseFile(dbpath);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue