/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2010-2012 University of Bonn. All rights reserved. * * * This file is part of MoleCuilder. * * MoleCuilder is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * MoleCuilder is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with MoleCuilder. If not, see . */ /* * NotMoleculeByIdAction.cpp * * Created on: May 12, 2010 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "CodePatterns/MemDebug.hpp" #include "molecule.hpp" #include "CodePatterns/Log.hpp" #include "CodePatterns/Verbose.hpp" #include "Descriptors/MoleculeIdDescriptor.hpp" #include "World.hpp" #include #include #include "NotMoleculeByIdAction.hpp" using namespace MoleCuilder; // and construct the stuff #include "NotMoleculeByIdAction.def" #include "Action_impl_pre.hpp" /** =========== define the function ====================== */ ActionState::ptr SelectionNotMoleculeByIdAction::performCall() { enum Sucess { NoStatus, AllMoleculesSelected, MoleculesUnselected, MoleculeMissing } status = NoStatus; const molids_t molids = params.molids.get(); molids_t undomolids; undomolids.reserve(molids.size()); for (molids_t::const_iterator iter = molids.begin(); iter != molids.end(); ++iter) { const molecule *Walker = const_cast(World::getInstance()). getMolecule(MoleculeById(*iter)); if (Walker != NULL) { if (World::getInstance().isSelected(Walker)) { LOG(1, "Unselecting mol " << Walker->getName()); World::getInstance().unselectMolecule(Walker); undomolids.push_back(*iter); if (status < MoleculeMissing) status = MoleculesUnselected; } else { if (status == NoStatus) status = AllMoleculesSelected; } } else { status = MoleculeMissing; } } LOG(0, World::getInstance().countSelectedMolecules() << " mols remain selected."); switch (status) { case MoleculeMissing: STATUS("Cannot find all mols by given ids."); return Action::failure; break; case AllMoleculesSelected: case MoleculesUnselected: return ActionState::ptr(new SelectionNotMoleculeByIdState(undomolids, params)); break; default: STATUS("No mols have been selected."); return Action::failure; break; } return Action::failure; } ActionState::ptr SelectionNotMoleculeByIdAction::performUndo(ActionState::ptr _state) { SelectionNotMoleculeByIdState *state = assert_cast(_state.get()); for (molids_t::const_iterator iter = state->undomolids.begin(); iter != state->undomolids.end(); ++iter) { const molecule *Walker = const_cast(World::getInstance()). getMolecule(MoleculeById(*iter)); World::getInstance().selectMolecule(Walker); } return ActionState::ptr(_state); } ActionState::ptr SelectionNotMoleculeByIdAction::performRedo(ActionState::ptr _state){ SelectionNotMoleculeByIdState *state = assert_cast(_state.get()); for (molids_t::const_iterator iter = state->undomolids.begin(); iter != state->undomolids.end(); ++iter) { const molecule *Walker = const_cast(World::getInstance()). getMolecule(MoleculeById(*iter)); World::getInstance().unselectMolecule(Walker); } return ActionState::ptr(_state); } bool SelectionNotMoleculeByIdAction::canUndo() { return true; } bool SelectionNotMoleculeByIdAction::shouldUndo() { return true; } /** =========== end of function ====================== */