Skip to content

Commit

Permalink
Port item id and uuid code
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 25, 2017
1 parent a515e95 commit 3cf06db
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 9 deletions.
29 changes: 29 additions & 0 deletions python/core/layout/qgslayoutitem.sip
Expand Up @@ -55,6 +55,35 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem
:rtype: str
%End

QString uuid() const;
%Docstring
Returns the item identification string. This is a unique random string set for the item
upon creation.
.. note::

There is no corresponding setter for the uuid - it's created automatically.
.. seealso:: id()
.. seealso:: setId()
:rtype: str
%End

QString id() const;
%Docstring
Returns the item's ID name. This is not necessarily unique, and duplicate ID names may exist
for a layout.
.. seealso:: setId()
.. seealso:: uuid()
:rtype: str
%End

virtual void setId( const QString &id );
%Docstring
Set the item's ``id`` name. This is not necessarily unique, and duplicate ID names may exist
for a layout.
.. seealso:: id()
.. seealso:: uuid()
%End

virtual void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget );

%Docstring
Expand Down
32 changes: 28 additions & 4 deletions src/core/layout/qgslayoutitem.cpp
Expand Up @@ -19,12 +19,14 @@
#include "qgslayoututils.h"
#include <QPainter>
#include <QStyleOptionGraphicsItem>
#include <QUuid>

#define CACHE_SIZE_LIMIT 5000

QgsLayoutItem::QgsLayoutItem( QgsLayout *layout )
: QgsLayoutObject( layout )
, QGraphicsRectItem( 0 )
, mUuid( QUuid::createUuid().toString() )
{
// needed to access current view transform during paint operations
setFlags( flags() | QGraphicsItem::ItemUsesExtendedStyleOption );
Expand All @@ -38,6 +40,28 @@ QgsLayoutItem::QgsLayoutItem( QgsLayout *layout )
initConnectionsToLayout();
}

void QgsLayoutItem::setId( const QString &id )
{
if ( id == mId )
{
return;
}

mId = id;
setToolTip( id );

//TODO
#if 0
//inform model that id data has changed
if ( mComposition )
{
mComposition->itemsModel()->updateItemDisplayName( this );
}

emit itemChanged();
#endif
}

void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget * )
{
if ( !painter || !painter->device() )
Expand Down Expand Up @@ -410,8 +434,8 @@ QPointF QgsLayoutItem::positionAtReferencePoint( const QgsLayoutItem::ReferenceP

bool QgsLayoutItem::writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
{
// element.setAttribute( "uuid", mUuid );
// element.setAttribute( "id", mId );
element.setAttribute( QStringLiteral( "uuid" ), mUuid );
element.setAttribute( QStringLiteral( "id" ), mId );
element.setAttribute( QStringLiteral( "referencePoint" ), QString::number( static_cast< int >( mReferencePoint ) ) );
element.setAttribute( QStringLiteral( "position" ), mItemPosition.encodePoint() );
element.setAttribute( QStringLiteral( "size" ), mItemSize.encodeSize() );
Expand Down Expand Up @@ -448,8 +472,8 @@ bool QgsLayoutItem::readPropertiesFromElement( const QDomElement &element, const
{
readObjectPropertiesFromElement( element, document, context );

// mUuid = element.attribute( "uuid", QUuid::createUuid().toString() );
// setId( element.attribute( "id" ) );
mUuid = element.attribute( QStringLiteral( "uuid" ), QUuid::createUuid().toString() );
setId( element.attribute( QStringLiteral( "id" ) ) );
mReferencePoint = static_cast< ReferencePoint >( element.attribute( QStringLiteral( "referencePoint" ) ).toInt() );
attemptMove( QgsLayoutPoint::decodePoint( element.attribute( QStringLiteral( "position" ) ) ) );
attemptResize( QgsLayoutSize::decodeSize( element.attribute( QStringLiteral( "size" ) ) ) );
Expand Down
31 changes: 31 additions & 0 deletions src/core/layout/qgslayoutitem.h
Expand Up @@ -73,6 +73,31 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
*/
virtual QString stringType() const = 0;

/**
* Returns the item identification string. This is a unique random string set for the item
* upon creation.
* \note There is no corresponding setter for the uuid - it's created automatically.
* \see id()
* \see setId()
*/
QString uuid() const { return mUuid; }

/**
* Returns the item's ID name. This is not necessarily unique, and duplicate ID names may exist
* for a layout.
* \see setId()
* \see uuid()
*/
QString id() const { return mId; }

/**
* Set the item's \a id name. This is not necessarily unique, and duplicate ID names may exist
* for a layout.
* \see id()
* \see uuid()
*/
virtual void setId( const QString &id );

/**
* Handles preparing a paint surface for the layout item and painting the item's
* content. Derived classes must not override this method, but instead implement
Expand Down Expand Up @@ -300,6 +325,12 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt

private:

//! id (not necessarily unique)
QString mId;

//! Unique id
QString mUuid;

ReferencePoint mReferencePoint = UpperLeft;
QgsLayoutSize mFixedSize;
QgsLayoutSize mMinimumSize;
Expand Down
4 changes: 2 additions & 2 deletions src/core/layout/qgslayoutobject.cpp
Expand Up @@ -131,7 +131,7 @@ bool QgsLayoutObject::writeObjectPropertiesToElement( QDomElement &parentElement
}

//create object element
QDomElement objectElement = document.createElement( "LayoutObject" );
QDomElement objectElement = document.createElement( QStringLiteral( "LayoutObject" ) );

QDomElement ddPropsElement = document.createElement( QStringLiteral( "dataDefinedProperties" ) );
mDataDefinedProperties.writeXml( ddPropsElement, sPropertyDefinitions );
Expand All @@ -152,7 +152,7 @@ bool QgsLayoutObject::readObjectPropertiesFromElement( const QDomElement &parent
return false;
}

QDomNodeList objectNodeList = parentElement.elementsByTagName( "LayoutObject" );
QDomNodeList objectNodeList = parentElement.elementsByTagName( QStringLiteral( "LayoutObject" ) );
if ( objectNodeList.size() < 1 )
{
return false;
Expand Down
28 changes: 25 additions & 3 deletions tests/src/core/testqgslayoutitem.cpp
Expand Up @@ -37,6 +37,8 @@ class TestQgsLayoutItem: public QObject
void init();// will be called before each testfunction is executed.
void cleanup();// will be called after every testfunction.
void creation(); //test creation of QgsLayoutItem
void uuid();
void id();
void registry();
void shouldDrawDebug();
void shouldDrawAntialiased();
Expand Down Expand Up @@ -180,6 +182,26 @@ void TestQgsLayoutItem::creation()
delete layout;
}

void TestQgsLayoutItem::uuid()
{
QgsProject p;
QgsLayout l( &p );

//basic test of uuid
TestItem item( &l );
TestItem item2( &l );
QVERIFY( item.uuid() != item2.uuid() );
}

void TestQgsLayoutItem::id()
{
QgsProject p;
QgsLayout l( &p );
TestItem item( &l );
item.setId( QStringLiteral( "test" ) );
QCOMPARE( item.id(), QStringLiteral( "test" ) );
}

void TestQgsLayoutItem::registry()
{
// test QgsLayoutItemRegistry
Expand Down Expand Up @@ -1229,12 +1251,12 @@ void TestQgsLayoutItem::writeReadXmlProperties()
original->attemptResize( QgsLayoutSize( 6, 8, QgsUnitTypes::LayoutCentimeters ) );
original->attemptMove( QgsLayoutPoint( 0.05, 0.09, QgsUnitTypes::LayoutMeters ) );
original->setItemRotation( 45.0 );
//original->setId( QString( "test" ) );
original->setId( QStringLiteral( "test" ) );

QgsLayoutItem *copy = createCopyViaXml( &l, original );

//QCOMPARE( copy->uuid(), original->uuid() );
//QCOMPARE( copy->id(), original->id() );
QCOMPARE( copy->uuid(), original->uuid() );
QCOMPARE( copy->id(), original->id() );
QgsProperty dd = copy->dataDefinedProperties().property( QgsLayoutObject::TestProperty );
QVERIFY( dd );
QVERIFY( dd.isActive() );
Expand Down

0 comments on commit 3cf06db

Please sign in to comment.