/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2010 University of Bonn. All rights reserved. * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. */ /* * MoleculeByFormulaAction.cpp * * Created on: May 12, 2010 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "Helpers/MemDebug.hpp" #include "Actions/SelectionAction/MoleculeByFormulaAction.hpp" #include "Actions/ActionRegistry.hpp" #include "Descriptors/MoleculeFormulaDescriptor.hpp" #include "molecule.hpp" #include "Helpers/Log.hpp" #include "Helpers/Verbose.hpp" #include "World.hpp" #include #include using namespace std; #include "UIElements/UIFactory.hpp" #include "UIElements/Dialog.hpp" #include "Actions/ValueStorage.hpp" // memento to remember the state when undoing class SelectionMoleculeByFormulaState : public ActionState { public: SelectionMoleculeByFormulaState(std::vector selectedMolecules, const std::string &_formula) : selectedMolecules(selectedMolecules), formula(_formula) {} std::vector selectedMolecules; std::string formula; }; const char SelectionMoleculeByFormulaAction::NAME[] = "select-molecule-by-formula"; SelectionMoleculeByFormulaAction::SelectionMoleculeByFormulaAction() : Action(NAME) {} SelectionMoleculeByFormulaAction::~SelectionMoleculeByFormulaAction() {} void SelectionMoleculeByFormula(const std::string &_formula) { ValueStorage::getInstance().setCurrentValue(SelectionMoleculeByFormulaAction::NAME, _formula); ActionRegistry::getInstance().getActionByName(SelectionMoleculeByFormulaAction::NAME)->call(Action::NonInteractive); }; void SelectionMoleculeByFormulaAction::getParametersfromValueStorage() {}; Dialog* SelectionMoleculeByFormulaAction::fillDialog(Dialog *dialog) { ASSERT(dialog,"No Dialog given when filling action dialog"); dialog->queryString(NAME, ValueStorage::getInstance().getDescription(NAME)); return dialog; } Action::state_ptr SelectionMoleculeByFormulaAction::performCall() { std::string formula; std::vector selectedMolecules = World::getInstance().getSelectedMolecules(); ValueStorage::getInstance().queryCurrentValue(NAME, formula); DoLog(1) && (Log() << Verbose(1) << "Selecting molecules with chemical formula " << formula << ":" << endl); std::vector matchingMolecules = World::getInstance().getAllMolecules(MoleculeByFormula(formula)); for (std::vector::const_iterator iter = matchingMolecules.begin(); iter != matchingMolecules.end(); ++iter) { DoLog(1) && (Log() << Verbose(1) << "\t" << (*iter)->getId() << ", " << (*iter)->getName() << "." << endl); } World::getInstance().selectAllMolecules(MoleculeByFormula(formula)); return Action::state_ptr(new SelectionMoleculeByFormulaState(selectedMolecules,formula)); } Action::state_ptr SelectionMoleculeByFormulaAction::performUndo(Action::state_ptr _state) { SelectionMoleculeByFormulaState *state = assert_cast(_state.get()); World::getInstance().clearMoleculeSelection(); for(std::vector::iterator iter = state->selectedMolecules.begin(); iter != state->selectedMolecules.end(); ++iter) World::getInstance().selectMolecule(*iter); return Action::state_ptr(_state); } Action::state_ptr SelectionMoleculeByFormulaAction::performRedo(Action::state_ptr _state){ SelectionMoleculeByFormulaState *state = assert_cast(_state.get()); World::getInstance().selectAllMolecules(MoleculeByFormula(state->formula)); return Action::state_ptr(_state); } bool SelectionMoleculeByFormulaAction::canUndo() { return true; } bool SelectionMoleculeByFormulaAction::shouldUndo() { return true; } const string SelectionMoleculeByFormulaAction::getName() { return NAME; }