Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement drawing of ruler markers
  • Loading branch information
mhugent committed Jan 29, 2013
1 parent 6ef3c2a commit aacff32
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/app/composer/qgscomposer.cpp
Expand Up @@ -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();
}
Expand Down
40 changes: 37 additions & 3 deletions src/gui/qgscomposerruler.cpp
@@ -1,5 +1,6 @@
#include "qgscomposerruler.h"
#include <QPainter>
#include <cmath>

const int RULER_MIN_SIZE = 20;

Expand All @@ -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();
}
3 changes: 3 additions & 0 deletions src/gui/qgscomposerruler.h
Expand Up @@ -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
24 changes: 24 additions & 0 deletions src/gui/qgscomposerview.cpp
Expand Up @@ -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() )
Expand Down Expand Up @@ -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 );
Expand Down
6 changes: 6 additions & 0 deletions src/gui/qgscomposerview.h
Expand Up @@ -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* );
Expand All @@ -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;
Expand Down

0 comments on commit aacff32

Please sign in to comment.