|
| 1 | +#include "qgsellipsesymbollayerv2.h" |
| 2 | +#include "qgsfeature.h" |
| 3 | +#include "qgsrendercontext.h" |
| 4 | +#include <QPainter> |
| 5 | +#include <QSet> |
| 6 | + |
| 7 | +QgsEllipseSymbolLayerV2::QgsEllipseSymbolLayerV2(): mDataDefinedWidth(-1), mDataDefinedHeight(-1), |
| 8 | + mDataDefinedOutlineWidth(-1), mFillColor( Qt::black ), mDataDefinedFillColor(-1), mOutlineColor( Qt::white ), mDataDefinedOutlineColor(-1) |
| 9 | +{ |
| 10 | + mSymbolName = "circle"; |
| 11 | + mPen.setColor( mOutlineColor ); |
| 12 | + mPen.setWidth( 1.0 ); |
| 13 | + mPen.setJoinStyle( Qt::MiterJoin ); |
| 14 | + mBrush.setColor( mFillColor ); |
| 15 | + mBrush.setStyle( Qt::SolidPattern ); |
| 16 | +} |
| 17 | + |
| 18 | +QgsEllipseSymbolLayerV2::~QgsEllipseSymbolLayerV2() |
| 19 | +{ |
| 20 | +} |
| 21 | + |
| 22 | +QgsSymbolLayerV2* QgsEllipseSymbolLayerV2::create( const QgsStringMap& properties ) |
| 23 | +{ |
| 24 | + QgsEllipseSymbolLayerV2* layer = new QgsEllipseSymbolLayerV2(); |
| 25 | + if( properties.contains( "symbol_name" ) ) |
| 26 | + { |
| 27 | + layer->setSymbolName( properties[ "symbol_name" ] ); |
| 28 | + } |
| 29 | + if( properties.contains( "symbol_width" ) ) |
| 30 | + { |
| 31 | + layer->setSymbolWidth( properties["symbol_width"].toDouble() ); |
| 32 | + } |
| 33 | + if( properties.contains( "data_defined_width" ) ) |
| 34 | + { |
| 35 | + layer->setDataDefinedWidth( properties["data_defined_width"].toInt() ); |
| 36 | + } |
| 37 | + if( properties.contains("symbol_height") ) |
| 38 | + { |
| 39 | + layer->setSymbolHeight( properties["symbol_height"].toDouble() ); |
| 40 | + } |
| 41 | + if( properties.contains( "data_defined_height" ) ) |
| 42 | + { |
| 43 | + layer->setDataDefinedHeight( properties["data_defined_height"].toInt() ); |
| 44 | + } |
| 45 | + if( properties.contains( "outline_width" ) ) |
| 46 | + { |
| 47 | + layer->setOutlineWidth( properties["outline_width"].toDouble() ); |
| 48 | + } |
| 49 | + if( properties.contains( "data_defined_outline_width" ) ) |
| 50 | + { |
| 51 | + layer->setDataDefinedOutlineWidth( properties["data_defined_outline_width"].toInt() ); |
| 52 | + } |
| 53 | + if( properties.contains( "fill_color" ) ) |
| 54 | + { |
| 55 | + layer->setFillColor( QgsSymbolLayerV2Utils::decodeColor( properties["fill_color"] ) ); |
| 56 | + } |
| 57 | + if( properties.contains( "data_defined_fill_color" ) ) |
| 58 | + { |
| 59 | + layer->setDataDefinedFillColor( properties["data_defined_fill_color"].toInt() ); |
| 60 | + } |
| 61 | + if( properties.contains( "outline_color" ) ) |
| 62 | + { |
| 63 | + layer->setOutlineColor( QgsSymbolLayerV2Utils::decodeColor( properties["outline_color"] ) ); |
| 64 | + } |
| 65 | + if( properties.contains( "data_defined_outline_color" ) ) |
| 66 | + { |
| 67 | + layer->setDataDefinedOutlineColor( properties[ "data_defined_outline_color" ].toInt() ); |
| 68 | + } |
| 69 | + return layer; |
| 70 | +} |
| 71 | + |
| 72 | +void QgsEllipseSymbolLayerV2::renderPoint( const QPointF& point, QgsSymbolV2RenderContext& context ) |
| 73 | +{ |
| 74 | + const QgsFeature* f = context.feature(); |
| 75 | + |
| 76 | + if( f ) |
| 77 | + { |
| 78 | + if( mDataDefinedOutlineWidth != -1 ) |
| 79 | + { |
| 80 | + double width = context.outputLineWidth( f->attributeMap()[mDataDefinedOutlineWidth].toDouble() ); |
| 81 | + mPen.setWidth( width ); |
| 82 | + } |
| 83 | + if( mDataDefinedFillColor != -1 ) |
| 84 | + { |
| 85 | + mBrush.setColor( QColor( f->attributeMap()[mDataDefinedFillColor].toString() ) ); |
| 86 | + } |
| 87 | + if( mDataDefinedOutlineColor != -1 ) |
| 88 | + { |
| 89 | + mPen.setColor( QColor( f->attributeMap()[mDataDefinedOutlineColor].toString() ) ); |
| 90 | + } |
| 91 | + if( mDataDefinedWidth != -1 || mDataDefinedHeight != -1 ) |
| 92 | + { |
| 93 | + preparePath( context, f ); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + QPainter* p = context.renderContext().painter(); |
| 98 | + if( !p ) |
| 99 | + { |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + QPointF off( context.outputLineWidth( mOffset.x() ), context.outputLineWidth( mOffset.y() ) ); |
| 104 | + |
| 105 | + QMatrix transform; |
| 106 | + transform.translate( point.x() + off.x(), point.y() + off.y() ); |
| 107 | + |
| 108 | + p->setPen( mPen ); |
| 109 | + p->setBrush( mBrush ); |
| 110 | + p->drawPath( transform.map( mPainterPath ) ); |
| 111 | +} |
| 112 | + |
| 113 | +QString QgsEllipseSymbolLayerV2::layerType() const |
| 114 | +{ |
| 115 | + return "Ellipse"; |
| 116 | +} |
| 117 | + |
| 118 | +void QgsEllipseSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context ) |
| 119 | +{ |
| 120 | + if( !hasDataDefinedProperty() ) |
| 121 | + { |
| 122 | + preparePath( context ); |
| 123 | + } |
| 124 | + mPen.setColor( mOutlineColor ); |
| 125 | + mPen.setWidth( context.outputLineWidth( mOutlineWidth ) ); |
| 126 | + mBrush.setColor( mFillColor ); |
| 127 | +} |
| 128 | + |
| 129 | +void QgsEllipseSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context ) |
| 130 | +{ |
| 131 | +} |
| 132 | + |
| 133 | +QgsSymbolLayerV2* QgsEllipseSymbolLayerV2::clone() const |
| 134 | +{ |
| 135 | + return QgsEllipseSymbolLayerV2::create( properties() ); |
| 136 | +} |
| 137 | + |
| 138 | +QgsStringMap QgsEllipseSymbolLayerV2::properties() const |
| 139 | +{ |
| 140 | + QgsStringMap map; |
| 141 | + map["symbol_name"] = mSymbolName; |
| 142 | + map["symbol_width"] = QString::number( mSymbolWidth ); |
| 143 | + map["data_defined_width"] = QString::number( mDataDefinedWidth ); |
| 144 | + map["symbol_height"] = QString::number( mSymbolHeight ); |
| 145 | + map["data_defined_height"] = QString::number( mDataDefinedHeight ); |
| 146 | + map["outline_width"] = QString::number( mOutlineWidth ); |
| 147 | + map["data_defined_outline_width"] = QString::number( mDataDefinedOutlineWidth ); |
| 148 | + map["fill_color"] = QgsSymbolLayerV2Utils::encodeColor( mFillColor ); |
| 149 | + map["data_defined_fill_color"] = QString::number( mDataDefinedFillColor ); |
| 150 | + map["outline_color"] = QgsSymbolLayerV2Utils::encodeColor( mOutlineColor ); |
| 151 | + map["data_defined_outline_color"] = QString::number( mDataDefinedOutlineColor ); |
| 152 | + return map; |
| 153 | +} |
| 154 | + |
| 155 | +bool QgsEllipseSymbolLayerV2::hasDataDefinedProperty() const |
| 156 | +{ |
| 157 | + return ( mDataDefinedWidth != -1 || mDataDefinedHeight != -1 || mDataDefinedOutlineWidth != -1 |
| 158 | + || mDataDefinedFillColor != -1 || mDataDefinedOutlineColor != -1 ); |
| 159 | +} |
| 160 | + |
| 161 | +void QgsEllipseSymbolLayerV2::preparePath( QgsSymbolV2RenderContext& context, const QgsFeature* f ) |
| 162 | +{ |
| 163 | + mPainterPath = QPainterPath(); |
| 164 | + |
| 165 | + double width = 0; |
| 166 | + if( f && mDataDefinedOutlineWidth != -1 ) |
| 167 | + { |
| 168 | + width = context.outputLineWidth( f->attributeMap()[mDataDefinedOutlineWidth].toDouble() ); |
| 169 | + } |
| 170 | + else |
| 171 | + { |
| 172 | + width = context.outputLineWidth( mSymbolWidth ); |
| 173 | + } |
| 174 | + |
| 175 | + double height = 0; |
| 176 | + if( f && mDataDefinedHeight != -1 ) |
| 177 | + { |
| 178 | + height = context.outputLineWidth( f->attributeMap()[mDataDefinedHeight].toDouble() ); |
| 179 | + } |
| 180 | + else |
| 181 | + { |
| 182 | + height = context.outputLineWidth( mSymbolHeight ); |
| 183 | + } |
| 184 | + |
| 185 | + if( mSymbolName == "circle" ) |
| 186 | + { |
| 187 | + mPainterPath.addEllipse( QRectF( -width / 2.0, -height / 2.0, width, height ) ); |
| 188 | + } |
| 189 | + else if( mSymbolName == "rectangle" ) |
| 190 | + { |
| 191 | + mPainterPath.addRect( QRectF( -width / 2.0, -height / 2.0, width, height ) ); |
| 192 | + } |
| 193 | + else if( mSymbolName == "cross" ) |
| 194 | + { |
| 195 | + mPainterPath.moveTo( 0, -height / 2.0 ); |
| 196 | + mPainterPath.lineTo( 0, height / 2.0 ); |
| 197 | + mPainterPath.moveTo( -width / 2.0, 0 ); |
| 198 | + mPainterPath.lineTo( width / 2.0, 0 ); |
| 199 | + } |
| 200 | + else if( mSymbolName == "triangle" ) |
| 201 | + { |
| 202 | + mPainterPath.moveTo( 0, -height / 2.0 ); |
| 203 | + mPainterPath.lineTo( -width / 2.0, height / 2.0 ); |
| 204 | + mPainterPath.lineTo( width / 2.0, height / 2.0 ); |
| 205 | + mPainterPath.lineTo( 0, -height / 2.0 ); |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +QSet<QString> QgsEllipseSymbolLayerV2::usedAttributes() const |
| 210 | +{ |
| 211 | + QSet<QString> dataDefinedAttributes; |
| 212 | + /*dataDefinedAttributes.insert( mDataDefinedWidth ); |
| 213 | + dataDefinedAttributes.insert( mDataDefinedHeight ); |
| 214 | + dataDefinedAttributes.insert( mDataDefinedOutlineWidth ); |
| 215 | + dataDefinedAttributes.insert( mDataDefinedFillColor ); |
| 216 | + dataDefinedAttributes.insert( mDataDefinedOutlineColor ); |
| 217 | + dataDefinedAttributes.remove( -1 );*/ |
| 218 | + return dataDefinedAttributes; |
| 219 | +} |
0 commit comments