plotwidget_transforms.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 
8 #include "ui_plotwidget_transforms.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 
21  : QDialog(plotwidget), ui(new Ui::plotwidget_transforms), _plotwidget_origin(plotwidget)
22 {
23  ui->setupUi(this);
24 
25  QDomDocument doc;
26  auto saved_state = plotwidget->xmlSaveState(doc);
27  _plotwidget = new PlotWidget(plotwidget->datamap(), this);
29  _plotwidget->xmlLoadState(saved_state);
30 
31  auto layout = new QVBoxLayout();
32  ui->framePlotPreview->setLayout(layout);
33  layout->addWidget(_plotwidget);
34  layout->setMargin(6);
35 
36  _plotwidget->zoomOut(false);
38 
39  setupTable();
40 
41  QSettings settings;
42  restoreGeometry(settings.value("DialogTransformEditor.geometry").toByteArray());
43 
44  ui->listCurves->setStyleSheet("QListView::item:selected { background: #ddeeff; }");
45 
46  auto names = TransformFactory::registeredTransforms();
47 
48  for (const auto& name : names)
49  {
50  ui->listTransforms->addItem(QString::fromStdString(name));
51  }
52 
53  if (ui->listCurves->count() != 0)
54  {
55  ui->listCurves->item(0)->setSelected(true);
56  }
57 }
58 
60 {
61  std::map<QString, QColor> colors = _plotwidget->getCurveColors();
62 
63  int row = 0;
64  for (auto& it : colors)
65  {
66  auto curve_it = _plotwidget->curveFromTitle(it.first);
67  auto name = QString::fromStdString(curve_it->src_name);
68  auto color = it.second;
69  auto item = new QListWidgetItem();
70  // item->setForeground(color);
71  ui->listCurves->addItem(item);
72  auto plot_row = new RowWidget(name, color);
73  item->setSizeHint(plot_row->sizeHint());
74  ui->listCurves->setItemWidget(item, plot_row);
75 
76  row++;
77  }
78 }
79 
81 {
82  QSettings settings;
83  settings.setValue("DialogTransformEditor.geometry", saveGeometry());
84 
85  delete ui;
86 }
87 
89 {
90  auto layout = new QHBoxLayout();
91  setLayout(layout);
92  _text = new QLabel(text, this);
93 
94  setStyleSheet(QString("color: %1;").arg(color.name()));
95  _color = color;
96 
97  layout->addWidget(_text);
98 }
99 
101 {
102  return _text->text();
103 }
104 
106 {
107  return _color;
108 }
109 
111 {
112  auto selected = ui->listCurves->selectedItems();
113  if (selected.size() != 1)
114  {
115  return;
116  }
117  auto item = selected.front();
118  auto row_widget = dynamic_cast<RowWidget*>(ui->listCurves->itemWidget(item));
119  auto curve_name = row_widget->text();
120 
121  auto curve_it = _plotwidget->curveFromTitle(curve_name);
122  int transform_row = 0;
123  if (auto ts = dynamic_cast<TransformedTimeseries*>(curve_it->curve->data()))
124  {
125  if (ts->transform())
126  {
127  for (int row = 1; row < ui->listTransforms->count(); row++)
128  {
129  if (ui->listTransforms->item(row)->text() == ts->transformName())
130  {
131  transform_row = row;
132  break;
133  }
134  }
135  }
136  }
137 
138  int selected_row = -1;
139  auto selected_transforms = ui->listTransforms->selectedItems();
140  if (selected_transforms.size() == 1)
141  {
142  selected_row = ui->listTransforms->row(selected_transforms.front());
143  }
144 
145  if (selected_row == transform_row)
146  {
147  // force callback
149  }
150  else
151  {
152  ui->listTransforms->item(transform_row)->setSelected(true);
153  }
154 }
155 
157 {
158  auto selected_curves = ui->listCurves->selectedItems();
159  if (selected_curves.size() != 1)
160  {
161  return;
162  }
163  auto row_widget =
164  dynamic_cast<RowWidget*>(ui->listCurves->itemWidget(selected_curves.front()));
165 
166  QString curve_name = row_widget->text();
167 
168  auto selected_transforms = ui->listTransforms->selectedItems();
169  if (selected_transforms.size() != 1)
170  {
171  return;
172  }
173  QString transform_ID = selected_transforms.front()->text();
174 
175  auto curve_info = _plotwidget->curveFromTitle(curve_name);
176  auto qwt_curve = curve_info->curve;
177  auto ts = dynamic_cast<TransformedTimeseries*>(curve_info->curve->data());
178 
179  QSignalBlocker block(ui->lineEditAlias);
180 
181  if (transform_ID.isEmpty() || transform_ID == ui->listTransforms->item(0)->text())
182  {
183  ts->setTransform({});
184  ts->updateCache(true);
185  ui->stackedWidgetArguments->setCurrentIndex(0);
186 
187  ui->lineEditAlias->setText("");
188  ui->lineEditAlias->setEnabled(false);
189  qwt_curve->setTitle(curve_name);
190  }
191  else
192  {
193  ts->setTransform(transform_ID);
194  ts->updateCache(true);
195  ui->lineEditAlias->setEnabled(true);
196 
197  QString curve_title = qwt_curve->title().text();
198  if (ts->alias().isEmpty())
199  {
200  auto src_name = QString::fromStdString(curve_info->src_name);
201  auto new_title = QString("%1[%2]").arg(src_name).arg(transform_ID);
202  ts->setAlias(new_title);
203  }
204 
205  ui->lineEditAlias->setText(ts->alias());
206  qwt_curve->setTitle(ts->alias());
207 
208  auto widget = ts->transform()->optionsWidget();
209  int index = ui->stackedWidgetArguments->indexOf(widget);
210  if (index == -1 && widget)
211  {
212  index = ui->stackedWidgetArguments->addWidget(widget);
213  }
214 
215  ui->stackedWidgetArguments->setCurrentIndex(index);
216 
217  if (_connected_transform_widgets.count(widget) == 0)
218  {
219  connect(ts->transform().get(), &TransformFunction::parametersChanged, this, [=]() {
220  ts->updateCache(true);
221  if (ui->checkBoxAutoZoom->isChecked())
222  {
223  _plotwidget->zoomOut(false);
224  }
225  else
226  {
227  _plotwidget->replot();
228  }
229  });
230  _connected_transform_widgets.insert(widget);
231  }
232  }
233 
234  _plotwidget->zoomOut(false);
235 }
236 
238 {
239  this->reject();
240 }
241 
243 {
245 
246  QSettings settings;
247  bool autozoom_filter_applied =
248  settings.value("Preferences::autozoom_filter_applied", true).toBool();
249  QDomDocument doc;
250  auto elem = _plotwidget->xmlSaveState(doc);
251  _plotwidget_origin->xmlLoadState(elem, autozoom_filter_applied);
252 
253  if (autozoom_filter_applied)
254  {
255  _plotwidget_origin->zoomOut(false);
256  }
257 
258  this->accept();
259 }
260 
262 {
263  auto selected_curves = ui->listCurves->selectedItems();
264  if (selected_curves.size() != 1)
265  {
266  return;
267  }
268  auto row_widget =
269  dynamic_cast<RowWidget*>(ui->listCurves->itemWidget(selected_curves.front()));
270 
271  QString curve_name = row_widget->text();
272 
273  auto curve_it = _plotwidget->curveFromTitle(curve_name);
274  auto ts = dynamic_cast<TransformedTimeseries*>(curve_it->curve->data());
275 
276  curve_it->curve->setTitle(ui->lineEditAlias->text());
277 
278  if (ts && ts->transform())
279  {
280  ts->setAlias(ui->lineEditAlias->text());
281  }
282 
283  _plotwidget->replot();
284 }
DialogTransformEditor::~DialogTransformEditor
~DialogTransformEditor()
Definition: plotwidget_transforms.cpp:80
color
color
Definition: color.h:16
DialogTransformEditor::_plotwidget
PlotWidget * _plotwidget
Definition: plotwidget_transforms.h:41
DialogTransformEditor::on_lineEditAlias_editingFinished
void on_lineEditAlias_editingFinished()
Definition: plotwidget_transforms.cpp:261
DialogTransformEditor::on_listCurves_itemSelectionChanged
void on_listCurves_itemSelectionChanged()
Definition: plotwidget_transforms.cpp:110
DialogTransformEditor::RowWidget::_color
QColor _color
Definition: plotwidget_transforms.h:58
DialogTransformEditor::RowWidget
Definition: plotwidget_transforms.h:48
DialogTransformEditor::RowWidget::color
QColor color() const
Definition: plotwidget_transforms.cpp:105
DialogTransformEditor::_connected_transform_widgets
std::set< QWidget * > _connected_transform_widgets
Definition: plotwidget_transforms.h:44
DialogTransformEditor::setupTable
void setupTable()
Definition: plotwidget_transforms.cpp:59
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
TransformedTimeseries
Definition: timeseries_qwt.h:74
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
DialogTransformEditor::RowWidget::_text
QLabel * _text
Definition: plotwidget_transforms.h:57
PlotWidget
Definition: plotwidget.h:38
PJ::PlotWidgetBase::replot
void replot()
Definition: plotwidget_base.cpp:780
PlotWidget::datamap
PlotDataMapRef & datamap()
Definition: plotwidget.h:64
DialogTransformEditor::_plotwidget_origin
PlotWidget * _plotwidget_origin
Definition: plotwidget_transforms.h:42
Ui
Definition: cheatsheet_dialog.h:6
PJ::PlotWidgetBase::curveFromTitle
CurveInfo * curveFromTitle(const QString &title)
Definition: plotwidget_base.cpp:737
TransformedTimeseries::setAlias
void setAlias(QString alias)
Definition: timeseries_qwt.cpp:134
plotwidget_transforms.h
PlotWidget::xmlLoadState
bool xmlLoadState(QDomElement &element, bool autozoom=true)
Definition: plotwidget.cpp:731
DialogTransformEditor::on_listTransforms_itemSelectionChanged
void on_listTransforms_itemSelectionChanged()
Definition: plotwidget_transforms.cpp:156
block
static void block(LexState *ls)
Definition: lparser.c:1293
DialogTransformEditor::on_pushButtonCancel_clicked
void on_pushButtonCancel_clicked()
Definition: plotwidget_transforms.cpp:237
DialogTransformEditor::DialogTransformEditor
DialogTransformEditor(PlotWidget *plotwidget)
Definition: plotwidget_transforms.cpp:20
PJ::PlotWidgetBase::CurveInfo::curve
QwtPlotCurve * curve
Definition: plotwidget_base.h:43
DialogTransformEditor::on_pushButtonSave_clicked
void on_pushButtonSave_clicked()
Definition: plotwidget_transforms.cpp:242
DialogTransformEditor::RowWidget::RowWidget
RowWidget(QString text, QColor color)
Definition: plotwidget_transforms.cpp:88
PlotWidget::xmlSaveState
QDomElement xmlSaveState(QDomDocument &doc) const
Definition: plotwidget.cpp:637
DialogTransformEditor::RowWidget::text
QString text() const
Definition: plotwidget_transforms.cpp:100
PlotWidget::zoomOut
void zoomOut(bool emit_signal)
Definition: plotwidget.cpp:1319
qwt_text.h
PlotWidget::timeOffset
double timeOffset() const
Definition: plotwidget.h:59
DialogTransformEditor::ui
Ui::plotwidget_transforms * ui
Definition: plotwidget_transforms.h:39


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45