From 039e5ac3d6c1d8b215c8291c74a95c6dff614dd6 Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Fri, 15 Nov 2024 13:35:17 -0500 Subject: [PATCH] Ensure VdW settings can be changed for each layer Signed-off-by: Geoff Hutchison --- .../qtplugins/vanderwaals/vanderwaals.cpp | 104 ++++++++++++++---- avogadro/qtplugins/vanderwaals/vanderwaals.h | 2 +- 2 files changed, 82 insertions(+), 24 deletions(-) diff --git a/avogadro/qtplugins/vanderwaals/vanderwaals.cpp b/avogadro/qtplugins/vanderwaals/vanderwaals.cpp index 5f4ed384a1..2511a6a9e8 100644 --- a/avogadro/qtplugins/vanderwaals/vanderwaals.cpp +++ b/avogadro/qtplugins/vanderwaals/vanderwaals.cpp @@ -23,13 +23,66 @@ using Rendering::GeometryNode; using Rendering::GroupNode; using Rendering::SphereGeometry; +struct LayerVdW : Core::LayerData +{ + QWidget* widget; + float opacity; + + LayerVdW() + { + widget = nullptr; + QSettings settings; + opacity = settings.value("vdw/opacity", 1.0).toFloat(); + } + + LayerVdW(std::string settings) + { + widget = nullptr; + deserialize(settings); + } + + LayerData* clone() final { return new LayerVdW(*this); } + + ~LayerVdW() override + { + if (widget) + widget->deleteLater(); + } + + std::string serialize() final { return std::to_string(opacity); } + void deserialize(std::string text) final + { + std::stringstream ss(text); + std::string aux; + ss >> aux; + opacity = std::stof(aux); + } + + void setupWidget(VanDerWaals* slot) + { + if (!widget) { + widget = new QWidget(qobject_cast(slot->parent())); + auto* form = new QFormLayout; + + // Opacity + auto* slider = new QSlider(Qt::Horizontal); + slider->setRange(0, 100); + slider->setTickInterval(1); + slider->setValue(round(opacity * 100)); + QObject::connect(slider, &QSlider::valueChanged, slot, + &VanDerWaals::setOpacity); + + form->addRow(QObject::tr("Opacity:"), slider); + widget->setLayout(form); + } + } +}; + VanDerWaals::VanDerWaals(QObject* p) : ScenePlugin(p) { m_layerManager = PluginLayerManager(m_name); QSettings settings; - // out of 255 - m_opacity = settings.value("vdw/opacity", 1.0).toFloat(); } VanDerWaals::~VanDerWaals() {} @@ -37,15 +90,19 @@ VanDerWaals::~VanDerWaals() {} void VanDerWaals::process(const QtGui::Molecule& molecule, Rendering::GroupNode& node) { + m_layerManager.load(); + // Add a sphere node to contain all of the VdW spheres. auto* geometry = new GeometryNode; node.addChild(geometry); auto* spheres = new SphereGeometry; spheres->identifier().molecule = &molecule; spheres->identifier().type = Rendering::AtomType; - spheres->setOpacity(m_opacity); - if (m_opacity < 1.0f) - spheres->setRenderPass(Rendering::TranslucentPass); + + auto* translucentSpheres = new SphereGeometry; + translucentSpheres->setRenderPass(Rendering::TranslucentPass); + translucentSpheres->identifier().molecule = &molecule; + translucentSpheres->identifier().type = Rendering::AtomType; auto selectedSpheres = new SphereGeometry; selectedSpheres->setOpacity(0.42); @@ -53,6 +110,7 @@ void VanDerWaals::process(const QtGui::Molecule& molecule, geometry->addDrawable(spheres); geometry->addDrawable(selectedSpheres); + geometry->addDrawable(translucentSpheres); for (Index i = 0; i < molecule.atomCount(); ++i) { Core::Atom atom = molecule.atom(i); @@ -63,7 +121,15 @@ void VanDerWaals::process(const QtGui::Molecule& molecule, Vector3ub color = atom.color(); auto radius = static_cast(Elements::radiusVDW(atomicNumber)); - spheres->addSphere(atom.position3d().cast(), color, radius, i); + float opacity = m_layerManager.getSetting(i)->opacity; + if (opacity < 1.0f) { + translucentSpheres->addSphere(atom.position3d().cast(), color, + radius, i); + translucentSpheres->setOpacity(opacity); + } else { + spheres->addSphere(atom.position3d().cast(), color, radius, i); + } + if (atom.selected()) { color = Vector3ub(0, 0, 255); radius += 0.3f; @@ -75,8 +141,12 @@ void VanDerWaals::process(const QtGui::Molecule& molecule, void VanDerWaals::setOpacity(int opacity) { - m_opacity = opacity / 100.0f; - emit drawablesChanged(); + m_opacity = static_cast(opacity) / 100.0f; + auto* interface = m_layerManager.getSetting(); + if (m_opacity != interface->opacity) { + interface->opacity = m_opacity; + emit drawablesChanged(); + } QSettings settings; settings.setValue("vdw/opacity", m_opacity); @@ -84,21 +154,9 @@ void VanDerWaals::setOpacity(int opacity) QWidget* VanDerWaals::setupWidget() { - if (!m_setupWidget) { - m_setupWidget = new QWidget(qobject_cast(parent())); - auto* form = new QFormLayout; - - // Opacity - auto* slide = new QSlider(Qt::Horizontal); - slide->setRange(0, 100); - slide->setTickInterval(1); - slide->setValue(round(m_opacity * 100)); - connect(slide, SIGNAL(valueChanged(int)), SLOT(setOpacity(int))); - - form->addRow(tr("Opacity:"), slide); - m_setupWidget->setLayout(form); - } - return m_setupWidget; + auto* interface = m_layerManager.getSetting(); + interface->setupWidget(this); + return interface->widget; } } // namespace Avogadro::QtPlugins diff --git a/avogadro/qtplugins/vanderwaals/vanderwaals.h b/avogadro/qtplugins/vanderwaals/vanderwaals.h index 0d25295045..02e3cd8064 100644 --- a/avogadro/qtplugins/vanderwaals/vanderwaals.h +++ b/avogadro/qtplugins/vanderwaals/vanderwaals.h @@ -41,7 +41,7 @@ class VanDerWaals : public QtGui::ScenePlugin QWidget* setupWidget() override; bool hasSetupWidget() const override { return true; } -private slots: +public slots: void setOpacity(int opacity); private: