Skip to content

Commit 886a120

Browse files
committedAug 7, 2017
Dropping guides outside of pages doesn't create new guides
1 parent b0956c9 commit 886a120

File tree

7 files changed

+83
-24
lines changed

7 files changed

+83
-24
lines changed
 

‎python/core/layout/qgslayoutpagecollection.sip

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ class QgsLayoutPageCollection : QObject
5959
:rtype: QgsLayoutItemPage
6060
%End
6161

62+
int pageNumber( QgsLayoutItemPage *page ) const;
63+
%Docstring
64+
Returns the page number for the specified ``page``, or -1 if the page
65+
is not contained in the collection.
66+
:rtype: int
67+
%End
68+
6269
void addPage( QgsLayoutItemPage *page /Transfer/ );
6370
%Docstring
6471
Adds a ``page`` to the collection. Ownership of the ``page`` is transferred

‎python/gui/layout/qgslayoutruler.sip

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ class QgsLayoutRuler: QWidget
6767
``position`` is in layout coordinates.
6868
%End
6969

70+
signals:
71+
void cursorPosChanged( QPointF );
72+
%Docstring
73+
Is emitted when mouse cursor coordinates change
74+
%End
75+
7076
protected:
7177
virtual void paintEvent( QPaintEvent *event );
7278

@@ -77,12 +83,6 @@ class QgsLayoutRuler: QWidget
7783
virtual void mouseReleaseEvent( QMouseEvent *event );
7884

7985

80-
signals:
81-
void cursorPosChanged( QPointF );
82-
%Docstring
83-
Is emitted when mouse cursor coordinates change
84-
%End
85-
8686
};
8787

8888
/************************************************************************

‎src/core/layout/qgslayoutpagecollection.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,11 @@ QgsLayoutItemPage *QgsLayoutPageCollection::page( int pageNumber )
163163
return mPages.value( pageNumber );
164164
}
165165

166+
int QgsLayoutPageCollection::pageNumber( QgsLayoutItemPage *page ) const
167+
{
168+
return mPages.indexOf( page );
169+
}
170+
166171
void QgsLayoutPageCollection::addPage( QgsLayoutItemPage *page )
167172
{
168173
mPages.append( page );

‎src/core/layout/qgslayoutpagecollection.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject
7373
*/
7474
QgsLayoutItemPage *page( int pageNumber );
7575

76+
/**
77+
* Returns the page number for the specified \a page, or -1 if the page
78+
* is not contained in the collection.
79+
*/
80+
int pageNumber( QgsLayoutItemPage *page ) const;
81+
7682
/**
7783
* Adds a \a page to the collection. Ownership of the \a page is transferred
7884
* to the collection, and the page will automatically be added to the collection's

‎src/gui/layout/qgslayoutruler.cpp

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,25 @@ void QgsLayoutRuler::drawMarkerPos( QPainter *painter )
260260
}
261261
}
262262

263+
void QgsLayoutRuler::createTemporaryGuideItem()
264+
{
265+
mGuideItem.reset( new QGraphicsLineItem() );
266+
267+
mGuideItem->setZValue( QgsLayout::ZGuide );
268+
QPen linePen( Qt::DashLine );
269+
linePen.setColor( Qt::red );
270+
linePen.setWidthF( 0 );
271+
mGuideItem->setPen( linePen );
272+
273+
mView->currentLayout()->addItem( mGuideItem.get() );
274+
}
275+
276+
QPointF QgsLayoutRuler::convertLocalPointToLayout( QPoint localPoint ) const
277+
{
278+
QPoint viewPoint = mView->mapFromGlobal( mapToGlobal( localPoint ) );
279+
return mView->mapToScene( viewPoint );
280+
}
281+
263282
void QgsLayoutRuler::drawRotatedText( QPainter *painter, QPointF pos, const QString &text )
264283
{
265284
painter->save();
@@ -432,9 +451,9 @@ void QgsLayoutRuler::mouseMoveEvent( QMouseEvent *event )
432451
if ( mCreatingGuide )
433452
{
434453
// event -> layout coordinates
454+
displayPos = convertLocalPointToLayout( event->pos() );
455+
435456
QgsLayout *layout = mView->currentLayout();
436-
QPoint viewPoint = mView->mapFromGlobal( mapToGlobal( event->pos() ) );
437-
displayPos = mView->mapToScene( viewPoint );
438457
int pageNo = layout->pageCollection()->pageNumberForPoint( displayPos );
439458
QgsLayoutItemPage *page = layout->pageCollection()->page( pageNo );
440459

@@ -484,15 +503,7 @@ void QgsLayoutRuler::mousePressEvent( QMouseEvent *event )
484503
if ( event->button() == Qt::LeftButton )
485504
{
486505
mCreatingGuide = true;
487-
mGuideItem.reset( new QGraphicsLineItem() );
488-
489-
mGuideItem->setZValue( QgsLayout::ZGuide );
490-
QPen linePen( Qt::DashLine );
491-
linePen.setColor( Qt::red );
492-
linePen.setWidthF( 0 );
493-
mGuideItem->setPen( linePen );
494-
495-
mView->currentLayout()->addItem( mGuideItem.get() );
506+
createTemporaryGuideItem();
496507

497508
switch ( mOrientation )
498509
{
@@ -516,12 +527,32 @@ void QgsLayoutRuler::mouseReleaseEvent( QMouseEvent *event )
516527
QApplication::restoreOverrideCursor();
517528
mGuideItem.reset();
518529

530+
// check that cursor left the ruler
531+
switch ( mOrientation )
532+
{
533+
case Qt::Horizontal:
534+
{
535+
if ( event->pos().y() <= height() )
536+
return;
537+
break;
538+
}
539+
case Qt::Vertical:
540+
{
541+
if ( event->pos().x() <= width() )
542+
return;
543+
break;
544+
}
545+
}
546+
519547
QgsLayout *layout = mView->currentLayout();
520548

521549
// create guide
522-
QPoint viewPoint = mView->mapFromGlobal( mapToGlobal( event->pos() ) );
523-
QPointF scenePos = mView->mapToScene( viewPoint );
524-
int page = layout->pageCollection()->pageNumberForPoint( scenePos );
550+
QPointF scenePos = convertLocalPointToLayout( event->pos() );
551+
QgsLayoutItemPage *page = layout->pageCollection()->pageAtPoint( scenePos );
552+
if ( !page )
553+
return; // dragged outside of a page
554+
555+
int pageNumber = layout->pageCollection()->pageNumber( page );
525556
std::unique_ptr< QgsLayoutGuide > guide;
526557
switch ( mOrientation )
527558
{
@@ -539,7 +570,7 @@ void QgsLayoutRuler::mouseReleaseEvent( QMouseEvent *event )
539570
break;
540571
}
541572
}
542-
guide->setPage( page );
573+
guide->setPage( pageNumber );
543574
mView->currentLayout()->guides().addGuide( guide.release() );
544575
}
545576
}

‎src/gui/layout/qgslayoutruler.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ class GUI_EXPORT QgsLayoutRuler: public QWidget
7878
*/
7979
void setCursorPosition( QPointF position );
8080

81+
signals:
82+
//! Is emitted when mouse cursor coordinates change
83+
void cursorPosChanged( QPointF );
84+
8185
protected:
8286
void paintEvent( QPaintEvent *event ) override;
8387
void mouseMoveEvent( QMouseEvent *event ) override;
@@ -129,9 +133,10 @@ class GUI_EXPORT QgsLayoutRuler: public QWidget
129133
//! Draw current marker pos on ruler
130134
void drawMarkerPos( QPainter *painter );
131135

132-
signals:
133-
//! Is emitted when mouse cursor coordinates change
134-
void cursorPosChanged( QPointF );
136+
void createTemporaryGuideItem();
137+
138+
QPointF convertLocalPointToLayout( QPoint localPoint ) const;
139+
135140

136141
};
137142

‎tests/src/python/test_qgslayoutpagecollection.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ def testPages(self):
7676
# add a page
7777
page = QgsLayoutItemPage(l)
7878
page.setPageSize('A4')
79+
self.assertEqual(collection.pageNumber(page), -1)
80+
7981
collection.addPage(page)
8082

8183
self.assertTrue(page in l.items())
@@ -85,6 +87,7 @@ def testPages(self):
8587
self.assertFalse(collection.page(-1))
8688
self.assertEqual(collection.page(0), page)
8789
self.assertFalse(collection.page(1))
90+
self.assertEqual(collection.pageNumber(page), 0)
8891

8992
# add a second page
9093
page2 = QgsLayoutItemPage(l)
@@ -96,6 +99,7 @@ def testPages(self):
9699
self.assertFalse(collection.page(-1))
97100
self.assertEqual(collection.page(0), page)
98101
self.assertEqual(collection.page(1), page2)
102+
self.assertEqual(collection.pageNumber(page2), 1)
99103

100104
# insert a page
101105
page3 = QgsLayoutItemPage(l)
@@ -108,6 +112,7 @@ def testPages(self):
108112
self.assertEqual(collection.page(0), page)
109113
self.assertEqual(collection.page(1), page3)
110114
self.assertEqual(collection.page(2), page2)
115+
self.assertEqual(collection.pageNumber(page3), 1)
111116

112117
# delete page
113118
collection.deletePage(-1)

0 commit comments

Comments
 (0)
Please sign in to comment.