Skip to content
Permalink
Browse files
Implement drawing of ruler markers
  • Loading branch information
mhugent committed Jan 29, 2013
1 parent 6ef3c2a commit aacff32c9681bf4187aac48fcc79a9422403a385
Showing with 73 additions and 3 deletions.
  1. +3 −0 src/app/composer/qgscomposer.cpp
  2. +37 −3 src/gui/qgscomposerruler.cpp
  3. +3 −0 src/gui/qgscomposerruler.h
  4. +24 −0 src/gui/qgscomposerview.cpp
  5. +6 −0 src/gui/qgscomposerview.h
@@ -526,20 +526,23 @@ void QgsComposer::zoomFull( void )
void QgsComposer::on_mActionZoomAll_triggered()
{
zoomFull();
mView->updateRulers();
mView->update();
emit zoomLevelChanged();
}

void QgsComposer::on_mActionZoomIn_triggered()
{
mView->scale( 2, 2 );
mView->updateRulers();
mView->update();
emit zoomLevelChanged();
}

void QgsComposer::on_mActionZoomOut_triggered()
{
mView->scale( .5, .5 );
mView->updateRulers();
mView->update();
emit zoomLevelChanged();
}
@@ -1,5 +1,6 @@
#include "qgscomposerruler.h"
#include <QPainter>
#include <cmath>

const int RULER_MIN_SIZE = 20;

@@ -19,8 +20,41 @@ QSize QgsComposerRuler::minimumSizeHint() const
void QgsComposerRuler::paintEvent( QPaintEvent* event )
{
Q_UNUSED( event );

//draw blue rectangle for a test
QPainter p( this );
p.fillRect( rect(), QColor( 0, 0, 255 ) );

QTransform t = mTransform.inverted();

if ( mDirection == Horizontal )
{
//start x-coordinate
double startX = t.map( QPointF( 0, 0 ) ).x();//-mTransform.dx() / mTransform.m11();
double endX = t.map( QPointF( width(), 0 ) ).x();//( -mTransform.dx() + width() ) / mTransform.m11();

double markerPos = ( floor( startX / 10.0 ) + 1 ) * 10.0 - RULER_MIN_SIZE; //marker position in mm
while ( markerPos <= endX )
{
if ( markerPos >= 0 && markerPos <= 296 ) //todo: need to know paper size
{
double pixelCoord = mTransform.map( QPointF( markerPos, 0 ) ).x();
p.drawLine( pixelCoord, 0, pixelCoord, RULER_MIN_SIZE );
}
markerPos += 10.0;
}

qWarning( QString::number( startX ).toLocal8Bit().data() );
qWarning( QString::number( endX ).toLocal8Bit().data() );
}
else //vertical
{
//p.fillRect( rect(), QColor( 0, 0, 255 ) );
}
}

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();
}
@@ -18,11 +18,14 @@ class QgsComposerRuler: public QWidget

QSize minimumSizeHint() const;

void setSceneTransform( const QTransform& transform );

protected:
void paintEvent( QPaintEvent* event );

private:
Direction mDirection;
QTransform mTransform;
};

#endif // QGSCOMPOSERRULER_H
@@ -256,6 +256,18 @@ void QgsComposerView::addShape( Tool currentTool )
}
}

void QgsComposerView::updateRulers()
{
if ( mHorizontalRuler )
{
mHorizontalRuler->setSceneTransform( viewportTransform() );
}
if ( mVerticalRuler )
{
mVerticalRuler->setSceneTransform( viewportTransform() );
}
}

void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
{
if ( !composition() )
@@ -627,6 +639,18 @@ void QgsComposerView::showEvent( QShowEvent* e )
e->ignore();
}

void QgsComposerView::resizeEvent( QResizeEvent* event )
{
QGraphicsView::resizeEvent( event );
updateRulers();
}

void QgsComposerView::scrollContentsBy( int dx, int dy )
{
QGraphicsView::scrollContentsBy( dx, dy );
updateRulers();
}

void QgsComposerView::setComposition( QgsComposition* c )
{
setScene( c );
@@ -90,6 +90,9 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
void setPaintingEnabled( bool enabled ) { mPaintingEnabled = enabled; }
bool paintingEnabled() const { return mPaintingEnabled; }

/**Update rulers with current scene rect*/
void updateRulers();

protected:
void mousePressEvent( QMouseEvent* );
void mouseReleaseEvent( QMouseEvent* );
@@ -106,6 +109,9 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
void hideEvent( QHideEvent* e );
void showEvent( QShowEvent* e );

void resizeEvent( QResizeEvent* event );
void scrollContentsBy( int dx, int dy );

private:
/**Status of shift key (used for multiple selection)*/
bool mShiftKeyPressed;

0 comments on commit aacff32

Please sign in to comment.