/
qgsmarkercatalogue.cpp
427 lines (364 loc) · 12.9 KB
/
qgsmarkercatalogue.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/***************************************************************************
qgsmarkercatalogue.cpp
-------------------
begin : March 2005
copyright : (C) 2005 by Radim Blazek
email : blazek@itc.it
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <cmath>
#include <assert.h>
#include <QPen>
#include <QBrush>
#include <QPainter>
#include <QImage>
#include <QString>
#include <QStringList>
#include <QRect>
#include <QPointF>
#include <QPolygonF>
#include <QDir>
#include <QPicture>
#include <QSvgRenderer>
#include "qgis.h"
#include "qgsapplication.h"
#include "qgsmarkercatalogue.h"
#include "qgslogger.h"
// MSVC compiler doesn't have defined M_PI in math.h
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define DEG2RAD(x) ((x)*M_PI/180)
//#define IMAGEDEBUG
QgsMarkerCatalogue *QgsMarkerCatalogue::mMarkerCatalogue = 0;
QgsMarkerCatalogue::QgsMarkerCatalogue()
{
refreshList();
}
void QgsMarkerCatalogue::refreshList()
{
// Init list
mList.clear();
// Hardcoded markers
mList.append( "hard:circle" );
mList.append( "hard:rectangle" );
mList.append( "hard:diamond" );
mList.append( "hard:pentagon" );
mList.append( "hard:cross" );
mList.append( "hard:cross2" );
mList.append( "hard:triangle" );
mList.append( "hard:equilateral_triangle" );
mList.append( "hard:star" );
mList.append( "hard:regular_star" );
mList.append( "hard:arrow" );
// SVG
QStringList svgPaths = QgsApplication::svgPaths();
for(int i=0; i<svgPaths.size(); i++)
{
// TODO recursive ?
QDir dir( svgPaths[i] );
QStringList dl = dir.entryList( QDir::Dirs );
for ( QStringList::iterator it = dl.begin(); it != dl.end(); ++it )
{
if ( *it == "." || *it == ".." ) continue;
QDir dir2( svgPaths[i] + *it );
QStringList dl2 = dir2.entryList( QStringList( "*.svg" ), QDir::Files );
for ( QStringList::iterator it2 = dl2.begin(); it2 != dl2.end(); ++it2 )
{
// TODO test if it is correct SVG
mList.append( "svg:" + svgPaths[i] + *it + "/" + *it2 );
}
}
}
emit markersRefreshed();
}
QStringList QgsMarkerCatalogue::list()
{
return mList;
}
QgsMarkerCatalogue::~QgsMarkerCatalogue()
{
}
QgsMarkerCatalogue *QgsMarkerCatalogue::instance()
{
if ( !QgsMarkerCatalogue::mMarkerCatalogue )
{
QgsMarkerCatalogue::mMarkerCatalogue = new QgsMarkerCatalogue();
}
return QgsMarkerCatalogue::mMarkerCatalogue;
}
QImage QgsMarkerCatalogue::imageMarker( QString fullName, double size, QPen pen, QBrush brush, double opacity )
{
//
// First prepare the paintdevice that the marker will be drawn onto
//
// Introduce a minimum size, we don't want it to disappear.
if ( size < 4 )
{
size = 4;
}
QImage myImage;
int imageSize;
if ( fullName.startsWith( "hard:" ) )
{
int pw = (( pen.width() == 0 ? 1 : pen.width() ) + 1 ) / 2 * 2; // make even (round up); handle cosmetic pen
imageSize = (( int ) size + pw ) / 2 * 2 + 1; // make image width, height odd; account for pen width
myImage = QImage( imageSize, imageSize, QImage::Format_ARGB32_Premultiplied );
}
else
{
// TODO Change this logic so width is size and height is same
// proportion of scale factor as in oritignal SVG TS XXX
//QPixmap myPixmap = QPixmap(width,height);
imageSize = (( int ) size ) / 2 * 2 + 1; // make image width, height odd
myImage = QImage( imageSize, imageSize, QImage::Format_ARGB32_Premultiplied );
}
// starting with transparent QImage
myImage.fill( 0 );
QPainter myPainter;
myPainter.begin( &myImage );
myPainter.setRenderHint( QPainter::Antialiasing );
myPainter.setOpacity( opacity );
//
// Now pass the paintdevice along to have the marker rendered on it
//
if ( fullName.startsWith( "svg:" ) )
{
if ( svgMarker( &myPainter, fullName.mid( 4 ), size ) )
return myImage;
QgsDebugMsg( QString( "%1 not found - replacing with hard:circle" ).arg( fullName ) );
fullName = "hard:circle";
}
if ( fullName.startsWith( "font:" ) )
{
if ( fontMarker( &myPainter, fullName.mid( 5 ), size ) )
return myImage;
QgsDebugMsg( QString( "%1 not found - replacing with hard:circle" ).arg( fullName ) );
fullName = "hard:circle";
}
if ( fullName.endsWith( ".svg", Qt::CaseInsensitive ) )
{
if ( svgMarker( &myPainter, fullName, size ) )
return myImage;
QgsDebugMsg( QString( "%1 not found - replacing with hard:circle" ).arg( fullName ) );
fullName = "hard:circle";
}
if ( fullName.startsWith( "hard:" ) )
{
hardMarker( &myPainter, imageSize, fullName.mid( 5 ), size, pen, brush );
#ifdef IMAGEDEBUG
QgsDebugMsg( "*** Saving hard marker to hardMarker.png ***" );
#ifdef QGISDEBUG
myImage.save( "hardMarker.png" );
#endif
#endif
return myImage;
}
return QImage(); // empty
}
QPicture QgsMarkerCatalogue::pictureMarker( QString fullName, double size, QPen pen, QBrush brush, double opacity )
{
//
// First prepare the paintdevice that the marker will be drawn onto
//
QPicture myPicture;
if ( fullName.left( 5 ) == "hard:" )
{
//Note teh +1 offset below is required because the
//otherwise the icons are getting clipped
myPicture = QPicture( size + 1 );
}
else
{
// TODO Change this logic so width is size and height is same
// proportion of scale factor as in oritignal SVG TS XXX
if ( size < 1 ) size = 1;
myPicture = QPicture( size );
}
QPainter myPainter( &myPicture );
myPainter.setRenderHint( QPainter::Antialiasing );
myPainter.setOpacity( opacity );
//
// Now pass the paintdevice along to have the marker rndered on it
//
if ( fullName.left( 4 ) == "svg:" )
{
if ( svgMarker( &myPainter, fullName.mid( 4 ), size ) )
return myPicture;
QgsDebugMsg( QString( "%1 not found - replacing with hard:circle" ).arg( fullName ) );
fullName = "hard:circle";
}
if ( fullName.left( 5 ) == "hard:" )
{
hardMarker( &myPainter, ( int ) size, fullName.mid( 5 ), size, pen, brush );
return myPicture;
}
return QPicture(); // empty
}
bool QgsMarkerCatalogue::fontMarker( QPainter *thepPainter, QString fullName, double scaleFactor )
{
QStringList args = fullName.split( "," );
if ( args.size() == 0 )
return false;
QChar c;
if ( args.size() > 0 )
{
if ( args[0] == "#" )
{
c = QChar( '#' );
}
else if ( args[0].startsWith( "#" ) )
{
c = QChar( args[0].mid( 1 ).toInt() );
}
else
{
c = args[0][0];
}
}
QString family = args.size() >= 2 ? args[1] : "Helvetica";
int weight = args.size() >= 3 ? args[2].toInt() : -1;
int italic = args.size() >= 4 ? args[3].toInt() != 0 : false;
thepPainter->setFont( QFont( family, scaleFactor, weight, italic ) );
thepPainter->drawText( 0, 0, c );
return true;
}
bool QgsMarkerCatalogue::svgMarker( QPainter * thepPainter, QString fileName, double scaleFactor )
{
QSvgRenderer mySVG;
if ( !mySVG.load( fileName ) )
return false;
mySVG.render( thepPainter );
return true;
}
void QgsMarkerCatalogue::hardMarker( QPainter * thepPainter, int imageSize, QString name, double s, QPen pen, QBrush brush )
{
// Size of polygon symbols is calculated so that the boundingbox is circumscribed
// around a circle with diameter mPointSize
#if 0
s = s - pen.widthF(); // to make the overall size of the symbol at the specified size
#else
// the size of the base symbol is at the specified size; the outline is applied additionally
#endif
// Circle radius, is used for other figures also, when compensating for line
// width is necessary.
double r = s / 2; // get half the size of the figure to be rendered (the radius)
QgsDebugMsg( QString( "Hard marker size %1" ).arg( s ) );
// Find out center coordinates of the QImage to draw on.
double x_c = ( double )( imageSize / 2 ) + 0.5; // add 1/2 pixel for proper rounding when the figure's coordinates are added
double y_c = x_c; // is square image
thepPainter->setPen( pen );
thepPainter->setBrush( brush );
QgsDebugMsg( QString( "Hard marker radius %1" ).arg( r ) );
// If radius is 0, draw a circle, so it wont disappear.
if ( name == "circle" || r < 1 )
{
// "A stroked ellipse has a size of rectangle.size() plus the pen width."
// (from Qt doc)
thepPainter->drawEllipse( QRectF( x_c - r, y_c - r, s, s ) ); // x,y,w,h
}
else if ( name == "rectangle" )
{
thepPainter->drawRect( QRectF( x_c - r, y_c - r, s, s ) ); // x,y,w,h
}
else if ( name == "diamond" )
{
QPolygonF pa;
pa << QPointF( x_c - r, y_c )
<< QPointF( x_c, y_c + r )
<< QPointF( x_c + r, y_c )
<< QPointF( x_c, y_c - r );
thepPainter->drawPolygon( pa );
}
else if ( name == "pentagon" )
{
QPolygonF pa;
pa << QPointF( x_c + ( r * sin( DEG2RAD( 288.0 ) ) ), y_c - ( r * cos( DEG2RAD( 288.0 ) ) ) )
<< QPointF( x_c + ( r * sin( DEG2RAD( 216.0 ) ) ), y_c - ( r * cos( DEG2RAD( 216.0 ) ) ) )
<< QPointF( x_c + ( r * sin( DEG2RAD( 144.0 ) ) ), y_c - ( r * cos( DEG2RAD( 144.0 ) ) ) )
<< QPointF( x_c + ( r * sin( DEG2RAD( 72.0 ) ) ), y_c - ( r * cos( DEG2RAD( 72.0 ) ) ) )
<< QPointF( x_c, y_c - r );
thepPainter->drawPolygon( pa );
}
else if ( name == "cross" )
{
thepPainter->drawLine( QPointF( x_c - r, y_c ), QPointF( x_c + r, y_c ) ); // horizontal
thepPainter->drawLine( QPointF( x_c, y_c - r ), QPointF( x_c, y_c + r ) ); // vertical
}
else if ( name == "cross2" )
{
thepPainter->drawLine( QPointF( x_c - r, y_c - r ), QPointF( x_c + r, y_c + r ) );
thepPainter->drawLine( QPointF( x_c - r, y_c + r ), QPointF( x_c + r, y_c - r ) );
}
else if ( name == "triangle" )
{
QPolygonF pa;
pa << QPointF( x_c - r, y_c + r )
<< QPointF( x_c + r, y_c + r )
<< QPointF( x_c, y_c - r );
thepPainter->drawPolygon( pa );
}
else if ( name == "equilateral_triangle" )
{
QPolygonF pa;
pa << QPointF( x_c + ( r * sin( DEG2RAD( 240.0 ) ) ), y_c - ( r * cos( DEG2RAD( 240.0 ) ) ) )
<< QPointF( x_c + ( r * sin( DEG2RAD( 120.0 ) ) ), y_c - ( r * cos( DEG2RAD( 120.0 ) ) ) )
<< QPointF( x_c, y_c - r ); // 0
thepPainter->drawPolygon( pa );
}
else if ( name == "star" )
{
double oneSixth = 2 * r / 6;
QPolygonF pa;
pa << QPointF( x_c, y_c - r )
<< QPointF( x_c - oneSixth, y_c - oneSixth )
<< QPointF( x_c - r, y_c - oneSixth )
<< QPointF( x_c - oneSixth, y_c )
<< QPointF( x_c - r, y_c + r )
<< QPointF( x_c, y_c + oneSixth )
<< QPointF( x_c + r, y_c + r )
<< QPointF( x_c + oneSixth, y_c )
<< QPointF( x_c + r, y_c - oneSixth )
<< QPointF( x_c + oneSixth, y_c - oneSixth );
thepPainter->drawPolygon( pa );
}
else if ( name == "regular_star" )
{
// control the 'fatness' of the star: cos(72)/cos(36) gives the classic star shape
double inner_r = r * cos( DEG2RAD( 72.0 ) ) / cos( DEG2RAD( 36.0 ) );
QPolygonF pa;
pa << QPointF( x_c + ( inner_r * sin( DEG2RAD( 324.0 ) ) ), y_c - ( inner_r * cos( DEG2RAD( 324.0 ) ) ) ) // 324
<< QPointF( x_c + ( r * sin( DEG2RAD( 288.0 ) ) ), y_c - ( r * cos( DEG2RAD( 288 ) ) ) ) // 288
<< QPointF( x_c + ( inner_r * sin( DEG2RAD( 252.0 ) ) ), y_c - ( inner_r * cos( DEG2RAD( 252.0 ) ) ) ) // 252
<< QPointF( x_c + ( r * sin( DEG2RAD( 216.0 ) ) ), y_c - ( r * cos( DEG2RAD( 216.0 ) ) ) ) // 216
<< QPointF( x_c, y_c + ( inner_r ) ) // 180
<< QPointF( x_c + ( r * sin( DEG2RAD( 144.0 ) ) ), y_c - ( r * cos( DEG2RAD( 144.0 ) ) ) ) // 144
<< QPointF( x_c + ( inner_r * sin( DEG2RAD( 108.0 ) ) ), y_c - ( inner_r * cos( DEG2RAD( 108.0 ) ) ) ) // 108
<< QPointF( x_c + ( r * sin( DEG2RAD( 72.0 ) ) ), y_c - ( r * cos( DEG2RAD( 72.0 ) ) ) ) // 72
<< QPointF( x_c + ( inner_r * sin( DEG2RAD( 36.0 ) ) ), y_c - ( inner_r * cos( DEG2RAD( 36.0 ) ) ) ) // 36
<< QPointF( x_c, y_c - r ); // 0
thepPainter->drawPolygon( pa );
}
else if ( name == "arrow" )
{
double oneEight = r / 4;
double quarter = r / 2;
QPolygonF pa;
pa << QPointF( x_c, y_c - r )
<< QPointF( x_c + quarter, y_c - quarter )
<< QPointF( x_c + oneEight, y_c - quarter )
<< QPointF( x_c + oneEight, y_c + r )
<< QPointF( x_c - oneEight, y_c + r )
<< QPointF( x_c - oneEight, y_c - quarter )
<< QPointF( x_c - quarter, y_c - quarter );
thepPainter->drawPolygon( pa );
}
thepPainter->end();
}