/* * NotMoleculeByFormulaAction.cpp * * Created on: May 12, 2010 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "Helpers/MemDebug.hpp" #include "Actions/SelectionAction/NotMoleculeByFormulaAction.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 SelectionNotMoleculeByFormulaState : public ActionState { public: SelectionNotMoleculeByFormulaState(std::vector selectedMolecules, const std::string &_formula) : selectedMolecules(selectedMolecules), formula(_formula) {} std::vector selectedMolecules; std::string formula; }; const char SelectionNotMoleculeByFormulaAction::NAME[] = "unselect-molecule-by-formula"; SelectionNotMoleculeByFormulaAction::SelectionNotMoleculeByFormulaAction() : Action(NAME) {} SelectionNotMoleculeByFormulaAction::~SelectionNotMoleculeByFormulaAction() {} void SelectionNotMoleculeByFormula(const std::string &_formula) { ValueStorage::getInstance().setCurrentValue(SelectionNotMoleculeByFormulaAction::NAME, _formula); ActionRegistry::getInstance().getActionByName(SelectionNotMoleculeByFormulaAction::NAME)->call(Action::NonInteractive); }; Dialog* SelectionNotMoleculeByFormulaAction::fillDialog(Dialog *dialog) { ASSERT(dialog,"No Dialog given when filling action dialog"); dialog->queryString(NAME, ValueStorage::getInstance().getDescription(NAME)); return dialog; } Action::state_ptr SelectionNotMoleculeByFormulaAction::performCall() { std::string formula; std::vector selectedMolecules = World::getInstance().getSelectedMolecules(); ValueStorage::getInstance().queryCurrentValue(NAME, formula); DoLog(1) && (Log() << Verbose(1) << "Unselecting 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().unselectAllMolecules(MoleculeByFormula(formula)); return Action::state_ptr(new SelectionNotMoleculeByFormulaState(selectedMolecules,formula)); } Action::state_ptr SelectionNotMoleculeByFormulaAction::performUndo(Action::state_ptr _state) { SelectionNotMoleculeByFormulaState *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 SelectionNotMoleculeByFormulaAction::performRedo(Action::state_ptr _state){ SelectionNotMoleculeByFormulaState *state = assert_cast(_state.get()); World::getInstance().unselectAllMolecules(MoleculeByFormula(state->formula)); return Action::state_ptr(_state); } bool SelectionNotMoleculeByFormulaAction::canUndo() { return true; } bool SelectionNotMoleculeByFormulaAction::shouldUndo() { return true; } const string SelectionNotMoleculeByFormulaAction::getName() { return NAME; }