Skip to content
Permalink
Browse files
Fix ruler layout
  • Loading branch information
mhugent committed Jan 30, 2013
1 parent aacff32 commit 0b01c56b44ebbd6c727eed0eeec699ba7f30bdf3
Showing with 53 additions and 23 deletions.
  1. +20 −3 src/app/composer/qgscomposer.cpp
  2. +30 −5 src/gui/qgscomposerruler.cpp
  3. +0 −15 src/gui/qgscomposerview.cpp
  4. +3 −0 src/gui/qgscomposerview.h
@@ -20,6 +20,7 @@

#include "qgisapp.h"
#include "qgsapplication.h"
#include "qgscomposerruler.h"
#include "qgscomposerview.h"
#include "qgscomposition.h"
#include "qgscompositionwidget.h"
@@ -245,8 +246,24 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )
setMouseTracking( true );
mViewFrame->setMouseTracking( true );

//create composer view
mView = new QgsComposerView( mViewFrame );
//create composer view and layout with rulers
mView = new QgsComposerView();
mView->setContentsMargins( 0, 0, 0, 0 );
QGridLayout* viewLayout = new QGridLayout();
viewLayout->setSpacing( 0 );
viewLayout->setMargin( 0 );
QgsComposerRuler* hRuler = new QgsComposerRuler( QgsComposerRuler::Horizontal );
mView->setHorizontalRuler( hRuler );
QgsComposerRuler* vRuler = new QgsComposerRuler( QgsComposerRuler::Vertical );
mView->setVerticalRuler( vRuler );
QWidget* fake = new QWidget();
fake->setBackgroundRole( QPalette::Window );
fake->setFixedSize( 20, 20 );
viewLayout->addWidget( fake, 0, 0 );
viewLayout->addWidget( hRuler, 0, 1 );
viewLayout->addWidget( vRuler, 1, 0 );
viewLayout->addWidget( mView, 1, 1 );
mViewFrame->setLayout( viewLayout );

//init undo/redo buttons
mComposition = new QgsComposition( mQgis->mapCanvas()->mapRenderer() );
@@ -313,7 +330,7 @@ QgsComposer::QgsComposer( QgisApp *qgis, const QString& title )

QGridLayout *l = new QGridLayout( mViewFrame );
l->setMargin( 0 );
l->addWidget( mView, 0, 0 );
l->addLayout( viewLayout, 0, 0 );

// Create size grip (needed by Mac OS X for QMainWindow if QStatusBar is not visible)
mSizeGrip = new QSizeGrip( this );
@@ -1,4 +1,5 @@
#include "qgscomposerruler.h"
#include "qgis.h"
#include <QPainter>
#include <cmath>

@@ -26,17 +27,23 @@ void QgsComposerRuler::paintEvent( QPaintEvent* event )

if ( mDirection == Horizontal )
{
if( doubleNear( width(), 0 ) )
{
return;
}

//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 startX = t.map( QPointF( 0, 0 ) ).x();
double endX = t.map( QPointF( width(), 0 ) ).x();

double markerPos = ( floor( startX / 10.0 ) + 1 ) * 10.0 - RULER_MIN_SIZE; //marker position in mm
double markerPos = ( floor( startX / 10.0 ) + 1 ) * 10.0; //marker position in mm
while ( markerPos <= endX )
{
if ( markerPos >= 0 && markerPos <= 296 ) //todo: need to know paper size
if ( markerPos >= 0 && markerPos <= 297 ) //todo: need to know paper size
{
double pixelCoord = mTransform.map( QPointF( markerPos, 0 ) ).x();
p.drawLine( pixelCoord, 0, pixelCoord, RULER_MIN_SIZE );
p.drawText( QPointF( pixelCoord + 2, RULER_MIN_SIZE / 2.0 ), QString::number( (int)( markerPos ) ) );
}
markerPos += 10.0;
}
@@ -46,7 +53,25 @@ void QgsComposerRuler::paintEvent( QPaintEvent* event )
}
else //vertical
{
//p.fillRect( rect(), QColor( 0, 0, 255 ) );
if( doubleNear( height(), 0 ) )
{
return;
}

double startY = t.map( QPointF( 0, 0 ) ).y();
double endY = t.map( QPointF( 0, height() ) ).y();

double markerPos = ( floor( startY / 10.0 ) + 1 ) + 10.0; //marker position in mm
while( markerPos <= endY )
{
if( markerPos >= 0 && markerPos <= 210 )
{
double pixelCoord = mTransform.map( QPointF( 0, markerPos ) ).y();
p.drawLine( 0, pixelCoord, RULER_MIN_SIZE, pixelCoord );
p.drawText( QPointF( RULER_MIN_SIZE / 2.0, pixelCoord + 2.0 ), QString::number( markerPos ) );
}
markerPos += 10.0;
}
}
}

@@ -50,21 +50,6 @@ QgsComposerView::QgsComposerView( QWidget* parent, const char* name, Qt::WFlags
Q_UNUSED( f );
Q_UNUSED( name );


QGridLayout* gridLayout = new QGridLayout();
gridLayout->setSpacing( 0 );
gridLayout->setMargin( 0 );
mHorizontalRuler = new QgsComposerRuler( QgsComposerRuler::Horizontal );
mVerticalRuler = new QgsComposerRuler( QgsComposerRuler::Vertical );
QWidget* fake = new QWidget();
fake->setBackgroundRole( QPalette::Window );
fake->setFixedSize( 20, 20 );
gridLayout->addWidget( fake, 0, 0 );
gridLayout->addWidget( mHorizontalRuler, 0, 1 );
gridLayout->addWidget( mVerticalRuler, 1, 0 );
gridLayout->addWidget( this->viewport(), 1, 1 );
this->setLayout( gridLayout );

setResizeAnchor( QGraphicsView::AnchorViewCenter );
setMouseTracking( true );
viewport()->setMouseTracking( true );
@@ -93,6 +93,9 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
/**Update rulers with current scene rect*/
void updateRulers();

void setHorizontalRuler( QgsComposerRuler* r ){ mHorizontalRuler = r; }
void setVerticalRuler( QgsComposerRuler* r ){ mVerticalRuler = r; }

protected:
void mousePressEvent( QMouseEvent* );
void mouseReleaseEvent( QMouseEvent* );

0 comments on commit 0b01c56

Please sign in to comment.