File size: 9,704 Bytes
a5ffdcd | 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 | /****************************************************************************
**
** This file is part of the LibreCAD project, a 2D CAD program
**
** Copyright (C) 2018 A. Stebich (librecad@mail.lordofbikes.de)
** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
**
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file gpl-2.0.txt included in the
** packaging of this file.
**
** This program 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 General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
**
** This copyright notice MUST APPEAR in all copies of the script!
**
**********************************************************************/
#include<iostream>
#include "rs_undo.h"
#include <unordered_set>
#include "rs_debug.h"
#include "rs_undocycle.h"
/**
* @return Number of Cycles that can be undone.
*/
int RS_Undo::countUndoCycles() {
RS_DEBUG->print("RS_Undo::countUndoCycles");
return std::distance(undoList.cbegin(), m_redoPointer);
}
/**
* @return Number of Cycles that can be redone.
*/
int RS_Undo::countRedoCycles() {
RS_DEBUG->print("RS_Undo::countRedoCycles");
return std::distance(m_redoPointer, undoList.cend());
}
/**
* @return true, when current undo cycle has at least one undoable
*/
bool RS_Undo::hasUndoable()
{
return nullptr != currentCycle && !currentCycle->empty();
}
/**
* Adds an Undo Cycle at the current position in the list.
* All Cycles after the new one are removed and the Undoabels
* on them deleted.
*/
void RS_Undo::addUndoCycle(std::shared_ptr<RS_UndoCycle> undoCycle) {
RS_DEBUG->print("RS_Undo::addUndoCycle");
// undoList.insert(++undoPointer, i);
undoList.push_back(std::move(undoCycle));
m_redoPointer = undoList.cend();
updateUndoState();
RS_DEBUG->print("RS_Undo::addUndoCycle: ok");
}
/**
* Starts a new cycle for one undo step. Every undoable that is
* added after calling this method goes into this cycle.
*/
void RS_Undo::startUndoCycle(){
if (1 < ++refCount) {
// only the first fresh top call starts a new cycle
return;
}
// anything after the current existing undoCycle will be removed
// if there are undo cycles behind undoPointer
// remove obsolete entities and undoCycles
if (undoList.cend() != m_redoPointer) {
// collect remaining undoables
std::unordered_set<RS_Undoable*> keep;
for (auto it = undoList.begin(); it != m_redoPointer; ++it) {
for (RS_Undoable* undoable: (*it)->getUndoables()){
keep.insert(undoable);
}
}
// collect obsolete undoables
std::unordered_set<RS_Undoable*> obsolete;
for (auto it = m_redoPointer; it != undoList.end(); ++it) {
for (RS_Undoable* undoable: (*it)->getUndoables()){
obsolete.insert(undoable);
}
}
// delete obsolete undoables which are not in keep list
for (RS_Undoable* undoable: obsolete) {
if (keep.end() == keep.find(undoable)) {
removeUndoable(undoable);
}
}
// clean up obsolete undoCycles
undoList.erase(m_redoPointer, undoList.cend());
m_redoPointer = undoList.cend();
}
// alloc new undoCycle
currentCycle = std::make_shared<RS_UndoCycle>();
}
/**
* Adds an undoable to the current undo cycle.
*/
void RS_Undo::addUndoable(RS_Undoable* u) {
RS_DEBUG->print("RS_Undo::%s(): begin", __func__);
if( nullptr == currentCycle) {
RS_DEBUG->print( RS_Debug::D_CRITICAL, "RS_Undo::%s(): invalid currentCycle, possibly missing startUndoCycle()", __func__);
return;
}
currentCycle->addUndoable(u);
RS_DEBUG->print("RS_Undo::%s(): end", __func__);
}
/**
* Ends the current undo cycle.
*/
void RS_Undo::endUndoCycle(){
if (0 < refCount) {
// compensate nested calls of start-/endUndoCycle()
if( 0 < --refCount) {
// not the final nested call, nothing to do yet
return;
}
}
else {
RS_DEBUG->print(RS_Debug::D_WARNING, "Warning: RS_Undo::endUndoCycle() called without previous startUndoCycle() %d", refCount);
return;
}
if (hasUndoable()) {
// only keep the undoCycle, when it contains undoables
addUndoCycle(currentCycle);
}
updateUndoState();
currentCycle.reset(); // invalidate currentCycle for next startUndoCycle()
}
/**
* Undoes the last undo cycle.
*/
bool RS_Undo::undo() {
RS_DEBUG->print("RS_Undo::undo");
if (m_redoPointer == undoList.cbegin())
return false;
m_redoPointer = std::prev(m_redoPointer);
std::shared_ptr<RS_UndoCycle> uc = *m_redoPointer;
updateUndoState();
uc->changeUndoState();
return true;
}
/**
* Redoes the undo cycle which was at last undone.
*/
bool RS_Undo::redo() {
RS_DEBUG->print("RS_Undo::redo");
if (m_redoPointer != undoList.cend()) {
std::shared_ptr<RS_UndoCycle> uc = *m_redoPointer;
m_redoPointer = std::next(m_redoPointer);
updateUndoState();
uc->changeUndoState();
return true;
}
return false;
}
/**
* enable/disable redo/undo buttons in main application window
* Author: Dongxu Li
**/
void RS_Undo::updateUndoState() const{
bool redoAvailable = m_redoPointer != undoList.end();
bool undoAvailable = !undoList.empty() && m_redoPointer != undoList.cbegin();
fireUndoStateChanged(undoAvailable, redoAvailable);
}
void RS_Undo::collectUndoState(bool& undoAvailable, bool& redoAvailable) const {
redoAvailable = m_redoPointer != undoList.end();
undoAvailable = !undoList.empty() && m_redoPointer != undoList.cbegin();
}
/**
* Dumps the undo list to stdout.
*/
std::ostream& operator << (std::ostream& os, RS_Undo& l) {
os << "Undo List: " << "\n";
int position = std::distance(l.undoList.cbegin(), l.m_redoPointer);
os << " Redo Pointer is at: " << position << "\n";
for(auto it = l.undoList.cbegin(); it != l.undoList.cend(); ++it) {
os << ((it != l.m_redoPointer) ? " " : " -->");
os << *it << "\n";
}
return os;
}
/**
* Testing Undoables, Undo Cycles and the Undo container.
*/
#ifdef RS_TEST
bool RS_Undo::test() {
int i, k;
RS_UndoStub undo;
//RS_UndoCycle* c1;
RS_Undoable* u1;
std::cout << "Testing RS_Undo\n";
std::cout << " Adding 500 cycles..";
// Add 500 Undo Cycles with i Undoables in every Cycle
for (i=1; i<=500; ++i) {
//c1 = new RS_UndoCycle();
undo.startUndoCycle();
for (k=1; k<=i; ++k) {
u1 = new RS_Undoable();
//c1->
undo.addUndoable(u1);
}
//undo.addUndoCycle(c1);
undo.endUndoCycle();
}
std::cout << "OK\n";
assert(undo.countUndoCycles()==500);
assert(undo.countRedoCycles()==0);
std::cout << " Undo 500 cycles..";
// Undo all 500 cycles
for (i=1; i<=500; ++i) {
undo.undo();
}
std::cout << "OK\n";
assert(undo.countUndoCycles()==0);
assert(undo.countRedoCycles()==500);
std::cout << " Redo 500 cycles..";
// Redo all 500 cycles
for (i=1; i<=500; ++i) {
undo.redo();
}
std::cout << "OK\n";
assert(undo.countUndoCycles()==500);
assert(undo.countRedoCycles()==0);
std::cout << " Undo 250 cycles..";
// Undo all 500 cycles
for (i=1; i<=250; ++i) {
undo.undo();
}
std::cout << "OK\n";
assert(undo.countUndoCycles()==250);
assert(undo.countRedoCycles()==250);
std::cout << " Adding 10 cycles..";
for (i=1; i<=10; ++i) {
//c1 = new RS_UndoCycle();
undo.startUndoCycle();
for (k=1; k<=10; ++k) {
u1 = new RS_Undoable();
//c1->addUndoable(u1);
undo.addUndoable(u1);
}
//undo.addUndoCycle(c1);
undo.endUndoCycle();
}
std::cout << "OK\n";
assert(undo.countUndoCycles()==260);
assert(undo.countRedoCycles()==0);
std::cout << " Undo 5 cycles..";
for (i=1; i<=5; ++i) {
undo.undo();
}
std::cout << "OK\n";
assert(undo.countUndoCycles()==255);
assert(undo.countRedoCycles()==5);
std::cout << " Redo 5 cycles..";
for (i=1; i<=5; ++i) {
undo.redo();
}
std::cout << "OK\n";
assert(undo.countUndoCycles()==260);
assert(undo.countRedoCycles()==0);
std::cout << " Undo 15 cycles..";
for (i=1; i<=15; ++i) {
undo.undo();
}
std::cout << "OK\n";
assert(undo.countUndoCycles()==245);
assert(undo.countRedoCycles()==15);
std::cout << " Adding 1 cycle..";
for (i=1; i<=1; ++i) {
//c1 = new RS_UndoCycle();
undo.startUndoCycle();
for (k=1; k<=10; ++k) {
u1 = new RS_Undoable();
//c1->addUndoable(u1);
undo.addUndoable(u1);
}
//undo.addUndoCycle(c1);
undo.endUndoCycle();
}
std::cout << "OK\n";
assert(undo.countUndoCycles()==246);
assert(undo.countRedoCycles()==0);
return true;
}
#endif
|