/
qgssymbol.cpp
594 lines (505 loc) · 18.5 KB
/
qgssymbol.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/***************************************************************************
QgsSymbol.cpp - description
-------------------
begin : Sun Aug 11 2002
copyright : (C) 2002 by Gary E.Sherman
email : sherman at mrcc dot com
Romans 3:23=>Romans 6:23=>Romans 5:8=>Romans 10:9,10=>Romans 12
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
/* $Id$ */
#include <cmath>
#include <iostream>
#include "qgssymbol.h"
#include "qgslogger.h"
#include "qgssymbologyutils.h"
#include "qgsmarkercatalogue.h"
#include "qgsapplication.h"
#include <QPainter>
#include <QDomNode>
#include <QDomDocument>
#include <QImage>
#include <QDir>
#include <QFileInfo>
//#include <QString>
//do we have to include qstring?
QgsSymbol::QgsSymbol(QGis::VectorType t, QString lvalue, QString uvalue, QString label) :
mLowerValue(lvalue),
mUpperValue(uvalue),
mLabel(label),
mType(t),
mPointSymbolName( "hard:circle" ),
mPointSize( DEFAULT_POINT_SIZE ),
mPointSymbolImage(1,1, QImage::Format_ARGB32_Premultiplied),
mWidthScale(1.0),
mCacheUpToDate( false ),
mCacheUpToDate2( false ),
mRotationClassificationField(-1),
mScaleClassificationField(-1)
{}
QgsSymbol::QgsSymbol(QGis::VectorType t, QString lvalue, QString uvalue, QString label, QColor c) :
mLowerValue(lvalue),
mUpperValue(uvalue),
mLabel(label),
mType(t),
mPen( c ),
mBrush( c ),
mPointSymbolName( "hard:circle" ),
mPointSize( DEFAULT_POINT_SIZE ),
mPointSymbolImage(1,1, QImage::Format_ARGB32_Premultiplied),
mWidthScale(1.0),
mCacheUpToDate( false ),
mCacheUpToDate2( false ),
mRotationClassificationField(-1),
mScaleClassificationField(-1)
{}
QgsSymbol::QgsSymbol()
: mPointSymbolName( "hard:circle" ),
mPointSize( DEFAULT_POINT_SIZE ),
mPointSymbolImage(1,1, QImage::Format_ARGB32_Premultiplied),
mWidthScale(1.0),
mCacheUpToDate( false ),
mCacheUpToDate2( false ),
mRotationClassificationField(-1),
mScaleClassificationField(-1)
{}
QgsSymbol::QgsSymbol(QColor c)
: mPen( c ),
mBrush( c ),
mPointSymbolName( "hard:circle" ),
mPointSize( DEFAULT_POINT_SIZE ),
mPointSymbolImage(1,1, QImage::Format_ARGB32_Premultiplied),
mWidthScale(1.0),
mCacheUpToDate( false ),
mCacheUpToDate2( false ),
mRotationClassificationField(-1),
mScaleClassificationField(-1)
{}
QgsSymbol::QgsSymbol(const QgsSymbol& s)
{
if (this != &s)
{
mLowerValue = s.mLowerValue;
mUpperValue = s.mUpperValue;
mLabel = s.mLabel;
mType = s.mType;
mPen = s.mPen;
mBrush = s.mBrush;
mTextureFilePath = s.mTextureFilePath;
mPointSymbolName = s.mPointSymbolName;
mPointSize = s.mPointSize;
mPointSymbolImage = s.mPointSymbolImage;
mPointSymbolImageSelected = s.mPointSymbolImageSelected;
mWidthScale = s.mWidthScale;
mPointSymbolImage2 = s.mPointSymbolImage2;
mPointSymbolImageSelected2 = s.mPointSymbolImageSelected2;
mCacheUpToDate = s.mCacheUpToDate;
mCacheUpToDate2 = s.mCacheUpToDate2;
mSelectionColor = s.mSelectionColor;
mSelectionColor2 = s.mSelectionColor2;
mRotationClassificationField = s.mRotationClassificationField;
mScaleClassificationField = s.mScaleClassificationField;
}
}
QgsSymbol::~QgsSymbol()
{
}
QColor QgsSymbol::color() const
{
return mPen.color();
}
void QgsSymbol::setColor(QColor c)
{
mPen.setColor(c);
mCacheUpToDate = mCacheUpToDate2 = false;
}
QColor QgsSymbol::fillColor() const
{
return mBrush.color();
}
void QgsSymbol::setFillColor(QColor c)
{
mBrush.setColor(c);
mCacheUpToDate = mCacheUpToDate2 = false;
}
double QgsSymbol::lineWidth() const
{
return mPen.widthF();
}
void QgsSymbol::setLineWidth(double w)
{
mPen.setWidthF(w);
mCacheUpToDate = mCacheUpToDate2 = false;
}
void QgsSymbol::setLineStyle( Qt::PenStyle s )
{
mPen.setStyle(s);
mCacheUpToDate = mCacheUpToDate2 = false;
}
void QgsSymbol::setFillStyle( Qt::BrushStyle s )
{
mBrush.setStyle(s);
mCacheUpToDate = mCacheUpToDate2 = false;
}
QString QgsSymbol::customTexture() const
{
return mTextureFilePath;
}
void QgsSymbol::setCustomTexture( QString path )
{
mTextureFilePath = path;
mBrush.setTextureImage(QImage (path));
mCacheUpToDate = mCacheUpToDate2 = false;
}
//should we set the path independently of setting the texture?
void QgsSymbol::setNamedPointSymbol(QString name)
{
// do some sanity checking for svgs...
QString myTempName = name;
myTempName.replace("svg:","");
QFile myFile(myTempName);
if (!myFile.exists())
{
QgsDebugMsg("\n\n\n *** Svg Symbol not found on fs ***")
QgsDebugMsg("Name: " + name);
//see if we can resolve the problem...
//by using the qgis svg dir from this local machine
//one day when user specified svg are allowed we need
//to adjust this logic probably...
QString svgPath = QgsApplication::svgPath();
QgsDebugMsg( "SvgPath: " + svgPath);
QFileInfo myInfo(myTempName);
QString myFileName = myInfo.fileName(); // foo.svg
QString myLowestDir = myInfo.dir().dirName();
QString myLocalPath = svgPath + QDir::separator() +
myLowestDir + QDir::separator() +
myFileName;
QgsDebugMsg("Alternative svg path: " + myLocalPath);
if (QFile(myLocalPath).exists())
{
name="svg:"+myLocalPath;
QgsDebugMsg("Svg found in alternative path");
}
else
{
//couldnt find the file, no happy ending :-(
QgsDebugMsg("Computed alternate path but no svg there either");
}
}
mPointSymbolName = name;
mCacheUpToDate = mCacheUpToDate2 = false;
}
QString QgsSymbol::pointSymbolName() const
{
return mPointSymbolName;
}
void QgsSymbol::setPointSize(double s)
{
if ( s < MINIMUM_POINT_SIZE )
mPointSize = MINIMUM_POINT_SIZE;
else
mPointSize = s;
mCacheUpToDate = mCacheUpToDate2 = false;
}
double QgsSymbol::pointSize() const
{
return mPointSize;
}
QImage QgsSymbol::getLineSymbolAsImage()
{
//Note by Tim: dont use premultiplied - it causes
//artifacts on the output icon!
QImage img(15, 15,QImage::Format_ARGB32 );//QImage::Format_ARGB32_Premultiplied);
img.fill(QColor(255,255,255,255).rgba());
QPainter p(&img);
p.setRenderHints(QPainter::Antialiasing);
p.setPen(mPen);
QPainterPath myPath;
myPath.moveTo(0, 0);
myPath.cubicTo(15, 0, 5, 7, 15, 15);
p.drawPath(myPath);
//p.drawLine(0, 0, 15, 15);
return img; //this is ok because of qts sharing mechanism
}
QImage QgsSymbol::getPolygonSymbolAsImage()
{
//Note by Tim: dont use premultiplied - it causes
//artifacts on the output icon!
QImage img(15, 15,QImage::Format_ARGB32); //, QImage::Format_ARGB32_Premultiplied);
img.fill(QColor(255,255,255,255).rgba());
QPainter p(&img);
p.setRenderHints(QPainter::Antialiasing);
p.setPen(mPen);
p.setBrush(mBrush);
QPolygon myPolygon;
//leave a little white space around so
//dont draw at 0,0,15,15
myPolygon << QPoint(2, 2)
<< QPoint(1, 5)
<< QPoint(1, 10)
<< QPoint(2, 12)
<< QPoint(5, 13)
<< QPoint(6, 13)
<< QPoint(8, 12)
<< QPoint(8, 12)
<< QPoint(10, 12)
<< QPoint(12, 13)
<< QPoint(13, 11)
<< QPoint(12, 8)
<< QPoint(11, 6)
<< QPoint(12, 5)
<< QPoint(13, 2)
<< QPoint(11, 1)
<< QPoint(10, 1)
<< QPoint(8, 2)
<< QPoint(6, 4)
<< QPoint(4, 2)
;
p.drawPolygon(myPolygon);
//p.drawRect(1, 1, 14, 14);
return img; //this is ok because of qts sharing mechanism
}
QImage QgsSymbol::getCachedPointSymbolAsImage( double widthScale,
bool selected, QColor selectionColor )
{
if ( !mCacheUpToDate2
|| ( selected && mSelectionColor != selectionColor ) )
{
if ( selected ) {
cache2( widthScale, selectionColor );
} else {
cache2( widthScale, mSelectionColor );
}
}
if ( selected )
{
return mPointSymbolImageSelected2;
}
else
{
return mPointSymbolImage2;
}
}
QImage QgsSymbol::getPointSymbolAsImage( double widthScale, bool selected, QColor selectionColor, double scale, \
double rotation, double rasterScaleFactor)
{
//QgsDebugMsg(QString("Symbol scale = %1, and rotation = %2").arg(scale).arg(rotation));
if ( 1.0 == (scale * rasterScaleFactor) && 0 == rotation )
{
// If scale is 1.0 and rotation 0.0, use cached image.
return getCachedPointSymbolAsImage( widthScale, selected, selectionColor );
}
QImage preRotateImage;
QPen pen = mPen;
double newWidth = mPen.widthF() * widthScale * rasterScaleFactor;
pen.setWidth(newWidth);
if ( selected )
{
pen.setColor ( selectionColor );
QBrush brush = mBrush;
preRotateImage = QgsMarkerCatalogue::instance()->imageMarker ( mPointSymbolName, (float)(mPointSize * scale * widthScale * rasterScaleFactor),
pen, mBrush );
}
else
{
preRotateImage = QgsMarkerCatalogue::instance()->imageMarker ( mPointSymbolName, (float)(mPointSize * scale * widthScale * rasterScaleFactor),
pen, mBrush );
}
QMatrix rotationMatrix;
rotationMatrix = rotationMatrix.rotate(rotation);
return preRotateImage.transformed(rotationMatrix, Qt::SmoothTransformation);
}
void QgsSymbol::cache( QColor selectionColor )
{
QPen pen = mPen;
pen.setColor ( selectionColor );
QBrush brush = mBrush;
// For symbols that have a different coloured border, the line
// below causes the fill colour to be wrong for the print
// composer. Not sure why...
// brush.setColor ( selectionColor );
mPointSymbolImage = QgsMarkerCatalogue::instance()->imageMarker ( mPointSymbolName, mPointSize,
mPen, mBrush );
mPointSymbolImageSelected = QgsMarkerCatalogue::instance()->imageMarker (
mPointSymbolName, mPointSize, pen, brush );
mSelectionColor = selectionColor;
mCacheUpToDate = true;
}
void QgsSymbol::cache2( double widthScale, QColor selectionColor )
{
//std::cerr << "QgsSymbol::cache2 widthScale = " << widthScale << std::endl;
QPen pen = mPen;
pen.setWidthF(widthScale * pen.widthF());
mPointSymbolImage2 = QgsMarkerCatalogue::instance()->imageMarker ( mPointSymbolName, mPointSize * widthScale,
pen, mBrush, false );
QBrush brush = mBrush;
brush.setColor ( selectionColor );
pen.setColor ( selectionColor );
mPointSymbolImageSelected2 = QgsMarkerCatalogue::instance()->imageMarker (
mPointSymbolName, mPointSize * widthScale, pen, brush, false );
mSelectionColor2 = selectionColor;
mWidthScale = widthScale;
mCacheUpToDate2 = true;
}
bool QgsSymbol::writeXML( QDomNode & item, QDomDocument & document ) const
{
bool returnval = false;
returnval = true; // no error checking yet
QDomElement symbol=document.createElement("symbol");
item.appendChild(symbol);
QDomElement lowervalue=document.createElement("lowervalue");
QDomText lowervaluetxt=document.createTextNode(mLowerValue);
symbol.appendChild(lowervalue);
lowervalue.appendChild(lowervaluetxt);
QDomElement uppervalue=document.createElement("uppervalue");
QDomText uppervaluetxt=document.createTextNode(mUpperValue);
symbol.appendChild(uppervalue);
uppervalue.appendChild(uppervaluetxt);
QDomElement label=document.createElement("label");
QDomText labeltxt=document.createTextNode(mLabel);
symbol.appendChild(label);
label.appendChild(labeltxt);
QDomElement pointsymbol=document.createElement("pointsymbol");
QDomText pointsymboltxt=document.createTextNode(pointSymbolName());
symbol.appendChild(pointsymbol);
pointsymbol.appendChild(pointsymboltxt);
QDomElement pointsize=document.createElement("pointsize");
QDomText pointsizetxt=document.createTextNode( QString::number(pointSize()) );
symbol.appendChild(pointsize);
pointsize.appendChild(pointsizetxt);
QDomElement rotationclassificationfield=document.createElement("rotationclassificationfield");
QDomText rotationclassificationfieldtxt=document.createTextNode(QString::number(mRotationClassificationField));
rotationclassificationfield.appendChild(rotationclassificationfieldtxt);
symbol.appendChild(rotationclassificationfield);
QDomElement scaleclassificationfield=document.createElement("scaleclassificationfield");
QDomText scaleclassificationfieldtxt=document.createTextNode(QString::number(mScaleClassificationField));
scaleclassificationfield.appendChild(scaleclassificationfieldtxt);
symbol.appendChild(scaleclassificationfield);
QDomElement outlinecolor=document.createElement("outlinecolor");
outlinecolor.setAttribute("red",QString::number(mPen.color().red()));
outlinecolor.setAttribute("green",QString::number(mPen.color().green()));
outlinecolor.setAttribute("blue",QString::number(mPen.color().blue()));
symbol.appendChild(outlinecolor);
QDomElement outlinestyle=document.createElement("outlinestyle");
QDomText outlinestyletxt=document.createTextNode(QgsSymbologyUtils::penStyle2QString(mPen.style()));
outlinestyle.appendChild(outlinestyletxt);
symbol.appendChild(outlinestyle);
QDomElement outlinewidth=document.createElement("outlinewidth");
QDomText outlinewidthtxt=document.createTextNode(QString::number(mPen.widthF()));
outlinewidth.appendChild(outlinewidthtxt);
symbol.appendChild(outlinewidth);
QDomElement fillcolor=document.createElement("fillcolor");
fillcolor.setAttribute("red",QString::number(mBrush.color().red()));
fillcolor.setAttribute("green",QString::number(mBrush.color().green()));
fillcolor.setAttribute("blue",QString::number(mBrush.color().blue()));
symbol.appendChild(fillcolor);
QDomElement fillpattern=document.createElement("fillpattern");
QDomText fillpatterntxt=document.createTextNode(QgsSymbologyUtils::brushStyle2QString(mBrush.style()));
fillpattern.appendChild(fillpatterntxt);
symbol.appendChild(fillpattern);
fillpattern.appendChild(fillpatterntxt);
QDomElement texturepath=document.createElement("texturepath");
QDomText texturepathtxt=document.createTextNode(mTextureFilePath);
symbol.appendChild(texturepath);
texturepath.appendChild(texturepathtxt);
return returnval;
}
bool QgsSymbol::readXML( QDomNode & synode )
{
// Legacy project file formats didn't have support for pointsymbol nor
// pointsize DOM elements. Therefore we should check whether these
// actually exist.
QDomNode lvalnode = synode.namedItem("lowervalue");
if( ! lvalnode.isNull() )
{
QDomElement lvalelement = lvalnode.toElement();
mLowerValue=lvalelement.text();
}
QDomNode uvalnode = synode.namedItem("uppervalue");
if( ! uvalnode.isNull() )
{
QDomElement uvalelement = uvalnode.toElement();
mUpperValue=uvalelement.text();
}
QDomNode labelnode = synode.namedItem("label");
if( ! labelnode.isNull() )
{
QDomElement labelelement = labelnode.toElement();
mLabel=labelelement.text();
}
QDomNode psymbnode = synode.namedItem("pointsymbol");
if ( ! psymbnode.isNull() )
{
QDomElement psymbelement = psymbnode.toElement();
setNamedPointSymbol( psymbelement.text() );
}
QDomNode psizenode = synode.namedItem("pointsize");
if ( ! psizenode.isNull() )
{
QDomElement psizeelement = psizenode.toElement();
setPointSize( psizeelement.text().toFloat() );
}
mRotationClassificationField = -1;
mScaleClassificationField = -1;
QDomNode classnode = synode.namedItem("rotationclassificationfield");
if ( !classnode.isNull() )
{
mRotationClassificationField = classnode.toElement().text().toInt();
QgsDebugMsg("Found rotationfield: " + QString::number(rotationClassificationField()));
}
classnode = synode.namedItem("scaleclassificationfield");
if ( !classnode.isNull() )
{
mScaleClassificationField = classnode.toElement().text().toInt();
QgsDebugMsg("Found scalefield: " + QString::number(scaleClassificationField()));
}
QDomNode outlcnode = synode.namedItem("outlinecolor");
QDomElement oulcelement = outlcnode.toElement();
int red = oulcelement.attribute("red").toInt();
int green = oulcelement.attribute("green").toInt();
int blue = oulcelement.attribute("blue").toInt();
setColor(QColor(red, green, blue));
QDomNode outlstnode = synode.namedItem("outlinestyle");
QDomElement outlstelement = outlstnode.toElement();
setLineStyle(QgsSymbologyUtils::qString2PenStyle(outlstelement.text()));
QDomNode outlwnode = synode.namedItem("outlinewidth");
QDomElement outlwelement = outlwnode.toElement();
setLineWidth(outlwelement.text().toDouble());
QDomNode fillcnode = synode.namedItem("fillcolor");
QDomElement fillcelement = fillcnode.toElement();
red = fillcelement.attribute("red").toInt();
green = fillcelement.attribute("green").toInt();
blue = fillcelement.attribute("blue").toInt();
setFillColor(QColor(red, green, blue));
QDomNode texturepathnode = synode.namedItem("texturepath");
QDomElement texturepathelement = texturepathnode.toElement();
setCustomTexture(texturepathelement.text());
//run this after setting the custom texture path, so we override the brush if it isn't the custom pattern brush.
QDomNode fillpnode = synode.namedItem("fillpattern");
QDomElement fillpelement = fillpnode.toElement();
setFillStyle(QgsSymbologyUtils::qString2BrushStyle(fillpelement.text()));
return true;
}
int QgsSymbol::rotationClassificationField() const
{
return mRotationClassificationField;
}
void QgsSymbol::setRotationClassificationField(int field)
{
mRotationClassificationField = field;
}
int QgsSymbol::scaleClassificationField() const
{
return mScaleClassificationField;
}
void QgsSymbol::setScaleClassificationField(int field)
{
mScaleClassificationField = field;
}