Skip to content

Commit

Permalink
Restore api for handling layout item page numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Nov 24, 2017
1 parent 714920f commit c4c0c83
Show file tree
Hide file tree
Showing 12 changed files with 332 additions and 19 deletions.
30 changes: 28 additions & 2 deletions python/core/layout/qgslayoutitem.sip
Expand Up @@ -296,7 +296,7 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem, QgsLayoutUndoObjectInt
.. seealso:: sizeWithUnits()
%End

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

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

If ``page`` is not left at the default -1 value, then the position specified by ``point``
refers to the relative position on the corresponding layout ``page`` (where a ``page``
of 0 represents the first page).

Note that the final position of the item may not match the specified target position,
as data defined item position may override the specified value.

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


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

int page() const;
%Docstring
Returns the page the item is currently on, with the first page returning 0.
.. seealso:: pagePos()
:rtype: int
%End

QPointF pagePos() const;
%Docstring
Returns the item's position (in layout units) relative to the top left corner of its current page.
.. seealso:: page()
.. seealso:: pagePositionWithUnits()
:rtype: QPointF
%End

QgsLayoutPoint pagePositionWithUnits() const;
%Docstring
Returns the item's position (in item units) relative to the top left corner of its current page.
.. seealso:: page()
.. seealso:: pagePos()
:rtype: QgsLayoutPoint
%End

QgsLayoutSize sizeWithUnits() const;
%Docstring
Returns the item's current size, including units.
Expand Down
2 changes: 1 addition & 1 deletion python/core/layout/qgslayoutitemgroup.sip
Expand Up @@ -62,7 +62,7 @@ class QgsLayoutItemGroup: QgsLayoutItem
virtual void setVisibility( const bool visible );


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

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

Expand Down
14 changes: 14 additions & 0 deletions python/core/layout/qgslayoutpagecollection.sip
Expand Up @@ -195,6 +195,20 @@ class QgsLayoutPageCollection : QObject, QgsLayoutSerializableObject
:rtype: QgsLayoutItemPage
%End

QPointF pagePositionToLayoutPosition( int page, const QgsLayoutPoint &position ) const;
%Docstring
Converts a ``position`` on a ``page`` to an absolute position in layout coordinates.\
.. seealso:: pagePositionToAbsolute()
:rtype: QPointF
%End

QgsLayoutPoint pagePositionToAbsolute( int page, const QgsLayoutPoint &position ) const;
%Docstring
Converts a ``position`` on a ``page`` to an absolute position in (maintaining the units from the input ``position``).
.. seealso:: pagePositionToLayoutPosition()
:rtype: QgsLayoutPoint
%End

QPointF positionOnPage( QPointF point ) const;
%Docstring
Returns the position within a page of a ``point`` in the layout (in layout units).
Expand Down
43 changes: 39 additions & 4 deletions src/core/layout/qgslayoutitem.cpp
Expand Up @@ -130,9 +130,7 @@ void QgsLayoutItem::setId( const QString &id )
mLayout->itemsModel()->updateItemDisplayName( this );
}

#if 0 //TODO
emit itemChanged();
#endif
emit changed();
}

void QgsLayoutItem::setSelected( bool selected )
Expand Down Expand Up @@ -377,7 +375,7 @@ void QgsLayoutItem::attemptResize( const QgsLayoutSize &s, bool includesFrame )
emit sizePositionChanged();
}

void QgsLayoutItem::attemptMove( const QgsLayoutPoint &p, bool useReferencePoint, bool includesFrame )
void QgsLayoutItem::attemptMove( const QgsLayoutPoint &p, bool useReferencePoint, bool includesFrame, int page )
{
if ( !mLayout )
{
Expand All @@ -387,6 +385,10 @@ void QgsLayoutItem::attemptMove( const QgsLayoutPoint &p, bool useReferencePoint
}

QgsLayoutPoint point = p;
if ( page >= 0 )
{
point = mLayout->pageCollection()->pagePositionToAbsolute( page, p );
}

if ( includesFrame )
{
Expand Down Expand Up @@ -433,6 +435,39 @@ void QgsLayoutItem::attemptSetSceneRect( const QRectF &rect, bool includesFrame
emit sizePositionChanged();
}

int QgsLayoutItem::page() const
{
if ( !mLayout )
return -1;

return mLayout->pageCollection()->pageNumberForPoint( pos() );
}

QPointF QgsLayoutItem::pagePos() const
{
QPointF p = pos();

if ( !mLayout )
return p;

// try to get page
QgsLayoutItemPage *pageItem = mLayout->pageCollection()->page( page() );
if ( !pageItem )
return p;

p.ry() -= pageItem->pos().y();
return p;
}

QgsLayoutPoint QgsLayoutItem::pagePositionWithUnits() const
{
QPointF p = pagePos();
if ( !mLayout )
return QgsLayoutPoint( p );

return mLayout->convertFromLayoutUnits( p, mItemPosition.units() );
}

void QgsLayoutItem::setScenePos( const QPointF &destinationPos )
{
//since setPos does not account for item rotation, use difference between
Expand Down
27 changes: 25 additions & 2 deletions src/core/layout/qgslayoutitem.h
Expand Up @@ -324,15 +324,18 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
* If \a includesFrame is true, then the position specified by \a point represents the
* point at which to place the outside of the item's frame.
*
* If \a page is not left at the default -1 value, then the position specified by \a point
* refers to the relative position on the corresponding layout \a page (where a \a page
* of 0 represents the first page).
*
* Note that the final position of the item may not match the specified target position,
* as data defined item position may override the specified value.
*
* \see attemptResize()
* \see referencePoint()
* \see positionWithUnits()
*/
virtual void attemptMove( const QgsLayoutPoint &point, bool useReferencePoint = true, bool includesFrame = false );

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

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

/**
* Returns the page the item is currently on, with the first page returning 0.
* \see pagePos()
*/
int page() const;

/**
* Returns the item's position (in layout units) relative to the top left corner of its current page.
* \see page()
* \see pagePositionWithUnits()
*/
QPointF pagePos() const;

/**
* Returns the item's position (in item units) relative to the top left corner of its current page.
* \see page()
* \see pagePos()
*/
QgsLayoutPoint pagePositionWithUnits() const;

/**
* Returns the item's current size, including units.
* \see attemptResize()
Expand Down
9 changes: 7 additions & 2 deletions src/core/layout/qgslayoutitemgroup.cpp
Expand Up @@ -127,7 +127,7 @@ void QgsLayoutItemGroup::setVisibility( const bool visible )
mLayout->undoStack()->endMacro();
}

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

QPointF scenePoint = mLayout->convertToLayoutUnits( point );
QPointF scenePoint;
if ( page < 0 )
scenePoint = mLayout->convertToLayoutUnits( point );
else
scenePoint = mLayout->pageCollection()->pagePositionToLayoutPosition( page, point );

double deltaX = scenePoint.x() - pos().x();
double deltaY = scenePoint.y() - pos().y();

Expand Down
2 changes: 1 addition & 1 deletion src/core/layout/qgslayoutitemgroup.h
Expand Up @@ -69,7 +69,7 @@ class CORE_EXPORT QgsLayoutItemGroup: public QgsLayoutItem
void setVisibility( const bool visible ) override;

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

void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget ) override;
Expand Down
21 changes: 21 additions & 0 deletions src/core/layout/qgslayoutpagecollection.cpp
Expand Up @@ -107,6 +107,27 @@ QgsLayoutItemPage *QgsLayoutPageCollection::pageAtPoint( QPointF point ) const
return nullptr;
}

QPointF QgsLayoutPageCollection::pagePositionToLayoutPosition( int page, const QgsLayoutPoint &position ) const
{
QPointF layoutUnitsPos = mLayout->convertToLayoutUnits( position );
if ( page > 0 && page < mPages.count() )
{
layoutUnitsPos.ry() += mPages.at( page )->pos().y();
}
return layoutUnitsPos;
}

QgsLayoutPoint QgsLayoutPageCollection::pagePositionToAbsolute( int page, const QgsLayoutPoint &position ) const
{
double vDelta = 0.0;
if ( page > 0 && page < mPages.count() )
{
vDelta = mLayout->convertFromLayoutUnits( mPages.at( page )->pos().y(), position.units() ).length();
}

return QgsLayoutPoint( position.x(), position.y() + vDelta, position.units() );
}

QPointF QgsLayoutPageCollection::positionOnPage( QPointF position ) const
{
double startCurrentPageY = 0;
Expand Down
12 changes: 12 additions & 0 deletions src/core/layout/qgslayoutpagecollection.h
Expand Up @@ -199,6 +199,18 @@ class CORE_EXPORT QgsLayoutPageCollection : public QObject, public QgsLayoutSeri
*/
QgsLayoutItemPage *pageAtPoint( QPointF point ) const;

/**
* Converts a \a position on a \a page to an absolute position in layout coordinates.\
* \see pagePositionToAbsolute()
*/
QPointF pagePositionToLayoutPosition( int page, const QgsLayoutPoint &position ) const;

/**
* Converts a \a position on a \a page to an absolute position in (maintaining the units from the input \a position).
* \see pagePositionToLayoutPosition()
*/
QgsLayoutPoint pagePositionToAbsolute( int page, const QgsLayoutPoint &position ) const;

/**
* Returns the position within a page of a \a point in the layout (in layout units).
*
Expand Down
10 changes: 3 additions & 7 deletions src/gui/layout/qgslayoutitemwidget.cpp
Expand Up @@ -331,7 +331,7 @@ void QgsLayoutItemPropertiesWidget::changeItemPosition()
mItem->layout()->undoStack()->beginCommand( mItem, tr( "Move Item" ), QgsLayoutItem::UndoIncrementalMove );

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

mItem->layout()->undoStack()->endCommand();
}
Expand Down Expand Up @@ -493,9 +493,7 @@ void QgsLayoutItemPropertiesWidget::setValuesForGuiPositionElements()
};
block( true );

QPointF pos; //TODO = mItem->pagePos();

QgsLayoutPoint point = mItem->positionWithUnits();
QgsLayoutPoint point = mItem->pagePositionWithUnits();

if ( !mFreezeXPosSpin )
mXPosSpin->setValue( point.x() );
Expand Down Expand Up @@ -571,10 +569,8 @@ void QgsLayoutItemPropertiesWidget::setValuesForGuiPositionElements()
mSizeLockAspectRatio->resetRatio();
mPosLockAspectRatio->resetRatio();

#if 0 //TODO
if ( !mFreezePageSpin )
mPageSpinBox->setValue( mItem->page() );
#endif
mPageSpinBox->setValue( mItem->page() + 1 );

block( false );
}
Expand Down

0 comments on commit c4c0c83

Please sign in to comment.