curvelist_panel.cpp
Go to the documentation of this file.
1 #include "curvelist_panel.h"
2 #include "ui_curvelist_panel.h"
4 #include <QDebug>
5 #include <QLayoutItem>
6 #include <QMenu>
7 #include <QSettings>
8 #include <QDrag>
9 #include <QMimeData>
10 #include <QHeaderView>
11 #include <QFontDatabase>
12 #include <QMessageBox>
13 #include <QApplication>
14 #include <QPainter>
15 #include <QCompleter>
16 #include <QStandardItem>
17 #include <QWheelEvent>
18 #include <QItemSelectionModel>
19 #include <QScrollBar>
20 #include <QTreeWidget>
21 
22 #include "svg_util.h"
23 
24 //-------------------------------------------------
25 
26 CurveListPanel::CurveListPanel(const CustomPlotMap& mapped_math_plots, QWidget* parent)
27  : QWidget(parent)
28  , ui(new Ui::CurveListPanel)
29  , _custom_view(new CurveTableView(this))
30  , _tree_view(new CurveTreeView(this))
31  , _custom_plots(mapped_math_plots)
32  , _column_width_dirty (true)
33 {
34  ui->setupUi(this);
35 
36  setFocusPolicy(Qt::ClickFocus);
37 
38  _tree_view->setObjectName("curveTreeView");
39  _custom_view->setObjectName("curveCustomView");
40 
41  auto layout1 = new QHBoxLayout();
42  ui->listPlaceholder1->setLayout(layout1);
43  layout1->addWidget(_tree_view, 1);
44  layout1->setMargin(0);
45 
46  auto layout2 = new QHBoxLayout();
47  ui->listPlaceholder2->setLayout(layout2);
48  layout2->addWidget(_custom_view, 1);
49  layout2->setMargin(0);
50 
51  QSettings settings;
52 
53  int point_size = settings.value("FilterableListWidget/table_point_size", 9).toInt();
54  changeFontSize(point_size);
55 
56  ui->splitter->setStretchFactor(0, 5);
57  ui->splitter->setStretchFactor(1, 1);
58 
59  connect(_custom_view->selectionModel(), &QItemSelectionModel::selectionChanged,
61 
62  connect(_custom_view->verticalScrollBar(), &QScrollBar::valueChanged, this, &CurveListPanel::refreshValues);
63 
64  connect(_tree_view->verticalScrollBar(), &QScrollBar::valueChanged, this, &CurveListPanel::refreshValues);
65 
66  connect(_tree_view, &QTreeWidget::itemExpanded, this, &CurveListPanel::refreshValues);
67 }
68 
70 {
71  delete ui;
72 }
73 
75 {
77  _tree_view->clear();
78  _numeric_data = nullptr;
79  ui->labelNumberDisplayed->setText("0 of 0");
80 }
81 
82 void CurveListPanel::addCurve(const QString& item_name)
83 {
84  _tree_view->addItem(item_name);
85  _column_width_dirty = true;
86 }
87 
88 void CurveListPanel::addCustom(const QString& item_name)
89 {
90  _custom_view->addItem(item_name);
91  _column_width_dirty = true;
92 }
93 
95 {
98  _column_width_dirty = false;
99  updateFilter();
100 }
101 
103 {
104  on_lineEditFilter_textChanged(ui->lineEditFilter->text());
105 }
106 
107 void CurveListPanel::keyPressEvent(QKeyEvent* event)
108 {
109  if (event->key() == Qt::Key_Delete)
110  {
112  }
113 }
114 
115 void CurveListPanel::changeFontSize(int point_size)
116 {
117  _custom_view->setFontSize(point_size);
118  _tree_view->setFontSize(point_size);
119 
120  QSettings settings;
121  settings.setValue("FilterableListWidget/table_point_size", point_size);
122 }
123 
125 {
126  // return ui->checkBoxHideSecondColumn->isChecked();
127  return false;
128 }
129 
130 void CurveListPanel::update2ndColumnValues(double tracker_time, std::unordered_map<std::string, PlotData>* numeric_data)
131 {
132  _tracker_time = tracker_time;
133  _numeric_data = numeric_data;
134 
135  refreshValues();
136 }
137 
139 {
141  {
142  return;
143  }
144 
145  auto FormattedNumber = [](double value) {
146  QString num_text = QString::number(value, 'f', 3);
147  if (num_text.contains('.'))
148  {
149  int idx = num_text.length() - 1;
150  while (num_text[idx] == '0')
151  {
152  num_text[idx] = ' ';
153  idx--;
154  }
155  if (num_text[idx] == '.')
156  num_text[idx] = ' ';
157  }
158  return num_text + " ";
159  };
160 
161  auto GetValue = [&](const std::string& name) -> nonstd::optional<double> {
162  auto it = _numeric_data->find(name);
163  if (it != _numeric_data->end())
164  {
165  auto& data = it->second;
166 
168  {
169  auto value = data.getYfromX(_tracker_time);
170  if (value)
171  {
172  return value;
173  }
174  }
175  else if (data.size() > 0)
176  {
177  return data.back().y;
178  }
179  }
180  return {};
181  };
182 
183  //------------------------------------
184  for (CurveTableView* table : { _custom_view })
185  {
186  table->setViewResizeEnabled(false);
187  const int vertical_height = table->visibleRegion().boundingRect().height();
188 
189  for (int row = 0; row < table->rowCount(); row++)
190  {
191  int vertical_pos = table->rowViewportPosition(row);
192  if (vertical_pos < 0 || table->isRowHidden(row))
193  {
194  continue;
195  }
196  if (vertical_pos > vertical_height)
197  {
198  break;
199  }
200 
201  const std::string& name = table->item(row, 0)->text().toStdString();
202  auto val = GetValue(name);
203  if (val)
204  {
205  table->item(row, 1)->setText(FormattedNumber(val.value()));
206  }
207  }
209  {
210  _column_width_dirty = false;
211  table->setViewResizeEnabled(true);
212  }
213  }
214  //------------------------------------
215  for (CurveTreeView* tree_view : {_tree_view})
216  {
217  const int vertical_height = tree_view->visibleRegion().boundingRect().height();
218 
219  auto DisplayValue = [&](QTreeWidgetItem* cell) {
220  QString curve_name = cell->data(0, Qt::UserRole).toString();
221 
222  if (!curve_name.isEmpty())
223  {
224  auto rect = cell->treeWidget()->visualItemRect(cell);
225 
226  if (rect.bottom() < 0 || cell->isHidden())
227  {
228  return;
229  }
230  if (rect.top() > vertical_height)
231  {
232  return;
233  }
234 
235  auto val = GetValue(curve_name.toStdString());
236  if (val)
237  {
238  cell->setText(1, FormattedNumber(val.value()));
239  }
240  }
241  };
242 
243  tree_view->setViewResizeEnabled(false);
244  tree_view->treeVisitor(DisplayValue);
245  // tree_view->setViewResizeEnabled(true);
246  }
247 }
248 
249 void CurveListPanel::on_lineEditFilter_textChanged(const QString& search_string)
250 {
251  bool updated = false;
252 
253  CurvesView* active_view = (CurvesView*)_tree_view;
254 
255  updated = active_view->applyVisibilityFilter(search_string);
256 
257  auto h_c = active_view->hiddenItemsCount();
258  int item_count = h_c.second;
259  int visible_count = item_count - h_c.first;
260 
261  ui->labelNumberDisplayed->setText(QString::number(visible_count) + QString(" of ") + QString::number(item_count));
262  if (updated)
263  {
264  emit hiddenItemsChanged();
265  }
266 }
267 
269 {
270  QMessageBox::StandardButton reply;
271  reply = QMessageBox::question(nullptr, tr("Warning"), tr("Do you really want to remove these data?\n"),
272  QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
273 
274  if (reply == QMessageBox::Yes)
275  {
278  }
279 
280  updateFilter();
281 }
282 
283 void CurveListPanel::removeCurve(const std::string& name)
284 {
285  QString curve_name = QString::fromStdString(name);
286  _tree_view->removeCurve(curve_name);
287  _custom_view->removeCurve(curve_name);
288 }
289 
291 {
292  std::array<CurvesView*, 2> views = { _tree_view, _custom_view };
293 
294  std::string suggested_name;
295  for (CurvesView* view : views)
296  {
297  auto curve_names = view->getSelectedNames();
298  if (curve_names.size() > 0)
299  {
300  suggested_name = (curve_names.front());
301  break;
302  }
303  }
304 
305  emit createMathPlot(suggested_name);
306  on_lineEditFilter_textChanged(ui->lineEditFilter->text());
307 }
308 
309 void CurveListPanel::onCustomSelectionChanged(const QItemSelection&, const QItemSelection&)
310 {
311  auto selected = _custom_view->getSelectedNames();
312 
313  bool enabled = (selected.size() == 1);
314  ui->buttonEditCustom->setEnabled(enabled);
315  ui->buttonEditCustom->setToolTip(enabled ? "Edit the selected custom timeserie" :
316  "Select a single custom Timeserie to Edit it");
317 }
318 
320 {
321  auto selected = _custom_view->getSelectedNames();
322  if (selected.size() == 1)
323  {
324  editMathPlot(selected.front());
325  }
326 }
327 
329 {
330  _custom_view->clearSelection();
331  _tree_view->clearSelection();
332 }
333 
335 {
336  _style_dir = theme;
337  ui->buttonAddCustom->setIcon(LoadSvgIcon(":/resources/svg/add_tab.svg", theme));
338  ui->buttonEditCustom->setIcon(LoadSvgIcon(":/resources/svg/pencil-edit.svg", theme));
339 }
340 
342 {
345  emit hiddenItemsChanged();
346 }
bool is2ndColumnHidden() const
enum MQTTPropertyCodes value
void onCustomSelectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
void addItem(const QString &item_name) override
void editMathPlot(const std::string &plot_name)
QIcon LoadSvgIcon(QString filename, QString style_name="light")
Definition: svg_util.h:17
virtual bool applyVisibilityFilter(const QString &filter_string)=0
void changeFontSize(int point_size)
std::vector< std::string > getSelectedNames() override
virtual void hideValuesColumn(bool hide) override
void removeCurve(const QString &name) override
void addItem(const QString &item_name) override
void createMathPlot(const std::string &linked_plot)
void addCurve(const QString &item_name)
std::vector< std::string > getSelectedNames() override
void setFontSize(int size)
CurveTreeView * _tree_view
#define max(A, B)
Definition: Socket.h:88
void deleteCurves(const std::vector< std::string > &curve_names)
table_core< false > table
Definition: forward.hpp:636
void on_stylesheetChanged(QString theme)
void clear() override
void refreshColumns() override
virtual void keyPressEvent(QKeyEvent *event) override
Ui::CurveListPanel * ui
CurveListPanel(const CustomPlotMap &mapped_math_plots, QWidget *parent)
const char * name
void addCustom(const QString &item_name)
std::unordered_map< std::string, CustomPlotPtr > CustomPlotMap
~CurveListPanel() override
void on_buttonEditCustom_clicked()
void hiddenItemsChanged()
void refreshColumns() override
void on_checkBoxShowValues_toggled(bool show)
std::unordered_map< std::string, PlotData > * _numeric_data
void update2ndColumnValues(double time, std::unordered_map< std::string, PlotData > *numeric_data)
dictionary data
Definition: mqtt_test.py:22
virtual std::pair< int, int > hiddenItemsCount()=0
CurveTableView * _custom_view
void removeCurve(const QString &name) override
void on_lineEditFilter_textChanged(const QString &search_string)
void on_buttonAddCustom_clicked()
virtual void hideValuesColumn(bool hide) override
void clear() override
void removeCurve(const std::string &name)


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:47:33