QGIS API Documentation 3.35.0-Master (6cf940c20d6)
Loading...
Searching...
No Matches
qgsmarkersymbollayer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmarkersymbollayer.cpp
3 ---------------------
4 begin : November 2009
5 copyright : (C) 2009 by Martin Dobias
6 email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "qgssymbollayerutils.h"
18
19#include "qgsdxfexport.h"
20#include "qgsdxfpaintdevice.h"
21#include "qgsfontutils.h"
22#include "qgsimagecache.h"
23#include "qgsimageoperation.h"
24#include "qgsrendercontext.h"
25#include "qgslogger.h"
26#include "qgssvgcache.h"
27#include "qgsunittypes.h"
28#include "qgssymbol.h"
29#include "qgsfillsymbol.h"
30#include "qgsfontmanager.h"
31
32#include <QPainter>
33#include <QSvgRenderer>
34#include <QFileInfo>
35#include <QDir>
36#include <QDomDocument>
37#include <QDomElement>
38#include <QUrlQuery>
39
40#include <cmath>
41
42Q_GUI_EXPORT extern int qt_defaultDpiX();
43Q_GUI_EXPORT extern int qt_defaultDpiY();
44
45static constexpr int MAX_FONT_CHARACTER_SIZE_IN_PIXELS = 500;
46
47static void _fixQPictureDPI( QPainter *p )
48{
49 // QPicture makes an assumption that we drawing to it with system DPI.
50 // Then when being drawn, it scales the painter. The following call
51 // negates the effect. There is no way of setting QPicture's DPI.
52 // See QTBUG-20361
53 p->scale( static_cast< double >( qt_defaultDpiX() ) / p->device()->logicalDpiX(),
54 static_cast< double >( qt_defaultDpiY() ) / p->device()->logicalDpiY() );
55}
56
57
59
60
61//
62// QgsSimpleMarkerSymbolLayerBase
63//
64
108
110 : mShape( shape )
111{
112 mSize = size;
113 mAngle = angle;
114 mOffset = QPointF( 0, 0 );
118}
119
121
123{
124 switch ( shape )
125 {
156 return true;
157
165 return false;
166 }
167 return true;
168}
169
171{
172 const bool hasDataDefinedRotation = context.renderHints() & Qgis::SymbolRenderHint::DynamicRotation
174 const bool hasDataDefinedSize = mDataDefinedProperties.isActive( QgsSymbolLayer::PropertySize );
175
176 // use either QPolygonF or QPainterPath for drawing
177 if ( !prepareMarkerShape( mShape ) ) // drawing as a polygon
178 {
179 prepareMarkerPath( mShape ); // drawing as a painter path
180 }
181
182 QTransform transform;
183
184 // scale the shape (if the size is not going to be modified)
185 if ( !hasDataDefinedSize )
186 {
187 double scaledSize = context.renderContext().convertToPainterUnits( mSize, mSizeUnit, mSizeMapUnitScale );
189 {
190 // rendering for symbol previews -- an size in meters in map units can't be calculated, so treat the size as millimeters
191 // and clamp it to a reasonable range. It's the best we can do in this situation!
192 scaledSize = std::min( std::max( context.renderContext().convertToPainterUnits( mSize, Qgis::RenderUnit::Millimeters ), 3.0 ), 100.0 );
193 }
194
195 const double half = scaledSize / 2.0;
196 transform.scale( half, half );
197 }
198
199 // rotate if the rotation is not going to be changed during the rendering
200 if ( !hasDataDefinedRotation && !qgsDoubleNear( mAngle, 0.0 ) )
201 {
202 transform.rotate( mAngle );
203 }
204
205 if ( !mPolygon.isEmpty() )
206 mPolygon = transform.map( mPolygon );
207 else
208 mPath = transform.map( mPath );
209
211}
212
214{
215 Q_UNUSED( context )
216}
217
219{
220 //making changes here? Don't forget to also update ::bounds if the changes affect the bounding box
221 //of the rendered point!
222
223 QPainter *p = context.renderContext().painter();
224 if ( !p )
225 {
226 return;
227 }
228
229 bool hasDataDefinedSize = false;
230 const double scaledSize = calculateSize( context, hasDataDefinedSize );
231
232 bool hasDataDefinedRotation = false;
233 QPointF offset;
234 double angle = 0;
235 calculateOffsetAndRotation( context, scaledSize, hasDataDefinedRotation, offset, angle );
236
237 //data defined shape?
238 bool createdNewPath = false;
239 bool ok = true;
240 Qgis::MarkerShape symbol = mShape;
242 {
243 context.setOriginalValueVariable( encodeShape( symbol ) );
245 if ( !QgsVariantUtils::isNull( exprVal ) )
246 {
247 const Qgis::MarkerShape decoded = decodeShape( exprVal.toString(), &ok );
248 if ( ok )
249 {
250 symbol = decoded;
251
252 if ( !prepareMarkerShape( symbol ) ) // drawing as a polygon
253 {
254 prepareMarkerPath( symbol ); // drawing as a painter path
255 }
256 createdNewPath = true;
257 }
258 }
259 else
260 {
261 symbol = mShape;
262 }
263 }
264
265 QTransform transform;
266
267 // move to the desired position
268 transform.translate( point.x() + offset.x(), point.y() + offset.y() );
269
270 // resize if necessary
271 if ( hasDataDefinedSize || createdNewPath )
272 {
273 double s = context.renderContext().convertToPainterUnits( scaledSize, mSizeUnit, mSizeMapUnitScale );
275 {
276 // rendering for symbol previews -- a size in meters in map units can't be calculated, so treat the size as millimeters
277 // and clamp it to a reasonable range. It's the best we can do in this situation!
278 s = std::min( std::max( context.renderContext().convertToPainterUnits( mSize, Qgis::RenderUnit::Millimeters ), 3.0 ), 100.0 );
279 }
280 const double half = s / 2.0;
281 transform.scale( half, half );
282 }
283
284 if ( !qgsDoubleNear( angle, 0.0 ) && ( hasDataDefinedRotation || createdNewPath ) )
285 {
286 transform.rotate( angle );
287 }
288
289 //need to pass: symbol, polygon, path
290
291 QPolygonF polygon;
292 QPainterPath path;
293 if ( !mPolygon.isEmpty() )
294 {
295 polygon = transform.map( mPolygon );
296 }
297 else
298 {
299 path = transform.map( mPath );
300 }
301 draw( context, symbol, polygon, path );
302}
303
305{
306 bool hasDataDefinedSize = false;
307 double scaledSize = calculateSize( context, hasDataDefinedSize );
308
309 bool hasDataDefinedRotation = false;
310 QPointF offset;
311 double angle = 0;
312 calculateOffsetAndRotation( context, scaledSize, hasDataDefinedRotation, offset, angle );
313
314 scaledSize = context.renderContext().convertToPainterUnits( scaledSize, mSizeUnit, mSizeMapUnitScale );
315
316 QTransform transform;
317
318 // move to the desired position
319 transform.translate( point.x() + offset.x(), point.y() + offset.y() );
320
321 if ( !qgsDoubleNear( angle, 0.0 ) )
322 transform.rotate( angle );
323
324 return transform.mapRect( QRectF( -scaledSize / 2.0,
325 -scaledSize / 2.0,
326 scaledSize,
327 scaledSize ) );
328}
329
331{
332 if ( ok )
333 *ok = true;
334 const QString cleaned = name.toLower().trimmed();
335
336 if ( cleaned == QLatin1String( "square" ) || cleaned == QLatin1String( "rectangle" ) )
338 else if ( cleaned == QLatin1String( "trapezoid" ) )
340 else if ( cleaned == QLatin1String( "parallelogram_right" ) )
342 else if ( cleaned == QLatin1String( "parallelogram_left" ) )
344 else if ( cleaned == QLatin1String( "square_with_corners" ) )
346 else if ( cleaned == QLatin1String( "rounded_square" ) )
348 else if ( cleaned == QLatin1String( "diamond" ) )
350 else if ( cleaned == QLatin1String( "shield" ) )
352 else if ( cleaned == QLatin1String( "pentagon" ) )
354 else if ( cleaned == QLatin1String( "hexagon" ) )
356 else if ( cleaned == QLatin1String( "octagon" ) )
358 else if ( cleaned == QLatin1String( "decagon" ) )
360 else if ( cleaned == QLatin1String( "triangle" ) )
362 else if ( cleaned == QLatin1String( "equilateral_triangle" ) )
364 else if ( cleaned == QLatin1String( "star_diamond" ) )
366 else if ( cleaned == QLatin1String( "star" ) || cleaned == QLatin1String( "regular_star" ) )
368 else if ( cleaned == QLatin1String( "heart" ) )
370 else if ( cleaned == QLatin1String( "arrow" ) )
372 else if ( cleaned == QLatin1String( "circle" ) )
374 else if ( cleaned == QLatin1String( "cross" ) )
376 else if ( cleaned == QLatin1String( "cross_fill" ) )
378 else if ( cleaned == QLatin1String( "cross2" ) || cleaned == QLatin1String( "x" ) )
380 else if ( cleaned == QLatin1String( "line" ) )
382 else if ( cleaned == QLatin1String( "arrowhead" ) )
384 else if ( cleaned == QLatin1String( "filled_arrowhead" ) )
386 else if ( cleaned == QLatin1String( "semi_circle" ) )
388 else if ( cleaned == QLatin1String( "third_circle" ) )
390 else if ( cleaned == QLatin1String( "quarter_circle" ) )
392 else if ( cleaned == QLatin1String( "quarter_square" ) )
394 else if ( cleaned == QLatin1String( "half_square" ) )
396 else if ( cleaned == QLatin1String( "diagonal_half_square" ) )
398 else if ( cleaned == QLatin1String( "right_half_triangle" ) )
400 else if ( cleaned == QLatin1String( "left_half_triangle" ) )
402 else if ( cleaned == QLatin1String( "asterisk_fill" ) )
404 else if ( cleaned == QLatin1String( "half_arc" ) )
406 else if ( cleaned == QLatin1String( "third_arc" ) )
408 else if ( cleaned == QLatin1String( "quarter_arc" ) )
410
411 if ( ok )
412 *ok = false;
414}
415
417{
418 switch ( shape )
419 {
421 return QStringLiteral( "square" );
423 return QStringLiteral( "quarter_square" );
425 return QStringLiteral( "half_square" );
427 return QStringLiteral( "diagonal_half_square" );
429 return QStringLiteral( "parallelogram_right" );
431 return QStringLiteral( "parallelogram_left" );
433 return QStringLiteral( "trapezoid" );
435 return QStringLiteral( "shield" );
437 return QStringLiteral( "diamond" );
439 return QStringLiteral( "pentagon" );
441 return QStringLiteral( "hexagon" );
443 return QStringLiteral( "octagon" );
445 return QStringLiteral( "decagon" );
447 return QStringLiteral( "square_with_corners" );
449 return QStringLiteral( "rounded_square" );
451 return QStringLiteral( "triangle" );
453 return QStringLiteral( "equilateral_triangle" );
455 return QStringLiteral( "left_half_triangle" );
457 return QStringLiteral( "right_half_triangle" );
459 return QStringLiteral( "star_diamond" );
461 return QStringLiteral( "star" );
463 return QStringLiteral( "heart" );
465 return QStringLiteral( "arrow" );
467 return QStringLiteral( "filled_arrowhead" );
469 return QStringLiteral( "cross_fill" );
471 return QStringLiteral( "circle" );
473 return QStringLiteral( "cross" );
475 return QStringLiteral( "cross2" );
477 return QStringLiteral( "line" );
479 return QStringLiteral( "arrowhead" );
481 return QStringLiteral( "semi_circle" );
483 return QStringLiteral( "third_circle" );
485 return QStringLiteral( "quarter_circle" );
487 return QStringLiteral( "asterisk_fill" );
489 return QStringLiteral( "half_arc" );
491 return QStringLiteral( "third_arc" );
493 return QStringLiteral( "quarter_arc" );
494 }
495 return QString();
496}
497
502
504{
505 polygon.clear();
506
507 switch ( shape )
508 {
510 polygon = QPolygonF( QRectF( QPointF( -1, -1 ), QPointF( 1, 1 ) ) );
511 return true;
512
514 {
515 static constexpr double VERTEX_OFFSET_FROM_ORIGIN = 0.6072;
516
517 polygon << QPointF( - VERTEX_OFFSET_FROM_ORIGIN, 1 )
518 << QPointF( VERTEX_OFFSET_FROM_ORIGIN, 1 )
519 << QPointF( 1, VERTEX_OFFSET_FROM_ORIGIN )
520 << QPointF( 1, -VERTEX_OFFSET_FROM_ORIGIN )
521 << QPointF( VERTEX_OFFSET_FROM_ORIGIN, -1 )
522 << QPointF( -VERTEX_OFFSET_FROM_ORIGIN, -1 )
523 << QPointF( -1, -VERTEX_OFFSET_FROM_ORIGIN )
524 << QPointF( -1, VERTEX_OFFSET_FROM_ORIGIN )
525 << QPointF( -VERTEX_OFFSET_FROM_ORIGIN, 1 );
526 return true;
527 }
528
530 polygon = QPolygonF( QRectF( QPointF( -1, -1 ), QPointF( 0, 0 ) ) );
531 return true;
532
534 polygon = QPolygonF( QRectF( QPointF( -1, -1 ), QPointF( 0, 1 ) ) );
535 return true;
536
538 polygon << QPointF( -1, -1 ) << QPointF( 1, 1 ) << QPointF( -1, 1 ) << QPointF( -1, -1 );
539 return true;
540
542 polygon << QPointF( 0.5, -0.5 )
543 << QPointF( 1, 0.5 )
544 << QPointF( -1, 0.5 )
545 << QPointF( -0.5, -0.5 )
546 << QPointF( 0.5, -0.5 );
547 return true;
548
550 polygon << QPointF( 0.5, 0.5 )
551 << QPointF( 1, -0.5 )
552 << QPointF( -0.5, -0.5 )
553 << QPointF( -1, 0.5 )
554 << QPointF( 0.5, 0.5 );
555 return true;
556
558 polygon << QPointF( 1, 0.5 )
559 << QPointF( 0.5, -0.5 )
560 << QPointF( -1, -0.5 )
561 << QPointF( -0.5, 0.5 )
562 << QPointF( 1, 0.5 );
563 return true;
564
566 polygon << QPointF( -1, 0 ) << QPointF( 0, 1 )
567 << QPointF( 1, 0 ) << QPointF( 0, -1 ) << QPointF( -1, 0 );
568 return true;
569
571 polygon << QPointF( 1, 0.5 )
572 << QPointF( 1, -1 )
573 << QPointF( -1, -1 )
574 << QPointF( -1, 0.5 )
575 << QPointF( 0, 1 )
576 << QPointF( 1, 0.5 );
577 return true;
578
580 /* angular-representation of hardcoded values used
581 polygon << QPointF( std::sin( DEG2RAD( 288.0 ) ), - std::cos( DEG2RAD( 288.0 ) ) )
582 << QPointF( std::sin( DEG2RAD( 216.0 ) ), - std::cos( DEG2RAD( 216.0 ) ) )
583 << QPointF( std::sin( DEG2RAD( 144.0 ) ), - std::cos( DEG2RAD( 144.0 ) ) )
584 << QPointF( std::sin( DEG2RAD( 72.0 ) ), - std::cos( DEG2RAD( 72.0 ) ) )
585 << QPointF( 0, -1 ); */
586 polygon << QPointF( -0.9511, -0.3090 )
587 << QPointF( -0.5878, 0.8090 )
588 << QPointF( 0.5878, 0.8090 )
589 << QPointF( 0.9511, -0.3090 )
590 << QPointF( 0, -1 )
591 << QPointF( -0.9511, -0.3090 );
592 return true;
593
595 /* angular-representation of hardcoded values used
596 polygon << QPointF( std::sin( DEG2RAD( 300.0 ) ), - std::cos( DEG2RAD( 300.0 ) ) )
597 << QPointF( std::sin( DEG2RAD( 240.0 ) ), - std::cos( DEG2RAD( 240.0 ) ) )
598 << QPointF( std::sin( DEG2RAD( 180.0 ) ), - std::cos( DEG2RAD( 180.0 ) ) )
599 << QPointF( std::sin( DEG2RAD( 120.0 ) ), - std::cos( DEG2RAD( 120.0 ) ) )
600 << QPointF( std::sin( DEG2RAD( 60.0 ) ), - std::cos( DEG2RAD( 60.0 ) ) )
601 << QPointF( 0, -1 ); */
602 polygon << QPointF( -0.8660, -0.5 )
603 << QPointF( -0.8660, 0.5 )
604 << QPointF( 0, 1 )
605 << QPointF( 0.8660, 0.5 )
606 << QPointF( 0.8660, -0.5 )
607 << QPointF( 0, -1 )
608 << QPointF( -0.8660, -0.5 );
609 return true;
610
612 {
613 static constexpr double VERTEX_OFFSET_FROM_ORIGIN = 1.0 / ( 1 + M_SQRT2 );
614
615 polygon << QPointF( - VERTEX_OFFSET_FROM_ORIGIN, 1 )
616 << QPointF( VERTEX_OFFSET_FROM_ORIGIN, 1 )
617 << QPointF( 1, VERTEX_OFFSET_FROM_ORIGIN )
618 << QPointF( 1, -VERTEX_OFFSET_FROM_ORIGIN )
619 << QPointF( VERTEX_OFFSET_FROM_ORIGIN, -1 )
620 << QPointF( -VERTEX_OFFSET_FROM_ORIGIN, -1 )
621 << QPointF( -1, -VERTEX_OFFSET_FROM_ORIGIN )
622 << QPointF( -1, VERTEX_OFFSET_FROM_ORIGIN )
623 << QPointF( -VERTEX_OFFSET_FROM_ORIGIN, 1 );
624 return true;
625 }
626
628 {
629
630 polygon << QPointF( 0.587785252, 0.809016994 )
631 << QPointF( 0.951056516, 0.309016994 )
632 << QPointF( 0.951056516, -0.309016994 )
633 << QPointF( 0.587785252, -0.809016994 )
634 << QPointF( 0, -1 )
635 << QPointF( -0.587785252, -0.809016994 )
636 << QPointF( -0.951056516, -0.309016994 )
637 << QPointF( -0.951056516, 0.309016994 )
638 << QPointF( -0.587785252, 0.809016994 )
639 << QPointF( 0, 1 )
640 << QPointF( 0.587785252, 0.809016994 );
641 return true;
642 }
643
645 polygon << QPointF( -1, 1 ) << QPointF( 1, 1 ) << QPointF( 0, -1 ) << QPointF( -1, 1 );
646 return true;
647
649 /* angular-representation of hardcoded values used
650 polygon << QPointF( std::sin( DEG2RAD( 240.0 ) ), - std::cos( DEG2RAD( 240.0 ) ) )
651 << QPointF( std::sin( DEG2RAD( 120.0 ) ), - std::cos( DEG2RAD( 120.0 ) ) )
652 << QPointF( 0, -1 ); */
653 polygon << QPointF( -0.8660, 0.5 )
654 << QPointF( 0.8660, 0.5 )
655 << QPointF( 0, -1 )
656 << QPointF( -0.8660, 0.5 );
657 return true;
658
660 polygon << QPointF( 0, 1 ) << QPointF( 1, 1 ) << QPointF( 0, -1 ) << QPointF( 0, 1 );
661 return true;
662
664 polygon << QPointF( -1, 1 ) << QPointF( 0, 1 ) << QPointF( 0, -1 ) << QPointF( -1, 1 );
665 return true;
666
668 {
669 const double inner_r = std::cos( DEG2RAD( 72.0 ) ) / std::cos( DEG2RAD( 36.0 ) );
670
671 polygon << QPointF( inner_r * std::sin( DEG2RAD( 315.0 ) ), - inner_r * std::cos( DEG2RAD( 315.0 ) ) )
672 << QPointF( std::sin( DEG2RAD( 270 ) ), - std::cos( DEG2RAD( 270 ) ) )
673 << QPointF( inner_r * std::sin( DEG2RAD( 225.0 ) ), - inner_r * std::cos( DEG2RAD( 225.0 ) ) )
674 << QPointF( std::sin( DEG2RAD( 180 ) ), - std::cos( DEG2RAD( 180 ) ) )
675 << QPointF( inner_r * std::sin( DEG2RAD( 135.0 ) ), - inner_r * std::cos( DEG2RAD( 135.0 ) ) )
676 << QPointF( std::sin( DEG2RAD( 90 ) ), - std::cos( DEG2RAD( 90 ) ) )
677 << QPointF( inner_r * std::sin( DEG2RAD( 45.0 ) ), - inner_r * std::cos( DEG2RAD( 45.0 ) ) )
678 << QPointF( std::sin( DEG2RAD( 0 ) ), - std::cos( DEG2RAD( 0 ) ) );
679 return true;
680 }
681
683 {
684 const double inner_r = std::cos( DEG2RAD( 72.0 ) ) / std::cos( DEG2RAD( 36.0 ) );
685
686 polygon << QPointF( inner_r * std::sin( DEG2RAD( 324.0 ) ), - inner_r * std::cos( DEG2RAD( 324.0 ) ) ) // 324
687 << QPointF( std::sin( DEG2RAD( 288.0 ) ), - std::cos( DEG2RAD( 288 ) ) ) // 288
688 << QPointF( inner_r * std::sin( DEG2RAD( 252.0 ) ), - inner_r * std::cos( DEG2RAD( 252.0 ) ) ) // 252
689 << QPointF( std::sin( DEG2RAD( 216.0 ) ), - std::cos( DEG2RAD( 216.0 ) ) ) // 216
690 << QPointF( 0, inner_r ) // 180
691 << QPointF( std::sin( DEG2RAD( 144.0 ) ), - std::cos( DEG2RAD( 144.0 ) ) ) // 144
692 << QPointF( inner_r * std::sin( DEG2RAD( 108.0 ) ), - inner_r * std::cos( DEG2RAD( 108.0 ) ) ) // 108
693 << QPointF( std::sin( DEG2RAD( 72.0 ) ), - std::cos( DEG2RAD( 72.0 ) ) ) // 72
694 << QPointF( inner_r * std::sin( DEG2RAD( 36.0 ) ), - inner_r * std::cos( DEG2RAD( 36.0 ) ) ) // 36
695 << QPointF( 0, -1 )
696 << QPointF( inner_r * std::sin( DEG2RAD( 324.0 ) ), - inner_r * std::cos( DEG2RAD( 324.0 ) ) ); // 324; // 0
697 return true;
698 }
699
701 polygon << QPointF( 0, -1 )
702 << QPointF( 0.5, -0.5 )
703 << QPointF( 0.25, -0.5 )
704 << QPointF( 0.25, 1 )
705 << QPointF( -0.25, 1 )
706 << QPointF( -0.25, -0.5 )
707 << QPointF( -0.5, -0.5 )
708 << QPointF( 0, -1 );
709 return true;
710
712 polygon << QPointF( 0, 0 ) << QPointF( -1, 1 ) << QPointF( -1, -1 ) << QPointF( 0, 0 );
713 return true;
714
716 polygon << QPointF( -1, -0.2 )
717 << QPointF( -1, -0.2 )
718 << QPointF( -1, 0.2 )
719 << QPointF( -0.2, 0.2 )
720 << QPointF( -0.2, 1 )
721 << QPointF( 0.2, 1 )
722 << QPointF( 0.2, 0.2 )
723 << QPointF( 1, 0.2 )
724 << QPointF( 1, -0.2 )
725 << QPointF( 0.2, -0.2 )
726 << QPointF( 0.2, -1 )
727 << QPointF( -0.2, -1 )
728 << QPointF( -0.2, -0.2 )
729 << QPointF( -1, -0.2 );
730 return true;
731
733 {
734 static constexpr double THICKNESS = 0.3;
735 static constexpr double HALF_THICKNESS = THICKNESS / 2.0;
736 static constexpr double INTERSECTION_POINT = THICKNESS / M_SQRT2;
737 static constexpr double DIAGONAL1 = M_SQRT1_2 - INTERSECTION_POINT * 0.5;
738 static constexpr double DIAGONAL2 = M_SQRT1_2 + INTERSECTION_POINT * 0.5;
739
740 polygon << QPointF( -HALF_THICKNESS, -1 )
741 << QPointF( HALF_THICKNESS, -1 )
742 << QPointF( HALF_THICKNESS, -HALF_THICKNESS - INTERSECTION_POINT )
743 << QPointF( DIAGONAL1, -DIAGONAL2 )
744 << QPointF( DIAGONAL2, -DIAGONAL1 )
745 << QPointF( HALF_THICKNESS + INTERSECTION_POINT, -HALF_THICKNESS )
746 << QPointF( 1, -HALF_THICKNESS )
747 << QPointF( 1, HALF_THICKNESS )
748 << QPointF( HALF_THICKNESS + INTERSECTION_POINT, HALF_THICKNESS )
749 << QPointF( DIAGONAL2, DIAGONAL1 )
750 << QPointF( DIAGONAL1, DIAGONAL2 )
751 << QPointF( HALF_THICKNESS, HALF_THICKNESS + INTERSECTION_POINT )
752 << QPointF( HALF_THICKNESS, 1 )
753 << QPointF( -HALF_THICKNESS, 1 )
754 << QPointF( -HALF_THICKNESS, HALF_THICKNESS + INTERSECTION_POINT )
755 << QPointF( -DIAGONAL1, DIAGONAL2 )
756 << QPointF( -DIAGONAL2, DIAGONAL1 )
757 << QPointF( -HALF_THICKNESS - INTERSECTION_POINT, HALF_THICKNESS )
758 << QPointF( -1, HALF_THICKNESS )
759 << QPointF( -1, -HALF_THICKNESS )
760 << QPointF( -HALF_THICKNESS - INTERSECTION_POINT, -HALF_THICKNESS )
761 << QPointF( -DIAGONAL2, -DIAGONAL1 )
762 << QPointF( -DIAGONAL1, -DIAGONAL2 )
763 << QPointF( -HALF_THICKNESS, -HALF_THICKNESS - INTERSECTION_POINT )
764 << QPointF( -HALF_THICKNESS, -1 );
765 return true;
766 }
767
781 return false;
782 }
783
784 return false;
785}
786
788{
789 mPath = QPainterPath();
790
791 switch ( symbol )
792 {
794
795 mPath.addEllipse( QRectF( -1, -1, 2, 2 ) ); // x,y,w,h
796 return true;
797
799 mPath.moveTo( -1, -1 );
800 mPath.addRoundedRect( -1, -1, 2, 2, 0.25, 0.25 );
801 return true;
802
804 mPath.arcTo( -1, -1, 2, 2, 0, 180 );
805 mPath.lineTo( 0, 0 );
806 return true;
807
809 mPath.arcTo( -1, -1, 2, 2, 90, 120 );
810 mPath.lineTo( 0, 0 );
811 return true;
812
814 mPath.arcTo( -1, -1, 2, 2, 90, 90 );
815 mPath.lineTo( 0, 0 );
816 return true;
817
819 mPath.moveTo( 1, 0 );
820 mPath.arcTo( -1, -1, 2, 2, 0, 180 );
821 return true;
822
824 mPath.moveTo( 0, -1 );
825 mPath.arcTo( -1, -1, 2, 2, 90, 120 );
826 return true;
827
829 mPath.moveTo( 0, -1 );
830 mPath.arcTo( -1, -1, 2, 2, 90, 90 );
831 return true;
832
834 mPath.moveTo( -1, 0 );
835 mPath.lineTo( 1, 0 ); // horizontal
836 mPath.moveTo( 0, -1 );
837 mPath.lineTo( 0, 1 ); // vertical
838 return true;
839
841 mPath.moveTo( -1, -1 );
842 mPath.lineTo( 1, 1 );
843 mPath.moveTo( 1, -1 );
844 mPath.lineTo( -1, 1 );
845 return true;
846
848 mPath.moveTo( 0, -1 );
849 mPath.lineTo( 0, 1 ); // vertical line
850 return true;
851
853 mPath.moveTo( -1, -1 );
854 mPath.lineTo( 0, 0 );
855 mPath.lineTo( -1, 1 );
856 return true;
857
859 mPath.moveTo( 0, 0.75 );
860 mPath.arcTo( 0, -1, 1, 1, -45, 210 );
861 mPath.arcTo( -1, -1, 1, 1, 15, 210 );
862 mPath.lineTo( 0, 0.75 );
863 return true;
864
889 return false;
890 }
891 return false;
892}
893
894double QgsSimpleMarkerSymbolLayerBase::calculateSize( QgsSymbolRenderContext &context, bool &hasDataDefinedSize ) const
895{
896 double scaledSize = mSize;
897
899 bool ok = true;
900 if ( hasDataDefinedSize )
901 {
904 mSize, &ok );
905 }
906
907 if ( hasDataDefinedSize && ok )
908 {
909 switch ( mScaleMethod )
910 {
912 scaledSize = std::sqrt( scaledSize );
913 break;
915 break;
916 }
917 }
918
919 return scaledSize;
920}
921
922void QgsSimpleMarkerSymbolLayerBase::calculateOffsetAndRotation( QgsSymbolRenderContext &context, double scaledSize, bool &hasDataDefinedRotation, QPointF &offset, double &angle ) const
923{
924 //offset
925 double offsetX = 0;
926 double offsetY = 0;
927 markerOffset( context, scaledSize, scaledSize, offsetX, offsetY );
928 offset = QPointF( offsetX, offsetY );
929
930 hasDataDefinedRotation = false;
931 //angle
932 bool ok = true;
935 {
938
939 // If the expression evaluation was not successful, fallback to static value
940 if ( !ok )
942
943 hasDataDefinedRotation = true;
944 }
945
946 hasDataDefinedRotation = context.renderHints() & Qgis::SymbolRenderHint::DynamicRotation || hasDataDefinedRotation;
947
948 if ( hasDataDefinedRotation )
949 {
950 // For non-point markers, "dataDefinedRotation" means following the
951 // shape (shape-data defined). For them, "field-data defined" does
952 // not work at all. TODO: if "field-data defined" ever gets implemented
953 // we'll need a way to distinguish here between the two, possibly
954 // using another flag in renderHints()
955 const QgsFeature *f = context.feature();
956 if ( f )
957 {
958 if ( f->hasGeometry() && f->geometry().type() == Qgis::GeometryType::Point )
959 {
960 const QgsMapToPixel &m2p = context.renderContext().mapToPixel();
961 angle += m2p.mapRotation();
962 }
963 }
964 }
965
966 if ( angle )
968}
969
970
971//
972// QgsSimpleMarkerSymbolLayer
973//
974
975QgsSimpleMarkerSymbolLayer::QgsSimpleMarkerSymbolLayer( Qgis::MarkerShape shape, double size, double angle, Qgis::ScaleMethod scaleMethod, const QColor &color, const QColor &strokeColor, Qt::PenJoinStyle penJoinStyle )
976 : QgsSimpleMarkerSymbolLayerBase( shape, size, angle, scaleMethod )
977 , mStrokeColor( strokeColor )
978 , mPenJoinStyle( penJoinStyle )
979{
980 mColor = color;
981}
982
984
986{
994
995 if ( props.contains( QStringLiteral( "name" ) ) )
996 {
997 shape = decodeShape( props[QStringLiteral( "name" )].toString() );
998 }
999 if ( props.contains( QStringLiteral( "color" ) ) )
1000 color = QgsSymbolLayerUtils::decodeColor( props[QStringLiteral( "color" )].toString() );
1001 if ( props.contains( QStringLiteral( "color_border" ) ) )
1002 {
1003 //pre 2.5 projects use "color_border"
1004 strokeColor = QgsSymbolLayerUtils::decodeColor( props[QStringLiteral( "color_border" )].toString() );
1005 }
1006 else if ( props.contains( QStringLiteral( "outline_color" ) ) )
1007 {
1008 strokeColor = QgsSymbolLayerUtils::decodeColor( props[QStringLiteral( "outline_color" )].toString() );
1009 }
1010 else if ( props.contains( QStringLiteral( "line_color" ) ) )
1011 {
1012 strokeColor = QgsSymbolLayerUtils::decodeColor( props[QStringLiteral( "line_color" )].toString() );
1013 }
1014 if ( props.contains( QStringLiteral( "joinstyle" ) ) )
1015 {
1016 penJoinStyle = QgsSymbolLayerUtils::decodePenJoinStyle( props[QStringLiteral( "joinstyle" )].toString() );
1017 }
1018 if ( props.contains( QStringLiteral( "size" ) ) )
1019 size = props[QStringLiteral( "size" )].toDouble();
1020 if ( props.contains( QStringLiteral( "angle" ) ) )
1021 angle = props[QStringLiteral( "angle" )].toDouble();
1022 if ( props.contains( QStringLiteral( "scale_method" ) ) )
1023 scaleMethod = QgsSymbolLayerUtils::decodeScaleMethod( props[QStringLiteral( "scale_method" )].toString() );
1024
1026 if ( props.contains( QStringLiteral( "offset" ) ) )
1027 m->setOffset( QgsSymbolLayerUtils::decodePoint( props[QStringLiteral( "offset" )].toString() ) );
1028 if ( props.contains( QStringLiteral( "offset_unit" ) ) )
1029 m->setOffsetUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "offset_unit" )].toString() ) );
1030 if ( props.contains( QStringLiteral( "offset_map_unit_scale" ) ) )
1031 m->setOffsetMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( props[QStringLiteral( "offset_map_unit_scale" )].toString() ) );
1032 if ( props.contains( QStringLiteral( "size_unit" ) ) )
1033 m->setSizeUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "size_unit" )].toString() ) );
1034 if ( props.contains( QStringLiteral( "size_map_unit_scale" ) ) )
1035 m->setSizeMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( props[QStringLiteral( "size_map_unit_scale" )].toString() ) );
1036
1037 if ( props.contains( QStringLiteral( "outline_style" ) ) )
1038 {
1039 m->setStrokeStyle( QgsSymbolLayerUtils::decodePenStyle( props[QStringLiteral( "outline_style" )].toString() ) );
1040 }
1041 else if ( props.contains( QStringLiteral( "line_style" ) ) )
1042 {
1043 m->setStrokeStyle( QgsSymbolLayerUtils::decodePenStyle( props[QStringLiteral( "line_style" )].toString() ) );
1044 }
1045 if ( props.contains( QStringLiteral( "outline_width" ) ) )
1046 {
1047 m->setStrokeWidth( props[QStringLiteral( "outline_width" )].toDouble() );
1048 }
1049 else if ( props.contains( QStringLiteral( "line_width" ) ) )
1050 {
1051 m->setStrokeWidth( props[QStringLiteral( "line_width" )].toDouble() );
1052 }
1053 if ( props.contains( QStringLiteral( "outline_width_unit" ) ) )
1054 {
1055 m->setStrokeWidthUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "outline_width_unit" )].toString() ) );
1056 }
1057 if ( props.contains( QStringLiteral( "line_width_unit" ) ) )
1058 {
1059 m->setStrokeWidthUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "line_width_unit" )].toString() ) );
1060 }
1061 if ( props.contains( QStringLiteral( "outline_width_map_unit_scale" ) ) )
1062 {
1063 m->setStrokeWidthMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( props[QStringLiteral( "outline_width_map_unit_scale" )].toString() ) );
1064 }
1065
1066 if ( props.contains( QStringLiteral( "horizontal_anchor_point" ) ) )
1067 {
1068 m->setHorizontalAnchorPoint( QgsMarkerSymbolLayer::HorizontalAnchorPoint( props[ QStringLiteral( "horizontal_anchor_point" )].toInt() ) );
1069 }
1070 if ( props.contains( QStringLiteral( "vertical_anchor_point" ) ) )
1071 {
1072 m->setVerticalAnchorPoint( QgsMarkerSymbolLayer::VerticalAnchorPoint( props[ QStringLiteral( "vertical_anchor_point" )].toInt() ) );
1073 }
1074
1075 if ( props.contains( QStringLiteral( "cap_style" ) ) )
1076 {
1077 m->setPenCapStyle( QgsSymbolLayerUtils::decodePenCapStyle( props[QStringLiteral( "cap_style" )].toString() ) );
1078 }
1079
1081
1082 return m;
1083}
1084
1085
1087{
1088 return QStringLiteral( "SimpleMarker" );
1089}
1090
1092{
1094
1095 QColor brushColor = mColor;
1096 QColor penColor = mStrokeColor;
1097
1098 brushColor.setAlphaF( mColor.alphaF() * context.opacity() );
1099 penColor.setAlphaF( mStrokeColor.alphaF() * context.opacity() );
1100
1101 mBrush = QBrush( brushColor );
1102 mPen = QPen( penColor );
1103 mPen.setStyle( mStrokeStyle );
1104 mPen.setCapStyle( mPenCapStyle );
1105 mPen.setJoinStyle( mPenJoinStyle );
1107
1108 QColor selBrushColor = context.renderContext().selectionColor();
1109 QColor selPenColor = selBrushColor == mColor ? selBrushColor : mStrokeColor;
1110 if ( context.opacity() < 1 && !SELECTION_IS_OPAQUE )
1111 {
1112 selBrushColor.setAlphaF( context.opacity() );
1113 selPenColor.setAlphaF( context.opacity() );
1114 }
1115 mSelBrush = QBrush( selBrushColor );
1116 mSelPen = QPen( selPenColor );
1117 mSelPen.setStyle( mStrokeStyle );
1119
1121 const bool hasDataDefinedSize = mDataDefinedProperties.isActive( QgsSymbolLayer::PropertySize );
1122
1123 // use caching only when:
1124 // - size, rotation, shape, color, stroke color is not data-defined
1125 // - drawing to screen (not printer)
1126 mUsingCache = !hasDataDefinedRotation && !hasDataDefinedSize && !context.renderContext().forceVectorOutput()
1130
1131 if ( mUsingCache )
1132 mCachedOpacity = context.opacity();
1133
1134 if ( !shapeIsFilled( mShape ) )
1135 {
1136 // some markers can't be drawn as a polygon (circle, cross)
1137 // For these set the selected stroke color to the selected color
1138 mSelPen.setColor( selBrushColor );
1139 }
1140
1141
1142 if ( mUsingCache )
1143 {
1144 if ( !prepareCache( context ) )
1145 {
1146 mUsingCache = false;
1147 }
1148 }
1149 else
1150 {
1151 mCache = QImage();
1152 mSelCache = QImage();
1153 }
1154}
1155
1156
1158{
1159 double scaledSize = context.renderContext().convertToPainterUnits( mSize, mSizeUnit, mSizeMapUnitScale );
1160 const double deviceRatio = context.renderContext().devicePixelRatio();
1162 {
1163 // rendering for symbol previews -- a size in meters in map units can't be calculated, so treat the size as millimeters
1164 // and clamp it to a reasonable range. It's the best we can do in this situation!
1165 scaledSize = std::min( std::max( context.renderContext().convertToPainterUnits( mSize, Qgis::RenderUnit::Millimeters ), 3.0 ), 100.0 );
1166 }
1167
1168 // take into account angle (which is not data-defined otherwise cache wouldn't be used)
1169 if ( !qgsDoubleNear( mAngle, 0.0 ) )
1170 {
1171 scaledSize = ( std::abs( std::sin( mAngle * M_PI / 180 ) ) + std::abs( std::cos( mAngle * M_PI / 180 ) ) ) * scaledSize;
1172 }
1173 // calculate necessary image size for the cache
1174 const double pw = static_cast< int >( std::round( ( ( qgsDoubleNear( mPen.widthF(), 0.0 ) ? 1 : mPen.widthF() * 4 ) + 1 ) ) ) / 2 * 2; // make even (round up); handle cosmetic pen
1175 const int imageSize = ( static_cast< int >( scaledSize ) + pw ) / 2 * 2 + 1; // make image width, height odd; account for pen width
1176 const double center = imageSize / 2.0;
1177 if ( imageSize * deviceRatio > MAXIMUM_CACHE_WIDTH )
1178 {
1179 return false;
1180 }
1181
1182 mCache = QImage( QSize( imageSize * deviceRatio,
1183 imageSize * deviceRatio ), QImage::Format_ARGB32_Premultiplied );
1184 mCache.setDevicePixelRatio( context.renderContext().devicePixelRatio() );
1185 mCache.setDotsPerMeterX( std::round( context.renderContext().scaleFactor() * 1000 ) );
1186 mCache.setDotsPerMeterY( std::round( context.renderContext().scaleFactor() * 1000 ) );
1187 mCache.fill( 0 );
1188
1189 const bool needsBrush = shapeIsFilled( mShape );
1190
1191 QPainter p;
1192 p.begin( &mCache );
1193 p.setRenderHint( QPainter::Antialiasing );
1194 p.setBrush( needsBrush ? mBrush : Qt::NoBrush );
1195 p.setPen( mPen );
1196 p.translate( QPointF( center, center ) );
1197 drawMarker( &p, context );
1198 p.end();
1199
1200 // Construct the selected version of the Cache
1201
1202 const QColor selColor = context.renderContext().selectionColor();
1203
1204 mSelCache = QImage( QSize( imageSize, imageSize ), QImage::Format_ARGB32_Premultiplied );
1205 mSelCache.fill( 0 );
1206
1207 p.begin( &mSelCache );
1208 p.setRenderHint( QPainter::Antialiasing );
1209 p.setBrush( needsBrush ? mSelBrush : Qt::NoBrush );
1210 p.setPen( mSelPen );
1211 p.translate( QPointF( center, center ) );
1212 drawMarker( &p, context );
1213 p.end();
1214
1215 // Check that the selected version is different. If not, then re-render,
1216 // filling the background with the selection color and using the normal
1217 // colors for the symbol .. could be ugly!
1218
1219 if ( mSelCache == mCache )
1220 {
1221 p.begin( &mSelCache );
1222 p.setRenderHint( QPainter::Antialiasing );
1223 p.fillRect( 0, 0, imageSize, imageSize, selColor );
1224 p.setBrush( needsBrush ? mBrush : Qt::NoBrush );
1225 p.setPen( mPen );
1226 p.translate( QPointF( center, center ) );
1227 drawMarker( &p, context );
1228 p.end();
1229 }
1230
1231 return true;
1232}
1233
1234void QgsSimpleMarkerSymbolLayer::draw( QgsSymbolRenderContext &context, Qgis::MarkerShape shape, const QPolygonF &polygon, const QPainterPath &path )
1235{
1236 //making changes here? Don't forget to also update ::bounds if the changes affect the bounding box
1237 //of the rendered point!
1238
1239 QPainter *p = context.renderContext().painter();
1240 if ( !p )
1241 {
1242 return;
1243 }
1244
1245 QColor brushColor = mColor;
1246 brushColor.setAlphaF( brushColor.alphaF() * context.opacity() );
1247 mBrush.setColor( brushColor );
1248
1249 QColor penColor = mStrokeColor;
1250 penColor.setAlphaF( penColor.alphaF() * context.opacity() );
1251 mPen.setColor( penColor );
1252
1253 bool ok = true;
1255 {
1258 if ( ok )
1259 {
1260 c.setAlphaF( c.alphaF() * context.opacity() );
1261 mBrush.setColor( c );
1262 }
1263 }
1265 {
1268 if ( ok )
1269 {
1270 c.setAlphaF( c.alphaF() * context.opacity() );
1271 mPen.setColor( c );
1272 mSelPen.setColor( c );
1273 }
1274 }
1276 {
1279 if ( ok )
1280 {
1283 }
1284 }
1286 {
1289 if ( ok )
1290 {
1293 }
1294 }
1296 {
1299 if ( ok )
1300 {
1301 mPen.setJoinStyle( QgsSymbolLayerUtils::decodePenJoinStyle( style ) );
1302 mSelPen.setJoinStyle( QgsSymbolLayerUtils::decodePenJoinStyle( style ) );
1303 }
1304 }
1306 {
1308 const QString style = mDataDefinedProperties.valueAsString( QgsSymbolLayer::PropertyCapStyle, context.renderContext().expressionContext(), QString(), &ok );
1309 if ( ok )
1310 {
1311 mPen.setCapStyle( QgsSymbolLayerUtils::decodePenCapStyle( style ) );
1312 mSelPen.setCapStyle( QgsSymbolLayerUtils::decodePenCapStyle( style ) );
1313 }
1314 }
1315
1316 const bool useSelectedColor = shouldRenderUsingSelectionColor( context );
1317 if ( shapeIsFilled( shape ) )
1318 {
1319 p->setBrush( useSelectedColor ? mSelBrush : mBrush );
1320 }
1321 else
1322 {
1323 p->setBrush( Qt::NoBrush );
1324 }
1325 p->setPen( useSelectedColor ? mSelPen : mPen );
1326
1327 if ( !polygon.isEmpty() )
1328 p->drawPolygon( polygon );
1329 else
1330 p->drawPath( path );
1331}
1332
1334{
1335 //making changes here? Don't forget to also update ::bounds if the changes affect the bounding box
1336 //of the rendered point!
1337
1338 QPainter *p = context.renderContext().painter();
1339 if ( !p )
1340 {
1341 return;
1342 }
1343
1344 if ( mUsingCache && qgsDoubleNear( mCachedOpacity, context.opacity() ) )
1345 {
1346 const bool useSelectedColor = shouldRenderUsingSelectionColor( context );
1347 const QImage &img = useSelectedColor ? mSelCache : mCache;
1348 const double s = img.width() / img.devicePixelRatioF();
1349
1350 bool hasDataDefinedSize = false;
1351 const double scaledSize = calculateSize( context, hasDataDefinedSize );
1352
1353 bool hasDataDefinedRotation = false;
1354 QPointF offset;
1355 double angle = 0;
1356 calculateOffsetAndRotation( context, scaledSize, hasDataDefinedRotation, offset, angle );
1357
1358 p->drawImage( QRectF( point.x() - s / 2.0 + offset.x(),
1359 point.y() - s / 2.0 + offset.y(),
1360 s, s ), img );
1361 }
1362 else
1363 {
1365 }
1366}
1367
1369{
1370 QVariantMap map;
1371 map[QStringLiteral( "name" )] = encodeShape( mShape );
1372 map[QStringLiteral( "color" )] = QgsSymbolLayerUtils::encodeColor( mColor );
1373 map[QStringLiteral( "outline_color" )] = QgsSymbolLayerUtils::encodeColor( mStrokeColor );
1374 map[QStringLiteral( "size" )] = QString::number( mSize );
1375 map[QStringLiteral( "size_unit" )] = QgsUnitTypes::encodeUnit( mSizeUnit );
1376 map[QStringLiteral( "size_map_unit_scale" )] = QgsSymbolLayerUtils::encodeMapUnitScale( mSizeMapUnitScale );
1377 map[QStringLiteral( "angle" )] = QString::number( mAngle );
1378 map[QStringLiteral( "offset" )] = QgsSymbolLayerUtils::encodePoint( mOffset );
1379 map[QStringLiteral( "offset_unit" )] = QgsUnitTypes::encodeUnit( mOffsetUnit );
1380 map[QStringLiteral( "offset_map_unit_scale" )] = QgsSymbolLayerUtils::encodeMapUnitScale( mOffsetMapUnitScale );
1381 map[QStringLiteral( "scale_method" )] = QgsSymbolLayerUtils::encodeScaleMethod( mScaleMethod );
1382 map[QStringLiteral( "outline_style" )] = QgsSymbolLayerUtils::encodePenStyle( mStrokeStyle );
1383 map[QStringLiteral( "outline_width" )] = QString::number( mStrokeWidth );
1384 map[QStringLiteral( "outline_width_unit" )] = QgsUnitTypes::encodeUnit( mStrokeWidthUnit );
1385 map[QStringLiteral( "outline_width_map_unit_scale" )] = QgsSymbolLayerUtils::encodeMapUnitScale( mStrokeWidthMapUnitScale );
1386 map[QStringLiteral( "joinstyle" )] = QgsSymbolLayerUtils::encodePenJoinStyle( mPenJoinStyle );
1387 map[QStringLiteral( "cap_style" )] = QgsSymbolLayerUtils::encodePenCapStyle( mPenCapStyle );
1388 map[QStringLiteral( "horizontal_anchor_point" )] = QString::number( mHorizontalAnchorPoint );
1389 map[QStringLiteral( "vertical_anchor_point" )] = QString::number( mVerticalAnchorPoint );
1390 return map;
1391}
1392
1412
1413void QgsSimpleMarkerSymbolLayer::writeSldMarker( QDomDocument &doc, QDomElement &element, const QVariantMap &props ) const
1414{
1415 // <Graphic>
1416 QDomElement graphicElem = doc.createElement( QStringLiteral( "se:Graphic" ) );
1417 element.appendChild( graphicElem );
1418
1420 const double size = QgsSymbolLayerUtils::rescaleUom( mSize, mSizeUnit, props );
1422
1423 // <Rotation>
1424 QString angleFunc;
1425
1427 {
1429 }
1430 else
1431 {
1432 bool ok;
1433 const double angle = props.value( QStringLiteral( "angle" ), QStringLiteral( "0" ) ).toDouble( &ok );
1434 if ( !ok )
1435 {
1436 angleFunc = QStringLiteral( "%1 + %2" ).arg( props.value( QStringLiteral( "angle" ), QStringLiteral( "0" ) ).toString() ).arg( mAngle );
1437 }
1438 else if ( !qgsDoubleNear( angle + mAngle, 0.0 ) )
1439 {
1440 angleFunc = QString::number( angle + mAngle );
1441 }
1442 }
1443
1444 QgsSymbolLayerUtils::createRotationElement( doc, graphicElem, angleFunc );
1445
1446 // <Displacement>
1447 const QPointF offset = QgsSymbolLayerUtils::rescaleUom( mOffset, mOffsetUnit, props );
1449}
1450
1451QString QgsSimpleMarkerSymbolLayer::ogrFeatureStyle( double mmScaleFactor, double mapUnitScaleFactor ) const
1452{
1453 Q_UNUSED( mmScaleFactor )
1454 Q_UNUSED( mapUnitScaleFactor )
1455#if 0
1456 QString ogrType = "3"; //default is circle
1457 if ( mName == "square" )
1458 {
1459 ogrType = "5";
1460 }
1461 else if ( mName == "triangle" )
1462 {
1463 ogrType = "7";
1464 }
1465 else if ( mName == "star" )
1466 {
1467 ogrType = "9";
1468 }
1469 else if ( mName == "circle" )
1470 {
1471 ogrType = "3";
1472 }
1473 else if ( mName == "cross" )
1474 {
1475 ogrType = "0";
1476 }
1477 else if ( mName == "x" || mName == "cross2" )
1478 {
1479 ogrType = "1";
1480 }
1481 else if ( mName == "line" )
1482 {
1483 ogrType = "10";
1484 }
1485
1486 QString ogrString;
1487 ogrString.append( "SYMBOL(" );
1488 ogrString.append( "id:" );
1489 ogrString.append( '\"' );
1490 ogrString.append( "ogr-sym-" );
1491 ogrString.append( ogrType );
1492 ogrString.append( '\"' );
1493 ogrString.append( ",c:" );
1494 ogrString.append( mColor.name() );
1495 ogrString.append( ",o:" );
1496 ogrString.append( mStrokeColor.name() );
1497 ogrString.append( QString( ",s:%1mm" ).arg( mSize ) );
1498 ogrString.append( ')' );
1499 return ogrString;
1500#endif //0
1501
1502 QString ogrString;
1503 ogrString.append( "PEN(" );
1504 ogrString.append( "c:" );
1505 ogrString.append( mColor.name() );
1506 ogrString.append( ",w:" );
1507 ogrString.append( QString::number( mSize ) );
1508 ogrString.append( "mm" );
1509 ogrString.append( ")" );
1510 return ogrString;
1511}
1512
1514{
1515 QgsDebugMsgLevel( QStringLiteral( "Entered." ), 4 );
1516
1517 QDomElement graphicElem = element.firstChildElement( QStringLiteral( "Graphic" ) );
1518 if ( graphicElem.isNull() )
1519 return nullptr;
1520
1521 QString name = QStringLiteral( "square" );
1522 QColor color, strokeColor;
1523 double strokeWidth, size;
1524 Qt::PenStyle strokeStyle;
1525
1527 return nullptr;
1528
1529 double angle = 0.0;
1530 QString angleFunc;
1531 if ( QgsSymbolLayerUtils::rotationFromSldElement( graphicElem, angleFunc ) )
1532 {
1533 bool ok;
1534 const double d = angleFunc.toDouble( &ok );
1535 if ( ok )
1536 angle = d;
1537 }
1538
1539 QPointF offset;
1541
1542 const Qgis::MarkerShape shape = decodeShape( name );
1543
1544 double scaleFactor = 1.0;
1545 const QString uom = element.attribute( QStringLiteral( "uom" ) );
1546 Qgis::RenderUnit sldUnitSize = QgsSymbolLayerUtils::decodeSldUom( uom, &scaleFactor );
1547 size = size * scaleFactor;
1548 offset.setX( offset.x() * scaleFactor );
1549 offset.setY( offset.y() * scaleFactor );
1550
1552 m->setOutputUnit( sldUnitSize );
1553 m->setColor( color );
1555 m->setAngle( angle );
1556 m->setOffset( offset );
1559 return m;
1560}
1561
1563{
1564 Q_UNUSED( context )
1565
1566 if ( mPolygon.count() != 0 )
1567 {
1568 p->drawPolygon( mPolygon );
1569 }
1570 else
1571 {
1572 p->drawPath( mPath );
1573 }
1574}
1575
1576bool QgsSimpleMarkerSymbolLayer::writeDxf( QgsDxfExport &e, double mmMapUnitScaleFactor, const QString &layerName, QgsSymbolRenderContext &context, QPointF shift ) const
1577{
1578 //data defined size?
1579 double size = mSize;
1580
1581 const bool hasDataDefinedSize = mDataDefinedProperties.isActive( QgsSymbolLayer::PropertySize );
1582
1583 //data defined size
1584 bool ok = true;
1585 if ( hasDataDefinedSize )
1586 {
1588
1589 if ( ok )
1590 {
1591 switch ( mScaleMethod )
1592 {
1594 size = std::sqrt( size );
1595 break;
1597 break;
1598 }
1599 }
1600 }
1601
1603 {
1604 size *= mmMapUnitScaleFactor;
1605 }
1606
1608 {
1610 }
1611 const double halfSize = size / 2.0;
1612
1613 //strokeWidth
1614 double strokeWidth = mStrokeWidth;
1615
1617 {
1620 }
1623 {
1625 }
1626
1627 //color
1628 QColor pc = mPen.color();
1629 QColor bc = mBrush.color();
1631 {
1634 }
1636 {
1639 }
1640
1641 //offset
1642 double offsetX = 0;
1643 double offsetY = 0;
1644 markerOffset( context, offsetX, offsetY );
1645 offsetX *= context.renderContext().mapToPixel().mapUnitsPerPixel();
1646 offsetY *= context.renderContext().mapToPixel().mapUnitsPerPixel();
1647
1648
1649 QPointF off( offsetX, offsetY );
1650
1651 //angle
1652 double angle = mAngle + mLineAngle;
1654 {
1657 }
1658
1661 {
1663 const QString shapeName = mDataDefinedProperties.valueAsString( QgsSymbolLayer::PropertyName, context.renderContext().expressionContext(), QString(), &ok );
1664 if ( ok )
1665 {
1666 shape = decodeShape( shapeName, &ok );
1667 if ( !ok )
1668 shape = mShape;
1669 }
1670 }
1671
1672 if ( angle )
1673 off = _rotatedOffset( off, angle );
1674
1676
1677 QTransform t;
1678 t.translate( shift.x() + off.x(), shift.y() - off.y() );
1679
1680 if ( !qgsDoubleNear( angle, 0.0 ) )
1681 t.rotate( -angle );
1682
1683 QPolygonF polygon;
1684 if ( shapeToPolygon( shape, polygon ) )
1685 {
1686 t.scale( halfSize, -halfSize );
1687
1688 polygon = t.map( polygon );
1689
1691 p.reserve( polygon.size() );
1692 for ( int i = 0; i < polygon.size(); i++ )
1693 {
1694 p << QgsPoint( polygon[i] );
1695 }
1696
1697 if ( mBrush.style() != Qt::NoBrush )
1698 e.writePolygon( QgsRingSequence() << p, layerName, QStringLiteral( "SOLID" ), bc );
1699 if ( mPen.style() != Qt::NoPen )
1700 e.writePolyline( p, layerName, QStringLiteral( "CONTINUOUS" ), pc, strokeWidth );
1701 }
1702 else if ( shape == Qgis::MarkerShape::Circle )
1703 {
1704 shift += QPointF( off.x(), -off.y() );
1705 if ( mBrush.style() != Qt::NoBrush )
1706 e.writeFilledCircle( layerName, bc, QgsPoint( shift ), halfSize );
1707 if ( mPen.style() != Qt::NoPen )
1708 e.writeCircle( layerName, pc, QgsPoint( shift ), halfSize, QStringLiteral( "CONTINUOUS" ), strokeWidth );
1709 }
1710 else if ( shape == Qgis::MarkerShape::Line )
1711 {
1712 const QPointF pt1 = t.map( QPointF( 0, -halfSize ) );
1713 const QPointF pt2 = t.map( QPointF( 0, halfSize ) );
1714
1715 if ( mPen.style() != Qt::NoPen )
1716 e.writeLine( QgsPoint( pt1 ), QgsPoint( pt2 ), layerName, QStringLiteral( "CONTINUOUS" ), pc, strokeWidth );
1717 }
1718 else if ( shape == Qgis::MarkerShape::Cross )
1719 {
1720 if ( mPen.style() != Qt::NoPen )
1721 {
1722 const QPointF pt1 = t.map( QPointF( -halfSize, 0 ) );
1723 const QPointF pt2 = t.map( QPointF( halfSize, 0 ) );
1724 const QPointF pt3 = t.map( QPointF( 0, -halfSize ) );
1725 const QPointF pt4 = t.map( QPointF( 0, halfSize ) );
1726
1727 e.writeLine( QgsPoint( pt1 ), QgsPoint( pt2 ), layerName, QStringLiteral( "CONTINUOUS" ), pc, strokeWidth );
1728 e.writeLine( QgsPoint( pt3 ), QgsPoint( pt4 ), layerName, QStringLiteral( "CONTINUOUS" ), pc, strokeWidth );
1729 }
1730 }
1731 else if ( shape == Qgis::MarkerShape::Cross2 )
1732 {
1733 if ( mPen.style() != Qt::NoPen )
1734 {
1735 const QPointF pt1 = t.map( QPointF( -halfSize, -halfSize ) );
1736 const QPointF pt2 = t.map( QPointF( halfSize, halfSize ) );
1737 const QPointF pt3 = t.map( QPointF( halfSize, -halfSize ) );
1738 const QPointF pt4 = t.map( QPointF( -halfSize, halfSize ) );
1739
1740 e.writeLine( QgsPoint( pt1 ), QgsPoint( pt2 ), layerName, QStringLiteral( "CONTINUOUS" ), pc, strokeWidth );
1741 e.writeLine( QgsPoint( pt3 ), QgsPoint( pt4 ), layerName, QStringLiteral( "CONTINUOUS" ), pc, strokeWidth );
1742 }
1743 }
1744 else if ( shape == Qgis::MarkerShape::ArrowHead )
1745 {
1746 if ( mPen.style() != Qt::NoPen )
1747 {
1748 const QPointF pt1 = t.map( QPointF( -halfSize, halfSize ) );
1749 const QPointF pt2 = t.map( QPointF( 0, 0 ) );
1750 const QPointF pt3 = t.map( QPointF( -halfSize, -halfSize ) );
1751
1752 e.writeLine( QgsPoint( pt1 ), QgsPoint( pt2 ), layerName, QStringLiteral( "CONTINUOUS" ), pc, strokeWidth );
1753 e.writeLine( QgsPoint( pt3 ), QgsPoint( pt2 ), layerName, QStringLiteral( "CONTINUOUS" ), pc, strokeWidth );
1754 }
1755 }
1756 else
1757 {
1758 QgsDebugError( QStringLiteral( "Unsupported dxf marker name %1" ).arg( encodeShape( shape ) ) );
1759 return false;
1760 }
1761
1762 return true;
1763}
1764
1765
1771
1780
1786
1795
1802
1804{
1805 QRectF symbolBounds = QgsSimpleMarkerSymbolLayerBase::bounds( point, context );
1806
1807 // need to account for stroke width
1808 double penWidth = mStrokeWidth;
1809 bool ok = true;
1811 {
1814 if ( ok )
1815 {
1816 penWidth = strokeWidth;
1817 }
1818 }
1821 {
1824 if ( ok && strokeStyle == QLatin1String( "no" ) )
1825 {
1826 penWidth = 0.0;
1827 }
1828 }
1829 else if ( mStrokeStyle == Qt::NoPen )
1830 penWidth = 0;
1831
1832 //antialiasing, add 1 pixel
1833 penWidth += 1;
1834
1835 //extend bounds by pen width / 2.0
1836 symbolBounds.adjust( -penWidth / 2.0, -penWidth / 2.0,
1837 penWidth / 2.0, penWidth / 2.0 );
1838
1839 return symbolBounds;
1840}
1841
1842void QgsSimpleMarkerSymbolLayer::setColor( const QColor &color )
1843{
1844 if ( shapeIsFilled( mShape ) )
1845 {
1847 }
1848 else
1849 {
1851 }
1852}
1853
1855{
1856 if ( shapeIsFilled( mShape ) )
1857 {
1858 return fillColor();
1859 }
1860 else
1861 {
1862 return strokeColor();
1863 }
1864}
1865
1866
1867
1868
1869//
1870// QgsFilledMarkerSymbolLayer
1871//
1872
1874 : QgsSimpleMarkerSymbolLayerBase( shape, size, angle, scaleMethod )
1875{
1876 mFill.reset( static_cast<QgsFillSymbol *>( QgsFillSymbol::createSimple( QVariantMap() ) ) );
1877}
1878
1880
1882{
1883 QString name = DEFAULT_SIMPLEMARKER_NAME;
1887
1888 if ( props.contains( QStringLiteral( "name" ) ) )
1889 name = props[QStringLiteral( "name" )].toString();
1890 if ( props.contains( QStringLiteral( "size" ) ) )
1891 size = props[QStringLiteral( "size" )].toDouble();
1892 if ( props.contains( QStringLiteral( "angle" ) ) )
1893 angle = props[QStringLiteral( "angle" )].toDouble();
1894 if ( props.contains( QStringLiteral( "scale_method" ) ) )
1895 scaleMethod = QgsSymbolLayerUtils::decodeScaleMethod( props[QStringLiteral( "scale_method" )].toString() );
1896
1898 if ( props.contains( QStringLiteral( "offset" ) ) )
1899 m->setOffset( QgsSymbolLayerUtils::decodePoint( props[QStringLiteral( "offset" )].toString() ) );
1900 if ( props.contains( QStringLiteral( "offset_unit" ) ) )
1901 m->setOffsetUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "offset_unit" )].toString() ) );
1902 if ( props.contains( QStringLiteral( "offset_map_unit_scale" ) ) )
1903 m->setOffsetMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( props[QStringLiteral( "offset_map_unit_scale" )].toString() ) );
1904 if ( props.contains( QStringLiteral( "size_unit" ) ) )
1905 m->setSizeUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "size_unit" )].toString() ) );
1906 if ( props.contains( QStringLiteral( "size_map_unit_scale" ) ) )
1907 m->setSizeMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( props[QStringLiteral( "size_map_unit_scale" )].toString() ) );
1908 if ( props.contains( QStringLiteral( "horizontal_anchor_point" ) ) )
1909 {
1910 m->setHorizontalAnchorPoint( QgsMarkerSymbolLayer::HorizontalAnchorPoint( props[ QStringLiteral( "horizontal_anchor_point" )].toInt() ) );
1911 }
1912 if ( props.contains( QStringLiteral( "vertical_anchor_point" ) ) )
1913 {
1914 m->setVerticalAnchorPoint( QgsMarkerSymbolLayer::VerticalAnchorPoint( props[ QStringLiteral( "vertical_anchor_point" )].toInt() ) );
1915 }
1916
1918
1920
1921 return m;
1922}
1923
1925{
1926 return QStringLiteral( "FilledMarker" );
1927}
1928
1930{
1931 if ( mFill )
1932 {
1933 mFill->startRender( context.renderContext(), context.fields() );
1934 }
1935
1937}
1938
1940{
1941 if ( mFill )
1942 {
1943 mFill->stopRender( context.renderContext() );
1944 }
1945}
1946
1948{
1949 QVariantMap map;
1950 map[QStringLiteral( "name" )] = encodeShape( mShape );
1951 map[QStringLiteral( "size" )] = QString::number( mSize );
1952 map[QStringLiteral( "size_unit" )] = QgsUnitTypes::encodeUnit( mSizeUnit );
1953 map[QStringLiteral( "size_map_unit_scale" )] = QgsSymbolLayerUtils::encodeMapUnitScale( mSizeMapUnitScale );
1954 map[QStringLiteral( "angle" )] = QString::number( mAngle );
1955 map[QStringLiteral( "offset" )] = QgsSymbolLayerUtils::encodePoint( mOffset );
1956 map[QStringLiteral( "offset_unit" )] = QgsUnitTypes::encodeUnit( mOffsetUnit );
1957 map[QStringLiteral( "offset_map_unit_scale" )] = QgsSymbolLayerUtils::encodeMapUnitScale( mOffsetMapUnitScale );
1958 map[QStringLiteral( "scale_method" )] = QgsSymbolLayerUtils::encodeScaleMethod( mScaleMethod );
1959 map[QStringLiteral( "horizontal_anchor_point" )] = QString::number( mHorizontalAnchorPoint );
1960 map[QStringLiteral( "vertical_anchor_point" )] = QString::number( mVerticalAnchorPoint );
1961
1962 if ( mFill )
1963 {
1964 map[QStringLiteral( "color" )] = QgsSymbolLayerUtils::encodeColor( mFill->color() );
1965 }
1966 return map;
1967}
1968
1977
1979{
1980 return mFill.get();
1981}
1982
1984{
1985 if ( symbol && symbol->type() == Qgis::SymbolType::Fill )
1986 {
1987 mFill.reset( static_cast<QgsFillSymbol *>( symbol ) );
1988 return true;
1989 }
1990 else
1991 {
1992 delete symbol;
1993 return false;
1994 }
1995}
1996
1998{
1999 if ( mFill )
2000 {
2001 return QgsSymbolLayerUtils::estimateMaxSymbolBleed( mFill.get(), context );
2002 }
2003 return 0;
2004}
2005
2007{
2008 QSet<QString> attr = QgsSimpleMarkerSymbolLayerBase::usedAttributes( context );
2009 if ( mFill )
2010 attr.unite( mFill->usedAttributes( context ) );
2011 return attr;
2012}
2013
2015{
2017 return true;
2018 if ( mFill && mFill->hasDataDefinedProperties() )
2019 return true;
2020 return false;
2021}
2022
2024{
2025 mColor = c;
2026 if ( mFill )
2027 mFill->setColor( c );
2028}
2029
2031{
2032 return mFill ? mFill->color() : mColor;
2033}
2034
2041
2043{
2045 if ( mFill )
2046 mFill->setOutputUnit( unit );
2047}
2048
2049void QgsFilledMarkerSymbolLayer::draw( QgsSymbolRenderContext &context, Qgis::MarkerShape shape, const QPolygonF &polygon, const QPainterPath &path )
2050{
2051 //making changes here? Don't forget to also update ::bounds if the changes affect the bounding box
2052 //of the rendered point!
2053
2054 QPainter *p = context.renderContext().painter();
2055 if ( !p )
2056 {
2057 return;
2058 }
2059
2060 const double prevOpacity = mFill->opacity();
2061 mFill->setOpacity( mFill->opacity() * context.opacity() );
2062
2063 if ( shapeIsFilled( shape ) )
2064 {
2065 p->setBrush( Qt::red );
2066 }
2067 else
2068 {
2069 p->setBrush( Qt::NoBrush );
2070 }
2071 p->setPen( Qt::black );
2072
2073 const bool prevIsSubsymbol = context.renderContext().flags() & Qgis::RenderContextFlag::RenderingSubSymbol;
2075
2076 const bool useSelectedColor = shouldRenderUsingSelectionColor( context );
2077 if ( !polygon.isEmpty() )
2078 {
2079 mFill->renderPolygon( polygon, /* rings */ nullptr, context.feature(), context.renderContext(), -1, useSelectedColor );
2080 }
2081 else
2082 {
2083 const QPolygonF poly = path.toFillPolygon();
2084 mFill->renderPolygon( poly, /* rings */ nullptr, context.feature(), context.renderContext(), -1, useSelectedColor );
2085 }
2086
2088
2089 mFill->setOpacity( prevOpacity );
2090}
2091
2092
2094
2095
2096QgsSvgMarkerSymbolLayer::QgsSvgMarkerSymbolLayer( const QString &path, double size, double angle, Qgis::ScaleMethod scaleMethod )
2097{
2098 mSize = size;
2099 mAngle = angle;
2100 mOffset = QPointF( 0, 0 );
2102 mStrokeWidth = 0.2;
2104 mColor = QColor( 35, 35, 35 );
2105 mStrokeColor = QColor( 35, 35, 35 );
2106 setPath( path );
2107}
2108
2110
2112{
2113 QString name;
2117
2118 if ( props.contains( QStringLiteral( "name" ) ) )
2119 name = props[QStringLiteral( "name" )].toString();
2120 if ( props.contains( QStringLiteral( "size" ) ) )
2121 size = props[QStringLiteral( "size" )].toDouble();
2122 if ( props.contains( QStringLiteral( "angle" ) ) )
2123 angle = props[QStringLiteral( "angle" )].toDouble();
2124 if ( props.contains( QStringLiteral( "scale_method" ) ) )
2125 scaleMethod = QgsSymbolLayerUtils::decodeScaleMethod( props[QStringLiteral( "scale_method" )].toString() );
2126
2128
2129 if ( props.contains( QStringLiteral( "size_unit" ) ) )
2130 m->setSizeUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "size_unit" )].toString() ) );
2131 if ( props.contains( QStringLiteral( "size_map_unit_scale" ) ) )
2132 m->setSizeMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( props[QStringLiteral( "size_map_unit_scale" )].toString() ) );
2133 if ( props.contains( QStringLiteral( "fixedAspectRatio" ) ) )
2134 m->setFixedAspectRatio( props[QStringLiteral( "fixedAspectRatio" )].toDouble() );
2135 if ( props.contains( QStringLiteral( "offset" ) ) )
2136 m->setOffset( QgsSymbolLayerUtils::decodePoint( props[QStringLiteral( "offset" )].toString() ) );
2137 if ( props.contains( QStringLiteral( "offset_unit" ) ) )
2138 m->setOffsetUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "offset_unit" )].toString() ) );
2139 if ( props.contains( QStringLiteral( "offset_map_unit_scale" ) ) )
2140 m->setOffsetMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( props[QStringLiteral( "offset_map_unit_scale" )].toString() ) );
2141 if ( props.contains( QStringLiteral( "fill" ) ) )
2142 {
2143 //pre 2.5 projects used "fill"
2144 m->setFillColor( QgsSymbolLayerUtils::decodeColor( props[QStringLiteral( "fill" )].toString() ) );
2145 }
2146 else if ( props.contains( QStringLiteral( "color" ) ) )
2147 {
2148 m->setFillColor( QgsSymbolLayerUtils::decodeColor( props[QStringLiteral( "color" )].toString() ) );
2149 }
2150 if ( props.contains( QStringLiteral( "outline" ) ) )
2151 {
2152 //pre 2.5 projects used "outline"
2153 m->setStrokeColor( QgsSymbolLayerUtils::decodeColor( props[QStringLiteral( "outline" )].toString() ) );
2154 }
2155 else if ( props.contains( QStringLiteral( "outline_color" ) ) )
2156 {
2157 m->setStrokeColor( QgsSymbolLayerUtils::decodeColor( props[QStringLiteral( "outline_color" )].toString() ) );
2158 }
2159 else if ( props.contains( QStringLiteral( "line_color" ) ) )
2160 {
2161 m->setStrokeColor( QgsSymbolLayerUtils::decodeColor( props[QStringLiteral( "line_color" )].toString() ) );
2162 }
2163
2164 if ( props.contains( QStringLiteral( "outline-width" ) ) )
2165 {
2166 //pre 2.5 projects used "outline-width"
2167 m->setStrokeWidth( props[QStringLiteral( "outline-width" )].toDouble() );
2168 }
2169 else if ( props.contains( QStringLiteral( "outline_width" ) ) )
2170 {
2171 m->setStrokeWidth( props[QStringLiteral( "outline_width" )].toDouble() );
2172 }
2173 else if ( props.contains( QStringLiteral( "line_width" ) ) )
2174 {
2175 m->setStrokeWidth( props[QStringLiteral( "line_width" )].toDouble() );
2176 }
2177
2178 if ( props.contains( QStringLiteral( "outline_width_unit" ) ) )
2179 {
2180 m->setStrokeWidthUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "outline_width_unit" )].toString() ) );
2181 }
2182 else if ( props.contains( QStringLiteral( "line_width_unit" ) ) )
2183 {
2184 m->setStrokeWidthUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "line_width_unit" )].toString() ) );
2185 }
2186 if ( props.contains( QStringLiteral( "outline_width_map_unit_scale" ) ) )
2187 m->setStrokeWidthMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( props[QStringLiteral( "outline_width_map_unit_scale" )].toString() ) );
2188
2189 if ( props.contains( QStringLiteral( "horizontal_anchor_point" ) ) )
2190 {
2191 m->setHorizontalAnchorPoint( QgsMarkerSymbolLayer::HorizontalAnchorPoint( props[ QStringLiteral( "horizontal_anchor_point" )].toInt() ) );
2192 }
2193 if ( props.contains( QStringLiteral( "vertical_anchor_point" ) ) )
2194 {
2195 m->setVerticalAnchorPoint( QgsMarkerSymbolLayer::VerticalAnchorPoint( props[ QStringLiteral( "vertical_anchor_point" )].toInt() ) );
2196 }
2197
2199
2201
2202 if ( props.contains( QStringLiteral( "parameters" ) ) )
2203 {
2204 const QVariantMap parameters = props[QStringLiteral( "parameters" )].toMap();
2206 }
2207
2208 return m;
2209}
2210
2211void QgsSvgMarkerSymbolLayer::resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving )
2212{
2213 const QVariantMap::iterator it = properties.find( QStringLiteral( "name" ) );
2214 if ( it != properties.end() )
2215 {
2216 if ( saving )
2217 {
2218 it.value() = QgsSymbolLayerUtils::svgSymbolPathToName( it.value().toString(), pathResolver );
2219 }
2220 else
2221 {
2222 it.value() = QgsSymbolLayerUtils::svgSymbolNameToPath( it.value().toString(), pathResolver );
2223 }
2224 }
2225}
2226
2227void QgsSvgMarkerSymbolLayer::setPath( const QString &path )
2228{
2230 mHasFillParam = false;
2231 mPath = path;
2232 QColor defaultFillColor, defaultStrokeColor;
2233 double strokeWidth, fillOpacity, strokeOpacity;
2234 bool hasFillOpacityParam = false, hasStrokeParam = false, hasStrokeWidthParam = false, hasStrokeOpacityParam = false;
2235 bool hasDefaultFillColor = false, hasDefaultFillOpacity = false, hasDefaultStrokeColor = false, hasDefaultStrokeWidth = false, hasDefaultStrokeOpacity = false;
2236 QgsApplication::svgCache()->containsParams( path, mHasFillParam, hasDefaultFillColor, defaultFillColor,
2237 hasFillOpacityParam, hasDefaultFillOpacity, fillOpacity,
2238 hasStrokeParam, hasDefaultStrokeColor, defaultStrokeColor,
2239 hasStrokeWidthParam, hasDefaultStrokeWidth, strokeWidth,
2240 hasStrokeOpacityParam, hasDefaultStrokeOpacity, strokeOpacity );
2241
2242 const double newFillOpacity = hasFillOpacityParam ? fillColor().alphaF() : 1.0;
2243 const double newStrokeOpacity = hasStrokeOpacityParam ? strokeColor().alphaF() : 1.0;
2244
2245 if ( hasDefaultFillColor )
2246 {
2247 defaultFillColor.setAlphaF( newFillOpacity );
2248 setFillColor( defaultFillColor );
2249 }
2250 if ( hasDefaultFillOpacity )
2251 {
2252 QColor c = fillColor();
2253 c.setAlphaF( fillOpacity );
2254 setFillColor( c );
2255 }
2256 if ( hasDefaultStrokeColor )
2257 {
2258 defaultStrokeColor.setAlphaF( newStrokeOpacity );
2259 setStrokeColor( defaultStrokeColor );
2260 }
2261 if ( hasDefaultStrokeWidth )
2262 {
2264 }
2265 if ( hasDefaultStrokeOpacity )
2266 {
2267 QColor c = strokeColor();
2268 c.setAlphaF( strokeOpacity );
2269 setStrokeColor( c );
2270 }
2271
2273}
2274
2276{
2277 if ( mDefaultAspectRatio == 0.0 )
2278 {
2279 //size
2280 const double size = mSize;
2281 //assume 88 dpi as standard value
2282 const double widthScaleFactor = 3.465;
2283 const QSizeF svgViewbox = QgsApplication::svgCache()->svgViewboxSize( mPath, size, mColor, mStrokeColor, mStrokeWidth, widthScaleFactor );
2284 // set default aspect ratio
2285 mDefaultAspectRatio = svgViewbox.isValid() ? svgViewbox.height() / svgViewbox.width() : 0.0;
2286 }
2287 return mDefaultAspectRatio;
2288}
2289
2291{
2292 const bool aPreservedAspectRatio = preservedAspectRatio();
2293 if ( aPreservedAspectRatio && !par )
2294 {
2296 }
2297 else if ( !aPreservedAspectRatio && par )
2298 {
2299 mFixedAspectRatio = 0.0;
2300 }
2301 return preservedAspectRatio();
2302}
2303
2304void QgsSvgMarkerSymbolLayer::setParameters( const QMap<QString, QgsProperty> &parameters )
2305{
2307}
2308
2309
2311{
2312 return QStringLiteral( "SvgMarker" );
2313}
2314
2316{
2317 QgsMarkerSymbolLayer::startRender( context ); // get anchor point expressions
2318 Q_UNUSED( context )
2319}
2320
2322{
2323 Q_UNUSED( context )
2324}
2325
2327{
2328 QPainter *p = context.renderContext().painter();
2329 if ( !p )
2330 return;
2331
2332 bool hasDataDefinedSize = false;
2333 const double scaledWidth = calculateSize( context, hasDataDefinedSize );
2334 const double devicePixelRatio = context.renderContext().devicePixelRatio();
2335 const double width = context.renderContext().convertToPainterUnits( scaledWidth, mSizeUnit, mSizeMapUnitScale );
2336
2337 //don't render symbols with a width below one or above 10,000 pixels
2338 if ( static_cast< int >( width ) < 1 || 10000.0 < width )
2339 {
2340 return;
2341 }
2342
2343 const QgsScopedQPainterState painterState( p );
2344
2345 bool hasDataDefinedAspectRatio = false;
2346 const double aspectRatio = calculateAspectRatio( context, scaledWidth, hasDataDefinedAspectRatio );
2347 double scaledHeight = scaledWidth * ( !qgsDoubleNear( aspectRatio, 0.0 ) ? aspectRatio : mDefaultAspectRatio );
2348
2350
2351 double strokeWidth = mStrokeWidth;
2353 {
2356 }
2358
2359 QColor fillColor = mColor;
2360 const bool useSelectedColor = shouldRenderUsingSelectionColor( context );
2361 if ( useSelectedColor && mHasFillParam )
2362 {
2364 }
2366 {
2369 }
2370
2371 QColor strokeColor = mStrokeColor;
2373 {
2376 }
2377
2378 QString path = mPath;
2380 {
2383 context.renderContext().pathResolver() );
2385 {
2386 // adjust height of data defined path
2387 const QSizeF svgViewbox = QgsApplication::svgCache()->svgViewboxSize( path, scaledWidth, fillColor, strokeColor, strokeWidth,
2388 context.renderContext().scaleFactor(), aspectRatio,
2389 ( context.renderContext().flags() & Qgis::RenderContextFlag::RenderBlocking ), evaluatedParameters );
2390 scaledHeight = svgViewbox.isValid() ? scaledWidth * svgViewbox.height() / svgViewbox.width() : scaledWidth;
2391 }
2392 }
2393
2394 QPointF outputOffset;
2395 double angle = 0.0;
2396 calculateOffsetAndRotation( context, scaledWidth, scaledHeight, outputOffset, angle );
2397
2398 p->translate( point + outputOffset );
2399
2400 const bool rotated = !qgsDoubleNear( angle, 0 );
2401 if ( rotated )
2402 p->rotate( angle );
2403
2404 bool fitsInCache = true;
2405 bool usePict = true;
2406 const bool rasterizeSelected = !mHasFillParam || mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyName );
2407 if ( ( !context.renderContext().forceVectorOutput() && !rotated ) || ( useSelectedColor && rasterizeSelected ) )
2408 {
2409 QImage img = QgsApplication::svgCache()->svgAsImage( path, width * devicePixelRatio, fillColor, strokeColor, strokeWidth,
2410 context.renderContext().scaleFactor(), fitsInCache, aspectRatio,
2411 ( context.renderContext().flags() & Qgis::RenderContextFlag::RenderBlocking ), evaluatedParameters );
2412 if ( fitsInCache && img.width() > 1 )
2413 {
2414 usePict = false;
2415
2416 if ( useSelectedColor )
2417 {
2419 }
2420
2421 //consider transparency
2422 if ( !qgsDoubleNear( context.opacity(), 1.0 ) )
2423 {
2424 QImage transparentImage = img.copy();
2425 QgsSymbolLayerUtils::multiplyImageOpacity( &transparentImage, context.opacity() );
2426 if ( devicePixelRatio == 1 )
2427 {
2428 p->drawImage( -transparentImage.width() / 2.0, -transparentImage.height() / 2.0, transparentImage );
2429 }
2430 else
2431 {
2432 p->drawImage( QRectF( -transparentImage.width() / 2.0 / devicePixelRatio, -transparentImage.height() / 2.0 / devicePixelRatio,
2433 transparentImage.width() / devicePixelRatio, transparentImage.height() / devicePixelRatio
2434 ), transparentImage );
2435 }
2436 }
2437 else
2438 {
2439 if ( devicePixelRatio == 1 )
2440 {
2441 p->drawImage( -img.width() / 2.0, -img.height() / 2.0, img );
2442 }
2443 else
2444 {
2445 p->drawImage( QRectF( -img.width() / 2.0 / devicePixelRatio, -img.height() / 2.0 / devicePixelRatio,
2446 img.width() / devicePixelRatio, img.height() / devicePixelRatio ), img );
2447 }
2448 }
2449 }
2450 }
2451
2452 if ( usePict || !fitsInCache )
2453 {
2454 p->setOpacity( context.opacity() );
2456 context.renderContext().scaleFactor(), context.renderContext().forceVectorOutput(), aspectRatio,
2457 ( context.renderContext().flags() & Qgis::RenderContextFlag::RenderBlocking ), evaluatedParameters );
2458 if ( pct.width() > 1 )
2459 {
2460 const QgsScopedQPainterState painterPictureState( p );
2461 _fixQPictureDPI( p );
2462 p->drawPicture( 0, 0, pct );
2463 }
2464 }
2465
2466 // workaround issue with nested QPictures forgetting antialiasing flag - see https://github.com/qgis/QGIS/issues/22909
2468}
2469
2470double QgsSvgMarkerSymbolLayer::calculateSize( QgsSymbolRenderContext &context, bool &hasDataDefinedSize ) const
2471{
2472 double scaledSize = mSize;
2474
2475 bool ok = true;
2476 if ( hasDataDefinedSize )
2477 {
2480 }
2481 else
2482 {
2484 if ( hasDataDefinedSize )
2485 {
2488 }
2489 }
2490
2491 if ( hasDataDefinedSize && ok )
2492 {
2493 switch ( mScaleMethod )
2494 {
2496 scaledSize = std::sqrt( scaledSize );
2497 break;
2499 break;
2500 }
2501 }
2502
2503 return scaledSize;
2504}
2505
2506double QgsSvgMarkerSymbolLayer::calculateAspectRatio( QgsSymbolRenderContext &context, double scaledSize, bool &hasDataDefinedAspectRatio ) const
2507{
2509 if ( !hasDataDefinedAspectRatio )
2510 return mFixedAspectRatio;
2511
2513 return 0.0;
2514
2515 double scaledAspectRatio = mDefaultAspectRatio;
2516 if ( mFixedAspectRatio > 0.0 )
2517 scaledAspectRatio = mFixedAspectRatio;
2518
2519 const double defaultHeight = mSize * scaledAspectRatio;
2520 scaledAspectRatio = defaultHeight / scaledSize;
2521
2522 bool ok = true;
2523 double scaledHeight = scaledSize * scaledAspectRatio;
2525 {
2526 context.setOriginalValueVariable( defaultHeight );
2528 }
2529
2530 if ( hasDataDefinedAspectRatio && ok )
2531 {
2532 switch ( mScaleMethod )
2533 {
2535 scaledHeight = sqrt( scaledHeight );
2536 break;
2538 break;
2539 }
2540 }
2541
2542 scaledAspectRatio = scaledHeight / scaledSize;
2543
2544 return scaledAspectRatio;
2545}
2546
2547void QgsSvgMarkerSymbolLayer::calculateOffsetAndRotation( QgsSymbolRenderContext &context, double scaledWidth, double scaledHeight, QPointF &offset, double &angle ) const
2548{
2549 //offset
2550 double offsetX = 0;
2551 double offsetY = 0;
2552 markerOffset( context, scaledWidth, scaledHeight, offsetX, offsetY );
2553 offset = QPointF( offsetX, offsetY );
2554
2557 {
2560 }
2561
2563 if ( hasDataDefinedRotation )
2564 {
2565 // For non-point markers, "dataDefinedRotation" means following the
2566 // shape (shape-data defined). For them, "field-data defined" does
2567 // not work at all. TODO: if "field-data defined" ever gets implemented
2568 // we'll need a way to distinguish here between the two, possibly
2569 // using another flag in renderHints()
2570 const QgsFeature *f = context.feature();
2571 if ( f )
2572 {
2573 if ( f->hasGeometry() && f->geometry().type() == Qgis::GeometryType::Point )
2574 {
2575 const QgsMapToPixel &m2p = context.renderContext().mapToPixel();
2576 angle += m2p.mapRotation();
2577 }
2578 }
2579 }
2580
2581 if ( angle )
2583}
2584
2585
2587{
2588 QVariantMap map;
2589 map[QStringLiteral( "name" )] = mPath;
2590 map[QStringLiteral( "size" )] = QString::number( mSize );
2591 map[QStringLiteral( "size_unit" )] = QgsUnitTypes::encodeUnit( mSizeUnit );
2592 map[QStringLiteral( "size_map_unit_scale" )] = QgsSymbolLayerUtils::encodeMapUnitScale( mSizeMapUnitScale );
2593 map[QStringLiteral( "fixedAspectRatio" )] = QString::number( mFixedAspectRatio );
2594 map[QStringLiteral( "angle" )] = QString::number( mAngle );
2595 map[QStringLiteral( "offset" )] = QgsSymbolLayerUtils::encodePoint( mOffset );
2596 map[QStringLiteral( "offset_unit" )] = QgsUnitTypes::encodeUnit( mOffsetUnit );
2597 map[QStringLiteral( "offset_map_unit_scale" )] = QgsSymbolLayerUtils::encodeMapUnitScale( mOffsetMapUnitScale );
2598 map[QStringLiteral( "scale_method" )] = QgsSymbolLayerUtils::encodeScaleMethod( mScaleMethod );
2599 map[QStringLiteral( "color" )] = QgsSymbolLayerUtils::encodeColor( mColor );
2600 map[QStringLiteral( "outline_color" )] = QgsSymbolLayerUtils::encodeColor( mStrokeColor );
2601 map[QStringLiteral( "outline_width" )] = QString::number( mStrokeWidth );
2602 map[QStringLiteral( "outline_width_unit" )] = QgsUnitTypes::encodeUnit( mStrokeWidthUnit );
2603 map[QStringLiteral( "outline_width_map_unit_scale" )] = QgsSymbolLayerUtils::encodeMapUnitScale( mStrokeWidthMapUnitScale );
2604 map[QStringLiteral( "horizontal_anchor_point" )] = QString::number( mHorizontalAnchorPoint );
2605 map[QStringLiteral( "vertical_anchor_point" )] = QString::number( mVerticalAnchorPoint );
2606
2607 map[QStringLiteral( "parameters" )] = QgsProperty::propertyMapToVariantMap( mParameters );
2608
2609 return map;
2610}
2611
2618
2641
2647
2649{
2651 if ( unit != mStrokeWidthUnit )
2652 {
2654 }
2655 return unit;
2656}
2657
2663
2672
2673void QgsSvgMarkerSymbolLayer::writeSldMarker( QDomDocument &doc, QDomElement &element, const QVariantMap &props ) const
2674{
2675 // <Graphic>
2676 QDomElement graphicElem = doc.createElement( QStringLiteral( "se:Graphic" ) );
2677 element.appendChild( graphicElem );
2678
2679 // encode a parametric SVG reference
2680 const double size = QgsSymbolLayerUtils::rescaleUom( mSize, mSizeUnit, props );
2683
2684 // <Rotation>
2685 QString angleFunc;
2686 bool ok;
2687 const double angle = props.value( QStringLiteral( "angle" ), QStringLiteral( "0" ) ).toDouble( &ok );
2688 if ( !ok )
2689 {
2690 angleFunc = QStringLiteral( "%1 + %2" ).arg( props.value( QStringLiteral( "angle" ), QStringLiteral( "0" ) ).toString() ).arg( mAngle );
2691 }
2692 else if ( !qgsDoubleNear( angle + mAngle, 0.0 ) )
2693 {
2694 angleFunc = QString::number( angle + mAngle );
2695 }
2696
2697 QgsSymbolLayerUtils::createRotationElement( doc, graphicElem, angleFunc );
2698
2699 // <Displacement>
2700 const QPointF offset = QgsSymbolLayerUtils::rescaleUom( mOffset, mOffsetUnit, props );
2702}
2703
2705{
2706 QgsDebugMsgLevel( QStringLiteral( "Entered." ), 4 );
2707
2708 QDomElement graphicElem = element.firstChildElement( QStringLiteral( "Graphic" ) );
2709 if ( graphicElem.isNull() )
2710 return nullptr;
2711
2712 QString path, mimeType;
2713 // Unused and to be DEPRECATED in externalGraphicFromSld
2714 QColor fillColor_;
2715 double size;
2716
2717 if ( !QgsSymbolLayerUtils::externalGraphicFromSld( graphicElem, path, mimeType, fillColor_, size ) )
2718 return nullptr;
2719
2720 double scaleFactor = 1.0;
2721 const QString uom = element.attribute( QStringLiteral( "uom" ) );
2722 Qgis::RenderUnit sldUnitSize = QgsSymbolLayerUtils::decodeSldUom( uom, &scaleFactor );
2723 size = size * scaleFactor;
2724
2725 if ( mimeType != QLatin1String( "image/svg+xml" ) )
2726 return nullptr;
2727
2728 double angle = 0.0;
2729 QString angleFunc;
2730 if ( QgsSymbolLayerUtils::rotationFromSldElement( graphicElem, angleFunc ) )
2731 {
2732 bool ok;
2733 const double d = angleFunc.toDouble( &ok );
2734 if ( ok )
2735 angle = d;
2736 }
2737
2738 QPointF offset;
2740
2741 // Extract parameters from URL
2742 QString realPath { path };
2743 QUrl svgUrl { path };
2744
2745 // Because color definition can start with '#', the url parsing won't recognize the query string entirely
2746 QUrlQuery queryString;
2747
2748 if ( svgUrl.hasQuery() && svgUrl.hasFragment() )
2749 {
2750 const QString queryPart { path.mid( path.indexOf( '?' ) + 1 ) };
2751 queryString.setQuery( queryPart );
2752 }
2753
2754 // Remove query for simple file paths
2755 if ( svgUrl.scheme().isEmpty() || svgUrl.isLocalFile() )
2756 {
2757 svgUrl.setQuery( QString() );
2758 realPath = svgUrl.path();
2759 }
2760
2762
2763 QMap<QString, QgsProperty> params;
2764
2765 bool ok;
2766
2767 if ( queryString.hasQueryItem( QStringLiteral( "fill" ) ) )
2768 {
2769 const QColor fillColor { queryString.queryItemValue( QStringLiteral( "fill" ) ) };
2770 m->setFillColor( fillColor );
2771 }
2772
2773 if ( queryString.hasQueryItem( QStringLiteral( "fill-opacity" ) ) )
2774 {
2775 const double alpha { queryString.queryItemValue( QStringLiteral( "fill-opacity" ) ).toDouble( &ok ) };
2776 if ( ok )
2777 {
2778 params.insert( QStringLiteral( "fill-opacity" ), QgsProperty::fromValue( alpha ) );
2779 }
2780 }
2781
2782 if ( queryString.hasQueryItem( QStringLiteral( "outline" ) ) )
2783 {
2784 const QColor strokeColor { queryString.queryItemValue( QStringLiteral( "outline" ) ) };
2786 }
2787
2788 if ( queryString.hasQueryItem( QStringLiteral( "outline-opacity" ) ) )
2789 {
2790 const double alpha { queryString.queryItemValue( QStringLiteral( "outline-opacity" ) ).toDouble( &ok ) };
2791 if ( ok )
2792 {
2793 params.insert( QStringLiteral( "outline-opacity" ), QgsProperty::fromValue( alpha ) );
2794 }
2795 }
2796
2797 if ( queryString.hasQueryItem( QStringLiteral( "outline-width" ) ) )
2798 {
2799 const int width { queryString.queryItemValue( QStringLiteral( "outline-width" ) ).toInt( &ok )};
2800 if ( ok )
2801 {
2802 m->setStrokeWidth( width );
2803 }
2804 }
2805
2806 if ( ! params.isEmpty() )
2807 {
2808 m->setParameters( params );
2809 }
2810
2811 m->setOutputUnit( sldUnitSize );
2812 m->setAngle( angle );
2813 m->setOffset( offset );
2814 return m;
2815}
2816
2817bool QgsSvgMarkerSymbolLayer::writeDxf( QgsDxfExport &e, double mmMapUnitScaleFactor, const QString &layerName, QgsSymbolRenderContext &context, QPointF shift ) const
2818{
2819 //size
2820 double size = mSize;
2821
2822 const bool hasDataDefinedSize = mDataDefinedProperties.isActive( QgsSymbolLayer::PropertySize );
2823
2824 bool ok = true;
2825 if ( hasDataDefinedSize )
2826 {
2829 }
2830
2831 if ( hasDataDefinedSize && ok )
2832 {
2833 switch ( mScaleMethod )
2834 {
2836 size = std::sqrt( size );
2837 break;
2839 break;
2840 }
2841 }
2842
2844 {
2845 size *= mmMapUnitScaleFactor;
2846 }
2847
2848 //offset, angle
2849 QPointF offset = mOffset;
2850
2852 {
2854 const QVariant val = mDataDefinedProperties.value( QgsSymbolLayer::PropertyOffset, context.renderContext().expressionContext(), QString() );
2855 const QPointF res = QgsSymbolLayerUtils::toPoint( val, &ok );
2856 if ( ok )
2857 offset = res;
2858 }
2859 const double offsetX = offset.x();
2860 const double offsetY = offset.y();
2861
2862 QPointF outputOffset( offsetX, offsetY );
2863
2864 double angle = mAngle + mLineAngle;
2866 {
2869 }
2870
2871 if ( angle )
2872 outputOffset = _rotatedOffset( outputOffset, angle );
2873
2875
2876 QString path = mPath;
2878 {
2881 context.renderContext().pathResolver() );
2882 }
2883
2884 double strokeWidth = mStrokeWidth;
2886 {
2889 }
2891
2892 QColor fillColor = mColor;
2894 {
2897 }
2898
2899 QColor strokeColor = mStrokeColor;
2901 {
2904 }
2905
2907
2908 const QByteArray &svgContent = QgsApplication::svgCache()->svgContent( path, size, fillColor, strokeColor, strokeWidth,
2910 ( context.renderContext().flags() & Qgis::RenderContextFlag::RenderBlocking ), evaluatedParameters );
2911
2912 QSvgRenderer r( svgContent );
2913 if ( !r.isValid() )
2914 return false;
2915
2916 QgsDxfPaintDevice pd( &e );
2917 pd.setDrawingSize( QSizeF( r.defaultSize() ) );
2918
2919 QSizeF outSize( r.defaultSize() );
2920 outSize.scale( size, size, Qt::KeepAspectRatio );
2921
2922 QPainter p;
2923 p.begin( &pd );
2924 if ( !qgsDoubleNear( angle, 0.0 ) )
2925 {
2926 p.translate( r.defaultSize().width() / 2.0, r.defaultSize().height() / 2.0 );
2927 p.rotate( angle );
2928 p.translate( -r.defaultSize().width() / 2.0, -r.defaultSize().height() / 2.0 );
2929 }
2930 pd.setShift( shift + QPointF( outputOffset.x(), -outputOffset.y() ) );
2931 pd.setOutputSize( QRectF( -outSize.width() / 2.0, -outSize.height() / 2.0, outSize.width(), outSize.height() ) );
2932 pd.setLayer( layerName );
2933 r.render( &p );
2934 p.end();
2935 return true;
2936}
2937
2939{
2940 bool hasDataDefinedSize = false;
2941 double scaledWidth = calculateSize( context, hasDataDefinedSize );
2942
2943 bool hasDataDefinedAspectRatio = false;
2944 const double aspectRatio = calculateAspectRatio( context, scaledWidth, hasDataDefinedAspectRatio );
2945 double scaledHeight = scaledWidth * ( !qgsDoubleNear( aspectRatio, 0.0 ) ? aspectRatio : mDefaultAspectRatio );
2946
2947 scaledWidth = context.renderContext().convertToPainterUnits( scaledWidth, mSizeUnit, mSizeMapUnitScale );
2948 scaledHeight = context.renderContext().convertToPainterUnits( scaledHeight, mSizeUnit, mSizeMapUnitScale );
2949
2950 //don't render symbols with size below one or above 10,000 pixels
2951 if ( static_cast< int >( scaledWidth ) < 1 || 10000.0 < scaledWidth )
2952 {
2953 return QRectF();
2954 }
2955
2956 QPointF outputOffset;
2957 double angle = 0.0;
2958 calculateOffsetAndRotation( context, scaledWidth, scaledHeight, outputOffset, angle );
2959
2960 double strokeWidth = mStrokeWidth;
2962 {
2965 }
2967
2968 QString path = mPath;
2970 {
2973 context.renderContext().pathResolver() );
2975 {
2976 // need to get colors to take advantage of cached SVGs
2977 QColor fillColor = mColor;
2979 {
2982 }
2983
2984 const QColor strokeColor = mStrokeColor;
2986 {
2989 }
2990
2992
2993 // adjust height of data defined path
2994 const QSizeF svgViewbox = QgsApplication::svgCache()->svgViewboxSize( path, scaledWidth, fillColor, strokeColor, strokeWidth,
2995 context.renderContext().scaleFactor(), aspectRatio,
2996 ( context.renderContext().flags() & Qgis::RenderContextFlag::RenderBlocking ), evaluatedParameters );
2997 scaledHeight = svgViewbox.isValid() ? scaledWidth * svgViewbox.height() / svgViewbox.width() : scaledWidth;
2998 }
2999 }
3000
3001 QTransform transform;
3002 // move to the desired position
3003 transform.translate( point.x() + outputOffset.x(), point.y() + outputOffset.y() );
3004
3005 if ( !qgsDoubleNear( angle, 0.0 ) )
3006 transform.rotate( angle );
3007
3008 //antialiasing
3009 strokeWidth += 1.0 / 2.0;
3010
3011 QRectF symbolBounds = transform.mapRect( QRectF( -scaledWidth / 2.0,
3012 -scaledHeight / 2.0,
3013 scaledWidth,
3014 scaledHeight ) );
3015
3016 //extend bounds by pen width / 2.0
3017 symbolBounds.adjust( -strokeWidth / 2.0, -strokeWidth / 2.0,
3018 strokeWidth / 2.0, strokeWidth / 2.0 );
3019
3020 return symbolBounds;
3021}
3022
3024
3025QgsRasterMarkerSymbolLayer::QgsRasterMarkerSymbolLayer( const QString &path, double size, double angle, Qgis::ScaleMethod scaleMethod )
3026 : mPath( path )
3027{
3028 mSize = size;
3029 mAngle = angle;
3030 mOffset = QPointF( 0, 0 );
3033}
3034
3036
3038{
3039 QString path;
3043
3044 if ( props.contains( QStringLiteral( "imageFile" ) ) )
3045 path = props[QStringLiteral( "imageFile" )].toString();
3046 if ( props.contains( QStringLiteral( "size" ) ) )
3047 size = props[QStringLiteral( "size" )].toDouble();
3048 if ( props.contains( QStringLiteral( "angle" ) ) )
3049 angle = props[QStringLiteral( "angle" )].toDouble();
3050 if ( props.contains( QStringLiteral( "scale_method" ) ) )
3051 scaleMethod = QgsSymbolLayerUtils::decodeScaleMethod( props[QStringLiteral( "scale_method" )].toString() );
3052
3053 std::unique_ptr< QgsRasterMarkerSymbolLayer > m = std::make_unique< QgsRasterMarkerSymbolLayer >( path, size, angle, scaleMethod );
3054 m->setCommonProperties( props );
3055 return m.release();
3056}
3057
3058void QgsRasterMarkerSymbolLayer::setCommonProperties( const QVariantMap &properties )
3059{
3060 if ( properties.contains( QStringLiteral( "alpha" ) ) )
3061 {
3062 setOpacity( properties[QStringLiteral( "alpha" )].toDouble() );
3063 }
3064
3065 if ( properties.contains( QStringLiteral( "size_unit" ) ) )
3066 setSizeUnit( QgsUnitTypes::decodeRenderUnit( properties[QStringLiteral( "size_unit" )].toString() ) );
3067 if ( properties.contains( QStringLiteral( "size_map_unit_scale" ) ) )
3068 setSizeMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( properties[QStringLiteral( "size_map_unit_scale" )].toString() ) );
3069 if ( properties.contains( QStringLiteral( "fixedAspectRatio" ) ) )
3070 setFixedAspectRatio( properties[QStringLiteral( "fixedAspectRatio" )].toDouble() );
3071
3072 if ( properties.contains( QStringLiteral( "offset" ) ) )
3073 setOffset( QgsSymbolLayerUtils::decodePoint( properties[QStringLiteral( "offset" )].toString() ) );
3074 if ( properties.contains( QStringLiteral( "offset_unit" ) ) )
3075 setOffsetUnit( QgsUnitTypes::decodeRenderUnit( properties[QStringLiteral( "offset_unit" )].toString() ) );
3076 if ( properties.contains( QStringLiteral( "offset_map_unit_scale" ) ) )
3077 setOffsetMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( properties[QStringLiteral( "offset_map_unit_scale" )].toString() ) );
3078
3079 if ( properties.contains( QStringLiteral( "horizontal_anchor_point" ) ) )
3080 {
3081 setHorizontalAnchorPoint( QgsMarkerSymbolLayer::HorizontalAnchorPoint( properties[ QStringLiteral( "horizontal_anchor_point" )].toInt() ) );
3082 }
3083 if ( properties.contains( QStringLiteral( "vertical_anchor_point" ) ) )
3084 {
3085 setVerticalAnchorPoint( QgsMarkerSymbolLayer::VerticalAnchorPoint( properties[ QStringLiteral( "vertical_anchor_point" )].toInt() ) );
3086 }
3087
3090}
3091
3092void QgsRasterMarkerSymbolLayer::resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving )
3093{
3094 const QVariantMap::iterator it = properties.find( QStringLiteral( "name" ) );
3095 if ( it != properties.end() && it.value().type() == QVariant::String )
3096 {
3097 if ( saving )
3098 it.value() = QgsSymbolLayerUtils::svgSymbolPathToName( it.value().toString(), pathResolver );
3099 else
3100 it.value() = QgsSymbolLayerUtils::svgSymbolNameToPath( it.value().toString(), pathResolver );
3101 }
3102}
3103
3104void QgsRasterMarkerSymbolLayer::setPath( const QString &path )
3105{
3106 mPath = path;
3108}
3109
3111{
3112 const bool aPreservedAspectRatio = preservedAspectRatio();
3113 if ( aPreservedAspectRatio && !par )
3114 {
3116 }
3117 else if ( !aPreservedAspectRatio && par )
3118 {
3119 mFixedAspectRatio = 0.0;
3120 }
3121 return preservedAspectRatio();
3122}
3123
3125{
3126 if ( mDefaultAspectRatio == 0.0 )
3127 {
3129 mDefaultAspectRatio = ( !size.isNull() && size.isValid() && size.width() > 0 ) ? static_cast< double >( size.height() ) / static_cast< double >( size.width() ) : 0.0;
3130 }
3131 return mDefaultAspectRatio;
3132}
3133
3135{
3136 return QStringLiteral( "RasterMarker" );
3137}
3138
3140{
3141 QPainter *p = context.renderContext().painter();
3142 if ( !p )
3143 return;
3144
3145 QString path = mPath;
3147 {
3150 }
3151
3152 if ( path.isEmpty() )
3153 return;
3154
3155 double width = 0.0;
3156 double height = 0.0;
3157
3158 bool hasDataDefinedSize = false;
3159 const double scaledSize = calculateSize( context, hasDataDefinedSize );
3160
3161 bool hasDataDefinedAspectRatio = false;
3162 const double aspectRatio = calculateAspectRatio( context, scaledSize, hasDataDefinedAspectRatio );
3163
3164 QPointF outputOffset;
3165 double angle = 0.0;
3166
3167 // RenderPercentage Unit Type takes original image size
3169 {
3171 if ( size.isEmpty() )
3172 return;
3173
3174 width = ( scaledSize * static_cast< double >( size.width() ) ) / 100.0;
3175 height = ( scaledSize * static_cast< double >( size.height() ) ) / 100.0;
3176
3177 // don't render symbols with size below one or above 10,000 pixels
3178 if ( static_cast< int >( width ) < 1 || 10000.0 < width || static_cast< int >( height ) < 1 || 10000.0 < height )
3179 return;
3180
3181 calculateOffsetAndRotation( context, width, height, outputOffset, angle );
3182 }
3183 else
3184 {
3185 width = context.renderContext().convertToPainterUnits( scaledSize, mSizeUnit, mSizeMapUnitScale );
3186 height = width * ( preservedAspectRatio() ? defaultAspectRatio() : aspectRatio );
3187
3188 if ( preservedAspectRatio() && path != mPath )
3189 {
3191 if ( !size.isNull() && size.isValid() && size.width() > 0 )
3192 {
3193 height = width * ( static_cast< double >( size.height() ) / static_cast< double >( size.width() ) );
3194 }
3195 }
3196
3197 // don't render symbols with size below one or above 10,000 pixels
3198 if ( static_cast< int >( width ) < 1 || 10000.0 < width )
3199 return;
3200
3201 calculateOffsetAndRotation( context, scaledSize, scaledSize * ( height / width ), outputOffset, angle );
3202 }
3203
3204 const QgsScopedQPainterState painterState( p );
3205 p->translate( point + outputOffset );
3206
3207 const bool rotated = !qgsDoubleNear( angle, 0 );
3208 if ( rotated )
3209 p->rotate( angle );
3210
3211 double opacity = mOpacity;
3213 {
3216 }
3217 opacity *= context.opacity();
3218
3219 QImage img = fetchImage( context.renderContext(), path, QSize( width, preservedAspectRatio() ? 0 : width * aspectRatio ), preservedAspectRatio(), opacity );
3220 if ( !img.isNull() )
3221 {
3222 const bool useSelectedColor = shouldRenderUsingSelectionColor( context );
3223 if ( useSelectedColor )
3224 {
3226 }
3227
3228 p->drawImage( -img.width() / 2.0, -img.height() / 2.0, img );
3229 }
3230}
3231
3232QImage QgsRasterMarkerSymbolLayer::fetchImage( QgsRenderContext &context, const QString &path, QSize size, bool preserveAspectRatio, double opacity ) const
3233{
3234 bool cached = false;
3235 return QgsApplication::imageCache()->pathAsImage( path, size, preserveAspectRatio, opacity, cached, context.flags() & Qgis::RenderContextFlag::RenderBlocking );
3236}
3237
3238double QgsRasterMarkerSymbolLayer::calculateSize( QgsSymbolRenderContext &context, bool &hasDataDefinedSize ) const
3239{
3240 double scaledSize = mSize;
3242
3243 bool ok = true;
3244 if ( hasDataDefinedSize )
3245 {
3248 }
3249 else
3250 {
3252 if ( hasDataDefinedSize )
3253 {
3256 }
3257 }
3258
3259 if ( hasDataDefinedSize && ok )
3260 {
3261 switch ( mScaleMethod )
3262 {
3264 scaledSize = std::sqrt( scaledSize );
3265 break;
3267 break;
3268 }
3269 }
3270
3271 return scaledSize;
3272}
3273
3274double QgsRasterMarkerSymbolLayer::calculateAspectRatio( QgsSymbolRenderContext &context, double scaledSize, bool &hasDataDefinedAspectRatio ) const
3275{
3277 if ( !hasDataDefinedAspectRatio )
3278 return mFixedAspectRatio;
3279
3281 return 0.0;
3282
3283 double scaledAspectRatio = mDefaultAspectRatio;
3284 if ( mFixedAspectRatio > 0.0 )
3285 scaledAspectRatio = mFixedAspectRatio;
3286
3287 const double defaultHeight = mSize * scaledAspectRatio;
3288 scaledAspectRatio = defaultHeight / scaledSize;
3289
3290 bool ok = true;
3291 double scaledHeight = scaledSize * scaledAspectRatio;
3293 {
3294 context.setOriginalValueVariable( defaultHeight );
3296 }
3297
3298 if ( hasDataDefinedAspectRatio && ok )
3299 {
3300 switch ( mScaleMethod )
3301 {
3303 scaledHeight = sqrt( scaledHeight );
3304 break;
3306 break;
3307 }
3308 }
3309
3310 scaledAspectRatio = scaledHeight / scaledSize;
3311
3312 return scaledAspectRatio;
3313}
3314
3315void QgsRasterMarkerSymbolLayer::calculateOffsetAndRotation( QgsSymbolRenderContext &context, double scaledWidth, double scaledHeight, QPointF &offset, double &angle ) const
3316{
3317 //offset
3318 double offsetX = 0;
3319 double offsetY = 0;
3320 markerOffset( context, scaledWidth, scaledHeight, offsetX, offsetY );
3321 offset = QPointF( offsetX, offsetY );
3322
3325 {
3328 }
3329
3331 if ( hasDataDefinedRotation )
3332 {
3333 const QgsFeature *f = context.feature();
3334 if ( f )
3335 {
3336 if ( f->hasGeometry() && f->geometry().type() == Qgis::GeometryType::Point )
3337 {
3338 const QgsMapToPixel &m2p = context.renderContext().mapToPixel();
3339 angle += m2p.mapRotation();
3340 }
3341 }
3342 }
3343
3344 if ( angle )
3346}
3347
3348
3350{
3351 QVariantMap map;
3352 map[QStringLiteral( "imageFile" )] = mPath;
3353 map[QStringLiteral( "size" )] = QString::number( mSize );
3354 map[QStringLiteral( "size_unit" )] = QgsUnitTypes::encodeUnit( mSizeUnit );
3355 map[QStringLiteral( "size_map_unit_scale" )] = QgsSymbolLayerUtils::encodeMapUnitScale( mSizeMapUnitScale );
3356 map[QStringLiteral( "fixedAspectRatio" )] = QString::number( mFixedAspectRatio );
3357 map[QStringLiteral( "angle" )] = QString::number( mAngle );
3358 map[QStringLiteral( "alpha" )] = QString::number( mOpacity );
3359 map[QStringLiteral( "offset" )] = QgsSymbolLayerUtils::encodePoint( mOffset );
3360 map[QStringLiteral( "offset_unit" )] = QgsUnitTypes::encodeUnit( mOffsetUnit );
3361 map[QStringLiteral( "offset_map_unit_scale" )] = QgsSymbolLayerUtils::encodeMapUnitScale( mOffsetMapUnitScale );
3362 map[QStringLiteral( "scale_method" )] = QgsSymbolLayerUtils::encodeScaleMethod( mScaleMethod );
3363 map[QStringLiteral( "horizontal_anchor_point" )] = QString::number( mHorizontalAnchorPoint );
3364 map[QStringLiteral( "vertical_anchor_point" )] = QString::number( mVerticalAnchorPoint );
3365 return map;
3366}
3367
3369{
3370 std::unique_ptr< QgsRasterMarkerSymbolLayer > m = std::make_unique< QgsRasterMarkerSymbolLayer >( mPath, mSize, mAngle );
3371 copyCommonProperties( m.get() );
3372 return m.release();
3373}
3374
3375
3390
3396
3398{
3399 return QColor();
3400}
3401
3406
3411
3413{
3414 bool hasDataDefinedSize = false;
3415 const double scaledSize = calculateSize( context, hasDataDefinedSize );
3416 const double width = context.renderContext().convertToPainterUnits( scaledSize, mSizeUnit, mSizeMapUnitScale );
3417 bool hasDataDefinedAspectRatio = false;
3418 const double aspectRatio = calculateAspectRatio( context, scaledSize, hasDataDefinedAspectRatio );
3419 const double height = width * ( preservedAspectRatio() ? defaultAspectRatio() : aspectRatio );
3420
3421 //don't render symbols with size below one or above 10,000 pixels
3422 if ( static_cast< int >( scaledSize ) < 1 || 10000.0 < scaledSize )
3423 {
3424 return QRectF();
3425 }
3426
3427 QPointF outputOffset;
3428 double angle = 0.0;
3429 calculateOffsetAndRotation( context, scaledSize, scaledSize * ( height / width ), outputOffset, angle );
3430
3431 QTransform transform;
3432
3433 // move to the desired position
3434 transform.translate( point.x() + outputOffset.x(), point.y() + outputOffset.y() );
3435
3436 if ( !qgsDoubleNear( angle, 0.0 ) )
3437 transform.rotate( angle );
3438
3439 QRectF symbolBounds = transform.mapRect( QRectF( -width / 2.0,
3440 -height / 2.0,
3441 width,
3442 height ) );
3443
3444 return symbolBounds;
3445}
3446
3448
3449QgsFontMarkerSymbolLayer::QgsFontMarkerSymbolLayer( const QString &fontFamily, QString chr, double pointSize, const QColor &color, double angle )
3450{
3451 mFontFamily = fontFamily;
3452 mString = chr;
3453 mColor = color;
3454 mAngle = angle;
3455 mSize = pointSize;
3456 mOrigSize = pointSize;
3458 mOffset = QPointF( 0, 0 );
3460 mStrokeColor = DEFAULT_FONTMARKER_BORDERCOLOR;
3461 mStrokeWidth = 0.0;
3462 mStrokeWidthUnit = Qgis::RenderUnit::Millimeters;
3463 mPenJoinStyle = DEFAULT_FONTMARKER_JOINSTYLE;
3464}
3465
3467
3469{
3471 QString string = DEFAULT_FONTMARKER_CHR;
3472 double pointSize = DEFAULT_FONTMARKER_SIZE;
3475
3476 if ( props.contains( QStringLiteral( "font" ) ) )
3477 fontFamily = props[QStringLiteral( "font" )].toString();
3478 if ( props.contains( QStringLiteral( "chr" ) ) && props[QStringLiteral( "chr" )].toString().length() > 0 )
3479 {
3480 string = props["chr"].toString();
3481 const thread_local QRegularExpression charRegExp( QStringLiteral( "%1([0-9]+)%1" ).arg( FONTMARKER_CHR_FIX ) );
3482 QRegularExpressionMatch match = charRegExp.match( string );
3483 while ( match.hasMatch() )
3484 {
3485 QChar replacement = QChar( match.captured( 1 ).toUShort() );
3486 string = string.mid( 0, match.capturedStart( 0 ) ) + replacement + string.mid( match.capturedEnd( 0 ) );
3487 match = charRegExp.match( string );
3488 }
3489 }
3490
3491 if ( props.contains( QStringLiteral( "size" ) ) )
3492 pointSize = props[QStringLiteral( "size" )].toDouble();
3493 if ( props.contains( QStringLiteral( "color" ) ) )
3494 color = QgsSymbolLayerUtils::decodeColor( props[QStringLiteral( "color" )].toString() );
3495 if ( props.contains( QStringLiteral( "angle" ) ) )
3496 angle = props[QStringLiteral( "angle" )].toDouble();
3497
3499
3500 if ( props.contains( QStringLiteral( "font_style" ) ) )
3501 m->setFontStyle( props[QStringLiteral( "font_style" )].toString() );
3502 if ( props.contains( QStringLiteral( "outline_color" ) ) )
3503 m->setStrokeColor( QgsSymbolLayerUtils::decodeColor( props[QStringLiteral( "outline_color" )].toString() ) );
3504 if ( props.contains( QStringLiteral( "outline_width" ) ) )
3505 m->setStrokeWidth( props[QStringLiteral( "outline_width" )].toDouble() );
3506 if ( props.contains( QStringLiteral( "offset" ) ) )
3507 m->setOffset( QgsSymbolLayerUtils::decodePoint( props[QStringLiteral( "offset" )].toString() ) );
3508 if ( props.contains( QStringLiteral( "offset_unit" ) ) )
3509 m->setOffsetUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "offset_unit" )].toString() ) );
3510 if ( props.contains( QStringLiteral( "offset_map_unit_scale" ) ) )
3511 m->setOffsetMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( props[QStringLiteral( "offset_map_unit_scale" )].toString() ) );
3512 if ( props.contains( QStringLiteral( "size_unit" ) ) )
3513 m->setSizeUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "size_unit" )].toString() ) );
3514 if ( props.contains( QStringLiteral( "size_map_unit_scale" ) ) )
3515 m->setSizeMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( props[QStringLiteral( "size_map_unit_scale" )].toString() ) );
3516 if ( props.contains( QStringLiteral( "outline_width_unit" ) ) )
3517 m->setStrokeWidthUnit( QgsUnitTypes::decodeRenderUnit( props[QStringLiteral( "outline_width_unit" )].toString() ) );
3518 if ( props.contains( QStringLiteral( "outline_width_map_unit_scale" ) ) )
3519 m->setStrokeWidthMapUnitScale( QgsSymbolLayerUtils::decodeMapUnitScale( props[QStringLiteral( "outline_width_map_unit_scale" )].toString() ) );
3520 if ( props.contains( QStringLiteral( "joinstyle" ) ) )
3521 m->setPenJoinStyle( QgsSymbolLayerUtils::decodePenJoinStyle( props[QStringLiteral( "joinstyle" )].toString() ) );
3522 if ( props.contains( QStringLiteral( "horizontal_anchor_point" ) ) )
3523 m->setHorizontalAnchorPoint( QgsMarkerSymbolLayer::HorizontalAnchorPoint( props[ QStringLiteral( "horizontal_anchor_point" )].toInt() ) );
3524 if ( props.contains( QStringLiteral( "vertical_anchor_point" ) ) )
3525 m->setVerticalAnchorPoint( QgsMarkerSymbolLayer::VerticalAnchorPoint( props[ QStringLiteral( "vertical_anchor_point" )].toInt() ) );
3526
3528
3529 return m;
3530}
3531
3533{
3534 return QStringLiteral( "FontMarker" );
3535}
3536
3538{
3539 QColor brushColor = mColor;
3540 QColor penColor = mStrokeColor;
3541
3542 brushColor.setAlphaF( mColor.alphaF() * context.opacity() );
3543 penColor.setAlphaF( mStrokeColor.alphaF() * context.opacity() );
3544
3545 mBrush = QBrush( brushColor );
3546 mPen = QPen( penColor );
3547 mPen.setJoinStyle( mPenJoinStyle );
3548 mPen.setWidthF( context.renderContext().convertToPainterUnits( mStrokeWidth, mStrokeWidthUnit, mStrokeWidthMapUnitScale ) );
3549
3550 mFont = QgsFontUtils::createFont( QgsApplication::fontManager()->processFontFamilyName( mFontFamily ) );
3551 if ( !mFontStyle.isEmpty() )
3552 {
3553 mFont.setStyleName( QgsFontUtils::translateNamedStyle( mFontStyle ) );
3554 }
3555
3556 double sizePixels = context.renderContext().convertToPainterUnits( mSize, mSizeUnit, mSizeMapUnitScale );
3557 mNonZeroFontSize = !qgsDoubleNear( sizePixels, 0.0 );
3558
3559 if ( mNonZeroFontSize && sizePixels > MAX_FONT_CHARACTER_SIZE_IN_PIXELS )
3560 {
3561 // if font is too large (e.g using map units and map is very zoomed in), then we limit
3562 // the font size and instead scale up the painter.
3563 // this avoids issues with massive font sizes (eg https://github.com/qgis/QGIS/issues/42270)
3564 mFontSizeScale = sizePixels / MAX_FONT_CHARACTER_SIZE_IN_PIXELS;
3565 sizePixels = MAX_FONT_CHARACTER_SIZE_IN_PIXELS;
3566 }
3567 else
3568 mFontSizeScale = 1.0;
3569
3570 // if a non zero, but small pixel size results, round up to 2 pixels so that a "dot" is at least visible
3571 // (if we set a <=1 pixel size here Qt will reset the font to a default size, leading to much too large symbols)
3572 mFont.setPixelSize( std::max( 2, static_cast< int >( std::round( sizePixels ) ) ) );
3573 mFontMetrics.reset( new QFontMetrics( mFont ) );
3574 mChrWidth = mFontMetrics->horizontalAdvance( mString );
3575 mChrOffset = QPointF( mChrWidth / 2.0, -mFontMetrics->ascent() / 2.0 );
3576 mOrigSize = mSize; // save in case the size would be data defined
3577
3578 // use caching only when not using a data defined character
3582 if ( mUseCachedPath )
3583 {
3584 QPointF chrOffset = mChrOffset;
3585 double chrWidth;
3586 const QString charToRender = characterToRender( context, chrOffset, chrWidth );
3587 mCachedPath = QPainterPath();
3588 mCachedPath.addText( -chrOffset.x(), -chrOffset.y(), mFont, charToRender );
3589 }
3590}
3591
3593{
3594 Q_UNUSED( context )
3595}
3596
3597QString QgsFontMarkerSymbolLayer::characterToRender( QgsSymbolRenderContext &context, QPointF &charOffset, double &charWidth )
3598{
3599 charOffset = mChrOffset;
3600 QString stringToRender = mString;
3602 {
3603 context.setOriginalValueVariable( mString );
3605 if ( stringToRender != mString )
3606 {
3607 charWidth = mFontMetrics->horizontalAdvance( stringToRender );
3608 charOffset = QPointF( charWidth / 2.0, -mFontMetrics->ascent() / 2.0 );
3609 }
3610 }
3611 return stringToRender;
3612}
3613
3614void QgsFontMarkerSymbolLayer::calculateOffsetAndRotation( QgsSymbolRenderContext &context,
3615 double scaledSize,
3616 bool &hasDataDefinedRotation,
3617 QPointF &offset,
3618 double &angle ) const
3619{
3620 //offset
3621 double offsetX = 0;
3622 double offsetY = 0;
3623 markerOffset( context, scaledSize, scaledSize, offsetX, offsetY );
3624 offset = QPointF( offsetX, offsetY );
3625
3626 //angle
3627 bool ok = true;
3630 {
3633
3634 // If the expression evaluation was not successful, fallback to static value
3635 if ( !ok )
3637 }
3638
3639 hasDataDefinedRotation = context.renderHints() & Qgis::SymbolRenderHint::DynamicRotation;
3640 if ( hasDataDefinedRotation )
3641 {
3642 // For non-point markers, "dataDefinedRotation" means following the
3643 // shape (shape-data defined). For them, "field-data defined" does
3644 // not work at all. TODO: if "field-data defined" ever gets implemented
3645 // we'll need a way to distinguish here between the two, possibly
3646 // using another flag in renderHints()
3647 const QgsFeature *f = context.feature();
3648 if ( f )
3649 {
3650 if ( f->hasGeometry() && f->geometry().type() == Qgis::GeometryType::Point )
3651 {
3652 const QgsMapToPixel &m2p = context.renderContext().mapToPixel();
3653 angle += m2p.mapRotation();
3654 }
3655 }
3656 }
3657
3658 if ( angle )
3660}
3661
3662double QgsFontMarkerSymbolLayer::calculateSize( QgsSymbolRenderContext &context )
3663{
3664 double scaledSize = mSize;
3665 const bool hasDataDefinedSize = mDataDefinedProperties.isActive( QgsSymbolLayer::PropertySize );
3666
3667 bool ok = true;
3668 if ( hasDataDefinedSize )
3669 {
3672 }
3673
3674 if ( hasDataDefinedSize && ok )
3675 {
3676 switch ( mScaleMethod )
3677 {
3679 scaledSize = std::sqrt( scaledSize );
3680 break;
3682 break;
3683 }
3684 }
3685 return scaledSize;
3686}
3687
3689{
3690 QPainter *p = context.renderContext().painter();
3691 if ( !p || !mNonZeroFontSize )
3692 return;
3693
3694 QTransform transform;
3695
3696 bool ok;
3697 QColor brushColor = mColor;
3699 {
3702 }
3703 const bool useSelectedColor = shouldRenderUsingSelectionColor( context );
3704 brushColor = useSelectedColor ? context.renderContext().selectionColor() : brushColor;
3705 if ( !useSelectedColor || !SELECTION_IS_OPAQUE )
3706 {
3707 brushColor.setAlphaF( brushColor.alphaF() * context.opacity() );
3708 }
3709 mBrush.setColor( brushColor );
3710
3711 QColor penColor = mStrokeColor;
3713 {
3716 }
3717 penColor.setAlphaF( penColor.alphaF() * context.opacity() );
3718
3719 double penWidth = context.renderContext().convertToPainterUnits( mStrokeWidth, mStrokeWidthUnit, mStrokeWidthMapUnitScale );
3721 {
3722 context.setOriginalValueVariable( mStrokeWidth );
3724 if ( ok )
3725 {
3726 penWidth = context.renderContext().convertToPainterUnits( strokeWidth, mStrokeWidthUnit, mStrokeWidthMapUnitScale );
3727 }
3728 }
3729
3731 {
3734 if ( ok )
3735 {
3736 mPen.setJoinStyle( QgsSymbolLayerUtils::decodePenJoinStyle( style ) );
3737 }
3738 }
3739
3740 const QgsScopedQPainterState painterState( p );
3741 p->setBrush( mBrush );
3742 if ( !qgsDoubleNear( penWidth, 0.0 ) )
3743 {
3744 mPen.setColor( penColor );
3745 mPen.setWidthF( penWidth );
3746 p->setPen( mPen );
3747 }
3748 else
3749 {
3750 p->setPen( Qt::NoPen );
3751 }
3752
3754 {
3755 context.setOriginalValueVariable( mFontFamily );
3757 const QString processedFamily = QgsApplication::fontManager()->processFontFamilyName( ok ? fontFamily : mFontFamily );
3758 QgsFontUtils::setFontFamily( mFont, processedFamily );
3759 }
3761 {
3762 context.setOriginalValueVariable( mFontStyle );
3765 }
3767 {
3768 mFontMetrics.reset( new QFontMetrics( mFont ) );
3769 }
3770
3771 QPointF chrOffset = mChrOffset;
3772 double chrWidth;
3773 const QString charToRender = characterToRender( context, chrOffset, chrWidth );
3774
3775 const double sizeToRender = calculateSize( context );
3776
3777 bool hasDataDefinedRotation = false;
3778 QPointF offset;
3779 double angle = 0;
3780 calculateOffsetAndRotation( context, sizeToRender, hasDataDefinedRotation, offset, angle );
3781
3782 p->translate( point.x() + offset.x(), point.y() + offset.y() );
3783
3784 if ( !qgsDoubleNear( angle, 0.0 ) )
3785 transform.rotate( angle );
3786
3787 if ( !qgsDoubleNear( sizeToRender, mOrigSize ) )
3788 {
3789 const double s = sizeToRender / mOrigSize;
3790 transform.scale( s, s );
3791 }
3792
3793 if ( !qgsDoubleNear( mFontSizeScale, 1.0 ) )
3794 transform.scale( mFontSizeScale, mFontSizeScale );
3795
3796 if ( mUseCachedPath )
3797 {
3798 p->drawPath( transform.map( mCachedPath ) );
3799 }
3800 else
3801 {
3802 QPainterPath path;
3803 path.addText( -chrOffset.x(), -chrOffset.y(), mFont, charToRender );
3804 p->drawPath( transform.map( path ) );
3805 }
3806}
3807
3809{
3810 QVariantMap props;
3811 props[QStringLiteral( "font" )] = mFontFamily;
3812 props[QStringLiteral( "font_style" )] = mFontStyle;
3813 QString chr = mString;
3814 for ( int i = 0; i < 32; i++ )
3815 {
3816 if ( i == 9 || i == 10 || i == 13 )
3817 {
3818 continue;
3819 }
3820 chr.replace( QChar( i ), QStringLiteral( "%1%2%1" ).arg( FONTMARKER_CHR_FIX, QString::number( i ) ) );
3821 }
3822 props[QStringLiteral( "chr" )] = chr;
3823 props[QStringLiteral( "size" )] = QString::number( mSize );
3824 props[QStringLiteral( "size_unit" )] = QgsUnitTypes::encodeUnit( mSizeUnit );
3825 props[QStringLiteral( "size_map_unit_scale" )] = QgsSymbolLayerUtils::encodeMapUnitScale( mSizeMapUnitScale );
3826 props[QStringLiteral( "color" )] = QgsSymbolLayerUtils::encodeColor( mColor );
3827 props[QStringLiteral( "outline_color" )] = QgsSymbolLayerUtils::encodeColor( mStrokeColor );
3828 props[QStringLiteral( "outline_width" )] = QString::number( mStrokeWidth );
3829 props[QStringLiteral( "outline_width_unit" )] = QgsUnitTypes::encodeUnit( mStrokeWidthUnit );
3830 props[QStringLiteral( "outline_width_map_unit_scale" )] = QgsSymbolLayerUtils::encodeMapUnitScale( mStrokeWidthMapUnitScale );
3831 props[QStringLiteral( "joinstyle" )] = QgsSymbolLayerUtils::encodePenJoinStyle( mPenJoinStyle );
3832 props[QStringLiteral( "angle" )] = QString::number( mAngle );
3833 props[QStringLiteral( "offset" )] = QgsSymbolLayerUtils::encodePoint( mOffset );
3834 props[QStringLiteral( "offset_unit" )] = QgsUnitTypes::encodeUnit( mOffsetUnit );
3835 props[QStringLiteral( "offset_map_unit_scale" )] = QgsSymbolLayerUtils::encodeMapUnitScale( mOffsetMapUnitScale );
3836 props[QStringLiteral( "horizontal_anchor_point" )] = QString::number( mHorizontalAnchorPoint );
3837 props[QStringLiteral( "vertical_anchor_point" )] = QString::number( mVerticalAnchorPoint );
3838 return props;
3839}
3840
3842{
3843 QgsFontMarkerSymbolLayer *m = new QgsFontMarkerSymbolLayer( mFontFamily, mString, mSize, mColor, mAngle );
3844 m->setFontStyle( mFontStyle );
3845 m->setStrokeColor( mStrokeColor );
3846 m->setStrokeWidth( mStrokeWidth );
3847 m->setStrokeWidthUnit( mStrokeWidthUnit );
3848 m->setStrokeWidthMapUnitScale( mStrokeWidthMapUnitScale );
3849 m->setPenJoinStyle( mPenJoinStyle );
3850 m->setOffset( mOffset );
3853 m->setSizeUnit( mSizeUnit );
3858 copyPaintEffect( m );
3859 return m;
3860}
3861
3862void QgsFontMarkerSymbolLayer::writeSldMarker( QDomDocument &doc, QDomElement &element, const QVariantMap &props ) const
3863{
3864 // <Graphic>
3865 QDomElement graphicElem = doc.createElement( QStringLiteral( "se:Graphic" ) );
3866 element.appendChild( graphicElem );
3867
3868 const QString fontPath = QStringLiteral( "ttf://%1" ).arg( mFontFamily );
3869 int markIndex = !mString.isEmpty() ? mString.at( 0 ).unicode() : 0;
3870 const double size = QgsSymbolLayerUtils::rescaleUom( mSize, mSizeUnit, props );
3871 QgsSymbolLayerUtils::externalMarkerToSld( doc, graphicElem, fontPath, QStringLiteral( "ttf" ), &markIndex, mColor, size );
3872
3873 // <Rotation>
3874 QString angleFunc;
3875 bool ok;
3876 const double angle = props.value( QStringLiteral( "angle" ), QStringLiteral( "0" ) ).toDouble( &ok );
3877 if ( !ok )
3878 {
3879 angleFunc = QStringLiteral( "%1 + %2" ).arg( props.value( QStringLiteral( "angle" ), QStringLiteral( "0" ) ).toString() ).arg( mAngle );
3880 }
3881 else if ( !qgsDoubleNear( angle + mAngle, 0.0 ) )
3882 {
3883 angleFunc = QString::number( angle + mAngle );
3884 }
3885 QgsSymbolLayerUtils::createRotationElement( doc, graphicElem, angleFunc );
3886
3887 // <Displacement>
3888 const QPointF offset = QgsSymbolLayerUtils::rescaleUom( mOffset, mOffsetUnit, props );
3890}
3891
3898
3900{
3902 mStrokeWidthUnit = unit;
3903}
3904
3906{
3907 QPointF chrOffset = mChrOffset;
3908 double chrWidth = mChrWidth;
3909 //calculate width of rendered character
3910 ( void )characterToRender( context, chrOffset, chrWidth );
3911
3912 if ( !mFontMetrics )
3913 mFontMetrics.reset( new QFontMetrics( mFont ) );
3914
3915 double scaledSize = calculateSize( context );
3916 if ( !qgsDoubleNear( scaledSize, mOrigSize ) )
3917 {
3918 chrWidth *= scaledSize / mOrigSize;
3919 }
3920 chrWidth *= mFontSizeScale;
3921
3922 bool hasDataDefinedRotation = false;
3923 QPointF offset;
3924 double angle = 0;
3925 calculateOffsetAndRotation( context, scaledSize, hasDataDefinedRotation, offset, angle );
3926 scaledSize = context.renderContext().convertToPainterUnits( scaledSize, mSizeUnit, mSizeMapUnitScale );
3927
3928 QTransform transform;
3929
3930 // move to the desired position
3931 transform.translate( point.x() + offset.x(), point.y() + offset.y() );
3932
3933 if ( !qgsDoubleNear( angle, 0.0 ) )
3934 transform.rotate( angle );
3935
3936 QRectF symbolBounds = transform.mapRect( QRectF( -chrWidth / 2.0,
3937 -scaledSize / 2.0,
3938 chrWidth,
3939 scaledSize ) );
3940 return symbolBounds;
3941}
3942
3944{
3945 QgsDebugMsgLevel( QStringLiteral( "Entered." ), 4 );
3946
3947 QDomElement graphicElem = element.firstChildElement( QStringLiteral( "Graphic" ) );
3948 if ( graphicElem.isNull() )
3949 return nullptr;
3950
3951 QString name, format;
3952 QColor color;
3953 double size;
3954 int chr;
3955
3956 if ( !QgsSymbolLayerUtils::externalMarkerFromSld( graphicElem, name, format, chr, color, size ) )
3957 return nullptr;
3958
3959 if ( !name.startsWith( QLatin1String( "ttf://" ) ) || format != QLatin1String( "ttf" ) )
3960 return nullptr;
3961
3962 const QString fontFamily = name.mid( 6 );
3963
3964 double angle = 0.0;
3965 QString angleFunc;
3966 if ( QgsSymbolLayerUtils::rotationFromSldElement( graphicElem, angleFunc ) )
3967 {
3968 bool ok;
3969 const double d = angleFunc.toDouble( &ok );
3970 if ( ok )
3971 angle = d;
3972 }
3973
3974 QPointF offset;
3976
3977 double scaleFactor = 1.0;
3978 const QString uom = element.attribute( QStringLiteral( "uom" ) );
3979 Qgis::RenderUnit sldUnitSize = QgsSymbolLayerUtils::decodeSldUom( uom, &scaleFactor );
3980 offset.setX( offset.x() * scaleFactor );
3981 offset.setY( offset.y() * scaleFactor );
3982 size = size * scaleFactor;
3983
3985 m->setOutputUnit( sldUnitSize );
3986 m->setAngle( angle );
3987 m->setOffset( offset );
3988 return m;
3989}
3990
3991void QgsFontMarkerSymbolLayer::resolveFonts( const QVariantMap &properties, const QgsReadWriteContext &context )
3992{
3993 const QString fontFamily = properties.value( QStringLiteral( "font" ), DEFAULT_FONTMARKER_FONT ).toString();
3994 const QString processedFamily = QgsApplication::fontManager()->processFontFamilyName( fontFamily );
3995 QString matched;
3996 if ( !QgsFontUtils::fontFamilyMatchOnSystem( processedFamily )
3997 && !QgsApplication::fontManager()->tryToDownloadFontFamily( processedFamily, matched ) )
3998 {
3999 context.pushMessage( QObject::tr( "Font “%1” not available on system" ).arg( processedFamily ) );
4000 }
4001}
4002
4004{
4005 QMap<QString, QgsProperty>::iterator it = mParameters.begin();
4006 for ( ; it != mParameters.end(); ++it )
4007 it.value().prepare( context.renderContext().expressionContext() );
4008
4010}
4011
4012
4014{
4015 QSet<QString> attrs = QgsMarkerSymbolLayer::usedAttributes( context );
4016
4017 QMap<QString, QgsProperty>::const_iterator it = mParameters.constBegin();
4018 for ( ; it != mParameters.constEnd(); ++it )
4019 {
4020 attrs.unite( it.value().referencedFields( context.expressionContext(), true ) );
4021 }
4022
4023 return attrs;
4024}
4025
4026//
4027// QgsAnimatedMarkerSymbolLayer
4028//
4029
4030QgsAnimatedMarkerSymbolLayer::QgsAnimatedMarkerSymbolLayer( const QString &path, double size, double angle )
4031 : QgsRasterMarkerSymbolLayer( path, size, angle )
4032{
4033
4034}
4035
4037
4039{
4040 QString path;
4043
4044 if ( properties.contains( QStringLiteral( "imageFile" ) ) )
4045 path = properties[QStringLiteral( "imageFile" )].toString();
4046 if ( properties.contains( QStringLiteral( "size" ) ) )
4047 size = properties[QStringLiteral( "size" )].toDouble();
4048 if ( properties.contains( QStringLiteral( "angle" ) ) )
4049 angle = properties[QStringLiteral( "angle" )].toDouble();
4050
4051 std::unique_ptr< QgsAnimatedMarkerSymbolLayer > m = std::make_unique< QgsAnimatedMarkerSymbolLayer >( path, size, angle );
4052 m->setFrameRate( properties.value( QStringLiteral( "frameRate" ), QStringLiteral( "10" ) ).toDouble() );
4053
4054 m->setCommonProperties( properties );
4055 return m.release();
4056}
4057
4059{
4060 return QStringLiteral( "AnimatedMarker" );
4061}
4062
4064{
4065 QVariantMap res = QgsRasterMarkerSymbolLayer::properties();
4066 res.insert( QStringLiteral( "frameRate" ), mFrameRateFps );
4067 return res;
4068}
4069
4071{
4072 std::unique_ptr< QgsAnimatedMarkerSymbolLayer > m = std::make_unique< QgsAnimatedMarkerSymbolLayer >( mPath, mSize, mAngle );
4073 m->setFrameRate( mFrameRateFps );
4074 copyCommonProperties( m.get() );
4075 return m.release();
4076}
4077
4079{
4081
4082 mPreparedPaths.clear();
4084 {
4086 mStaticPath = true;
4087 }
4088 else
4089 {
4090 mStaticPath = false;
4091 }
4092}
4093
4094QImage QgsAnimatedMarkerSymbolLayer::fetchImage( QgsRenderContext &context, const QString &path, QSize size, bool preserveAspectRatio, double opacity ) const
4095{
4096 if ( !mStaticPath && !mPreparedPaths.contains( path ) )
4097 {
4099 mPreparedPaths.insert( path );
4100 }
4101
4102 const long long mapFrameNumber = context.currentFrame();
4104 const double markerAnimationDuration = totalFrameCount / mFrameRateFps;
4105
4106 double animationTimeSeconds = 0;
4107 if ( mapFrameNumber >= 0 && context.frameRate() > 0 )
4108 {
4109 // render is part of an animation, so we base the calculated frame on that
4110 animationTimeSeconds = mapFrameNumber / context.frameRate();
4111 }
4112 else
4113 {
4114 // render is outside of animation, so base the calculated frame on the current epoch
4115 animationTimeSeconds = QDateTime::currentMSecsSinceEpoch() / 1000.0;
4116 }
4117
4118 const double markerAnimationProgressSeconds = std::fmod( animationTimeSeconds, markerAnimationDuration );
4119 const int movieFrame = static_cast< int >( std::floor( markerAnimationProgressSeconds * mFrameRateFps ) );
4120
4121 bool cached = false;
4122 return QgsApplication::imageCache()->pathAsImage( path, size, preserveAspectRatio, opacity, cached, context.flags() & Qgis::RenderContextFlag::RenderBlocking, 96, movieFrame );
4123}
4124
@ DynamicRotation
Rotation of symbol may be changed during rendering and symbol should not be cached.
ScaleMethod
Scale methods.
Definition qgis.h:382
@ ScaleDiameter
Calculate scale by the diameter.
@ ScaleArea
Calculate scale by the area.
MarkerShape
Marker shapes.
Definition qgis.h:2314
@ Pentagon
Pentagon.
@ EquilateralTriangle
Equilateral triangle.
@ SemiCircle
Semi circle (top half)
@ QuarterCircle
Quarter circle (top left quarter)
@ LeftHalfTriangle
Left half of triangle.
@ ArrowHead
Right facing arrow head (unfilled, lines only)
@ ParallelogramRight
Parallelogram that slants right (since QGIS 3.28)
@ AsteriskFill
A filled asterisk shape (since QGIS 3.18)
@ Octagon
Octagon (since QGIS 3.18)
@ HalfArc
A line-only half arc (since QGIS 3.20)
@ Line
Vertical line.
@ QuarterSquare
Quarter square (top left quarter)
@ Triangle
Triangle.
@ Cross2
Rotated cross (lines only), 'x' shape.
@ Trapezoid
Trapezoid (since QGIS 3.28)
@ ArrowHeadFilled
Right facing filled arrow head.
@ Shield
A shape consisting of a triangle attached to a rectangle (since QGIS 3.28)
@ HalfSquare
Half square (left half)
@ CrossFill
Solid filled cross.
@ Decagon
Decagon (since QGIS 3.28)
@ RoundedSquare
A square with rounded corners (since QGIS 3.28)
@ RightHalfTriangle
Right half of triangle.
@ ThirdCircle
One third circle (top left third)
@ ThirdArc
A line-only one third arc (since QGIS 3.20)
@ SquareWithCorners
A square with diagonal corners (since QGIS 3.18)
@ QuarterArc
A line-only one quarter arc (since QGIS 3.20)
@ DiamondStar
A 4-sided star (since QGIS 3.28)
@ Cross
Cross (lines only)
@ ParallelogramLeft
Parallelogram that slants left (since QGIS 3.28)
@ Heart
Heart (since QGIS 3.28)
@ DiagonalHalfSquare
Diagonal half square (bottom left half)
RenderUnit
Rendering size units.
Definition qgis.h:3721
@ Percentage
Percentage of another measurement (e.g., canvas size, feature size)
@ Millimeters
Millimeters.
@ Unknown
Mixed or unknown units.
@ MapUnits
Map units.
@ MetersInMapUnits
Meters value as Map units.
@ RenderingSubSymbol
Set whenever a sub-symbol of a parent symbol is currently being rendered. Can be used during symbol a...
@ RenderSymbolPreview
The render is for a symbol preview only and map based properties may not be available,...
@ RenderBlocking
Render and load remote sources in the same thread to ensure rendering remote sources (svg and images)...
@ Fill
Fill symbol.
QColor valueAsColor(int key, const QgsExpressionContext &context, const QColor &defaultColor=QColor(), bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a color.
double valueAsDouble(int key, const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a double.
QString valueAsString(int key, const QgsExpressionContext &context, const QString &defaultString=QString(), bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a string.
Animated marker symbol layer class.
QVariantMap properties() const override
Should be reimplemented by subclasses to return a string map that contains the configuration informat...
QgsAnimatedMarkerSymbolLayer * clone() const override
Shall be reimplemented by subclasses to create a deep copy of the instance.
~QgsAnimatedMarkerSymbolLayer() override
void startRender(QgsSymbolRenderContext &context) override
Called before a set of rendering operations commences on the supplied render context.
QImage fetchImage(QgsRenderContext &context, const QString &path, QSize size, bool preserveAspectRatio, double opacity) const override
Fetches the image to render.
QgsAnimatedMarkerSymbolLayer(const QString &path=QString(), double size=DEFAULT_RASTERMARKER_SIZE, double angle=DEFAULT_RASTERMARKER_ANGLE)
Constructor for animated marker symbol layer using the specified source image path.
static QgsSymbolLayer * create(const QVariantMap &properties=QVariantMap())
Creates an animated marker symbol layer from a string map of properties.
QString layerType() const override
Returns a string that represents this layer type.
static QgsImageCache * imageCache()
Returns the application's image cache, used for caching resampled versions of raster images.
static QgsSvgCache * svgCache()
Returns the application's SVG cache, used for caching SVG images and handling parameter replacement w...
static QgsFontManager * fontManager()
Returns the application font manager, which manages available fonts and font installation for the QGI...
Exports QGIS layers to the DXF format.
void writeFilledCircle(const QString &layer, const QColor &color, const QgsPoint &pt, double radius)
Write filled circle (as hatch)
void writeCircle(const QString &layer, const QColor &color, const QgsPoint &pt, double radius, const QString &lineStyleName, double width)
Write circle (as polyline)
static double mapUnitScaleFactor(double scale, Qgis::RenderUnit symbolUnits, Qgis::DistanceUnit mapUnits, double mapUnitsPerPixel=1.0)
Returns scale factor for conversion to map units.
void writeLine(const QgsPoint &pt1, const QgsPoint &pt2, const QString &layer, const QString &lineStyleName, const QColor &color, double width=-1)
Write line (as a polyline)
void writePolygon(const QgsRingSequence &polygon, const QString &layer, const QString &hatchPattern, const QColor &color)
Draw dxf filled polygon (HATCH)
Qgis::DistanceUnit mapUnits() const
Retrieve map units.
double symbologyScale() const
Returns the reference scale for output.
void clipValueToMapUnitScale(double &value, const QgsMapUnitScale &scale, double pixelToMMFactor) const
Clips value to scale minimum/maximum.
void writePolyline(const QgsPointSequence &line, const QString &layer, const QString &lineStyleName, const QColor &color, double width=-1)
Draw dxf primitives (LWPOLYLINE)
A paint device for drawing into dxf files.
void setShift(QPointF shift)
void setLayer(const QString &layer)
void setOutputSize(const QRectF &r)
void setDrawingSize(QSizeF size)
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:56
QgsGeometry geometry
Definition qgsfeature.h:67
bool hasGeometry() const
Returns true if the feature has an associated geometry.
A fill symbol type, for rendering Polygon and MultiPolygon geometries.
static QgsFillSymbol * createSimple(const QVariantMap &properties)
Create a fill symbol with one symbol layer: SimpleFill with specified properties.
Filled marker symbol layer, consisting of a shape which is rendered using a QgsFillSymbol.
QSet< QString > usedAttributes(const QgsRenderContext &context) const override
Returns the set of attributes referenced by the layer.
void setOutputUnit(Qgis::RenderUnit unit) override
Sets the units to use for sizes and widths within the symbol layer.
QVariantMap properties() const override
Should be reimplemented by subclasses to return a string map that contains the configuration informat...
QColor color() const override
Returns the "representative" color of the symbol layer.
QgsFilledMarkerSymbolLayer * clone() const override
Shall be reimplemented by subclasses to create a deep copy of the instance.
bool hasDataDefinedProperties() const override
Returns true if the symbol layer (or any of its sub-symbols) contains data defined properties.
void startRender(QgsSymbolRenderContext &context) override
Called before a set of rendering operations commences on the supplied render context.
bool usesMapUnits() const override
Returns true if the symbol layer has any components which use map unit based sizes.
QgsSymbol * subSymbol() override
Returns the symbol's sub symbol, if present.
~QgsFilledMarkerSymbolLayer() override
bool setSubSymbol(QgsSymbol *symbol) override
Sets layer's subsymbol. takes ownership of the passed symbol.
void stopRender(QgsSymbolRenderContext &context) override
Called after a set of rendering operations has finished on the supplied render context.
double estimateMaxBleed(const QgsRenderContext &context) const override
Returns the estimated maximum distance which the layer style will bleed outside the drawn shape when ...
void setColor(const QColor &c) override
Sets the "representative" color for the symbol layer.
QString layerType() const override
Returns a string that represents this layer type.
static QgsSymbolLayer * create(const QVariantMap &properties=QVariantMap())
Creates a new QgsFilledMarkerSymbolLayer.
QgsFilledMarkerSymbolLayer(Qgis::MarkerShape shape=Qgis::MarkerShape::Circle, double size=DEFAULT_SIMPLEMARKER_SIZE, double angle=DEFAULT_SIMPLEMARKER_ANGLE, Qgis::ScaleMethod scaleMethod=DEFAULT_SCALE_METHOD)
Constructor for QgsFilledMarkerSymbolLayer.
QString processFontFamilyName(const QString &name) const
Processes a font family name, applying any matching fontFamilyReplacements() to the name.
void setStrokeWidthUnit(Qgis::RenderUnit unit)
Sets the stroke width unit.
~QgsFontMarkerSymbolLayer() override
void setStrokeColor(const QColor &color) override
Sets the stroke color for the symbol layer.
QgsFontMarkerSymbolLayer(const QString &fontFamily=DEFAULT_FONTMARKER_FONT, QString chr=DEFAULT_FONTMARKER_CHR, double pointSize=DEFAULT_FONTMARKER_SIZE, const QColor &color=DEFAULT_FONTMARKER_COLOR, double angle=DEFAULT_FONTMARKER_ANGLE)
Constructs a font marker symbol layer.
void setStrokeWidthMapUnitScale(const QgsMapUnitScale &scale)
Sets the stroke width map unit scale.
double strokeWidth() const
Returns the marker's stroke width.
QVariantMap properties() const override
Should be reimplemented by subclasses to return a string map that contains the configuration informat...
void renderPoint(QPointF point, QgsSymbolRenderContext &context) override
Renders a marker at the specified point.
void setFontStyle(const QString &style)
Sets the font style for the font which will be used to render the point.
bool usesMapUnits() const override
Returns true if the symbol layer has any components which use map unit based sizes.
QString fontStyle() const
Returns the font style for the associated font which will be used to render the point.
QString fontFamily() const
Returns the font family name for the associated font which will be used to render the point.
QRectF bounds(QPointF point, QgsSymbolRenderContext &context) override
Returns the approximate bounding box of the marker symbol layer, taking into account any data defined...
void writeSldMarker(QDomDocument &doc, QDomElement &element, const QVariantMap &props) const override
Writes the symbol layer definition as a SLD XML element.
void setStrokeWidth(double width)
Set's the marker's stroke width.
static void resolveFonts(const QVariantMap &properties, const QgsReadWriteContext &context)
Resolves fonts from a properties map, raising warnings in the specified context if the required fonts...
void startRender(QgsSymbolRenderContext &context) override
Called before a set of rendering operations commences on the supplied render context.
void setPenJoinStyle(Qt::PenJoinStyle style)
Sets the stroke join style.
static QgsSymbolLayer * createFromSld(QDomElement &element)
Creates a new QgsFontMarkerSymbolLayer from an SLD XML element.
QgsFontMarkerSymbolLayer * clone() const override
Shall be reimplemented by subclasses to create a deep copy of the instance.
void setOutputUnit(Qgis::RenderUnit unit) override
Sets the units to use for sizes and widths within the symbol layer.
void stopRender(QgsSymbolRenderContext &context) override
Called after a set of rendering operations has finished on the supplied render context.
QString layerType() const override
Returns a string that represents this layer type.
static QgsSymbolLayer * create(const QVariantMap &properties=QVariantMap())
Creates a new QgsFontMarkerSymbolLayer from a property map (see properties())
static QString translateNamedStyle(const QString &namedStyle)
Returns the localized named style of a font, if such a translation is available.
static QFont createFont(const QString &family, int pointSize=-1, int weight=-1, bool italic=false)
Creates a font with the specified family.
static bool fontFamilyMatchOnSystem(const QString &family, QString *chosen=nullptr, bool *match=nullptr)
Check whether font family is on system.
static bool updateFontViaStyle(QFont &f, const QString &fontstyle, bool fallback=false)
Updates font with named style and retain all font properties.
static void setFontFamily(QFont &font, const QString &family)
Sets the family for a font object.
Qgis::GeometryType type
QSize originalSize(const QString &path, bool blocking=false) const
Returns the original size (in pixels) of the image at the specified path.
int totalFrameCount(const QString &path, bool blocking=false)
Returns the total frame count of the image at the specified path.
QImage pathAsImage(const QString &path, const QSize size, const bool keepAspectRatio, const double opacity, bool &fitsInCache, bool blocking=false, double targetDpi=96, int frameNumber=-1, bool *isMissing=nullptr)
Returns the specified path rendered as an image.
void prepareAnimation(const QString &path)
Prepares for optimized retrieval of frames for the animation at the given path.
static void overlayColor(QImage &image, const QColor &color)
Overlays a color onto an image.
Perform transforms between map coordinates and device coordinates.
double mapUnitsPerPixel() const
Returns the current map units per pixel.
double mapRotation() const
Returns the current map rotation in degrees (clockwise).
Struct for storing maximum and minimum scales for measurements in map units.
Abstract base class for marker symbol layers.
double mSize
Marker size.
Qgis::RenderUnit mOffsetUnit
Offset units.
QPointF offset() const
Returns the marker's offset, which is the horizontal and vertical displacement which the rendered mar...
double mLineAngle
Line rotation angle (see setLineAngle() for details)
HorizontalAnchorPoint
Symbol horizontal anchor points.
void setOffsetUnit(Qgis::RenderUnit unit)
Sets the units for the symbol's offset.
void setAngle(double angle)
Sets the rotation angle for the marker.
Qgis::ScaleMethod scaleMethod() const
Returns the method to use for scaling the marker's size.
Qgis::RenderUnit outputUnit() const override
Returns the units to use for sizes and widths within the symbol layer.
void setVerticalAnchorPoint(VerticalAnchorPoint v)
Sets the vertical anchor point for positioning the symbol.
QPointF mOffset
Marker offset.
void setHorizontalAnchorPoint(HorizontalAnchorPoint h)
Sets the horizontal anchor point for positioning the symbol.
QgsMapUnitScale mapUnitScale() const override
void setOffset(QPointF offset)
Sets the marker's offset, which is the horizontal and vertical displacement which the rendered marker...
void setSizeMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale for the symbol's size.
double size() const
Returns the symbol size.
QgsMapUnitScale mOffsetMapUnitScale
Offset map unit scale.
HorizontalAnchorPoint mHorizontalAnchorPoint
Horizontal anchor point.
static QPointF _rotatedOffset(QPointF offset, double angle)
Adjusts a marker offset to account for rotation.
Qgis::ScaleMethod mScaleMethod
Marker size scaling method.
QgsMapUnitScale mSizeMapUnitScale
Marker size map unit scale.
Qgis::RenderUnit mSizeUnit
Marker size unit.