Skip to content

Commit 25ee41c

Browse files
committed
use symbologyv2 for lines and add marker symbols (in addition to Cross)
1 parent 0181324 commit 25ee41c

File tree

3 files changed

+163
-47
lines changed

3 files changed

+163
-47
lines changed

src/app/qgsdecorationgrid.cpp

+110-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "qgspoint.h"
2828
#include "qgsproject.h"
2929
#include "qgssymbollayerv2utils.h" //for pointOnLineWithDistance
30+
#include "qgssymbolv2.h" //for symbology
31+
#include "qgsrendercontext.h"
3032

3133
#include <QPainter>
3234
#include <QAction>
@@ -39,6 +41,7 @@
3941
#include <QMenu>
4042
#include <QFile>
4143
#include <QLocale>
44+
#include <QDomDocument>
4245

4346
//non qt includes
4447
#include <cmath>
@@ -55,12 +58,29 @@ QgsDecorationGrid::QgsDecorationGrid( QObject* parent )
5558
: QgsDecorationItem( parent )
5659
{
5760
setName( "Grid" );
61+
mLineSymbol = 0;
62+
mMarkerSymbol = 0;
5863
projectRead();
5964
}
6065

6166
QgsDecorationGrid::~QgsDecorationGrid()
6267
{
68+
if ( mLineSymbol )
69+
delete mLineSymbol;
70+
if ( mMarkerSymbol )
71+
delete mMarkerSymbol;
72+
}
6373

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;
6484
}
6585

6686
void QgsDecorationGrid::projectRead()
@@ -79,6 +99,34 @@ void QgsDecorationGrid::projectRead()
7999
mGridAnnotationFont.fromString( QgsProject::instance()->readEntry( mNameConfig, "/AnnotationFont", "" ) );
80100
mAnnotationFrameDistance = QgsProject::instance()->readDoubleEntry( mNameConfig, "/AnnotationFrameDistance", 0 );
81101
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+
82130
}
83131

84132
void QgsDecorationGrid::saveToProject()
@@ -97,6 +145,25 @@ void QgsDecorationGrid::saveToProject()
97145
QgsProject::instance()->writeEntry( mNameConfig, "/AnnotationFont", mGridAnnotationFont.toString() );
98146
QgsProject::instance()->writeEntry( mNameConfig, "/AnnotationFrameDistance", mAnnotationFrameDistance );
99147
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 &lt;
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+
100167
}
101168

102169

@@ -115,7 +182,7 @@ void QgsDecorationGrid::render( QPainter * p )
115182
if ( ! mEnabled )
116183
return;
117184

118-
p->setPen( mGridPen );
185+
// p->setPen( mGridPen );
119186

120187
QList< QPair< double, QLineF > > verticalLines;
121188
yGridLines( verticalLines, p );
@@ -130,17 +197,34 @@ void QgsDecorationGrid::render( QPainter * p )
130197
//simpler approach: draw vertical lines first, then horizontal ones
131198
if ( mGridStyle == QgsDecorationGrid::Solid )
132199
{
200+
if ( ! mLineSymbol )
201+
return;
202+
203+
QgsRenderContext context;
204+
context.setPainter( p );
205+
mLineSymbol->startRender( context, 0 );
206+
133207
for ( ; vIt != verticalLines.constEnd(); ++vIt )
134208
{
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 );
136214
}
137215

138216
for ( ; hIt != horizontalLines.constEnd(); ++hIt )
139217
{
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 );
141223
}
224+
225+
mLineSymbol->stopRender( context );
142226
}
143-
else //cross
227+
else if ( mGridStyle == QgsDecorationGrid::Cross )
144228
{
145229
QPointF intersectionPoint, crossEnd1, crossEnd2;
146230
for ( ; vIt != verticalLines.constEnd(); ++vIt )
@@ -186,8 +270,30 @@ void QgsDecorationGrid::render( QPainter * p )
186270
crossEnd1 = QgsSymbolLayerV2Utils::pointOnLineWithDistance( hIt->second.p2(), hIt->second.p1(), mCrossLength );
187271
p->drawLine( hIt->second.p2(), crossEnd1 );
188272
}
273+
}
274+
else //marker
275+
{
276+
if ( ! mMarkerSymbol )
277+
return;
189278

279+
QgsRenderContext context;
280+
context.setPainter( p );
281+
mMarkerSymbol->startRender( context, 0 );
190282

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 );
191297
}
192298

193299
// p->setClipRect( thisPaintRect , Qt::NoClip );

src/app/qgsdecorationgrid.h

+15-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "qgsdecorationitem.h"
2222

2323
class QPainter;
24+
class QgsLineSymbolV2;
25+
class QgsMarkerSymbolV2;
2426

2527
#include <QColor>
2628
#include <QPen>
@@ -38,7 +40,8 @@ class QgsDecorationGrid: public QgsDecorationItem
3840
enum GridStyle
3941
{
4042
Solid = 0, //solid lines
41-
Cross //only draw line crossings
43+
Cross = 1, //only draw line crossings
44+
Marker = 2 //user-defined marker
4245
};
4346

4447
enum GridAnnotationPosition
@@ -126,6 +129,14 @@ class QgsDecorationGrid: public QgsDecorationItem
126129
void setCrossLength( double l ) {mCrossLength = l;}
127130
double crossLength() {return mCrossLength;}
128131

132+
/**Set symbol that is used to draw grid lines. Takes ownership*/
133+
void setLineSymbol( QgsLineSymbolV2* symbol );
134+
const QgsLineSymbolV2* lineSymbol() const { return mLineSymbol; }
135+
136+
/**Set symbol that is used to draw markers. Takes ownership*/
137+
void setMarkerSymbol( QgsMarkerSymbolV2* symbol );
138+
const QgsMarkerSymbolV2* markerSymbol() const { return mMarkerSymbol; }
139+
129140
public slots:
130141
//! set values on the gui when a project is read or the gui first loaded
131142
void projectRead();
@@ -175,6 +186,9 @@ class QgsDecorationGrid: public QgsDecorationItem
175186
/**The length of the cross sides for mGridStyle Cross*/
176187
double mCrossLength;
177188

189+
QgsLineSymbolV2* mLineSymbol;
190+
QgsMarkerSymbolV2* mMarkerSymbol;
191+
178192
/**Draw coordinates for mGridAnnotationType Coordinate
179193
@param p drawing painter
180194
@param hLines horizontal coordinate lines in item coordinates

src/ui/qgsdecorationgriddialog.ui

+38-42
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>511</width>
10-
<height>353</height>
9+
<width>531</width>
10+
<height>349</height>
1111
</rect>
1212
</property>
1313
<property name="windowTitle">
@@ -18,7 +18,7 @@
1818
<enum>QLayout::SetMinimumSize</enum>
1919
</property>
2020
<item>
21-
<layout class="QGridLayout" name="gridLayout_3">
21+
<layout class="QGridLayout" name="gridLayout_3" columnminimumwidth="125,0,0,0,0">
2222
<item row="1" column="0">
2323
<widget class="QLabel" name="mGridTypeLabel">
2424
<property name="accessibleName">
@@ -139,45 +139,15 @@
139139
</widget>
140140
</item>
141141
<item row="7" column="0">
142-
<widget class="QLabel" name="mLineWidthLabel">
142+
<widget class="QLabel" name="mLineSymbolLabel">
143143
<property name="text">
144-
<string>Line width</string>
144+
<string>Line symbol</string>
145145
</property>
146146
<property name="wordWrap">
147147
<bool>true</bool>
148148
</property>
149149
</widget>
150150
</item>
151-
<item row="7" column="2">
152-
<widget class="QDoubleSpinBox" name="mLineWidthSpinBox">
153-
<property name="decimals">
154-
<number>5</number>
155-
</property>
156-
</widget>
157-
</item>
158-
<item row="8" column="0">
159-
<widget class="QLabel" name="mLineColorLabel">
160-
<property name="text">
161-
<string>Line color</string>
162-
</property>
163-
<property name="wordWrap">
164-
<bool>true</bool>
165-
</property>
166-
</widget>
167-
</item>
168-
<item row="8" column="2">
169-
<widget class="QgsColorButton" name="mLineColorButton">
170-
<property name="sizePolicy">
171-
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
172-
<horstretch>0</horstretch>
173-
<verstretch>0</verstretch>
174-
</sizepolicy>
175-
</property>
176-
<property name="text">
177-
<string/>
178-
</property>
179-
</widget>
180-
</item>
181151
<item row="5" column="3">
182152
<spacer name="horizontalSpacer">
183153
<property name="orientation">
@@ -296,6 +266,39 @@
296266
</layout>
297267
</widget>
298268
</item>
269+
<item row="8" column="0">
270+
<widget class="QLabel" name="mMarkerSymbolLabel">
271+
<property name="text">
272+
<string>Marker symbol</string>
273+
</property>
274+
</widget>
275+
</item>
276+
<item row="7" column="2">
277+
<widget class="QPushButton" name="mLineSymbolButton">
278+
<property name="sizePolicy">
279+
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
280+
<horstretch>0</horstretch>
281+
<verstretch>0</verstretch>
282+
</sizepolicy>
283+
</property>
284+
<property name="text">
285+
<string/>
286+
</property>
287+
</widget>
288+
</item>
289+
<item row="8" column="2">
290+
<widget class="QPushButton" name="mMarkerSymbolButton">
291+
<property name="sizePolicy">
292+
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
293+
<horstretch>0</horstretch>
294+
<verstretch>0</verstretch>
295+
</sizepolicy>
296+
</property>
297+
<property name="text">
298+
<string/>
299+
</property>
300+
</widget>
301+
</item>
299302
</layout>
300303
</item>
301304
<item>
@@ -310,13 +313,6 @@
310313
</item>
311314
</layout>
312315
</widget>
313-
<customwidgets>
314-
<customwidget>
315-
<class>QgsColorButton</class>
316-
<extends>QToolButton</extends>
317-
<header>qgscolorbutton.h</header>
318-
</customwidget>
319-
</customwidgets>
320316
<resources/>
321317
<connections/>
322318
</ui>

0 commit comments

Comments
 (0)