Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix drawing of rulers with multipage layouts
  • Loading branch information
nyalldawson committed Jul 25, 2017
1 parent 4cf9827 commit 416e1e4
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 32 deletions.
7 changes: 7 additions & 0 deletions python/core/layout/qgslayoutpagecollection.sip
Expand Up @@ -137,6 +137,13 @@ Size of page shadow, in layout coordinates
:rtype: float
%End

signals:

void changed();
%Docstring
Emitted when pages are added or removed from the collection.
%End

};

/************************************************************************
Expand Down
2 changes: 2 additions & 0 deletions python/gui/layout/qgslayoutview.sip
Expand Up @@ -186,6 +186,8 @@ class QgsLayoutView: QGraphicsView

virtual void resizeEvent( QResizeEvent *event );

virtual void scrollContentsBy( int dx, int dy );


};

Expand Down
7 changes: 1 addition & 6 deletions src/app/layout/qgslayoutdesignerdialog.cpp
Expand Up @@ -257,15 +257,10 @@ void QgsLayoutDesignerDialog::open()
{
show();
activate();
mView->zoomFull(); // zoomFull() does not work properly until we have called show()

#if 0 // TODO

if ( mView )
{
mView->updateRulers();
mView->zoomFull(); // zoomFull() does not work properly until we have called show()
}
#endif
}

void QgsLayoutDesignerDialog::activate()
Expand Down
1 change: 1 addition & 0 deletions src/core/layout/qgslayoutpagecollection.cpp
Expand Up @@ -52,6 +52,7 @@ void QgsLayoutPageCollection::reflow()
p.setY( currentY );
}
mLayout->updateBounds();
emit changed();
}

double QgsLayoutPageCollection::maximumPageWidth() const
Expand Down
7 changes: 7 additions & 0 deletions src/core/layout/qgslayoutpagecollection.h
Expand Up @@ -145,6 +145,13 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject
*/
double maximumPageWidth() const;

signals:

/**
* Emitted when pages are added or removed from the collection.
*/
void changed();

private:

QgsLayout *mLayout = nullptr;
Expand Down
47 changes: 21 additions & 26 deletions src/gui/layout/qgslayoutruler.cpp
Expand Up @@ -66,9 +66,8 @@ void QgsLayoutRuler::paintEvent( QPaintEvent *event )
{
return;
}
#if 0

QgsLayout *layout = mView->currentLayout();
#endif
QPainter p( this );

QTransform t = mTransform.inverted();
Expand Down Expand Up @@ -137,14 +136,23 @@ void QgsLayoutRuler::paintEvent( QPaintEvent *event )
double startY = t.map( QPointF( 0, 0 ) ).y(); //start position in mm (total including space between pages)
double endY = t.map( QPointF( 0, height() ) ).y(); //stop position in mm (total including space between pages)

#if 0 // TODO
int startPage = ( int )( startY / ( layout->paperHeight() + layout->spaceBetweenPages() ) );
#endif

// work out start page
int startPage = 0;
if ( startPage < 0 )
int endPage = 0;
double currentY = 0;
double currentPageY = 0;
for ( int page = 0; page < layout->pageCollection()->pageCount(); ++page )
{
startPage = 0;
if ( currentY < startY )
{
startPage = page;
currentPageY = currentY;
}
endPage = page;

currentY += layout->pageCollection()->page( startPage )->rect().height() + layout->pageCollection()->SPACE_BETWEEN_PAGES;
if ( currentY > endY )
break;
}

if ( startY < 0 )
Expand Down Expand Up @@ -177,30 +185,21 @@ void QgsLayoutRuler::paintEvent( QPaintEvent *event )
drawSmallDivisions( &p, beforePageCoord + mmDisplay, numSmallDivisions, -mmDisplay, startY );
}

#if 0 //TODO
int endPage = ( int )( endY / ( layout->paperHeight() + layout->spaceBetweenPages() ) );
if ( endPage > ( mLayout->numPages() - 1 ) )
{
endPage = mLayout->numPages() - 1;
}
#endif
int endPage = 0;

double nextPageStartPos = 0;
int nextPageStartPixel = 0;

for ( int i = startPage; i <= endPage; ++i )
{
double pageCoord = 0; //page coordinate in mm
//total (composition) coordinate in mm, including space between pages
#if 0 //TODO
double totalCoord = i * ( layout->paperHeight() + layout->spaceBetweenPages() );

double totalCoord = currentPageY;

//position of next page
if ( i < endPage )
{
//not the last page
nextPageStartPos = ( i + 1 ) * ( layout->paperHeight() + layout->spaceBetweenPages() );
nextPageStartPos = currentPageY + layout->pageCollection()->page( i )->rect().height() + layout->pageCollection()->SPACE_BETWEEN_PAGES;
nextPageStartPixel = mTransform.map( QPointF( 0, nextPageStartPos ) ).y();
}
else
Expand All @@ -209,8 +208,7 @@ void QgsLayoutRuler::paintEvent( QPaintEvent *event )
nextPageStartPos = 0;
nextPageStartPixel = 0;
}
#endif
double totalCoord = 0;

while ( ( totalCoord < nextPageStartPos ) || ( ( nextPageStartPos == 0 ) && ( totalCoord <= endY ) ) )
{
double pixelCoord = mTransform.map( QPointF( 0, totalCoord ) ).y();
Expand All @@ -232,6 +230,7 @@ void QgsLayoutRuler::paintEvent( QPaintEvent *event )
pageCoord += mmDisplay;
totalCoord += mmDisplay;
}
currentPageY += layout->pageCollection()->page( i )->rect().height() + layout->pageCollection()->SPACE_BETWEEN_PAGES;
}
break;
}
Expand Down Expand Up @@ -407,10 +406,6 @@ int QgsLayoutRuler::optimumNumberDivisions( double rulerScale, int scaleMultiple

void QgsLayoutRuler::setSceneTransform( const QTransform &transform )
{
#if 0
QString debug = QString::number( transform.dx() ) + ',' + QString::number( transform.dy() ) + ','
+ QString::number( transform.m11() ) + ',' + QString::number( transform.m22() );
#endif
mTransform = transform;
update();
}
Expand Down
10 changes: 10 additions & 0 deletions src/gui/layout/qgslayoutview.cpp
Expand Up @@ -53,6 +53,9 @@ void QgsLayoutView::setCurrentLayout( QgsLayout *layout )
{
setScene( layout );

connect( layout->pageCollection(), &QgsLayoutPageCollection::changed, this, &QgsLayoutView::updateRulers );
updateRulers();

//emit layoutSet, so that designer dialogs can update for the new layout
emit layoutSet( layout );
}
Expand Down Expand Up @@ -131,6 +134,7 @@ void QgsLayoutView::setVerticalRuler( QgsLayoutRuler *ruler )
void QgsLayoutView::zoomFull()
{
fitInView( scene()->sceneRect(), Qt::KeepAspectRatio );
updateRulers();
emit zoomLevelChanged();
}

Expand Down Expand Up @@ -305,6 +309,12 @@ void QgsLayoutView::resizeEvent( QResizeEvent *event )
emit zoomLevelChanged();
}

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

void QgsLayoutView::updateRulers()
{
if ( mHorizontalRuler )
Expand Down
1 change: 1 addition & 0 deletions src/gui/layout/qgslayoutview.h
Expand Up @@ -209,6 +209,7 @@ class GUI_EXPORT QgsLayoutView: public QGraphicsView
void keyPressEvent( QKeyEvent *event ) override;
void keyReleaseEvent( QKeyEvent *event ) override;
void resizeEvent( QResizeEvent *event ) override;
void scrollContentsBy( int dx, int dy ) override;

private slots:

Expand Down

0 comments on commit 416e1e4

Please sign in to comment.