Skip to content

Commit

Permalink
Save NlohmannParser (JSON) settings (#971)
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelJansson authored Apr 29, 2024
1 parent 832b2dd commit e3ec836
Showing 1 changed file with 50 additions and 17 deletions.
67 changes: 50 additions & 17 deletions plotjuggler_app/nlohmann_parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "nlohmann/json.hpp"
#include "PlotJuggler/messageparser_base.h"
#include <QDebug>
#include <QSettings>

using namespace PJ;

Expand Down Expand Up @@ -110,31 +111,67 @@ class QCheckBoxClose : public QGroupBox
class NlohmannParserCreator : public ParserFactoryPlugin
{
public:
NlohmannParserCreator()
NlohmannParserCreator(const char* encoding)
{
_encoding = encoding;
_checkbox_use_timestamp = new QCheckBoxClose("use field as timestamp if available");
loadSettings();
}

template <typename ParserT>
MessageParserPtr createParserImpl(const std::string& topic_name, PlotDataMapRef& data)
{
saveSettings();

std::string timestamp_name = _checkbox_use_timestamp->lineedit->text().toStdString();
return std::make_shared<ParserT>(
topic_name, data, _checkbox_use_timestamp->isChecked(), timestamp_name);
}

virtual QWidget* optionsWidget()
{
loadSettings();

return _checkbox_use_timestamp;
}

const char* encoding() const override
{
return _encoding;
}

virtual void loadSettings()
{
QSettings settings;
QString prefix = QString("NlohmannParser.") + QString(encoding());
bool checked = settings.value(prefix + ".timestampEnabled", false).toBool();
QString field = settings.value(prefix + ".timestampFieldName", "").toString();

_checkbox_use_timestamp->setChecked(checked);
_checkbox_use_timestamp->lineedit->setText(field);
}

virtual void saveSettings()
{
QSettings settings;
QString prefix = QString("NlohmannParser.") + QString(encoding());
settings.setValue(prefix + ".timestampEnabled", _checkbox_use_timestamp->isChecked());
settings.setValue(prefix + ".timestampFieldName",
_checkbox_use_timestamp->lineedit->text());
}

protected:
QCheckBoxClose* _checkbox_use_timestamp;
const char* _encoding;
};

class JSON_ParserFactory : public NlohmannParserCreator
{
public:
JSON_ParserFactory() : NlohmannParserCreator("json")
{
}

MessageParserPtr createParser(const std::string& topic_name,
const std::string& /*type_name*/,
const std::string& /*schema*/,
Expand All @@ -146,15 +183,15 @@ class JSON_ParserFactory : public NlohmannParserCreator
{
return "JSON_ParserFactory";
}
const char* encoding() const override
{
return "json";
}
};

class CBOR_ParserFactory : public NlohmannParserCreator
{
public:
CBOR_ParserFactory() : NlohmannParserCreator("cbor")
{
}

MessageParserPtr createParser(const std::string& topic_name, const std::string&,
const std::string&, PlotDataMapRef& data) override
{
Expand All @@ -164,15 +201,15 @@ class CBOR_ParserFactory : public NlohmannParserCreator
{
return "CBOR_ParserFactory";
}
const char* encoding() const override
{
return "cbor";
}
};

class BSON_ParserFactory : public NlohmannParserCreator
{
public:
BSON_ParserFactory() : NlohmannParserCreator("bson")
{
}

MessageParserPtr createParser(const std::string& topic_name, const std::string&,
const std::string&, PlotDataMapRef& data) override
{
Expand All @@ -182,15 +219,15 @@ class BSON_ParserFactory : public NlohmannParserCreator
{
return "BSON_ParserFactory";
}
const char* encoding() const override
{
return "bson";
}
};

class MessagePack_ParserFactory : public NlohmannParserCreator
{
public:
MessagePack_ParserFactory() : NlohmannParserCreator("msgpack")
{
}

MessageParserPtr createParser(const std::string& topic_name, const std::string&,
const std::string&, PlotDataMapRef& data) override
{
Expand All @@ -200,10 +237,6 @@ class MessagePack_ParserFactory : public NlohmannParserCreator
{
return "MessagePack_ParserFactory";
}
const char* encoding() const override
{
return "msgpack";
}
};

#endif // NLOHMANN_PARSERS_H

0 comments on commit e3ec836

Please sign in to comment.