QGIS API Documentation  3.2.0-Bonn (bc43194)
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 "qgslayoutmultiframe.h"
18 #include "qgslayoutitemregistry.h"
19 #include "qgslayout.h"
20 
22  : QgsLayoutItem( layout )
23  , mMultiFrame( multiFrame )
24  , mMultiFrameUuid( multiFrame ? multiFrame->uuid() : QString() )
25 {
26 
27  //default to no background
28  setBackgroundEnabled( false );
29 
30  if ( multiFrame )
31  {
32  //repaint frame when multiframe content changes
33  connect( multiFrame, &QgsLayoutMultiFrame::contentsChanged, this, [ = ]
34  {
35  update();
36  } );
37 
38  //force recalculation of rect, so that multiframe specified sizes can be applied
40  }
41 }
42 
44 {
45  return new QgsLayoutFrame( layout, nullptr );
46 }
47 
49 {
50  return mMultiFrame;
51 }
52 
54 {
55  if ( !mMultiFrame )
56  return QgsLayoutSize();
57 
58  //calculate index of frame
59  int frameIndex = mMultiFrame->frameIndex( const_cast< QgsLayoutFrame * >( this ) );
60  //check minimum size
61  return QgsLayoutSize( mMultiFrame->minFrameSize( frameIndex ), QgsUnitTypes::LayoutMillimeters );
62 }
63 
65 {
66  if ( !mMultiFrame )
67  return QgsLayoutSize();
68 
69  //calculate index of frame
70  int frameIndex = mMultiFrame->frameIndex( const_cast< QgsLayoutFrame * >( this ) );
71  //check fixed size
72  return QgsLayoutSize( mMultiFrame->fixedFrameSize( frameIndex ), QgsUnitTypes::LayoutMillimeters );
73 }
74 
76 {
78 }
79 
80 QIcon QgsLayoutFrame::icon() const
81 {
82  if ( mMultiFrame )
83  return mMultiFrame->icon();
84  else
85  return QIcon();
86 }
87 
89 {
90  mHidePageIfEmpty = hidePageIfEmpty;
91 }
92 
94 {
95  if ( hideBackgroundIfEmpty == mHideBackgroundIfEmpty )
96  {
97  return;
98  }
99 
100  mHideBackgroundIfEmpty = hideBackgroundIfEmpty;
101  update();
102 }
103 
105 {
106  if ( !mMultiFrame )
107  {
108  return true;
109  }
110 
111  double multiFrameHeight = mMultiFrame->totalSize().height();
112  if ( multiFrameHeight <= mSection.top() )
113  {
114  //multiframe height is less than top of this frame's visible portion
115  return true;
116  }
117 
118  return false;
119 
120 }
121 
123 {
124  if ( !mMultiFrame )
126 
127  //start with multiframe's context
128  QgsExpressionContext context = mMultiFrame->createExpressionContext();
129 
130  //add frame's individual context
132 
133  return context;
134 }
135 
136 
138 {
139  if ( !id().isEmpty() )
140  {
141  return id();
142  }
143 
144  if ( mMultiFrame )
145  {
146  return mMultiFrame->displayName();
147  }
148 
149  return tr( "<Frame>" );
150 }
151 
153 {
154  if ( mMultiFrame )
155  mMultiFrame->handleFrameRemoval( this );
156 
158 }
159 
161 {
162  if ( mMultiFrame )
163  {
164  //calculate index of frame
165  int frameIndex = mMultiFrame->frameIndex( this );
166  Q_ASSERT_X( frameIndex >= 0, "QgsLayoutFrame::draw", "Invalid frame index for frame" );
167  mMultiFrame->render( context, mSection, frameIndex );
168  }
169 }
170 
172 {
173  if ( !isEmpty() || !mHideBackgroundIfEmpty )
174  {
175  QgsLayoutItem::drawFrame( context );
176  }
177 }
178 
180 {
181  if ( !isEmpty() || !mHideBackgroundIfEmpty )
182  {
184  }
185 }
186 
187 bool QgsLayoutFrame::writePropertiesToElement( QDomElement &parentElement, QDomDocument &, const QgsReadWriteContext & ) const
188 {
189  parentElement.setAttribute( QStringLiteral( "multiFrame" ), mMultiFrameUuid );
190  parentElement.setAttribute( QStringLiteral( "multiFrameTemplateUuid" ), mMultiFrameUuid );
191  parentElement.setAttribute( QStringLiteral( "sectionX" ), QString::number( mSection.x() ) );
192  parentElement.setAttribute( QStringLiteral( "sectionY" ), QString::number( mSection.y() ) );
193  parentElement.setAttribute( QStringLiteral( "sectionWidth" ), QString::number( mSection.width() ) );
194  parentElement.setAttribute( QStringLiteral( "sectionHeight" ), QString::number( mSection.height() ) );
195  parentElement.setAttribute( QStringLiteral( "hidePageIfEmpty" ), mHidePageIfEmpty );
196  parentElement.setAttribute( QStringLiteral( "hideBackgroundIfEmpty" ), mHideBackgroundIfEmpty );
197  return true;
198 }
199 
200 bool QgsLayoutFrame::readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &, const QgsReadWriteContext & )
201 {
202  double x = itemElem.attribute( QStringLiteral( "sectionX" ) ).toDouble();
203  double y = itemElem.attribute( QStringLiteral( "sectionY" ) ).toDouble();
204  double width = itemElem.attribute( QStringLiteral( "sectionWidth" ) ).toDouble();
205  double height = itemElem.attribute( QStringLiteral( "sectionHeight" ) ).toDouble();
206  mSection = QRectF( x, y, width, height );
207  mHidePageIfEmpty = itemElem.attribute( QStringLiteral( "hidePageIfEmpty" ), QStringLiteral( "0" ) ).toInt();
208  mHideBackgroundIfEmpty = itemElem.attribute( QStringLiteral( "hideBackgroundIfEmpty" ), QStringLiteral( "0" ) ).toInt();
209 
210  mMultiFrameUuid = itemElem.attribute( QStringLiteral( "multiFrame" ) );
211  if ( mMultiFrameUuid.isEmpty( ) )
212  {
213  mMultiFrameUuid = itemElem.attribute( QStringLiteral( "multiFrameTemplateUuid" ) );
214  }
215  mMultiFrame = mLayout->multiFrameByUuid( mMultiFrameUuid );
216  return true;
217 }
The class is used as a container of context for various read/write operations on other objects...
void draw(QgsLayoutItemRenderContext &context) override
Draws the item&#39;s contents using the specified item render context.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
virtual QSizeF totalSize() const =0
Returns the total size of the multiframe&#39;s content, in layout units.
QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the objects&#39; current state.
virtual QIcon icon() const
Returns the item&#39;s icon.
virtual QSizeF minFrameSize(int frameIndex=-1) const
Returns the minimum size for a frames, if desired.
Base class for graphical items within a QgsLayout.
QString displayName() const override
Gets item display name.
int type() const override
int frameIndex(QgsLayoutFrame *frame) const
Returns the index of a frame within the multiframe.
QgsExpressionContext createExpressionContext() const override
This method needs to be reimplemented in all classes which implement this interface and return an exp...
bool readPropertiesFromElement(const QDomElement &itemElement, const QDomDocument &document, const QgsReadWriteContext &context) override
Sets item state from a DOM element.
void cleanup() override
Called just before a batch of items are deleted, allowing them to run cleanup tasks.
Abstract base class for layout items with the ability to distribute the content to several frames (Qg...
static QgsExpressionContextScope * layoutItemScope(const QgsLayoutItem *item)
Creates a new scope which contains variables and functions relating to a QgsLayoutItem.
void refreshItemSize()
Refreshes an item&#39;s size by rechecking it against any possible item fixed or minimum sizes...
virtual void drawFrame(QgsRenderContext &context)
Draws the frame around the item.
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.
virtual void drawBackground(QgsRenderContext &context)
Draws the background for the item.
void drawBackground(QgsRenderContext &context) override
Draws the background for the item.
QgsLayoutSize fixedSize() const override
Returns the fixed size of the item, if applicable, or an empty size if item can be freely resized...
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
virtual void cleanup()
Called just before a batch of items are deleted, allowing them to run cleanup tasks.
QPointer< QgsLayout > mLayout
QgsLayoutMultiFrame * multiFrame() const
Returns the parent multiframe for the frame.
void setHideBackgroundIfEmpty(bool hideBackgroundIfEmpty)
Sets whether the background and frame stroke should be hidden 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...
QString id() const
Returns the item&#39;s ID name.
QIcon icon() const override
Returns the item&#39;s icon.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:49
Frame item, part of a QgsLayoutMultiFrame object.
void setBackgroundEnabled(bool drawBackground)
Sets whether this item has a background drawn under it or not.
Contains settings and helpers relating to a render of a QgsLayoutItem.
Definition: qgslayoutitem.h:43
QgsLayoutFrame(QgsLayout *layout, QgsLayoutMultiFrame *multiFrame)
Constructor for QgsLayoutFrame, with the specified parent layout and belonging to a multiFrame...
virtual QString displayName() const
Returns the multiframe display name.
Contains information about the context of a rendering operation.
virtual void render(QgsLayoutItemRenderContext &context, const QRectF &renderExtent, int frameIndex)=0
Renders a portion of the multiframe&#39;s content into a render context.
virtual QSizeF fixedFrameSize(int frameIndex=-1) const
Returns the fixed size for a frame, if desired.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
bool hidePageIfEmpty() const
Returns whether the page should be hidden (ie, not included in layout exports) if this frame is empty...
bool hideBackgroundIfEmpty() const
Returns whether the background and frame stroke should be hidden if this frame is empty...
This class provides a method of storing sizes, consisting of a width and height, for use in QGIS layo...
Definition: qgslayoutsize.h:40
void drawFrame(QgsRenderContext &context) override
Draws the frame around the item.
bool isEmpty() const
Returns whether the frame is empty.
bool writePropertiesToElement(QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context) const override
Stores item state within an XML DOM element.
Base class for frame items, which form a layout multiframe item.
void setHidePageIfEmpty(bool hidePageIfEmpty)
Sets whether the page should be hidden (ie, not included in layout exports) if this frame is empty...
static QgsLayoutFrame * create(QgsLayout *layout)
Creates a new QgsLayoutFrame belonging to the specified layout.