Skip to content
Permalink
Browse files

Port methods to retrieve layout items

  • Loading branch information
nyalldawson committed Jul 28, 2017
1 parent 3bc9de5 commit 66e1cf04e6d573e226a816aa34e1ad5944fb7bd1
Showing with 105 additions and 0 deletions.
  1. +8 −0 python/core/layout/qgslayout.sip
  2. +15 −0 src/core/layout/qgslayout.cpp
  3. +25 −0 src/core/layout/qgslayout.h
  4. +57 −0 tests/src/core/testqgslayout.cpp
@@ -67,6 +67,14 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator
.. seealso:: name()
%End


QgsLayoutItem *itemByUuid( const QString &uuid );
%Docstring
Returns the layout item with matching ``uuid`` unique identifier, or a None
if a matching item could not be found.
:rtype: QgsLayoutItem
%End

void setUnits( QgsUnitTypes::LayoutUnit units );
%Docstring
Sets the native measurement ``units`` for the layout. These also form the default unit
@@ -48,6 +48,21 @@ QgsProject *QgsLayout::project() const
return mProject;
}

QgsLayoutItem *QgsLayout::itemByUuid( const QString &uuid )
{
QList<QgsLayoutItem *> itemList;
layoutItems( itemList );
Q_FOREACH ( QgsLayoutItem *item, itemList )
{
if ( item->uuid() == uuid )
{
return item;
}
}

return nullptr;
}

double QgsLayout::convertToLayoutUnits( const QgsLayoutMeasurement &measurement ) const
{
return mContext.measurementConverter().convert( measurement, mUnits ).length();
@@ -86,6 +86,31 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
*/
void setName( const QString &name ) { mName = name; }

/**
* Returns a list of layout items of a specific type.
* \note not available in Python bindings
*/
template<class T> void layoutItems( QList<T *> &itemList ) SIP_SKIP
{
itemList.clear();
QList<QGraphicsItem *> graphicsItemList = items();
QList<QGraphicsItem *>::iterator itemIt = graphicsItemList.begin();
for ( ; itemIt != graphicsItemList.end(); ++itemIt )
{
T *item = dynamic_cast<T *>( *itemIt );
if ( item )
{
itemList.push_back( item );
}
}
}

/**
* Returns the layout item with matching \a uuid unique identifier, or a nullptr
* if a matching item could not be found.
*/
QgsLayoutItem *itemByUuid( const QString &uuid );

/**
* Sets the native measurement \a units for the layout. These also form the default unit
* for measurements for the layout.
@@ -39,6 +39,8 @@ class TestQgsLayout: public QObject
void referenceMap();
void bounds();
void addItem();
void layoutItems();
void layoutItemByUuid();

private:
QString mReport;
@@ -360,6 +362,61 @@ void TestQgsLayout::addItem()
QGSCOMPARENEAR( l.sceneRect().height(), 171, 0.001 );
}

void TestQgsLayout::layoutItems()
{
QgsProject p;
QgsLayout l( &p );
l.pageCollection()->deletePage( 0 );

QgsLayoutItemRectangularShape *shape1 = new QgsLayoutItemRectangularShape( &l );
l.addLayoutItem( shape1 );

QgsLayoutItemRectangularShape *shape2 = new QgsLayoutItemRectangularShape( &l );
l.addLayoutItem( shape2 );

QgsLayoutItemMap *map1 = new QgsLayoutItemMap( &l );
l.addLayoutItem( map1 );

QList< QgsLayoutItem * > items;
l.layoutItems( items );
QCOMPARE( items.count(), 3 );
QVERIFY( items.contains( shape1 ) );
QVERIFY( items.contains( shape2 ) );
QVERIFY( items.contains( map1 ) );

QList< QgsLayoutItemRectangularShape * > shapes;
l.layoutItems( shapes );
QCOMPARE( shapes.count(), 2 );
QVERIFY( shapes.contains( shape1 ) );
QVERIFY( shapes.contains( shape2 ) );

QList< QgsLayoutItemMap * > maps;
l.layoutItems( maps );
QCOMPARE( maps.count(), 1 );
QVERIFY( maps.contains( map1 ) );
}

void TestQgsLayout::layoutItemByUuid()
{
QgsProject p;
QgsLayout l( &p );
l.pageCollection()->deletePage( 0 );

QgsLayoutItemRectangularShape *shape1 = new QgsLayoutItemRectangularShape( &l );
l.addLayoutItem( shape1 );

QgsLayoutItemRectangularShape *shape2 = new QgsLayoutItemRectangularShape( &l );
l.addLayoutItem( shape2 );

QgsLayoutItemMap *map1 = new QgsLayoutItemMap( &l );
l.addLayoutItem( map1 );

QVERIFY( !l.itemByUuid( QStringLiteral( "xxx" ) ) );
QCOMPARE( l.itemByUuid( shape1->uuid() ), shape1 );
QCOMPARE( l.itemByUuid( shape2->uuid() ), shape2 );
QCOMPARE( l.itemByUuid( map1->uuid() ), map1 );
}


QGSTEST_MAIN( TestQgsLayout )
#include "testqgslayout.moc"

0 comments on commit 66e1cf0

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