37#include <QStyleOptionGraphicsItem>
40#include "moc_qgslayoutitem.cpp"
42using namespace Qt::StringLiterals;
44#define CACHE_SIZE_LIMIT 5000
47 : mRenderContext( context )
56 , QGraphicsRectItem( nullptr )
57 , mUuid( QUuid::createUuid().toString() )
62 setFlags( flags() | QGraphicsItem::ItemUsesExtendedStyleOption | QGraphicsItem::ItemIsSelectable );
64 setCacheMode( QGraphicsItem::DeviceCoordinateCache );
68 mItemPosition =
QgsLayoutPoint( scenePos().x(), scenePos().y(), initialUnits );
69 mItemSize =
QgsLayoutSize( rect().width(), rect().height(), initialUnits );
75 initConnectionsToLayout();
80 mLayoutManagesZValue =
true;
81 mLayout->itemsModel()->addItemAtTop(
this );
85 mLayoutManagesZValue =
false;
96 if (
mLayout && mLayoutManagesZValue )
98 mLayout->itemsModel()->removeItem(
this );
105 if ( !
id().isEmpty() )
113 return tr(
"<%1>" ).arg( metadata->visibleName() );
116 return tr(
"<item>" );
141 if ( !shouldBlockUndoCommands() )
142 mLayout->undoStack()->beginCommand(
this, tr(
"Change Item ID" ) );
146 if ( !shouldBlockUndoCommands() )
147 mLayout->undoStack()->endCommand();
154 mLayout->itemsModel()->updateItemDisplayName(
this );
162 QGraphicsRectItem::setSelected( selected );
166 mLayout->itemsModel()->updateItemSelectStatus(
this );
172 if ( visible == isVisible() )
178 std::unique_ptr< QgsAbstractLayoutUndoCommand > command;
179 if ( !shouldBlockUndoCommands() )
181 command.reset(
createCommand( visible ? tr(
"Show Item" ) : tr(
"Hide Item" ), 0 ) );
182 command->saveBeforeState();
185 QGraphicsItem::setVisible( visible );
189 command->saveAfterState();
190 mLayout->undoStack()->push( command.release() );
196 mLayout->itemsModel()->updateItemVisibility(
this );
202 if (
locked == mIsLocked )
207 if ( !shouldBlockUndoCommands() )
208 mLayout->undoStack()->beginCommand(
this,
locked ? tr(
"Lock Item" ) : tr(
"Unlock Item" ) );
212 if ( !shouldBlockUndoCommands() )
213 mLayout->undoStack()->endCommand();
218 mLayout->itemsModel()->updateItemLockStatus(
this );
227 return !mParentGroupUuid.isEmpty() &&
mLayout &&
static_cast< bool >(
mLayout->itemByUuid( mParentGroupUuid ) );
232 if ( !
mLayout || mParentGroupUuid.isEmpty() )
235 return qobject_cast< QgsLayoutItemGroup * >(
mLayout->itemByUuid( mParentGroupUuid ) );
241 mParentGroupUuid.clear();
243 mParentGroupUuid = group->
uuid();
244 setFlag( QGraphicsItem::ItemIsSelectable, !
static_cast< bool>( group ) );
270 if ( !
mLayout ||
mLayout->renderContext().currentExportLayer() == -1 )
276 return mLayout->renderContext().currentExportLayer() < layers;
292 if ( shouldDrawDebugRect() )
298 const bool previewRender = !
mLayout ||
mLayout->renderContext().isPreviewRender();
300 const bool useImageCache =
false;
302 QPainter::CompositionMode
blendMode = blendModeForRender();
307 forceRasterOutput =
false;
309 else if (
blendMode != QPainter::CompositionMode_SourceOver )
312 forceRasterOutput =
true;
315 if ( useImageCache || forceRasterOutput )
317 double widthInPixels = 0;
318 double heightInPixels = 0;
328 widthInPixels = boundingRect().width() * layoutUnitsToPixels;
329 heightInPixels = boundingRect().height() * layoutUnitsToPixels;
336 if ( widthInPixels > heightInPixels )
340 heightInPixels /= scale;
346 widthInPixels /= scale;
348 destinationDpi = destinationDpi / scale;
351 if ( previewRender && !mItemCachedImage.isNull() &&
qgsDoubleNear( mItemCacheDpi, destinationDpi ) )
356 preparePainter( painter );
357 const double cacheScale = destinationDpi / mItemCacheDpi;
359 painter->setCompositionMode(
blendMode );
360 painter->drawImage( boundingRect().x() * context.
scaleFactor() / cacheScale,
361 boundingRect().y() * context.
scaleFactor() / cacheScale, mItemCachedImage );
366 QImage image = QImage( widthInPixels, heightInPixels, QImage::Format_ARGB32 );
367 image.fill( Qt::transparent );
368 image.setDotsPerMeterX( 1000 * destinationDpi * 25.4 );
369 image.setDotsPerMeterY( 1000 * destinationDpi * 25.4 );
370 QPainter p( &image );
372 preparePainter( &p );
384 draw( itemRenderContext );
395 painter->setCompositionMode(
blendMode );
396 painter->drawImage( boundingRect().x() * context.
scaleFactor(),
397 boundingRect().y() * context.
scaleFactor(), image );
401 mItemCacheDpi = destinationDpi;
402 mItemCachedImage = image;
410 preparePainter( painter );
420 draw( itemRenderContext );
434 if ( point == mReferencePoint )
439 mReferencePoint = point;
442 updateStoredItemPosition();
466 const QSizeF targetSizeLayoutUnits =
mLayout->convertToLayoutUnits( evaluatedSize );
467 QSizeF actualSizeLayoutUnits = applyMinimumSize( targetSizeLayoutUnits );
468 actualSizeLayoutUnits = applyFixedSize( actualSizeLayoutUnits );
471 if ( actualSizeLayoutUnits == rect().size() )
477 mItemSize = actualSizeTargetUnits;
479 setRect( 0, 0, actualSizeLayoutUnits.width(), actualSizeLayoutUnits.height() );
496 point =
mLayout->pageCollection()->pagePositionToAbsolute(
page, p );
503 point.
setX( point.
x() + bleed );
504 point.
setY( point.
y() + bleed );
508 if ( !useReferencePoint )
513 evaluatedPoint = applyDataDefinedPosition( evaluatedPoint );
514 const QPointF evaluatedPointLayoutUnits =
mLayout->convertToLayoutUnits( evaluatedPoint );
516 if ( topLeftPointLayoutUnits == scenePos() && point.
units() == mItemPosition.units() )
523 mItemPosition = referencePointTargetUnits;
524 setScenePos( topLeftPointLayoutUnits );
530 const QPointF newPos = rect.topLeft();
532 blockSignals(
true );
534 const QgsLayoutSize newSize =
mLayout->convertFromLayoutUnits( rect.size(), mItemSize.units() );
540 blockSignals(
false );
548 moveBy( deltaX, deltaY );
554 itemPos.
setX( itemPos.
x() + deltaPos.
x() );
555 itemPos.
setY( itemPos.
y() + deltaPos.
y() );
564 return mLayout->pageCollection()->pageNumberForPoint( pos() );
579 p.ry() -= pageItem->pos().y();
589 return mLayout->convertFromLayoutUnits( p, mItemPosition.units() );
592void QgsLayoutItem::setScenePos(
const QPointF destinationPos )
597 if (
auto *lParentItem = parentItem() )
598 setPos( pos() + ( destinationPos - scenePos() ) + lParentItem->scenePos() );
600 setPos( pos() + ( destinationPos - scenePos() ) );
603bool QgsLayoutItem::shouldBlockUndoCommands()
const
620 return !mEvaluatedExcludeFromExports;
625 return mItemRotation;
630 QDomElement element = doc.createElement( u
"LayoutItem"_s );
631 element.setAttribute( u
"type"_s, QString::number(
type() ) );
633 element.setAttribute( u
"uuid"_s, mUuid );
634 element.setAttribute( u
"templateUuid"_s, mUuid );
635 element.setAttribute( u
"id"_s, mId );
636 element.setAttribute( u
"referencePoint"_s, QString::number(
static_cast< int >( mReferencePoint ) ) );
637 element.setAttribute( u
"position"_s, mItemPosition.encodePoint() );
639 element.setAttribute( u
"size"_s, mItemSize.encodeSize() );
640 element.setAttribute( u
"itemRotation"_s, QString::number( mItemRotation ) );
641 element.setAttribute( u
"groupUuid"_s, mParentGroupUuid );
643 element.setAttribute( u
"zValue"_s, QString::number( zValue() ) );
644 element.setAttribute( u
"visibility"_s, isVisible() );
648 element.setAttribute( u
"positionLock"_s, u
"true"_s );
652 element.setAttribute( u
"positionLock"_s, u
"false"_s );
658 element.setAttribute( u
"frame"_s, u
"true"_s );
662 element.setAttribute( u
"frame"_s, u
"false"_s );
668 element.setAttribute( u
"background"_s, u
"true"_s );
672 element.setAttribute( u
"background"_s, u
"false"_s );
676 QDomElement frameColorElem = doc.createElement( u
"FrameColor"_s );
677 frameColorElem.setAttribute( u
"red"_s, QString::number( mFrameColor.red() ) );
678 frameColorElem.setAttribute( u
"green"_s, QString::number( mFrameColor.green() ) );
679 frameColorElem.setAttribute( u
"blue"_s, QString::number( mFrameColor.blue() ) );
680 frameColorElem.setAttribute( u
"alpha"_s, QString::number( mFrameColor.alpha() ) );
681 element.appendChild( frameColorElem );
682 element.setAttribute( u
"outlineWidthM"_s, mFrameWidth.encodeMeasurement() );
686 QDomElement bgColorElem = doc.createElement( u
"BackgroundColor"_s );
687 bgColorElem.setAttribute( u
"red"_s, QString::number( mBackgroundColor.red() ) );
688 bgColorElem.setAttribute( u
"green"_s, QString::number( mBackgroundColor.green() ) );
689 bgColorElem.setAttribute( u
"blue"_s, QString::number( mBackgroundColor.blue() ) );
690 bgColorElem.setAttribute( u
"alpha"_s, QString::number( mBackgroundColor.alpha() ) );
691 element.appendChild( bgColorElem );
697 element.setAttribute( u
"opacity"_s, QString::number( mOpacity ) );
699 element.setAttribute( u
"excludeFromExports"_s, mExcludeFromExports );
704 parentElement.appendChild( element );
711 if ( element.nodeName() !=
"LayoutItem"_L1 )
718 mBlockUndoCommands =
true;
719 mUuid = element.attribute( u
"uuid"_s, QUuid::createUuid().toString() );
720 setId( element.attribute( u
"id"_s ) );
721 mReferencePoint =
static_cast< ReferencePoint >( element.attribute( u
"referencePoint"_s ).toInt() );
722 setItemRotation( element.attribute( u
"itemRotation"_s, u
"0"_s ).toDouble() );
726 mParentGroupUuid = element.attribute( u
"groupUuid"_s );
727 if ( !mParentGroupUuid.isEmpty() )
731 group->addItem(
this );
734 mTemplateUuid = element.attribute( u
"templateUuid"_s );
737 const QString positionLock = element.attribute( u
"positionLock"_s );
738 if ( positionLock.compare(
"true"_L1, Qt::CaseInsensitive ) == 0 )
747 setVisibility( element.attribute( u
"visibility"_s, u
"1"_s ) !=
"0"_L1 );
748 setZValue( element.attribute( u
"zValue"_s ).toDouble() );
751 const QString frame = element.attribute( u
"frame"_s );
752 if ( frame.compare(
"true"_L1, Qt::CaseInsensitive ) == 0 )
762 const QString background = element.attribute( u
"background"_s );
763 if ( background.compare(
"true"_L1, Qt::CaseInsensitive ) == 0 )
775 const QDomNodeList frameColorList = element.elementsByTagName( u
"FrameColor"_s );
776 if ( !frameColorList.isEmpty() )
778 const QDomElement frameColorElem = frameColorList.at( 0 ).toElement();
780 bool greenOk =
false;
782 bool alphaOk =
false;
783 int penRed, penGreen, penBlue, penAlpha;
785 penRed = frameColorElem.attribute( u
"red"_s ).toDouble( &redOk );
786 penGreen = frameColorElem.attribute( u
"green"_s ).toDouble( &greenOk );
787 penBlue = frameColorElem.attribute( u
"blue"_s ).toDouble( &blueOk );
788 penAlpha = frameColorElem.attribute( u
"alpha"_s ).toDouble( &alphaOk );
790 if ( redOk && greenOk && blueOk && alphaOk )
792 mFrameColor = QColor( penRed, penGreen, penBlue, penAlpha );
798 const QDomNodeList bgColorList = element.elementsByTagName( u
"BackgroundColor"_s );
799 if ( !bgColorList.isEmpty() )
801 const QDomElement bgColorElem = bgColorList.at( 0 ).toElement();
802 bool redOk, greenOk, blueOk, alphaOk;
803 int bgRed, bgGreen, bgBlue, bgAlpha;
804 bgRed = bgColorElem.attribute( u
"red"_s ).toDouble( &redOk );
805 bgGreen = bgColorElem.attribute( u
"green"_s ).toDouble( &greenOk );
806 bgBlue = bgColorElem.attribute( u
"blue"_s ).toDouble( &blueOk );
807 bgAlpha = bgColorElem.attribute( u
"alpha"_s ).toDouble( &alphaOk );
808 if ( redOk && greenOk && blueOk && alphaOk )
810 mBackgroundColor = QColor( bgRed, bgGreen, bgBlue, bgAlpha );
811 setBrush( QBrush( mBackgroundColor, Qt::SolidPattern ) );
821 if ( element.hasAttribute( u
"opacity"_s ) )
823 setItemOpacity( element.attribute( u
"opacity"_s, u
"1"_s ).toDouble() );
827 setItemOpacity( 1.0 - element.attribute( u
"transparency"_s, u
"0"_s ).toInt() / 100.0 );
830 mExcludeFromExports = element.attribute( u
"excludeFromExports"_s, u
"0"_s ).toInt();
831 mEvaluatedExcludeFromExports = mExcludeFromExports;
835 mBlockUndoCommands =
false;
848 return new QgsLayoutItemUndoCommand(
this, text,
id, parent );
866 if ( mFrameColor == color )
879 if ( mFrameWidth == width )
891 if ( mFrameJoinStyle == style )
896 mFrameJoinStyle = style;
898 QPen itemPen = pen();
899 itemPen.setJoinStyle( mFrameJoinStyle );
912 mBackgroundColor = color;
929 if ( !mItemCachedImage.isNull() )
935 return mExcludeFromExports;
940 mExcludeFromExports = exclude;
952 blendMode() != QPainter::CompositionMode_SourceOver;
962 return pen().widthF() / 2.0;
968 return rect().adjusted( -frameBleed, -frameBleed, frameBleed, frameBleed );
991 mLayout->undoStack()->beginCommand(
this, commandText, command );
997 mLayout->undoStack()->endCommand();
1003 mLayout->undoStack()->cancelCommand();
1016 return QgsLayoutPoint( evaluatedX, evaluatedY, position.
units() );
1019void QgsLayoutItem::applyDataDefinedOrientation(
double &width,
double &height,
const QgsExpressionContext &context )
1023 if ( ok && !orientationString.isEmpty() )
1028 double heightD = 0.0, widthD = 0.0;
1029 switch ( orientation )
1033 heightD = std::max( height, width );
1034 widthD = std::min( height, width );
1039 heightD = std::min( height, width );
1040 widthD = std::max( height, width );
1069 double evaluatedWidth = size.
width();
1070 double evaluatedHeight = size.
height();
1074 evaluatedWidth = convertedSize.
width();
1075 evaluatedHeight = convertedSize.
height();
1083 applyDataDefinedOrientation( evaluatedWidth, evaluatedHeight, context );
1088QPainter::CompositionMode QgsLayoutItem::blendModeForRender()
const
1090 QPainter::CompositionMode mode = mEvaluatedBlendMode;
1095 mode = QPainter::CompositionMode_SourceOver;
1100double QgsLayoutItem::applyDataDefinedRotation(
const double rotation )
1109 return evaluatedRotation;
1149 const bool exclude = mExcludeFromExports;
1159 if ( angle >= 360.0 || angle <= -360.0 )
1161 angle = std::fmod( angle, 360.0 );
1166 const double rotationRequired = angle - rotation();
1169 mItemRotation = angle;
1172void QgsLayoutItem::updateStoredItemPosition()
1175 mItemPosition =
mLayout->convertFromLayoutUnits( layoutPosReferencePoint, mItemPosition.
units() );
1180 double evaluatedAngle = angle + rotation();
1182 mItemRotation = evaluatedAngle;
1184 QPointF itemTransformOrigin = mapFromScene( transformOrigin );
1203 Q_UNUSED( visitor );
1222 if ( !mItemCachedImage.isNull() )
1224 mItemCachedImage = QImage();
1243 painter->setRenderHint( QPainter::Antialiasing,
false );
1244 painter->setPen( Qt::NoPen );
1245 painter->setBrush( QColor( 100, 255, 100, 200 ) );
1246 painter->drawRect( rect() );
1252 path.addRect( QRectF( 0, 0, rect().width(), rect().height() ) );
1258 if ( !mFrame || !context.
painter() )
1261 QPainter *p = context.
painter();
1266 p->setBrush( Qt::NoBrush );
1274 if ( !mBackground || !context.
painter() )
1279 QPainter *p = context.
painter();
1280 p->setBrush( brush() );
1281 p->setPen( Qt::NoPen );
1290 bool fitsInCache =
false;
1291 const int xSize = std::floor(
static_cast<double>( QFontMetrics( QFont() ).horizontalAdvance(
'X' ) ) * painter->device()->devicePixelRatioF() );
1292 const QImage refreshingImage =
QgsApplication::svgCache()->
svgAsImage( u
":/images/composer/refreshing_item.svg"_s, xSize * 3, QColor(), QColor(), 1, 1, fitsInCache );
1295 painter->scale( 1.0 / previewScaleFactor / painter->device()->devicePixelRatioF(), 1.0 / previewScaleFactor / painter->device()->devicePixelRatioF() );
1296 painter->drawImage( xSize, xSize, refreshingImage );
1307 mMinimumSize = size;
1326QPointF QgsLayoutItem::itemPositionAtReferencePoint(
const ReferencePoint reference,
const QSizeF size )
const
1328 switch ( reference )
1331 return QPointF( size.width() / 2.0, 0 );
1333 return QPointF( size.width(), 0 );
1335 return QPointF( 0, size.height() / 2.0 );
1337 return QPointF( size.width() / 2.0, size.height() / 2.0 );
1339 return QPointF( size.width(), size.height() / 2.0 );
1341 return QPointF( 0, size.height() );
1343 return QPointF( size.width() / 2.0, size.height() );
1345 return QPointF( size.width(), size.height() );
1347 return QPointF( 0, 0 );
1350 return QPointF( 0, 0 );
1355 const QPointF itemPosition = mapFromScene( position );
1356 const QPointF adjustedPointInsideItem = itemPosition - itemPositionAtReferencePoint( reference, size );
1357 return mapToScene( adjustedPointInsideItem );
1362 const QPointF pointWithinItem = itemPositionAtReferencePoint( reference, rect().size() );
1363 return mapToScene( pointWithinItem );
1368 const QPointF topLeft =
mLayout->convertToLayoutUnits( point );
1369 const QPointF refPoint = topLeft + itemPositionAtReferencePoint( mReferencePoint, rect().size() );
1370 return mLayout->convertFromLayoutUnits( refPoint, point.
units() );
1384void QgsLayoutItem::initConnectionsToLayout()
1391void QgsLayoutItem::preparePainter( QPainter *painter )
1393 if ( !painter || !painter->device() )
1398 painter->setRenderHint( QPainter::Antialiasing, shouldDrawAntialiased() );
1403bool QgsLayoutItem::shouldDrawAntialiased()
const
1412bool QgsLayoutItem::shouldDrawDebugRect()
const
1417QSizeF QgsLayoutItem::applyMinimumSize(
const QSizeF targetSize )
1423 const QSizeF minimumSizeLayoutUnits =
mLayout->convertToLayoutUnits(
minimumSize() );
1424 return targetSize.expandedTo( minimumSizeLayoutUnits );
1427QSizeF QgsLayoutItem::applyFixedSize(
const QSizeF targetSize )
1434 QSizeF size = targetSize;
1435 const QSizeF fixedSizeLayoutUnits =
mLayout->convertToLayoutUnits(
fixedSize() );
1436 if ( fixedSizeLayoutUnits.width() > 0 )
1437 size.setWidth( fixedSizeLayoutUnits.width() );
1438 if ( fixedSizeLayoutUnits.height() > 0 )
1439 size.setHeight( fixedSizeLayoutUnits.height() );
1446 double r = mItemRotation;
1458 if ( !transformPoint.isNull() )
1462 QLineF refLine = QLineF( mapToScene( transformPoint ), mapToScene( QPointF( 0, 0 ) ) );
1464 refLine.setAngle( refLine.angle() - r + rotation() );
1466 const QPointF rotatedReferencePoint = refLine.p2();
1467 setPos( rotatedReferencePoint );
1470 setTransformOriginPoint( 0, 0 );
1471 QGraphicsItem::setRotation( r );
1474 updateStoredItemPosition();
1489 mEvaluatedOpacity = opacity / 100.0;
1495 setOpacity( mEvaluatedOpacity );
1508 setPen( Qt::NoPen );
1518 itemPen = QPen( frameColor );
1522 itemPen = QPen( mFrameColor );
1524 itemPen.setJoinStyle( mFrameJoinStyle );
1527 itemPen.setWidthF(
mLayout->convertToLayoutUnits( mFrameWidth ) );
1529 itemPen.setWidthF( mFrameWidth.length() );
1541 return useDataDefined ? brush().color() : mBackgroundColor;
1556 setBrush( QBrush( mBackgroundColor, Qt::SolidPattern ) );
1566 QPainter::CompositionMode
blendMode = mBlendMode;
1571 if ( ok && !blendStr.isEmpty() )
1573 const QString blendstr = blendStr.trimmed();
1583 setCacheMode( QGraphicsItem::DeviceCoordinateCache );
1585 setCacheMode( QGraphicsItem::NoCache );
@ ForceVector
Always force vector-based rendering, even when the result will be visually different to a raster-base...
LayoutUnit
Layout measurement units.
@ Millimeters
Millimeters.
BlendMode
Blending modes defining the available composition modes that can be used when painting.
@ Debug
Debug/testing mode, items are drawn as solid rectangles.
@ LosslessImageRendering
Render images losslessly whenever possible, instead of the default lossy jpeg rendering used for some...
@ Antialiasing
Use antialiasing when drawing items.
Base class for commands to undo/redo layout and layout object changes.
static QgsPageSizeRegistry * pageSizeRegistry()
Returns the application's page size registry, used for managing layout page sizes.
static QgsLayoutItemRegistry * layoutItemRegistry()
Returns the application's layout item registry, used for layout item types.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static QgsSvgCache * svgCache()
Returns the application's SVG cache, used for caching SVG images and handling parameter replacement w...
static QgsExpressionContextScope * layoutItemScope(const QgsLayoutItem *item)
Creates a new scope which contains variables and functions relating to a QgsLayoutItem.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
A geometry is the spatial representation of a feature.
static void multiplyOpacity(QImage &image, double factor, QgsFeedback *feedback=nullptr)
Multiplies opacity of image pixel values by a factor.
Item representing the paper in a layout.
Orientation
Page orientation.
@ Landscape
Landscape orientation.
@ Portrait
Portrait orientation.
@ LayoutItem
Base class for items.
Contains settings and helpers relating to a render of a QgsLayoutItem.
QgsLayoutItemRenderContext(QgsRenderContext &context, double viewScaleFactor=1.0)
Constructor for QgsLayoutItemRenderContext.
double viewScaleFactor() const
Returns the current view zoom (scale factor).
virtual void drawDebugRect(QPainter *painter)
Draws a debugging rectangle of the item's current bounds within the specified painter.
virtual void drawFrame(QgsRenderContext &context)
Draws the frame around the item.
friend class QgsLayoutItemGroup
QgsAbstractLayoutUndoCommand * createCommand(const QString &text, int id, QUndoCommand *parent=nullptr) override
Creates a new layout undo command with the specified text and parent.
virtual QgsGeometry clipPath() const
Returns the clipping path generated by this item, in layout coordinates.
virtual QPainterPath framePath() const
Returns the path to use when drawing the item's frame or background.
virtual void cleanup()
Called just before a batch of items are deleted, allowing them to run cleanup tasks.
bool isGroupMember() const
Returns true if the item is part of a QgsLayoutItemGroup group.
QColor backgroundColor(bool useDataDefined=true) const
Returns the background color for this item.
void drawRefreshingOverlay(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle)
Draws a "refreshing" overlay icon on the item.
bool writeXml(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const
Stores the item state in a DOM element.
virtual void refreshDataDefinedProperty(QgsLayoutObject::DataDefinedProperty property=QgsLayoutObject::DataDefinedProperty::AllProperties)
Refreshes a data defined property for the item by reevaluating the property's value and redrawing the...
virtual void setFrameStrokeWidth(QgsLayoutMeasurement width)
Sets the frame stroke width.
virtual bool accept(QgsStyleEntityVisitorInterface *visitor) const
Accepts the specified style entity visitor, causing it to visit all style entities associated with th...
void refreshItemRotation(QPointF *origin=nullptr)
Refreshes an item's rotation by rechecking it against any possible overrides such as data defined rot...
UndoCommand
Layout item undo commands, used for collapsing undo commands.
QgsLayoutItemGroup * parentGroup() const
Returns the item's parent group, if the item is part of a QgsLayoutItemGroup group.
double itemRotation() const
Returns the current rotation for the item, in degrees clockwise.
QgsLayoutItem(QgsLayout *layout, bool manageZValue=true)
Constructor for QgsLayoutItem, with the specified parent layout.
virtual void setSelected(bool selected)
Sets whether the item should be selected.
QPointF adjustPointForReferencePosition(QPointF point, QSizeF size, ReferencePoint reference) const
Adjusts the specified point at which a reference position of the item sits and returns the top left c...
virtual void zoomContent(double factor, QPointF point)
Zooms content of item.
virtual void setItemRotation(double rotation, bool adjustPosition=true)
Sets the layout item's rotation, in degrees clockwise.
void rotationChanged(double newRotation)
Emitted on item rotation change.
virtual QgsLayoutItem::ExportLayerDetail exportLayerDetails() const
Returns the details for the specified current export layer.
void setBackgroundColor(const QColor &color)
Sets the background color for this item.
virtual QIcon icon() const
Returns the item's icon.
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
Handles preparing a paint surface for the layout item and painting the item's content.
bool excludeFromExports() const
Returns whether the item should be excluded from layout exports and prints.
virtual bool nextExportPart()
Moves to the next export part for a multi-layered export item, during a multi-layered export.
double itemOpacity() const
Returns the item's opacity.
virtual QRectF rectWithFrame() const
Returns the item's rectangular bounds, including any bleed caused by the item's frame.
void beginCommand(const QString &commandText, UndoCommand command=UndoNone)
Starts new undo command for this item.
void cancelCommand()
Cancels the current item command and discards it.
void setItemOpacity(double opacity)
Sets the item's opacity.
virtual void setVisibility(bool visible)
Sets whether the item is visible.
virtual void redraw()
Triggers a redraw (update) of the item.
QgsLayoutPoint positionWithUnits() const
Returns the item's current position, including units.
QgsLayoutPoint topLeftToReferencePoint(const QgsLayoutPoint &point) const
Returns the position for the reference point of the item, if the top-left of the item was placed at t...
ReferencePoint
Fixed position reference point.
@ LowerMiddle
Lower center of item.
@ MiddleLeft
Middle left of item.
@ UpperRight
Upper right corner of item.
@ LowerLeft
Lower left corner of item.
@ UpperLeft
Upper left corner of item.
@ UpperMiddle
Upper center of item.
@ MiddleRight
Middle right of item.
@ LowerRight
Lower right corner of item.
virtual void startLayeredExport()
Starts a multi-layer export operation.
virtual bool writePropertiesToElement(QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context) const
Stores item state within an XML DOM element.
void endCommand()
Completes the current item command and push it onto the layout's undo stack.
void refreshItemSize()
Refreshes an item's size by rechecking it against any possible item fixed or minimum sizes.
int page() const
Returns the page the item is currently on, with the first page returning 0.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
virtual void setId(const QString &id)
Set the item's id name.
void setFrameStrokeColor(const QColor &color)
Sets the frame stroke color.
virtual void finalizeRestoreFromXml()
Called after all pending items have been restored from XML.
void setFrameJoinStyle(Qt::PenJoinStyle style)
Sets the join style used when drawing the item's frame.
void refreshBackgroundColor(bool updateItem=true)
Refresh item's background color, considering data defined colors.
virtual void rotateItem(double angle, QPointF transformOrigin)
Rotates the item by a specified angle in degrees clockwise around a specified reference point.
bool readXml(const QDomElement &itemElement, const QDomDocument &document, const QgsReadWriteContext &context)
Sets the item state from a DOM element.
virtual void setFrameEnabled(bool drawFrame)
Sets whether this item has a frame drawn around it or not.
void setLocked(bool locked)
Sets whether the item is locked, preventing mouse interactions with the item.
~QgsLayoutItem() override
int type() const override
Returns a unique graphics item type identifier.
virtual void drawBackground(QgsRenderContext &context)
Draws the background for the item.
virtual QString displayName() const
Gets item display name.
virtual QgsLayoutSize minimumSize() const
Returns the minimum allowed size of the item, if applicable, or an empty size if item can be freely r...
virtual void attemptResize(const QgsLayoutSize &size, bool includesFrame=false)
Attempts to resize the item to a specified target size.
virtual bool requiresRasterization() const
Returns true if the item is drawn in such a way that forces the whole layout to be rasterized when ex...
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
virtual void moveContent(double dx, double dy)
Moves the content of the item, by a specified dx and dy in layout units.
@ FlagOverridesPaint
Item overrides the default layout item painting method.
@ FlagDisableSceneCaching
Item should not have QGraphicsItem caching enabled.
virtual void stopLayeredExport()
Stops a multi-layer export operation.
virtual bool containsAdvancedEffects() const
Returns true if the item contains contents with blend modes or transparency effects which can only be...
virtual QgsLayoutSize fixedSize() const
Returns the fixed size of the item, if applicable, or an empty size if item can be freely resized.
virtual void setMoveContentPreviewOffset(double dx, double dy)
Sets temporary offset for the item, by a specified dx and dy in layout units.
void setExcludeFromExports(bool exclude)
Sets whether the item should be excluded from layout exports and prints.
void sizePositionChanged()
Emitted when the item's size or position changes.
void lockChanged()
Emitted if the item's lock status changes.
virtual QSizeF applyItemSizeConstraint(QSizeF targetSize)
Applies any item-specific size constraint handling to a given targetSize in layout units.
virtual void invalidateCache()
Forces a deferred update of any cached image the item uses.
void refreshFrame(bool updateItem=true)
Refresh item's frame, considering data defined colors and frame size.
virtual QString uuid() const
Returns the item identification string.
virtual void attemptMove(const QgsLayoutPoint &point, bool useReferencePoint=true, bool includesFrame=false, int page=-1)
Attempts to move the item to a specified point.
QPointF pagePos() const
Returns the item's position (in layout units) relative to the top left corner of its current page.
QString id() const
Returns the item's ID name.
void setBlendMode(QPainter::CompositionMode mode)
Sets the item's composition blending mode.
bool frameEnabled() const
Returns true if the item includes a frame.
void frameChanged()
Emitted if the item's frame style changes.
virtual Flags itemFlags() const
Returns the item's flags, which indicate how the item behaves.
void attemptMoveBy(double deltaX, double deltaY)
Attempts to shift the item's position by a specified deltaX and deltaY, in layout units.
void setReferencePoint(ReferencePoint point)
Sets the reference point for positioning of the layout item.
virtual bool readPropertiesFromElement(const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context)
Sets item state from a DOM element.
ExportLayerBehavior
Behavior of item when exporting to layered outputs.
@ CanGroupWithAnyOtherItem
Item can be placed on a layer with any other item (default behavior).
virtual Q_DECL_DEPRECATED int numberExportLayers() const
Returns the number of layers that this item requires for exporting during layered exports (e....
virtual bool isRefreshing() const
Returns true if the item is currently refreshing content in the background.
void refreshBlendMode()
Refresh item's blend mode, considering data defined blend mode.
void setParentGroup(QgsLayoutItemGroup *group)
Sets the item's parent group.
QPointF positionAtReferencePoint(ReferencePoint reference) const
Returns the current position (in layout units) of a reference point for the item.
void refreshOpacity(bool updateItem=true)
Refresh item's opacity, considering data defined opacity.
QgsLayoutSize applyDataDefinedSize(const QgsLayoutSize &size)
Applies any present data defined size overrides to the specified layout size.
virtual void setMinimumSize(const QgsLayoutSize &size)
Sets the minimum allowed size for the layout item.
void refresh() override
Refreshes the item, causing a recalculation of any property overrides and recalculation of its positi...
void attemptSetSceneRect(const QRectF &rect, bool includesFrame=false)
Attempts to update the item's position and size to match the passed rect in layout coordinates.
virtual double estimatedFrameBleed() const
Returns the estimated amount the item's frame bleeds outside the item's actual rectangle.
virtual ExportLayerBehavior exportLayerBehavior() const
Returns the behavior of this item during exporting to layered exports (e.g.
void setBackgroundEnabled(bool drawBackground)
Sets whether this item has a background drawn under it or not.
void refreshItemPosition()
Refreshes an item's position by rechecking it against any possible overrides such as data defined pos...
virtual void setFixedSize(const QgsLayoutSize &size)
Sets a fixed size for the layout item, which prevents it from being freely resized.
QPainter::CompositionMode blendMode() const
Returns the item's composition blending mode.
virtual void draw(QgsLayoutItemRenderContext &context)=0
Draws the item's contents using the specified item render context.
QgsLayoutPoint pagePositionWithUnits() const
Returns the item's position (in item units) relative to the top left corner of its current page.
Provides a method of storing measurements for use in QGIS layouts using a variety of different measur...
static QgsLayoutMeasurement decodeMeasurement(const QString &string)
Decodes a measurement from a string.
QgsPropertyCollection mDataDefinedProperties
bool readObjectPropertiesFromElement(const QDomElement &parentElement, const QDomDocument &document, const QgsReadWriteContext &context)
Sets object properties from a DOM element.
const QgsLayout * layout() const
Returns the layout the object is attached to.
void changed()
Emitted when the object's properties change.
virtual void refresh()
Refreshes the object, causing a recalculation of any property overrides.
QPointer< QgsLayout > mLayout
QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the objects' current state.
DataDefinedProperty
Data defined properties for different item types.
@ PositionX
X position on page.
@ ItemWidth
Width of item.
@ ExcludeFromExports
Exclude item from exports.
@ ItemRotation
Rotation of item.
@ PositionY
Y position on page.
@ PaperOrientation
Paper orientation.
@ ItemHeight
Height of item.
@ BackgroundColor
Item background color.
@ BlendMode
Item blend mode.
@ PresetPaperSize
Preset paper size for composition.
@ FrameColor
Item frame color.
@ AllProperties
All properties for item.
QgsLayoutObject(QgsLayout *layout)
Constructor for QgsLayoutObject, with the specified parent layout.
bool writeObjectPropertiesToElement(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const
Stores object properties within an XML DOM element.
Provides a method of storing points, consisting of an x and y coordinate, for use in QGIS layouts.
double x() const
Returns x coordinate of point.
QPointF toQPointF() const
Converts the layout point to a QPointF.
void setX(const double x)
Sets the x coordinate of point.
static QgsLayoutPoint decodePoint(const QString &string)
Decodes a point from a string.
double y() const
Returns y coordinate of point.
Qgis::LayoutUnit units() const
Returns the units for the point.
void setY(const double y)
Sets y coordinate of point.
Provides a method of storing sizes, consisting of a width and height, for use in QGIS layouts.
static QgsLayoutSize decodeSize(const QString &string)
Decodes a size from a string.
double height() const
Returns the height of the size.
void setWidth(const double width)
Sets the width for the size.
Qgis::LayoutUnit units() const
Returns the units for the size.
double width() const
Returns the width of the size.
void setHeight(const double height)
Sets the height for the size.
static QgsRenderContext createRenderContextForLayout(QgsLayout *layout, QPainter *painter, double dpi=-1)
Creates a render context suitable for the specified layout and painter destination.
static bool itemIsAClippingSource(const QgsLayoutItem *item)
Returns true if an item is a clipping item for another layout item.
static QgsLayoutItemPage::Orientation decodePaperOrientation(const QString &string, bool &ok)
Decodes a string representing a paper orientation and returns the decoded orientation.
static double normalizedAngle(double angle, bool allowNegative=false)
Ensures that an angle (in degrees) is in the range 0 <= angle < 360.
static Q_DECL_DEPRECATED double scaleFactorFromItemStyle(const QStyleOptionGraphicsItem *style)
Extracts the scale factor from an item style.
@ ZItem
Minimum z value for items.
A named page size for layouts.
QgsLayoutSize size
Page size.
static Qgis::BlendMode getBlendModeEnum(QPainter::CompositionMode blendMode)
Returns a Qgis::BlendMode corresponding to a QPainter::CompositionMode.
static QPainter::CompositionMode getCompositionMode(Qgis::BlendMode blendMode)
Returns a QPainter::CompositionMode corresponding to a Qgis::BlendMode.
A container for the context for various read/write operations on objects.
Contains information about the context of a rendering operation.
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
QPainter * painter()
Returns the destination QPainter for the render operation.
void setPainterFlagsUsingContext(QPainter *painter=nullptr) const
Sets relevant flags on a destination painter, using the flags and settings currently defined for the ...
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
Scoped object for saving and restoring a QPainter object's state.
An interface for classes which can visit style entity (e.g.
QImage svgAsImage(const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth, double widthScaleFactor, bool &fitsInCache, double fixedAspectRatio=0, bool blocking=false, const QMap< QString, QString > ¶meters=QMap< QString, QString >())
Returns an SVG drawing as a QImage.
static Qt::PenJoinStyle decodePenJoinStyle(const QString &str)
static QPainter::CompositionMode decodeBlendMode(const QString &s)
static QString encodePenJoinStyle(Qt::PenJoinStyle style)
#define Q_NOWARN_DEPRECATED_POP
#define Q_NOWARN_DEPRECATED_PUSH
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Contains details of a particular export layer relating to a layout item.