Debian Codegraph @

avogadro/qtplugins/licorice/licorice.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 "licorice.h"
7
8 #include <avogadro/core/elements.h>
9 #include <avogadro/qtgui/molecule.h>
10 #include <avogadro/rendering/cylindergeometry.h>
11 #include <avogadro/rendering/geometrynode.h>
12 #include <avogadro/rendering/groupnode.h>
13 #include <avogadro/rendering/spheregeometry.h>
14
15 #include <QtCore/QSettings>
16 #include <QtWidgets/QFormLayout>
17 #include <QtWidgets/QSlider>
18
19 namespace Avogadro::QtPlugins {
20
21 using QtGui::Molecule;
22 using QtGui::PluginLayerManager;
23 using Rendering::CylinderGeometry;
24 using Rendering::GeometryNode;
25 using Rendering::GroupNode;
26 using Rendering::SphereGeometry;
27
28 struct LayerLicorice : Core::LayerData
29 {
30 QWidget* widget;
31 float opacity;
32
33 LayerLicorice()
34 {
35 widget = nullptr;
36 QSettings settings;
37 opacity = settings.value("licorice/opacity", 1.0).toFloat();
38 }
39
40 LayerLicorice(std::string settings)
41 {
42 widget = nullptr;
43 deserialize(settings);
44 }
45
46 LayerData* clone() final { return new LayerLicorice(*this); }
47
48 ~LayerLicorice() override
49 {
50 if (widget)
51 widget->deleteLater();
52 }
53
54 std::string serialize() final { return std::to_string(opacity); }
55
56 void deserialize(std::string text) final
57 {
58 std::stringstream ss(text);
59 std::string aux;
60 ss >> aux;
61 opacity = std::stof(aux);
62 }
63
64 void setupWidget(Licorice* slot)
65 {
66 if (!widget) {
67 widget = new QWidget(qobject_cast<QWidget*>(slot->parent()));
68 auto* form = new QFormLayout;
69
70 // Opacity
71 auto* slider = new QSlider(Qt::Horizontal);
72 slider->setRange(0, 100);
73 slider->setTickInterval(1);
74 slider->setValue(round(opacity * 100));
75 QObject::connect(slider, &QSlider::valueChanged, slot,
76 &Licorice::setOpacity);
77
78 form->addRow(QObject::tr("Opacity:"), slider);
79 widget->setLayout(form);
80 }
81 }
82 };
83
84 Licorice::Licorice(QObject* p) : ScenePlugin(p)
85 {
86 m_layerManager = PluginLayerManager(m_name);
87 }
88
89 Licorice::~Licorice() {}
90
91 void Licorice::process(const Molecule& molecule, Rendering::GroupNode& node)
92 {
93 m_layerManager.load<LayerLicorice>();
94
95 // Use a common radius for all spheres and cylinders.
96 float radius(0.2f);
97 float selectedRadius(radius * 2.0f);
98
99 // Add a sphere node to contain all of the spheres.
100 auto* geometry = new GeometryNode;
101 node.addChild(geometry);
102 auto* spheres = new SphereGeometry;
103 spheres->identifier().molecule = &molecule;
104 spheres->identifier().type = Rendering::AtomType;
105
106 auto selectedSpheres = new SphereGeometry;
107 selectedSpheres->setOpacity(0.42);
108
109 auto translucentSpheres = new SphereGeometry;
110 translucentSpheres->setRenderPass(Rendering::TranslucentPass);
111 translucentSpheres->identifier().molecule = &molecule;
112 translucentSpheres->identifier().type = Rendering::AtomType;
113
114 geometry->addDrawable(spheres);
115 geometry->addDrawable(selectedSpheres);
116 geometry->addDrawable(translucentSpheres);
117
118 float opacity = 1.0;
119
120 for (Index i = 0; i < molecule.atomCount(); ++i) {
121 Core::Atom atom = molecule.atom(i);
122 if (!m_layerManager.atomEnabled(i)) {
123 continue;
124 }
125 Vector3ub color = atom.color();
126
127 auto* interface =
128 m_layerManager.getSetting<LayerLicorice>(m_layerManager.getLayerID(i));
129 opacity = interface->opacity;
130 if (opacity < 1.0f) {
131 translucentSpheres->addSphere(atom.position3d().cast<float>(), color,
132 radius, i);
133 translucentSpheres->setOpacity(opacity);
134 } else
135 spheres->addSphere(atom.position3d().cast<float>(), color, radius, i);
136
137 if (atom.selected()) {
138 color = Vector3ub(0, 0, 255);
139 selectedSpheres->addSphere(atom.position3d().cast<float>(), color,
140 selectedRadius, i);
141 }
142 }
143
144 auto* cylinders = new CylinderGeometry;
145 cylinders->identifier().molecule = &molecule;
146 cylinders->identifier().type = Rendering::BondType;
147
148 auto* translucentCylinders = new CylinderGeometry;
149 translucentCylinders->setRenderPass(Rendering::TranslucentPass);
150 translucentCylinders->identifier().molecule = &molecule;
151 translucentCylinders->identifier().type = Rendering::BondType;
152
153 geometry->addDrawable(cylinders);
154 geometry->addDrawable(translucentCylinders);
155
156 for (Index i = 0; i < molecule.bondCount(); ++i) {
157 Core::Bond bond = molecule.bond(i);
158 if (!m_layerManager.bondEnabled(bond.atom1().index(),
159 bond.atom2().index())) {
160 continue;
161 }
162
163 auto* interface1 = m_layerManager.getSetting<LayerLicorice>(
164 m_layerManager.getLayerID(bond.atom1().index()));
165 auto* interface2 = m_layerManager.getSetting<LayerLicorice>(
166 m_layerManager.getLayerID(bond.atom2().index()));
167
168 bool doOpaque = true;
169 if (interface1->opacity < 1.0f || interface2->opacity < 1.0f) {
170 opacity = std::min(interface1->opacity, interface2->opacity);
171 translucentCylinders->setOpacity(opacity);
172 doOpaque = false;
173 }
174
175 Vector3f pos1 = bond.atom1().position3d().cast<float>();
176 Vector3f pos2 = bond.atom2().position3d().cast<float>();
177 Vector3ub color1 = bond.atom1().color();
178 Vector3ub color2 = bond.atom2().color();
179 Vector3f bondVector = pos2 - pos1;
180 float bondLength = bondVector.norm();
181 bondVector /= bondLength;
182
183 if (doOpaque)
184 cylinders->addCylinder(pos1, pos2, radius, color1, color2, i);
185 else
186 translucentCylinders->addCylinder(pos1, pos2, radius, color1, color2, i);
187 }
188 }
189
190 void Licorice::setOpacity(int opacity)
191 {
192 m_opacity = static_cast<float>(opacity) / 100.0f;
193 auto* interface = m_layerManager.getSetting<LayerLicorice>();
194 if (m_opacity != interface->opacity) {
195 interface->opacity = m_opacity;
196 emit drawablesChanged();
197 }
198
199 QSettings settings;
200 settings.setValue("licorice/opacity", m_opacity);
201 }
202
203 QWidget* Licorice::setupWidget()
204 {
205 auto* interface = m_layerManager.getSetting<LayerLicorice>();
206 interface->setupWidget(this);
207 return interface->widget;
208 }
209
210 } // namespace Avogadro::QtPlugins