Debian Codegraph @

avogadro/qtplugins/spectra/spectra.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 "spectra.h"
7 #include "spectradialog.h"
8
9 #include <avogadro/core/array.h>
10 #include <avogadro/core/variant.h>
11 #include <avogadro/core/vector.h>
12 #include <avogadro/qtgui/molecule.h>
13
14 #include <avogadro/qtgui/chartdialog.h>
15 #include <avogadro/qtgui/chartwidget.h>
16
17 #include <QAction>
18 #include <QDebug>
19 #include <QtCore/QTimer>
20 #include <QtWidgets/QFileDialog>
21
22 namespace Avogadro::QtPlugins {
23
24 Spectra::Spectra(QObject* p)
25 : ExtensionPlugin(p), m_molecule(nullptr), m_dialog(nullptr)
26 {
27 auto* action = new QAction(this);
28 action->setEnabled(false);
29 action->setText(tr("Plot Spectra…"));
30 action->setProperty("menu priority", -900);
31 connect(action, SIGNAL(triggered()), SLOT(openDialog()));
32 m_actions.push_back(action);
33 }
34
35 QList<QAction*> Spectra::actions() const
36 {
37 return m_actions;
38 }
39
40 QStringList Spectra::menuPath(QAction*) const
41 {
42 QStringList path;
43 path << tr("&Analyze");
44 return path;
45 }
46
47 void Spectra::setMolecule(QtGui::Molecule* mol)
48 {
49 if (m_molecule != nullptr)
50 m_molecule->disconnect(this);
51
52 // extract vibrational and other spectra
53 m_molecule = mol;
54
55 if (m_molecule == nullptr) {
56 return;
57 }
58
59 bool enableAction = false;
60 // check to see if it has IR or Raman data
61 if (!m_molecule->vibrationFrequencies().empty())
62 enableAction = true;
63 // check if there are other spectra
64 if (!m_molecule->spectraTypes().empty())
65 enableAction = true;
66
67 foreach (auto action, m_actions)
68 action->setEnabled(enableAction);
69
70 connect(m_molecule, SIGNAL(changed(unsigned int)),
71 SLOT(moleculeChanged(unsigned int)));
72
73 if (enableAction && m_dialog != nullptr) {
74 gatherSpectra();
75 }
76 }
77
78 void Spectra::moleculeChanged(unsigned int changes)
79 {
80 if (m_molecule == nullptr)
81 return;
82
83 bool enableAction = false;
84 // check to see if it has IR or Raman data
85 if (!m_molecule->vibrationFrequencies().empty())
86 enableAction = true;
87 // check if there are other spectra
88 if (!m_molecule->spectraTypes().empty())
89 enableAction = true;
90
91 foreach (auto action, m_actions)
92 action->setEnabled(enableAction);
93
94 if (enableAction && m_dialog != nullptr) {
95 gatherSpectra();
96 }
97 }
98
99 void Spectra::openDialog()
100 {
101 if (m_molecule == nullptr)
102 return;
103
104 if (m_dialog == nullptr) {
105 m_dialog = new SpectraDialog(qobject_cast<QWidget*>(this->parent()));
106 }
107
108 gatherSpectra();
109 // update the elements
110 auto elements = m_molecule->atomicNumbers();
111 std::vector<unsigned char> atomicNumbers(elements.begin(), elements.end());
112 m_dialog->setElements(atomicNumbers);
113 m_dialog->show();
114 m_dialog->raise();
115 m_dialog->activateWindow();
116 }
117
118 void Spectra::gatherSpectra()
119 {
120 if (m_molecule == nullptr || m_dialog == nullptr)
121 return;
122
123 std::map<std::string, MatrixX> spectra;
124 // copy any spectra from the molecule
125 for (const auto& type : m_molecule->spectraTypes()) {
126 spectra[type] = m_molecule->spectra(type);
127 }
128
129 // check to see if it has IR or Raman data
130 if (!m_molecule->vibrationFrequencies().empty()) {
131 const unsigned int n = m_molecule->vibrationFrequencies().size();
132
133 MatrixX ir(n, 2);
134 // check max intensity
135 for (unsigned int i = 0; i < n; ++i) {
136 ir(i, 0) = m_molecule->vibrationFrequencies()[i];
137 ir(i, 1) = m_molecule->vibrationIRIntensities()[i];
138 }
139
140 spectra["IR"] = ir;
141
142 if (m_molecule->vibrationRamanIntensities().size() ==
143 m_molecule->vibrationFrequencies().size()) {
144 MatrixX raman(n, 2);
145 for (unsigned int i = 0; i < n; ++i) {
146 raman(i, 0) = m_molecule->vibrationFrequencies()[i];
147 raman(i, 1) = m_molecule->vibrationRamanIntensities()[i];
148 }
149 spectra["Raman"] = raman;
150 }
151 }
152
153 m_dialog->setSpectra(spectra);
154 }
155
156 } // namespace Avogadro::QtPlugins