/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2010-2012 University of Bonn. All rights reserved. * * * This file is part of MoleCuilder. * * MoleCuilder is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * MoleCuilder 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 MoleCuilder. If not, see . */ /* * ActionHistory.cpp * * Created on: Mar 25, 2010 * Author: crueger */ // include config.h #ifdef HAVE_CONFIG_H #include #endif //#include "CodePatterns/MemDebug.hpp" #include "Actions/ActionHistory.hpp" #include "Actions/ActionExceptions.hpp" #include #include "CodePatterns/Assert.hpp" using namespace MoleCuilder; ActionHistory::ActionHistory() { Action::createStaticStateEntities(); } ActionHistory::~ActionHistory() { Action::removeStaticStateEntities(); } void ActionHistory::undoLast(){ if (!hasUndo()){ LOG(1, "Undo performed when the undo-queue was empty."); return; } //ASSERT(history.size(),"Undo performed when the undo-queue was empty"); HistoryElement elem = history.back(); LOG(1, "INFO: Undoing action " << elem.action->getName()); while ((!marked.empty()) && (marked.top() == &elem)) { LOG(2, "DEBUG: Undoing marked item in ActionHistory, hence resetting mark."); marked.pop(); } history.pop_back(); ActionState::ptr newState = elem.action->undo(elem.state); if (newState == Action::failure) throw ActionFailureException() << ActionNameString(elem.action->getName()); yrotsih.push_back(HistoryElement(elem.action,newState)); } void ActionHistory::redoLast(){ if (!hasRedo()){ LOG(1, "Redo performed when the redo-queue was empty."); return; } //ASSERT(yrotsih.size(),"Redo performed when the redo-queue was empty"); HistoryElement elem = yrotsih.back(); LOG(1, "INFO: Redoing action " << elem.action->getName()); yrotsih.pop_back(); ActionState::ptr oldState = elem.action->redo(elem.state); if (oldState == Action::failure) throw ActionFailureException() << ActionNameString(elem.action->getName()); history.push_back(HistoryElement(elem.action,oldState)); } bool ActionHistory::hasUndo(){ return history.size()>0; } bool ActionHistory::hasRedo(){ return yrotsih.size()>0; } void ActionHistory::setMark() { HistoryElement * const mark = &(history.back()); if (mark == NULL) { ELOG(2, "setMark - must not push NULL as element"); return; } marked.push(mark); } void ActionHistory::unsetMark() { if (!marked.empty()) { marked.pop(); } else { ELOG(2, "unsetMark - stack is already empty."); } } void ActionHistory::undoTillMark() { if (marked.empty()) { ELOG(2, "There is no undo mark, not undoing anything."); } else { const HistoryElement * const mark = marked.top(); while (mark != &(history.back())) undoLast(); marked.pop(); } } void ActionHistory::addElement(Action* action,ActionState::ptr state){ yrotsih.clear(); history.push_back(HistoryElement(action,state)); } void ActionHistory::clear(){ history.clear(); yrotsih.clear(); } //void ActionHistory::init(){ // ActionHistory *hist = new ActionHistory(); // setInstance(hist); //} /****************** Contained actions *******************/