From e721e7f71e4ddc6f8b4fac7dbcda858fa9716562 Mon Sep 17 00:00:00 2001 From: Marco Hugentobler Date: Wed, 15 Jan 2014 16:56:51 +0100 Subject: [PATCH] Bugfixes for dxf export --- src/core/dxf/qgsdxfexport.cpp | 80 +++++++------------ src/core/dxf/qgsdxfexport.h | 6 +- .../symbology-ng/qgsfillsymbollayerv2.cpp | 34 ++++++-- src/core/symbology-ng/qgsfillsymbollayerv2.h | 8 +- .../symbology-ng/qgslinesymbollayerv2.cpp | 27 +++++++ src/core/symbology-ng/qgslinesymbollayerv2.h | 3 + src/core/symbology-ng/qgssymbollayerv2.cpp | 9 ++- src/core/symbology-ng/qgssymbollayerv2.h | 6 +- 8 files changed, 101 insertions(+), 72 deletions(-) diff --git a/src/core/dxf/qgsdxfexport.cpp b/src/core/dxf/qgsdxfexport.cpp index 171c18a49f12..b0fa7b2612d0 100644 --- a/src/core/dxf/qgsdxfexport.cpp +++ b/src/core/dxf/qgsdxfexport.cpp @@ -547,8 +547,9 @@ void QgsDxfExport::writeEntities() continue; } - QgsRenderContext ctx; + QgsRenderContext ctx = renderContext(); ctx.setRendererScale( mSymbologyScaleDenominator ); + QgsSymbolV2RenderContext sctx( ctx, QgsSymbolV2::MM , 1.0, false, 0, 0 ); QgsFeatureRendererV2* renderer = vl->rendererV2(); renderer->startRender( ctx, vl ); @@ -569,9 +570,10 @@ void QgsDxfExport::writeEntities() QgsFeature fet; while ( featureIt.nextFeature( fet ) ) { + sctx.setFeature( &fet ); if ( mSymbologyExport == NoSymbology ) { - addFeature( fet, dxfLayerName( vl->name() ), 0, 0 ); //no symbology at all + addFeature( sctx, dxfLayerName( vl->name() ), 0, 0 ); //no symbology at all } else { @@ -593,7 +595,7 @@ void QgsDxfExport::writeEntities() int nSymbolLayers = ( *symbolIt )->symbolLayerCount(); for ( int i = 0; i < nSymbolLayers; ++i ) { - addFeature( fet, dxfLayerName( vl->name() ), ( *symbolIt )->symbolLayer( i ), *symbolIt ); + addFeature( sctx, dxfLayerName( vl->name() ), ( *symbolIt )->symbolLayer( i ), *symbolIt ); } } } @@ -605,7 +607,7 @@ void QgsDxfExport::writeEntities() { continue; } - addFeature( fet, dxfLayerName( vl->name() ), s->symbolLayer( 0 ), s ); + addFeature( sctx, dxfLayerName( vl->name() ), s->symbolLayer( 0 ), s ); } } } @@ -629,7 +631,9 @@ void QgsDxfExport::writeEntitiesSymbolLevels( QgsVectorLayer* layer ) } QHash< QgsSymbolV2*, QList > features; - startRender( layer ); + QgsRenderContext ctx = renderContext(); + QgsSymbolV2RenderContext sctx( ctx, QgsSymbolV2::MM , 1.0, false, 0, 0 ); + renderer->startRender( ctx, layer ); //get iterator QgsFeatureRequest req; @@ -695,11 +699,11 @@ void QgsDxfExport::writeEntitiesSymbolLevels( QgsVectorLayer* layer ) QList::iterator featureIt = featureList.begin(); for ( ; featureIt != featureList.end(); ++featureIt ) { - addFeature( *featureIt, layer->name(), levelIt.key()->symbolLayer( llayer ), levelIt.key() ); + addFeature( sctx, layer->name(), levelIt.key()->symbolLayer( llayer ), levelIt.key() ); } } } - stopRender( layer ); + renderer->stopRender( ctx ); } void QgsDxfExport::writeEndFile() @@ -874,17 +878,23 @@ QgsRectangle QgsDxfExport::dxfExtent() const return extent; } -void QgsDxfExport::addFeature( const QgsFeature& fet, const QString& layer, const QgsSymbolLayerV2* symbolLayer, const QgsSymbolV2* symbol ) +void QgsDxfExport::addFeature( const QgsSymbolV2RenderContext& ctx, const QString& layer, const QgsSymbolLayerV2* symbolLayer, const QgsSymbolV2* symbol ) { - QgsGeometry* geom = fet.geometry(); + const QgsFeature* fet = ctx.feature(); + if ( !fet ) + { + return; + } + + QgsGeometry* geom = fet->geometry(); if ( geom ) { int c = 0; if ( mSymbologyExport != NoSymbology ) { - c = colorFromSymbolLayer( symbolLayer ); + c = colorFromSymbolLayer( symbolLayer, ctx ); } - double width = symbolLayer->dxfWidth( *this );; + double width = symbolLayer->dxfWidth( *this, ctx ); QString lineStyleName = "CONTINUOUS"; if ( mSymbologyExport != NoSymbology ) { @@ -895,7 +905,7 @@ void QgsDxfExport::addFeature( const QgsFeature& fet, const QString& layer, cons //single point if ( geometryType == QGis::WKBPoint || geometryType == QGis::WKBPoint25D ) { - writePoint( geom->asPoint(), layer, c, &fet, symbolLayer, symbol ); + writePoint( geom->asPoint(), layer, c, fet, symbolLayer, symbol ); } //multipoint @@ -905,7 +915,7 @@ void QgsDxfExport::addFeature( const QgsFeature& fet, const QString& layer, cons QgsMultiPoint::const_iterator it = multiPoint.constBegin(); for ( ; it != multiPoint.constEnd(); ++it ) { - writePoint( *it, layer, c, &fet, symbolLayer, symbol ); + writePoint( *it, layer, c, fet, symbolLayer, symbol ); } } @@ -974,14 +984,14 @@ double QgsDxfExport::scaleToMapUnits( double value, QgsSymbolV2::OutputUnit symb return value; } -int QgsDxfExport::colorFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer ) +int QgsDxfExport::colorFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer, const QgsSymbolV2RenderContext& ctx ) { if ( !symbolLayer ) { return 0; } - QColor c = symbolLayer->dxfColor(); + QColor c = symbolLayer->dxfColor( ctx ); return closestColorMatch( c.rgba() ); } @@ -1046,40 +1056,6 @@ QgsRenderContext QgsDxfExport::renderContext() const return context; } -void QgsDxfExport::startRender( QgsVectorLayer* vl ) const -{ - if ( !vl ) - { - return; - } - - QgsFeatureRendererV2* renderer = vl->rendererV2(); - if ( !renderer ) - { - return; - } - - QgsRenderContext ctx = renderContext(); - renderer->startRender( ctx, vl ); -} - -void QgsDxfExport::stopRender( QgsVectorLayer* vl ) const -{ - if ( !vl ) - { - return; - } - - QgsFeatureRendererV2* renderer = vl->rendererV2(); - if ( !renderer ) - { - return; - } - - QgsRenderContext ctx = renderContext(); - renderer->stopRender( ctx ); -} - double QgsDxfExport::mapUnitScaleFactor( double scaleDenominator, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits ) { if ( symbolUnits == QgsSymbolV2::MapUnit ) @@ -1188,9 +1164,9 @@ void QgsDxfExport::writeSymbolLayerLinestyle( const QgsSymbolLayerV2* symbolLaye QgsSymbolV2::OutputUnit unit; QVector customLinestyle = symbolLayer->dxfCustomDashPattern( unit ); - if ( customLinestyle.size() < 0 ) + if ( customLinestyle.size() > 0 ) { - QString name = QString( "symbolLayer%1" ).arg( mSymbolLayerCounter ); + QString name = QString( "symbolLayer%1" ).arg( mSymbolLayerCounter++ ); writeLinestyle( name, customLinestyle, unit ); mLineStyles.insert( symbolLayer, name ); } @@ -1220,7 +1196,7 @@ void QgsDxfExport::writeLinestyle( const QString& styleName, const QVector::const_iterator dashIt = pattern.constBegin(); for ( ; dashIt != pattern.constEnd(); ++dashIt ) { - length += *dashIt; + length += ( *dashIt * mapUnitScaleFactor( mSymbologyScaleDenominator, u, mMapUnits ) ); } writeGroup( 0, "LTYPE" ); diff --git a/src/core/dxf/qgsdxfexport.h b/src/core/dxf/qgsdxfexport.h index 4878537e391a..76647f6e9629 100644 --- a/src/core/dxf/qgsdxfexport.h +++ b/src/core/dxf/qgsdxfexport.h @@ -137,11 +137,11 @@ class CORE_EXPORT QgsDxfExport QgsRectangle dxfExtent() const; - void addFeature( const QgsFeature& fet, const QString& layer, const QgsSymbolLayerV2* symbolLayer, const QgsSymbolV2* symbol ); + void addFeature( const QgsSymbolV2RenderContext& ctx, const QString& layer, const QgsSymbolLayerV2* symbolLayer, const QgsSymbolV2* symbol ); double scaleToMapUnits( double value, QgsSymbolV2::OutputUnit symbolUnits, QGis::UnitType mapUnits ) const; //returns dxf palette index from symbol layer color - static int colorFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer ); + static int colorFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer, const QgsSymbolV2RenderContext& ctx ); QString lineStyleFromSymbolLayer( const QgsSymbolLayerV2* symbolLayer ); //functions for dxf palette @@ -150,8 +150,6 @@ class CORE_EXPORT QgsDxfExport //helper functions for symbology export QgsRenderContext renderContext() const; - void startRender( QgsVectorLayer* vl ) const; - void stopRender( QgsVectorLayer* vl ) const; QList< QPair< QgsSymbolLayerV2*, QgsSymbolV2* > > symbolLayers(); static int nLineTypes( const QList< QPair< QgsSymbolLayerV2*, QgsSymbolV2*> >& symbolLayers ); diff --git a/src/core/symbology-ng/qgsfillsymbollayerv2.cpp b/src/core/symbology-ng/qgsfillsymbollayerv2.cpp index acd9ae076dd4..52affde537d7 100644 --- a/src/core/symbology-ng/qgsfillsymbollayerv2.cpp +++ b/src/core/symbology-ng/qgsfillsymbollayerv2.cpp @@ -289,19 +289,35 @@ double QgsSimpleFillSymbolLayerV2::estimateMaxBleed() const return penBleed + offsetBleed; } -double QgsSimpleFillSymbolLayerV2::dxfWidth( const QgsDxfExport& e ) const +double QgsSimpleFillSymbolLayerV2::dxfWidth( const QgsDxfExport& e, const QgsSymbolV2RenderContext& context ) const { - return mBorderWidth * e.mapUnitScaleFactor( e.symbologyScaleDenominator(), mBorderWidthUnit, e.mapUnits() ); + double width = mBorderWidth; + QgsExpression* widthBorderExpression = expression( "width_border" ); + if ( widthBorderExpression ) + { + width = widthBorderExpression->evaluate( const_cast( context.feature() ) ).toDouble(); + } + return width * e.mapUnitScaleFactor( e.symbologyScaleDenominator(), mBorderWidthUnit, e.mapUnits() ); } -QColor QgsSimpleFillSymbolLayerV2::dxfColor() const +QColor QgsSimpleFillSymbolLayerV2::dxfColor( const QgsSymbolV2RenderContext& context ) const { if ( mBrushStyle == Qt::NoBrush ) { + QgsExpression* colorBorderExpression = expression( "color_border" ); + if ( colorBorderExpression ) + { + return QgsSymbolLayerV2Utils::decodeColor( colorBorderExpression->evaluate( const_cast( context.feature() ) ).toString() ); + } return mBorderColor; } else { + QgsExpression* colorExpression = expression( "color" ); + if ( colorExpression ) + { + return QgsSymbolLayerV2Utils::decodeColor( colorExpression->evaluate( const_cast( context.feature() ) ).toString() ); + } return mColor; } } @@ -858,12 +874,18 @@ double QgsImageFillSymbolLayer::estimateMaxBleed() const return subLayerBleed; } -double QgsImageFillSymbolLayer::dxfWidth( const QgsDxfExport& e ) const +double QgsImageFillSymbolLayer::dxfWidth( const QgsDxfExport& e, const QgsSymbolV2RenderContext& context ) const { - return mOutlineWidth * e.mapUnitScaleFactor( e.symbologyScaleDenominator(), mOutlineWidthUnit, e.mapUnits() ); + double width = mOutlineWidth; + QgsExpression* widthExpression = expression( "width" ); + if ( widthExpression ) + { + width = widthExpression->evaluate( const_cast( context.feature() ) ).toDouble(); + } + return width * e.mapUnitScaleFactor( e.symbologyScaleDenominator(), mOutlineWidthUnit, e.mapUnits() ); } -QColor QgsImageFillSymbolLayer::dxfColor() const +QColor QgsImageFillSymbolLayer::dxfColor( const QgsSymbolV2RenderContext& context ) const { if ( !mOutline ) { diff --git a/src/core/symbology-ng/qgsfillsymbollayerv2.h b/src/core/symbology-ng/qgsfillsymbollayerv2.h index fa357241e645..9582dbe76383 100644 --- a/src/core/symbology-ng/qgsfillsymbollayerv2.h +++ b/src/core/symbology-ng/qgsfillsymbollayerv2.h @@ -99,8 +99,8 @@ class CORE_EXPORT QgsSimpleFillSymbolLayerV2 : public QgsFillSymbolLayerV2 double estimateMaxBleed() const; - double dxfWidth( const QgsDxfExport& e ) const; - QColor dxfColor() const; + double dxfWidth( const QgsDxfExport& e, const QgsSymbolV2RenderContext& context ) const; + QColor dxfColor( const QgsSymbolV2RenderContext& context ) const; Qt::PenStyle dxfPenStyle() const; protected: @@ -287,8 +287,8 @@ class CORE_EXPORT QgsImageFillSymbolLayer: public QgsFillSymbolLayerV2 double estimateMaxBleed() const; - virtual double dxfWidth( const QgsDxfExport& e ) const; - virtual QColor dxfColor() const; + virtual double dxfWidth( const QgsDxfExport& e, const QgsSymbolV2RenderContext& context ) const; + virtual QColor dxfColor( const QgsSymbolV2RenderContext& context ) const; virtual Qt::PenStyle dxfPenStyle() const; protected: diff --git a/src/core/symbology-ng/qgslinesymbollayerv2.cpp b/src/core/symbology-ng/qgslinesymbollayerv2.cpp index 53379b405af7..eefd75f88d3c 100644 --- a/src/core/symbology-ng/qgslinesymbollayerv2.cpp +++ b/src/core/symbology-ng/qgslinesymbollayerv2.cpp @@ -14,6 +14,7 @@ ***************************************************************************/ #include "qgslinesymbollayerv2.h" +#include "qgsdxfexport.h" #include "qgssymbollayerv2utils.h" #include "qgsexpression.h" #include "qgsrendercontext.h" @@ -395,6 +396,32 @@ QVector QgsSimpleLineSymbolLayerV2::dxfCustomDashPattern( QgsSymbolV2::Ou return mUseCustomDashPattern ? mCustomDashVector : QVector() ; } +double QgsSimpleLineSymbolLayerV2::dxfWidth( const QgsDxfExport& e, const QgsSymbolV2RenderContext& context ) const +{ + double width = mWidth; + QgsExpression* strokeWidthExpression = expression( "width" ); + if ( strokeWidthExpression ) + { + width = strokeWidthExpression->evaluate( const_cast( context.feature() ) ).toDouble() * e.mapUnitScaleFactor( e.symbologyScaleDenominator(), widthUnit(), e.mapUnits() ); + } + else if ( context.renderHints() & QgsSymbolV2::DataDefinedSizeScale ) + { + width = mWidth * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mWidthUnit ); + } + + return width * e.mapUnitScaleFactor( e.symbologyScaleDenominator(), widthUnit(), e.mapUnits() ); +} + +QColor QgsSimpleLineSymbolLayerV2::dxfColor( const QgsSymbolV2RenderContext& context ) const +{ + QgsExpression* strokeColorExpression = expression( "color" ); + if ( strokeColorExpression ) + { + return ( QgsSymbolLayerV2Utils::decodeColor( strokeColorExpression->evaluate( const_cast( context.feature() ) ).toString() ) ); + } + return mColor; +} + ///////// diff --git a/src/core/symbology-ng/qgslinesymbollayerv2.h b/src/core/symbology-ng/qgslinesymbollayerv2.h index 38cbd085ca1f..7222f27c5856 100644 --- a/src/core/symbology-ng/qgslinesymbollayerv2.h +++ b/src/core/symbology-ng/qgslinesymbollayerv2.h @@ -93,6 +93,9 @@ class CORE_EXPORT QgsSimpleLineSymbolLayerV2 : public QgsLineSymbolLayerV2 QVector dxfCustomDashPattern( QgsSymbolV2::OutputUnit& unit ) const; + double dxfWidth( const QgsDxfExport& e, const QgsSymbolV2RenderContext& context ) const; + QColor dxfColor( const QgsSymbolV2RenderContext& context ) const; + protected: Qt::PenStyle mPenStyle; Qt::PenJoinStyle mPenJoinStyle; diff --git a/src/core/symbology-ng/qgssymbollayerv2.cpp b/src/core/symbology-ng/qgssymbollayerv2.cpp index 81b9aa59ae51..096984abb10a 100644 --- a/src/core/symbology-ng/qgssymbollayerv2.cpp +++ b/src/core/symbology-ng/qgssymbollayerv2.cpp @@ -94,14 +94,16 @@ bool QgsSymbolLayerV2::writeDxf( QgsDxfExport& e, return false; } -double QgsSymbolLayerV2::dxfWidth( const QgsDxfExport& e ) const +double QgsSymbolLayerV2::dxfWidth( const QgsDxfExport& e, const QgsSymbolV2RenderContext& context ) const { Q_UNUSED( e ); + Q_UNUSED( context ); return 1.0; } -QColor QgsSymbolLayerV2::dxfColor() const +QColor QgsSymbolLayerV2::dxfColor( const QgsSymbolV2RenderContext& context ) const { + Q_UNUSED( context ); return color(); } @@ -363,8 +365,9 @@ void QgsLineSymbolLayerV2::renderPolygonOutline( const QPolygonF& points, QList< } } -double QgsLineSymbolLayerV2::dxfWidth( const QgsDxfExport& e ) const +double QgsLineSymbolLayerV2::dxfWidth( const QgsDxfExport& e, const QgsSymbolV2RenderContext& context ) const { + Q_UNUSED( context ); return ( width() * e.mapUnitScaleFactor( e.symbologyScaleDenominator(), widthUnit(), e.mapUnits() ) ); } diff --git a/src/core/symbology-ng/qgssymbollayerv2.h b/src/core/symbology-ng/qgssymbollayerv2.h index 20c6ad615e9b..63c9544ac66b 100644 --- a/src/core/symbology-ng/qgssymbollayerv2.h +++ b/src/core/symbology-ng/qgssymbollayerv2.h @@ -119,9 +119,9 @@ class CORE_EXPORT QgsSymbolLayerV2 const QgsFeature* f, const QPointF& shift = QPointF( 0.0, 0.0 ) ) const; - virtual double dxfWidth( const QgsDxfExport& e ) const; + virtual double dxfWidth( const QgsDxfExport& e, const QgsSymbolV2RenderContext& context ) const; - virtual QColor dxfColor() const; + virtual QColor dxfColor( const QgsSymbolV2RenderContext& context ) const; virtual QVector dxfCustomDashPattern( QgsSymbolV2::OutputUnit& unit ) const; virtual Qt::PenStyle dxfPenStyle() const; @@ -254,7 +254,7 @@ class CORE_EXPORT QgsLineSymbolLayerV2 : public QgsSymbolLayerV2 void drawPreviewIcon( QgsSymbolV2RenderContext& context, QSize size ); - virtual double dxfWidth( const QgsDxfExport& e ) const; + virtual double dxfWidth( const QgsDxfExport& e, const QgsSymbolV2RenderContext& context ) const; protected: QgsLineSymbolLayerV2( bool locked = false );