summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Client/Code/Core/Commands/CmdStack.cpp
blob: a1c6e943e3aa7c8a0d352f7b51d284e8a7768581 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/****************************************************************************
**
** Copyright (C) 2002 NVIDIA Corporation.
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "CmdStack.h"
#include "Cmd.h"
#include "CmdStackModifier.h"

CCmdStack::CCmdStack()
{

}

CCmdStack::~CCmdStack()
{
    // Delete all the commands that are in the queues.
    Clear();
}

//=============================================================================
/**
 * Add a command to be executed.
 * If the command can be undone then it will be added to the undo/redo list.
 * Any commands that are in the redo list will be deleted and the redo list will
 * be flushed. If this command can be merged with the previous command then it
 * will be, after it is executed. If it is merged then it will be deleted
 * immediately. If the command cannot be undone then it will be deleted
 * immediately.
 *
 * @param inCommand the command to be executed.
 * @return bool true if inCommand was deleted, false otherwise.
 */
//=============================================================================
bool CCmdStack::ExecuteCommand(CCmd *inCommand)
{
    // Default the delete to true.
    bool shouldDeleteCommand = true;
    // Execute the command.
    unsigned long theUpdateMask = inCommand->Do();

    // If the listener is not null then do the notifications.
    if (m_Listener) {
        m_Listener->CommandUpdate(theUpdateMask);

        // Set the modified flag if it needs to be set.
        if (inCommand->ShouldSetModifiedFlag()) {
            m_Listener->SetCommandModifiedFlag(true);
        }
    }

    // If the command can be undone then see if it needs to be merged or added to the undo list.
    if (inCommand->IsUndoable()) {
        // If there are previous commands then see if the command should be merged.
        if (m_UndoList.size() != 0) {
            CCmd *thePreviousCommand = m_UndoList.back();
            // If the previous command is committed then add the command.
            if (thePreviousCommand->IsCommitted()) {
                // Add the command and don't delete it.
                m_UndoList.push_back(inCommand);
                shouldDeleteCommand = false;
            }
            // Check if it can be merged.
            else if (thePreviousCommand->CanMerge(inCommand)) {
                inCommand->Merge(thePreviousCommand);
                delete thePreviousCommand;
                m_UndoList.pop_back();
                m_UndoList.push_back(inCommand);
                shouldDeleteCommand = false;
            } else {
                // Add the command and don't delete it.
                m_UndoList.push_back(inCommand);
                shouldDeleteCommand = false;
            }
        } else {
            // This is the first command in the list.
            m_UndoList.push_back(inCommand);
            shouldDeleteCommand = false;
        }

        // Cannot redo after a new command that can be undone.
        EmptyRedoStack();
    }

    // Delete the command if it needs to be.
    if (shouldDeleteCommand) {
        delete inCommand;
    }

    // Keep the undo list below it's max size.
    while (m_UndoList.size() > m_MaxUndoStackSize) {
        CCmd *theLastCommand = m_UndoList.front();
        delete theLastCommand;
        m_UndoList.erase(m_UndoList.begin());
    }

    return shouldDeleteCommand;
}

//=============================================================================
/**
 * Perform a single undo operation.
 * This will take the last command to be executed and undo it then push it onto
 * the redo stack. The command is set to committed so it will not be merged with
 * any other commands.
 */
//=============================================================================
void CCmdStack::Undo()
{
    if (m_CommandStackModifier) {
        if (m_CommandStackModifier->preUndo() == false)
            return;
    }

    if (m_UndoList.size() > 0) {
        m_undoingOrRedoing = true;
        CCmd *theLastCommand = m_UndoList.back();
        m_UndoList.pop_back();

        unsigned long theUpdateMask = theLastCommand->Undo();

        // Once a command is undone then it is considered committed. Prevents merging after this has
        // been redone.
        theLastCommand->SetCommitted(true);

        m_RedoList.push_back(theLastCommand);

        // If the listener is not null then do the notifications.
        if (m_Listener) {
            m_Listener->CommandUpdate(theUpdateMask);

            // Set the modified flag if it needs to be set.
            if (theLastCommand->ShouldSetModifiedFlag())
                m_Listener->SetCommandModifiedFlag(true);
        }
        m_undoingOrRedoing = false;
    }
}

//=============================================================================
/**
 * Perform a single redo operation.
 * This will take the last command to be undone and redo it, then push it onto
 * the undo stack.
 */
//=============================================================================
void CCmdStack::Redo()
{
    if (m_RedoList.size() > 0) {
        m_undoingOrRedoing = true;
        CCmd *theLastCommand = m_RedoList.back();
        m_RedoList.pop_back();

        unsigned long theUpdateMask = theLastCommand->Do();

        m_UndoList.push_back(theLastCommand);

        // If the listener is not null then do the notifications.
        if (m_Listener) {
            m_Listener->CommandUpdate(theUpdateMask);

            // Set the modified flag if it needs to be set.
            if (theLastCommand->ShouldSetModifiedFlag())
                m_Listener->SetCommandModifiedFlag(true);
        }
        m_undoingOrRedoing = false;
    }
}

//=============================================================================
/**
 * @return true if an undo operation can be done.
 */
//=============================================================================
bool CCmdStack::CanUndo()
{
    if (m_CommandStackModifier && m_CommandStackModifier->canUndo())
        return true;
    return m_UndoList.size() > 0;
}

//=============================================================================
/**
 * @return true if a redo operation can be done.
 */
//=============================================================================
bool CCmdStack::CanRedo()
{
    return m_RedoList.size() > 0;
}

bool CCmdStack::isUndoingOrRedoing() const
{
    return m_undoingOrRedoing;
}

//=============================================================================
/**
 * Remove the last command from the undo list
 */
//=============================================================================
void CCmdStack::RemoveLastUndo()
{
    if (m_UndoList.size() > 0)
        m_UndoList.pop_back();
}

//=============================================================================
/**
 * Commit the last command on the undo stack.
 * This marks the last command as committed and prevents it from merging with
 * any future commands.
 */
//=============================================================================
void CCmdStack::CommitLastCommand()
{
    if (m_UndoList.size() > 0) {
        CCmd *theLastCommand = m_UndoList.back();
        theLastCommand->SetCommitted(true);
    }
}

//=============================================================================
/**
 * Clear the entire undo/redo stack.
 * This is used when a new presentation is loaded and all the previous commands
 * are no longer needed. All commands will be deleted.
 */
//=============================================================================
void CCmdStack::Clear()
{
    EmptyRedoStack();
    EmptyUndoStack();
}

//=============================================================================
/**
 * Clear any invalid commands
 * This is called when commands are invalidated due objects being deleted.
 */
//=============================================================================
void CCmdStack::ClearInvalid()
{
    TCmdList::iterator thePos = m_UndoList.begin();
    while (thePos != m_UndoList.end()) {
        if ((*thePos)->IsInvalidated()) // continue with the next item that comes after.
        {
            delete (*thePos);
            thePos = m_UndoList.erase(thePos);
        } else
            ++thePos;
    }
    thePos = m_RedoList.begin();
    while (thePos != m_RedoList.end()) {
        if ((*thePos)->IsInvalidated()) // continue with the next item that comes after.
        {
            delete (*thePos);
            thePos = m_RedoList.erase(thePos);
        } else
            ++thePos;
    }
}

//=============================================================================
/**
 * Clears out the undo stack and deletes all the commands within it.
 * This is used on delete and when a new presentation is loaded up.
 */
//=============================================================================
void CCmdStack::EmptyUndoStack()
{
    TCmdList::iterator thePos;
    for (thePos = m_UndoList.begin(); thePos != m_UndoList.end(); ++thePos) {
        delete (*thePos);
    }

    m_UndoList.clear();
}

//=============================================================================
/**
 * Clears out the redo stack and deletes all the commands within it.
 * This is used after a new command is added redo can no longer be done. The
 * commands can be deleted because they can never be executed again.
 */
//=============================================================================
void CCmdStack::EmptyRedoStack()
{
    TCmdList::iterator thePos;
    for (thePos = m_RedoList.begin(); thePos != m_RedoList.end(); ++thePos) {
        delete (*thePos);
    }

    m_RedoList.clear();
}

//=============================================================================
/**
 * Gets the description of the next Undo command.
 *
 * @return the description or "" if no undo command is available.
 */
//=============================================================================
QString CCmdStack::GetUndoDescription()
{
    QString theDescription;
    if (m_UndoList.size() > 0) {
        theDescription = m_UndoList.back()->ToString();
    }
    return theDescription;
}

//=============================================================================
/**
 * Gets the description of the next Redo command.
 *
 * @return the description of "" if no redo command is available.
 */
//=============================================================================
QString CCmdStack::GetRedoDescription()
{
    QString theDescription;
    if (m_RedoList.size() > 0) {
        theDescription = m_RedoList.back()->ToString();
    }
    return theDescription;
}

void CCmdStack::SetCommandStackModifier(ICmdStackModifier *inModifier)
{
    m_CommandStackModifier = inModifier;
}

//=============================================================================
/**
 * Set a listener on this command stack which will be notified every time a
 * command has executed which should set the modified flag.
 *
 * @param inListener the listener to be notified.
 */
//=============================================================================
void CCmdStack::SetModificationListener(CModificationListener *inListener)
{
    m_Listener = inListener;
}

//=============================================================================
/**
 * Get the list of commands that can be undone.
 */
CCmdStack::TCmdList CCmdStack::GetUndoStack()
{
    return m_UndoList;
}

//=============================================================================
/**
 * Get the list of commands that can be redone.
 */
CCmdStack::TCmdList CCmdStack::GetRedoStack()
{
    return m_RedoList;
}