| 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 |
#include "molecule.h" |
| 7 |
#include "rwmolecule.h" |
| 8 |
|
| 9 |
#include <iostream> |
| 10 |
|
| 11 |
// for HTML-formatted formulas |
| 12 |
#include <QtCore/QRegularExpression> |
| 13 |
|
| 14 |
namespace Avogadro::QtGui { |
| 15 |
|
| 16 |
using std::swap; |
| 17 |
|
| 18 |
Molecule::Molecule(QObject* p) |
| 19 |
: QObject(p), Core::Molecule(), m_undoMolecule(new RWMolecule(*this, this)) |
| 20 |
{ |
| 21 |
m_undoMolecule->setInteractive(false); |
| 22 |
} |
| 23 |
|
| 24 |
Molecule::Molecule(const Molecule& other) |
| 25 |
: QObject(), Core::Molecule(other), |
| 26 |
m_undoMolecule(new RWMolecule(*this, this)) |
| 27 |
{ |
| 28 |
m_undoMolecule->setInteractive(false); |
| 29 |
// Now assign the unique ids |
| 30 |
for (Index i = 0; i < atomCount(); i++) |
| 31 |
m_atomUniqueIds.push_back(i); |
| 32 |
|
| 33 |
for (Index i = 0; i < bondCount(); i++) |
| 34 |
m_bondUniqueIds.push_back(i); |
| 35 |
} |
| 36 |
|
| 37 |
Molecule::Molecule(const Core::Molecule& other) |
| 38 |
: QObject(), Core::Molecule(other) |
| 39 |
{ |
| 40 |
// Now assign the unique ids |
| 41 |
for (Index i = 0; i < atomCount(); i++) |
| 42 |
m_atomUniqueIds.push_back(i); |
| 43 |
|
| 44 |
for (Index i = 0; i < bondCount(); i++) |
| 45 |
m_bondUniqueIds.push_back(i); |
| 46 |
} |
| 47 |
|
| 48 |
Molecule& Molecule::operator=(const Molecule& other) |
| 49 |
{ |
| 50 |
// Call the base classes assignment operator |
| 51 |
Core::Molecule::operator=(other); |
| 52 |
|
| 53 |
// Copy over the unique ids |
| 54 |
m_atomUniqueIds = other.m_atomUniqueIds; |
| 55 |
m_bondUniqueIds = other.m_bondUniqueIds; |
| 56 |
|
| 57 |
return *this; |
| 58 |
} |
| 59 |
|
| 60 |
Molecule& Molecule::operator=(const Core::Molecule& other) |
| 61 |
{ |
| 62 |
// Call the base classes assignment operator |
| 63 |
Core::Molecule::operator=(other); |
| 64 |
|
| 65 |
// Reset the unique ids. |
| 66 |
m_atomUniqueIds.clear(); |
| 67 |
for (Index i = 0; i < atomCount(); ++i) |
| 68 |
m_atomUniqueIds.push_back(i); |
| 69 |
|
| 70 |
m_bondUniqueIds.clear(); |
| 71 |
for (Index i = 0; i < bondCount(); ++i) |
| 72 |
m_bondUniqueIds.push_back(i); |
| 73 |
|
| 74 |
return *this; |
| 75 |
} |
| 76 |
|
| 77 |
Molecule::~Molecule() {} |
| 78 |
|
| 79 |
Molecule::AtomType Molecule::addAtom(unsigned char number) |
| 80 |
{ |
| 81 |
m_atomUniqueIds.push_back(atomCount()); |
| 82 |
AtomType a = Core::Molecule::addAtom(number); |
| 83 |
return a; |
| 84 |
} |
| 85 |
|
| 86 |
Molecule::AtomType Molecule::addAtom(unsigned char number, Index uniqueId) |
| 87 |
{ |
| 88 |
if (uniqueId >= static_cast<Index>(m_atomUniqueIds.size()) || |
| 89 |
m_atomUniqueIds[uniqueId] != MaxIndex) { |
| 90 |
return AtomType(); |
| 91 |
} |
| 92 |
m_atomUniqueIds[uniqueId] = atomCount(); |
| 93 |
AtomType a = Core::Molecule::addAtom(number); |
| 94 |
return a; |
| 95 |
} |
| 96 |
|
| 97 |
Molecule::AtomType Molecule::addAtom(unsigned char number, Vector3 position3d, |
| 98 |
Index uniqueId) |
| 99 |
{ |
| 100 |
if (uniqueId >= static_cast<Index>(m_atomUniqueIds.size())) { |
| 101 |
// Add atom using our own addAtom (which handles unique IDs) |
| 102 |
// then set the position |
| 103 |
auto atom = Molecule::addAtom(number); |
| 104 |
if (atom.isValid()) |
| 105 |
atom.setPosition3d(position3d); |
| 106 |
return atom; |
| 107 |
} else { |
| 108 |
auto atom = Molecule::addAtom(number, uniqueId); |
| 109 |
if (atom.isValid()) |
| 110 |
atom.setPosition3d(position3d); |
| 111 |
return atom; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
bool Molecule::removeAtom(Index index) |
| 116 |
{ |
| 117 |
if (index >= atomCount()) |
| 118 |
return false; |
| 119 |
Index uniqueId = findAtomUniqueId(index); |
| 120 |
if (uniqueId == MaxIndex) |
| 121 |
return false; |
| 122 |
|
| 123 |
// Unique ID of an atom that was removed: |
| 124 |
m_atomUniqueIds[uniqueId] = MaxIndex; |
| 125 |
auto newSize = static_cast<Index>(atomCount() - 1); |
| 126 |
|
| 127 |
// Before removing the atom we must first remove any bonds to it. |
| 128 |
Core::Molecule::removeAtom(index); |
| 129 |
|
| 130 |
if (index != newSize) { |
| 131 |
// movedAtomUID |
| 132 |
uniqueId = findAtomUniqueId(newSize); |
| 133 |
assert(uniqueId != MaxIndex); |
| 134 |
m_atomUniqueIds[uniqueId] = index; |
| 135 |
} |
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
bool Molecule::removeAtom(const AtomType& atom_) |
| 140 |
{ |
| 141 |
return removeAtom(atom_.index()); |
| 142 |
} |
| 143 |
|
| 144 |
Molecule::AtomType Molecule::atomByUniqueId(Index uniqueId) |
| 145 |
{ |
| 146 |
if (uniqueId >= static_cast<Index>(m_atomUniqueIds.size()) || |
| 147 |
m_atomUniqueIds[uniqueId] == MaxIndex) { |
| 148 |
return AtomType(); |
| 149 |
} else { |
| 150 |
return AtomType(this, m_atomUniqueIds[uniqueId]); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
Index Molecule::atomUniqueId(const AtomType& a) const |
| 155 |
{ |
| 156 |
if (a.molecule() != this) |
| 157 |
return MaxIndex; |
| 158 |
return findAtomUniqueId(a.index()); |
| 159 |
} |
| 160 |
|
| 161 |
Index Molecule::atomUniqueId(Index a) const |
| 162 |
{ |
| 163 |
return findAtomUniqueId(a); |
| 164 |
} |
| 165 |
|
| 166 |
Molecule::BondType Molecule::addBond(const AtomType& a, const AtomType& b, |
| 167 |
unsigned char order) |
| 168 |
{ |
| 169 |
m_bondUniqueIds.push_back(bondCount()); |
| 170 |
|
| 171 |
assert(a.isValid() && a.molecule() == this); |
| 172 |
assert(b.isValid() && b.molecule() == this); |
| 173 |
|
| 174 |
BondType bond_ = Core::Molecule::addBond(a.index(), b.index(), order); |
| 175 |
return bond_; |
| 176 |
} |
| 177 |
|
| 178 |
Molecule::BondType Molecule::addBond(Avogadro::Index atomId1, |
| 179 |
Avogadro::Index atomId2, |
| 180 |
unsigned char order) |
| 181 |
{ |
| 182 |
m_bondUniqueIds.push_back(bondCount()); |
| 183 |
return Core::Molecule::addBond(atomId1, atomId2, order); |
| 184 |
} |
| 185 |
|
| 186 |
void Molecule::addBonds(const Core::Array<std::pair<Index, Index>>& bonds, |
| 187 |
const Core::Array<unsigned char>& orders) |
| 188 |
{ |
| 189 |
assert(orders.size() == bonds.size()); |
| 190 |
for (Index i = 0; i < orders.size(); ++i) { |
| 191 |
addBond(bonds[i].first, bonds[i].second, orders[i]); |
| 192 |
} |
| 193 |
} |
| 194 |
void Molecule::swapBond(Index a, Index b) |
| 195 |
{ |
| 196 |
Index uniqueA = findBondUniqueId(a); |
| 197 |
Index uniqueB = findBondUniqueId(b); |
| 198 |
assert(uniqueA != MaxIndex && uniqueB != MaxIndex); |
| 199 |
swap(m_bondUniqueIds[uniqueA], m_bondUniqueIds[uniqueB]); |
| 200 |
Core::Molecule::swapBond(a, b); |
| 201 |
} |
| 202 |
|
| 203 |
void Molecule::swapAtom(Index a, Index b) |
| 204 |
{ |
| 205 |
if (a == b) { |
| 206 |
return; |
| 207 |
} |
| 208 |
Index uniqueA = findAtomUniqueId(a); |
| 209 |
Index uniqueB = findAtomUniqueId(b); |
| 210 |
assert(uniqueA != MaxIndex && uniqueB != MaxIndex); |
| 211 |
swap(m_atomUniqueIds[uniqueA], m_atomUniqueIds[uniqueB]); |
| 212 |
Core::Molecule::swapAtom(a, b); |
| 213 |
} |
| 214 |
|
| 215 |
Molecule::BondType Molecule::addBond(Index a, Index b, unsigned char order, |
| 216 |
Index uniqueId) |
| 217 |
{ |
| 218 |
if (uniqueId >= static_cast<Index>(m_bondUniqueIds.size()) || |
| 219 |
m_bondUniqueIds[uniqueId] != MaxIndex) { |
| 220 |
return BondType(); |
| 221 |
} |
| 222 |
|
| 223 |
m_bondUniqueIds[uniqueId] = bondCount(); |
| 224 |
return Core::Molecule::addBond(a, b, order); |
| 225 |
} |
| 226 |
|
| 227 |
Molecule::BondType Molecule::addBond(const AtomType& a, const AtomType& b, |
| 228 |
unsigned char order, Index uniqueId) |
| 229 |
{ |
| 230 |
if (uniqueId >= static_cast<Index>(m_bondUniqueIds.size()) || |
| 231 |
m_bondUniqueIds[uniqueId] != MaxIndex) { |
| 232 |
return BondType(); |
| 233 |
} |
| 234 |
|
| 235 |
m_bondUniqueIds[uniqueId] = bondCount(); |
| 236 |
return Core::Molecule::addBond(a, b, order); |
| 237 |
} |
| 238 |
|
| 239 |
bool Molecule::removeBond(Index index) |
| 240 |
{ |
| 241 |
if (index >= bondCount()) |
| 242 |
return false; |
| 243 |
Index uniqueId = findBondUniqueId(index); |
| 244 |
if (uniqueId == MaxIndex) |
| 245 |
return false; |
| 246 |
|
| 247 |
m_bondUniqueIds[uniqueId] = MaxIndex; // Unique ID of a bond that was removed. |
| 248 |
|
| 249 |
auto newSize = static_cast<Index>(bondCount() - 1); |
| 250 |
if (index != newSize) { |
| 251 |
Index movedBondUID = findBondUniqueId(newSize); |
| 252 |
assert(movedBondUID != MaxIndex); |
| 253 |
m_bondUniqueIds[movedBondUID] = index; |
| 254 |
} |
| 255 |
Core::Molecule::removeBond(index); |
| 256 |
return true; |
| 257 |
} |
| 258 |
|
| 259 |
bool Molecule::removeBond(const BondType& bond_) |
| 260 |
{ |
| 261 |
return removeBond(bond_.index()); |
| 262 |
} |
| 263 |
|
| 264 |
bool Molecule::removeBond(const AtomType& a, const AtomType& b) |
| 265 |
{ |
| 266 |
return removeBond(bond(a, b).index()); |
| 267 |
} |
| 268 |
|
| 269 |
bool Molecule::removeBond(Index a, Index b) |
| 270 |
{ |
| 271 |
return removeBond(bond(a, b).index()); |
| 272 |
} |
| 273 |
|
| 274 |
Molecule::BondType Molecule::bondByUniqueId(Index uniqueId) |
| 275 |
{ |
| 276 |
if (uniqueId >= static_cast<Index>(m_bondUniqueIds.size()) || |
| 277 |
m_bondUniqueIds[uniqueId] == MaxIndex) { |
| 278 |
return BondType(); |
| 279 |
} else { |
| 280 |
return BondType(this, static_cast<Index>(m_bondUniqueIds[uniqueId])); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
Index Molecule::bondUniqueId(const BondType& b) const |
| 285 |
{ |
| 286 |
if (b.molecule() != this) |
| 287 |
return MaxIndex; |
| 288 |
return findBondUniqueId(b.index()); |
| 289 |
} |
| 290 |
|
| 291 |
Index Molecule::bondUniqueId(Index b) const |
| 292 |
{ |
| 293 |
return findBondUniqueId(b); |
| 294 |
} |
| 295 |
|
| 296 |
void Molecule::emitChanged(unsigned int change) |
| 297 |
{ |
| 298 |
if (change != NoChange) |
| 299 |
emit changed(change); |
| 300 |
} |
| 301 |
|
| 302 |
void Molecule::emitUpdate() const |
| 303 |
{ |
| 304 |
emit update(); |
| 305 |
} |
| 306 |
|
| 307 |
Index Molecule::findAtomUniqueId(Index index) const |
| 308 |
{ |
| 309 |
for (Index i = 0; i < static_cast<Index>(m_atomUniqueIds.size()); ++i) { |
| 310 |
if (m_atomUniqueIds[i] == index) |
| 311 |
return i; |
| 312 |
} |
| 313 |
return MaxIndex; |
| 314 |
} |
| 315 |
|
| 316 |
Index Molecule::findBondUniqueId(Index index) const |
| 317 |
{ |
| 318 |
for (Index i = 0; i < static_cast<Index>(m_bondUniqueIds.size()); ++i) |
| 319 |
if (m_bondUniqueIds[i] == index) |
| 320 |
return i; |
| 321 |
return MaxIndex; |
| 322 |
} |
| 323 |
|
| 324 |
RWMolecule* Molecule::undoMolecule() |
| 325 |
{ |
| 326 |
return m_undoMolecule; |
| 327 |
} |
| 328 |
|
| 329 |
QString Molecule::formattedFormula() const |
| 330 |
{ |
| 331 |
// Get composition from core (handles isotopes and unit cell fractions) |
| 332 |
std::map<std::string, size_t> componentsCount = formulaComposition(); |
| 333 |
|
| 334 |
QString formula; |
| 335 |
// loop through the components |
| 336 |
// if carbon is present, it goes first |
| 337 |
// if carbon is present, hydrogen is next |
| 338 |
// then alphabetical |
| 339 |
// and any components with a number in front get a superscript |
| 340 |
|
| 341 |
std::map<std::string, size_t>::iterator iter; |
| 342 |
iter = componentsCount.find("C"); |
| 343 |
if (iter != componentsCount.end()) { |
| 344 |
formula += "C"; |
| 345 |
if (iter->second > 1) |
| 346 |
formula += QString("<sub>%1</sub>").arg(iter->second); |
| 347 |
componentsCount.erase(iter); |
| 348 |
|
| 349 |
// hydrogen goes next if carbon is present |
| 350 |
iter = componentsCount.find("H"); |
| 351 |
if (iter != componentsCount.end()) { |
| 352 |
formula += "H"; |
| 353 |
if (iter->second > 1) |
| 354 |
formula += QString("<sub>%1</sub>").arg(iter->second); |
| 355 |
componentsCount.erase(iter); |
| 356 |
} |
| 357 |
// also deuterium and tritium |
| 358 |
iter = componentsCount.find("D"); |
| 359 |
if (iter != componentsCount.end()) { |
| 360 |
formula += "D"; |
| 361 |
if (iter->second > 1) |
| 362 |
formula += QString("<sub>%1</sub>").arg(iter->second); |
| 363 |
componentsCount.erase(iter); |
| 364 |
} |
| 365 |
iter = componentsCount.find("T"); |
| 366 |
if (iter != componentsCount.end()) { |
| 367 |
formula += "T"; |
| 368 |
if (iter->second > 1) |
| 369 |
formula += QString("<sub>%1</sub>").arg(iter->second); |
| 370 |
componentsCount.erase(iter); |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
for (iter = componentsCount.begin(); iter != componentsCount.end(); ++iter) { |
| 375 |
// check if iter->first starts with a digit |
| 376 |
if (iter->first[0] >= '0' && iter->first[0] <= '9') { |
| 377 |
// get the digits for a superscript |
| 378 |
QString digits; |
| 379 |
for (unsigned int i = 0; i < iter->first.length(); ++i) { |
| 380 |
if (iter->first[i] >= '0' && iter->first[i] <= '9') |
| 381 |
digits += iter->first[i]; |
| 382 |
} |
| 383 |
formula += QString("<sup>%1</sup>").arg(digits); |
| 384 |
// take the substring from the digit to the end |
| 385 |
formula += iter->first.substr(digits.length()); |
| 386 |
} else |
| 387 |
formula += iter->first; |
| 388 |
|
| 389 |
if (iter->second > 1) |
| 390 |
formula += QString("<sub>%1</sub>").arg(iter->second); |
| 391 |
} |
| 392 |
|
| 393 |
// add total charge as a superscript |
| 394 |
int charge = totalCharge(); |
| 395 |
if (charge == -1) |
| 396 |
formula += QString("<sup>-</sup>"); |
| 397 |
else if (charge < -1) |
| 398 |
formula += QString("<sup>%1</sup>").arg(charge); |
| 399 |
else if (charge == 1) |
| 400 |
formula += QString("<sup>+</sup>"); |
| 401 |
else if (charge > 1) |
| 402 |
formula += QString("<sup>+%1</sup>").arg(charge); |
| 403 |
|
| 404 |
// add doublet or triplet for spin multiplicity as radical dot |
| 405 |
int spinMultiplicity = totalSpinMultiplicity(); |
| 406 |
if (spinMultiplicity == 2) |
| 407 |
formula += "<sup>•</sup>"; |
| 408 |
else if (spinMultiplicity == 3) |
| 409 |
formula += "<sup>••</sup>"; |
| 410 |
|
| 411 |
return formula; |
| 412 |
} |
| 413 |
|
| 414 |
bool Molecule::isInteractive() const |
| 415 |
{ |
| 416 |
if (m_undoMolecule == nullptr) |
| 417 |
return false; |
| 418 |
|
| 419 |
return m_undoMolecule->isInteractive(); |
| 420 |
} |
| 421 |
|
| 422 |
} // namespace Avogadro::QtGui |