plotwidget_editor.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 "plotwidget_editor.h"
8 #include "ui_plotwidget_editor.h"
9 #include <QHBoxLayout>
10 #include <QVBoxLayout>
11 #include <QSettings>
12 #include <QListWidgetItem>
13 #include <QLabel>
14 #include <QMouseEvent>
15 #include <QPushButton>
16 #include <QDialogButtonBox>
17 #include <QDebug>
18 #include "qwt_text.h"
19 
20 const double MAX_DOUBLE = std::numeric_limits<double>::max() / 2;
21 
22 PlotwidgetEditor::PlotwidgetEditor(PlotWidget* plotwidget, QWidget* parent)
23  : QDialog(parent), ui(new Ui::PlotWidgetEditor), _plotwidget_origin(plotwidget)
24 {
25  ui->setupUi(this);
26 
27  installEventFilter(this);
28 
29  // setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
30 
32 
33  QDomDocument doc;
34  auto saved_state = plotwidget->xmlSaveState(doc);
35 
36  _plotwidget = new PlotWidget(plotwidget->datamap(), this);
37  _plotwidget->xmlLoadState(saved_state);
40 
42 
43  auto layout = new QVBoxLayout();
44  ui->framePlotPreview->setLayout(layout);
45  layout->addWidget(_plotwidget);
46  layout->setMargin(6);
47 
48  _plotwidget->zoomOut(false);
49 
50  setupTable();
51 
52  QSettings settings;
53  restoreGeometry(settings.value("PlotwidgetEditor.geometry").toByteArray());
54 
55  if (_plotwidget->curveStyle() == PlotWidgetBase::LINES)
56  {
57  ui->radioLines->setChecked(true);
58  }
59  else if (_plotwidget->curveStyle() == PlotWidgetBase::DOTS)
60  {
61  ui->radioPoints->setChecked(true);
62  }
63  else if (_plotwidget->curveStyle() == PlotWidgetBase::STICKS)
64  {
65  ui->radioSticks->setChecked(true);
66  }
67  else
68  {
69  ui->radioBoth->setChecked(true);
70  }
71 
72  ui->lineLimitMax->setValidator(new QDoubleValidator(this));
73  ui->lineLimitMin->setValidator(new QDoubleValidator(this));
74 
75  auto ylimits = _plotwidget->customAxisLimit();
76  auto range_x = _plotwidget->getVisualizationRangeX();
77  Range suggested_limits = _plotwidget->getVisualizationRangeY(range_x);
78 
79  if (ylimits.min != -MAX_DOUBLE)
80  {
81  ui->checkBoxMin->setChecked(true);
82  ui->lineLimitMin->setText(QString::number(ylimits.min));
83  }
84  else
85  {
86  ui->lineLimitMin->setText(QString::number(suggested_limits.min));
87  }
88 
89  if (ylimits.max != MAX_DOUBLE)
90  {
91  ui->checkBoxMax->setChecked(true);
92  ui->lineLimitMax->setText(QString::number(ylimits.max));
93  }
94  else
95  {
96  ui->lineLimitMax->setText(QString::number(suggested_limits.max));
97  }
98 
99  // ui->listWidget->widget_background_disabled("QListView::item:selected { background:
100  // #ddeeff; }");
101 
102  if (ui->listWidget->count() != 0)
103  {
104  ui->listWidget->item(0)->setSelected(true);
105  }
106 }
107 
109 {
110  QSettings settings;
111  settings.setValue("PlotwidgetEditor.geometry", saveGeometry());
112 
113  delete _plotwidget;
114  delete ui;
115 }
116 
118 {
119  auto selected = ui->listWidget->selectedItems();
120  if (selected.size() != 1)
121  {
122  return;
123  }
124  auto item = selected.front();
125  if (item)
126  {
127  auto row_widget = dynamic_cast<EditorRowWidget*>(ui->listWidget->itemWidget(item));
128  auto name = row_widget->text();
129  if (row_widget->color() != c)
130  {
131  row_widget->setColor(c);
132  }
133 
134  _plotwidget->on_changeCurveColor(item->data(Qt::UserRole).toString(), c);
135  }
136 }
137 
139 {
140  auto wheel_layout = new QVBoxLayout();
141  wheel_layout->setMargin(0);
142  wheel_layout->setSpacing(5);
143  ui->widgetWheel->setLayout(wheel_layout);
145  wheel_layout->addWidget(_color_wheel);
146 
148  _color_preview->setMaximumHeight(25);
149 
150  wheel_layout->addWidget(_color_preview);
151 
154 
157 
159  [this](QColor col) {
160  QSignalBlocker block(ui->editColotText);
161  ui->editColotText->setText(col.name());
162  });
163 
164  _color_wheel->setColor(Qt::blue);
165 }
166 
168 {
169  int row_count = ui->listWidget->count();
170  for (int row = 0; row < row_count; row++)
171  {
172  auto item = ui->listWidget->item(row);
173  auto widget = ui->listWidget->itemWidget(item);
174  if (widget == w)
175  {
176  QString curve = dynamic_cast<EditorRowWidget*>(w)->text();
177 
178  ui->listWidget->takeItem(row);
179  _plotwidget->removeCurve(curve);
180  widget->deleteLater();
181  row_count--;
182  break;
183  }
184  }
185  if (row_count == 0)
186  {
187  disableWidgets();
188  }
189  _plotwidget->replot();
190 }
191 
193 {
194  ui->widgetColor->setEnabled(false);
195  _color_wheel->setEnabled(false);
196  _color_preview->setEnabled(false);
197 
198  ui->frameLimits->setEnabled(false);
199  ui->frameStyle->setEnabled(false);
200 }
201 
203 {
204  std::map<QString, QColor> colors = _plotwidget->getCurveColors();
205 
206  int row = 0;
207  for (auto& it : colors)
208  {
209  auto alias = it.first;
210  auto color = it.second;
211  auto item = new QListWidgetItem();
212  // even if it is not visible, we store here the original name (not alias)
213  item->setData(Qt::UserRole, it.first);
214 
215  ui->listWidget->addItem(item);
216  auto plot_row = new EditorRowWidget(alias, color);
217  item->setSizeHint(plot_row->sizeHint());
218  ui->listWidget->setItemWidget(item, plot_row);
219 
220  connect(plot_row, &EditorRowWidget::deleteRow, this,
221  [this](QWidget* w) { onDeleteRow(w); });
222  row++;
223  }
224  if (row == 0)
225  {
226  disableWidgets();
227  }
228 }
229 
231 {
232  double ymin = -MAX_DOUBLE;
233  double ymax = MAX_DOUBLE;
234 
235  if (ui->checkBoxMax->isChecked() && !ui->lineLimitMax->text().isEmpty())
236  {
237  bool ok = false;
238  double val = ui->lineLimitMax->text().toDouble(&ok);
239  if (ok)
240  {
241  ymax = val;
242  }
243  }
244 
245  if (ui->checkBoxMin->isChecked() && !ui->lineLimitMin->text().isEmpty())
246  {
247  bool ok = false;
248  double val = ui->lineLimitMin->text().toDouble(&ok);
249  if (ok)
250  {
251  ymin = val;
252  }
253  }
254 
255  if (ymin > ymax)
256  {
257  // swap
258  ui->lineLimitMin->setText(QString::number(ymax));
259  ui->lineLimitMax->setText(QString::number(ymin));
260  std::swap(ymin, ymax);
261  }
262 
263  Range range;
264  range.min = ymin;
265  range.max = ymax;
267 }
268 
270 {
271  if (text.size() == 7 && text[0] == '#' && QColor::isValidColor(text))
272  {
273  QColor col(text);
274  _color_wheel->setColor(col);
275  _color_preview->setColor(col);
276  }
277 }
278 
280 {
281  if (checked)
282  {
283  _plotwidget->changeCurvesStyle(PlotWidgetBase::LINES);
284  }
285 }
286 
288 {
289  if (checked)
290  {
291  _plotwidget->changeCurvesStyle(PlotWidgetBase::DOTS);
292  }
293 }
294 
296 {
297  if (checked)
298  {
299  _plotwidget->changeCurvesStyle(PlotWidgetBase::LINES_AND_DOTS);
300  }
301 }
302 
304 {
305  if (checked)
306  {
307  _plotwidget->changeCurvesStyle(PlotWidgetBase::STICKS);
308  }
309 }
310 
312 {
313  ui->lineLimitMax->setEnabled(checked);
314  updateLimits();
315 }
316 
318 {
319  ui->lineLimitMin->setEnabled(checked);
320  updateLimits();
321 }
322 
324 {
325  Range no_limits;
326  no_limits.min = -MAX_DOUBLE;
327  no_limits.max = +MAX_DOUBLE;
328 
329  _plotwidget->setCustomAxisLimits(no_limits);
330 
331  auto range_x = _plotwidget->getVisualizationRangeX();
332  Range limits = _plotwidget->getVisualizationRangeY(range_x);
333 
334  ui->lineLimitMin->setText(QString::number(limits.min));
335  ui->lineLimitMax->setText(QString::number(limits.max));
336 }
337 
339 {
340  updateLimits();
341 }
342 
344 {
345  updateLimits();
346 }
347 
349 {
350  this->reject();
351 }
352 
354 {
355  QDomDocument doc;
357  auto elem = _plotwidget->xmlSaveState(doc);
359  this->accept();
360 }
361 
362 EditorRowWidget::EditorRowWidget(QString text, QColor color) : QWidget()
363 {
364  setMouseTracking(true);
365  const QSize button_size(20, 20);
366 
367  auto layout = new QHBoxLayout();
368  setLayout(layout);
369  _text = new QLabel(text, this);
370 
371  _empty_spacer = new QWidget();
372  _empty_spacer->setFixedSize(button_size);
373 
374  setColor(color);
375  _delete_button = new QPushButton(this);
376  _delete_button->setFlat(true);
377  _delete_button->setFixedSize(button_size);
378  auto icon = QIcon(":/resources/svg/trash.svg");
379  _delete_button->setStyleSheet("QPushButton:hover{ border: 0px;}");
380 
381  _delete_button->setIcon(icon);
382  _delete_button->setIconSize(button_size);
383 
384  layout->addWidget(_empty_spacer);
385  layout->addWidget(_delete_button);
386  layout->addWidget(_text);
387 
388  _delete_button->setHidden(true);
389 
390  connect(_delete_button, &QPushButton::clicked, this,
391  [this]() { emit deleteRow(this); });
392 }
393 
395 {
396  _delete_button->setHidden(false);
397  _empty_spacer->setHidden(true);
398 }
399 
401 {
402  _delete_button->setHidden(true);
403  _empty_spacer->setHidden(false);
404 }
405 
406 QString EditorRowWidget::text() const
407 {
408  return _text->text();
409 }
410 
412 {
413  setStyleSheet(QString("color: %1;").arg(color.name()));
414  _color = color;
415 }
416 
418 {
419  return _color;
420 }
421 
423 {
424  auto selected = ui->listWidget->selectedItems();
425  if (selected.size() == 0 || ui->listWidget->count() == 0)
426  {
427  ui->widgetColor->setEnabled(false);
428  ui->editColotText->setText("#000000");
429  return;
430  }
431 
432  ui->widgetColor->setEnabled(true);
433 
434  if (selected.size() != 1)
435  {
436  return;
437  }
438 
439  auto item = selected.front();
440  auto row_widget = dynamic_cast<EditorRowWidget*>(ui->listWidget->itemWidget(item));
441  if (row_widget)
442  {
443  ui->editColotText->setText(row_widget->color().name());
444  }
445 }
Display an analog widget that allows the selection of a HSV color.
Definition: color_wheel.hpp:35
void on_editColotText_textChanged(const QString &arg1)
double max
Definition: plotdatabase.h:32
PlotWidget * _plotwidget_origin
bool xmlLoadState(QDomElement &element, bool autozoom=true)
Definition: plotwidget.cpp:720
PlotWidget * _plotwidget
void setColor(QColor color)
QPushButton * _delete_button
Range getVisualizationRangeY(Range range_X) const override
virtual PJ::Range getVisualizationRangeX() const
const double MAX_DOUBLE
void leaveEvent(QEvent *ev) override
void on_checkBoxMax_toggled(bool checked)
void on_radioLines_toggled(bool checked)
void setColor(const QColor &c)
Set current color.
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition: core.h:1736
QColor color() const
void deleteRow(QWidget *_this)
QDomElement xmlSaveState(QDomDocument &doc) const
Definition: plotwidget.cpp:634
void onColorChanged(QColor c)
void changeCurvesStyle(CurveStyle style)
Ui::PlotWidgetEditor * ui
QRectF _bounding_rect_original
void setColor(QColor c)
Set current color.
color_widgets::ColorPreview * _color_preview
void zoomOut(bool emit_signal)
void on_radioBoth_toggled(bool checked)
Definition: lz4.c:1706
void on_radioPoints_toggled(bool checked)
NLOHMANN_BASIC_JSON_TPL_DECLARATION void swap(nlohmann::NLOHMANN_BASIC_JSON_TPL &j1, nlohmann::NLOHMANN_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name) is_nothrow_move_constructible< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< nlohmann::NLOHMANN_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition: json.hpp:21884
void onDeleteRow(QWidget *w)
void removeCurve(const QString &title) override
Definition: plotwidget.cpp:419
static void block(LexState *ls)
Definition: lparser.c:1293
void setCustomAxisLimits(Range range)
double timeOffset() const
Definition: plotwidget.h:59
QString text() const
CurveStyle curveStyle() const
void enterEvent(QEvent *ev) override
void on_lineLimitMax_textChanged(const QString &text)
QWidget * _empty_spacer
color
Definition: color.h:23
void on_checkBoxMin_toggled(bool checked)
QRectF currentBoundingRect() const
void on_radioSticks_toggled(bool checked)
void on_changeTimeOffset(double offset)
Range customAxisLimit() const
void setContextMenuEnabled(bool enabled)
Definition: plotwidget.cpp:139
double min
Definition: plotdatabase.h:31
PlotwidgetEditor(PlotWidget *plotwidget, QWidget *parent=nullptr)
void setZoomRectangle(QRectF rect, bool emit_signal)
Definition: plotwidget.cpp:945
void on_listWidget_itemSelectionChanged()
void on_lineLimitMin_textChanged(const QString &text)
PlotDataMapRef & datamap()
Definition: plotwidget.h:64
color_widgets::ColorWheel * _color_wheel
std::map< QString, QColor > getCurveColors() const
EditorRowWidget(QString text, QColor color)
void on_changeCurveColor(const QString &curve_name, QColor new_color)


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Jun 19 2023 03:01:38