| 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 |
#ifndef AVOGADRO_QTGUI_RWMOLECULE_UNDO_H |
| 6 |
#define AVOGADRO_QTGUI_RWMOLECULE_UNDO_H |
| 7 |
|
| 8 |
#include "rwmolecule.h" |
| 9 |
|
| 10 |
#include <QUndoCommand> |
| 11 |
#include <cassert> |
| 12 |
|
| 13 |
namespace Avogadro { |
| 14 |
namespace QtGui { |
| 15 |
|
| 16 |
using Core::Array; |
| 17 |
using Core::AtomHybridization; |
| 18 |
using Core::UnitCell; |
| 19 |
|
| 20 |
// Base class for all undo commands used by this class. |
| 21 |
// Used to expose molecule internals without needing to add explicit friendships |
| 22 |
// between all undo commands and the container. |
| 23 |
// Subclasses that want to support automatic merging should use the |
| 24 |
// MergeUndoCommand interface below. |
| 25 |
// Subclass implementations are inline in the code below. Specific undo |
| 26 |
// operations are kept near the RWMolecule methods that use them. |
| 27 |
class RWMolecule::UndoCommand : public QUndoCommand |
| 28 |
{ |
| 29 |
public: |
| 30 |
UndoCommand(RWMolecule& m) |
| 31 |
: QUndoCommand(tr("Modify Molecule")), m_mol(m), m_molecule(m.m_molecule) |
| 32 |
{ |
| 33 |
} |
| 34 |
|
| 35 |
protected: |
| 36 |
Array<Vector3>& positions3d() { return m_molecule.atomPositions3d(); } |
| 37 |
Array<Index>& atomUniqueIds() { return m_mol.m_molecule.atomUniqueIds(); } |
| 38 |
Array<Index>& bondUniqueIds() { return m_mol.m_molecule.bondUniqueIds(); } |
| 39 |
|
| 40 |
RWMolecule& m_mol; |
| 41 |
QtGui::Molecule& m_molecule; |
| 42 |
}; |
| 43 |
|
| 44 |
namespace { |
| 45 |
enum MergeIds |
| 46 |
{ |
| 47 |
SetPositions3dMergeId = 0, |
| 48 |
SetPosition3dMergeId, |
| 49 |
SetForceVectorMergeId, |
| 50 |
SetBondOrderMergeId, |
| 51 |
ModifySelectionMergeId, |
| 52 |
ModifyColorsMergeId |
| 53 |
}; |
| 54 |
|
| 55 |
// Base class for undo commands that can be merged together, overriding the |
| 56 |
// "after" state of an old command with that of the new command. |
| 57 |
// Intended for use with RWMolecule's interactive mode. |
| 58 |
// To add a new class, add a new entry to the MergeIds enum above and use that |
| 59 |
// symbolic value as the template parameter. Implement the mergeWith() method |
| 60 |
// to update the "after" state. See SetPositions3dCommand for an example. |
| 61 |
template <int Id> |
| 62 |
class MergeUndoCommand : public RWMolecule::UndoCommand |
| 63 |
{ |
| 64 |
bool m_canMerge; |
| 65 |
|
| 66 |
public: |
| 67 |
MergeUndoCommand(RWMolecule& m) : UndoCommand(m), m_canMerge(false) {} |
| 68 |
void setCanMerge(bool merge) { m_canMerge = merge; } |
| 69 |
bool canMerge() const { return m_canMerge; } |
| 70 |
int id() const override { return m_canMerge ? Id : -1; } |
| 71 |
}; |
| 72 |
} // namespace |
| 73 |
|
| 74 |
namespace { |
| 75 |
class AddAtomCommand : public RWMolecule::UndoCommand |
| 76 |
{ |
| 77 |
unsigned char m_atomicNumber; |
| 78 |
bool m_usingPositions; |
| 79 |
Index m_atomId; |
| 80 |
Index m_atomUid; |
| 81 |
size_t m_layer; |
| 82 |
|
| 83 |
public: |
| 84 |
AddAtomCommand(RWMolecule& m, unsigned char aN, bool usingPositions, |
| 85 |
Index atomId, Index uid) |
| 86 |
: UndoCommand(m), m_atomicNumber(aN), m_usingPositions(usingPositions), |
| 87 |
m_atomId(atomId), m_atomUid(uid) |
| 88 |
{ |
| 89 |
m_layer = m_molecule.layer().activeLayer(); |
| 90 |
} |
| 91 |
|
| 92 |
void redo() override |
| 93 |
{ |
| 94 |
assert(m_molecule.atomCount() == m_atomId); |
| 95 |
if (m_usingPositions) |
| 96 |
m_molecule.addAtom(m_atomicNumber, Vector3::Zero(), m_atomUid); |
| 97 |
else |
| 98 |
m_molecule.addAtom(m_atomicNumber, m_atomUid); |
| 99 |
m_molecule.layer().addAtom(m_layer, m_atomId); |
| 100 |
} |
| 101 |
|
| 102 |
void undo() override |
| 103 |
{ |
| 104 |
assert(m_molecule.atomCount() == m_atomId + 1); |
| 105 |
m_layer = m_molecule.layer().getLayerID(m_atomId); |
| 106 |
m_molecule.removeAtom(m_atomId); |
| 107 |
} |
| 108 |
}; |
| 109 |
} // namespace |
| 110 |
|
| 111 |
namespace { |
| 112 |
class RemoveAtomCommand : public RWMolecule::UndoCommand |
| 113 |
{ |
| 114 |
Index m_atomId; |
| 115 |
Index m_atomUid; |
| 116 |
unsigned char m_atomicNumber; |
| 117 |
Vector3 m_position3d; |
| 118 |
Array<std::pair<Index, Index>> m_bonds; |
| 119 |
Array<unsigned char> m_orders; |
| 120 |
size_t m_layer; |
| 121 |
|
| 122 |
public: |
| 123 |
RemoveAtomCommand(RWMolecule& m, Index atomId, Index uid, unsigned char aN, |
| 124 |
const Vector3& pos) |
| 125 |
: UndoCommand(m), m_atomId(atomId), m_atomUid(uid), m_atomicNumber(aN), |
| 126 |
m_position3d(pos) |
| 127 |
{ |
| 128 |
} |
| 129 |
|
| 130 |
void redo() override |
| 131 |
{ |
| 132 |
assert(m_atomId < m_molecule.atomCount()); |
| 133 |
m_layer = m_molecule.layer().getLayerID(m_atomId); |
| 134 |
m_bonds = m_molecule.getAtomBonds(m_atomId); |
| 135 |
m_orders = m_molecule.getAtomOrders(m_atomId); |
| 136 |
m_molecule.removeAtom(m_atomId); |
| 137 |
} |
| 138 |
|
| 139 |
void undo() override |
| 140 |
{ |
| 141 |
m_molecule.addAtom(m_atomicNumber, m_position3d, m_atomUid); |
| 142 |
// Swap the moved and unremoved atom data if needed |
| 143 |
Index movedId = m_mol.atomCount() - 1; |
| 144 |
m_molecule.layer().addAtom(m_layer, movedId); |
| 145 |
m_molecule.swapAtom(m_atomId, movedId); |
| 146 |
m_molecule.addBonds(m_bonds, m_orders); |
| 147 |
m_bonds.clear(); |
| 148 |
} |
| 149 |
}; |
| 150 |
} // namespace |
| 151 |
|
| 152 |
namespace { |
| 153 |
class SetAtomicNumbersCommand : public RWMolecule::UndoCommand |
| 154 |
{ |
| 155 |
Core::Array<unsigned char> m_oldAtomicNumbers; |
| 156 |
Core::Array<unsigned char> m_newAtomicNumbers; |
| 157 |
|
| 158 |
public: |
| 159 |
SetAtomicNumbersCommand(RWMolecule& m, |
| 160 |
const Core::Array<unsigned char>& oldAtomicNumbers, |
| 161 |
const Core::Array<unsigned char>& newAtomicNumbers) |
| 162 |
: UndoCommand(m), m_oldAtomicNumbers(oldAtomicNumbers), |
| 163 |
m_newAtomicNumbers(newAtomicNumbers) |
| 164 |
{ |
| 165 |
} |
| 166 |
|
| 167 |
void redo() override { m_molecule.setAtomicNumbers(m_newAtomicNumbers); } |
| 168 |
|
| 169 |
void undo() override { m_molecule.setAtomicNumbers(m_oldAtomicNumbers); } |
| 170 |
}; |
| 171 |
} // namespace |
| 172 |
|
| 173 |
namespace { |
| 174 |
class SetAtomicNumberCommand : public RWMolecule::UndoCommand |
| 175 |
{ |
| 176 |
Index m_atomId; |
| 177 |
unsigned char m_oldAtomicNumber; |
| 178 |
unsigned char m_newAtomicNumber; |
| 179 |
|
| 180 |
public: |
| 181 |
SetAtomicNumberCommand(RWMolecule& m, Index atomId, |
| 182 |
unsigned char oldAtomicNumber, |
| 183 |
unsigned char newAtomicNumber) |
| 184 |
: UndoCommand(m), m_atomId(atomId), m_oldAtomicNumber(oldAtomicNumber), |
| 185 |
m_newAtomicNumber(newAtomicNumber) |
| 186 |
{ |
| 187 |
} |
| 188 |
|
| 189 |
void redo() override |
| 190 |
{ |
| 191 |
m_molecule.setAtomicNumber(m_atomId, m_newAtomicNumber); |
| 192 |
} |
| 193 |
|
| 194 |
void undo() override |
| 195 |
{ |
| 196 |
m_molecule.setAtomicNumber(m_atomId, m_oldAtomicNumber); |
| 197 |
} |
| 198 |
}; |
| 199 |
} // namespace |
| 200 |
|
| 201 |
namespace { |
| 202 |
class SetPositions3dCommand : public MergeUndoCommand<SetPositions3dMergeId> |
| 203 |
{ |
| 204 |
Core::Array<Vector3> m_oldPositions3d; |
| 205 |
Core::Array<Vector3> m_newPositions3d; |
| 206 |
|
| 207 |
public: |
| 208 |
SetPositions3dCommand(RWMolecule& m, |
| 209 |
const Core::Array<Vector3>& oldPositions3d, |
| 210 |
const Core::Array<Vector3>& newPositions3d) |
| 211 |
: MergeUndoCommand<SetPositions3dMergeId>(m), |
| 212 |
m_oldPositions3d(oldPositions3d), m_newPositions3d(newPositions3d) |
| 213 |
{ |
| 214 |
} |
| 215 |
|
| 216 |
void redo() override { positions3d() = m_newPositions3d; } |
| 217 |
|
| 218 |
void undo() override { positions3d() = m_oldPositions3d; } |
| 219 |
|
| 220 |
bool mergeWith(const QUndoCommand* other) override |
| 221 |
{ |
| 222 |
const SetPositions3dCommand* o = |
| 223 |
dynamic_cast<const SetPositions3dCommand*>(other); |
| 224 |
if (o) { |
| 225 |
m_newPositions3d = o->m_newPositions3d; |
| 226 |
return true; |
| 227 |
} |
| 228 |
return false; |
| 229 |
} |
| 230 |
}; |
| 231 |
} // namespace |
| 232 |
|
| 233 |
namespace { |
| 234 |
class SetPosition3dCommand : public MergeUndoCommand<SetPosition3dMergeId> |
| 235 |
{ |
| 236 |
Array<Index> m_atomIds; |
| 237 |
Array<Vector3> m_oldPosition3ds; |
| 238 |
Array<Vector3> m_newPosition3ds; |
| 239 |
|
| 240 |
public: |
| 241 |
SetPosition3dCommand(RWMolecule& m, Index atomId, |
| 242 |
const Vector3& oldPosition3d, |
| 243 |
const Vector3& newPosition3d) |
| 244 |
: MergeUndoCommand<SetPosition3dMergeId>(m), m_atomIds(1, atomId), |
| 245 |
m_oldPosition3ds(1, oldPosition3d), m_newPosition3ds(1, newPosition3d) |
| 246 |
{ |
| 247 |
} |
| 248 |
|
| 249 |
void redo() override |
| 250 |
{ |
| 251 |
for (size_t i = 0; i < m_atomIds.size(); ++i) |
| 252 |
positions3d()[m_atomIds[i]] = m_newPosition3ds[i]; |
| 253 |
} |
| 254 |
|
| 255 |
void undo() override |
| 256 |
{ |
| 257 |
for (size_t i = 0; i < m_atomIds.size(); ++i) |
| 258 |
positions3d()[m_atomIds[i]] = m_oldPosition3ds[i]; |
| 259 |
} |
| 260 |
|
| 261 |
bool mergeWith(const QUndoCommand* o) override |
| 262 |
{ |
| 263 |
const SetPosition3dCommand* other = |
| 264 |
dynamic_cast<const SetPosition3dCommand*>(o); |
| 265 |
if (!other) |
| 266 |
return false; |
| 267 |
|
| 268 |
size_t numAtoms = other->m_atomIds.size(); |
| 269 |
if (numAtoms != other->m_oldPosition3ds.size() || |
| 270 |
numAtoms != other->m_newPosition3ds.size()) { |
| 271 |
return false; |
| 272 |
} |
| 273 |
|
| 274 |
for (size_t i = 0; i < numAtoms; ++i) { |
| 275 |
const Index& atomId = other->m_atomIds[i]; |
| 276 |
const Vector3& oldPos = other->m_oldPosition3ds[i]; |
| 277 |
const Vector3& newPos = other->m_newPosition3ds[i]; |
| 278 |
|
| 279 |
Array<Index>::const_iterator idsBegin = m_atomIds.begin(); |
| 280 |
Array<Index>::const_iterator idsEnd = m_atomIds.end(); |
| 281 |
Array<Index>::const_iterator match = std::find(idsBegin, idsEnd, atomId); |
| 282 |
|
| 283 |
if (match == idsEnd) { |
| 284 |
// Append a new atom: |
| 285 |
m_atomIds.push_back(atomId); |
| 286 |
m_oldPosition3ds.push_back(oldPos); |
| 287 |
m_newPosition3ds.push_back(newPos); |
| 288 |
} else { |
| 289 |
// Overwrite the existing movement: |
| 290 |
size_t offset = std::distance(idsBegin, match); |
| 291 |
assert(m_atomIds[offset] == atomId); |
| 292 |
m_newPosition3ds[offset] = newPos; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
return true; |
| 297 |
} |
| 298 |
}; |
| 299 |
} // namespace |
| 300 |
|
| 301 |
namespace { |
| 302 |
class SetAtomHybridizationCommand : public RWMolecule::UndoCommand |
| 303 |
{ |
| 304 |
Index m_atomId; |
| 305 |
AtomHybridization m_oldHybridization; |
| 306 |
AtomHybridization m_newHybridization; |
| 307 |
|
| 308 |
public: |
| 309 |
SetAtomHybridizationCommand(RWMolecule& m, Index atomId, |
| 310 |
AtomHybridization oldHybridization, |
| 311 |
AtomHybridization newHybridization) |
| 312 |
: UndoCommand(m), m_atomId(atomId), m_oldHybridization(oldHybridization), |
| 313 |
m_newHybridization(newHybridization) |
| 314 |
{ |
| 315 |
} |
| 316 |
|
| 317 |
void redo() override |
| 318 |
{ |
| 319 |
m_molecule.setHybridization(m_atomId, m_newHybridization); |
| 320 |
} |
| 321 |
|
| 322 |
void undo() override |
| 323 |
{ |
| 324 |
m_molecule.setHybridization(m_atomId, m_oldHybridization); |
| 325 |
} |
| 326 |
}; |
| 327 |
} // namespace |
| 328 |
|
| 329 |
namespace { |
| 330 |
class SetAtomFormalChargeCommand : public RWMolecule::UndoCommand |
| 331 |
{ |
| 332 |
Index m_atomId; |
| 333 |
signed char m_oldCharge; |
| 334 |
signed char m_newCharge; |
| 335 |
|
| 336 |
public: |
| 337 |
SetAtomFormalChargeCommand(RWMolecule& m, Index atomId, signed char oldCharge, |
| 338 |
signed char newCharge) |
| 339 |
: UndoCommand(m), m_atomId(atomId), m_oldCharge(oldCharge), |
| 340 |
m_newCharge(newCharge) |
| 341 |
{ |
| 342 |
} |
| 343 |
|
| 344 |
void redo() override { m_molecule.setFormalCharge(m_atomId, m_newCharge); } |
| 345 |
|
| 346 |
void undo() override { m_molecule.setFormalCharge(m_atomId, m_oldCharge); } |
| 347 |
}; |
| 348 |
} // namespace |
| 349 |
|
| 350 |
namespace { |
| 351 |
class SetAtomColorCommand : public RWMolecule::UndoCommand |
| 352 |
{ |
| 353 |
Index m_atomId; |
| 354 |
Vector3ub m_oldColor; |
| 355 |
Vector3ub m_newColor; |
| 356 |
|
| 357 |
public: |
| 358 |
SetAtomColorCommand(RWMolecule& m, Index atomId, Vector3ub oldColor, |
| 359 |
Vector3ub newColor) |
| 360 |
: UndoCommand(m), m_atomId(atomId), m_oldColor(oldColor), |
| 361 |
m_newColor(newColor) |
| 362 |
{ |
| 363 |
} |
| 364 |
|
| 365 |
void redo() override { m_molecule.setColor(m_atomId, m_newColor); } |
| 366 |
|
| 367 |
void undo() override { m_molecule.setColor(m_atomId, m_oldColor); } |
| 368 |
}; |
| 369 |
|
| 370 |
class SetResidueColorCommand : public RWMolecule::UndoCommand |
| 371 |
{ |
| 372 |
Index m_residueId; |
| 373 |
Vector3ub m_oldColor; |
| 374 |
Vector3ub m_newColor; |
| 375 |
|
| 376 |
public: |
| 377 |
SetResidueColorCommand(RWMolecule& m, Index residueId, Vector3ub oldColor, |
| 378 |
Vector3ub newColor) |
| 379 |
: UndoCommand(m), m_residueId(residueId), m_oldColor(oldColor), |
| 380 |
m_newColor(newColor) |
| 381 |
{ |
| 382 |
} |
| 383 |
|
| 384 |
void redo() override { m_molecule.residue(m_residueId).setColor(m_newColor); } |
| 385 |
|
| 386 |
void undo() override { m_molecule.residue(m_residueId).setColor(m_oldColor); } |
| 387 |
}; |
| 388 |
|
| 389 |
class SetLayerCommand : public RWMolecule::UndoCommand |
| 390 |
{ |
| 391 |
Index m_atomId; |
| 392 |
size_t m_oldLayer; |
| 393 |
size_t m_newLayer; |
| 394 |
|
| 395 |
public: |
| 396 |
SetLayerCommand(RWMolecule& m, Index atomId, size_t oldLayer, size_t newLayer) |
| 397 |
: UndoCommand(m), m_atomId(atomId), m_oldLayer(oldLayer), |
| 398 |
m_newLayer(newLayer) |
| 399 |
{ |
| 400 |
} |
| 401 |
|
| 402 |
void redo() override { m_molecule.setLayer(m_atomId, m_newLayer); } |
| 403 |
|
| 404 |
void undo() override { m_molecule.setLayer(m_atomId, m_oldLayer); } |
| 405 |
}; |
| 406 |
|
| 407 |
class AddBondCommand : public RWMolecule::UndoCommand |
| 408 |
{ |
| 409 |
unsigned char m_bondOrder; |
| 410 |
std::pair<Index, Index> m_bondPair; |
| 411 |
Index m_bondId; |
| 412 |
Index m_bondUid; |
| 413 |
|
| 414 |
public: |
| 415 |
AddBondCommand(RWMolecule& m, unsigned char order, |
| 416 |
const std::pair<Index, Index>& bondPair, Index bondId, |
| 417 |
Index uid) |
| 418 |
: UndoCommand(m), m_bondOrder(order), m_bondPair(bondPair), |
| 419 |
m_bondId(bondId), m_bondUid(uid) |
| 420 |
{ |
| 421 |
} |
| 422 |
|
| 423 |
void redo() override |
| 424 |
{ |
| 425 |
assert(m_molecule.bondCount() == m_bondId); |
| 426 |
m_molecule.addBond(m_bondPair.first, m_bondPair.second, m_bondOrder); |
| 427 |
} |
| 428 |
|
| 429 |
void undo() override |
| 430 |
{ |
| 431 |
// we know this is the top so just a simple remove |
| 432 |
m_molecule.removeBond(m_bondId); |
| 433 |
} |
| 434 |
}; |
| 435 |
} // namespace |
| 436 |
|
| 437 |
namespace { |
| 438 |
class RemoveBondCommand : public RWMolecule::UndoCommand |
| 439 |
{ |
| 440 |
Index m_bondId; |
| 441 |
Index m_bondUid; |
| 442 |
std::pair<Index, Index> m_bondPair; |
| 443 |
unsigned char m_bondOrder; |
| 444 |
|
| 445 |
public: |
| 446 |
RemoveBondCommand(RWMolecule& m, Index bondId, Index bondUid, |
| 447 |
const std::pair<Index, Index>& bondPair, |
| 448 |
unsigned char bondOrder) |
| 449 |
: UndoCommand(m), m_bondId(bondId), m_bondUid(bondUid), |
| 450 |
m_bondPair(bondPair), m_bondOrder(bondOrder) |
| 451 |
{ |
| 452 |
} |
| 453 |
|
| 454 |
void redo() override { m_molecule.removeBond(m_bondId); } |
| 455 |
|
| 456 |
void undo() override |
| 457 |
{ |
| 458 |
m_molecule.addBond(m_bondPair.first, m_bondPair.second, m_bondOrder, |
| 459 |
m_bondUid); |
| 460 |
Index movedId = m_molecule.bondCount() - 1; |
| 461 |
m_molecule.swapBond(m_bondId, movedId); |
| 462 |
} |
| 463 |
}; |
| 464 |
} // namespace |
| 465 |
|
| 466 |
namespace { |
| 467 |
class SetBondOrdersCommand : public RWMolecule::UndoCommand |
| 468 |
{ |
| 469 |
Array<unsigned char> m_oldBondOrders; |
| 470 |
Array<unsigned char> m_newBondOrders; |
| 471 |
|
| 472 |
public: |
| 473 |
SetBondOrdersCommand(RWMolecule& m, const Array<unsigned char>& oldBondOrders, |
| 474 |
const Array<unsigned char>& newBondOrders) |
| 475 |
: UndoCommand(m), m_oldBondOrders(oldBondOrders), |
| 476 |
m_newBondOrders(newBondOrders) |
| 477 |
{ |
| 478 |
} |
| 479 |
|
| 480 |
void redo() override { m_molecule.setBondOrders(m_newBondOrders); } |
| 481 |
|
| 482 |
void undo() override { m_molecule.setBondOrders(m_oldBondOrders); } |
| 483 |
}; |
| 484 |
} // namespace |
| 485 |
|
| 486 |
namespace { |
| 487 |
class SetBondOrderCommand : public MergeUndoCommand<SetBondOrderMergeId> |
| 488 |
{ |
| 489 |
Index m_bondId; |
| 490 |
unsigned char m_oldBondOrder; |
| 491 |
unsigned char m_newBondOrder; |
| 492 |
|
| 493 |
public: |
| 494 |
SetBondOrderCommand(RWMolecule& m, Index bondId, unsigned char oldBondOrder, |
| 495 |
unsigned char newBondOrder) |
| 496 |
: MergeUndoCommand<SetBondOrderMergeId>(m), m_bondId(bondId), |
| 497 |
m_oldBondOrder(oldBondOrder), m_newBondOrder(newBondOrder) |
| 498 |
{ |
| 499 |
} |
| 500 |
|
| 501 |
void redo() override { m_molecule.setBondOrder(m_bondId, m_newBondOrder); } |
| 502 |
|
| 503 |
void undo() override { m_molecule.setBondOrder(m_bondId, m_oldBondOrder); } |
| 504 |
|
| 505 |
bool mergeWith(const QUndoCommand* other) override |
| 506 |
{ |
| 507 |
const SetBondOrderCommand* o = |
| 508 |
dynamic_cast<const SetBondOrderCommand*>(other); |
| 509 |
// Only merge when the bondIds match. |
| 510 |
if (o && o->m_bondId == this->m_bondId) { |
| 511 |
this->m_newBondOrder = o->m_newBondOrder; |
| 512 |
return true; |
| 513 |
} |
| 514 |
return false; |
| 515 |
} |
| 516 |
}; |
| 517 |
} // namespace |
| 518 |
|
| 519 |
namespace { |
| 520 |
class SetBondPairsCommand : public RWMolecule::UndoCommand |
| 521 |
{ |
| 522 |
Array<std::pair<Index, Index>> m_oldBondPairs; |
| 523 |
Array<std::pair<Index, Index>> m_newBondPairs; |
| 524 |
|
| 525 |
public: |
| 526 |
SetBondPairsCommand(RWMolecule& m, |
| 527 |
const Array<std::pair<Index, Index>>& oldBondPairs, |
| 528 |
const Array<std::pair<Index, Index>>& newBondPairs) |
| 529 |
: UndoCommand(m), m_oldBondPairs(oldBondPairs), m_newBondPairs(newBondPairs) |
| 530 |
{ |
| 531 |
} |
| 532 |
|
| 533 |
void redo() override { m_molecule.setBondPairs(m_newBondPairs); } |
| 534 |
|
| 535 |
void undo() override { m_molecule.setBondPairs(m_oldBondPairs); } |
| 536 |
}; |
| 537 |
} // namespace |
| 538 |
|
| 539 |
namespace { |
| 540 |
class SetBondPairCommand : public RWMolecule::UndoCommand |
| 541 |
{ |
| 542 |
Index m_bondId; |
| 543 |
std::pair<Index, Index> m_oldBondPair; |
| 544 |
std::pair<Index, Index> m_newBondPair; |
| 545 |
|
| 546 |
public: |
| 547 |
SetBondPairCommand(RWMolecule& m, Index bondId, |
| 548 |
const std::pair<Index, Index>& oldBondPair, |
| 549 |
const std::pair<Index, Index>& newBondPair) |
| 550 |
: UndoCommand(m), m_bondId(bondId), m_oldBondPair(oldBondPair), |
| 551 |
m_newBondPair(newBondPair) |
| 552 |
{ |
| 553 |
} |
| 554 |
|
| 555 |
void redo() override { m_molecule.setBondPair(m_bondId, m_newBondPair); } |
| 556 |
|
| 557 |
void undo() override { m_molecule.setBondPair(m_bondId, m_oldBondPair); } |
| 558 |
}; |
| 559 |
} // namespace |
| 560 |
|
| 561 |
namespace { |
| 562 |
class AddUnitCellCommand : public RWMolecule::UndoCommand |
| 563 |
{ |
| 564 |
UnitCell m_newUnitCell; |
| 565 |
|
| 566 |
public: |
| 567 |
AddUnitCellCommand(RWMolecule& m, const UnitCell& newUnitCell) |
| 568 |
: UndoCommand(m), m_newUnitCell(newUnitCell) |
| 569 |
{ |
| 570 |
} |
| 571 |
|
| 572 |
void redo() override |
| 573 |
{ |
| 574 |
m_mol.molecule().setUnitCell(new UnitCell(m_newUnitCell)); |
| 575 |
} |
| 576 |
|
| 577 |
void undo() override { m_mol.molecule().setUnitCell(nullptr); } |
| 578 |
}; |
| 579 |
} // namespace |
| 580 |
|
| 581 |
namespace { |
| 582 |
class RemoveUnitCellCommand : public RWMolecule::UndoCommand |
| 583 |
{ |
| 584 |
UnitCell m_oldUnitCell; |
| 585 |
|
| 586 |
public: |
| 587 |
RemoveUnitCellCommand(RWMolecule& m, const UnitCell& oldUnitCell) |
| 588 |
: UndoCommand(m), m_oldUnitCell(oldUnitCell) |
| 589 |
{ |
| 590 |
} |
| 591 |
|
| 592 |
void redo() override { m_mol.molecule().setUnitCell(nullptr); } |
| 593 |
|
| 594 |
void undo() override |
| 595 |
{ |
| 596 |
m_mol.molecule().setUnitCell(new UnitCell(m_oldUnitCell)); |
| 597 |
} |
| 598 |
}; |
| 599 |
} // namespace |
| 600 |
|
| 601 |
namespace { |
| 602 |
class ModifyMoleculeCommand : public RWMolecule::UndoCommand |
| 603 |
{ |
| 604 |
Molecule m_oldMolecule; |
| 605 |
Molecule m_newMolecule; |
| 606 |
|
| 607 |
public: |
| 608 |
ModifyMoleculeCommand(RWMolecule& m, const Molecule& oldMolecule, |
| 609 |
const Molecule& newMolecule) |
| 610 |
: UndoCommand(m), m_oldMolecule(oldMolecule), m_newMolecule(newMolecule) |
| 611 |
{ |
| 612 |
} |
| 613 |
|
| 614 |
void redo() override { m_mol.molecule() = m_newMolecule; } |
| 615 |
|
| 616 |
void undo() override { m_mol.molecule() = m_oldMolecule; } |
| 617 |
}; |
| 618 |
} // namespace |
| 619 |
|
| 620 |
namespace { |
| 621 |
class SetForceVectorCommand : public MergeUndoCommand<SetForceVectorMergeId> |
| 622 |
{ |
| 623 |
Array<Index> m_atomIds; |
| 624 |
Array<Vector3> m_oldForceVectors; |
| 625 |
Array<Vector3> m_newForceVectors; |
| 626 |
|
| 627 |
public: |
| 628 |
SetForceVectorCommand(RWMolecule& m, Index atomId, |
| 629 |
const Vector3& oldForceVector, |
| 630 |
const Vector3& newForceVector) |
| 631 |
: MergeUndoCommand<SetForceVectorMergeId>(m), m_atomIds(1, atomId), |
| 632 |
m_oldForceVectors(1, oldForceVector), m_newForceVectors(1, newForceVector) |
| 633 |
{ |
| 634 |
} |
| 635 |
|
| 636 |
void redo() override |
| 637 |
{ |
| 638 |
for (size_t i = 0; i < m_atomIds.size(); ++i) |
| 639 |
m_molecule.setForceVector(m_atomIds[i], m_newForceVectors[i]); |
| 640 |
} |
| 641 |
|
| 642 |
void undo() override |
| 643 |
{ |
| 644 |
for (size_t i = 0; i < m_atomIds.size(); ++i) |
| 645 |
m_molecule.setForceVector(m_atomIds[i], m_oldForceVectors[i]); |
| 646 |
} |
| 647 |
|
| 648 |
bool mergeWith(const QUndoCommand* o) override |
| 649 |
{ |
| 650 |
const SetForceVectorCommand* other = |
| 651 |
dynamic_cast<const SetForceVectorCommand*>(o); |
| 652 |
if (!other) |
| 653 |
return false; |
| 654 |
|
| 655 |
size_t numAtoms = other->m_atomIds.size(); |
| 656 |
if (numAtoms != other->m_oldForceVectors.size() || |
| 657 |
numAtoms != other->m_newForceVectors.size()) { |
| 658 |
return false; |
| 659 |
} |
| 660 |
|
| 661 |
for (size_t i = 0; i < numAtoms; ++i) { |
| 662 |
const Index& atomId = other->m_atomIds[i]; |
| 663 |
const Vector3& oldPos = other->m_oldForceVectors[i]; |
| 664 |
const Vector3& newPos = other->m_newForceVectors[i]; |
| 665 |
|
| 666 |
Array<Index>::const_iterator idsBegin = m_atomIds.begin(); |
| 667 |
Array<Index>::const_iterator idsEnd = m_atomIds.end(); |
| 668 |
Array<Index>::const_iterator match = std::find(idsBegin, idsEnd, atomId); |
| 669 |
|
| 670 |
if (match == idsEnd) { |
| 671 |
// Append a new atom: |
| 672 |
m_atomIds.push_back(atomId); |
| 673 |
m_oldForceVectors.push_back(oldPos); |
| 674 |
m_newForceVectors.push_back(newPos); |
| 675 |
} else { |
| 676 |
// Overwrite the existing movement: |
| 677 |
size_t offset = std::distance(idsBegin, match); |
| 678 |
assert(m_atomIds[offset] == atomId); |
| 679 |
m_newForceVectors[offset] = newPos; |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
return true; |
| 684 |
} |
| 685 |
}; |
| 686 |
} // namespace |
| 687 |
|
| 688 |
namespace { |
| 689 |
class ModifyAtomLabelCommand : public RWMolecule::UndoCommand |
| 690 |
{ |
| 691 |
Index m_atomId; |
| 692 |
std::string m_newLabel; |
| 693 |
std::string m_oldLabel; |
| 694 |
|
| 695 |
public: |
| 696 |
ModifyAtomLabelCommand(RWMolecule& m, Index atomId, const std::string& label) |
| 697 |
: UndoCommand(m), m_atomId(atomId), m_newLabel(label) |
| 698 |
{ |
| 699 |
m_oldLabel = m_mol.molecule().atomLabel(m_atomId); |
| 700 |
} |
| 701 |
|
| 702 |
void redo() override { m_mol.molecule().setAtomLabel(m_atomId, m_newLabel); } |
| 703 |
|
| 704 |
void undo() override { m_mol.molecule().setAtomLabel(m_atomId, m_oldLabel); } |
| 705 |
}; |
| 706 |
} // namespace |
| 707 |
|
| 708 |
namespace { |
| 709 |
class ModifyBondLabelCommand : public RWMolecule::UndoCommand |
| 710 |
{ |
| 711 |
Index m_bondId; |
| 712 |
std::string m_newLabel; |
| 713 |
std::string m_oldLabel; |
| 714 |
|
| 715 |
public: |
| 716 |
ModifyBondLabelCommand(RWMolecule& m, Index bondId, const std::string& label) |
| 717 |
: UndoCommand(m), m_bondId(bondId), m_newLabel(label) |
| 718 |
{ |
| 719 |
m_oldLabel = m_mol.molecule().bondLabel(m_bondId); |
| 720 |
} |
| 721 |
|
| 722 |
void redo() override { m_mol.molecule().setBondLabel(m_bondId, m_newLabel); } |
| 723 |
|
| 724 |
void undo() override { m_mol.molecule().setBondLabel(m_bondId, m_oldLabel); } |
| 725 |
}; |
| 726 |
} // namespace |
| 727 |
|
| 728 |
namespace { |
| 729 |
class ModifySelectionCommand : public MergeUndoCommand<ModifySelectionMergeId> |
| 730 |
{ |
| 731 |
std::vector<bool> m_newSelectedAtoms; |
| 732 |
std::vector<bool> m_oldSelectedAtoms; |
| 733 |
|
| 734 |
public: |
| 735 |
ModifySelectionCommand(RWMolecule& m, Index atomId, bool selected) |
| 736 |
: MergeUndoCommand<ModifySelectionMergeId>(m) |
| 737 |
{ |
| 738 |
Index atomCount = m_mol.molecule().atomCount(); |
| 739 |
m_oldSelectedAtoms.resize(atomCount); |
| 740 |
m_newSelectedAtoms.resize(atomCount); |
| 741 |
|
| 742 |
for (Index i = 0; i < atomCount; ++i) { |
| 743 |
m_oldSelectedAtoms[i] = m_molecule.atomSelected(i); |
| 744 |
m_newSelectedAtoms[i] = m_molecule.atomSelected(i); |
| 745 |
} |
| 746 |
|
| 747 |
m_newSelectedAtoms[atomId] = selected; |
| 748 |
} |
| 749 |
|
| 750 |
void redo() override |
| 751 |
{ |
| 752 |
Index atomCount = m_mol.molecule().atomCount(); |
| 753 |
for (Index i = 0; i < atomCount; ++i) |
| 754 |
m_mol.molecule().setAtomSelected(i, m_newSelectedAtoms[i]); |
| 755 |
} |
| 756 |
|
| 757 |
void undo() override |
| 758 |
{ |
| 759 |
Index atomCount = m_mol.molecule().atomCount(); |
| 760 |
for (Index i = 0; i < atomCount; ++i) |
| 761 |
m_mol.molecule().setAtomSelected(i, m_oldSelectedAtoms[i]); |
| 762 |
} |
| 763 |
|
| 764 |
bool mergeWith(const QUndoCommand* other) override |
| 765 |
{ |
| 766 |
const ModifySelectionCommand* o = |
| 767 |
dynamic_cast<const ModifySelectionCommand*>(other); |
| 768 |
if (!o) |
| 769 |
return false; |
| 770 |
|
| 771 |
// check if the atom count matches |
| 772 |
Index numAtoms = m_oldSelectedAtoms.size(); |
| 773 |
|
| 774 |
if (numAtoms != o->m_oldSelectedAtoms.size() || |
| 775 |
numAtoms != o->m_newSelectedAtoms.size()) |
| 776 |
return false; |
| 777 |
|
| 778 |
// merge these, by grabbing the new selected atoms |
| 779 |
m_newSelectedAtoms = o->m_newSelectedAtoms; |
| 780 |
return true; |
| 781 |
} |
| 782 |
}; |
| 783 |
} // namespace |
| 784 |
|
| 785 |
/* |
| 786 |
namespace { |
| 787 |
class ModifyColorCommand : public MergeUndoCommand<ModifyColorMergeId> |
| 788 |
{ |
| 789 |
Array<Vector3ub> m_newSelectedAtoms; |
| 790 |
Array<Vector3ub> m_oldSelectedAtoms; |
| 791 |
|
| 792 |
public: |
| 793 |
ModifySelectionCommand(RWMolecule& m, Index atomId, bool selected) |
| 794 |
: MergeUndoCommand<ModifySelectionMergeId>(m) |
| 795 |
{ |
| 796 |
Index atomCount = m_mol.molecule().atomCount(); |
| 797 |
m_oldSelectedAtoms.reserve(atomCount); |
| 798 |
m_newSelectedAtoms.reserve(atomCount); |
| 799 |
|
| 800 |
for (Index i = 0; i < atomCount; ++i) { |
| 801 |
m_oldSelectedAtoms[i] = m_molecule.atomSelected(i); |
| 802 |
m_newSelectedAtoms[i] = m_molecule.atomSelected(i); |
| 803 |
} |
| 804 |
|
| 805 |
m_newSelectedAtoms[atomId] = selected; |
| 806 |
} |
| 807 |
|
| 808 |
void redo() override |
| 809 |
{ |
| 810 |
Index atomCount = m_mol.molecule().atomCount(); |
| 811 |
for (Index i = 0; i < atomCount; ++i) |
| 812 |
m_mol.molecule().setAtomSelected(i, m_newSelectedAtoms[i]); |
| 813 |
} |
| 814 |
|
| 815 |
void undo() override |
| 816 |
{ |
| 817 |
Index atomCount = m_mol.molecule().atomCount(); |
| 818 |
for (Index i = 0; i < atomCount; ++i) |
| 819 |
m_mol.molecule().setAtomSelected(i, m_oldSelectedAtoms[i]); |
| 820 |
} |
| 821 |
|
| 822 |
bool mergeWith(const QUndoCommand* other) override |
| 823 |
{ |
| 824 |
const ModifySelectionCommand* o = |
| 825 |
dynamic_cast<const ModifySelectionCommand*>(other); |
| 826 |
if (o) { |
| 827 |
// check if the atom count matches |
| 828 |
Index numAtoms = m_oldSelectedAtoms.size(); |
| 829 |
if (numAtoms != o->m_oldSelectedAtoms.size() || |
| 830 |
numAtoms != o->m_newSelectedAtoms.size()) { |
| 831 |
return false; |
| 832 |
} |
| 833 |
|
| 834 |
// merge these, by grabbing the new selected atoms |
| 835 |
for (Index i = 0; i < numAtoms; ++i) |
| 836 |
m_newSelectedAtoms[i] = o->m_newSelectedAtoms[i]; |
| 837 |
|
| 838 |
return true; |
| 839 |
} |
| 840 |
return false; |
| 841 |
} |
| 842 |
}; |
| 843 |
} // namespace |
| 844 |
*/ |
| 845 |
|
| 846 |
} // namespace QtGui |
| 847 |
} // namespace Avogadro |
| 848 |
#endif |