Port methods to retrieve layout items

This commit is contained in:
Nyall Dawson 2017-07-28 15:54:17 +10:00
parent 3bc9de51fd
commit 66e1cf04e6
4 changed files with 105 additions and 0 deletions

View File

@ -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

View File

@ -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();

View File

@ -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.

View File

@ -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"