Skip to content

Commit

Permalink
use line pattern fill symbol layer line symbol for fill instead of fo…
Browse files Browse the repository at this point in the history
…r outline
  • Loading branch information
blazek committed Jan 17, 2014
1 parent 71e09ec commit 8f8ea0e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 26 deletions.
114 changes: 89 additions & 25 deletions src/core/symbology-ng/qgsfillsymbollayerv2.cpp
Expand Up @@ -870,8 +870,12 @@ bool QgsImageFillSymbolLayer::setSubSymbol( QgsSymbolV2* symbol )

double QgsImageFillSymbolLayer::estimateMaxBleed() const
{
double subLayerBleed = mOutline->symbolLayer( 0 )->estimateMaxBleed();
return subLayerBleed;
if ( mOutline && mOutline->symbolLayer( 0 ) )
{
double subLayerBleed = mOutline->symbolLayer( 0 )->estimateMaxBleed();
return subLayerBleed;
}
return 0;
}

double QgsImageFillSymbolLayer::dxfWidth( const QgsDxfExport& e, const QgsSymbolV2RenderContext& context ) const
Expand Down Expand Up @@ -936,7 +940,6 @@ QgsSVGFillSymbolLayer::QgsSVGFillSymbolLayer( const QByteArray& svgData, double

QgsSVGFillSymbolLayer::~QgsSVGFillSymbolLayer()
{
delete mOutline;
delete mSvgPattern;
}

Expand Down Expand Up @@ -1399,12 +1402,47 @@ void QgsSVGFillSymbolLayer::setDefaultSvgParams()


QgsLinePatternFillSymbolLayer::QgsLinePatternFillSymbolLayer(): QgsImageFillSymbolLayer(), mDistanceUnit( QgsSymbolV2::MM ), mLineWidthUnit( QgsSymbolV2::MM ),
mOffsetUnit( QgsSymbolV2::MM )
mOffsetUnit( QgsSymbolV2::MM ), mFillLineSymbol( 0 )
{
setSubSymbol( new QgsLineSymbolV2() );
QgsImageFillSymbolLayer::setSubSymbol( 0 ); //no outline
}

QgsLinePatternFillSymbolLayer::~QgsLinePatternFillSymbolLayer()
{
delete mFillLineSymbol;
}

bool QgsLinePatternFillSymbolLayer::setSubSymbol( QgsSymbolV2* symbol )
{
if ( !symbol )
{
return false;
}

if ( symbol->type() == QgsSymbolV2::Line )
{
QgsLineSymbolV2* lineSymbol = dynamic_cast<QgsLineSymbolV2*>( symbol );
if ( lineSymbol )
{
delete mFillLineSymbol;
mFillLineSymbol = lineSymbol;

return true;
}
}
delete symbol;
return false;
}

QgsSymbolV2* QgsLinePatternFillSymbolLayer::subSymbol()
{
return mFillLineSymbol;
}

double QgsLinePatternFillSymbolLayer::estimateMaxBleed() const
{
return 0;
}

void QgsLinePatternFillSymbolLayer::setOutputUnit( QgsSymbolV2::OutputUnit unit )
Expand Down Expand Up @@ -1507,8 +1545,10 @@ QString QgsLinePatternFillSymbolLayer::layerType() const
void QgsLinePatternFillSymbolLayer::applyPattern( const QgsSymbolV2RenderContext& context, QBrush& brush, double lineAngle, double distance,
double lineWidth, const QColor& color )
{
Q_UNUSED( lineWidth );
Q_UNUSED( color );
const QgsRenderContext& ctx = context.renderContext();
double outlinePixelWidth = lineWidth * QgsSymbolLayerV2Utils::pixelSizeScaleFactor( ctx, mLineWidthUnit );
//double outlinePixelWidth = lineWidth * QgsSymbolLayerV2Utils::pixelSizeScaleFactor( ctx, mLineWidthUnit );
double outputPixelDist = distance * QgsSymbolLayerV2Utils::pixelSizeScaleFactor( ctx, mDistanceUnit );
double outputPixelOffset = mOffset * QgsSymbolLayerV2Utils::pixelSizeScaleFactor( ctx, mOffsetUnit );

Expand Down Expand Up @@ -1538,13 +1578,6 @@ void QgsLinePatternFillSymbolLayer::applyPattern( const QgsSymbolV2RenderContext

QImage patternImage( width, height, QImage::Format_ARGB32 );
patternImage.fill( 0 );
QPainter p( &patternImage );

p.setRenderHint( QPainter::Antialiasing, true );
QPen pen( color );
pen.setWidthF( outlinePixelWidth );
pen.setCapStyle( Qt::FlatCap );
p.setPen( pen );

QPoint p1, p2, p3, p4, p5, p6;
if ( qgsDoubleNear( lineAngle, 0.0 ) || qgsDoubleNear( lineAngle, 360.0 ) || qgsDoubleNear( lineAngle, 180.0 ) )
Expand Down Expand Up @@ -1607,10 +1640,44 @@ void QgsLinePatternFillSymbolLayer::applyPattern( const QgsSymbolV2RenderContext
p2 = QPoint( tempPt.x(), tempPt.y() );;
}

p.drawLine( p1, p2 );
p.drawLine( p3, p4 );
p.drawLine( p5, p6 );
p.end();
if ( mFillLineSymbol )
{
QPainter p( &patternImage );

#if 0
// DEBUG: Draw rectangle
//p.setRenderHint( QPainter::Antialiasing, true );
QPen pen( QColor( Qt::black ) );
pen.setWidthF( 0.1 );
pen.setCapStyle( Qt::FlatCap );
p.setPen( pen );
QPolygon polygon = QPolygon() << QPoint( 0, 0 ) << QPoint( width, 0 ) << QPoint( width, height ) << QPoint( 0, height ) << QPoint( 0, 0 ) ;
p.drawPolygon( polygon );
#endif

// line rendering needs context for drawing on patternImage
QgsRenderContext lineRenderContext;
lineRenderContext.setPainter( &p );
lineRenderContext.setRasterScaleFactor( 1.0 );
lineRenderContext.setScaleFactor( context.renderContext().scaleFactor() * context.renderContext().rasterScaleFactor() );
QgsMapToPixel mtp( context.renderContext().mapToPixel().mapUnitsPerPixel() / context.renderContext().rasterScaleFactor() );
lineRenderContext.setMapToPixel( mtp );
lineRenderContext.setForceVectorOutput( false );

mFillLineSymbol->startRender( lineRenderContext );

QVector<QPolygon> polygons;
polygons.append( QPolygon() << p1 << p2 );
polygons.append( QPolygon() << p3 << p4 );
polygons.append( QPolygon() << p5 << p6 );
foreach ( QPolygon polygon, polygons )
{
mFillLineSymbol->renderPolyline( polygon, context.feature(), lineRenderContext, -1, context.selected() );
}

mFillLineSymbol->stopRender( lineRenderContext );
p.end();
}

//set image to mBrush
if ( !qgsDoubleNear( context.alpha(), 1.0 ) )
Expand All @@ -1633,9 +1700,9 @@ void QgsLinePatternFillSymbolLayer::startRender( QgsSymbolV2RenderContext& conte
{
applyPattern( context, mBrush, mLineAngle, mDistance, mLineWidth, mColor );

if ( mOutline )
if ( mFillLineSymbol )
{
mOutline->startRender( context.renderContext() );
mFillLineSymbol->startRender( context.renderContext() );
}

prepareExpressions( context.layer(), context.renderContext().rendererScale() );
Expand Down Expand Up @@ -1663,9 +1730,9 @@ QgsStringMap QgsLinePatternFillSymbolLayer::properties() const
QgsSymbolLayerV2* QgsLinePatternFillSymbolLayer::clone() const
{
QgsLinePatternFillSymbolLayer* clonedLayer = static_cast<QgsLinePatternFillSymbolLayer*>( QgsLinePatternFillSymbolLayer::create( properties() ) );
if ( mOutline )
if ( mFillLineSymbol )
{
clonedLayer->setSubSymbol( mOutline->clone() );
clonedLayer->setSubSymbol( mFillLineSymbol->clone() );
}
clonedLayer->setDistanceUnit( mDistanceUnit );
clonedLayer->setLineWidthUnit( mLineWidthUnit );
Expand Down Expand Up @@ -1713,12 +1780,9 @@ void QgsLinePatternFillSymbolLayer::toSld( QDomDocument &doc, QDomElement &eleme
QPointF lineOffset( sin( mLineAngle ) * mOffset, cos( mLineAngle ) * mOffset );
QgsSymbolLayerV2Utils::createDisplacementElement( doc, graphicElem, lineOffset );

if ( mOutline )
if ( mFillLineSymbol )
{
// the outline sub symbol should be stored within the Stroke element,
// but it will be stored in a separated LineSymbolizer because it could
// have more than one layer
mOutline->toSld( doc, element, props );
mFillLineSymbol->toSld( doc, element, props );
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/core/symbology-ng/qgsfillsymbollayerv2.h
Expand Up @@ -285,7 +285,7 @@ class CORE_EXPORT QgsImageFillSymbolLayer: public QgsFillSymbolLayerV2
void setOutlineWidthUnit( QgsSymbolV2::OutputUnit unit ) { mOutlineWidthUnit = unit; }
QgsSymbolV2::OutputUnit outlineWidthUnit() const { return mOutlineWidthUnit; }

double estimateMaxBleed() const;
virtual double estimateMaxBleed() const;

virtual double dxfWidth( const QgsDxfExport& e, const QgsSymbolV2RenderContext& context ) const;
virtual QColor dxfColor( const QgsSymbolV2RenderContext& context ) const;
Expand Down Expand Up @@ -407,6 +407,8 @@ class CORE_EXPORT QgsLinePatternFillSymbolLayer: public QgsImageFillSymbolLayer

void toSld( QDomDocument &doc, QDomElement &element, QgsStringMap props ) const;

double estimateMaxBleed() const;

QString ogrFeatureStyleWidth( double widthScaleFactor ) const;

//getters and setters
Expand All @@ -433,6 +435,9 @@ class CORE_EXPORT QgsLinePatternFillSymbolLayer: public QgsImageFillSymbolLayer
void setOutputUnit( QgsSymbolV2::OutputUnit unit );
QgsSymbolV2::OutputUnit outputUnit() const;

bool setSubSymbol( QgsSymbolV2* symbol );
QgsSymbolV2* subSymbol();

protected:
/**Distance (in mm or map units) between lines*/
double mDistance;
Expand All @@ -452,6 +457,9 @@ class CORE_EXPORT QgsLinePatternFillSymbolLayer: public QgsImageFillSymbolLayer
private:
/**Applies the svg pattern to the brush*/
void applyPattern( const QgsSymbolV2RenderContext& context, QBrush& brush, double lineAngle, double distance, double lineWidth, const QColor& color );

/**Fill line*/
QgsLineSymbolV2* mFillLineSymbol;
};

class CORE_EXPORT QgsPointPatternFillSymbolLayer: public QgsImageFillSymbolLayer
Expand Down

0 comments on commit 8f8ea0e

Please sign in to comment.