Skip to content

Commit

Permalink
Merge pull request #7274 from m-kuhn/raii
Browse files Browse the repository at this point in the history
Code cleanup
  • Loading branch information
m-kuhn committed Jun 20, 2018
2 parents 77d2284 + bf4604f commit 2778266
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp
Expand Up @@ -157,9 +157,11 @@ void QgsValueRelationWidgetWrapper::setValue( const QVariant &value )

// This block is needed because item->setCheckState triggers dataChanged gets back to value()
// and iterate over all items again! This can be extremely slow on large items sets.
mTableWidget->blockSignals( true );
for ( int j = 0; j < mTableWidget->rowCount(); j++ )
{
auto signalBlockedTableWidget = whileBlocking( mTableWidget );
Q_UNUSED( signalBlockedTableWidget )

for ( int i = 0; i < nofColumns; ++i )
{
QTableWidgetItem *item = mTableWidget->item( j, i );
Expand All @@ -170,7 +172,6 @@ void QgsValueRelationWidgetWrapper::setValue( const QVariant &value )
}
}
}
mTableWidget->blockSignals( false );
// let's trigger the signal now, once and for all
if ( lastChangedItem )
lastChangedItem->setCheckState( checkList.contains( lastChangedItem->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
Expand Down Expand Up @@ -222,12 +223,12 @@ void QgsValueRelationWidgetWrapper::setFeature( const QgsFeature &feature )
// and signals unblocked (we want this to propagate to the feature itself)
if ( formFeature().isValid()
&& ! formFeature().attribute( fieldIdx() ).isValid()
&& mCache.size() > 0
&& ! mCache.empty()
&& ! config( QStringLiteral( "AllowNull" ) ).toBool( ) )
{
// This is deferred because at the time the feature is set in one widget it is not
// set in the next, which is typically the "down" in a drill-down
QTimer::singleShot( 0, [ = ]
QTimer::singleShot( 0, [ this ]
{
setValue( mCache.at( 0 ).key );
} );
Expand All @@ -246,7 +247,7 @@ void QgsValueRelationWidgetWrapper::populate( )
{
mCache = QgsValueRelationFieldFormatter::createCache( config( ), formFeature() );
}
else if ( mCache.isEmpty() )
else if ( mCache.empty() )
{
mCache = QgsValueRelationFieldFormatter::createCache( config( ) );
}
Expand All @@ -268,7 +269,7 @@ void QgsValueRelationWidgetWrapper::populate( )
{
const int nofColumns = columnCount();

if ( mCache.size() > 0 )
if ( ! mCache.empty() )
{
mTableWidget->setRowCount( ( mCache.size() + nofColumns - 1 ) / nofColumns );
}
Expand All @@ -277,7 +278,8 @@ void QgsValueRelationWidgetWrapper::populate( )
mTableWidget->setColumnCount( nofColumns );

whileBlocking( mTableWidget )->clear();
int row = 0, column = 0;
int row = 0;
int column = 0;
for ( const QgsValueRelationFieldFormatter::ValueRelationItem &element : qgis::as_const( mCache ) )
{
if ( column == nofColumns )
Expand Down Expand Up @@ -340,7 +342,9 @@ void QgsValueRelationWidgetWrapper::setEnabled( bool enabled )

if ( mTableWidget )
{
mTableWidget->blockSignals( true );
auto signalBlockedTableWidget = whileBlocking( mTableWidget );
Q_UNUSED( signalBlockedTableWidget )

for ( int j = 0; j < mTableWidget->rowCount(); j++ )
{
for ( int i = 0; i < mTableWidget->columnCount(); ++i )
Expand All @@ -355,7 +359,6 @@ void QgsValueRelationWidgetWrapper::setEnabled( bool enabled )
}
}
}
mTableWidget->blockSignals( false );
}
else
QgsEditorWidgetWrapper::setEnabled( enabled );
Expand Down

0 comments on commit 2778266

Please sign in to comment.