35#include "moc_qgsfeaturelistview.cpp"
37using namespace Qt::StringLiterals;
42 setSelectionMode( QAbstractItemView::ExtendedSelection );
44 mUpdateEditSelectionTimerWithSelection.setSingleShot(
true );
45 connect( &mUpdateEditSelectionTimerWithSelection, &QTimer::timeout,
this, [
this]() {
46 updateEditSelection(
true );
49 mUpdateEditSelectionTimerWithSelection.setInterval( 0 );
51 mUpdateEditSelectionTimerWithoutSelection.setSingleShot(
true );
52 connect( &mUpdateEditSelectionTimerWithoutSelection, &QTimer::timeout,
this, [
this]() {
53 updateEditSelection(
false );
56 mUpdateEditSelectionTimerWithoutSelection.setInterval( 0 );
61 return mModel->layerCache();
69 delete mFeatureSelectionModel;
70 delete mCurrentEditSelectionModel;
72 mCurrentEditSelectionModel =
new QItemSelectionModel( mModel->masterModel(),
this );
73 if ( !mFeatureSelectionManager )
76 mFeatureSelectionManager = mOwnedFeatureSelectionManager;
80 setSelectionModel( mFeatureSelectionModel );
82 ensureEditSelection( true );
85 if ( mItemDelegate && mItemDelegate->parent() ==
this )
91 mItemDelegate->setEditSelectionModel( mCurrentEditSelectionModel );
92 setItemDelegate( mItemDelegate );
94 mItemDelegate->setFeatureSelectionModel( mFeatureSelectionModel );
97 connect( mCurrentEditSelectionModel, &QItemSelectionModel::selectionChanged,
this, &QgsFeatureListView::editSelectionChanged );
99 connect(
featureListModel, &QgsFeatureListModel::rowsRemoved,
this, [
this]() { ensureEditSelection(); } );
100 connect(
featureListModel, &QgsFeatureListModel::rowsInserted,
this, [
this]() { ensureEditSelection(); } );
101 connect(
featureListModel, &QgsFeatureListModel::modelReset,
this, [
this]() { ensureEditSelection(); } );
106 if ( mModel->setDisplayExpression( expression ) )
119 return mModel->displayExpression();
124 return mModel->parserErrorString();
130 const QModelIndexList selectedIndexes = mCurrentEditSelectionModel->selectedIndexes();
131 for (
const QModelIndex &idx : selectedIndexes )
140 mItemDelegate->setCurrentFeatureEdited( state );
141 viewport()->update( visualRegionForSelection( mCurrentEditSelectionModel->selection() ) );
146 if ( event->button() != Qt::LeftButton )
148 QListView::mousePressEvent( event );
154 const QPoint pos =
event->pos();
156 const QModelIndex index = indexAt( pos );
160 mDragMode = DragMode::MoveSelection;
161 if ( index.isValid() )
162 setEditSelection( mModel->mapToMaster( index ), QItemSelectionModel::ClearAndSelect );
166 mDragMode = DragMode::ExpandSelection;
167 mFeatureSelectionModel->enableSync(
false );
168 selectRow( index,
true );
178void QgsFeatureListView::editSelectionChanged(
const QItemSelection &selected,
const QItemSelection &deselected )
180 if ( isVisible() && updatesEnabled() )
184 viewport()->update( visualRegionForSelection( localDeselected ) | visualRegionForSelection( localSelected ) );
188 if ( !selected.isEmpty() )
190 const QModelIndexList indexList = selected.indexes();
191 if ( !indexList.isEmpty() )
193 QgsFeature selectedFeature;
194 mModel->featureByIndex( mModel->mapFromMaster( indexList.first() ), selectedFeature );
195 mLastEditSelectionFid = selectedFeature.
id();
199 const QItemSelection currentSelection = mCurrentEditSelectionModel->selection();
200 if ( currentSelection.size() == 1 )
202 QModelIndexList indexList = currentSelection.indexes();
203 if ( !indexList.isEmpty() )
206 mModel->featureByIndex( mModel->mapFromMaster( indexList.first() ), feat );
212 else if ( mModel->rowCount() == 0 )
220 QItemSelection selection;
221 selection.append( QItemSelectionRange( mModel->index( 0, 0 ), mModel->index( mModel->rowCount() - 1, 0 ) ) );
223 mFeatureSelectionModel->selectFeatures( selection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
228 QItemSelection selection;
229 QModelIndex firstModelIdx;
231 const auto constFids = fids;
234 const QModelIndex modelIdx = mModel->fidToIdx( fid );
236 if ( !firstModelIdx.isValid() )
237 firstModelIdx = modelIdx;
239 selection.append( QItemSelectionRange( mModel->mapToMaster( modelIdx ) ) );
247 mCurrentEditSelectionModel->select( selection, QItemSelectionModel::ClearAndSelect );
248 scrollTo( firstModelIdx );
258 Q_ASSERT( index.model() == mModel->masterModel() || !index.isValid() );
262 mCurrentEditSelectionModel->select( index, command );
269 const auto constIndexes = indexes;
270 for (
const QModelIndex &index : constIndexes )
278 setDirtyRegion( viewport()->rect() );
285 const QPoint pos =
event->pos();
286 const QModelIndex index = indexAt( pos );
290 case QgsFeatureListView::DragMode::Inactive:
293 case QgsFeatureListView::DragMode::ExpandSelection:
295 selectRow( index,
false );
299 case QgsFeatureListView::DragMode::MoveSelection:
301 if ( index.isValid() )
302 setEditSelection( mModel->mapToMaster( index ), QItemSelectionModel::ClearAndSelect );
315 if ( event->button() != Qt::LeftButton )
317 QListView::mouseReleaseEvent( event );
323 case QgsFeatureListView::DragMode::ExpandSelection:
324 if ( mFeatureSelectionModel )
325 mFeatureSelectionModel->enableSync(
true );
327 case QgsFeatureListView::DragMode::Inactive:
328 case QgsFeatureListView::DragMode::MoveSelection:
332 mDragMode = DragMode::Inactive;
337 switch ( event->key() )
340 editOtherFeature( Previous );
344 editOtherFeature( Next );
348 QListView::keyPressEvent( event );
352void QgsFeatureListView::editOtherFeature( QgsFeatureListView::PositionInList positionInList )
355 if ( 0 != mCurrentEditSelectionModel->selectedIndexes().count() )
357 const QModelIndex localIndex = mModel->
mapFromMaster( mCurrentEditSelectionModel->selectedIndexes().first() );
358 currentRow = localIndex.row();
361 QModelIndex newLocalIndex;
362 QModelIndex newIndex;
364 switch ( positionInList )
367 newLocalIndex = mModel->index( 0, 0 );
371 newLocalIndex = mModel->index( currentRow - 1, 0 );
375 newLocalIndex = mModel->index( currentRow + 1, 0 );
379 newLocalIndex = mModel->index( mModel->rowCount() - 1, 0 );
383 newIndex = mModel->mapToMaster( newLocalIndex );
384 if ( newIndex.isValid() )
387 scrollTo( newLocalIndex );
393 const QModelIndex index = indexAt( event->pos() );
395 if ( index.isValid() )
407 menu->exec( event->globalPos() );
411void QgsFeatureListView::selectRow(
const QModelIndex &index,
bool anchor )
413 QItemSelectionModel::SelectionFlags command = selectionCommand( index );
414 const int row = index.row();
419 if ( selectionMode() != QListView::SingleSelection
420 && command.testFlag( QItemSelectionModel::Toggle ) )
423 mCtrlDragSelectionFlag = mFeatureSelectionModel->
isSelected( index )
424 ? QItemSelectionModel::Deselect
425 : QItemSelectionModel::Select;
426 command &= ~QItemSelectionModel::Toggle;
427 command |= mCtrlDragSelectionFlag;
429 command |= QItemSelectionModel::Current;
432 const QModelIndex tl = model()->index( std::min( mRowAnchor, row ), 0 );
433 const QModelIndex br = model()->index( std::max( mRowAnchor, row ), model()->columnCount() - 1 );
435 mFeatureSelectionModel->selectFeatures( QItemSelection( tl, br ), command );
438void QgsFeatureListView::ensureEditSelection(
bool inSelection )
442 mUpdateEditSelectionTimerWithSelection.start();
446 mUpdateEditSelectionTimerWithoutSelection.start();
450void QgsFeatureListView::updateEditSelection(
bool inSelection )
452 if ( !mModel->rowCount() )
460 const QModelIndexList selectedIndexes = mCurrentEditSelectionModel->selectedIndexes();
466 bool editSelectionUpdateRequested =
false;
469 bool validEditSelectionAvailable =
false;
471 if ( selectedIndexes.isEmpty() || !selectedIndexes.first().isValid() || mModel->mapFromMaster( selectedIndexes.first() ).row() == -1 )
473 validEditSelectionAvailable =
false;
477 validEditSelectionAvailable =
true;
485 if ( !validEditSelectionAvailable )
487 editSelectionUpdateRequested =
true;
494 if ( !selectedFids.contains( mModel->idxToFid( mModel->mapFromMaster( selectedIndexes.first() ) ) ) )
496 editSelectionUpdateRequested =
true;
504 if ( !validEditSelectionAvailable )
505 editSelectionUpdateRequested =
true;
508 if ( editSelectionUpdateRequested )
515 int rowToSelect = -1;
525 if ( selectedFids.isEmpty() )
531 const int rowCount = mModel->rowCount();
532 for (
int i = 0; i < rowCount; i++ )
534 if ( selectedFids.contains( mModel->idxToFid( mModel->index( i, 0 ) ) ) )
541 if ( rowToSelect == -1 && !validEditSelectionAvailable )
547 if ( rowToSelect != -1 )
549 setEditSelection( mModel->mapToMaster( mModel->index( rowToSelect, 0 ) ), QItemSelectionModel::ClearAndSelect );
556 mFeatureSelectionManager = featureSelectionManager;
558 if ( mFeatureSelectionModel )
559 mFeatureSelectionModel->setFeatureSelectionManager( mFeatureSelectionManager );
562 if ( mOwnedFeatureSelectionManager )
564 mOwnedFeatureSelectionManager->deleteLater();
565 mOwnedFeatureSelectionManager =
nullptr;
@ FeatureId
Get the feature id of the feature in this row.
A proxy model for feature lists.
virtual QModelIndex mapFromMaster(const QModelIndex &sourceIndex) const
@ FeatureWithGeometryRole
Feature with all attributes and geometry,.
virtual QItemSelection mapSelectionFromMaster(const QItemSelection &selection) const
Custom item delegate for feature list views.
void currentEditSelectionProgressChanged(int progress, int count)
Emitted whenever the current edit selection has been changed.
const QString displayExpression() const
Returns the expression which is currently used to render the features.
void keyPressEvent(QKeyEvent *event) override
void contextMenuEvent(QContextMenuEvent *event) override
void setCurrentFeatureEdited(bool state)
Sets if the currently shown form has received any edit events so far.
void displayExpressionChanged(const QString &expression)
Emitted whenever the display expression is successfully changed.
void repaintRequested(const QModelIndexList &indexes)
void mouseMoveEvent(QMouseEvent *event) override
void setEditSelection(const QgsFeatureIds &fids)
Set the feature(s) to be edited.
void setFeatureSelectionManager(QgsIFeatureSelectionManager *featureSelectionManager)
setFeatureSelectionManager
QgsFeatureIds currentEditSelection()
Gets the currentEditSelection.
void mousePressEvent(QMouseEvent *event) override
bool setDisplayExpression(const QString &displayExpression)
The display expression is an expression used to render the fields into a single string which is displ...
void selectAll() override
Select all currently visible features.
QgsVectorLayerCache * layerCache()
Returns the layer cache.
QString parserErrorString()
Returns a detailed message about errors while parsing a QgsExpression.
void mouseReleaseEvent(QMouseEvent *event) override
void willShowContextMenu(QgsActionMenu *menu, const QModelIndex &atIndex)
Emitted when the context menu is created to add the specific actions to it.
QgsFeatureListModel * featureListModel()
Gets the featureListModel used by this view.
QgsFeatureListView(QWidget *parent=nullptr)
Creates a feature list view.
virtual void setModel(QgsFeatureListModel *featureListModel)
Set the QgsFeatureListModel which is used to retrieve information.
void currentEditSelectionChanged(QgsFeature &feat)
Emitted whenever the current edit selection has been changed.
void aboutToChangeEditSelection(bool &ok)
Item selection model for selecting features.
virtual bool isSelected(QgsFeatureId fid)
Returns the selection status of a given feature id.
void requestRepaint(const QModelIndexList &indexes)
Request a repaint of a list of model indexes.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Is an interface class to abstract feature selection handling.
Caches features for a given QgsVectorLayer.
QgsVectorLayer * layer()
Returns the layer to which this cache belongs.
Manages vector layer selections.
Q_INVOKABLE const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
void attributeValueChanged(QgsFeatureId fid, int idx, const QVariant &value)
Emitted whenever an attribute value change is done in the edit buffer.
void selectionChanged(const QgsFeatureIds &selected, const QgsFeatureIds &deselected, bool clearAndSelect)
Emitted when selection was changed.
QSet< QgsFeatureId > QgsFeatureIds
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
#define QgsDebugError(str)