From 5967db9bd6e858cbd1677372d893514fd7cad8e6 Mon Sep 17 00:00:00 2001 From: Larry Shaffer Date: Fri, 8 Feb 2013 19:27:26 -0700 Subject: [PATCH] Fix Show/Hide Labels to allow NULL values in table to work for show value --- src/app/qgslabelpropertydialog.cpp | 6 +++++- src/app/qgsmaptoolshowhidelabels.cpp | 23 ++++++++++------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/app/qgslabelpropertydialog.cpp b/src/app/qgslabelpropertydialog.cpp index b54ae7327b6e..ef4d60acddeb 100644 --- a/src/app/qgslabelpropertydialog.cpp +++ b/src/app/qgslabelpropertydialog.cpp @@ -120,9 +120,13 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const switch ( propIt.key() ) { case QgsPalLayerSettings::Show: + { // new scope to assign variables mShowLabelChkbx->setEnabled( true ); - mShowLabelChkbx->setChecked( mCurLabelFeat.attribute( propIt.value() ).toInt() != 0 ); + bool showSuccess; + int showLabel = mCurLabelFeat.attribute( propIt.value() ).toInt( &showSuccess ); + mShowLabelChkbx->setChecked( !showSuccess || showLabel != 0 ); break; + } case QgsPalLayerSettings::AlwaysShow: mAlwaysShowChkbx->setEnabled( true ); mAlwaysShowChkbx->setChecked( mCurLabelFeat.attribute( propIt.value() ).toBool() ); diff --git a/src/app/qgsmaptoolshowhidelabels.cpp b/src/app/qgsmaptoolshowhidelabels.cpp index f53d5fad999b..220460c28fee 100644 --- a/src/app/qgsmaptoolshowhidelabels.cpp +++ b/src/app/qgsmaptoolshowhidelabels.cpp @@ -271,35 +271,32 @@ bool QgsMapToolShowHideLabels::showHideLabel( QgsVectorLayer* vlayer, // verify attribute table has proper field setup bool showSuccess; int showCol; - int show; + int showVal; - if ( !dataDefinedShowHide( vlayer, fid, show, showSuccess, showCol ) ) + if ( !dataDefinedShowHide( vlayer, fid, showVal, showSuccess, showCol ) ) { return false; } + int curVal = hide ? 0 : 1; + // check if attribute value is already the same - QgsFeature f; - if ( !vlayer->getFeatures( QgsFeatureRequest().setFilterFid( fid ).setFlags( QgsFeatureRequest::NoGeometry ) ).nextFeature( f ) ) + if ( showSuccess && showVal == curVal ) { return false; } - int colVal = hide ? 0 : 1; - QVariant fQVal = f.attributes()[showCol]; - bool convToInt; - int fVal = fQVal.toInt( &convToInt ); - - if ( !convToInt || fVal == colVal ) + // allow NULL (maybe default) value to stand for show label (i.e. 1) + // skip NULL attributes if trying to show label + if ( !showSuccess && curVal == 1 ) { return false; } // different attribute value, edit table - QString labelText = currentLabelText( 24 ); QString editTxt = hide ? tr( "Hid label" ) : tr( "Showed label" ); - vlayer->beginEditCommand( editTxt + QString( " '%1'" ).arg( labelText ) ); - if ( !vlayer->changeAttributeValue( fid, showCol, colVal, true ) ) + vlayer->beginEditCommand( editTxt ); + if ( !vlayer->changeAttributeValue( fid, showCol, curVal, false ) ) { QgsDebugMsg( "Failed write to attribute table" ); vlayer->endEditCommand();