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