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  const auto ts = info.curve->data();
47 
48  bool first = true;
49 
50  for (size_t i = 0; i < ts->size(); i++)
51  {
52  const auto p = ts->sample(i);
53  if (calcVisibleRange())
54  {
55  if (p.x() < range.min)
56  {
57  continue;
58  }
59  if (p.x() > range.max)
60  {
61  break;
62  }
63  }
64  stat.count++;
65  if (first)
66  {
67  stat.min = p.y();
68  stat.max = p.y();
69  first = false;
70  }
71  else
72  {
73  stat.min = std::min(stat.min, p.y());
74  stat.max = std::max(stat.max, p.y());
75  }
76  stat.mean_tot += p.y();
77  }
78  statistics[info.curve->title().text()] = stat;
79  }
80 
81  ui->tableWidget->setRowCount(statistics.size());
82  int row = 0;
83  for (const auto& it : statistics)
84  {
85  const auto& stat = it.second;
86 
87  std::array<QString, 5> row_values;
88  row_values[0] = it.first;
89  row_values[1] = QString::number(stat.count);
90  row_values[2] = QString::number(stat.min, 'f');
91  row_values[3] = QString::number(stat.max, 'f');
92  double mean = stat.mean_tot / double(stat.count);
93  row_values[4] = QString::number(mean, 'f');
94 
95  for (size_t col = 0; col < row_values.size(); col++)
96  {
97  if (auto item = ui->tableWidget->item(row, col))
98  {
99  item->setText(row_values[col]);
100  }
101  else
102  {
103  ui->tableWidget->setItem(row, col, new QTableWidgetItem(row_values[col]));
104  }
105  }
106  row++;
107  }
108 }
109 
110 void StatisticsDialog::setTitle(QString title)
111 {
112  if (title == "...")
113  {
114  title = "";
115  }
116  setWindowTitle(QString("Statistics | %1").arg(title));
117 }
118 
119 void StatisticsDialog::closeEvent(QCloseEvent* event)
120 {
121  QWidget::closeEvent(event);
122  emit rejected();
123 }
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:47
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:110
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:45
Ui
Definition: cheatsheet_dialog.h:6
PJ::Range::max
double max
Definition: plotdatabase.h:27
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:119
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 Nov 11 2024 03:23:47