statistics_dialog.cpp
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5  */
6 
7 #include "statistics_dialog.h"
8 #include "ui_statistics_dialog.h"
9 #include <QTableWidgetItem>
10 #include "qwt_text.h"
11 
13  : QDialog(parent), ui(new Ui::statistics_dialog), _parent(parent)
14 {
15  ui->setupUi(this);
16 
17  setWindowTitle(QString("Statistics | %1").arg(_parent->windowTitle()));
18  setWindowFlag(Qt::Tool);
19 
20  ui->tableWidget->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
21 
22  connect(ui->rangeComboBox, qOverload<int>(&QComboBox::currentIndexChanged), this,
23  [this]() {
24  auto rect = _parent->currentBoundingRect();
25  update({ rect.left(), rect.right() });
26  });
27 }
28 
30 {
31  delete ui;
32 }
33 
35 {
36  return (ui->rangeComboBox->currentIndex() == 0);
37 }
38 
40 {
41  std::map<QString, Statistics> statistics;
42 
43  for (const auto& info : _parent->curveList())
44  {
45  Statistics stat;
46  double start_time;
47  double end_time;
48  const auto ts = info.curve->data();
49 
50  bool first = true;
51 
52  for (size_t i = 0; i < ts->size(); i++)
53  {
54  const auto p = ts->sample(i);
55  if (calcVisibleRange())
56  {
57  if (p.x() < range.min)
58  {
59  continue;
60  }
61  if (p.x() > range.max)
62  {
63  break;
64  }
65  }
66  stat.count++;
67  if (first)
68  {
69  start_time = p.x();
70  end_time = p.x();
71  stat.min = p.y();
72  stat.max = p.y();
73  first = false;
74  }
75  else
76  {
77  start_time = std::min(start_time, p.x());
78  end_time = std::max(end_time, p.x());
79  stat.min = std::min(stat.min, p.y());
80  stat.max = std::max(stat.max, p.y());
81  }
82  stat.mean_tot += p.y();
83  }
84  stat.mean_interval = (end_time - start_time) / double(stat.count);
85  statistics[info.curve->title().text()] = stat;
86  }
87 
88  ui->tableWidget->setRowCount(statistics.size());
89  int row = 0;
90  for (const auto& it : statistics)
91  {
92  const auto& stat = it.second;
93 
94  std::array<QString, 6> row_values;
95  row_values[0] = it.first;
96  row_values[1] = QString::number(stat.count);
97  row_values[2] = QString::number(stat.min, 'f');
98  row_values[3] = QString::number(stat.max, 'f');
99  double mean = stat.mean_tot / double(stat.count);
100  row_values[4] = QString::number(mean, 'f');
101  row_values[5] = QString::number(stat.mean_interval, 'f');
102 
103  for (size_t col = 0; col < row_values.size(); col++)
104  {
105  if (auto item = ui->tableWidget->item(row, col))
106  {
107  item->setText(row_values[col]);
108  }
109  else
110  {
111  ui->tableWidget->setItem(row, col, new QTableWidgetItem(row_values[col]));
112  }
113  }
114  row++;
115  }
116 }
117 
118 void StatisticsDialog::setTitle(QString title)
119 {
120  if (title == "...")
121  {
122  title = "";
123  }
124  setWindowTitle(QString("Statistics | %1").arg(title));
125 }
126 
127 void StatisticsDialog::closeEvent(QCloseEvent* event)
128 {
129  QWidget::closeEvent(event);
130  emit rejected();
131 }
StatisticsDialog::calcVisibleRange
bool calcVisibleRange()
Definition: statistics_dialog.cpp:34
PJ::PlotWidgetBase::curveList
const std::list< CurveInfo > & curveList() const
Definition: plotwidget_base.cpp:480
detail::first
auto first(const T &value, const Tail &...) -> const T &
Definition: compile.h:60
Statistics::min
double min
Definition: statistics_dialog.h:23
StatisticsDialog::_parent
PlotWidget * _parent
Definition: statistics_dialog.h:48
arg
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition: core.h:1875
Statistics
Definition: statistics_dialog.h:20
StatisticsDialog::setTitle
void setTitle(QString title)
Definition: statistics_dialog.cpp:118
PlotWidget
Definition: plotwidget.h:38
StatisticsDialog::StatisticsDialog
StatisticsDialog(PlotWidget *parent=nullptr)
Definition: statistics_dialog.cpp:12
StatisticsDialog::ui
Ui::statistics_dialog * ui
Definition: statistics_dialog.h:46
Ui
Definition: cheatsheet_dialog.h:6
PJ::Range::max
double max
Definition: plotdatabase.h:27
Statistics::mean_interval
double mean_interval
Definition: statistics_dialog.h:26
statistics_dialog.h
PJ::Range
Definition: plotdatabase.h:24
Statistics::mean_tot
double mean_tot
Definition: statistics_dialog.h:25
Statistics::count
size_t count
Definition: statistics_dialog.h:22
StatisticsDialog::~StatisticsDialog
~StatisticsDialog()
Definition: statistics_dialog.cpp:29
StatisticsDialog::closeEvent
void closeEvent(QCloseEvent *event)
Definition: statistics_dialog.cpp:127
qwt_text.h
StatisticsDialog::update
void update(Range range)
Definition: statistics_dialog.cpp:39
PJ::Range::min
double min
Definition: plotdatabase.h:26
Statistics::max
double max
Definition: statistics_dialog.h:24


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon May 26 2025 02:22:38