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 )
60 mFeatureSelectionModel =
new QgsFeatureSelectionModel( featureListModel, featureListModel, mFeatureSelectionManager,
this );
61 setSelectionModel( mFeatureSelectionModel );
64 ensureEditSelection(
true );
67 if ( mItemDelegate && mItemDelegate->parent() == this )
74 setItemDelegate( mItemDelegate );
81 connect( mCurrentEditSelectionModel, &QItemSelectionModel::selectionChanged,
this, &QgsFeatureListView::editSelectionChanged );
83 connect( featureListModel, &QgsFeatureListModel::rowsRemoved,
this, [
this ]() { ensureEditSelection(); } );
84 connect( featureListModel, &QgsFeatureListModel::rowsInserted,
this, [
this ]() { ensureEditSelection(); } );
85 connect( featureListModel, &QgsFeatureListModel::modelReset,
this, [
this ]() { ensureEditSelection(); } );
114 const QModelIndexList selectedIndexes = mCurrentEditSelectionModel->selectedIndexes();
115 for (
const QModelIndex &idx : selectedIndexes )
125 viewport()->update( visualRegionForSelection( mCurrentEditSelectionModel->selection() ) );
132 QPoint pos =
event->pos();
134 QModelIndex index = indexAt( pos );
138 mEditSelectionDrag =
true;
144 selectRow( index,
true );
150 QgsDebugMsg( QStringLiteral(
"No model assigned to this view" ) );
154 void QgsFeatureListView::editSelectionChanged(
const QItemSelection &deselected,
const QItemSelection &selected )
156 if ( isVisible() && updatesEnabled() )
160 viewport()->update( visualRegionForSelection( localDeselected ) | visualRegionForSelection( localSelected ) );
163 QItemSelection currentSelection = mCurrentEditSelectionModel->selection();
164 if ( currentSelection.size() == 1 )
166 QModelIndexList indexList = currentSelection.indexes();
167 if ( !indexList.isEmpty() )
180 QItemSelection selection;
181 selection.append( QItemSelectionRange( mModel->index( 0, 0 ), mModel->index( mModel->
rowCount() - 1, 0 ) ) );
183 mFeatureSelectionModel->
selectFeatures( selection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
188 QItemSelection selection;
190 const auto constFids = fids;
200 mCurrentEditSelectionModel->select( selection, QItemSelectionModel::ClearAndSelect );
210 qWarning() <<
"Index from wrong model passed in";
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 )
243 selectRow( index,
false );
251 if ( mEditSelectionDrag )
253 mEditSelectionDrag =
false;
257 if ( mFeatureSelectionModel )
264 switch ( event->key() )
267 editOtherFeature( Previous );
271 editOtherFeature( Next );
275 QListView::keyPressEvent( event );
279 void QgsFeatureListView::editOtherFeature( QgsFeatureListView::PositionInList positionInList )
282 if ( 0 != mCurrentEditSelectionModel->selectedIndexes().count() )
284 QModelIndex localIndex = mModel->
mapFromMaster( mCurrentEditSelectionModel->selectedIndexes().first() );
285 currentRow = localIndex.row();
288 QModelIndex newLocalIndex;
289 QModelIndex newIndex;
291 switch ( positionInList )
294 newLocalIndex = mModel->index( 0, 0 );
298 newLocalIndex = mModel->index( currentRow - 1, 0 );
302 newLocalIndex = mModel->index( currentRow + 1, 0 );
306 newLocalIndex = mModel->index( mModel->
rowCount() - 1, 0 );
311 if ( newIndex.isValid() )
314 scrollTo( newLocalIndex );
320 QModelIndex index = indexAt( event->pos() );
322 if ( index.isValid() )
330 menu->exec( event->globalPos() );
334 void QgsFeatureListView::selectRow(
const QModelIndex &index,
bool anchor )
336 QItemSelectionModel::SelectionFlags command = selectionCommand( index );
337 int row = index.row();
342 if ( selectionMode() != QListView::SingleSelection
343 && command.testFlag( QItemSelectionModel::Toggle ) )
346 mCtrlDragSelectionFlag = mFeatureSelectionModel->
isSelected( index )
347 ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
348 command &= ~QItemSelectionModel::Toggle;
349 command |= mCtrlDragSelectionFlag;
351 command |= QItemSelectionModel::Current;
354 QModelIndex tl = model()->index( std::min( mRowAnchor, row ), 0 );
355 QModelIndex br = model()->index( std::max( mRowAnchor, row ), model()->columnCount() - 1 );
357 mFeatureSelectionModel->
selectFeatures( QItemSelection( tl, br ), command );
360 void QgsFeatureListView::ensureEditSelection(
bool inSelection )
365 const QModelIndexList selectedIndexes = mCurrentEditSelectionModel->selectedIndexes();
371 bool editSelectionUpdateRequested =
false;
374 bool validEditSelectionAvailable =
false;
376 if ( selectedIndexes.isEmpty() || mModel->
mapFromMaster( selectedIndexes.first() ).row() == -1 )
378 validEditSelectionAvailable =
false;
382 validEditSelectionAvailable =
true;
390 if ( !validEditSelectionAvailable )
392 editSelectionUpdateRequested =
true;
399 if ( !selectedFids.contains( mModel->
idxToFid( mModel->
mapFromMaster( selectedIndexes.first() ) ) ) )
401 editSelectionUpdateRequested =
true;
409 if ( !validEditSelectionAvailable )
410 editSelectionUpdateRequested =
true;
413 if ( editSelectionUpdateRequested )
415 if ( !mUpdateEditSelectionTimer.isSingleShot() )
417 mUpdateEditSelectionTimer.setSingleShot(
true );
418 connect( &mUpdateEditSelectionTimer, &QTimer::timeout,
this, [
this, inSelection, validEditSelectionAvailable ]()
425 int rowToSelect = -1;
430 const int rowCount = mModel->
rowCount();
432 for (
int i = 0; i < rowCount; i++ )
434 if ( selectedFids.contains( mModel->
idxToFid( mModel->index( i, 0 ) ) ) )
440 if ( rowToSelect == -1 && !validEditSelectionAvailable )
447 if ( rowToSelect != -1 )
452 mUpdateEditSelectionTimer.setInterval( 0 );
454 mUpdateEditSelectionTimer.start();
460 delete mFeatureSelectionManager;
462 mFeatureSelectionManager = featureSelectionManager;
464 if ( mFeatureSelectionModel )
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.
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.
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.