Skip to content

Commit

Permalink
allow user to control size for NULL values
Browse files Browse the repository at this point in the history
added a field in the assistant to specify the symbol size when
expression evaluates to NULL
  • Loading branch information
vmora authored and nyalldawson committed Jun 24, 2015
1 parent 61fbe1c commit 32a72cd
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 17 deletions.
8 changes: 7 additions & 1 deletion python/core/qgsscaleexpression.sip
Expand Up @@ -35,8 +35,9 @@ class QgsScaleExpression : QgsExpression
* @param maxValue maximum value, corresponds to specified maximum size
* @param minSize minimum size
* @param maxSize maximum size
* @param nullSize size in case expression evaluates to NULL
*/
QgsScaleExpression( Type type, const QString& baseExpression, double minValue, double maxValue, double minSize, double maxSize );
QgsScaleExpression( Type type, const QString& baseExpression, double minValue, double maxValue, double minSize, double maxSize, double nullSize = 0 );

operator bool() const;

Expand Down Expand Up @@ -68,6 +69,11 @@ class QgsScaleExpression : QgsExpression
*/
double maxValue() const;

/** Returns the size value when expression evaluates to NULL.
* @see nullSize
*/
double nullSize() const;

/** Returns the base expression string (or field reference) used for
* calculating the values to be mapped to a size.
*/
Expand Down
18 changes: 12 additions & 6 deletions src/core/qgsscaleexpression.cpp
Expand Up @@ -25,18 +25,20 @@ QgsScaleExpression::QgsScaleExpression( const QString& expression )
, mMaxSize( 10 )
, mMinValue( 0 )
, mMaxValue( 100 )
, mNullSize( 0 )
{
init();
}

QgsScaleExpression::QgsScaleExpression( Type type, const QString& baseExpression, double minValue, double maxValue, double minSize, double maxSize )
: QgsExpression( createExpression( type, baseExpression, minValue, maxValue, minSize, maxSize ) )
QgsScaleExpression::QgsScaleExpression( Type type, const QString& baseExpression, double minValue, double maxValue, double minSize, double maxSize, double nullSize )
: QgsExpression( createExpression( type, baseExpression, minValue, maxValue, minSize, maxSize, nullSize ) )
, mExpression( baseExpression )
, mType( type )
, mMinSize( minSize )
, mMaxSize( maxSize )
, mMinValue( minValue )
, mMaxValue( maxValue )
, mNullSize( nullSize )
{

}
Expand All @@ -62,6 +64,9 @@ void QgsScaleExpression::init()
f = dynamic_cast<const NodeFunction*>( args[0] );
if ( !f )
return;
mNullSize = QgsExpression( args[1]->dump() ).evaluate().toDouble( &ok );
if ( ! ok )
return;
args = f->args()->list();
}

Expand Down Expand Up @@ -106,23 +111,24 @@ void QgsScaleExpression::init()
mExpression = args[0]->dump();
}

QString QgsScaleExpression::createExpression( Type type, const QString & baseExpr, double minValue, double maxValue, double minSize, double maxSize )
QString QgsScaleExpression::createExpression( Type type, const QString & baseExpr, double minValue, double maxValue, double minSize, double maxSize, double nullSize )
{
QString minValueString = QString::number( minValue );
QString maxValueString = QString::number( maxValue );
QString minSizeString = QString::number( minSize );
QString maxSizeString = QString::number( maxSize );
QString nullSizeString = QString::number( nullSize );

switch ( type )
{
case Linear:
return QString( "coalesce(scale_linear(%1, %2, %3, %4, %5), 0)" ).arg( baseExpr, minValueString, maxValueString, minSizeString, maxSizeString );
return QString( "coalesce(scale_linear(%1, %2, %3, %4, %5), %6)" ).arg( baseExpr, minValueString, maxValueString, minSizeString, maxSizeString, nullSizeString );

case Area:
return QString( "coalesce(scale_exp(%1, %2, %3, %4, %5, 0.5), 0)" ).arg( baseExpr, minValueString, maxValueString, minSizeString, maxSizeString );
return QString( "coalesce(scale_exp(%1, %2, %3, %4, %5, 0.5), %6)" ).arg( baseExpr, minValueString, maxValueString, minSizeString, maxSizeString, nullSizeString );

case Flannery:
return QString( "coalesce(scale_exp(%1, %2, %3, %4, %5, 0.57), 0)" ).arg( baseExpr, minValueString, maxValueString, minSizeString, maxSizeString );
return QString( "coalesce(scale_exp(%1, %2, %3, %4, %5, 0.57), %6)" ).arg( baseExpr, minValueString, maxValueString, minSizeString, maxSizeString, nullSizeString );

case Unknown:
break;
Expand Down
11 changes: 9 additions & 2 deletions src/core/qgsscaleexpression.h
Expand Up @@ -51,8 +51,9 @@ class CORE_EXPORT QgsScaleExpression : public QgsExpression
* @param maxValue maximum value, corresponds to specified maximum size
* @param minSize minimum size
* @param maxSize maximum size
* @param nullSize size in case expression evaluates to NULL
*/
QgsScaleExpression( Type type, const QString& baseExpression, double minValue, double maxValue, double minSize, double maxSize );
QgsScaleExpression( Type type, const QString& baseExpression, double minValue, double maxValue, double minSize, double maxSize, double nullSize = 0 );

operator bool() const { return ! mExpression.isEmpty(); }

Expand Down Expand Up @@ -84,6 +85,11 @@ class CORE_EXPORT QgsScaleExpression : public QgsExpression
*/
double maxValue() const { return mMaxValue; }

/** Returns the size value when expression evaluates to NULL.
* @see nullSize
*/
double nullSize() const { return mNullSize; }

/** Returns the base expression string (or field reference) used for
* calculating the values to be mapped to a size.
*/
Expand All @@ -101,9 +107,10 @@ class CORE_EXPORT QgsScaleExpression : public QgsExpression
double mMaxSize;
double mMinValue;
double mMaxValue;
double mNullSize;

void init();
static QString createExpression( Type type, const QString& baseExpr, double minValue, double maxValue, double minSize, double maxSize );
static QString createExpression( Type type, const QString& baseExpr, double minValue, double maxValue, double minSize, double maxSize, double nullSize );

};

Expand Down
6 changes: 5 additions & 1 deletion src/gui/symbology-ng/qgssizescalewidget.cpp
Expand Up @@ -73,6 +73,7 @@ void QgsSizeScaleWidget::setFromSymbol()
maxValueSpinBox->setValue( expr.maxValue() );
minSizeSpinBox->setValue( expr.minSize() );
maxSizeSpinBox->setValue( expr.maxSize() );
nullSizeSpinBox->setValue( expr.nullSize() );
}
updatePreview();
}
Expand Down Expand Up @@ -113,6 +114,7 @@ QgsSizeScaleWidget::QgsSizeScaleWidget( const QgsVectorLayer * layer, const QgsM
maxSizeSpinBox->setShowClearButton( false );
minValueSpinBox->setShowClearButton( false );
maxValueSpinBox->setShowClearButton( false );
nullSizeSpinBox->setShowClearButton( false );

// setup ui from expression if any
setFromSymbol();
Expand All @@ -121,6 +123,7 @@ QgsSizeScaleWidget::QgsSizeScaleWidget( const QgsVectorLayer * layer, const QgsM
connect( maxSizeSpinBox, SIGNAL( valueChanged( double ) ), this, SLOT( updatePreview() ) );
connect( minValueSpinBox, SIGNAL( valueChanged( double ) ), this, SLOT( updatePreview() ) );
connect( maxValueSpinBox, SIGNAL( valueChanged( double ) ), this, SLOT( updatePreview() ) );
connect( nullSizeSpinBox, SIGNAL( valueChanged( double ) ), this, SLOT( updatePreview() ) );
//potentially very expensive for large layers:
connect( mExpressionWidget, SIGNAL( fieldChanged( QString ) ), this, SLOT( computeFromLayerTriggered() ) );
connect( scaleMethodComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( updatePreview() ) );
Expand All @@ -144,7 +147,8 @@ QgsScaleExpression *QgsSizeScaleWidget::createExpression() const
minValueSpinBox->value(),
maxValueSpinBox->value(),
minSizeSpinBox->value(),
maxSizeSpinBox->value() );
maxSizeSpinBox->value(),
nullSizeSpinBox->value() );
}

void QgsSizeScaleWidget::updatePreview()
Expand Down
45 changes: 38 additions & 7 deletions src/ui/symbollayer/widget_size_scale.ui
Expand Up @@ -6,15 +6,15 @@
<rect>
<x>0</x>
<y>0</y>
<width>576</width>
<width>644</width>
<height>343</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<item row="0" column="1">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_2">
Expand Down Expand Up @@ -150,10 +150,7 @@
</item>
</layout>
</item>
<item row="0" column="1" rowspan="2">
<widget class="QTreeView" name="treeView"/>
</item>
<item row="1" column="0">
<item row="1" column="1">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
Expand All @@ -166,7 +163,10 @@
</property>
</spacer>
</item>
<item row="2" column="0" colspan="2">
<item row="0" column="2" rowspan="2">
<widget class="QTreeView" name="treeView"/>
</item>
<item row="6" column="1" colspan="2">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
Expand All @@ -176,6 +176,37 @@
</property>
</widget>
</item>
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_8">
<property name="text">
<string>Size when field is NULL</string>
</property>
</widget>
</item>
<item>
<widget class="QgsDoubleSpinBox" name="nullSizeSpinBox">
<property name="singleStep">
<double>0.100000000000000</double>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>
Expand Down

0 comments on commit 32a72cd

Please sign in to comment.