Skip to content

Commit a12673c

Browse files
authored
Merge pull request #5794 from nyalldawson/layout_next2
[layouts] Copy/paste, templates, etc
2 parents ca75e8c + 8e77701 commit a12673c

File tree

124 files changed

+4188
-597
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+4188
-597
lines changed

python/core/composer/qgscomposeritem.sip

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -17,99 +17,6 @@ class QgsComposerItem: QgsComposerObject, QGraphicsRectItem
1717

1818
%TypeHeaderCode
1919
#include "qgscomposeritem.h"
20-
#include <qgscomposerarrow.h>
21-
#include <qgscomposerframe.h>
22-
#include <qgscomposeritemgroup.h>
23-
#include <qgscomposerlabel.h>
24-
#include <qgscomposerlegend.h>
25-
#include <qgscomposermap.h>
26-
#include <qgspaperitem.h>
27-
#include <qgscomposerpicture.h>
28-
#include <qgscomposerscalebar.h>
29-
#include <qgscomposershape.h>
30-
#include <qgscomposerpolygon.h>
31-
#include <qgscomposerpolyline.h>
32-
#include <qgscomposertexttable.h>
33-
#include <qgslayoutitemshape.h>
34-
#include <qgslayoutitempage.h>
35-
%End
36-
%ConvertToSubClassCode
37-
// the conversions have to be static, because they're using multiple inheritance
38-
// (seen in PyQt4 .sip files for some QGraphicsItem classes)
39-
if ( dynamic_cast< QgsComposerItem * >( sipCpp ) )
40-
{
41-
switch ( sipCpp->type() )
42-
{
43-
case QgsComposerItem::ComposerItem:
44-
sipType = sipType_QgsComposerItem;
45-
*sipCppRet = static_cast<QgsComposerItem *>( sipCpp );
46-
break;
47-
case QgsComposerItem::ComposerArrow:
48-
sipType = sipType_QgsComposerArrow;
49-
*sipCppRet = static_cast<QgsComposerArrow *>( sipCpp );
50-
break;
51-
case QgsComposerItem::ComposerItemGroup:
52-
sipType = sipType_QgsComposerItemGroup;
53-
*sipCppRet = static_cast<QgsComposerItemGroup *>( sipCpp );
54-
break;
55-
case QgsComposerItem::ComposerLabel:
56-
sipType = sipType_QgsComposerLabel;
57-
*sipCppRet = static_cast<QgsComposerLabel *>( sipCpp );
58-
break;
59-
case QgsComposerItem::ComposerLegend:
60-
sipType = sipType_QgsComposerLegend;
61-
*sipCppRet = static_cast<QgsComposerLegend *>( sipCpp );
62-
break;
63-
case QgsComposerItem::ComposerMap:
64-
sipType = sipType_QgsComposerMap;
65-
*sipCppRet = static_cast<QgsComposerMap *>( sipCpp );
66-
break;
67-
case QgsComposerItem::ComposerPaper:
68-
sipType = sipType_QgsPaperItem;
69-
*sipCppRet = static_cast<QgsPaperItem *>( sipCpp );
70-
break;
71-
case QgsComposerItem::ComposerPicture:
72-
sipType = sipType_QgsComposerPicture;
73-
*sipCppRet = static_cast<QgsComposerPicture *>( sipCpp );
74-
break;
75-
case QgsComposerItem::ComposerScaleBar:
76-
sipType = sipType_QgsComposerScaleBar;
77-
*sipCppRet = static_cast<QgsComposerScaleBar *>( sipCpp );
78-
break;
79-
case QgsComposerItem::ComposerShape:
80-
sipType = sipType_QgsComposerShape;
81-
*sipCppRet = static_cast<QgsComposerShape *>( sipCpp );
82-
break;
83-
case QgsComposerItem::ComposerPolygon:
84-
sipType = sipType_QgsComposerPolygon;
85-
*sipCppRet = static_cast<QgsComposerPolygon *>( sipCpp );
86-
break;
87-
case QgsComposerItem::ComposerPolyline:
88-
sipType = sipType_QgsComposerPolyline;
89-
*sipCppRet = static_cast<QgsComposerPolyline *>( sipCpp );
90-
break;
91-
case QgsComposerItem::ComposerFrame:
92-
sipType = sipType_QgsComposerFrame;
93-
*sipCppRet = static_cast<QgsComposerFrame *>( sipCpp );
94-
break;
95-
default:
96-
sipType = 0;
97-
}
98-
}
99-
else
100-
{
101-
switch ( sipCpp->type() )
102-
{
103-
// really, these *should* use the constants from QgsLayoutItemRegistry, but sip doesn't like that!
104-
case QGraphicsItem::UserType + 101:
105-
sipType = sipType_QgsLayoutItemPage;
106-
*sipCppRet = static_cast<QgsLayoutItemPage *>( sipCpp );
107-
break;
108-
default:
109-
sipType = 0;
110-
}
111-
}
112-
11320
%End
11421
public:
11522

python/core/composer/qgslayoutmanager.sip

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ class QgsLayoutManager : QObject
1515
%Docstring
1616
.. versionadded:: 3.0
1717

18-
Manages storage of a set of compositions.
18+
Manages storage of a set of layouts.
1919

2020
QgsLayoutManager handles the storage, serializing and deserializing
21-
of QgsCompositions. Usually this class is not constructed directly, but
21+
of QgsLayouts. Usually this class is not constructed directly, but
2222
rather accessed through a QgsProject via QgsProject.layoutManager().
2323

24-
QgsLayoutManager retains ownership of all the compositions contained
24+
QgsLayoutManager retains ownership of all the layouts contained
2525
in the manager.
2626
%End
2727

@@ -48,6 +48,16 @@ class QgsLayoutManager : QObject
4848
:rtype: bool
4949
%End
5050

51+
bool addLayout( QgsLayout *layout /Transfer/ );
52+
%Docstring
53+
Adds a ``layout`` to the manager. Ownership of the layout is transferred to the manager.
54+
Returns true if the addition was successful, or false if the layout could not be added (eg
55+
as a result of a duplicate layout name).
56+
.. seealso:: :py:func:`removeLayout()`
57+
.. seealso:: :py:func:`layoutAdded()`
58+
:rtype: bool
59+
%End
60+
5161
bool removeComposition( QgsComposition *composition );
5262
%Docstring
5363
Removes a composition from the manager. The composition is deleted.
@@ -56,14 +66,26 @@ class QgsLayoutManager : QObject
5666
.. seealso:: :py:func:`addComposition()`
5767
.. seealso:: :py:func:`compositionRemoved()`
5868
.. seealso:: :py:func:`compositionAboutToBeRemoved()`
69+
.. seealso:: :py:func:`clear()`
70+
:rtype: bool
71+
%End
72+
73+
bool removeLayout( QgsLayout *layout );
74+
%Docstring
75+
Removes a ``layout`` from the manager. The layout is deleted.
76+
Returns true if the removal was successful, or false if the removal failed (eg as a result
77+
of removing a layout which is not contained in the manager).
78+
.. seealso:: :py:func:`addLayout()`
79+
.. seealso:: :py:func:`layoutRemoved()`
80+
.. seealso:: :py:func:`layoutAboutToBeRemoved()`
5981
.. seealso:: :py:func:`clear()`
6082
:rtype: bool
6183
%End
6284

6385
void clear();
6486
%Docstring
65-
Removes and deletes all compositions from the manager.
66-
.. seealso:: :py:func:`removeComposition()`
87+
Removes and deletes all layouts from the manager.
88+
.. seealso:: :py:func:`removeLayout()`
6789
%End
6890

6991
QList< QgsComposition * > compositions() const;
@@ -72,16 +94,29 @@ class QgsLayoutManager : QObject
7294
:rtype: list of QgsComposition
7395
%End
7496

97+
QList< QgsLayout * > layouts() const;
98+
%Docstring
99+
Returns a list of all layouts contained in the manager.
100+
:rtype: list of QgsLayout
101+
%End
102+
75103
QgsComposition *compositionByName( const QString &name ) const;
76104
%Docstring
77105
Returns the composition with a matching name, or None if no matching compositions
78106
were found.
79107
:rtype: QgsComposition
80108
%End
81109

110+
QgsLayout *layoutByName( const QString &name ) const;
111+
%Docstring
112+
Returns the layout with a matching name, or None if no matching layouts
113+
were found.
114+
:rtype: QgsLayout
115+
%End
116+
82117
bool readXml( const QDomElement &element, const QDomDocument &doc );
83118
%Docstring
84-
Reads the manager's state from a DOM element, restoring all compositions
119+
Reads the manager's state from a DOM element, restoring all layouts
85120
present in the XML document.
86121
.. seealso:: :py:func:`writeXml()`
87122
:rtype: bool
@@ -109,38 +144,78 @@ class QgsLayoutManager : QObject
109144
:rtype: QgsComposition
110145
%End
111146

112-
QString generateUniqueTitle() const;
147+
QgsLayout *duplicateLayout( const QgsLayout *layout, const QString &newName );
148+
%Docstring
149+
Duplicates an existing ``layout`` from the manager. The new
150+
layout will automatically be stored in the manager.
151+
Returns new the layout if duplication was successful.
152+
:rtype: QgsLayout
153+
%End
154+
155+
QString generateUniqueComposerTitle() const;
113156
%Docstring
114157
Generates a unique title for a new composition, which does not
115158
clash with any already contained by the manager.
116159
:rtype: str
117160
%End
118161

162+
QString generateUniqueTitle() const;
163+
%Docstring
164+
Generates a unique title for a new layout, which does not
165+
clash with any already contained by the manager.
166+
:rtype: str
167+
%End
168+
119169
signals:
120170

121171
void compositionAboutToBeAdded( const QString &name );
122172
%Docstring
123173
Emitted when a composition is about to be added to the manager
174+
%End
175+
176+
void layoutAboutToBeAdded( const QString &name );
177+
%Docstring
178+
Emitted when a layout is about to be added to the manager
124179
%End
125180

126181
void compositionAdded( const QString &name );
127182
%Docstring
128183
Emitted when a composition has been added to the manager
184+
%End
185+
186+
void layoutAdded( const QString &name );
187+
%Docstring
188+
Emitted when a layout has been added to the manager
129189
%End
130190

131191
void compositionRemoved( const QString &name );
132192
%Docstring
133193
Emitted when a composition was removed from the manager
194+
%End
195+
196+
void layoutRemoved( const QString &name );
197+
%Docstring
198+
Emitted when a layout was removed from the manager
134199
%End
135200

136201
void compositionAboutToBeRemoved( const QString &name );
137202
%Docstring
138203
Emitted when a composition is about to be removed from the manager
204+
%End
205+
206+
void layoutAboutToBeRemoved( const QString &name );
207+
%Docstring
208+
Emitted when a layout is about to be removed from the manager
139209
%End
140210

141211
void compositionRenamed( QgsComposition *composition, const QString &newName );
142212
%Docstring
143213
Emitted when a composition is renamed
214+
%End
215+
216+
void layoutRenamed( QgsLayout *layout, const QString &newName );
217+
%Docstring
218+
Emitted when a layout is renamed
144219
%End
145220

146221
};

python/core/layout/qgslayout.sip

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator, QgsLayoutUndoOb
4949
a new layout.
5050
%End
5151

52+
void clear();
53+
%Docstring
54+
Clears the layout.
55+
56+
Calling this method removes all items and pages from the layout.
57+
%End
58+
5259
QgsProject *project() const;
5360
%Docstring
5461
The project associated with the layout. Used to get access to layers, map themes,
@@ -171,10 +178,14 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator, QgsLayoutUndoOb
171178
which deferred z-order updates.
172179
%End
173180

174-
QgsLayoutItem *itemByUuid( const QString &uuid );
181+
QgsLayoutItem *itemByUuid( const QString &uuid, bool includeTemplateUuids = false );
175182
%Docstring
176183
Returns the layout item with matching ``uuid`` unique identifier, or a None
177184
if a matching item could not be found.
185+
186+
If ``includeTemplateUuids`` is true, then item's QgsLayoutItem.templateUuid()
187+
will also be tested when trying to match the uuid.
188+
178189
.. seealso:: :py:func:`multiFrameByUuid()`
179190
:rtype: QgsLayoutItem
180191
%End
@@ -410,6 +421,28 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator, QgsLayoutUndoOb
410421
:rtype: list of QgsLayoutMultiFrame
411422
%End
412423

424+
bool saveAsTemplate( const QString &path, const QgsReadWriteContext &context ) const;
425+
%Docstring
426+
Saves the layout as a template at the given file ``path``.
427+
Returns true if save was successful.
428+
.. seealso:: :py:func:`loadFromTemplate()`
429+
:rtype: bool
430+
%End
431+
432+
QList< QgsLayoutItem * > loadFromTemplate( const QDomDocument &document, const QgsReadWriteContext &context, bool clearExisting = true, bool *ok /Out/ = 0 );
433+
%Docstring
434+
Load a layout template ``document``.
435+
436+
By default this method will clear all items from the existing layout and real all layout
437+
settings from the template. Setting ``clearExisting`` to false will only add new items
438+
from the template, without overwriting the existing items or layout settings.
439+
440+
If ``ok`` is specified, it will be set to true if the load was successful.
441+
442+
Returns a list of loaded items.
443+
:rtype: list of QgsLayoutItem
444+
%End
445+
413446
QDomElement writeXml( QDomDocument &document, const QgsReadWriteContext &context ) const;
414447
%Docstring
415448
Returns the layout's state encapsulated in a DOM element.
@@ -424,6 +457,23 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator, QgsLayoutUndoOb
424457
:rtype: bool
425458
%End
426459

460+
QList< QgsLayoutItem * > addItemsFromXml( const QDomElement &parentElement, const QDomDocument &document,
461+
const QgsReadWriteContext &context,
462+
QPointF *position = 0, bool pasteInPlace = false );
463+
%Docstring
464+
Add items from an XML representation to the layout. Used for project file reading and pasting items from clipboard.
465+
466+
The ``position`` argument is optional, and if it is not specified the items will be restored to their
467+
original position from the XML serialization. If specified, the items will be positioned such that the top-left
468+
bounds of all added items is located at this ``position``.
469+
470+
The ``pasteInPlace`` argument determines whether the serialized position should be respected, but remapped to the
471+
origin of the page corresponding to the page at ``position``.
472+
473+
A list of the newly added items is returned.
474+
:rtype: list of QgsLayoutItem
475+
%End
476+
427477
QgsLayoutUndoStack *undoStack();
428478
%Docstring
429479
Returns a pointer to the layout's undo stack, which manages undo/redo states for the layout
@@ -491,6 +541,12 @@ class QgsLayout : QGraphicsScene, QgsExpressionContextGenerator, QgsLayoutUndoOb
491541
and updated.
492542
%End
493543

544+
void nameChanged( const QString &name );
545+
%Docstring
546+
Emitted when the layout's name is changed.
547+
.. seealso:: :py:func:`setName()`
548+
%End
549+
494550
};
495551

496552

python/core/layout/qgslayoutframe.sip

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ class QgsLayoutFrame: QgsLayoutItem
3535

3636
virtual int type() const;
3737

38-
virtual QString stringType() const;
39-
4038
virtual QString uuid() const;
4139

4240

0 commit comments

Comments
 (0)