Port item id and uuid code

This commit is contained in:
Nyall Dawson 2017-07-18 15:56:34 +10:00
parent a515e953b8
commit 3cf06db467
5 changed files with 115 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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

View File

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