Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow semi-transparent ball-and-sticks and vdw rendering #1783

Merged
merged 6 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions avogadro/qtplugins/ballandstick/ballandstick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
bool showHydrogens;
float atomScale;
float bondRadius;
float opacity;

LayerBallAndStick()
{
Expand All @@ -46,6 +47,7 @@
bondRadius = settings.value("ballandstick/bondRadius", 0.1).toDouble();
multiBonds = settings.value("ballandstick/multiBonds", true).toBool();
showHydrogens = settings.value("ballandstick/showHydrogens", true).toBool();
opacity = settings.value("ballandstick/opacity", 1.0).toDouble();
}

~LayerBallAndStick() override
Expand All @@ -57,7 +59,8 @@
std::string serialize() final
{
return boolToString(multiBonds) + " " + boolToString(showHydrogens) + " " +
std::to_string(atomScale) + " " + std::to_string(bondRadius);
std::to_string(atomScale) + " " + std::to_string(bondRadius) + " " +
std::to_string(opacity);
}

void deserialize(std::string text) final
Expand All @@ -72,6 +75,9 @@
atomScale = std::stof(aux);
ss >> aux;
bondRadius = std::stof(aux);
ss >> aux;
if (!aux.empty())
opacity = std::stof(aux); // backwards compatibility
}

void setupWidget(BallAndStick* slot)
Expand All @@ -98,6 +104,16 @@
QObject::connect(bondRadiusSlider, &QSlider::valueChanged, slot,
&BallAndStick::bondRadiusChanged);
f->addRow(QObject::tr("Bond scale"), bondRadiusSlider);

auto* opacitySlider = new QSlider(Qt::Horizontal);
opacitySlider->setMinimum(0);
opacitySlider->setMaximum(100);
opacitySlider->setTickInterval(1);
opacitySlider->setValue(static_cast<int>(opacity * 100));
QObject::connect(opacitySlider, &QSlider::valueChanged, slot,
&BallAndStick::opacityChanged);
f->addRow(QObject::tr("Opacity"), opacitySlider);

v->addLayout(f);

auto* check = new QCheckBox(QObject::tr("Show multiple bonds"));
Expand Down Expand Up @@ -134,11 +150,15 @@
auto* geometry = new GeometryNode;
node.addChild(geometry);
auto* spheres = new SphereGeometry;
auto selectedSpheres = new SphereGeometry;
selectedSpheres->setOpacity(0.42);
spheres->identifier().molecule = reinterpret_cast<const void*>(&molecule);
spheres->identifier().type = Rendering::AtomType;
spheres->setOpacity(m_layerManager.getSetting<LayerBallAndStick>().opacity);
if (m_layerManager.getSetting<LayerBallAndStick>().opacity < 1.0f)
spheres->setRenderPass(Rendering::TranslucentPass);
geometry->addDrawable(spheres);

auto selectedSpheres = new SphereGeometry;
selectedSpheres->setOpacity(0.42);
geometry->addDrawable(selectedSpheres);

for (Index i = 0; i < molecule.atomCount(); ++i) {
Expand Down Expand Up @@ -168,6 +188,9 @@
auto* cylinders = new CylinderGeometry;
cylinders->identifier().molecule = &molecule;
cylinders->identifier().type = Rendering::BondType;
cylinders->setOpacity(m_layerManager.getSetting<LayerBallAndStick>().opacity);
if (m_layerManager.getSetting<LayerBallAndStick>().opacity < 1.0f)
cylinders->setRenderPass(Rendering::TranslucentPass);
geometry->addDrawable(cylinders);
for (Index i = 0; i < molecule.bondCount(); ++i) {
Core::Bond bond = molecule.bond(i);
Expand Down Expand Up @@ -237,6 +260,19 @@
return interface.widget;
}

void BallAndStick::opacityChanged(int opacity)
{
m_opacity = static_cast<float>(opacity) / 100.0f;
auto& interface = m_layerManager.getSetting<LayerBallAndStick>();
if (m_opacity != interface.opacity) {
Fixed Show fixed Hide fixed
interface.opacity = m_opacity;
emit drawablesChanged();
}

QSettings settings;
settings.setValue("ballandstick/opacity", m_opacity);
}

void BallAndStick::atomRadiusChanged(int value)
{
m_atomScale = static_cast<float>(value) / 10.0f;
Expand Down
2 changes: 2 additions & 0 deletions avogadro/qtplugins/ballandstick/ballandstick.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ public slots:
void bondRadiusChanged(int value);
void multiBonds(bool show);
void showHydrogens(bool show);
void opacityChanged(int value);

private:
Rendering::GroupNode* m_group;
std::string m_name = "Ball and Stick";
float m_atomScale = 0.3f;
float m_bondRadius = 0.1f;
float m_opacity = 1.0f;
};

} // end namespace QtPlugins
Expand Down
43 changes: 42 additions & 1 deletion avogadro/qtplugins/vanderwaals/vanderwaals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
#include <avogadro/rendering/groupnode.h>
#include <avogadro/rendering/spheregeometry.h>

#include <QtCore/QSettings>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QSlider>

namespace Avogadro::QtPlugins {

using Core::Elements;
Expand All @@ -22,6 +26,10 @@ using Rendering::SphereGeometry;
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() {}
Expand All @@ -35,8 +43,13 @@ void VanDerWaals::process(const QtGui::Molecule& molecule,
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 selectedSpheres = new SphereGeometry;
selectedSpheres->setOpacity(0.42);
selectedSpheres->setRenderPass(Rendering::TranslucentPass);

geometry->addDrawable(spheres);
geometry->addDrawable(selectedSpheres);
Expand All @@ -60,4 +73,32 @@ void VanDerWaals::process(const QtGui::Molecule& molecule,
}
}

} // namespace Avogadro
void VanDerWaals::setOpacity(int opacity)
{
m_opacity = opacity / 100.0f;
emit drawablesChanged();

QSettings settings;
settings.setValue("vdw/opacity", m_opacity);
}

QWidget* VanDerWaals::setupWidget()
{
if (!m_setupWidget) {
m_setupWidget = new QWidget(qobject_cast<QWidget*>(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;
}

} // namespace Avogadro::QtPlugins
8 changes: 8 additions & 0 deletions avogadro/qtplugins/vanderwaals/vanderwaals.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@ class VanDerWaals : public QtGui::ScenePlugin
return DefaultBehavior::False;
}

QWidget* setupWidget() override;
bool hasSetupWidget() const override { return true; }

private slots:
void setOpacity(int opacity);

private:
std::string m_name = "Van der Waals";
QWidget* m_setupWidget = nullptr;
float m_opacity = 1.0;
};
} // namespace QtPlugins
} // namespace Avogadro
Expand Down
9 changes: 0 additions & 9 deletions avogadro/qtplugins/vanderwaalsao/CMakeLists.txt

This file was deleted.

54 changes: 0 additions & 54 deletions avogadro/qtplugins/vanderwaalsao/vanderwaalsao.cpp

This file was deleted.

47 changes: 0 additions & 47 deletions avogadro/qtplugins/vanderwaalsao/vanderwaalsao.h

This file was deleted.

Loading
Loading