5 #include <QGuiApplication>
32 _ui =
new Ui::PublisherCSV_DIalog();
34 _dialog->setAttribute(Qt::WA_DeleteOnClose);
36 _start_time = std::numeric_limits<double>::quiet_NaN();
37 _end_time = std::numeric_limits<double>::quiet_NaN();
38 _ui->lineEditStart->setText(
"");
39 _ui->lineEditEnd->setText(
"");
45 [
this]() {
_ui->labelNotification->setText(
""); });
50 connect(
_ui->buttonGetStart, &QPushButton::clicked,
this, [
this]() {
51 _start_time = _previous_time;
52 _ui->checkBoxFirst->setChecked(false);
53 _ui->lineEditStart->setEnabled(true);
54 _ui->lineEditStart->setText(QString::number(_previous_time,
'f', 3));
58 connect(
_ui->buttonGetEnd, &QPushButton::clicked,
this, [
this]() {
59 _end_time = _previous_time;
60 _ui->checkBoxLast->setChecked(false);
61 _ui->lineEditEnd->setEnabled(true);
62 _ui->lineEditEnd->setText(QString::number(_previous_time,
'f', 3));
66 connect(
_ui->checkBoxFirst, &QCheckBox::toggled,
this, [
this](
bool checked) {
67 _ui->lineEditStart->setEnabled(!checked);
68 _start_time = (checked) ? std::numeric_limits<double>::lowest() : _previous_time;
72 connect(
_ui->checkBoxLast, &QCheckBox::toggled,
this, [
this](
bool checked) {
73 _ui->lineEditEnd->setEnabled(!checked);
74 _end_time = (checked) ? std::numeric_limits<double>::max() : _previous_time;
79 connect(
_ui->buttonStatisticsClip, &QPushButton::clicked,
this, [
this]() {
80 auto csv_string = generateStatisticsCSV(_start_time, _end_time);
81 QClipboard* clipboard = QGuiApplication::clipboard();
82 clipboard->setText(csv_string);
83 _ui->labelNotification->setText(
"Statistics copied to Clipboard");
84 _notification_timer->start(2000);
88 connect(
_ui->buttonRangeClip, &QPushButton::clicked,
this, [
this]() {
89 auto csv_string = generateRangeCSV(_start_time, _end_time);
90 QClipboard* clipboard = QGuiApplication::clipboard();
91 clipboard->setText(csv_string);
92 _ui->labelNotification->setText(
"Range data copied to Clipboard");
93 _notification_timer->start(2000);
97 connect(
_ui->buttonStatisticsFile, &QPushButton::clicked,
this, [
this]() {
98 auto csv_string = generateStatisticsCSV(_start_time, _end_time);
99 this->saveFile(csv_string);
103 connect(
_ui->buttonRangeFile, &QPushButton::clicked,
this, [
this]() {
104 auto csv_string = generateRangeCSV(_start_time, _end_time);
105 this->saveFile(csv_string);
109 _dialog->setWindowFlag(Qt::WindowStaysOnTopHint);
129 std::map<std::string, const PJ::PlotData*> ordered_map;
132 ordered_map.insert({ it.first, &it.second });
135 std::stringstream out;
136 out <<
"Series,Current,Min,Max,Average\n";
137 out <<
"Start Time," << time_start <<
"\n";
138 out <<
"End Time," << time_end <<
"\n";
141 for (
const auto& it : ordered_map)
143 const auto&
name = it.first;
144 const auto& plot = *(it.second);
145 int index = plot.getIndexFromX(time_start);
153 auto point = plot.at(index);
155 if (point.x > time_end)
159 if (index + 1 == plot.size())
164 double min_value = point.y;
166 double total = point.y;
170 while (index < plot.size())
172 point = plot.at(index);
173 if (point.x > time_end)
177 double value = point.y;
179 min_value = std::min(min_value, value);
185 out << ((current_value) ? std::to_string(current_value.value()) :
"");
187 out << std::to_string(min_value) <<
',';
189 out << std::to_string(total /
double(
count)) <<
'\n';
191 return QString::fromStdString(out.str());
197 *
first =
_ui->lineEditStart->text().toDouble(&
ok);
202 *last =
_ui->lineEditEnd->text().toDouble(&
ok);
213 _ui->buttonRangeClip->setEnabled(
enable);
214 _ui->buttonRangeFile->setEnabled(
enable);
215 _ui->buttonStatisticsClip->setEnabled(
enable);
216 _ui->buttonStatisticsFile->setEnabled(
enable);
224 QString directory_path =
225 settings.value(
"StatePublisherCSV.saveDirectory", QDir::currentPath()).toString();
227 QString fileName = QFileDialog::getSaveFileName(
228 nullptr, tr(
"Save as CSV file"), directory_path, tr(
"CSV files (*.csv)"));
230 if (fileName.isEmpty())
234 if (!fileName.endsWith(
".csv"))
236 fileName.append(
".csv");
239 QFile file(fileName);
240 if (!file.open(QIODevice::WriteOnly))
242 QMessageBox::warning(
nullptr,
"Error",
243 QString(
"Failed to open the file [%1]").
arg(fileName));
247 file.write(text.toUtf8());
250 directory_path = QFileInfo(fileName).absolutePath();
251 settings.setValue(
"StatePublisherCSV.saveDirectory", directory_path);
256 using PlotPair = std::pair<std::string, const PJ::PlotData*>;
258 std::vector<PlotPair> ordered_plotdata;
262 if (it.second.size() == 0 || it.second.front().x > time_end ||
263 it.second.back().x < time_start)
267 ordered_plotdata.push_back({ it.first, &it.second });
269 const size_t plot_count = ordered_plotdata.size();
271 std::sort(ordered_plotdata.begin(), ordered_plotdata.end(),
272 [](
const PlotPair& a,
const PlotPair& b) { return a.first < b.first; });
275 std::vector<size_t> indices(plot_count, 0);
277 const auto NaN = std::numeric_limits<double>::quiet_NaN();
278 std::vector<double> row_values(plot_count, NaN);
282 for (
size_t i = 0; i < plot_count; i++)
284 labels += QString::fromStdString(ordered_plotdata[i].
first);
285 labels += (i + 1 < plot_count) ?
"," :
"\n";
287 const PJ::PlotData* plotdata = (ordered_plotdata[i].second);
291 index = plotdata->
size();
293 indices[i] = index + 1;
297 QStringList rows = { labels };
303 double min_time = std::numeric_limits<double>::max();
305 for (
size_t i = 0; i < plot_count; i++)
307 size_t index = indices[i];
308 const PJ::PlotData* plotdata = (ordered_plotdata[i].second);
311 if (index >= plotdata->
size())
315 const auto& point = plotdata->
at(index);
316 if (point.x > time_end)
323 if (min_time > point.x)
327 std::fill(row_values.begin(), row_values.begin() + i, NaN);
328 row_values[i] = point.y;
330 else if (std::abs(min_time - point.x) < std::numeric_limits<double>::epsilon())
332 row_values[i] = point.y;
336 if (min_time > time_end || done)
342 QString row_str = QString::number(min_time,
'f', 6) +
",";
344 for (
size_t i = 0; i < plot_count; i++)
348 row_str += QString::number(row_values[i],
'f', 9);
352 row_str += (i + 1 < plot_count) ?
"," :
"\n";
354 rows.push_back(row_str);
356 return rows.join(
"");