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

Add label editor to create custom labels #740

Merged
merged 5 commits into from Aug 31, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
190 changes: 138 additions & 52 deletions avogadro/qtplugins/label/label.cpp
Expand Up @@ -15,6 +15,7 @@

#include <QtCore/QSettings>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QDoubleSpinBox>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QVBoxLayout>
Expand Down Expand Up @@ -57,19 +58,28 @@ TextLabel3D* createLabel(const std::string& text, const Vector3f& pos,

struct LayerLabel : Core::LayerData
{
enum LabelOptions : char
{
None = 0x00,
Index = 0x01,
Name = 0x02,
Custom = 0x04,
Ordinal = 0x08
};
char atomOptions;
char residueOptions;

QWidget* widget;
bool atomLabel;
bool residueLabel;
float radiusScalar;
Vector3ub color;

LayerLabel()
{
widget = nullptr;
QSettings settings;
atomLabel = settings.value("label/atomLabel", true).toBool();
residueLabel = settings.value("label/residueLabel", false).toBool();
radiusScalar = settings.value("label/radiusScalar", 0.5).toDouble();
atomOptions = char(settings.value("label/atomoptions", 0x02).toInt());
residueOptions = char(settings.value("label/residueoptions", 0x00).toInt());
radiusScalar = settings.value("label/radiusscalar", 0.5).toDouble();

QColor q_color =
settings.value("label/color", QColor(Qt::white)).value<QColor>();
Expand All @@ -86,18 +96,20 @@ struct LayerLabel : Core::LayerData

std::string serialize() override final
{
return boolToString(atomLabel) + " " + boolToString(residueLabel) + " " +
std::to_string(radiusScalar) + " " + std::to_string(color[0]) + " " +
std::to_string(color[1]) + " " + std::to_string(color[2]);
std::string aux = (const char*)atomOptions;
std::string aux2 = (const char*)residueOptions;
return aux + " " + aux2 + " " + 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
{
std::stringstream ss(text);
std::string aux;
ss >> aux;
atomLabel = stringToBool(aux);
atomOptions = aux[0];
ss >> aux;
residueLabel = stringToBool(aux);
residueOptions = aux[0];
ss >> aux;
radiusScalar = std::stof(aux);
ss >> aux;
Expand Down Expand Up @@ -131,19 +143,74 @@ struct LayerLabel : Core::LayerData
SLOT(setRadiusScalar(double)));
form->addRow(QObject::tr("Distance from center:"), spin);

v->addLayout(form);

// residue or atoms?
QCheckBox* check = new QCheckBox(QObject::tr("Atom Labels"));
check->setChecked(atomLabel);
QObject::connect(check, &QCheckBox::clicked, slot, &Label::atomLabel);
v->addWidget(check);

check = new QCheckBox(QObject::tr("Residue Labels"));
check->setChecked(residueLabel);
QObject::connect(check, &QCheckBox::clicked, slot, &Label::residueLabel);
v->addWidget(check);
QComboBox* atom = new QComboBox;
atom->setObjectName("atom");
for (char i = 0x00; i < std::pow(2, 4); ++i) {
if (i == 0) {
atom->addItem(QObject::tr("None"), QVariant(LabelOptions::None));
} else {
char val = 0x00;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just use a string list. I know this works in English, but it won't work for translation in many languages.

QStringList options;
options << tr("None") << tr("Index") << tr("Element Symbol") << tr("Symbol + Index"); // etc

QString text;
if (i & LabelOptions::Custom) {
text = QObject::tr("Custom");
val = LabelOptions::Custom;
}
if (i & LabelOptions::Index) {
text += text == "" ? QObject::tr("Index") : QObject::tr(", In.");
val |= LabelOptions::Index;
}
if (i & LabelOptions::Name) {
text += text == "" ? QObject::tr("Element") : QObject::tr(", El.");
val |= LabelOptions::Name;
}
if (i & LabelOptions::Ordinal) {
text += text == "" ? QObject::tr("Element & Ordinal")
: QObject::tr(", El.&Or.");
val |= LabelOptions::Ordinal;
}
atom->addItem(text, QVariant(val));
if (val == atomOptions) {
atom->setCurrentText(text);
}
}
}
QObject::connect(atom, SIGNAL(currentIndexChanged(int)), slot,
SLOT(atomLabelType(int)));
int index = atom->findData(int(atomOptions));
atom->model()->sort(0, Qt::AscendingOrder);
form->addRow(QObject::tr("Atom label:"), atom);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tr("Atom Label:")


QComboBox* residue = new QComboBox;
residue->setObjectName("residue");
for (char i = 0x00; i < std::pow(2, 2); ++i) {
if (i == 0) {
residue->addItem(QObject::tr("None"), QVariant(LabelOptions::None));
} else {
char val = 0x00;
QString text;
if (i & LabelOptions::Index) {
text = QObject::tr("ID");
val |= LabelOptions::Index;
}
if (i & LabelOptions::Name) {
text += text == "" ? QObject::tr("Name") : QObject::tr(" & Name");
val |= LabelOptions::Name;
}
if (val != 0x00) {
residue->addItem(text, QVariant(val));
if (val == residueOptions) {
residue->setCurrentText(text);
}
}
}
}
QObject::connect(residue, SIGNAL(currentIndexChanged(int)), slot,
SLOT(residueLabelType(int)));
index = residue->findData(int(residueOptions));
residue->model()->sort(0, Qt::AscendingOrder);
form->addRow(QObject::tr("residue label:"), residue);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tr("Residue Label:")


v->addLayout(form);
v->addStretch(1);
widget->setLayout(v);
}
Expand All @@ -162,10 +229,10 @@ void Label::process(const Core::Molecule& molecule, Rendering::GroupNode& node)
m_layerManager.load<LayerLabel>();
for (size_t layer = 0; layer < m_layerManager.layerCount(); ++layer) {
LayerLabel& interface = m_layerManager.getSetting<LayerLabel>(layer);
if (interface.residueLabel) {
if (interface.residueOptions) {
processResidue(molecule, node, layer);
}
if (interface.atomLabel) {
if (interface.atomOptions) {
processAtom(molecule, node, layer);
}
}
Expand All @@ -183,7 +250,7 @@ void Label::processResidue(const Core::Molecule& molecule,
!m_layerManager.atomEnabled(layer, caAtom.index())) {
continue;
}
auto text = residue.residueName();
auto name = residue.residueName();
const auto atoms = residue.residueAtoms();
Vector3f pos = Vector3f::Zero();
for (const auto& atom : atoms) {
Expand All @@ -201,7 +268,15 @@ void Label::processResidue(const Core::Molecule& molecule,
}
}

Vector3ub color = m_layerManager.getSetting<LayerLabel>(layer).color;
auto& interface = m_layerManager.getSetting<LayerLabel>(layer);
Vector3ub color = interface.color;
std::string text = "";
if (interface.residueOptions & LayerLabel::LabelOptions::Index) {
text = std::to_string(residue.residueId());
}
if (interface.residueOptions & LayerLabel::LabelOptions::Name) {
text += (text == "" ? "" : " / ") + name;
}
TextLabel3D* residueLabel = createLabel(text, pos, radius, color);
geometry->addDrawable(residueLabel);
}
Expand All @@ -228,19 +303,32 @@ void Label::processAtom(const Core::Molecule& molecule,
continue;
}

auto text = atom.label();
if (text == "") {
text = Elements::symbol(atomicNumber) +
std::to_string(atomCount[atomicNumber]);
}
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, color);
geometry->addDrawable(atomLabel);
std::string text = "";
if (interface.atomOptions & LayerLabel::LabelOptions::Custom) {
text += (text == "" ? "" : " / ") + atom.label();
}
if (interface.atomOptions & LayerLabel::LabelOptions::Index) {
text += (text == "" ? "" : " / ") + std::to_string(atom.index());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

atom.index() + 1 // most users expect Atom 1, Atom 2, etc. rather than starting at zero...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but the "Select by Atom Index" is indexed at 0. I can change both elements to indexed at 1 if you prefer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think most users expect atom numbers to start at 1.

}
if (interface.atomOptions & LayerLabel::LabelOptions::Name) {
text +=
(text == "" ? "" : " / ") + std::string(Elements::symbol(atomicNumber));
}
if (interface.atomOptions & LayerLabel::LabelOptions::Ordinal) {
text += (text == "" ? "" : " / ") +
std::string(Elements::symbol(atomicNumber) +
std::to_string(atomCount[atomicNumber]));
}
if (text != "") {
const Vector3f pos(atom.position3d().cast<float>());
Vector3ub color = interface.color;
float radius = static_cast<float>(Elements::radiusVDW(atomicNumber)) *
interface.radiusScalar;

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

Expand All @@ -258,26 +346,24 @@ void Label::setColor(const QColor& color)
settings.setValue("label/color", color);
}

void Label::atomLabel(bool show)
void Label::atomLabelType(int index)
{
LayerLabel& interface = m_layerManager.getSetting<LayerLabel>();
if (show != interface.atomLabel) {
interface.atomLabel = show;
emit drawablesChanged();
}
QSettings settings;
settings.setValue("label/atomLabel", show);
interface.atomOptions = char(setupWidget()
->findChildren<QComboBox*>("atom")[0]
->itemData(index)
.toInt());
emit drawablesChanged();
}

void Label::residueLabel(bool show)
void Label::residueLabelType(int index)
{
LayerLabel& interface = m_layerManager.getSetting<LayerLabel>();
if (show != interface.residueLabel) {
interface.residueLabel = show;
emit drawablesChanged();
}
QSettings settings;
settings.setValue("label/residueLabel", show);
interface.residueOptions = char(setupWidget()
->findChildren<QComboBox*>("residue")[0]
->itemData(index)
.toInt());
emit drawablesChanged();
}

void Label::setRadiusScalar(double radius)
Expand Down
7 changes: 4 additions & 3 deletions avogadro/qtplugins/label/label.h
Expand Up @@ -30,11 +30,12 @@ class Label : public QtGui::ScenePlugin
}

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

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

Expand Down