Skip to content
Permalink
Browse files

Move population of layout gui registry from GUI->app

Since we don't want all the item type subclass config widgets
to have to reside in gui, we need to populate the registry
from app instead.
  • Loading branch information
nyalldawson committed Oct 10, 2017
1 parent edea38f commit d3836e502a6493d9c17aa5a1f3763e7beec19a69
@@ -29,6 +29,14 @@ class QgsLayoutItemMap : QgsLayoutItem
virtual QString stringType() const;


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

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

protected:

virtual void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = 0 );
@@ -159,13 +159,6 @@ class QgsLayoutItemGuiRegistry : QObject

~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
@@ -174,6 +174,7 @@ SET(QGIS_APP_SRCS
composer/qgsatlascompositionwidget.cpp

layout/qgslayoutaddpagesdialog.cpp
layout/qgslayoutapputils.cpp
layout/qgslayoutdesignerdialog.cpp
layout/qgslayoutguidewidget.cpp
layout/qgslayoutappmenuprovider.cpp
@@ -0,0 +1,48 @@
/***************************************************************************
qgslayoutapputils.cpp
---------------------
Date : October 2017
Copyright : (C) 2017 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgslayoutapputils.h"
#include "qgsgui.h"
#include "qgslayoutitemguiregistry.h"
#include "qgslayoutitemregistry.h"
#include "qgslayoutviewrubberband.h"

void QgsLayoutAppUtils::registerGuiForKnownItemTypes()
{
QgsLayoutItemGuiRegistry *registry = QgsGui::layoutItemGuiRegistry();

registry->addItemGroup( QgsLayoutItemGuiGroup( QStringLiteral( "shapes" ), QObject::tr( "Shape" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicShape.svg" ) ) ) );

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

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

registry->addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutMap, QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddMap.svg" ) ), nullptr, 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" ) ) );
}
@@ -0,0 +1,36 @@
/***************************************************************************
qgslayoutapputils.h
-------------------
Date : October 2017
Copyright : (C) 2017 Nyall Dawson
Email : nyall dot dawson at gmail dot com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSLAYOUTAPPUTILS_H
#define QGSLAYOUTAPPUTILS_H

#include "qgis.h"

/**
* Utils for layout handling from app.
*/
class QgsLayoutAppUtils
{

public:

/**
* Registers the GUI handlers for known layout item types.
*/
static void registerGuiForKnownItemTypes();

};

#endif // QGSLAYOUTAPPUTILS_H
@@ -206,6 +206,7 @@ Q_GUI_EXPORT extern int qt_defaultDpiX();
#include "qgslayertreeviewdefaultactions.h"
#include "qgslayoutdesignerdialog.h"
#include "qgslayoutmanager.h"
#include "qgslayoutapputils.h"
#include "qgslocatorwidget.h"
#include "qgslocator.h"
#include "qgsinbuiltlocatorfilters.h"
@@ -835,6 +836,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
functionProfile( &QgisApp::legendLayerSelectionChanged, this, QStringLiteral( "Legend layer selection changed" ) );
functionProfile( &QgisApp::init3D, this, QStringLiteral( "Initialize 3D support" ) );
functionProfile( &QgisApp::initNativeProcessing, this, QStringLiteral( "Initialize native processing" ) );
functionProfile( &QgisApp::initLayouts, this, QStringLiteral( "Initialize layouts support" ) );

QgsApplication::annotationRegistry()->addAnnotationType( QgsAnnotationMetadata( QStringLiteral( "FormAnnotationItem" ), &QgsFormAnnotation::create ) );
connect( QgsProject::instance()->annotationManager(), &QgsAnnotationManager::annotationAdded, this, &QgisApp::annotationCreated );
@@ -10149,6 +10151,11 @@ void QgisApp::initNativeProcessing()
QgsApplication::processingRegistry()->addProvider( new QgsNativeAlgorithms( QgsApplication::processingRegistry() ) );
}

void QgisApp::initLayouts()
{
QgsLayoutAppUtils::registerGuiForKnownItemTypes();
}

void QgisApp::new3DMapCanvas()
{
#ifdef HAVE_3D
@@ -1752,6 +1752,7 @@ class APP_EXPORT QgisApp : public QMainWindow, private Ui::MainWindow
void createDecorations();
void init3D();
void initNativeProcessing();
void initLayouts();

//! Creates a new 3D map dock without initializing its position or contents
Qgs3DMapCanvasDockWidget *createNew3DMapCanvasDock( const QString &name );
@@ -34,6 +34,11 @@ QString QgsLayoutItemMap::stringType() const
return QStringLiteral( "ItemMap" );
}

QgsLayoutItemMap *QgsLayoutItemMap::create( QgsLayout *layout, const QVariantMap & )
{
return new QgsLayoutItemMap( layout );
}

void QgsLayoutItemMap::draw( QgsRenderContext &, const QStyleOptionGraphicsItem * )
{

@@ -41,6 +41,13 @@ class CORE_EXPORT QgsLayoutItemMap : public QgsLayoutItem
int type() const override;
QString stringType() const override;

/**
* Returns a new map item for the specified \a layout.
*
* The caller takes responsibility for deleting the returned object.
*/
static QgsLayoutItemMap *create( QgsLayout *layout, const QVariantMap &settings ) SIP_FACTORY;

protected:

void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override;
@@ -16,6 +16,7 @@

#include "qgslayoutitemregistry.h"
#include "qgslayoutitemshape.h"
#include "qgslayoutitemmap.h"
#include "qgslayoutitempage.h"
#include "qgslayoutitemgroup.h"
#include "qgsgloweffect.h"
@@ -48,6 +49,8 @@ bool QgsLayoutItemRegistry::populate()

addLayoutItemType( new QgsLayoutItemMetadata( LayoutPage, QStringLiteral( "Page" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionFileNew.svg" ) ), QgsLayoutItemPage::create ) );

addLayoutItemType( new QgsLayoutItemMetadata( LayoutMap, QStringLiteral( "Map" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddMap.svg" ) ), QgsLayoutItemMap::create ) );

addLayoutItemType( new QgsLayoutItemMetadata( LayoutRectangle, QStringLiteral( "Rectangle" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ), QgsLayoutItemRectangularShape::create ) );
addLayoutItemType( new QgsLayoutItemMetadata( LayoutEllipse, QStringLiteral( "Ellipse" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicCircle.svg" ) ), QgsLayoutItemEllipseShape::create ) );
addLayoutItemType( new QgsLayoutItemMetadata( LayoutTriangle, QStringLiteral( "Triangle" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicTriangle.svg" ) ), QgsLayoutItemTriangleShape::create ) );
@@ -36,33 +36,6 @@ QgsLayoutItemGuiRegistry::~QgsLayoutItemGuiRegistry()
qDeleteAll( mMetadata );
}

bool QgsLayoutItemGuiRegistry::populate()
{
if ( !mMetadata.isEmpty() )
return false;

addItemGroup( QgsLayoutItemGuiGroup( QStringLiteral( "shapes" ), tr( "Shape" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicShape.svg" ) ) ) );

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

addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutItem + 1002, QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddLabel.svg" ) ), nullptr, createRubberBand ) );
addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutRectangle, QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ), nullptr, createRubberBand, QStringLiteral( "shapes" ) ) );
addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutEllipse, QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicCircle.svg" ) ), nullptr, createEllipseBand, QStringLiteral( "shapes" ) ) );
addLayoutItemGuiMetadata( new QgsLayoutItemGuiMetadata( QgsLayoutItemRegistry::LayoutTriangle, QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicTriangle.svg" ) ), nullptr, createTriangleBand, QStringLiteral( "shapes" ) ) );
return true;
}

QgsLayoutItemAbstractGuiMetadata *QgsLayoutItemGuiRegistry::itemMetadata( int type ) const
{
return mMetadata.value( type );
@@ -250,12 +250,6 @@ class GUI_EXPORT QgsLayoutItemGuiRegistry : public QObject

~QgsLayoutItemGuiRegistry();

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

//! QgsLayoutItemGuiRegistry cannot be copied.
QgsLayoutItemGuiRegistry( const QgsLayoutItemGuiRegistry &rh ) = delete;
//! QgsLayoutItemGuiRegistry cannot be copied.
@@ -96,5 +96,4 @@ QgsGui::QgsGui()
mMapLayerActionRegistry = new QgsMapLayerActionRegistry();
mSourceSelectProviderRegistry = new QgsSourceSelectProviderRegistry();
mLayoutItemGuiRegistry = new QgsLayoutItemGuiRegistry();
mLayoutItemGuiRegistry->populate();
}
@@ -309,13 +309,6 @@ void TestQgsLayoutView::guiRegistry()
QCOMPARE( registry.itemGroup( QStringLiteral( "g1" ) ).id, QStringLiteral( "g1" ) );
// can't add duplicate group
QVERIFY( !registry.addItemGroup( QgsLayoutItemGuiGroup( QStringLiteral( "g1" ) ) ) );

//test populate
QgsLayoutItemGuiRegistry reg2;
QVERIFY( reg2.itemTypes().isEmpty() );
QVERIFY( reg2.populate() );
QVERIFY( !reg2.itemTypes().isEmpty() );
QVERIFY( !reg2.populate() );
}

void TestQgsLayoutView::rubberBand()

0 comments on commit d3836e5

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