Skip to content

Commit

Permalink
fix(TopoChecker): Reset correctly the model
Browse files Browse the repository at this point in the history
Fixes #53425
Reset view model correctly to avoid QAbstractItemModel accessing some missing rows/errors
  • Loading branch information
troopa81 authored and nyalldawson committed Jun 8, 2024
1 parent 01b9fce commit 47712c5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 37 deletions.
17 changes: 11 additions & 6 deletions src/plugins/topology/checkDock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ checkDock::checkDock( QgisInterface *qIface, QWidget *parent )
mFixButton->hide();
mFixBox->hide();

mErrorListModel = new DockFilterModel( mErrorList, parent );
mErrorListModel = new DockFilterModel( parent );
mErrorListModel->setFilterCaseSensitivity( Qt::CaseInsensitive );
mErrorTableView->setModel( mErrorListModel );
mErrorTableView->setSelectionBehavior( QAbstractItemView::SelectRows );
Expand Down Expand Up @@ -156,12 +156,17 @@ void checkDock::deleteErrors()
updateFilterComboBox();

mErrorList.clear();
mErrorListModel->resetModel();
updateModel();

qDeleteAll( mRbErrorMarkers );
mRbErrorMarkers.clear();
}

void checkDock::updateModel()
{
mErrorListModel->setErrors( mErrorList );
}

void checkDock::parseErrorListByLayer( const QString &layerId )
{
QgsVectorLayer *layer = QgsProject::instance()->mapLayer<QgsVectorLayer *>( layerId );
Expand All @@ -179,7 +184,7 @@ void checkDock::parseErrorListByLayer( const QString &layerId )
++it;
}

mErrorListModel->resetModel();
updateModel();
mComment->setText( tr( "No errors were found" ) );
}

Expand All @@ -200,7 +205,7 @@ void checkDock::parseErrorListByFeature( int featureId )
}

mComment->setText( tr( "No errors were found" ) );
mErrorListModel->resetModel();
updateModel();
}

void checkDock::configure()
Expand Down Expand Up @@ -323,7 +328,7 @@ void checkDock::fix()
if ( mErrorList.at( row )->fix( fixName ) )
{
mErrorList.removeAt( row );
mErrorListModel->resetModel();
updateModel();
//parseErrorListByFeature();
mComment->setText( tr( "%n error(s) were found", nullptr, mErrorList.count() ) );
qgsInterface->mapCanvas()->refresh();
Expand Down Expand Up @@ -402,7 +407,7 @@ void checkDock::runTests( ValidateType type )
updateFilterComboBox();

mToggleRubberband->setChecked( true );
mErrorListModel->resetModel();
updateModel();
}

void checkDock::updateFilterComboBox()
Expand Down
7 changes: 7 additions & 0 deletions src/plugins/topology/checkDock.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ class checkDock : public QgsDockWidget, private Ui::checkDock
void filterErrors();

private:

/**
* Update check table model according to current errors
* \since QGIS 3.38
*/
void updateModel();

rulesDialog *mConfigureDialog = nullptr;

QObjectUniquePtr<QgsRubberBand> mRBConflict;
Expand Down
33 changes: 17 additions & 16 deletions src/plugins/topology/dockModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@
#include "qgsvectorlayer.h"
#include <qlogging.h>

DockModel::DockModel( ErrorList &errorList, QObject *parent = nullptr )
: mErrorlist( errorList )
DockModel::DockModel( QObject *parent )
{
Q_UNUSED( parent )
mHeader << QObject::tr( "Error" ) << QObject::tr( "Layer" ) << QObject::tr( "Feature ID" );

}

void DockModel::setErrors( const ErrorList &errorList )
{
beginResetModel();
mErrorlist = errorList;
endResetModel();
}

int DockModel::rowCount( const QModelIndex &parent ) const
{
Q_UNUSED( parent )
Expand All @@ -48,12 +54,13 @@ QVariant DockModel::headerData( int section, Qt::Orientation orientation, int ro
{
return QVariant( section );
}
else
else if ( section >= 0 && section < mHeader.count() )
{
return mHeader[section];
}
}
else return QVariant();

return QAbstractItemModel::headerData( section, orientation, role );
}

QVariant DockModel::data( const QModelIndex &index, int role ) const
Expand Down Expand Up @@ -122,32 +129,26 @@ Qt::ItemFlags DockModel::flags( const QModelIndex &index ) const
return flags;
}

void DockModel::resetModel()
{
beginResetModel();
endResetModel();
}

void DockModel::reload( const QModelIndex &index1, const QModelIndex &index2 )

{
emit dataChanged( index1, index2 );
}

DockFilterModel::DockFilterModel( ErrorList &errorList, QObject *parent = nullptr )
DockFilterModel::DockFilterModel( QObject *parent )
: QSortFilterProxyModel( parent )
, mDockModel( new DockModel( errorList, parent ) )
, mDockModel( new DockModel( parent ) )
{
setSourceModel( mDockModel );
setFilterKeyColumn( 0 );
}

void DockFilterModel::reload( const QModelIndex &index1, const QModelIndex &index2 )
void DockFilterModel::setErrors( const ErrorList &errorList )
{
mDockModel->reload( index1, index2 );
mDockModel->setErrors( errorList );
}

void DockFilterModel::resetModel()
void DockFilterModel::reload( const QModelIndex &index1, const QModelIndex &index2 )
{
mDockModel->resetModel();
mDockModel->reload( index1, index2 );
}
29 changes: 14 additions & 15 deletions src/plugins/topology/dockModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ class DockModel : public QAbstractTableModel

/**
* Constructor
* \param errorList reference to the ErrorList where errors will be stored
* \param parent parent object
*/
DockModel( ErrorList &errorList, QObject *parent );
DockModel( QObject *parent = nullptr );

/**
* \param errorList reference to the ErrorList where errors will be stored
* \since 3.38
*/
void setErrors( const ErrorList &errorList );

/**
* Returns header data
Expand Down Expand Up @@ -86,13 +91,8 @@ class DockModel : public QAbstractTableModel
*/
void reload( const QModelIndex &index1, const QModelIndex &index2 );

/**
* Resets the model
*/
void resetModel();

private:
ErrorList &mErrorlist;
ErrorList mErrorlist;
QList<QString> mHeader;
};

Expand All @@ -104,25 +104,24 @@ class DockFilterModel : public QSortFilterProxyModel

/**
* Constructor
* \param errorList reference to the ErrorList where errors will be stored
* \param parent parent object
*/
DockFilterModel( ErrorList &errorList, QObject *parent );
DockFilterModel( QObject *parent = nullptr );

~DockFilterModel() = default;

/**
* \param errorList reference to the ErrorList where errors will be stored
*/
void setErrors( const ErrorList &errorList );

/**
* Reloads the model data between indices
* \param index1 start index
* \param index2 end index
*/
void reload( const QModelIndex &index1, const QModelIndex &index2 );

/**
* Resets the model
*/
void resetModel();

private:

DockModel *mDockModel = nullptr;
Expand Down

0 comments on commit 47712c5

Please sign in to comment.