@@ -25,8 +25,6 @@ QgsLayoutItemGroup::QgsLayoutItemGroup( QgsLayout *layout )
25
25
26
26
QgsLayoutItemGroup::~QgsLayoutItemGroup ()
27
27
{
28
- if ( mLayout )
29
- mLayout ->undoStack ()->beginMacro ( tr ( " Removed group" ) );
30
28
// loop through group members and remove them from the scene
31
29
for ( QgsLayoutItem *item : qgsAsConst ( mItems ) )
32
30
{
@@ -37,10 +35,8 @@ QgsLayoutItemGroup::~QgsLayoutItemGroup()
37
35
if ( mLayout )
38
36
mLayout ->removeLayoutItem ( item );
39
37
else
40
- item-> deleteLater () ;
38
+ delete item;
41
39
}
42
- if ( mLayout )
43
- mLayout ->undoStack ()->endMacro ();
44
40
}
45
41
46
42
int QgsLayoutItemGroup::type () const
@@ -63,6 +59,11 @@ QString QgsLayoutItemGroup::displayName() const
63
59
return tr ( " <Group>" );
64
60
}
65
61
62
+ QgsLayoutItemGroup *QgsLayoutItemGroup::create ( QgsLayout *layout, const QVariantMap & )
63
+ {
64
+ return new QgsLayoutItemGroup ( layout );
65
+ }
66
+
66
67
void QgsLayoutItemGroup::addItem ( QgsLayoutItem *item )
67
68
{
68
69
if ( !item )
@@ -107,7 +108,7 @@ QList<QgsLayoutItem *> QgsLayoutItemGroup::items() const
107
108
108
109
void QgsLayoutItemGroup::setVisibility ( const bool visible )
109
110
{
110
- if ( mLayout )
111
+ if ( ! shouldBlockUndoCommands () )
111
112
mLayout ->undoStack ()->beginMacro ( tr ( " Set group visibility" ) );
112
113
// also set visibility for all items within the group
113
114
for ( QgsLayoutItem *item : qgsAsConst ( mItems ) )
@@ -118,7 +119,7 @@ void QgsLayoutItemGroup::setVisibility( const bool visible )
118
119
}
119
120
// lastly set visibility for group item itself
120
121
QgsLayoutItem::setVisibility ( visible );
121
- if ( mLayout )
122
+ if ( ! shouldBlockUndoCommands () )
122
123
mLayout ->undoStack ()->endMacro ();
123
124
}
124
125
@@ -127,7 +128,8 @@ void QgsLayoutItemGroup::attemptMove( const QgsLayoutPoint &point )
127
128
if ( !mLayout )
128
129
return ;
129
130
130
- mLayout ->undoStack ()->beginMacro ( tr ( " Moved group" ) );
131
+ if ( !shouldBlockUndoCommands () )
132
+ mLayout ->undoStack ()->beginMacro ( tr ( " Moved group" ) );
131
133
132
134
QPointF scenePoint = mLayout ->convertToLayoutUnits ( point );
133
135
double deltaX = scenePoint.x () - pos ().x ();
@@ -139,7 +141,12 @@ void QgsLayoutItemGroup::attemptMove( const QgsLayoutPoint &point )
139
141
if ( !item )
140
142
continue ;
141
143
142
- mLayout ->undoStack ()->beginCommand ( item, QString () );
144
+ std::unique_ptr< QgsAbstractLayoutUndoCommand > command;
145
+ if ( !shouldBlockUndoCommands () )
146
+ {
147
+ command.reset ( createCommand ( QString (), 0 ) );
148
+ command->saveBeforeState ();
149
+ }
143
150
144
151
// need to convert delta from layout units -> item units
145
152
QgsLayoutPoint itemPos = item->positionWithUnits ();
@@ -148,11 +155,16 @@ void QgsLayoutItemGroup::attemptMove( const QgsLayoutPoint &point )
148
155
itemPos.setY ( itemPos.y () + deltaPos.y () );
149
156
item->attemptMove ( itemPos );
150
157
151
- mLayout ->undoStack ()->endCommand ();
158
+ if ( command )
159
+ {
160
+ command->saveAfterState ();
161
+ mLayout ->undoStack ()->stack ()->push ( command.release () );
162
+ }
152
163
}
153
164
// lastly move group item itself
154
165
QgsLayoutItem::attemptMove ( point );
155
- mLayout ->undoStack ()->endMacro ();
166
+ if ( !shouldBlockUndoCommands () )
167
+ mLayout ->undoStack ()->endMacro ();
156
168
resetBoundingRect ();
157
169
}
158
170
@@ -161,7 +173,8 @@ void QgsLayoutItemGroup::attemptResize( const QgsLayoutSize &size )
161
173
if ( !mLayout )
162
174
return ;
163
175
164
- mLayout ->undoStack ()->beginMacro ( tr ( " Resized group" ) );
176
+ if ( !shouldBlockUndoCommands () )
177
+ mLayout ->undoStack ()->beginMacro ( tr ( " Resized group" ) );
165
178
166
179
QRectF oldRect = rect ();
167
180
QSizeF newSizeLayoutUnits = mLayout ->convertToLayoutUnits ( size );
@@ -174,6 +187,13 @@ void QgsLayoutItemGroup::attemptResize( const QgsLayoutSize &size )
174
187
if ( !item )
175
188
continue ;
176
189
190
+ std::unique_ptr< QgsAbstractLayoutUndoCommand > command;
191
+ if ( !shouldBlockUndoCommands () )
192
+ {
193
+ command.reset ( createCommand ( QString (), 0 ) );
194
+ command->saveBeforeState ();
195
+ }
196
+
177
197
QRectF itemRect = mapRectFromItem ( item, item->rect () );
178
198
QgsLayoutUtils::relativeResizeRect ( itemRect, oldRect, newRect );
179
199
@@ -186,11 +206,76 @@ void QgsLayoutItemGroup::attemptResize( const QgsLayoutSize &size )
186
206
187
207
QgsLayoutSize itemSize = mLayout ->convertFromLayoutUnits ( itemRect.size (), item->sizeWithUnits ().units () );
188
208
item->attemptResize ( itemSize );
209
+
210
+ if ( command )
211
+ {
212
+ command->saveAfterState ();
213
+ mLayout ->undoStack ()->stack ()->push ( command.release () );
214
+ }
189
215
}
190
216
QgsLayoutItem::attemptResize ( size );
191
- mLayout ->undoStack ()->endMacro ();
217
+ if ( !shouldBlockUndoCommands () )
218
+ mLayout ->undoStack ()->endMacro ();
219
+
220
+ resetBoundingRect ();
221
+ }
222
+
223
+ bool QgsLayoutItemGroup::writeXml ( QDomElement &parentElement, QDomDocument &document, const QgsReadWriteContext &context ) const
224
+ {
225
+ QDomElement element = document.createElement ( QStringLiteral ( " LayoutItem" ) );
226
+ element.setAttribute ( QStringLiteral ( " type" ), stringType () );
227
+
228
+ writePropertiesToElement ( element, document, context );
229
+
230
+ for ( QgsLayoutItem *item : mItems )
231
+ {
232
+ if ( !item )
233
+ continue ;
234
+
235
+ QDomElement childItem = document.createElement ( QStringLiteral ( " ComposerItemGroupElement" ) );
236
+ childItem.setAttribute ( QStringLiteral ( " uuid" ), item->uuid () );
237
+ element.appendChild ( childItem );
238
+ }
239
+
240
+ parentElement.appendChild ( element );
241
+
242
+ return true ;
243
+ }
244
+
245
+ bool QgsLayoutItemGroup::readXml ( const QDomElement &itemElement, const QDomDocument &document, const QgsReadWriteContext &context )
246
+ {
247
+ if ( itemElement.nodeName () != QStringLiteral ( " LayoutItem" ) || itemElement.attribute ( QStringLiteral ( " type" ) ) != stringType () )
248
+ {
249
+ return false ;
250
+ }
251
+
252
+ bool result = readPropertiesFromElement ( itemElement, document, context );
253
+
254
+ QList<QgsLayoutItem *> items;
255
+ mLayout ->layoutItems ( items );
256
+
257
+ QDomNodeList elementNodes = itemElement.elementsByTagName ( QStringLiteral ( " ComposerItemGroupElement" ) );
258
+ for ( int i = 0 ; i < elementNodes.count (); ++i )
259
+ {
260
+ QDomNode elementNode = elementNodes.at ( i );
261
+ if ( !elementNode.isElement () )
262
+ continue ;
263
+
264
+ QString uuid = elementNode.toElement ().attribute ( QStringLiteral ( " uuid" ) );
265
+
266
+ for ( QgsLayoutItem *item : qgsAsConst ( items ) )
267
+ {
268
+ if ( item && ( item->mUuid == uuid /* TODO || item->mTemplateUuid == uuid */ ) )
269
+ {
270
+ addItem ( item );
271
+ break ;
272
+ }
273
+ }
274
+ }
192
275
193
276
resetBoundingRect ();
277
+
278
+ return result;
194
279
}
195
280
196
281
void QgsLayoutItemGroup::paint ( QPainter *, const QStyleOptionGraphicsItem *, QWidget * )
0 commit comments