plotwidget_editor.cpp
Go to the documentation of this file.
1 #include "plotwidget_editor.h"
2 #include "ui_plotwidget_editor.h"
3 #include <QHBoxLayout>
4 #include <QVBoxLayout>
5 #include <QSettings>
6 #include <QListWidgetItem>
7 #include <QLabel>
8 #include <QMouseEvent>
9 #include <QPushButton>
10 #include <QDialogButtonBox>
11 #include <QDebug>
12 #include "qwt_text.h"
13 
15 
16 PlotwidgetEditor::PlotwidgetEditor(PlotWidget *plotwidget, QWidget *parent) :
17  QDialog(parent),
18  ui(new Ui::PlotWidgetEditor),
19  _plotwidget_origin(plotwidget)
20 {
21  ui->setupUi(this);
22 
23  installEventFilter(this);
24 
25 // setWindowFlags(windowFlags() | Qt::FramelessWindowHint);
26 
28 
29  QDomDocument doc;
30  auto saved_state = plotwidget->xmlSaveState(doc);
31 
32  _plotwidget = new PlotWidget(plotwidget->datamap());
33  _plotwidget->xmlLoadState(saved_state);
34  _plotwidget->on_changeTimeOffset( plotwidget->timeOffset() );
36 
38 
39  auto layout = new QVBoxLayout();
40  ui->framePlotPreview->setLayout(layout);
41  layout->addWidget(_plotwidget);
42  layout->setMargin(6);
43 
44  _plotwidget->zoomOut(false);
45 
46  setupTable();
47 
48  QSettings settings;
49  restoreGeometry(settings.value("PlotwidgetEditor.geometry").toByteArray());
50 
52  {
53  ui->radioLines->setChecked(true);
54  }
56  {
57  ui->radioPoints->setChecked(true);
58  }
59  else {
60  ui->radioBoth->setChecked(true);
61  }
62 
63  ui->lineLimitMax->setValidator(new QDoubleValidator(this));
64  ui->lineLimitMin->setValidator(new QDoubleValidator(this));
65 
66  auto ylimits = _plotwidget->customAxisLimit();
67  auto range_x = _plotwidget->getMaximumRangeX();
68  Range suggested_limits = _plotwidget->getMaximumRangeY(range_x);
69 
70  if( ylimits.min != -MAX_DOUBLE)
71  {
72  ui->checkBoxMin->setChecked(true);
73  ui->lineLimitMin->setText(QString::number(ylimits.min));
74  }
75  else{
76  ui->lineLimitMin->setText(QString::number(suggested_limits.min));
77  }
78 
79  if( ylimits.max != MAX_DOUBLE)
80  {
81  ui->checkBoxMax->setChecked(true);
82  ui->lineLimitMax->setText(QString::number(ylimits.max));
83  }
84  else{
85  ui->lineLimitMax->setText(QString::number(suggested_limits.max));
86  }
87 
88  //ui->listWidget->widget_background_disabled("QListView::item:selected { background: #ddeeff; }");
89 
90  if( ui->listWidget->count() != 0 ){
91  ui->listWidget->item(0)->setSelected(true);
92  }
93 }
94 
96 {
97  QSettings settings;
98  settings.setValue("PlotwidgetEditor.geometry", saveGeometry());
99 
100  delete ui;
101 }
102 
103 
105 {
106  auto selected = ui->listWidget->selectedItems();
107  if( selected.size() != 1)
108  {
109  return;
110  }
111  auto item = selected.front();
112  if( item ){
113  auto row_widget = dynamic_cast<EditorRowWidget*>( ui->listWidget->itemWidget(item) );
114  auto name = row_widget->text();
115  if( row_widget->color() != c)
116  {
117  row_widget->setColor( c );
118  }
119 
120  _plotwidget->on_changeCurveColor( item->data(Qt::UserRole).toString(), c );
121  }
122 }
123 
125 {
126  auto wheel_layout = new QVBoxLayout();
127  wheel_layout->setMargin(0);
128  wheel_layout->setSpacing(5);
129  ui->widgetWheel->setLayout( wheel_layout );
131  wheel_layout->addWidget(_color_wheel);
132 
134  _color_preview->setMaximumHeight(25);
135 
136  wheel_layout->addWidget(_color_preview);
137 
140 
143 
145  this, [this](QColor col)
146  {
147  QSignalBlocker block( ui->editColotText );
148  ui->editColotText->setText( col.name() );
149  });
150 
151  _color_wheel->setColor(Qt::blue);
152 }
153 
155 {
156  int row_count = ui->listWidget->count();
157  for(int row = 0; row < row_count; row++ )
158  {
159  auto item = ui->listWidget->item(row);
160  auto widget = ui->listWidget->itemWidget(item);
161  if( widget == w )
162  {
163  QString curve = dynamic_cast<EditorRowWidget*>(w)->text();
164 
165  ui->listWidget->takeItem(row);
166  _plotwidget->removeCurve(curve);
167  widget->deleteLater();
168  row_count--;
169  break;
170  }
171  }
172  if( row_count == 0)
173  {
174  disableWidgets();
175  }
176  _plotwidget->replot();
177 }
178 
180 {
181  ui->widgetColor->setEnabled(false);
182  _color_wheel->setEnabled(false);
183  _color_preview->setEnabled(false);
184 
185  ui->frameLimits->setEnabled(false);
186  ui->frameStyle->setEnabled(false);
187 }
188 
190 {
191  std::map<QString, QColor> colors = _plotwidget->getCurveColors();
192 
193  int row = 0;
194  for (auto& it : colors)
195  {
196  auto alias = it.first;
197  auto color = it.second;
198  auto item = new QListWidgetItem();
199  // even if it is not visible, we store here the original name (not alias)
200  item->setData(Qt::UserRole, it.first );
201 
202  ui->listWidget->addItem( item );
203  auto plot_row = new EditorRowWidget(alias, color) ;
204  item->setSizeHint( plot_row->sizeHint() );
205  ui->listWidget->setItemWidget(item, plot_row );
206 
207  connect(plot_row, &EditorRowWidget::deleteRow, this, [this](QWidget* w) { onDeleteRow(w); });
208  row++;
209  }
210  if( row == 0 )
211  {
212  disableWidgets();
213  }
214 }
215 
217 {
218  double ymin = -MAX_DOUBLE;
219  double ymax = MAX_DOUBLE;
220 
221  if (ui->checkBoxMax->isChecked() && !ui->lineLimitMax->text().isEmpty())
222  {
223  bool ok = false;
224  double val = ui->lineLimitMax->text().toDouble(&ok);
225  if(ok) { ymax = val; }
226  }
227 
228  if (ui->checkBoxMin->isChecked() && !ui->lineLimitMin->text().isEmpty())
229  {
230  bool ok = false;
231  double val = ui->lineLimitMin->text().toDouble(&ok);
232  if(ok) { ymin = val; }
233  }
234 
235  if (ymin > ymax)
236  {
237  // swap
238  ui->lineLimitMin->setText(QString::number(ymax));
239  ui->lineLimitMax->setText(QString::number(ymin));
240  std::swap(ymin, ymax);
241  }
242 
243  Range range;
244  range.min = ymin;
245  range.max = ymax;
247 }
248 
250 {
251  if( text.size() == 7 && text[0] == '#' && QColor::isValidColor(text))
252  {
253  QColor col(text);
254  _color_wheel->setColor( col );
255  _color_preview->setColor( col );
256  }
257 }
258 
260 {
261  if(checked)
262  {
264  }
265 }
266 
267 
269 {
270  if(checked)
271  {
273  }
274 }
275 
277 {
278  if(checked)
279  {
281  }
282 }
283 
285 {
286  ui->lineLimitMax->setEnabled(checked);
287  updateLimits();
288 }
289 
291 {
292  ui->lineLimitMin->setEnabled(checked);
293  updateLimits();
294 }
295 
297 {
298  Range no_limits;
299  no_limits.min = -MAX_DOUBLE;
300  no_limits.max = +MAX_DOUBLE;
301 
302  _plotwidget->setCustomAxisLimits(no_limits);
303 
304  auto range_x = _plotwidget->getMaximumRangeX();
305  Range limits = _plotwidget->getMaximumRangeY(range_x);
306 
307  ui->lineLimitMin->setText(QString::number(limits.min));
308  ui->lineLimitMax->setText(QString::number(limits.max));
309 }
310 
312 {
313  updateLimits();
314 }
315 
317 {
318  updateLimits();
319 }
320 
322 {
323  this->reject();
324 }
325 
327 {
328  QDomDocument doc;
330  auto elem = _plotwidget->xmlSaveState(doc);
332  this->accept();
333 }
334 
335 EditorRowWidget::EditorRowWidget(QString text, QColor color): QWidget()
336 {
337  setMouseTracking(true);
338  const QSize button_size(20,20);
339 
340  auto layout = new QHBoxLayout();
341  setLayout( layout );
342  _text = new QLabel(text, this);
343 
344  _empty_spacer = new QWidget();
345  _empty_spacer->setFixedSize( button_size );
346 
347  setColor(color);
348  _delete_button = new QPushButton(this);
349  _delete_button->setFlat(true);
350  _delete_button->setFixedSize( button_size );
351  auto icon = QIcon(":/resources/svg/remove_red.svg");
352  _delete_button->setStyleSheet("QPushButton:hover{ border: 0px;}");
353 
354  _delete_button->setIcon( icon) ;
355  _delete_button->setIconSize( button_size );
356 
357  layout->addWidget(_empty_spacer);
358  layout->addWidget(_delete_button);
359  layout->addWidget(_text);
360 
361  _delete_button->setHidden(true);
362 
363  connect( _delete_button, &QPushButton::clicked, this, [this](){ emit deleteRow(this); });
364 }
365 
367 {
368  _delete_button->setHidden(false);
369  _empty_spacer->setHidden(true);
370 }
371 
373 {
374  _delete_button->setHidden(true);
375  _empty_spacer->setHidden(false);
376 }
377 
378 QString EditorRowWidget::text() const
379 {
380  return _text->text();
381 }
382 
384 {
385  setStyleSheet( QString("color: %1;").arg(color.name()));
386  _color = color;
387 }
388 
390 {
391  return _color;
392 }
393 
395 {
396  auto selected = ui->listWidget->selectedItems();
397  if( selected.size() == 0 || ui->listWidget->count() == 0)
398  {
399  ui->widgetColor->setEnabled(false);
400  ui->editColotText->setText( "#000000" );
401  return;
402  }
403 
404  ui->widgetColor->setEnabled(true);
405 
406  if( selected.size() != 1)
407  {
408  return;
409  }
410 
411  auto item = selected.front();
412  auto row_widget = dynamic_cast<EditorRowWidget*>( ui->listWidget->itemWidget(item) );
413  if( row_widget )
414  {
415  ui->editColotText->setText( row_widget->color().name() );
416  }
417 }
Display an analog widget that allows the selection of a HSV color.
Definition: color_wheel.hpp:35
QwtPlotCurve::CurveStyle curveStyle() const
Definition: plotwidget.h:109
void on_editColotText_textChanged(const QString &arg1)
double max
Definition: plotdata.h:24
void replot() override
QDomElement xmlSaveState(QDomDocument &doc) const
Definition: plotwidget.cpp:765
PlotWidget * _plotwidget_origin
QString text() const
PlotWidget * _plotwidget
std::map< QString, QColor > getCurveColors() const
void setColor(QColor color)
QColor color() const
QPushButton * _delete_button
QRectF canvasBoundingRect() const
Definition: plotwidget.cpp:979
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.
void deleteRow(QWidget *_this)
void onColorChanged(QColor c)
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)
#define max(A, B)
Definition: Socket.h:88
void on_radioPoints_toggled(bool checked)
void onDeleteRow(QWidget *w)
static void block(LexState *ls)
Definition: lparser.c:1295
void setCustomAxisLimits(Range range)
void enterEvent(QEvent *ev) override
const char * name
detail::named_arg< Char, T > arg(const Char *name, const T &arg)
Definition: core.h:1656
void on_lineLimitMax_textChanged(const QString &text)
void removeCurve(const QString &title)
Definition: plotwidget.cpp:524
bool xmlLoadState(QDomElement &element)
Definition: plotwidget.cpp:835
QWidget * _empty_spacer
void on_checkBoxMin_toggled(bool checked)
Range customAxisLimit() const
MQTTClient c
Definition: test10.c:1656
double timeOffset() const
Definition: plotwidget.h:102
void on_changeTimeOffset(double offset)
void setContextMenuEnabled(bool enabled)
Definition: plotwidget.cpp:227
Range getMaximumRangeY(Range range_X) const
double min
Definition: plotdata.h:23
PlotwidgetEditor(PlotWidget *plotwidget, QWidget *parent=nullptr)
void setZoomRectangle(QRectF rect, bool emit_signal)
void on_listWidget_itemSelectionChanged()
void on_lineLimitMin_textChanged(const QString &text)
PlotDataMapRef & datamap()
Definition: plotwidget.h:97
color_widgets::ColorWheel * _color_wheel
Range getMaximumRangeX() const
EditorRowWidget(QString text, QColor color)
void changeCurveStyle(QwtPlotCurve::CurveStyle style)
void on_changeCurveColor(const QString &curve_name, QColor new_color)


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:10