Skip to content

Commit 9342a9c

Browse files
committed
[FEATURE]: use a custom linestyle for composer grid
1 parent 727d66a commit 9342a9c

File tree

5 files changed

+150
-101
lines changed

5 files changed

+150
-101
lines changed

src/app/composer/qgscomposermapwidget.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ QgsComposerMapWidget::QgsComposerMapWidget( QgsComposerMap* composerMap ): QWidg
8181
}
8282

8383
updateOverviewSymbolMarker();
84+
updateLineSymbolMarker();
8485

8586
updateGuiElements();
8687
blockAllSignals( false );
@@ -412,10 +413,6 @@ void QgsComposerMapWidget::updateGuiElements()
412413

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

415-
QPen gridPen = mComposerMap->gridPen();
416-
mLineWidthSpinBox->setValue( gridPen.widthF() );
417-
mLineColorButton->setColor( gridPen.color() );
418-
419416
blockAllSignals( false );
420417
}
421418
}
@@ -468,8 +465,7 @@ void QgsComposerMapWidget::blockAllSignals( bool b )
468465
mKeepLayerListCheckBox->blockSignals( b );
469466
mSetToMapCanvasExtentButton->blockSignals( b );
470467
mUpdatePreviewButton->blockSignals( b );
471-
mLineWidthSpinBox->blockSignals( b );
472-
mLineColorButton->blockSignals( b );
468+
mGridLineStyleButton->blockSignals( b );
473469
mDrawAnnotationCheckBox->blockSignals( b );
474470
mAnnotationFontButton->blockSignals( b );
475471
mAnnotationFormatComboBox->blockSignals( b );
@@ -696,20 +692,19 @@ void QgsComposerMapWidget::on_mLineWidthSpinBox_valueChanged( double d )
696692
mComposerMap->endCommand();
697693
}
698694

699-
void QgsComposerMapWidget::on_mLineColorButton_clicked()
695+
void QgsComposerMapWidget::on_mGridLineStyleButton_clicked()
700696
{
701697
if ( !mComposerMap )
702698
{
703699
return;
704700
}
705-
QColor newColor = QColorDialog::getColor( mLineColorButton->color() );
706-
if ( newColor.isValid() )
701+
702+
QgsSymbolV2SelectorDialog d( mComposerMap->gridLineSymbol(), QgsStyleV2::defaultStyle(), 0 );
703+
if ( d.exec() == QDialog::Accepted )
707704
{
708-
mComposerMap->beginCommand( tr( "Grid pen changed" ) );
709-
mLineColorButton->setColor( newColor );
710-
mComposerMap->setGridPenColor( newColor );
711-
mComposerMap->endCommand();
705+
updateLineSymbolMarker();
712706
}
707+
713708
mComposerMap->update();
714709
}
715710

@@ -1022,6 +1017,15 @@ void QgsComposerMapWidget::updateOverviewSymbolMarker()
10221017
}
10231018
}
10241019

1020+
void QgsComposerMapWidget::updateLineSymbolMarker()
1021+
{
1022+
if ( mComposerMap )
1023+
{
1024+
QIcon icon = QgsSymbolLayerV2Utils::symbolPreviewIcon( mComposerMap->gridLineSymbol(), mGridLineStyleButton->iconSize() );
1025+
mGridLineStyleButton->setIcon( icon );
1026+
}
1027+
}
1028+
10251029
void QgsComposerMapWidget::refreshMapComboBox()
10261030
{
10271031
if ( !mComposerMap )

src/app/composer/qgscomposermapwidget.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class QgsComposerMapWidget: public QWidget, private Ui::QgsComposerMapWidgetBase
5858
void on_mOffsetXSpinBox_editingFinished();
5959
void on_mOffsetYSpinBox_editingFinished();
6060
void on_mLineWidthSpinBox_valueChanged( double d );
61-
void on_mLineColorButton_clicked();
61+
void on_mGridLineStyleButton_clicked();
6262
void on_mGridTypeComboBox_currentIndexChanged( const QString& text );
6363
void on_mCrossWidthSpinBox_valueChanged( double d );
6464
void on_mAnnotationFontButton_clicked();
@@ -116,6 +116,7 @@ class QgsComposerMapWidget: public QWidget, private Ui::QgsComposerMapWidgetBase
116116
void initAnnotationDirectionBox( QComboBox* c, QgsComposerMap::GridAnnotationDirection dir );
117117

118118
void updateOverviewSymbolMarker();
119+
void updateLineSymbolMarker();
119120

120121
/**Updates the map combo box with the current composer map ids*/
121122
void refreshMapComboBox();

src/core/composer/qgscomposermap.cpp

Lines changed: 99 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
4848
{
4949
mComposition = composition;
5050
mOverviewFrameMapSymbol = 0;
51+
mGridLineSymbol = 0;
5152
createDefaultOverviewFrameSymbol();
53+
createDefaultGridLineSymbol();
5254

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

7980
initGridAnnotationFormatFromProject();
8081
}
@@ -88,6 +89,7 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition )
8889
mMapCanvas( 0 ), mDrawCanvasItems( true )
8990
{
9091
mOverviewFrameMapSymbol = 0;
92+
mGridLineSymbol = 0;
9193
createDefaultOverviewFrameSymbol();
9294

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

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

108109
initGridAnnotationFormatFromProject();
109110
}
110111

111112
QgsComposerMap::~QgsComposerMap()
112113
{
113114
delete mOverviewFrameMapSymbol;
115+
delete mGridLineSymbol;
114116
}
115117

116118
void QgsComposerMap::draw( QPainter *painter, const QgsRectangle& extent, const QSize& size, int dpi )
@@ -702,13 +704,11 @@ bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const
702704
gridElem.setAttribute( "intervalY", QString::number( mGridIntervalY ) );
703705
gridElem.setAttribute( "offsetX", QString::number( mGridOffsetX ) );
704706
gridElem.setAttribute( "offsetY", QString::number( mGridOffsetY ) );
705-
gridElem.setAttribute( "penWidth", QString::number( mGridPen.widthF() ) );
706-
gridElem.setAttribute( "penColorRed", mGridPen.color().red() );
707-
gridElem.setAttribute( "penColorGreen", mGridPen.color().green() );
708-
gridElem.setAttribute( "penColorBlue", mGridPen.color().blue() );
709707
gridElem.setAttribute( "crossLength", QString::number( mCrossLength ) );
710708
gridElem.setAttribute( "gridFrameStyle", mGridFrameStyle );
711709
gridElem.setAttribute( "gridFrameWidth", QString::number( mGridFrameWidth ) );
710+
QDomElement gridLineStyleElem = QgsSymbolLayerV2Utils::saveSymbol( QString(), mGridLineSymbol, doc );
711+
gridElem.appendChild( gridLineStyleElem );
712712

713713
//grid annotation
714714
QDomElement annotationElem = doc.createElement( "Annotation" );
@@ -838,14 +838,26 @@ bool QgsComposerMap::readXML( const QDomElement& itemElem, const QDomDocument& d
838838
mGridIntervalY = gridElem.attribute( "intervalY", "0" ).toDouble();
839839
mGridOffsetX = gridElem.attribute( "offsetX", "0" ).toDouble();
840840
mGridOffsetY = gridElem.attribute( "offsetY", "0" ).toDouble();
841-
mGridPen.setWidthF( gridElem.attribute( "penWidth", "0" ).toDouble() );
842-
mGridPen.setColor( QColor( gridElem.attribute( "penColorRed", "0" ).toInt(),
843-
gridElem.attribute( "penColorGreen", "0" ).toInt(),
844-
gridElem.attribute( "penColorBlue", "0" ).toInt() ) );
845841
mCrossLength = gridElem.attribute( "crossLength", "3" ).toDouble();
846842
mGridFrameStyle = ( QgsComposerMap::GridFrameStyle )gridElem.attribute( "gridFrameStyle", "0" ).toInt();
847843
mGridFrameWidth = gridElem.attribute( "gridFrameWidth", "2.0" ).toDouble();
848844

845+
QDomElement gridSymbolElem = gridElem.firstChildElement( "symbol" );
846+
delete mGridLineSymbol;
847+
if ( gridSymbolElem.isNull( ) )
848+
{
849+
//old project file, read penWidth /penColorRed, penColorGreen, penColorBlue
850+
mGridLineSymbol = QgsLineSymbolV2::createSimple( QgsStringMap() );
851+
mGridLineSymbol->setWidth( gridElem.attribute( "penWidth", "0" ).toDouble() );
852+
mGridLineSymbol->setColor( QColor( gridElem.attribute( "penColorRed", "0" ).toInt(),
853+
gridElem.attribute( "penColorGreen", "0" ).toInt(),
854+
gridElem.attribute( "penColorBlue", "0" ).toInt() ) );
855+
}
856+
else
857+
{
858+
mGridLineSymbol = dynamic_cast<QgsLineSymbolV2*>( QgsSymbolLayerV2Utils::loadSymbol( gridSymbolElem ) );
859+
}
860+
849861
QDomNodeList annotationNodeList = gridElem.elementsByTagName( "Annotation" );
850862
if ( annotationNodeList.size() > 0 )
851863
{
@@ -916,8 +928,6 @@ void QgsComposerMap::syncLayerSet()
916928

917929
void QgsComposerMap::drawGrid( QPainter* p )
918930
{
919-
p->setPen( mGridPen );
920-
921931
QList< QPair< double, QLineF > > verticalLines;
922932
yGridLines( verticalLines );
923933
QList< QPair< double, QLineF > >::const_iterator vIt = verticalLines.constBegin();
@@ -933,12 +943,12 @@ void QgsComposerMap::drawGrid( QPainter* p )
933943
{
934944
for ( ; vIt != verticalLines.constEnd(); ++vIt )
935945
{
936-
p->drawLine( vIt->second );
946+
drawGridLine( vIt->second, p );
937947
}
938948

939949
for ( ; hIt != horizontalLines.constEnd(); ++hIt )
940950
{
941-
p->drawLine( hIt->second );
951+
drawGridLine( hIt->second, p );
942952
}
943953
}
944954
else //cross
@@ -948,7 +958,7 @@ void QgsComposerMap::drawGrid( QPainter* p )
948958
{
949959
//start mark
950960
crossEnd1 = QgsSymbolLayerV2Utils::pointOnLineWithDistance( vIt->second.p1(), vIt->second.p2(), mCrossLength );
951-
p->drawLine( vIt->second.p1(), crossEnd1 );
961+
drawGridLine( QLineF( vIt->second.p1(), crossEnd1 ), p );
952962

953963
//test for intersection with every horizontal line
954964
hIt = horizontalLines.constBegin();
@@ -958,20 +968,20 @@ void QgsComposerMap::drawGrid( QPainter* p )
958968
{
959969
crossEnd1 = QgsSymbolLayerV2Utils::pointOnLineWithDistance( intersectionPoint, vIt->second.p1(), mCrossLength );
960970
crossEnd2 = QgsSymbolLayerV2Utils::pointOnLineWithDistance( intersectionPoint, vIt->second.p2(), mCrossLength );
961-
p->drawLine( crossEnd1, crossEnd2 );
971+
drawGridLine( QLineF( crossEnd1, crossEnd2 ), p );
962972
}
963973
}
964974
//end mark
965975
QPointF crossEnd2 = QgsSymbolLayerV2Utils::pointOnLineWithDistance( vIt->second.p2(), vIt->second.p1(), mCrossLength );
966-
p->drawLine( vIt->second.p2(), crossEnd2 );
976+
drawGridLine( QLineF( vIt->second.p2(), crossEnd2 ), p );
967977
}
968978

969979
hIt = horizontalLines.constBegin();
970980
for ( ; hIt != horizontalLines.constEnd(); ++hIt )
971981
{
972982
//start mark
973983
crossEnd1 = QgsSymbolLayerV2Utils::pointOnLineWithDistance( hIt->second.p1(), hIt->second.p2(), mCrossLength );
974-
p->drawLine( hIt->second.p1(), crossEnd1 );
984+
drawGridLine( QLineF( hIt->second.p1(), crossEnd1 ), p );
975985

976986
vIt = verticalLines.constBegin();
977987
for ( ; vIt != verticalLines.constEnd(); ++vIt )
@@ -980,15 +990,13 @@ void QgsComposerMap::drawGrid( QPainter* p )
980990
{
981991
crossEnd1 = QgsSymbolLayerV2Utils::pointOnLineWithDistance( intersectionPoint, hIt->second.p1(), mCrossLength );
982992
crossEnd2 = QgsSymbolLayerV2Utils::pointOnLineWithDistance( intersectionPoint, hIt->second.p2(), mCrossLength );
983-
p->drawLine( crossEnd1, crossEnd2 );
993+
drawGridLine( QLineF( crossEnd1, crossEnd2 ), p );
984994
}
985995
}
986996
//end mark
987997
crossEnd1 = QgsSymbolLayerV2Utils::pointOnLineWithDistance( hIt->second.p2(), hIt->second.p1(), mCrossLength );
988-
p->drawLine( hIt->second.p2(), crossEnd1 );
998+
drawGridLine( QLineF( hIt->second.p2(), crossEnd1 ), p );
989999
}
990-
991-
9921000
}
9931001

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

1031+
void QgsComposerMap::drawGridLine( const QLineF& line, QPainter* p )
1032+
{
1033+
if ( !mGridLineSymbol || !p )
1034+
{
1035+
return;
1036+
}
1037+
1038+
//setup render context
1039+
QgsRenderContext context;
1040+
context.setPainter( p );
1041+
if ( mPreviewMode == Rectangle )
1042+
{
1043+
return;
1044+
}
1045+
else
1046+
{
1047+
context.setScaleFactor( 1.0 );
1048+
context.setRasterScaleFactor( mComposition->printResolution() / 25.4 );
1049+
}
1050+
1051+
QPolygonF poly;
1052+
poly << line.p1() << line.p2();
1053+
mGridLineSymbol->startRender( context );
1054+
mGridLineSymbol->renderPolyline( poly, 0, context );
1055+
mGridLineSymbol->stopRender( context );
1056+
}
1057+
10231058
void QgsComposerMap::drawGridFrameBorder( QPainter* p, const QMap< double, double >& borderPos, Border border )
10241059
{
10251060
double currentCoord = - mGridFrameWidth;
@@ -1433,12 +1468,36 @@ int QgsComposerMap::yGridLines( QList< QPair< double, QLineF > >& lines ) const
14331468

14341469
void QgsComposerMap::setGridPenWidth( double w )
14351470
{
1436-
mGridPen.setWidthF( w );
1471+
if ( mGridLineSymbol )
1472+
{
1473+
mGridLineSymbol->setWidth( w );
1474+
}
14371475
}
14381476

14391477
void QgsComposerMap::setGridPenColor( const QColor& c )
14401478
{
1441-
mGridPen.setColor( c );
1479+
if ( mGridLineSymbol )
1480+
{
1481+
mGridLineSymbol->setColor( c );
1482+
}
1483+
}
1484+
1485+
void QgsComposerMap::setGridPen( const QPen& p )
1486+
{
1487+
setGridPenWidth( p.widthF() );
1488+
setGridPenColor( p.color() );
1489+
}
1490+
1491+
QPen QgsComposerMap::gridPen() const
1492+
{
1493+
QPen p;
1494+
if ( mGridLineSymbol )
1495+
{
1496+
p.setWidthF( mGridLineSymbol->width() );
1497+
p.setColor( mGridLineSymbol->color() );
1498+
p.setCapStyle( Qt::FlatCap );
1499+
}
1500+
return p;
14421501
}
14431502

14441503
QRectF QgsComposerMap::boundingRect() const
@@ -1627,6 +1686,12 @@ void QgsComposerMap::setOverviewFrameMapSymbol( QgsFillSymbolV2* symbol )
16271686
mOverviewFrameMapSymbol = symbol;
16281687
}
16291688

1689+
void QgsComposerMap::setGridLineSymbol( QgsLineSymbolV2* symbol )
1690+
{
1691+
delete mGridLineSymbol;
1692+
mGridLineSymbol = symbol;
1693+
}
1694+
16301695
void QgsComposerMap::transformShift( double& xShift, double& yShift ) const
16311696
{
16321697
double mmToMapUnits = 1.0 / mapUnitsToMM();
@@ -1989,6 +2054,16 @@ void QgsComposerMap::createDefaultOverviewFrameSymbol()
19892054
mOverviewFrameMapSymbol->setAlpha( 0.3 );
19902055
}
19912056

2057+
void QgsComposerMap::createDefaultGridLineSymbol()
2058+
{
2059+
delete mGridLineSymbol;
2060+
QgsStringMap properties;
2061+
properties.insert( "color", "0,0,0,255" );
2062+
properties.insert( "width", "0.3" );
2063+
properties.insert( "capstyle", "flat" );
2064+
mGridLineSymbol = QgsLineSymbolV2::createSimple( properties );
2065+
}
2066+
19922067
void QgsComposerMap::initGridAnnotationFormatFromProject()
19932068
{
19942069
QString format = QgsProject::instance()->readEntry( "PositionPrecision", "/DegreeFormat", "D" );

0 commit comments

Comments
 (0)