Debian Codegraph @

avogadro/qtplugins/wireframe/wireframe.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 "wireframe.h"
7
8 #include <avogadro/core/elements.h>
9 #include <avogadro/qtgui/molecule.h>
10 #include <avogadro/rendering/geometrynode.h>
11 #include <avogadro/rendering/groupnode.h>
12 #include <avogadro/rendering/widelinegeometry.h>
13 #include <avogadro/rendering/spheregeometry.h>
14
15 #include <QtCore/QSettings>
16 #include <QtWidgets/QCheckBox>
17 #include <QtWidgets/QDoubleSpinBox>
18 #include <QtWidgets/QFormLayout>
19 #include <QtWidgets/QHBoxLayout>
20 #include <QtWidgets/QLabel>
21 #include <QtWidgets/QVBoxLayout>
22 #include <QtWidgets/QWidget>
23
24 namespace Avogadro::QtPlugins {
25
26 using Core::Array;
27 using Core::Elements;
28 using QtGui::PluginLayerManager;
29 using Rendering::GeometryNode;
30 using Rendering::GroupNode;
31 using Rendering::SphereGeometry;
32 using Rendering::WideLineGeometry;
33
34 struct LayerWireframe : Core::LayerData
35 {
36 QWidget* widget;
37 bool multiBonds;
38 bool showHydrogens;
39 float lineWidth;
40
41 LayerWireframe()
42 {
43 widget = nullptr;
44 QSettings settings;
45 multiBonds = settings.value("wireframe/multiBonds", true).toBool();
46 showHydrogens = settings.value("wireframe/showHydrogens", true).toBool();
47 lineWidth = settings.value("wireframe/lineWidth", 1.0).toDouble();
48 }
49
50 LayerWireframe(std::string settings)
51 {
52 widget = nullptr;
53 deserialize(settings);
54 }
55
56 LayerData* clone() final { return new LayerWireframe(*this); }
57
58 ~LayerWireframe() override
59 {
60 if (widget)
61 widget->deleteLater();
62 }
63
64 std::string serialize() final
65 {
66 return boolToString(multiBonds) + " " + boolToString(showHydrogens) + " " +
67 std::to_string(lineWidth);
68 }
69 void deserialize(std::string text) final
70 {
71 std::stringstream ss(text);
72 std::string aux;
73 ss >> aux;
74 multiBonds = stringToBool(aux);
75 ss >> aux;
76 showHydrogens = stringToBool(aux);
77 ss >> aux;
78 lineWidth = std::stof(aux);
79 }
80
81 void setupWidget(Wireframe* slot)
82 {
83 if (!widget) {
84 widget = new QWidget(qobject_cast<QWidget*>(slot->parent()));
85 auto* v = new QVBoxLayout;
86
87 // line width
88 auto* spin = new QDoubleSpinBox;
89 spin->setRange(0.5, 5.0);
90 spin->setSingleStep(0.25);
91 spin->setDecimals(2);
92 spin->setValue(lineWidth);
93 QObject::connect(spin, SIGNAL(valueChanged(double)), slot,
94 SLOT(setWidth(double)));
95 auto* form = new QFormLayout;
96 form->addRow(QObject::tr("Line width:"), spin);
97 v->addLayout(form);
98
99 // options
100 auto* check = new QCheckBox(QObject::tr("Show multiple bonds"));
101 check->setChecked(multiBonds);
102 QObject::connect(check, &QCheckBox::clicked, slot,
103 &Wireframe::multiBonds);
104 v->addWidget(check);
105
106 check = new QCheckBox(QObject::tr("Show hydrogens"));
107 check->setChecked(showHydrogens);
108 QObject::connect(check, &QCheckBox::clicked, slot,
109 &Wireframe::showHydrogens);
110 v->addWidget(check);
111
112 v->addStretch(1);
113 widget->setLayout(v);
114 }
115 }
116 };
117
118 Wireframe::Wireframe(QObject* p) : ScenePlugin(p), m_group(nullptr)
119 {
120 m_layerManager = PluginLayerManager(m_name);
121 }
122
123 Wireframe::~Wireframe() {}
124
125 void Wireframe::process(const QtGui::Molecule& molecule,
126 Rendering::GroupNode& node)
127 {
128 m_layerManager.load<LayerWireframe>();
129 // Add a node to contain all of the lines.
130 m_group = &node;
131 auto* geometry = new GeometryNode;
132 node.addChild(geometry);
133
134 auto* lines = new WideLineGeometry;
135 lines->identifier().molecule = &molecule;
136 lines->identifier().type = Rendering::BondType;
137 // add tiny atom sites for selection
138 auto atoms = new SphereGeometry;
139 atoms->identifier().molecule = &molecule;
140 atoms->identifier().type = Rendering::AtomType;
141 auto selectedAtoms = new SphereGeometry;
142 selectedAtoms->setOpacity(0.42);
143 Vector3ub selectedColor(0, 0, 255);
144
145 geometry->addDrawable(lines);
146 geometry->addDrawable(atoms);
147 geometry->addDrawable(selectedAtoms);
148 lines->reserve(molecule.bondCount());
149 for (Index i = 0; i < molecule.bondCount(); ++i) {
150 Core::Bond bond = molecule.bond(i);
151 if (!m_layerManager.bondEnabled(bond.atom1().index(),
152 bond.atom2().index())) {
153 continue;
154 }
155 auto* interface1 = m_layerManager.getSetting<LayerWireframe>(
156 m_layerManager.getLayerID(bond.atom1().index()));
157 auto* interface2 = m_layerManager.getSetting<LayerWireframe>(
158 m_layerManager.getLayerID(bond.atom2().index()));
159 if (!interface1->showHydrogens && !interface2->showHydrogens &&
160 (bond.atom1().atomicNumber() == 1 ||
161 bond.atom2().atomicNumber() == 1)) {
162 continue;
163 }
164 Vector3f pos1 = bond.atom1().position3d().cast<float>();
165 Vector3f pos2 = bond.atom2().position3d().cast<float>();
166 Vector3ub color1(Elements::color(bond.atom1().atomicNumber()));
167 Vector3ub color2(Elements::color(bond.atom2().atomicNumber()));
168 float lineWidth = interface1->lineWidth;
169
170 if (interface1->multiBonds || interface2->multiBonds)
171 lineWidth *= bond.order();
172 // Scale from the old pixel-based range (0.5-5.0) to world-space units
173 lines->addLine(pos1, pos2, color1, color2,
174 lineWidth * WideLineGeometry::lineWidthScale);
175 // add small spheres to allow the selection tool to work
176 // smaller than this gets ignored
177 atoms->addSphere(pos1, color1, 0.001f, bond.atom1().index());
178 atoms->addSphere(pos2, color2, 0.001f, bond.atom2().index());
179 if (bond.atom1().selected())
180 selectedAtoms->addSphere(pos1, selectedColor, 0.3f, i);
181 if (bond.atom2().selected())
182 selectedAtoms->addSphere(pos2, selectedColor, 0.3f, i);
183 }
184 }
185
186 QWidget* Wireframe::setupWidget()
187 {
188 auto* interface = m_layerManager.getSetting<LayerWireframe>();
189 interface->setupWidget(this);
190 return interface->widget;
191 }
192
193 void Wireframe::multiBonds(bool show)
194 {
195 auto* interface = m_layerManager.getSetting<LayerWireframe>();
196 if (show != interface->multiBonds) {
197 interface->multiBonds = show;
198 emit drawablesChanged();
199 }
200 QSettings settings;
201 settings.setValue("wireframe/multiBonds", show);
202 }
203
204 void Wireframe::showHydrogens(bool show)
205 {
206 auto* interface = m_layerManager.getSetting<LayerWireframe>();
207 if (show != interface->showHydrogens) {
208 interface->showHydrogens = show;
209 emit drawablesChanged();
210 }
211 QSettings settings;
212 settings.setValue("wireframe/showHydrogens", show);
213 }
214
215 void Wireframe::setWidth(double width)
216 {
217 auto* interface = m_layerManager.getSetting<LayerWireframe>();
218 interface->lineWidth = float(width);
219 emit drawablesChanged();
220
221 QSettings settings;
222 settings.setValue("wireframe/lineWidth", width);
223 }
224
225 } // namespace Avogadro::QtPlugins