source: src/Actions/AtomAction/AddAction.cpp@ 399c69

Candidate_v1.7.0 stable
Last change on this file since 399c69 was 399c69, checked in by Frederik Heber <frederik.heber@…>, 20 months ago

Rewrote AddAction to use UndoRedoHelpers.

  • Property mode set to 100644
File size: 4.0 KB
RevLine 
[bcf653]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[0aa122]4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[94d5ac6]5 *
6 *
7 * This file is part of MoleCuilder.
8 *
9 * MoleCuilder is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * MoleCuilder is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
[bcf653]21 */
22
[97ebf8]23/*
24 * AddAction.cpp
25 *
26 * Created on: May 9, 2010
27 * Author: heber
28 */
29
[bf3817]30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
[9eb71b3]35//#include "CodePatterns/MemDebug.hpp"
[112b09]36
[399c69]37#include "Actions/UndoRedoHelpers.hpp"
[ea2830]38#include "Descriptors/AtomIdDescriptor.hpp"
[6f0841]39#include "Atom/atom.hpp"
[3bdb6d]40#include "Element/element.hpp"
[ad011c]41#include "CodePatterns/Log.hpp"
[f0a3ec]42#include "molecule.hpp"
[57f243]43#include "LinearAlgebra/Vector.hpp"
[ad011c]44#include "CodePatterns/Verbose.hpp"
[97ebf8]45#include "World.hpp"
46
47#include <iostream>
48#include <string>
49
[8bb05e]50#include "Actions/AtomAction/AddAction.hpp"
[9d33ba]51
[ce7fdc]52using namespace MoleCuilder;
53
[8bb05e]54// and construct the stuff
55#include "AddAction.def"
56#include "Action_impl_pre.hpp"
57/** =========== define the function ====================== */
[ceaab1]58atom * getNewAtom(const AtomAddAction::AtomAddParameters &_params)
59{
[85537a]60 atom * first = World::getInstance().createAtom();
[ceaab1]61 first->setType(_params.elemental.get());
62 first->setPosition(_params.position.get());
63
64 return first;
65}
66
[399c69]67ActionState::ptr AtomAddAction::performCall() {
68 // execute action
69 std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
[8ad68b]70
[399c69]71 std::vector<AtomicInfo> Walkers;
72 moleculeId_t molId = -1;
73 if (!molecules.empty()) {
74 if (molecules.size() == 1) {
75 atom *first = getNewAtom(params);
76 molecule *mol = *molecules.begin();
[ceaab1]77 LOG(1, "Adding new atom with element " << first->getType()->getName()
78 << " at " << (first->getPosition()) << " to selected molecule "
79 << mol->getName()+".");
80 mol->AddAtom(first);
[399c69]81 molId = mol->getId();
82 Walkers.push_back(AtomicInfo(*first));
83 } else {
84 ELOG(1, "Multiple molecules selected. Cannot add atom to multiple molecules.");
[ceaab1]85 }
86 } else {
[399c69]87 atom * first = getNewAtom(params);
[ceaab1]88 molecule *mol = World::getInstance().createMolecule();
89 mol->setName("none");
90 mol->AddAtom(first);
91 LOG(1, "Adding new atom with element " << first->getType()->getName()
92 << " at " << (first->getPosition()) << " to new molecule.");
[399c69]93 molId = mol->getId();
94 Walkers.push_back(AtomicInfo(*first));
[97ebf8]95 }
[399c69]96 WalkersInMolecule_t WalkersInMolecule = { { molId, Walkers } };
[ceaab1]97
98
[399c69]99 // create undo state
100 AtomAddState *UndoState = new AtomAddState(WalkersInMolecule, params);
[8ad68b]101
[399c69]102 if ((molecules.size() > 1) || (Walkers.empty()))
[ceaab1]103 return Action::failure;
104 else
[399c69]105 return ActionState::ptr(UndoState);
[97ebf8]106}
107
[399c69]108ActionState::ptr AtomAddAction::performUndo(ActionState::ptr _state){
[ea2830]109 AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
[97ebf8]110
[399c69]111 // simple remove again all previously added atoms
112 RemoveAtomsFromAtomicInfo(state->WalkersInMolecule.begin()->second);
[ea2830]113
[b5b01e]114 return ActionState::ptr(_state);
[97ebf8]115}
116
[399c69]117ActionState::ptr AtomAddAction::performRedo(ActionState::ptr _state) {
[ea2830]118 AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
119
[399c69]120 // add all removed atoms again
121 if (AddMoleculesFromAtomicInfo(state->WalkersInMolecule))
122 return ActionState::ptr(_state);
123 else {
124 STATUS("Failed to re-add removed atoms.");
[ceaab1]125 return Action::failure;
[ea2830]126 }
[97ebf8]127}
128
129bool AtomAddAction::canUndo() {
[ea2830]130 return true;
[97ebf8]131}
132
133bool AtomAddAction::shouldUndo() {
[ea2830]134 return true;
[97ebf8]135}
[8bb05e]136/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.