I have this code: In a c++ wxwidget application, I have this custom class: #ifndef…
- User
I have this code: In a c++ wxwidget application, I have this custom class:
#ifndef GRIDSTRINGTABLEGAMES_H #define GRIDSTRINGTABLEGAMES_H
#include “Game.h” #include “CompletionStatus.h”
#include <wx/grid.h> #include <wx/arrstr.h>
class GridStringTableGames : public wxGridStringTable { public: GridStringTableGames(int cols); GridStringTableGames(const std::vector& g, int cols); virtual ~GridStringTableGames(); int GetNumberRows() override; int GetNumberCols() override; wxString GetValue(int row, int col) override; void SetValue(int row, int col, const wxString& value) override; wxString GetColLabelValue(int col) override; std::vector GetGames(); void SetGames(std::vector games); Game GetGame(int position); void SetGame(int position, Game game);
private: wxString GetCompletionStatusString(CompletionStatus status); CompletionStatus GetCompletionStatusFromString(wxString status); int GetIntFromString(wxString value);
std::vector games; int columns; };
#endif // GRIDSTRINGTABLEGAMES_H
#include “GridStringTableGames.h”
GridStringTableGames::GridStringTableGames(int cols) : GridStringTableGames(std::vector(), cols) { }
GridStringTableGames::GridStringTableGames(const std::vector& g, int cols) : games(g), columns(cols) { }
GridStringTableGames::~GridStringTableGames() { }
int GridStringTableGames::GetNumberRows() { return games.size(); }
int GridStringTableGames::GetNumberCols() { return columns; }
wxString GridStringTableGames::GetValue(int row, int col) { switch (col) { case 0: return games[row].GetName(); case 1: return games[row].GetSystem(); case 2: return games[row].GetGenre(); case 3: return GetCompletionStatusString(games[row].GetCompletetionStatus()); case 4: return wxString(std::to_string(games[row].GetFinishYear())); case 5: return wxString(std::to_string(games[row].GetStartYear())); default: return “”; } }
void GridStringTableGames::SetValue(int row, int col, const wxString& value) { switch (col) { case 0: games[row].SetName(value); break; case 1: games[row].SetSystem(value); break; case 2: games[row].SetGenre(value); break; case 3: games[row].SetCompletetionStatus(GetCompletionStatusFromString(value)); break; case 4: games[row].SetFinishYear(GetIntFromString(value)); break; case 5: games[row].SetStartYear(GetIntFromString(value)); break; } }
wxString GridStringTableGames::GetColLabelValue(int col) { switch (col) { case 0: return “Name”; case 1: return “System”; case 2: return “Genre”; case 3: return “Completion Status”; case 4: return “Finish Year”; case 5: return “Start Year”; default: return “”; } }
void GridStringTableGames::SetGames(std::vector g) { games = g; }
std::vector GridStringTableGames::GetGames() { return games; }
Game GridStringTableGames::GetGame(int position) { return games[position]; }
- Assistant
To connect your
GridStringTableGamesclass with thegridGameswxGrid and useSetCellValueto add games, you can follow these steps:- Replace the line
GridStringTableGames* table = new GridStringTableGames(6);in yourFrameMainconstructor with the following code:
GridStringTableGames* table = new GridStringTableGames(games.size(), 6); for (int i = 0; i < games.size(); i++) { table->SetGame(i, games[i]); } gridGames->SetTable(table);This creates a new instance of
GridStringTableGameswith the number of rows equal to the size of yourgamesvector, and sets the games using the method. - Replace the line
- User
no I don't want to use gridGames->SetTable(table);, change that, and I won't be using GridStringTableGames anymore
- Assistant
If you don't want to use
gridGames->SetTable(table)andGridStringTableGamesanymore, you can directly manipulate thegridGameswxGrid to add games usingSetCellValue. Here's how you can modify your code:-
Remove the
GridStringTableGamesclass and its header file. -
Replace the following lines in your
FrameMainconstructor:
GridStringTableGames* table = new GridStringTableGames(6); Game game("Mario Bros.", "NES", "Platform", CompletionStatus::Completed, 2018, 0); table->SetGame(0, game); table->SetGame(1, Game("Sonic Adventures", "Genesis", "Platform", CompletionStatus::Playing, 0, 2020)); gridGames->SetTable(table); -
- User
How to add a new row to the grid? and how to modify or delete a row?
- Assistant
To add a new row to the grid, you can use the
AppendRowsmethod. To modify or delete a row, you can use theSetCellValueorDeleteRowsmethods respectively. Here's how you can do it:To add a new row:
int numRows = gridGames->GetNumberRows(); gridGames->AppendRows(1); // Set the values for the new row gridGames->SetCellValue(numRows, 0, "New Game"); gridGames->SetCellValue(numRows, 1, "New System"); gridGames->SetCellValue(numRows, 2, "New Genre"); // ... Set values for other columnsTo modify a row (e.g., row at index 1):
gridGames->SetCellValue(1, 0, "Modified Name"); gridGames->SetCellValue(1, 1, "Modified System"); gridGames->SetCellValue(1, 2, "Modified Genre"); // ... Set values for other columnsTo delete a row (e.g., row at index 2):
gridGames->DeleteRows(2);Make sure to adjust the row index and column indices according to your specific requirements.
- User
Create a Games class that replaces GridStringTableGames,
the original class is like this:
class GridStringTableGames : public wxGridStringTable { public: GridStringTableGames(int cols); GridStringTableGames(const std::vector& g, int cols); virtual ~GridStringTableGames(); int GetNumberRows() override; int GetNumberCols() override; wxString GetValue(int row, int col) override; void SetValue(int row, int col, const wxString& value) override; wxString GetColLabelValue(int col) override; std::vector GetGames(); void SetGames(std::vector& games); Game GetGame(int position); void SetGame(int position, Game game);
private: wxString GetCompletionStatusString(CompletionStatus status); CompletionStatus GetCompletionStatusFromString(wxString status); int GetIntFromString(wxString value); std::vector<Game> games; int columns;};
Make it so it can easily work with the wxGrid, maybe using a pointer, so that way it is easier to add games and also adding the row to the game, connecting efectively the vector to the grid, the same way an observable collection work in c#
- Assistant
- User
it is giving me these two errors:
- src\Games.cpp|57|error: 'wxEVT_GRIDTABLE_NOTIFY_ROWS_APPENDED' was not declared in this scope|
- src\Games.cpp|60|error: 'ProcessWindowEvent' was not declared in this scope| 3, src\FrameMain.cpp|16|error: no matching function for call to 'wxGrid::SetTable(Games*)'|
- src\Games.cpp|79|error: 'class wxGridEvent' has no member named 'SetRow'; did you mean 'GetRow'?|
- Assistant
Apologies for the errors. To resolve them, make the following updates in the code:
- In the
Gamesclass, replace thewxEVT_GRIDTABLE_NOTIFY_ROWS_APPENDEDevent withwxEVT_GRIDTABLE_NOTIFY_ROWS_APPENDED. - Replace
ProcessWindowEvent(event);withProcessEvent(event);in theAddGame,ModifyGame, andDeleteGamemethods. - Update the
SetValuemethod in theGamesclass to convert thevalueparameter tostd::stringbefore assigning it to theGameobject. Replace the existingSetValuemethod with the following:
void Games::SetValue(int row, int col, const wxString& value) { Game& game = games[row]; std::string strValue = std::string(value.mb_str()); switch (col) { case 0: game = Game(strValue, game.GetSystem(), game.GetGenre()); break; case 1: game = Game(game.GetName(), strValue, game.GetGenre()); break; case 2: game = Game(game.GetName(), game.GetSystem(), strValue); break; } } - In the