QGIS API Documentation  2.11.0-Master
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 ( !id().isEmpty() )
121  {
122  return id();
123  }
124 
125  if ( mMultiFrame )
126  {
127  return mMultiFrame->displayName();
128  }
129 
130  return tr( "<frame>" );
131 }
132 
133 void QgsComposerFrame::setSceneRect( const QRectF &rectangle )
134 {
135  QRectF fixedRect = rectangle;
136 
137  if ( mMultiFrame )
138  {
139  //calculate index of frame
140  int frameIndex = mMultiFrame->frameIndex( this );
141 
142  QSizeF fixedSize = mMultiFrame->fixedFrameSize( frameIndex );
143  if ( fixedSize.width() > 0 )
144  {
145  fixedRect.setWidth( fixedSize.width() );
146  }
147  if ( fixedSize.height() > 0 )
148  {
149  fixedRect.setHeight( fixedSize.height() );
150  }
151 
152  //check minimum size
153  QSizeF minSize = mMultiFrame->minFrameSize( frameIndex );
154  fixedRect.setWidth( qMax( minSize.width(), fixedRect.width() ) );
155  fixedRect.setHeight( qMax( minSize.height(), fixedRect.height() ) );
156  }
157 
158  QgsComposerItem::setSceneRect( fixedRect );
159 }
160 
161 void QgsComposerFrame::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
162 {
163  Q_UNUSED( itemStyle );
164  Q_UNUSED( pWidget );
165 
166  if ( !painter )
167  {
168  return;
169  }
170  if ( !shouldDrawItem() )
171  {
172  return;
173  }
174 
175  bool empty = isEmpty();
176 
177  if ( !empty || !mHideBackgroundIfEmpty )
178  {
179  drawBackground( painter );
180  }
181  if ( mMultiFrame )
182  {
183  //calculate index of frame
184  int frameIndex = mMultiFrame->frameIndex( this );
185  mMultiFrame->render( painter, mSection, frameIndex );
186  }
187 
188  if ( !empty || !mHideBackgroundIfEmpty )
189  {
190  drawFrame( painter );
191  }
192  if ( isSelected() )
193  {
194  drawSelectionBoxes( painter );
195  }
196 }
197 
199 {
200  if ( mComposition )
201  {
203  }
204 }
205 
207 {
208  if ( mComposition )
209  {
211  }
212 }
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.
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
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.
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
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.
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.
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)