| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#ifndef AVOGADRO_QTGUI_PERSISTENTBOND_H |
| 7 |
#define AVOGADRO_QTGUI_PERSISTENTBOND_H |
| 8 |
|
| 9 |
#include <avogadro/core/avogadrocore.h> |
| 10 |
|
| 11 |
namespace Avogadro { |
| 12 |
namespace QtGui { |
| 13 |
|
| 14 |
/** |
| 15 |
* @class PersistentBond persistentbond.h <avogadro/qtcore/persistentbond.h> |
| 16 |
* @brief The PersistentBond object provides a container for a persistent bond |
| 17 |
* reference that can be held onto. The bond() method gets the underlying bond |
| 18 |
* using the unique ID mechanism of the molecule. |
| 19 |
*/ |
| 20 |
template <typename Molecule_T> |
| 21 |
class PersistentBond |
| 22 |
{ |
| 23 |
public: |
| 24 |
typedef Molecule_T MoleculeType; |
| 25 |
typedef typename Molecule_T::BondType BondType; |
| 26 |
|
| 27 |
/** |
| 28 |
* @brief Create a persistent bond, with the specified unique id. |
| 29 |
* @param m The molecule the persistent bond belongs to. |
| 30 |
* @param uniqueId The unique identifier for the bond. |
| 31 |
*/ |
| 32 |
explicit PersistentBond(MoleculeType* m = nullptr, Index uniqueId = MaxIndex) |
| 33 |
: m_molecule(m), m_uniqueId(uniqueId) |
| 34 |
{ |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @brief Create a persistent bond from a standard bond object. |
| 39 |
* @param b The bond that a persistent reference should be created for. |
| 40 |
*/ |
| 41 |
explicit PersistentBond(const BondType& b); |
| 42 |
|
| 43 |
/** |
| 44 |
* @brief Set the molecule and unique ID for the persistent object. |
| 45 |
* @param m The molecule that contains the bond. |
| 46 |
* @param uniqueId The unique ID of the bond. |
| 47 |
*/ |
| 48 |
void set(MoleculeType* m, Index uniqueId); |
| 49 |
|
| 50 |
/** |
| 51 |
* @brief Set the persistent bond from a standard bond object. |
| 52 |
* @param b The bond that a persistent reference should be created for. |
| 53 |
*/ |
| 54 |
void set(const BondType& b); |
| 55 |
|
| 56 |
/** |
| 57 |
* @brief Reset the object to an invalid state. |
| 58 |
*/ |
| 59 |
void reset(); |
| 60 |
|
| 61 |
/** |
| 62 |
* @return True if the persistent bond is valid. |
| 63 |
*/ |
| 64 |
bool isValid() const; |
| 65 |
|
| 66 |
/** |
| 67 |
* @return The molecule the bond is a part of. |
| 68 |
*/ |
| 69 |
MoleculeType* molecule() const { return m_molecule; } |
| 70 |
|
| 71 |
/** |
| 72 |
* @brief The persistent unique ID of the bond. |
| 73 |
* @return The unique ID of the bond. |
| 74 |
*/ |
| 75 |
Index uniqueIdentifier() const { return m_uniqueId; } |
| 76 |
|
| 77 |
/** |
| 78 |
* @brief Obtain the bond being held by the persistent object. |
| 79 |
* @return A reference to the bond held by the object. |
| 80 |
*/ |
| 81 |
BondType bond() const; |
| 82 |
|
| 83 |
private: |
| 84 |
MoleculeType* m_molecule; |
| 85 |
Index m_uniqueId; |
| 86 |
}; |
| 87 |
|
| 88 |
template <typename Molecule_T> |
| 89 |
PersistentBond<Molecule_T>::PersistentBond(const BondType& b) |
| 90 |
: m_molecule(dynamic_cast<MoleculeType*>(b.molecule())) |
| 91 |
{ |
| 92 |
m_uniqueId = m_molecule ? m_molecule->bondUniqueId(b) : MaxIndex; |
| 93 |
} |
| 94 |
|
| 95 |
template <typename Molecule_T> |
| 96 |
void PersistentBond<Molecule_T>::set(MoleculeType* m, Index uniqueId) |
| 97 |
{ |
| 98 |
m_molecule = m; |
| 99 |
m_uniqueId = uniqueId; |
| 100 |
} |
| 101 |
|
| 102 |
template <typename Molecule_T> |
| 103 |
void PersistentBond<Molecule_T>::set(const BondType& b) |
| 104 |
{ |
| 105 |
m_molecule = dynamic_cast<MoleculeType*>(b.molecule()); |
| 106 |
m_uniqueId = m_molecule ? m_molecule->bondUniqueId(b) : MaxIndex; |
| 107 |
} |
| 108 |
|
| 109 |
template <typename Molecule_T> |
| 110 |
void PersistentBond<Molecule_T>::reset() |
| 111 |
{ |
| 112 |
set(nullptr, MaxIndex); |
| 113 |
} |
| 114 |
|
| 115 |
template <typename Molecule_T> |
| 116 |
bool PersistentBond<Molecule_T>::isValid() const |
| 117 |
{ |
| 118 |
return bond().isValid(); |
| 119 |
} |
| 120 |
|
| 121 |
template <typename Molecule_T> |
| 122 |
typename Molecule_T::BondType PersistentBond<Molecule_T>::bond() const |
| 123 |
{ |
| 124 |
return m_molecule ? m_molecule->bondByUniqueId(m_uniqueId) : BondType(); |
| 125 |
} |
| 126 |
|
| 127 |
} // namespace QtGui |
| 128 |
} // namespace Avogadro |
| 129 |
|
| 130 |
#endif // AVOGADRO_QTGUI_PERSISTENTBOND_H |