Skip to content
Permalink
Browse files

Fix cursor position display for multipage layouts

  • Loading branch information
nyalldawson committed Jul 22, 2017
1 parent 416e1e4 commit 0ab3b8e0ad7c3d2a5415a81a061e2dc9d881b540
@@ -137,6 +137,25 @@ Size of page shadow, in layout coordinates
:rtype: float
%End

int pageNumberForPoint( QPointF point ) const;
%Docstring
Returns the page number corresponding to a ``point`` in the layout (in layout units).

Page numbers in collections begin at 0 - so a page number of 0 indicates the
first page.

.. seealso:: positionOnPage()
:rtype: int
%End

QPointF positionOnPage( QPointF point ) const;
%Docstring
Returns the position within a page of a ``point`` in the layout (in layout units).

.. seealso:: pageNumberForPoint()
:rtype: QPointF
%End

signals:

void changed();
@@ -428,16 +428,12 @@ void QgsLayoutDesignerDialog::updateStatusCursorPos( QPointF position )
}

//convert cursor position to position on current page
#if 0 // TODO
QPointF pagePosition = mView->currentLayout()->positionOnPage( cursorPosition );
int currentPage = mView->currentLayout()->pageNumberForPoint( cursorPosition );
#endif
QPointF pagePosition = position;
int currentPage = 1;
QPointF pagePosition = mLayout->pageCollection()->positionOnPage( position );
int currentPage = mLayout->pageCollection()->pageNumberForPoint( position );

mStatusCursorXLabel->setText( QString( tr( "x: %1 mm" ) ).arg( pagePosition.x() ) );
mStatusCursorYLabel->setText( QString( tr( "y: %1 mm" ) ).arg( pagePosition.y() ) );
mStatusCursorPageLabel->setText( QString( tr( "page: %1" ) ).arg( currentPage ) );
mStatusCursorPageLabel->setText( QString( tr( "page: %1" ) ).arg( currentPage + 1 ) );
}

void QgsLayoutDesignerDialog::toggleFullScreen( bool enabled )
@@ -65,6 +65,52 @@ double QgsLayoutPageCollection::maximumPageWidth() const
return maxWidth;
}

int QgsLayoutPageCollection::pageNumberForPoint( QPointF point ) const
{
int pageNumber = 0;
double startNextPageY = 0;
Q_FOREACH ( QgsLayoutItemPage *page, mPages )
{
startNextPageY += page->rect().height() + SPACE_BETWEEN_PAGES;
if ( startNextPageY > point.y() )
break;
pageNumber++;
}

if ( pageNumber > mPages.count() - 1 )
pageNumber = mPages.count() - 1;
return pageNumber;
}

QPointF QgsLayoutPageCollection::positionOnPage( QPointF position ) const
{
double startCurrentPageY = 0;
double startNextPageY = 0;
int pageNumber = 0;
Q_FOREACH ( QgsLayoutItemPage *page, mPages )
{
startCurrentPageY = startNextPageY;
startNextPageY += page->rect().height() + SPACE_BETWEEN_PAGES;
if ( startNextPageY > position.y() )
break;
pageNumber++;
}

double y;
if ( pageNumber == mPages.size() )
{
//y coordinate is greater then the end of the last page, so return distance between
//top of last page and y coordinate
y = position.y() - ( startNextPageY - SPACE_BETWEEN_PAGES );
}
else
{
//y coordinate is less then the end of the last page
y = position.y() - startCurrentPageY;
}
return QPointF( position.x(), y );
}

QgsLayout *QgsLayoutPageCollection::layout() const
{
return mLayout;
@@ -145,6 +145,23 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject
*/
double maximumPageWidth() const;

/**
* Returns the page number corresponding to a \a point in the layout (in layout units).
*
* Page numbers in collections begin at 0 - so a page number of 0 indicates the
* first page.
*
* \see positionOnPage()
*/
int pageNumberForPoint( QPointF point ) const;

/**
* Returns the position within a page of a \a point in the layout (in layout units).
*
* \see pageNumberForPoint()
*/
QPointF positionOnPage( QPointF point ) const;

signals:

/**
@@ -25,7 +25,7 @@
QgsLayoutPageCollection,
QgsSimpleFillSymbolLayer,
QgsFillSymbol)
from qgis.PyQt.QtCore import Qt, QCoreApplication, QEvent
from qgis.PyQt.QtCore import Qt, QCoreApplication, QEvent, QPointF
from qgis.testing import start_app, unittest

start_app()
@@ -253,6 +253,49 @@ def testDataDefinedSize(self):
self.assertEqual(page3.pos().x(), 0)
self.assertEqual(page3.pos().y(), 200)

def testPositionOnPage(self):
"""
Test pageNumberForPoint and positionOnPage
"""
p = QgsProject()
l = QgsLayout(p)
collection = l.pageCollection()

# add a page
page = QgsLayoutItemPage(l)
page.setPageSize('A4')
collection.addPage(page)

self.assertEqual(collection.pageNumberForPoint(QPointF(-100, -100)), 0)
self.assertEqual(collection.pageNumberForPoint(QPointF(-100, -1)), 0)
self.assertEqual(collection.pageNumberForPoint(QPointF(-100, 1)), 0)
self.assertEqual(collection.pageNumberForPoint(QPointF(-100, 270)), 0)
self.assertEqual(collection.pageNumberForPoint(QPointF(-100, 1270)), 0)

self.assertEqual(collection.positionOnPage(QPointF(-100, -100)), QPointF(-100, -100))
self.assertEqual(collection.positionOnPage(QPointF(-100, -1)), QPointF(-100, -1))
self.assertEqual(collection.positionOnPage(QPointF(-100, 1)), QPointF(-100, 1))
self.assertEqual(collection.positionOnPage(QPointF(-100, 270)), QPointF(-100, 270))
self.assertEqual(collection.positionOnPage(QPointF(-100, 1270)), QPointF(-100, 973))

page2 = QgsLayoutItemPage(l)
page2.setPageSize('A5')
collection.addPage(page2)

self.assertEqual(collection.pageNumberForPoint(QPointF(-100, -100)), 0)
self.assertEqual(collection.pageNumberForPoint(QPointF(-100, -1)), 0)
self.assertEqual(collection.pageNumberForPoint(QPointF(-100, 1)), 0)
self.assertEqual(collection.pageNumberForPoint(QPointF(-100, 270)), 0)
self.assertEqual(collection.pageNumberForPoint(QPointF(-100, 370)), 1)
self.assertEqual(collection.pageNumberForPoint(QPointF(-100, 1270)), 1)

self.assertEqual(collection.positionOnPage(QPointF(-100, -100)), QPointF(-100, -100))
self.assertEqual(collection.positionOnPage(QPointF(-100, -1)), QPointF(-100, -1))
self.assertEqual(collection.positionOnPage(QPointF(-100, 1)), QPointF(-100, 1))
self.assertEqual(collection.positionOnPage(QPointF(-100, 270)), QPointF(-100, 270))
self.assertEqual(collection.positionOnPage(QPointF(-100, 370)), QPointF(-100, 63))
self.assertEqual(collection.positionOnPage(QPointF(-100, 1270)), QPointF(-100, 753))


if __name__ == '__main__':
unittest.main()

0 comments on commit 0ab3b8e

Please sign in to comment.
You can’t perform that action at this time.