Skip to content

Commit

Permalink
Possibility to specify several output units for font marker and ellip…
Browse files Browse the repository at this point in the history
…se symbol layers
  • Loading branch information
mhugent committed Mar 12, 2013
1 parent 6c47be6 commit a477956
Show file tree
Hide file tree
Showing 9 changed files with 305 additions and 78 deletions.
53 changes: 43 additions & 10 deletions src/core/symbology-ng/qgsellipsesymbollayerv2.cpp
Expand Up @@ -23,8 +23,8 @@
#include <QDomDocument>
#include <QDomElement>

QgsEllipseSymbolLayerV2::QgsEllipseSymbolLayerV2(): mSymbolName( "circle" ), mSymbolWidth( 4 ), mSymbolHeight( 3 ),
mFillColor( Qt::black ), mOutlineColor( Qt::white ), mOutlineWidth( 0 )
QgsEllipseSymbolLayerV2::QgsEllipseSymbolLayerV2(): mSymbolName( "circle" ), mSymbolWidth( 4 ), mSymbolWidthUnit( QgsSymbolV2::MM ), mSymbolHeight( 3 ),
mSymbolHeightUnit( QgsSymbolV2::MM ), mFillColor( Qt::black ), mOutlineColor( Qt::white ), mOutlineWidth( 0 ), mOutlineWidthUnit( QgsSymbolV2::MM )
{
mPen.setColor( mOutlineColor );
mPen.setWidth( 1.0 );
Expand Down Expand Up @@ -57,10 +57,18 @@ QgsSymbolLayerV2* QgsEllipseSymbolLayerV2::create( const QgsStringMap& propertie
{
layer->setSymbolWidth( properties["symbol_width"].toDouble() );
}
if ( properties.contains( "symbol_width_unit" ) )
{
layer->setSymbolWidthUnit( QgsSymbolLayerV2Utils::decodeOutputUnit( properties["symbol_width_unit"] ) );
}
if ( properties.contains( "symbol_height" ) )
{
layer->setSymbolHeight( properties["symbol_height"].toDouble() );
}
if ( properties.contains( "symbol_height_unit" ) )
{
layer->setSymbolHeightUnit( QgsSymbolLayerV2Utils::decodeOutputUnit( properties["symbol_height_unit"] ) );
}
if ( properties.contains( "angle" ) )
{
layer->setAngle( properties["angle"].toDouble() );
Expand All @@ -69,6 +77,10 @@ QgsSymbolLayerV2* QgsEllipseSymbolLayerV2::create( const QgsStringMap& propertie
{
layer->setOutlineWidth( properties["outline_width"].toDouble() );
}
if ( properties.contains( "outline_width_unit" ) )
{
layer->setOutlineWidthUnit( QgsSymbolLayerV2Utils::decodeOutputUnit( properties["outline_width_unit"] ) );
}
if ( properties.contains( "fill_color" ) )
{
layer->setFillColor( QgsSymbolLayerV2Utils::decodeColor( properties["fill_color"] ) );
Expand Down Expand Up @@ -119,7 +131,7 @@ void QgsEllipseSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2Rend
{
if ( mOutlineWidthIndex != -1 )
{
double width = context.outputLineWidth( f->attribute( mOutlineWidthIndex ).toDouble() );
double width = f->attribute( mOutlineWidthIndex ).toDouble() * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mOutlineWidthUnit );
mPen.setWidthF( width );
}
if ( mFillColorIndex != -1 )
Expand Down Expand Up @@ -179,7 +191,7 @@ void QgsEllipseSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
preparePath( mSymbolName, context );
}
mPen.setColor( mOutlineColor );
mPen.setWidthF( context.outputLineWidth( mOutlineWidth ) );
mPen.setWidthF( mOutlineWidth * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mOutlineWidthUnit ) );
mBrush.setColor( mFillColor );

//resolve data defined attribute indices
Expand Down Expand Up @@ -319,12 +331,15 @@ QgsStringMap QgsEllipseSymbolLayerV2::properties() const
QgsStringMap map;
map["symbol_name"] = mSymbolName;
map["symbol_width"] = QString::number( mSymbolWidth );
map["symbol_width_unit"] = QgsSymbolLayerV2Utils::encodeOutputUnit( mSymbolWidthUnit );
map["width_field"] = mWidthField;
map["symbol_height"] = QString::number( mSymbolHeight );
map["symbol_height_unit"] = QgsSymbolLayerV2Utils::encodeOutputUnit( mSymbolHeightUnit );
map["height_field"] = mHeightField;
map["angle"] = QString::number( mAngle );
map["rotation_field"] = mRotationField;
map["outline_width"] = QString::number( mOutlineWidth );
map["outline_width_unit"] = QgsSymbolLayerV2Utils::encodeOutputUnit( mOutlineWidthUnit );
map["outline_width_field"] = mOutlineWidthField;
map["fill_color"] = QgsSymbolLayerV2Utils::encodeColor( mFillColor );
map["fill_color_field"] = mFillColorField;
Expand All @@ -343,34 +358,35 @@ bool QgsEllipseSymbolLayerV2::hasDataDefinedProperty() const
void QgsEllipseSymbolLayerV2::preparePath( const QString& symbolName, QgsSymbolV2RenderContext& context, const QgsFeature* f )
{
mPainterPath = QPainterPath();
const QgsRenderContext& ct = context.renderContext();

double width = 0;

if ( f && mWidthIndex != -1 ) //1. priority: data defined setting on symbol layer level
{
width = context.outputLineWidth( f->attribute( mWidthIndex ).toDouble() );
width = f->attribute( mWidthIndex ).toDouble() * QgsSymbolLayerV2Utils::lineWidthScaleFactor( ct, mSymbolWidthUnit );
}
else if ( context.renderHints() & QgsSymbolV2::DataDefinedSizeScale ) //2. priority: is data defined size on symbol level
{
width = context.outputLineWidth( mSize );
width = mSize * QgsSymbolLayerV2Utils::lineWidthScaleFactor( ct, mSymbolWidthUnit );
}
else //3. priority: global width setting
{
width = context.outputLineWidth( mSymbolWidth );
width = mSymbolWidth * QgsSymbolLayerV2Utils::lineWidthScaleFactor( ct, mSymbolWidthUnit );
}

double height = 0;
if ( f && mHeightIndex != -1 ) //1. priority: data defined setting on symbol layer level
{
height = context.outputLineWidth( f->attribute( mHeightIndex ).toDouble() );
height = f->attribute( mHeightIndex ).toDouble() * QgsSymbolLayerV2Utils::lineWidthScaleFactor( ct, mSymbolHeightUnit );
}
else if ( context.renderHints() & QgsSymbolV2::DataDefinedSizeScale ) //2. priority: is data defined size on symbol level
{
height = context.outputLineWidth( mSize );
height = mSize * QgsSymbolLayerV2Utils::lineWidthScaleFactor( ct, mSymbolHeightUnit );
}
else //3. priority: global height setting
{
height = context.outputLineWidth( mSymbolHeight );
height = mSymbolHeight * QgsSymbolLayerV2Utils::lineWidthScaleFactor( ct, mSymbolHeightUnit );
}

if ( symbolName == "circle" )
Expand Down Expand Up @@ -430,3 +446,20 @@ QSet<QString> QgsEllipseSymbolLayerV2::usedAttributes() const
}
return dataDefinedAttributes;
}

void QgsEllipseSymbolLayerV2::setOutputUnit( QgsSymbolV2::OutputUnit unit )
{
mSymbolWidthUnit = unit;
mSymbolHeightUnit = unit;
mOutlineWidthUnit = unit;
}

QgsSymbolV2::OutputUnit QgsEllipseSymbolLayerV2::outputUnit() const
{
QgsSymbolV2::OutputUnit unit = mSymbolWidthUnit;
if ( mSymbolHeightUnit != unit || mOutlineWidthUnit != unit )
{
return QgsSymbolV2::Mixed;
}
return unit;
}
15 changes: 15 additions & 0 deletions src/core/symbology-ng/qgsellipsesymbollayerv2.h
Expand Up @@ -79,13 +79,28 @@ class CORE_EXPORT QgsEllipseSymbolLayerV2: public QgsMarkerSymbolLayerV2

QSet<QString> usedAttributes() const;

void setSymbolWidthUnit( QgsSymbolV2::OutputUnit unit ) { mSymbolWidthUnit = unit; }
QgsSymbolV2::OutputUnit symbolWidthUnit() const { return mSymbolWidthUnit; }

void setSymbolHeightUnit( QgsSymbolV2::OutputUnit unit ) { mSymbolHeightUnit = unit; }
QgsSymbolV2::OutputUnit symbolHeightUnit() const { return mSymbolHeightUnit; }

void setOutlineWidthUnit( QgsSymbolV2::OutputUnit unit ) { mOutlineWidthUnit = unit; }
QgsSymbolV2::OutputUnit outlineWidthUnit() const { return mOutlineWidthUnit; }

void setOutputUnit( QgsSymbolV2::OutputUnit unit );
QgsSymbolV2::OutputUnit outputUnit() const;

private:
QString mSymbolName;
double mSymbolWidth;
QgsSymbolV2::OutputUnit mSymbolWidthUnit;
double mSymbolHeight;
QgsSymbolV2::OutputUnit mSymbolHeightUnit;
QColor mFillColor;
QColor mOutlineColor;
double mOutlineWidth;
QgsSymbolV2::OutputUnit mOutlineWidthUnit;

#if 0
/**Take width from attribute (-1 if fixed width)*/
Expand Down
16 changes: 14 additions & 2 deletions src/core/symbology-ng/qgsmarkersymbollayerv2.cpp
Expand Up @@ -921,7 +921,9 @@ QgsFontMarkerSymbolLayerV2::QgsFontMarkerSymbolLayerV2( QString fontFamily, QCha
mColor = color;
mAngle = angle;
mSize = pointSize;
mSizeUnit = QgsSymbolV2::MM;
mOffset = QPointF( 0, 0 );
mOffsetUnit = QgsSymbolV2::MM;
}

QgsSymbolLayerV2* QgsFontMarkerSymbolLayerV2::create( const QgsStringMap& props )
Expand All @@ -946,6 +948,10 @@ QgsSymbolLayerV2* QgsFontMarkerSymbolLayerV2::create( const QgsStringMap& props
QgsFontMarkerSymbolLayerV2* m = new QgsFontMarkerSymbolLayerV2( fontFamily, chr, pointSize, color, angle );
if ( props.contains( "offset" ) )
m->setOffset( QgsSymbolLayerV2Utils::decodePoint( props["offset"] ) );
if ( props.contains( "offset_unit" ) )
m->setOffsetUnit( QgsSymbolLayerV2Utils::decodeOutputUnit( props["offset_unit" ] ) );
if ( props.contains( "size_unit" ) )
m->setSizeUnit( QgsSymbolLayerV2Utils::decodeOutputUnit( props["size_unit"] ) );
return m;
}

Expand All @@ -957,7 +963,7 @@ QString QgsFontMarkerSymbolLayerV2::layerType() const
void QgsFontMarkerSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context )
{
mFont = QFont( mFontFamily );
mFont.setPixelSize( context.outputLineWidth( mSize ) );
mFont.setPixelSize( mSize * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mSizeUnit ) );
QFontMetrics fm( mFont );
mChrOffset = QPointF( fm.width( mChr ) / 2, -fm.ascent() / 2 );

Expand All @@ -979,7 +985,9 @@ void QgsFontMarkerSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2R


p->save();
QPointF outputOffset = QPointF( context.outputLineWidth( mOffset.x() ), context.outputLineWidth( mOffset.y() ) );
double offsetX = mOffset.x() * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mOffsetUnit );
double offsetY = mOffset.y() * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mOffsetUnit );
QPointF outputOffset( offsetX, offsetY );
if ( mAngle )
outputOffset = _rotatedOffset( outputOffset, mAngle );
p->translate( point + outputOffset );
Expand All @@ -1003,16 +1011,20 @@ QgsStringMap QgsFontMarkerSymbolLayerV2::properties() const
props["font"] = mFontFamily;
props["chr"] = mChr;
props["size"] = QString::number( mSize );
props["size_unit"] = QgsSymbolLayerV2Utils::encodeOutputUnit( mSizeUnit );
props["color"] = QgsSymbolLayerV2Utils::encodeColor( mColor );
props["angle"] = QString::number( mAngle );
props["offset"] = QgsSymbolLayerV2Utils::encodePoint( mOffset );
props["offset_unit"] = QgsSymbolLayerV2Utils::encodeOutputUnit( mOffsetUnit );
return props;
}

QgsSymbolLayerV2* QgsFontMarkerSymbolLayerV2::clone() const
{
QgsFontMarkerSymbolLayerV2* m = new QgsFontMarkerSymbolLayerV2( mFontFamily, mChr, mSize, mColor, mAngle );
m->setOffset( mOffset );
m->setOffsetUnit( mOffsetUnit );
m->setSizeUnit( mSizeUnit );
return m;
}

Expand Down
30 changes: 30 additions & 0 deletions src/gui/symbology-ng/qgsellipsesymbollayerv2widget.cpp
Expand Up @@ -72,6 +72,9 @@ void QgsEllipseSymbolLayerV2Widget::setSymbolLayer( QgsSymbolLayerV2* layer )
blockComboSignals( true );
if ( mLayer )
{
mSymbolWidthUnitComboBox->setCurrentIndex( mLayer->symbolWidthUnit() );
mOutlineWidthUnitComboBox->setCurrentIndex( mLayer->outlineWidthUnit() );
mSymbolHeightUnitComboBox->setCurrentIndex( mLayer->symbolHeightUnit() );

if ( mLayer->widthField().isEmpty() )
{
Expand Down Expand Up @@ -312,6 +315,30 @@ void QgsEllipseSymbolLayerV2Widget::on_mDDShapeComboBox_currentIndexChanged( int
}
}

void QgsEllipseSymbolLayerV2Widget::on_mSymbolWidthUnitComboBox_currentIndexChanged( int index )
{
if ( mLayer )
{
mLayer->setSymbolWidthUnit(( QgsSymbolV2::OutputUnit ) index );
}
}

void QgsEllipseSymbolLayerV2Widget::on_mOutlineWidthUnitComboBox_currentIndexChanged( int index )
{
if ( mLayer )
{
mLayer->setOutlineWidthUnit(( QgsSymbolV2::OutputUnit ) index );
}
}

void QgsEllipseSymbolLayerV2Widget::on_mSymbolHeightUnitComboBox_currentIndexChanged( int index )
{
if ( mLayer )
{
mLayer->setSymbolHeightUnit(( QgsSymbolV2::OutputUnit ) index );
}
}

void QgsEllipseSymbolLayerV2Widget::blockComboSignals( bool block )
{
mDDSymbolWidthComboBox->blockSignals( block );
Expand All @@ -321,4 +348,7 @@ void QgsEllipseSymbolLayerV2Widget::blockComboSignals( bool block )
mDDFillColorComboBox->blockSignals( block );
mDDOutlineColorComboBox->blockSignals( block );
mDDShapeComboBox->blockSignals( block );
mSymbolWidthUnitComboBox->blockSignals( block );
mOutlineWidthUnitComboBox->blockSignals( block );
mSymbolHeightUnitComboBox->blockSignals( block );
}
4 changes: 4 additions & 0 deletions src/gui/symbology-ng/qgsellipsesymbollayerv2widget.h
Expand Up @@ -57,6 +57,10 @@ class GUI_EXPORT QgsEllipseSymbolLayerV2Widget: public QgsSymbolLayerV2Widget, p
void on_mDDFillColorComboBox_currentIndexChanged( int idx );
void on_mDDOutlineColorComboBox_currentIndexChanged( int idx );
void on_mDDShapeComboBox_currentIndexChanged( int idx );

void on_mSymbolWidthUnitComboBox_currentIndexChanged( int index );
void on_mOutlineWidthUnitComboBox_currentIndexChanged( int index );
void on_mSymbolHeightUnitComboBox_currentIndexChanged( int index );
};

#endif // QGSELLIPSESYMBOLLAYERV2WIDGET_H
23 changes: 23 additions & 0 deletions src/gui/symbology-ng/qgssymbollayerv2widget.cpp
Expand Up @@ -1429,6 +1429,13 @@ void QgsFontMarkerSymbolLayerV2Widget::setSymbolLayer( QgsSymbolLayerV2* layer )
spinOffsetY->setValue( mLayer->offset().y() );
spinOffsetY->blockSignals( false );

mSizeUnitComboBox->blockSignals( true );
mSizeUnitComboBox->setCurrentIndex( mLayer->sizeUnit() );
mSizeUnitComboBox->blockSignals( false );

mOffsetUnitComboBox->blockSignals( true );
mOffsetUnitComboBox->setCurrentIndex( mLayer->offsetUnit() );
mOffsetUnitComboBox->blockSignals( false );
}

QgsSymbolLayerV2* QgsFontMarkerSymbolLayerV2Widget::symbolLayer()
Expand Down Expand Up @@ -1485,6 +1492,22 @@ void QgsFontMarkerSymbolLayerV2Widget::setOffset()
emit changed();
}

void QgsFontMarkerSymbolLayerV2Widget::on_mSizeUnitComboBox_currentIndexChanged( int index )
{
if ( mLayer )
{
mLayer->setSizeUnit(( QgsSymbolV2::OutputUnit ) index );
}
}

void QgsFontMarkerSymbolLayerV2Widget::on_mOffsetUnitComboBox_currentIndexChanged( int index )
{
if ( mLayer )
{
mLayer->setOffsetUnit(( QgsSymbolV2::OutputUnit ) index );
}
}


///////////////

Expand Down
2 changes: 2 additions & 0 deletions src/gui/symbology-ng/qgssymbollayerv2widget.h
Expand Up @@ -371,6 +371,8 @@ class GUI_EXPORT QgsFontMarkerSymbolLayerV2Widget : public QgsSymbolLayerV2Widge
void setAngle( double angle );
void setCharacter( const QChar& chr );
void setOffset();
void on_mSizeUnitComboBox_currentIndexChanged( int index );
void on_mOffsetUnitComboBox_currentIndexChanged( int index );

protected:
QgsFontMarkerSymbolLayerV2* mLayer;
Expand Down

0 comments on commit a477956

Please sign in to comment.