36 setSelectionMode( QAbstractItemView::ExtendedSelection );
38 mUpdateEditSelectionTimerWithSelection.setSingleShot(
true );
39 connect( &mUpdateEditSelectionTimerWithSelection, &QTimer::timeout,
this, [
this ]()
41 updateEditSelection(
true );
44 mUpdateEditSelectionTimerWithSelection.setInterval( 0 );
46 mUpdateEditSelectionTimerWithoutSelection.setSingleShot(
true );
47 connect( &mUpdateEditSelectionTimerWithoutSelection, &QTimer::timeout,
this, [
this ]()
49 updateEditSelection(
false );
52 mUpdateEditSelectionTimerWithoutSelection.setInterval( 0 );
65 delete mFeatureSelectionModel;
66 delete mCurrentEditSelectionModel;
68 mCurrentEditSelectionModel =
new QItemSelectionModel( mModel->
masterModel(),
this );
69 if ( !mFeatureSelectionManager )
72 mFeatureSelectionManager = mOwnedFeatureSelectionManager;
76 setSelectionModel( mFeatureSelectionModel );
79 ensureEditSelection( true );
82 if ( mItemDelegate && mItemDelegate->parent() ==
this )
89 setItemDelegate( mItemDelegate );
96 connect( mCurrentEditSelectionModel, &QItemSelectionModel::selectionChanged,
this, &QgsFeatureListView::editSelectionChanged );
98 connect(
featureListModel, &QgsFeatureListModel::rowsRemoved,
this, [
this ]() { ensureEditSelection(); } );
99 connect(
featureListModel, &QgsFeatureListModel::rowsInserted,
this, [
this ]() { ensureEditSelection(); } );
100 connect(
featureListModel, &QgsFeatureListModel::modelReset,
this, [
this ]() { ensureEditSelection(); } );
129 const QModelIndexList selectedIndexes = mCurrentEditSelectionModel->selectedIndexes();
130 for (
const QModelIndex &idx : selectedIndexes )
140 viewport()->update( visualRegionForSelection( mCurrentEditSelectionModel->selection() ) );
145 if ( event->button() != Qt::LeftButton )
147 QListView::mousePressEvent( event );
153 const QPoint pos =
event->pos();
155 const QModelIndex index = indexAt( pos );
159 mDragMode = DragMode::MoveSelection;
160 if ( index.isValid() )
165 mDragMode = DragMode::ExpandSelection;
167 selectRow( index,
true );
173 QgsDebugMsg( QStringLiteral(
"No model assigned to this view" ) );
177void QgsFeatureListView::editSelectionChanged(
const QItemSelection &deselected,
const QItemSelection &selected )
179 if ( isVisible() && updatesEnabled() )
183 viewport()->update( visualRegionForSelection( localDeselected ) | visualRegionForSelection( localSelected ) );
186 const QItemSelection currentSelection = mCurrentEditSelectionModel->selection();
187 if ( currentSelection.size() == 1 )
189 QModelIndexList indexList = currentSelection.indexes();
190 if ( !indexList.isEmpty() )
207 QItemSelection selection;
208 selection.append( QItemSelectionRange( mModel->index( 0, 0 ), mModel->index( mModel->
rowCount() - 1, 0 ) ) );
210 mFeatureSelectionModel->
selectFeatures( selection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
215 QItemSelection selection;
216 QModelIndex firstModelIdx;
218 const auto constFids = fids;
221 const QModelIndex modelIdx = mModel->
fidToIdx( fid );
223 if ( ! firstModelIdx.isValid() )
224 firstModelIdx = modelIdx;
226 selection.append( QItemSelectionRange( mModel->
mapToMaster( modelIdx ) ) );
234 mCurrentEditSelectionModel->select( selection, QItemSelectionModel::ClearAndSelect );
235 scrollTo( firstModelIdx );
245 Q_ASSERT( index.model() == mModel->
masterModel() || !index.isValid() );
249 mCurrentEditSelectionModel->select( index, command );
256 const auto constIndexes = indexes;
257 for (
const QModelIndex &index : constIndexes )
265 setDirtyRegion( viewport()->rect() );
272 const QPoint pos =
event->pos();
273 const QModelIndex index = indexAt( pos );
277 case QgsFeatureListView::DragMode::Inactive:
280 case QgsFeatureListView::DragMode::ExpandSelection:
282 selectRow( index,
false );
286 case QgsFeatureListView::DragMode::MoveSelection:
288 if ( index.isValid() )
296 QgsDebugMsg( QStringLiteral(
"No model assigned to this view" ) );
302 if ( event->button() != Qt::LeftButton )
304 QListView::mouseReleaseEvent( event );
310 case QgsFeatureListView::DragMode::ExpandSelection:
311 if ( mFeatureSelectionModel )
314 case QgsFeatureListView::DragMode::Inactive:
315 case QgsFeatureListView::DragMode::MoveSelection:
319 mDragMode = DragMode::Inactive;
324 switch ( event->key() )
327 editOtherFeature( Previous );
331 editOtherFeature( Next );
335 QListView::keyPressEvent( event );
339void QgsFeatureListView::editOtherFeature( QgsFeatureListView::PositionInList positionInList )
342 if ( 0 != mCurrentEditSelectionModel->selectedIndexes().count() )
344 const QModelIndex localIndex = mModel->
mapFromMaster( mCurrentEditSelectionModel->selectedIndexes().first() );
345 currentRow = localIndex.row();
348 QModelIndex newLocalIndex;
349 QModelIndex newIndex;
351 switch ( positionInList )
354 newLocalIndex = mModel->index( 0, 0 );
358 newLocalIndex = mModel->index( currentRow - 1, 0 );
362 newLocalIndex = mModel->index( currentRow + 1, 0 );
366 newLocalIndex = mModel->index( mModel->
rowCount() - 1, 0 );
371 if ( newIndex.isValid() )
374 scrollTo( newLocalIndex );
380 const QModelIndex index = indexAt( event->pos() );
382 if ( index.isValid() )
394 menu->exec( event->globalPos() );
398void QgsFeatureListView::selectRow(
const QModelIndex &index,
bool anchor )
400 QItemSelectionModel::SelectionFlags command = selectionCommand( index );
401 const int row = index.row();
406 if ( selectionMode() != QListView::SingleSelection
407 && command.testFlag( QItemSelectionModel::Toggle ) )
410 mCtrlDragSelectionFlag = mFeatureSelectionModel->
isSelected( index )
411 ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
412 command &= ~QItemSelectionModel::Toggle;
413 command |= mCtrlDragSelectionFlag;
415 command |= QItemSelectionModel::Current;
418 const QModelIndex tl = model()->index( std::min( mRowAnchor, row ), 0 );
419 const QModelIndex br = model()->index( std::max( mRowAnchor, row ), model()->columnCount() - 1 );
421 mFeatureSelectionModel->
selectFeatures( QItemSelection( tl, br ), command );
424void QgsFeatureListView::ensureEditSelection(
bool inSelection )
429 mUpdateEditSelectionTimerWithSelection.start();
433 mUpdateEditSelectionTimerWithoutSelection.start();
437void QgsFeatureListView::updateEditSelection(
bool inSelection )
447 const QModelIndexList selectedIndexes = mCurrentEditSelectionModel->selectedIndexes();
453 bool editSelectionUpdateRequested =
false;
456 bool validEditSelectionAvailable =
false;
458 if ( selectedIndexes.isEmpty() || !selectedIndexes.first().isValid() || mModel->
mapFromMaster( selectedIndexes.first() ).row() == -1 )
460 validEditSelectionAvailable =
false;
464 validEditSelectionAvailable =
true;
472 if ( !validEditSelectionAvailable )
474 editSelectionUpdateRequested =
true;
481 if ( !selectedFids.contains( mModel->
idxToFid( mModel->
mapFromMaster( selectedIndexes.first() ) ) ) )
483 editSelectionUpdateRequested =
true;
491 if ( !validEditSelectionAvailable )
492 editSelectionUpdateRequested =
true;
495 if ( editSelectionUpdateRequested )
502 int rowToSelect = -1;
507 const int rowCount = mModel->
rowCount();
509 for (
int i = 0; i < rowCount; i++ )
511 if ( selectedFids.contains( mModel->
idxToFid( mModel->index( i, 0 ) ) ) )
517 if ( rowToSelect == -1 && !validEditSelectionAvailable )
524 if ( rowToSelect != -1 )
533 mFeatureSelectionManager = featureSelectionManager;
535 if ( mFeatureSelectionModel )
539 if ( mOwnedFeatureSelectionManager )
541 mOwnedFeatureSelectionManager->deleteLater();
542 mOwnedFeatureSelectionManager =
nullptr;
@ FeatureIdRole
Get the feature id of the feature in this row.
QgsFeatureId idxToFid(const QModelIndex &index) const
Returns the feature ID corresponding to an index from the model.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
bool featureByIndex(const QModelIndex &index, QgsFeature &feat)
virtual QModelIndex mapToMaster(const QModelIndex &proxyIndex) const
QModelIndex fidToIdx(QgsFeatureId fid) const
Returns the model index corresponding to a feature ID.
QString parserErrorString()
Returns a detailed message about errors while parsing a QgsExpression.
QVariant data(const QModelIndex &index, int role) const override
bool setDisplayExpression(const QString &expression)
virtual QModelIndex mapFromMaster(const QModelIndex &sourceIndex) const
QString displayExpression() const
QgsVectorLayerCache * layerCache()
Returns the vector layer cache which is being used to populate the model.
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
virtual QItemSelection mapSelectionFromMaster(const QItemSelection &selection) const
QgsAttributeTableModel * masterModel()
void setEditSelectionModel(QItemSelectionModel *editSelectionModel)
void setCurrentFeatureEdited(bool state)
void setFeatureSelectionModel(QgsFeatureSelectionModel *featureSelectionModel)
Element positionToElement(QPoint pos)
Shows a list of features and renders a edit button next to each feature.
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 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)
void enableSync(bool enable)
Enables or disables synchronisation to the QgsVectorLayer When synchronisation is disabled,...
virtual void selectFeatures(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
Select features on this table.
virtual bool isSelected(QgsFeatureId fid)
Returns the selection status of a given feature id.
virtual void setFeatureSelectionManager(QgsIFeatureSelectionManager *featureSelectionManager)
void requestRepaint()
Request a repaint of the visible items of connected views.
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.
This class caches features of a given QgsVectorLayer.
QgsVectorLayer * layer()
Returns the layer to which this cache belongs.
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