Skip to content

Commit ac811a3

Browse files
committed
Add getSelectionRange to UIAbstractTableView and UITreeView.
1 parent e15925d commit ac811a3

File tree

5 files changed

+75
-2
lines changed

5 files changed

+75
-2
lines changed

include/eepp/ui/abstract/uiabstracttableview.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class EE_API UIAbstractTableView : public UIAbstractView {
4242

4343
virtual void selectAll();
4444

45+
virtual std::vector<ModelIndex> getSelectionRange( const ModelIndex& start,
46+
const ModelIndex& end ) const;
47+
4548
const Float& getDragBorderDistance() const;
4649

4750
void setDragBorderDistance( const Float& dragBorderDistance );

include/eepp/ui/models/modelselection.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ class EE_API ModelSelection {
7373
return *mIndexes.begin();
7474
}
7575

76+
ModelIndex last() const {
77+
Lock l( mMutex );
78+
if ( mIndexes.empty() )
79+
return {};
80+
return mIndexes.back();
81+
}
82+
7683
void removeAllMatching( std::function<bool( ModelIndex const& )> filter );
7784

7885
template <typename Function> void changeFromModel( Function f ) {

include/eepp/ui/uitreeview.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ class EE_API UITreeView : public UIAbstractTableView {
149149

150150
virtual size_t getItemCount() const;
151151

152+
std::vector<ModelIndex> getSelectionRange( const ModelIndex& start,
153+
const ModelIndex& end ) const override;
154+
152155
UITreeView::MetadataForIndex& getIndexMetadata( const ModelIndex& index ) const;
153156

154157
virtual void onColumnSizeChange( const size_t& colIndex, bool fromUserInteraction = false );

src/eepp/ui/abstract/uiabstracttableview.cpp

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ void UIAbstractTableView::selectAll() {
111111
}
112112
}
113113

114+
std::vector<ModelIndex> UIAbstractTableView::getSelectionRange( const ModelIndex& start,
115+
const ModelIndex& end ) const {
116+
std::vector<ModelIndex> range;
117+
if ( !getModel() )
118+
return range;
119+
int minRow = eemin( start.row(), end.row() );
120+
int maxRow = eemax( start.row(), end.row() );
121+
for ( int i = minRow; i <= maxRow; ++i ) {
122+
range.push_back( getModel()->index( i, start.column() ) );
123+
}
124+
return range;
125+
}
126+
114127
size_t UIAbstractTableView::getItemCount() const {
115128
if ( !getModel() )
116129
return 0;
@@ -507,17 +520,34 @@ UITableRow* UIAbstractTableView::createRow() {
507520
return;
508521
auto index = event->getNode()->asType<UITableRow>()->getCurIndex();
509522
if ( mSelectionKind == SelectionKind::Single &&
510-
getInput()->getSanitizedModState() & KeyMod::getDefaultModifier() ) {
523+
( getInput()->getSanitizedModState() & KeyMod::getDefaultModifier() ) ) {
511524
getSelection().remove( index );
512525
} else {
513526
if ( mSelectionKind == SelectionKind::Multiple &&
514-
getInput()->getSanitizedModState() & KeyMod::getDefaultModifier() ) {
527+
( getInput()->getSanitizedModState() & KeyMod::getDefaultModifier() ) ) {
515528
getSelection().toggle( index );
529+
} else if ( mSelectionKind == SelectionKind::Multiple &&
530+
( getInput()->getSanitizedModState() & KEYMOD_SHIFT ) &&
531+
!getSelection().isEmpty() ) {
532+
getSelection().set( getSelectionRange( getSelection().first(), index ) );
533+
} else if ( mSelectionKind == SelectionKind::Multiple ) {
534+
if ( !getSelection().contains( index ) )
535+
getSelection().set( index );
516536
} else {
517537
getSelection().set( index );
518538
}
519539
}
520540
} );
541+
rowWidget->on( Event::MouseClick, [this]( const Event* event ) {
542+
if ( !( event->asMouseEvent()->getFlags() & ( EE_BUTTON_LMASK | EE_BUTTON_RMASK ) ) ||
543+
!isRowSelection() )
544+
return;
545+
546+
auto index = event->getNode()->asType<UITableRow>()->getCurIndex();
547+
if ( 0 == getInput()->getSanitizedModState() ) {
548+
getSelection().set( index );
549+
}
550+
} );
521551
onRowCreated( rowWidget );
522552
return rowWidget;
523553
}

src/eepp/ui/uitreeview.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,36 @@ size_t UITreeView::getItemCount() const {
105105
return count;
106106
}
107107

108+
std::vector<ModelIndex> UITreeView::getSelectionRange( const ModelIndex& start,
109+
const ModelIndex& end ) const {
110+
std::vector<ModelIndex> range;
111+
if ( !getModel() )
112+
return range;
113+
114+
bool foundStart = false;
115+
traverseTree( [&]( const int&, const ModelIndex& index, const size_t&, const Float& ) {
116+
bool isStart = index == start;
117+
bool isEnd = index == end;
118+
119+
if ( isStart || isEnd ) {
120+
if ( !foundStart ) {
121+
foundStart = true;
122+
range.push_back( index );
123+
if ( start == end )
124+
return IterationDecision::Stop;
125+
} else {
126+
range.push_back( index );
127+
return IterationDecision::Stop;
128+
}
129+
} else if ( foundStart ) {
130+
range.push_back( index );
131+
}
132+
return IterationDecision::Continue;
133+
} );
134+
135+
return range;
136+
}
137+
108138
void UITreeView::onColumnSizeChange( const size_t& colIndex, bool fromUserInteraction ) {
109139
UIAbstractTableView::onColumnSizeChange( colIndex, fromUserInteraction );
110140
updateContentSize();

0 commit comments

Comments
 (0)