Refactor wxGridSelection to store selection as blocks only

Store all types of selection with an array of blocks instead of arrays of
cells, blocks, rows and columns.

It (hopefully) simplifies the code and allows us to implement editing of
the last selection block much easier.
This commit is contained in:
Ilya Sinitsyn
2020-03-03 00:09:50 +07:00
committed by Vadim Zeitlin
parent d1c8bba2b6
commit 02509cbc39
3 changed files with 381 additions and 946 deletions

View File

@@ -17,6 +17,10 @@
#include "wx/grid.h" #include "wx/grid.h"
#include "wx/vector.h"
typedef wxVector<wxGridBlockCoords> wxVectorGridBlockCoords;
class WXDLLIMPEXP_CORE wxGridSelection class WXDLLIMPEXP_CORE wxGridSelection
{ {
public: public:
@@ -48,16 +52,6 @@ public:
kbd, sendEvent); kbd, sendEvent);
} }
void SelectCell(int row, int col,
const wxKeyboardState& kbd = wxKeyboardState(),
bool sendEvent = true);
void SelectCell(const wxGridCellCoords& coords,
const wxKeyboardState& kbd = wxKeyboardState(),
bool sendEvent = true)
{
SelectCell(coords.GetRow(), coords.GetCol(), kbd, sendEvent);
}
void ToggleCellSelection(int row, int col, void ToggleCellSelection(int row, int col,
const wxKeyboardState& kbd = wxKeyboardState()); const wxKeyboardState& kbd = wxKeyboardState());
void ToggleCellSelection(const wxGridCellCoords& coords, void ToggleCellSelection(const wxGridCellCoords& coords,
@@ -66,35 +60,20 @@ public:
ToggleCellSelection(coords.GetRow(), coords.GetCol(), kbd); ToggleCellSelection(coords.GetRow(), coords.GetCol(), kbd);
} }
void DeselectBlock(const wxGridBlockCoords& block,
const wxKeyboardState& kbd = wxKeyboardState(),
bool sendEvent = true );
void ClearSelection(); void ClearSelection();
void UpdateRows( size_t pos, int numRows ); void UpdateRows( size_t pos, int numRows );
void UpdateCols( size_t pos, int numCols ); void UpdateCols( size_t pos, int numCols );
const wxGridCellCoordsArray& GetCellSelection() const wxGridCellCoordsArray GetCellSelection() const;
{ wxGridCellCoordsArray GetBlockSelectionTopLeft() const;
return m_cellSelection; wxGridCellCoordsArray GetBlockSelectionBottomRight() const;
} wxArrayInt GetRowSelection() const;
wxArrayInt GetColSelection() const;
const wxGridCellCoordsArray& GetBlockSelectionTopLeft() const
{
return m_blockSelectionTopLeft;
}
const wxGridCellCoordsArray& GetBlockSelectionBottomRight() const
{
return m_blockSelectionBottomRight;
}
const wxArrayInt& GetRowSelection() const
{
return m_rowSelection;
}
const wxArrayInt& GetColSelection() const
{
return m_colSelection;
}
private: private:
int BlockContain( int topRow1, int leftCol1, int BlockContain( int topRow1, int leftCol1,
@@ -105,28 +84,27 @@ private:
// -1, if Block2 contains Block1, // -1, if Block2 contains Block1,
// 0, otherwise // 0, otherwise
int BlockContainsCell( int topRow, int leftCol, void SelectBlockNoEvent(const wxGridBlockCoords& block)
int bottomRow, int rightCol,
int row, int col )
// returns 1, if Block contains Cell,
// 0, otherwise
{ {
return ( topRow <= row && row <= bottomRow && SelectBlock(block.GetTopRow(), block.GetLeftCol(),
leftCol <= col && col <= rightCol ); block.GetBottomRow(), block.GetRightCol(),
}
void SelectBlockNoEvent(int topRow, int leftCol,
int bottomRow, int rightCol)
{
SelectBlock(topRow, leftCol, bottomRow, rightCol,
wxKeyboardState(), false); wxKeyboardState(), false);
} }
wxGridCellCoordsArray m_cellSelection; // Really select the block and don't check for the current selection mode.
wxGridCellCoordsArray m_blockSelectionTopLeft; void Select(const wxGridBlockCoords& block,
wxGridCellCoordsArray m_blockSelectionBottomRight; const wxKeyboardState& kbd, bool sendEvent);
wxArrayInt m_rowSelection;
wxArrayInt m_colSelection; // If the new block containing any of the passed blocks, remove them.
// if a new block contained in the passed blockc, return.
// Otherwise add the new block to the blocks array.
void MergeOrAddBlock(wxVectorGridBlockCoords& blocks,
const wxGridBlockCoords& block);
// The vector of selection blocks. We expect that the users select
// relatively small amount of blocks. K-D tree can be used to speed up
// searching speed.
wxVectorGridBlockCoords m_selection;
wxGrid *m_grid; wxGrid *m_grid;
wxGrid::wxGridSelectionModes m_selectionMode; wxGrid::wxGridSelectionModes m_selectionMode;

View File

@@ -1904,7 +1904,7 @@ public:
wxGridBlockCoords Canonicalize() const; wxGridBlockCoords Canonicalize() const;
/** /**
Whether the blocks intersects. Whether the blocks intersect.
@return @return
@true, if the block intersects with the other, @false, otherwise. @true, if the block intersects with the other, @false, otherwise.

File diff suppressed because it is too large Load Diff