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 );
208 Q_ASSERT( index.model() == mModel->
masterModel() || !index.isValid() );
211 mCurrentEditSelectionModel->select( index, command );
216 const auto constIndexes = indexes;
217 for (
const QModelIndex &index : constIndexes )
225 setDirtyRegion( viewport()->rect() );
230 QPoint pos =
event->pos();
232 QModelIndex index = indexAt( pos );
234 if ( mEditSelectionDrag )
240 selectRow( index,
false );
248 if ( mEditSelectionDrag )
250 mEditSelectionDrag =
false;
254 if ( mFeatureSelectionModel )
261 switch ( event->key() )
264 editOtherFeature( Previous );
268 editOtherFeature( Next );
272 QListView::keyPressEvent( event );
276 void QgsFeatureListView::editOtherFeature( QgsFeatureListView::PositionInList positionInList )
279 if ( 0 != mCurrentEditSelectionModel->selectedIndexes().count() )
281 QModelIndex localIndex = mModel->
mapFromMaster( mCurrentEditSelectionModel->selectedIndexes().first() );
282 currentRow = localIndex.row();
285 QModelIndex newLocalIndex;
286 QModelIndex newIndex;
288 switch ( positionInList )
291 newLocalIndex = mModel->index( 0, 0 );
295 newLocalIndex = mModel->index( currentRow - 1, 0 );
299 newLocalIndex = mModel->index( currentRow + 1, 0 );
303 newLocalIndex = mModel->index( mModel->
rowCount() - 1, 0 );
308 if ( newIndex.isValid() )
311 scrollTo( newLocalIndex );
317 QModelIndex index = indexAt( event->pos() );
319 if ( index.isValid() )
327 menu->exec( event->globalPos() );
331 void QgsFeatureListView::selectRow(
const QModelIndex &index,
bool anchor )
333 QItemSelectionModel::SelectionFlags command = selectionCommand( index );
334 int row = index.row();
339 if ( selectionMode() != QListView::SingleSelection
340 && command.testFlag( QItemSelectionModel::Toggle ) )
343 mCtrlDragSelectionFlag = mFeatureSelectionModel->
isSelected( index )
344 ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
345 command &= ~QItemSelectionModel::Toggle;
346 command |= mCtrlDragSelectionFlag;
348 command |= QItemSelectionModel::Current;
351 QModelIndex tl = model()->index( std::min( mRowAnchor, row ), 0 );
352 QModelIndex br = model()->index( std::max( mRowAnchor, row ), model()->columnCount() - 1 );
354 mFeatureSelectionModel->
selectFeatures( QItemSelection( tl, br ), command );
357 void QgsFeatureListView::ensureEditSelection(
bool inSelection )
362 const QModelIndexList selectedIndexes = mCurrentEditSelectionModel->selectedIndexes();
368 bool editSelectionUpdateRequested =
false;
371 bool validEditSelectionAvailable =
false;
373 if ( selectedIndexes.isEmpty() || !selectedIndexes.first().isValid() || mModel->
mapFromMaster( selectedIndexes.first() ).row() == -1 )
375 validEditSelectionAvailable =
false;
379 validEditSelectionAvailable =
true;
387 if ( !validEditSelectionAvailable )
389 editSelectionUpdateRequested =
true;
396 if ( !selectedFids.contains( mModel->
idxToFid( mModel->
mapFromMaster( selectedIndexes.first() ) ) ) )
398 editSelectionUpdateRequested =
true;
406 if ( !validEditSelectionAvailable )
407 editSelectionUpdateRequested =
true;
410 if ( editSelectionUpdateRequested )
412 if ( !mUpdateEditSelectionTimer.isSingleShot() )
414 mUpdateEditSelectionTimer.setSingleShot(
true );
415 connect( &mUpdateEditSelectionTimer, &QTimer::timeout,
this, [
this, inSelection, validEditSelectionAvailable ]()
422 int rowToSelect = -1;
427 const int rowCount = mModel->
rowCount();
429 for (
int i = 0; i < rowCount; i++ )
431 if ( selectedFids.contains( mModel->
idxToFid( mModel->index( i, 0 ) ) ) )
437 if ( rowToSelect == -1 && !validEditSelectionAvailable )
444 if ( rowToSelect != -1 )
449 mUpdateEditSelectionTimer.setInterval( 0 );
451 mUpdateEditSelectionTimer.start();
457 delete mFeatureSelectionManager;
459 mFeatureSelectionManager = featureSelectionManager;
461 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.