Skip to content

Commit

Permalink
Refactor layout item gui registry to allow metadata for duplicate ite…
Browse files Browse the repository at this point in the history
…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 Nov 7, 2017
1 parent f524c80 commit 8ca6d3e
Show file tree
Hide file tree
Showing 30 changed files with 461 additions and 508 deletions.
2 changes: 1 addition & 1 deletion python/core/layout/qgslayoutitemgroup.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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``.

Expand Down
2 changes: 1 addition & 1 deletion python/core/layout/qgslayoutitemmap.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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``.

Expand Down
2 changes: 1 addition & 1 deletion python/core/layout/qgslayoutitempage.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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``.

Expand Down
12 changes: 5 additions & 7 deletions python/core/layout/qgslayoutitemregistry.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

// item
PluginItem,
Expand Down Expand Up @@ -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

Expand Down
127 changes: 26 additions & 101 deletions python/core/layout/qgslayoutitemshape.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 );
Expand All @@ -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:

Expand All @@ -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 *
Expand Down
38 changes: 29 additions & 9 deletions python/gui/layout/qgslayoutitemguiregistry.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand All @@ -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

};


Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
18 changes: 11 additions & 7 deletions python/gui/layout/qgslayoutviewtooladditem.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
26 changes: 21 additions & 5 deletions src/app/layout/qgslayoutapputils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "qgslayoutitemguiregistry.h"
#include "qgslayoutitemregistry.h"
#include "qgslayoutviewrubberband.h"
#include "qgslayoutitemshape.h"
#include "qgslayoutmapwidget.h"
#include "qgslayoutitemmap.h"

Expand All @@ -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();
} ) );
}
Loading

0 comments on commit 8ca6d3e

Please sign in to comment.