Debian Codegraph @

avogadro/qtplugins/constraints/constraintsdialog.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 "constraintsdialog.h"
7 #include "ui_constraintsdialog.h"
8
9 #include <QDebug>
10 #include <QtCore/QSortFilterProxyModel>
11
12 #include <avogadro/core/angletools.h>
13 #include <avogadro/qtgui/rwmolecule.h>
14
15 using Avogadro::Core::Constraint;
16 using Avogadro::QtGui::Molecule;
17
18 namespace Avogadro {
19 namespace QtPlugins {
20
21 ConstraintsDialog::ConstraintsDialog(QWidget* parent_, Qt::WindowFlags f)
22 : QDialog(parent_, f), ui(new Ui::ConstraintsDialog),
23 m_model(new ConstraintsModel)
24 {
25 ui->setupUi(this);
26
27 connect(ui->comboType, SIGNAL(currentIndexChanged(int)), this,
28 SLOT(changeType(int)));
29 connect(ui->okButton, SIGNAL(clicked()), this, SLOT(acceptConstraints()));
30 connect(ui->addConstraint, SIGNAL(clicked()), this, SLOT(addConstraint()));
31 connect(ui->getConstraint, SIGNAL(clicked()), this,
32 SLOT(getConstraint()));
33 connect(ui->deleteConstraint, SIGNAL(clicked()), this,
34 SLOT(deleteConstraint()));
35 connect(ui->deleteAllConstraints, SIGNAL(clicked()), this,
36 SLOT(deleteAllConstraints()));
37
38 changeType(0);
39
40 // TODO use sort model
41 auto* proxyModel = new QSortFilterProxyModel(this);
42 proxyModel->setSourceModel(m_model);
43 proxyModel->setDynamicSortFilter(true);
44 proxyModel->setSortLocaleAware(true);
45 // this role will received direct floating-point numbers from the model
46 proxyModel->setSortRole(Qt::UserRole);
47
48 auto* view = ui->constraintsTableView;
49 view->setModel(proxyModel);
50 view->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
51 view->resizeColumnsToContents();
52 view->setSelectionBehavior(QAbstractItemView::SelectRows);
53 view->setAlternatingRowColors(true);
54 // TODO: Allow sorting the table
55 // requires remapping
56
57 connect(view->selectionModel(),
58 SIGNAL(currentChanged(const QModelIndex&, const QModelIndex&)), this,
59 SLOT(highlightSelected(const QModelIndex&, const QModelIndex&)));
60 }
61
62 ConstraintsDialog::~ConstraintsDialog()
63 {
64 delete ui;
65 m_model->deleteLater();
66 }
67
68 void ConstraintsDialog::setMolecule(QtGui::Molecule* molecule)
69 {
70 m_molecule = molecule;
71 m_model->setConstraints(molecule->constraints());
72
73 connect(m_molecule, SIGNAL(changed(unsigned int)), this,
74 SLOT(updateConstraints()));
75 }
76
77 void ConstraintsDialog::highlightSelected(const QModelIndex& newIndex,
78 const QModelIndex& oldIndex)
79 {
80 // get the selected row in the table
81 auto row = ui->constraintsTableView->currentIndex().row();
82 m_model->currentRow = row;
83 if (row < 0 || row >= m_model->rowCount())
84 return;
85 // get the constraint
86 auto constraint = m_model->constraint(row);
87 if (constraint.type() == Constraint::None)
88 return;
89
90 // unselect everything else in the molecule
91 for (Index i = 0; i < m_molecule->atomCount(); ++i)
92 m_molecule->undoMolecule()->setAtomSelected(i, false);
93
94 // select the atoms in the constraint
95 m_molecule->undoMolecule()->setAtomSelected(constraint.aIndex(), true);
96 m_molecule->undoMolecule()->setAtomSelected(constraint.bIndex(), true);
97 if (constraint.cIndex() != MaxIndex)
98 m_molecule->undoMolecule()->setAtomSelected(constraint.cIndex(), true);
99 if (constraint.dIndex() != MaxIndex)
100 m_molecule->undoMolecule()->setAtomSelected(constraint.dIndex(), true);
101
102 m_molecule->emitChanged(Molecule::Selection);
103 }
104
105 void ConstraintsDialog::updateConstraints()
106 {
107 if (m_molecule == nullptr || m_model == nullptr)
108 return;
109
110 m_model->setConstraints(m_molecule->constraints());
111 ui->constraintsTableView->resizeColumnsToContents();
112
113 // update the maximum atoms on the spin boxes
114 Index maxAtom = m_molecule->atomCount();
115 ui->editA->setMaximum(maxAtom);
116 ui->editB->setMaximum(maxAtom);
117 ui->editC->setMaximum(maxAtom);
118 ui->editD->setMaximum(maxAtom);
119
120 // get the list of selected atoms (if any)
121 std::vector<Index> selectedAtoms;
122 for (Index i = 0; i < m_molecule->atomCount(); ++i)
123 if (m_molecule->atomSelected(i))
124 selectedAtoms.push_back(i);
125
126 // change the type
127 if (selectedAtoms.size() == 2)
128 changeType(0);
129 else if (selectedAtoms.size() == 3)
130 changeType(1);
131 else if (selectedAtoms.size() == 4)
132 changeType(2);
133
134 // Each entry bonded[i] holds the atom indices bonded to selectedAtoms[i]
135 std::vector<std::vector<Index>> bonded(selectedAtoms.size());
136
137 for (Index i = 0; i < selectedAtoms.size(); ++i) {
138 Index ai = selectedAtoms[i];
139 for (Index j = i + 1; j < selectedAtoms.size(); ++j) {
140 Index aj = selectedAtoms[j];
141 if (m_molecule->bond(ai, aj).isValid()) {
142 bonded[i].push_back(j); // neighbor atom local index
143 bonded[j].push_back(i); // symmetric
144 }
145 }
146 }
147 std::vector<Index> degree(selectedAtoms.size(), 0);
148 for (Index i = 0; i < selectedAtoms.size(); ++i)
149 degree[i] = static_cast<Index>(bonded[i].size());
150
151 // Arrange selected atoms according to connectivity
152 if (selectedAtoms.size() == 3) {
153 Index i1, i2, i3;
154 bool assigned = false;
155
156 for (Index i = 0; i < selectedAtoms.size(); ++i) {
157 if (degree[i] == 2) {
158 i2 = selectedAtoms[i];
159 i1 = selectedAtoms[bonded[i][0]];
160 i3 = selectedAtoms[bonded[i][1]];
161 assigned = true;
162 }
163 else if (degree[i] == 0) {
164 return;
165 }
166 if (assigned)
167 break;
168 }
169 if (not assigned)
170 return;
171
172 // Set corrrect order
173 selectedAtoms[0] = i1;
174 selectedAtoms[1] = i2;
175 selectedAtoms[2] = i3;
176 }
177
178 else if (selectedAtoms.size() == 4) {
179 Index i1, i2, i3, i4;
180 bool assigned = false;
181
182 for (Index i = 0; i < selectedAtoms.size(); ++i) {
183 if (degree[i] == 2) {
184 i2 = selectedAtoms[i];
185 if (degree[bonded[i][0]] == 2) {
186 i1 = selectedAtoms[bonded[i][1]];
187 i3 = bonded[i][0];
188 }
189 else if (degree[bonded[i][1]] == 2) {
190 i1 = selectedAtoms[bonded[i][0]];
191 i3 = bonded[i][1];
192 }
193 else {
194 return;
195 }
196 if (selectedAtoms[bonded[i3][0]] == i2) {
197 i4 = selectedAtoms[bonded[i3][1]];
198 }
199 else {
200 i4 = selectedAtoms[bonded[i3][0]];
201 }
202 i3 = selectedAtoms[i3];
203 assigned = true;
204 }
205 else if (degree[i] == 0) {
206 return;
207 }
208 if (assigned)
209 break;
210 }
211 if (not assigned)
212 return;
213
214 // Set corrrect order
215 selectedAtoms[0] = i1;
216 selectedAtoms[1] = i2;
217 selectedAtoms[2] = i3;
218 selectedAtoms[3] = i4;
219 }
220
221 // now set the edit boxes to the selected atoms
222 if (selectedAtoms.size() >= 2) {
223 ui->editA->setValue(selectedAtoms[0] + 1);
224 ui->editB->setValue(selectedAtoms[1] + 1);
225 }
226 if (selectedAtoms.size() >= 3) {
227 ui->editC->setValue(selectedAtoms[2] + 1);
228 }
229 if (selectedAtoms.size() == 4) {
230 ui->editD->setValue(selectedAtoms[3] + 1);
231 }
232
233 // finally set the default value in editValue
234 if (selectedAtoms.size() == 2) {
235 Vector3 a = m_molecule->atomPosition3d(selectedAtoms[0]);
236 Vector3 b = m_molecule->atomPosition3d(selectedAtoms[1]);
237 double distance = (a - b).norm();
238 ui->editValue->setValue(distance);
239 } else if (selectedAtoms.size() == 3) {
240 Vector3 a = m_molecule->atomPosition3d(selectedAtoms[0]);
241 Vector3 b = m_molecule->atomPosition3d(selectedAtoms[1]);
242 Vector3 c = m_molecule->atomPosition3d(selectedAtoms[2]);
243 double angle = calculateAngle(a, b, c);
244 ui->editValue->setValue(angle);
245 } else if (selectedAtoms.size() == 4) {
246 // todo detect if torsion or out-of-plane bending
247 Vector3 a = m_molecule->atomPosition3d(selectedAtoms[0]);
248 Vector3 b = m_molecule->atomPosition3d(selectedAtoms[1]);
249 Vector3 c = m_molecule->atomPosition3d(selectedAtoms[2]);
250 Vector3 d = m_molecule->atomPosition3d(selectedAtoms[3]);
251 double dihedral = calculateDihedral(a, b, c, d);
252 ui->editValue->setValue(dihedral);
253 }
254 }
255
256 void ConstraintsDialog::changeType(int newType)
257 {
258 // disable B-C-D and enable what we need
259 ui->editB->setEnabled(false);
260 ui->editC->setEnabled(false);
261 ui->editD->setEnabled(false);
262
263 ui->editB->setMinimum(0);
264 ui->editC->setMinimum(0);
265 ui->editD->setMinimum(0);
266
267 switch (newType) {
268 case 2: // torsion
269 ui->editD->setEnabled(true);
270 ui->editD->setMinimum(1);
271 case 1: // angle
272 ui->editC->setEnabled(true);
273 ui->editC->setMinimum(1);
274 ui->editD->setValue(0);
275 case 0: // distance
276 ui->editB->setEnabled(true);
277 ui->editB->setMinimum(1);
278 ui->editC->setValue(0);
279 ui->editD->setValue(0);
280 }
281
282 if (newType == 0) {
283 ui->editValue->setSuffix("Å");
284 ui->editValue->setMinimum(0.0);
285 ui->editValue->setMaximum(1000.0);
286 ui->comboType->setCurrentIndex(0);
287 }
288 else if (newType == 1) {
289 ui->editValue->setSuffix("°");
290 ui->editValue->setMinimum(0.0);
291 ui->editValue->setMaximum(180.0);
292 ui->comboType->setCurrentIndex(1);
293 }
294 else {
295 ui->editValue->setSuffix("°");
296 ui->editValue->setMinimum(-180.0);
297 ui->editValue->setMaximum(180.0);
298 ui->comboType->setCurrentIndex(2);
299 }
300 }
301
302 void ConstraintsDialog::acceptConstraints()
303 {
304 hide();
305 }
306
307 void ConstraintsDialog::getConstraint()
308 {
309 if (m_molecule == nullptr || m_molecule == nullptr)
310 return;
311
312 // get the new constraints
313 m_molecule->setConstraints(m_model->constraints());
314 m_molecule->emitChanged(Molecule::Constraints);
315 }
316
317 void ConstraintsDialog::deleteConstraint()
318 {
319 if (m_molecule == nullptr || m_molecule == nullptr)
320 return;
321
322 auto row = ui->constraintsTableView->currentIndex().row();
323 row = m_model->currentRow;
324
325 if (!ui->constraintsTableView->selectionModel()->hasSelection())
326 return;
327
328 m_model->deleteConstraint(row);
329 // get the new constraints
330 m_molecule->setConstraints(m_model->constraints());
331 m_molecule->emitChanged(Molecule::Constraints);
332 }
333
334 void ConstraintsDialog::addConstraint()
335 {
336 if (m_molecule == nullptr)
337 return;
338
339 // TODO: Check user input for sanity
340 Constraint::Type type;
341 switch (ui->comboType->currentIndex()) {
342 case 1:
343 type = Constraint::AngleConstraint;
344 break;
345 case 2:
346 type = Constraint::TorsionConstraint;
347 break;
348 case 0:
349 default:
350 type = Constraint::DistanceConstraint;
351 break;
352 }
353 double value = ui->editValue->value();
354 int atomIdA = ui->editA->value();
355 int atomIdB = ui->editB->value();
356 int atomIdC = ui->editC->value();
357 int atomIdD = ui->editD->value();
358
359 Index a, b, c, d;
360 if (atomIdA < 1 || atomIdA > m_molecule->atomCount())
361 return;
362 else
363 a = atomIdA - 1;
364
365 if (atomIdB < 1 || atomIdB > m_molecule->atomCount())
366 return;
367 else
368 b = atomIdB - 1;
369
370 if (atomIdC < 1 || atomIdC > m_molecule->atomCount())
371 c = MaxIndex;
372 else
373 c = atomIdC - 1;
374
375 if (atomIdD < 1 || atomIdD > m_molecule->atomCount())
376 d = MaxIndex;
377 else
378 d = atomIdD - 1;
379
380 if (type == Constraint::DistanceConstraint) {
381 if (a == b || value == 0.0)
382 return;
383 } else if (type == Constraint::AngleConstraint) {
384 if (a == b || b == c)
385 return;
386 } else if (type == Constraint::TorsionConstraint)
387 if (a == b || a == c || a == d || b == c || b == d || c == d)
388 return;
389
390 Constraint newConstraint(a, b, c, d, value);
391 newConstraint.setType(type);
392 m_molecule->addConstraint(newConstraint);
393 m_model->addConstraint(type, a, b, c, d, value);
394 m_molecule->emitChanged(Molecule::Constraints);
395 }
396
397 void ConstraintsDialog::deleteAllConstraints()
398 {
399 if (m_molecule == nullptr)
400 return;
401
402 m_molecule->clearConstraints();
403 m_molecule->emitChanged(Molecule::Constraints);
404 // update the model
405 m_model->clear();
406 }
407
408 } // namespace QtPlugins
409 } // namespace Avogadro