Skip to content

Commit

Permalink
First experimental version of line pattern drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jul 6, 2011
1 parent ae176a9 commit afecac7
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/core/symbology-ng/qgsfillsymbollayerv2.cpp
Expand Up @@ -403,6 +403,54 @@ QString QgsLinePatternFillSymbolLayer::layerType() const

void QgsLinePatternFillSymbolLayer::startRender( QgsSymbolV2RenderContext& context )
{
//create image
int height, width;
if( doubleNear( mAngle, 0 ) )
{
height = context.outputPixelSize( mDistance );
width = height; //width can be set to arbitrary value
}
else
{
height = context.outputPixelSize( mDistance );
width = height / tan( mAngle * M_PI / 180 );
}

double outlinePixelWidth = context.outputPixelSize( mLineWidth );

//find a suitable multiple of width and heigh

QImage patternImage( width, height, QImage::Format_ARGB32);
patternImage.fill( 0 );
QPainter p( &patternImage );
p.setRenderHint( QPainter::Antialiasing, true );
QPen pen( mColor );
pen.setWidthF( outlinePixelWidth );
pen.setCapStyle( Qt::FlatCap );
p.setPen( pen );

//draw line and dots in the border
p.drawLine( QPointF( 0, height ), QPointF( width, 0 ) );

//todo: calculate triangles more accurately
double d1 = (outlinePixelWidth / 2.0) / cos( mAngle * M_PI / 180 );
double d2 = (outlinePixelWidth / 2.0) / cos( (90 - mAngle) * M_PI / 180 );

p.setPen( QPen( Qt::NoPen ) );
p.setBrush( QBrush( mColor ) );
QPolygonF triangle1;
triangle1 << QPointF( 0, 0 ) << QPointF( 0, d1 ) << QPointF( d2, 0 ) << QPointF( 0, 0 );
p.drawPolygon( triangle1 );

QPolygonF triangle2;
triangle2 << QPointF( width, height ) << QPointF( width - d2, height ) << QPointF( width, height - d1 ) << QPointF( width, height );
p.drawPolygon( triangle2 );

p.end();

//set image to mBrush
mBrush.setTextureImage( patternImage );

}

void QgsLinePatternFillSymbolLayer::stopRender( QgsSymbolV2RenderContext& context )
Expand All @@ -411,6 +459,14 @@ void QgsLinePatternFillSymbolLayer::stopRender( QgsSymbolV2RenderContext& contex

void QgsLinePatternFillSymbolLayer::renderPolygon( const QPolygonF& points, QList<QPolygonF>* rings, QgsSymbolV2RenderContext& context )
{
QPainter* p = context.renderContext().painter();
if ( !p )
{
return;
}

p->setBrush( mBrush );
_renderPolygon( p, points, rings );
}

QgsStringMap QgsLinePatternFillSymbolLayer::properties() const
Expand Down
1 change: 1 addition & 0 deletions src/core/symbology-ng/qgsfillsymbollayerv2.h
Expand Up @@ -160,6 +160,7 @@ class CORE_EXPORT QgsLinePatternFillSymbolLayer: public QgsFillSymbolLayerV2
double mLineWidth;
QColor mColor;
//todo: line type
QBrush mBrush;
};


Expand Down
4 changes: 4 additions & 0 deletions src/gui/symbology-ng/qgssymbollayerv2widget.cpp
Expand Up @@ -942,6 +942,7 @@ void QgsLinePatternFillSymbolLayerWidget::on_mAngleSpinBox_valueChanged( double
if( mLayer )
{
mLayer->setAngle( d );
emit changed();
}
}

Expand All @@ -950,6 +951,7 @@ void QgsLinePatternFillSymbolLayerWidget::on_mDistanceSpinBox_valueChanged( doub
if( mLayer )
{
mLayer->setDistance( d );
emit changed();
}
}

Expand All @@ -958,6 +960,7 @@ void QgsLinePatternFillSymbolLayerWidget::on_mLineWidthSpinBox_valueChanged( dou
if( mLayer )
{
mLayer->setLineWidth( d );
emit changed();
}
}

Expand All @@ -969,6 +972,7 @@ void QgsLinePatternFillSymbolLayerWidget::on_mColorPushButton_clicked()
if( c.isValid() )
{
mLayer->setColor( c );
emit changed();
}
}
}
Expand Down

0 comments on commit afecac7

Please sign in to comment.