Skip to content
Permalink
Browse files
Show snap lines in ruler mouse move event
  • Loading branch information
mhugent committed Feb 18, 2013
1 parent 31a9021 commit cb1f5c3e59f62024d18e63f0b9fd5d3bb9201a23
Showing with 69 additions and 27 deletions.
  1. +1 −0 src/app/composer/qgscomposer.cpp
  2. +57 −24 src/gui/qgscomposerruler.cpp
  3. +11 −3 src/gui/qgscomposerruler.h
@@ -254,6 +254,7 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
mHorizontalRuler = new QgsComposerRuler( QgsComposerRuler::Horizontal );
mVerticalRuler = new QgsComposerRuler( QgsComposerRuler::Vertical );
QWidget* fake = new QWidget();
fake->setAttribute( Qt::WA_NoMousePropagation );
fake->setBackgroundRole( QPalette::Window );
fake->setFixedSize( 20, 20 );
mViewLayout->addWidget( fake, 0, 0 );
@@ -1,13 +1,16 @@
#include "qgscomposerruler.h"
#include "qgscomposition.h"
#include "qgis.h"
#include <QDragEnterEvent>
#include <QGraphicsLineItem>
#include <QPainter>
#include <cmath>

const int RULER_MIN_SIZE = 20;

QgsComposerRuler::QgsComposerRuler( QgsComposerRuler::Direction d ): QWidget( 0 ), mDirection( d ), mComposition( 0 )
QgsComposerRuler::QgsComposerRuler( QgsComposerRuler::Direction d ): QWidget( 0 ), mDirection( d ), mComposition( 0 ), mLineSnapItem( 0 )
{
setMouseTracking( true );
}

QgsComposerRuler::~QgsComposerRuler()
@@ -111,29 +114,6 @@ void QgsComposerRuler::paintEvent( QPaintEvent* event )
}
}

#if 0
double markerPos = ( floor( startY / mmDisplay ) + 1 ) * mmDisplay; //marker position in mm
while ( markerPos <= endY )
{
int page = ( int )( markerPos / ( mComposition->paperHeight() + mComposition->spaceBetweenPages() ) );
double pageCoord = markerPos - page * ( mComposition->paperHeight() + mComposition->spaceBetweenPages() );
if ( page >= mComposition->numPages() )
{
break;
}

if ( pageCoord < 0 || pageCoord > mComposition->paperHeight() ) //marker is in a page gap
{
markerPos += mmDisplay;
continue;
}
double pixelCoord = mTransform.map( QPointF( 0, markerPos ) ).y();
p.drawLine( 0, pixelCoord, RULER_MIN_SIZE, pixelCoord );
p.drawText( QPointF( 0, pixelCoord - 2.0 ), QString::number( pageCoord ) );
markerPos += mmDisplay;
}
#endif //0

p.setPen( QColor( Qt::red ) );
p.drawLine( 0, mMarkerPos.y(), RULER_MIN_SIZE, mMarkerPos.y() );
}
@@ -146,3 +126,56 @@ void QgsComposerRuler::setSceneTransform( const QTransform& transform )
mTransform = transform;
update();
}

void QgsComposerRuler::mouseMoveEvent( QMouseEvent* event )
{
qWarning( "QgsComposerRuler::mouseMoveEvent" );
updateMarker( event->posF() );
setSnapLinePosition( event->posF() );
}

void QgsComposerRuler::mouseReleaseEvent( QMouseEvent* event )
{
Q_UNUSED( event );
delete mLineSnapItem;
mLineSnapItem = 0;
}

void QgsComposerRuler::mousePressEvent( QMouseEvent* event )
{
delete mLineSnapItem;
mLineSnapItem = 0;
mLineSnapItem = createLineSnapItem();
mComposition->addItem( mLineSnapItem );
setSnapLinePosition( event->posF() );
mLineSnapItem->show();
}

void QgsComposerRuler::setSnapLinePosition( const QPointF& pos )
{
if ( !mLineSnapItem || !mComposition )
{
return;
}

QPointF transformedPt = mTransform.inverted().map( pos );
if ( mDirection == Horizontal )
{
mLineSnapItem->setLine( QLineF( transformedPt.x(), 0, transformedPt.x(), mComposition->height() ) );
}
else //vertical
{
mLineSnapItem->setLine( QLineF( 0, transformedPt.y(), mComposition->width(), transformedPt.y() ) );
}
}

QGraphicsLineItem* QgsComposerRuler::createLineSnapItem()
{
QGraphicsLineItem* item = new QGraphicsLineItem();
QPen linePen( Qt::SolidLine );
linePen.setColor( Qt::red );
linePen.setWidthF( 0.5 );
item->setPen( linePen );
item->setZValue( 100 );
return item;
}
@@ -3,6 +3,7 @@

#include <QWidget>
class QgsComposition;
class QGraphicsLineItem;

/**A class to show paper scale and the current cursor position*/
class QgsComposerRuler: public QWidget
@@ -22,17 +23,24 @@ 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; }
void setComposition( QgsComposition* c ) { mComposition = c; }
QgsComposition* composition() { return mComposition; }

protected:
void paintEvent( QPaintEvent* event );
void mouseMoveEvent( QMouseEvent* event );
void mouseReleaseEvent( QMouseEvent* event );
void mousePressEvent( QMouseEvent* event );

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

void setSnapLinePosition( const QPointF& pos );
static QGraphicsLineItem* createLineSnapItem();
};

#endif // QGSCOMPOSERRULER_H

0 comments on commit cb1f5c3

Please sign in to comment.