Skip to content
Permalink
Browse files

Restore more unit tests

  • Loading branch information
nyalldawson committed Dec 28, 2017
1 parent ec67ddf commit 88a839061d20501432b7138210bc995898967ca8
@@ -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;
@@ -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 )
@@ -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.
@@ -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();

@@ -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();
@@ -399,7 +400,6 @@ void TestQgsLayout::layoutItemByUuid()
{
QgsProject p;
QgsLayout l( &p );
l.pageCollection()->deletePage( 0 );

QgsLayoutItemShape *shape1 = new QgsLayoutItemShape( &l );
l.addLayoutItem( shape1 );
@@ -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
@@ -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 );

0 comments on commit 88a8390

Please sign in to comment.
You can’t perform that action at this time.