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 );
62 if ( mItemDelegate && mItemDelegate->parent() == this )
69 setItemDelegate( mItemDelegate );
76 connect( mCurrentEditSelectionModel, &QItemSelectionModel::selectionChanged,
this, &QgsFeatureListView::editSelectionChanged );
78 connect( featureListModel, &QgsFeatureListModel::rowsRemoved,
this, &QgsFeatureListView::ensureEditSelection );
79 connect( featureListModel, &QgsFeatureListModel::rowsInserted,
this, &QgsFeatureListView::ensureEditSelection );
80 connect( featureListModel, &QgsFeatureListModel::modelReset,
this, &QgsFeatureListView::ensureEditSelection );
109 const QModelIndexList selectedIndexes = mCurrentEditSelectionModel->selectedIndexes();
110 for (
const QModelIndex &idx : selectedIndexes )
120 viewport()->update( visualRegionForSelection( mCurrentEditSelectionModel->selection() ) );
127 QPoint pos =
event->pos();
129 QModelIndex index = indexAt( pos );
133 mEditSelectionDrag =
true;
139 selectRow( index,
true );
149 void QgsFeatureListView::editSelectionChanged(
const QItemSelection &deselected,
const QItemSelection &selected )
151 if ( isVisible() && updatesEnabled() )
155 viewport()->update( visualRegionForSelection( localDeselected ) | visualRegionForSelection( localSelected ) );
158 QItemSelection currentSelection = mCurrentEditSelectionModel->selection();
159 if ( currentSelection.size() == 1 )
161 QModelIndexList indexList = currentSelection.indexes();
162 if ( !indexList.isEmpty() )
174 QItemSelection selection;
175 selection.append( QItemSelectionRange( mModel->
index( 0, 0 ), mModel->
index( mModel->
rowCount() - 1, 0 ) ) );
177 mFeatureSelectionModel->
selectFeatures( selection, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows );
182 QItemSelection selection;
193 mCurrentEditSelectionModel->select( selection, QItemSelectionModel::ClearAndSelect );
202 mCurrentEditSelectionModel->select( index, command );
207 Q_FOREACH (
const QModelIndex &index, indexes )
215 setDirtyRegion( viewport()->rect() );
220 QPoint pos =
event->pos();
222 QModelIndex index = indexAt( pos );
224 if ( mEditSelectionDrag )
230 selectRow( index,
false );
238 if ( mEditSelectionDrag )
240 mEditSelectionDrag =
false;
244 if ( mFeatureSelectionModel )
251 if ( Qt::Key_Up == event->key() || Qt::Key_Down ==
event->key() )
254 if ( 0 != mCurrentEditSelectionModel->selectedIndexes().count() )
256 QModelIndex localIndex = mModel->
mapFromMaster( mCurrentEditSelectionModel->selectedIndexes().first() );
257 currentRow = localIndex.row();
260 QModelIndex newLocalIndex;
261 QModelIndex newIndex;
263 switch ( event->key() )
266 newLocalIndex = mModel->
index( currentRow - 1, 0 );
268 if ( newIndex.isValid() )
271 scrollTo( newLocalIndex );
276 newLocalIndex = mModel->
index( currentRow + 1, 0 );
278 if ( newIndex.isValid() )
281 scrollTo( newLocalIndex );
291 QListView::keyPressEvent( event );
297 QModelIndex index = indexAt( event->pos() );
299 if ( index.isValid() )
307 menu->exec( event->globalPos() );
311 void QgsFeatureListView::selectRow(
const QModelIndex &index,
bool anchor )
313 QItemSelectionModel::SelectionFlags command = selectionCommand( index );
314 int row = index.row();
319 if ( selectionMode() != QListView::SingleSelection
320 && command.testFlag( QItemSelectionModel::Toggle ) )
323 mCtrlDragSelectionFlag = mFeatureSelectionModel->
isSelected( index )
324 ? QItemSelectionModel::Deselect : QItemSelectionModel::Select;
325 command &= ~QItemSelectionModel::Toggle;
326 command |= mCtrlDragSelectionFlag;
328 command |= QItemSelectionModel::Current;
331 QModelIndex tl = model()->index( std::min( mRowAnchor, row ), 0 );
332 QModelIndex br = model()->index( std::max( mRowAnchor, row ), model()->columnCount() - 1 );
334 mFeatureSelectionModel->
selectFeatures( QItemSelection( tl, br ), command );
337 void QgsFeatureListView::ensureEditSelection()
339 QModelIndexList selectedIndexes = mCurrentEditSelectionModel->selectedIndexes();
341 if ( ( selectedIndexes.isEmpty()
342 || mModel->
mapFromMaster( selectedIndexes.first() ).row() == -1 )
345 mCurrentEditSelectionModel->select( mModel->
mapToMaster( mModel->
index( 0, 0 ) ), QItemSelectionModel::Select );
351 delete mFeatureSelectionManager;
353 mFeatureSelectionManager = featureSelectionManager;
355 if ( mFeatureSelectionModel )
virtual bool isSelected(QgsFeatureId fid)
Returns the selection status of a given feature id.
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...
QSet< QgsFeatureId > QgsFeatureIds
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.
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.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Shows a list of features and renders a edit button next to each feature.
QgsFeatureListModel * featureListModel()
Get 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.
QString displayExpression() const
void setCurrentFeatureEdited(bool state)
void attributeValueChanged(QgsFeatureId fid, int idx, const QVariant &value)
Is emitted whenever an attribute value change is done in the edit buffer.
QgsFeatureIds currentEditSelection()
Get 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
QModelIndex fidToIdx(const QgsFeatureId fid) const
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()
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 void setModel(QgsFeatureListModel *featureListModel)
Set the QgsFeatureListModel which is used to retrieve information.
QgsVectorLayerCache * layerCache()
Returns the layer cache.