Skip to content

Commit

Permalink
Merge pull request cryos#17 from cryos/rwmol-enhancements
Browse files Browse the repository at this point in the history
RWmol enhancements for formal charges and hybridizations
  • Loading branch information
ghutchis committed May 27, 2015
2 parents 81c26e4 + e6fea7e commit 7c4430a
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 10 deletions.
8 changes: 6 additions & 2 deletions avogadro/core/molecule.h
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ inline bool Molecule::setAtomicNumber(Index atomId, unsigned char number)
return hyb;
}

inline bool Molecule::setHybridizations(const Core::Array<AtomHybridization> &hybs)
inline bool Molecule::setHybridizations(const Core::Array<AtomHybridization> &hybs)
{
if (hybs.size() == atomCount()) {
m_hybridizations = hybs;
Expand All @@ -579,9 +579,11 @@ inline bool Molecule::setAtomicNumber(Index atomId, unsigned char number)
return false;
}

inline bool Molecule::setHybridization(Index atomId, AtomHybridization hyb)
inline bool Molecule::setHybridization(Index atomId, AtomHybridization hyb)
{
if (atomId < atomCount()) {
if (atomId >= m_hybridizations.size())
m_hybridizations.resize(atomCount(), HybridizationUnknown);
m_hybridizations[atomId] = hyb;
return true;
}
Expand All @@ -606,6 +608,8 @@ inline bool Molecule::setFormalCharges(const Core::Array<signed char> &charges)
inline bool Molecule::setFormalCharge(Index atomId, signed char charge)
{
if (atomId < atomCount()) {
if (atomId >= m_formalCharges.size())
m_formalCharges.resize(atomCount(), 0);
m_formalCharges[atomId] = charge;
return true;
}
Expand Down
92 changes: 89 additions & 3 deletions avogadro/qtgui/rwmolecule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
This source file is part of the Avogadro project.
Copyright 2013 Kitware, Inc.
Copyright 2013-2015 Kitware, Inc.
This source code is released under the New BSD License, (the "License").
Expand All @@ -21,11 +21,12 @@
#include <algorithm>
#include <cassert>

using Avogadro::Core::Array;

namespace Avogadro {
namespace QtGui {

using Core::Array;
using Core::AtomHybridization;

// Base class for all undo commands used by this class.
// Used to expose molecule internals without needing to add explicit friendships
// between all undo commands and the container.
Expand All @@ -43,6 +44,8 @@ class RWMolecule::UndoCommand : public QUndoCommand
Array<Index>& bondUniqueIds() { return m_mol.m_molecule.bondUniqueIds(); }
Array<unsigned char>& atomicNumbers() { return m_mol.m_molecule.atomicNumbers(); }
Array<Vector3>& positions3d() { return m_mol.m_molecule.atomPositions3d(); }
Array<AtomHybridization>& hybridizations() { return m_mol.m_molecule.hybridizations(); }
Array<signed char>& formalCharges() { return m_mol.m_molecule.formalCharges(); }
Array<std::pair<Index, Index> >& bondPairs() { return m_mol.m_molecule.bondPairs(); }
Array<unsigned char>& bondOrders() { return m_mol.m_molecule.bondOrders(); }
RWMolecule &m_mol;
Expand Down Expand Up @@ -477,6 +480,89 @@ bool RWMolecule::setAtomPosition3d(Index atomId, const Vector3 &pos)
return true;
}

namespace {
class SetAtomHybridizationCommand : public RWMolecule::UndoCommand
{
Index m_atomId;
Core::AtomHybridization m_oldHybridization;
Core::AtomHybridization m_newHybridization;
public:
SetAtomHybridizationCommand(RWMolecule &m, Index atomId,
Core::AtomHybridization oldHybridization,
Core::AtomHybridization newHybridization)
: UndoCommand(m),
m_atomId(atomId),
m_oldHybridization(oldHybridization),
m_newHybridization(newHybridization)
{
}

void redo() AVO_OVERRIDE
{
hybridizations()[m_atomId] = m_newHybridization;
}

void undo() AVO_OVERRIDE
{
hybridizations()[m_atomId] = m_oldHybridization;
}
};
} // end anon namespace

bool RWMolecule::setHybridization(Index atomId, Core::AtomHybridization hyb)
{
if (atomId >= atomCount())
return false;

SetAtomicNumberCommand *comm =
new SetAtomicNumberCommand(*this, atomId,
m_molecule.hybridization(atomId), hyb);
comm->setText(tr("Change Atom Hybridization"));
m_undoStack.push(comm);
return true;
}

namespace {
class SetAtomFormalChargeCommand : public RWMolecule::UndoCommand
{
Index m_atomId;
signed char m_oldCharge;
signed char m_newCharge;
public:
SetAtomFormalChargeCommand(RWMolecule &m, Index atomId,
signed char oldCharge, signed char newCharge)
: UndoCommand(m),
m_atomId(atomId),
m_oldCharge(oldCharge),
m_newCharge(newCharge)
{
}

void redo() AVO_OVERRIDE
{
formalCharges()[m_atomId] = m_newCharge;
}

void undo() AVO_OVERRIDE
{
formalCharges()[m_atomId] = m_oldCharge;
}
};
} // end anon namespace

bool RWMolecule::setFormalCharge(Index atomId, signed char charge)
{
if (atomId >= atomCount())
return false;

SetAtomFormalChargeCommand *comm =
new SetAtomFormalChargeCommand(*this, atomId,
m_molecule.formalCharge(atomId), charge);
comm->setText(tr("Change Atom Formal Charge"));
m_undoStack.push(comm);
return true;
}

namespace {
class AddBondCommand : public RWMolecule::UndoCommand
{
Expand Down
46 changes: 41 additions & 5 deletions avogadro/qtgui/rwmolecule.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
This source file is part of the Avogadro project.
Copyright 2013 Kitware, Inc.
Copyright 2013-2015 Kitware, Inc.
This source code is released under the New BSD License, (the "License").
Expand Down Expand Up @@ -208,11 +208,37 @@ class AVOGADROQTGUI_EXPORT RWMolecule : public QObject
return m_molecule.m_positions2d;
}

Core::AtomHybridization hybridization(Index) const { return Core::HybridizationUnknown; }
bool setHybridization(Index, Core::AtomHybridization) { return false; }
/**
* Get the hybridization for the requested atom.
* @param atomId The index of the atom.
* @return The hybridization of the atom indexed at @a atomId, or
* 0 if @a atomId is invalid.
*/
Core::AtomHybridization hybridization(Index atomId) const;

/**
* Set the hybridization of a single atom.
* @param atomId The index of the atom to modify.
* @param hyb The new hybridization.
* @return True on success, false otherwise.
*/
bool setHybridization(Index atomId, Core::AtomHybridization hyb);

/**
* Get the formal charge for the requested atom.
* @param atomId The index of the atom.
* @return The formal charge of the atom indexed at @a atomId, or
* 0 if @a atomId is invalid.
*/
signed char formalCharge(Index atomId) const;

signed char formalCharge(Index) const { return 0; }
bool setFormalCharge(Index, signed char) { return false; }
/**
* Set the formal charge of a single atom.
* @param atomId The index of the atom to modify.
* @param charge The new formal charge.
* @return True on success, false otherwise.
*/
bool setFormalCharge(Index atomId, signed char charge);

/**
* Create a new bond in the molecule.
Expand Down Expand Up @@ -502,6 +528,16 @@ inline Vector3 RWMolecule::atomPosition3d(Index atomId) const
return m_molecule.atomPosition3d(atomId);
}

inline Core::AtomHybridization RWMolecule::hybridization(Index atomId) const
{
return m_molecule.hybridization(atomId);
}

inline signed char RWMolecule::formalCharge(Index atomId) const
{
return m_molecule.formalCharge(atomId);
}

inline RWMolecule::BondType RWMolecule::addBond(const AtomType &atom1,
const AtomType &atom2,
unsigned char order)
Expand Down

0 comments on commit 7c4430a

Please sign in to comment.