QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
qgslayoutframe.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutframe.cpp
3 ------------------
4 begin : October 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgslayoutframe.h"
17#include "moc_qgslayoutframe.cpp"
18#include "qgslayoutmultiframe.h"
20#include "qgslayout.h"
22
24 : QgsLayoutItem( layout )
25 , mMultiFrame( multiFrame )
26 , mMultiFrameUuid( multiFrame ? multiFrame->uuid() : QString() )
27{
28
29 //default to no background
30 setBackgroundEnabled( false );
31
32 if ( multiFrame )
33 {
34 //repaint frame when multiframe content changes
36 {
37 update();
38 } );
39
40 //force recalculation of rect, so that multiframe specified sizes can be applied
42 }
43}
44
49
51{
52 return new QgsLayoutFrame( layout, nullptr );
53}
54
56{
57 return mMultiFrame;
58}
59
61{
62 if ( !mMultiFrame )
63 return QgsLayoutSize();
64
65 //calculate index of frame
66 const int frameIndex = mMultiFrame->frameIndex( const_cast< QgsLayoutFrame * >( this ) );
67 //check minimum size
68 return QgsLayoutSize( mMultiFrame->minFrameSize( frameIndex ), Qgis::LayoutUnit::Millimeters );
69}
70
72{
73 if ( !mMultiFrame )
74 return QgsLayoutSize();
75
76 //calculate index of frame
77 const int frameIndex = mMultiFrame->frameIndex( const_cast< QgsLayoutFrame * >( this ) );
78 //check fixed size
79 return QgsLayoutSize( mMultiFrame->fixedFrameSize( frameIndex ), Qgis::LayoutUnit::Millimeters );
80}
81
86
88{
89 if ( mMultiFrame )
90 return mMultiFrame->icon();
91 else
92 return QIcon();
93}
94
95void QgsLayoutFrame::setHidePageIfEmpty( const bool hidePageIfEmpty )
96{
97 mHidePageIfEmpty = hidePageIfEmpty;
98}
99
100void QgsLayoutFrame::setHideBackgroundIfEmpty( const bool hideBackgroundIfEmpty )
101{
102 if ( hideBackgroundIfEmpty == mHideBackgroundIfEmpty )
103 {
104 return;
105 }
106
107 mHideBackgroundIfEmpty = hideBackgroundIfEmpty;
108 update();
109}
110
112{
113 if ( !mMultiFrame )
114 {
115 return true;
116 }
117
118 const double multiFrameHeight = mMultiFrame->totalSize().height();
119 if ( multiFrameHeight <= mSection.top() )
120 {
121 //multiframe height is less than top of this frame's visible portion
122 return true;
123 }
124
125 return false;
126
127}
128
130{
131 if ( !mMultiFrame )
133
134 //start with multiframe's context
135 QgsExpressionContext context = mMultiFrame->createExpressionContext();
136
137 //add frame's individual context
139
140 return context;
141}
142
147
149{
150 if ( !id().isEmpty() )
151 {
152 return id();
153 }
154
155 if ( mMultiFrame )
156 {
157 return mMultiFrame->displayName();
158 }
159
160 return tr( "<Frame>" );
161}
162
164{
165 if ( mMultiFrame )
166 mMultiFrame->handleFrameRemoval( this );
167 mMultiFrame = nullptr;
168
170}
171
173{
174 if ( mMultiFrame )
175 {
176 //calculate index of frame
177 const int frameIndex = mMultiFrame->frameIndex( this );
178 Q_ASSERT_X( frameIndex >= 0, "QgsLayoutFrame::draw", "Invalid frame index for frame" );
179 mMultiFrame->render( context, mSection, frameIndex );
180 }
181}
182
184{
185 if ( !isEmpty() || !mHideBackgroundIfEmpty )
186 {
187 QgsLayoutItem::drawFrame( context );
188 }
189}
190
192{
193 if ( !isEmpty() || !mHideBackgroundIfEmpty )
194 {
196 }
197}
198
199bool QgsLayoutFrame::writePropertiesToElement( QDomElement &parentElement, QDomDocument &, const QgsReadWriteContext & ) const
200{
201 parentElement.setAttribute( QStringLiteral( "multiFrame" ), mMultiFrameUuid );
202 parentElement.setAttribute( QStringLiteral( "multiFrameTemplateUuid" ), mMultiFrameUuid );
203 parentElement.setAttribute( QStringLiteral( "sectionX" ), QString::number( mSection.x() ) );
204 parentElement.setAttribute( QStringLiteral( "sectionY" ), QString::number( mSection.y() ) );
205 parentElement.setAttribute( QStringLiteral( "sectionWidth" ), QString::number( mSection.width() ) );
206 parentElement.setAttribute( QStringLiteral( "sectionHeight" ), QString::number( mSection.height() ) );
207 parentElement.setAttribute( QStringLiteral( "hidePageIfEmpty" ), mHidePageIfEmpty );
208 parentElement.setAttribute( QStringLiteral( "hideBackgroundIfEmpty" ), mHideBackgroundIfEmpty );
209 return true;
210}
211
212bool QgsLayoutFrame::readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &, const QgsReadWriteContext & )
213{
214 const double x = itemElem.attribute( QStringLiteral( "sectionX" ) ).toDouble();
215 const double y = itemElem.attribute( QStringLiteral( "sectionY" ) ).toDouble();
216 const double width = itemElem.attribute( QStringLiteral( "sectionWidth" ) ).toDouble();
217 const double height = itemElem.attribute( QStringLiteral( "sectionHeight" ) ).toDouble();
218 mSection = QRectF( x, y, width, height );
219 mHidePageIfEmpty = itemElem.attribute( QStringLiteral( "hidePageIfEmpty" ), QStringLiteral( "0" ) ).toInt();
220 mHideBackgroundIfEmpty = itemElem.attribute( QStringLiteral( "hideBackgroundIfEmpty" ), QStringLiteral( "0" ) ).toInt();
221
222 mMultiFrameUuid = itemElem.attribute( QStringLiteral( "multiFrame" ) );
223 if ( mMultiFrameUuid.isEmpty( ) )
224 {
225 mMultiFrameUuid = itemElem.attribute( QStringLiteral( "multiFrameTemplateUuid" ) );
226 }
227 mMultiFrame = mLayout->multiFrameByUuid( mMultiFrameUuid );
228 return true;
229}
@ Millimeters
Millimeters.
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.
Base class for frame items, which form a layout multiframe item.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
QgsLayoutSize fixedSize() const override
Returns the fixed size of the item, if applicable, or an empty size if item can be freely resized.
bool isEmpty() const
Returns whether the frame is empty.
int type() const override
QIcon icon() const override
Returns the item's icon.
bool readPropertiesFromElement(const QDomElement &itemElement, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets item state from a DOM element.
QgsLayoutFrame(QgsLayout *layout, QgsLayoutMultiFrame *multiFrame)
Constructor for QgsLayoutFrame, with the specified parent layout and belonging to a multiFrame.
void drawBackground(QgsRenderContext &context) override
Draws the background for the item.
void draw(QgsLayoutItemRenderContext &context) override
Draws the item's contents using the specified item render context.
~QgsLayoutFrame() override
QgsLayoutMultiFrame * multiFrame() const
Returns the parent multiframe for the frame.
QString displayName() const override
Gets item display name.
static QgsLayoutFrame * create(QgsLayout *layout)
Creates a new QgsLayoutFrame belonging to the specified layout.
ExportLayerBehavior exportLayerBehavior() const override
Returns the behavior of this item during exporting to layered exports (e.g.
bool writePropertiesToElement(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores item state within an XML DOM element.
bool hideBackgroundIfEmpty() const
Returns whether the background and frame stroke should be hidden if this frame is empty.
bool hidePageIfEmpty() const
Returns whether the page should be hidden (ie, not included in layout exports) if this frame is empty...
void setHideBackgroundIfEmpty(bool hideBackgroundIfEmpty)
Sets whether the background and frame stroke should be hidden if this frame is empty.
void drawFrame(QgsRenderContext &context) override
Draws the frame around the item.
void setHidePageIfEmpty(bool hidePageIfEmpty)
Sets whether the page should be hidden (ie, not included in layout exports) if this frame is empty.
QgsLayoutSize minimumSize() const override
Returns the minimum allowed size of the item, if applicable, or an empty size if item can be freely r...
void cleanup() override
Called just before a batch of items are deleted, allowing them to run cleanup tasks.
@ LayoutFrame
Frame item, part of a QgsLayoutMultiFrame object.
Contains settings and helpers relating to a render of a QgsLayoutItem.
Base class for graphical items within a QgsLayout.
virtual void drawFrame(QgsRenderContext &context)
Draws the frame around the item.
virtual void cleanup()
Called just before a batch of items are deleted, allowing them to run cleanup tasks.
void refreshItemSize()
Refreshes an item's size by rechecking it against any possible item fixed or minimum sizes.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
virtual void drawBackground(QgsRenderContext &context)
Draws the background for the item.
QString id() const
Returns the item's ID name.
ExportLayerBehavior
Behavior of item when exporting to layered outputs.
@ CanGroupWithItemsOfSameType
Item can only be placed on layers with other items of the same type, but multiple items of this type ...
void setBackgroundEnabled(bool drawBackground)
Sets whether this item has a background drawn under it or not.
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
virtual QSizeF totalSize() const =0
Returns the total size of the multiframe's content, in layout units.
void contentsChanged()
Emitted when the contents of the multi frame have changed and the frames must be redrawn.
virtual QSizeF fixedFrameSize(int frameIndex=-1) const
Returns the fixed size for a frame, if desired.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
virtual QIcon icon() const
Returns the item's icon.
virtual QSizeF minFrameSize(int frameIndex=-1) const
Returns the minimum size for a frames, if desired.
int frameIndex(QgsLayoutFrame *frame) const
Returns the index of a frame within the multiframe.
virtual QString displayName() const
Returns the multiframe display name.
virtual void render(QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex)=0
Renders a portion of the multiframe's content into a render context.
const QgsLayout * layout() const
Returns the layout the object is attached to.
QPointer< QgsLayout > mLayout
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:49
The class is used as a container of context for various read/write operations on other objects.
Contains information about the context of a rendering operation.