QGIS API Documentation 3.99.0-Master (d270888f95f)
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
19#include "qgslayout.h"
21#include "qgslayoutmultiframe.h"
22
23#include <QString>
24
25#include "moc_qgslayoutframe.cpp"
26
27using namespace Qt::StringLiterals;
28
31 , mMultiFrame( multiFrame )
32 , mMultiFrameUuid( multiFrame ? multiFrame->uuid() : QString() )
33{
34
35 //default to no background
36 setBackgroundEnabled( false );
37
38 if ( multiFrame )
39 {
40 //repaint frame when multiframe content changes
42 {
43 update();
44 } );
45
46 //force recalculation of rect, so that multiframe specified sizes can be applied
48 }
49}
50
55
60
62{
63 return mMultiFrame;
64}
65
67{
68 if ( !mMultiFrame )
69 return QgsLayoutSize();
70
71 //calculate index of frame
72 const int frameIndex = mMultiFrame->frameIndex( const_cast< QgsLayoutFrame * >( this ) );
73 //check minimum size
74 return QgsLayoutSize( mMultiFrame->minFrameSize( frameIndex ), Qgis::LayoutUnit::Millimeters );
75}
76
78{
79 if ( !mMultiFrame )
80 return QgsLayoutSize();
81
82 //calculate index of frame
83 const int frameIndex = mMultiFrame->frameIndex( const_cast< QgsLayoutFrame * >( this ) );
84 //check fixed size
85 return QgsLayoutSize( mMultiFrame->fixedFrameSize( frameIndex ), Qgis::LayoutUnit::Millimeters );
86}
87
92
94{
95 if ( mMultiFrame )
96 return mMultiFrame->icon();
97 else
98 return QIcon();
99}
100
102{
103 mHidePageIfEmpty = hidePageIfEmpty;
104}
105
107{
108 if ( hideBackgroundIfEmpty == mHideBackgroundIfEmpty )
109 {
110 return;
111 }
112
113 mHideBackgroundIfEmpty = hideBackgroundIfEmpty;
114 update();
115}
116
118{
119 if ( !mMultiFrame )
120 {
121 return true;
122 }
123
124 const double multiFrameHeight = mMultiFrame->totalSize().height();
125 if ( multiFrameHeight <= mSection.top() )
126 {
127 //multiframe height is less than top of this frame's visible portion
128 return true;
129 }
130
131 return false;
132
133}
134
136{
137 if ( !mMultiFrame )
139
140 //start with multiframe's context
141 QgsExpressionContext context = mMultiFrame->createExpressionContext();
142
143 //add frame's individual context
145
146 return context;
147}
148
153
155{
156 if ( !id().isEmpty() )
157 {
158 return id();
159 }
160
161 if ( mMultiFrame )
162 {
163 return mMultiFrame->displayName();
164 }
165
166 return tr( "<Frame>" );
167}
168
170{
171 if ( mMultiFrame )
172 mMultiFrame->handleFrameRemoval( this );
173 mMultiFrame = nullptr;
174
176}
177
179{
180 if ( mMultiFrame )
181 {
182 //calculate index of frame
183 const int frameIndex = mMultiFrame->frameIndex( this );
184 Q_ASSERT_X( frameIndex >= 0, "QgsLayoutFrame::draw", "Invalid frame index for frame" );
185 mMultiFrame->render( context, mSection, frameIndex );
186 }
187}
188
190{
191 if ( !isEmpty() || !mHideBackgroundIfEmpty )
192 {
193 QgsLayoutItem::drawFrame( context );
194 }
195}
196
198{
199 if ( !isEmpty() || !mHideBackgroundIfEmpty )
200 {
202 }
203}
204
205bool QgsLayoutFrame::writePropertiesToElement( QDomElement &parentElement, QDomDocument &, const QgsReadWriteContext & ) const
206{
207 parentElement.setAttribute( u"multiFrame"_s, mMultiFrameUuid );
208 parentElement.setAttribute( u"multiFrameTemplateUuid"_s, mMultiFrameUuid );
209 parentElement.setAttribute( u"sectionX"_s, QString::number( mSection.x() ) );
210 parentElement.setAttribute( u"sectionY"_s, QString::number( mSection.y() ) );
211 parentElement.setAttribute( u"sectionWidth"_s, QString::number( mSection.width() ) );
212 parentElement.setAttribute( u"sectionHeight"_s, QString::number( mSection.height() ) );
213 parentElement.setAttribute( u"hidePageIfEmpty"_s, mHidePageIfEmpty );
214 parentElement.setAttribute( u"hideBackgroundIfEmpty"_s, mHideBackgroundIfEmpty );
215 return true;
216}
217
218bool QgsLayoutFrame::readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &, const QgsReadWriteContext & )
219{
220 const double x = itemElem.attribute( u"sectionX"_s ).toDouble();
221 const double y = itemElem.attribute( u"sectionY"_s ).toDouble();
222 const double width = itemElem.attribute( u"sectionWidth"_s ).toDouble();
223 const double height = itemElem.attribute( u"sectionHeight"_s ).toDouble();
224 mSection = QRectF( x, y, width, height );
225 mHidePageIfEmpty = itemElem.attribute( u"hidePageIfEmpty"_s, u"0"_s ).toInt();
226 mHideBackgroundIfEmpty = itemElem.attribute( u"hideBackgroundIfEmpty"_s, u"0"_s ).toInt();
227
228 mMultiFrameUuid = itemElem.attribute( u"multiFrame"_s );
229 if ( mMultiFrameUuid.isEmpty( ) )
230 {
231 mMultiFrameUuid = itemElem.attribute( u"multiFrameTemplateUuid"_s );
232 }
233 mMultiFrame = mLayout->multiFrameByUuid( mMultiFrameUuid );
234 return true;
235}
@ Millimeters
Millimeters.
Definition qgis.h:5276
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.
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.
friend class QgsLayoutMultiFrame
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.
virtual void drawFrame(QgsRenderContext &context)
Draws the frame around the item.
friend class QgsLayout
virtual void cleanup()
Called just before a batch of items are deleted, allowing them to run cleanup tasks.
QgsLayoutItem(QgsLayout *layout, bool manageZValue=true)
Constructor for QgsLayoutItem, with the specified parent layout.
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.
virtual QString uuid() const
Returns the item identification string.
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.
void contentsChanged()
Emitted when the contents of the multi frame have changed and the frames must be redrawn.
const QgsLayout * layout() const
Returns the layout the object is attached to.
QPointer< QgsLayout > mLayout
Provides a method of storing sizes, consisting of a width and height, for use in QGIS layouts.
A container for the context for various read/write operations on objects.
Contains information about the context of a rendering operation.