QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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
31  connect( mf, SIGNAL( contentsChanged() ), this, SLOT( repaint() ) );
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 
39 QgsComposerFrame::QgsComposerFrame()
40  : QgsComposerItem( 0, 0, 0, 0, 0 )
41  , mMultiFrame( 0 )
42  , mHidePageIfEmpty( false )
43  , mHideBackgroundIfEmpty( false )
44 {
45  //default to no background
46  setBackgroundEnabled( false );
47 }
48 
50 {
51 }
52 
54 {
55  QDomElement frameElem = doc.createElement( "ComposerFrame" );
56  frameElem.setAttribute( "sectionX", QString::number( mSection.x() ) );
57  frameElem.setAttribute( "sectionY", QString::number( mSection.y() ) );
58  frameElem.setAttribute( "sectionWidth", QString::number( mSection.width() ) );
59  frameElem.setAttribute( "sectionHeight", QString::number( mSection.height() ) );
60  frameElem.setAttribute( "hidePageIfEmpty", mHidePageIfEmpty );
61  frameElem.setAttribute( "hideBackgroundIfEmpty", mHideBackgroundIfEmpty );
62  elem.appendChild( frameElem );
63 
64  return _writeXML( frameElem, doc );
65 }
66 
67 bool QgsComposerFrame::readXML( const QDomElement& itemElem, const QDomDocument& doc )
68 {
69  double x = itemElem.attribute( "sectionX" ).toDouble();
70  double y = itemElem.attribute( "sectionY" ).toDouble();
71  double width = itemElem.attribute( "sectionWidth" ).toDouble();
72  double height = itemElem.attribute( "sectionHeight" ).toDouble();
73  mSection = QRectF( x, y, width, height );
74  mHidePageIfEmpty = itemElem.attribute( "hidePageIfEmpty", "0" ).toInt();
75  mHideBackgroundIfEmpty = itemElem.attribute( "hideBackgroundIfEmpty", "0" ).toInt();
76  QDomElement composerItem = itemElem.firstChildElement( "ComposerItem" );
77  if ( composerItem.isNull() )
78  {
79  return false;
80  }
81  return _readXML( composerItem, doc );
82 }
83 
84 void QgsComposerFrame::setHidePageIfEmpty( const bool hidePageIfEmpty )
85 {
86  mHidePageIfEmpty = hidePageIfEmpty;
87 }
88 
89 void QgsComposerFrame::setHideBackgroundIfEmpty( const bool hideBackgroundIfEmpty )
90 {
91  if ( hideBackgroundIfEmpty == mHideBackgroundIfEmpty )
92  {
93  return;
94  }
95 
96  mHideBackgroundIfEmpty = hideBackgroundIfEmpty;
97  update();
98 }
99 
101 {
102  if ( !mMultiFrame )
103  {
104  return true;
105  }
106 
107  double multiFrameHeight = mMultiFrame->totalSize().height();
108  if ( multiFrameHeight <= mSection.top() )
109  {
110  //multiframe height is less than top of this frame's visible portion
111  return true;
112  }
113 
114  return false;
115 
116 }
117 
119 {
120  if ( !mMultiFrame )
122 
123  //start with multiframe's context
124  QgsExpressionContext* context = mMultiFrame->createExpressionContext();
125  //add frame's individual context
127 
128  return context;
129 }
130 
132 {
133  if ( !id().isEmpty() )
134  {
135  return id();
136  }
137 
138  if ( mMultiFrame )
139  {
140  return mMultiFrame->displayName();
141  }
142 
143  return tr( "<frame>" );
144 }
145 
146 void QgsComposerFrame::setSceneRect( const QRectF &rectangle )
147 {
148  QRectF fixedRect = rectangle;
149 
150  if ( mMultiFrame )
151  {
152  //calculate index of frame
153  int frameIndex = mMultiFrame->frameIndex( this );
154 
155  QSizeF fixedSize = mMultiFrame->fixedFrameSize( frameIndex );
156  if ( fixedSize.width() > 0 )
157  {
158  fixedRect.setWidth( fixedSize.width() );
159  }
160  if ( fixedSize.height() > 0 )
161  {
162  fixedRect.setHeight( fixedSize.height() );
163  }
164 
165  //check minimum size
166  QSizeF minSize = mMultiFrame->minFrameSize( frameIndex );
167  fixedRect.setWidth( qMax( minSize.width(), fixedRect.width() ) );
168  fixedRect.setHeight( qMax( minSize.height(), fixedRect.height() ) );
169  }
170 
171  QgsComposerItem::setSceneRect( fixedRect );
172 }
173 
174 void QgsComposerFrame::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
175 {
176  Q_UNUSED( itemStyle );
177  Q_UNUSED( pWidget );
178 
179  if ( !painter )
180  {
181  return;
182  }
183  if ( !shouldDrawItem() )
184  {
185  return;
186  }
187 
188  bool empty = isEmpty();
189 
190  if ( !empty || !mHideBackgroundIfEmpty )
191  {
192  drawBackground( painter );
193  }
194  if ( mMultiFrame )
195  {
196  //calculate index of frame
197  int frameIndex = mMultiFrame->frameIndex( this );
198  mMultiFrame->render( painter, mSection, frameIndex );
199  }
200 
201  if ( !empty || !mHideBackgroundIfEmpty )
202  {
203  drawFrame( painter );
204  }
205  if ( isSelected() )
206  {
207  drawSelectionBoxes( painter );
208  }
209 }
210 
212 {
213  if ( mComposition )
214  {
216  }
217 }
218 
220 {
221  if ( mComposition )
222  {
224  }
225 }
void paint(QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget) override
qreal x() const
qreal y() const
QDomNode appendChild(const QDomNode &newChild)
qreal x() const
qreal y() const
QString attribute(const QString &name, const QString &defValue) const
void setHideBackgroundIfEmpty(const bool hideBackgroundIfEmpty)
Sets whether the background and frame border should be hidden if this frame is empty.
virtual QString displayName() const override
Get item display name.
virtual QString displayName() const
Get multiframe display name.
A item that forms part of a map composition.
virtual QgsExpressionContext * createExpressionContext() const override
Creates an expression context relating to the objects's current state.
qreal top() const
virtual void drawFrame(QPainter *p)
Draw black frame around item.
virtual QSizeF totalSize() const =0
Returns the total size of the multiframe's content.
double toDouble(bool *ok) const
virtual QgsExpressionContext * createExpressionContext() const override
Creates an expression context relating to the objects's current state.
QString tr(const char *sourceText, const char *disambiguation, int n)
void update(const QRectF &rect)
void setHeight(qreal height)
bool _readXML(const QDomElement &itemElem, const QDomDocument &doc)
Reads parameter that are not subclass specific in document.
virtual QgsExpressionContext * createExpressionContext() const
Creates an expression context relating to the objects's current state.
QPointF pos() const
QString number(int n, int base)
virtual void drawSelectionBoxes(QPainter *p)
Draws additional graphics on selected items.
void setAttribute(const QString &name, const QString &value)
bool isSelected() const
void endItemCommand() override
int toInt(bool *ok, int base) const
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 hideBackgroundIfEmpty() const
Returns whether the background and frame border should be hidden if this frame is empty...
bool shouldDrawItem() const
Returns whether the item should be drawn in the current context.
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 QSizeF minFrameSize(const int frameIndex=-1) const
Returns the minimum size for a frames, if desired.
bool isEmpty() const
Returns whether the frame is empty.
bool isNull() const
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
qreal width() const
bool _writeXML(QDomElement &itemElem, QDomDocument &doc) const
Writes parameter that are not subclass specific in document.
void setWidth(qreal width)
virtual void drawBackground(QPainter *p)
Draw background.
bool writeXML(QDomElement &elem, QDomDocument &doc) const override
Stores item state in DOM element.
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
QDomElement firstChildElement(const QString &tagName) const
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 Q_DECL_DEPRECATED void render(QPainter *p, const QRectF &renderExtent)
Renders a portion of the multiframe's content into a painter.
virtual QSizeF fixedFrameSize(const int frameIndex=-1) const
Returns the fixed size for a frame, if desired.
bool hidePageIfEmpty() const
Returns whether the page should be hidden (ie, not included in composer exports) if this frame is emp...
int frameIndex(QgsComposerFrame *frame) const
Returns the index of a frame within the multiframe.
qreal height() const
bool readXML(const QDomElement &itemElem, const QDomDocument &doc) override
Sets item state from DOM element.
QDomElement createElement(const QString &tagName)
qreal height() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QgsComposerMultiFrame * multiFrame() const
Returns the parent multiframe for the frame.
qreal width() const
QRectF rect() const
void beginMultiFrameCommand(QgsComposerMultiFrame *multiFrame, const QString &text, const QgsComposerMultiFrameMergeCommand::Context c=QgsComposerMultiFrameMergeCommand::Unknown)
QString id() const
Get item's id (which is not necessarly unique)