| 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_MOLECULE_H |
| 7 |
#define AVOGADRO_QTGUI_MOLECULE_H |
| 8 |
|
| 9 |
#include "avogadroqtguiexport.h" |
| 10 |
|
| 11 |
#include "persistentatom.h" |
| 12 |
#include "persistentbond.h" |
| 13 |
|
| 14 |
#include <avogadro/core/avogadrocore.h> |
| 15 |
#include <avogadro/core/molecule.h> |
| 16 |
|
| 17 |
#include <QtCore/QObject> |
| 18 |
#include <list> |
| 19 |
|
| 20 |
namespace Avogadro { |
| 21 |
|
| 22 |
namespace QtGui { |
| 23 |
|
| 24 |
class Mesh; |
| 25 |
class RWMolecule; |
| 26 |
|
| 27 |
/** |
| 28 |
* @class Molecule molecule.h <avogadro/qtgui/molecule.h> |
| 29 |
* @brief A QObject derived molecule object with signals/slots. |
| 30 |
*/ |
| 31 |
|
| 32 |
class AVOGADROQTGUI_EXPORT Molecule : public QObject, public Core::Molecule |
| 33 |
{ |
| 34 |
Q_OBJECT |
| 35 |
|
| 36 |
public: |
| 37 |
/** Typedef for Atom class. */ |
| 38 |
typedef Core::Molecule::AtomType AtomType; |
| 39 |
|
| 40 |
/** Typedef for PersistentAtom class. */ |
| 41 |
typedef PersistentAtom<Molecule> PersistentAtomType; |
| 42 |
|
| 43 |
/** Typedef for Bond class. */ |
| 44 |
typedef Core::Molecule::BondType BondType; |
| 45 |
|
| 46 |
/** Typedef for PersistentBond class. */ |
| 47 |
typedef PersistentBond<Molecule> PersistentBondType; |
| 48 |
|
| 49 |
Molecule(QObject* parent_ = nullptr); |
| 50 |
~Molecule() override; |
| 51 |
/** copy constructor */ |
| 52 |
Molecule(const Molecule& other); |
| 53 |
|
| 54 |
/** copy constructor to copy data from base instance */ |
| 55 |
Molecule(const Core::Molecule& other); |
| 56 |
|
| 57 |
/** Assignment operator */ |
| 58 |
Molecule& operator=(const Molecule& other); |
| 59 |
|
| 60 |
/** Assignment operator to copy data from base instance */ |
| 61 |
Molecule& operator=(const Core::Molecule& other); |
| 62 |
|
| 63 |
/** |
| 64 |
* \enum MoleculeChange |
| 65 |
*Enumeration of change types that can be given. |
| 66 |
*/ |
| 67 |
enum MoleculeChange |
| 68 |
{ |
| 69 |
/** Useful for initializing MoleculeChange variables. */ |
| 70 |
NoChange = 0, |
| 71 |
/** Object types that can be changed. */ |
| 72 |
Atoms = 1, |
| 73 |
Bonds = 2, |
| 74 |
UnitCell = 4, |
| 75 |
Selection = 8, |
| 76 |
Layers = 16, |
| 77 |
Properties = 32, |
| 78 |
Constraints = 64, |
| 79 |
Residues = 128, |
| 80 |
/** Indicates a new crystal structure (triggers space group fill). */ |
| 81 |
NewCrystal = 256, |
| 82 |
/** Operations that can affect the above types. */ |
| 83 |
Added = 1024, |
| 84 |
Removed = 2048, |
| 85 |
Modified = 4096, |
| 86 |
Moved = 8192 |
| 87 |
}; |
| 88 |
Q_DECLARE_FLAGS(MoleculeChanges, MoleculeChange) |
| 89 |
|
| 90 |
/** |
| 91 |
* Add an atom with @p atomicNumber to the molecule. |
| 92 |
* @return The atom created. |
| 93 |
*/ |
| 94 |
AtomType addAtom(unsigned char atomicNumber) override; |
| 95 |
|
| 96 |
/** |
| 97 |
* Add an atom with @p atomicNumber and @p uniqueId to the molecule. |
| 98 |
* @return The atom created. This can be invalid if the unique ID was already |
| 99 |
* in use. |
| 100 |
*/ |
| 101 |
virtual AtomType addAtom(unsigned char atomicNumber, Index uniqueId); |
| 102 |
|
| 103 |
AtomType addAtom(unsigned char number, Vector3 position3d, |
| 104 |
Index uniqueId = MaxIndex); |
| 105 |
|
| 106 |
/** |
| 107 |
* @brief Remove the specified atom from the molecule. |
| 108 |
* @param index The index of the atom to be removed. |
| 109 |
* @return True on success, false if the atom was not found. |
| 110 |
*/ |
| 111 |
bool removeAtom(Index index) override; |
| 112 |
|
| 113 |
/** |
| 114 |
* @brief Remove the specified atom from the molecule. |
| 115 |
* @param atom The atom to be removed. |
| 116 |
* @return True on success, false if the atom was not found. |
| 117 |
* @overload |
| 118 |
*/ |
| 119 |
bool removeAtom(const AtomType& atom) override; |
| 120 |
|
| 121 |
/** |
| 122 |
* @brief Get the atom referenced by the @p uniqueId, the isValid method |
| 123 |
* should be queried to ensure the id still referenced a valid atom. |
| 124 |
* @param uniqueId The unique identifier for the atom. |
| 125 |
* @return An Atom object, check it is valid before using it. |
| 126 |
*/ |
| 127 |
AtomType atomByUniqueId(Index uniqueId); |
| 128 |
|
| 129 |
/** |
| 130 |
* @brief Get the unique ID of the atom, this will uniquely reference the atom |
| 131 |
* as long as it exists. |
| 132 |
* @param atom The atom to obtain the unique ID of. |
| 133 |
* @return The unique identifier for the atom, MaxIndex if the atom is invalid |
| 134 |
* or does not belong to this molecule. |
| 135 |
* @{ |
| 136 |
*/ |
| 137 |
Index atomUniqueId(const AtomType& atom) const; |
| 138 |
Index atomUniqueId(Index atom) const; |
| 139 |
/** @} */ |
| 140 |
|
| 141 |
Core::Array<Index>& atomUniqueIds() { return m_atomUniqueIds; } |
| 142 |
|
| 143 |
/** |
| 144 |
* @brief Add a bond between the specified atoms. |
| 145 |
* @param a The first atom in the bond. |
| 146 |
* @param b The second atom in the bond. |
| 147 |
* @param bondOrder The order of the bond. |
| 148 |
* @return The bond created. |
| 149 |
*/ |
| 150 |
BondType addBond(const AtomType& a, const AtomType& b, |
| 151 |
unsigned char bondOrder = 1) override; |
| 152 |
|
| 153 |
/** |
| 154 |
* @brief Add a bond between the specified atoms. |
| 155 |
* @param atomId1 The index of the first atom in the bond. |
| 156 |
* @param atomId2 The index of the second atom in the bond. |
| 157 |
* @param bondOrder The order of the bond. |
| 158 |
* @return The bond created. |
| 159 |
*/ |
| 160 |
BondType addBond(Index atomId1, Index atomId2, |
| 161 |
unsigned char bondOrder = 1) override; |
| 162 |
|
| 163 |
void addBonds(const Core::Array<std::pair<Index, Index>>& bonds, |
| 164 |
const Core::Array<unsigned char>& orders); |
| 165 |
/** |
| 166 |
* @brief Add a bond between the specified atoms. |
| 167 |
* @param a The first atom in the bond. |
| 168 |
* @param b The second atom in the bond. |
| 169 |
* @param bondOrder The order of the bond. |
| 170 |
* @param uniqueId The unique ID to use for the bond. |
| 171 |
* @return The bond created. This can be invalid if the unique ID was already |
| 172 |
* in use. |
| 173 |
*/ |
| 174 |
virtual BondType addBond(const AtomType& a, const AtomType& b, |
| 175 |
unsigned char bondOrder, Index uniqueId); |
| 176 |
|
| 177 |
/** |
| 178 |
* @brief Add a bond between the specified atoms. |
| 179 |
* @param atomId1 The index of the first atom in the bond. |
| 180 |
* @param atomId2 The index of the second atom in the bond. |
| 181 |
* @param bondOrder The order of the bond. |
| 182 |
* @param uniqueId The unique ID to use for the bond. |
| 183 |
* @return The bond created. This can be invalid if the unique ID was already |
| 184 |
* in use. |
| 185 |
*/ |
| 186 |
virtual BondType addBond(Index atomId1, Index atomId2, |
| 187 |
unsigned char bondOrder, Index uniqueId); |
| 188 |
|
| 189 |
/** |
| 190 |
* @brief Remove the specified bond. |
| 191 |
* @param index The index of the bond to be removed. |
| 192 |
* @return True on success, false if the bond was not found. |
| 193 |
*/ |
| 194 |
bool removeBond(Index index) override; |
| 195 |
|
| 196 |
/** |
| 197 |
* @brief Remove the specified bond. |
| 198 |
* @param bond The bond to be removed. |
| 199 |
* @return True on success, false if the bond was not found. |
| 200 |
* @overload |
| 201 |
*/ |
| 202 |
bool removeBond(const BondType& bond) override; |
| 203 |
|
| 204 |
/** |
| 205 |
* @brief Remove the specified bond. |
| 206 |
* @param atom1 One atom in the bond. |
| 207 |
* @param atom2 The other atom in the bond. |
| 208 |
* @return True on success, false if the bond was not found. |
| 209 |
* @overload |
| 210 |
* @{ |
| 211 |
*/ |
| 212 |
bool removeBond(const AtomType& atom1, const AtomType& atom2) override; |
| 213 |
bool removeBond(Index atom1, Index atom2) override; |
| 214 |
/** @} */ |
| 215 |
|
| 216 |
/** |
| 217 |
* @brief Get the bond referenced by the @p uniqueId, the isValid method |
| 218 |
* should be queried to ensure the id still referenced a valid bond. |
| 219 |
* @param uniqueId The unique identifier for the bond. |
| 220 |
* @return A Bond object, check it is valid before using it. |
| 221 |
*/ |
| 222 |
BondType bondByUniqueId(Index uniqueId); |
| 223 |
|
| 224 |
/** |
| 225 |
* @brief Get the unique ID of the bond, this will uniquely reference the bond |
| 226 |
* as long as it exists. |
| 227 |
* @param bond The bond to obtain the unique ID of. |
| 228 |
* @return The unique identifier for the bond, MaxIndex if the bond is invalid |
| 229 |
* or does not belong to this molecule. |
| 230 |
* @{ |
| 231 |
*/ |
| 232 |
Index bondUniqueId(const BondType& bond) const; |
| 233 |
Index bondUniqueId(Index bond) const; |
| 234 |
/** @} */ |
| 235 |
|
| 236 |
Core::Array<Index>& bondUniqueIds() { return m_bondUniqueIds; } |
| 237 |
|
| 238 |
Index findAtomUniqueId(Index index) const; |
| 239 |
Index findBondUniqueId(Index index) const; |
| 240 |
|
| 241 |
RWMolecule* undoMolecule(); |
| 242 |
|
| 243 |
/** |
| 244 |
* @brief Get the formatted chemical formula for the molecule. |
| 245 |
* @return The formatted chemical formula in HTML format. |
| 246 |
* This will include subscripts for atom counts and superscripts for charge |
| 247 |
* and spin multiplicity. |
| 248 |
*/ |
| 249 |
QString formattedFormula() const; |
| 250 |
|
| 251 |
bool isInteractive() const; |
| 252 |
|
| 253 |
void swapBond(Index a, Index b); |
| 254 |
void swapAtom(Index a, Index b); |
| 255 |
|
| 256 |
public slots: |
| 257 |
/** |
| 258 |
* @brief Force the molecule to emit the changed() signal. |
| 259 |
* @param change See changed(). |
| 260 |
*/ |
| 261 |
void emitChanged(unsigned int change); |
| 262 |
|
| 263 |
/** |
| 264 |
* @brief Request an update through the update() signal |
| 265 |
*/ |
| 266 |
void emitUpdate() const; |
| 267 |
|
| 268 |
signals: |
| 269 |
/** |
| 270 |
* @brief Indicates that the molecule has changed. |
| 271 |
* @param change Use the MoleculeChange enum to check what has changed. |
| 272 |
* |
| 273 |
* The @p change variable indicates what has changed, i.e. if |
| 274 |
* change & Atoms == true then atoms were changed in some way, and if |
| 275 |
* change & Removed == true then one or more atoms were removed. |
| 276 |
*/ |
| 277 |
void changed(unsigned int change) const; |
| 278 |
|
| 279 |
/** |
| 280 |
* @brief Request an update of the molecule. |
| 281 |
* (e.g., re-compute properties) |
| 282 |
*/ |
| 283 |
void update() const; |
| 284 |
|
| 285 |
private: |
| 286 |
Core::Array<Index> m_atomUniqueIds; |
| 287 |
Core::Array<Index> m_bondUniqueIds; |
| 288 |
|
| 289 |
friend class RWMolecule; |
| 290 |
|
| 291 |
RWMolecule* m_undoMolecule; |
| 292 |
}; |
| 293 |
|
| 294 |
Q_DECLARE_OPERATORS_FOR_FLAGS(Molecule::MoleculeChanges) |
| 295 |
|
| 296 |
} // namespace QtGui |
| 297 |
} // namespace Avogadro |
| 298 |
|
| 299 |
#endif // AVOGADRO_QTGUI_MOLECULE_H |