Skip to content

Commit 5967db9

Browse files
committed
Fix Show/Hide Labels to allow NULL values in table to work for show value
1 parent 3676a38 commit 5967db9

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/app/qgslabelpropertydialog.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,13 @@ void QgsLabelPropertyDialog::init( const QString& layerId, int featureId, const
120120
switch ( propIt.key() )
121121
{
122122
case QgsPalLayerSettings::Show:
123+
{ // new scope to assign variables
123124
mShowLabelChkbx->setEnabled( true );
124-
mShowLabelChkbx->setChecked( mCurLabelFeat.attribute( propIt.value() ).toInt() != 0 );
125+
bool showSuccess;
126+
int showLabel = mCurLabelFeat.attribute( propIt.value() ).toInt( &showSuccess );
127+
mShowLabelChkbx->setChecked( !showSuccess || showLabel != 0 );
125128
break;
129+
}
126130
case QgsPalLayerSettings::AlwaysShow:
127131
mAlwaysShowChkbx->setEnabled( true );
128132
mAlwaysShowChkbx->setChecked( mCurLabelFeat.attribute( propIt.value() ).toBool() );

src/app/qgsmaptoolshowhidelabels.cpp

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -271,35 +271,32 @@ bool QgsMapToolShowHideLabels::showHideLabel( QgsVectorLayer* vlayer,
271271
// verify attribute table has proper field setup
272272
bool showSuccess;
273273
int showCol;
274-
int show;
274+
int showVal;
275275

276-
if ( !dataDefinedShowHide( vlayer, fid, show, showSuccess, showCol ) )
276+
if ( !dataDefinedShowHide( vlayer, fid, showVal, showSuccess, showCol ) )
277277
{
278278
return false;
279279
}
280280

281+
int curVal = hide ? 0 : 1;
282+
281283
// check if attribute value is already the same
282-
QgsFeature f;
283-
if ( !vlayer->getFeatures( QgsFeatureRequest().setFilterFid( fid ).setFlags( QgsFeatureRequest::NoGeometry ) ).nextFeature( f ) )
284+
if ( showSuccess && showVal == curVal )
284285
{
285286
return false;
286287
}
287288

288-
int colVal = hide ? 0 : 1;
289-
QVariant fQVal = f.attributes()[showCol];
290-
bool convToInt;
291-
int fVal = fQVal.toInt( &convToInt );
292-
293-
if ( !convToInt || fVal == colVal )
289+
// allow NULL (maybe default) value to stand for show label (i.e. 1)
290+
// skip NULL attributes if trying to show label
291+
if ( !showSuccess && curVal == 1 )
294292
{
295293
return false;
296294
}
297295

298296
// different attribute value, edit table
299-
QString labelText = currentLabelText( 24 );
300297
QString editTxt = hide ? tr( "Hid label" ) : tr( "Showed label" );
301-
vlayer->beginEditCommand( editTxt + QString( " '%1'" ).arg( labelText ) );
302-
if ( !vlayer->changeAttributeValue( fid, showCol, colVal, true ) )
298+
vlayer->beginEditCommand( editTxt );
299+
if ( !vlayer->changeAttributeValue( fid, showCol, curVal, false ) )
303300
{
304301
QgsDebugMsg( "Failed write to attribute table" );
305302
vlayer->endEditCommand();

0 commit comments

Comments
 (0)