QGIS API Documentation  2.99.0-Master (01468d0)
qgscomposerframe.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposerframe.cpp
3  ------------------------------------------------------------
4  begin : July 2012
5  copyright : (C) 2012 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
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 "qgscomposerframe.h"
17 #include "qgscomposermultiframe.h"
18 #include "qgscomposition.h"
19 
20 QgsComposerFrame::QgsComposerFrame( QgsComposition *c, QgsComposerMultiFrame *mf, qreal x, qreal y, qreal width, qreal height )
21  : QgsComposerItem( x, y, width, height, c )
22  , mMultiFrame( mf )
23  , mHidePageIfEmpty( false )
24  , mHideBackgroundIfEmpty( false )
25 {
26 
27  //default to no background
28  setBackgroundEnabled( false );
29 
30  //repaint frame when multiframe content changes
32  if ( mf )
33  {
34  //force recalculation of rect, so that multiframe specified sizes can be applied
35  setSceneRect( QRectF( pos().x(), pos().y(), rect().width(), rect().height() ) );
36  }
37 }
38 
40  : QgsComposerItem( 0, 0, 0, 0, nullptr )
41  , mHidePageIfEmpty( false )
42  , mHideBackgroundIfEmpty( false )
43 {
44  //default to no background
45  setBackgroundEnabled( false );
46 }
47 
48 bool QgsComposerFrame::writeXml( QDomElement &elem, QDomDocument &doc ) const
49 {
50  QDomElement frameElem = doc.createElement( QStringLiteral( "ComposerFrame" ) );
51  frameElem.setAttribute( QStringLiteral( "sectionX" ), QString::number( mSection.x() ) );
52  frameElem.setAttribute( QStringLiteral( "sectionY" ), QString::number( mSection.y() ) );
53  frameElem.setAttribute( QStringLiteral( "sectionWidth" ), QString::number( mSection.width() ) );
54  frameElem.setAttribute( QStringLiteral( "sectionHeight" ), QString::number( mSection.height() ) );
55  frameElem.setAttribute( QStringLiteral( "hidePageIfEmpty" ), mHidePageIfEmpty );
56  frameElem.setAttribute( QStringLiteral( "hideBackgroundIfEmpty" ), mHideBackgroundIfEmpty );
57  elem.appendChild( frameElem );
58 
59  return _writeXml( frameElem, doc );
60 }
61 
62 bool QgsComposerFrame::readXml( const QDomElement &itemElem, const QDomDocument &doc )
63 {
64  double x = itemElem.attribute( QStringLiteral( "sectionX" ) ).toDouble();
65  double y = itemElem.attribute( QStringLiteral( "sectionY" ) ).toDouble();
66  double width = itemElem.attribute( QStringLiteral( "sectionWidth" ) ).toDouble();
67  double height = itemElem.attribute( QStringLiteral( "sectionHeight" ) ).toDouble();
68  mSection = QRectF( x, y, width, height );
69  mHidePageIfEmpty = itemElem.attribute( QStringLiteral( "hidePageIfEmpty" ), QStringLiteral( "0" ) ).toInt();
70  mHideBackgroundIfEmpty = itemElem.attribute( QStringLiteral( "hideBackgroundIfEmpty" ), QStringLiteral( "0" ) ).toInt();
71  QDomElement composerItem = itemElem.firstChildElement( QStringLiteral( "ComposerItem" ) );
72  if ( composerItem.isNull() )
73  {
74  return false;
75  }
76  return _readXml( composerItem, doc );
77 }
78 
80 {
81  mHidePageIfEmpty = hidePageIfEmpty;
82 }
83 
85 {
86  if ( hideBackgroundIfEmpty == mHideBackgroundIfEmpty )
87  {
88  return;
89  }
90 
91  mHideBackgroundIfEmpty = hideBackgroundIfEmpty;
92  update();
93 }
94 
96 {
97  if ( !mMultiFrame )
98  {
99  return true;
100  }
101 
102  double multiFrameHeight = mMultiFrame->totalSize().height();
103  if ( multiFrameHeight <= mSection.top() )
104  {
105  //multiframe height is less than top of this frame's visible portion
106  return true;
107  }
108 
109  return false;
110 
111 }
112 
114 {
115  if ( !mMultiFrame )
117 
118  //start with multiframe's context
119  QgsExpressionContext context = mMultiFrame->createExpressionContext();
120  //add frame's individual context
122 
123  return context;
124 }
125 
127 {
128  if ( !id().isEmpty() )
129  {
130  return id();
131  }
132 
133  if ( mMultiFrame )
134  {
135  return mMultiFrame->displayName();
136  }
137 
138  return tr( "<frame>" );
139 }
140 
141 void QgsComposerFrame::setSceneRect( const QRectF &rectangle )
142 {
143  QRectF fixedRect = rectangle;
144 
145  if ( mMultiFrame )
146  {
147  //calculate index of frame
148  int frameIndex = mMultiFrame->frameIndex( this );
149 
150  QSizeF fixedSize = mMultiFrame->fixedFrameSize( frameIndex );
151  if ( fixedSize.width() > 0 )
152  {
153  fixedRect.setWidth( fixedSize.width() );
154  }
155  if ( fixedSize.height() > 0 )
156  {
157  fixedRect.setHeight( fixedSize.height() );
158  }
159 
160  //check minimum size
161  QSizeF minSize = mMultiFrame->minFrameSize( frameIndex );
162  fixedRect.setWidth( std::max( minSize.width(), fixedRect.width() ) );
163  fixedRect.setHeight( std::max( minSize.height(), fixedRect.height() ) );
164  }
165 
166  QgsComposerItem::setSceneRect( fixedRect );
167 }
168 
169 void QgsComposerFrame::paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget )
170 {
171  Q_UNUSED( itemStyle );
172  Q_UNUSED( pWidget );
173 
174  if ( !painter )
175  {
176  return;
177  }
178  if ( !shouldDrawItem() )
179  {
180  return;
181  }
182 
183  bool empty = isEmpty();
184 
185  if ( !empty || !mHideBackgroundIfEmpty )
186  {
187  drawBackground( painter );
188  }
189  if ( mMultiFrame )
190  {
191  //calculate index of frame
192  int frameIndex = mMultiFrame->frameIndex( this );
193  mMultiFrame->render( painter, mSection, frameIndex );
194  }
195 
196  if ( !empty || !mHideBackgroundIfEmpty )
197  {
198  drawFrame( painter );
199  }
200  if ( isSelected() )
201  {
202  drawSelectionBoxes( painter );
203  }
204 }
205 
206 void QgsComposerFrame::beginItemCommand( const QString &text )
207 {
208  if ( mComposition )
209  {
211  }
212 }
213 
215 {
216  if ( mComposition )
217  {
219  }
220 }
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
virtual QgsExpressionContext createExpressionContext() const
Creates an expression context relating to the objects&#39; current state.
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
void setHideBackgroundIfEmpty(const bool hideBackgroundIfEmpty)
Sets whether the background and frame stroke should be hidden if this frame is empty.
virtual QString displayName() const override
Get item display name.
bool writeXml(QDomElement &elem, QDomDocument &doc) const override
Stores item state in DOM element.
virtual QSizeF minFrameSize(const int frameIndex=-1) const
Returns the minimum size for a frames, if desired.
A item that forms part of a map composition.
virtual QString displayName() const
Get multiframe display name.
virtual void drawFrame(QPainter *p)
Draw black frame around item.
virtual QSizeF totalSize() const =0
Returns the total size of the multiframe&#39;s content.
bool isEmpty() const
Returns whether the frame is empty.
bool _writeXml(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document. Usually called from writeXml methods of ...
virtual void drawSelectionBoxes(QPainter *p)
Draws additional graphics on selected items.
void endItemCommand() override
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Abstract base class for composer items with the ability to distribute the content to several frames (...
void setSceneRect(const QRectF &rectangle) override
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
void repaint() override
bool _readXml(const QDomElement &itemElem, const QDomDocument &doc)
Reads parameter that are not subclass specific in document. Usually called from readXml methods of su...
void setBackgroundEnabled(const bool drawBackground)
Set whether this item has a Background drawn around it or not.
static QgsExpressionContextScope * composerItemScope(const QgsComposerItem *composerItem)
Creates a new scope which contains variables and functions relating to a QgsComposerItem.
Graphics scene for map printing.
void beginItemCommand(const QString &text) override
virtual QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the item&#39;s current state.
void setHidePageIfEmpty(const bool hidePageIfEmpty)
Sets whether the page should be hidden (ie, not included in composer exports) if this frame is empty...
QgsComposition * mComposition
bool readXml(const QDomElement &itemElem, const QDomDocument &doc) override
Sets item state from DOM element.
void contentsChanged()
Emitted when the contents of the multi frame have changed and the frames must be redrawn.
bool hidePageIfEmpty() const
Returns whether the page should be hidden (ie, not included in composer exports) if this frame is emp...
bool hideBackgroundIfEmpty() const
Returns whether the background and frame stroke should be hidden if this frame is empty...
QgsComposerMultiFrame * multiFrame() const
Returns the parent multiframe for the frame.
virtual void drawBackground(QPainter *p)
Draw background.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
virtual QSizeF fixedFrameSize(const int frameIndex=-1) const
Returns the fixed size for a frame, if desired.
virtual void setSceneRect(const QRectF &rectangle)
Sets this items bound in scene coordinates such that 1 item size units corresponds to 1 scene size un...
QgsComposerFrame(QgsComposition *c, QgsComposerMultiFrame *mf, qreal x, qreal y, qreal width, qreal height)
virtual void render(QPainter *painter, const QRectF &renderExtent, const int frameIndex)=0
Renders a portion of the multiframe&#39;s content into a painter.
int frameIndex(QgsComposerFrame *frame) const
Returns the index of a frame within the multiframe.
QgsComposerItem(QgsComposition *composition, bool manageZValue=true)
Constructor.
virtual QgsExpressionContext createExpressionContext() const override
Creates an expression context relating to the item&#39;s current state.
QString id() const
Get item&#39;s id (which is not necessarly unique)
void beginMultiFrameCommand(QgsComposerMultiFrame *multiFrame, const QString &text, const QgsComposerMultiFrameMergeCommand::Context c=QgsComposerMultiFrameMergeCommand::Unknown)