Game list can now be sorted

This commit is contained in:
Loic Guegan 2022-12-26 08:26:02 +01:00
parent 54cb4dc6c6
commit 2bd85f53bc
3 changed files with 33 additions and 18 deletions

View file

@ -59,8 +59,8 @@ MainWindow::MainWindow()
NewGame(std::shared_ptr<Game>(new Game()));
// Temporary TO REMOVE JUST FOR TESTS:
//BaseTab *bt = new BaseTab((wxFrame *)notebook, "/home/loic/Downloads/PGN.pgn");
//this->AddPage(bt,bt);
BaseTab *bt = new BaseTab((wxFrame *)notebook, "/home/loic/pgn/twic1467.pgn");
this->AddPage(bt,bt);
}
void MainWindow::AddPage(wxWindow* window, TabInfos* infos){

View file

@ -13,7 +13,7 @@ GameListManager::GameListManager(wxListCtrl *game_list): game_list(game_list), g
void GameListManager::AddGame(CType White,CType Black,CType Event,CType Round, CType Result, CType Eco){
// Update rows elements
rows.push_back({White,Black,Event,Round,Result,Eco});
rows.push_back({game_counter,White,Black,Event,Round,Result,Eco});
// Display the row
DisplayRow(game_counter);
game_counter++;
@ -22,7 +22,7 @@ void GameListManager::AddGame(CType White,CType Black,CType Event,CType Round, C
void GameListManager::DisplayRow(long id){
RType row=rows[id];
long index =
game_list->InsertItem(game_counter, std::to_string(id)); // want this for col. 1
game_list->InsertItem(game_counter, std::to_string(row.id)); // want this for col. 1
game_list->SetItem(index, 1, row.White);
game_list->SetItem(index, 2, row.Black);
game_list->SetItem(index, 3, row.Event);
@ -30,16 +30,16 @@ void GameListManager::DisplayRow(long id){
game_list->SetItem(index, 5, row.Result);
game_list->SetItem(index, 6, row.Eco);
if(std::find(opened_items.begin(), opened_items.end(), index) != opened_items.end())
MarkItemAsOpen(index);
if(std::find(deleted_items.begin(), deleted_items.end(), index) != deleted_items.end())
MarkItemAsDeleted(index);
if(std::find(opened_games.begin(), opened_games.end(), row.id) != opened_games.end())
BG_OPEN(index);
if(std::find(deleted_games.begin(), deleted_games.end(), row.id) != deleted_games.end())
BG_DELETE(index);
}
void GameListManager::Clear(){
game_list->DeleteAllItems();
deleted_items.clear();
opened_items.clear();
deleted_games.clear();
opened_games.clear();
rows.clear();
}
@ -48,13 +48,25 @@ void GameListManager::ClearDisplayedRow(){
}
void GameListManager::MarkItemAsOpen(long item){
opened_items.push_back(item);
game_list->SetItemBackgroundColour(item, *wxGREEN);
opened_games.push_back(rows[item].id);
BG_OPEN(item);
}
void GameListManager::MarkItemAsDeleted(long item){
deleted_items.push_back(item);
game_list->SetItemBackgroundColour(item, *wxRED);
deleted_games.push_back(rows[item].id);
BG_DELETE(item);
}
void GameListManager::SortBy(short col){
ClearDisplayedRow();
switch(col){
case 1:
std::sort(rows.begin(),rows.end(), [&](RType a, RType b){return(a.White < b.White);});
break;
default:
std::sort(rows.begin(),rows.end(), [&](RType a, RType b){return(a.id < b.id);});
}
DISPLAY_ALL_ROWS();
}
std::vector<long> GameListManager::GetSelectedItems(){
@ -94,8 +106,6 @@ void GameListManager::Filter(std::string terms){
void GameListManager::ClearFilter() {
ClearDisplayedRow();
for(int i=0;i<rows.size();i++){
DisplayRow(i);
}
DISPLAY_ALL_ROWS();
}

View file

@ -5,10 +5,14 @@
#include <vector>
#define TERMS_IN(COL) (row.COL.find(terms) != std::string::npos)
#define BG_OPEN(INDEX) game_list->SetItemBackgroundColour(INDEX, *wxGREEN)
#define BG_DELETE(INDEX) game_list->SetItemBackgroundColour(INDEX, *wxRED)
#define DISPLAY_ALL_ROWS() {for(int i=0;i<rows.size();i++){DisplayRow(i);}}
typedef std::string CType;
typedef struct Item {
long id;
CType White;
CType Black;
CType Event;
@ -21,7 +25,7 @@ class GameListManager {
wxListCtrl *game_list;
long game_counter;
std::vector<RType> rows;
std::vector<long> deleted_items, opened_items;
std::vector<long> deleted_games, opened_games;
void DisplayRow(long id);
void ClearDisplayedRow();
@ -35,4 +39,5 @@ public:
long GetItemGameId(long item);
void Filter(std::string terms);
void ClearFilter();
void SortBy(short col);
};