Skip to content

Commit

Permalink
Cleanup label serialize, fixing a potential crash
Browse files Browse the repository at this point in the history
Signed-off-by: Geoff Hutchison <[email protected]>
  • Loading branch information
ghutchis committed Sep 10, 2023
1 parent 8970c8b commit 43aaa79
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions avogadro/qtplugins/label/label.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

#include "label.h"

#include <iostream>
#include <sstream>

#include <avogadro/core/elements.h>
#include <avogadro/core/residue.h>
#include <avogadro/qtgui/colorbutton.h>
Expand Down Expand Up @@ -57,16 +60,16 @@ TextLabel3D* createLabel(const std::string& text, const Vector3f& pos,

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

QWidget* widget;
float radiusScalar;
Expand All @@ -76,8 +79,8 @@ struct LayerLabel : Core::LayerData
{
widget = nullptr;
QSettings settings;
atomOptions = char(settings.value("label/atomoptions", 0x02).toInt());
residueOptions = char(settings.value("label/residueoptions", 0x00).toInt());
atomOptions = settings.value("label/atomoptions", 0x02).toInt();
residueOptions = settings.value("label/residueoptions", 0x00).toInt();
radiusScalar = settings.value("label/radiusscalar", 0.5).toDouble();

auto q_color =
Expand All @@ -95,21 +98,20 @@ struct LayerLabel : Core::LayerData

std::string serialize() final
{
/// FIXME: What is this attempting to do? It causes compiler warnings.
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]);
std::stringstream output;
output << atomOptions << " " << residueOptions << " " << radiusScalar << " "
<< (int)color[0] << " " << (int)color[1] << " " << (int)color[2];
return output.str();
}

void deserialize(std::string text) final
{
std::stringstream ss(text);
std::string aux;
ss >> aux;
atomOptions = aux[0];
atomOptions = std::stoi(aux);
ss >> aux;
residueOptions = aux[0];
residueOptions = std::stoi(aux);
ss >> aux;
radiusScalar = std::stof(aux);
ss >> aux;
Expand Down Expand Up @@ -388,4 +390,4 @@ QWidget* Label::setupWidget()
return interface.widget;
}

} // namespace Avogadro
} // namespace Avogadro::QtPlugins

0 comments on commit 43aaa79

Please sign in to comment.