source: src/Actions/AtomAction/AddAction.cpp@ 92c52f

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 92c52f was ea2830, checked in by Frederik Heber <heber@…>, 15 years ago

Added Undo/Redo capability to AddAction.

  • added test for undo/redo to Simple_configuration/3.
  • Property mode set to 100644
File size: 3.8 KB
Line 
1/*
2 * AddAction.cpp
3 *
4 * Created on: May 9, 2010
5 * Author: heber
6 */
7
8#include "Helpers/MemDebug.hpp"
9
10#include "Actions/AtomAction/AddAction.hpp"
11#include "Actions/ActionRegistry.hpp"
12#include "Descriptors/AtomIdDescriptor.hpp"
13#include "atom.hpp"
14#include "element.hpp"
15#include "Helpers/Log.hpp"
16#include "molecule.hpp"
17#include "LinearAlgebra/Vector.hpp"
18#include "Helpers/Verbose.hpp"
19#include "World.hpp"
20
21#include <iostream>
22#include <string>
23
24using namespace std;
25
26#include "UIElements/UIFactory.hpp"
27#include "UIElements/Dialog.hpp"
28#include "Actions/ValueStorage.hpp"
29
30// memento to remember the state when undoing
31
32class AtomAddState : public ActionState {
33public:
34 AtomAddState(const Vector &_position, const element *_elemental, const atomId_t _id) :
35 position(_position),
36 elemental(_elemental),
37 id(_id)
38 {}
39 Vector position;
40 const element *elemental;
41 atomId_t id;
42};
43
44const char AtomAddAction::NAME[] = "add-atom";
45
46AtomAddAction::AtomAddAction() :
47 Action(NAME)
48{}
49
50AtomAddAction::~AtomAddAction()
51{}
52
53void AtomAdd(element *elemental, Vector &position) {
54 ValueStorage::getInstance().setCurrentValue(AtomAddAction::NAME, elemental);
55 ValueStorage::getInstance().setCurrentValue("position", elemental);
56 ActionRegistry::getInstance().getActionByName(AtomAddAction::NAME)->call(Action::NonInteractive);
57};
58
59Dialog * AtomAddAction::fillDialog(Dialog *dialog) {
60 ASSERT(dialog,"No Dialog given when filling action dialog");
61
62 dialog->queryElement(NAME, ValueStorage::getInstance().getDescription(NAME));
63 dialog->queryVector("position", true, ValueStorage::getInstance().getDescription("position"));
64
65 return dialog;
66}
67
68Action::state_ptr AtomAddAction::performCall() {
69 const element * elemental = NULL;
70 Vector position;
71
72 // obtain information
73 ValueStorage::getInstance().queryCurrentValue(NAME, elemental);
74 ValueStorage::getInstance().queryCurrentValue("position", position);
75
76 // execute action
77 atom * first = World::getInstance().createAtom();
78 first->setType(elemental);
79 first->setPosition(position);
80 DoLog(1) && (Log() << Verbose(1) << "Adding new atom with element " << first->getType()->getName() << " at " << (first->getPosition()) << "." << endl);
81 // TODO: remove when all of World's atoms are stored.
82 std::vector<molecule *> molecules = World::getInstance().getAllMolecules();
83 if (!molecules.empty()) {
84 std::vector<molecule *>::iterator iter = molecules.begin();
85 (*iter)->AddAtom(first);
86 }
87 return Action::state_ptr(new AtomAddState(position, elemental, first->getId()));
88}
89
90Action::state_ptr AtomAddAction::performUndo(Action::state_ptr _state) {
91 AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
92
93 DoLog(1) && (Log() << Verbose(1) << "Removing atom with id " << state->id << "." << endl);
94 World::getInstance().destroyAtom(state->id);
95
96 return Action::state_ptr(_state);
97}
98
99Action::state_ptr AtomAddAction::performRedo(Action::state_ptr _state){
100 AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
101
102 atom * first = World::getInstance().createAtom();
103 first->setType(state->elemental);
104 first->setPosition(state->position);
105 DoLog(1) && (Log() << Verbose(1) << "Re-adding new atom with element " << state->elemental->getName() << " at " << state->position << "." << endl);
106 // TODO: remove when all of World's atoms are stored.
107 std::vector<molecule *> molecules = World::getInstance().getAllMolecules();
108 if (!molecules.empty()) {
109 std::vector<molecule *>::iterator iter = molecules.begin();
110 (*iter)->AddAtom(first);
111 }
112 if (first->getId() != state->id)
113 if (!first->changeId(state->id))
114 return Action::failure;
115 return Action::state_ptr(_state);
116}
117
118bool AtomAddAction::canUndo() {
119 return true;
120}
121
122bool AtomAddAction::shouldUndo() {
123 return true;
124}
125
126const string AtomAddAction::getName() {
127 return NAME;
128}
Note: See TracBrowser for help on using the repository browser.