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

Save NlohmannParser (JSON) settings #971

Merged
merged 4 commits into from
Apr 29, 2024
Merged
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
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
Loading