Skip to content

Commit

Permalink
Restore more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 5, 2018
1 parent ec67ddf commit 88a8390
Show file tree
Hide file tree
Showing 8 changed files with 447 additions and 28 deletions.
11 changes: 11 additions & 0 deletions python/core/layout/qgslayout.sip
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,17 @@ If ``includeTemplateUuids`` is true, then item's :py:func:`QgsLayoutItem.templat
will also be tested when trying to match the uuid.

.. seealso:: :py:func:`multiFrameByUuid()`

.. seealso:: :py:func:`itemById()`
%End

QgsLayoutItem *itemById( const QString &id ) const;
%Docstring
Returns a layout item given its ``id``.
Since item IDs are not necessarely unique, this function returns the first matching
item found.

.. seealso:: :py:func:`itemByUuid()`
%End

QgsLayoutMultiFrame *multiFrameByUuid( const QString &uuid ) const;
Expand Down
14 changes: 14 additions & 0 deletions src/core/layout/qgslayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,20 @@ QgsLayoutItem *QgsLayout::itemByUuid( const QString &uuid, bool includeTemplateU
return nullptr;
}

QgsLayoutItem *QgsLayout::itemById( const QString &id ) const
{
const QList<QGraphicsItem *> itemList = items();
for ( QGraphicsItem *item : itemList )
{
QgsLayoutItem *layoutItem = dynamic_cast<QgsLayoutItem *>( item );
if ( layoutItem && layoutItem->id() == id )
{
return layoutItem;
}
}
return nullptr;
}

QgsLayoutMultiFrame *QgsLayout::multiFrameByUuid( const QString &uuid ) const
{
for ( QgsLayoutMultiFrame *mf : mMultiFrames )
Expand Down
9 changes: 9 additions & 0 deletions src/core/layout/qgslayout.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,18 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
* will also be tested when trying to match the uuid.
*
* \see multiFrameByUuid()
* \see itemById()
*/
QgsLayoutItem *itemByUuid( const QString &uuid, bool includeTemplateUuids = false ) const;

/**
* Returns a layout item given its \a id.
* Since item IDs are not necessarely unique, this function returns the first matching
* item found.
* \see itemByUuid()
*/
QgsLayoutItem *itemById( const QString &id ) const;

/**
* Returns the layout multiframe with matching \a uuid unique identifier, or a nullptr
* if a matching multiframe could not be found.
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsexpressioncontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ class GetLayoutItemVariables : public QgsScopedExpressionFunction

QString id = values.at( 0 ).toString().toLower();

const QgsLayoutItem *item = mLayout->itemByUuid( id );
const QgsLayoutItem *item = mLayout->itemById( id );
if ( !item )
return QVariant();

Expand Down
24 changes: 23 additions & 1 deletion tests/src/core/testqgslayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class TestQgsLayout: public QObject
void addItem();
void layoutItems();
void layoutItemByUuid();
void layoutItemById();
void undoRedoOccurred();
void itemsOnPage(); //test fetching matching items on a set page
void shouldExportPage();
Expand Down Expand Up @@ -399,7 +400,6 @@ void TestQgsLayout::layoutItemByUuid()
{
QgsProject p;
QgsLayout l( &p );
l.pageCollection()->deletePage( 0 );

QgsLayoutItemShape *shape1 = new QgsLayoutItemShape( &l );
l.addLayoutItem( shape1 );
Expand All @@ -416,6 +416,28 @@ void TestQgsLayout::layoutItemByUuid()
QCOMPARE( l.itemByUuid( map1->uuid() ), map1 );
}

void TestQgsLayout::layoutItemById()
{
QgsProject p;
QgsLayout l( &p );

QgsLayoutItemShape *shape1 = new QgsLayoutItemShape( &l );
l.addLayoutItem( shape1 );
shape1->setId( QStringLiteral( "shape" ) );

QgsLayoutItemShape *shape2 = new QgsLayoutItemShape( &l );
l.addLayoutItem( shape2 );
shape2->setId( QStringLiteral( "shape" ) );

QgsLayoutItemMap *map1 = new QgsLayoutItemMap( &l );
l.addLayoutItem( map1 );
map1->setId( QStringLiteral( "map" ) );

QVERIFY( !l.itemById( QStringLiteral( "xxx" ) ) );
QVERIFY( l.itemById( QStringLiteral( "shape" ) ) == shape1 || l.itemById( QStringLiteral( "shape" ) ) == shape2 );
QCOMPARE( l.itemById( map1->id() ), map1 );
}

void TestQgsLayout::undoRedoOccurred()
{
// test emitting undo/redo occurred signal
Expand Down
6 changes: 3 additions & 3 deletions tests/src/core/testqgslayoutitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1399,15 +1399,15 @@ void TestQgsLayoutItem::itemVariablesFunction()
QVERIFY( !r.isValid() );

QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
map->setExtent( extent );
map->attemptSetSceneRect( QRectF( 30, 60, 200, 100 ) );
map->setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
map->attemptSetSceneRect( QRectF( 30, 60, 200, 100 ) );
map->setExtent( extent );
l.addLayoutItem( map );
map->setId( QStringLiteral( "map_id" ) );

c = l.createExpressionContext();
r = e.evaluate( &c );
QGSCOMPARENEAR( r.toDouble(), 1.38916e+08, 100 );
QGSCOMPARENEAR( r.toDouble(), 184764103, 100 );

QgsExpression e2( QStringLiteral( "map_get( item_variables( 'map_id' ), 'map_crs' )" ) );
r = e2.evaluate( &c );
Expand Down
Loading

0 comments on commit 88a8390

Please sign in to comment.