Skip to content

Commit

Permalink
Some progress, but can't set per-layer settings?
Browse files Browse the repository at this point in the history
Signed-off-by: Geoff Hutchison <[email protected]>
  • Loading branch information
ghutchis committed Nov 14, 2024
1 parent 313204f commit a37e2f5
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 130 deletions.
16 changes: 13 additions & 3 deletions avogadro/qtgui/pluginlayermanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

#include <avogadro/core/layermanager.h>
#include <cassert>
#include <iostream>

namespace Avogadro {
namespace QtGui {
Expand Down Expand Up @@ -79,22 +80,31 @@ class AVOGADROQTGUI_EXPORT PluginLayerManager : protected Core::LayerManager
/** @return custom data T derived from LayerData. if @p layer is equal to
* MaxIndex returns activeLayer */
template <typename T>
T& getSetting(size_t layer = MaxIndex)
T* getSetting(size_t layer = MaxIndex)
{
auto info = m_molToInfo[m_activeMolecule];

if (layer == MaxIndex) {
layer = info->layer.activeLayer();
}

// std::cout << "name: " << m_name << " layer " << layer << " " <<
// info->layer.activeLayer() << std::endl;

assert(layer <= info->layer.maxLayer());
if (info->settings.find(m_name) == info->settings.end()) {
std::cout << " hit the end? " << std::endl;
info->settings[m_name] = Core::Array<Core::LayerData*>();
}

// do we need to create new layers in the array?
while (info->settings[m_name].size() < layer + 1) {
std::cout << " loop " << info->settings[m_name].size() << " " << layer
<< std::endl;
info->settings[m_name].push_back(new T());
}
auto result = static_cast<T*>(info->settings[m_name][layer]);
return *result;
auto* result = static_cast<T*>(info->settings[m_name][layer]);
return result;
}

private:
Expand Down
102 changes: 68 additions & 34 deletions avogadro/qtplugins/ballandstick/ballandstick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <QtWidgets/QVBoxLayout>
#include <QtWidgets/QWidget>

#include <iostream>

namespace Avogadro::QtPlugins {

using Core::Elements;
Expand All @@ -43,6 +45,8 @@ struct LayerBallAndStick : Core::LayerData
{
widget = nullptr;
QSettings settings;
std::cout << " new settings " << std::endl;

atomScale = settings.value("ballandstick/atomScale", 0.3).toDouble();
bondRadius = settings.value("ballandstick/bondRadius", 0.1).toDouble();
multiBonds = settings.value("ballandstick/multiBonds", true).toBool();
Expand All @@ -58,13 +62,17 @@ struct LayerBallAndStick : Core::LayerData

std::string serialize() final
{
std::cout << " serialize " << std::endl;

return boolToString(multiBonds) + " " + boolToString(showHydrogens) + " " +
std::to_string(atomScale) + " " + std::to_string(bondRadius) + " " +
std::to_string(opacity);
}

void deserialize(std::string text) final
{
std::cout << " deserialize " << text << std::endl;

std::stringstream ss(text);
std::string aux;
ss >> aux;
Expand Down Expand Up @@ -152,11 +160,18 @@ void BallAndStick::process(const QtGui::Molecule& molecule,
auto* spheres = new SphereGeometry;
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);

// if we have to draw any translucent spheres, we need to add a separate
// geometry node for them
auto translucentSpheres = new SphereGeometry;
translucentSpheres->setRenderPass(Rendering::TranslucentPass);
translucentSpheres->identifier().molecule =
reinterpret_cast<const void*>(&molecule);
translucentSpheres->identifier().type = Rendering::AtomType;
geometry->addDrawable(translucentSpheres);

// for the selected atoms
auto selectedSpheres = new SphereGeometry;
selectedSpheres->setOpacity(0.42);
geometry->addDrawable(selectedSpheres);
Expand All @@ -167,17 +182,28 @@ void BallAndStick::process(const QtGui::Molecule& molecule,
continue;
}
unsigned char atomicNumber = atom.atomicNumber();
auto& interface = m_layerManager.getSetting<LayerBallAndStick>(

auto* interface = m_layerManager.getSetting<LayerBallAndStick>(
m_layerManager.getLayerID(i));
if (atomicNumber == 1 && !interface.showHydrogens)
if (atomicNumber == 1 && !interface->showHydrogens)
continue;

Vector3ub color = atom.color();
auto radius = static_cast<float>(Elements::radiusVDW(atomicNumber));
float scale = interface.atomScale;
spheres->addSphere(atom.position3d().cast<float>(), color, radius * scale,
i);
float scale = interface->atomScale;
std::cout << " atom " << i << " radius " << radius << " scale " << scale
<< " opacity " << interface->opacity << std::endl;

if (interface->opacity < 1.0f) {
translucentSpheres->addSphere(atom.position3d().cast<float>(), color,
radius * scale, i);
translucentSpheres->setOpacity(interface->opacity);
} else
spheres->addSphere(atom.position3d().cast<float>(), color, radius * scale,
i);

if (atom.selected()) {
// add the selected indicator
color = Vector3ub(0, 0, 255);
radius *= 1.2;
selectedSpheres->addSphere(atom.position3d().cast<float>(), color,
Expand All @@ -188,29 +214,33 @@ void BallAndStick::process(const QtGui::Molecule& molecule,
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);

auto* translucentBonds = new CylinderGeometry;
translucentBonds->setRenderPass(Rendering::TranslucentPass);
translucentBonds->identifier().molecule = &molecule;
translucentBonds->identifier().type = Rendering::BondType;
geometry->addDrawable(translucentBonds);

for (Index i = 0; i < molecule.bondCount(); ++i) {
Core::Bond bond = molecule.bond(i);
if (!m_layerManager.bondEnabled(bond.atom1().index(),
bond.atom2().index())) {
continue;
}

auto& interface1 = m_layerManager.getSetting<LayerBallAndStick>(
auto* interface1 = m_layerManager.getSetting<LayerBallAndStick>(
m_layerManager.getLayerID(bond.atom1().index()));
auto& interface2 = m_layerManager.getSetting<LayerBallAndStick>(
auto* interface2 = m_layerManager.getSetting<LayerBallAndStick>(
m_layerManager.getLayerID(bond.atom2().index()));

if (!interface1.showHydrogens && !interface2.showHydrogens &&
if (!interface1->showHydrogens && !interface2->showHydrogens &&
(bond.atom1().atomicNumber() == 1 ||
bond.atom2().atomicNumber() == 1)) {
continue;
}

float bondRadius = (interface1.bondRadius + interface2.bondRadius) * 0.5f;
float bondRadius = (interface1->bondRadius + interface2->bondRadius) * 0.5f;

Vector3f pos1 = bond.atom1().position3d().cast<float>();
Vector3f pos2 = bond.atom2().position3d().cast<float>();
Expand All @@ -220,7 +250,8 @@ void BallAndStick::process(const QtGui::Molecule& molecule,
float bondLength = bondVector.norm();
bondVector /= bondLength;

switch (interface1.multiBonds || interface2.multiBonds ? bond.order() : 1) {
switch (interface1->multiBonds || interface2->multiBonds ? bond.order()
: 1) {
case 3: {
Vector3f delta = bondVector.unitOrthogonal();
// Rotate 45 degrees around the bond vector.
Expand Down Expand Up @@ -255,17 +286,20 @@ void BallAndStick::process(const QtGui::Molecule& molecule,

QWidget* BallAndStick::setupWidget()
{
auto& interface = m_layerManager.getSetting<LayerBallAndStick>();
interface.setupWidget(this);
return interface.widget;
auto* interface = m_layerManager.getSetting<LayerBallAndStick>();
interface->setupWidget(this);
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) {
interface.opacity = m_opacity;
auto* interface = m_layerManager.getSetting<LayerBallAndStick>();
if (m_opacity != interface->opacity) {

Check notice

Code scanning / CodeQL

Equality test on floating-point values Note

Equality checks on floating point values can yield unexpected results.
std::cout << " previous opacity " << interface->opacity << " new opacity "
<< m_opacity << std::endl;

interface->opacity = m_opacity;
emit drawablesChanged();
}

Expand All @@ -277,9 +311,9 @@ void BallAndStick::atomRadiusChanged(int value)
{
m_atomScale = static_cast<float>(value) / 10.0f;

auto& interface = m_layerManager.getSetting<LayerBallAndStick>();
if (m_atomScale != interface.atomScale) {
interface.atomScale = m_atomScale;
auto* interface = m_layerManager.getSetting<LayerBallAndStick>();
if (m_atomScale != interface->atomScale) {

Check notice

Code scanning / CodeQL

Equality test on floating-point values Note

Equality checks on floating point values can yield unexpected results.
interface->atomScale = m_atomScale;
emit drawablesChanged();
}

Expand All @@ -291,9 +325,9 @@ void BallAndStick::bondRadiusChanged(int value)
{
m_bondRadius = static_cast<float>(value) / 10.0f;

auto& interface = m_layerManager.getSetting<LayerBallAndStick>();
if (m_bondRadius != interface.bondRadius) {
interface.bondRadius = m_bondRadius;
auto* interface = m_layerManager.getSetting<LayerBallAndStick>();
if (m_bondRadius != interface->bondRadius) {

Check notice

Code scanning / CodeQL

Equality test on floating-point values Note

Equality checks on floating point values can yield unexpected results.
interface->bondRadius = m_bondRadius;
emit drawablesChanged();
}

Expand All @@ -303,9 +337,9 @@ void BallAndStick::bondRadiusChanged(int value)

void BallAndStick::multiBonds(bool show)
{
auto& interface = m_layerManager.getSetting<LayerBallAndStick>();
if (show != interface.multiBonds) {
interface.multiBonds = show;
auto* interface = m_layerManager.getSetting<LayerBallAndStick>();
if (show != interface->multiBonds) {
interface->multiBonds = show;
emit drawablesChanged();
}
QSettings settings;
Expand All @@ -314,9 +348,9 @@ void BallAndStick::multiBonds(bool show)

void BallAndStick::showHydrogens(bool show)
{
auto& interface = m_layerManager.getSetting<LayerBallAndStick>();
if (show != interface.showHydrogens) {
interface.showHydrogens = show;
auto* interface = m_layerManager.getSetting<LayerBallAndStick>();
if (show != interface->showHydrogens) {
interface->showHydrogens = show;
emit drawablesChanged();
}
QSettings settings;
Expand Down
Loading

0 comments on commit a37e2f5

Please sign in to comment.