Debian Codegraph @

avogadro/qtplugins/alchemy/alchemy.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 "alchemy.h"
7
8 #include <avogadro/core/elements.h>
9 #include <avogadro/qtgui/molecule.h>
10
11 #include <QAction>
12 #include <QtCore/QSettings>
13 #include <QtWidgets/QDialog>
14 #include <QtWidgets/QInputDialog>
15
16 #include <vector>
17
18 namespace Avogadro::QtPlugins {
19
20 using Core::Array;
21 using Core::Elements;
22
23 Alchemy::Alchemy(QObject* parent_)
24 : Avogadro::QtGui::ExtensionPlugin(parent_), m_molecule(nullptr),
25 m_action(new QAction(tr("Change Elements…"), this))
26 {
27 m_action->setProperty("menu priority", 750);
28
29 connect(m_action, &QAction::triggered, this, &Alchemy::changeElements);
30 }
31
32 Alchemy::~Alchemy() {}
33
34 QList<QAction*> Alchemy::actions() const
35 {
36 QList<QAction*> result;
37 return result << m_action;
38 }
39
40 QStringList Alchemy::menuPath(QAction*) const
41 {
42 return QStringList() << tr("&Build");
43 }
44
45 void Alchemy::setMolecule(QtGui::Molecule* mol)
46 {
47 m_molecule = mol;
48 }
49
50 void Alchemy::changeElements()
51 {
52 if (!m_molecule)
53 return;
54
55 // assemble the list of elements
56 QStringList choices;
57 for (unsigned char i = 0; i < Elements::elementCount(); ++i) {
58 QString choice("%1: %2");
59 choice = choice.arg(i).arg(Elements::name(i));
60 choices << choice;
61 }
62
63 // get the element of the first selected atom
64 unsigned char firstElement = 0;
65 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
66 if (m_molecule->atomSelected(i)) {
67 firstElement = m_molecule->atom(i).atomicNumber();
68 break;
69 }
70 }
71
72 bool ok = false;
73 QString currentChoice = QInputDialog::getItem(
74 qobject_cast<QWidget*>(parent()), tr("Change Elements"), tr("Element:"),
75 choices, static_cast<int>(firstElement), false, &ok);
76 if (!ok)
77 return;
78
79 unsigned char newElement = currentChoice.section(':', 0, 0).toUShort();
80 // loop through the selected atoms and change their elements
81 for (Index i = 0; i < m_molecule->atomCount(); ++i) {
82 if (m_molecule->atomSelected(i))
83 m_molecule->atom(i).setAtomicNumber(newElement);
84 }
85
86 m_molecule->emitChanged(QtGui::Molecule::Atoms);
87 }
88
89 } // namespace Avogadro::QtPlugins