QGIS API Documentation 4.1.0-Master (60fea48833c)
Loading...
Searching...
No Matches
qgsshapegenerator.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsshapegenerator.cpp
3 ----------------
4 begin : March 2021
5 copyright : (C) 2021 Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsshapegenerator.h"
19
20#include <algorithm>
21
22#include "qgsgeometryutils.h"
23
24#include <QLineF>
25#include <QList>
26#include <QPainterPath>
27
28QLineF segment( int index, QRectF rect, double radius )
29{
30 const int yMultiplier = rect.height() < 0 ? -1 : 1;
31 switch ( index )
32 {
33 case 0:
34 return QLineF( rect.left() + radius, rect.top(), rect.right() - radius, rect.top() );
35 case 1:
36 return QLineF( rect.right(), rect.top() + yMultiplier * radius, rect.right(), rect.bottom() - yMultiplier * radius );
37 case 2:
38 return QLineF( rect.right() - radius, rect.bottom(), rect.left() + radius, rect.bottom() );
39 case 3:
40 return QLineF( rect.left(), rect.bottom() - yMultiplier * radius, rect.left(), rect.top() + yMultiplier * radius );
41 default:
42 return QLineF();
43 }
44}
45
46QPolygonF QgsShapeGenerator::createBalloon( const QgsPointXY &origin, const QRectF &rect, double wedgeWidth )
47{
48 return createBalloon( origin, rect, wedgeWidth, 0 ).toFillPolygon();
49}
50
51QPainterPath QgsShapeGenerator::createBalloon( const QgsPointXY &origin, const QRectF &rect, double wedgeWidth, double cornerRadius )
52{
53 int balloonSegment = -1;
54 QPointF balloonSegmentPoint1;
55 QPointF balloonSegmentPoint2;
56
57 const bool invertedY = rect.height() < 0;
58
59 cornerRadius = std::min( cornerRadius, std::min( std::fabs( rect.height() ), rect.width() ) / 2.0 );
60
61 //first test if the point is in the frame. In that case we don't need a balloon and can just use a rect
62 if ( rect.contains( origin.toQPointF() ) )
63 {
64 balloonSegment = -1;
65 }
66 else
67 {
68 //edge list
69 QList<QLineF> segmentList;
70 segmentList << segment( 0, rect, cornerRadius );
71 segmentList << segment( 1, rect, cornerRadius );
72 segmentList << segment( 2, rect, cornerRadius );
73 segmentList << segment( 3, rect, cornerRadius );
74
75 // find closest edge / closest edge point
76 double minEdgeDist = std::numeric_limits<double>::max();
77 int minEdgeIndex = -1;
78 QLineF minEdge;
79 QgsPointXY minEdgePoint( 0, 0 );
80
81 for ( int i = 0; i < 4; ++i )
82 {
83 QLineF currentSegment = segmentList.at( i );
84 QgsPointXY currentMinDistPoint;
85 double currentMinDist = origin.sqrDistToSegment( currentSegment.x1(), currentSegment.y1(), currentSegment.x2(), currentSegment.y2(), currentMinDistPoint );
86 bool isPreferredSegment = false;
87 if ( qgsDoubleNear( currentMinDist, minEdgeDist ) )
88 {
89 // two segments are close - work out which looks nicer
90 const double angle = fmod( origin.azimuth( currentMinDistPoint ) + 360.0, 360.0 );
91 if ( angle < 45 || angle > 315 )
92 isPreferredSegment = i == 0;
93 else if ( angle < 135 )
94 isPreferredSegment = i == 3;
95 else if ( angle < 225 )
96 isPreferredSegment = i == 2;
97 else
98 isPreferredSegment = i == 1;
99 }
100 else if ( currentMinDist < minEdgeDist )
101 isPreferredSegment = true;
102
103 if ( isPreferredSegment )
104 {
105 minEdgeIndex = i;
106 minEdgePoint = currentMinDistPoint;
107 minEdgeDist = currentMinDist;
108 minEdge = currentSegment;
109 }
110 }
111
112 if ( minEdgeIndex >= 0 )
113 {
114 balloonSegment = minEdgeIndex;
115 QPointF minEdgeEnd = minEdge.p2();
116 balloonSegmentPoint1 = QPointF( minEdgePoint.x(), minEdgePoint.y() );
117
118 const double segmentLength = minEdge.length();
119 const double clampedWedgeWidth = std::clamp( wedgeWidth, 0.0, segmentLength );
120 if ( std::sqrt( minEdgePoint.sqrDist( minEdgeEnd.x(), minEdgeEnd.y() ) ) < clampedWedgeWidth )
121 {
122 double x = 0;
123 double y = 0;
124 QgsGeometryUtilsBase::pointOnLineWithDistance( minEdge.p2().x(), minEdge.p2().y(), minEdge.p1().x(), minEdge.p1().y(), clampedWedgeWidth, x, y );
125 balloonSegmentPoint1 = QPointF( x, y );
126 }
127
128 {
129 double x = 0;
130 double y = 0;
131 QgsGeometryUtilsBase::pointOnLineWithDistance( balloonSegmentPoint1.x(), balloonSegmentPoint1.y(), minEdge.p2().x(), minEdge.p2().y(), clampedWedgeWidth, x, y );
132 balloonSegmentPoint2 = QPointF( x, y );
133 }
134 }
135 }
136
137 QPainterPath path;
138 QPointF p0;
139 QPointF p1;
140 for ( int i = 0; i < 4; ++i )
141 {
142 QLineF currentSegment = segment( i, rect, cornerRadius );
143 if ( i == 0 )
144 {
145 p0 = currentSegment.p1();
146 path.moveTo( currentSegment.p1() );
147 }
148 else
149 {
150 if ( invertedY )
151 path.arcTo( std::min( p1.x(), currentSegment.p1().x() ), std::min( p1.y(), currentSegment.p1().y() ), cornerRadius, cornerRadius, i == 1 ? -90 : ( i == 2 ? 0 : 90 ), 90 );
152 else
153 path.arcTo( std::min( p1.x(), currentSegment.p1().x() ), std::min( p1.y(), currentSegment.p1().y() ), cornerRadius, cornerRadius, i == 1 ? 90 : ( i == 2 ? 0 : -90 ), -90 );
154 }
155
156 if ( i == balloonSegment )
157 {
158 path.lineTo( balloonSegmentPoint1 );
159 path.lineTo( origin.toQPointF() );
160 path.lineTo( balloonSegmentPoint2 );
161 }
162
163 p1 = currentSegment.p2();
164 path.lineTo( p1 );
165 }
166
167 if ( invertedY )
168 path.arcTo( std::min( p1.x(), p0.x() ), std::min( p1.y(), p0.y() ), cornerRadius, cornerRadius, 180, 90 );
169 else
170 path.arcTo( std::min( p1.x(), p0.x() ), std::min( p1.y(), p0.y() ), cornerRadius, cornerRadius, -180, -90 );
171
172 return path;
173}
static void pointOnLineWithDistance(double x1, double y1, double x2, double y2, double distance, double &x, double &y, double *z1=nullptr, double *z2=nullptr, double *z=nullptr, double *m1=nullptr, double *m2=nullptr, double *m=nullptr)
Calculates the point a specified distance from (x1, y1) toward a second point (x2,...
Represents a 2D point.
Definition qgspointxy.h:62
double sqrDist(double x, double y) const
Returns the squared distance between this point a specified x, y coordinate.
Definition qgspointxy.h:189
double azimuth(const QgsPointXY &other) const
Calculates azimuth between this point and other one (clockwise in degree, starting from north).
double y
Definition qgspointxy.h:66
double x
Definition qgspointxy.h:65
double sqrDistToSegment(double x1, double y1, double x2, double y2, QgsPointXY &minDistPoint, double epsilon=Qgis::DEFAULT_SEGMENT_EPSILON) const
Returns the minimum distance between this point and a segment.
QPointF toQPointF() const
Converts a point to a QPointF.
Definition qgspointxy.h:168
static QPolygonF createBalloon(const QgsPointXY &origin, const QRectF &rect, double wedgeWidth)
Generates a "balloon"/"talking bubble" style shape (as a QPolygonF).
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6975
QLineF segment(int index, QRectF rect, double radius)