16 #include <QHeaderView>
38 setSelectionMode( QAbstractItemView::ExtendedSelection );
51 delete mFeatureSelectionModel;
52 delete mCurrentEditSelectionModel;
54 mCurrentEditSelectionModel =
new QItemSelectionModel( mModel->
masterModel(),
this );
55 if ( !mFeatureSelectionManager )
58 mFeatureSelectionManager = mOwnedFeatureSelectionManager;
62 setSelectionModel( mFeatureSelectionModel );
65 ensureEditSelection( true );
68 if ( mItemDelegate && mItemDelegate->parent() ==
this )
75 setItemDelegate( mItemDelegate );
82 connect( mCurrentEditSelectionModel, &QItemSelectionModel::selectionChanged,
this, &QgsFeatureListView::editSelectionChanged );
84 connect(
featureListModel, &QgsFeatureListModel::rowsRemoved,
this, [
this ]() { ensureEditSelection(); } );
85 connect(
featureListModel, &QgsFeatureListModel::rowsInserted,
this, [
this ]() { ensureEditSelection(); } );
86 connect(
featureListModel, &QgsFeatureListModel::modelReset,
this, [
this ]() { ensureEditSelection(); } );
115 const QModelIndexList selectedIndexes = mCurrentEditSelectionModel->selectedIndexes();
116 for (
const QModelIndex &idx : selectedIndexes )
126 viewport()->update( visualRegionForSelection( mCurrentEditSelectionModel->selection() ) );
133 QPoint pos =
event->pos();
135 QModelIndex index = indexAt( pos );
140 mEditSelectionDrag =
true;
141 if ( index.isValid() )
147 selectRow( index,
true );
153 QgsDebugMsg( QStringLiteral(
"No model assigned to this view" ) );
157 void QgsFeatureListView::editSelectionChanged(
const QItemSelection &deselected,
const QItemSelection &selected )
159 if ( isVisible() && updatesEnabled() )
163 viewport()->update( visualRegionForSelection( localDeselected ) | visualRegionForSelection( localSelected ) );
165 updateEditSelectionDependencies();
168 void QgsFeatureListView::updateEditSelectionDependencies()
170 QItemSelection currentSelection = mCurrentEditSelectionModel->selection();
171 if ( currentSelection.size() == 1 )
173 QModelIndexList indexList = currentSelection.indexes();
174 if ( !indexList.isEmpty() )
187 QItemSelection selection;
188 selection.append( QItemSelectionRange( mModel->index( 0, 0 ), mModel->index( mModel->
rowCount() - 1, 0 ) ) );
190 mFeatureSelectionModel->
selectFeatures( selection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
195 QItemSelection selection;
197 const auto constFids = fids;
207 mCurrentEditSelectionModel->select( selection, QItemSelectionModel::ClearAndSelect );
216 Q_ASSERT( index.model() == mModel->
masterModel() || !index.isValid() );
219 mCurrentEditSelectionModel->select( index, command );
224 const auto constIndexes = indexes;
225 for (
const QModelIndex &index : constIndexes )
233 setDirtyRegion( viewport()->rect() );
238 QPoint pos =
event->pos();
240 QModelIndex index = indexAt( pos );
242 if ( mEditSelectionDrag )
244 if ( index.isValid() )
249 selectRow( index,
false );
257 if ( mEditSelectionDrag )
259 mEditSelectionDrag =
false;
263 if ( mFeatureSelectionModel )
270 switch ( event->key() )
273 editOtherFeature( Previous );
277 editOtherFeature( Next );
281 QListView::keyPressEvent( event );
285 void QgsFeatureListView::editOtherFeature( QgsFeatureListView::PositionInList positionInList )
288 if ( 0 != mCurrentEditSelectionModel->selectedIndexes().count() )
290 QModelIndex localIndex = mModel->
mapFromMaster( mCurrentEditSelectionModel->selectedIndexes().first() );
291 currentRow = localIndex.row();
294 QModelIndex newLocalIndex;
295 QModelIndex newIndex;
297 switch ( positionInList )
300 newLocalIndex = mModel->index( 0, 0 );
304 newLocalIndex = mModel->index( currentRow - 1, 0 );
308 newLocalIndex = mModel->index( currentRow + 1, 0 );
312 newLocalIndex = mModel->index( mModel->
rowCount() - 1, 0 );
317 if ( newIndex.isValid() )
320 scrollTo( newLocalIndex );
326 QModelIndex index = indexAt( event->pos() );
328 if ( index.isValid() )
340 menu->exec( event->globalPos() );
344 void QgsFeatureListView::selectRow(
const QModelIndex &index,
bool anchor )
346 QItemSelectionModel::SelectionFlags command = selectionCommand( index );
347 int row = index.row();
352 if ( selectionMode() != QListView::SingleSelection
353 && command.testFlag( QItemSelectionModel::Toggle ) )
356 mCtrlDragSelectionFlag = mFeatureSelectionModel->
isSelected( index )
357 ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
358 command &= ~QItemSelectionModel::Toggle;
359 command |= mCtrlDragSelectionFlag;
361 command |= QItemSelectionModel::Current;
364 QModelIndex tl = model()->index( std::min( mRowAnchor, row ), 0 );
365 QModelIndex br = model()->index( std::max( mRowAnchor, row ), model()->columnCount() - 1 );
367 mFeatureSelectionModel->
selectFeatures( QItemSelection( tl, br ), command );
370 void QgsFeatureListView::ensureEditSelection(
bool inSelection )
380 const QModelIndexList selectedIndexes = mCurrentEditSelectionModel->selectedIndexes();
386 bool editSelectionUpdateRequested =
false;
389 bool validEditSelectionAvailable =
false;
391 if ( selectedIndexes.isEmpty() || !selectedIndexes.first().isValid() || mModel->
mapFromMaster( selectedIndexes.first() ).row() == -1 )
393 validEditSelectionAvailable =
false;
397 validEditSelectionAvailable =
true;
405 if ( !validEditSelectionAvailable )
407 editSelectionUpdateRequested =
true;
414 if ( !selectedFids.contains( mModel->
idxToFid( mModel->
mapFromMaster( selectedIndexes.first() ) ) ) )
416 editSelectionUpdateRequested =
true;
424 if ( !validEditSelectionAvailable )
425 editSelectionUpdateRequested =
true;
428 if ( editSelectionUpdateRequested )
430 if ( !mUpdateEditSelectionTimer.isSingleShot() )
432 mUpdateEditSelectionTimer.setSingleShot(
true );
433 connect( &mUpdateEditSelectionTimer, &QTimer::timeout,
this, [
this, inSelection, validEditSelectionAvailable ]()
440 int rowToSelect = -1;
445 const int rowCount = mModel->
rowCount();
447 for (
int i = 0; i < rowCount; i++ )
449 if ( selectedFids.contains( mModel->
idxToFid( mModel->index( i, 0 ) ) ) )
455 if ( rowToSelect == -1 && !validEditSelectionAvailable )
462 if ( rowToSelect != -1 )
467 mUpdateEditSelectionTimer.setInterval( 0 );
469 mUpdateEditSelectionTimer.start();
471 updateEditSelectionDependencies();
476 mFeatureSelectionManager = featureSelectionManager;
478 if ( mFeatureSelectionModel )
482 if ( mOwnedFeatureSelectionManager )
484 mOwnedFeatureSelectionManager->deleteLater();
485 mOwnedFeatureSelectionManager =
nullptr;