In c++ wxwidgets, I am trying to make a move functions for a wxgrid, so it gets the old position and the new position and move the row data to the new position. So far it works perfectly for one item but when I try to add a new item the order gets wrong, help me solve this issue.
This is how it works right now: A - B - C - D, I move D to 0, the output is: D - A - B - C, but when I move B to 1 the output is wrong: C - B - D - A, so the first position is wrong now, but the newly added is correct.
This is my code:
void MoveRow(wxGrid* grid, int rowPos, int newRowPos)
{
grid->InsertRows(newRowPos);
code
for (int col = 0; col < grid->GetNumberCols(); col++)
{
grid->SetCellValue(newRowPos, col, grid->GetCellValue(rowPos + 1, col));
}
grid->DeleteRows(rowPos + 1);
if (rowValues.Count() >= 1)
{
for (size_t i = 0; i < rowValues.Count(); i++)
{
int row = games->GetGameRow(rowValues[i]);
std::vector<int>::iterator it = std::find(pinnedRows.begin(), pinnedRows.end(), row);
if (it == pinnedRows.end())
{
pinnedRows.push_back(row);
}
else
{
pinnedRows.erase(it);
}
}
SetPinnedRows();
}
}
void FrameMain::SetPinnedRows()
{
for (size_t i = 0; i < pinnedRows.size(); i++)
{
Utils::MoveRow(gridGames, pinnedRows[i], i);
}
code
SetPinnedRowsColor();
}
Assistant
The issue in your code is that the order of the pinnedRows vector is not being updated correctly when you move a row. As a result, the order of the pinned rows in the grid becomes incorrect.
To solve this issue, you need to update the pinnedRows vector whenever a row is moved. Here's an updated version of your code that maintains the correct order:
User
can you update the pinnedrows vector in SetPinnedRows instead, so the moverows can be re-used later without the vector?
Assistant
Certainly! Here's an updated version of the code that updates the pinnedRows vector in the SetPinnedRows function:
cpp
User
this is wrong, pinnedrows vector contains the rows not the position, the position is set by the index, so pinnedrows[0] means the position 0 of the row.
Assistant
Apologies for the confusion. If the pinnedRows vector contains the rows and not the positions, we can update the MoveRow function to also update the pinnedRows vector accordingly. Here's the updated code:
cpp
User
no it is still wrong, the problem is this: The grid has a row order that never changes, so the pinnedRows vector contains these rows index of which rows are pinned. So when I add a new pinned row, I get the row index and store it in pinnedrows JUST to move around because the pinned rows should be at the top of the grid, but the grid rows doesn't change the numbers are still the same, take this into account when solving my problem, and if you have to modify everything do it so I can pin and get the pinned rows on the top
Assistant
I apologize for the previous misunderstandings. Based on your clarification, it seems like the pinnedRows vector contains the row indices of the pinned rows, and the objective is to move the pinned rows to the top of the grid while preserving the order of the remaining rows. Here's an updated solution that addresses this requirement:
cpp
void MoveRow(wxGrid* grid, int rowPos, int newRowPos, std::vector<int>& pinnedRows)
{
grid->InsertRows(newRowPos);
if (newRowPos < rowPos) {
rowPos++;
}
for (int col = 0; col < grid->GetNumberCols(); col++)
{
grid->SetCellValue(newRowPos, col, grid->GetCellValue(rowPos, col));
}
grid->DeleteRows(rowPos);
// Update pinnedRows vector
for (size_t i = 0; i < pinnedRows.size(); i++)
{
if (pinnedRows[i] == rowPos)
{
pinnedRows[i] = newRowPos;
break;
}
}
}
void FrameMain::OnPinRows(wxCommandEvent& event)
{
wxArrayString rowValues = Utils::GetSelectedRowValues(gridGames, 0);
if (rowValues.Count() >= 1)
{
for (size_t i = 0; i < rowValues.Count(); i++)
{
int row = games->GetGameRow(rowValues[i]);
std::vector<int>::iterator it = std::find(pinnedRows.begin(), pinnedRows.end(), row);
if (it == pinnedRows.end())
{
pinnedRows.push_back(row);
}
else
{
pinnedRows.erase(it);
}
}
SetPinnedRows();
}
}
void FrameMain::SetPinnedRows()
{
std::sort(pinnedRows.begin(), pinnedRows.end());
for (size_t i = 0; i < pinnedRows.size(); i++)
{
Utils::MoveRow(gridGames, pinnedRows[i], i, pinnedRows);
}
SetPinnedRowsColor();
}
In the MoveRow function, I added an additional parameter std::vector<int>& pinnedRows which is a reference to the pinnedRows vector. Inside the function, after moving the row, I update the pinnedRows vector to reflect the new order.
In the SetPinnedRows function, I added a call to std::sort to ensure that the pinned rows are in the correct order before calling the MoveRow function.
By updating the pinnedRows vector correctly, the order of the pinned rows in the grid should be maintained properly.
void MoveRow(wxGrid* grid, int rowPos, int newRowPos)
{
grid->InsertRows(newRowPos);
if (newRowPos < rowPos) {
rowPos++;
}
for (int col = 0; col < grid->GetNumberCols(); col++)
{
grid->SetCellValue(newRowPos, col, grid->GetCellValue(rowPos, col));
}
grid->DeleteRows(rowPos);
}
void FrameMain::OnPinRows(wxCommandEvent& event)
{
wxArrayString rowValues = Utils::GetSelectedRowValues(gridGames, 0);
if (rowValues.Count() >= 1)
{
for (size_t i = 0; i < rowValues.Count(); i++)
{
int row = games->GetGameRow(rowValues[i]);
std::vector<int>::iterator it = std::find(pinnedRows.begin(), pinnedRows.end(), row);
if (it == pinnedRows.end())
{
pinnedRows.push_back(row);
}
else
{
pinnedRows.erase(it);
}
}
SetPinnedRows();
}
}
void FrameMain::SetPinnedRows()
{
std::sort(pinnedRows.begin(), pinnedRows.end());
std::vector<int> newPinnedRows;
for (size_t i = 0; i < pinnedRows.size(); i++)
{
int rowPos = pinnedRows[i];
Utils::MoveRow(gridGames, rowPos, i);
newPinnedRows.push_back(i);
}
pinnedRows = newPinnedRows;
SetPinnedRowsColor();
}
In the SetPinnedRows function, I added a new vector newPinnedRows to store the updated order of the pinned rows. After calling the MoveRow function, I add the new position (i.e., i) to the newPinnedRows vector. Finally, I update the pinnedRows vector to newPinnedRows to reflect the new order of the pinned rows.
By updating the pinnedRows vector in the SetPinnedRows function, the MoveRow function can be reused without relying on the vector.
void MoveRow(wxGrid* grid, int rowPos, int newRowPos, std::vector<int>& pinnedRows)
{
grid->InsertRows(newRowPos);
if (newRowPos < rowPos) {
rowPos++;
}
for (int col = 0; col < grid->GetNumberCols(); col++)
{
grid->SetCellValue(newRowPos, col, grid->GetCellValue(rowPos, col));
}
grid->DeleteRows(rowPos);
// Update pinnedRows vector
int row = pinnedRows[rowPos];
pinnedRows.erase(pinnedRows.begin() + rowPos);
pinnedRows.insert(pinnedRows.begin() + newRowPos, row);
}
void FrameMain::OnPinRows(wxCommandEvent& event)
{
wxArrayString rowValues = Utils::GetSelectedRowValues(gridGames, 0);
if (rowValues.Count() >= 1)
{
for (size_t i = 0; i < rowValues.Count(); i++)
{
int row = games->GetGameRow(rowValues[i]);
std::vector<int>::iterator it = std::find(pinnedRows.begin(), pinnedRows.end(), row);
if (it == pinnedRows.end())
{
pinnedRows.push_back(row);
}
else
{
pinnedRows.erase(it);
}
}
SetPinnedRows();
}
}
void FrameMain::SetPinnedRows()
{
std::sort(pinnedRows.begin(), pinnedRows.end());
for (size_t i = 0; i < pinnedRows.size(); i++)
{
int rowPos = games->GetGameRow(pinnedRows[i]);
Utils::MoveRow(gridGames, rowPos, i, pinnedRows);
}
SetPinnedRowsColor();
}
In the MoveRow function, I updated the code to update the pinnedRows vector based on the row positions. First, I store the value of pinnedRows[rowPos] in a variable row. Then, I remove the element at rowPos from the pinnedRows vector using the erase function. Finally, I insert the row value at the newRowPos using the insert function.
By updating the pinnedRows vector according to the row positions, the order of the pinnedRows vector will be maintained correctly when moving rows.
cpp
void MoveRow(wxGrid* grid, int rowPos, int newRowPos)
{
// Move the row in the grid
for (int col = 0; col < grid->GetNumberCols(); col++)
{
grid->SetCellValue(newRowPos, col, grid->GetCellValue(rowPos, col));
}
if (newRowPos < rowPos) {
rowPos++;
}
grid->DeleteRows(rowPos);
}
void FrameMain::OnPinRows(wxCommandEvent& event)
{
wxArrayString rowValues = Utils::GetSelectedRowValues(gridGames, 0);
if (rowValues.Count() >= 1)
{
for (size_t i = 0; i < rowValues.Count(); i++)
{
int row = games->GetGameRow(rowValues[i]);
// If the row is not already pinned, add it to the pinnedRows vector
if(std::find(pinnedRows.begin(), pinnedRows.end(), row) == pinnedRows.end())
{
pinnedRows.push_back(row);
}
}
SetPinnedRows();
}
}
void FrameMain::SetPinnedRows()
{
// Sort the pinnedRows vector in ascending order
std::sort(pinnedRows.begin(), pinnedRows.end());
// Move the pinned rows to the top of the grid
for (size_t i = 0; i < pinnedRows.size(); i++)
{
int rowPos = pinnedRows[i];
Utils::MoveRow(gridGames, rowPos, i);
}
// Update the pinnedRows vector with the new row indices
for (size_t i = 0; i < pinnedRows.size(); i++)
{
pinnedRows[i] = i;
}
SetPinnedRowsColor();
}
In the MoveRow function, I have removed the grid->InsertRows(newRowPos); and modified the logic accordingly to move the specified row to the new position while shifting other rows if necessary.
In the OnPinRows function, I have made a small change to only add the row index to the pinnedRows vector if it is not already included.
In the SetPinnedRows function, after moving the pinned rows to the top of the grid, I update the pinnedRows vector to contain the new row indices based on their positions in the grid.
This should ensure that the pinned rows remain at the top of the grid and maintain the correct order.