Skip to content

Commit 3cf06db

Browse files
committedJul 25, 2017
Port item id and uuid code
1 parent a515e95 commit 3cf06db

File tree

5 files changed

+115
-9
lines changed

5 files changed

+115
-9
lines changed
 

‎python/core/layout/qgslayoutitem.sip

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,35 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem
5555
:rtype: str
5656
%End
5757

58+
QString uuid() const;
59+
%Docstring
60+
Returns the item identification string. This is a unique random string set for the item
61+
upon creation.
62+
.. note::
63+
64+
There is no corresponding setter for the uuid - it's created automatically.
65+
.. seealso:: id()
66+
.. seealso:: setId()
67+
:rtype: str
68+
%End
69+
70+
QString id() const;
71+
%Docstring
72+
Returns the item's ID name. This is not necessarily unique, and duplicate ID names may exist
73+
for a layout.
74+
.. seealso:: setId()
75+
.. seealso:: uuid()
76+
:rtype: str
77+
%End
78+
79+
virtual void setId( const QString &id );
80+
%Docstring
81+
Set the item's ``id`` name. This is not necessarily unique, and duplicate ID names may exist
82+
for a layout.
83+
.. seealso:: id()
84+
.. seealso:: uuid()
85+
%End
86+
5887
virtual void paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget *pWidget );
5988

6089
%Docstring

‎src/core/layout/qgslayoutitem.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
#include "qgslayoututils.h"
2020
#include <QPainter>
2121
#include <QStyleOptionGraphicsItem>
22+
#include <QUuid>
2223

2324
#define CACHE_SIZE_LIMIT 5000
2425

2526
QgsLayoutItem::QgsLayoutItem( QgsLayout *layout )
2627
: QgsLayoutObject( layout )
2728
, QGraphicsRectItem( 0 )
29+
, mUuid( QUuid::createUuid().toString() )
2830
{
2931
// needed to access current view transform during paint operations
3032
setFlags( flags() | QGraphicsItem::ItemUsesExtendedStyleOption );
@@ -38,6 +40,28 @@ QgsLayoutItem::QgsLayoutItem( QgsLayout *layout )
3840
initConnectionsToLayout();
3941
}
4042

43+
void QgsLayoutItem::setId( const QString &id )
44+
{
45+
if ( id == mId )
46+
{
47+
return;
48+
}
49+
50+
mId = id;
51+
setToolTip( id );
52+
53+
//TODO
54+
#if 0
55+
//inform model that id data has changed
56+
if ( mComposition )
57+
{
58+
mComposition->itemsModel()->updateItemDisplayName( this );
59+
}
60+
61+
emit itemChanged();
62+
#endif
63+
}
64+
4165
void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *itemStyle, QWidget * )
4266
{
4367
if ( !painter || !painter->device() )
@@ -410,8 +434,8 @@ QPointF QgsLayoutItem::positionAtReferencePoint( const QgsLayoutItem::ReferenceP
410434

411435
bool QgsLayoutItem::writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const
412436
{
413-
// element.setAttribute( "uuid", mUuid );
414-
// element.setAttribute( "id", mId );
437+
element.setAttribute( QStringLiteral( "uuid" ), mUuid );
438+
element.setAttribute( QStringLiteral( "id" ), mId );
415439
element.setAttribute( QStringLiteral( "referencePoint" ), QString::number( static_cast< int >( mReferencePoint ) ) );
416440
element.setAttribute( QStringLiteral( "position" ), mItemPosition.encodePoint() );
417441
element.setAttribute( QStringLiteral( "size" ), mItemSize.encodeSize() );
@@ -448,8 +472,8 @@ bool QgsLayoutItem::readPropertiesFromElement( const QDomElement &element, const
448472
{
449473
readObjectPropertiesFromElement( element, document, context );
450474

451-
// mUuid = element.attribute( "uuid", QUuid::createUuid().toString() );
452-
// setId( element.attribute( "id" ) );
475+
mUuid = element.attribute( QStringLiteral( "uuid" ), QUuid::createUuid().toString() );
476+
setId( element.attribute( QStringLiteral( "id" ) ) );
453477
mReferencePoint = static_cast< ReferencePoint >( element.attribute( QStringLiteral( "referencePoint" ) ).toInt() );
454478
attemptMove( QgsLayoutPoint::decodePoint( element.attribute( QStringLiteral( "position" ) ) ) );
455479
attemptResize( QgsLayoutSize::decodeSize( element.attribute( QStringLiteral( "size" ) ) ) );

‎src/core/layout/qgslayoutitem.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,31 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
7373
*/
7474
virtual QString stringType() const = 0;
7575

76+
/**
77+
* Returns the item identification string. This is a unique random string set for the item
78+
* upon creation.
79+
* \note There is no corresponding setter for the uuid - it's created automatically.
80+
* \see id()
81+
* \see setId()
82+
*/
83+
QString uuid() const { return mUuid; }
84+
85+
/**
86+
* Returns the item's ID name. This is not necessarily unique, and duplicate ID names may exist
87+
* for a layout.
88+
* \see setId()
89+
* \see uuid()
90+
*/
91+
QString id() const { return mId; }
92+
93+
/**
94+
* Set the item's \a id name. This is not necessarily unique, and duplicate ID names may exist
95+
* for a layout.
96+
* \see id()
97+
* \see uuid()
98+
*/
99+
virtual void setId( const QString &id );
100+
76101
/**
77102
* Handles preparing a paint surface for the layout item and painting the item's
78103
* content. Derived classes must not override this method, but instead implement
@@ -300,6 +325,12 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
300325

301326
private:
302327

328+
//! id (not necessarily unique)
329+
QString mId;
330+
331+
//! Unique id
332+
QString mUuid;
333+
303334
ReferencePoint mReferencePoint = UpperLeft;
304335
QgsLayoutSize mFixedSize;
305336
QgsLayoutSize mMinimumSize;

‎src/core/layout/qgslayoutobject.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ bool QgsLayoutObject::writeObjectPropertiesToElement( QDomElement &parentElement
131131
}
132132

133133
//create object element
134-
QDomElement objectElement = document.createElement( "LayoutObject" );
134+
QDomElement objectElement = document.createElement( QStringLiteral( "LayoutObject" ) );
135135

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

155-
QDomNodeList objectNodeList = parentElement.elementsByTagName( "LayoutObject" );
155+
QDomNodeList objectNodeList = parentElement.elementsByTagName( QStringLiteral( "LayoutObject" ) );
156156
if ( objectNodeList.size() < 1 )
157157
{
158158
return false;

‎tests/src/core/testqgslayoutitem.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class TestQgsLayoutItem: public QObject
3737
void init();// will be called before each testfunction is executed.
3838
void cleanup();// will be called after every testfunction.
3939
void creation(); //test creation of QgsLayoutItem
40+
void uuid();
41+
void id();
4042
void registry();
4143
void shouldDrawDebug();
4244
void shouldDrawAntialiased();
@@ -180,6 +182,26 @@ void TestQgsLayoutItem::creation()
180182
delete layout;
181183
}
182184

185+
void TestQgsLayoutItem::uuid()
186+
{
187+
QgsProject p;
188+
QgsLayout l( &p );
189+
190+
//basic test of uuid
191+
TestItem item( &l );
192+
TestItem item2( &l );
193+
QVERIFY( item.uuid() != item2.uuid() );
194+
}
195+
196+
void TestQgsLayoutItem::id()
197+
{
198+
QgsProject p;
199+
QgsLayout l( &p );
200+
TestItem item( &l );
201+
item.setId( QStringLiteral( "test" ) );
202+
QCOMPARE( item.id(), QStringLiteral( "test" ) );
203+
}
204+
183205
void TestQgsLayoutItem::registry()
184206
{
185207
// test QgsLayoutItemRegistry
@@ -1229,12 +1251,12 @@ void TestQgsLayoutItem::writeReadXmlProperties()
12291251
original->attemptResize( QgsLayoutSize( 6, 8, QgsUnitTypes::LayoutCentimeters ) );
12301252
original->attemptMove( QgsLayoutPoint( 0.05, 0.09, QgsUnitTypes::LayoutMeters ) );
12311253
original->setItemRotation( 45.0 );
1232-
//original->setId( QString( "test" ) );
1254+
original->setId( QStringLiteral( "test" ) );
12331255

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

1236-
//QCOMPARE( copy->uuid(), original->uuid() );
1237-
//QCOMPARE( copy->id(), original->id() );
1258+
QCOMPARE( copy->uuid(), original->uuid() );
1259+
QCOMPARE( copy->id(), original->id() );
12381260
QgsProperty dd = copy->dataDefinedProperties().property( QgsLayoutObject::TestProperty );
12391261
QVERIFY( dd );
12401262
QVERIFY( dd.isActive() );

0 commit comments

Comments
 (0)
Please sign in to comment.