Skip to content

Commit

Permalink
Store reference to composition in ruler to get page info
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Feb 17, 2013
1 parent 5f8e9e1 commit 4662802
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/gui/qgscomposerruler.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include "qgscomposerruler.h"
#include "qgscomposition.h"
#include "qgis.h"
#include <QPainter>
#include <cmath>

const int RULER_MIN_SIZE = 20;

QgsComposerRuler::QgsComposerRuler( QgsComposerRuler::Direction d ): QWidget( 0 ), mDirection( d )
QgsComposerRuler::QgsComposerRuler( QgsComposerRuler::Direction d ): QWidget( 0 ), mDirection( d ), mComposition( 0 )
{
}

Expand All @@ -21,6 +22,11 @@ QSize QgsComposerRuler::minimumSizeHint() const
void QgsComposerRuler::paintEvent( QPaintEvent* event )
{
Q_UNUSED( event );
if ( !mComposition )
{
return;
}

QPainter p( this );

QTransform t = mTransform.inverted();
Expand All @@ -39,7 +45,7 @@ void QgsComposerRuler::paintEvent( QPaintEvent* event )
double markerPos = ( floor( startX / 10.0 ) + 1 ) * 10.0; //marker position in mm
while ( markerPos <= endX )
{
if ( markerPos >= 0 && markerPos <= 297 ) //todo: need to know paper size
if ( markerPos >= 0 && markerPos <= mComposition->paperWidth() ) //todo: need to know paper size
{
double pixelCoord = mTransform.map( QPointF( markerPos, 0 ) ).x();
p.drawLine( pixelCoord, 0, pixelCoord, RULER_MIN_SIZE );
Expand All @@ -64,7 +70,7 @@ void QgsComposerRuler::paintEvent( QPaintEvent* event )
double markerPos = ( floor( startY / 10.0 ) + 1 ) * 10.0; //marker position in mm
while ( markerPos <= endY )
{
if ( markerPos >= 0 && markerPos <= 210 )
if ( markerPos >= 0 && markerPos <= mComposition->paperHeight() )
{
double pixelCoord = mTransform.map( QPointF( 0, markerPos ) ).y();
p.drawLine( 0, pixelCoord, RULER_MIN_SIZE, pixelCoord );
Expand All @@ -82,7 +88,6 @@ void QgsComposerRuler::setSceneTransform( const QTransform& transform )
{
QString debug = QString::number( transform.dx() ) + "," + QString::number( transform.dy() ) + ","
+ QString::number( transform.m11() ) + "," + QString::number( transform.m22() );
qWarning( debug.toLocal8Bit().data() );
mTransform = transform;
update();
}
5 changes: 5 additions & 0 deletions src/gui/qgscomposerruler.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define QGSCOMPOSERRULER_H

#include <QWidget>
class QgsComposition;

/**A class to show paper scale and the current cursor position*/
class QgsComposerRuler: public QWidget
Expand All @@ -21,13 +22,17 @@ class QgsComposerRuler: public QWidget
void setSceneTransform( const QTransform& transform );
void updateMarker( const QPointF& pos ) { mMarkerPos = pos; repaint(); }

void setComposition( const QgsComposition* c ) { mComposition = c; }
const QgsComposition* composition() const { return mComposition; }

protected:
void paintEvent( QPaintEvent* event );

private:
Direction mDirection;
QTransform mTransform;
QPointF mMarkerPos;
const QgsComposition* mComposition; //reference to composition for paper size, nPages
};

#endif // QGSCOMPOSERRULER_H
8 changes: 8 additions & 0 deletions src/gui/qgscomposerview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,14 @@ void QgsComposerView::scrollContentsBy( int dx, int dy )
void QgsComposerView::setComposition( QgsComposition* c )
{
setScene( c );
if ( mHorizontalRuler )
{
mHorizontalRuler->setComposition( c );
}
if ( mVerticalRuler )
{
mVerticalRuler->setComposition( c );
}
}

QgsComposition* QgsComposerView::composition()
Expand Down

0 comments on commit 4662802

Please sign in to comment.