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

Labels #715

Merged
merged 4 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 15 additions & 0 deletions avogadro/core/atom.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ class AtomTemplate
*/
void setForceVector(const Vector3& force);
Vector3 forceVector() const;

void setLabel(const std::string& label);
std::string label() const;
/** @} */

private:
Expand Down Expand Up @@ -365,6 +368,18 @@ Vector3 AtomTemplate<Molecule_T>::forceVector() const
: Vector3::Zero();
}

template <class Molecule_T>
void AtomTemplate<Molecule_T>::setLabel(const std::string& label)
{
m_molecule->setLabel(m_index, label);
}

template <class Molecule_T>
std::string AtomTemplate<Molecule_T>::label() const
{
return m_molecule->label(m_index);
}

} // namespace Core
} // namespace Avogadro

Expand Down
8 changes: 6 additions & 2 deletions avogadro/core/molecule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ Molecule::Molecule()
Molecule::Molecule(const Molecule& other)
: m_data(other.m_data), m_customElementMap(other.m_customElementMap),
m_positions2d(other.m_positions2d), m_positions3d(other.m_positions3d),
m_coordinates3d(other.m_coordinates3d), m_timesteps(other.m_timesteps),
m_hybridizations(other.m_hybridizations),
m_label(other.m_label), m_coordinates3d(other.m_coordinates3d),
m_timesteps(other.m_timesteps), m_hybridizations(other.m_hybridizations),
m_formalCharges(other.m_formalCharges), m_colors(other.m_colors),
m_vibrationFrequencies(other.m_vibrationFrequencies),
m_vibrationIntensities(other.m_vibrationIntensities),
Expand Down Expand Up @@ -63,6 +63,7 @@ Molecule::Molecule(Molecule&& other) noexcept
m_customElementMap(std::move(other.m_customElementMap)),
m_positions2d(std::move(other.m_positions2d)),
m_positions3d(std::move(other.m_positions3d)),
m_label(std::move(other.m_label)),
m_coordinates3d(std::move(other.m_coordinates3d)),
m_timesteps(std::move(other.m_timesteps)),
m_hybridizations(std::move(other.m_hybridizations)),
Expand Down Expand Up @@ -94,6 +95,7 @@ Molecule& Molecule::operator=(const Molecule& other)
m_customElementMap = other.m_customElementMap;
m_positions2d = other.m_positions2d;
m_positions3d = other.m_positions3d;
m_label = other.m_label;
m_coordinates3d = other.m_coordinates3d;
m_timesteps = other.m_timesteps;
m_hybridizations = other.m_hybridizations;
Expand Down Expand Up @@ -144,6 +146,7 @@ Molecule& Molecule::operator=(Molecule&& other) noexcept
m_customElementMap = std::move(other.m_customElementMap);
m_positions2d = std::move(other.m_positions2d);
m_positions3d = std::move(other.m_positions3d);
m_label = std::move(other.m_label);
m_coordinates3d = std::move(other.m_coordinates3d);
m_timesteps = std::move(other.m_timesteps);
m_hybridizations = std::move(other.m_hybridizations);
Expand Down Expand Up @@ -397,6 +400,7 @@ void Molecule::clearAtoms()
{
m_positions2d.clear();
m_positions3d.clear();
m_label.clear();
m_hybridizations.clear();
m_formalCharges.clear();
m_colors.clear();
Expand Down
30 changes: 30 additions & 0 deletions avogadro/core/molecule.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,10 @@ class AVOGADROCORE_EXPORT Molecule
*/
bool setAtomPosition3d(Index atomId, const Vector3& pos);

std::string label(Index atomId) const;
bool setLabel(const Core::Array<std::string>& label);
bool setLabel(Index atomId, const std::string& label);

/**
* Set whether the specified atom is selected or not.
*/
Expand Down Expand Up @@ -664,6 +668,7 @@ class AVOGADROCORE_EXPORT Molecule
CustomElementMap m_customElementMap;
Array<Vector2> m_positions2d;
Array<Vector3> m_positions3d;
Array<std::string> m_label;
Array<Array<Vector3>> m_coordinates3d; // Used for conformers/trajectories.
Array<double> m_timesteps;
Array<AtomHybridization> m_hybridizations;
Expand Down Expand Up @@ -857,6 +862,31 @@ inline bool Molecule::setAtomPosition3d(Index atomId, const Vector3& pos)
return false;
}

inline std::string Molecule::label(Index atomId) const
{
return atomId < m_label.size() ? m_label[atomId] : "";
}

inline bool Molecule::setLabel(const Core::Array<std::string>& label)
{
if (label.size() == atomCount() || label.size() == 0) {
m_label = label;
return true;
}
return false;
}

inline bool Molecule::setLabel(Index atomId, const std::string& label)
{
if (atomId < atomCount()) {
if (atomId >= m_label.size())
m_label.resize(atomCount(), "");
m_label[atomId] = label;
return true;
}
return false;
}

inline void Molecule::setAtomSelected(Index atomId, bool selected)
{
if (atomId < atomCount()) {
Expand Down
19 changes: 17 additions & 2 deletions avogadro/io/cjsonformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ bool CjsonFormat::read(std::istream& file, Molecule& molecule)
}
}

// todo? 2d position
// labels
json labels = atoms["labels"];
for (size_t i = 0; i < atomCount; ++i) {
molecule.atom(i).setLabel(labels[i]);
}

// Check for coordinate sets, and read them in if found, e.g. trajectories.
json coordSets = atoms["coords"]["3dSets"];
if (coordSets.is_array() && coordSets.size()) {
Expand Down Expand Up @@ -232,8 +239,9 @@ bool CjsonFormat::read(std::istream& file, Molecule& molecule)
newResidue.addResidueAtom(item.key(), atom);
}
}

// todo colors
json color = residue["color"];
Vector3ub col = Vector3ub(color[0], color[1], color[2]);
newResidue.setColor(col);
}
}

Expand Down Expand Up @@ -725,6 +733,13 @@ bool CjsonFormat::write(std::ostream& file, const Molecule& molecule)
}
}

// labels
json labels;
for (size_t i = 0; i < molecule.atomCount(); ++i) {
labels.push_back(molecule.label(i));
}
root["atoms"]["labels"] = labels;

auto layer = LayerManager::getMoleculeInfo(&molecule)->layer;
if (layer.atomCount()) {
json atomLayer;
Expand Down
9 changes: 9 additions & 0 deletions avogadro/qtgui/rwmolecule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ bool RWMolecule::setAtomPositions3d(const Core::Array<Vector3>& pos,
return true;
}

bool RWMolecule::setLabel(Index atomId, const std::string& label,
const QString& undoText)
{
ModifyLabelCommand* comm = new ModifyLabelCommand(*this, atomId, label);
comm->setText(undoText);
m_undoStack.push(comm);
return true;
}

bool RWMolecule::setAtomPosition3d(Index atomId, const Vector3& pos,
const QString& undoText)
{
Expand Down
8 changes: 8 additions & 0 deletions avogadro/qtgui/rwmolecule.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ class AVOGADROQTGUI_EXPORT RWMolecule : public QObject
*/
Vector3 atomPosition3d(Index atomId) const;

std::string label(Index atomId) const;
bool setLabel(Index atomId, const std::string& label,
const QString& undoText = QStringLiteral("Change Atom Label"));
/**
* Replace the current array of 3D atomic coordinates.
* @param pos The new coordinate array. Must be of length atomCount().
Expand Down Expand Up @@ -739,6 +742,11 @@ inline Vector3 RWMolecule::atomPosition3d(Index atomId) const
return m_molecule.atomPosition3d(atomId);
}

inline std::string RWMolecule::label(Index atomId) const
{
return m_molecule.label(atomId);
}

inline Core::AtomHybridization RWMolecule::hybridization(Index atomId) const
{
return m_molecule.hybridization(atomId);
Expand Down
21 changes: 21 additions & 0 deletions avogadro/qtgui/rwmolecule_undo.h
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,27 @@ class SetForceVectorCommand : public MergeUndoCommand<SetForceVectorMergeId>
}
};
} // namespace

namespace {
class ModifyLabelCommand : public RWMolecule::UndoCommand
{
Index m_atomId;
std::string m_newLabel;
std::string m_oldLabel;

public:
ModifyLabelCommand(RWMolecule& m, Index atomId, const std::string& label)
: UndoCommand(m), m_atomId(atomId), m_newLabel(label)
{
m_oldLabel = m_mol.molecule().label(m_atomId);
}

void redo() override { m_mol.molecule().setLabel(m_atomId, m_newLabel); }

void undo() override { m_mol.molecule().setLabel(m_atomId, m_oldLabel); }
};
} // namespace

} // namespace QtGui
} // namespace Avogadro
#endif
1 change: 1 addition & 0 deletions avogadro/qtplugins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ add_subdirectory(fetchpdb)
add_subdirectory(hydrogens)
add_subdirectory(importpqr)
add_subdirectory(insertfragment)
add_subdirectory(label)
add_subdirectory(lammpsinput)
add_subdirectory(lineformatinput)
add_subdirectory(manipulator)
Expand Down
11 changes: 11 additions & 0 deletions avogadro/qtplugins/label/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

avogadro_plugin(Label
"Labels rendering scheme"
ScenePlugin
label.h
Label
label.cpp
"")


target_link_libraries(Label LINK_PRIVATE AvogadroRendering)
Loading