Branch data Line data Source code
1 : : #include "UndoManager.h"
2 : :
3 : : #include <QDateTime>
4 : : #include <QLoggingCategory>
5 : :
6 [ + + + - : 427 : Q_LOGGING_CATEGORY(lcUndo, "mailjd.undo")
+ - - - ]
7 : :
8 : 145 : UndoManager::UndoManager(QObject *parent) : QObject(parent) {}
9 : :
10 : 362 : void UndoManager::push(const QString &description,
11 : : std::function<void()> undoAction) {
12 : 362 : UndoEntry entry;
13 : 362 : entry.description = description;
14 : 362 : entry.undo = std::move(undoAction);
15 : 362 : entry.timestamp = QDateTime::currentMSecsSinceEpoch();
16 : :
17 [ + - ]: 362 : m_stack.append(std::move(entry));
18 : :
19 : : // Enforce max depth — discard oldest entries
20 [ + + ]: 397 : while (m_stack.size() > kMaxUndoDepth) {
21 : 35 : m_stack.removeFirst();
22 : : }
23 : :
24 [ + - + - : 724 : qCInfo(lcUndo) << "Push:" << description
+ - + - +
+ ]
25 [ + - + - : 362 : << "(stack size:" << m_stack.size() << ")";
+ - ]
26 [ + - ]: 362 : emit undoAvailable(true);
27 : 362 : }
28 : :
29 : 40 : bool UndoManager::undo() {
30 [ + + ]: 40 : if (m_stack.isEmpty()) {
31 [ + - + - : 10 : qCInfo(lcUndo) << "Undo: stack empty";
+ - + + ]
32 : 5 : return false;
33 : : }
34 : :
35 [ + - ]: 35 : UndoEntry entry = m_stack.takeLast();
36 [ + - + - : 70 : qCInfo(lcUndo) << "Undo:" << entry.description
+ - + - +
+ ]
37 [ + - + - : 35 : << "(stack size:" << m_stack.size() << ")";
+ - ]
38 : :
39 [ + + ]: 35 : if (entry.undo) {
40 [ + - ]: 34 : entry.undo();
41 : : }
42 : :
43 [ + - ]: 35 : emit undoPerformed(entry.description);
44 [ + - ]: 35 : emit undoAvailable(!m_stack.isEmpty());
45 : 35 : return true;
46 : 35 : }
47 : :
48 : 36 : bool UndoManager::canUndo() const { return !m_stack.isEmpty(); }
49 : :
50 : 14 : QString UndoManager::lastDescription() const {
51 [ + + ]: 14 : if (m_stack.isEmpty())
52 : 5 : return {};
53 : 9 : return m_stack.last().description;
54 : : }
55 : :
56 : 26 : int UndoManager::stackSize() const { return m_stack.size(); }
57 : :
58 : 77 : void UndoManager::clear() {
59 [ + + ]: 77 : if (m_stack.isEmpty())
60 : 52 : return;
61 : 25 : m_stack.clear();
62 [ + - + - : 50 : qCInfo(lcUndo) << "Stack cleared";
+ - + + ]
63 : 25 : emit undoAvailable(false);
64 : : }
|