| 1 | /*
|
|---|
| 2 | * SaveBondsAction.cpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: May 10, 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/MoleculeAction/SaveBondsAction.hpp"
|
|---|
| 16 | #include "Actions/ActionRegistry.hpp"
|
|---|
| 17 | #include "bondgraph.hpp"
|
|---|
| 18 | #include "config.hpp"
|
|---|
| 19 | #include "Helpers/Log.hpp"
|
|---|
| 20 | #include "molecule.hpp"
|
|---|
| 21 | #include "Helpers/Verbose.hpp"
|
|---|
| 22 | #include "World.hpp"
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 | #include <iostream>
|
|---|
| 26 | #include <fstream>
|
|---|
| 27 | #include <string>
|
|---|
| 28 |
|
|---|
| 29 | using namespace std;
|
|---|
| 30 |
|
|---|
| 31 | #include "UIElements/UIFactory.hpp"
|
|---|
| 32 | #include "UIElements/Dialog.hpp"
|
|---|
| 33 | #include "Actions/ValueStorage.hpp"
|
|---|
| 34 |
|
|---|
| 35 | /****** MoleculeSaveBondsAction *****/
|
|---|
| 36 |
|
|---|
| 37 | // memento to remember the state when undoing
|
|---|
| 38 |
|
|---|
| 39 | //class MoleculeSaveBondsState : public ActionState {
|
|---|
| 40 | //public:
|
|---|
| 41 | // MoleculeSaveBondsState(molecule* _mol,std::string _lastName) :
|
|---|
| 42 | // mol(_mol),
|
|---|
| 43 | // lastName(_lastName)
|
|---|
| 44 | // {}
|
|---|
| 45 | // molecule* mol;
|
|---|
| 46 | // std::string lastName;
|
|---|
| 47 | //};
|
|---|
| 48 |
|
|---|
| 49 | const char MoleculeSaveBondsAction::NAME[] = "save-bonds";
|
|---|
| 50 |
|
|---|
| 51 | MoleculeSaveBondsAction::MoleculeSaveBondsAction() :
|
|---|
| 52 | Action(NAME)
|
|---|
| 53 | {}
|
|---|
| 54 |
|
|---|
| 55 | MoleculeSaveBondsAction::~MoleculeSaveBondsAction()
|
|---|
| 56 | {}
|
|---|
| 57 |
|
|---|
| 58 | void MoleculeSaveBonds(std::string &bondsfile) {
|
|---|
| 59 | ValueStorage::getInstance().setCurrentValue(MoleculeSaveBondsAction::NAME, bondsfile);
|
|---|
| 60 | ActionRegistry::getInstance().getActionByName(MoleculeSaveBondsAction::NAME)->call(Action::NonInteractive);
|
|---|
| 61 | };
|
|---|
| 62 |
|
|---|
| 63 | Dialog* MoleculeSaveBondsAction::fillDialog(Dialog *dialog) {
|
|---|
| 64 | ASSERT(dialog,"No Dialog given when filling action dialog");
|
|---|
| 65 |
|
|---|
| 66 | dialog->queryString(NAME, ValueStorage::getInstance().getDescription(NAME));
|
|---|
| 67 |
|
|---|
| 68 | return dialog;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | Action::state_ptr MoleculeSaveBondsAction::performCall() {
|
|---|
| 72 | string filename;
|
|---|
| 73 | molecule *mol = NULL;
|
|---|
| 74 |
|
|---|
| 75 | ValueStorage::getInstance().queryCurrentValue(NAME, filename);
|
|---|
| 76 |
|
|---|
| 77 | for (World::MoleculeSelectionIterator iter = World::getInstance().beginMoleculeSelection(); iter != World::getInstance().endMoleculeSelection(); ++iter) {
|
|---|
| 78 | mol = iter->second;
|
|---|
| 79 | DoLog(0) && (Log() << Verbose(0) << "Storing bonds to path " << filename << "." << endl);
|
|---|
| 80 | World::getInstance().getConfig()->BG->ConstructBondGraph(mol);
|
|---|
| 81 | // TODO: sollte stream, nicht filenamen direkt nutzen, besser fuer unit tests
|
|---|
| 82 | mol->StoreBondsToFile(filename);
|
|---|
| 83 | }
|
|---|
| 84 | return Action::success;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | Action::state_ptr MoleculeSaveBondsAction::performUndo(Action::state_ptr _state) {
|
|---|
| 88 | // MoleculeSaveBondsState *state = assert_cast<MoleculeSaveBondsState*>(_state.get());
|
|---|
| 89 |
|
|---|
| 90 | // string newName = state->mol->getName();
|
|---|
| 91 | // state->mol->setName(state->lastName);
|
|---|
| 92 |
|
|---|
| 93 | return Action::failure;
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | Action::state_ptr MoleculeSaveBondsAction::performRedo(Action::state_ptr _state){
|
|---|
| 97 | // Undo and redo have to do the same for this action
|
|---|
| 98 | return performUndo(_state);
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | bool MoleculeSaveBondsAction::canUndo() {
|
|---|
| 102 | return false;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | bool MoleculeSaveBondsAction::shouldUndo() {
|
|---|
| 106 | return false;
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | const string MoleculeSaveBondsAction::getName() {
|
|---|
| 110 | return NAME;
|
|---|
| 111 | }
|
|---|