QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 "qgslayout.h"
20 #include "qgsproject.h"
21 #include <QUndoStack>
22 
24  : mLayout( layout )
25  , mUndoStack( new QUndoStack( layout ) )
26 {
27  connect( mUndoStack.get(), &QUndoStack::indexChanged, this, &QgsLayoutUndoStack::indexChanged );
28 }
29 
30 void QgsLayoutUndoStack::beginMacro( const QString &commandText )
31 {
32  if ( mBlockedCommands == 0 )
33  mUndoStack->beginMacro( commandText );
34 }
35 
37 {
38  if ( mBlockedCommands == 0 )
39  mUndoStack->endMacro();
40 }
41 
42 void QgsLayoutUndoStack::beginCommand( QgsLayoutUndoObjectInterface *object, const QString &commandText, int id )
43 {
44  if ( !object )
45  {
46  return;
47  }
48 
49  mActiveCommands.emplace_back( std::unique_ptr< QgsAbstractLayoutUndoCommand >( object->createCommand( commandText, id, nullptr ) ) );
50  mActiveCommands.back()->saveBeforeState();
51 }
52 
54 {
55  if ( mActiveCommands.empty() )
56  return;
57 
58  mActiveCommands.back()->saveAfterState();
59  if ( mBlockedCommands == 0 && mActiveCommands.back()->containsChange() ) //protect against empty commands
60  {
61  mUndoStack->push( mActiveCommands.back().release() );
62  mLayout->project()->setDirty( true );
63  }
64 
65  mActiveCommands.pop_back();
66 }
67 
69 {
70  if ( mActiveCommands.empty() )
71  return;
72 
73  mActiveCommands.pop_back();
74 }
75 
77 {
78  return mUndoStack.get();
79 }
80 
82 {
83  mUndoRedoOccurredItemUuids.insert( item->uuid() );
84 }
85 
87 {
88  if ( blocked )
89  {
90  mBlockedCommands++;
91  }
92  else
93  {
94  if ( mBlockedCommands > 0 )
95  mBlockedCommands--;
96  }
97 }
98 
100 {
101  return mBlockedCommands > 0;
102 }
103 
104 void QgsLayoutUndoStack::push( QUndoCommand *cmd )
105 {
106  if ( mBlockedCommands > 0 )
107  delete cmd;
108  else
109  {
110  mUndoStack->push( cmd );
111  mLayout->project()->setDirty( true );
112  }
113 }
114 
115 void QgsLayoutUndoStack::indexChanged()
116 {
117  if ( mUndoRedoOccurredItemUuids.empty() )
118  return;
119 
120  emit undoRedoOccurredForItems( mUndoRedoOccurredItemUuids );
121  mUndoRedoOccurredItemUuids.clear();
122 }
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:51
QgsProject * project() const
The project associated with the layout.
Definition: qgslayout.cpp:132
void setDirty(bool b=true)
Flag the project as dirty (modified).
Definition: qgsproject.cpp:521