Skip to content

Commit

Permalink
Merge pull request #730 from ghutchis/add-label-color
Browse files Browse the repository at this point in the history
Add a color button to customize labels
  • Loading branch information
ghutchis authored Aug 14, 2021
2 parents cd5aa6e + e459650 commit 677c786
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 9 deletions.
50 changes: 44 additions & 6 deletions avogadro/qtplugins/label/label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <avogadro/core/elements.h>
#include <avogadro/core/molecule.h>
#include <avogadro/core/residue.h>
#include <avogadro/qtgui/colorbutton.h>
#include <avogadro/rendering/geometrynode.h>
#include <avogadro/rendering/scene.h>
#include <avogadro/rendering/textlabel3d.h>
Expand Down Expand Up @@ -36,14 +37,14 @@ typedef Array<Molecule::BondType> NeighborListType;

namespace {
TextLabel3D* createLabel(const std::string& text, const Vector3f& pos,
float radius)
float radius, const Vector3ub& color)
{
Rendering::TextProperties tprop;
tprop.setAlign(Rendering::TextProperties::HCenter,
Rendering::TextProperties::VCenter);
tprop.setFontFamily(Rendering::TextProperties::SansSerif);

tprop.setColorRgb(255, 255, 255);
tprop.setColorRgb(color[0], color[1], color[2]);
TextLabel3D* label = new TextLabel3D;
label->setText(text);
label->setRenderPass(Rendering::OpaquePass);
Expand All @@ -60,6 +61,7 @@ struct LayerLabel : Core::LayerData
bool atomLabel;
bool residueLabel;
float radiusScalar;
Vector3ub color;

LayerLabel()
{
Expand All @@ -68,6 +70,12 @@ struct LayerLabel : Core::LayerData
atomLabel = settings.value("label/atomLabel", true).toBool();
residueLabel = settings.value("label/residueLabel", false).toBool();
radiusScalar = settings.value("label/radiusScalar", 0.5).toDouble();

QColor q_color =
settings.value("label/color", QColor(Qt::white)).value<QColor>();
color[0] = static_cast<unsigned char>(q_color.red());
color[1] = static_cast<unsigned char>(q_color.green());
color[2] = static_cast<unsigned char>(q_color.blue());
}

~LayerLabel()
Expand All @@ -79,7 +87,8 @@ struct LayerLabel : Core::LayerData
std::string serialize() override final
{
return boolToString(atomLabel) + " " + boolToString(residueLabel) + " " +
std::to_string(radiusScalar);
std::to_string(radiusScalar) + " " + std::to_string(color[0])
+ " " + std::to_string(color[1]) + " " + std::to_string(color[2]);
}
void deserialize(std::string text) override final
{
Expand All @@ -91,6 +100,12 @@ struct LayerLabel : Core::LayerData
residueLabel = stringToBool(aux);
ss >> aux;
radiusScalar = std::stof(aux);
ss >> aux;
color[0] = std::stoi(aux);
ss >> aux;
color[1] = std::stoi(aux);
ss >> aux;
color[2] = std::stoi(aux);
}

void setupWidget(Label* slot)
Expand All @@ -99,6 +114,13 @@ struct LayerLabel : Core::LayerData
widget = new QWidget(qobject_cast<QWidget*>(slot->parent()));
QVBoxLayout* v = new QVBoxLayout;

QFormLayout* form = new QFormLayout;
// color button
QtGui::ColorButton* color = new QtGui::ColorButton;
QObject::connect(color, SIGNAL(colorChanged(const QColor&)), slot,
SLOT(setColor(const QColor&)));
form->addRow(QObject::tr("Color:"), color);

// radius scalar
QDoubleSpinBox* spin = new QDoubleSpinBox;
spin->setRange(0.0, 1.5);
Expand All @@ -107,8 +129,8 @@ struct LayerLabel : Core::LayerData
spin->setValue(radiusScalar);
QObject::connect(spin, SIGNAL(valueChanged(double)), slot,
SLOT(setRadiusScalar(double)));
QFormLayout* form = new QFormLayout;
form->addRow(QObject::tr("Distance from center:"), spin);

v->addLayout(form);

// residue or atoms?
Expand Down Expand Up @@ -179,7 +201,8 @@ void Label::processResidue(const Core::Molecule& molecule,
}
}

TextLabel3D* residueLabel = createLabel(text, pos, radius);
Vector3ub color = m_layerManager.getSetting<LayerLabel>(layer).color;
TextLabel3D* residueLabel = createLabel(text, pos, radius, color);
geometry->addDrawable(residueLabel);
}
}
Expand Down Expand Up @@ -212,14 +235,29 @@ void Label::processAtom(const Core::Molecule& molecule,
}
const Vector3f pos(atom.position3d().cast<float>());
LayerLabel& interface = m_layerManager.getSetting<LayerLabel>(layer);
Vector3ub color = interface.color;
float radius = static_cast<float>(Elements::radiusVDW(atomicNumber)) *
interface.radiusScalar;

TextLabel3D* atomLabel = createLabel(text, pos, radius);
TextLabel3D* atomLabel = createLabel(text, pos, radius, color);
geometry->addDrawable(atomLabel);
}
}

void Label::setColor(const QColor& color)
{
LayerLabel& interface = m_layerManager.getSetting<LayerLabel>();

interface.color[0] = static_cast<unsigned char>(color.red());
interface.color[1] = static_cast<unsigned char>(color.green());
interface.color[2] = static_cast<unsigned char>(color.blue());

emit drawablesChanged();

QSettings settings;
settings.setValue("label/color", color);
}

void Label::atomLabel(bool show)
{
LayerLabel& interface = m_layerManager.getSetting<LayerLabel>();
Expand Down
8 changes: 5 additions & 3 deletions avogadro/qtplugins/label/label.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#ifndef AVOGADRO_QTPLUGINS_LABEL_H
#define AVOGADRO_QTPLUGINS_LABEL_H

#include <avogadro/core/vector.h>
#include <avogadro/qtgui/sceneplugin.h>

namespace Avogadro {
namespace QtPlugins {

Expand All @@ -22,20 +22,21 @@ class Label : public QtGui::ScenePlugin
explicit Label(QObject* parent = nullptr);
~Label() override;

QString name() const override { return tr(m_name.c_str()); }
QString name() const override { return tr("Labels"); }

QString description() const override
{
return tr("Display labels on ball and stick style.");
}

QWidget* setupWidget() override;
void process(const Core::Molecule& molecule, Rendering::GroupNode& node);
void process(const Core::Molecule& molecule, Rendering::GroupNode& node) override;

public slots:
void atomLabel(bool show);
void residueLabel(bool show);
void setRadiusScalar(double radius);
void setColor(const QColor& color);

private:
void processAtom(const Core::Molecule& molecule, Rendering::GroupNode& node,
Expand All @@ -45,6 +46,7 @@ public slots:

Rendering::GroupNode* m_group;
std::string m_name = "Labels";
Vector3ub m_color;
};

} // end namespace QtPlugins
Expand Down

0 comments on commit 677c786

Please sign in to comment.