Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
308 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,219 @@ | ||
#include "qgsellipsesymbollayerv2.h" | ||
#include "qgsfeature.h" | ||
#include "qgsrendercontext.h" | ||
#include <QPainter> | ||
#include <QSet> | ||
|
||
QgsEllipseSymbolLayerV2::QgsEllipseSymbolLayerV2(): mDataDefinedWidth(-1), mDataDefinedHeight(-1), | ||
mDataDefinedOutlineWidth(-1), mFillColor( Qt::black ), mDataDefinedFillColor(-1), mOutlineColor( Qt::white ), mDataDefinedOutlineColor(-1) | ||
{ | ||
mSymbolName = "circle"; | ||
mPen.setColor( mOutlineColor ); | ||
mPen.setWidth( 1.0 ); | ||
mPen.setJoinStyle( Qt::MiterJoin ); | ||
mBrush.setColor( mFillColor ); | ||
mBrush.setStyle( Qt::SolidPattern ); | ||
} | ||
|
||
QgsEllipseSymbolLayerV2::~QgsEllipseSymbolLayerV2() | ||
{ | ||
} | ||
|
||
QgsSymbolLayerV2* QgsEllipseSymbolLayerV2::create( const QgsStringMap& properties ) | ||
{ | ||
QgsEllipseSymbolLayerV2* layer = new QgsEllipseSymbolLayerV2(); | ||
if( properties.contains( "symbol_name" ) ) | ||
{ | ||
layer->setSymbolName( properties[ "symbol_name" ] ); | ||
} | ||
if( properties.contains( "symbol_width" ) ) | ||
{ | ||
layer->setSymbolWidth( properties["symbol_width"].toDouble() ); | ||
} | ||
if( properties.contains( "data_defined_width" ) ) | ||
{ | ||
layer->setDataDefinedWidth( properties["data_defined_width"].toInt() ); | ||
} | ||
if( properties.contains("symbol_height") ) | ||
{ | ||
layer->setSymbolHeight( properties["symbol_height"].toDouble() ); | ||
} | ||
if( properties.contains( "data_defined_height" ) ) | ||
{ | ||
layer->setDataDefinedHeight( properties["data_defined_height"].toInt() ); | ||
} | ||
if( properties.contains( "outline_width" ) ) | ||
{ | ||
layer->setOutlineWidth( properties["outline_width"].toDouble() ); | ||
} | ||
if( properties.contains( "data_defined_outline_width" ) ) | ||
{ | ||
layer->setDataDefinedOutlineWidth( properties["data_defined_outline_width"].toInt() ); | ||
} | ||
if( properties.contains( "fill_color" ) ) | ||
{ | ||
layer->setFillColor( QgsSymbolLayerV2Utils::decodeColor( properties["fill_color"] ) ); | ||
} | ||
if( properties.contains( "data_defined_fill_color" ) ) | ||
{ | ||
layer->setDataDefinedFillColor( properties["data_defined_fill_color"].toInt() ); | ||
} | ||
if( properties.contains( "outline_color" ) ) | ||
{ | ||
layer->setOutlineColor( QgsSymbolLayerV2Utils::decodeColor( properties["outline_color"] ) ); | ||
} | ||
if( properties.contains( "data_defined_outline_color" ) ) | ||
{ | ||
layer->setDataDefinedOutlineColor( properties[ "data_defined_outline_color" ].toInt() ); | ||
} | ||
return layer; | ||
} | ||
|
||
void QgsEllipseSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context ) | ||
{ | ||
const QgsFeature* f = context.feature(); | ||
|
||
if( f ) | ||
{ | ||
if( mDataDefinedOutlineWidth != -1 ) | ||
{ | ||
double width = context.outputLineWidth( f->attributeMap()[mDataDefinedOutlineWidth].toDouble() ); | ||
mPen.setWidth( width ); | ||
} | ||
if( mDataDefinedFillColor != -1 ) | ||
{ | ||
mBrush.setColor( QColor( f->attributeMap()[mDataDefinedFillColor].toString() ) ); | ||
} | ||
if( mDataDefinedOutlineColor != -1 ) | ||
{ | ||
mPen.setColor( QColor( f->attributeMap()[mDataDefinedOutlineColor].toString() ) ); | ||
} | ||
if( mDataDefinedWidth != -1 || mDataDefinedHeight != -1 ) | ||
{ | ||
preparePath( context, f ); | ||
} | ||
} | ||
|
||
QPainter* p = context.renderContext().painter(); | ||
if( !p ) | ||
{ | ||
return; | ||
} | ||
|
||
QPointF off( context.outputLineWidth( mOffset.x() ), context.outputLineWidth( mOffset.y() ) ); | ||
|
||
QMatrix transform; | ||
transform.translate( point.x() + off.x(), point.y() + off.y() ); | ||
|
||
p->setPen( mPen ); | ||
p->setBrush( mBrush ); | ||
p->drawPath( transform.map( mPainterPath ) ); | ||
} | ||
|
||
QString QgsEllipseSymbolLayerV2::layerType() const | ||
{ | ||
return "Ellipse"; | ||
} | ||
|
||
void QgsEllipseSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context ) | ||
{ | ||
if( !hasDataDefinedProperty() ) | ||
{ | ||
preparePath( context ); | ||
} | ||
mPen.setColor( mOutlineColor ); | ||
mPen.setWidth( context.outputLineWidth( mOutlineWidth ) ); | ||
mBrush.setColor( mFillColor ); | ||
} | ||
|
||
void QgsEllipseSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context ) | ||
{ | ||
} | ||
|
||
QgsSymbolLayerV2* QgsEllipseSymbolLayerV2::clone() const | ||
{ | ||
return QgsEllipseSymbolLayerV2::create( properties() ); | ||
} | ||
|
||
QgsStringMap QgsEllipseSymbolLayerV2::properties() const | ||
{ | ||
QgsStringMap map; | ||
map["symbol_name"] = mSymbolName; | ||
map["symbol_width"] = QString::number( mSymbolWidth ); | ||
map["data_defined_width"] = QString::number( mDataDefinedWidth ); | ||
map["symbol_height"] = QString::number( mSymbolHeight ); | ||
map["data_defined_height"] = QString::number( mDataDefinedHeight ); | ||
map["outline_width"] = QString::number( mOutlineWidth ); | ||
map["data_defined_outline_width"] = QString::number( mDataDefinedOutlineWidth ); | ||
map["fill_color"] = QgsSymbolLayerV2Utils::encodeColor( mFillColor ); | ||
map["data_defined_fill_color"] = QString::number( mDataDefinedFillColor ); | ||
map["outline_color"] = QgsSymbolLayerV2Utils::encodeColor( mOutlineColor ); | ||
map["data_defined_outline_color"] = QString::number( mDataDefinedOutlineColor ); | ||
return map; | ||
} | ||
|
||
bool QgsEllipseSymbolLayerV2::hasDataDefinedProperty() const | ||
{ | ||
return ( mDataDefinedWidth != -1 || mDataDefinedHeight != -1 || mDataDefinedOutlineWidth != -1 | ||
|| mDataDefinedFillColor != -1 || mDataDefinedOutlineColor != -1 ); | ||
} | ||
|
||
void QgsEllipseSymbolLayerV2::preparePath( QgsSymbolV2RenderContext& context, const QgsFeature* f ) | ||
{ | ||
mPainterPath = QPainterPath(); | ||
|
||
double width = 0; | ||
if( f && mDataDefinedOutlineWidth != -1 ) | ||
{ | ||
width = context.outputLineWidth( f->attributeMap()[mDataDefinedOutlineWidth].toDouble() ); | ||
} | ||
else | ||
{ | ||
width = context.outputLineWidth( mSymbolWidth ); | ||
} | ||
|
||
double height = 0; | ||
if( f && mDataDefinedHeight != -1 ) | ||
{ | ||
height = context.outputLineWidth( f->attributeMap()[mDataDefinedHeight].toDouble() ); | ||
} | ||
else | ||
{ | ||
height = context.outputLineWidth( mSymbolHeight ); | ||
} | ||
|
||
if( mSymbolName == "circle" ) | ||
{ | ||
mPainterPath.addEllipse( QRectF( -width / 2.0, -height / 2.0, width, height ) ); | ||
} | ||
else if( mSymbolName == "rectangle" ) | ||
{ | ||
mPainterPath.addRect( QRectF( -width / 2.0, -height / 2.0, width, height ) ); | ||
} | ||
else if( mSymbolName == "cross" ) | ||
{ | ||
mPainterPath.moveTo( 0, -height / 2.0 ); | ||
mPainterPath.lineTo( 0, height / 2.0 ); | ||
mPainterPath.moveTo( -width / 2.0, 0 ); | ||
mPainterPath.lineTo( width / 2.0, 0 ); | ||
} | ||
else if( mSymbolName == "triangle" ) | ||
{ | ||
mPainterPath.moveTo( 0, -height / 2.0 ); | ||
mPainterPath.lineTo( -width / 2.0, height / 2.0 ); | ||
mPainterPath.lineTo( width / 2.0, height / 2.0 ); | ||
mPainterPath.lineTo( 0, -height / 2.0 ); | ||
} | ||
} | ||
|
||
QSet<QString> QgsEllipseSymbolLayerV2::usedAttributes() const | ||
{ | ||
QSet<QString> dataDefinedAttributes; | ||
/*dataDefinedAttributes.insert( mDataDefinedWidth ); | ||
dataDefinedAttributes.insert( mDataDefinedHeight ); | ||
dataDefinedAttributes.insert( mDataDefinedOutlineWidth ); | ||
dataDefinedAttributes.insert( mDataDefinedFillColor ); | ||
dataDefinedAttributes.insert( mDataDefinedOutlineColor ); | ||
dataDefinedAttributes.remove( -1 );*/ | ||
return dataDefinedAttributes; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#ifndef QGSELLIPSESYMBOLLAYERV2_H | ||
#define QGSELLIPSESYMBOLLAYERV2_H | ||
|
||
#include "qgsmarkersymbollayerv2.h" | ||
#include <QPainterPath> | ||
|
||
/**A symbol layer for rendering objects with major and minor axis (e.g. ellipse, rectangle )*/ | ||
class QgsEllipseSymbolLayerV2: public QgsMarkerSymbolLayerV2 | ||
{ | ||
public: | ||
QgsEllipseSymbolLayerV2(); | ||
~QgsEllipseSymbolLayerV2(); | ||
|
||
static QgsSymbolLayerV2* create( const QgsStringMap& properties = QgsStringMap() ); | ||
|
||
void renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context ); | ||
QString layerType() const; | ||
void startRender( QgsSymbolV2RenderContext& context ); | ||
void stopRender( QgsSymbolV2RenderContext& context ); | ||
QgsSymbolLayerV2* clone() const; | ||
QgsStringMap properties() const; | ||
|
||
void setSymbolName( const QString& name ){ mSymbolName = name; } | ||
QString symbolName() const{ return mSymbolName; } | ||
|
||
void setSymbolWidth( double w ){ mSymbolWidth = w; } | ||
double symbolWidth() const { return mSymbolWidth; } | ||
|
||
void setDataDefinedWidth( int c ){ mDataDefinedWidth = c; } | ||
int dataDefinedWidth() const { return mDataDefinedWidth; } | ||
|
||
void setSymbolHeight( double h ){ mSymbolHeight = h; } | ||
double symbolHeight() const { return mSymbolHeight; } | ||
|
||
void setDataDefinedHeight( int c ){ mDataDefinedHeight = c; } | ||
int dataDefinedHeight() const { return mDataDefinedHeight; } | ||
|
||
void setOutlineWidth( double w ){ mOutlineWidth = w; } | ||
double outlineWidth() const { return mOutlineWidth; } | ||
|
||
void setDataDefinedOutlineWidth( int c ){ mDataDefinedOutlineWidth = c; } | ||
int dataDefinedOutlineWidth() const { return mDataDefinedOutlineWidth; } | ||
|
||
void setFillColor( const QColor& c ){ mFillColor = c;} | ||
QColor fillColor() const { return mFillColor; } | ||
|
||
void setDataDefinedFillColor( int c ){ mDataDefinedFillColor = c; } | ||
int dataDefinedFillColor() const { return mDataDefinedFillColor; } | ||
|
||
void setOutlineColor( const QColor& c ){ mOutlineColor = c; } | ||
QColor outlineColor() const { return mOutlineColor; } | ||
|
||
void setDataDefinedOutlineColor( int c ){ mDataDefinedOutlineColor = c; } | ||
int dataDefinedOutlineColor() const { return mDataDefinedOutlineColor; } | ||
|
||
QSet<QString> usedAttributes() const; | ||
|
||
private: | ||
QString mSymbolName; | ||
double mSymbolWidth; | ||
/**Take width from attribute (-1 if fixed width)*/ | ||
int mDataDefinedWidth; | ||
double mSymbolHeight; | ||
/**Take height from attribute (-1 if fixed height)*/ | ||
int mDataDefinedHeight; | ||
double mOutlineWidth; | ||
/**Take outline width from attribute (-1 if fixed outline width)*/ | ||
int mDataDefinedOutlineWidth; | ||
QColor mFillColor; | ||
/**Take fill color from attribute (-1 if fixed fill color)*/ | ||
int mDataDefinedFillColor; | ||
QColor mOutlineColor; | ||
/**Take outline color from attribute (-1 if fixed outline color)*/ | ||
int mDataDefinedOutlineColor; | ||
QPainterPath mPainterPath; | ||
|
||
QPen mPen; | ||
QBrush mBrush; | ||
|
||
/**Setup mPainterPath | ||
@param feature to render (0 if no data defined rendering)*/ | ||
void preparePath( QgsSymbolV2RenderContext& context, const QgsFeature* f = 0 ); | ||
|
||
/**True if this symbol layer uses a data defined property*/ | ||
bool hasDataDefinedProperty() const; | ||
}; | ||
|
||
#endif // QGSELLIPSESYMBOLLAYERV2_H |