Skip to content

Commit

Permalink
[FEATURE] Make sure composer grids are always drawn on top of composi…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
nyalldawson authored and mhugent committed Oct 29, 2013
1 parent d17ea5d commit 025e453
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 22 deletions.
80 changes: 60 additions & 20 deletions src/core/composer/qgspaperitem.cpp
Expand Up @@ -17,38 +17,28 @@

#include "qgspaperitem.h"
#include "qgscomposition.h"
#include "qgslogger.h"
#include <QGraphicsRectItem>
#include <QPainter>

QgsPaperItem::QgsPaperItem( QgsComposition* c ): QgsComposerItem( c, false )
{
initialize();
}

QgsPaperItem::QgsPaperItem( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition ): QgsComposerItem( x, y, width, height, composition, false )
{
initialize();
}
//QgsPaperGrid

QgsPaperItem::QgsPaperItem(): QgsComposerItem( 0, false )
QgsPaperGrid::QgsPaperGrid( double x, double y, double width, double height, QgsComposition* composition ): QGraphicsRectItem( 0, 0, width, height ), mComposition( composition )
{
initialize();
setFlag( QGraphicsItem::ItemIsSelectable, false );
setFlag( QGraphicsItem::ItemIsMovable, false );
setZValue( 1000 );
setPos( x, y );
}

QgsPaperItem::~QgsPaperItem()
QgsPaperGrid::~QgsPaperGrid()
{

}

void QgsPaperItem::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
void QgsPaperGrid::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
{
Q_UNUSED( itemStyle );
Q_UNUSED( pWidget );
if ( !painter )
{
return;
}

drawBackground( painter );

//draw grid
if ( mComposition )
Expand Down Expand Up @@ -127,6 +117,44 @@ void QgsPaperItem::paint( QPainter* painter, const QStyleOptionGraphicsItem* ite
}
}


//QgsPaperItem

QgsPaperItem::QgsPaperItem( QgsComposition* c ): QgsComposerItem( c, false ),
mPageGrid( 0 )
{
initialize();
}

QgsPaperItem::QgsPaperItem( qreal x, qreal y, qreal width, qreal height, QgsComposition* composition ): QgsComposerItem( x, y, width, height, composition, false ),
mPageGrid( 0 )
{
initialize();
}

QgsPaperItem::QgsPaperItem(): QgsComposerItem( 0, false ),
mPageGrid( 0 )
{
initialize();
}

QgsPaperItem::~QgsPaperItem()
{
delete mPageGrid;
}

void QgsPaperItem::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
{
Q_UNUSED( itemStyle );
Q_UNUSED( pWidget );
if ( !painter )
{
return;
}

drawBackground( painter );
}

bool QgsPaperItem::writeXML( QDomElement& elem, QDomDocument & doc ) const
{
Q_UNUSED( elem );
Expand All @@ -141,9 +169,21 @@ bool QgsPaperItem::readXML( const QDomElement& itemElem, const QDomDocument& doc
return true;
}

void QgsPaperItem::setSceneRect( const QRectF& rectangle )
{
QgsComposerItem::setSceneRect( rectangle );
//update size and position of attached QgsPaperGrid to reflect new page size and position
mPageGrid->setRect( 0, 0, rect().width(), rect().height() );
mPageGrid->setPos( transform().dx(), transform().dy() );
}

void QgsPaperItem::initialize()
{
setFlag( QGraphicsItem::ItemIsSelectable, false );
setFlag( QGraphicsItem::ItemIsMovable, false );
setZValue( 0 );

//create a new QgsPaperGrid for this page, and add it to the composition
mPageGrid = new QgsPaperGrid( transform().dx(), transform().dy(), rect().width(), rect().height(), mComposition );
mComposition->addItem( mPageGrid );
}
23 changes: 21 additions & 2 deletions src/core/composer/qgspaperitem.h
Expand Up @@ -19,9 +19,24 @@
#define QGSPAPERITEM_H

#include "qgscomposeritem.h"
#include <QGraphicsRectItem>

/**Item representing the paper. May draw the snapping grid lines if composition is in
preview mode*/
/**Item representing a grid. This is drawn seperately to the underlying paper item since the grid needs to be
* drawn above all other composer items, while the paper item is drawn below all others.*/
class CORE_EXPORT QgsPaperGrid: public QGraphicsRectItem
{
public:
QgsPaperGrid( double x, double y, double width, double height, QgsComposition* composition );
~QgsPaperGrid();

/** \brief Reimplementation of QCanvasItem::paint*/
void paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget );

private:
QgsComposition* mComposition;
};

/**Item representing the paper.*/
class CORE_EXPORT QgsPaperItem: public QgsComposerItem
{
public:
Expand All @@ -47,10 +62,14 @@ class CORE_EXPORT QgsPaperItem: public QgsComposerItem
*/
bool readXML( const QDomElement& itemElem, const QDomDocument& doc );

virtual void setSceneRect( const QRectF& rectangle );

private:
QgsPaperItem();
/**Set flags and z-value*/
void initialize();

QgsPaperGrid* mPageGrid;
};

#endif

0 comments on commit 025e453

Please sign in to comment.