16 #include <QHeaderView> 38 setSelectionMode( QAbstractItemView::ExtendedSelection );
48 QListView::setModel( featureListModel );
51 delete mFeatureSelectionModel;
53 mCurrentEditSelectionModel =
new QItemSelectionModel( mModel->
masterModel(), this );
54 if ( !mFeatureSelectionManager )
59 mFeatureSelectionModel =
new QgsFeatureSelectionModel( featureListModel, featureListModel, mFeatureSelectionManager,
this );
60 setSelectionModel( mFeatureSelectionModel );
63 ensureEditSelection(
true );
66 if ( mItemDelegate && mItemDelegate->parent() == this )
73 setItemDelegate( mItemDelegate );
80 connect( mCurrentEditSelectionModel, &QItemSelectionModel::selectionChanged,
this, &QgsFeatureListView::editSelectionChanged );
82 connect( featureListModel, &QgsFeatureListModel::rowsRemoved,
this, [
this ]() { ensureEditSelection(); } );
83 connect( featureListModel, &QgsFeatureListModel::rowsInserted,
this, [
this ]() { ensureEditSelection(); } );
84 connect( featureListModel, &QgsFeatureListModel::modelReset,
this, [
this ]() { ensureEditSelection(); } );
113 const QModelIndexList selectedIndexes = mCurrentEditSelectionModel->selectedIndexes();
114 for (
const QModelIndex &idx : selectedIndexes )
124 viewport()->update( visualRegionForSelection( mCurrentEditSelectionModel->selection() ) );
131 QPoint pos =
event->pos();
133 QModelIndex index = indexAt( pos );
137 mEditSelectionDrag =
true;
143 selectRow( index,
true );
149 QgsDebugMsg( QStringLiteral(
"No model assigned to this view" ) );
153 void QgsFeatureListView::editSelectionChanged(
const QItemSelection &deselected,
const QItemSelection &selected )
155 if ( isVisible() && updatesEnabled() )
159 viewport()->update( visualRegionForSelection( localDeselected ) | visualRegionForSelection( localSelected ) );
162 QItemSelection currentSelection = mCurrentEditSelectionModel->selection();
163 if ( currentSelection.size() == 1 )
165 QModelIndexList indexList = currentSelection.indexes();
166 if ( !indexList.isEmpty() )
178 QItemSelection selection;
179 selection.append( QItemSelectionRange( mModel->index( 0, 0 ), mModel->index( mModel->
rowCount() - 1, 0 ) ) );
181 mFeatureSelectionModel->
selectFeatures( selection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
186 QItemSelection selection;
197 mCurrentEditSelectionModel->select( selection, QItemSelectionModel::ClearAndSelect );
207 qWarning() <<
"Index from wrong model passed in";
211 mCurrentEditSelectionModel->select( index, command );
216 Q_FOREACH (
const QModelIndex &index, indexes )
224 setDirtyRegion( viewport()->rect() );
229 QPoint pos =
event->pos();
231 QModelIndex index = indexAt( pos );
233 if ( mEditSelectionDrag )
239 selectRow( index,
false );
247 if ( mEditSelectionDrag )
249 mEditSelectionDrag =
false;
253 if ( mFeatureSelectionModel )
260 if ( Qt::Key_Up == event->key() || Qt::Key_Down ==
event->key() )
263 if ( 0 != mCurrentEditSelectionModel->selectedIndexes().count() )
265 QModelIndex localIndex = mModel->
mapFromMaster( mCurrentEditSelectionModel->selectedIndexes().first() );
266 currentRow = localIndex.row();
269 QModelIndex newLocalIndex;
270 QModelIndex newIndex;
272 switch ( event->key() )
275 newLocalIndex = mModel->index( currentRow - 1, 0 );
277 if ( newIndex.isValid() )
280 scrollTo( newLocalIndex );
285 newLocalIndex = mModel->index( currentRow + 1, 0 );
287 if ( newIndex.isValid() )
290 scrollTo( newLocalIndex );
300 QListView::keyPressEvent( event );
306 QModelIndex index = indexAt( event->pos() );
308 if ( index.isValid() )
316 menu->exec( event->globalPos() );
320 void QgsFeatureListView::selectRow(
const QModelIndex &index,
bool anchor )
322 QItemSelectionModel::SelectionFlags command = selectionCommand( index );
323 int row = index.row();
328 if ( selectionMode() != QListView::SingleSelection
329 && command.testFlag( QItemSelectionModel::Toggle ) )
332 mCtrlDragSelectionFlag = mFeatureSelectionModel->
isSelected( index )
333 ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
334 command &= ~QItemSelectionModel::Toggle;
335 command |= mCtrlDragSelectionFlag;
337 command |= QItemSelectionModel::Current;
340 QModelIndex tl = model()->index( std::min( mRowAnchor, row ), 0 );
341 QModelIndex br = model()->index( std::max( mRowAnchor, row ), model()->columnCount() - 1 );
343 mFeatureSelectionModel->
selectFeatures( QItemSelection( tl, br ), command );
346 void QgsFeatureListView::ensureEditSelection(
bool inSelection )
351 const QModelIndexList selectedIndexes = mCurrentEditSelectionModel->selectedIndexes();
357 bool editSelectionUpdateRequested =
false;
360 bool validEditSelectionAvailable =
false;
362 if ( selectedIndexes.isEmpty() || mModel->
mapFromMaster( selectedIndexes.first() ).row() == -1 )
364 validEditSelectionAvailable =
false;
368 validEditSelectionAvailable =
true;
376 if ( !validEditSelectionAvailable )
378 editSelectionUpdateRequested =
true;
385 if ( !selectedFids.contains( mModel->
idxToFid( mModel->
mapFromMaster( selectedIndexes.first() ) ) ) )
387 editSelectionUpdateRequested =
true;
395 if ( !validEditSelectionAvailable )
396 editSelectionUpdateRequested =
true;
399 if ( editSelectionUpdateRequested )
401 if ( !mUpdateEditSelectionTimer.isSingleShot() )
403 mUpdateEditSelectionTimer.setSingleShot(
true );
404 connect( &mUpdateEditSelectionTimer, &QTimer::timeout,
this, [
this, inSelection, validEditSelectionAvailable ]()
411 int rowToSelect = -1;
416 const int rowCount = mModel->
rowCount();
418 for (
int i = 0; i < rowCount; i++ )
420 if ( selectedFids.contains( mModel->
idxToFid( mModel->index( i, 0 ) ) ) )
426 if ( rowToSelect == -1 && !validEditSelectionAvailable )
433 if ( rowToSelect != -1 )
438 mUpdateEditSelectionTimer.setInterval( 0 );
440 mUpdateEditSelectionTimer.start();
446 delete mFeatureSelectionManager;
448 mFeatureSelectionManager = featureSelectionManager;
450 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.
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...
virtual QModelIndex mapToMaster(const QModelIndex &proxyIndex) const
int rowCount(const QModelIndex &parent=QModelIndex()) const override
void willShowContextMenu(QgsActionMenu *menu, const QModelIndex &atIndex)
Is 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.
virtual QModelIndex mapFromMaster(const QModelIndex &sourceIndex) const
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...
const QString displayExpression() const
Returns the expression which is currently used to render the features.
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.
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.
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)
Is emitted, whenever the display expression is successfully changed.
void setCurrentFeatureEdited(bool state)
void selectionChanged(const QgsFeatureIds &selected, const QgsFeatureIds &deselected, bool clearAndSelect)
This signal is emitted when selection was changed.
void attributeValueChanged(QgsFeatureId fid, int idx, const QVariant &value)
Is emitted whenever an attribute value change is done in the edit buffer.
const QgsFeatureIds & selectedFeatureIds() const
Returns a list of the selected features IDs in this layer.
QgsFeatureIds currentEditSelection()
Gets the currentEditSelection.
void mouseMoveEvent(QMouseEvent *event) override
QgsAttributeTableModel * masterModel()
QModelIndex fidToIdx(QgsFeatureId fid) const
Returns the model index corresponding to a feature ID.
This class caches features of a given QgsVectorLayer.
void setEditSelection(const QgsFeatureIds &fids)
Set the feature(s) to be edited.
void selectAll() override
Select all currently visible features.
void mousePressEvent(QMouseEvent *event) override
void setFeatureSelectionManager(QgsIFeatureSelectionManager *featureSelectionManager)
setFeatureSelectionManager
QgsFeatureId idxToFid(const QModelIndex &index) const
Returns the feature ID corresponding to an index from the model.
void contextMenuEvent(QContextMenuEvent *event) override
void keyPressEvent(QKeyEvent *event) override
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)
void setEditSelectionModel(QItemSelectionModel *editSelectionModel)
Is an interface class to abstract feature selection handling.
void currentEditSelectionChanged(QgsFeature &feat)
Is emitted, whenever the current edit selection has been changed.
virtual QItemSelection mapSelectionFromMaster(const QItemSelection &selection) const
virtual void setModel(QgsFeatureListModel *featureListModel)
Set the QgsFeatureListModel which is used to retrieve information.
QString displayExpression() const
QgsVectorLayerCache * layerCache()
Returns the layer cache.