Debian Codegraph @

avogadro/qtplugins/select/select.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 "select.h"
7
8 #include <avogadro/core/residue.h>
9 #include <avogadro/qtgui/molecule.h>
10 #include <avogadro/qtgui/periodictableview.h>
11 #include <avogadro/qtgui/rwlayermanager.h>
12 #include <avogadro/qtgui/rwmolecule.h>
13
14 #include <QAction>
15 #include <QtCore/QDebug>
16 #include <QtCore/QRegularExpression>
17 #include <QtCore/QRegularExpressionMatch>
18 #include <QtGui/QKeySequence>
19 #include <QtWidgets/QInputDialog>
20
21 #include <QtCore/QStringList>
22
23 using Avogadro::QtGui::Molecule;
24
25 namespace Avogadro::QtPlugins {
26
27 Select::Select(QObject* parent_)
28 : Avogadro::QtGui::ExtensionPlugin(parent_), m_molecule(nullptr),
29 m_elements(nullptr), m_layerManager("Select")
30 {
31 auto* action = new QAction(tr("Select All"), this);
32 action->setShortcut(QKeySequence("Ctrl+A"));
33 action->setProperty("menu priority", 990);
34 connect(action, SIGNAL(triggered()), SLOT(selectAll()));
35 m_actions.append(action);
36
37 action = new QAction(tr("Select None"), this);
38 action->setShortcut(QKeySequence("Ctrl+Shift+A"));
39 action->setProperty("menu priority", 980);
40 connect(action, SIGNAL(triggered()), SLOT(selectNone()));
41 m_actions.append(action);
42
43 action = new QAction(this);
44 action->setSeparator(true);
45 action->setProperty("menu priority", 970);
46 m_actions.append(action);
47
48 action = new QAction(tr("Invert Selection"), this);
49 action->setProperty("menu priority", 890);
50 connect(action, SIGNAL(triggered()), SLOT(invertSelection()));
51 m_actions.append(action);
52
53 action = new QAction(tr("Select by Element…"), this);
54 action->setProperty("menu priority", 880);
55 connect(action, SIGNAL(triggered()), SLOT(selectElement()));
56 m_actions.append(action);
57
58 action = new QAction(tr("Select by Atom Index…"), this);
59 action->setProperty("menu priority", 870);
60 connect(action, SIGNAL(triggered()), SLOT(selectAtomIndex()));
61 m_actions.append(action);
62
63 action = new QAction(tr("Select by Residue…"), this);
64 action->setProperty("menu priority", 860);
65 connect(action, SIGNAL(triggered()), SLOT(selectResidue()));
66 m_actions.append(action);
67
68 action = new QAction(tr("Select Backbone Atoms…"), this);
69 action->setProperty("menu priority", 858);
70 connect(action, SIGNAL(triggered()), SLOT(selectBackboneAtoms()));
71 m_actions.append(action);
72
73 action = new QAction(tr("Select Sidechain Atoms…"), this);
74 action->setProperty("menu priority", 855);
75 connect(action, SIGNAL(triggered()), SLOT(selectSidechainAtoms()));
76 m_actions.append(action);
77
78 action = new QAction(tr("Select Water"), this);
79 action->setProperty("menu priority", 850);
80 connect(action, SIGNAL(triggered()), SLOT(selectWater()));
81 m_actions.append(action);
82
83 action = new QAction(this);
84 action->setProperty("menu priority", 840);
85 action->setSeparator(true);
86 m_actions.append(action);
87
88 action = new QAction(tr("Enlarge Selection"), this);
89 action->setProperty("menu priority", 790);
90 connect(action, SIGNAL(triggered()), SLOT(enlargeSelection()));
91 m_actions.append(action);
92
93 action = new QAction(tr("Shrink Selection"), this);
94 action->setProperty("menu priority", 780);
95 connect(action, SIGNAL(triggered()), SLOT(shrinkSelection()));
96 m_actions.append(action);
97
98 action = new QAction(this);
99 action->setProperty("menu priority", 700);
100 action->setSeparator(true);
101 m_actions.append(action);
102
103 action = new QAction(tr("Create New Layer from Selection"), this);
104 action->setProperty("menu priority", 300);
105 connect(action, SIGNAL(triggered()), SLOT(createLayerFromSelection()));
106 m_actions.append(action);
107 }
108
109 Select::~Select()
110 {
111 if (m_elements)
112 m_elements->deleteLater();
113 }
114
115 QString Select::description() const
116 {
117 return tr("Change selections");
118 }
119
120 QList<QAction*> Select::actions() const
121 {
122 return m_actions;
123 }
124
125 QStringList Select::menuPath(QAction*) const
126 {
127 return QStringList() << tr("&Select");
128 }
129
130 void Select::setMolecule(QtGui::Molecule* mol)
131 {
132 m_molecule = mol;
133 }
134
135 bool Select::evalSelect(bool input, Index index) const
136 {
137 return !m_layerManager.atomLocked(index) && input;
138 }
139
140 void Select::selectAll()
141 {
142 if (m_molecule && m_molecule->atomCount() > 0) {
143 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
144 m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(true, i));
145 }
146
147 m_molecule->emitChanged(Molecule::Atoms);
148 }
149 }
150
151 void Select::selectNone()
152 {
153 if (m_molecule && m_molecule->atomCount() > 0) {
154 for (Index i = 0; i < m_molecule->atomCount(); ++i)
155 m_molecule->undoMolecule()->setAtomSelected(i, false);
156
157 m_molecule->emitChanged(Molecule::Atoms);
158 }
159 }
160
161 void Select::selectElement()
162 {
163 if (m_molecule == nullptr || m_molecule->atomCount() == 0)
164 return;
165
166 if (m_elements == nullptr) {
167 m_elements = new QtGui::PeriodicTableView(qobject_cast<QWidget*>(parent()));
168 connect(m_elements, SIGNAL(elementChanged(int)), this,
169 SLOT(selectElement(int)));
170 }
171
172 m_elements->show();
173 }
174
175 void Select::selectElement(int element)
176 {
177 if (m_molecule == nullptr || m_molecule->atomCount() == 0)
178 return;
179
180 QString undoText = tr("Select Element");
181
182 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
183 if (m_molecule->atomicNumber(i) == element) {
184 m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(true, i),
185 undoText);
186 } else
187 m_molecule->undoMolecule()->setAtomSelected(i, false, undoText);
188 }
189
190 m_molecule->emitChanged(Molecule::Atoms);
191 }
192
193 bool Select::isWaterOxygen(Index i)
194 {
195 if (m_molecule->atomicNumber(i) != 8)
196 return false;
197
198 // check to see if it has two bonds
199 auto bonds = m_molecule->bonds(i);
200 if (bonds.size() != 2)
201 return false;
202
203 // check to see that both bonds are to hydrogens
204 for (auto& bond : bonds) {
205 if (m_molecule->atomicNumber(bond.getOtherAtom(i).index()) != 1)
206 return false;
207 }
208
209 return true;
210 }
211
212 void Select::selectWater()
213 {
214 if (m_molecule == nullptr || m_molecule->atomCount() == 0)
215 return;
216
217 QString undoText = tr("Select Water");
218
219 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
220 auto atomicNumber = m_molecule->atomicNumber(i);
221 if (atomicNumber == 8 && isWaterOxygen(i)) {
222 m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(true, i));
223 continue;
224 } else if (atomicNumber == 1) {
225 // check if it's attached to a water oxygen
226 auto bonds = m_molecule->bonds(i);
227 bool isWater = false;
228 // check the bonds for a water oxygen
229 for (auto& bond : bonds) {
230 if (isWaterOxygen(bond.getOtherAtom(i).index())) {
231 m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(true, i),
232 undoText);
233 isWater = true;
234 break;
235 }
236 }
237 if (!isWater)
238 m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(false, i),
239 undoText);
240
241 continue;
242 }
243
244 m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(false, i),
245 undoText);
246 }
247 // also select water residues (which may be isolated "O" atoms)
248 for (const auto& residue : m_molecule->residues()) {
249 if (residue.residueName() == "HOH") {
250 for (auto atom : residue.residueAtoms()) {
251 Index i = atom.index();
252 m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(true, i),
253 undoText);
254 }
255 }
256 }
257
258 m_molecule->emitChanged(Molecule::Atoms);
259 }
260
261 void Select::selectBackboneAtoms()
262 {
263 if (m_molecule == nullptr || m_molecule->atomCount() == 0)
264 return;
265
266 // unselect everything
267 selectNone();
268
269 QString undoText = tr("Select Backbone");
270
271 for (const auto& residue : m_molecule->residues()) {
272 for (auto atom : residue.residueAtoms()) {
273 auto name = residue.atomName(atom);
274 if (name == "CA" || name == "C" || name == "N" || name == "O") {
275 Index i = atom.index();
276 m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(true, i),
277 undoText);
278 }
279
280 // also select hydrogens connected to the backbone atoms
281 if (atom.atomicNumber() == 1) {
282 auto bonds = m_molecule->bonds(atom.index());
283 if (bonds.size() == 1) {
284 auto otherAtom = bonds[0].getOtherAtom(atom.index());
285 auto otherName = residue.atomName(otherAtom);
286 if (otherName == "CA" || otherName == "C" || otherName == "N" ||
287 otherName == "O") {
288 Index i = atom.index();
289 m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(true, i),
290 undoText);
291 }
292 }
293 }
294 }
295 }
296
297 m_molecule->emitChanged(Molecule::Atoms);
298 }
299
300 void Select::selectSidechainAtoms()
301 {
302 if (m_molecule == nullptr || m_molecule->atomCount() == 0)
303 return;
304
305 // unselect everything
306 selectNone();
307
308 QString undoText = tr("Select Sidechain");
309
310 for (const auto& residue : m_molecule->residues()) {
311 for (auto atom : residue.residueAtoms()) {
312 auto name = residue.atomName(atom);
313 if (name != "CA" && name != "C" && name != "N" && name != "O") {
314 Index i = atom.index();
315 m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(true, i),
316 undoText);
317 }
318
319 // or is it a hydrogen connected to a backbone atom?
320 // (then we don't want to select it)
321 if (atom.atomicNumber() == 1) {
322 auto bonds = m_molecule->bonds(atom.index());
323 if (bonds.size() == 1) {
324 auto otherAtom = bonds[0].getOtherAtom(atom.index());
325 auto otherName = residue.atomName(otherAtom);
326 if (otherName == "CA" || otherName == "C" || otherName == "N" ||
327 otherName == "O") {
328 Index i = atom.index();
329 m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(false, i),
330 undoText);
331 }
332 }
333 }
334 }
335 }
336
337 m_molecule->emitChanged(Molecule::Atoms);
338 }
339
340 Vector3 Select::getSelectionCenter()
341 {
342 Vector3 center(0, 0, 0);
343 int count = 0;
344 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
345 if (m_molecule->atomSelected(i)) {
346 center += m_molecule->atomPosition3d(i);
347 ++count;
348 }
349 }
350
351 if (count > 0)
352 center /= count;
353
354 return center;
355 }
356
357 void Select::enlargeSelection()
358 {
359 if (m_molecule == nullptr || m_molecule->atomCount() == 0)
360 return;
361
362 Vector3 center = getSelectionCenter();
363 // find the current max distance of the selection
364 Real maxDistance = 0.0;
365 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
366 if (m_molecule->atomSelected(i)) {
367 // we'll use the squaredNorm to save a bunch of square roots
368 Vector3 displacement = m_molecule->atomPosition3d(i) - center;
369 Real distance = displacement.squaredNorm();
370 if (distance > maxDistance)
371 maxDistance = distance;
372 }
373 }
374 maxDistance = sqrt(maxDistance) + 2.5;
375 maxDistance *= maxDistance; // square to compare with .squaredNorm() values
376
377 QString undoText = tr("Enlarge Selection");
378
379 // now select all atoms within the NEW max distance
380 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
381 if (!m_molecule->atomSelected(i)) {
382 Vector3 displacement = m_molecule->atomPosition3d(i) - center;
383 Real distance = displacement.squaredNorm();
384 if (distance < maxDistance) {
385 m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(true, i),
386 undoText);
387 }
388 }
389 }
390
391 m_molecule->emitChanged(Molecule::Atoms);
392 }
393
394 void Select::shrinkSelection()
395 {
396 if (m_molecule == nullptr || m_molecule->atomCount() == 0)
397 return;
398
399 Vector3 center = getSelectionCenter();
400 // find the current max distance of the selection
401 Real maxDistance = 0.0;
402 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
403 if (m_molecule->atomSelected(i)) {
404 // we'll use the squaredNorm to save a bunch of square roots
405 Vector3 displacement = m_molecule->atomPosition3d(i) - center;
406 Real distance = displacement.squaredNorm();
407 if (distance > maxDistance)
408 maxDistance = distance;
409 }
410 }
411 maxDistance = sqrt(maxDistance) - 2.5;
412 if (maxDistance < 0.0)
413 maxDistance = 0.0;
414 maxDistance *= maxDistance; // square to compare with .squaredNorm() values
415
416 QString undoText = tr("Shrink Selection");
417
418 // now select ONLY atoms within the NEW max distance
419 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
420 Vector3 displacement = m_molecule->atomPosition3d(i) - center;
421 Real distance = displacement.squaredNorm();
422 if (distance < maxDistance)
423 m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(true, i),
424 undoText);
425 else
426 m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(false, i),
427 undoText);
428 }
429
430 m_molecule->emitChanged(Molecule::Atoms);
431 }
432
433 void Select::selectAtomIndex()
434 {
435 if (m_molecule == nullptr || m_molecule->atomCount() == 0)
436 return;
437
438 bool ok;
439 QString text = QInputDialog::getText(
440 qobject_cast<QWidget*>(parent()), tr("Select Atoms by Index"),
441 tr("Atoms to Select:"), QLineEdit::Normal, QString(), &ok);
442
443 if (!ok || text.isEmpty())
444 return;
445
446 QString undoText = tr("Select Atom");
447
448 auto list = text.simplified().split(',');
449 foreach (const QString item, list) {
450 // check if it's a range
451 if (item.contains('-')) {
452 auto range = item.split('-');
453 if (range.size() == 2) {
454 bool ok1, ok2;
455 int k = m_molecule->atomCount();
456 int start = range.first().toInt(&ok1);
457 int last = range.back().toInt(&ok2);
458 if (ok1 && ok2) {
459 if (start < k)
460 for (int i = start; i <= last; ++i)
461 m_molecule->undoMolecule()->setAtomSelected(
462 i, evalSelect(true, i), undoText);
463 }
464 }
465 } else {
466 int i = item.toInt(&ok);
467 int k = m_molecule->atomCount();
468 if (ok)
469 if (i < k)
470 m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(true, i),
471 undoText);
472 }
473 }
474
475 m_molecule->emitChanged(Molecule::Atoms);
476 }
477
478 void Select::selectResidue()
479 {
480 if (m_molecule == nullptr || m_molecule->atomCount() == 0)
481 return;
482
483 bool ok;
484 QString text = QInputDialog::getText(
485 qobject_cast<QWidget*>(parent()), tr("Select Atoms by Residue"),
486 tr("Residues to Select:"), QLineEdit::Normal, QString(), &ok);
487
488 if (!ok || text.isEmpty())
489 return;
490
491 QString undoText = tr("Select Residue");
492
493 auto list = text.simplified().split(',');
494 foreach (const QString item, list) {
495 const QString label = item.simplified(); // get rid of whitespace
496 // check if it's a number - select that residue index
497 int index = label.toInt(&ok);
498 if (ok) {
499 auto residueList = m_molecule->residues();
500 if (index >= 1 && index < static_cast<int>(residueList.size())) {
501 auto residue = residueList[index];
502 for (auto& atom : residue.residueAtoms()) {
503 Index i = atom.index();
504 m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(true, i),
505 undoText);
506 }
507 } // index makes sense
508 continue;
509 }
510
511 // okay it's not just a number, so see if it's HIS57, etc.
512 QRegularExpression re("([a-zA-Z]+)([0-9]+)");
513 QRegularExpressionMatch match = re.match(label);
514 if (match.hasMatch()) {
515 QString name = match.captured(1);
516 index = match.captured(2).toInt();
517
518 auto residueList = m_molecule->residues();
519 if (index >= 1 && index < static_cast<int>(residueList.size())) {
520 auto residue = residueList[index];
521 if (name == residue.residueName().c_str()) {
522 for (auto atom : residue.residueAtoms()) {
523 Index i = atom.index();
524 m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(true, i),
525 undoText);
526 }
527 } // check if name matches specified (e.g. HIS57 is really a HIS)
528 } // index makes sense
529 } else {
530 // standard residue name
531 for (const auto& residue : m_molecule->residues()) {
532 if (label == residue.residueName().c_str()) {
533 // select the atoms of the residue
534 for (auto atom : residue.residueAtoms()) {
535 Index i = atom.index();
536 m_molecule->undoMolecule()->setAtomSelected(i, evalSelect(true, i),
537 undoText);
538 }
539 } // residue matches label
540 } // for(residues)
541 continue;
542 } // 3-character labels
543 }
544
545 m_molecule->emitChanged(Molecule::Atoms);
546 }
547
548 void Select::invertSelection()
549 {
550 if (m_molecule && m_molecule->atomCount() > 0) {
551 for (Index i = 0; i < m_molecule->atomCount(); ++i)
552 m_molecule->undoMolecule()->setAtomSelected(
553 i, evalSelect(!m_molecule->atomSelected(i), i), tr("Invert Selection"));
554 m_molecule->emitChanged(Molecule::Atoms);
555 }
556 }
557
558 void Select::createLayerFromSelection()
559 {
560 if (m_molecule == nullptr || m_molecule->atomCount() == 0)
561 return;
562
563 QtGui::RWMolecule* rwmol = m_molecule->undoMolecule();
564 rwmol->beginMergeMode(tr("Change Layer"));
565 Molecule::MoleculeChanges changes =
566 Molecule::Atoms | Molecule::Layers | Molecule::Modified;
567
568 auto& layerInfo = Core::LayerManager::getMoleculeInfo(m_molecule)->layer;
569 QtGui::RWLayerManager rwLayerManager;
570 rwLayerManager.addLayer(rwmol);
571 int layer = layerInfo.maxLayer();
572
573 for (Index i = 0; i < rwmol->atomCount(); ++i) {
574 auto a = rwmol->atom(i);
575 if (a.selected()) {
576 a.setLayer(layer);
577 }
578 }
579 rwmol->endMergeMode();
580 rwmol->emitChanged(changes);
581 }
582
583 } // namespace Avogadro::QtPlugins