source: src/Actions/AtomAction/AddAction.cpp@ 14c57a

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 14c57a was 13e3c3, checked in by Frederik Heber <heber@…>, 15 years ago

Merge branch 'SmallFixes' into stable

Conflicts:

src/Shapes/BaseShapes.cpp

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