Skip to content
Permalink
Browse files

Added possibility to scale a markers area. Up to now, only the diamet…

…er could be scaled.
  • Loading branch information
m-kuhn committed Aug 13, 2012
1 parent f378a2a commit ca9d49bfc6c1af7d7f8269cf015746c819d8fe72
@@ -130,13 +130,19 @@ class CORE_EXPORT QgsCategorizedSymbolRendererV2 : public QgsFeatureRendererV2
//! @note added in 1.6
QString sizeScaleField() const { return mSizeScaleField; }

//! @note added in 2.0
void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod ) { mScaleMethod = scaleMethod; }
//! @note added in 2.0
QgsSymbolV2::ScaleMethod scaleMethod() const { return mScaleMethod; }

protected:
QString mAttrName;
QgsCategoryList mCategories;
QgsSymbolV2* mSourceSymbol;
QgsVectorColorRampV2* mSourceColorRamp;
QString mRotationField;
QString mSizeScaleField;
QgsSymbolV2::ScaleMethod mScaleMethod;

//! attribute index (derived from attribute name in startRender)
int mAttrNum;
@@ -151,6 +151,12 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
//! @note added in 1.6
QString sizeScaleField() const { return mSizeScaleField; }

//! @note added in 2.0
void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod ) { mScaleMethod = scaleMethod; }
//! @note added in 2.0
QgsSymbolV2::ScaleMethod scaleMethod() const { return mScaleMethod; }


protected:
QString mAttrName;
QgsRangeList mRanges;
@@ -159,6 +165,7 @@ class CORE_EXPORT QgsGraduatedSymbolRendererV2 : public QgsFeatureRendererV2
QgsVectorColorRampV2* mSourceColorRamp;
QString mRotationField;
QString mSizeScaleField;
QgsSymbolV2::ScaleMethod mScaleMethod;

//! attribute index (derived from attribute name in startRender)
int mAttrNum;
@@ -48,14 +48,15 @@ static QPointF _rotatedOffset( const QPointF& offset, double angle )

//////

QgsSimpleMarkerSymbolLayerV2::QgsSimpleMarkerSymbolLayerV2( QString name, QColor color, QColor borderColor, double size, double angle )
QgsSimpleMarkerSymbolLayerV2::QgsSimpleMarkerSymbolLayerV2( QString name, QColor color, QColor borderColor, double size, double angle, QgsSymbolV2::ScaleMethod scaleMethod )
{
mName = name;
mColor = color;
mBorderColor = borderColor;
mSize = size;
mAngle = angle;
mOffset = QPointF( 0, 0 );
mScaleMethod = scaleMethod;
}

QgsSymbolLayerV2* QgsSimpleMarkerSymbolLayerV2::create( const QgsStringMap& props )
@@ -65,6 +66,7 @@ QgsSymbolLayerV2* QgsSimpleMarkerSymbolLayerV2::create( const QgsStringMap& prop
QColor borderColor = DEFAULT_SIMPLEMARKER_BORDERCOLOR;
double size = DEFAULT_SIMPLEMARKER_SIZE;
double angle = DEFAULT_SIMPLEMARKER_ANGLE;
QgsSymbolV2::ScaleMethod scaleMethod = DEFAULT_SCALE_METHOD;

if ( props.contains( "name" ) )
name = props["name"];
@@ -76,8 +78,10 @@ QgsSymbolLayerV2* QgsSimpleMarkerSymbolLayerV2::create( const QgsStringMap& prop
size = props["size"].toDouble();
if ( props.contains( "angle" ) )
angle = props["angle"].toDouble();
if ( props.contains( "scale_method" ) )
scaleMethod = QgsSymbolLayerV2Utils::decodeScaleMethod( props["scale_method"] );

QgsSimpleMarkerSymbolLayerV2* m = new QgsSimpleMarkerSymbolLayerV2( name, color, borderColor, size, angle );
QgsSimpleMarkerSymbolLayerV2* m = new QgsSimpleMarkerSymbolLayerV2( name, color, borderColor, size, angle, scaleMethod );
if ( props.contains( "offset" ) )
m->setOffset( QgsSymbolLayerV2Utils::decodePoint( props["offset"] ) );
return m;
@@ -402,6 +406,14 @@ void QgsSimpleMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV
if ( hasDataDefinedSize )
{
double scaledSize = context.outputLineWidth( mSize );

switch( mScaleMethod )
{
case QgsSymbolV2::ScaleArea:
scaledSize = sqrt( scaledSize );
break;
}

double half = scaledSize / 2.0;
transform.scale( half, half );
}
@@ -432,12 +444,13 @@ QgsStringMap QgsSimpleMarkerSymbolLayerV2::properties() const
map["size"] = QString::number( mSize );
map["angle"] = QString::number( mAngle );
map["offset"] = QgsSymbolLayerV2Utils::encodePoint( mOffset );
map["scale_method"] = QgsSymbolLayerV2Utils::encodeScaleMethod( mScaleMethod );
return map;
}

QgsSymbolLayerV2* QgsSimpleMarkerSymbolLayerV2::clone() const
{
QgsSimpleMarkerSymbolLayerV2* m = new QgsSimpleMarkerSymbolLayerV2( mName, mColor, mBorderColor, mSize, mAngle );
QgsSimpleMarkerSymbolLayerV2* m = new QgsSimpleMarkerSymbolLayerV2( mName, mColor, mBorderColor, mSize, mAngle, mScaleMethod );
m->setOffset( mOffset );
return m;
}
@@ -23,6 +23,7 @@
#define DEFAULT_SIMPLEMARKER_BORDERCOLOR QColor(0,0,0)
#define DEFAULT_SIMPLEMARKER_SIZE DEFAULT_POINT_SIZE
#define DEFAULT_SIMPLEMARKER_ANGLE 0
#define DEFAULT_SCALE_METHOD QgsSymbolV2::ScaleArea

#include <QPen>
#include <QBrush>
@@ -37,7 +38,8 @@ class CORE_EXPORT QgsSimpleMarkerSymbolLayerV2 : public QgsMarkerSymbolLayerV2
QColor color = DEFAULT_SIMPLEMARKER_COLOR,
QColor borderColor = DEFAULT_SIMPLEMARKER_BORDERCOLOR,
double size = DEFAULT_SIMPLEMARKER_SIZE,
double angle = DEFAULT_SIMPLEMARKER_ANGLE );
double angle = DEFAULT_SIMPLEMARKER_ANGLE,
QgsSymbolV2::ScaleMethod scaleMethod = DEFAULT_SCALE_METHOD );

// static stuff

@@ -61,6 +61,7 @@ QgsSymbolV2* QgsSingleSymbolRendererV2::symbolForFeature( QgsFeature& feature )
markerSymbol->setAngle( rotation );
if ( mSizeScaleFieldIdx != -1 )
markerSymbol->setSize( sizeScale * mOrigSize );
markerSymbol->setScaleMethod( mScaleMethod );
}
else if ( mTempSymbol->type() == QgsSymbolV2::Line )
{
@@ -183,6 +184,7 @@ QgsFeatureRendererV2* QgsSingleSymbolRendererV2::clone()
r->setUsingSymbolLevels( usingSymbolLevels() );
r->setRotationField( rotationField() );
r->setSizeScaleField( sizeScaleField() );
r->setScaleMethod( scaleMethod() );
return r;
}

@@ -233,7 +235,10 @@ QgsFeatureRendererV2* QgsSingleSymbolRendererV2::create( QDomElement& element )

QDomElement sizeScaleElem = element.firstChildElement( "sizescale" );
if ( !sizeScaleElem.isNull() )
{
r->setSizeScaleField( sizeScaleElem.attribute( "field" ) );
r->setScaleMethod( QgsSymbolLayerV2Utils::decodeScaleMethod( sizeScaleElem.attribute( "scalemethod" ) ) );
}

// TODO: symbol levels
return r;
@@ -344,6 +349,7 @@ QDomElement QgsSingleSymbolRendererV2::save( QDomDocument& doc )

QDomElement sizeScaleElem = doc.createElement( "sizescale" );
sizeScaleElem.setAttribute( "field", mSizeScaleField );
sizeScaleElem.setAttribute( "scalemethod", QgsSymbolLayerV2Utils::encodeScaleMethod( mScaleMethod ) );
rendererElem.appendChild( sizeScaleElem );

return rendererElem;
@@ -17,6 +17,7 @@

#include "qgis.h"
#include "qgsrendererv2.h"
#include "qgssymbolv2.h"

class CORE_EXPORT QgsSingleSymbolRendererV2 : public QgsFeatureRendererV2
{
@@ -47,6 +48,11 @@ class CORE_EXPORT QgsSingleSymbolRendererV2 : public QgsFeatureRendererV2
//! @note added in 1.5
QString sizeScaleField() const { return mSizeScaleField; }

//! @note added in 2.0
void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod ) { mScaleMethod = scaleMethod; }
//! @note added in 2.0
QgsSymbolV2::ScaleMethod scaleMethod() const { return mScaleMethod; }

virtual QString dump();

virtual QgsFeatureRendererV2* clone();
@@ -77,6 +83,7 @@ class CORE_EXPORT QgsSingleSymbolRendererV2 : public QgsFeatureRendererV2
QgsSymbolV2* mSymbol;
QString mRotationField;
QString mSizeScaleField;
QgsSymbolV2::ScaleMethod mScaleMethod;

// temporary stuff for rendering
int mRotationFieldIdx, mSizeScaleFieldIdx;
@@ -105,6 +105,9 @@ class CORE_EXPORT QgsMarkerSymbolLayerV2 : public QgsSymbolLayerV2
void setSize( double size ) { mSize = size; }
double size() const { return mSize; }

void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod ) { mScaleMethod = scaleMethod; }
QgsSymbolV2::ScaleMethod scaleMethod() const { return mScaleMethod; }

void setOffset( QPointF offset ) { mOffset = offset; }
QPointF offset() { return mOffset; }

@@ -119,6 +122,7 @@ class CORE_EXPORT QgsMarkerSymbolLayerV2 : public QgsSymbolLayerV2
double mAngle;
double mSize;
QPointF mOffset;
QgsSymbolV2::ScaleMethod mScaleMethod;
};

class CORE_EXPORT QgsLineSymbolLayerV2 : public QgsSymbolLayerV2
@@ -448,6 +448,38 @@ QVector<qreal> QgsSymbolLayerV2Utils::decodeSldRealVector( const QString& s )
return resultVector;
}

QString QgsSymbolLayerV2Utils::encodeScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod )
{
QString encodedValue;

switch( scaleMethod )
{
case QgsSymbolV2::ScaleDiameter:
encodedValue = "diameter";
break;
case QgsSymbolV2::ScaleArea:
encodedValue = "area";
break;
}
return encodedValue;
}

QgsSymbolV2::ScaleMethod QgsSymbolLayerV2Utils::decodeScaleMethod( QString str )
{
QgsSymbolV2::ScaleMethod scaleMethod;

if ( str == "diameter" )
{
scaleMethod = QgsSymbolV2::ScaleDiameter;
}
else
{
scaleMethod = QgsSymbolV2::ScaleArea;
}

return scaleMethod;
}

QIcon QgsSymbolLayerV2Utils::symbolPreviewIcon( QgsSymbolV2* symbol, QSize size )
{
return QIcon( symbolPreviewPixmap( symbol, size ) );
@@ -90,6 +90,9 @@ class CORE_EXPORT QgsSymbolLayerV2Utils
static QString encodeSldUom( QgsSymbolV2::OutputUnit unit, double *scaleFactor );
static QgsSymbolV2::OutputUnit decodeSldUom( QString str, double *scaleFactor );

static QString encodeScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
static QgsSymbolV2::ScaleMethod decodeScaleMethod( QString str );

static QIcon symbolPreviewIcon( QgsSymbolV2* symbol, QSize size );
static QIcon symbolLayerPreviewIcon( QgsSymbolLayerV2* layer, QgsSymbolV2::OutputUnit u, QSize size );
static QIcon colorRampPreviewIcon( QgsVectorColorRampV2* ramp, QSize size );
@@ -444,7 +444,7 @@ double QgsMarkerSymbolV2::angle()
return 0;

// return angle of the first symbol layer
const QgsMarkerSymbolLayerV2 *layer = static_cast<const QgsMarkerSymbolLayerV2 *>( *it );
const QgsMarkerSymbolLayerV2* layer = static_cast<const QgsMarkerSymbolLayerV2 *>( *it );
return layer->angle();
}

@@ -480,6 +480,28 @@ double QgsMarkerSymbolV2::size()
return maxSize;
}


void QgsMarkerSymbolV2::setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod )
{
for ( QgsSymbolLayerV2List::iterator it = mLayers.begin(); it != mLayers.end(); ++it )
{
QgsMarkerSymbolLayerV2* layer = static_cast<QgsMarkerSymbolLayerV2*>( *it );
layer->setScaleMethod( scaleMethod );
}
}

QgsSymbolV2::ScaleMethod QgsMarkerSymbolV2::scaleMethod()
{
QgsSymbolLayerV2List::const_iterator it = mLayers.begin();

if ( it == mLayers.end() )
return DEFAULT_SCALE_METHOD;

// return scale method of the first symbol layer
const QgsMarkerSymbolLayerV2* layer = static_cast<const QgsMarkerSymbolLayerV2 *>( *it );
return layer->scaleMethod();
}

void QgsMarkerSymbolV2::renderPoint( const QPointF& point, const QgsFeature* f, QgsRenderContext& context, int layer, bool selected )
{
QgsSymbolV2RenderContext symbolContext( context, mOutputUnit, mAlpha, selected, mRenderHints, f );
@@ -56,6 +56,12 @@ class CORE_EXPORT QgsSymbolV2
Fill
};

enum ScaleMethod
{
ScaleArea,
ScaleDiameter
};

//! @note added in 1.5
enum RenderHint
{
@@ -219,6 +225,9 @@ class CORE_EXPORT QgsMarkerSymbolV2 : public QgsSymbolV2
void setSize( double size );
double size();

void setScaleMethod( QgsSymbolV2::ScaleMethod scaleMethod );
ScaleMethod scaleMethod();

void renderPoint( const QPointF& point, const QgsFeature* f, QgsRenderContext& context, int layer = -1, bool selected = false );

virtual QgsSymbolV2* clone() const;
@@ -104,7 +104,7 @@ QgsCategorizedSymbolRendererV2Widget::QgsCategorizedSymbolRendererV2Widget( QgsV
advMenu->addAction( tr( "Symbol levels..." ), this, SLOT( showSymbolLevels() ) );

mDataDefinedMenus = new QgsRendererV2DataDefinedMenus( advMenu, mLayer->pendingFields(),
mRenderer->rotationField(), mRenderer->sizeScaleField() );
mRenderer->rotationField(), mRenderer->sizeScaleField(), mRenderer->scaleMethod() );
connect( mDataDefinedMenus, SIGNAL( rotationFieldChanged( QString ) ), this, SLOT( rotationFieldChanged( QString ) ) );
connect( mDataDefinedMenus, SIGNAL( sizeScaleFieldChanged( QString ) ), this, SLOT( sizeScaleFieldChanged( QString ) ) );
btnAdvanced->setMenu( advMenu );
@@ -103,7 +103,7 @@ QgsGraduatedSymbolRendererV2Widget::QgsGraduatedSymbolRendererV2Widget( QgsVecto
advMenu->addAction( tr( "Symbol levels..." ), this, SLOT( showSymbolLevels() ) );

mDataDefinedMenus = new QgsRendererV2DataDefinedMenus( advMenu, mLayer->pendingFields(),
mRenderer->rotationField(), mRenderer->sizeScaleField() );
mRenderer->rotationField(), mRenderer->sizeScaleField(), mRenderer->scaleMethod() );
connect( mDataDefinedMenus, SIGNAL( rotationFieldChanged( QString ) ), this, SLOT( rotationFieldChanged( QString ) ) );
connect( mDataDefinedMenus, SIGNAL( sizeScaleFieldChanged( QString ) ), this, SLOT( sizeScaleFieldChanged( QString ) ) );
btnAdvanced->setMenu( advMenu );

0 comments on commit ca9d49b

Please sign in to comment.
You can’t perform that action at this time.