QGIS API Documentation  2.99.0-Master (0cba29c)
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  , mMultiFrame( nullptr )
42  , mHidePageIfEmpty( false )
43  , mHideBackgroundIfEmpty( false )
44 {
45  //default to no background
46  setBackgroundEnabled( false );
47 }
48 
49 bool QgsComposerFrame::writeXml( QDomElement &elem, QDomDocument &doc ) const
50 {
51  QDomElement frameElem = doc.createElement( QStringLiteral( "ComposerFrame" ) );
52  frameElem.setAttribute( QStringLiteral( "sectionX" ), QString::number( mSection.x() ) );
53  frameElem.setAttribute( QStringLiteral( "sectionY" ), QString::number( mSection.y() ) );
54  frameElem.setAttribute( QStringLiteral( "sectionWidth" ), QString::number( mSection.width() ) );
55  frameElem.setAttribute( QStringLiteral( "sectionHeight" ), QString::number( mSection.height() ) );
56  frameElem.setAttribute( QStringLiteral( "hidePageIfEmpty" ), mHidePageIfEmpty );
57  frameElem.setAttribute( QStringLiteral( "hideBackgroundIfEmpty" ), mHideBackgroundIfEmpty );
58  elem.appendChild( frameElem );
59 
60  return _writeXml( frameElem, doc );
61 }
62 
63 bool QgsComposerFrame::readXml( const QDomElement &itemElem, const QDomDocument &doc )
64 {
65  double x = itemElem.attribute( QStringLiteral( "sectionX" ) ).toDouble();
66  double y = itemElem.attribute( QStringLiteral( "sectionY" ) ).toDouble();
67  double width = itemElem.attribute( QStringLiteral( "sectionWidth" ) ).toDouble();
68  double height = itemElem.attribute( QStringLiteral( "sectionHeight" ) ).toDouble();
69  mSection = QRectF( x, y, width, height );
70  mHidePageIfEmpty = itemElem.attribute( QStringLiteral( "hidePageIfEmpty" ), QStringLiteral( "0" ) ).toInt();
71  mHideBackgroundIfEmpty = itemElem.attribute( QStringLiteral( "hideBackgroundIfEmpty" ), QStringLiteral( "0" ) ).toInt();
72  QDomElement composerItem = itemElem.firstChildElement( QStringLiteral( "ComposerItem" ) );
73  if ( composerItem.isNull() )
74  {
75  return false;
76  }
77  return _readXml( composerItem, doc );
78 }
79 
81 {
82  mHidePageIfEmpty = hidePageIfEmpty;
83 }
84 
86 {
87  if ( hideBackgroundIfEmpty == mHideBackgroundIfEmpty )
88  {
89  return;
90  }
91 
92  mHideBackgroundIfEmpty = hideBackgroundIfEmpty;
93  update();
94 }
95 
97 {
98  if ( !mMultiFrame )
99  {
100  return true;
101  }
102 
103  double multiFrameHeight = mMultiFrame->totalSize().height();
104  if ( multiFrameHeight <= mSection.top() )
105  {
106  //multiframe height is less than top of this frame's visible portion
107  return true;
108  }
109 
110  return false;
111 
112 }
113 
115 {
116  if ( !mMultiFrame )
118 
119  //start with multiframe's context
120  QgsExpressionContext context = mMultiFrame->createExpressionContext();
121  //add frame's individual context
123 
124  return context;
125 }
126 
128 {
129  if ( !id().isEmpty() )
130  {
131  return id();
132  }
133 
134  if ( mMultiFrame )
135  {
136  return mMultiFrame->displayName();
137  }
138 
139  return tr( "<frame>" );
140 }
141 
142 void QgsComposerFrame::setSceneRect( const QRectF &rectangle )
143 {
144  QRectF fixedRect = rectangle;
145 
146  if ( mMultiFrame )
147  {
148  //calculate index of frame
149  int frameIndex = mMultiFrame->frameIndex( this );
150 
151  QSizeF fixedSize = mMultiFrame->fixedFrameSize( frameIndex );
152  if ( fixedSize.width() > 0 )
153  {
154  fixedRect.setWidth( fixedSize.width() );
155  }
156  if ( fixedSize.height() > 0 )
157  {
158  fixedRect.setHeight( fixedSize.height() );
159  }
160 
161  //check minimum size
162  QSizeF minSize = mMultiFrame->minFrameSize( frameIndex );
163  fixedRect.setWidth( qMax( minSize.width(), fixedRect.width() ) );
164  fixedRect.setHeight( qMax( minSize.height(), fixedRect.height() ) );
165  }
166 
167  QgsComposerItem::setSceneRect( fixedRect );
168 }
169 
170 void QgsComposerFrame::paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget )
171 {
172  Q_UNUSED( itemStyle );
173  Q_UNUSED( pWidget );
174 
175  if ( !painter )
176  {
177  return;
178  }
179  if ( !shouldDrawItem() )
180  {
181  return;
182  }
183 
184  bool empty = isEmpty();
185 
186  if ( !empty || !mHideBackgroundIfEmpty )
187  {
188  drawBackground( painter );
189  }
190  if ( mMultiFrame )
191  {
192  //calculate index of frame
193  int frameIndex = mMultiFrame->frameIndex( this );
194  mMultiFrame->render( painter, mSection, frameIndex );
195  }
196 
197  if ( !empty || !mHideBackgroundIfEmpty )
198  {
199  drawFrame( painter );
200  }
201  if ( isSelected() )
202  {
203  drawSelectionBoxes( painter );
204  }
205 }
206 
207 void QgsComposerFrame::beginItemCommand( const QString &text )
208 {
209  if ( mComposition )
210  {
212  }
213 }
214 
216 {
217  if ( mComposition )
218  {
220  }
221 }
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)