Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Actions/BondAction/BondAddAction.cpp

    r88afc9 r26b4d62  
    5757ActionState::ptr BondAddAction::performCall() {
    5858  // check preconditions
    59   World& world = World::getInstance();
    60   if (world.countSelectedAtoms() <= 1) {
    61     STATUS("There must be at least two atoms selected for BondAction Add.");
     59  if (World::getInstance().countSelectedAtoms() != 2) {
     60    STATUS("Exactly two atoms must be selected for BondAction Add.");
    6261    return Action::failure;
    6362  }
    64 
    65   bondPairIds_t bondPairIds;
    66   for (World::AtomSelectionConstIterator firstiter = world.beginAtomSelection();
    67       firstiter != world.endAtomSelection(); ++firstiter) {
    68     for (World::AtomSelectionConstIterator seconditer = firstiter;
    69         seconditer != world.endAtomSelection(); ++seconditer) {
    70       if (firstiter == seconditer)
    71         continue;
    72       if (!(firstiter->second)->IsBondedTo(WorldTime::getTime(), seconditer->second))
    73         bondPairIds.push_back(
    74             std::make_pair((firstiter->second)->getId(), (seconditer->second)->getId()));
    75     }
    76   }
    77   if (bondPairIds.empty()) {
    78     STATUS("All bonds are already present.");
     63  const std::vector<atom *> selected_atoms = World::getInstance().getSelectedAtoms();
     64  if (selected_atoms[0]->IsBondedTo(WorldTime::getTime(), selected_atoms[1])) {
     65    STATUS("There already is a bond in between the two selected atoms.");
    7966    return Action::failure;
    8067  }
    8168
    8269  // create undo
    83   BondAddState *UndoState = new BondAddState(bondPairIds, params);
     70  BondAddState *UndoState = new BondAddState(selected_atoms[0]->getId(), selected_atoms[1]->getId(), params);
    8471
    8572  // execute action
    86   for (bondPairIds_t::const_iterator iter = bondPairIds.begin();
    87       iter != bondPairIds.end(); ++iter) {
    88     atom *firstatom = world.getAtom(AtomById(iter->first));
    89     atom *secondatom = world.getAtom(AtomById(iter->second));
    90     ASSERT((firstatom != NULL) && (secondatom != NULL),
    91         "BondAddAction::performCall() - at least one of the ids "
    92         +toString(iter->first)+" or "+toString(iter->second)+" is not present.");
    93     firstatom->addBond(WorldTime::getTime(), secondatom);
    94     ASSERT( firstatom->IsBondedTo(WorldTime::getTime(), secondatom),
     73  selected_atoms[0]->addBond(WorldTime::getTime(), selected_atoms[1]);
     74  ASSERT( selected_atoms[0]->IsBondedTo(WorldTime::getTime(), selected_atoms[1]),
    9575      "BondAddAction::performCall() - adding bond in between "
    96       +toString(*firstatom)+" and "+toString(*secondatom)+" failed.");
    97   }
     76      +toString(*selected_atoms[0])+" and "+toString(*selected_atoms[1])+" failed.");
    9877
    9978  return ActionState::ptr(UndoState);
     
    10483
    10584  // check whether bond already existed
    106   World& world = World::getInstance();
    107   for (bondPairIds_t::const_iterator iter = state->bondPairIds.begin();
    108       iter != state->bondPairIds.end(); ++iter) {
    109     atom *firstatom = world.getAtom(AtomById(iter->first));
    110     atom *secondatom = world.getAtom(AtomById(iter->second));
    111     ASSERT((firstatom != NULL) && (secondatom != NULL),
    112         "BondAddAction::performCall() - at least one of the ids "
    113         +toString(iter->first)+" or "+toString(iter->second)+" is not present.");
    114     if (firstatom->IsBondedTo(WorldTime::getTime(), secondatom)) {
    115       firstatom->removeBond(WorldTime::getTime(), secondatom);
    116     } else {
    117       ELOG(2, "There is no bond in between "+toString(iter->first)
    118           +" and "+toString(iter->second)+".");
    119     }
     85  atom * const first = World::getInstance().getAtom(AtomById(state->firstId));
     86  atom * const second = World::getInstance().getAtom(AtomById(state->secondId));
     87  ASSERT((first != NULL) && (second != NULL),
     88      "BondAddAction::performUndo() - at least one of the ids "
     89      +toString(state->firstId)+" or "+toString(state->secondId)+" is not present.");
     90  if (first->IsBondedTo(WorldTime::getTime(), second)) {
     91    first->removeBond(WorldTime::getTime(), second);
     92  } else {
     93    ELOG(2, "There is no bond in between "+toString(state->firstId)
     94        +" and "+toString(state->secondId)+".");
    12095  }
    12196
     
    127102
    128103  // check whether bond already existed
    129   World& world = World::getInstance();
    130   for (bondPairIds_t::const_iterator iter = state->bondPairIds.begin();
    131       iter != state->bondPairIds.end(); ++iter) {
    132     atom * const firstatom = world.getAtom(AtomById(iter->first));
    133     atom * const secondatom = world.getAtom(AtomById(iter->second));
    134     ASSERT((firstatom != NULL) && (secondatom != NULL),
    135         "BondAddAction::performCall() - at least one of the ids "
    136         +toString(iter->first)+" or "+toString(iter->second)+" is not present.");
    137     if (!firstatom->IsBondedTo(WorldTime::getTime(), secondatom)) {
    138       firstatom->addBond(WorldTime::getTime(), secondatom);
    139     } else {
    140       ELOG(2, "There is already a bond in between "+toString(iter->first)
    141           +" and "+toString(iter->second)+".");
    142     }
     104  atom * const first = World::getInstance().getAtom(AtomById(state->firstId));
     105  atom * const second = World::getInstance().getAtom(AtomById(state->secondId));
     106  ASSERT((first != NULL) && (second != NULL),
     107      "BondAddAction::performRedo() - at least one of the ids "
     108      +toString(state->firstId)+" or "+toString(state->secondId)+" is not present.");
     109  if (!first->IsBondedTo(WorldTime::getTime(), second)) {
     110    first->addBond(WorldTime::getTime(), second);
     111  } else {
     112    ELOG(2, "There is already a bond in between "+toString(state->firstId)
     113        +" and "+toString(state->secondId)+".");
    143114  }
    144115
Note: See TracChangeset for help on using the changeset viewer.