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 if (_plotwidget->curveStyle() == PlotWidgetBase::STEPS)
68  {
69  ui->radioSteps->setChecked(true);
70  }
71  else if (_plotwidget->curveStyle() == PlotWidgetBase::STEPSINV)
72  {
73  ui->radioStepsInv->setChecked(true);
74  }
75  else
76  {
77  ui->radioBoth->setChecked(true);
78  }
79 
80  ui->lineLimitMax->setValidator(new QDoubleValidator(this));
81  ui->lineLimitMin->setValidator(new QDoubleValidator(this));
82 
83  auto ylimits = _plotwidget->customAxisLimit();
84  auto range_x = _plotwidget->getVisualizationRangeX();
85  Range suggested_limits = _plotwidget->getVisualizationRangeY(range_x);
86 
87  if (ylimits.min != -MAX_DOUBLE)
88  {
89  ui->checkBoxMin->setChecked(true);
90  ui->lineLimitMin->setText(QString::number(ylimits.min));
91  }
92  else
93  {
94  ui->lineLimitMin->setText(QString::number(suggested_limits.min));
95  }
96 
97  if (ylimits.max != MAX_DOUBLE)
98  {
99  ui->checkBoxMax->setChecked(true);
100  ui->lineLimitMax->setText(QString::number(ylimits.max));
101  }
102  else
103  {
104  ui->lineLimitMax->setText(QString::number(suggested_limits.max));
105  }
106 
107  // ui->listWidget->widget_background_disabled("QListView::item:selected { background:
108  // #ddeeff; }");
109 
110  if (ui->listWidget->count() != 0)
111  {
112  ui->listWidget->item(0)->setSelected(true);
113  }
114 }
115 
117 {
118  QSettings settings;
119  settings.setValue("PlotwidgetEditor.geometry", saveGeometry());
120 
121  delete _plotwidget;
122  delete ui;
123 }
124 
126 {
127  auto selected = ui->listWidget->selectedItems();
128  if (selected.size() != 1)
129  {
130  return;
131  }
132  auto item = selected.front();
133  if (item)
134  {
135  auto row_widget = dynamic_cast<EditorRowWidget*>(ui->listWidget->itemWidget(item));
136  auto name = row_widget->text();
137  if (row_widget->color() != c)
138  {
139  row_widget->setColor(c);
140  }
141 
142  _plotwidget->on_changeCurveColor(item->data(Qt::UserRole).toString(), c);
143  }
144 }
145 
147 {
148  auto wheel_layout = new QVBoxLayout();
149  wheel_layout->setMargin(0);
150  wheel_layout->setSpacing(5);
151  ui->widgetWheel->setLayout(wheel_layout);
153  wheel_layout->addWidget(_color_wheel);
154 
156  _color_preview->setMaximumHeight(25);
157 
158  wheel_layout->addWidget(_color_preview);
159 
162 
165 
167  [this](QColor col) {
168  QSignalBlocker block(ui->editColotText);
169  ui->editColotText->setText(col.name());
170  });
171 
172  _color_wheel->setColor(Qt::blue);
173 }
174 
176 {
177  int row_count = ui->listWidget->count();
178  for (int row = 0; row < row_count; row++)
179  {
180  auto item = ui->listWidget->item(row);
181  auto widget = ui->listWidget->itemWidget(item);
182  if (widget == w)
183  {
184  QString curve = dynamic_cast<EditorRowWidget*>(w)->text();
185 
186  ui->listWidget->takeItem(row);
187  _plotwidget->removeCurve(curve);
188  widget->deleteLater();
189  row_count--;
190  break;
191  }
192  }
193  if (row_count == 0)
194  {
195  disableWidgets();
196  }
197  _plotwidget->replot();
198 }
199 
201 {
202  ui->widgetColor->setEnabled(false);
203  _color_wheel->setEnabled(false);
204  _color_preview->setEnabled(false);
205 
206  ui->frameLimits->setEnabled(false);
207  ui->frameStyle->setEnabled(false);
208 }
209 
211 {
212  std::map<QString, QColor> colors = _plotwidget->getCurveColors();
213 
214  int row = 0;
215  for (auto& it : colors)
216  {
217  auto alias = it.first;
218  auto color = it.second;
219  auto item = new QListWidgetItem();
220  // even if it is not visible, we store here the original name (not alias)
221  item->setData(Qt::UserRole, it.first);
222 
223  ui->listWidget->addItem(item);
224  auto plot_row = new EditorRowWidget(alias, color);
225  item->setSizeHint(plot_row->sizeHint());
226  ui->listWidget->setItemWidget(item, plot_row);
227 
228  connect(plot_row, &EditorRowWidget::deleteRow, this,
229  [this](QWidget* w) { onDeleteRow(w); });
230  row++;
231  }
232  if (row == 0)
233  {
234  disableWidgets();
235  }
236 }
237 
239 {
240  double ymin = -MAX_DOUBLE;
241  double ymax = MAX_DOUBLE;
242 
243  if (ui->checkBoxMax->isChecked() && !ui->lineLimitMax->text().isEmpty())
244  {
245  bool ok = false;
246  double val = ui->lineLimitMax->text().toDouble(&ok);
247  if (ok)
248  {
249  ymax = val;
250  }
251  }
252 
253  if (ui->checkBoxMin->isChecked() && !ui->lineLimitMin->text().isEmpty())
254  {
255  bool ok = false;
256  double val = ui->lineLimitMin->text().toDouble(&ok);
257  if (ok)
258  {
259  ymin = val;
260  }
261  }
262 
263  if (ymin > ymax)
264  {
265  // swap
266  ui->lineLimitMin->setText(QString::number(ymax));
267  ui->lineLimitMax->setText(QString::number(ymin));
268  std::swap(ymin, ymax);
269  }
270 
271  Range range;
272  range.min = ymin;
273  range.max = ymax;
275 }
276 
278 {
279  if (text.size() == 7 && text[0] == '#' && QColor::isValidColor(text))
280  {
281  QColor col(text);
282  _color_wheel->setColor(col);
283  _color_preview->setColor(col);
284  }
285 }
286 
288 {
289  if (checked)
290  {
291  _plotwidget->changeCurvesStyle(PlotWidgetBase::LINES);
292  }
293 }
294 
296 {
297  if (checked)
298  {
299  _plotwidget->changeCurvesStyle(PlotWidgetBase::DOTS);
300  }
301 }
302 
304 {
305  if (checked)
306  {
307  _plotwidget->changeCurvesStyle(PlotWidgetBase::LINES_AND_DOTS);
308  }
309 }
310 
312 {
313  if (checked)
314  {
315  _plotwidget->changeCurvesStyle(PlotWidgetBase::STICKS);
316  }
317 }
318 
320 {
321  if (checked)
322  {
323  _plotwidget->changeCurvesStyle(PlotWidgetBase::STEPS);
324  }
325 }
326 
328 {
329  if (checked)
330  {
331  _plotwidget->changeCurvesStyle(PlotWidgetBase::STEPSINV);
332  }
333 }
334 
336 {
337  ui->lineLimitMax->setEnabled(checked);
338  updateLimits();
339 }
340 
342 {
343  ui->lineLimitMin->setEnabled(checked);
344  updateLimits();
345 }
346 
348 {
349  Range no_limits;
350  no_limits.min = -MAX_DOUBLE;
351  no_limits.max = +MAX_DOUBLE;
352 
353  _plotwidget->setCustomAxisLimits(no_limits);
354 
355  auto range_x = _plotwidget->getVisualizationRangeX();
356  Range limits = _plotwidget->getVisualizationRangeY(range_x);
357 
358  ui->lineLimitMin->setText(QString::number(limits.min));
359  ui->lineLimitMax->setText(QString::number(limits.max));
360 }
361 
363 {
364  updateLimits();
365 }
366 
368 {
369  updateLimits();
370 }
371 
373 {
374  this->reject();
375 }
376 
378 {
379  QDomDocument doc;
381  auto elem = _plotwidget->xmlSaveState(doc);
383  this->accept();
384 }
385 
386 EditorRowWidget::EditorRowWidget(QString text, QColor color) : QWidget()
387 {
388  setMouseTracking(true);
389  const QSize button_size(20, 20);
390 
391  auto layout = new QHBoxLayout();
392  setLayout(layout);
393  _text = new QLabel(text, this);
394 
395  _empty_spacer = new QWidget();
396  _empty_spacer->setFixedSize(button_size);
397 
398  setColor(color);
399  _delete_button = new QPushButton(this);
400  _delete_button->setFlat(true);
401  _delete_button->setFixedSize(button_size);
402  auto icon = QIcon(":/resources/svg/trash.svg");
403  _delete_button->setStyleSheet("QPushButton:hover{ border: 0px;}");
404 
405  _delete_button->setIcon(icon);
406  _delete_button->setIconSize(button_size);
407 
408  layout->addWidget(_empty_spacer);
409  layout->addWidget(_delete_button);
410  layout->addWidget(_text);
411 
412  _delete_button->setHidden(true);
413 
414  connect(_delete_button, &QPushButton::clicked, this,
415  [this]() { emit deleteRow(this); });
416 }
417 
419 {
420  _delete_button->setHidden(false);
421  _empty_spacer->setHidden(true);
422 }
423 
425 {
426  _delete_button->setHidden(true);
427  _empty_spacer->setHidden(false);
428 }
429 
430 QString EditorRowWidget::text() const
431 {
432  return _text->text();
433 }
434 
436 {
437  setStyleSheet(QString("color: %1;").arg(color.name()));
438  _color = color;
439 }
440 
442 {
443  return _color;
444 }
445 
447 {
448  auto selected = ui->listWidget->selectedItems();
449  if (selected.size() == 0 || ui->listWidget->count() == 0)
450  {
451  ui->widgetColor->setEnabled(false);
452  ui->editColotText->setText("#000000");
453  return;
454  }
455 
456  ui->widgetColor->setEnabled(true);
457 
458  if (selected.size() != 1)
459  {
460  return;
461  }
462 
463  auto item = selected.front();
464  auto row_widget = dynamic_cast<EditorRowWidget*>(ui->listWidget->itemWidget(item));
465  if (row_widget)
466  {
467  ui->editColotText->setText(row_widget->color().name());
468  }
469 }
PlotwidgetEditor::on_radioPoints_toggled
void on_radioPoints_toggled(bool checked)
Definition: plotwidget_editor.cpp:295
color
color
Definition: color.h:16
color_widgets::ColorPreview::setColor
void setColor(const QColor &c)
Set current color.
Definition: color_preview.cpp:131
PlotwidgetEditor::onDeleteRow
void onDeleteRow(QWidget *w)
Definition: plotwidget_editor.cpp:175
EditorRowWidget::color
QColor color() const
Definition: plotwidget_editor.cpp:441
PlotwidgetEditor::on_radioStepsInv_toggled
void on_radioStepsInv_toggled(bool checked)
Definition: plotwidget_editor.cpp:327
MAX_DOUBLE
const double MAX_DOUBLE
Definition: plotwidget_editor.cpp:20
PlotwidgetEditor::on_lineLimitMax_textChanged
void on_lineLimitMax_textChanged(const QString &text)
Definition: plotwidget_editor.cpp:362
EditorRowWidget::setColor
void setColor(QColor color)
Definition: plotwidget_editor.cpp:435
PlotwidgetEditor::on_checkBoxMin_toggled
void on_checkBoxMin_toggled(bool checked)
Definition: plotwidget_editor.cpp:341
PlotwidgetEditor::updateLimits
void updateLimits()
Definition: plotwidget_editor.cpp:238
EditorRowWidget::leaveEvent
void leaveEvent(QEvent *ev) override
Definition: plotwidget_editor.cpp:424
PlotwidgetEditor::on_radioSticks_toggled
void on_radioSticks_toggled(bool checked)
Definition: plotwidget_editor.cpp:311
EditorRowWidget::_delete_button
QPushButton * _delete_button
Definition: plotwidget_editor.h:44
PlotWidget::customAxisLimit
Range customAxisLimit() const
Definition: plotwidget.cpp:1480
arg
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
Definition: core.h:1875
PlotWidget::setContextMenuEnabled
void setContextMenuEnabled(bool enabled)
Definition: plotwidget.cpp:139
PJ::PlotWidgetBase::curveStyle
CurveStyle curveStyle() const
Definition: plotwidget_base.cpp:499
PJ::PlotWidgetBase::getCurveColors
std::map< QString, QColor > getCurveColors() const
Definition: plotwidget_base.cpp:687
PlotWidget::on_changeTimeOffset
void on_changeTimeOffset(double offset)
Definition: plotwidget.cpp:1074
PlotwidgetEditor::PlotwidgetEditor
PlotwidgetEditor(PlotWidget *plotwidget, QWidget *parent=nullptr)
Definition: plotwidget_editor.cpp:22
EditorRowWidget::text
QString text() const
Definition: plotwidget_editor.cpp:430
EditorRowWidget::_text
QLabel * _text
Definition: plotwidget_editor.h:42
ok
ROSCPP_DECL bool ok()
PlotwidgetEditor::on_lineLimitMin_textChanged
void on_lineLimitMin_textChanged(const QString &text)
Definition: plotwidget_editor.cpp:367
PlotWidget
Definition: plotwidget.h:38
PlotwidgetEditor::on_pushButtonReset_clicked
void on_pushButtonReset_clicked()
Definition: plotwidget_editor.cpp:347
PJ::PlotWidgetBase::getVisualizationRangeX
virtual PJ::Range getVisualizationRangeX() const
Definition: plotwidget_base.cpp:189
PJ::PlotWidgetBase::replot
void replot()
Definition: plotwidget_base.cpp:780
PlotwidgetEditor::on_pushButtonSave_pressed
void on_pushButtonSave_pressed()
Definition: plotwidget_editor.cpp:377
PlotWidget::setZoomRectangle
void setZoomRectangle(QRectF rect, bool emit_signal)
Definition: plotwidget.cpp:964
PlotwidgetEditor::setupTable
void setupTable()
Definition: plotwidget_editor.cpp:210
PlotwidgetEditor::on_listWidget_itemSelectionChanged
void on_listWidget_itemSelectionChanged()
Definition: plotwidget_editor.cpp:446
EditorRowWidget
Definition: plotwidget_editor.h:22
PlotWidget::on_changeCurveColor
void on_changeCurveColor(const QString &curve_name, QColor new_color)
Definition: plotwidget.cpp:1174
PlotWidget::datamap
PlotDataMapRef & datamap()
Definition: plotwidget.h:64
color_widgets::ColorWheel
Display an analog widget that allows the selection of a HSV color.
Definition: color_wheel.hpp:35
Ui
Definition: cheatsheet_dialog.h:6
PlotwidgetEditor::_color_wheel
color_widgets::ColorWheel * _color_wheel
Definition: plotwidget_editor.h:94
PJ::Range::max
double max
Definition: plotdatabase.h:27
PlotwidgetEditor::on_editColotText_textChanged
void on_editColotText_textChanged(const QString &arg1)
Definition: plotwidget_editor.cpp:277
std::swap
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
color_widgets::ColorWheel::colorChanged
void colorChanged(QColor)
PlotwidgetEditor::_plotwidget_origin
PlotWidget * _plotwidget_origin
Definition: plotwidget_editor.h:97
PlotWidget::xmlLoadState
bool xmlLoadState(QDomElement &element, bool autozoom=true)
Definition: plotwidget.cpp:731
EditorRowWidget::_empty_spacer
QWidget * _empty_spacer
Definition: plotwidget_editor.h:45
PlotwidgetEditor::setupColorWidget
void setupColorWidget()
Definition: plotwidget_editor.cpp:146
PJ::Range
Definition: plotdatabase.h:24
EditorRowWidget::enterEvent
void enterEvent(QEvent *ev) override
Definition: plotwidget_editor.cpp:418
PlotWidget::getVisualizationRangeY
Range getVisualizationRangeY(Range range_X) const override
Definition: plotwidget.cpp:1122
block
static void block(LexState *ls)
Definition: lparser.c:1293
EditorRowWidget::_color
QColor _color
Definition: plotwidget_editor.h:43
PlotwidgetEditor::_plotwidget
PlotWidget * _plotwidget
Definition: plotwidget_editor.h:96
EditorRowWidget::EditorRowWidget
EditorRowWidget(QString text, QColor color)
Definition: plotwidget_editor.cpp:386
color_widgets::ColorPreview
Definition: color_preview.hpp:33
PlotwidgetEditor::~PlotwidgetEditor
~PlotwidgetEditor()
Definition: plotwidget_editor.cpp:116
EditorRowWidget::deleteRow
void deleteRow(QWidget *_this)
PlotWidget::xmlSaveState
QDomElement xmlSaveState(QDomDocument &doc) const
Definition: plotwidget.cpp:637
PlotwidgetEditor::onColorChanged
void onColorChanged(QColor c)
Definition: plotwidget_editor.cpp:125
PlotwidgetEditor::on_checkBoxMax_toggled
void on_checkBoxMax_toggled(bool checked)
Definition: plotwidget_editor.cpp:335
PlotwidgetEditor::on_radioLines_toggled
void on_radioLines_toggled(bool checked)
Definition: plotwidget_editor.cpp:287
PlotwidgetEditor::disableWidgets
void disableWidgets()
Definition: plotwidget_editor.cpp:200
plotwidget_editor.h
PlotwidgetEditor::on_pushButtonCancel_pressed
void on_pushButtonCancel_pressed()
Definition: plotwidget_editor.cpp:372
PlotwidgetEditor::_bounding_rect_original
QRectF _bounding_rect_original
Definition: plotwidget_editor.h:98
PlotwidgetEditor::on_radioSteps_toggled
void on_radioSteps_toggled(bool checked)
Definition: plotwidget_editor.cpp:319
PlotWidget::zoomOut
void zoomOut(bool emit_signal)
Definition: plotwidget.cpp:1319
PlotwidgetEditor::on_radioBoth_toggled
void on_radioBoth_toggled(bool checked)
Definition: plotwidget_editor.cpp:303
qwt_text.h
color_widgets::ColorWheel::setColor
void setColor(QColor c)
Set current color.
Definition: color_wheel.cpp:435
PJ::PlotWidgetBase::changeCurvesStyle
void changeCurvesStyle(CurveStyle style)
Definition: plotwidget_base.cpp:727
PlotwidgetEditor::ui
Ui::PlotWidgetEditor * ui
Definition: plotwidget_editor.h:92
PlotWidget::removeCurve
void removeCurve(const QString &title) override
Definition: plotwidget.cpp:419
PJ::Range::min
double min
Definition: plotdatabase.h:26
PlotwidgetEditor::_color_preview
color_widgets::ColorPreview * _color_preview
Definition: plotwidget_editor.h:95
PlotWidget::timeOffset
double timeOffset() const
Definition: plotwidget.h:59
PlotWidget::setCustomAxisLimits
void setCustomAxisLimits(Range range)
Definition: plotwidget.cpp:1473
PJ::PlotWidgetBase::currentBoundingRect
QRectF currentBoundingRect() const
Definition: plotwidget_base.cpp:294


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Aug 11 2024 02:24:23