Skip to content

Commit c4c0c83

Browse files
committed
Restore api for handling layout item page numbers
1 parent 714920f commit c4c0c83

12 files changed

+332
-19
lines changed

python/core/layout/qgslayoutitem.sip

+28-2
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem, QgsLayoutUndoObjectInt
296296
.. seealso:: sizeWithUnits()
297297
%End
298298

299-
virtual void attemptMove( const QgsLayoutPoint &point, bool useReferencePoint = true, bool includesFrame = false );
299+
virtual void attemptMove( const QgsLayoutPoint &point, bool useReferencePoint = true, bool includesFrame = false, int page = -1 );
300300
%Docstring
301301
Attempts to move the item to a specified ``point``.
302302

@@ -310,6 +310,10 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem, QgsLayoutUndoObjectInt
310310
If ``includesFrame`` is true, then the position specified by ``point`` represents the
311311
point at which to place the outside of the item's frame.
312312

313+
If ``page`` is not left at the default -1 value, then the position specified by ``point``
314+
refers to the relative position on the corresponding layout ``page`` (where a ``page``
315+
of 0 represents the first page).
316+
313317
Note that the final position of the item may not match the specified target position,
314318
as data defined item position may override the specified value.
315319

@@ -318,7 +322,6 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem, QgsLayoutUndoObjectInt
318322
.. seealso:: positionWithUnits()
319323
%End
320324

321-
322325
void attemptSetSceneRect( const QRectF &rect, bool includesFrame = false );
323326
%Docstring
324327
Attempts to update the item's position and size to match the passed ``rect`` in layout
@@ -347,6 +350,29 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem, QgsLayoutUndoObjectInt
347350
:rtype: QgsLayoutPoint
348351
%End
349352

353+
int page() const;
354+
%Docstring
355+
Returns the page the item is currently on, with the first page returning 0.
356+
.. seealso:: pagePos()
357+
:rtype: int
358+
%End
359+
360+
QPointF pagePos() const;
361+
%Docstring
362+
Returns the item's position (in layout units) relative to the top left corner of its current page.
363+
.. seealso:: page()
364+
.. seealso:: pagePositionWithUnits()
365+
:rtype: QPointF
366+
%End
367+
368+
QgsLayoutPoint pagePositionWithUnits() const;
369+
%Docstring
370+
Returns the item's position (in item units) relative to the top left corner of its current page.
371+
.. seealso:: page()
372+
.. seealso:: pagePos()
373+
:rtype: QgsLayoutPoint
374+
%End
375+
350376
QgsLayoutSize sizeWithUnits() const;
351377
%Docstring
352378
Returns the item's current size, including units.

python/core/layout/qgslayoutitemgroup.sip

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class QgsLayoutItemGroup: QgsLayoutItem
6262
virtual void setVisibility( const bool visible );
6363

6464

65-
virtual void attemptMove( const QgsLayoutPoint &point, bool useReferencePoint = true, bool includesFrame = false );
65+
virtual void attemptMove( const QgsLayoutPoint &point, bool useReferencePoint = true, bool includesFrame = false, int page = -1 );
6666

6767
virtual void attemptResize( const QgsLayoutSize &size, bool includesFrame = false );
6868

python/core/layout/qgslayoutpagecollection.sip

+14
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,20 @@ class QgsLayoutPageCollection : QObject, QgsLayoutSerializableObject
195195
:rtype: QgsLayoutItemPage
196196
%End
197197

198+
QPointF pagePositionToLayoutPosition( int page, const QgsLayoutPoint &position ) const;
199+
%Docstring
200+
Converts a ``position`` on a ``page`` to an absolute position in layout coordinates.\
201+
.. seealso:: pagePositionToAbsolute()
202+
:rtype: QPointF
203+
%End
204+
205+
QgsLayoutPoint pagePositionToAbsolute( int page, const QgsLayoutPoint &position ) const;
206+
%Docstring
207+
Converts a ``position`` on a ``page`` to an absolute position in (maintaining the units from the input ``position``).
208+
.. seealso:: pagePositionToLayoutPosition()
209+
:rtype: QgsLayoutPoint
210+
%End
211+
198212
QPointF positionOnPage( QPointF point ) const;
199213
%Docstring
200214
Returns the position within a page of a ``point`` in the layout (in layout units).

src/core/layout/qgslayoutitem.cpp

+39-4
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,7 @@ void QgsLayoutItem::setId( const QString &id )
130130
mLayout->itemsModel()->updateItemDisplayName( this );
131131
}
132132

133-
#if 0 //TODO
134-
emit itemChanged();
135-
#endif
133+
emit changed();
136134
}
137135

138136
void QgsLayoutItem::setSelected( bool selected )
@@ -377,7 +375,7 @@ void QgsLayoutItem::attemptResize( const QgsLayoutSize &s, bool includesFrame )
377375
emit sizePositionChanged();
378376
}
379377

380-
void QgsLayoutItem::attemptMove( const QgsLayoutPoint &p, bool useReferencePoint, bool includesFrame )
378+
void QgsLayoutItem::attemptMove( const QgsLayoutPoint &p, bool useReferencePoint, bool includesFrame, int page )
381379
{
382380
if ( !mLayout )
383381
{
@@ -387,6 +385,10 @@ void QgsLayoutItem::attemptMove( const QgsLayoutPoint &p, bool useReferencePoint
387385
}
388386

389387
QgsLayoutPoint point = p;
388+
if ( page >= 0 )
389+
{
390+
point = mLayout->pageCollection()->pagePositionToAbsolute( page, p );
391+
}
390392

391393
if ( includesFrame )
392394
{
@@ -433,6 +435,39 @@ void QgsLayoutItem::attemptSetSceneRect( const QRectF &rect, bool includesFrame
433435
emit sizePositionChanged();
434436
}
435437

438+
int QgsLayoutItem::page() const
439+
{
440+
if ( !mLayout )
441+
return -1;
442+
443+
return mLayout->pageCollection()->pageNumberForPoint( pos() );
444+
}
445+
446+
QPointF QgsLayoutItem::pagePos() const
447+
{
448+
QPointF p = pos();
449+
450+
if ( !mLayout )
451+
return p;
452+
453+
// try to get page
454+
QgsLayoutItemPage *pageItem = mLayout->pageCollection()->page( page() );
455+
if ( !pageItem )
456+
return p;
457+
458+
p.ry() -= pageItem->pos().y();
459+
return p;
460+
}
461+
462+
QgsLayoutPoint QgsLayoutItem::pagePositionWithUnits() const
463+
{
464+
QPointF p = pagePos();
465+
if ( !mLayout )
466+
return QgsLayoutPoint( p );
467+
468+
return mLayout->convertFromLayoutUnits( p, mItemPosition.units() );
469+
}
470+
436471
void QgsLayoutItem::setScenePos( const QPointF &destinationPos )
437472
{
438473
//since setPos does not account for item rotation, use difference between

src/core/layout/qgslayoutitem.h

+25-2
Original file line numberDiff line numberDiff line change
@@ -324,15 +324,18 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
324324
* If \a includesFrame is true, then the position specified by \a point represents the
325325
* point at which to place the outside of the item's frame.
326326
*
327+
* If \a page is not left at the default -1 value, then the position specified by \a point
328+
* refers to the relative position on the corresponding layout \a page (where a \a page
329+
* of 0 represents the first page).
330+
*
327331
* Note that the final position of the item may not match the specified target position,
328332
* as data defined item position may override the specified value.
329333
*
330334
* \see attemptResize()
331335
* \see referencePoint()
332336
* \see positionWithUnits()
333337
*/
334-
virtual void attemptMove( const QgsLayoutPoint &point, bool useReferencePoint = true, bool includesFrame = false );
335-
338+
virtual void attemptMove( const QgsLayoutPoint &point, bool useReferencePoint = true, bool includesFrame = false, int page = -1 );
336339

337340
/**
338341
* Attempts to update the item's position and size to match the passed \a rect in layout
@@ -361,6 +364,26 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
361364
*/
362365
QgsLayoutPoint positionWithUnits() const { return mItemPosition; }
363366

367+
/**
368+
* Returns the page the item is currently on, with the first page returning 0.
369+
* \see pagePos()
370+
*/
371+
int page() const;
372+
373+
/**
374+
* Returns the item's position (in layout units) relative to the top left corner of its current page.
375+
* \see page()
376+
* \see pagePositionWithUnits()
377+
*/
378+
QPointF pagePos() const;
379+
380+
/**
381+
* Returns the item's position (in item units) relative to the top left corner of its current page.
382+
* \see page()
383+
* \see pagePos()
384+
*/
385+
QgsLayoutPoint pagePositionWithUnits() const;
386+
364387
/**
365388
* Returns the item's current size, including units.
366389
* \see attemptResize()

src/core/layout/qgslayoutitemgroup.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void QgsLayoutItemGroup::setVisibility( const bool visible )
127127
mLayout->undoStack()->endMacro();
128128
}
129129

130-
void QgsLayoutItemGroup::attemptMove( const QgsLayoutPoint &point, bool useReferencePoint, bool includesFrame )
130+
void QgsLayoutItemGroup::attemptMove( const QgsLayoutPoint &point, bool useReferencePoint, bool includesFrame, int page )
131131
{
132132
Q_UNUSED( useReferencePoint ); //groups should always have reference point in top left
133133
if ( !mLayout )
@@ -136,7 +136,12 @@ void QgsLayoutItemGroup::attemptMove( const QgsLayoutPoint &point, bool useRefer
136136
if ( !shouldBlockUndoCommands() )
137137
mLayout->undoStack()->beginMacro( tr( "Move group" ) );
138138

139-
QPointF scenePoint = mLayout->convertToLayoutUnits( point );
139+
QPointF scenePoint;
140+
if ( page < 0 )
141+
scenePoint = mLayout->convertToLayoutUnits( point );
142+
else
143+
scenePoint = mLayout->pageCollection()->pagePositionToLayoutPosition( page, point );
144+
140145
double deltaX = scenePoint.x() - pos().x();
141146
double deltaY = scenePoint.y() - pos().y();
142147

src/core/layout/qgslayoutitemgroup.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class CORE_EXPORT QgsLayoutItemGroup: public QgsLayoutItem
6969
void setVisibility( const bool visible ) override;
7070

7171
//overridden to move child items
72-
void attemptMove( const QgsLayoutPoint &point, bool useReferencePoint = true, bool includesFrame = false ) override;
72+
void attemptMove( const QgsLayoutPoint &point, bool useReferencePoint = true, bool includesFrame = false, int page = -1 ) override;
7373
void attemptResize( const QgsLayoutSize &size, bool includesFrame = false ) override;
7474

7575
void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;

src/core/layout/qgslayoutpagecollection.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,27 @@ QgsLayoutItemPage *QgsLayoutPageCollection::pageAtPoint( QPointF point ) const
107107
return nullptr;
108108
}
109109

110+
QPointF QgsLayoutPageCollection::pagePositionToLayoutPosition( int page, const QgsLayoutPoint &position ) const
111+
{
112+
QPointF layoutUnitsPos = mLayout->convertToLayoutUnits( position );
113+
if ( page > 0 && page < mPages.count() )
114+
{
115+
layoutUnitsPos.ry() += mPages.at( page )->pos().y();
116+
}
117+
return layoutUnitsPos;
118+
}
119+
120+
QgsLayoutPoint QgsLayoutPageCollection::pagePositionToAbsolute( int page, const QgsLayoutPoint &position ) const
121+
{
122+
double vDelta = 0.0;
123+
if ( page > 0 && page < mPages.count() )
124+
{
125+
vDelta = mLayout->convertFromLayoutUnits( mPages.at( page )->pos().y(), position.units() ).length();
126+
}
127+
128+
return QgsLayoutPoint( position.x(), position.y() + vDelta, position.units() );
129+
}
130+
110131
QPointF QgsLayoutPageCollection::positionOnPage( QPointF position ) const
111132
{
112133
double startCurrentPageY = 0;

src/core/layout/qgslayoutpagecollection.h

+12
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,18 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject, public QgsLayoutSeri
199199
*/
200200
QgsLayoutItemPage *pageAtPoint( QPointF point ) const;
201201

202+
/**
203+
* Converts a \a position on a \a page to an absolute position in layout coordinates.\
204+
* \see pagePositionToAbsolute()
205+
*/
206+
QPointF pagePositionToLayoutPosition( int page, const QgsLayoutPoint &position ) const;
207+
208+
/**
209+
* Converts a \a position on a \a page to an absolute position in (maintaining the units from the input \a position).
210+
* \see pagePositionToLayoutPosition()
211+
*/
212+
QgsLayoutPoint pagePositionToAbsolute( int page, const QgsLayoutPoint &position ) const;
213+
202214
/**
203215
* Returns the position within a page of a \a point in the layout (in layout units).
204216
*

src/gui/layout/qgslayoutitemwidget.cpp

+3-7
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ void QgsLayoutItemPropertiesWidget::changeItemPosition()
331331
mItem->layout()->undoStack()->beginCommand( mItem, tr( "Move Item" ), QgsLayoutItem::UndoIncrementalMove );
332332

333333
QgsLayoutPoint point( mXPosSpin->value(), mYPosSpin->value(), mPosUnitsComboBox->unit() );
334-
mItem->attemptMove( point );
334+
mItem->attemptMove( point, true, false, mPageSpinBox->value() - 1 );
335335

336336
mItem->layout()->undoStack()->endCommand();
337337
}
@@ -493,9 +493,7 @@ void QgsLayoutItemPropertiesWidget::setValuesForGuiPositionElements()
493493
};
494494
block( true );
495495

496-
QPointF pos; //TODO = mItem->pagePos();
497-
498-
QgsLayoutPoint point = mItem->positionWithUnits();
496+
QgsLayoutPoint point = mItem->pagePositionWithUnits();
499497

500498
if ( !mFreezeXPosSpin )
501499
mXPosSpin->setValue( point.x() );
@@ -571,10 +569,8 @@ void QgsLayoutItemPropertiesWidget::setValuesForGuiPositionElements()
571569
mSizeLockAspectRatio->resetRatio();
572570
mPosLockAspectRatio->resetRatio();
573571

574-
#if 0 //TODO
575572
if ( !mFreezePageSpin )
576-
mPageSpinBox->setValue( mItem->page() );
577-
#endif
573+
mPageSpinBox->setValue( mItem->page() + 1 );
578574

579575
block( false );
580576
}

0 commit comments

Comments
 (0)