Debian Codegraph @

avogadro/qtplugins/crystal/crystalscene.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 "crystalscene.h"
7
8 #include <avogadro/core/unitcell.h>
9 #include <avogadro/qtgui/colorbutton.h>
10 #include <avogadro/qtgui/molecule.h>
11 #include <avogadro/rendering/cylindergeometry.h>
12 #include <avogadro/rendering/geometrynode.h>
13 #include <avogadro/rendering/groupnode.h>
14 #include <avogadro/rendering/spheregeometry.h>
15
16 #include <QtCore/QSettings>
17 #include <QtWidgets/QCheckBox>
18 #include <QtWidgets/QDoubleSpinBox>
19 #include <QtWidgets/QFormLayout>
20 #include <QtWidgets/QVBoxLayout>
21
22 #include <QDebug>
23
24 namespace Avogadro::QtPlugins {
25
26 using Core::UnitCell;
27 using Rendering::CylinderGeometry;
28 using Rendering::GeometryNode;
29 using Rendering::GroupNode;
30 using Rendering::SphereGeometry;
31
32 const Vector3ub red = { 255, 0, 0 };
33 const Vector3ub green = { 0, 255, 0 };
34 const Vector3ub blue = { 0, 0, 255 };
35
36 CrystalScene::CrystalScene(QObject* p) : ScenePlugin(p), m_setupWidget(nullptr)
37 {
38 m_layerManager = QtGui::PluginLayerManager(m_name);
39
40 QSettings settings;
41 m_lineWidth = settings.value("crystal/lineWidth", 2.0).toDouble();
42
43 auto color =
44 settings.value("crystal/color", QColor(Qt::white)).value<QColor>();
45 m_color[0] = static_cast<unsigned char>(color.red());
46 m_color[1] = static_cast<unsigned char>(color.green());
47 m_color[2] = static_cast<unsigned char>(color.blue());
48
49 m_multiColor = settings.value("crystal/multiColor", true).toBool();
50 }
51
52 CrystalScene::~CrystalScene() {}
53
54 void CrystalScene::process(const QtGui::Molecule& molecule, GroupNode& node)
55 {
56 if (const UnitCell* cell = molecule.unitCell()) {
57 auto* geometry = new GeometryNode;
58 node.addChild(geometry);
59
60 auto* cylinders = new CylinderGeometry;
61 geometry->addDrawable(cylinders);
62 auto* spheres = new SphereGeometry;
63 geometry->addDrawable(spheres);
64
65 auto color = m_color;
66 float radius = static_cast<float>(m_lineWidth) * 0.0125f;
67
68 Vector3f a = cell->aVector().cast<float>();
69 Vector3f b = cell->bVector().cast<float>();
70 Vector3f c = cell->cVector().cast<float>();
71
72 Vector3f o(Vector3f::Zero());
73
74 // 8 corner spheres
75 Vector3f corners[8] = { o, o + a, o + b, o + a + b,
76 o + c, o + a + c, o + b + c, o + a + b + c };
77 for (const auto& corner : corners)
78 spheres->addSphere(corner, color, radius);
79
80 // a-axis edges
81 Vector3ub aColor = m_multiColor ? red : color;
82 cylinders->addCylinder(o, o + a, radius, aColor);
83 cylinders->addCylinder(o + c, o + a + c, radius, color);
84 cylinders->addCylinder(o + b, o + a + b, radius, color);
85 cylinders->addCylinder(o + b + c, o + a + b + c, radius, color);
86
87 // b-axis edges
88 Vector3ub bColor = m_multiColor ? green : color;
89 cylinders->addCylinder(o, o + b, radius, bColor);
90 cylinders->addCylinder(o + c, o + b + c, radius, color);
91 cylinders->addCylinder(o + a, o + a + b, radius, color);
92 cylinders->addCylinder(o + a + c, o + a + b + c, radius, color);
93
94 // c-axis edges
95 Vector3ub cColor = m_multiColor ? blue : color;
96 cylinders->addCylinder(o, o + c, radius, cColor);
97 cylinders->addCylinder(o + a, o + a + c, radius, color);
98 cylinders->addCylinder(o + b, o + b + c, radius, color);
99 cylinders->addCylinder(o + a + b, o + a + b + c, radius, color);
100 }
101 }
102
103 void CrystalScene::setLineWidth(double width)
104 {
105 m_lineWidth = width;
106 emit drawablesChanged();
107
108 QSettings settings;
109 settings.setValue("crystal/lineWidth", width);
110 }
111
112 void CrystalScene::setColor(const QColor& color)
113 {
114 m_color[0] = static_cast<unsigned char>(color.red());
115 m_color[1] = static_cast<unsigned char>(color.green());
116 m_color[2] = static_cast<unsigned char>(color.blue());
117
118 emit drawablesChanged();
119
120 QSettings settings;
121 settings.setValue("crystal/color", color);
122 }
123
124 void CrystalScene::setMultiColor(bool multiColor)
125 {
126 m_multiColor = multiColor;
127 emit drawablesChanged();
128
129 QSettings settings;
130 settings.setValue("crystal/multiColor", multiColor);
131 }
132
133 QWidget* CrystalScene::setupWidget()
134 {
135 if (!m_setupWidget) {
136 m_setupWidget = new QWidget(qobject_cast<QWidget*>(parent()));
137 auto* v = new QVBoxLayout;
138
139 // line width
140 auto* spin = new QDoubleSpinBox;
141 spin->setRange(0.5, 5.0);
142 spin->setSingleStep(0.25);
143 spin->setDecimals(2);
144 spin->setValue(m_lineWidth);
145 connect(spin, SIGNAL(valueChanged(double)), SLOT(setLineWidth(double)));
146 auto* form = new QFormLayout;
147 form->addRow(tr("Line width:"), spin);
148
149 auto* multiColor = new QCheckBox;
150 multiColor->setChecked(m_multiColor);
151 form->addRow(tr("Color axes:"), multiColor);
152 connect(multiColor, SIGNAL(toggled(bool)), SLOT(setMultiColor(bool)));
153
154 auto* color = new QtGui::ColorButton;
155 connect(color, SIGNAL(colorChanged(const QColor&)),
156 SLOT(setColor(const QColor&)));
157 form->addRow(tr("Line color:"), color);
158
159 v->addLayout(form);
160
161 v->addStretch(1);
162 m_setupWidget->setLayout(v);
163 }
164 return m_setupWidget;
165 }
166
167 } // namespace Avogadro::QtPlugins