File: undo_redo_manager.cpp

package info (click to toggle)
sight 25.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,180 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (179 lines) | stat: -rw-r--r-- 4,960 bytes parent folder | download | duplicates (2)
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
/************************************************************************
 *
 * Copyright (C) 2009-2023 IRCAD France
 * Copyright (C) 2012-2017 IHU Strasbourg
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "ui/history/undo_redo_manager.hpp"

#include <core/spy_log.hpp>

namespace sight::ui::history
{

//-----------------------------------------------------------------------------

undo_redo_manager::undo_redo_manager(std::size_t _max_memory, std::size_t _max_commands) :
    m_max_memory(_max_memory),
    m_max_commands(_max_commands == 0 ? 1 : _max_commands)
{
    SIGHT_ASSERT("The number of commands must be greater than 0", _max_commands > 0);
}

//-----------------------------------------------------------------------------

bool undo_redo_manager::enqueue(command::sptr _cmd)
{
    if(m_max_memory == 0)
    {
        SIGHT_WARN("Cannot add a command because maxMemory is 0.");
        return false;
    }

    if(_cmd->size() > m_max_memory)
    {
        SIGHT_WARN("The current command is bigger than the maximum history size");
        return false;
    }

    // Remove all commands following the current history point.
    if(!m_command_queue.empty())
    {
        for(std::size_t i = m_command_queue.size() - 1 ; i > std::size_t(m_command_index) ; --i)
        {
            m_used_memory -= m_command_queue[i]->size();
            m_command_queue.pop_back();
        }
    }

    // Remove the oldest command if we reached the maximum number of commands.
    if(m_max_commands == m_command_queue.size())
    {
        pop_front();
    }

    // Remove the oldest commands if we reached the maximum history size.
    while(m_used_memory + _cmd->size() > m_max_memory)
    {
        pop_front();
    }

    m_command_queue.push_back(_cmd);
    m_used_memory  += _cmd->size();
    m_command_index = static_cast<std::int64_t>(m_command_queue.size() - 1);

    return true;
}

//-----------------------------------------------------------------------------

bool undo_redo_manager::redo()
{
    bool success = false;

    if(std::size_t(m_command_index) != m_command_queue.size() - 1)
    {
        m_command_index++;
        success = m_command_queue[std::size_t(m_command_index)]->redo();
    }

    return success;
}

//-----------------------------------------------------------------------------

bool undo_redo_manager::undo()
{
    bool success = false;

    if(m_command_index > -1)
    {
        success = m_command_queue[std::size_t(m_command_index)]->undo();
        m_command_index--;
    }

    return success;
}

//-----------------------------------------------------------------------------

bool undo_redo_manager::can_undo() const
{
    return m_command_index > -1;
}

//-----------------------------------------------------------------------------

bool undo_redo_manager::can_redo() const
{
    return (std::size_t(m_command_index) != this->get_command_count() - 1) && (this->get_command_count() > 0);
}

//-----------------------------------------------------------------------------

void undo_redo_manager::clear()
{
    m_command_queue.clear();
    m_command_index = -1;
    m_used_memory   = 0;
}

//-----------------------------------------------------------------------------

std::size_t undo_redo_manager::get_command_count() const
{
    return m_command_queue.size();
}

//-----------------------------------------------------------------------------

void undo_redo_manager::set_command_count(std::size_t _cmd_count)
{
    this->clear();
    m_max_commands = _cmd_count;
}

//-----------------------------------------------------------------------------

std::size_t undo_redo_manager::get_history_size() const
{
    return m_used_memory;
}

//-----------------------------------------------------------------------------

void undo_redo_manager::set_history_size(std::size_t _hist_size)
{
    this->clear();
    m_max_memory = _hist_size;
}

//-----------------------------------------------------------------------------

void undo_redo_manager::pop_front()
{
    auto it = m_command_queue.begin();

    m_used_memory -= (*it)->size();
    m_command_queue.pop_front();
}

//-----------------------------------------------------------------------------

} // namespace sight::ui::history