Skip to content
Permalink
Browse files

Split layout item registry into two separate registries

Instead of relying on forward declared c++ classes from
gui in QgsLayoutItemRegistry, instead create a
QgsLayoutItemGuiRegistry which handles registration
of all the GUI specific behavior relating to layout items.

Remove all GUI related code from QgsLayoutItemRegistry.

This creates a cleaner split between core/gui code, and
given that there'll be a lot of gui specific behavior
which needs to be handled by a registry it makes sense
to keep this isolated in gui.

It also plays nicer with the sip bindings, which can't
handle forward declared gui classes in core.
  • Loading branch information
nyalldawson committed Jul 11, 2017
1 parent 0307cac commit cf488d34a474d7629289ddf12321037e63842a1b
@@ -13,9 +13,13 @@ class QgsLayoutItemAbstractMetadata
{
%Docstring
Stores metadata about one layout item class.

A companion class, QgsLayoutItemAbstractGuiMetadata, handles the
GUI behavior of QgsLayoutItems.

.. note::

In C++ you can use QgsSymbolLayerMetadata convenience class.
In C++ you can use QgsLayoutItemMetadata convenience class.
.. versionadded:: 3.0
%End

@@ -56,13 +60,6 @@ class QgsLayoutItemAbstractMetadata
:rtype: QgsLayoutItem
%End

virtual QWidget *createItemWidget() /Factory/;
%Docstring
Creates a configuration widget for layout items of this type. Can return None if no configuration GUI is required.
:rtype: QWidget
%End


virtual void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving );
%Docstring
Resolve paths in the item's ``properties`` (if there are any paths).
@@ -79,8 +76,6 @@ class QgsLayoutItemAbstractMetadata





class QgsLayoutItemRegistry : QObject
{
%Docstring
@@ -89,6 +84,9 @@ class QgsLayoutItemRegistry : QObject
QgsLayoutItemRegistry is not usually directly created, but rather accessed through
QgsApplication.layoutItemRegistry().

A companion class, QgsLayoutItemGuiRegistry, handles the GUI behavior
of layout items.

.. versionadded:: 3.0
%End

@@ -147,13 +145,6 @@ class QgsLayoutItemRegistry : QObject
:rtype: QgsLayoutItem
%End

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


void resolvePaths( int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) const;
%Docstring
Resolve paths in properties of a particular symbol layer.
@@ -42,7 +42,6 @@
%Include editorwidgets/core/qgseditorwidgetautoconf.sip
%Include layertree/qgslayertreeembeddedconfigwidget.sip
%Include layertree/qgslayertreeembeddedwidgetregistry.sip
%Include layout/qgslayoutitemregistryguiutils.sip
%Include layout/qgslayoutviewmouseevent.sip
%Include layout/qgslayoutviewrubberband.sip
%Include locator/qgslocatorcontext.sip
@@ -278,6 +277,7 @@
%Include layertree/qgslayertreeview.sip
%Include layertree/qgslayertreeviewdefaultactions.sip
%Include layout/qgslayoutdesignerinterface.sip
%Include layout/qgslayoutitemguiregistry.sip
%Include layout/qgslayoutview.sip
%Include layout/qgslayoutviewtool.sip
%Include layout/qgslayoutviewtooladditem.sip
@@ -0,0 +1,154 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/layout/qgslayoutitemguiregistry.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/




class QgsLayoutItemAbstractGuiMetadata
{
%Docstring
Stores GUI metadata about one layout item class.

This is a companion to QgsLayoutItemAbstractMetadata, storing only
the components related to the GUI behavior of a layout item.

.. note::

In C++ you can use QgsLayoutItemGuiMetadata convenience class.
.. versionadded:: 3.0
%End

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

QgsLayoutItemAbstractGuiMetadata( int type );
%Docstring
Constructor for QgsLayoutItemAbstractGuiMetadata with the specified class ``type``.
%End

virtual ~QgsLayoutItemAbstractGuiMetadata();

int type() const;
%Docstring
Returns the unique item type code for the layout item class.
:rtype: int
%End

virtual QIcon creationIcon() const;
%Docstring
Returns an icon representing creation of the layout item type.
:rtype: QIcon
%End

virtual QWidget *createItemWidget() /Factory/;
%Docstring
Creates a configuration widget for layout items of this type. Can return None if no configuration GUI is required.
:rtype: QWidget
%End

virtual QgsLayoutViewRubberBand *createRubberBand( QgsLayoutView *view ) /Factory/;
%Docstring
Creates a rubber band for use when creating layout items of this type. Can return None if no rubber band
should be created. The default behavior is to create a rectangular rubber band.
:rtype: QgsLayoutViewRubberBand
%End

};




class QgsLayoutItemGuiRegistry : QObject
{
%Docstring
Registry of available layout item GUI behavior.

QgsLayoutItemGuiRegistry is not usually directly created, but rather accessed through
QgsGui.layoutItemGuiRegistry().

This acts as a companion to QgsLayoutItemRegistry, handling only
the components related to the GUI behavior of layout items.

.. versionadded:: 3.0
%End

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

QgsLayoutItemGuiRegistry( QObject *parent = 0 );
%Docstring
Creates a new empty item GUI registry.

QgsLayoutItemGuiRegistry is not usually directly created, but rather accessed through
QgsGui.layoutItemGuiRegistry().

.. seealso:: populate()
%End

~QgsLayoutItemGuiRegistry();

bool populate();
%Docstring
Populates the registry with standard item types. If called on a non-empty registry
then this will have no effect and will return false.
:rtype: bool
%End


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

bool addLayoutItemGuiMetadata( QgsLayoutItemAbstractGuiMetadata *metadata /Transfer/ );
%Docstring
Registers the gui metadata for a new layout item type. Takes ownership of the metadata instance.
:rtype: bool
%End

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


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

signals:

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

private:
QgsLayoutItemGuiRegistry( const QgsLayoutItemGuiRegistry &rh );
};




/************************************************************************
* This file has been generated automatically from *
* *
* src/gui/layout/qgslayoutitemguiregistry.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
@@ -56,6 +56,12 @@ class QgsGui
:rtype: QgsMapLayerActionRegistry
%End

static QgsLayoutItemGuiRegistry *layoutItemGuiRegistry();
%Docstring
Returns the global layout item GUI registry, used for registering the GUI behavior of layout items.
:rtype: QgsLayoutItemGuiRegistry
%End

~QgsGui();

private:
@@ -26,6 +26,8 @@
#include "qgslayoutviewtoolpan.h"
#include "qgslayoutviewtoolzoom.h"
#include "qgslayoutviewtoolselect.h"
#include "qgsgui.h"
#include "qgslayoutitemguiregistry.h"

QgsAppLayoutDesignerInterface::QgsAppLayoutDesignerInterface( QgsLayoutDesignerDialog *dialog )
: QgsLayoutDesignerInterface( dialog )
@@ -89,14 +91,12 @@ QgsLayoutDesignerDialog::QgsLayoutDesignerDialog( QWidget *parent, Qt::WindowFla
connect( mActionClose, &QAction::triggered, this, &QWidget::close );

// populate with initial items...
QMap< int, QString> types = QgsApplication::layoutItemRegistry()->itemTypes();
QMap< int, QString>::const_iterator typeIt = types.constBegin();
for ( ; typeIt != types.constEnd(); ++typeIt )
Q_FOREACH ( int type, QgsGui::layoutItemGuiRegistry()->itemTypes() )
{
itemTypeAdded( typeIt.key(), typeIt.value() );
itemTypeAdded( type );
}
//..and listen out for new item types
connect( QgsApplication::layoutItemRegistry(), &QgsLayoutItemRegistry::typeAdded, this, &QgsLayoutDesignerDialog::itemTypeAdded );
connect( QgsGui::layoutItemGuiRegistry(), &QgsLayoutItemGuiRegistry::typeAdded, this, &QgsLayoutDesignerDialog::itemTypeAdded );

mAddItemTool = new QgsLayoutViewToolAddItem( mView );
mPanTool = new QgsLayoutViewToolPan( mView );
@@ -192,14 +192,15 @@ void QgsLayoutDesignerDialog::closeEvent( QCloseEvent * )
saveWindowState();
}

void QgsLayoutDesignerDialog::itemTypeAdded( int type, const QString &name )
void QgsLayoutDesignerDialog::itemTypeAdded( int type )
{
QString name = QgsApplication::layoutItemRegistry()->itemMetadata( type )->visibleName();
// update UI for new item type
QAction *action = new QAction( tr( "Add %1" ).arg( name ), this );
action->setToolTip( tr( "Adds a new %1 to the layout" ).arg( name ) );
action->setCheckable( true );
action->setData( type );
action->setIcon( QgsApplication::layoutItemRegistry()->itemMetadata( type )->icon() );
action->setIcon( QgsGui::layoutItemGuiRegistry()->itemMetadata( type )->creationIcon() );
mToolsActionGroup->addAction( action );
mItemMenu->addAction( action );
mToolsToolbar->addAction( action );
@@ -115,7 +115,7 @@ class QgsLayoutDesignerDialog: public QMainWindow, private Ui::QgsLayoutDesigner

private slots:

void itemTypeAdded( int type, const QString &name );
void itemTypeAdded( int type );

private:

@@ -65,25 +65,6 @@ QgsLayoutItem *QgsLayoutItemRegistry::createItem( int type, QgsLayout *layout, c
return mMetadata[type]->createItem( layout, properties );
}

QWidget *QgsLayoutItemRegistry::createItemWidget( int type ) const
{
if ( !mMetadata.contains( type ) )
return nullptr;

return mMetadata[type]->createItemWidget();
}

QgsLayoutViewRubberBand *QgsLayoutItemRegistry::createItemRubberBand( int type, QgsLayoutView *view ) const
{
if ( mRubberBandFunctions.contains( type ) )
return mRubberBandFunctions.value( type )( view );

if ( !mMetadata.contains( type ) )
return nullptr;

return mMetadata[type]->createRubberBand( view );
}

void QgsLayoutItemRegistry::resolvePaths( int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) const
{
if ( !mMetadata.contains( type ) )

0 comments on commit cf488d3

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