| 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 "vanderwaals.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/spheregeometry.h> |
| 13 |
|
| 14 |
#include <QtCore/QSettings> |
| 15 |
#include <QtWidgets/QFormLayout> |
| 16 |
#include <QtWidgets/QSlider> |
| 17 |
|
| 18 |
namespace Avogadro::QtPlugins { |
| 19 |
|
| 20 |
using Core::Elements; |
| 21 |
using QtGui::PluginLayerManager; |
| 22 |
using Rendering::GeometryNode; |
| 23 |
using Rendering::GroupNode; |
| 24 |
using Rendering::SphereGeometry; |
| 25 |
|
| 26 |
struct LayerVdW : Core::LayerData |
| 27 |
{ |
| 28 |
QWidget* widget; |
| 29 |
float opacity; |
| 30 |
|
| 31 |
LayerVdW() |
| 32 |
{ |
| 33 |
widget = nullptr; |
| 34 |
QSettings settings; |
| 35 |
opacity = settings.value("vdw/opacity", 1.0).toFloat(); |
| 36 |
} |
| 37 |
|
| 38 |
LayerVdW(std::string settings) |
| 39 |
{ |
| 40 |
widget = nullptr; |
| 41 |
deserialize(settings); |
| 42 |
} |
| 43 |
|
| 44 |
LayerData* clone() final { return new LayerVdW(*this); } |
| 45 |
|
| 46 |
~LayerVdW() override |
| 47 |
{ |
| 48 |
if (widget) |
| 49 |
widget->deleteLater(); |
| 50 |
} |
| 51 |
|
| 52 |
std::string serialize() final { return std::to_string(opacity); } |
| 53 |
void deserialize(std::string text) final |
| 54 |
{ |
| 55 |
std::stringstream ss(text); |
| 56 |
std::string aux; |
| 57 |
ss >> aux; |
| 58 |
opacity = std::stof(aux); |
| 59 |
} |
| 60 |
|
| 61 |
void setupWidget(VanDerWaals* slot) |
| 62 |
{ |
| 63 |
if (!widget) { |
| 64 |
widget = new QWidget(qobject_cast<QWidget*>(slot->parent())); |
| 65 |
auto* form = new QFormLayout; |
| 66 |
|
| 67 |
// Opacity |
| 68 |
auto* slider = new QSlider(Qt::Horizontal); |
| 69 |
slider->setRange(0, 100); |
| 70 |
slider->setTickInterval(1); |
| 71 |
slider->setValue(round(opacity * 100)); |
| 72 |
QObject::connect(slider, &QSlider::valueChanged, slot, |
| 73 |
&VanDerWaals::setOpacity); |
| 74 |
|
| 75 |
form->addRow(QObject::tr("Opacity:"), slider); |
| 76 |
widget->setLayout(form); |
| 77 |
} |
| 78 |
} |
| 79 |
}; |
| 80 |
|
| 81 |
VanDerWaals::VanDerWaals(QObject* p) : ScenePlugin(p) |
| 82 |
{ |
| 83 |
m_layerManager = PluginLayerManager(m_name); |
| 84 |
|
| 85 |
QSettings settings; |
| 86 |
} |
| 87 |
|
| 88 |
VanDerWaals::~VanDerWaals() {} |
| 89 |
|
| 90 |
void VanDerWaals::process(const QtGui::Molecule& molecule, |
| 91 |
Rendering::GroupNode& node) |
| 92 |
{ |
| 93 |
m_layerManager.load<LayerVdW>(); |
| 94 |
|
| 95 |
// Add a sphere node to contain all of the VdW spheres. |
| 96 |
auto* geometry = new GeometryNode; |
| 97 |
node.addChild(geometry); |
| 98 |
auto* spheres = new SphereGeometry; |
| 99 |
spheres->identifier().molecule = &molecule; |
| 100 |
spheres->identifier().type = Rendering::AtomType; |
| 101 |
|
| 102 |
auto* translucentSpheres = new SphereGeometry; |
| 103 |
translucentSpheres->setRenderPass(Rendering::TranslucentPass); |
| 104 |
translucentSpheres->identifier().molecule = &molecule; |
| 105 |
translucentSpheres->identifier().type = Rendering::AtomType; |
| 106 |
|
| 107 |
auto selectedSpheres = new SphereGeometry; |
| 108 |
selectedSpheres->setOpacity(0.42); |
| 109 |
selectedSpheres->setRenderPass(Rendering::TranslucentPass); |
| 110 |
|
| 111 |
geometry->addDrawable(spheres); |
| 112 |
geometry->addDrawable(selectedSpheres); |
| 113 |
geometry->addDrawable(translucentSpheres); |
| 114 |
|
| 115 |
for (Index i = 0; i < molecule.atomCount(); ++i) { |
| 116 |
Core::Atom atom = molecule.atom(i); |
| 117 |
if (!m_layerManager.atomEnabled(i)) { |
| 118 |
continue; |
| 119 |
} |
| 120 |
unsigned char atomicNumber = atom.atomicNumber(); |
| 121 |
|
| 122 |
Vector3ub color = atom.color(); |
| 123 |
auto radius = static_cast<float>(Elements::radiusVDW(atomicNumber)); |
| 124 |
auto* interface = |
| 125 |
m_layerManager.getSetting<LayerVdW>(m_layerManager.getLayerID(i)); |
| 126 |
float opacity = interface->opacity; |
| 127 |
if (opacity < 1.0f) { |
| 128 |
translucentSpheres->addSphere(atom.position3d().cast<float>(), color, |
| 129 |
radius, i); |
| 130 |
translucentSpheres->setOpacity(opacity); |
| 131 |
} else { |
| 132 |
spheres->addSphere(atom.position3d().cast<float>(), color, radius, i); |
| 133 |
} |
| 134 |
|
| 135 |
if (atom.selected()) { |
| 136 |
color = Vector3ub(0, 0, 255); |
| 137 |
radius += 0.3f; |
| 138 |
selectedSpheres->addSphere(atom.position3d().cast<float>(), color, radius, |
| 139 |
i); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
void VanDerWaals::setOpacity(int opacity) |
| 145 |
{ |
| 146 |
m_opacity = static_cast<float>(opacity) / 100.0f; |
| 147 |
auto* interface = m_layerManager.getSetting<LayerVdW>(); |
| 148 |
if (m_opacity != interface->opacity) { |
| 149 |
interface->opacity = m_opacity; |
| 150 |
emit drawablesChanged(); |
| 151 |
} |
| 152 |
|
| 153 |
QSettings settings; |
| 154 |
settings.setValue("vdw/opacity", m_opacity); |
| 155 |
} |
| 156 |
|
| 157 |
QWidget* VanDerWaals::setupWidget() |
| 158 |
{ |
| 159 |
auto* interface = m_layerManager.getSetting<LayerVdW>(); |
| 160 |
interface->setupWidget(this); |
| 161 |
return interface->widget; |
| 162 |
} |
| 163 |
|
| 164 |
} // namespace Avogadro::QtPlugins |