Skip to content

Commit cf488d3

Browse files
committedJul 11, 2017
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.
1 parent 0307cac commit cf488d3

18 files changed

+580
-229
lines changed
 

‎python/core/layout/qgslayoutitemregistry.sip

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ class QgsLayoutItemAbstractMetadata
1313
{
1414
%Docstring
1515
Stores metadata about one layout item class.
16+
17+
A companion class, QgsLayoutItemAbstractGuiMetadata, handles the
18+
GUI behavior of QgsLayoutItems.
19+
1620
.. note::
1721

18-
In C++ you can use QgsSymbolLayerMetadata convenience class.
22+
In C++ you can use QgsLayoutItemMetadata convenience class.
1923
.. versionadded:: 3.0
2024
%End
2125

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

59-
virtual QWidget *createItemWidget() /Factory/;
60-
%Docstring
61-
Creates a configuration widget for layout items of this type. Can return None if no configuration GUI is required.
62-
:rtype: QWidget
63-
%End
64-
65-
6663
virtual void resolvePaths( QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving );
6764
%Docstring
6865
Resolve paths in the item's ``properties`` (if there are any paths).
@@ -79,8 +76,6 @@ class QgsLayoutItemAbstractMetadata
7976

8077

8178

82-
83-
8479
class QgsLayoutItemRegistry : QObject
8580
{
8681
%Docstring
@@ -89,6 +84,9 @@ class QgsLayoutItemRegistry : QObject
8984
QgsLayoutItemRegistry is not usually directly created, but rather accessed through
9085
QgsApplication.layoutItemRegistry().
9186

87+
A companion class, QgsLayoutItemGuiRegistry, handles the GUI behavior
88+
of layout items.
89+
9290
.. versionadded:: 3.0
9391
%End
9492

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

150-
QWidget *createItemWidget( int type ) const /Factory/;
151-
%Docstring
152-
Creates a new instance of a layout item configuration widget for the specified item ``type``.
153-
:rtype: QWidget
154-
%End
155-
156-
157148
void resolvePaths( int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) const;
158149
%Docstring
159150
Resolve paths in properties of a particular symbol layer.

‎python/gui/gui_auto.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
%Include editorwidgets/core/qgseditorwidgetautoconf.sip
4343
%Include layertree/qgslayertreeembeddedconfigwidget.sip
4444
%Include layertree/qgslayertreeembeddedwidgetregistry.sip
45-
%Include layout/qgslayoutitemregistryguiutils.sip
4645
%Include layout/qgslayoutviewmouseevent.sip
4746
%Include layout/qgslayoutviewrubberband.sip
4847
%Include locator/qgslocatorcontext.sip
@@ -278,6 +277,7 @@
278277
%Include layertree/qgslayertreeview.sip
279278
%Include layertree/qgslayertreeviewdefaultactions.sip
280279
%Include layout/qgslayoutdesignerinterface.sip
280+
%Include layout/qgslayoutitemguiregistry.sip
281281
%Include layout/qgslayoutview.sip
282282
%Include layout/qgslayoutviewtool.sip
283283
%Include layout/qgslayoutviewtooladditem.sip
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/gui/layout/qgslayoutitemguiregistry.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
class QgsLayoutItemAbstractGuiMetadata
13+
{
14+
%Docstring
15+
Stores GUI metadata about one layout item class.
16+
17+
This is a companion to QgsLayoutItemAbstractMetadata, storing only
18+
the components related to the GUI behavior of a layout item.
19+
20+
.. note::
21+
22+
In C++ you can use QgsLayoutItemGuiMetadata convenience class.
23+
.. versionadded:: 3.0
24+
%End
25+
26+
%TypeHeaderCode
27+
#include "qgslayoutitemguiregistry.h"
28+
%End
29+
public:
30+
31+
QgsLayoutItemAbstractGuiMetadata( int type );
32+
%Docstring
33+
Constructor for QgsLayoutItemAbstractGuiMetadata with the specified class ``type``.
34+
%End
35+
36+
virtual ~QgsLayoutItemAbstractGuiMetadata();
37+
38+
int type() const;
39+
%Docstring
40+
Returns the unique item type code for the layout item class.
41+
:rtype: int
42+
%End
43+
44+
virtual QIcon creationIcon() const;
45+
%Docstring
46+
Returns an icon representing creation of the layout item type.
47+
:rtype: QIcon
48+
%End
49+
50+
virtual QWidget *createItemWidget() /Factory/;
51+
%Docstring
52+
Creates a configuration widget for layout items of this type. Can return None if no configuration GUI is required.
53+
:rtype: QWidget
54+
%End
55+
56+
virtual QgsLayoutViewRubberBand *createRubberBand( QgsLayoutView *view ) /Factory/;
57+
%Docstring
58+
Creates a rubber band for use when creating layout items of this type. Can return None if no rubber band
59+
should be created. The default behavior is to create a rectangular rubber band.
60+
:rtype: QgsLayoutViewRubberBand
61+
%End
62+
63+
};
64+
65+
66+
67+
68+
class QgsLayoutItemGuiRegistry : QObject
69+
{
70+
%Docstring
71+
Registry of available layout item GUI behavior.
72+
73+
QgsLayoutItemGuiRegistry is not usually directly created, but rather accessed through
74+
QgsGui.layoutItemGuiRegistry().
75+
76+
This acts as a companion to QgsLayoutItemRegistry, handling only
77+
the components related to the GUI behavior of layout items.
78+
79+
.. versionadded:: 3.0
80+
%End
81+
82+
%TypeHeaderCode
83+
#include "qgslayoutitemguiregistry.h"
84+
%End
85+
public:
86+
87+
QgsLayoutItemGuiRegistry( QObject *parent = 0 );
88+
%Docstring
89+
Creates a new empty item GUI registry.
90+
91+
QgsLayoutItemGuiRegistry is not usually directly created, but rather accessed through
92+
QgsGui.layoutItemGuiRegistry().
93+
94+
.. seealso:: populate()
95+
%End
96+
97+
~QgsLayoutItemGuiRegistry();
98+
99+
bool populate();
100+
%Docstring
101+
Populates the registry with standard item types. If called on a non-empty registry
102+
then this will have no effect and will return false.
103+
:rtype: bool
104+
%End
105+
106+
107+
QgsLayoutItemAbstractGuiMetadata *itemMetadata( int type ) const;
108+
%Docstring
109+
Returns the metadata for the specified item ``type``. Returns None if
110+
a corresponding type was not found in the registry.
111+
:rtype: QgsLayoutItemAbstractGuiMetadata
112+
%End
113+
114+
bool addLayoutItemGuiMetadata( QgsLayoutItemAbstractGuiMetadata *metadata /Transfer/ );
115+
%Docstring
116+
Registers the gui metadata for a new layout item type. Takes ownership of the metadata instance.
117+
:rtype: bool
118+
%End
119+
120+
QWidget *createItemWidget( int type ) const /Factory/;
121+
%Docstring
122+
Creates a new instance of a layout item configuration widget for the specified item ``type``.
123+
:rtype: QWidget
124+
%End
125+
126+
127+
QList< int > itemTypes() const;
128+
%Docstring
129+
Returns a list of available item types handled by the registry.
130+
:rtype: list of int
131+
%End
132+
133+
signals:
134+
135+
void typeAdded( int type );
136+
%Docstring
137+
Emitted whenever a new item type is added to the registry, with the specified
138+
``type``.
139+
%End
140+
141+
private:
142+
QgsLayoutItemGuiRegistry( const QgsLayoutItemGuiRegistry &rh );
143+
};
144+
145+
146+
147+
148+
/************************************************************************
149+
* This file has been generated automatically from *
150+
* *
151+
* src/gui/layout/qgslayoutitemguiregistry.h *
152+
* *
153+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
154+
************************************************************************/

‎python/gui/qgsgui.sip

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ class QgsGui
5656
:rtype: QgsMapLayerActionRegistry
5757
%End
5858

59+
static QgsLayoutItemGuiRegistry *layoutItemGuiRegistry();
60+
%Docstring
61+
Returns the global layout item GUI registry, used for registering the GUI behavior of layout items.
62+
:rtype: QgsLayoutItemGuiRegistry
63+
%End
64+
5965
~QgsGui();
6066

6167
private:

‎src/app/layout/qgslayoutdesignerdialog.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
#include "qgslayoutviewtoolpan.h"
2727
#include "qgslayoutviewtoolzoom.h"
2828
#include "qgslayoutviewtoolselect.h"
29+
#include "qgsgui.h"
30+
#include "qgslayoutitemguiregistry.h"
2931

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

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

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

195-
void QgsLayoutDesignerDialog::itemTypeAdded( int type, const QString &name )
195+
void QgsLayoutDesignerDialog::itemTypeAdded( int type )
196196
{
197+
QString name = QgsApplication::layoutItemRegistry()->itemMetadata( type )->visibleName();
197198
// update UI for new item type
198199
QAction *action = new QAction( tr( "Add %1" ).arg( name ), this );
199200
action->setToolTip( tr( "Adds a new %1 to the layout" ).arg( name ) );
200201
action->setCheckable( true );
201202
action->setData( type );
202-
action->setIcon( QgsApplication::layoutItemRegistry()->itemMetadata( type )->icon() );
203+
action->setIcon( QgsGui::layoutItemGuiRegistry()->itemMetadata( type )->creationIcon() );
203204
mToolsActionGroup->addAction( action );
204205
mItemMenu->addAction( action );
205206
mToolsToolbar->addAction( action );

‎src/app/layout/qgslayoutdesignerdialog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class QgsLayoutDesignerDialog: public QMainWindow, private Ui::QgsLayoutDesigner
115115

116116
private slots:
117117

118-
void itemTypeAdded( int type, const QString &name );
118+
void itemTypeAdded( int type );
119119

120120
private:
121121

‎src/core/layout/qgslayoutitemregistry.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,6 @@ QgsLayoutItem *QgsLayoutItemRegistry::createItem( int type, QgsLayout *layout, c
6565
return mMetadata[type]->createItem( layout, properties );
6666
}
6767

68-
QWidget *QgsLayoutItemRegistry::createItemWidget( int type ) const
69-
{
70-
if ( !mMetadata.contains( type ) )
71-
return nullptr;
72-
73-
return mMetadata[type]->createItemWidget();
74-
}
75-
76-
QgsLayoutViewRubberBand *QgsLayoutItemRegistry::createItemRubberBand( int type, QgsLayoutView *view ) const
77-
{
78-
if ( mRubberBandFunctions.contains( type ) )
79-
return mRubberBandFunctions.value( type )( view );
80-
81-
if ( !mMetadata.contains( type ) )
82-
return nullptr;
83-
84-
return mMetadata[type]->createRubberBand( view );
85-
}
86-
8768
void QgsLayoutItemRegistry::resolvePaths( int type, QVariantMap &properties, const QgsPathResolver &pathResolver, bool saving ) const
8869
{
8970
if ( !mMetadata.contains( type ) )

0 commit comments

Comments
 (0)