Skip to content

Commit

Permalink
Port methods to retrieve layout items
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 4, 2017
1 parent 3bc9de5 commit 66e1cf0
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 0 deletions.
8 changes: 8 additions & 0 deletions python/core/layout/qgslayout.sip
Expand Up @@ -67,6 +67,14 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator
.. seealso:: name() .. seealso:: name()
%End %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 ); void setUnits( QgsUnitTypes::LayoutUnit units );
%Docstring %Docstring
Sets the native measurement ``units`` for the layout. These also form the default unit Sets the native measurement ``units`` for the layout. These also form the default unit
Expand Down
15 changes: 15 additions & 0 deletions src/core/layout/qgslayout.cpp
Expand Up @@ -48,6 +48,21 @@ QgsProject *QgsLayout::project() const
return mProject; 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 double QgsLayout::convertToLayoutUnits( const QgsLayoutMeasurement &measurement ) const
{ {
return mContext.measurementConverter().convert( measurement, mUnits ).length(); return mContext.measurementConverter().convert( measurement, mUnits ).length();
Expand Down
25 changes: 25 additions & 0 deletions src/core/layout/qgslayout.h
Expand Up @@ -86,6 +86,31 @@ class CORE_EXPORT QgsLayout : public QGraphicsScene, public QgsExpressionContext
*/ */
void setName( const QString &name ) { mName = name; } 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 * Sets the native measurement \a units for the layout. These also form the default unit
* for measurements for the layout. * for measurements for the layout.
Expand Down
57 changes: 57 additions & 0 deletions tests/src/core/testqgslayout.cpp
Expand Up @@ -39,6 +39,8 @@ class TestQgsLayout: public QObject
void referenceMap(); void referenceMap();
void bounds(); void bounds();
void addItem(); void addItem();
void layoutItems();
void layoutItemByUuid();


private: private:
QString mReport; QString mReport;
Expand Down Expand Up @@ -360,6 +362,61 @@ void TestQgsLayout::addItem()
QGSCOMPARENEAR( l.sceneRect().height(), 171, 0.001 ); 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 ) QGSTEST_MAIN( TestQgsLayout )
#include "testqgslayout.moc" #include "testqgslayout.moc"

0 comments on commit 66e1cf0

Please sign in to comment.