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