[FEATURE]: Apply patch from Stefan Ziegler which adds the option to select decimals places and + signs for numeric labels. Thanks\!

This commit is contained in:
Marco Hugentobler 2011-07-19 16:22:45 +02:00
parent 5f9f9762bd
commit 4c004569f6
5 changed files with 111 additions and 5 deletions

View File

@ -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 );
@ -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[] =
@ -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;
@ -451,7 +478,7 @@ 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 );
@ -459,6 +486,8 @@ void QgsLabelingGui::updateUi()
bool scale = chkScaleBasedVisibility->isChecked();
spinScaleMin->setEnabled( scale );
spinScaleMax->setEnabled( scale );
spinDecimals->setEnabled( chkFormattedNumbers->isChecked() );
}
void QgsLabelingGui::changeBufferColor()

View File

@ -40,7 +40,6 @@
#include <qglobal.h>
#include <cmath>
#include <cstring>
#include <cfloat>

View File

@ -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;
@ -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;
@ -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();
@ -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 );
@ -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;

View File

@ -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'

View File

@ -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">
@ -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">
@ -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">