Skip to content

Commit 8ca6d3e

Browse files
committed
Refactor layout item gui registry to allow metadata for duplicate item types
This allows registration of item creation actions which create items with an existing item type, but custom creation functions. E.g. allowing actions for creation of ellipse/triangles/etc from a single layout item type.
1 parent f524c80 commit 8ca6d3e

30 files changed

+461
-508
lines changed

python/core/layout/qgslayoutitemgroup.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class QgsLayoutItemGroup: QgsLayoutItem
3333
virtual QString displayName() const;
3434

3535

36-
static QgsLayoutItemGroup *create( QgsLayout *layout, const QVariantMap &settings ) /Factory/;
36+
static QgsLayoutItemGroup *create( QgsLayout *layout ) /Factory/;
3737
%Docstring
3838
Returns a new group item for the specified ``layout``.
3939

python/core/layout/qgslayoutitemmap.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class QgsLayoutItemMap : QgsLayoutItem
2929
virtual QString stringType() const;
3030

3131

32-
static QgsLayoutItemMap *create( QgsLayout *layout, const QVariantMap &settings ) /Factory/;
32+
static QgsLayoutItemMap *create( QgsLayout *layout ) /Factory/;
3333
%Docstring
3434
Returns a new map item for the specified ``layout``.
3535

python/core/layout/qgslayoutitempage.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class QgsLayoutItemPage : QgsLayoutItem
3838
Constructor for QgsLayoutItemPage, with the specified parent ``layout``.
3939
%End
4040

41-
static QgsLayoutItemPage *create( QgsLayout *layout, const QVariantMap &settings ) /Factory/;
41+
static QgsLayoutItemPage *create( QgsLayout *layout ) /Factory/;
4242
%Docstring
4343
Returns a new page item for the specified ``layout``.
4444

python/core/layout/qgslayoutitemregistry.sip

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ class QgsLayoutItemAbstractMetadata
5454
:rtype: str
5555
%End
5656

57-
virtual QgsLayoutItem *createItem( QgsLayout *layout, const QVariantMap &properties ) = 0 /Factory/;
57+
virtual QgsLayoutItem *createItem( QgsLayout *layout ) = 0 /Factory/;
5858
%Docstring
59-
Creates a layout item of this class for a specified ``layout``, given the map of ``properties``.
59+
Creates a layout item of this class for a specified ``layout``.
6060
:rtype: QgsLayoutItem
6161
%End
6262

@@ -103,9 +103,7 @@ class QgsLayoutItemRegistry : QObject
103103
// known
104104
LayoutPage,
105105
LayoutMap,
106-
LayoutRectangle,
107-
LayoutEllipse,
108-
LayoutTriangle,
106+
LayoutShape,
109107

110108
// item
111109
PluginItem,
@@ -144,9 +142,9 @@ class QgsLayoutItemRegistry : QObject
144142
:rtype: bool
145143
%End
146144

147-
QgsLayoutItem *createItem( int type, QgsLayout *layout, const QVariantMap &properties = QVariantMap() ) const /Factory/;
145+
QgsLayoutItem *createItem( int type, QgsLayout *layout ) const /Factory/;
148146
%Docstring
149-
Creates a new instance of a layout item given the item ``type``, target ``layout`` and ``properties``.
147+
Creates a new instance of a layout item given the item ``type``, and target ``layout``.
150148
:rtype: QgsLayoutItem
151149
%End
152150

python/core/layout/qgslayoutitemshape.sip

Lines changed: 26 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class QgsLayoutItemShape : QgsLayoutItem
1212
{
1313
%Docstring
14-
Base class for layout items which are basic shapes (e.g. rectangles, ellipses).
14+
Layout item for basic filled shapes (e.g. rectangles, ellipses).
1515
.. versionadded:: 3.0
1616
%End
1717

@@ -20,58 +20,47 @@ class QgsLayoutItemShape : QgsLayoutItem
2020
%End
2121
public:
2222

23-
void setSymbol( QgsFillSymbol *symbol );
24-
%Docstring
25-
Sets the fill ``symbol`` used to draw the shape. Ownership is not transferred
26-
and a clone of the symbol is made.
27-
.. seealso:: symbol()
28-
%End
29-
30-
QgsFillSymbol *symbol();
31-
%Docstring
32-
Returns the fill symbol used to draw the shape.
33-
.. seealso:: setSymbol()
34-
:rtype: QgsFillSymbol
35-
%End
36-
37-
virtual QRectF boundingRect() const;
38-
23+
enum Shape
24+
{
25+
Ellipse,
26+
Rectangle,
27+
Triangle
28+
};
3929

40-
protected:
4130

4231
explicit QgsLayoutItemShape( QgsLayout *layout );
4332
%Docstring
4433
Constructor for QgsLayoutItemShape, with the specified parent ``layout``.
4534
%End
4635

47-
};
48-
36+
virtual int type() const;
37+
virtual QString stringType() const;
4938

50-
class QgsLayoutItemRectangularShape : QgsLayoutItemShape
51-
{
39+
QgsLayoutItemShape::Shape shapeType() const;
5240
%Docstring
53-
A rectangular shape item for layouts.
54-
.. versionadded:: 3.0
41+
Returns the type of shape (e.g. rectangle, ellipse, etc).
42+
.. seealso:: setShapeType()
43+
:rtype: QgsLayoutItemShape.Shape
5544
%End
5645

57-
%TypeHeaderCode
58-
#include "qgslayoutitemshape.h"
46+
void setShapeType( QgsLayoutItemShape::Shape type );
47+
%Docstring
48+
Sets the ``type`` of shape (e.g. rectangle, ellipse, etc).
49+
.. seealso:: shapeType()
5950
%End
60-
public:
6151

62-
explicit QgsLayoutItemRectangularShape( QgsLayout *layout );
52+
void setSymbol( QgsFillSymbol *symbol );
6353
%Docstring
64-
Constructor for QgsLayoutItemRectangularShape, with the specified parent ``layout``.
54+
Sets the fill ``symbol`` used to draw the shape. Ownership is not transferred
55+
and a clone of the symbol is made.
56+
.. seealso:: symbol()
6557
%End
66-
virtual int type() const;
67-
virtual QString stringType() const;
6858

69-
static QgsLayoutItemRectangularShape *create( QgsLayout *layout, const QVariantMap &settings ) /Factory/;
59+
QgsFillSymbol *symbol();
7060
%Docstring
71-
Returns a new rectangular item for the specified ``layout``.
72-
73-
The caller takes responsibility for deleting the returned object.
74-
:rtype: QgsLayoutItemRectangularShape
61+
Returns the fill symbol used to draw the shape.
62+
.. seealso:: setSymbol()
63+
:rtype: QgsFillSymbol
7564
%End
7665

7766
void setCornerRadius( QgsLayoutMeasurement radius );
@@ -87,39 +76,8 @@ class QgsLayoutItemRectangularShape : QgsLayoutItemShape
8776
:rtype: QgsLayoutMeasurement
8877
%End
8978

90-
protected:
91-
92-
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
93-
94-
95-
};
96-
97-
class QgsLayoutItemEllipseShape : QgsLayoutItemShape
98-
{
99-
%Docstring
100-
A ellipse shape item for layouts.
101-
.. versionadded:: 3.0
102-
%End
103-
104-
%TypeHeaderCode
105-
#include "qgslayoutitemshape.h"
106-
%End
107-
public:
108-
109-
explicit QgsLayoutItemEllipseShape( QgsLayout *layout );
110-
%Docstring
111-
Constructor for QgsLayoutItemEllipseShape, with the specified parent ``layout``.
112-
%End
113-
virtual int type() const;
114-
virtual QString stringType() const;
115-
116-
static QgsLayoutItemEllipseShape *create( QgsLayout *layout, const QVariantMap &settings ) /Factory/;
117-
%Docstring
118-
Returns a new ellipse item for the specified ``layout``.
79+
virtual QRectF boundingRect() const;
11980

120-
The caller takes responsibility for deleting the returned object.
121-
:rtype: QgsLayoutItemEllipseShape
122-
%End
12381

12482
protected:
12583

@@ -128,39 +86,6 @@ class QgsLayoutItemEllipseShape : QgsLayoutItemShape
12886

12987
};
13088

131-
class QgsLayoutItemTriangleShape : QgsLayoutItemShape
132-
{
133-
%Docstring
134-
A triangle shape item for layouts.
135-
.. versionadded:: 3.0
136-
%End
137-
138-
%TypeHeaderCode
139-
#include "qgslayoutitemshape.h"
140-
%End
141-
public:
142-
143-
explicit QgsLayoutItemTriangleShape( QgsLayout *layout );
144-
%Docstring
145-
Constructor for QgsLayoutItemTriangleShape, with the specified parent ``layout``.
146-
%End
147-
virtual int type() const;
148-
virtual QString stringType() const;
149-
150-
static QgsLayoutItemTriangleShape *create( QgsLayout *layout, const QVariantMap &settings ) /Factory/;
151-
%Docstring
152-
Returns a new triangle item for the specified ``layout``.
153-
154-
The caller takes responsibility for deleting the returned object.
155-
:rtype: QgsLayoutItemTriangleShape
156-
%End
157-
158-
protected:
159-
160-
virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
161-
162-
163-
};
16489

16590
/************************************************************************
16691
* This file has been generated automatically from *

python/gui/layout/qgslayoutitemguiregistry.sip

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ class QgsLayoutItemAbstractGuiMetadata
3535
typedef QFlags<QgsLayoutItemAbstractGuiMetadata::Flag> Flags;
3636

3737

38-
QgsLayoutItemAbstractGuiMetadata( int type, const QString &groupId = QString(), Flags flags = 0 );
38+
QgsLayoutItemAbstractGuiMetadata( int type, const QString &visibleName, const QString &groupId = QString(), Flags flags = 0 );
3939
%Docstring
4040
Constructor for QgsLayoutItemAbstractGuiMetadata with the specified class ``type``.
4141

42+
``visibleName`` should be set to a translated, user visible name identifying the corresponding layout item.
43+
4244
An optional ``groupId`` can be set, which allows grouping of related layout item classes. See QgsLayoutItemGuiMetadata for details.
4345
%End
4446

@@ -62,6 +64,12 @@ class QgsLayoutItemAbstractGuiMetadata
6264
:rtype: str
6365
%End
6466

67+
QString visibleName() const;
68+
%Docstring
69+
Returns a translated, user visible name identifying the corresponding layout item.
70+
:rtype: str
71+
%End
72+
6573
virtual QIcon creationIcon() const;
6674
%Docstring
6775
Returns an icon representing creation of the layout item type.
@@ -81,6 +89,12 @@ class QgsLayoutItemAbstractGuiMetadata
8189
:rtype: QgsLayoutViewRubberBand
8290
%End
8391

92+
virtual QgsLayoutItem *createItem( QgsLayout *layout ) /Factory/;
93+
%Docstring
94+
Creates an instance of the corresponding item type.
95+
:rtype: QgsLayoutItem
96+
%End
97+
8498
};
8599

86100

@@ -160,10 +174,10 @@ class QgsLayoutItemGuiRegistry : QObject
160174
~QgsLayoutItemGuiRegistry();
161175

162176

163-
QgsLayoutItemAbstractGuiMetadata *itemMetadata( int type ) const;
177+
QgsLayoutItemAbstractGuiMetadata *itemMetadata( const QString &uuid ) const;
164178
%Docstring
165-
Returns the metadata for the specified item ``type``. Returns None if
166-
a corresponding type was not found in the registry.
179+
Returns the metadata for the specified item ``uuid``. Returns None if
180+
a corresponding uuid was not found in the registry.
167181
:rtype: QgsLayoutItemAbstractGuiMetadata
168182
%End
169183

@@ -192,25 +206,31 @@ class QgsLayoutItemGuiRegistry : QObject
192206
:rtype: QgsLayoutItemGuiGroup
193207
%End
194208

209+
QgsLayoutItem *createItem( const QString &uuid, QgsLayout *layout ) const /Factory/;
210+
%Docstring
211+
Creates a new instance of a layout item given the item metadata ``uuid``, target ``layout``.
212+
:rtype: QgsLayoutItem
213+
%End
214+
195215
QgsLayoutItemBaseWidget *createItemWidget( QgsLayoutItem *item ) const /Factory/;
196216
%Docstring
197217
Creates a new instance of a layout item configuration widget for the specified ``item``.
198218
:rtype: QgsLayoutItemBaseWidget
199219
%End
200220

201221

202-
QList< int > itemTypes() const;
222+
QList< QString > itemUuids() const;
203223
%Docstring
204-
Returns a list of available item types handled by the registry.
205-
:rtype: list of int
224+
Returns a list of available item metadata uuids handled by the registry.
225+
:rtype: list of str
206226
%End
207227

208228
signals:
209229

210-
void typeAdded( int type );
230+
void typeAdded( const QString &uuid );
211231
%Docstring
212232
Emitted whenever a new item type is added to the registry, with the specified
213-
``type``.
233+
``uuid``.
214234
%End
215235

216236
private:

python/gui/layout/qgslayoutviewtooladditem.sip

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,21 @@ class QgsLayoutViewToolAddItem : QgsLayoutViewTool
2222

2323
QgsLayoutViewToolAddItem( QgsLayoutView *view /TransferThis/ );
2424

25-
int itemType() const;
25+
QString itemMetadataUuid() const;
2626
%Docstring
27-
Returns the item type for items created by the tool.
28-
.. seealso:: setItemType()
29-
:rtype: int
27+
Returns the item metadata uuid for items created by the tool.
28+
.. seealso:: setItemMetadataUuid()
29+
:rtype: str
3030
%End
3131

32-
void setItemType( int type );
32+
void setItemMetadataUuid( const QString &uuid );
3333
%Docstring
34-
Sets the item ``type`` for items created by the tool.
35-
.. seealso:: itemType()
34+
Sets the item metadata ``uuid`` for items created by the tool.
35+
36+
The \uuid associates the current tool behavior with a metadata entry
37+
from QgsLayoutItemGuiRegistry.
38+
39+
.. seealso:: itemMetadataUuid()
3640
%End
3741

3842
virtual void layoutPressEvent( QgsLayoutViewMouseEvent *event );

src/app/layout/qgslayoutapputils.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qgslayoutitemguiregistry.h"
1919
#include "qgslayoutitemregistry.h"
2020
#include "qgslayoutviewrubberband.h"
21+
#include "qgslayoutitemshape.h"
2122
#include "qgslayoutmapwidget.h"
2223
#include "qgslayoutitemmap.h"
2324

@@ -40,15 +41,30 @@ void QgsLayoutAppUtils::registerGuiForKnownItemTypes()
4041
return new QgsLayoutViewTriangleRubberBand( view );
4142
} );
4243

43-
registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutItem + 1002, QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddLabel.svg" ) ), nullptr, createRubberBand ) );
44+
registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutItem + 1002, QStringLiteral( "test" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddLabel.svg" ) ), nullptr, createRubberBand ) );
4445

45-
registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutMap, QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddMap.svg" ) ),
46+
registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutMap, QObject::tr( "Map" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddMap.svg" ) ),
4647
[ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
4748
{
4849
return new QgsLayoutMapWidget( qobject_cast< QgsLayoutItemMap * >( item ) );
4950
}, createRubberBand ) );
5051

51-
registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutRectangle, QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ), nullptr, createRubberBand, QStringLiteral( "shapes" ) ) );
52-
registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutEllipse, QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicCircle.svg" ) ), nullptr, createEllipseBand, QStringLiteral( "shapes" ) ) );
53-
registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutTriangle, QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicTriangle.svg" ) ), nullptr, createTriangleBand, QStringLiteral( "shapes" ) ) );
52+
registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutShape, QObject::tr( "Rectangle" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ), nullptr, createRubberBand, QStringLiteral( "shapes" ), 0, []( QgsLayout * layout )->QgsLayoutItem*
53+
{
54+
std::unique_ptr< QgsLayoutItemShape > shape = qgis::make_unique< QgsLayoutItemShape >( layout );
55+
shape->setShapeType( QgsLayoutItemShape::Rectangle );
56+
return shape.release();
57+
} ) );
58+
registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutShape, QObject::tr( "Ellipse" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicCircle.svg" ) ), nullptr, createEllipseBand, QStringLiteral( "shapes" ), 0, []( QgsLayout * layout )->QgsLayoutItem*
59+
{
60+
std::unique_ptr< QgsLayoutItemShape > shape = qgis::make_unique< QgsLayoutItemShape >( layout );
61+
shape->setShapeType( QgsLayoutItemShape::Ellipse );
62+
return shape.release();
63+
} ) );
64+
registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutShape, QObject::tr( "Triangle" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicTriangle.svg" ) ), nullptr, createTriangleBand, QStringLiteral( "shapes" ), 0, []( QgsLayout * layout )->QgsLayoutItem*
65+
{
66+
std::unique_ptr< QgsLayoutItemShape > shape = qgis::make_unique< QgsLayoutItemShape >( layout );
67+
shape->setShapeType( QgsLayoutItemShape::Triangle );
68+
return shape.release();
69+
} ) );
5470
}

0 commit comments

Comments
 (0)