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
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
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/>.
21 */
22
23/*
24 * AddAction.cpp
25 *
26 * Created on: May 9, 2010
27 * Author: heber
28 */
29
30// include config.h
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34
35//#include "CodePatterns/MemDebug.hpp"
36
37#include "Actions/UndoRedoHelpers.hpp"
38#include "Descriptors/AtomIdDescriptor.hpp"
39#include "Atom/atom.hpp"
40#include "Element/element.hpp"
41#include "CodePatterns/Log.hpp"
42#include "molecule.hpp"
43#include "LinearAlgebra/Vector.hpp"
44#include "CodePatterns/Verbose.hpp"
45#include "World.hpp"
46
47#include <iostream>
48#include <string>
49
50#include "Actions/AtomAction/AddAction.hpp"
51
52using namespace MoleCuilder;
53
54// and construct the stuff
55#include "AddAction.def"
56#include "Action_impl_pre.hpp"
57/** =========== define the function ====================== */
58atom * getNewAtom(const AtomAddAction::AtomAddParameters &_params)
59{
60 atom * first = World::getInstance().createAtom();
61 first->setType(_params.elemental.get());
62 first->setPosition(_params.position.get());
63
64 return first;
65}
66
67ActionState::ptr AtomAddAction::performCall() {
68 // execute action
69 std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
70
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();
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);
81 molId = mol->getId();
82 Walkers.push_back(AtomicInfo(*first));
83 } else {
84 ELOG(1, "Multiple molecules selected. Cannot add atom to multiple molecules.");
85 }
86 } else {
87 atom * first = getNewAtom(params);
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.");
93 molId = mol->getId();
94 Walkers.push_back(AtomicInfo(*first));
95 }
96 WalkersInMolecule_t WalkersInMolecule = { { molId, Walkers } };
97
98
99 // create undo state
100 AtomAddState *UndoState = new AtomAddState(WalkersInMolecule, params);
101
102 if ((molecules.size() > 1) || (Walkers.empty()))
103 return Action::failure;
104 else
105 return ActionState::ptr(UndoState);
106}
107
108ActionState::ptr AtomAddAction::performUndo(ActionState::ptr _state){
109 AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
110
111 // simple remove again all previously added atoms
112 RemoveAtomsFromAtomicInfo(state->WalkersInMolecule.begin()->second);
113
114 return ActionState::ptr(_state);
115}
116
117ActionState::ptr AtomAddAction::performRedo(ActionState::ptr _state) {
118 AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
119
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.");
125 return Action::failure;
126 }
127}
128
129bool AtomAddAction::canUndo() {
130 return true;
131}
132
133bool AtomAddAction::shouldUndo() {
134 return true;
135}
136/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.