Skip to content

Commit

Permalink
Add a signal when an item is added to a layout
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 9, 2021
1 parent 6bd24e5 commit 309a2a6
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions python/core/auto_generated/layout/qgslayout.sip.in
Expand Up @@ -663,6 +663,13 @@ and updated.
Emitted whenever the ``total`` number of background tasks running in items from the layout changes.

.. versionadded:: 3.10
%End

void itemAdded( QgsLayoutItem *item );
%Docstring
Emitted when an ``item`` was added to the layout.

.. versionadded:: 3.20
%End

};
Expand Down
1 change: 1 addition & 0 deletions src/core/layout/qgslayout.cpp
Expand Up @@ -872,6 +872,7 @@ void QgsLayout::addLayoutItemPrivate( QgsLayoutItem *item )
updateBounds();
mItemsModel->rebuildZList();
connect( item, &QgsLayoutItem::backgroundTaskCountChanged, this, &QgsLayout::itemBackgroundTaskCountChanged );
emit itemAdded( item );
}

void QgsLayout::removeLayoutItemPrivate( QgsLayoutItem *item )
Expand Down
7 changes: 7 additions & 0 deletions src/core/layout/qgslayout.h
Expand Up @@ -713,6 +713,13 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
*/
void backgroundTaskCountChanged( int total );

/**
* Emitted when an \a item was added to the layout.
*
* \since QGIS 3.20
*/
void itemAdded( QgsLayoutItem *item );

private slots:
void itemBackgroundTaskCountChanged( int count );

Expand Down
3 changes: 3 additions & 0 deletions tests/src/core/testqgslayout.cpp
Expand Up @@ -383,7 +383,10 @@ void TestQgsLayout::addItem()
shape1->attemptResize( QgsLayoutSize( 140, 70 ) );
shape1->attemptMove( QgsLayoutPoint( 90, 50 ) );

QSignalSpy itemAddedSpy( &l, &QgsLayout::itemAdded );
l.addLayoutItem( shape1 );
QCOMPARE( itemAddedSpy.count(), 1 );
QCOMPARE( itemAddedSpy.at( 0 ).at( 0 ).value< QgsLayoutItem * >(), shape1 );
QVERIFY( l.items().contains( shape1 ) );
// bounds should be updated to include item
QGSCOMPARENEAR( l.sceneRect().left(), 89.850, 0.001 );
Expand Down

0 comments on commit 309a2a6

Please sign in to comment.