27
27
#include " qgspoint.h"
28
28
#include " qgsproject.h"
29
29
#include " qgssymbollayerv2utils.h" // for pointOnLineWithDistance
30
+ #include " qgssymbolv2.h" // for symbology
31
+ #include " qgsrendercontext.h"
30
32
31
33
#include < QPainter>
32
34
#include < QAction>
39
41
#include < QMenu>
40
42
#include < QFile>
41
43
#include < QLocale>
44
+ #include < QDomDocument>
42
45
43
46
// non qt includes
44
47
#include < cmath>
@@ -55,12 +58,29 @@ QgsDecorationGrid::QgsDecorationGrid( QObject* parent )
55
58
: QgsDecorationItem( parent )
56
59
{
57
60
setName ( " Grid" );
61
+ mLineSymbol = 0 ;
62
+ mMarkerSymbol = 0 ;
58
63
projectRead ();
59
64
}
60
65
61
66
QgsDecorationGrid::~QgsDecorationGrid ()
62
67
{
68
+ if ( mLineSymbol )
69
+ delete mLineSymbol ;
70
+ if ( mMarkerSymbol )
71
+ delete mMarkerSymbol ;
72
+ }
63
73
74
+ void QgsDecorationGrid::setLineSymbol ( QgsLineSymbolV2* symbol )
75
+ {
76
+ delete mLineSymbol ;
77
+ mLineSymbol = symbol;
78
+ }
79
+
80
+ void QgsDecorationGrid::setMarkerSymbol ( QgsMarkerSymbolV2* symbol )
81
+ {
82
+ delete mMarkerSymbol ;
83
+ mMarkerSymbol = symbol;
64
84
}
65
85
66
86
void QgsDecorationGrid::projectRead ()
@@ -79,6 +99,34 @@ void QgsDecorationGrid::projectRead()
79
99
mGridAnnotationFont .fromString ( QgsProject::instance ()->readEntry ( mNameConfig , " /AnnotationFont" , " " ) );
80
100
mAnnotationFrameDistance = QgsProject::instance ()->readDoubleEntry ( mNameConfig , " /AnnotationFrameDistance" , 0 );
81
101
mGridAnnotationPrecision = QgsProject::instance ()->readNumEntry ( mNameConfig , " /AnnotationPrecision" , 3 );
102
+
103
+ // read symbol info from xml
104
+ QDomDocument doc;
105
+ QDomElement elem;
106
+ QString xml;
107
+ xml = QgsProject::instance ()->readEntry ( mNameConfig , " /LineSymbol" );
108
+ if ( xml != " " )
109
+ {
110
+ doc.setContent ( xml );
111
+ elem = doc.documentElement ();
112
+ if ( mLineSymbol )
113
+ delete mLineSymbol ;
114
+ mLineSymbol = dynamic_cast <QgsLineSymbolV2*>( QgsSymbolLayerV2Utils::loadSymbol ( elem ) );
115
+ if ( ! mLineSymbol )
116
+ mLineSymbol = new QgsLineSymbolV2 ();
117
+ }
118
+ xml = QgsProject::instance ()->readEntry ( mNameConfig , " /MarkerSymbol" );
119
+ if ( xml != " " )
120
+ {
121
+ doc.setContent ( xml );
122
+ elem = doc.documentElement ();
123
+ if ( mMarkerSymbol )
124
+ delete mMarkerSymbol ;
125
+ mMarkerSymbol = dynamic_cast <QgsMarkerSymbolV2*>( QgsSymbolLayerV2Utils::loadSymbol ( elem ) );
126
+ if ( ! mMarkerSymbol )
127
+ mMarkerSymbol = new QgsMarkerSymbolV2 ();
128
+ }
129
+
82
130
}
83
131
84
132
void QgsDecorationGrid::saveToProject ()
@@ -97,6 +145,25 @@ void QgsDecorationGrid::saveToProject()
97
145
QgsProject::instance ()->writeEntry ( mNameConfig , " /AnnotationFont" , mGridAnnotationFont .toString () );
98
146
QgsProject::instance ()->writeEntry ( mNameConfig , " /AnnotationFrameDistance" , mAnnotationFrameDistance );
99
147
QgsProject::instance ()->writeEntry ( mNameConfig , " /AnnotationPrecision" , mGridAnnotationPrecision );
148
+
149
+ // write symbol info to xml
150
+ QDomDocument doc;
151
+ QDomElement elem;
152
+ if ( mLineSymbol )
153
+ {
154
+ elem = QgsSymbolLayerV2Utils::saveSymbol ( " line symbol" , mLineSymbol , doc );
155
+ doc.appendChild ( elem );
156
+ // FIXME this works, but XML will not be valid as < is replaced by <
157
+ QgsProject::instance ()->writeEntry ( mNameConfig , " /LineSymbol" , doc.toString () );
158
+ }
159
+ if ( mLineSymbol )
160
+ {
161
+ doc.setContent ( QString ( " " ) );
162
+ elem = QgsSymbolLayerV2Utils::saveSymbol ( " marker symbol" , mMarkerSymbol , doc );
163
+ doc.appendChild ( elem );
164
+ QgsProject::instance ()->writeEntry ( mNameConfig , " /MarkerSymbol" , doc.toString () );
165
+ }
166
+
100
167
}
101
168
102
169
@@ -115,7 +182,7 @@ void QgsDecorationGrid::render( QPainter * p )
115
182
if ( ! mEnabled )
116
183
return ;
117
184
118
- p->setPen ( mGridPen );
185
+ // p->setPen( mGridPen );
119
186
120
187
QList< QPair< double , QLineF > > verticalLines;
121
188
yGridLines ( verticalLines, p );
@@ -130,17 +197,34 @@ void QgsDecorationGrid::render( QPainter * p )
130
197
// simpler approach: draw vertical lines first, then horizontal ones
131
198
if ( mGridStyle == QgsDecorationGrid::Solid )
132
199
{
200
+ if ( ! mLineSymbol )
201
+ return ;
202
+
203
+ QgsRenderContext context;
204
+ context.setPainter ( p );
205
+ mLineSymbol ->startRender ( context, 0 );
206
+
133
207
for ( ; vIt != verticalLines.constEnd (); ++vIt )
134
208
{
135
- p->drawLine ( vIt->second );
209
+ // p->drawLine( vIt->second );
210
+ // need to convert QLineF to QPolygonF ...
211
+ QVector<QPointF> poly;
212
+ poly << vIt->second .p1 () << vIt->second .p2 ();
213
+ mLineSymbol ->renderPolyline ( QPolygonF ( poly ), 0 , context );
136
214
}
137
215
138
216
for ( ; hIt != horizontalLines.constEnd (); ++hIt )
139
217
{
140
- p->drawLine ( hIt->second );
218
+ // p->drawLine( hIt->second );
219
+ // need to convert QLineF to QPolygonF ...
220
+ QVector<QPointF> poly;
221
+ poly << hIt->second .p1 () << hIt->second .p2 ();
222
+ mLineSymbol ->renderPolyline ( QPolygonF ( poly ), 0 , context );
141
223
}
224
+
225
+ mLineSymbol ->stopRender ( context );
142
226
}
143
- else // cross
227
+ else if ( mGridStyle == QgsDecorationGrid::Cross )
144
228
{
145
229
QPointF intersectionPoint, crossEnd1, crossEnd2;
146
230
for ( ; vIt != verticalLines.constEnd (); ++vIt )
@@ -186,8 +270,30 @@ void QgsDecorationGrid::render( QPainter * p )
186
270
crossEnd1 = QgsSymbolLayerV2Utils::pointOnLineWithDistance ( hIt->second .p2 (), hIt->second .p1 (), mCrossLength );
187
271
p->drawLine ( hIt->second .p2 (), crossEnd1 );
188
272
}
273
+ }
274
+ else // marker
275
+ {
276
+ if ( ! mMarkerSymbol )
277
+ return ;
189
278
279
+ QgsRenderContext context;
280
+ context.setPainter ( p );
281
+ mMarkerSymbol ->startRender ( context, 0 );
190
282
283
+ QPointF intersectionPoint, crossEnd1, crossEnd2;
284
+ for ( ; vIt != verticalLines.constEnd (); ++vIt )
285
+ {
286
+ // test for intersection with every horizontal line
287
+ hIt = horizontalLines.constBegin ();
288
+ for ( ; hIt != horizontalLines.constEnd (); ++hIt )
289
+ {
290
+ if ( hIt->second .intersect ( vIt->second , &intersectionPoint ) == QLineF::BoundedIntersection )
291
+ {
292
+ mMarkerSymbol ->renderPoint ( intersectionPoint, 0 , context );
293
+ }
294
+ }
295
+ }
296
+ mMarkerSymbol ->stopRender ( context );
191
297
}
192
298
193
299
// p->setClipRect( thisPaintRect , Qt::NoClip );
0 commit comments