Skip to content

Commit

Permalink
Use a single format string using Qt format for renderer label
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanW2 committed Sep 28, 2014
1 parent 428375e commit fcfafa0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 98 deletions.
12 changes: 3 additions & 9 deletions python/core/symbology-ng/qgsgraduatedsymbolrendererv2.sip
Expand Up @@ -46,19 +46,13 @@ class QgsRendererRangeV2LabelFormat
%End
public:
QgsRendererRangeV2LabelFormat();
QgsRendererRangeV2LabelFormat( QString prefix, QString separator, QString suffix, int decimalPlaces=4, bool trimTrailingZeroes=false );
QgsRendererRangeV2LabelFormat( QString format, int decimalPlaces=4, bool trimTrailingZeroes=false );

bool operator==( const QgsRendererRangeV2LabelFormat & other ) const;
bool operator!=( const QgsRendererRangeV2LabelFormat & other ) const;

QString prefix() const;
void setPrefix( QString prefix );

QString separator() const;
void setSeparator( QString separator );

QString suffix() const;
void setSuffix( QString suffix );
QString format() const;
void setFormat( QString format );

int decimalPlaces() const;
void setDecimalPlaces( int decimalPlaces );
Expand Down
24 changes: 7 additions & 17 deletions src/core/symbology-ng/qgsgraduatedsymbolrendererv2.cpp
Expand Up @@ -167,21 +167,17 @@ void QgsRendererRangeV2::toSld( QDomDocument &doc, QDomElement &element, QgsStri
///////////

QgsRendererRangeV2LabelFormat::QgsRendererRangeV2LabelFormat():
mPrefix( "" ),
mSeparator( " - " ),
mSuffix( "" ),
mFormat( " %1 - %2 " ),
mDecimalPlaces( 4 ),
mTrimTrailingZeroes( false ),
mReTrailingZeroes( "\\.?0*$" )
{
}

QgsRendererRangeV2LabelFormat::QgsRendererRangeV2LabelFormat( QString prefix, QString separator, QString suffix, int decimalPlaces, bool trimTrailingZeroes ):
QgsRendererRangeV2LabelFormat::QgsRendererRangeV2LabelFormat( QString format, int decimalPlaces, bool trimTrailingZeroes ):
mReTrailingZeroes( "\\.?0*$" )
{
setPrefix( prefix );
setSeparator( separator );
setSuffix( suffix );
setFormat( format );
setDecimalPlaces( decimalPlaces );
setTrimTrailingZeroes( trimTrailingZeroes );
}
Expand All @@ -190,9 +186,7 @@ QgsRendererRangeV2LabelFormat::QgsRendererRangeV2LabelFormat( QString prefix, QS
bool QgsRendererRangeV2LabelFormat::operator==( const QgsRendererRangeV2LabelFormat &other ) const
{
return
prefix() == other.prefix() &&
separator() == other.separator() &&
suffix() == other.suffix() &&
format() == other.format() &&
decimalPlaces() == other.decimalPlaces() &&
trimTrailingZeroes() == other.trimTrailingZeroes();
}
Expand Down Expand Up @@ -226,23 +220,19 @@ QString QgsRendererRangeV2LabelFormat::labelForRange( double lower, double upper
if ( upperStr.contains( '.' ) ) upperStr = upperStr.replace( mReTrailingZeroes, "" );
}

return mPrefix + lowerStr + mSeparator + upperStr + mSuffix;
return mFormat.arg(lowerStr, upperStr);
}

void QgsRendererRangeV2LabelFormat::setFromDomElement( QDomElement &element )
{
mPrefix = element.attribute( "prefix", "" );
mSeparator = element.attribute( "separator", " - " );
mSuffix = element.attribute( "suffix", "" );
mFormat = element.attribute( "format", " %1 - %2" );
mDecimalPlaces = element.attribute( "decimalplaces", "4" ).toInt();
mTrimTrailingZeroes = element.attribute( "trimtrailingzeroes", "false" ) == "true";
}

void QgsRendererRangeV2LabelFormat::saveToDomElement( QDomElement &element )
{
element.setAttribute( "prefix", mPrefix );
element.setAttribute( "separator", mSeparator );
element.setAttribute( "suffix", mSuffix );
element.setAttribute( "format", mFormat );
element.setAttribute( "decimalplaces", mDecimalPlaces );
element.setAttribute( "trimtrailingzeroes", mTrimTrailingZeroes ? "true" : "false" );
}
Expand Down
16 changes: 4 additions & 12 deletions src/core/symbology-ng/qgsgraduatedsymbolrendererv2.h
Expand Up @@ -71,19 +71,13 @@ class CORE_EXPORT QgsRendererRangeV2LabelFormat
{
public:
QgsRendererRangeV2LabelFormat();
QgsRendererRangeV2LabelFormat( QString prefix, QString separator, QString suffix, int decimalPlaces = 4, bool trimTrailingZeroes = false );
QgsRendererRangeV2LabelFormat( QString format, int decimalPlaces = 4, bool trimTrailingZeroes = false );

bool operator==( const QgsRendererRangeV2LabelFormat & other ) const;
bool operator!=( const QgsRendererRangeV2LabelFormat & other ) const;

QString prefix() const { return mPrefix; }
void setPrefix( QString prefix ) { mPrefix = prefix; }

QString separator() const { return mSeparator; }
void setSeparator( QString separator ) { mSeparator = separator; }

QString suffix() const { return mSuffix; }
void setSuffix( QString suffix ) { mSuffix = suffix; }
QString format() const { return mFormat; }
void setFormat( QString format ) { mFormat = format; }

int decimalPlaces() const { return mDecimalPlaces; }
void setDecimalPlaces( int decimalPlaces );
Expand All @@ -99,9 +93,7 @@ class CORE_EXPORT QgsRendererRangeV2LabelFormat
void saveToDomElement( QDomElement &element );

protected:
QString mPrefix;
QString mSeparator;
QString mSuffix;
QString mFormat;
int mDecimalPlaces;
bool mTrimTrailingZeroes;
QRegExp mReTrailingZeroes;
Expand Down
16 changes: 4 additions & 12 deletions src/gui/symbology-ng/qgsgraduatedsymbolrendererv2widget.cpp
Expand Up @@ -457,9 +457,7 @@ void QgsGraduatedSymbolRendererV2Widget::connectUpdateHandlers()
connect( cbxInvertedColorRamp, SIGNAL( toggled( bool ) ) , this, SLOT( reapplyColorRamp() ) );
connect( spinDecimalPlaces, SIGNAL( valueChanged( int ) ), this, SLOT( labelFormatChanged() ) );
connect( cbxTrimTrailingZeroes, SIGNAL( toggled( bool ) ), this, SLOT( labelFormatChanged() ) );
connect( txtPrefix, SIGNAL( textChanged( QString ) ), this, SLOT( labelFormatChanged() ) );
connect( txtSeparator, SIGNAL( textChanged( QString ) ), this, SLOT( labelFormatChanged() ) );
connect( txtSuffix, SIGNAL( textChanged( QString ) ), this, SLOT( labelFormatChanged() ) );
connect( txtFormat, SIGNAL( textChanged( QString ) ), this, SLOT( labelFormatChanged() ) );

connect( mModel, SIGNAL( rowsMoved() ), this, SLOT( rowsMoved() ) );
connect( mModel, SIGNAL( dataChanged( QModelIndex, QModelIndex ) ), this, SLOT( modelDataChanged( ) ) );
Expand All @@ -475,9 +473,7 @@ void QgsGraduatedSymbolRendererV2Widget::disconnectUpdateHandlers()
disconnect( cbxInvertedColorRamp, SIGNAL( toggled( bool ) ) , this, SLOT( reapplyColorRamp() ) );
disconnect( spinDecimalPlaces, SIGNAL( valueChanged( int ) ), this, SLOT( labelFormatChanged() ) );
disconnect( cbxTrimTrailingZeroes, SIGNAL( toggled( bool ) ), this, SLOT( labelFormatChanged() ) );
disconnect( txtPrefix, SIGNAL( textChanged( QString ) ), this, SLOT( labelFormatChanged() ) );
disconnect( txtSeparator, SIGNAL( textChanged( QString ) ), this, SLOT( labelFormatChanged() ) );
disconnect( txtSuffix, SIGNAL( textChanged( QString ) ), this, SLOT( labelFormatChanged() ) );
disconnect( txtFormat, SIGNAL( textChanged( QString ) ), this, SLOT( labelFormatChanged() ) );

disconnect( mModel, SIGNAL( rowsMoved() ), this, SLOT( rowsMoved() ) );
disconnect( mModel, SIGNAL( dataChanged( QModelIndex, QModelIndex ) ), this, SLOT( modelDataChanged( ) ) );
Expand Down Expand Up @@ -518,9 +514,7 @@ void QgsGraduatedSymbolRendererV2Widget::updateUiFromRenderer( bool updateCount
}

QgsRendererRangeV2LabelFormat labelFormat = mRenderer->labelFormat();
txtPrefix->setText( labelFormat.prefix() );
txtSeparator->setText( labelFormat.separator() );
txtSuffix->setText( labelFormat.suffix() );
txtFormat->setText( labelFormat.format() );
spinDecimalPlaces->setValue( labelFormat.decimalPlaces() );
cbxTrimTrailingZeroes->setChecked( labelFormat.trimTrailingZeroes() );

Expand Down Expand Up @@ -859,9 +853,7 @@ void QgsGraduatedSymbolRendererV2Widget::scaleMethodChanged( QgsSymbolV2::ScaleM
void QgsGraduatedSymbolRendererV2Widget::labelFormatChanged()
{
QgsRendererRangeV2LabelFormat labelFormat = QgsRendererRangeV2LabelFormat(
txtPrefix->text(),
txtSeparator->text(),
txtSuffix->text(),
txtFormat->text(),
spinDecimalPlaces->value(),
cbxTrimTrailingZeroes->isChecked() );
mRenderer->setLabelFormat( labelFormat, true );
Expand Down
66 changes: 18 additions & 48 deletions src/ui/qgsgraduatedsymbolrendererv2widget.ui
Expand Up @@ -6,7 +6,7 @@
<rect>
<x>0</x>
<y>0</y>
<width>637</width>
<width>647</width>
<height>339</height>
</rect>
</property>
Expand Down Expand Up @@ -186,13 +186,13 @@
<item row="3" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Label </string>
<string>Label Format</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
<property name="buddy">
<cstring>txtPrefix</cstring>
<cstring>txtFormat</cstring>
</property>
</widget>
</item>
Expand All @@ -202,55 +202,14 @@
<number>0</number>
</property>
<item>
<widget class="QLineEdit" name="txtPrefix">
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>#.##</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="txtSeparator">
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_17">
<property name="text">
<string>#.##</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="txtSuffix">
<widget class="QLineEdit" name="txtFormat">
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</item>
<item row="3" column="2">
<widget class="QLabel" name="label_16">
<property name="text">
<string>Decimal places</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="buddy">
<cstring>spinDecimalPlaces</cstring>
</property>
</widget>
</item>
<item row="3" column="3">
<layout class="QHBoxLayout" name="horizontalLayout_7">
<item>
Expand Down Expand Up @@ -281,6 +240,19 @@
</item>
</layout>
</item>
<item row="3" column="2">
<widget class="QLabel" name="label_16">
<property name="text">
<string>Decimal places</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="buddy">
<cstring>spinDecimalPlaces</cstring>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand Down Expand Up @@ -386,9 +358,7 @@
</customwidget>
</customwidgets>
<tabstops>
<tabstop>txtPrefix</tabstop>
<tabstop>txtSeparator</tabstop>
<tabstop>txtSuffix</tabstop>
<tabstop>txtFormat</tabstop>
<tabstop>spinDecimalPlaces</tabstop>
<tabstop>cbxTrimTrailingZeroes</tabstop>
<tabstop>btnChangeGraduatedSymbol</tabstop>
Expand Down

0 comments on commit fcfafa0

Please sign in to comment.