QGIS API Documentation 3.99.0-Master (26c88405ac0)
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 "moc_qgslayoutframe.cpp"
24
27 , mMultiFrame( multiFrame )
28 , mMultiFrameUuid( multiFrame ? multiFrame->uuid() : QString() )
29{
30
31 //default to no background
32 setBackgroundEnabled( false );
33
34 if ( multiFrame )
35 {
36 //repaint frame when multiframe content changes
38 {
39 update();
40 } );
41
42 //force recalculation of rect, so that multiframe specified sizes can be applied
44 }
45}
46
51
56
58{
59 return mMultiFrame;
60}
61
63{
64 if ( !mMultiFrame )
65 return QgsLayoutSize();
66
67 //calculate index of frame
68 const int frameIndex = mMultiFrame->frameIndex( const_cast< QgsLayoutFrame * >( this ) );
69 //check minimum size
70 return QgsLayoutSize( mMultiFrame->minFrameSize( frameIndex ), Qgis::LayoutUnit::Millimeters );
71}
72
74{
75 if ( !mMultiFrame )
76 return QgsLayoutSize();
77
78 //calculate index of frame
79 const int frameIndex = mMultiFrame->frameIndex( const_cast< QgsLayoutFrame * >( this ) );
80 //check fixed size
81 return QgsLayoutSize( mMultiFrame->fixedFrameSize( frameIndex ), Qgis::LayoutUnit::Millimeters );
82}
83
88
90{
91 if ( mMultiFrame )
92 return mMultiFrame->icon();
93 else
94 return QIcon();
95}
96
98{
99 mHidePageIfEmpty = hidePageIfEmpty;
100}
101
103{
104 if ( hideBackgroundIfEmpty == mHideBackgroundIfEmpty )
105 {
106 return;
107 }
108
109 mHideBackgroundIfEmpty = hideBackgroundIfEmpty;
110 update();
111}
112
114{
115 if ( !mMultiFrame )
116 {
117 return true;
118 }
119
120 const double multiFrameHeight = mMultiFrame->totalSize().height();
121 if ( multiFrameHeight <= mSection.top() )
122 {
123 //multiframe height is less than top of this frame's visible portion
124 return true;
125 }
126
127 return false;
128
129}
130
132{
133 if ( !mMultiFrame )
135
136 //start with multiframe's context
137 QgsExpressionContext context = mMultiFrame->createExpressionContext();
138
139 //add frame's individual context
141
142 return context;
143}
144
149
151{
152 if ( !id().isEmpty() )
153 {
154 return id();
155 }
156
157 if ( mMultiFrame )
158 {
159 return mMultiFrame->displayName();
160 }
161
162 return tr( "<Frame>" );
163}
164
166{
167 if ( mMultiFrame )
168 mMultiFrame->handleFrameRemoval( this );
169 mMultiFrame = nullptr;
170
172}
173
175{
176 if ( mMultiFrame )
177 {
178 //calculate index of frame
179 const int frameIndex = mMultiFrame->frameIndex( this );
180 Q_ASSERT_X( frameIndex >= 0, "QgsLayoutFrame::draw", "Invalid frame index for frame" );
181 mMultiFrame->render( context, mSection, frameIndex );
182 }
183}
184
186{
187 if ( !isEmpty() || !mHideBackgroundIfEmpty )
188 {
189 QgsLayoutItem::drawFrame( context );
190 }
191}
192
194{
195 if ( !isEmpty() || !mHideBackgroundIfEmpty )
196 {
198 }
199}
200
201bool QgsLayoutFrame::writePropertiesToElement( QDomElement &parentElement, QDomDocument &, const QgsReadWriteContext & ) const
202{
203 parentElement.setAttribute( QStringLiteral( "multiFrame" ), mMultiFrameUuid );
204 parentElement.setAttribute( QStringLiteral( "multiFrameTemplateUuid" ), mMultiFrameUuid );
205 parentElement.setAttribute( QStringLiteral( "sectionX" ), QString::number( mSection.x() ) );
206 parentElement.setAttribute( QStringLiteral( "sectionY" ), QString::number( mSection.y() ) );
207 parentElement.setAttribute( QStringLiteral( "sectionWidth" ), QString::number( mSection.width() ) );
208 parentElement.setAttribute( QStringLiteral( "sectionHeight" ), QString::number( mSection.height() ) );
209 parentElement.setAttribute( QStringLiteral( "hidePageIfEmpty" ), mHidePageIfEmpty );
210 parentElement.setAttribute( QStringLiteral( "hideBackgroundIfEmpty" ), mHideBackgroundIfEmpty );
211 return true;
212}
213
214bool QgsLayoutFrame::readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &, const QgsReadWriteContext & )
215{
216 const double x = itemElem.attribute( QStringLiteral( "sectionX" ) ).toDouble();
217 const double y = itemElem.attribute( QStringLiteral( "sectionY" ) ).toDouble();
218 const double width = itemElem.attribute( QStringLiteral( "sectionWidth" ) ).toDouble();
219 const double height = itemElem.attribute( QStringLiteral( "sectionHeight" ) ).toDouble();
220 mSection = QRectF( x, y, width, height );
221 mHidePageIfEmpty = itemElem.attribute( QStringLiteral( "hidePageIfEmpty" ), QStringLiteral( "0" ) ).toInt();
222 mHideBackgroundIfEmpty = itemElem.attribute( QStringLiteral( "hideBackgroundIfEmpty" ), QStringLiteral( "0" ) ).toInt();
223
224 mMultiFrameUuid = itemElem.attribute( QStringLiteral( "multiFrame" ) );
225 if ( mMultiFrameUuid.isEmpty( ) )
226 {
227 mMultiFrameUuid = itemElem.attribute( QStringLiteral( "multiFrameTemplateUuid" ) );
228 }
229 mMultiFrame = mLayout->multiFrameByUuid( mMultiFrameUuid );
230 return true;
231}
@ Millimeters
Millimeters.
Definition qgis.h:5204
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.