QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 }
QgsLayoutUndoObjectInterface::createCommand
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.
qgslayoutundostack.h
QgsLayoutUndoStack::blockCommands
void blockCommands(bool blocked)
Sets whether undo commands for the layout should be temporarily blocked.
Definition: qgslayoutundostack.cpp:86
QgsLayoutUndoStack::isBlocked
bool isBlocked() const
Returns true if undo commands are currently blocked.
Definition: qgslayoutundostack.cpp:99
QgsLayoutUndoStack::endMacro
void endMacro()
Ends a macro command.
Definition: qgslayoutundostack.cpp:36
QgsLayoutUndoStack::undoRedoOccurredForItems
void undoRedoOccurredForItems(QSet< QString > itemUuids)
Emitted when an undo or redo action has occurred, which affected a set of layout itemUuids.
QgsProject::setDirty
void setDirty(bool b=true)
Flag the project as dirty (modified).
Definition: qgsproject.cpp:576
QgsLayoutUndoStack::QgsLayoutUndoStack
QgsLayoutUndoStack(QgsLayout *layout)
Constructor for QgsLayoutUndoStack, for the specified parent layout.
Definition: qgslayoutundostack.cpp:23
QgsLayoutItem
Base class for graphical items within a QgsLayout.
Definition: qgslayoutitem.h:112
QgsLayoutUndoStack::cancelCommand
void cancelCommand()
Cancels the active command, discarding it without pushing to the undo history.
Definition: qgslayoutundostack.cpp:68
qgslayout.h
QgsLayoutUndoObjectInterface
Interface for layout objects which support undo/redo commands.
Definition: qgslayoutundocommand.h:128
QgsLayoutUndoStack::push
void push(QUndoCommand *command)
Manually pushes a command to the stack, and takes ownership of the command.
Definition: qgslayoutundostack.cpp:104
QgsLayoutUndoStack::beginMacro
void beginMacro(const QString &commandText)
Starts a macro command, with the given descriptive commandText.
Definition: qgslayoutundostack.cpp:30
QgsLayoutItem::uuid
virtual QString uuid() const
Returns the item identification string.
Definition: qgslayoutitem.h:345
QgsLayoutUndoStack::endCommand
void endCommand()
Saves final state of an object and pushes the active command to the undo history.
Definition: qgslayoutundostack.cpp:53
QgsLayout
Base class for layouts, which can contain items such as maps, labels, scalebars, etc.
Definition: qgslayout.h:50
QgsLayoutUndoStack::beginCommand
void beginCommand(QgsLayoutUndoObjectInterface *object, const QString &commandText, int id=0)
Begins a new undo command for the specified object.
Definition: qgslayoutundostack.cpp:42
QgsLayoutUndoStack::stack
QUndoStack * stack()
Returns a pointer to the internal QUndoStack.
Definition: qgslayoutundostack.cpp:76
QgsLayout::project
QgsProject * project() const
The project associated with the layout.
Definition: qgslayout.cpp:132
QgsLayoutUndoStack::notifyUndoRedoOccurred
void notifyUndoRedoOccurred(QgsLayoutItem *item)
Notifies the stack that an undo or redo action occurred for a specified item.
Definition: qgslayoutundostack.cpp:81
qgsproject.h