Skip to content

Commit aacff32

Browse files
committed
Implement drawing of ruler markers
1 parent 6ef3c2a commit aacff32

File tree

5 files changed

+73
-3
lines changed

5 files changed

+73
-3
lines changed

src/app/composer/qgscomposer.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -526,20 +526,23 @@ void QgsComposer::zoomFull( void )
526526
void QgsComposer::on_mActionZoomAll_triggered()
527527
{
528528
zoomFull();
529+
mView->updateRulers();
529530
mView->update();
530531
emit zoomLevelChanged();
531532
}
532533

533534
void QgsComposer::on_mActionZoomIn_triggered()
534535
{
535536
mView->scale( 2, 2 );
537+
mView->updateRulers();
536538
mView->update();
537539
emit zoomLevelChanged();
538540
}
539541

540542
void QgsComposer::on_mActionZoomOut_triggered()
541543
{
542544
mView->scale( .5, .5 );
545+
mView->updateRulers();
543546
mView->update();
544547
emit zoomLevelChanged();
545548
}

src/gui/qgscomposerruler.cpp

+37-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "qgscomposerruler.h"
22
#include <QPainter>
3+
#include <cmath>
34

45
const int RULER_MIN_SIZE = 20;
56

@@ -19,8 +20,41 @@ QSize QgsComposerRuler::minimumSizeHint() const
1920
void QgsComposerRuler::paintEvent( QPaintEvent* event )
2021
{
2122
Q_UNUSED( event );
22-
23-
//draw blue rectangle for a test
2423
QPainter p( this );
25-
p.fillRect( rect(), QColor( 0, 0, 255 ) );
24+
25+
QTransform t = mTransform.inverted();
26+
27+
if ( mDirection == Horizontal )
28+
{
29+
//start x-coordinate
30+
double startX = t.map( QPointF( 0, 0 ) ).x();//-mTransform.dx() / mTransform.m11();
31+
double endX = t.map( QPointF( width(), 0 ) ).x();//( -mTransform.dx() + width() ) / mTransform.m11();
32+
33+
double markerPos = ( floor( startX / 10.0 ) + 1 ) * 10.0 - RULER_MIN_SIZE; //marker position in mm
34+
while ( markerPos <= endX )
35+
{
36+
if ( markerPos >= 0 && markerPos <= 296 ) //todo: need to know paper size
37+
{
38+
double pixelCoord = mTransform.map( QPointF( markerPos, 0 ) ).x();
39+
p.drawLine( pixelCoord, 0, pixelCoord, RULER_MIN_SIZE );
40+
}
41+
markerPos += 10.0;
42+
}
43+
44+
qWarning( QString::number( startX ).toLocal8Bit().data() );
45+
qWarning( QString::number( endX ).toLocal8Bit().data() );
46+
}
47+
else //vertical
48+
{
49+
//p.fillRect( rect(), QColor( 0, 0, 255 ) );
50+
}
51+
}
52+
53+
void QgsComposerRuler::setSceneTransform( const QTransform& transform )
54+
{
55+
QString debug = QString::number( transform.dx() ) + "," + QString::number( transform.dy() ) + ","
56+
+ QString::number( transform.m11() ) + "," + QString::number( transform.m22() );
57+
qWarning( debug.toLocal8Bit().data() );
58+
mTransform = transform;
59+
update();
2660
}

src/gui/qgscomposerruler.h

+3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ class QgsComposerRuler: public QWidget
1818

1919
QSize minimumSizeHint() const;
2020

21+
void setSceneTransform( const QTransform& transform );
22+
2123
protected:
2224
void paintEvent( QPaintEvent* event );
2325

2426
private:
2527
Direction mDirection;
28+
QTransform mTransform;
2629
};
2730

2831
#endif // QGSCOMPOSERRULER_H

src/gui/qgscomposerview.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,18 @@ void QgsComposerView::addShape( Tool currentTool )
256256
}
257257
}
258258

259+
void QgsComposerView::updateRulers()
260+
{
261+
if ( mHorizontalRuler )
262+
{
263+
mHorizontalRuler->setSceneTransform( viewportTransform() );
264+
}
265+
if ( mVerticalRuler )
266+
{
267+
mVerticalRuler->setSceneTransform( viewportTransform() );
268+
}
269+
}
270+
259271
void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
260272
{
261273
if ( !composition() )
@@ -627,6 +639,18 @@ void QgsComposerView::showEvent( QShowEvent* e )
627639
e->ignore();
628640
}
629641

642+
void QgsComposerView::resizeEvent( QResizeEvent* event )
643+
{
644+
QGraphicsView::resizeEvent( event );
645+
updateRulers();
646+
}
647+
648+
void QgsComposerView::scrollContentsBy( int dx, int dy )
649+
{
650+
QGraphicsView::scrollContentsBy( dx, dy );
651+
updateRulers();
652+
}
653+
630654
void QgsComposerView::setComposition( QgsComposition* c )
631655
{
632656
setScene( c );

src/gui/qgscomposerview.h

+6
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
9090
void setPaintingEnabled( bool enabled ) { mPaintingEnabled = enabled; }
9191
bool paintingEnabled() const { return mPaintingEnabled; }
9292

93+
/**Update rulers with current scene rect*/
94+
void updateRulers();
95+
9396
protected:
9497
void mousePressEvent( QMouseEvent* );
9598
void mouseReleaseEvent( QMouseEvent* );
@@ -106,6 +109,9 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
106109
void hideEvent( QHideEvent* e );
107110
void showEvent( QShowEvent* e );
108111

112+
void resizeEvent( QResizeEvent* event );
113+
void scrollContentsBy( int dx, int dy );
114+
109115
private:
110116
/**Status of shift key (used for multiple selection)*/
111117
bool mShiftKeyPressed;

0 commit comments

Comments
 (0)