Skip to content
Permalink
Browse files

Refine item widget creation methods in layout item gui registry

  • Loading branch information
nyalldawson committed Jul 22, 2017
1 parent 72bf292 commit 0f90e23fe6f45974c676ba70e5c523b397fcecdf
@@ -55,10 +55,10 @@ class QgsLayoutItemAbstractGuiMetadata
:rtype: QIcon
%End

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

virtual QgsLayoutViewRubberBand *createRubberBand( QgsLayoutView *view ) /Factory/;
@@ -186,10 +186,10 @@ class QgsLayoutItemGuiRegistry : QObject
:rtype: QgsLayoutItemGuiGroup
%End

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


@@ -92,12 +92,15 @@ const QgsLayoutItemGuiGroup &QgsLayoutItemGuiRegistry::itemGroup( const QString
return mItemGroups[ id ];
}

QWidget *QgsLayoutItemGuiRegistry::createItemWidget( int type ) const
QgsLayoutItemBaseWidget *QgsLayoutItemGuiRegistry::createItemWidget( QgsLayoutItem *item ) const
{
if ( !mMetadata.contains( type ) )
if ( !item )
return nullptr;

if ( !mMetadata.contains( item->type() ) )
return nullptr;

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

QgsLayoutViewRubberBand *QgsLayoutItemGuiRegistry::createItemRubberBand( int type, QgsLayoutView *view ) const
@@ -30,6 +30,7 @@ class QgsLayout;
class QgsLayoutView;
class QgsLayoutItem;
class QgsLayoutViewRubberBand;
class QgsLayoutItemBaseWidget;

/**
* \ingroup gui
@@ -73,9 +74,9 @@ class GUI_EXPORT QgsLayoutItemAbstractGuiMetadata
virtual QIcon creationIcon() const { return QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ); }

/**
* Creates a configuration widget for layout items of this type. Can return nullptr if no configuration GUI is required.
* Creates a configuration widget for an \a item of this type. Can return nullptr if no configuration GUI is required.
*/
virtual QWidget *createItemWidget() SIP_FACTORY { return nullptr; }
virtual QgsLayoutItemBaseWidget *createItemWidget( QgsLayoutItem *item ) SIP_FACTORY { Q_UNUSED( item ); return nullptr; }

/**
* Creates a rubber band for use when creating layout items of this type. Can return nullptr if no rubber band
@@ -91,7 +92,7 @@ class GUI_EXPORT QgsLayoutItemAbstractGuiMetadata
};

//! Layout item configuration widget creation function
typedef std::function<QWidget *()> QgsLayoutItemWidgetFunc SIP_SKIP;
typedef std::function<QgsLayoutItemBaseWidget *( QgsLayoutItem * )> QgsLayoutItemWidgetFunc SIP_SKIP;

//! Layout rubber band creation function
typedef std::function<QgsLayoutViewRubberBand *( QgsLayoutView * )> QgsLayoutItemRubberBandFunc SIP_SKIP;
@@ -149,7 +150,7 @@ class GUI_EXPORT QgsLayoutItemGuiMetadata : public QgsLayoutItemAbstractGuiMetad
void setRubberBandCreationFunction( QgsLayoutItemRubberBandFunc function ) { mRubberBandFunc = function; }

QIcon creationIcon() const override { return mIcon.isNull() ? QgsLayoutItemAbstractGuiMetadata::creationIcon() : mIcon; }
QWidget *createItemWidget() override { return mWidgetFunc ? mWidgetFunc() : nullptr; }
QgsLayoutItemBaseWidget *createItemWidget( QgsLayoutItem *item ) override { return mWidgetFunc ? mWidgetFunc( item ) : nullptr; }
QgsLayoutViewRubberBand *createRubberBand( QgsLayoutView *view ) override { return mRubberBandFunc ? mRubberBandFunc( view ) : nullptr; }

protected:
@@ -275,9 +276,9 @@ class GUI_EXPORT QgsLayoutItemGuiRegistry : public QObject
const QgsLayoutItemGuiGroup &itemGroup( const QString &id );

/**
* Creates a new instance of a layout item configuration widget for the specified item \a type.
* Creates a new instance of a layout item configuration widget for the specified \a item.
*/
QWidget *createItemWidget( int type ) const SIP_FACTORY;
QgsLayoutItemBaseWidget *createItemWidget( QgsLayoutItem *item ) const SIP_FACTORY;

/**
* Creates a new rubber band item for the specified item \a type and destination \a view.
@@ -22,6 +22,7 @@
#include "qgslayoutviewrubberband.h"
#include "qgslayoutitemregistry.h"
#include "qgslayoutitemguiregistry.h"
#include "qgslayoutitemwidget.h"
#include "qgstestutils.h"
#include "qgsproject.h"
#include "qgsgui.h"
@@ -242,6 +243,7 @@ class TestItem : public QgsLayoutItem

//implement pure virtual methods
int type() const override { return QgsLayoutItemRegistry::LayoutItem + 101; }
QString stringType() const override { return QStringLiteral( "testitem" ); }
void draw( QgsRenderContext &, const QStyleOptionGraphicsItem * = nullptr ) override
{ }
};
@@ -259,42 +261,45 @@ void TestQgsLayoutView::guiRegistry()
// empty registry
QVERIFY( !registry.itemMetadata( -1 ) );
QVERIFY( registry.itemTypes().isEmpty() );
QVERIFY( !registry.createItemWidget( 1 ) );
QVERIFY( !registry.createItemWidget( nullptr ) );
QVERIFY( !registry.createItemWidget( nullptr ) );
TestItem *testItem = new TestItem( nullptr );
QVERIFY( !registry.createItemWidget( testItem ) ); // not in registry

QSignalSpy spyTypeAdded( &registry, &QgsLayoutItemGuiRegistry::typeAdded );

// add a dummy item to registry
auto createWidget = []()->QWidget*
auto createWidget = []( QgsLayoutItem * item )->QgsLayoutItemBaseWidget*
{
return new QWidget();
return new QgsLayoutItemBaseWidget( nullptr, item );
};

auto createRubberBand = []( QgsLayoutView * view )->QgsLayoutViewRubberBand *
{
return new QgsLayoutViewRectangularRubberBand( view );
};

QgsLayoutItemGuiMetadata *metadata = new QgsLayoutItemGuiMetadata( 2, QIcon(), createWidget, createRubberBand );
QgsLayoutItemGuiMetadata *metadata = new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutItem + 101, QIcon(), createWidget, createRubberBand );
QVERIFY( registry.addLayoutItemGuiMetadata( metadata ) );
QCOMPARE( spyTypeAdded.count(), 1 );
QCOMPARE( spyTypeAdded.value( 0 ).at( 0 ).toInt(), 2 );
QCOMPARE( spyTypeAdded.value( 0 ).at( 0 ).toInt(), QgsLayoutItemRegistry::LayoutItem + 101 );
// duplicate type id
QVERIFY( !registry.addLayoutItemGuiMetadata( metadata ) );
QCOMPARE( spyTypeAdded.count(), 1 );

//retrieve metadata
QVERIFY( !registry.itemMetadata( -1 ) );
QVERIFY( registry.itemMetadata( 2 ) );
QVERIFY( registry.itemMetadata( QgsLayoutItemRegistry::LayoutItem + 101 ) );
QCOMPARE( registry.itemTypes().count(), 1 );
QCOMPARE( registry.itemTypes().value( 0 ), 2 );
QCOMPARE( registry.itemTypes().value( 0 ), QgsLayoutItemRegistry::LayoutItem + 101 );

QWidget *widget = registry.createItemWidget( 2 );
QWidget *widget = registry.createItemWidget( testItem );
QVERIFY( widget );
delete widget;

QgsLayoutView *view = new QgsLayoutView();
//should use metadata's method
QgsLayoutViewRubberBand *band = registry.createItemRubberBand( 2, view );
QgsLayoutViewRubberBand *band = registry.createItemRubberBand( QgsLayoutItemRegistry::LayoutItem + 101, view );
QVERIFY( band );
QVERIFY( dynamic_cast< QgsLayoutViewRectangularRubberBand * >( band ) );
QCOMPARE( band->view(), view );

0 comments on commit 0f90e23

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