Implement wxGrid selection blocks iterating interface

This commit is contained in:
Ilya Sinitsyn
2020-03-27 03:23:45 +07:00
committed by Vadim Zeitlin
parent 8ebdf101b8
commit f8015b13b1
5 changed files with 168 additions and 0 deletions

View File

@@ -868,6 +868,66 @@ struct wxGridBlockDiffResult
wxGridBlockCoords m_parts[4]; wxGridBlockCoords m_parts[4];
}; };
// ----------------------------------------------------------------------------
// wxGridSelectionRange: the range of grid selection blocks
// ----------------------------------------------------------------------------
class wxGridSelectionRange
{
public:
typedef wxGridBlockCoords* iterator;
wxGridSelectionRange() :
m_begin(NULL),
m_end(NULL),
m_it(NULL)
{
}
// Get the current selection block coordinates.
const wxGridBlockCoords& GetBlockCoords() const
{
static wxGridBlockCoords empty;
return Valid() ? *m_it : empty;
}
// Iterate to the next block.
void Next()
{
if ( Valid() )
++m_it;
}
// Whether the iterator is valid.
bool Valid() const
{
return m_it != m_end;
}
iterator begin() const
{
return m_begin;
}
iterator end() const
{
return m_end;
}
private:
wxGridSelectionRange(iterator begin, iterator end) :
m_begin(begin),
m_end(end),
m_it(begin)
{
}
const iterator m_begin;
const iterator m_end;
iterator m_it;
friend class wxGrid;
};
// For comparisons... // For comparisons...
// //
@@ -1918,6 +1978,7 @@ public:
bool IsInSelection( const wxGridCellCoords& coords ) const bool IsInSelection( const wxGridCellCoords& coords ) const
{ return IsInSelection( coords.GetRow(), coords.GetCol() ); } { return IsInSelection( coords.GetRow(), coords.GetCol() ); }
wxGridSelectionRange GetSelectionRange() const;
wxGridCellCoordsArray GetSelectedCells() const; wxGridCellCoordsArray GetSelectedCells() const;
wxGridCellCoordsArray GetSelectionBlockTopLeft() const; wxGridCellCoordsArray GetSelectionBlockTopLeft() const;
wxGridCellCoordsArray GetSelectionBlockBottomRight() const; wxGridCellCoordsArray GetSelectionBlockBottomRight() const;

View File

@@ -89,6 +89,8 @@ public:
wxArrayInt GetRowSelection() const; wxArrayInt GetRowSelection() const;
wxArrayInt GetColSelection() const; wxArrayInt GetColSelection() const;
wxVectorGridBlockCoords& GetBlocks() { return m_selection; }
private: private:
int BlockContain( int topRow1, int leftCol1, int BlockContain( int topRow1, int leftCol1,
int bottomRow1, int rightCol1, int bottomRow1, int rightCol1,

View File

@@ -2005,6 +2005,45 @@ struct wxGridBlockDiffResult
wxGridBlockCoords m_parts[4]; wxGridBlockCoords m_parts[4];
}; };
/**
The range of grid selection blocks.
@since 3.1.4
*/
class wxGridSelectionRange
{
public:
/**
Default constructor initializes the iterator.
*/
wxGridSelectionRange();
/**
* Get the current selection block coordinates.
*/
const wxGridBlockCoords& GetBlockCoords() const;
/**
* Iterate to the next block.
*/
void Next();
/**
* Whether the iterator is valid.
*/
bool Valid();
/**
* The function to allow using range-based for.
*/
wxGridBlockCoords *begin() const;
/**
* The function to allow using range-based for.
*/
wxGridBlockCoords *end() const;
};
/** /**
@class wxGridTableBase @class wxGridTableBase
@@ -4608,6 +4647,13 @@ public:
*/ */
void DeselectCell( int row, int col ); void DeselectCell( int row, int col );
/**
Returns an range of grid selection blocks.
@since 3.1.4
*/
wxGridSelectionRange GetSelectionRange() const;
/** /**
Returns an array of individually selected cells. Returns an array of individually selected cells.
@@ -4623,6 +4669,9 @@ public:
a grid with a million of columns, we don't want to create an array with a grid with a million of columns, we don't want to create an array with
a million of entries in this function, instead it returns an empty a million of entries in this function, instead it returns an empty
array and GetSelectedCols() returns an array containing one element). array and GetSelectedCols() returns an array containing one element).
The function can be slow for the big grids, use GetSelectionRange()
in the new code.
*/ */
wxGridCellCoordsArray GetSelectedCells() const; wxGridCellCoordsArray GetSelectedCells() const;
@@ -4634,6 +4683,9 @@ public:
individually selected but not those being part of the block selection individually selected but not those being part of the block selection
or being selected in virtue of all of their cells being selected or being selected in virtue of all of their cells being selected
individually, please see GetSelectedCells() for more details. individually, please see GetSelectedCells() for more details.
The function can be slow for the big grids, use GetSelectionRange()
in the new code.
*/ */
wxArrayInt GetSelectedCols() const; wxArrayInt GetSelectedCols() const;
@@ -4645,6 +4697,9 @@ public:
selected but not those being part of the block selection or being selected but not those being part of the block selection or being
selected in virtue of all of their cells being selected individually, selected in virtue of all of their cells being selected individually,
please see GetSelectedCells() for more details. please see GetSelectedCells() for more details.
The function can be slow for the big grids, use GetSelectionRange()
in the new code.
*/ */
wxArrayInt GetSelectedRows() const; wxArrayInt GetSelectedRows() const;
@@ -4660,6 +4715,9 @@ public:
Please see GetSelectedCells() for more information about the selection Please see GetSelectedCells() for more information about the selection
representation in wxGrid. representation in wxGrid.
The function can be slow for the big grids, use GetSelectionRange()
in the new code.
@see GetSelectionBlockTopLeft() @see GetSelectionBlockTopLeft()
*/ */
wxGridCellCoordsArray GetSelectionBlockBottomRight() const; wxGridCellCoordsArray GetSelectionBlockBottomRight() const;
@@ -4670,6 +4728,9 @@ public:
Please see GetSelectedCells() for more information about the selection Please see GetSelectedCells() for more information about the selection
representation in wxGrid. representation in wxGrid.
The function can be slow for the big grids, use GetSelectionRange()
in the new code.
@see GetSelectionBlockBottomRight() @see GetSelectionBlockBottomRight()
*/ */
wxGridCellCoordsArray GetSelectionBlockTopLeft() const; wxGridCellCoordsArray GetSelectionBlockTopLeft() const;

View File

@@ -10231,6 +10231,15 @@ bool wxGrid::IsInSelection( int row, int col ) const
return m_selection && m_selection->IsInSelection(row, col); return m_selection && m_selection->IsInSelection(row, col);
} }
wxGridSelectionRange wxGrid::GetSelectionRange() const
{
if ( !m_selection )
return wxGridSelectionRange();
wxVectorGridBlockCoords& blocks = m_selection->GetBlocks();
return wxGridSelectionRange(blocks.begin(), blocks.end());
}
wxGridCellCoordsArray wxGrid::GetSelectedCells() const wxGridCellCoordsArray wxGrid::GetSelectedCells() const
{ {
if (!m_selection) if (!m_selection)

View File

@@ -524,6 +524,41 @@ TEST_CASE_METHOD(GridTestCase, "Grid::Selection", "[grid]")
CHECK(!m_grid->IsInSelection(3, 0)); CHECK(!m_grid->IsInSelection(3, 0));
} }
TEST_CASE_METHOD(GridTestCase, "Grid::SelectionIterator", "[grid]")
{
CHECK(!m_grid->GetSelectionRange().Valid());
m_grid->SelectBlock(1, 0, 3, 1);
wxGridSelectionRange sel = m_grid->GetSelectionRange();
CHECK(sel.Valid());
CHECK(sel.GetBlockCoords() == wxGridBlockCoords(1, 0, 3, 1));
sel.Next();
CHECK(!sel.Valid());
#if __cplusplus >= 201103L || wxCHECK_VISUALC_VERSION(10)
m_grid->SelectBlock(4, 0, 7, 1, true);
int index = 0;
for ( const wxGridBlockCoords& block : m_grid->GetSelectionRange() )
{
switch ( index )
{
case 0:
CHECK(block == wxGridBlockCoords(1, 0, 3, 1));
break;
case 1:
CHECK(block == wxGridBlockCoords(4, 0, 7, 1));
break;
default:
FAIL("Unexpected iterations count");
break;
}
++index;
}
#endif
}
TEST_CASE_METHOD(GridTestCase, "Grid::SelectEmptyGrid", "[grid]") TEST_CASE_METHOD(GridTestCase, "Grid::SelectEmptyGrid", "[grid]")
{ {
for ( int i = 0; i < 2; ++i ) for ( int i = 0; i < 2; ++i )