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