Skip to content

Commit 2a75ad8

Browse files
committed
More work on layout multiframe undo/redo
1 parent 12e69ab commit 2a75ad8

File tree

7 files changed

+44
-2
lines changed

7 files changed

+44
-2
lines changed

python/core/layout/qgslayoutframe.sip

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

3838
virtual QString stringType() const;
3939

40+
virtual QString uuid() const;
41+
4042

4143
virtual QString displayName() const;
4244

python/core/layout/qgslayoutitem.sip

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem, QgsLayoutUndoObjectInt
135135
:rtype: str
136136
%End
137137

138-
QString uuid() const;
138+
virtual QString uuid() const;
139139
%Docstring
140140
Returns the item identification string. This is a unique random string set for the item
141141
upon creation.

src/core/layout/qgslayoutframe.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
QgsLayoutFrame::QgsLayoutFrame( QgsLayout *layout, QgsLayoutMultiFrame *multiFrame )
2222
: QgsLayoutItem( layout )
2323
, mMultiFrame( multiFrame )
24+
, mMultiFrameUuid( multiFrame ? multiFrame->uuid() : QString() )
2425
{
2526

2627
//default to no background
@@ -93,6 +94,14 @@ QString QgsLayoutFrame::stringType() const
9394
return QStringLiteral( "ItemFrame" );
9495
}
9596

97+
QString QgsLayoutFrame::uuid() const
98+
{
99+
if ( mMultiFrame )
100+
return mMultiFrame->uuid() + ':' + mMultiFrame->frameIndex( const_cast< QgsLayoutFrame * >( this ) );
101+
else
102+
return QgsLayoutItem::uuid();
103+
}
104+
96105
void QgsLayoutFrame::setHidePageIfEmpty( const bool hidePageIfEmpty )
97106
{
98107
mHidePageIfEmpty = hidePageIfEmpty;

src/core/layout/qgslayoutframe.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class CORE_EXPORT QgsLayoutFrame: public QgsLayoutItem
4747

4848
int type() const override;
4949
QString stringType() const override;
50+
QString uuid() const override;
5051

5152
//Overridden to allow multiframe to set display name
5253
QString displayName() const override;
@@ -126,6 +127,7 @@ class CORE_EXPORT QgsLayoutFrame: public QgsLayoutItem
126127
private:
127128
QgsLayoutFrame() = delete;
128129
QgsLayoutMultiFrame *mMultiFrame = nullptr;
130+
QString mMultiFrameUuid;
129131
QRectF mSection;
130132

131133
//! If true, layout will not export page if this frame is empty

src/core/layout/qgslayoutitem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
172172
* \see id()
173173
* \see setId()
174174
*/
175-
QString uuid() const { return mUuid; }
175+
virtual QString uuid() const { return mUuid; }
176176

177177
/**
178178
* Returns the item's ID name. This is not necessarily unique, and duplicate ID names may exist

src/gui/layout/qgslayoutviewtooladditem.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,23 @@ void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *even
8484

8585
QRectF rect = mRubberBand->finish( event->snappedPoint(), event->modifiers() );
8686

87+
QString undoText;
88+
if ( QgsLayoutItemAbstractGuiMetadata *metadata = QgsGui::layoutItemGuiRegistry()->itemMetadata( mItemMetadataId ) )
89+
{
90+
undoText = tr( "Create %1" ).arg( metadata->visibleName() );
91+
}
92+
else
93+
{
94+
undoText = tr( "Create Item" );
95+
}
96+
layout()->undoStack()->beginMacro( undoText );
97+
8798
QgsLayoutItem *item = QgsGui::layoutItemGuiRegistry()->createItem( mItemMetadataId, layout() );
8899
if ( !item )
100+
{
101+
layout()->undoStack()->endMacro();
89102
return;
103+
}
90104

91105
// click? or click-and-drag?
92106
bool clickOnly = !isClickAndDrag( mMousePressStartPos, event->pos() );
@@ -104,6 +118,7 @@ void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *even
104118
else
105119
{
106120
delete item;
121+
layout()->undoStack()->endMacro();
107122
return;
108123
}
109124
}
@@ -126,6 +141,8 @@ void QgsLayoutViewToolAddItem::layoutReleaseEvent( QgsLayoutViewMouseEvent *even
126141
if ( item->layout() != layout() )
127142
layout()->addLayoutItem( item );
128143
layout()->setSelectedItem( item );
144+
145+
layout()->undoStack()->endMacro();
129146
emit createdItem();
130147
}
131148

tests/src/core/testqgslayoutmultiframe.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class TestQgsLayoutMultiFrame : public QObject
4343
void addRemovePage(); //test if page is added and removed for RepeatUntilFinished mode
4444
void undoRedo(); //test that combinations of frame/multiframe undo/redo don't crash
4545
void undoRedoRemovedFrame(); //test that undo doesn't crash with removed frames
46+
void undoRedoRemovedFrame2();
4647
void registry();
4748

4849
private:
@@ -455,11 +456,13 @@ void TestQgsLayoutMultiFrame::undoRedoRemovedFrame()
455456

456457
auto dumpStack = [ = ]
457458
{
459+
#if 0 // for debugging
458460
// dump stack
459461
for ( int i = 0; i < mLayout->undoStack()->stack()->count(); ++i )
460462
{
461463
QgsDebugMsg( QString( "%1: %2 %3" ).arg( i ).arg( mLayout->undoStack()->stack()->command( i )->text(), i + 1 == mLayout->undoStack()->stack()->index() ? QString( "<---" ) : QString() ) );
462464
}
465+
#endif
463466
};
464467
dumpStack();
465468
//undo changes
@@ -500,6 +503,15 @@ void TestQgsLayoutMultiFrame::undoRedoRemovedFrame()
500503
delete htmlItem;
501504
}
502505

506+
void TestQgsLayoutMultiFrame::undoRedoRemovedFrame2()
507+
{
508+
QgsLayoutItemHtml *htmlItem = new QgsLayoutItemHtml( mLayout );
509+
QgsLayoutFrame *frame1 = new QgsLayoutFrame( mLayout, htmlItem );
510+
frame1->attemptSetSceneRect( QRectF( 0, 0, 100, 200 ) );
511+
htmlItem->addFrame( frame1 );
512+
513+
}
514+
503515
void TestQgsLayoutMultiFrame::registry()
504516
{
505517
// test QgsLayoutItemRegistry

0 commit comments

Comments
 (0)