QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgspaperitem.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspaperitem.cpp
3  -------------------
4  begin : September 2008
5  copyright : (C) 2008 by Marco Hugentobler
6  email : marco dot hugentobler at karto dot baug dot ethz dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgspaperitem.h"
19 #include "qgscomposition.h"
20 #include "qgsstylev2.h"
21 #include "qgslogger.h"
22 #include <QGraphicsRectItem>
23 #include <QPainter>
24 
25 //QgsPaperGrid
26 
27 QgsPaperGrid::QgsPaperGrid( double x, double y, double width, double height, QgsComposition* composition ): QGraphicsRectItem( 0, 0, width, height ), mComposition( composition )
28 {
29  setFlag( QGraphicsItem::ItemIsSelectable, false );
30  setFlag( QGraphicsItem::ItemIsMovable, false );
31  setZValue( 1000 );
32  setPos( x, y );
33 }
34 
36 {
37 }
38 
39 void QgsPaperGrid::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
40 {
41  Q_UNUSED( itemStyle );
42  Q_UNUSED( pWidget );
43 
44  //draw grid
45  if ( mComposition )
46  {
49  {
50  int gridMultiplyX = ( int )( mComposition->snapGridOffsetX() / mComposition->snapGridResolution() );
51  int gridMultiplyY = ( int )( mComposition->snapGridOffsetY() / mComposition->snapGridResolution() );
52  double currentXCoord = mComposition->snapGridOffsetX() - gridMultiplyX * mComposition->snapGridResolution();
53  double currentYCoord;
54  double minYCoord = mComposition->snapGridOffsetY() - gridMultiplyY * mComposition->snapGridResolution();
55 
56  painter->save();
57  //turn of antialiasing so grid is nice and sharp
58  painter->setRenderHint( QPainter::Antialiasing, false );
59 
61  {
62  painter->setPen( mComposition->gridPen() );
63 
64  //draw vertical lines
65  for ( ; currentXCoord <= rect().width(); currentXCoord += mComposition->snapGridResolution() )
66  {
67  painter->drawLine( QPointF( currentXCoord, 0 ), QPointF( currentXCoord, rect().height() ) );
68  }
69 
70  //draw horizontal lines
71  currentYCoord = minYCoord;
72  for ( ; currentYCoord <= rect().height(); currentYCoord += mComposition->snapGridResolution() )
73  {
74  painter->drawLine( QPointF( 0, currentYCoord ), QPointF( rect().width(), currentYCoord ) );
75  }
76  }
77  else //'Dots' or 'Crosses'
78  {
79  QPen gridPen = mComposition->gridPen();
80  painter->setPen( gridPen );
81  painter->setBrush( QBrush( gridPen.color() ) );
82  double halfCrossLength = 1;
84  {
85  //dots are actually drawn as tiny crosses a few pixels across
86  //check QGraphicsView to get current transform
87  if ( scene() )
88  {
89  QList<QGraphicsView*> viewList = scene()->views();
90  if ( viewList.size() > 0 )
91  {
92  QGraphicsView* currentView = viewList.at( 0 );
93  if ( currentView->isVisible() )
94  {
95  //set halfCrossLength to equivalent of 1 pixel
96  halfCrossLength = 1 / currentView->transform().m11();
97  }
98  }
99  }
100  }
102  {
103  halfCrossLength = mComposition->snapGridResolution() / 6;
104  }
105 
106  for ( ; currentXCoord <= rect().width(); currentXCoord += mComposition->snapGridResolution() )
107  {
108  currentYCoord = minYCoord;
109  for ( ; currentYCoord <= rect().height(); currentYCoord += mComposition->snapGridResolution() )
110  {
111  painter->drawLine( QPointF( currentXCoord - halfCrossLength, currentYCoord ), QPointF( currentXCoord + halfCrossLength, currentYCoord ) );
112  painter->drawLine( QPointF( currentXCoord, currentYCoord - halfCrossLength ), QPointF( currentXCoord, currentYCoord + halfCrossLength ) );
113  }
114  }
115  }
116  painter->restore();
117  }
118  }
119 }
120 
121 
122 //QgsPaperItem
123 
125  mPageGrid( 0 )
126 {
127  initialize();
128 }
129 
130 QgsPaperItem::QgsPaperItem( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition ): QgsComposerItem( x, y, width, height, composition, false ),
131  mPageGrid( 0 ), mPageMargin( 0 )
132 {
133  initialize();
134 }
135 
137  mPageGrid( 0 ), mPageMargin( 0 )
138 {
139  initialize();
140 }
141 
143 {
144  delete mPageGrid;
145 }
146 
147 void QgsPaperItem::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
148 {
149  Q_UNUSED( itemStyle );
150  Q_UNUSED( pWidget );
151  if ( !painter )
152  {
153  return;
154  }
155 
156  QgsRenderContext context;
157  context.setPainter( painter );
158  context.setScaleFactor( 1.0 );
160  {
162  }
163  else
164  {
166  }
167 
168  painter->save();
169 
171  {
172  //if in preview mode, draw page border and shadow so that it's
173  //still possible to tell where pages with a transparent style begin and end
174  painter->setRenderHint( QPainter::Antialiasing, false );
175 
176  //shadow
177  painter->setBrush( QBrush( QColor( 150, 150, 150 ) ) );
178  painter->setPen( Qt::NoPen );
179  painter->drawRect( QRectF( 1, 1, rect().width() + 1, rect().height() + 1 ) );
180 
181  //page area
182  painter->setBrush( QColor( 215, 215, 215 ) );
183  painter->setPen( QPen( QColor( 100, 100, 100 ) ) );
184  painter->drawRect( QRectF( 0, 0, rect().width(), rect().height() ) );
185  }
186 
187  painter->setRenderHint( QPainter::Antialiasing );
189 
191  QPolygonF pagePolygon = QPolygonF( QRectF( mPageMargin, mPageMargin, rect().width() - 2 * mPageMargin, rect().height() - 2 * mPageMargin ) );
192  QList<QPolygonF> rings; //empty list
193 
194  //need to render using atlas feature properties?
196  {
197  //using an atlas, so render using current atlas feature
198  //since there may be data defined symbols using atlas feature properties
199  mComposition->pageStyleSymbol()->renderPolygon( pagePolygon, &rings, mComposition->atlasComposition().currentFeature(), context );
200  }
201  else
202  {
203  mComposition->pageStyleSymbol()->renderPolygon( pagePolygon, &rings, 0, context );
204  }
205 
206  mComposition->pageStyleSymbol()->stopRender( context );
207  painter->restore();
208 }
209 
211 {
212  //get max bleed from symbol
214 
215  //Now subtract 1 pixel to prevent semi-transparent borders at edge of solid page caused by
216  //anti-aliased painting. This may cause a pixel to be cropped from certain edge lines/symbols,
217  //but that can be counteracted by adding a dummy transparent line symbol layer with a wider line width
218  mPageMargin = maxBleed - ( 25.4 / mComposition->printResolution() );
219 }
220 
221 bool QgsPaperItem::writeXML( QDomElement& elem, QDomDocument & doc ) const
222 {
223  Q_UNUSED( elem );
224  Q_UNUSED( doc );
225  return true;
226 }
227 
228 bool QgsPaperItem::readXML( const QDomElement& itemElem, const QDomDocument& doc )
229 {
230  Q_UNUSED( itemElem );
231  Q_UNUSED( doc );
232  return true;
233 }
234 
235 void QgsPaperItem::setSceneRect( const QRectF& rectangle )
236 {
237  QgsComposerItem::setSceneRect( rectangle );
238  //update size and position of attached QgsPaperGrid to reflect new page size and position
239  mPageGrid->setRect( 0, 0, rect().width(), rect().height() );
240  mPageGrid->setPos( pos().x(), pos().y() );
241 }
242 
244 {
245  setFlag( QGraphicsItem::ItemIsSelectable, false );
246  setFlag( QGraphicsItem::ItemIsMovable, false );
247  setZValue( 0 );
248 
249  //even though we aren't going to use it to draw the page, set the pen width as 4
250  //so that the page border and shadow is fully rendered within its scene rect
251  //(QGraphicsRectItem considers the pen width when calculating an item's scene rect)
252  setPen( QPen( QBrush( Qt::NoBrush ), 4 ) );
253 
254  //create a new QgsPaperGrid for this page, and add it to the composition
255  mPageGrid = new QgsPaperGrid( pos().x(), pos().y(), rect().width(), rect().height(), mComposition );
256  mComposition->addItem( mPageGrid );
257 }