Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
composite-undo-stack-observer.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Heavily inspired by Inkscape::XML::CompositeNodeObserver.
4 *
5 * Authors:
6 * David Yip <yipdw@rose-hulman.edu>
7 *
8 * Copyright (c) 2005 Authors
9 *
10 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
11 */
12
13#include <algorithm>
14
16#include "xml/event.h"
17
18namespace Inkscape {
19
20void
22{
23 if (!this->_iterating) {
24 _active.emplace_back(observer);
25 } else {
26 _pending.emplace_back(observer);
27 }
28}
29
30void
32{
33 if (!this->_iterating) {
34 // logical-or operator short-circuits
35 this->_remove_one(this->_active, observer) || this->_remove_one(this->_pending, observer);
36 } else {
37 this->_mark_one(this->_active, observer) || this->_mark_one(this->_pending, observer);
38 }
39}
40
41void
43{
44 this->_lock();
45 for (auto &i : _active) {
46 if (!i.to_remove) {
47 i.issueUndo(log);
48 }
49 }
50 this->_unlock();
51}
52
53void
55{
56
57 this->_lock();
58 for (auto &i : _active) {
59 if (!i.to_remove) {
60 i.issueRedo(log);
61 }
62 }
63 this->_unlock();
64}
65
66void
68{
69 this->_lock();
70 for (auto &i : _active) {
71 if (!i.to_remove) {
72 i.issueUndoCommit(log);
73 }
74 }
75 this->_unlock();
76}
77
78void
80{
81 _lock();
82 for (auto &i : _active) {
83 if (!i.to_remove) {
84 i.issueUndoExpired(log);
85 }
86 }
87 _unlock();
88}
89
90void
92{
93 this->_lock();
94 for (auto &i : _active) {
95 if (!i.to_remove) {
96 i.issueClearUndo();
97 }
98 }
99 this->_unlock();
100}
101
102void
104{
105 this->_lock();
106 for (auto &i : _active) {
107 if (!i.to_remove) {
108 i.issueClearRedo();
109 }
110 }
111 this->_unlock();
112}
113
114bool
116{
117 UndoStackObserverRecord eq_comp(o);
118
119 auto i = std::find(list.begin(), list.end(), eq_comp);
120
121 if (i != list.end()) {
122 list.erase(i);
123 return true;
124 } else {
125 return false;
126 }
127}
128
129bool
131{
132 UndoStackObserverRecord eq_comp(o);
133
134 auto i = std::find(list.begin(), list.end(), eq_comp);
135
136 if (i != list.end()) {
137 i->to_remove = true;
138 return true;
139 } else {
140 return false;
141 }
142}
143
144void
146{
147 if (!--_iterating) {
148 // Remove marked observers
149 const auto pred = [](UndoStackObserverRecord const &i) -> bool { return i.to_remove; };
150
151 auto newEnd = std::remove_if(_active.begin(), _active.end(), pred);
152 _active.erase(newEnd, _active.end());
153
154 newEnd = std::remove_if(_pending.begin(), _pending.end(), pred);
155 _pending.erase(newEnd, _pending.end());
156
157 // Merge pending and active
158 _active.insert(_active.end(), _pending.begin(), _pending.end());
159 _pending.clear();
160 }
161}
162
163}
void notifyUndoCommitEvent(Event *log) override
Notify all registered UndoStackObservers of an event log being committed to the undo stack.
bool _mark_one(UndoObserverRecordList &list, UndoStackObserver &rec)
void notifyRedoEvent(Event *log) override
Notify all registered UndoStackObservers of a redo event.
void notifyClearUndoEvent() override
Triggered when the undo log is cleared.
void add(UndoStackObserver &observer)
Add an UndoStackObserver.
void remove(UndoStackObserver &observer)
Remove an UndoStackObserver.
bool _remove_one(UndoObserverRecordList &list, UndoStackObserver &rec)
void notifyUndoExpired(Event *log) override
Notify all registered UndoStackObservers of an event log being expired from the back of the undo stac...
void notifyClearRedoEvent() override
Triggered when the redo log is cleared.
void notifyUndoEvent(Event *log) override
Notify all registered UndoStackObservers of an undo event.
std::vector< UndoStackObserverRecord > UndoObserverRecordList
A list of UndoStackObserverRecords, used to aggregate multiple UndoStackObservers.
Observes changes made to the undo and redo stacks.
NodeObserver const * observer
Helper class to stream background task notifications as a series of messages.
Piecewise< SBasis > log(Interval in)
Definition pw-funcs.cpp:37
Event object representing a change of the XML document.