Skip to content

Commit

Permalink
Some comments and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jbendes committed May 24, 2023
1 parent 344cc3b commit 6ceab8d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 22 deletions.
35 changes: 35 additions & 0 deletions plotjuggler_base/include/PlotJuggler/util/string_util.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

#ifndef STRING_UTIL_HPP
#define STRING_UTIL_HPP

#include <string>
#include <vector>
#include <sstream>

namespace PJ
{

std::vector<std::string> split(const std::string& str, char delimiter)
{
std::vector<std::string> ret;

std::stringstream ss {str};
std::string tok;

while (getline(ss, tok, delimiter)) ret.push_back(std::move(tok));

auto len = str.size();
if (len > 0 && str[len-1] == delimiter) ret.push_back("");

return ret;
}

} // namespace PJ

#endif // STRING_UTIL_HPP

23 changes: 16 additions & 7 deletions plotjuggler_plugins/DataLoadZcm/dataload_zcm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <QWidget>
#include <QFileDialog>

#include "PlotJuggler/util/string_util.hpp"

#include <iostream>

#include <zcm/zcm-cpp.hpp>
Expand Down Expand Up @@ -48,17 +50,25 @@ DataLoadZcm::DataLoadZcm()
_ui->lineEditFolder->setText(filename);
}
});
// When the "Default" button is pushed, load from getenv("ZCMTYPES_PATH")
// When the "Default" button is pushed, load from getenv("PJ_ZCMTYPES_LIB_PATH")
connect(_ui->buttonDefaultFolder, &QPushButton::clicked, this,
[this](){
QString folder = getenv("ZCMTYPES_PATH");
QString folder = getenv("PJ_ZCMTYPES_LIB_PATH");
if(folder.isEmpty()){
QMessageBox::warning(nullptr, "Error", "Environment variable ZCMTYPES_PATH not set");
QMessageBox::warning(nullptr, "Error", "Environment variable PJ_ZCMTYPES_LIB_PATH not set");
}
else {
_ui->lineEditFolder->setText(getenv("ZCMTYPES_PATH"));
_ui->lineEditFolder->setText(getenv("PJ_ZCMTYPES_LIB_PATH"));
}
});

_extensions.emplace_back("zcmlog");
const char* envExtensionsStr = getenv("PJ_ZCM_EXTENSIONS");
if (envExtensionsStr) {
auto envExtensions = PJ::split(envExtensionsStr, ':');
for (auto& e : envExtensions) _extensionsStr.push_back(e);
for (auto& e : _extensionsStr) _extensions.push_back(e.c_str());
}
}

DataLoadZcm::~DataLoadZcm()
Expand All @@ -72,8 +82,7 @@ const char* DataLoadZcm::name() const

const vector<const char*>& DataLoadZcm::compatibleFileExtensions() const
{
static vector<const char*> extensions = { "zcmlog" };
return extensions;
return _extensions;
}

static int processInputLog(const string& logpath,
Expand Down Expand Up @@ -143,7 +152,7 @@ bool DataLoadZcm::launchDialog(const string& filepath, unordered_set<string>& ch
QSettings settings;
_dialog->restoreGeometry(settings.value("DataLoadZcm.geometry").toByteArray());

auto type_path = settings.value("DataLoadZcm.folder", getenv("ZCMTYPES_PATH")).toString();
auto type_path = settings.value("DataLoadZcm.folder", getenv("PJ_ZCMTYPES_LIB_PATH")).toString();
_ui->lineEditFolder->setText(type_path);

channels.clear();
Expand Down
3 changes: 3 additions & 0 deletions plotjuggler_plugins/DataLoadZcm/dataload_zcm.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@ class DataLoadZcm : public PJ::DataLoader
QDialog* _dialog;
Ui::DialogZcm* _ui;

std::vector<std::string> _extensionsStr;
std::vector<const char*> _extensions;

bool launchDialog(const std::string& filepath, std::unordered_set<std::string>& channels);
};
28 changes: 15 additions & 13 deletions plotjuggler_plugins/DataStreamZcm/datastream_zcm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,22 @@ DataStreamZcm::DataStreamZcm(): _subs(nullptr), _running(false)
connect(_ui->buttonSelectFolder, &QPushButton::clicked, this,
[this](){
QString filename = QFileDialog::getOpenFileName(
nullptr, tr("Select ZCM Type File"),
nullptr, tr("Select ZCM Type Library File"),
{}, "*.so");
// if valid, update lineEditFolder
if(!filename.isEmpty()) {
_ui->lineEditFolder->setText(filename);
}
});
// When the "Default" button is pushed, load from getenv("ZCMTYPES_PATH")
// When the "Default" button is pushed, load from getenv("PJ_ZCMTYPES_LIB_PATH")
connect(_ui->buttonDefaultFolder, &QPushButton::clicked, this,
[this](){
QString folder = getenv("ZCMTYPES_PATH");
QString folder = getenv("PJ_ZCMTYPES_LIB_PATH");
if(folder.isEmpty()){
QMessageBox::warning(nullptr, "Error", "Environment variable ZCMTYPES_PATH not set");
QMessageBox::warning(nullptr, "Error", "Environment variable PJ_ZCMTYPES_LIB_PATH not set");
}
else {
_ui->lineEditFolder->setText(getenv("ZCMTYPES_PATH"));
_ui->lineEditFolder->setText(getenv("PJ_ZCMTYPES_LIB_PATH"));
}
});
}
Expand All @@ -83,16 +83,15 @@ bool DataStreamZcm::start(QStringList*)
return false;
}

// Initialize zmc here, only once if everything goes well
// RRR (Bendes): Can we make this configurable via a transport string?
// "ipc" is just 1 option
// Initialize zcm here, only once if everything goes well
if(!_zcm) {
_zcm.reset(new zcm::ZCM("ipc"));

if (!_zcm->good()) {
QMessageBox::warning(nullptr, "Error", "Failed to create zcm::ZCM()");
_zcm.reset();
_subs = nullptr;
// QUESTION: to we need to call first "delete _subs" ?
// Who have the ownership of that pointer?
return false;
}
}
Expand All @@ -103,7 +102,7 @@ bool DataStreamZcm::start(QStringList*)
// Initialize the lineEdits in the ui with the previous value;
QSettings settings;
auto const prev_folder = settings.value("DataStreamZcm::folder",
getenv("ZCMTYPES_PATH")).toString();
getenv("PJ_ZCMTYPES_LIB_PATH")).toString();
_ui->lineEditFolder->setText(prev_folder);
auto const subscribe_text = settings.value("DataStreamZcm::subscribe", ".*").toString();
_ui->lineEditSubscribe->setText(subscribe_text);
Expand Down Expand Up @@ -136,10 +135,10 @@ bool DataStreamZcm::start(QStringList*)
if(_subscribe_string != _ui->lineEditSubscribe->text() || !_subs)
{
_subscribe_string = _ui->lineEditSubscribe->text();
if (_subs) _zcm->unsubscribe(_subs);
_subs =_zcm->subscribe(_subscribe_string.toStdString(), &DataStreamZcm::handler, this);
if (!_zcm->good()) {
QMessageBox::warning(nullptr, "Error", "Failed to create zcm::TypeDb()");
// QUESTION: is there any cleanup that need to be done here?
if (!_subs) {
QMessageBox::warning(nullptr, "Error", "Failed to subscribe");
return false;
}
}
Expand All @@ -155,6 +154,7 @@ void DataStreamZcm::shutdown()
return;
}
_zcm->stop();
_zcm.reset(nullptr);
_running = false;
}

Expand All @@ -165,6 +165,7 @@ bool DataStreamZcm::isRunning() const

bool DataStreamZcm::xmlSaveState(QDomDocument& doc, QDomElement& parent_element) const
{
// RRR (Bendes): Probably also transport string here
QDomElement elem = doc.createElement("config");
elem.setAttribute("folder", _types_folder);
elem.setAttribute("subscribe", _subscribe_string);
Expand All @@ -177,6 +178,7 @@ bool DataStreamZcm::xmlLoadState(const QDomElement& parent_element)
QDomElement elem = parent_element.firstChildElement("config");
if (!elem.isNull())
{
// RRR (Bendes): Probably also transport string here
_types_folder = elem.attribute("folder");
_subscribe_string = elem.attribute("subscribe");
QSettings settings;
Expand Down
4 changes: 2 additions & 2 deletions plotjuggler_plugins/DataStreamZcm/datastream_zcm.ui
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
</rect>
</property>
<property name="windowTitle">
<string>DialogZmc</string>
<string>DialogZcm</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Zmc types library: </string>
<string>Zcm types library: </string>
</property>
</widget>
</item>
Expand Down

0 comments on commit 6ceab8d

Please sign in to comment.