Skip to content

Commit

Permalink
[FEATURE]: use a custom linestyle for composer grid
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Nov 30, 2012
1 parent 727d66a commit 9342a9c
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 101 deletions.
30 changes: 17 additions & 13 deletions src/app/composer/qgscomposermapwidget.cpp
Expand Up @@ -81,6 +81,7 @@ QgsComposerMapWidget::QgsComposerMapWidget( QgsComposerMap* composerMap ): QWidg
}

updateOverviewSymbolMarker();
updateLineSymbolMarker();

updateGuiElements();
blockAllSignals( false );
Expand Down Expand Up @@ -412,10 +413,6 @@ void QgsComposerMapWidget::updateGuiElements()

mCoordinatePrecisionSpinBox->setValue( mComposerMap->gridAnnotationPrecision() );

QPen gridPen = mComposerMap->gridPen();
mLineWidthSpinBox->setValue( gridPen.widthF() );
mLineColorButton->setColor( gridPen.color() );

blockAllSignals( false );
}
}
Expand Down Expand Up @@ -468,8 +465,7 @@ void QgsComposerMapWidget::blockAllSignals( bool b )
mKeepLayerListCheckBox->blockSignals( b );
mSetToMapCanvasExtentButton->blockSignals( b );
mUpdatePreviewButton->blockSignals( b );
mLineWidthSpinBox->blockSignals( b );
mLineColorButton->blockSignals( b );
mGridLineStyleButton->blockSignals( b );
mDrawAnnotationCheckBox->blockSignals( b );
mAnnotationFontButton->blockSignals( b );
mAnnotationFormatComboBox->blockSignals( b );
Expand Down Expand Up @@ -696,20 +692,19 @@ void QgsComposerMapWidget::on_mLineWidthSpinBox_valueChanged( double d )
mComposerMap->endCommand();
}

void QgsComposerMapWidget::on_mLineColorButton_clicked()
void QgsComposerMapWidget::on_mGridLineStyleButton_clicked()
{
if ( !mComposerMap )
{
return;
}
QColor newColor = QColorDialog::getColor( mLineColorButton->color() );
if ( newColor.isValid() )

QgsSymbolV2SelectorDialog d( mComposerMap->gridLineSymbol(), QgsStyleV2::defaultStyle(), 0 );
if ( d.exec() == QDialog::Accepted )
{
mComposerMap->beginCommand( tr( "Grid pen changed" ) );
mLineColorButton->setColor( newColor );
mComposerMap->setGridPenColor( newColor );
mComposerMap->endCommand();
updateLineSymbolMarker();
}

mComposerMap->update();
}

Expand Down Expand Up @@ -1022,6 +1017,15 @@ void QgsComposerMapWidget::updateOverviewSymbolMarker()
}
}

void QgsComposerMapWidget::updateLineSymbolMarker()
{
if ( mComposerMap )
{
QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( mComposerMap->gridLineSymbol(), mGridLineStyleButton->iconSize() );
mGridLineStyleButton->setIcon( icon );
}
}

void QgsComposerMapWidget::refreshMapComboBox()
{
if ( !mComposerMap )
Expand Down
3 changes: 2 additions & 1 deletion src/app/composer/qgscomposermapwidget.h
Expand Up @@ -58,7 +58,7 @@ class QgsComposerMapWidget: public QWidget, private Ui::QgsComposerMapWidgetBase
void on_mOffsetXSpinBox_editingFinished();
void on_mOffsetYSpinBox_editingFinished();
void on_mLineWidthSpinBox_valueChanged( double d );
void on_mLineColorButton_clicked();
void on_mGridLineStyleButton_clicked();
void on_mGridTypeComboBox_currentIndexChanged( const QString& text );
void on_mCrossWidthSpinBox_valueChanged( double d );
void on_mAnnotationFontButton_clicked();
Expand Down Expand Up @@ -116,6 +116,7 @@ class QgsComposerMapWidget: public QWidget, private Ui::QgsComposerMapWidgetBase
void initAnnotationDirectionBox( QComboBox* c, QgsComposerMap::GridAnnotationDirection dir );

void updateOverviewSymbolMarker();
void updateLineSymbolMarker();

/**Updates the map combo box with the current composer map ids*/
void refreshMapComboBox();
Expand Down
123 changes: 99 additions & 24 deletions src/core/composer/qgscomposermap.cpp
Expand Up @@ -48,7 +48,9 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
{
mComposition = composition;
mOverviewFrameMapSymbol = 0;
mGridLineSymbol = 0;
createDefaultOverviewFrameSymbol();
createDefaultGridLineSymbol();

mId = 0;
assignFreeId();
Expand All @@ -74,7 +76,6 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
}
setSceneRect( QRectF( x, y, width, height ) );
setToolTip( tr( "Map %1" ).arg( mId ) );
mGridPen.setCapStyle( Qt::FlatCap );

initGridAnnotationFormatFromProject();
}
Expand All @@ -88,6 +89,7 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition )
mMapCanvas( 0 ), mDrawCanvasItems( true )
{
mOverviewFrameMapSymbol = 0;
mGridLineSymbol = 0;
createDefaultOverviewFrameSymbol();

//Offset
Expand All @@ -103,14 +105,14 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition )
mCurrentRectangle = rect();

setToolTip( tr( "Map %1" ).arg( mId ) );
mGridPen.setCapStyle( Qt::FlatCap );

initGridAnnotationFormatFromProject();
}

QgsComposerMap::~QgsComposerMap()
{
delete mOverviewFrameMapSymbol;
delete mGridLineSymbol;
}

void QgsComposerMap::draw( QPainter *painter, const QgsRectangle& extent, const QSize& size, int dpi )
Expand Down Expand Up @@ -702,13 +704,11 @@ bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const
gridElem.setAttribute( "intervalY", QString::number( mGridIntervalY ) );
gridElem.setAttribute( "offsetX", QString::number( mGridOffsetX ) );
gridElem.setAttribute( "offsetY", QString::number( mGridOffsetY ) );
gridElem.setAttribute( "penWidth", QString::number( mGridPen.widthF() ) );
gridElem.setAttribute( "penColorRed", mGridPen.color().red() );
gridElem.setAttribute( "penColorGreen", mGridPen.color().green() );
gridElem.setAttribute( "penColorBlue", mGridPen.color().blue() );
gridElem.setAttribute( "crossLength", QString::number( mCrossLength ) );
gridElem.setAttribute( "gridFrameStyle", mGridFrameStyle );
gridElem.setAttribute( "gridFrameWidth", QString::number( mGridFrameWidth ) );
QDomElement gridLineStyleElem = QgsSymbolLayerV2Utils::saveSymbol( QString(), mGridLineSymbol, doc );
gridElem.appendChild( gridLineStyleElem );

//grid annotation
QDomElement annotationElem = doc.createElement( "Annotation" );
Expand Down Expand Up @@ -838,14 +838,26 @@ bool QgsComposerMap::readXML( const QDomElement& itemElem, const QDomDocument& d
mGridIntervalY = gridElem.attribute( "intervalY", "0" ).toDouble();
mGridOffsetX = gridElem.attribute( "offsetX", "0" ).toDouble();
mGridOffsetY = gridElem.attribute( "offsetY", "0" ).toDouble();
mGridPen.setWidthF( gridElem.attribute( "penWidth", "0" ).toDouble() );
mGridPen.setColor( QColor( gridElem.attribute( "penColorRed", "0" ).toInt(),
gridElem.attribute( "penColorGreen", "0" ).toInt(),
gridElem.attribute( "penColorBlue", "0" ).toInt() ) );
mCrossLength = gridElem.attribute( "crossLength", "3" ).toDouble();
mGridFrameStyle = ( QgsComposerMap::GridFrameStyle )gridElem.attribute( "gridFrameStyle", "0" ).toInt();
mGridFrameWidth = gridElem.attribute( "gridFrameWidth", "2.0" ).toDouble();

QDomElement gridSymbolElem = gridElem.firstChildElement( "symbol" );
delete mGridLineSymbol;
if ( gridSymbolElem.isNull( ) )
{
//old project file, read penWidth /penColorRed, penColorGreen, penColorBlue
mGridLineSymbol = QgsLineSymbolV2::createSimple( QgsStringMap() );
mGridLineSymbol->setWidth( gridElem.attribute( "penWidth", "0" ).toDouble() );
mGridLineSymbol->setColor( QColor( gridElem.attribute( "penColorRed", "0" ).toInt(),
gridElem.attribute( "penColorGreen", "0" ).toInt(),
gridElem.attribute( "penColorBlue", "0" ).toInt() ) );
}
else
{
mGridLineSymbol = dynamic_cast<QgsLineSymbolV2*>( QgsSymbolLayerV2Utils::loadSymbol( gridSymbolElem ) );
}

QDomNodeList annotationNodeList = gridElem.elementsByTagName( "Annotation" );
if ( annotationNodeList.size() > 0 )
{
Expand Down Expand Up @@ -916,8 +928,6 @@ void QgsComposerMap::syncLayerSet()

void QgsComposerMap::drawGrid( QPainter* p )
{
p->setPen( mGridPen );

QList< QPair< double, QLineF > > verticalLines;
yGridLines( verticalLines );
QList< QPair< double, QLineF > >::const_iterator vIt = verticalLines.constBegin();
Expand All @@ -933,12 +943,12 @@ void QgsComposerMap::drawGrid( QPainter* p )
{
for ( ; vIt != verticalLines.constEnd(); ++vIt )
{
p->drawLine( vIt->second );
drawGridLine( vIt->second, p );
}

for ( ; hIt != horizontalLines.constEnd(); ++hIt )
{
p->drawLine( hIt->second );
drawGridLine( hIt->second, p );
}
}
else //cross
Expand All @@ -948,7 +958,7 @@ void QgsComposerMap::drawGrid( QPainter* p )
{
//start mark
crossEnd1 = QgsSymbolLayerV2Utils::pointOnLineWithDistance( vIt->second.p1(), vIt->second.p2(), mCrossLength );
p->drawLine( vIt->second.p1(), crossEnd1 );
drawGridLine( QLineF( vIt->second.p1(), crossEnd1 ), p );

//test for intersection with every horizontal line
hIt = horizontalLines.constBegin();
Expand All @@ -958,20 +968,20 @@ void QgsComposerMap::drawGrid( QPainter* p )
{
crossEnd1 = QgsSymbolLayerV2Utils::pointOnLineWithDistance( intersectionPoint, vIt->second.p1(), mCrossLength );
crossEnd2 = QgsSymbolLayerV2Utils::pointOnLineWithDistance( intersectionPoint, vIt->second.p2(), mCrossLength );
p->drawLine( crossEnd1, crossEnd2 );
drawGridLine( QLineF( crossEnd1, crossEnd2 ), p );
}
}
//end mark
QPointF crossEnd2 = QgsSymbolLayerV2Utils::pointOnLineWithDistance( vIt->second.p2(), vIt->second.p1(), mCrossLength );
p->drawLine( vIt->second.p2(), crossEnd2 );
drawGridLine( QLineF( vIt->second.p2(), crossEnd2 ), p );
}

hIt = horizontalLines.constBegin();
for ( ; hIt != horizontalLines.constEnd(); ++hIt )
{
//start mark
crossEnd1 = QgsSymbolLayerV2Utils::pointOnLineWithDistance( hIt->second.p1(), hIt->second.p2(), mCrossLength );
p->drawLine( hIt->second.p1(), crossEnd1 );
drawGridLine( QLineF( hIt->second.p1(), crossEnd1 ), p );

vIt = verticalLines.constBegin();
for ( ; vIt != verticalLines.constEnd(); ++vIt )
Expand All @@ -980,15 +990,13 @@ void QgsComposerMap::drawGrid( QPainter* p )
{
crossEnd1 = QgsSymbolLayerV2Utils::pointOnLineWithDistance( intersectionPoint, hIt->second.p1(), mCrossLength );
crossEnd2 = QgsSymbolLayerV2Utils::pointOnLineWithDistance( intersectionPoint, hIt->second.p2(), mCrossLength );
p->drawLine( crossEnd1, crossEnd2 );
drawGridLine( QLineF( crossEnd1, crossEnd2 ), p );
}
}
//end mark
crossEnd1 = QgsSymbolLayerV2Utils::pointOnLineWithDistance( hIt->second.p2(), hIt->second.p1(), mCrossLength );
p->drawLine( hIt->second.p2(), crossEnd1 );
drawGridLine( QLineF( hIt->second.p2(), crossEnd1 ), p );
}


}

p->setClipRect( thisPaintRect , Qt::NoClip );
Expand Down Expand Up @@ -1020,6 +1028,33 @@ void QgsComposerMap::drawGridFrame( QPainter* p, const QList< QPair< double, QLi
drawGridFrameBorder( p, bottomGridFrame, QgsComposerMap::Bottom );
}

void QgsComposerMap::drawGridLine( const QLineF& line, QPainter* p )
{
if ( !mGridLineSymbol || !p )
{
return;
}

//setup render context
QgsRenderContext context;
context.setPainter( p );
if ( mPreviewMode == Rectangle )
{
return;
}
else
{
context.setScaleFactor( 1.0 );
context.setRasterScaleFactor( mComposition->printResolution() / 25.4 );
}

QPolygonF poly;
poly << line.p1() << line.p2();
mGridLineSymbol->startRender( context );
mGridLineSymbol->renderPolyline( poly, 0, context );
mGridLineSymbol->stopRender( context );
}

void QgsComposerMap::drawGridFrameBorder( QPainter* p, const QMap< double, double >& borderPos, Border border )
{
double currentCoord = - mGridFrameWidth;
Expand Down Expand Up @@ -1433,12 +1468,36 @@ int QgsComposerMap::yGridLines( QList< QPair< double, QLineF > >& lines ) const

void QgsComposerMap::setGridPenWidth( double w )
{
mGridPen.setWidthF( w );
if ( mGridLineSymbol )
{
mGridLineSymbol->setWidth( w );
}
}

void QgsComposerMap::setGridPenColor( const QColor& c )
{
mGridPen.setColor( c );
if ( mGridLineSymbol )
{
mGridLineSymbol->setColor( c );
}
}

void QgsComposerMap::setGridPen( const QPen& p )
{
setGridPenWidth( p.widthF() );
setGridPenColor( p.color() );
}

QPen QgsComposerMap::gridPen() const
{
QPen p;
if ( mGridLineSymbol )
{
p.setWidthF( mGridLineSymbol->width() );
p.setColor( mGridLineSymbol->color() );
p.setCapStyle( Qt::FlatCap );
}
return p;
}

QRectF QgsComposerMap::boundingRect() const
Expand Down Expand Up @@ -1627,6 +1686,12 @@ void QgsComposerMap::setOverviewFrameMapSymbol( QgsFillSymbolV2* symbol )
mOverviewFrameMapSymbol = symbol;
}

void QgsComposerMap::setGridLineSymbol( QgsLineSymbolV2* symbol )
{
delete mGridLineSymbol;
mGridLineSymbol = symbol;
}

void QgsComposerMap::transformShift( double& xShift, double& yShift ) const
{
double mmToMapUnits = 1.0 / mapUnitsToMM();
Expand Down Expand Up @@ -1989,6 +2054,16 @@ void QgsComposerMap::createDefaultOverviewFrameSymbol()
mOverviewFrameMapSymbol->setAlpha( 0.3 );
}

void QgsComposerMap::createDefaultGridLineSymbol()
{
delete mGridLineSymbol;
QgsStringMap properties;
properties.insert( "color", "0,0,0,255" );
properties.insert( "width", "0.3" );
properties.insert( "capstyle", "flat" );
mGridLineSymbol = QgsLineSymbolV2::createSimple( properties );
}

void QgsComposerMap::initGridAnnotationFormatFromProject()
{
QString format = QgsProject::instance()->readEntry( "PositionPrecision", "/DegreeFormat", "D" );
Expand Down

0 comments on commit 9342a9c

Please sign in to comment.