Skip to content

Commit

Permalink
Move population of layout gui registry from GUI->app
Browse files Browse the repository at this point in the history
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 Nov 7, 2017
1 parent edea38f commit d3836e5
Show file tree
Hide file tree
Showing 14 changed files with 116 additions and 48 deletions.
8 changes: 8 additions & 0 deletions python/core/layout/qgslayoutitemmap.sip
Expand Up @@ -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 );
Expand Down
7 changes: 0 additions & 7 deletions python/gui/layout/qgslayoutitemguiregistry.sip
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/app/CMakeLists.txt
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions src/app/layout/qgslayoutapputils.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" ) ) );
}
36 changes: 36 additions & 0 deletions src/app/layout/qgslayoutapputils.h
@@ -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
7 changes: 7 additions & 0 deletions src/app/qgisapp.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/app/qgisapp.h
Expand Up @@ -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 );
Expand Down
5 changes: 5 additions & 0 deletions src/core/layout/qgslayoutitemmap.cpp
Expand Up @@ -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 * )
{

Expand Down
7 changes: 7 additions & 0 deletions src/core/layout/qgslayoutitemmap.h
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/core/layout/qgslayoutitemregistry.cpp
Expand Up @@ -16,6 +16,7 @@

#include "qgslayoutitemregistry.h"
#include "qgslayoutitemshape.h"
#include "qgslayoutitemmap.h"
#include "qgslayoutitempage.h"
#include "qgslayoutitemgroup.h"
#include "qgsgloweffect.h"
Expand Down Expand Up @@ -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 ) );
Expand Down
27 changes: 0 additions & 27 deletions src/gui/layout/qgslayoutitemguiregistry.cpp
Expand Up @@ -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 );
Expand Down
6 changes: 0 additions & 6 deletions src/gui/layout/qgslayoutitemguiregistry.h
Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion src/gui/qgsgui.cpp
Expand Up @@ -96,5 +96,4 @@ QgsGui::QgsGui()
mMapLayerActionRegistry = new QgsMapLayerActionRegistry();
mSourceSelectProviderRegistry = new QgsSourceSelectProviderRegistry();
mLayoutItemGuiRegistry = new QgsLayoutItemGuiRegistry();
mLayoutItemGuiRegistry->populate();
}
7 changes: 0 additions & 7 deletions tests/src/gui/testqgslayoutview.cpp
Expand Up @@ -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()
Expand Down

0 comments on commit d3836e5

Please sign in to comment.