| 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 "crystal.h" |
| 7 |
|
| 8 |
#include "importcrystaldialog.h" |
| 9 |
#include "supercelldialog.h" |
| 10 |
#include "unitcelldialog.h" |
| 11 |
#include "volumescalingdialog.h" |
| 12 |
|
| 13 |
#include <avogadro/core/crystaltools.h> |
| 14 |
#include <avogadro/core/unitcell.h> |
| 15 |
|
| 16 |
#include <avogadro/qtgui/molecule.h> |
| 17 |
#include <avogadro/qtgui/rwmolecule.h> |
| 18 |
|
| 19 |
#include <QAction> |
| 20 |
#include <QMessageBox> |
| 21 |
#include <QStringList> |
| 22 |
|
| 23 |
using Avogadro::Core::CrystalTools; |
| 24 |
using Avogadro::QtGui::Molecule; |
| 25 |
|
| 26 |
namespace Avogadro::QtPlugins { |
| 27 |
|
| 28 |
Crystal::Crystal(QObject* parent_) |
| 29 |
: Avogadro::QtGui::ExtensionPlugin(parent_), m_molecule(nullptr), |
| 30 |
m_unitCellDialog(nullptr), |
| 31 |
m_importCrystalClipboardAction(new QAction(this)), |
| 32 |
m_editUnitCellAction(new QAction(this)), |
| 33 |
m_buildSupercellAction(new QAction(this)), |
| 34 |
m_niggliReduceAction(new QAction(this)), |
| 35 |
m_scaleVolumeAction(new QAction(this)), |
| 36 |
m_standardOrientationAction(new QAction(this)), |
| 37 |
m_toggleUnitCellAction(new QAction(this)), |
| 38 |
m_wrapAtomsToCellAction(new QAction(this)) |
| 39 |
{ |
| 40 |
m_importCrystalClipboardAction->setText(tr("Import Crystal from Clipboard…")); |
| 41 |
connect(m_importCrystalClipboardAction, SIGNAL(triggered()), |
| 42 |
SLOT(importCrystalClipboard())); |
| 43 |
m_actions.push_back(m_importCrystalClipboardAction); |
| 44 |
m_importCrystalClipboardAction->setProperty("menu priority", 220); |
| 45 |
|
| 46 |
// this will be changed when the molecule is set: |
| 47 |
m_toggleUnitCellAction->setText(tr("Toggle Unit Cell")); |
| 48 |
connect(m_toggleUnitCellAction, SIGNAL(triggered()), SLOT(toggleUnitCell())); |
| 49 |
m_actions.push_back(m_toggleUnitCellAction); |
| 50 |
m_toggleUnitCellAction->setProperty("menu priority", 210); |
| 51 |
|
| 52 |
m_editUnitCellAction->setText(tr("Edit Unit Cell…")); |
| 53 |
connect(m_editUnitCellAction, SIGNAL(triggered()), SLOT(editUnitCell())); |
| 54 |
m_actions.push_back(m_editUnitCellAction); |
| 55 |
m_editUnitCellAction->setProperty("menu priority", 190); |
| 56 |
|
| 57 |
m_wrapAtomsToCellAction->setText(tr("&Wrap Atoms to Unit Cell")); |
| 58 |
connect(m_wrapAtomsToCellAction, SIGNAL(triggered()), |
| 59 |
SLOT(wrapAtomsToCell())); |
| 60 |
m_actions.push_back(m_wrapAtomsToCellAction); |
| 61 |
m_wrapAtomsToCellAction->setProperty("menu priority", 180); |
| 62 |
|
| 63 |
m_standardOrientationAction->setText(tr("Rotate to Standard &Orientation")); |
| 64 |
connect(m_standardOrientationAction, SIGNAL(triggered()), |
| 65 |
SLOT(standardOrientation())); |
| 66 |
m_actions.push_back(m_standardOrientationAction); |
| 67 |
m_standardOrientationAction->setProperty("menu priority", 170); |
| 68 |
|
| 69 |
m_scaleVolumeAction->setText(tr("Scale Cell &Volume…")); |
| 70 |
connect(m_scaleVolumeAction, SIGNAL(triggered()), SLOT(scaleVolume())); |
| 71 |
m_actions.push_back(m_scaleVolumeAction); |
| 72 |
m_scaleVolumeAction->setProperty("menu priority", 160); |
| 73 |
|
| 74 |
m_buildSupercellAction->setText(tr("Build &Supercell…")); |
| 75 |
connect(m_buildSupercellAction, SIGNAL(triggered()), SLOT(buildSupercell())); |
| 76 |
m_actions.push_back(m_buildSupercellAction); |
| 77 |
m_buildSupercellAction->setProperty("menu priority", 150); |
| 78 |
|
| 79 |
m_niggliReduceAction->setText(tr("Reduce Cell (&Niggli)")); |
| 80 |
connect(m_niggliReduceAction, SIGNAL(triggered()), SLOT(niggliReduce())); |
| 81 |
m_actions.push_back(m_niggliReduceAction); |
| 82 |
m_niggliReduceAction->setProperty("menu priority", 140); |
| 83 |
|
| 84 |
updateActions(); |
| 85 |
} |
| 86 |
|
| 87 |
Crystal::~Crystal() |
| 88 |
{ |
| 89 |
if (m_unitCellDialog) |
| 90 |
m_unitCellDialog->deleteLater(); |
| 91 |
|
| 92 |
qDeleteAll(m_actions); |
| 93 |
m_actions.clear(); |
| 94 |
} |
| 95 |
|
| 96 |
QList<QAction*> Crystal::actions() const |
| 97 |
{ |
| 98 |
return m_actions; |
| 99 |
} |
| 100 |
|
| 101 |
QStringList Crystal::menuPath(QAction*) const |
| 102 |
{ |
| 103 |
return QStringList() << tr("&Crystal"); |
| 104 |
} |
| 105 |
|
| 106 |
void Crystal::setMolecule(QtGui::Molecule* mol) |
| 107 |
{ |
| 108 |
if (m_molecule == mol) |
| 109 |
return; |
| 110 |
|
| 111 |
if (m_molecule) |
| 112 |
m_molecule->disconnect(this); |
| 113 |
|
| 114 |
m_molecule = mol; |
| 115 |
if (m_unitCellDialog) |
| 116 |
m_unitCellDialog->setMolecule(m_molecule); |
| 117 |
|
| 118 |
if (m_molecule) |
| 119 |
connect(m_molecule, SIGNAL(changed(uint)), SLOT(moleculeChanged(uint))); |
| 120 |
|
| 121 |
updateActions(); |
| 122 |
} |
| 123 |
|
| 124 |
void Crystal::moleculeChanged(unsigned int c) |
| 125 |
{ |
| 126 |
Q_ASSERT(m_molecule == qobject_cast<Molecule*>(sender())); |
| 127 |
|
| 128 |
auto changes = static_cast<Molecule::MoleculeChanges>(c); |
| 129 |
|
| 130 |
if (changes & Molecule::UnitCell) { |
| 131 |
if (changes & Molecule::Added || changes & Molecule::Removed) |
| 132 |
updateActions(); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
void Crystal::updateActions() |
| 137 |
{ |
| 138 |
// Disable everything for nullptr molecules. |
| 139 |
if (!m_molecule) { |
| 140 |
foreach (QAction* action, m_actions) |
| 141 |
action->setEnabled(false); |
| 142 |
return; |
| 143 |
} |
| 144 |
|
| 145 |
if (m_molecule->unitCell()) { |
| 146 |
foreach (QAction* action, m_actions) |
| 147 |
action->setEnabled(true); |
| 148 |
|
| 149 |
m_toggleUnitCellAction->setText(tr("Remove &Unit Cell")); |
| 150 |
} else { |
| 151 |
foreach (QAction* action, m_actions) |
| 152 |
action->setEnabled(false); |
| 153 |
|
| 154 |
m_importCrystalClipboardAction->setEnabled(true); |
| 155 |
m_toggleUnitCellAction->setEnabled(true); |
| 156 |
m_toggleUnitCellAction->setText(tr("Add &Unit Cell")); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
void Crystal::importCrystalClipboard() |
| 161 |
{ |
| 162 |
ImportCrystalDialog d; |
| 163 |
Core::Molecule m; |
| 164 |
if (d.importCrystalClipboard(m)) { |
| 165 |
// If we succeeded, update m_molecule |
| 166 |
Molecule::MoleculeChanges changes = Molecule::Added | Molecule::Atoms | |
| 167 |
Molecule::UnitCell | |
| 168 |
Molecule::NewCrystal; |
| 169 |
QString undoText = tr("Import Crystal from Clipboard"); |
| 170 |
m_molecule->undoMolecule()->modifyMolecule(m, changes, undoText); |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
void Crystal::registerCommands() |
| 175 |
{ |
| 176 |
emit registerCommand("wrapUnitCell", tr("Wrap atoms into the unit cell.")); |
| 177 |
emit registerCommand("standardCrystalOrientation", |
| 178 |
tr("Rotate the unit cell to the standard orientation.")); |
| 179 |
} |
| 180 |
|
| 181 |
bool Crystal::handleCommand(const QString& command, |
| 182 |
[[maybe_unused]] const QVariantMap& options) |
| 183 |
{ |
| 184 |
if (m_molecule == nullptr) |
| 185 |
return false; // No molecule to handle the command. |
| 186 |
|
| 187 |
if (command == "wrapUnitCell") { |
| 188 |
wrapAtomsToCell(); |
| 189 |
return true; |
| 190 |
} else if (command == "standardCrystalOrientation") { |
| 191 |
standardOrientation(); |
| 192 |
return true; |
| 193 |
} |
| 194 |
return false; |
| 195 |
} |
| 196 |
|
| 197 |
void Crystal::editUnitCell() |
| 198 |
{ |
| 199 |
if (!m_unitCellDialog) { |
| 200 |
m_unitCellDialog = new UnitCellDialog(qobject_cast<QWidget*>(parent())); |
| 201 |
m_unitCellDialog->setMolecule(m_molecule); |
| 202 |
} |
| 203 |
|
| 204 |
m_unitCellDialog->show(); |
| 205 |
m_unitCellDialog->raise(); |
| 206 |
m_unitCellDialog->activateWindow(); |
| 207 |
} |
| 208 |
|
| 209 |
void Crystal::buildSupercell() |
| 210 |
{ |
| 211 |
SupercellDialog d; |
| 212 |
d.buildSupercell(*m_molecule); |
| 213 |
} |
| 214 |
|
| 215 |
void Crystal::niggliReduce() |
| 216 |
{ |
| 217 |
if (CrystalTools::isNiggliReduced(*m_molecule)) { |
| 218 |
QMessageBox::information( |
| 219 |
qobject_cast<QWidget*>(parent()), tr("Niggli Reduce Crystal"), |
| 220 |
tr("The unit cell is already reduced."), QMessageBox::Ok); |
| 221 |
return; |
| 222 |
} |
| 223 |
m_molecule->undoMolecule()->niggliReduceCell(); |
| 224 |
} |
| 225 |
|
| 226 |
void Crystal::scaleVolume() |
| 227 |
{ |
| 228 |
if (!m_molecule->unitCell()) |
| 229 |
return; |
| 230 |
|
| 231 |
VolumeScalingDialog dlg; |
| 232 |
dlg.setCurrentVolume(m_molecule->unitCell()->volume()); |
| 233 |
int reply = dlg.exec(); |
| 234 |
if (reply != QDialog::Accepted) |
| 235 |
return; |
| 236 |
|
| 237 |
m_molecule->undoMolecule()->setCellVolume( |
| 238 |
dlg.newVolume(), |
| 239 |
dlg.transformAtoms() ? CrystalTools::TransformAtoms : CrystalTools::None); |
| 240 |
} |
| 241 |
|
| 242 |
void Crystal::standardOrientation() |
| 243 |
{ |
| 244 |
m_molecule->undoMolecule()->rotateCellToStandardOrientation(); |
| 245 |
} |
| 246 |
|
| 247 |
void Crystal::toggleUnitCell() |
| 248 |
{ |
| 249 |
if (m_molecule->unitCell()) { |
| 250 |
m_molecule->undoMolecule()->removeUnitCell(); |
| 251 |
} else { |
| 252 |
m_molecule->undoMolecule()->addUnitCell(); |
| 253 |
editUnitCell(); |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
void Crystal::wrapAtomsToCell() |
| 258 |
{ |
| 259 |
m_molecule->undoMolecule()->wrapAtomsToCell(); |
| 260 |
} |
| 261 |
|
| 262 |
} // namespace Avogadro::QtPlugins |