@@ -48,7 +48,9 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
48
48
{
49
49
mComposition = composition;
50
50
mOverviewFrameMapSymbol = 0 ;
51
+ mGridLineSymbol = 0 ;
51
52
createDefaultOverviewFrameSymbol ();
53
+ createDefaultGridLineSymbol ();
52
54
53
55
mId = 0 ;
54
56
assignFreeId ();
@@ -74,7 +76,6 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
74
76
}
75
77
setSceneRect ( QRectF ( x, y, width, height ) );
76
78
setToolTip ( tr ( " Map %1" ).arg ( mId ) );
77
- mGridPen .setCapStyle ( Qt::FlatCap );
78
79
79
80
initGridAnnotationFormatFromProject ();
80
81
}
@@ -88,6 +89,7 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition )
88
89
mMapCanvas( 0 ), mDrawCanvasItems( true )
89
90
{
90
91
mOverviewFrameMapSymbol = 0 ;
92
+ mGridLineSymbol = 0 ;
91
93
createDefaultOverviewFrameSymbol ();
92
94
93
95
// Offset
@@ -103,14 +105,14 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition )
103
105
mCurrentRectangle = rect ();
104
106
105
107
setToolTip ( tr ( " Map %1" ).arg ( mId ) );
106
- mGridPen .setCapStyle ( Qt::FlatCap );
107
108
108
109
initGridAnnotationFormatFromProject ();
109
110
}
110
111
111
112
QgsComposerMap::~QgsComposerMap ()
112
113
{
113
114
delete mOverviewFrameMapSymbol ;
115
+ delete mGridLineSymbol ;
114
116
}
115
117
116
118
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
702
704
gridElem.setAttribute ( " intervalY" , QString::number ( mGridIntervalY ) );
703
705
gridElem.setAttribute ( " offsetX" , QString::number ( mGridOffsetX ) );
704
706
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 () );
709
707
gridElem.setAttribute ( " crossLength" , QString::number ( mCrossLength ) );
710
708
gridElem.setAttribute ( " gridFrameStyle" , mGridFrameStyle );
711
709
gridElem.setAttribute ( " gridFrameWidth" , QString::number ( mGridFrameWidth ) );
710
+ QDomElement gridLineStyleElem = QgsSymbolLayerV2Utils::saveSymbol ( QString (), mGridLineSymbol , doc );
711
+ gridElem.appendChild ( gridLineStyleElem );
712
712
713
713
// grid annotation
714
714
QDomElement annotationElem = doc.createElement ( " Annotation" );
@@ -838,14 +838,26 @@ bool QgsComposerMap::readXML( const QDomElement& itemElem, const QDomDocument& d
838
838
mGridIntervalY = gridElem.attribute ( " intervalY" , " 0" ).toDouble ();
839
839
mGridOffsetX = gridElem.attribute ( " offsetX" , " 0" ).toDouble ();
840
840
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 () ) );
845
841
mCrossLength = gridElem.attribute ( " crossLength" , " 3" ).toDouble ();
846
842
mGridFrameStyle = ( QgsComposerMap::GridFrameStyle )gridElem.attribute ( " gridFrameStyle" , " 0" ).toInt ();
847
843
mGridFrameWidth = gridElem.attribute ( " gridFrameWidth" , " 2.0" ).toDouble ();
848
844
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
+
849
861
QDomNodeList annotationNodeList = gridElem.elementsByTagName ( " Annotation" );
850
862
if ( annotationNodeList.size () > 0 )
851
863
{
@@ -916,8 +928,6 @@ void QgsComposerMap::syncLayerSet()
916
928
917
929
void QgsComposerMap::drawGrid ( QPainter* p )
918
930
{
919
- p->setPen ( mGridPen );
920
-
921
931
QList< QPair< double , QLineF > > verticalLines;
922
932
yGridLines ( verticalLines );
923
933
QList< QPair< double , QLineF > >::const_iterator vIt = verticalLines.constBegin ();
@@ -933,12 +943,12 @@ void QgsComposerMap::drawGrid( QPainter* p )
933
943
{
934
944
for ( ; vIt != verticalLines.constEnd (); ++vIt )
935
945
{
936
- p-> drawLine ( vIt->second );
946
+ drawGridLine ( vIt->second , p );
937
947
}
938
948
939
949
for ( ; hIt != horizontalLines.constEnd (); ++hIt )
940
950
{
941
- p-> drawLine ( hIt->second );
951
+ drawGridLine ( hIt->second , p );
942
952
}
943
953
}
944
954
else // cross
@@ -948,7 +958,7 @@ void QgsComposerMap::drawGrid( QPainter* p )
948
958
{
949
959
// start mark
950
960
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 );
952
962
953
963
// test for intersection with every horizontal line
954
964
hIt = horizontalLines.constBegin ();
@@ -958,20 +968,20 @@ void QgsComposerMap::drawGrid( QPainter* p )
958
968
{
959
969
crossEnd1 = QgsSymbolLayerV2Utils::pointOnLineWithDistance ( intersectionPoint, vIt->second .p1 (), mCrossLength );
960
970
crossEnd2 = QgsSymbolLayerV2Utils::pointOnLineWithDistance ( intersectionPoint, vIt->second .p2 (), mCrossLength );
961
- p-> drawLine ( crossEnd1, crossEnd2 );
971
+ drawGridLine ( QLineF ( crossEnd1, crossEnd2 ), p );
962
972
}
963
973
}
964
974
// end mark
965
975
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 );
967
977
}
968
978
969
979
hIt = horizontalLines.constBegin ();
970
980
for ( ; hIt != horizontalLines.constEnd (); ++hIt )
971
981
{
972
982
// start mark
973
983
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 );
975
985
976
986
vIt = verticalLines.constBegin ();
977
987
for ( ; vIt != verticalLines.constEnd (); ++vIt )
@@ -980,15 +990,13 @@ void QgsComposerMap::drawGrid( QPainter* p )
980
990
{
981
991
crossEnd1 = QgsSymbolLayerV2Utils::pointOnLineWithDistance ( intersectionPoint, hIt->second .p1 (), mCrossLength );
982
992
crossEnd2 = QgsSymbolLayerV2Utils::pointOnLineWithDistance ( intersectionPoint, hIt->second .p2 (), mCrossLength );
983
- p-> drawLine ( crossEnd1, crossEnd2 );
993
+ drawGridLine ( QLineF ( crossEnd1, crossEnd2 ), p );
984
994
}
985
995
}
986
996
// end mark
987
997
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 );
989
999
}
990
-
991
-
992
1000
}
993
1001
994
1002
p->setClipRect ( thisPaintRect , Qt::NoClip );
@@ -1020,6 +1028,33 @@ void QgsComposerMap::drawGridFrame( QPainter* p, const QList< QPair< double, QLi
1020
1028
drawGridFrameBorder ( p, bottomGridFrame, QgsComposerMap::Bottom );
1021
1029
}
1022
1030
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
+
1023
1058
void QgsComposerMap::drawGridFrameBorder ( QPainter* p, const QMap< double , double >& borderPos, Border border )
1024
1059
{
1025
1060
double currentCoord = - mGridFrameWidth ;
@@ -1433,12 +1468,36 @@ int QgsComposerMap::yGridLines( QList< QPair< double, QLineF > >& lines ) const
1433
1468
1434
1469
void QgsComposerMap::setGridPenWidth ( double w )
1435
1470
{
1436
- mGridPen .setWidthF ( w );
1471
+ if ( mGridLineSymbol )
1472
+ {
1473
+ mGridLineSymbol ->setWidth ( w );
1474
+ }
1437
1475
}
1438
1476
1439
1477
void QgsComposerMap::setGridPenColor ( const QColor& c )
1440
1478
{
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;
1442
1501
}
1443
1502
1444
1503
QRectF QgsComposerMap::boundingRect () const
@@ -1627,6 +1686,12 @@ void QgsComposerMap::setOverviewFrameMapSymbol( QgsFillSymbolV2* symbol )
1627
1686
mOverviewFrameMapSymbol = symbol;
1628
1687
}
1629
1688
1689
+ void QgsComposerMap::setGridLineSymbol ( QgsLineSymbolV2* symbol )
1690
+ {
1691
+ delete mGridLineSymbol ;
1692
+ mGridLineSymbol = symbol;
1693
+ }
1694
+
1630
1695
void QgsComposerMap::transformShift ( double & xShift, double & yShift ) const
1631
1696
{
1632
1697
double mmToMapUnits = 1.0 / mapUnitsToMM ();
@@ -1989,6 +2054,16 @@ void QgsComposerMap::createDefaultOverviewFrameSymbol()
1989
2054
mOverviewFrameMapSymbol ->setAlpha ( 0.3 );
1990
2055
}
1991
2056
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
+
1992
2067
void QgsComposerMap::initGridAnnotationFormatFromProject ()
1993
2068
{
1994
2069
QString format = QgsProject::instance ()->readEntry ( " PositionPrecision" , " /DegreeFormat" , " D" );
0 commit comments