Changeset c30959


Ignore:
Timestamp:
Apr 23, 2021, 8:51:43 PM (5 years ago)
Author:
Frederik Heber <frederik.heber@…>
Branches:
Candidate_v1.7.0, stable
Children:
f01bb3
Parents:
e70818
git-author:
Frederik Heber <frederik.heber@…> (11/17/20 22:25:47)
git-committer:
Frederik Heber <frederik.heber@…> (04/23/21 20:51:43)
Message:

BondAddAction also adds to molecule.

  • NOTE: This basically does nothing as new atoms (AddAtom action) always get a dummy molecule. However, removing them from the dummy molecule and readding one is quite complicated.
  • if we add bonds between new atoms (without a molecule set) to and atoms belonging all to the same molecule, then we automatically add the new atoms to this molecule as well. This saves the need for any GraphUpdateMolecules() call afterwards.
  • also implemented undo and redo for this.
Location:
src/Actions/BondAction
Files:
2 edited

Legend:

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

    re70818 rc30959  
    4141#include "CodePatterns/Verbose.hpp"
    4242#include "Descriptors/AtomIdDescriptor.hpp"
     43#include "molecule.hpp"
    4344#include "World.hpp"
    4445#include "WorldTime.hpp"
     
    6364  }
    6465
     66  // check if we are adding new atoms to a molecule
     67  molecules_t molecules;
     68  const molecule *add_to_mol_const = NULL;
     69  molecules.reserve(world.countSelectedAtoms());
     70  for (World::AtomSelectionConstIterator firstiter = world.beginAtomSelection();
     71      firstiter != world.endAtomSelection(); ++firstiter) {
     72    const molecule * const current_mol = firstiter->second->getMolecule();
     73    molecules.push_back(current_mol);
     74    if (current_mol != NULL) {
     75      if (add_to_mol_const == NULL)
     76        add_to_mol_const = current_mol;
     77      else if (add_to_mol_const != current_mol) {
     78        // we encountered a second molecule, don't set the molecules
     79        molecules.clear();
     80        add_to_mol_const = NULL;
     81        break;
     82      }
     83    }
     84  }
     85
    6586  bondPairIds_t bondPairIds;
    6687  for (World::AtomSelectionConstIterator firstiter = world.beginAtomSelection();
     
    81102
    82103  // create undo
    83   BondAddState *UndoState = new BondAddState(bondPairIds, params);
     104  molecule *add_to_mol = NULL;
     105  if (add_to_mol_const != NULL) {
     106    add_to_mol = world.getMolecule(MoleculeById(add_to_mol_const->getId()));
     107    ASSERT( add_to_mol != NULL,
     108        "BondAddAction::performCall() - could not obtain molecule from World.");
     109  }
     110  BondAddState *UndoState = new BondAddState(bondPairIds, molecules, add_to_mol, params);
    84111
    85112  // execute action
    86113  for (bondPairIds_t::const_iterator iter = bondPairIds.begin();
    87114      iter != bondPairIds.end(); ++iter) {
    88     atom *firstatom = world.getAtom(AtomById(iter->first));
    89     atom *secondatom = world.getAtom(AtomById(iter->second));
     115    atom * const firstatom = world.getAtom(AtomById(iter->first));
     116    atom * const secondatom = world.getAtom(AtomById(iter->second));
    90117    ASSERT((firstatom != NULL) && (secondatom != NULL),
    91118        "BondAddAction::performCall() - at least one of the ids "
     
    97124      +toString(*firstatom)+" and "+toString(*secondatom)+" failed.");
    98125  }
     126  if (add_to_mol != NULL)
     127    for (World::AtomSelectionIterator firstiter = world.beginAtomSelection();
     128        firstiter != world.endAtomSelection(); ++firstiter) {
     129      if (firstiter->second->getMolecule() != add_to_mol)
     130        add_to_mol->AddAtom(firstiter->second);
     131    }
    99132
    100133  return ActionState::ptr(UndoState);
     
    120153    }
    121154  }
     155  if (state->add_to_mol != NULL) {
     156    molecules_t::const_iterator mol_iter = state->molecules.begin();
     157    for (World::AtomSelectionIterator firstiter = world.beginAtomSelection();
     158        firstiter != world.endAtomSelection(); ++firstiter) {
     159      const molecule * const current_mol = *mol_iter++;
     160      atom * const Walker = firstiter->second;
     161      if (current_mol != Walker->getMolecule()) {
     162        if (Walker->getMolecule() != NULL)
     163          Walker->removeFromMolecule();
     164        if (current_mol != NULL) {
     165          molecule *add_to_mol = world.getMolecule(MoleculeById(current_mol->getId()));
     166          add_to_mol->AddAtom(Walker);
     167        }
     168      }
     169    }
     170    ASSERT(mol_iter == state->molecules.end(),
     171        "BondAddAction::performUndo() - number of molecule ptrs not coinciding with number of selected atoms.");
     172  }
    122173
    123174  return ActionState::ptr(_state);
     
    143194    }
    144195  }
     196  if (state->add_to_mol != NULL)
     197    for (World::AtomSelectionIterator firstiter = world.beginAtomSelection();
     198        firstiter != world.endAtomSelection(); ++firstiter) {
     199      if (firstiter->second->getMolecule() != state->add_to_mol)
     200        state->add_to_mol->AddAtom(firstiter->second);
     201    }
    145202
    146203  return ActionState::ptr(_state);
  • src/Actions/BondAction/BondAddAction.def

    re70818 rc30959  
    1212
    1313typedef std::vector<std::pair<atomId_t,atomId_t> > bondPairIds_t;
     14typedef std::vector<const molecule*> molecules_t;
    1415
    1516#include "Parameters/Validators/RangeValidator.hpp"
     
    2728(RangeValidator<int>(1,10))
    2829
    29 #define statetypes (bondPairIds_t)
    30 #define statereferences (bondPairIds)
     30#define statetypes (bondPairIds_t)(molecules_t)(molecule *)
     31#define statereferences (bondPairIds)(molecules)(add_to_mol)
    3132
    3233// some defines for all the names, you may use ACTION, STATE and PARAMS
Note: See TracChangeset for help on using the changeset viewer.