Skip to content

Commit 4c00456

Browse files
committed
[FEATURE]: Apply patch from Stefan Ziegler which adds the option to select decimals places and + signs for numeric labels. Thanks\!
1 parent 5f9f976 commit 4c00456

File tree

5 files changed

+111
-5
lines changed

5 files changed

+111
-5
lines changed

src/app/qgslabelinggui.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,20 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM
145145
btnTextColor->setColor( lyr.textColor );
146146
btnBufferColor->setColor( lyr.bufferColor );
147147

148+
bool formattedNumbers = lyr.formatNumbers;
149+
bool plusSign = lyr.plusSign;
150+
151+
chkFormattedNumbers->setChecked( formattedNumbers );
152+
if ( formattedNumbers )
153+
{
154+
spinDecimals->setValue( lyr.decimals );
155+
}
156+
if ( plusSign )
157+
{
158+
chkPlusSign->setChecked( plusSign );
159+
}
160+
161+
148162
if ( lyr.fontSizeInMapUnits )
149163
{
150164
mFontSizeUnitComboBox->setCurrentIndex( 1 );
@@ -163,6 +177,7 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM
163177

164178
connect( chkBuffer, SIGNAL( toggled( bool ) ), this, SLOT( updateUi() ) );
165179
connect( chkScaleBasedVisibility, SIGNAL( toggled( bool ) ), this, SLOT( updateUi() ) );
180+
connect( chkFormattedNumbers, SIGNAL( toggled( bool ) ), this, SLOT( updateUi() ) );
166181

167182
// setup connection to changes in the placement
168183
QRadioButton* placementRadios[] =
@@ -269,6 +284,18 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
269284
{
270285
lyr.bufferSize = 0;
271286
}
287+
if ( chkFormattedNumbers->isChecked() )
288+
{
289+
lyr.formatNumbers = true;
290+
lyr.decimals = spinDecimals->value();
291+
lyr.plusSign = chkPlusSign->isChecked();
292+
}
293+
else
294+
{
295+
lyr.formatNumbers = false;
296+
lyr.decimals = spinDecimals->value();
297+
lyr.plusSign = true;
298+
}
272299
if ( chkAddDirectionSymbol->isChecked() )
273300
{
274301
lyr.addDirectionSymbol = true;
@@ -451,14 +478,16 @@ void QgsLabelingGui::showEngineConfigDialog()
451478

452479
void QgsLabelingGui::updateUi()
453480
{
454-
// enable/disable scale-based, buffer
481+
// enable/disable scale-based, buffer, decimals
455482
bool buf = chkBuffer->isChecked();
456483
spinBufferSize->setEnabled( buf );
457484
btnBufferColor->setEnabled( buf );
458485

459486
bool scale = chkScaleBasedVisibility->isChecked();
460487
spinScaleMin->setEnabled( scale );
461488
spinScaleMax->setEnabled( scale );
489+
490+
spinDecimals->setEnabled( chkFormattedNumbers->isChecked() );
462491
}
463492

464493
void QgsLabelingGui::changeBufferColor()

src/core/pal/feature.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040

4141
#include <qglobal.h>
4242

43-
4443
#include <cmath>
4544
#include <cstring>
4645
#include <cfloat>

src/core/qgspallabeling.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ QgsPalLayerSettings::QgsPalLayerSettings()
145145
scaleMax = 0;
146146
bufferSize = 1;
147147
bufferColor = Qt::white;
148+
formatNumbers = false;
149+
decimals = 3;
150+
plusSign = false;
148151
labelPerPart = false;
149152
mergeLines = false;
150153
multiLineLabels = false;
@@ -172,6 +175,9 @@ QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
172175
scaleMax = s.scaleMax;
173176
bufferSize = s.bufferSize;
174177
bufferColor = s.bufferColor;
178+
formatNumbers = s.formatNumbers;
179+
decimals = s.decimals;
180+
plusSign = s.plusSign;
175181
labelPerPart = s.labelPerPart;
176182
mergeLines = s.mergeLines;
177183
multiLineLabels = s.multiLineLabels;
@@ -303,6 +309,9 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
303309
scaleMax = layer->customProperty( "labeling/scaleMax" ).toInt();
304310
bufferSize = layer->customProperty( "labeling/bufferSize" ).toDouble();
305311
bufferColor = _readColor( layer, "labeling/bufferColor" );
312+
formatNumbers = layer->customProperty( "labeling/formatNumbers" ).toBool();
313+
decimals = layer->customProperty( "labeling/decimals" ).toInt();
314+
plusSign = layer->customProperty( "labeling/plussign" ).toInt();
306315
labelPerPart = layer->customProperty( "labeling/labelPerPart" ).toBool();
307316
mergeLines = layer->customProperty( "labeling/mergeLines" ).toBool();
308317
multiLineLabels = layer->customProperty( "labeling/multiLineLabels" ).toBool();
@@ -338,6 +347,9 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer )
338347
layer->setCustomProperty( "labeling/scaleMax", scaleMax );
339348
layer->setCustomProperty( "labeling/bufferSize", bufferSize );
340349
_writeColor( layer, "labeling/bufferColor", bufferColor );
350+
layer->setCustomProperty( "labeling/formatNumbers", formatNumbers );
351+
layer->setCustomProperty( "labeling/decimals", decimals );
352+
layer->setCustomProperty( "labeling/plussign", plusSign );
341353
layer->setCustomProperty( "labeling/labelPerPart", labelPerPart );
342354
layer->setCustomProperty( "labeling/mergeLines", mergeLines );
343355
layer->setCustomProperty( "labeling/multiLineLabels", multiLineLabels );
@@ -437,7 +449,25 @@ void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF* fm, QString t
437449

438450
void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext& context )
439451
{
440-
QString labelText = f.attributeMap()[fieldIndex].toString();
452+
453+
QString labelText;
454+
if ( formatNumbers == true
455+
&& ( f.attributeMap()[fieldIndex].type() == QVariant::Int || f.attributeMap()[fieldIndex].type() == QVariant::Double ) )
456+
{
457+
QString numberFormat;
458+
double d = f.attributeMap()[fieldIndex].toDouble();
459+
if ( d > 0 && plusSign == true )
460+
{
461+
numberFormat.append( "+" );
462+
}
463+
numberFormat.append( "%1" );
464+
labelText = numberFormat.arg( d, 0, 'f', decimals );
465+
}
466+
else
467+
{
468+
labelText = f.attributeMap()[fieldIndex].toString();
469+
}
470+
441471
double labelX, labelY; // will receive label size
442472
QFont labelFont = textFont;
443473

src/core/qgspallabeling.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ class CORE_EXPORT QgsPalLayerSettings
111111
int scaleMin, scaleMax; // disabled if both are zero
112112
double bufferSize; //buffer size (in mm)
113113
QColor bufferColor;
114+
bool formatNumbers;
115+
int decimals;
116+
bool plusSign;
114117
bool labelPerPart; // whether to label every feature's part or only the biggest one
115118
bool mergeLines;
116119
bool multiLineLabels; //draw labels on multiple lines if they contain '\n'

src/ui/qgslabelingguibase.ui

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,51 @@
372372
</layout>
373373
</widget>
374374
</item>
375+
<item row="3" column="0">
376+
<widget class="QGroupBox" name="chkFormattedNumbers">
377+
<property name="title">
378+
<string>Formatted numbers</string>
379+
</property>
380+
<property name="checkable">
381+
<bool>true</bool>
382+
</property>
383+
<layout class="QGridLayout" name="gridLayout_15">
384+
<item row="0" column="1">
385+
<widget class="QSpinBox" name="spinDecimals"/>
386+
</item>
387+
<item row="0" column="3">
388+
<spacer name="horizontalSpacer_4">
389+
<property name="orientation">
390+
<enum>Qt::Horizontal</enum>
391+
</property>
392+
<property name="sizeHint" stdset="0">
393+
<size>
394+
<width>468</width>
395+
<height>20</height>
396+
</size>
397+
</property>
398+
</spacer>
399+
</item>
400+
<item row="0" column="0">
401+
<widget class="QLabel" name="label">
402+
<property name="text">
403+
<string>Deicmal places </string>
404+
</property>
405+
</widget>
406+
</item>
407+
<item row="0" column="2">
408+
<widget class="QCheckBox" name="chkPlusSign">
409+
<property name="layoutDirection">
410+
<enum>Qt::RightToLeft</enum>
411+
</property>
412+
<property name="text">
413+
<string>Show plus sign</string>
414+
</property>
415+
</widget>
416+
</item>
417+
</layout>
418+
</widget>
419+
</item>
375420
</layout>
376421
</widget>
377422
<widget class="QWidget" name="tab">
@@ -394,7 +439,7 @@
394439
<x>0</x>
395440
<y>0</y>
396441
<width>643</width>
397-
<height>435</height>
442+
<height>478</height>
398443
</rect>
399444
</property>
400445
<layout class="QGridLayout" name="gridLayout_13">
@@ -848,7 +893,7 @@
848893
<x>0</x>
849894
<y>0</y>
850895
<width>643</width>
851-
<height>532</height>
896+
<height>586</height>
852897
</rect>
853898
</property>
854899
<layout class="QGridLayout" name="gridLayout_11">

0 commit comments

Comments
 (0)