Skip to content
Permalink
Browse files

Refactor layout item gui registry to allow metadata for duplicate ite…

…m 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.
  • Loading branch information
nyalldawson committed Oct 16, 2017
1 parent f524c80 commit 8ca6d3e7173bb6009fec7d3fe4406f82e8e1f5fc
Showing with 461 additions and 508 deletions.
  1. +1 −1 python/core/layout/qgslayoutitemgroup.sip
  2. +1 −1 python/core/layout/qgslayoutitemmap.sip
  3. +1 −1 python/core/layout/qgslayoutitempage.sip
  4. +5 −7 python/core/layout/qgslayoutitemregistry.sip
  5. +26 −101 python/core/layout/qgslayoutitemshape.sip
  6. +29 −9 python/gui/layout/qgslayoutitemguiregistry.sip
  7. +11 −7 python/gui/layout/qgslayoutviewtooladditem.sip
  8. +21 −5 src/app/layout/qgslayoutapputils.cpp
  9. +14 −13 src/app/layout/qgslayoutdesignerdialog.cpp
  10. +2 −2 src/app/layout/qgslayoutdesignerdialog.h
  11. +1 −1 src/core/layout/qgslayoutitemgroup.cpp
  12. +1 −1 src/core/layout/qgslayoutitemgroup.h
  13. +1 −1 src/core/layout/qgslayoutitemmap.cpp
  14. +1 −1 src/core/layout/qgslayoutitemmap.h
  15. +1 −2 src/core/layout/qgslayoutitempage.cpp
  16. +1 −1 src/core/layout/qgslayoutitempage.h
  17. +9 −10 src/core/layout/qgslayoutitemregistry.cpp
  18. +7 −9 src/core/layout/qgslayoutitemregistry.h
  19. +65 −108 src/core/layout/qgslayoutitemshape.cpp
  20. +44 −116 src/core/layout/qgslayoutitemshape.h
  21. +38 −12 src/gui/layout/qgslayoutitemguiregistry.cpp
  22. +51 −13 src/gui/layout/qgslayoutitemguiregistry.h
  23. +8 −6 src/gui/layout/qgslayoutviewtooladditem.cpp
  24. +11 −7 src/gui/layout/qgslayoutviewtooladditem.h
  25. +9 −9 tests/src/core/testqgslayout.cpp
  26. +14 −14 tests/src/core/testqgslayoutitem.cpp
  27. +30 −30 tests/src/core/testqgslayoutitemgroup.cpp
  28. +7 −5 tests/src/core/testqgslayoutshapes.cpp
  29. +48 −12 tests/src/gui/testqgslayoutview.cpp
  30. +3 −3 tests/src/python/test_qgslayoutitem.py
@@ -33,7 +33,7 @@ class QgsLayoutItemGroup: QgsLayoutItem
virtual QString displayName() const;


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

@@ -29,7 +29,7 @@ class QgsLayoutItemMap : QgsLayoutItem
virtual QString stringType() const;


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

@@ -38,7 +38,7 @@ class QgsLayoutItemPage : QgsLayoutItem
Constructor for QgsLayoutItemPage, with the specified parent ``layout``.
%End

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

@@ -54,9 +54,9 @@ class QgsLayoutItemAbstractMetadata
:rtype: str
%End

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

@@ -103,9 +103,7 @@ class QgsLayoutItemRegistry : QObject
// known
LayoutPage,
LayoutMap,
LayoutRectangle,
LayoutEllipse,
LayoutTriangle,
LayoutShape,

// item
PluginItem,
@@ -144,9 +142,9 @@ class QgsLayoutItemRegistry : QObject
:rtype: bool
%End

QgsLayoutItem *createItem( int type, QgsLayout *layout, const QVariantMap &properties = QVariantMap() ) const /Factory/;
QgsLayoutItem *createItem( int type, QgsLayout *layout ) const /Factory/;
%Docstring
Creates a new instance of a layout item given the item ``type``, target ``layout`` and ``properties``.
Creates a new instance of a layout item given the item ``type``, and target ``layout``.
:rtype: QgsLayoutItem
%End

@@ -11,7 +11,7 @@
class QgsLayoutItemShape : QgsLayoutItem
{
%Docstring
Base class for layout items which are basic shapes (e.g. rectangles, ellipses).
Layout item for basic filled shapes (e.g. rectangles, ellipses).
.. versionadded:: 3.0
%End

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

void setSymbol( QgsFillSymbol *symbol );
%Docstring
Sets the fill ``symbol`` used to draw the shape. Ownership is not transferred
and a clone of the symbol is made.
.. seealso:: symbol()
%End

QgsFillSymbol *symbol();
%Docstring
Returns the fill symbol used to draw the shape.
.. seealso:: setSymbol()
:rtype: QgsFillSymbol
%End

virtual QRectF boundingRect() const;

enum Shape
{
Ellipse,
Rectangle,
Triangle
};

protected:

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

};

virtual int type() const;
virtual QString stringType() const;

class QgsLayoutItemRectangularShape : QgsLayoutItemShape
{
QgsLayoutItemShape::Shape shapeType() const;
%Docstring
A rectangular shape item for layouts.
.. versionadded:: 3.0
Returns the type of shape (e.g. rectangle, ellipse, etc).
.. seealso:: setShapeType()
:rtype: QgsLayoutItemShape.Shape
%End

%TypeHeaderCode
#include "qgslayoutitemshape.h"
void setShapeType( QgsLayoutItemShape::Shape type );
%Docstring
Sets the ``type`` of shape (e.g. rectangle, ellipse, etc).
.. seealso:: shapeType()
%End
public:

explicit QgsLayoutItemRectangularShape( QgsLayout *layout );
void setSymbol( QgsFillSymbol *symbol );
%Docstring
Constructor for QgsLayoutItemRectangularShape, with the specified parent ``layout``.
Sets the fill ``symbol`` used to draw the shape. Ownership is not transferred
and a clone of the symbol is made.
.. seealso:: symbol()
%End
virtual int type() const;
virtual QString stringType() const;

static QgsLayoutItemRectangularShape *create( QgsLayout *layout, const QVariantMap &settings ) /Factory/;
QgsFillSymbol *symbol();
%Docstring
Returns a new rectangular item for the specified ``layout``.

The caller takes responsibility for deleting the returned object.
:rtype: QgsLayoutItemRectangularShape
Returns the fill symbol used to draw the shape.
.. seealso:: setSymbol()
:rtype: QgsFillSymbol
%End

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

protected:

virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );


};

class QgsLayoutItemEllipseShape : QgsLayoutItemShape
{
%Docstring
A ellipse shape item for layouts.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgslayoutitemshape.h"
%End
public:

explicit QgsLayoutItemEllipseShape( QgsLayout *layout );
%Docstring
Constructor for QgsLayoutItemEllipseShape, with the specified parent ``layout``.
%End
virtual int type() const;
virtual QString stringType() const;

static QgsLayoutItemEllipseShape *create( QgsLayout *layout, const QVariantMap &settings ) /Factory/;
%Docstring
Returns a new ellipse item for the specified ``layout``.
virtual QRectF boundingRect() const;

The caller takes responsibility for deleting the returned object.
:rtype: QgsLayoutItemEllipseShape
%End

protected:

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

};

class QgsLayoutItemTriangleShape : QgsLayoutItemShape
{
%Docstring
A triangle shape item for layouts.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgslayoutitemshape.h"
%End
public:

explicit QgsLayoutItemTriangleShape( QgsLayout *layout );
%Docstring
Constructor for QgsLayoutItemTriangleShape, with the specified parent ``layout``.
%End
virtual int type() const;
virtual QString stringType() const;

static QgsLayoutItemTriangleShape *create( QgsLayout *layout, const QVariantMap &settings ) /Factory/;
%Docstring
Returns a new triangle item for the specified ``layout``.

The caller takes responsibility for deleting the returned object.
:rtype: QgsLayoutItemTriangleShape
%End

protected:

virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );


};

/************************************************************************
* This file has been generated automatically from *
@@ -35,10 +35,12 @@ class QgsLayoutItemAbstractGuiMetadata
typedef QFlags<QgsLayoutItemAbstractGuiMetadata::Flag> Flags;


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

``visibleName`` should be set to a translated, user visible name identifying the corresponding layout item.

An optional ``groupId`` can be set, which allows grouping of related layout item classes. See QgsLayoutItemGuiMetadata for details.
%End

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

QString visibleName() const;
%Docstring
Returns a translated, user visible name identifying the corresponding layout item.
:rtype: str
%End

virtual QIcon creationIcon() const;
%Docstring
Returns an icon representing creation of the layout item type.
@@ -81,6 +89,12 @@ class QgsLayoutItemAbstractGuiMetadata
:rtype: QgsLayoutViewRubberBand
%End

virtual QgsLayoutItem *createItem( QgsLayout *layout ) /Factory/;
%Docstring
Creates an instance of the corresponding item type.
:rtype: QgsLayoutItem
%End

};


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


QgsLayoutItemAbstractGuiMetadata *itemMetadata( int type ) const;
QgsLayoutItemAbstractGuiMetadata *itemMetadata( const QString &uuid ) const;
%Docstring
Returns the metadata for the specified item ``type``. Returns None if
a corresponding type was not found in the registry.
Returns the metadata for the specified item ``uuid``. Returns None if
a corresponding uuid was not found in the registry.
:rtype: QgsLayoutItemAbstractGuiMetadata
%End

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

QgsLayoutItem *createItem( const QString &uuid, QgsLayout *layout ) const /Factory/;
%Docstring
Creates a new instance of a layout item given the item metadata ``uuid``, target ``layout``.
:rtype: QgsLayoutItem
%End

QgsLayoutItemBaseWidget *createItemWidget( QgsLayoutItem *item ) const /Factory/;
%Docstring
Creates a new instance of a layout item configuration widget for the specified ``item``.
:rtype: QgsLayoutItemBaseWidget
%End


QList< int > itemTypes() const;
QList< QString > itemUuids() const;
%Docstring
Returns a list of available item types handled by the registry.
:rtype: list of int
Returns a list of available item metadata uuids handled by the registry.
:rtype: list of str
%End

signals:

void typeAdded( int type );
void typeAdded( const QString &uuid );
%Docstring
Emitted whenever a new item type is added to the registry, with the specified
``type``.
``uuid``.
%End

private:
@@ -22,17 +22,21 @@ class QgsLayoutViewToolAddItem : QgsLayoutViewTool

QgsLayoutViewToolAddItem( QgsLayoutView *view /TransferThis/ );

int itemType() const;
QString itemMetadataUuid() const;
%Docstring
Returns the item type for items created by the tool.
.. seealso:: setItemType()
:rtype: int
Returns the item metadata uuid for items created by the tool.
.. seealso:: setItemMetadataUuid()
:rtype: str
%End

void setItemType( int type );
void setItemMetadataUuid( const QString &uuid );
%Docstring
Sets the item ``type`` for items created by the tool.
.. seealso:: itemType()
Sets the item metadata ``uuid`` for items created by the tool.

The \uuid associates the current tool behavior with a metadata entry
from QgsLayoutItemGuiRegistry.

.. seealso:: itemMetadataUuid()
%End

virtual void layoutPressEvent( QgsLayoutViewMouseEvent *event );
@@ -18,6 +18,7 @@
#include "qgslayoutitemguiregistry.h"
#include "qgslayoutitemregistry.h"
#include "qgslayoutviewrubberband.h"
#include "qgslayoutitemshape.h"
#include "qgslayoutmapwidget.h"
#include "qgslayoutitemmap.h"

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

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

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

registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutRectangle, QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ), nullptr, createRubberBand, QStringLiteral( "shapes" ) ) );
registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutEllipse, QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicCircle.svg" ) ), nullptr, createEllipseBand, QStringLiteral( "shapes" ) ) );
registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutTriangle, QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicTriangle.svg" ) ), nullptr, createTriangleBand, QStringLiteral( "shapes" ) ) );
registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutShape, QObject::tr( "Rectangle" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ), nullptr, createRubberBand, QStringLiteral( "shapes" ), 0, []( QgsLayout * layout )->QgsLayoutItem*
{
std::unique_ptr< QgsLayoutItemShape > shape = qgis::make_unique< QgsLayoutItemShape >( layout );
shape->setShapeType( QgsLayoutItemShape::Rectangle );
return shape.release();
} ) );
registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutShape, QObject::tr( "Ellipse" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicCircle.svg" ) ), nullptr, createEllipseBand, QStringLiteral( "shapes" ), 0, []( QgsLayout * layout )->QgsLayoutItem*
{
std::unique_ptr< QgsLayoutItemShape > shape = qgis::make_unique< QgsLayoutItemShape >( layout );
shape->setShapeType( QgsLayoutItemShape::Ellipse );
return shape.release();
} ) );
registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutShape, QObject::tr( "Triangle" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicTriangle.svg" ) ), nullptr, createTriangleBand, QStringLiteral( "shapes" ), 0, []( QgsLayout * layout )->QgsLayoutItem*
{
std::unique_ptr< QgsLayoutItemShape > shape = qgis::make_unique< QgsLayoutItemShape >( layout );
shape->setShapeType( QgsLayoutItemShape::Triangle );
return shape.release();
} ) );
}

0 comments on commit 8ca6d3e

Please sign in to comment.
You can’t perform that action at this time.