24 mPen.setWidthF( 2.0 );
25 mPen.setColor( QColor( 0, 0, 0 ) );
26 mPen.setCapStyle( Qt::FlatCap );
27 mBrush.setStyle( Qt::SolidPattern );
46 attrVal = expression->
evaluate( &expressionContext );
54 const double val = attrVal.toDouble( &ok );
66 return std::max( size.width(), size.height() );
77 Q_UNUSED( attributes )
84 QPainter *p =
c.painter();
92 const double w = spu.width();
93 const double h = spu.height();
95 const double baseX = position.x();
96 const double baseY = position.y() - h;
98 QVector<QPointF> textPositions;
100 for (
int i = 0; i < nCategories; ++i )
104 textPositions.push_back( QPointF( baseX + ( w / nCategories ) * i + w / nCategories / 2.0, baseY + h / 2.0 ) );
108 textPositions.push_back( QPointF( baseX + w / 2.0, baseY + h / nCategories * i + w / nCategories / 2.0 ) );
116 p->setBrush( mBrush );
121 p->drawEllipse( baseX, baseY, w, h );
124 QList<QPointF> intersect;
125 const QPointF center( baseX + w / 2.0, baseY + h / 2.0 );
126 const double r1 = w / 2.0;
127 const double r2 = h / 2.0;
129 for (
int i = 1; i < nCategories; ++i )
133 lineEllipseIntersection( QPointF( baseX + w / nCategories * i, baseY ), QPointF( baseX + w / nCategories * i, baseY + h ), center, r1, r2, intersect );
137 lineEllipseIntersection( QPointF( baseX, baseY + h / nCategories * i ), QPointF( baseX + w, baseY + h / nCategories * i ), center, r1, r2, intersect );
139 if ( intersect.size() > 1 )
141 p->drawLine( intersect.at( 0 ), intersect.at( 1 ) );
147 p->drawRect( QRectF( baseX, baseY, w, h ) );
148 for (
int i = 1; i < nCategories; ++i )
152 p->drawLine( QPointF( baseX + w / nCategories * i, baseY ), QPointF( baseX + w / nCategories * i, baseY + h ) );
156 p->drawLine( QPointF( baseX, baseY + h / nCategories * i ), QPointF( baseX + w, baseY + h / nCategories * i ) );
163 triangle << QPointF( baseX, baseY + h ) << QPointF( baseX + w, baseY + h ) << QPointF( baseX + w / 2.0, baseY );
164 p->drawPolygon( triangle );
166 const QLineF triangleEdgeLeft( baseX + w / 2.0, baseY, baseX, baseY + h );
167 const QLineF triangleEdgeRight( baseX + w, baseY + h, baseX + w / 2.0, baseY );
168 QPointF intersectionPoint1, intersectionPoint2;
170 for (
int i = 1; i < nCategories; ++i )
174 const QLineF verticalLine( baseX + w / nCategories * i, baseY + h, baseX + w / nCategories * i, baseY );
175 if ( baseX + w / nCategories * i < baseX + w / 2.0 )
177 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
178 verticalLine.intersect( triangleEdgeLeft, &intersectionPoint1 );
180 verticalLine.intersects( triangleEdgeLeft, &intersectionPoint1 );
185 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
186 verticalLine.intersect( triangleEdgeRight, &intersectionPoint1 );
188 verticalLine.intersects( triangleEdgeRight, &intersectionPoint1 );
191 p->drawLine( QPointF( baseX + w / nCategories * i, baseY + h ), intersectionPoint1 );
195 const QLineF horizontalLine( baseX, baseY + h / nCategories * i, baseX + w, baseY + h / nCategories * i );
196 #if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
197 horizontalLine.intersect( triangleEdgeLeft, &intersectionPoint1 );
198 horizontalLine.intersect( triangleEdgeRight, &intersectionPoint2 );
200 horizontalLine.intersects( triangleEdgeLeft, &intersectionPoint1 );
201 horizontalLine.intersects( triangleEdgeRight, &intersectionPoint2 );
203 p->drawLine( intersectionPoint1, intersectionPoint2 );
210 const QFontMetricsF fontMetrics( sFont );
218 for (
int i = 0; i < textPositions.size(); ++i )
221 const QString val = expression->
evaluate( &expressionContext ).toString();
224 const double textWidth = fontMetrics.horizontalAdvance( val );
225 const double textHeight = fontMetrics.height();
229 const QPointF position = textPositions.at( i );
237 xOffset = textHeight / 2.0;
241 xOffset = fontMetrics.xHeight();
244 p->drawText( QPointF( position.x() - textWidth / 2.0, position.y() + xOffset ), val );
248 void QgsTextDiagram::lineEllipseIntersection( QPointF lineStart, QPointF lineEnd, QPointF ellipseMid,
double r1,
double r2, QList<QPointF> &result )
const
252 const double rrx = r1 * r1;
253 const double rry = r2 * r2;
254 const double x21 = lineEnd.x() - lineStart.x();
255 const double y21 = lineEnd.y() - lineStart.y();
256 const double x10 = lineStart.x() - ellipseMid.x();
257 const double y10 = lineStart.y() - ellipseMid.y();
258 const double a = x21 * x21 / rrx + y21 * y21 / rry;
259 const double b = x21 * x10 / rrx + y21 * y10 / rry;
260 const double c = x10 * x10 / rrx + y10 * y10 / rry;
261 const double d = b * b - a * (
c - 1 );
264 const double e = std::sqrt( d );
265 const double u1 = ( -b - e ) / a;
266 const double u2 = ( -b + e ) / a;
268 if ( -0.00001 <= u1 && u1 < 1.00001 )
270 result.push_back( QPointF( lineStart.x() + x21 * u1, lineStart.y() + y21 * u1 ) );
272 if ( -0.00001 <= u2 && u2 <= 1.00001 )
274 result.push_back( QPointF( lineStart.x() + x21 * u2, lineStart.y() + y21 * u2 ) );
Additional diagram settings for interpolated size rendering.
bool classificationAttributeIsExpression
QString classificationField
Name of the field for classification.
QString classificationAttributeExpression
Stores the settings for rendering a single diagram.
LabelPlacementMethod labelPlacementMethod
QList< QString > categoryAttributes
QList< QColor > categoryColors
void setPenWidth(QPen &pen, const QgsDiagramSettings &s, const QgsRenderContext &c)
Changes the pen width to match the current settings and rendering context.
QSizeF sizePainterUnits(QSizeF size, const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
QgsExpression * getExpression(const QString &expression, const QgsExpressionContext &context)
Returns a prepared expression for the specified context.
QSizeF sizeForValue(double value, const QgsDiagramSettings &s, const QgsDiagramInterpolationSettings &is) const
Returns the scaled size of a diagram for a value, respecting the specified diagram interpolation sett...
QFont scaledFont(const QgsDiagramSettings &s, const QgsRenderContext &c)
Calculates a size to match the current settings and rendering context.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
Class for parsing and evaluation of expressions (formerly called "search strings").
QVariant evaluate()
Evaluate the feature and return the result.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
QVariant attribute(const QString &name) const
Lookup attribute value by attribute name.
bool isEmpty() const
Checks whether the container is empty.
Contains information about the context of a rendering operation.
void renderDiagram(const QgsFeature &feature, QgsRenderContext &c, const QgsDiagramSettings &s, QPointF position) override
Draws the diagram at the given position (in pixel coordinates)
QgsTextDiagram * clone() const override
Returns an instance that is equivalent to this one.
QSizeF diagramSize(const QgsAttributes &attributes, const QgsRenderContext &c, const QgsDiagramSettings &s) override
Returns the size in map units the diagram will use to render.
QString diagramName() const override
Gets a descriptive name for this diagram type.
double legendSize(double value, const QgsDiagramSettings &s, const QgsDiagramInterpolationSettings &is) const override
Returns the size of the legend item for the diagram corresponding to a specified value.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define DIAGRAM_NAME_TEXT