Skip to content

Commit 025e453

Browse files
nyalldawsonmhugent
authored andcommittedOct 29, 2013
[FEATURE] Make sure composer grids are always drawn on top of composition
1 parent d17ea5d commit 025e453

File tree

2 files changed

+81
-22
lines changed

2 files changed

+81
-22
lines changed
 

‎src/core/composer/qgspaperitem.cpp

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,28 @@
1717

1818
#include "qgspaperitem.h"
1919
#include "qgscomposition.h"
20+
#include "qgslogger.h"
21+
#include <QGraphicsRectItem>
2022
#include <QPainter>
2123

22-
QgsPaperItem::QgsPaperItem( QgsComposition* c ): QgsComposerItem( c, false )
23-
{
24-
initialize();
25-
}
26-
27-
QgsPaperItem::QgsPaperItem( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition ): QgsComposerItem( x, y, width, height, composition, false )
28-
{
29-
initialize();
30-
}
24+
//QgsPaperGrid
3125

32-
QgsPaperItem::QgsPaperItem(): QgsComposerItem( 0, false )
26+
QgsPaperGrid::QgsPaperGrid( double x, double y, double width, double height, QgsComposition* composition ): QGraphicsRectItem( 0, 0, width, height ), mComposition( composition )
3327
{
34-
initialize();
28+
setFlag( QGraphicsItem::ItemIsSelectable, false );
29+
setFlag( QGraphicsItem::ItemIsMovable, false );
30+
setZValue( 1000 );
31+
setPos( x, y );
3532
}
3633

37-
QgsPaperItem::~QgsPaperItem()
34+
QgsPaperGrid::~QgsPaperGrid()
3835
{
39-
4036
}
4137

42-
void QgsPaperItem::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
38+
void QgsPaperGrid::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
4339
{
4440
Q_UNUSED( itemStyle );
4541
Q_UNUSED( pWidget );
46-
if ( !painter )
47-
{
48-
return;
49-
}
50-
51-
drawBackground( painter );
5242

5343
//draw grid
5444
if ( mComposition )
@@ -127,6 +117,44 @@ void QgsPaperItem::paint( QPainter* painter, const QStyleOptionGraphicsItem* ite
127117
}
128118
}
129119

120+
121+
//QgsPaperItem
122+
123+
QgsPaperItem::QgsPaperItem( QgsComposition* c ): QgsComposerItem( c, false ),
124+
mPageGrid( 0 )
125+
{
126+
initialize();
127+
}
128+
129+
QgsPaperItem::QgsPaperItem( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition ): QgsComposerItem( x, y, width, height, composition, false ),
130+
mPageGrid( 0 )
131+
{
132+
initialize();
133+
}
134+
135+
QgsPaperItem::QgsPaperItem(): QgsComposerItem( 0, false ),
136+
mPageGrid( 0 )
137+
{
138+
initialize();
139+
}
140+
141+
QgsPaperItem::~QgsPaperItem()
142+
{
143+
delete mPageGrid;
144+
}
145+
146+
void QgsPaperItem::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
147+
{
148+
Q_UNUSED( itemStyle );
149+
Q_UNUSED( pWidget );
150+
if ( !painter )
151+
{
152+
return;
153+
}
154+
155+
drawBackground( painter );
156+
}
157+
130158
bool QgsPaperItem::writeXML( QDomElement& elem, QDomDocument & doc ) const
131159
{
132160
Q_UNUSED( elem );
@@ -141,9 +169,21 @@ bool QgsPaperItem::readXML( const QDomElement& itemElem, const QDomDocument& doc
141169
return true;
142170
}
143171

172+
void QgsPaperItem::setSceneRect( const QRectF& rectangle )
173+
{
174+
QgsComposerItem::setSceneRect( rectangle );
175+
//update size and position of attached QgsPaperGrid to reflect new page size and position
176+
mPageGrid->setRect( 0, 0, rect().width(), rect().height() );
177+
mPageGrid->setPos( transform().dx(), transform().dy() );
178+
}
179+
144180
void QgsPaperItem::initialize()
145181
{
146182
setFlag( QGraphicsItem::ItemIsSelectable, false );
147183
setFlag( QGraphicsItem::ItemIsMovable, false );
148184
setZValue( 0 );
185+
186+
//create a new QgsPaperGrid for this page, and add it to the composition
187+
mPageGrid = new QgsPaperGrid( transform().dx(), transform().dy(), rect().width(), rect().height(), mComposition );
188+
mComposition->addItem( mPageGrid );
149189
}

‎src/core/composer/qgspaperitem.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,24 @@
1919
#define QGSPAPERITEM_H
2020

2121
#include "qgscomposeritem.h"
22+
#include <QGraphicsRectItem>
2223

23-
/**Item representing the paper. May draw the snapping grid lines if composition is in
24-
preview mode*/
24+
/**Item representing a grid. This is drawn seperately to the underlying paper item since the grid needs to be
25+
* drawn above all other composer items, while the paper item is drawn below all others.*/
26+
class CORE_EXPORT QgsPaperGrid: public QGraphicsRectItem
27+
{
28+
public:
29+
QgsPaperGrid( double x, double y, double width, double height, QgsComposition* composition );
30+
~QgsPaperGrid();
31+
32+
/** \brief Reimplementation of QCanvasItem::paint*/
33+
void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget );
34+
35+
private:
36+
QgsComposition* mComposition;
37+
};
38+
39+
/**Item representing the paper.*/
2540
class CORE_EXPORT QgsPaperItem: public QgsComposerItem
2641
{
2742
public:
@@ -47,10 +62,14 @@ class CORE_EXPORT QgsPaperItem: public QgsComposerItem
4762
*/
4863
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );
4964

65+
virtual void setSceneRect( const QRectF& rectangle );
66+
5067
private:
5168
QgsPaperItem();
5269
/**Set flags and z-value*/
5370
void initialize();
71+
72+
QgsPaperGrid* mPageGrid;
5473
};
5574

5675
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.