Changes in / [08d9df7:9a8a6b]


Ignore:
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • src/Actions/AtomAction/AddAction.cpp

    r08d9df7 r9a8a6b  
    5555#include "Action_impl_pre.hpp"
    5656/** =========== define the function ====================== */
     57
     58atom * getNewAtom(const AtomAddAction::AtomAddParameters &_params)
     59{
     60  atom * first = World::getInstance().createAtom();
     61  first->setType(_params.elemental.get());
     62  first->setPosition(_params.position.get());
     63
     64  return first;
     65}
     66
     67std::vector<atomId_t> createAtoms(
     68    const AtomAddAction::AtomAddParameters &_params,
     69    std::vector<molecule *> &_molecules)
     70{
     71  std::vector<atomId_t> ids;
     72  if (!_molecules.empty()) {
     73    if (_molecules.size() == 1) {
     74        atom *first = getNewAtom(_params);
     75        molecule *mol = *_molecules.begin();
     76        LOG(1, "Adding new atom with element " << first->getType()->getName()
     77            << " at " << (first->getPosition()) << " to selected molecule "
     78            << mol->getName()+".");
     79        mol->AddAtom(first);
     80      ids.push_back(first->getId());
     81    }
     82  } else {
     83    atom *first = getNewAtom(_params);
     84    molecule *mol = World::getInstance().createMolecule();
     85    mol->setName("none");
     86    mol->AddAtom(first);
     87    LOG(1, "Adding new atom with element " << first->getType()->getName()
     88        << " at " << (first->getPosition()) << " to new molecule.");
     89    ids.push_back(first->getId());
     90  }
     91
     92  return ids;
     93}
     94
    5795ActionState::ptr AtomAddAction::performCall() {
    5896  // execute action
    59   atom * first = World::getInstance().createAtom();
    60   first->setType(params.elemental.get());
    61   first->setPosition(params.position.get());
    62   LOG(1, "Adding new atom with element " << first->getType()->getName() << " at " << (first->getPosition()) << ".");
    63   // TODO: remove when all of World's atoms are stored.
    64   std::vector<molecule *> molecules = World::getInstance().getAllMolecules();
    65   if (!molecules.empty()) {
    66     std::vector<molecule *>::iterator iter = molecules.begin();
    67     (*iter)->AddAtom(first);
    68   }
    69   return ActionState::ptr(new AtomAddState(first->getId(), params));
     97  std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
     98  std::vector<atomId_t> ids = createAtoms(params, molecules);
     99
     100  if (molecules.size() > 1)
     101     return Action::failure;
     102  else
     103    return ActionState::ptr(new AtomAddState(ids, params));
    70104}
    71105
     
    73107  AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
    74108
    75   LOG(1, "Removing atom with id " << state->id << ".");
    76   World::getInstance().destroyAtom(state->id);
     109  for (std::vector<atomId_t>::const_iterator iter = state->ids.begin();
     110      iter != state->ids.end(); ++iter) {
     111    LOG(1, "Removing atom with id " << *iter << ".");
     112    World::getInstance().destroyAtom(*iter);
     113  }
    77114
    78115  return ActionState::ptr(_state);
     
    82119  AtomAddState *state = assert_cast<AtomAddState*>(_state.get());
    83120
    84   atom * first = World::getInstance().createAtom();
    85   first->setType(state->params.elemental.get());
    86   first->setPosition(state->params.position.get());
    87   LOG(1, "Re-adding new atom with element " << state->params.elemental.get()->getName() << " at " << state->params.position.get() << ".");
    88   // TODO: remove when all of World's atoms are stored.
    89   std::vector<molecule *> molecules = World::getInstance().getAllMolecules();
    90   if (!molecules.empty()) {
    91     std::vector<molecule *>::iterator iter = molecules.begin();
    92     (*iter)->AddAtom(first);
     121  std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
     122  std::vector<atomId_t> newids = createAtoms(params, molecules);
     123
     124  if (newids.size() != state->ids.size()) {
     125    STATUS("Could not recreate all atoms after undo.");
     126    for (std::vector<atomId_t>::const_iterator iter = newids.begin(); iter != newids.end();++iter)
     127      World::getInstance().destroyAtom(*iter);
     128    return Action::failure;
    93129  }
    94   if (first->getId() != state->id)
    95     if (!first->changeId(state->id)) {
    96       STATUS("Could not change atom id "+toString(first->getId())+"->"+toString(state->id)+".");
    97       return Action::failure;
    98     }
    99130
    100   return ActionState::ptr(_state);
     131  std::vector<atomId_t>::const_iterator newiter = newids.begin();
     132  std::vector<atomId_t>::const_iterator olditer = state->ids.begin();
     133  bool status=true;
     134  for (; newiter != newids.end(); ++newiter, ++olditer) {
     135    atom * first = World::getInstance().getAtom(AtomById(*newiter));
     136    ASSERT( first != NULL,
     137        "AtomAddAction::performRedo() - re-created atom not present?");
     138    if (first->getId() != *olditer)
     139      if (!first->changeId(*olditer)) {
     140        STATUS("Could not change atom id "+toString(first->getId())+"->"+toString(*olditer)+".");
     141        // remove all created atoms
     142        for (std::vector<atomId_t>::const_iterator iter = state->ids.begin(); iter != olditer;++iter)
     143          World::getInstance().destroyAtom(*iter);
     144        olditer = state->ids.end();
     145        for (std::vector<atomId_t>::const_iterator iter = newiter; iter != newids.end();++iter)
     146          World::getInstance().destroyAtom(*iter);
     147        status = false;
     148        break;
     149      }
     150  }
     151  ASSERT( olditer == state->ids.end(),
     152      "AtomAddAction::performRedo() - after all unequal amount of ids in new and old?");
     153
     154  if (!status) {
     155    return Action::failure;
     156  } else
     157    return ActionState::ptr(_state);
    101158}
    102159
  • src/Actions/AtomAction/AddAction.def

    r08d9df7 r9a8a6b  
    2727(BoxVectorValidator())
    2828
    29 #define statetypes (const atomId_t)
    30 #define statereferences (id)
     29#define statetypes (std::vector<atomId_t>)
     30#define statereferences (ids)
    3131
    3232// some defines for all the names, you may use ACTION, STATE and PARAMS
  • src/Atom/atom.cpp

    r08d9df7 r9a8a6b  
    264264  // first we move ourselves in the world
    265265  // the world lets us know if that succeeded
     266  atomId_t oldid = id;
    266267  if(world->changeAtomId(id,newId,this)){
    267268    OBSERVE;
    268269    id = newId;
     270    if (mol != NULL)
     271      mol->changeAtomId(oldid, newId);
    269272    NOTIFY(IndexChanged);
    270273    return true;
  • src/molecule.cpp

    r08d9df7 r9a8a6b  
    168168    return false;
    169169  }
     170}
     171
     172bool molecule::changeAtomId(int oldId, int newId)
     173{
     174  OBSERVE;
     175  if ((!atomIds.contains( oldId )) || (atomIds.contains( newId )))
     176    return false;
     177  atomIds.erase( oldId );
     178  atomIds.insert( newId );
     179  return true;
    170180}
    171181
  • src/molecule.hpp

    r08d9df7 r9a8a6b  
    237237   */
    238238  bool changeAtomNr(int oldNr, int newNr, atom* target=0);
     239
     240  friend bool atom::changeId(atomId_t newId);
     241  /**
     242   * used when changing an ParticleInfo::Id.
     243   * Note that this number is global (and the molecule uses it to know which atoms belong to it)
     244   *
     245   * @param oldId old Id
     246   * @param newId new Id to set
     247   * @return indicates wether the change could be done or not.
     248   */
     249  bool changeAtomId(int oldId, int newId);
    239250
    240251  /** Updates the internal lookup fro local to global indices.
  • tests/regression/Atoms/Add/post/test.pdb

    r08d9df7 r9a8a6b  
    11REMARK created by molecuilder on Wed Feb  2 18:06:07 2011
    2 ATOM      1 H01 0-   00         10.000  10.000  10.000  0.00  0.00           H 0
     2ATOM      1 H01 0non 01         10.000  10.000  10.000  0.00  0.00           H 0
    33END
Note: See TracChangeset for help on using the changeset viewer.