Debian Codegraph @

avogadro/qtplugins/bonding/bonding.cpp

raw
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 "bonding.h"
7
8 #include <avogadro/core/elements.h>
9 #include <avogadro/qtgui/molecule.h>
10
11 #include <QAction>
12 #include <QDialog>
13 #include <QSettings>
14
15 #include <vector>
16
17 #include "ui_bondingdialog.h"
18
19 namespace Avogadro::QtPlugins {
20
21 using Core::Array;
22 using Core::Elements;
23
24 using NeighborListType = Avogadro::Core::Array<Avogadro::Core::Bond>;
25
26 Bonding::Bonding(QObject* parent_)
27 : Avogadro::QtGui::ExtensionPlugin(parent_),
28 m_action(new QAction(tr("Bond Atoms"), this)),
29 m_orderAction(new QAction(tr("Perceive Bond Orders"), this)),
30 m_clearAction(new QAction(tr("Remove Bonds"), this)),
31 m_configAction(new QAction(tr("Configure Bonding…"), this)),
32 m_createBondsAction(new QAction(tr("Bond Selected Atoms"), this)),
33 m_dialog(nullptr), m_ui(nullptr)
34 {
35 QSettings settings;
36 m_tolerance = settings.value("bonding/tolerance", 0.45).toDouble();
37 m_minDistance = settings.value("bonding/minDistance", 0.32).toDouble();
38
39 m_action->setShortcut(QKeySequence("Ctrl+B"));
40 m_action->setProperty("menu priority", 750);
41 m_createBondsAction->setProperty("menu priority", 740);
42 m_orderAction->setProperty("menu priority", 735);
43 m_clearAction->setShortcut(QKeySequence("Ctrl+Shift+B"));
44 m_clearAction->setProperty("menu priority", 720);
45
46 connect(m_action, SIGNAL(triggered()), SLOT(bond()));
47 connect(m_createBondsAction, SIGNAL(triggered()), SLOT(createBond()));
48 connect(m_orderAction, SIGNAL(triggered()), SLOT(bondOrders()));
49 connect(m_clearAction, SIGNAL(triggered()), SLOT(clearBonds()));
50 connect(m_configAction, SIGNAL(triggered()), SLOT(configure()));
51 }
52
53 QList<QAction*> Bonding::actions() const
54 {
55 QList<QAction*> result;
56 return result << m_action << m_createBondsAction << m_orderAction
57 << m_clearAction << m_configAction;
58 }
59
60 QStringList Bonding::menuPath(QAction*) const
61 {
62 return QStringList() << tr("&Build") << tr("Bond");
63 }
64
65 void Bonding::setMolecule(QtGui::Molecule* mol)
66 {
67 m_molecule = mol;
68 }
69
70 void Bonding::registerCommands()
71 {
72 emit registerCommand("removeBonds",
73 tr("Remove bonds from all or selected atoms."));
74 emit registerCommand("createBonds",
75 tr("Create bonds between all or selected atoms."));
76 emit registerCommand("addBondOrders", tr("Perceive bond orders."));
77 }
78
79 bool Bonding::handleCommand(const QString& command,
80 [[maybe_unused]] const QVariantMap& options)
81 {
82 if (m_molecule == nullptr)
83 return false; // No molecule to handle the command.
84
85 if (command == "removeBonds") {
86 clearBonds();
87 return true;
88 } else if (command == "createBonds") {
89 bond();
90 return true;
91 } else if (command == "addBondOrders") {
92 bondOrders();
93 return true;
94 }
95 return false;
96 }
97
98 void Bonding::configure()
99 {
100 if (!m_ui) {
101 m_dialog = new QDialog(qobject_cast<QWidget*>(parent()));
102 m_ui = new Ui::BondingDialog;
103 m_ui->setupUi(m_dialog);
104
105 m_ui->toleranceSpinBox->setValue(m_tolerance);
106 m_ui->minimumSpinBox->setValue(m_minDistance);
107
108 connect(m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(setValues()));
109 connect(m_ui->buttonBox, SIGNAL(rejected()), m_dialog, SLOT(close()));
110 }
111
112 m_dialog->show();
113 m_dialog->raise();
114 m_dialog->activateWindow();
115 }
116
117 void Bonding::setValues()
118 {
119 if (m_dialog == nullptr || m_ui == nullptr)
120 return;
121 m_dialog->close();
122
123 m_tolerance = m_ui->toleranceSpinBox->value();
124 m_minDistance = m_ui->minimumSpinBox->value();
125
126 QSettings settings;
127 settings.setValue("bonding/tolerance", m_tolerance);
128 settings.setValue("bonding/minDistance", m_minDistance);
129 }
130
131 void Bonding::createBond()
132 {
133 // Create bond between selected atoms no matter the distance
134 if (!m_molecule)
135 return;
136
137 if (m_molecule->isSelectionEmpty())
138 return;
139
140 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
141 if (!m_molecule->atomSelected(i))
142 continue;
143
144 for (Index j = i + 1; j < m_molecule->atomCount(); ++j) {
145 if (!m_molecule->atomSelected(j))
146 continue;
147
148 m_molecule->addBond(i, j, 1);
149 }
150 }
151
152 m_molecule->emitChanged(QtGui::Molecule::Bonds);
153 }
154
155 void Bonding::bond()
156 {
157 // Yes, this is largely reproduced from Core::Molecule::perceiveBondsSimple
158 // .. but that class doesn't know about selections
159 if (!m_molecule)
160 return;
161
162 // Check for 3D coordinates, can't do bond perception without this.
163 if (m_molecule->atomPositions3d().size() != m_molecule->atomCount())
164 return;
165
166 // cache atomic radii
167 std::vector<double> radii(m_molecule->atomCount());
168 for (size_t i = 0; i < radii.size(); i++) {
169 radii[i] = Elements::radiusCovalent(m_molecule->atomicNumbers()[i]);
170 if (radii[i] <= 0.0)
171 radii[i] = 0.0;
172 }
173
174 bool emptySelection = m_molecule->isSelectionEmpty();
175 double minSq = m_minDistance * m_minDistance;
176
177 // Main bond perception loop based on a simple distance metric.
178 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
179 if (!emptySelection && !m_molecule->atomSelected(i))
180 continue;
181
182 Vector3 ipos = m_molecule->atomPositions3d()[i];
183 for (Index j = i + 1; j < m_molecule->atomCount(); ++j) {
184 if (!emptySelection && !m_molecule->atomSelected(j))
185 continue;
186
187 double cutoff = radii[i] + radii[j] + m_tolerance;
188 Vector3 jpos = m_molecule->atomPositions3d()[j];
189 Vector3 diff = jpos - ipos;
190
191 if (std::fabs(diff[0]) > cutoff || std::fabs(diff[1]) > cutoff ||
192 std::fabs(diff[2]) > cutoff ||
193 (m_molecule->atomicNumbers()[i] == 1 &&
194 m_molecule->atomicNumbers()[j] == 1)) {
195 continue;
196 }
197
198 // check radius and add bond if needed
199 double cutoffSq = cutoff * cutoff;
200 double diffsq = diff.squaredNorm();
201 if (diffsq < cutoffSq && diffsq > minSq)
202 m_molecule->addBond(m_molecule->atom(i), m_molecule->atom(j), 1);
203 }
204 }
205 m_molecule->emitChanged(QtGui::Molecule::Bonds);
206 }
207
208 void Bonding::bondOrders()
209 {
210 m_molecule->perceiveBondOrders();
211 m_molecule->emitChanged(QtGui::Molecule::Bonds);
212 }
213
214 void Bonding::clearBonds()
215 {
216 // remove any bonds connected to the selected atoms
217 // Array<BondType> bonds(Index a);
218 if (m_molecule->isSelectionEmpty())
219 m_molecule->clearBonds();
220 else {
221 std::vector<size_t> bondIndices;
222 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
223 if (!m_molecule->atomSelected(i))
224 continue;
225
226 // OK, the atom is selected, get the bonds to delete
227 const NeighborListType bonds = m_molecule->bonds(i);
228 for (auto bond : bonds) {
229 bondIndices.push_back(bond.index());
230 }
231 } // end looping through atoms
232
233 // now delete the bonds
234 for (auto it = bondIndices.rbegin(), itEnd = bondIndices.rend();
235 it != itEnd; ++it) {
236 m_molecule->removeBond(*it);
237 }
238 } // end else(selected atoms)
239 m_molecule->emitChanged(QtGui::Molecule::Bonds);
240 }
241
242 } // namespace Avogadro::QtPlugins