QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgslayoutundostack.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslayoutundostack.cpp
3 ------------------------
4 begin : July 2017
5 copyright : (C) 2017 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgslayoutundostack.h"
19#include "moc_qgslayoutundostack.cpp"
20#include "qgslayout.h"
21#include "qgsproject.h"
22#include <QUndoStack>
23
25 : mLayout( layout )
26 , mUndoStack( new QUndoStack( layout ) )
27{
28 connect( mUndoStack.get(), &QUndoStack::indexChanged, this, &QgsLayoutUndoStack::indexChanged );
29}
30
31void QgsLayoutUndoStack::beginMacro( const QString &commandText )
32{
33 if ( mBlockedCommands == 0 )
34 mUndoStack->beginMacro( commandText );
35}
36
38{
39 if ( mBlockedCommands == 0 )
40 mUndoStack->endMacro();
41}
42
43void QgsLayoutUndoStack::beginCommand( QgsLayoutUndoObjectInterface *object, const QString &commandText, int id )
44{
45 if ( !object )
46 {
47 return;
48 }
49
50 mActiveCommands.emplace_back( std::unique_ptr< QgsAbstractLayoutUndoCommand >( object->createCommand( commandText, id, nullptr ) ) );
51 mActiveCommands.back()->saveBeforeState();
52}
53
55{
56 if ( mActiveCommands.empty() )
57 return;
58
59 mActiveCommands.back()->saveAfterState();
60 if ( mBlockedCommands == 0 && mActiveCommands.back()->containsChange() ) //protect against empty commands
61 {
62 mUndoStack->push( mActiveCommands.back().release() );
63 mLayout->project()->setDirty( true );
64 }
65
66 mActiveCommands.pop_back();
67}
68
70{
71 if ( mActiveCommands.empty() )
72 return;
73
74 mActiveCommands.pop_back();
75}
76
78{
79 return mUndoStack.get();
80}
81
83{
84 mUndoRedoOccurredItemUuids.insert( item->uuid() );
85}
86
88{
89 if ( blocked )
90 {
91 mBlockedCommands++;
92 }
93 else
94 {
95 if ( mBlockedCommands > 0 )
96 mBlockedCommands--;
97 }
98}
99
101{
102 return mBlockedCommands > 0;
103}
104
105void QgsLayoutUndoStack::push( QUndoCommand *cmd )
106{
107 if ( mBlockedCommands > 0 )
108 delete cmd;
109 else
110 {
111 mUndoStack->push( cmd );
112 mLayout->project()->setDirty( true );
113 }
114}
115
116void QgsLayoutUndoStack::indexChanged()
117{
118 if ( mUndoRedoOccurredItemUuids.empty() )
119 return;
120
121 emit undoRedoOccurredForItems( mUndoRedoOccurredItemUuids );
122 mUndoRedoOccurredItemUuids.clear();
123}
Base class for graphical items within a QgsLayout.
virtual QString uuid() const
Returns the item identification string.
Interface for layout objects which support undo/redo commands.
virtual QgsAbstractLayoutUndoCommand * createCommand(const QString &text, int id=0, QUndoCommand *parent=nullptr)=0
Creates a new layout undo command with the specified text and parent.
void endCommand()
Saves final state of an object and pushes the active command to the undo history.
void cancelCommand()
Cancels the active command, discarding it without pushing to the undo history.
void beginMacro(const QString &commandText)
Starts a macro command, with the given descriptive commandText.
void beginCommand(QgsLayoutUndoObjectInterface *object, const QString &commandText, int id=0)
Begins a new undo command for the specified object.
void notifyUndoRedoOccurred(QgsLayoutItem *item)
Notifies the stack that an undo or redo action occurred for a specified item.
void push(QUndoCommand *command)
Manually pushes a command to the stack, and takes ownership of the command.
void endMacro()
Ends a macro command.
bool isBlocked() const
Returns true if undo commands are currently blocked.
void undoRedoOccurredForItems(QSet< QString > itemUuids)
Emitted when an undo or redo action has occurred, which affected a set of layout itemUuids.
QUndoStack * stack()
Returns a pointer to the internal QUndoStack.
void blockCommands(bool blocked)
Sets whether undo commands for the layout should be temporarily blocked.
QgsLayoutUndoStack(QgsLayout *layout)
Constructor for QgsLayoutUndoStack, for the specified parent layout.
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition qgslayout.h:49
QgsProject * project() const
The project associated with the layout.
void setDirty(bool b=true)
Flag the project as dirty (modified).