16 #include <QHeaderView> 38 setSelectionMode( QAbstractItemView::ExtendedSelection );
48 QListView::setModel( featureListModel );
51 delete mFeatureSelectionModel;
52 delete mCurrentEditSelectionModel;
54 mCurrentEditSelectionModel =
new QItemSelectionModel( mModel->
masterModel(), this );
55 if ( !mFeatureSelectionManager )
58 mFeatureSelectionManager = mOwnedFeatureSelectionManager;
61 mFeatureSelectionModel =
new QgsFeatureSelectionModel( featureListModel, featureListModel, mFeatureSelectionManager,
this );
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 ) );
166 QItemSelection currentSelection = mCurrentEditSelectionModel->selection();
167 if ( currentSelection.size() == 1 )
169 QModelIndexList indexList = currentSelection.indexes();
170 if ( !indexList.isEmpty() )
183 QItemSelection selection;
184 selection.append( QItemSelectionRange( mModel->index( 0, 0 ), mModel->index( mModel->
rowCount() - 1, 0 ) ) );
186 mFeatureSelectionModel->
selectFeatures( selection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
191 QItemSelection selection;
193 const auto constFids = fids;
203 mCurrentEditSelectionModel->select( selection, QItemSelectionModel::ClearAndSelect );
211 Q_ASSERT( index.model() == mModel->
masterModel() || !index.isValid() );
214 mCurrentEditSelectionModel->select( index, command );
219 const auto constIndexes = indexes;
220 for (
const QModelIndex &index : constIndexes )
228 setDirtyRegion( viewport()->rect() );
233 QPoint pos =
event->pos();
235 QModelIndex index = indexAt( pos );
237 if ( mEditSelectionDrag )
239 if ( index.isValid() )
244 selectRow( index,
false );
252 if ( mEditSelectionDrag )
254 mEditSelectionDrag =
false;
258 if ( mFeatureSelectionModel )
265 switch ( event->key() )
268 editOtherFeature( Previous );
272 editOtherFeature( Next );
276 QListView::keyPressEvent( event );
280 void QgsFeatureListView::editOtherFeature( QgsFeatureListView::PositionInList positionInList )
283 if ( 0 != mCurrentEditSelectionModel->selectedIndexes().count() )
285 QModelIndex localIndex = mModel->
mapFromMaster( mCurrentEditSelectionModel->selectedIndexes().first() );
286 currentRow = localIndex.row();
289 QModelIndex newLocalIndex;
290 QModelIndex newIndex;
292 switch ( positionInList )
295 newLocalIndex = mModel->index( 0, 0 );
299 newLocalIndex = mModel->index( currentRow - 1, 0 );
303 newLocalIndex = mModel->index( currentRow + 1, 0 );
307 newLocalIndex = mModel->index( mModel->
rowCount() - 1, 0 );
312 if ( newIndex.isValid() )
315 scrollTo( newLocalIndex );
321 QModelIndex index = indexAt( event->pos() );
323 if ( index.isValid() )
335 menu->exec( event->globalPos() );
339 void QgsFeatureListView::selectRow(
const QModelIndex &index,
bool anchor )
341 QItemSelectionModel::SelectionFlags command = selectionCommand( index );
342 int row = index.row();
347 if ( selectionMode() != QListView::SingleSelection
348 && command.testFlag( QItemSelectionModel::Toggle ) )
351 mCtrlDragSelectionFlag = mFeatureSelectionModel->
isSelected( index )
352 ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
353 command &= ~QItemSelectionModel::Toggle;
354 command |= mCtrlDragSelectionFlag;
356 command |= QItemSelectionModel::Current;
359 QModelIndex tl = model()->index( std::min( mRowAnchor, row ), 0 );
360 QModelIndex br = model()->index( std::max( mRowAnchor, row ), model()->columnCount() - 1 );
362 mFeatureSelectionModel->
selectFeatures( QItemSelection( tl, br ), command );
365 void QgsFeatureListView::ensureEditSelection(
bool inSelection )
370 const QModelIndexList selectedIndexes = mCurrentEditSelectionModel->selectedIndexes();
376 bool editSelectionUpdateRequested =
false;
379 bool validEditSelectionAvailable =
false;
381 if ( selectedIndexes.isEmpty() || !selectedIndexes.first().isValid() || mModel->
mapFromMaster( selectedIndexes.first() ).row() == -1 )
383 validEditSelectionAvailable =
false;
387 validEditSelectionAvailable =
true;
395 if ( !validEditSelectionAvailable )
397 editSelectionUpdateRequested =
true;
404 if ( !selectedFids.contains( mModel->
idxToFid( mModel->
mapFromMaster( selectedIndexes.first() ) ) ) )
406 editSelectionUpdateRequested =
true;
414 if ( !validEditSelectionAvailable )
415 editSelectionUpdateRequested =
true;
418 if ( editSelectionUpdateRequested )
420 if ( !mUpdateEditSelectionTimer.isSingleShot() )
422 mUpdateEditSelectionTimer.setSingleShot(
true );
423 connect( &mUpdateEditSelectionTimer, &QTimer::timeout,
this, [
this, inSelection, validEditSelectionAvailable ]()
430 int rowToSelect = -1;
435 const int rowCount = mModel->
rowCount();
437 for (
int i = 0; i < rowCount; i++ )
439 if ( selectedFids.contains( mModel->
idxToFid( mModel->index( i, 0 ) ) ) )
445 if ( rowToSelect == -1 && !validEditSelectionAvailable )
452 if ( rowToSelect != -1 )
457 mUpdateEditSelectionTimer.setInterval( 0 );
459 mUpdateEditSelectionTimer.start();
465 mFeatureSelectionManager = featureSelectionManager;
467 if ( mFeatureSelectionModel )
471 if ( mOwnedFeatureSelectionManager )
473 mOwnedFeatureSelectionManager->deleteLater();
474 mOwnedFeatureSelectionManager =
nullptr;
virtual bool isSelected(QgsFeatureId fid)
Returns the selection status of a given feature id.
QSet< QgsFeatureId > QgsFeatureIds
void setCurrentFeatureEdited(bool state)
Sets if the currently shown form has received any edit events so far.
QgsFeatureId idxToFid(const QModelIndex &index) const
Returns the feature ID corresponding to an index from the model.
void mouseReleaseEvent(QMouseEvent *event) override
bool setDisplayExpression(const QString &expression)
bool setDisplayExpression(const QString &displayExpression)
The display expression is an expression used to render the fields into a single string which is displ...
void currentEditSelectionProgressChanged(int progress, int count)
Emitted whenever the current edit selection has been changed.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void willShowContextMenu(QgsActionMenu *menu, const QModelIndex &atIndex)
Emitted when the context menu is created to add the specific actions to it.
bool featureByIndex(const QModelIndex &index, QgsFeature &feat)
virtual void selectFeatures(const QItemSelection &selection, QItemSelectionModel::SelectionFlags command)
Select features on this table.
QgsVectorLayer * layer()
Returns the layer to which this cache belongs.
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
void enableSync(bool enable)
Enables or disables synchronisation to the QgsVectorLayer When synchronisation is disabled...
QString parserErrorString()
Returns a detailed message about errors while parsing a QgsExpression.
virtual QModelIndex mapToMaster(const QModelIndex &proxyIndex) const
Get the feature id of the feature in this row.
Shows a list of features and renders a edit button next to each feature.
QgsFeatureListModel * featureListModel()
Gets the featureListModel used by this view.
Q_INVOKABLE const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
void requestRepaint()
Request a repaint of the visible items of connected views.
QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
void setFeatureSelectionModel(QgsFeatureSelectionModel *featureSelectionModel)
QVariant data(const QModelIndex &index, int role) const override
void aboutToChangeEditSelection(bool &ok)
QString parserErrorString()
Returns a detailed message about errors while parsing a QgsExpression.
virtual void setFeatureSelectionManager(QgsIFeatureSelectionManager *featureSelectionManager)
void displayExpressionChanged(const QString &expression)
Emitted whenever the display expression is successfully changed.
QString displayExpression() const
void setCurrentFeatureEdited(bool state)
void selectionChanged(const QgsFeatureIds &selected, const QgsFeatureIds &deselected, bool clearAndSelect)
Emitted when selection was changed.
void attributeValueChanged(QgsFeatureId fid, int idx, const QVariant &value)
Emitted whenever an attribute value change is done in the edit buffer.
QgsFeatureIds currentEditSelection()
Gets the currentEditSelection.
void mouseMoveEvent(QMouseEvent *event) override
QgsAttributeTableModel * masterModel()
virtual QModelIndex mapFromMaster(const QModelIndex &sourceIndex) const
This class caches features of a given QgsVectorLayer.
void setEditSelection(const QgsFeatureIds &fids)
Set the feature(s) to be edited.
const QString displayExpression() const
Returns the expression which is currently used to render the features.
void selectAll() override
Select all currently visible features.
void mousePressEvent(QMouseEvent *event) override
void setFeatureSelectionManager(QgsIFeatureSelectionManager *featureSelectionManager)
setFeatureSelectionManager
void contextMenuEvent(QContextMenuEvent *event) override
void keyPressEvent(QKeyEvent *event) override
virtual QItemSelection mapSelectionFromMaster(const QItemSelection &selection) const
QgsFeatureListView(QWidget *parent=nullptr)
Creates a feature list view.
QgsVectorLayerCache * layerCache()
Returns the vector layer cache which is being used to populate the model.
Element positionToElement(QPoint pos)
QModelIndex fidToIdx(QgsFeatureId fid) const
Returns the model index corresponding to a feature ID.
void setEditSelectionModel(QItemSelectionModel *editSelectionModel)
Is an interface class to abstract feature selection handling.
void currentEditSelectionChanged(QgsFeature &feat)
Emitted whenever the current edit selection has been changed.
virtual void setModel(QgsFeatureListModel *featureListModel)
Set the QgsFeatureListModel which is used to retrieve information.
QgsVectorLayerCache * layerCache()
Returns the layer cache.