/*
* 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 .
*/
/*
* AddAction.cpp
*
* Created on: May 9, 2010
* Author: heber
*/
// include config.h
#ifdef HAVE_CONFIG_H
#include
#endif
//#include "CodePatterns/MemDebug.hpp"
#include "Actions/UndoRedoHelpers.hpp"
#include "Descriptors/AtomIdDescriptor.hpp"
#include "Atom/atom.hpp"
#include "Element/element.hpp"
#include "CodePatterns/Log.hpp"
#include "molecule.hpp"
#include "LinearAlgebra/Vector.hpp"
#include "CodePatterns/Verbose.hpp"
#include "World.hpp"
#include
#include
#include "Actions/AtomAction/AddAction.hpp"
using namespace MoleCuilder;
// and construct the stuff
#include "AddAction.def"
#include "Action_impl_pre.hpp"
/** =========== define the function ====================== */
atom * getNewAtom(const AtomAddAction::AtomAddParameters &_params)
{
atom * first = World::getInstance().createAtom();
first->setType(_params.elemental.get());
first->setPosition(_params.position.get());
return first;
}
ActionState::ptr AtomAddAction::performCall() {
// execute action
std::vector molecules = World::getInstance().getSelectedMolecules();
std::vector Walkers;
moleculeId_t molId = -1;
if (!molecules.empty()) {
if (molecules.size() == 1) {
atom *first = getNewAtom(params);
molecule *mol = *molecules.begin();
LOG(1, "Adding new atom with element " << first->getType()->getName()
<< " at " << (first->getPosition()) << " to selected molecule "
<< mol->getName()+".");
mol->AddAtom(first);
molId = mol->getId();
Walkers.push_back(AtomicInfo(*first));
} else {
ELOG(1, "Multiple molecules selected. Cannot add atom to multiple molecules.");
}
} else {
atom * first = getNewAtom(params);
molecule *mol = World::getInstance().createMolecule();
mol->setName("none");
mol->AddAtom(first);
LOG(1, "Adding new atom with element " << first->getType()->getName()
<< " at " << (first->getPosition()) << " to new molecule.");
molId = mol->getId();
Walkers.push_back(AtomicInfo(*first));
}
WalkersInMolecule_t WalkersInMolecule = { { molId, Walkers } };
// create undo state
AtomAddState *UndoState = new AtomAddState(WalkersInMolecule, params);
if ((molecules.size() > 1) || (Walkers.empty()))
return Action::failure;
else
return ActionState::ptr(UndoState);
}
ActionState::ptr AtomAddAction::performUndo(ActionState::ptr _state){
AtomAddState *state = assert_cast(_state.get());
// simple remove again all previously added atoms
RemoveAtomsFromAtomicInfo(state->WalkersInMolecule.begin()->second);
return ActionState::ptr(_state);
}
ActionState::ptr AtomAddAction::performRedo(ActionState::ptr _state) {
AtomAddState *state = assert_cast(_state.get());
// add all removed atoms again
if (AddMoleculesFromAtomicInfo(state->WalkersInMolecule))
return ActionState::ptr(_state);
else {
STATUS("Failed to re-add removed atoms.");
return Action::failure;
}
}
bool AtomAddAction::canUndo() {
return true;
}
bool AtomAddAction::shouldUndo() {
return true;
}
/** =========== end of function ====================== */