19#include "moc_qgslayoutitemmapgrid.cpp"
48#define MAX_GRID_LINES 1000
79 return qobject_cast<QgsLayoutItemMapGrid *>(
item );
85 return qobject_cast<QgsLayoutItemMapGrid *>(
item );
90 QList< QgsLayoutItemMapGrid * > list;
113 const QDomNodeList mapGridNodeList = elem.elementsByTagName( QStringLiteral(
"ComposerMapGrid" ) );
114 for (
int i = 0; i < mapGridNodeList.size(); ++i )
116 const QDomElement mapGridElem = mapGridNodeList.at( i ).toElement();
118 mapGrid->
readXml( mapGridElem, doc, context );
132 return std::max( std::max( std::max( top, right ), bottom ), left );
146 double gridTop = 0.0;
147 double gridRight = 0.0;
148 double gridBottom = 0.0;
149 double gridLeft = 0.0;
151 top = std::max( top, gridTop );
152 right = std::max( right, gridRight );
153 bottom = std::max( bottom, gridBottom );
154 left = std::max( left, gridLeft );
170 return QVector2D( 0, 1 );
172 return QVector2D( -1, 0 );
174 return QVector2D( 0, -1 );
176 return QVector2D( 1, 0 );
184 return QVector2D( borderVector.y(), -borderVector.x() );
194 const QString defaultFontString = settings.
value( QStringLiteral(
"LayoutDesigner/defaultFont" ), QVariant(),
QgsSettings::Gui ).toString();
195 if ( !defaultFontString.isEmpty() )
199 mAnnotationFormat.
setFont( font );
202 createDefaultGridLineSymbol();
203 createDefaultGridMarkerSymbol();
216void QgsLayoutItemMapGrid::createDefaultGridLineSymbol()
218 QVariantMap properties;
219 properties.insert( QStringLiteral(
"color" ), QStringLiteral(
"0,0,0,255" ) );
220 properties.insert( QStringLiteral(
"width" ), QStringLiteral(
"0.3" ) );
221 properties.insert( QStringLiteral(
"capstyle" ), QStringLiteral(
"flat" ) );
225void QgsLayoutItemMapGrid::createDefaultGridMarkerSymbol()
227 QVariantMap properties;
228 properties.insert( QStringLiteral(
"name" ), QStringLiteral(
"circle" ) );
229 properties.insert( QStringLiteral(
"size" ), QStringLiteral(
"2.0" ) );
230 properties.insert( QStringLiteral(
"color" ), QStringLiteral(
"0,0,0,255" ) );
236 if ( mGridLineSymbol )
238 mGridLineSymbol->setWidth( width );
244 if ( mGridLineSymbol )
246 mGridLineSymbol->setColor(
c );
257 QDomElement mapGridElem = doc.createElement( QStringLiteral(
"ComposerMapGrid" ) );
258 mapGridElem.setAttribute( QStringLiteral(
"gridStyle" ), mGridStyle );
259 mapGridElem.setAttribute( QStringLiteral(
"intervalX" ),
qgsDoubleToString( mGridIntervalX ) );
260 mapGridElem.setAttribute( QStringLiteral(
"intervalY" ),
qgsDoubleToString( mGridIntervalY ) );
261 mapGridElem.setAttribute( QStringLiteral(
"offsetX" ),
qgsDoubleToString( mGridOffsetX ) );
262 mapGridElem.setAttribute( QStringLiteral(
"offsetY" ),
qgsDoubleToString( mGridOffsetY ) );
263 mapGridElem.setAttribute( QStringLiteral(
"crossLength" ),
qgsDoubleToString( mCrossLength ) );
265 QDomElement lineStyleElem = doc.createElement( QStringLiteral(
"lineStyle" ) );
267 lineStyleElem.appendChild( gridLineStyleElem );
268 mapGridElem.appendChild( lineStyleElem );
270 QDomElement markerStyleElem = doc.createElement( QStringLiteral(
"markerStyle" ) );
272 markerStyleElem.appendChild( gridMarkerStyleElem );
273 mapGridElem.appendChild( markerStyleElem );
275 mapGridElem.setAttribute( QStringLiteral(
"gridFrameStyle" ), mGridFrameStyle );
276 mapGridElem.setAttribute( QStringLiteral(
"gridFrameSideFlags" ), mGridFrameSides );
277 mapGridElem.setAttribute( QStringLiteral(
"gridFrameWidth" ),
qgsDoubleToString( mGridFrameWidth ) );
278 mapGridElem.setAttribute( QStringLiteral(
"gridFrameMargin" ),
qgsDoubleToString( mGridFrameMargin ) );
279 mapGridElem.setAttribute( QStringLiteral(
"gridFramePenThickness" ),
qgsDoubleToString( mGridFramePenThickness ) );
283 mapGridElem.setAttribute( QStringLiteral(
"leftFrameDivisions" ), mLeftFrameDivisions );
284 mapGridElem.setAttribute( QStringLiteral(
"rightFrameDivisions" ), mRightFrameDivisions );
285 mapGridElem.setAttribute( QStringLiteral(
"topFrameDivisions" ), mTopFrameDivisions );
286 mapGridElem.setAttribute( QStringLiteral(
"bottomFrameDivisions" ), mBottomFrameDivisions );
287 mapGridElem.setAttribute( QStringLiteral(
"rotatedTicksLengthMode" ), mRotatedTicksLengthMode );
288 mapGridElem.setAttribute( QStringLiteral(
"rotatedTicksEnabled" ), mRotatedTicksEnabled );
289 mapGridElem.setAttribute( QStringLiteral(
"rotatedTicksMinimumAngle" ), QString::number( mRotatedTicksMinimumAngle ) );
290 mapGridElem.setAttribute( QStringLiteral(
"rotatedTicksMarginToCorner" ), QString::number( mRotatedTicksMarginToCorner ) );
291 mapGridElem.setAttribute( QStringLiteral(
"rotatedAnnotationsLengthMode" ), mRotatedAnnotationsLengthMode );
292 mapGridElem.setAttribute( QStringLiteral(
"rotatedAnnotationsEnabled" ), mRotatedAnnotationsEnabled );
293 mapGridElem.setAttribute( QStringLiteral(
"rotatedAnnotationsMinimumAngle" ), QString::number( mRotatedAnnotationsMinimumAngle ) );
294 mapGridElem.setAttribute( QStringLiteral(
"rotatedAnnotationsMarginToCorner" ), QString::number( mRotatedAnnotationsMarginToCorner ) );
300 mapGridElem.setAttribute( QStringLiteral(
"annotationFormat" ), mGridAnnotationFormat );
301 mapGridElem.setAttribute( QStringLiteral(
"showAnnotation" ), mShowGridAnnotation );
302 mapGridElem.setAttribute( QStringLiteral(
"annotationExpression" ), mGridAnnotationExpressionString );
303 mapGridElem.setAttribute( QStringLiteral(
"leftAnnotationDisplay" ), mLeftGridAnnotationDisplay );
304 mapGridElem.setAttribute( QStringLiteral(
"rightAnnotationDisplay" ), mRightGridAnnotationDisplay );
305 mapGridElem.setAttribute( QStringLiteral(
"topAnnotationDisplay" ), mTopGridAnnotationDisplay );
306 mapGridElem.setAttribute( QStringLiteral(
"bottomAnnotationDisplay" ), mBottomGridAnnotationDisplay );
307 mapGridElem.setAttribute( QStringLiteral(
"leftAnnotationPosition" ), mLeftGridAnnotationPosition );
308 mapGridElem.setAttribute( QStringLiteral(
"rightAnnotationPosition" ), mRightGridAnnotationPosition );
309 mapGridElem.setAttribute( QStringLiteral(
"topAnnotationPosition" ), mTopGridAnnotationPosition );
310 mapGridElem.setAttribute( QStringLiteral(
"bottomAnnotationPosition" ), mBottomGridAnnotationPosition );
311 mapGridElem.setAttribute( QStringLiteral(
"leftAnnotationDirection" ), mLeftGridAnnotationDirection );
312 mapGridElem.setAttribute( QStringLiteral(
"rightAnnotationDirection" ), mRightGridAnnotationDirection );
313 mapGridElem.setAttribute( QStringLiteral(
"topAnnotationDirection" ), mTopGridAnnotationDirection );
314 mapGridElem.setAttribute( QStringLiteral(
"bottomAnnotationDirection" ), mBottomGridAnnotationDirection );
315 mapGridElem.setAttribute( QStringLiteral(
"frameAnnotationDistance" ), QString::number( mAnnotationFrameDistance ) );
316 mapGridElem.appendChild( mAnnotationFormat.
writeXml( doc, context ) );
317 mapGridElem.setAttribute( QStringLiteral(
"annotationPrecision" ), mGridAnnotationPrecision );
318 mapGridElem.setAttribute( QStringLiteral(
"unit" ), mGridUnit );
319 mapGridElem.setAttribute( QStringLiteral(
"blendMode" ), mBlendMode );
320 mapGridElem.setAttribute( QStringLiteral(
"minimumIntervalWidth" ), QString::number( mMinimumIntervalWidth ) );
321 mapGridElem.setAttribute( QStringLiteral(
"maximumIntervalWidth" ), QString::number( mMaximumIntervalWidth ) );
324 elem.appendChild( mapGridElem );
331 if ( itemElem.isNull() )
340 mGridIntervalX = itemElem.attribute( QStringLiteral(
"intervalX" ), QStringLiteral(
"0" ) ).toDouble();
341 mGridIntervalY = itemElem.attribute( QStringLiteral(
"intervalY" ), QStringLiteral(
"0" ) ).toDouble();
342 mGridOffsetX = itemElem.attribute( QStringLiteral(
"offsetX" ), QStringLiteral(
"0" ) ).toDouble();
343 mGridOffsetY = itemElem.attribute( QStringLiteral(
"offsetY" ), QStringLiteral(
"0" ) ).toDouble();
344 mCrossLength = itemElem.attribute( QStringLiteral(
"crossLength" ), QStringLiteral(
"3" ) ).toDouble();
345 mGridFrameStyle =
static_cast< QgsLayoutItemMapGrid::FrameStyle >( itemElem.attribute( QStringLiteral(
"gridFrameStyle" ), QStringLiteral(
"0" ) ).toInt() );
347 mGridFrameWidth = itemElem.attribute( QStringLiteral(
"gridFrameWidth" ), QStringLiteral(
"2.0" ) ).toDouble();
348 mGridFrameMargin = itemElem.attribute( QStringLiteral(
"gridFrameMargin" ), QStringLiteral(
"0.0" ) ).toDouble();
349 mGridFramePenThickness = itemElem.attribute( QStringLiteral(
"gridFramePenThickness" ), QStringLiteral(
"0.3" ) ).toDouble();
351 mGridFrameFillColor1 =
QgsColorUtils::colorFromString( itemElem.attribute( QStringLiteral(
"frameFillColor1" ), QStringLiteral(
"255,255,255,255" ) ) );
352 mGridFrameFillColor2 =
QgsColorUtils::colorFromString( itemElem.attribute( QStringLiteral(
"frameFillColor2" ), QStringLiteral(
"0,0,0,255" ) ) );
357 mRotatedTicksLengthMode =
TickLengthMode( itemElem.attribute( QStringLiteral(
"rotatedTicksLengthMode" ), QStringLiteral(
"0" ) ).toInt() );
358 mRotatedTicksEnabled = itemElem.attribute( QStringLiteral(
"rotatedTicksEnabled" ), QStringLiteral(
"0" ) ) != QLatin1String(
"0" );
359 mRotatedTicksMinimumAngle = itemElem.attribute( QStringLiteral(
"rotatedTicksMinimumAngle" ), QStringLiteral(
"0" ) ).toDouble();
360 mRotatedTicksMarginToCorner = itemElem.attribute( QStringLiteral(
"rotatedTicksMarginToCorner" ), QStringLiteral(
"0" ) ).toDouble();
361 mRotatedAnnotationsLengthMode =
TickLengthMode( itemElem.attribute( QStringLiteral(
"rotatedAnnotationsLengthMode" ), QStringLiteral(
"0" ) ).toInt() );
362 mRotatedAnnotationsEnabled = itemElem.attribute( QStringLiteral(
"rotatedAnnotationsEnabled" ), QStringLiteral(
"0" ) ) != QLatin1String(
"0" );
363 mRotatedAnnotationsMinimumAngle = itemElem.attribute( QStringLiteral(
"rotatedAnnotationsMinimumAngle" ), QStringLiteral(
"0" ) ).toDouble();
364 mRotatedAnnotationsMarginToCorner = itemElem.attribute( QStringLiteral(
"rotatedAnnotationsMarginToCorner" ), QStringLiteral(
"0" ) ).toDouble();
366 const QDomElement lineStyleElem = itemElem.firstChildElement( QStringLiteral(
"lineStyle" ) );
367 if ( !lineStyleElem.isNull() )
369 const QDomElement symbolElem = lineStyleElem.firstChildElement( QStringLiteral(
"symbol" ) );
370 if ( !symbolElem.isNull() )
372 mGridLineSymbol = QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( symbolElem, context );
379 mGridLineSymbol->setWidth( itemElem.attribute( QStringLiteral(
"penWidth" ), QStringLiteral(
"0" ) ).toDouble() );
380 mGridLineSymbol->setColor( QColor( itemElem.attribute( QStringLiteral(
"penColorRed" ), QStringLiteral(
"0" ) ).toInt(),
381 itemElem.attribute( QStringLiteral(
"penColorGreen" ), QStringLiteral(
"0" ) ).toInt(),
382 itemElem.attribute( QStringLiteral(
"penColorBlue" ), QStringLiteral(
"0" ) ).toInt() ) );
385 const QDomElement markerStyleElem = itemElem.firstChildElement( QStringLiteral(
"markerStyle" ) );
386 if ( !markerStyleElem.isNull() )
388 const QDomElement symbolElem = markerStyleElem.firstChildElement( QStringLiteral(
"symbol" ) );
389 if ( !symbolElem.isNull() )
391 mGridMarkerSymbol = QgsSymbolLayerUtils::loadSymbol<QgsMarkerSymbol>( symbolElem, context );
395 if ( !mCRS.
readXml( itemElem ) )
398 mBlendMode =
static_cast< QPainter::CompositionMode
>( itemElem.attribute( QStringLiteral(
"blendMode" ), QStringLiteral(
"0" ) ).toUInt() );
401 mShowGridAnnotation = ( itemElem.attribute( QStringLiteral(
"showAnnotation" ), QStringLiteral(
"0" ) ) != QLatin1String(
"0" ) );
403 mGridAnnotationExpressionString = itemElem.attribute( QStringLiteral(
"annotationExpression" ) );
404 mGridAnnotationExpression.reset();
409 mLeftGridAnnotationDisplay =
QgsLayoutItemMapGrid::DisplayMode( itemElem.attribute( QStringLiteral(
"leftAnnotationDisplay" ), QStringLiteral(
"0" ) ).toInt() );
410 mRightGridAnnotationDisplay =
QgsLayoutItemMapGrid::DisplayMode( itemElem.attribute( QStringLiteral(
"rightAnnotationDisplay" ), QStringLiteral(
"0" ) ).toInt() );
412 mBottomGridAnnotationDisplay =
QgsLayoutItemMapGrid::DisplayMode( itemElem.attribute( QStringLiteral(
"bottomAnnotationDisplay" ), QStringLiteral(
"0" ) ).toInt() );
418 mAnnotationFrameDistance = itemElem.attribute( QStringLiteral(
"frameAnnotationDistance" ), QStringLiteral(
"0" ) ).toDouble();
420 if ( !itemElem.firstChildElement(
"text-style" ).isNull() )
422 mAnnotationFormat.
readXml( itemElem, context );
429 font.fromString( itemElem.attribute(
"annotationFont", QString() ) );
431 mAnnotationFormat.
setFont( font );
432 mAnnotationFormat.
setSize( font.pointSizeF() );
437 mGridAnnotationPrecision = itemElem.attribute( QStringLiteral(
"annotationPrecision" ), QStringLiteral(
"3" ) ).toInt();
438 const int gridUnitInt = itemElem.attribute( QStringLiteral(
"unit" ), QString::number(
MapUnit ) ).toInt();
440 mMinimumIntervalWidth = itemElem.attribute( QStringLiteral(
"minimumIntervalWidth" ), QStringLiteral(
"50" ) ).toDouble();
441 mMaximumIntervalWidth = itemElem.attribute( QStringLiteral(
"maximumIntervalWidth" ), QStringLiteral(
"100" ) ).toDouble();
443 refreshDataDefinedProperties();
453 mTransformDirty =
true;
459 return mBlendMode != QPainter::CompositionMode_SourceOver;
462QPolygonF QgsLayoutItemMapGrid::scalePolygon(
const QPolygonF &polygon,
const double scale )
const
464 const QTransform t = QTransform::fromScale( scale, scale );
465 return t.map( polygon );
468void QgsLayoutItemMapGrid::drawGridCrsTransform(
QgsRenderContext &context,
double dotsPerMM,
bool calculateLinesOnly )
const
470 if ( !
mMap || !mEvaluatedEnabled )
477 if ( mapPolygon != mPrevMapPolygon )
479 mTransformDirty =
true;
480 mPrevMapPolygon = mapPolygon;
483 if ( mTransformDirty )
485 calculateCrsTransformLines();
489 if ( !calculateLinesOnly )
493 QList< GridLine >::const_iterator gridIt = mGridLines.constBegin();
494 for ( ; gridIt != mGridLines.constEnd(); ++gridIt )
498 drawGridLine( scalePolygon( gridIt->line, dotsPerMM ), context );
503 const double maxX =
mMap->rect().width();
504 const double maxY =
mMap->rect().height();
506 QList< QgsPointXY >::const_iterator intersectionIt = mTransformedIntersections.constBegin();
507 for ( ; intersectionIt != mTransformedIntersections.constEnd(); ++intersectionIt )
509 const double x = intersectionIt->x();
510 const double y = intersectionIt->y();
514 const QLineF line1 = QLineF( x - mEvaluatedCrossLength, y, x + mEvaluatedCrossLength, y );
515 line1.p1().rx() = line1.p1().x() < 0 ? 0 : line1.p1().x();
516 line1.p2().rx() = line1.p2().x() > maxX ? maxX : line1.p2().x();
517 const QLineF line2 = QLineF( x, y - mEvaluatedCrossLength, x, y + mEvaluatedCrossLength );
518 line2.p1().ry() = line2.p1().y() < 0 ? 0 : line2.p1().y();
519 line2.p2().ry() = line2.p2().y() > maxY ? maxY : line2.p2().y();
522 drawGridLine( QLineF( line1.p1() * dotsPerMM, line1.p2() * dotsPerMM ), context );
523 drawGridLine( QLineF( line2.p1() * dotsPerMM, line2.p2() * dotsPerMM ), context );
527 drawGridMarker( QPointF( x, y ) * dotsPerMM, context );
534void QgsLayoutItemMapGrid::calculateCrsTransformLines()
const
538 if ( crsGridParams( crsBoundingRect, inverseTr ) != 0 )
545 xGridLinesCrsTransform( crsBoundingRect, inverseTr );
546 yGridLinesCrsTransform( crsBoundingRect, inverseTr );
553 QList< QgsGeometry > xLines;
554 QList< QgsGeometry > yLines;
555 QList< GridLine >::const_iterator gridIt = mGridLines.constBegin();
556 for ( ; gridIt != mGridLines.constEnd(); ++gridIt )
560 for (
int i = 0; i < gridIt->line.size(); ++i )
562 line.append(
QgsPointXY( gridIt->line.at( i ).x(), gridIt->line.at( i ).y() ) );
571 mTransformedIntersections.clear();
572 QList< QgsGeometry >::const_iterator yLineIt = yLines.constBegin();
573 for ( ; yLineIt != yLines.constEnd(); ++yLineIt )
575 QList< QgsGeometry >::const_iterator xLineIt = xLines.constBegin();
576 for ( ; xLineIt != xLines.constEnd(); ++xLineIt )
580 if ( intersects.
isNull() )
588 mTransformedIntersections << vertex;
596 mTransformDirty =
false;
601 if ( !
mMap || !mEvaluatedEnabled )
605 QPaintDevice *paintDevice = p->device();
612 p->setCompositionMode( mBlendMode );
615 const QRectF thisPaintRect = QRectF( 0, 0,
mMap->rect().width(),
mMap->rect().height() );
616 p->setClipRect( thisPaintRect );
617 if ( thisPaintRect != mPrevPaintRect )
620 mTransformDirty =
true;
621 mPrevPaintRect = thisPaintRect;
625 const double dotsPerMM = paintDevice->logicalDpiX() / 25.4;
626 p->scale( 1 / dotsPerMM, 1 / dotsPerMM );
642 drawGridCrsTransform( context, dotsPerMM );
649 drawGridNoTransform( context, dotsPerMM );
654 p->setClipping(
false );
658 p->setClipRect(
mMap->mapRectFromScene(
mMap->sceneBoundingRect() ).adjusted( -10, -10, 10, 10 ) );
663 updateGridLinesAnnotationsPositions();
670 if ( mShowGridAnnotation )
676void QgsLayoutItemMapGrid::updateGridLinesAnnotationsPositions()
const
678 QList< GridLine >::iterator it = mGridLines.begin();
679 for ( ; it != mGridLines.end(); ++it )
681 it->startAnnotation.border = borderForLineCoord( it->line.first(), it->coordinateType );
682 it->endAnnotation.border = borderForLineCoord( it->line.last(), it->coordinateType );
683 it->startAnnotation.position = QVector2D( it->line.first() );
684 it->endAnnotation.position = QVector2D( it->line.last() );
685 it->startAnnotation.vector = QVector2D( it->line.at( 1 ) - it->line.first() ).normalized();
686 it->endAnnotation.vector = QVector2D( it->line.at( it->line.count() - 2 ) - it->line.last() ).normalized();
688 it->startAnnotation.angle = atan2( it->startAnnotation.vector.x() * normS.y() - it->startAnnotation.vector.y() * normS.x(), it->startAnnotation.vector.x() * normS.x() + it->startAnnotation.vector.y() * normS.y() );
690 it->endAnnotation.angle = atan2( it->endAnnotation.vector.x() * normE.y() - it->endAnnotation.vector.y() * normE.x(), it->endAnnotation.vector.x() * normE.x() + it->endAnnotation.vector.y() * normE.y() );
694void QgsLayoutItemMapGrid::drawGridNoTransform(
QgsRenderContext &context,
double dotsPerMM,
bool calculateLinesOnly )
const
701 if ( calculateLinesOnly )
704 QList< GridLine >::const_iterator vIt = mGridLines.constBegin();
705 QList< GridLine >::const_iterator hIt = mGridLines.constBegin();
713 for ( ; vIt != mGridLines.constEnd(); ++vIt )
717 line = QLineF( vIt->line.first() * dotsPerMM, vIt->line.last() * dotsPerMM );
722 drawGridLine( line, context );
725 for ( ; hIt != mGridLines.constEnd(); ++hIt )
729 line = QLineF( hIt->line.first() * dotsPerMM, hIt->line.last() * dotsPerMM );
734 drawGridLine( line, context );
740 QPointF intersectionPoint, crossEnd1, crossEnd2;
741 for ( ; vIt != mGridLines.constEnd(); ++vIt )
746 l1 = QLineF( vIt->line.first(), vIt->line.last() );
749 hIt = mGridLines.constBegin();
750 for ( ; hIt != mGridLines.constEnd(); ++hIt )
755 l2 = QLineF( hIt->line.first(), hIt->line.last() );
757 if ( l2.intersects( l1, &intersectionPoint ) == QLineF::BoundedIntersection )
762 crossEnd1 = ( ( intersectionPoint - l1.p1() ).manhattanLength() > 0.01 ) ?
764 crossEnd2 = ( ( intersectionPoint - l1.p2() ).manhattanLength() > 0.01 ) ?
767 drawGridLine( QLineF( crossEnd1 * dotsPerMM, crossEnd2 * dotsPerMM ), context );
771 drawGridMarker( intersectionPoint * dotsPerMM, context );
783 hIt = mGridLines.constBegin();
784 for ( ; hIt != mGridLines.constEnd(); ++hIt )
789 l1 = QLineF( hIt->line.first(), hIt->line.last() );
791 vIt = mGridLines.constBegin();
792 for ( ; vIt != mGridLines.constEnd(); ++vIt )
797 l2 = QLineF( vIt->line.first(), vIt->line.last() );
799 if ( l2.intersects( l1, &intersectionPoint ) == QLineF::BoundedIntersection )
802 crossEnd1 = ( ( intersectionPoint - l1.p1() ).manhattanLength() > 0.01 ) ?
804 crossEnd2 = ( ( intersectionPoint - l1.p2() ).manhattanLength() > 0.01 ) ?
807 drawGridLine( QLineF( crossEnd1 * dotsPerMM, crossEnd2 * dotsPerMM ), context );
814void QgsLayoutItemMapGrid::drawGridFrame( QPainter *p, GridExtension *extension )
const
823 switch ( mGridFrameStyle )
827 drawGridFrameZebra( p, extension );
832 drawGridFrameTicks( p, extension );
837 drawGridFrameLine( p, extension );
848void QgsLayoutItemMapGrid::drawGridLine(
const QLineF &line,
QgsRenderContext &context )
const
851 poly << line.p1() << line.p2();
852 drawGridLine( poly, context );
855void QgsLayoutItemMapGrid::drawGridLine(
const QPolygonF &line,
QgsRenderContext &context )
const
862 mGridLineSymbol->startRender( context );
863 mGridLineSymbol->renderPolyline( line,
nullptr, context );
864 mGridLineSymbol->stopRender( context );
867void QgsLayoutItemMapGrid::drawGridMarker( QPointF point,
QgsRenderContext &context )
const
874 mGridMarkerSymbol->startRender( context );
875 mGridMarkerSymbol->renderPoint( point,
nullptr, context );
876 mGridMarkerSymbol->stopRender( context );
879void QgsLayoutItemMapGrid::drawGridFrameZebra( QPainter *p, GridExtension *extension )
const
899void QgsLayoutItemMapGrid::drawGridFrameZebraBorder( QPainter *p, BorderSide border,
double *extension )
const
908 *extension = mEvaluatedGridFrameMargin + mEvaluatedGridFrameWidth + mEvaluatedGridFrameLineThickness / 2.0;
912 double currentCoord = 0.0;
919 bool drawTLBox =
false;
920 bool drawTRBox =
false;
921 bool drawBLBox =
false;
922 bool drawBRBox =
false;
924 QMap< double, double > pos = QMap< double, double >();
925 QList< GridLine >::const_iterator it = mGridLines.constBegin();
926 for ( ; it != mGridLines.constEnd(); ++it )
929 for (
int i = 0 ; i < 2 ; ++i )
931 const GridLineAnnotation annot = ( i == 0 ) ? it->startAnnotation : it->endAnnotation;
934 if ( annot.border != border )
937 if ( ! shouldShowDivisionForSide( it->coordinateType, annot.border ) )
941 pos.insert( annot.position.y(), it->coordinate );
943 pos.insert( annot.position.x(), it->coordinate );
950 pos.insert(
mMap->rect().height(),
mMap->rect().height() );
966 pos.insert(
mMap->rect().width(),
mMap->rect().width() );
970 QPen framePen = QPen( mGridFramePenColor );
971 framePen.setWidthF( mEvaluatedGridFrameLineThickness );
972 framePen.setJoinStyle( Qt::MiterJoin );
973 p->setPen( framePen );
975 QMap< double, double >::const_iterator posIt = pos.constBegin();
976 for ( ; posIt != pos.constEnd(); ++posIt )
978 p->setBrush( QBrush( color1 ? mGridFrameFillColor1 : mGridFrameFillColor2 ) );
981 height = posIt.key() - currentCoord;
982 width = mEvaluatedGridFrameWidth;
983 x = ( border ==
QgsLayoutItemMapGrid::Left ) ? -( mEvaluatedGridFrameWidth + mEvaluatedGridFrameMargin ) :
mMap->rect().width() + mEvaluatedGridFrameMargin;
988 height = mEvaluatedGridFrameWidth;
989 width = posIt.key() - currentCoord;
991 y = ( border ==
QgsLayoutItemMapGrid::Top ) ? -( mEvaluatedGridFrameWidth + mEvaluatedGridFrameMargin ) :
mMap->rect().height() + mEvaluatedGridFrameMargin;
993 p->drawRect( QRectF( x, y, width, height ) );
994 currentCoord = posIt.key();
1001 width = height = ( mEvaluatedGridFrameWidth + mEvaluatedGridFrameMargin ) ;
1002 p->setBrush( QBrush( mGridFrameFillColor1 ) );
1004 p->drawRect( QRectF( -( mEvaluatedGridFrameWidth + mEvaluatedGridFrameMargin ), -( mEvaluatedGridFrameWidth + mEvaluatedGridFrameMargin ), width, height ) );
1006 p->drawRect( QRectF(
mMap->rect().width(), -( mEvaluatedGridFrameWidth + mEvaluatedGridFrameMargin ), width, height ) );
1008 p->drawRect( QRectF( -( mEvaluatedGridFrameWidth + mEvaluatedGridFrameMargin ),
mMap->rect().height(), width, height ) );
1010 p->drawRect( QRectF(
mMap->rect().width(),
mMap->rect().height(), width, height ) );
1014void QgsLayoutItemMapGrid::drawGridFrameTicks( QPainter *p, GridExtension *extension )
const
1024 QPen framePen = QPen( mGridFramePenColor );
1025 framePen.setWidthF( mEvaluatedGridFrameLineThickness );
1026 framePen.setCapStyle( Qt::FlatCap );
1027 p->setBrush( Qt::NoBrush );
1028 p->setPen( framePen );
1031 QList< GridLine >::iterator it = mGridLines.begin();
1032 for ( ; it != mGridLines.end(); ++it )
1035 for (
int i = 0 ; i < 2 ; ++i )
1037 const GridLineAnnotation annot = ( i == 0 ) ? it->startAnnotation : it->endAnnotation;
1039 if ( ! shouldShowDivisionForSide( it->coordinateType, annot.border ) )
1043 if ( abs( annot.angle ) / M_PI * 180.0 > 90.0 - mRotatedTicksMinimumAngle + 0.0001 )
1051 facingLeft = ( annot.angle != 0 );
1052 facingRight = ( annot.angle != 0 );
1056 facingLeft = ( annot.angle > 0 );
1057 facingRight = ( annot.angle < 0 );
1061 facingLeft = ( annot.angle < 0 );
1062 facingRight = ( annot.angle > 0 );
1065 if ( annot.border ==
BorderSide::Top && ( ( facingLeft && annot.position.x() < mRotatedTicksMarginToCorner ) ||
1066 ( facingRight && annot.position.x() >
mMap->rect().width() - mRotatedTicksMarginToCorner ) ) )
1068 if ( annot.border ==
BorderSide::Bottom && ( ( facingLeft && annot.position.x() >
mMap->rect().width() - mRotatedTicksMarginToCorner ) ||
1069 ( facingRight && annot.position.x() < mRotatedTicksMarginToCorner ) ) )
1071 if ( annot.border ==
BorderSide::Left && ( ( facingLeft && annot.position.y() >
mMap->rect().height() - mRotatedTicksMarginToCorner ) ||
1072 ( facingRight && annot.position.y() < mRotatedTicksMarginToCorner ) ) )
1074 if ( annot.border ==
BorderSide::Right && ( ( facingLeft && annot.position.y() < mRotatedTicksMarginToCorner ) ||
1075 ( facingRight && annot.position.y() >
mMap->rect().height() - mRotatedTicksMarginToCorner ) ) )
1079 const QVector2D vector = ( mRotatedTicksEnabled ) ? annot.vector : normalVector;
1081 double fA = mEvaluatedGridFrameMargin;
1082 double fB = mEvaluatedGridFrameMargin + mEvaluatedGridFrameWidth;
1084 if ( mRotatedTicksEnabled && mRotatedTicksLengthMode ==
OrthogonalTicks )
1086 fA /= QVector2D::dotProduct( vector, normalVector );
1087 fB /= QVector2D::dotProduct( vector, normalVector );
1094 extension->UpdateBorder( annot.border, fB );
1102 pA = annot.position + fA * vector;
1103 pB = annot.position + fB * vector;
1107 pA = annot.position - fA * vector;
1108 pB = annot.position - fB * vector;
1112 pA = annot.position - fB * vector;
1113 pB = annot.position + ( fB - 2.0 * mEvaluatedGridFrameMargin ) * vector;
1115 p->drawLine( QLineF( pA.toPointF(), pB.toPointF() ) );
1121void QgsLayoutItemMapGrid::drawGridFrameLine( QPainter *p, GridExtension *extension )
const
1131 QPen framePen = QPen( mGridFramePenColor );
1132 framePen.setWidthF( mEvaluatedGridFrameLineThickness );
1133 framePen.setCapStyle( Qt::SquareCap );
1134 p->setBrush( Qt::NoBrush );
1135 p->setPen( framePen );
1145 p->drawLine( QLineF( 0 - mEvaluatedGridFrameMargin, 0 - mEvaluatedGridFrameMargin, 0 - mEvaluatedGridFrameMargin,
mMap->rect().height() + mEvaluatedGridFrameMargin ) );
1153 p->drawLine( QLineF(
mMap->rect().width() + mEvaluatedGridFrameMargin, 0 - mEvaluatedGridFrameMargin,
mMap->rect().width() + mEvaluatedGridFrameMargin,
mMap->rect().height() + mEvaluatedGridFrameMargin ) );
1159 extension->UpdateBorder(
QgsLayoutItemMapGrid::Top, mEvaluatedGridFrameMargin + mEvaluatedGridFrameLineThickness / 2.0 );
1161 p->drawLine( QLineF( 0 - mEvaluatedGridFrameMargin, 0 - mEvaluatedGridFrameMargin,
mMap->rect().width() + mEvaluatedGridFrameMargin, 0 - mEvaluatedGridFrameMargin ) );
1169 p->drawLine( QLineF( 0 - mEvaluatedGridFrameMargin,
mMap->rect().height() + mEvaluatedGridFrameMargin,
mMap->rect().width() + mEvaluatedGridFrameMargin,
mMap->rect().height() + mEvaluatedGridFrameMargin ) );
1172 if ( ! extension && drawDiagonals )
1177 const double X1 = 0 - mEvaluatedGridFrameMargin + mEvaluatedGridFrameLineThickness / 2.0;
1178 const double Y1 = 0 - mEvaluatedGridFrameMargin + mEvaluatedGridFrameLineThickness / 2.0;
1179 p->drawLine( QLineF( 0, 0, X1, Y1 ) );
1184 const double X1 =
mMap->rect().width() + mEvaluatedGridFrameMargin - mEvaluatedGridFrameLineThickness / 2.0 ;
1185 const double Y1 =
mMap->rect().height() + mEvaluatedGridFrameMargin - mEvaluatedGridFrameLineThickness / 2.0 ;
1186 p->drawLine( QLineF(
mMap->rect().width(),
mMap->rect().height(), X1, Y1 ) );
1191 const double X1 =
mMap->rect().width() + mEvaluatedGridFrameMargin - mEvaluatedGridFrameLineThickness / 2.0 ;
1192 const double Y1 = 0 - mEvaluatedGridFrameMargin + mEvaluatedGridFrameLineThickness / 2.0 ;
1193 p->drawLine( QLineF(
mMap->rect().width(), 0, X1, Y1 ) );
1198 const double X1 = 0 - mEvaluatedGridFrameMargin + mEvaluatedGridFrameLineThickness / 2.0 ;
1199 const double Y1 =
mMap->rect().height() + mEvaluatedGridFrameMargin - mEvaluatedGridFrameLineThickness / 2.0 ;
1200 p->drawLine( QLineF( 0,
mMap->rect().height(), X1, Y1 ) );
1206 GridExtension *extension )
const
1208 QString currentAnnotationString;
1209 QList< GridLine >::const_iterator it = mGridLines.constBegin();
1210 for ( ; it != mGridLines.constEnd(); ++it )
1212 currentAnnotationString = gridAnnotationString( it->coordinate, it->coordinateType, expressionContext );
1213 drawCoordinateAnnotation( context, it->startAnnotation, currentAnnotationString, it->coordinateType, extension );
1214 drawCoordinateAnnotation( context, it->endAnnotation, currentAnnotationString, it->coordinateType, extension );
1218void QgsLayoutItemMapGrid::drawCoordinateAnnotation(
QgsRenderContext &context, GridLineAnnotation annot,
const QString &annotationString,
const AnnotationCoordinate coordinateType, GridExtension *extension )
const
1225 if ( ! shouldShowAnnotationForSide( coordinateType, annot.border ) )
1235 : (
QgsTextRenderer::textHeight( context, mAnnotationFormat,
'0', false ) ) ) / context.convertToPainterUnits( 1,
Qgis::RenderUnit::Millimeters );
1237 double xpos = annot.position.x();
1238 double ypos = annot.position.y();
1239 QPointF anchor = QPointF();
1246 if ( abs( annot.angle ) / M_PI * 180.0 > 90.0 - mRotatedAnnotationsMinimumAngle + 0.0001 )
1250 const QVector2D vector = ( mRotatedAnnotationsEnabled ) ? annot.vector : normalVector;
1253 double f = mEvaluatedAnnotationFrameDistance;
1261 f += mEvaluatedGridFrameWidth;
1262 if ( hasBorderWidth )
1263 f += mEvaluatedGridFrameLineThickness / 2.0;
1268 if ( mRotatedAnnotationsEnabled && mRotatedAnnotationsLengthMode ==
OrthogonalTicks )
1270 f /= QVector2D::dotProduct( vector, normalVector );
1273 const QVector2D pos = annot.position + f * vector;
1286 rotation = atan2( vector.y(), vector.x() ) / M_PI * 180;
1288 if ( rotation <= -90 || rotation > 90 )
1291 anchor.setX( outside ? 0 : textWidth );
1295 anchor.setX( outside ? textWidth : 0 );
1299 anchor.setY( 0.5 * textHeight );
1301 anchor.setY( -1.5 * textHeight );
1303 anchor.setY( -0.5 * textHeight );
1309 anchor.setX( 0.5 * textWidth );
1310 anchor.setY( -0.5 * textHeight );
1312 anchor.setY( outside ? 0 : -textHeight );
1314 anchor.setX( outside ? 0 : textWidth );
1316 anchor.setY( outside ? -textHeight : 0 );
1318 anchor.setX( outside ? textWidth : 0 );
1323 anchor.setX( 0.5 * textWidth );
1324 anchor.setY( -0.5 * textHeight );
1326 anchor.setX( outside ? 0 : textWidth );
1328 anchor.setY( outside ? -textHeight : 0 );
1330 anchor.setX( outside ? textWidth : 0 );
1332 anchor.setY( outside ? 0 : -textHeight );
1337 anchor.setX( 0.5 * textWidth );
1338 anchor.setY( -0.5 * textHeight );
1340 anchor.setX( outside ? textWidth : 0 );
1342 anchor.setY( outside ? 0 : -textHeight );
1344 anchor.setX( outside ? 0 : textWidth );
1346 anchor.setY( outside ? -textHeight : 0 );
1351 rotation = atan2( borderVector.y(), borderVector.x() ) / M_PI * 180;
1352 anchor.setX( 0.5 * textWidth );
1354 anchor.setY( -textHeight );
1362 extension->UpdateBorder( frameBorder, -f + textWidth );
1364 extension->UpdateAll( textWidth / 2.0 );
1367 if ( extension || !context.
painter() )
1371 bool facingLeft = ( annot.angle < 0 );
1372 bool facingRight = ( annot.angle > 0 );
1375 facingLeft = !facingLeft;
1376 facingRight = !facingRight;
1378 if ( annot.border ==
BorderSide::Top && ( ( facingLeft && annot.position.x() < mRotatedAnnotationsMarginToCorner ) ||
1379 ( facingRight && annot.position.x() >
mMap->rect().width() - mRotatedAnnotationsMarginToCorner ) ) )
1381 if ( annot.border ==
BorderSide::Bottom && ( ( facingLeft && annot.position.x() >
mMap->rect().width() - mRotatedAnnotationsMarginToCorner ) ||
1382 ( facingRight && annot.position.x() < mRotatedAnnotationsMarginToCorner ) ) )
1384 if ( annot.border ==
BorderSide::Left && ( ( facingLeft && annot.position.y() >
mMap->rect().height() - mRotatedAnnotationsMarginToCorner ) ||
1385 ( facingRight && annot.position.y() < mRotatedAnnotationsMarginToCorner ) ) )
1387 if ( annot.border ==
BorderSide::Right && ( ( facingLeft && annot.position.y() < mRotatedAnnotationsMarginToCorner ) ||
1388 ( facingRight && annot.position.y() >
mMap->rect().height() - mRotatedAnnotationsMarginToCorner ) ) )
1392 context.
painter()->translate( QPointF( xpos, ypos ) );
1393 context.
painter()->rotate( rotation );
1394 context.
painter()->translate( -anchor );
1402 bool geographic =
false;
1416 const double wrappedX = std::fmod( value, 360.0 );
1417 if ( wrappedX > 180.0 )
1419 value = wrappedX - 360.0;
1421 else if ( wrappedX < -180.0 )
1423 value = wrappedX + 360.0;
1429 return QString::number( value,
'f', mGridAnnotationPrecision );
1435 const double coordRounded =
qgsRound( value, mGridAnnotationPrecision );
1439 if ( !geographic || ( coordRounded != 180.0 && coordRounded != 0.0 ) )
1441 hemisphere = value < 0 ? QObject::tr(
"W" ) : QObject::tr(
"E" );
1447 if ( !geographic || coordRounded != 0.0 )
1449 hemisphere = value < 0 ? QObject::tr(
"S" ) : QObject::tr(
"N" );
1455 return QString::number( std::fabs( value ),
'f', mGridAnnotationPrecision ) + QChar( 176 ) + hemisphere;
1459 return QString::number( std::fabs( value ),
'f', mGridAnnotationPrecision ) + hemisphere;
1466 if ( !mGridAnnotationExpression )
1468 mGridAnnotationExpression.reset(
new QgsExpression( mGridAnnotationExpressionString ) );
1469 mGridAnnotationExpression->prepare( &expressionContext );
1471 return mGridAnnotationExpression->evaluate( &expressionContext ).toString();
1476 switch ( mGridAnnotationFormat )
1526int QgsLayoutItemMapGrid::xGridLines()
const
1528 if ( !
mMap || mEvaluatedIntervalY <= 0.0 )
1535 QRectF mapBoundingRect = mapPolygon.boundingRect();
1536 double gridIntervalY = mEvaluatedIntervalY;
1537 double gridOffsetY = mEvaluatedOffsetY;
1538 double annotationScale = 1.0;
1539 switch ( mGridUnit )
1544 mapBoundingRect =
mMap->rect();
1545 mapPolygon = QPolygonF(
mMap->rect() );
1546 if ( mGridUnit ==
CM )
1548 annotationScale = 0.1;
1549 gridIntervalY *= 10;
1561 const double roundCorrection = mapBoundingRect.top() > gridOffsetY ? 1.0 : 0.0;
1562 double currentLevel =
static_cast< int >( ( mapBoundingRect.top() - gridOffsetY ) / gridIntervalY + roundCorrection ) * gridIntervalY + gridOffsetY;
1564 int gridLineCount = 0;
1569 double yCanvasCoord;
1570 while ( currentLevel <= mapBoundingRect.bottom() && gridLineCount <
MAX_GRID_LINES )
1572 yCanvasCoord =
mMap->rect().height() * ( 1 - ( currentLevel - mapBoundingRect.top() ) / mapBoundingRect.height() );
1574 newLine.coordinate = currentLevel * annotationScale;
1576 newLine.line = QPolygonF() << QPointF( 0, yCanvasCoord ) << QPointF(
mMap->rect().width(), yCanvasCoord );
1577 mGridLines.append( newLine );
1578 currentLevel += gridIntervalY;
1585 QVector<QLineF> borderLines;
1586 borderLines << QLineF( mapPolygon.at( 0 ), mapPolygon.at( 1 ) );
1587 borderLines << QLineF( mapPolygon.at( 1 ), mapPolygon.at( 2 ) );
1588 borderLines << QLineF( mapPolygon.at( 2 ), mapPolygon.at( 3 ) );
1589 borderLines << QLineF( mapPolygon.at( 3 ), mapPolygon.at( 0 ) );
1591 QVector<QPointF> intersectionList;
1593 while ( currentLevel <= mapBoundingRect.bottom() && gridLineCount <
MAX_GRID_LINES )
1595 intersectionList.clear();
1596 const QLineF gridLine( mapBoundingRect.left(), currentLevel, mapBoundingRect.right(), currentLevel );
1598 QVector<QLineF>::const_iterator it = borderLines.constBegin();
1599 for ( ; it != borderLines.constEnd(); ++it )
1601 QPointF intersectionPoint;
1602 if ( it->intersects( gridLine, &intersectionPoint ) == QLineF::BoundedIntersection )
1604 intersectionList.push_back( intersectionPoint );
1605 if ( intersectionList.size() >= 2 )
1612 if ( intersectionList.size() >= 2 )
1615 newLine.coordinate = currentLevel;
1618 mGridLines.append( newLine );
1621 currentLevel += gridIntervalY;
1628int QgsLayoutItemMapGrid::yGridLines()
const
1630 if ( !
mMap || mEvaluatedIntervalX <= 0.0 )
1636 QRectF mapBoundingRect = mapPolygon.boundingRect();
1637 double gridIntervalX = mEvaluatedIntervalX;
1638 double gridOffsetX = mEvaluatedOffsetX;
1639 double annotationScale = 1.0;
1640 switch ( mGridUnit )
1645 mapBoundingRect =
mMap->rect();
1646 mapPolygon = QPolygonF(
mMap->rect() );
1647 if ( mGridUnit ==
CM )
1649 annotationScale = 0.1;
1650 gridIntervalX *= 10;
1662 const double roundCorrection = mapBoundingRect.left() > gridOffsetX ? 1.0 : 0.0;
1663 double currentLevel =
static_cast< int >( ( mapBoundingRect.left() - gridOffsetX ) / gridIntervalX + roundCorrection ) * gridIntervalX + gridOffsetX;
1665 int gridLineCount = 0;
1669 double xCanvasCoord;
1670 while ( currentLevel <= mapBoundingRect.right() && gridLineCount <
MAX_GRID_LINES )
1672 xCanvasCoord =
mMap->rect().width() * ( currentLevel - mapBoundingRect.left() ) / mapBoundingRect.width();
1675 newLine.coordinate = currentLevel * annotationScale;
1677 newLine.line = QPolygonF() << QPointF( xCanvasCoord, 0 ) << QPointF( xCanvasCoord,
mMap->rect().height() );
1678 mGridLines.append( newLine );
1679 currentLevel += gridIntervalX;
1686 QVector<QLineF> borderLines;
1687 borderLines << QLineF( mapPolygon.at( 0 ), mapPolygon.at( 1 ) );
1688 borderLines << QLineF( mapPolygon.at( 1 ), mapPolygon.at( 2 ) );
1689 borderLines << QLineF( mapPolygon.at( 2 ), mapPolygon.at( 3 ) );
1690 borderLines << QLineF( mapPolygon.at( 3 ), mapPolygon.at( 0 ) );
1692 QVector<QPointF> intersectionList;
1694 while ( currentLevel <= mapBoundingRect.right() && gridLineCount <
MAX_GRID_LINES )
1696 intersectionList.clear();
1697 const QLineF gridLine( currentLevel, mapBoundingRect.bottom(), currentLevel, mapBoundingRect.top() );
1699 QVector<QLineF>::const_iterator it = borderLines.constBegin();
1700 for ( ; it != borderLines.constEnd(); ++it )
1702 QPointF intersectionPoint;
1703 if ( it->intersects( gridLine, &intersectionPoint ) == QLineF::BoundedIntersection )
1705 intersectionList.push_back( intersectionPoint );
1706 if ( intersectionList.size() >= 2 )
1713 if ( intersectionList.size() >= 2 )
1716 newLine.coordinate = currentLevel;
1719 mGridLines.append( newLine );
1722 currentLevel += gridIntervalX;
1730 if ( !
mMap || mEvaluatedIntervalY <= 0.0 )
1735 const double roundCorrection = bbox.
yMaximum() > mEvaluatedOffsetY ? 1.0 : 0.0;
1736 double currentLevel =
static_cast< int >( ( bbox.
yMaximum() - mEvaluatedOffsetY ) / mEvaluatedIntervalY + roundCorrection ) * mEvaluatedIntervalY + mEvaluatedOffsetY;
1738 const double minX = bbox.
xMinimum();
1739 const double maxX = bbox.
xMaximum();
1740 double step = ( maxX - minX ) / 20;
1742 bool crosses180 =
false;
1743 bool crossed180 =
false;
1748 step = ( maxX + 360.0 - minX ) / 20;
1754 int gridLineCount = 0;
1758 double currentX = minX;
1762 if ( ( !crosses180 || crossed180 ) && ( currentX > maxX ) )
1779 if ( crosses180 && currentX > 180.0 )
1787 const QList<QPolygonF> lineSegments = trimLinesToMap( gridLine,
QgsRectangle(
mMap->rect() ) );
1788 QList<QPolygonF>::const_iterator lineIt = lineSegments.constBegin();
1789 for ( ; lineIt != lineSegments.constEnd(); ++lineIt )
1791 if ( !( *lineIt ).isEmpty() )
1794 newLine.coordinate = currentLevel;
1796 newLine.line = QPolygonF( *lineIt );
1797 mGridLines.append( newLine );
1801 currentLevel -= mEvaluatedIntervalY;
1809 if ( !
mMap || mEvaluatedIntervalX <= 0.0 )
1814 const double roundCorrection = bbox.
xMinimum() > mEvaluatedOffsetX ? 1.0 : 0.0;
1815 double currentLevel =
static_cast< int >( ( bbox.
xMinimum() - mEvaluatedOffsetX ) / mEvaluatedIntervalX + roundCorrection ) * mEvaluatedIntervalX + mEvaluatedOffsetX;
1817 const double minY = bbox.
yMinimum();
1818 const double maxY = bbox.
yMaximum();
1819 const double step = ( maxY - minY ) / 20;
1824 bool crosses180 =
false;
1825 bool crossed180 =
false;
1832 int gridLineCount = 0;
1833 while ( ( currentLevel <= bbox.
xMaximum() || ( crosses180 && !crossed180 ) ) && gridLineCount <
MAX_GRID_LINES )
1836 double currentY = minY;
1840 if ( currentY > maxY )
1860 const QList<QPolygonF> lineSegments = trimLinesToMap( gridLine,
QgsRectangle(
mMap->rect() ) );
1861 QList<QPolygonF>::const_iterator lineIt = lineSegments.constBegin();
1862 for ( ; lineIt != lineSegments.constEnd(); ++lineIt )
1864 if ( !( *lineIt ).isEmpty() )
1867 newLine.coordinate = currentLevel;
1869 newLine.line = QPolygonF( *lineIt );
1870 mGridLines.append( newLine );
1874 currentLevel += mEvaluatedIntervalX;
1875 if ( crosses180 && currentLevel > 180.0 )
1877 currentLevel -= 360.0;
1906 return shouldShowForDisplayMode( coordinate, mEvaluatedLeftGridAnnotationDisplay );
1908 return shouldShowForDisplayMode( coordinate, mEvaluatedRightGridAnnotationDisplay );
1910 return shouldShowForDisplayMode( coordinate, mEvaluatedTopGridAnnotationDisplay );
1912 return shouldShowForDisplayMode( coordinate, mEvaluatedBottomGridAnnotationDisplay );
1926 if ( ddValue.compare( QLatin1String(
"x_only" ), Qt::CaseInsensitive ) == 0 )
1928 else if ( ddValue.compare( QLatin1String(
"y_only" ), Qt::CaseInsensitive ) == 0 )
1930 else if ( ddValue.compare( QLatin1String(
"disabled" ), Qt::CaseInsensitive ) == 0 )
1932 else if ( ddValue.compare( QLatin1String(
"all" ), Qt::CaseInsensitive ) == 0 )
1938void QgsLayoutItemMapGrid::refreshDataDefinedProperties()
1943 mTransformDirty = mTransformDirty
1950 switch ( mGridUnit )
1963 if ( mMaximumIntervalWidth < mMinimumIntervalWidth )
1965 mEvaluatedEnabled =
false;
1970 const double mapWidthMapUnits = mapWidth();
1971 const double minUnitsPerSeg = ( mMinimumIntervalWidth * mapWidthMapUnits ) / mapWidthMm;
1972 const double maxUnitsPerSeg = ( mMaximumIntervalWidth * mapWidthMapUnits ) / mapWidthMm;
1974 mEvaluatedIntervalX = interval;
1975 mEvaluatedIntervalY = interval;
1976 mTransformDirty =
true;
1998double QgsLayoutItemMapGrid::mapWidth()
const
2009 return mapExtent.
width();
2029 QgsDebugError( QStringLiteral(
"An error occurred while calculating length" ) );
2035bool sortByDistance( QPair<qreal, QgsLayoutItemMapGrid::BorderSide> a, QPair<qreal, QgsLayoutItemMapGrid::BorderSide> b )
2037 return a.first < b.first;
2035bool sortByDistance( QPair<qreal, QgsLayoutItemMapGrid::BorderSide> a, QPair<qreal, QgsLayoutItemMapGrid::BorderSide> b ) {
…}
2050 if ( ( p.y() <= tolerance && p.x() <= tolerance )
2051 || ( p.y() <= tolerance && p.x() >= (
mMap->rect().width() - tolerance ) )
2052 || ( p.y() >= (
mMap->rect().height() - tolerance ) && p.x() <= tolerance )
2053 || ( p.y() >= (
mMap->rect().height() - tolerance ) && p.x() >= (
mMap->rect().width() - tolerance ) )
2059 if ( p.x() <= tolerance )
2070 if ( p.y() <= tolerance )
2082 QList< QPair<qreal, QgsLayoutItemMapGrid::BorderSide > > distanceToSide;
2088 std::sort( distanceToSide.begin(), distanceToSide.end(),
sortByDistance );
2089 return distanceToSide.at( 0 ).second;
2094 mGridLineSymbol.reset( symbol );
2099 return mGridLineSymbol.get();
2104 return mGridLineSymbol.get();
2109 mGridMarkerSymbol.reset( symbol );
2114 return mGridMarkerSymbol.get();
2119 return mGridMarkerSymbol.get();
2124 mAnnotationFormat.
setFont( font );
2125 if ( font.pointSizeF() > 0 )
2127 mAnnotationFormat.
setSize( font.pointSizeF() );
2130 else if ( font.pixelSize() > 0 )
2132 mAnnotationFormat.
setSize( font.pixelSize() );
2139 return mAnnotationFormat.
toQFont();
2144 mAnnotationFormat.
setColor( color );
2149 return mAnnotationFormat.
color();
2157 mLeftGridAnnotationDisplay = display;
2160 mRightGridAnnotationDisplay = display;
2163 mTopGridAnnotationDisplay = display;
2166 mBottomGridAnnotationDisplay = display;
2170 refreshDataDefinedProperties();
2184 return mLeftGridAnnotationDisplay;
2186 return mRightGridAnnotationDisplay;
2188 return mTopGridAnnotationDisplay;
2190 return mBottomGridAnnotationDisplay;
2192 return mBottomGridAnnotationDisplay;
2199 double bottom = 0.0;
2202 return std::max( std::max( std::max( top, right ), bottom ), left );
2212 if ( !
mMap || !mEvaluatedEnabled )
2222 GridExtension extension;
2225 switch ( mGridUnit )
2232 drawGridCrsTransform( context, 0,
true );
2239 drawGridNoTransform( context, 0,
true );
2244 updateGridLinesAnnotationsPositions();
2248 drawGridFrame(
nullptr, &extension );
2251 if ( mShowGridAnnotation )
2256 top = extension.top;
2257 right = extension.right;
2258 bottom = extension.bottom;
2259 left = extension.left;
2265 refreshDataDefinedProperties();
2270 if ( unit == mGridUnit )
2275 mTransformDirty =
true;
2284 mGridIntervalX = interval;
2285 mTransformDirty =
true;
2286 refreshDataDefinedProperties();
2295 mGridIntervalY = interval;
2296 mTransformDirty =
true;
2297 refreshDataDefinedProperties();
2306 mGridOffsetX = offset;
2307 mTransformDirty =
true;
2308 refreshDataDefinedProperties();
2317 mGridOffsetY = offset;
2318 mTransformDirty =
true;
2319 refreshDataDefinedProperties();
2328 mMinimumIntervalWidth = minWidth;
2329 mTransformDirty =
true;
2330 refreshDataDefinedProperties();
2339 mMaximumIntervalWidth = maxWidth;
2340 mTransformDirty =
true;
2341 refreshDataDefinedProperties();
2346 if (
style == mGridStyle )
2351 mTransformDirty =
true;
2356 mCrossLength = length;
2357 refreshDataDefinedProperties();
2365 mLeftGridAnnotationDirection = direction;
2368 mRightGridAnnotationDirection = direction;
2371 mTopGridAnnotationDirection = direction;
2374 mBottomGridAnnotationDirection = direction;
2387 mGridFrameSides = flags;
2393 mGridFrameSides |= flag;
2395 mGridFrameSides &= ~flag;
2400 return mGridFrameSides;
2409 context.
setHighlightedVariables( QStringList() << QStringLiteral(
"grid_number" ) << QStringLiteral(
"grid_axis" ) );
2415 if ( mGridLineSymbol )
2421 if ( mGridMarkerSymbol )
2433 mTransformDirty =
true;
2434 refreshDataDefinedProperties();
2441 return mGridFrameSides.testFlag( flag );
2446 mGridFrameWidth = width;
2447 refreshDataDefinedProperties();
2452 mGridFrameMargin = margin;
2453 refreshDataDefinedProperties();
2458 mGridFramePenThickness = width;
2459 refreshDataDefinedProperties();
2464 mLeftGridAnnotationDirection = direction;
2465 mRightGridAnnotationDirection = direction;
2466 mTopGridAnnotationDirection = direction;
2467 mBottomGridAnnotationDirection = direction;
2475 mLeftGridAnnotationPosition = position;
2478 mRightGridAnnotationPosition = position;
2481 mTopGridAnnotationPosition = position;
2484 mBottomGridAnnotationPosition = position;
2500 return mLeftGridAnnotationPosition;
2502 return mRightGridAnnotationPosition;
2504 return mTopGridAnnotationPosition;
2506 return mBottomGridAnnotationPosition;
2508 return mLeftGridAnnotationPosition;
2513 mAnnotationFrameDistance = distance;
2514 refreshDataDefinedProperties();
2521 return mLeftGridAnnotationDirection;
2527 return mLeftGridAnnotationDirection;
2529 return mRightGridAnnotationDirection;
2531 return mTopGridAnnotationDirection;
2533 return mBottomGridAnnotationDirection;
2535 return mLeftGridAnnotationDirection;
2540 mGridAnnotationExpressionString = expression;
2541 mGridAnnotationExpression.reset();
2549 mLeftFrameDivisions = divisions;
2552 mRightFrameDivisions = divisions;
2555 mTopFrameDivisions = divisions;
2558 mBottomFrameDivisions = divisions;
2562 refreshDataDefinedProperties();
2575 return mLeftFrameDivisions;
2577 return mRightFrameDivisions;
2579 return mTopFrameDivisions;
2581 return mBottomFrameDivisions;
2583 return mLeftFrameDivisions;
2599 const QRectF mbr = mapPolygon.boundingRect();
2600 const QgsRectangle mapBoundingRect( mbr.left(), mbr.bottom(), mbr.right(), mbr.top() );
2606 QgsPointXY lowerLeft( mapBoundingRect.xMinimum(), mapBoundingRect.yMinimum() );
2607 QgsPointXY upperRight( mapBoundingRect.xMaximum(), mapBoundingRect.yMaximum() );
2609 lowerLeft = tr.transform( lowerLeft.x(), lowerLeft.y() );
2610 upperRight = tr.transform( upperRight.x(), upperRight.y() );
2612 if ( lowerLeft.x() > upperRight.x() )
2639QList<QPolygonF> QgsLayoutItemMapGrid::trimLinesToMap(
const QPolygonF &line,
const QgsRectangle &rect )
2647 QList<QPolygonF> trimmedLines;
2648 QVector<QgsGeometry>::const_iterator geomIt = intersectedParts.constBegin();
2649 for ( ; geomIt != intersectedParts.constEnd(); ++geomIt )
2651 trimmedLines << ( *geomIt ).asQPolygonF();
2653 return trimmedLines;
2728 refreshDataDefinedProperties();
Provides global constants and enumerations for use throughout the application.
@ Millimeters
Millimeters.
DistanceUnit
Units of distance.
@ Unknown
Unknown distance unit.
@ Millimeters
Millimeters.
@ Points
Points (e.g., for font sizes)
@ ApplyScalingWorkaroundForTextRendering
Whether a scaling workaround designed to stablise the rendering of small font sizes (or for painters ...
@ Forward
Forward transform (from source to destination)
bool valueAsBool(int key, const QgsExpressionContext &context, bool defaultValue=false, bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as an boolean.
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.
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
Represents a coordinate reference system (CRS).
bool isValid() const
Returns whether this CRS is correctly initialized and usable.
bool readXml(const QDomNode &node)
Restores state from the given DOM node.
bool writeXml(QDomNode &node, QDomDocument &doc) const
Stores state to the given Dom node in the given document.
Qgis::DistanceUnit mapUnits
Custom exception class for Coordinate Reference System related exceptions.
A general purpose distance and area calculator, capable of performing ellipsoid based calculations.
double measureLine(const QVector< QgsPointXY > &points) const
Measures the length of a line with multiple segments.
void setSourceCrs(const QgsCoordinateReferenceSystem &crs, const QgsCoordinateTransformContext &context)
Sets source spatial reference system crs.
Qgis::DistanceUnit lengthUnits() const
Returns the units of distance for length calculations made by this object.
bool setEllipsoid(const QString &ellipsoid)
Sets the ellipsoid by its acronym.
Single scope for storing variables and functions for use within a QgsExpressionContext.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
void setVariable(const QString &name, const QVariant &value, bool isStatic=false)
Convenience method for setting a variable in the context scope by name name and value.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
QgsExpressionContextScope * lastScope()
Returns the last scope added to the context.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
void setHighlightedVariables(const QStringList &variableNames)
Sets the list of variable names within the context intended to be highlighted to the user.
Handles parsing and evaluation of expressions (formerly called "search strings").
static bool setFromXmlChildNode(QFont &font, const QDomElement &element, const QString &childNode)
Sets the properties of a font to match the properties stored in an XML child node.
static void setFontFamily(QFont &font, const QString &family)
Sets the family for a font object.
A geometry is the spatial representation of a feature.
static QgsGeometry fromRect(const QgsRectangle &rect)
Creates a new geometry from a QgsRectangle.
static QgsGeometry fromQPolygonF(const QPolygonF &polygon)
Construct geometry from a QPolygonF.
static QgsGeometry fromPolylineXY(const QgsPolylineXY &polyline)
Creates a new LineString geometry from a list of QgsPointXY points.
QgsPoint vertexAt(int atVertex) const
Returns coordinates of a vertex.
QVector< QgsGeometry > asGeometryCollection() const
Returns contents of the geometry as a list of geometries.
QgsGeometry intersection(const QgsGeometry &geometry, const QgsGeometryParameters ¶meters=QgsGeometryParameters()) const
Returns a geometry representing the points shared by this geometry and other.
QList< QgsLayoutItemMapGrid * > asList() const
Returns a list of QgsLayoutItemMapGrids contained by the stack.
void calculateMaxGridExtension(double &top, double &right, double &bottom, double &left) const
Calculates the maximum distance grids within the stack extend beyond the QgsLayoutItemMap's item rect...
void removeGrid(const QString &gridId)
Removes a grid with matching gridId from the stack and deletes the corresponding QgsLayoutItemMapGrid...
double maxGridExtension() const
Calculates the maximum distance grids within the stack extend beyond the QgsLayoutItemMap's item rect...
void addGrid(QgsLayoutItemMapGrid *grid)
Adds a new map grid to the stack and takes ownership of the grid.
QgsLayoutItemMapGrid * grid(const QString &gridId) const
Returns a reference to a grid with matching gridId within the stack.
bool readXml(const QDomElement &elem, const QDomDocument &doc, const QgsReadWriteContext &context) override
Sets the item stack's state from a DOM document, where element is a DOM node corresponding to a 'Layo...
QgsLayoutItemMapGrid & operator[](int index)
Returns a reference to a grid at the specified index within the stack.
void moveGridUp(const QString &gridId)
Moves a grid with matching gridId up the stack, causing it to be rendered above other grids.
void moveGridDown(const QString &gridId)
Moves a grid with matching gridId down the stack, causing it to be rendered below other grids.
QgsLayoutItemMapGridStack(QgsLayoutItemMap *map)
Constructor for QgsLayoutItemMapGridStack, attached to the specified map.
An individual grid which is drawn above the map content in a QgsLayoutItemMap.
void setFrameSideFlags(QgsLayoutItemMapGrid::FrameSideFlags flags)
Sets flags for grid frame sides.
bool rotatedAnnotationsEnabled() const
Gets whether annotations rotation for rotated or reprojected grids is enabled.
QString annotationExpression() const
Returns the expression used for drawing grid annotations.
double rotatedTicksMarginToCorner() const
Gets the margin to corners (in canvas units) below which outwards facing ticks are not drawn.
GridStyle
Grid drawing style.
@ Markers
Draw markers at intersections of grid lines.
@ Cross
Draw line crosses at intersections of grid lines.
@ FrameAnnotationsOnly
No grid lines over the map, only draw frame and annotations.
void calculateMaxExtension(double &top, double &right, double &bottom, double &left) const
Calculates the maximum distance the grid extends beyond the QgsLayoutItemMap's item rect.
void setFrameFillColor2(const QColor &color)
Sets the second fill color used for the grid frame.
GridUnit
Unit for grid values.
@ CM
Grid units in centimeters.
@ MM
Grid units in millimeters.
@ DynamicPageSizeBased
Dynamically sized, based on a on-page size range.
@ MapUnit
Grid units follow map units.
bool writeXml(QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context) const override
Stores map item state in a DOM element, where element is the DOM element corresponding to a 'LayoutMa...
void refresh() override
Refreshes the object, causing a recalculation of any property overrides.
GridStyle style() const
Returns the grid's style, which controls how the grid is drawn over the map's contents.
void setFrameSideFlag(QgsLayoutItemMapGrid::FrameSideFlag flag, bool on=true)
Sets whether the grid frame is drawn for a certain side of the map item.
FrameSideFlag
Flags for controlling which side of the map a frame is drawn on.
@ FrameTop
Top side of map.
@ FrameBottom
Bottom side of map.
@ FrameLeft
Left side of map.
@ FrameRight
Right side of map.
Q_DECL_DEPRECATED void setAnnotationFontColor(const QColor &color)
Sets the font color used for drawing grid annotations.
void setAnnotationFormat(const AnnotationFormat format)
Sets the format for drawing grid annotations.
double frameWidth() const
Gets the grid frame width in layout units.
void draw(QPainter *painter) override
Draws the item on to a destination painter.
double crossLength() const
Retrieves the length (in layout units) of the cross segments drawn for the grid.
void setIntervalY(double interval)
Sets the interval between grid lines in the y-direction.
void setRotatedTicksMinimumAngle(const double angle)
Sets the minimum angle (in degrees) below which ticks are not drawn.
Q_DECL_DEPRECATED QColor annotationFontColor() const
Returns the font color used for drawing grid annotations.
QPainter::CompositionMode blendMode() const
Retrieves the blending mode used for drawing the grid.
void setAnnotationEnabled(const bool enabled)
Sets whether annotations should be shown for the grid.
QgsTextFormat annotationTextFormat() const
Returns the text format used when rendering grid annotations.
AnnotationFormat annotationFormat() const
Returns the format for drawing grid annotations.
double framePenSize() const
Retrieves the width of the stroke drawn in the grid frame.
void setFramePenColor(const QColor &color)
Sets the color of the stroke drawn in the grid frame.
void setFramePenSize(const double width)
Sets the width of the stroke drawn in the grid frame.
bool accept(QgsStyleEntityVisitorInterface *visitor) const override
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
void setAnnotationDisplay(DisplayMode display, BorderSide border)
Sets what types of grid annotations should be drawn for a specified side of the map frame,...
void setRotatedTicksEnabled(const bool state)
Enable/disable ticks rotation for rotated or reprojected grids.
Q_DECL_DEPRECATED QFont annotationFont() const
Returns the font used for drawing grid annotations.
double maxExtension() const
Calculates the maximum distance the grid extends beyond the QgsLayoutItemMap's item rect (in layout u...
AnnotationPosition
Position for grid annotations.
@ InsideMapFrame
Draw annotations inside the map frame.
@ OutsideMapFrame
Draw annotations outside the map frame.
TickLengthMode rotatedAnnotationsLengthMode() const
Returns the annotation length calculation mode.
double rotatedTicksMinimumAngle() const
Gets the minimum angle (in degrees) below which ticks are not drawn.
void setAnnotationPosition(AnnotationPosition position, BorderSide side)
Sets the position for the grid annotations on a specified side of the map frame.
AnnotationPosition annotationPosition(BorderSide side) const
Returns the position for the grid annotations on a specified side of the map frame.
void setUnits(GridUnit unit)
Sets the unit to use for grid measurements such as the interval and offset for grid lines.
QgsLayoutItemMapGrid::FrameSideFlags frameSideFlags() const
Returns the flags which control which sides of the map item the grid frame is drawn on.
bool readXml(const QDomElement &itemElem, const QDomDocument &doc, const QgsReadWriteContext &context) override
Sets the map item state from a DOM document, where element is the DOM node corresponding to a 'Layout...
bool testFrameSideFlag(FrameSideFlag flag) const
Tests whether the grid frame should be drawn on a specified side of the map item.
void setFrameDivisions(DisplayMode divisions, BorderSide side)
Sets what type of grid divisions should be used for frames on a specified side of the map.
void setMinimumIntervalWidth(double width)
Sets the minimum width (in millimeters) for grid segments.
AnnotationCoordinate
Annotation coordinate type.
@ Latitude
Coordinate is a latitude value.
@ Longitude
Coordinate is a longitude value.
void setIntervalX(double interval)
Sets the interval between grid lines in the x-direction.
void setCrossLength(const double length)
Sets the length (in layout units) of the cross segments drawn for the grid.
void setRotatedTicksLengthMode(const TickLengthMode mode)
Sets the tick length calculation mode.
void setEnabled(bool enabled) override
Controls whether the item will be drawn.
DisplayMode
Display settings for grid annotations and frames.
@ LongitudeOnly
Show longitude/x annotations/divisions only.
@ ShowAll
Show both latitude and longitude annotations/divisions.
@ LatitudeOnly
Show latitude/y annotations/divisions only.
void setAnnotationFrameDistance(const double distance)
Sets the distance between the map frame and annotations.
void crsChanged()
Emitted whenever the grid's CRS is changed.
void setRotatedAnnotationsMinimumAngle(const double angle)
Sets the minimum angle (in degrees) below which annotations are not drawn.
void setFrameMargin(const double margin)
Sets the grid frame margin (in layout units).
double rotatedAnnotationsMinimumAngle() const
Gets the minimum angle (in degrees) below which annotations are not drawn.
void setAnnotationTextFormat(const QgsTextFormat &format)
Sets the text format to use when rendering grid annotations.
QgsLayoutItemMapGrid(const QString &name, QgsLayoutItemMap *map)
Constructor for QgsLayoutItemMapGrid.
~QgsLayoutItemMapGrid() override
void copyProperties(const QgsLayoutItemMapGrid *other)
Copies properties from specified map grid.
void setBlendMode(const QPainter::CompositionMode mode)
Sets the blending mode used for drawing the grid.
void setMarkerSymbol(QgsMarkerSymbol *symbol)
Sets the marker symbol used for drawing grid points.
bool annotationEnabled() const
Returns whether annotations are shown for the grid.
void setMaximumIntervalWidth(double width)
Sets the maximum width (in millimeters) for grid segments.
void setFrameStyle(const FrameStyle style)
Sets the grid frame style.
QFlags< FrameSideFlag > FrameSideFlags
QColor framePenColor() const
Retrieves the color of the stroke drawn in the grid frame.
bool usesAdvancedEffects() const override
Returns true if the item is drawn using advanced effects, such as blend modes.
FrameStyle frameStyle() const
Returns the grid frame style.
Q_DECL_DEPRECATED void setAnnotationFont(const QFont &font)
Sets the font used for drawing grid annotations.
void setRotatedTicksMarginToCorner(const double margin)
Sets the margin to corners (in canvas units) below which outwards facing ticks are not drawn.
void setAnnotationPrecision(const int precision)
Sets the coordinate precision for grid annotations.
QColor frameFillColor2() const
Retrieves the second fill color for the grid frame.
void setLineSymbol(QgsLineSymbol *symbol)
Sets the line symbol used for drawing grid lines.
QgsCoordinateReferenceSystem crs() const
Retrieves the CRS for the grid.
void setRotatedAnnotationsMarginToCorner(const double margin)
Sets the margin to corners (in canvas units) below which outwards facing annotations are not drawn.
double rotatedAnnotationsMarginToCorner() const
Gets the margin to corners (in canvas units) below which outwards facing annotations are not drawn.
void setRotatedAnnotationsLengthMode(const TickLengthMode mode)
Sets the annotation length calculation mode.
TickLengthMode
Tick length mode (useful for rotated grids)
@ OrthogonalTicks
Align ticks orthogonaly.
double offsetX() const
Returns the offset for grid lines in the x-direction.
bool rotatedTicksEnabled() const
Gets whether ticks rotation for rotated or reprojected grids is enabled.
AnnotationFormat
Format for displaying grid annotations.
@ DegreeMinuteSecondNoSuffix
Degree/minutes/seconds, use - for S/W coordinates.
@ DegreeMinuteSecondPadded
Degree/minutes/seconds, with minutes using leading zeros where required.
@ DegreeMinuteSecond
Degree/minutes/seconds, use NSEW suffix.
@ DecimalWithSuffix
Decimal degrees, use NSEW suffix.
@ DegreeMinute
Degree/minutes, use NSEW suffix.
@ DegreeMinuteNoSuffix
Degree/minutes, use - for S/W coordinates.
@ Decimal
Decimal degrees, use - for S/W coordinates.
@ DegreeMinutePadded
Degree/minutes, with minutes using leading zeros where required.
@ CustomFormat
Custom expression-based format.
DisplayMode frameDivisions(BorderSide side) const
Returns the type of grid divisions which are used for frames on a specified side of the map.
AnnotationDirection
Direction of grid annotations.
@ OnTick
Draw annotations parallel to tick (on the line)
@ Horizontal
Draw annotations horizontally.
@ Vertical
Draw annotations vertically, ascending.
@ AboveTick
Draw annotations parallel to tick (above the line)
@ UnderTick
Draw annotations parallel to tick (under the line)
@ VerticalDescending
Draw annotations vertically, descending.
double annotationFrameDistance() const
Returns the distance between the map frame and annotations.
double intervalY() const
Returns the interval between grid lines in the y-direction.
GridUnit units() const
Returns the units used for grid measurements such as the interval and offset for grid lines.
void setCrs(const QgsCoordinateReferenceSystem &crs)
Sets the crs for the grid.
double minimumIntervalWidth() const
Returns the minimum width (in millimeters) for grid segments.
void setOffsetY(double offset)
Sets the offset for grid lines in the y-direction.
const QgsMarkerSymbol * markerSymbol() const
Returns the marker symbol used for drawing grid points.
const QgsLineSymbol * lineSymbol() const
Returns the line symbol used for drawing grid lines.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QColor frameFillColor1() const
Retrieves the first fill color for the grid frame.
void setRotatedAnnotationsEnabled(const bool state)
Enable/disable annotations rotation for rotated or reprojected grids.
FrameStyle
Style for grid frame.
@ Zebra
Black/white pattern.
@ InteriorTicks
Tick markers drawn inside map frame.
@ LineBorder
Simple solid line frame.
@ InteriorExteriorTicks
Tick markers drawn both inside and outside the map frame.
@ LineBorderNautical
Simple solid line frame, with nautical style diagonals on corners.
@ ExteriorTicks
Tick markers drawn outside map frame.
@ NoFrame
Disable grid frame.
@ ZebraNautical
Black/white pattern, with nautical style diagonals on corners.
void setFrameWidth(const double width)
Sets the grid frame width (in layout units).
void setFrameFillColor1(const QColor &color)
Sets the first fill color used for the grid frame.
double frameMargin() const
Sets the grid frame Margin (in layout units).
DisplayMode annotationDisplay(BorderSide border) const
Returns the display mode for the grid annotations on a specified side of the map frame.
void setOffsetX(double offset)
Sets the offset for grid lines in the x-direction.
BorderSide
Border sides for annotations.
AnnotationDirection annotationDirection(BorderSide border) const
Returns the direction for drawing frame annotations, on the specified side of the map.
void setAnnotationDirection(AnnotationDirection direction, BorderSide side)
Sets the direction for drawing frame annotations for the specified map side.
void setGridLineColor(const QColor &color)
Sets the color of grid lines.
void setGridLineWidth(double width)
Sets the width of grid lines (in layout units).
int annotationPrecision() const
Returns the coordinate precision for grid annotations, which is the number of decimal places shown wh...
void setStyle(GridStyle style)
Sets the grid style, which controls how the grid is drawn over the map's contents.
double maximumIntervalWidth() const
Returns the maximum width (in millimeters) for grid segments.
void setAnnotationExpression(const QString &expression)
Sets the expression used for drawing grid annotations.
TickLengthMode rotatedTicksLengthMode() const
Returns the grid frame style.
double intervalX() const
Returns the interval between grid lines in the x-direction.
A collection of map items which are drawn above the map content in a QgsLayoutItemMap.
void addItem(QgsLayoutItemMapItem *item)
Adds a new map item to the stack and takes ownership of the item.
void removeItem(const QString &itemId)
Removes an item which matching itemId from the stack and deletes the corresponding QgsLayoutItemMapIt...
QgsLayoutItemMapItem * item(int index) const
Returns a reference to the item at the specified index within the stack.
void moveItemUp(const QString &itemId)
Moves an item which matching itemId up the stack, causing it to be rendered above other items.
void removeItems()
Clears the item stack and deletes all QgsLayoutItemMapItems contained by the stack.
QList< QgsLayoutItemMapItem * > mItems
void moveItemDown(const QString &itemId)
Moves an item which matching itemId up the stack, causing it to be rendered above other items.
An item which is drawn inside a QgsLayoutItemMap, e.g., a grid or map overview.
QgsLayoutItemMap * mMap
Associated map.
virtual bool readXml(const QDomElement &element, const QDomDocument &doc, const QgsReadWriteContext &context)
Sets the map item state from a DOM document, where element is the DOM node corresponding to a 'Layout...
virtual bool writeXml(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores map item state in a DOM element, where element is the DOM element corresponding to a 'LayoutMa...
virtual void setEnabled(bool enabled)
Controls whether the item will be drawn.
bool enabled() const
Returns whether the item will be drawn.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
Layout graphical items for displaying a map.
void extentChanged()
Emitted when the map's extent changes.
QPointF mapToItemCoords(QPointF mapCoords) const
Transforms map coordinates to item coordinates (considering rotation and move offset)
void updateBoundingRect()
Updates the bounding rect of this item. Call this function before doing any changes related to annota...
void mapRotationChanged(double newRotation)
Emitted when the map's rotation changes.
void crsChanged()
Emitted when the map's coordinate reference system is changed.
QPolygonF transformedMapPolygon() const
Returns extent that considers rotation and shift with mOffsetX / mOffsetY.
double mapRotation(QgsLayoutObject::PropertyValueType valueType=QgsLayoutObject::EvaluatedValue) const
Returns the rotation used for drawing the map within the layout item, in degrees clockwise.
QgsRectangle extent() const
Returns the current map extent.
QgsCoordinateReferenceSystem crs() const
Returns coordinate reference system used for rendering the map.
QgsLayoutSize sizeWithUnits() const
Returns the item's current size, including units.
bool frameEnabled() const
Returns true if the item includes a frame.
QgsPropertyCollection mDataDefinedProperties
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the object's property collection, used for data defined overrides.
const QgsLayout * layout() const
Returns the layout the object is attached to.
QPointer< QgsLayout > mLayout
@ MapGridOffsetY
Map grid offset Y.
@ MapGridFrameDivisionsBottom
Map frame division display bottom.
@ MapGridEnabled
Map grid enabled.
@ MapGridIntervalX
Map grid interval X.
@ MapGridFrameDivisionsTop
Map frame division display top.
@ MapGridAnnotationDisplayLeft
Map annotation display left.
@ MapGridFrameMargin
Map grid frame margin.
@ MapGridCrossSize
Map grid cross size.
@ MapGridAnnotationDisplayRight
Map annotation display right.
@ MapGridOffsetX
Map grid offset X.
@ MapGridAnnotationDisplayTop
Map annotation display top.
@ MapGridAnnotationDisplayBottom
Map annotation display bottom.
@ MapGridFrameDivisionsRight
Map frame division display right.
@ MapGridLabelDistance
Map grid label distance.
@ MapGridIntervalY
Map grid interval Y.
@ MapGridFrameSize
Map grid frame size.
@ MapGridFrameLineThickness
Map grid frame line thickness.
@ MapGridFrameDivisionsLeft
Map frame division display left.
void setDataDefinedProperties(const QgsPropertyCollection &collection)
Sets the objects's property collection, used for data defined overrides.
QgsLayoutRenderContext::Flags flags() const
Returns the current combination of flags used for rendering the layout.
@ FlagAntialiasing
Use antialiasing when drawing items.
static QgsRenderContext createRenderContextForLayout(QgsLayout *layout, QPainter *painter, double dpi=-1)
Creates a render context suitable for the specified layout and painter destination.
static double calculatePrettySize(double minimumSize, double maximumSize)
Calculates a "pretty" size which falls between the range [minimumSize, maximumSize].
QgsLayoutRenderContext & renderContext()
Returns a reference to the layout's render context, which stores information relating to the current ...
A line symbol type, for rendering LineString and MultiLineString geometries.
QgsLineSymbol * clone() const override
Returns a deep copy of this symbol.
static std::unique_ptr< QgsLineSymbol > createSimple(const QVariantMap &properties)
Create a line symbol with one symbol layer: SimpleLine with specified properties.
A marker symbol type, for rendering Point and MultiPoint geometries.
static std::unique_ptr< QgsMarkerSymbol > createSimple(const QVariantMap &properties)
Create a marker symbol with one symbol layer: SimpleMarker with specified properties.
QgsMarkerSymbol * clone() const override
Returns a deep copy of this symbol.
bool isEmpty() const
Returns true if the geometry is empty.
bool isActive(int key) const final
Returns true if the collection contains an active property with the specified key.
A container for the context for various read/write operations on objects.
A rectangle specified with double values.
Contains information about the context of a rendering operation.
void setForceVectorOutput(bool force)
Sets whether rendering operations should use vector operations instead of any faster raster shortcuts...
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
QPainter * painter()
Returns the destination QPainter for the render operation.
QgsExpressionContext & expressionContext()
Gets the expression context.
void setFlag(Qgis::RenderContextFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected)
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
Scoped object for saving and restoring a QPainter object's state.
Scoped object for temporary scaling of a QgsRenderContext for pixel based rendering.
Stores settings for use within QGIS.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
An interface for classes which can visit style entity (e.g.
virtual bool visit(const QgsStyleEntityVisitorInterface::StyleLeaf &entity)
Called when the visitor will visit a style entity.
A symbol entity for QgsStyle databases.
static QPointF pointOnLineWithDistance(QPointF startPoint, QPointF directionPoint, double distance)
Returns a point on the line from startPoint to directionPoint that is a certain distance away from th...
static QDomElement saveSymbol(const QString &symbolName, const QgsSymbol *symbol, QDomDocument &doc, const QgsReadWriteContext &context)
Writes a symbol definition to XML.
void setColor(const QColor &color)
Sets the color that text will be rendered in.
void setSize(double size)
Sets the size for rendered text.
void setFont(const QFont &font)
Sets the font used for rendering text.
void setSizeUnit(Qgis::RenderUnit unit)
Sets the units for the size of rendered text.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
QFont toQFont() const
Returns a QFont matching the relevant settings from this text format.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
QColor color() const
Returns the color that text will be rendered in.
Handles rendering text using rich formatting options, including drop shadows, buffers and background ...
static double textWidth(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, QFontMetricsF *fontMetrics=nullptr)
Returns the width of a text based on a given format.
static void drawText(const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, Qgis::TextVerticalAlignment vAlignment=Qgis::TextVerticalAlignment::Top, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Rectangle)
Draws text within a rectangle using the specified settings.
static double textHeight(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Point, QFontMetricsF *fontMetrics=nullptr, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), double maxLineWidth=0)
Returns the height of a text based on a given format.
static Q_INVOKABLE double fromUnitToUnitFactor(Qgis::DistanceUnit fromUnit, Qgis::DistanceUnit toUnit)
Returns the conversion factor between the specified distance units.
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
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
double qgsRound(double number, int places)
Returns a double number, rounded (as close as possible) to the specified number of places.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
QVector< QgsPointXY > QgsPolylineXY
Polyline as represented as a vector of two-dimensional points.
QgsLayoutItemMapGrid::DisplayMode gridAnnotationDisplayModeFromDD(QString ddValue, QgsLayoutItemMapGrid::DisplayMode defValue)
bool sortByDistance(QPair< qreal, QgsLayoutItemMapGrid::BorderSide > a, QPair< qreal, QgsLayoutItemMapGrid::BorderSide > b)
QVector2D borderToNormal2D(QgsLayoutItemMapGrid::BorderSide border)
QVector2D borderToVector2D(QgsLayoutItemMapGrid::BorderSide border)
#define QgsDebugError(str)
const QgsCoordinateReferenceSystem & crs
Single variable definition for use within a QgsExpressionContextScope.
Contains information relating to the style entity currently being visited.