| 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_RWMOLECULE_H |
| 7 |
#define AVOGADRO_QTGUI_RWMOLECULE_H |
| 8 |
|
| 9 |
#include "avogadroqtguiexport.h" |
| 10 |
#include <QtCore/QObject> |
| 11 |
|
| 12 |
#include "molecule.h" //qtgui::Molecule |
| 13 |
#include "persistentatom.h" |
| 14 |
#include "persistentbond.h" |
| 15 |
|
| 16 |
#include <avogadro/core/array.h> |
| 17 |
#include <avogadro/core/atom.h> |
| 18 |
#include <avogadro/core/avogadrocore.h> |
| 19 |
#include <avogadro/core/bond.h> |
| 20 |
#include <avogadro/core/crystaltools.h> |
| 21 |
#include <avogadro/core/residue.h> |
| 22 |
#include <avogadro/core/unitcell.h> |
| 23 |
#include <avogadro/core/vector.h> |
| 24 |
|
| 25 |
#include <QUndoStack> |
| 26 |
|
| 27 |
namespace Avogadro { |
| 28 |
namespace QtGui { |
| 29 |
|
| 30 |
/** Concrete atom/bond proxy classes for RWMolecule. @{ */ |
| 31 |
class RWAtom; |
| 32 |
class RWBond; |
| 33 |
/** @} */ |
| 34 |
|
| 35 |
/** |
| 36 |
* @class RWMolecule rwmolecule.h <avogadro/qtgui/rwmolecule.h> |
| 37 |
* @brief The RWMolecule class is an editable molecule class that automatically |
| 38 |
* populates an undo stack. |
| 39 |
* |
| 40 |
* This class implements the molecule API and composes a QUndoStack |
| 41 |
* (undoStack()). New undo commands are automatically generated and push each |
| 42 |
* time a non-const method is called. |
| 43 |
* |
| 44 |
* An interactive mode is supported that causes "noisy" commands, such as |
| 45 |
* atom position changes, to be merged into a single command, saving memory |
| 46 |
* keeping the stack usable during interactive editing of the molecule. Use |
| 47 |
* setInteractive(bool) to toggle interactive mode. |
| 48 |
* |
| 49 |
* Similarly, multiple sequences of commands can be compressed into a single |
| 50 |
* named action using the QUndoStack's macro capability. Call |
| 51 |
* undoStack().beginMacro(tr("User Description Of Change")) to begin a macro, |
| 52 |
* and undoStack().endMacro() when finished. |
| 53 |
*/ |
| 54 |
class AVOGADROQTGUI_EXPORT RWMolecule : public QObject |
| 55 |
{ |
| 56 |
Q_OBJECT |
| 57 |
|
| 58 |
public: |
| 59 |
/** Typedef for Atom class. */ |
| 60 |
typedef RWAtom AtomType; |
| 61 |
|
| 62 |
/** Typedef for PersistentAtom class. */ |
| 63 |
typedef PersistentAtom<RWMolecule> PersistentAtomType; |
| 64 |
|
| 65 |
/** Typedef for Bond class. */ |
| 66 |
typedef RWBond BondType; |
| 67 |
|
| 68 |
/** Typedef for PersistentBond class. */ |
| 69 |
typedef PersistentBond<RWMolecule> PersistentBondType; |
| 70 |
|
| 71 |
/** Construct a molecule with the atoms/bonds of mol. */ |
| 72 |
explicit RWMolecule(Molecule& mol, QObject* parent = nullptr); |
| 73 |
|
| 74 |
~RWMolecule() override; |
| 75 |
|
| 76 |
Molecule& molecule() { return m_molecule; } |
| 77 |
const Molecule& molecule() const { return m_molecule; } |
| 78 |
|
| 79 |
/** |
| 80 |
* Add a new atom to the molecule. |
| 81 |
* @param atomicNumber The atomic number of the new atom. |
| 82 |
* @param usingPositions Whether or not to use positions for this atom. |
| 83 |
* Default is true. Set to false if the atom |
| 84 |
* will not be using coordinates. |
| 85 |
* @return The new Atom object. |
| 86 |
*/ |
| 87 |
AtomType addAtom(unsigned char atomicNumber, bool usingPositions = true); |
| 88 |
|
| 89 |
/** |
| 90 |
* Add a new atom to the molecule and set its position. |
| 91 |
* @param atomicNumber The atomic number of the new atom. |
| 92 |
* @param position3d The position of the atom. |
| 93 |
* @return The new Atom object. |
| 94 |
*/ |
| 95 |
AtomType addAtom(unsigned char atomicNumber, const Vector3& position3d); |
| 96 |
|
| 97 |
/** |
| 98 |
* Obtain an atom object. |
| 99 |
* @param atomId The index of the atom to return. |
| 100 |
* @return The requested atom object. Will be invalid if @a atomId >= @a |
| 101 |
* atomCount(). |
| 102 |
*/ |
| 103 |
AtomType atom(Index atomId) const; |
| 104 |
|
| 105 |
/** |
| 106 |
* Obtain an atom object from it's unique id. |
| 107 |
* @param atomUId The unique of the requested atom. |
| 108 |
* @return The requested atom object. Will be invalid if @a atomUId is not |
| 109 |
* in use. |
| 110 |
*/ |
| 111 |
AtomType atomByUniqueId(Index atomUId) const; |
| 112 |
|
| 113 |
/** |
| 114 |
* @return The unique id of the atom. |
| 115 |
* @{ |
| 116 |
*/ |
| 117 |
Index atomUniqueId(Index atomId) const; |
| 118 |
Index atomUniqueId(const AtomType& atom) const; |
| 119 |
/** @} */ |
| 120 |
|
| 121 |
/** |
| 122 |
* @return The number of atoms in the molecule. |
| 123 |
*/ |
| 124 |
Index atomCount() const; |
| 125 |
|
| 126 |
/** |
| 127 |
* @return The number of atoms in the molecule with the atomic number @a |
| 128 |
* atomicNumber. |
| 129 |
*/ |
| 130 |
Index atomCount(unsigned char atomicNumber) const; |
| 131 |
|
| 132 |
/** |
| 133 |
* Delete the specified atom from this molecule. |
| 134 |
* @return True on success, false otherwise. |
| 135 |
* @note This also removes all bonds connected to the atom. |
| 136 |
* @{ |
| 137 |
*/ |
| 138 |
bool removeAtom(Index atomId); |
| 139 |
bool removeAtom(const AtomType& atom); |
| 140 |
/** @} */ |
| 141 |
|
| 142 |
/** |
| 143 |
* Delete all atoms from this molecule. |
| 144 |
* @note This also removes all bonds. |
| 145 |
*/ |
| 146 |
void clearAtoms(); |
| 147 |
|
| 148 |
/** |
| 149 |
* Adjust hydrogens for an atom. |
| 150 |
* @param atomId The index of the atom. |
| 151 |
* @note Checks to make sure the atom is valid before adjusting the hydrogens. |
| 152 |
*/ |
| 153 |
void adjustHydrogens(Index atomId); |
| 154 |
|
| 155 |
/** |
| 156 |
* Adjust hydrogens for multiple atoms. |
| 157 |
* @param atomIds The indices for the atoms. |
| 158 |
* @note Checks to make sure the atoms are valid before adjusting the |
| 159 |
* hydrogens. |
| 160 |
*/ |
| 161 |
void adjustHydrogens(const Core::Array<Index>& atomIds); |
| 162 |
|
| 163 |
/** |
| 164 |
* @return An array containing atomic numbers for all atoms in the molecule, |
| 165 |
* indexed by atom index. |
| 166 |
*/ |
| 167 |
const Core::Array<unsigned char>& atomicNumbers() const; |
| 168 |
|
| 169 |
/** |
| 170 |
* Get the atomic number for the requested atom. |
| 171 |
* @param atomId The index of the atom. |
| 172 |
* @return The atomic number of the atom indexed at @a atomId, or |
| 173 |
* Avogadro::InvalidElement if @a atomId is invalid. |
| 174 |
*/ |
| 175 |
unsigned char atomicNumber(Index atomId) const; |
| 176 |
|
| 177 |
/** |
| 178 |
* Replace the current array of atomic numbers. |
| 179 |
* @param nums The new atomic number array. Must be of length atomCount(). |
| 180 |
* @return True on success, false otherwise. |
| 181 |
*/ |
| 182 |
bool setAtomicNumbers(const Core::Array<unsigned char>& nums); |
| 183 |
|
| 184 |
/** |
| 185 |
* Set the atomic number of a single atom. |
| 186 |
* @param atomId The index of the atom to modify. |
| 187 |
* @param atomicNumber The new atomic number. |
| 188 |
* @return True on success, false otherwise. |
| 189 |
*/ |
| 190 |
bool setAtomicNumber(Index atomId, unsigned char atomicNumber); |
| 191 |
|
| 192 |
/** |
| 193 |
* @return An array containing 3D coordinates for all atoms in the molecule, |
| 194 |
* indexed by atom index. |
| 195 |
* @note May be empty if position information has not been set for any atoms. |
| 196 |
*/ |
| 197 |
const Core::Array<Vector3>& atomPositions3d() const; |
| 198 |
|
| 199 |
/** |
| 200 |
* Get the 3D position of a single atom. |
| 201 |
* @param atomId The index of the atom. |
| 202 |
* @return The position of the atom, or Vector3::Zero() if no position |
| 203 |
* information has been set. |
| 204 |
*/ |
| 205 |
Vector3 atomPosition3d(Index atomId) const; |
| 206 |
|
| 207 |
/** |
| 208 |
* Replace the current array of 3D atomic coordinates. |
| 209 |
* @param pos The new coordinate array. Must be of length atomCount(). |
| 210 |
* @param undoText The undo text to be displayed for undo commands. |
| 211 |
* @return True on success, false otherwise. |
| 212 |
*/ |
| 213 |
bool setAtomPositions3d( |
| 214 |
const Core::Array<Vector3>& pos, |
| 215 |
const QString& undoText = tr("Change Atom Positions")); |
| 216 |
|
| 217 |
/** |
| 218 |
* Set the 3D position of a single atom. |
| 219 |
* @param atomId The index of the atom to modify. |
| 220 |
* @param pos The new position of the atom. |
| 221 |
* @param undoText The undo text to be displayed for undo commands. |
| 222 |
* @return True on success, false otherwise. |
| 223 |
*/ |
| 224 |
bool setAtomPosition3d(Index atomId, const Vector3& pos, |
| 225 |
const QString& undoText = tr("Change Atom Position")); |
| 226 |
|
| 227 |
/** |
| 228 |
* Get the label on an atom. |
| 229 |
* @param atomId The index of the atom. |
| 230 |
* @return The label of the atom indexed at @a atomId, or an empty string if |
| 231 |
* @a atomId is invalid or has no label. |
| 232 |
*/ |
| 233 |
std::string atomLabel(Index atomId) const; |
| 234 |
|
| 235 |
/** |
| 236 |
* Set the label of a single atom. |
| 237 |
* @param atomId The index of the atom to modify. |
| 238 |
* @param label The new label. |
| 239 |
* @param undoText The undo text to be displayed for undo commands. |
| 240 |
* @return True on success, false otherwise. |
| 241 |
*/ |
| 242 |
bool setAtomLabel(Index atomId, const std::string& label, |
| 243 |
const QString& undoText = tr("Change Atom Label")); |
| 244 |
|
| 245 |
/** |
| 246 |
* Get the label on a bond |
| 247 |
* @param bondId The index of the bond. |
| 248 |
* @return The label of the bond indexed at @a bondId, or an empty string if |
| 249 |
* @a bondId is invalid or has no label. |
| 250 |
*/ |
| 251 |
std::string bondLabel(Index bondId) const; |
| 252 |
|
| 253 |
/** |
| 254 |
* Set the label of a single bond. |
| 255 |
* @param bondId The index of the bond to modify. |
| 256 |
* @param label The new label. |
| 257 |
* @param undoText The undo text to be displayed for undo commands. |
| 258 |
* @return True on success, false otherwise. |
| 259 |
*/ |
| 260 |
bool setBondLabel(Index bondId, const std::string& label, |
| 261 |
const QString& undoText = tr("Change Bond Label")); |
| 262 |
|
| 263 |
/** |
| 264 |
* Set whether the specified atom is selected or not. |
| 265 |
*/ |
| 266 |
void setAtomSelected(Index atomId, bool selected, |
| 267 |
const QString& undoText = tr("Change Selection")); |
| 268 |
|
| 269 |
/** |
| 270 |
* Query whether the supplied atom index has been selected. |
| 271 |
*/ |
| 272 |
bool atomSelected(Index atomId) const; |
| 273 |
|
| 274 |
bool setAtomPosition2d(Index, const Vector2&) { return false; } |
| 275 |
Vector2 atomPosition2d(Index) { return Vector2(0, 0); } |
| 276 |
const Core::Array<Vector2>& atomPositions2d() const |
| 277 |
{ |
| 278 |
return m_molecule.m_positions2d; |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Get the hybridization for the requested atom. |
| 283 |
* @param atomId The index of the atom. |
| 284 |
* @return The hybridization of the atom indexed at @a atomId, or |
| 285 |
* 0 if @a atomId is invalid. |
| 286 |
*/ |
| 287 |
Core::AtomHybridization hybridization(Index atomId) const; |
| 288 |
|
| 289 |
/** |
| 290 |
* Set the hybridization of a single atom. |
| 291 |
* @param atomId The index of the atom to modify. |
| 292 |
* @param hyb The new hybridization. |
| 293 |
* @return True on success, false otherwise. |
| 294 |
*/ |
| 295 |
bool setHybridization(Index atomId, Core::AtomHybridization hyb); |
| 296 |
|
| 297 |
/** |
| 298 |
* Get the formal charge for the requested atom. |
| 299 |
* @param atomId The index of the atom. |
| 300 |
* @return The formal charge of the atom indexed at @a atomId, or |
| 301 |
* 0 if @a atomId is invalid. |
| 302 |
*/ |
| 303 |
signed char formalCharge(Index atomId) const; |
| 304 |
|
| 305 |
/** |
| 306 |
* Set the formal charge of a single atom. |
| 307 |
* @param atomId The index of the atom to modify. |
| 308 |
* @param charge The new formal charge. |
| 309 |
* @return True on success, false otherwise. |
| 310 |
*/ |
| 311 |
bool setFormalCharge(Index atomId, signed char charge); |
| 312 |
|
| 313 |
/** |
| 314 |
* Get the isotope for the requested atom. |
| 315 |
* @param atomId The index of the atom. |
| 316 |
* @return The isotope of the atom indexed at @a atomId, or |
| 317 |
* 0 if @a atomId is invalid. (default is 0 for most common isotope) |
| 318 |
*/ |
| 319 |
unsigned short isotope(Index atomId) const; |
| 320 |
|
| 321 |
/** |
| 322 |
* Set the isotope of a single atom. |
| 323 |
* @param atomId The index of the atom to modify. |
| 324 |
* @param isotope The new isotope |
| 325 |
* @return True on success, false otherwise. |
| 326 |
*/ |
| 327 |
bool setIsotope(Index atomId, unsigned short isotope); |
| 328 |
|
| 329 |
/** |
| 330 |
* Get the color for the requested atom. |
| 331 |
* @param atomId The index of the atom. |
| 332 |
* @return The color of the atom indexed at @a atomId, or |
| 333 |
* (0, 0, 0) if @a atomId is invalid. If no color is set for the |
| 334 |
* given atomId, the default color for the atomic number of |
| 335 |
* the atomId is returned. |
| 336 |
*/ |
| 337 |
Vector3ub color(Index atomId) const; |
| 338 |
|
| 339 |
/** |
| 340 |
* Set the color of a single atom. |
| 341 |
* @param atomId The index of the atom to modify. |
| 342 |
* @param color The new color. |
| 343 |
* @return True on success, false otherwise. |
| 344 |
*/ |
| 345 |
bool setColor(Index atomId, Vector3ub color); |
| 346 |
|
| 347 |
/** |
| 348 |
* Set the color of a single residue. |
| 349 |
* @param residueId The index of the residue to modify. |
| 350 |
* @param color The new color. |
| 351 |
* @return True on success, false otherwise. |
| 352 |
*/ |
| 353 |
bool setResidueColor(Index residueId, Vector3ub color); |
| 354 |
|
| 355 |
bool setLayer(Index atomId, size_t layer); |
| 356 |
size_t layer(Index atomId) const; |
| 357 |
|
| 358 |
/** |
| 359 |
* Create a new bond in the molecule. |
| 360 |
* @param atom1 The first atom in the bond. |
| 361 |
* @param atom2 The second order in the bond. |
| 362 |
* @param order The bond order. |
| 363 |
* @return The new bond object. Will be invalid if @a atom1 or @a atom2 does |
| 364 |
* not exist. |
| 365 |
* @{ |
| 366 |
*/ |
| 367 |
BondType addBond(Index atom1, Index atom2, unsigned char order = 1); |
| 368 |
BondType addBond(const AtomType& atom1, const AtomType& atom2, |
| 369 |
unsigned char order = 1); |
| 370 |
/** @} */ |
| 371 |
|
| 372 |
/** |
| 373 |
* Get a bond object. |
| 374 |
* @param bondId The index of the requested bond. |
| 375 |
* @return The requested bond object. Will be invalid if @a bondId >= |
| 376 |
* @a bondCount(). |
| 377 |
*/ |
| 378 |
BondType bond(Index bondId) const; |
| 379 |
|
| 380 |
/** |
| 381 |
* Get a bond object. |
| 382 |
* @param atom1 The index of one atom in the bond. |
| 383 |
* @param atom2 The index of the other atom in bond. |
| 384 |
* @return The requested bond object. Will be invalid if @a atom1 or @a atom2 |
| 385 |
* do not exist. |
| 386 |
*/ |
| 387 |
BondType bond(Index atom1, Index atom2) const; |
| 388 |
|
| 389 |
/** |
| 390 |
* Get a bond object. |
| 391 |
* @param atom1 One atom in the bond. |
| 392 |
* @param atom2 The other atom in bond. |
| 393 |
* @return The requested bond object. Will be invalid if @a atom1 or @a atom2 |
| 394 |
* are invalid. |
| 395 |
*/ |
| 396 |
BondType bond(const AtomType& atom1, const AtomType& atom2) const; |
| 397 |
|
| 398 |
/** |
| 399 |
* Get a bond object from its unique id. |
| 400 |
* @param bondUid The unique id of the bond. |
| 401 |
* @return The requested bond object. Will be invalid if @a bondUid is not in |
| 402 |
* use. |
| 403 |
*/ |
| 404 |
BondType bondByUniqueId(Index bondUid) const; |
| 405 |
|
| 406 |
/** |
| 407 |
* Get the unique id of a bond. |
| 408 |
* @param bondId The index of the requested bond. |
| 409 |
* @return The unique id currently assigned to the bond at index @a bondId |
| 410 |
*/ |
| 411 |
Index bondUniqueId(Index bondId) const; |
| 412 |
|
| 413 |
/** |
| 414 |
* Get the unique id of a bond. |
| 415 |
* @param bond The requested bond object. |
| 416 |
* @return The unique id currently assigned to @a bond. |
| 417 |
*/ |
| 418 |
Index bondUniqueId(const BondType& bond) const; |
| 419 |
|
| 420 |
/** |
| 421 |
* @return The number of bonds in the molecule. |
| 422 |
*/ |
| 423 |
Index bondCount() const; |
| 424 |
|
| 425 |
/** |
| 426 |
* Remove the requested bond. |
| 427 |
* @return True on success, false otherwise. |
| 428 |
* @{ |
| 429 |
*/ |
| 430 |
bool removeBond(Index bondId); |
| 431 |
bool removeBond(const BondType& bond); |
| 432 |
bool removeBond(Index atom1, Index atom2); |
| 433 |
bool removeBond(const AtomType& atom1, const AtomType& atom2); |
| 434 |
/** @} */ |
| 435 |
|
| 436 |
/** |
| 437 |
* Remove all bonds from the molecule. |
| 438 |
*/ |
| 439 |
void clearBonds(); |
| 440 |
|
| 441 |
/** |
| 442 |
* Find bonds connected to an atom. |
| 443 |
* @param atom The atom of interest. |
| 444 |
* @return An array of bond objects that are attached to the specified atom. |
| 445 |
*/ |
| 446 |
Core::Array<BondType> bonds(const AtomType& atom) const; |
| 447 |
|
| 448 |
/** |
| 449 |
* Find bonds connected to an atom. |
| 450 |
* @param atomId The index for the atom of interest. |
| 451 |
* @return An array of bond objects that are attached to the specified atom. |
| 452 |
*/ |
| 453 |
Core::Array<BondType> bonds(const Index& atomId) const; |
| 454 |
|
| 455 |
/** |
| 456 |
* @return An array of bond orders for all bonds in the molecule, indexed by |
| 457 |
* bond index. |
| 458 |
*/ |
| 459 |
const Core::Array<unsigned char>& bondOrders() const; |
| 460 |
|
| 461 |
/** |
| 462 |
* Get the order of a bond. |
| 463 |
* @param bondId The id of the bond. |
| 464 |
* @return The bond order. |
| 465 |
*/ |
| 466 |
unsigned char bondOrder(Index bondId) const; |
| 467 |
|
| 468 |
/** |
| 469 |
* Replace the current array of bond orders. |
| 470 |
* @param orders The new array. |
| 471 |
* @return True on success, false on failure. |
| 472 |
*/ |
| 473 |
bool setBondOrders(const Core::Array<unsigned char>& orders); |
| 474 |
|
| 475 |
/** |
| 476 |
* Set the order of a bond in the molecule. |
| 477 |
* @param bondId The bond's index. |
| 478 |
* @param order The new order of the bond. |
| 479 |
* @return True on success, false on failure. |
| 480 |
*/ |
| 481 |
bool setBondOrder(Index bondId, unsigned char order); |
| 482 |
|
| 483 |
/** |
| 484 |
* @return An array of all bonded atoms in the molecule, indexed by bond |
| 485 |
* index. |
| 486 |
* Each bond pair is represented by a pair of atom indices. |
| 487 |
*/ |
| 488 |
const Core::Array<std::pair<Index, Index>>& bondPairs() const; |
| 489 |
|
| 490 |
/** |
| 491 |
* Get the set of bonded atoms corresponding to @a bondId. |
| 492 |
* @param bondId The index of the requested bond. |
| 493 |
* @return The bonded atom pair, represented as a pair of atom indices. |
| 494 |
*/ |
| 495 |
std::pair<Index, Index> bondPair(Index bondId) const; |
| 496 |
|
| 497 |
/** |
| 498 |
* Replace the current array of bonded atoms. |
| 499 |
* @param pairs The array. |
| 500 |
* @return True on success, false on failure. |
| 501 |
* @note The bonded atoms are represented as a pair of bond indices. |
| 502 |
* @note If needed, the elements in @a pairs will be modified to ensure that |
| 503 |
* the first atom index is less than the second. |
| 504 |
*/ |
| 505 |
bool setBondPairs(const Core::Array<std::pair<Index, Index>>& pairs); |
| 506 |
|
| 507 |
/** |
| 508 |
* Set the bonded atoms for a bond. |
| 509 |
* @param bondId The bond to modify. |
| 510 |
* @param pair The new bond pair. |
| 511 |
* @return True on success, false otherwise. |
| 512 |
* @note If needed, @a pair will be modified to ensure that the first atom |
| 513 |
* index is less than the second. |
| 514 |
*/ |
| 515 |
bool setBondPair(Index bondId, const std::pair<Index, Index>& pair); |
| 516 |
|
| 517 |
/** |
| 518 |
* Add a default unit cell to the molecule. Does nothing if there already |
| 519 |
* is a unit cell. Changes are emitted. |
| 520 |
*/ |
| 521 |
void addUnitCell(); |
| 522 |
|
| 523 |
/** |
| 524 |
* Remove the unit cell from the molecule. Does nothing if there is |
| 525 |
* no unit cell. Changes are emitted. |
| 526 |
*/ |
| 527 |
void removeUnitCell(); |
| 528 |
|
| 529 |
/** |
| 530 |
* Add a residue to the molecule. |
| 531 |
* @param residue The residue to be added. |
| 532 |
* @param offset The atomic index offset for the new residue. |
| 533 |
*/ |
| 534 |
void addResidue(const Core::Residue& residue, Index offset = 0); |
| 535 |
|
| 536 |
/** |
| 537 |
* Generic edit that changes the current molecule to be @a newMolecule. |
| 538 |
* Also sets the text for the undo command to be @a undoText. Changes are |
| 539 |
* emitted. |
| 540 |
* @param newMolecule The new molecule to be set. |
| 541 |
* @param changes The changes to be emitted. |
| 542 |
* @param undoText The text description for the undo command. |
| 543 |
*/ |
| 544 |
void modifyMolecule( |
| 545 |
const Molecule& newMolecule, Molecule::MoleculeChanges changes, |
| 546 |
const QString& undoText = QStringLiteral("Modify Molecule")); |
| 547 |
|
| 548 |
/** |
| 549 |
* Generic edit that adds @a newMolecule to the current molecule. |
| 550 |
* Also sets the text for the undo command to be @a undoText. Changes are |
| 551 |
* emitted. |
| 552 |
* @param addMolecule The new molecule to be set. |
| 553 |
* @param undoText The text description for the undo command. |
| 554 |
*/ |
| 555 |
void appendMolecule( |
| 556 |
const Molecule& addMolecule, |
| 557 |
const QString& undoText = QStringLiteral("Append Molecule")); |
| 558 |
|
| 559 |
/** |
| 560 |
* Edit the unit cell by replacing the current cell matrix with a new cell |
| 561 |
* matrix. Changes are emitted. |
| 562 |
* @param cellMatrix The new cell matrix to be set. |
| 563 |
* @param opts If TransformAtoms is specified, the atoms in @a molecule are |
| 564 |
* adjusted so that their fractional (lattice) coordinates are preserved. This |
| 565 |
* option is ignored if the input molecule has no unit cell. |
| 566 |
*/ |
| 567 |
void editUnitCell(Matrix3 cellMatrix, Core::CrystalTools::Options opts); |
| 568 |
|
| 569 |
/** |
| 570 |
* Wrap atoms to the unit cell. Changes are emitted. |
| 571 |
*/ |
| 572 |
void wrapAtomsToCell(); |
| 573 |
|
| 574 |
/** |
| 575 |
* Rotate cell to standard orientation. Changes are emitted. |
| 576 |
*/ |
| 577 |
void rotateCellToStandardOrientation(); |
| 578 |
|
| 579 |
/** |
| 580 |
* Scale a cell's volume. Changes are emitted. |
| 581 |
* @param newVolume The new volume to be set. |
| 582 |
* @param options If CrystalTools::TransformAtoms is set, then |
| 583 |
* the atoms will be transformed during the scaling. |
| 584 |
*/ |
| 585 |
void setCellVolume(double newVolume, Core::CrystalTools::Options options); |
| 586 |
|
| 587 |
/** |
| 588 |
* Build a supercell. Changes are emitted. |
| 589 |
* @param a The final number of units along the A vector (at least 1). |
| 590 |
* @param b The final number of units along the B vector (at least 1). |
| 591 |
* @param c The final number of units along the C vector (at least 1). |
| 592 |
*/ |
| 593 |
void buildSupercell(unsigned int a, unsigned int b, unsigned int c); |
| 594 |
|
| 595 |
/** |
| 596 |
* Perform a Niggli reduction on the cell. Changes are emitted. |
| 597 |
*/ |
| 598 |
void niggliReduceCell(); |
| 599 |
|
| 600 |
/** |
| 601 |
* Use spglib to reduce the cell to its primitive form. Changes are emitted. |
| 602 |
* @param cartTol Cartesian tolerance for primitive reduction. |
| 603 |
* @param copyTol If > 0, add translational boundary copies with this |
| 604 |
* tolerance in the same undo step. |
| 605 |
* @return True if the algorithm succeeded, and false if it failed. |
| 606 |
*/ |
| 607 |
bool reduceCellToPrimitive(double cartTol = 1e-5, double copyTol = 0.0); |
| 608 |
|
| 609 |
/** |
| 610 |
* Use spglib to convert the cell to its conventional form. Changes are |
| 611 |
* emitted. |
| 612 |
* @param cartTol Cartesian tolerance for conventionalization. |
| 613 |
* @param copyTol If > 0, add translational boundary copies with this |
| 614 |
* tolerance in the same undo step. |
| 615 |
* @return True if the algorithm succeeded, and false if it failed. |
| 616 |
*/ |
| 617 |
bool conventionalizeCell(double cartTol = 1e-5, double copyTol = 0.0); |
| 618 |
|
| 619 |
/** |
| 620 |
* Use spglib to symmetrize the cell. Changes are emitted. |
| 621 |
* @param cartTol Cartesian tolerance for symmetrization. |
| 622 |
* @param copyTol If > 0, add translational boundary copies with this |
| 623 |
* tolerance in the same undo step. |
| 624 |
* @return True if the algorithm succeeded, and false if it failed. |
| 625 |
*/ |
| 626 |
bool symmetrizeCell(double cartTol = 1e-5, double copyTol = 0.0); |
| 627 |
|
| 628 |
/** |
| 629 |
* Fill unit cell using transforms for the space group. Changes are emitted. |
| 630 |
* @param hallNumber The hall number to be used for transforming the cell. |
| 631 |
* @param cartTol Cartesian tolerance for comparing atom positions. |
| 632 |
* @param allCopies If true, all copies will be filled (i.e. edges and |
| 633 |
* corners) |
| 634 |
* @return True if the algorithm succeeded, and false if it failed. |
| 635 |
*/ |
| 636 |
bool fillUnitCell(unsigned short hallNumber, double cartTol = 1e-5, |
| 637 |
bool allCopies = false); |
| 638 |
|
| 639 |
/** |
| 640 |
* Add translational copies of atoms on the unit cell boundary without |
| 641 |
* applying symmetry operations. Changes are emitted. |
| 642 |
* @param cartTol Cartesian tolerance for comparing atom positions. |
| 643 |
* @return True if the algorithm succeeded, and false if it failed. |
| 644 |
*/ |
| 645 |
bool fillTranslationalCopies(double cartTol = 1e-5); |
| 646 |
|
| 647 |
/** |
| 648 |
* Use transforms to reduce a cell to its asymmetric unit. Changes are |
| 649 |
* emitted. |
| 650 |
* @param hallNumber The hall number to be used for obtaining the transforms. |
| 651 |
* @param cartTol Cartesian tolerance for comparing atom positions. |
| 652 |
* @return True if the algorithm succeeded, and false if it failed. |
| 653 |
*/ |
| 654 |
bool reduceCellToAsymmetricUnit(unsigned short hallNumber, |
| 655 |
double cartTol = 1e-5); |
| 656 |
|
| 657 |
/** |
| 658 |
* Call this function when you wish to merge all undo commands. |
| 659 |
* It turns on interactive mode to merge similar undo commands in a series |
| 660 |
* (in order to save space), and it uses QUndoStack's beginMacro() to merge |
| 661 |
* dissimilar undo commands together. You must call endMergeMode() to end |
| 662 |
* the merging section (undo and redo are unavailable while merge mode is |
| 663 |
* on). |
| 664 |
* @param undoName The name of the undo command |
| 665 |
*/ |
| 666 |
void beginMergeMode(const QString& undoName = QStringLiteral("Draw")); |
| 667 |
|
| 668 |
/** |
| 669 |
* Call this function when you wish merge mode to end. This will turn off |
| 670 |
* interactive mode, and it will call QUndoStack's endMacro(). All of the |
| 671 |
* undo commands pushed between beginMergeMode() and endMergeMode() will be |
| 672 |
* merged into one undo command. beginMergeMode() should have been called |
| 673 |
* before this is called. |
| 674 |
*/ |
| 675 |
void endMergeMode(); |
| 676 |
|
| 677 |
/** |
| 678 |
* @brief Begin or end an interactive edit. |
| 679 |
* |
| 680 |
* If enabled, certain undo operations will be merged together. For instance, |
| 681 |
* an editor dragging an atom through space in response to mouse movement will |
| 682 |
* only generate a single undo command containing the initial and final |
| 683 |
* positions and discard the intermediate states. If disabled, each |
| 684 |
* intermediate action will appear in the undo log. |
| 685 |
*/ |
| 686 |
void setInteractive(bool b); |
| 687 |
|
| 688 |
/** |
| 689 |
* @return True if interactive mode is enabled, false otherwise. |
| 690 |
* @sa setInteractive |
| 691 |
*/ |
| 692 |
bool isInteractive() const; |
| 693 |
|
| 694 |
/** |
| 695 |
* @return The QUndoStack for this molecule. |
| 696 |
* @{ |
| 697 |
*/ |
| 698 |
QUndoStack& undoStack(); |
| 699 |
const QUndoStack& undoStack() const; |
| 700 |
/** @} */ |
| 701 |
|
| 702 |
class UndoCommand; |
| 703 |
friend class UndoCommand; |
| 704 |
|
| 705 |
/** Returns a vector of forces for the atoms in the molecule. */ |
| 706 |
const Core::Array<Vector3>& forceVectors() const; |
| 707 |
|
| 708 |
/** |
| 709 |
* Replace the current array of force vectors. |
| 710 |
* @param pos The new force vector array. Must be of length atomCount(). |
| 711 |
* @param undoText The undo text to be displayed for undo commands. |
| 712 |
* @return True on success, false otherwise. |
| 713 |
*/ |
| 714 |
bool setForceVector( |
| 715 |
Index atomId, const Vector3& pos, |
| 716 |
const QString& undoText = QStringLiteral("Change Force Vectors")); |
| 717 |
|
| 718 |
public slots: |
| 719 |
/** |
| 720 |
* @brief Force the molecule to emit the changed() signal. |
| 721 |
* @param change See changed(). |
| 722 |
*/ |
| 723 |
void emitChanged(unsigned int change); |
| 724 |
|
| 725 |
signals: |
| 726 |
/** |
| 727 |
* @brief Indicates that the molecule has changed. |
| 728 |
* @param change Use the MoleculeChange enum to check what has changed. |
| 729 |
* |
| 730 |
* The @p change variable indicates what has changed, i.e. if |
| 731 |
* change & Atoms == true then atoms were changed in some way, and if |
| 732 |
* change & Removed == true then one or more atoms were removed. |
| 733 |
*/ |
| 734 |
void changed(unsigned int change); |
| 735 |
|
| 736 |
protected: |
| 737 |
Index findAtomUniqueId(Index atomId) const; |
| 738 |
Index findBondUniqueId(Index bondId) const; |
| 739 |
|
| 740 |
/** |
| 741 |
* @brief m_molecule still stored all data, this class acts upon it and builds |
| 742 |
* an undo/redo stack that can be used to offer undo and redo. |
| 743 |
*/ |
| 744 |
Molecule& m_molecule; |
| 745 |
bool m_interactive; |
| 746 |
|
| 747 |
QUndoStack m_undoStack; |
| 748 |
|
| 749 |
friend class Molecule; |
| 750 |
}; |
| 751 |
|
| 752 |
class AVOGADROQTGUI_EXPORT RWAtom : public Core::AtomTemplate<RWMolecule> |
| 753 |
{ |
| 754 |
public: |
| 755 |
RWAtom() : Core::AtomTemplate<RWMolecule>() {} |
| 756 |
RWAtom(RWMolecule* m, Index i) : Core::AtomTemplate<RWMolecule>(m, i) {} |
| 757 |
}; |
| 758 |
|
| 759 |
class AVOGADROQTGUI_EXPORT RWBond : public Core::BondTemplate<RWMolecule> |
| 760 |
{ |
| 761 |
public: |
| 762 |
RWBond() : Core::BondTemplate<RWMolecule>() {} |
| 763 |
RWBond(RWMolecule* m, Index i) : Core::BondTemplate<RWMolecule>(m, i) {} |
| 764 |
}; |
| 765 |
|
| 766 |
inline RWMolecule::AtomType RWMolecule::atom(Index atomId) const |
| 767 |
{ |
| 768 |
return AtomType(const_cast<RWMolecule*>(this), atomId); |
| 769 |
} |
| 770 |
|
| 771 |
inline RWMolecule::AtomType RWMolecule::atomByUniqueId(Index atomUId) const |
| 772 |
{ |
| 773 |
return atomUId < m_molecule.m_atomUniqueIds.size() |
| 774 |
? AtomType(const_cast<RWMolecule*>(this), |
| 775 |
m_molecule.m_atomUniqueIds[atomUId]) |
| 776 |
: AtomType(); |
| 777 |
} |
| 778 |
|
| 779 |
inline Index RWMolecule::atomUniqueId(Index atomId) const |
| 780 |
{ |
| 781 |
return findAtomUniqueId(atomId); |
| 782 |
} |
| 783 |
|
| 784 |
inline Index RWMolecule::atomUniqueId(const RWMolecule::AtomType& a) const |
| 785 |
{ |
| 786 |
return a.molecule() == this ? findAtomUniqueId(a.index()) : MaxIndex; |
| 787 |
} |
| 788 |
|
| 789 |
inline Index RWMolecule::atomCount() const |
| 790 |
{ |
| 791 |
return m_molecule.atomCount(); |
| 792 |
} |
| 793 |
|
| 794 |
inline bool RWMolecule::removeAtom(const AtomType& a) |
| 795 |
{ |
| 796 |
return a.molecule() == this ? removeAtom(a.index()) : false; |
| 797 |
} |
| 798 |
|
| 799 |
inline const Core::Array<unsigned char>& RWMolecule::atomicNumbers() const |
| 800 |
{ |
| 801 |
return m_molecule.atomicNumbers(); |
| 802 |
} |
| 803 |
|
| 804 |
inline unsigned char RWMolecule::atomicNumber(Index atomId) const |
| 805 |
{ |
| 806 |
return m_molecule.atomicNumber(atomId); |
| 807 |
} |
| 808 |
|
| 809 |
inline const Core::Array<Vector3>& RWMolecule::atomPositions3d() const |
| 810 |
{ |
| 811 |
return m_molecule.atomPositions3d(); |
| 812 |
} |
| 813 |
|
| 814 |
inline Vector3 RWMolecule::atomPosition3d(Index atomId) const |
| 815 |
{ |
| 816 |
return m_molecule.atomPosition3d(atomId); |
| 817 |
} |
| 818 |
|
| 819 |
inline std::string RWMolecule::atomLabel(Index atomId) const |
| 820 |
{ |
| 821 |
return m_molecule.atomLabel(atomId); |
| 822 |
} |
| 823 |
|
| 824 |
inline std::string RWMolecule::bondLabel(Index bondId) const |
| 825 |
{ |
| 826 |
return m_molecule.bondLabel(bondId); |
| 827 |
} |
| 828 |
|
| 829 |
inline Core::AtomHybridization RWMolecule::hybridization(Index atomId) const |
| 830 |
{ |
| 831 |
return m_molecule.hybridization(atomId); |
| 832 |
} |
| 833 |
|
| 834 |
inline signed char RWMolecule::formalCharge(Index atomId) const |
| 835 |
{ |
| 836 |
return m_molecule.formalCharge(atomId); |
| 837 |
} |
| 838 |
|
| 839 |
inline unsigned short RWMolecule::isotope(Index atomId) const |
| 840 |
{ |
| 841 |
return m_molecule.isotope(atomId); |
| 842 |
} |
| 843 |
|
| 844 |
inline Vector3ub RWMolecule::color(Index atomId) const |
| 845 |
{ |
| 846 |
return m_molecule.color(atomId); |
| 847 |
} |
| 848 |
|
| 849 |
inline size_t RWMolecule::layer(Index atomId) const |
| 850 |
{ |
| 851 |
return m_molecule.layer(atomId); |
| 852 |
} |
| 853 |
|
| 854 |
inline RWMolecule::BondType RWMolecule::addBond(const AtomType& atom1, |
| 855 |
const AtomType& atom2, |
| 856 |
unsigned char order) |
| 857 |
{ |
| 858 |
if (atom1.molecule() != atom2.molecule() || atom1.molecule() != this) |
| 859 |
return BondType(); |
| 860 |
return addBond(atom1.index(), atom2.index(), order); |
| 861 |
} |
| 862 |
|
| 863 |
inline RWMolecule::BondType RWMolecule::bond(Index bondId) const |
| 864 |
{ |
| 865 |
return BondType(const_cast<RWMolecule*>(this), bondId); |
| 866 |
} |
| 867 |
|
| 868 |
inline RWMolecule::BondType RWMolecule::bond( |
| 869 |
const RWMolecule::AtomType& atom1, const RWMolecule::AtomType& atom2) const |
| 870 |
{ |
| 871 |
if (atom1.molecule() == atom2.molecule() && atom1.molecule() == this) |
| 872 |
return bond(atom1.index(), atom2.index()); |
| 873 |
return BondType(); |
| 874 |
} |
| 875 |
|
| 876 |
inline RWMolecule::BondType RWMolecule::bondByUniqueId(Index bondUid) const |
| 877 |
{ |
| 878 |
return bondUid < m_molecule.m_bondUniqueIds.size() |
| 879 |
? BondType(const_cast<RWMolecule*>(this), |
| 880 |
m_molecule.m_bondUniqueIds[bondUid]) |
| 881 |
: BondType(); |
| 882 |
} |
| 883 |
|
| 884 |
inline Index RWMolecule::bondUniqueId(Index bondId) const |
| 885 |
{ |
| 886 |
return findBondUniqueId(bondId); |
| 887 |
} |
| 888 |
|
| 889 |
inline Index RWMolecule::bondUniqueId(const RWMolecule::BondType& b) const |
| 890 |
{ |
| 891 |
return b.molecule() == this ? findBondUniqueId(b.index()) : MaxIndex; |
| 892 |
} |
| 893 |
|
| 894 |
inline Index RWMolecule::bondCount() const |
| 895 |
{ |
| 896 |
return m_molecule.bondCount(); |
| 897 |
} |
| 898 |
|
| 899 |
inline bool RWMolecule::removeBond(const RWMolecule::BondType& b) |
| 900 |
{ |
| 901 |
return b.molecule() == this ? removeBond(b.index()) : false; |
| 902 |
} |
| 903 |
|
| 904 |
inline bool RWMolecule::removeBond(Index atom1, Index atom2) |
| 905 |
{ |
| 906 |
return removeBond(bond(atom1, atom2).index()); |
| 907 |
} |
| 908 |
|
| 909 |
inline bool RWMolecule::removeBond(const RWMolecule::AtomType& atom1, |
| 910 |
const RWMolecule::AtomType& atom2) |
| 911 |
{ |
| 912 |
if (atom1.molecule() != atom2.molecule() || atom1.molecule() != this) |
| 913 |
return false; |
| 914 |
return removeBond(bond(atom1.index(), atom2.index()).index()); |
| 915 |
} |
| 916 |
|
| 917 |
inline Core::Array<RWMolecule::BondType> RWMolecule::bonds( |
| 918 |
const RWMolecule::AtomType& a) const |
| 919 |
{ |
| 920 |
return a.molecule() == this ? bonds(a.index()) : Core::Array<BondType>(); |
| 921 |
} |
| 922 |
|
| 923 |
inline Core::Array<RWMolecule::BondType> RWMolecule::bonds( |
| 924 |
const Index& atomId) const |
| 925 |
{ |
| 926 |
auto atomBonds = m_molecule.bonds(atomId); |
| 927 |
Core::Array<RWMolecule::BondType> result; |
| 928 |
for (Index i = 0; i < atomBonds.size(); ++i) { |
| 929 |
result.push_back( |
| 930 |
BondType(const_cast<RWMolecule*>(this), atomBonds[i].index())); |
| 931 |
} |
| 932 |
return result; |
| 933 |
} |
| 934 |
|
| 935 |
inline const Core::Array<unsigned char>& RWMolecule::bondOrders() const |
| 936 |
{ |
| 937 |
return m_molecule.bondOrders(); |
| 938 |
} |
| 939 |
|
| 940 |
inline unsigned char RWMolecule::bondOrder(Index bondId) const |
| 941 |
{ |
| 942 |
return m_molecule.bondOrder(bondId); |
| 943 |
} |
| 944 |
|
| 945 |
inline const Core::Array<std::pair<Index, Index>>& RWMolecule::bondPairs() const |
| 946 |
{ |
| 947 |
return m_molecule.bondPairs(); |
| 948 |
} |
| 949 |
|
| 950 |
inline std::pair<Index, Index> RWMolecule::bondPair(Index bondId) const |
| 951 |
{ |
| 952 |
return m_molecule.bondPair(bondId); |
| 953 |
} |
| 954 |
|
| 955 |
inline void RWMolecule::beginMergeMode(const QString& undoName) |
| 956 |
{ |
| 957 |
m_interactive = true; |
| 958 |
m_undoStack.beginMacro(undoName); |
| 959 |
} |
| 960 |
|
| 961 |
inline void RWMolecule::endMergeMode() |
| 962 |
{ |
| 963 |
m_interactive = false; |
| 964 |
m_undoStack.endMacro(); |
| 965 |
} |
| 966 |
|
| 967 |
inline void RWMolecule::setInteractive(bool b) |
| 968 |
{ |
| 969 |
m_interactive = b; |
| 970 |
} |
| 971 |
|
| 972 |
inline bool RWMolecule::isInteractive() const |
| 973 |
{ |
| 974 |
return m_interactive; |
| 975 |
} |
| 976 |
|
| 977 |
inline QUndoStack& RWMolecule::undoStack() |
| 978 |
{ |
| 979 |
return m_undoStack; |
| 980 |
} |
| 981 |
|
| 982 |
inline const QUndoStack& RWMolecule::undoStack() const |
| 983 |
{ |
| 984 |
return m_undoStack; |
| 985 |
} |
| 986 |
|
| 987 |
inline const Core::Array<Vector3>& RWMolecule::forceVectors() const |
| 988 |
{ |
| 989 |
return m_molecule.forceVectors(); |
| 990 |
} |
| 991 |
|
| 992 |
} // namespace QtGui |
| 993 |
} // namespace Avogadro |
| 994 |
|
| 995 |
#endif // AVOGADRO_QTGUI_RWMOLECULE_H |