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