Skip to content

Commit

Permalink
[FEATURE]: Apply patch from Stefan Ziegler which adds the option to s…
Browse files Browse the repository at this point in the history
…elect decimals places and + signs for numeric labels. Thanks\!
  • Loading branch information
mhugent committed Jul 19, 2011
1 parent 5f9f976 commit 4c00456
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 5 deletions.
31 changes: 30 additions & 1 deletion src/app/qgslabelinggui.cpp
Expand Up @@ -145,6 +145,20 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM
btnTextColor->setColor( lyr.textColor );
btnBufferColor->setColor( lyr.bufferColor );

bool formattedNumbers = lyr.formatNumbers;
bool plusSign = lyr.plusSign;

chkFormattedNumbers->setChecked( formattedNumbers );
if ( formattedNumbers )
{
spinDecimals->setValue( lyr.decimals );
}
if ( plusSign )
{
chkPlusSign->setChecked( plusSign );
}


if ( lyr.fontSizeInMapUnits )
{
mFontSizeUnitComboBox->setCurrentIndex( 1 );
Expand All @@ -163,6 +177,7 @@ QgsLabelingGui::QgsLabelingGui( QgsPalLabeling* lbl, QgsVectorLayer* layer, QgsM

connect( chkBuffer, SIGNAL( toggled( bool ) ), this, SLOT( updateUi() ) );
connect( chkScaleBasedVisibility, SIGNAL( toggled( bool ) ), this, SLOT( updateUi() ) );
connect( chkFormattedNumbers, SIGNAL( toggled( bool ) ), this, SLOT( updateUi() ) );

// setup connection to changes in the placement
QRadioButton* placementRadios[] =
Expand Down Expand Up @@ -269,6 +284,18 @@ QgsPalLayerSettings QgsLabelingGui::layerSettings()
{
lyr.bufferSize = 0;
}
if ( chkFormattedNumbers->isChecked() )
{
lyr.formatNumbers = true;
lyr.decimals = spinDecimals->value();
lyr.plusSign = chkPlusSign->isChecked();
}
else
{
lyr.formatNumbers = false;
lyr.decimals = spinDecimals->value();
lyr.plusSign = true;
}
if ( chkAddDirectionSymbol->isChecked() )
{
lyr.addDirectionSymbol = true;
Expand Down Expand Up @@ -451,14 +478,16 @@ void QgsLabelingGui::showEngineConfigDialog()

void QgsLabelingGui::updateUi()
{
// enable/disable scale-based, buffer
// enable/disable scale-based, buffer, decimals
bool buf = chkBuffer->isChecked();
spinBufferSize->setEnabled( buf );
btnBufferColor->setEnabled( buf );

bool scale = chkScaleBasedVisibility->isChecked();
spinScaleMin->setEnabled( scale );
spinScaleMax->setEnabled( scale );

spinDecimals->setEnabled( chkFormattedNumbers->isChecked() );
}

void QgsLabelingGui::changeBufferColor()
Expand Down
1 change: 0 additions & 1 deletion src/core/pal/feature.cpp
Expand Up @@ -40,7 +40,6 @@

#include <qglobal.h>


#include <cmath>
#include <cstring>
#include <cfloat>
Expand Down
32 changes: 31 additions & 1 deletion src/core/qgspallabeling.cpp
Expand Up @@ -145,6 +145,9 @@ QgsPalLayerSettings::QgsPalLayerSettings()
scaleMax = 0;
bufferSize = 1;
bufferColor = Qt::white;
formatNumbers = false;
decimals = 3;
plusSign = false;
labelPerPart = false;
mergeLines = false;
multiLineLabels = false;
Expand Down Expand Up @@ -172,6 +175,9 @@ QgsPalLayerSettings::QgsPalLayerSettings( const QgsPalLayerSettings& s )
scaleMax = s.scaleMax;
bufferSize = s.bufferSize;
bufferColor = s.bufferColor;
formatNumbers = s.formatNumbers;
decimals = s.decimals;
plusSign = s.plusSign;
labelPerPart = s.labelPerPart;
mergeLines = s.mergeLines;
multiLineLabels = s.multiLineLabels;
Expand Down Expand Up @@ -303,6 +309,9 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer* layer )
scaleMax = layer->customProperty( "labeling/scaleMax" ).toInt();
bufferSize = layer->customProperty( "labeling/bufferSize" ).toDouble();
bufferColor = _readColor( layer, "labeling/bufferColor" );
formatNumbers = layer->customProperty( "labeling/formatNumbers" ).toBool();
decimals = layer->customProperty( "labeling/decimals" ).toInt();
plusSign = layer->customProperty( "labeling/plussign" ).toInt();
labelPerPart = layer->customProperty( "labeling/labelPerPart" ).toBool();
mergeLines = layer->customProperty( "labeling/mergeLines" ).toBool();
multiLineLabels = layer->customProperty( "labeling/multiLineLabels" ).toBool();
Expand Down Expand Up @@ -338,6 +347,9 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer* layer )
layer->setCustomProperty( "labeling/scaleMax", scaleMax );
layer->setCustomProperty( "labeling/bufferSize", bufferSize );
_writeColor( layer, "labeling/bufferColor", bufferColor );
layer->setCustomProperty( "labeling/formatNumbers", formatNumbers );
layer->setCustomProperty( "labeling/decimals", decimals );
layer->setCustomProperty( "labeling/plussign", plusSign );
layer->setCustomProperty( "labeling/labelPerPart", labelPerPart );
layer->setCustomProperty( "labeling/mergeLines", mergeLines );
layer->setCustomProperty( "labeling/multiLineLabels", multiLineLabels );
Expand Down Expand Up @@ -437,7 +449,25 @@ void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF* fm, QString t

void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext& context )
{
QString labelText = f.attributeMap()[fieldIndex].toString();

QString labelText;
if ( formatNumbers == true
&& ( f.attributeMap()[fieldIndex].type() == QVariant::Int || f.attributeMap()[fieldIndex].type() == QVariant::Double ) )
{
QString numberFormat;
double d = f.attributeMap()[fieldIndex].toDouble();
if ( d > 0 && plusSign == true )
{
numberFormat.append( "+" );
}
numberFormat.append( "%1" );
labelText = numberFormat.arg( d, 0, 'f', decimals );
}
else
{
labelText = f.attributeMap()[fieldIndex].toString();
}

double labelX, labelY; // will receive label size
QFont labelFont = textFont;

Expand Down
3 changes: 3 additions & 0 deletions src/core/qgspallabeling.h
Expand Up @@ -111,6 +111,9 @@ class CORE_EXPORT QgsPalLayerSettings
int scaleMin, scaleMax; // disabled if both are zero
double bufferSize; //buffer size (in mm)
QColor bufferColor;
bool formatNumbers;
int decimals;
bool plusSign;
bool labelPerPart; // whether to label every feature's part or only the biggest one
bool mergeLines;
bool multiLineLabels; //draw labels on multiple lines if they contain '\n'
Expand Down
49 changes: 47 additions & 2 deletions src/ui/qgslabelingguibase.ui
Expand Up @@ -372,6 +372,51 @@
</layout>
</widget>
</item>
<item row="3" column="0">
<widget class="QGroupBox" name="chkFormattedNumbers">
<property name="title">
<string>Formatted numbers</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<layout class="QGridLayout" name="gridLayout_15">
<item row="0" column="1">
<widget class="QSpinBox" name="spinDecimals"/>
</item>
<item row="0" column="3">
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>468</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Deicmal places </string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QCheckBox" name="chkPlusSign">
<property name="layoutDirection">
<enum>Qt::RightToLeft</enum>
</property>
<property name="text">
<string>Show plus sign</string>
</property>
</widget>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab">
Expand All @@ -394,7 +439,7 @@
<x>0</x>
<y>0</y>
<width>643</width>
<height>435</height>
<height>478</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_13">
Expand Down Expand Up @@ -848,7 +893,7 @@
<x>0</x>
<y>0</y>
<width>643</width>
<height>532</height>
<height>586</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout_11">
Expand Down

0 comments on commit 4c00456

Please sign in to comment.