StaticPlotterWidgetController.cpp
Go to the documentation of this file.
2 
3 
5 
6 #include <QStackedLayout>
7 #pragma GCC diagnostic push
8 #pragma GCC diagnostic ignored "-Wpedantic"
9 #include <qwt_legend.h>
10 #include <qwt_plot_magnifier.h>
11 #include <qwt_plot_panner.h>
12 #include <qwt_series_data.h>
13 #include <qwt_plot_canvas.h>
14 #include <qwt_legend_label.h>
15 #pragma GCC diagnostic pop
16 #include <QToolBar>
17 
18 namespace armarx
19 {
20 
22  customToolbar(nullptr)
23 
24  {
25  ui.setupUi(getWidget());
26  plotter = new QwtPlot(ui.plotWidget);
27  QStackedLayout* stackedLayout = new QStackedLayout(ui.plotWidget);
28  stackedLayout->addWidget(plotter);
29  ////////////////
30  // Setup Plotter
31  ///////////////
32  // panning with the left mouse button
33  (void) new QwtPlotPanner(plotter->canvas());
34 
35  // zoom in/out with the wheel
36  /*QwtPlotMagnifier* magnifier = */new QwtPlotMagnifier(plotter->canvas());
37 
38  // plotter->canvas()->setPaintAttribute(QwtPlotCanvas::BackingStore, false); //increases performance for incremental drawing
39 
40  QwtLegend* legend = new QwtLegend;
41  legend->setDefaultItemMode(QwtLegendData::Mode::Checkable);
42  plotter->insertLegend(legend, QwtPlot::BottomLegend);
43 
44 
45  plotter->setAxisTitle(QwtPlot::xBottom, "Time (in sec)");
46  plotter->enableAxis(QwtPlot::yLeft, true);
47 
48  // plotter->enableAxis(QwtPlot::yRight, true);
49  plotter->setAxisAutoScale(QwtPlot::yLeft, true);
50  plotter->setAxisAutoScale(QwtPlot::xBottom, true);
51  plotter->setAutoReplot();
52 
53  // plotter->setCanvasBackground(* new QBrush(Qt::white));
54 
55  connect(plotter, SIGNAL(legendChecked(QwtPlotItem*, bool)),
56  SLOT(showCurve(QwtPlotItem*, bool)));
57 
58  connect(this, SIGNAL(plotAdded(QString)),
59  this,
60  SLOT(addToPlotList(QString)));
61 
62  connect(ui.listWidgetPlots, SIGNAL(currentTextChanged(QString)),
63  this,
64  SLOT(changePlot(QString)));
65 
66  // connect(&timer, SIGNAL(timeout()), this, SLOT(updateGraph()));
67  // stackedLayout = new QStackedLayout(widget);
68  // stackedLayout->addWidget(plotter);
69 
70  }
71 
73  {
74  usingTopic("StaticPlotter");
75  // offeringTopic("StaticPlotter");
76  }
77 
79  {
80  // topicPrx = getTopic<StaticPlotterInterfacePrx>("StaticPlotter");
81  // task = new SimpleRunningTask<>([&]
82  // {
83  // sleep(1);
84  // ARMARX_INFO << "Sending";
85 
86  // Vector2fSeq data {{1, 0},
87  // {200, 300},
88  // {300, 100}
89  // };
90  // Vector2fSeq data2 {{1, 0},
91  // {200, 500},
92  // {340, 600}
93  // };
94  // topicPrx->addPlot("TestCurve", {{"curve", data}, {"curve2", data2}});
95  // topicPrx->addPlotWithTimestampVector("TestCurve2", {0, 1, 2}, {{"curve", {1, 45, 8}}});
96  // });
97  // task->start();
98  }
99 
100  void StaticPlotterWidgetController::showCurve(QwtPlotItem* item, bool on)
101  {
102  item->setVisible(on);
103  QwtLegend* lgd = qobject_cast<QwtLegend*>(plotter->legend());
104 
105  QList<QWidget*> legendWidgets =
106  lgd->legendWidgets(plotter->itemToInfo(item));
107 
108  if (legendWidgets.size() == 1)
109  {
110  QwtLegendLabel* legendLabel =
111  qobject_cast<QwtLegendLabel*>(legendWidgets[0]);
112 
113  if (legendLabel)
114  {
115  legendLabel->setChecked(on);
116  }
117  }
118  plotter->replot();
119  }
120 
122  {
123  ui.listWidgetPlots->clear();
124  plotter->detachItems();
125  plotter->replot();
126  std::unique_lock lock(dataMutex);
127  plotsMap.clear();
128  }
129 
131  {
132  if (ui.listWidgetPlots->findItems(plotName, Qt::MatchExactly).isEmpty())
133  {
134  ui.listWidgetPlots->addItem(plotName);
135  }
136  else if (ui.listWidgetPlots->currentItem() != NULL && ui.listWidgetPlots->currentItem()->text() == plotName)
137  {
138  changePlot(plotName);
139  }
140  }
141 
143  {
144  if (plotName.isEmpty())
145  {
146  return;
147  }
148  std::unique_lock lock(dataMutex);
149  auto it = plotsMap.find(plotName);
150  if (it == plotsMap.end())
151  {
152  ARMARX_INFO << "Did not find plot with name " << plotName;
153  return;
154  }
155  ARMARX_INFO << "Changing plot";
156  plotter->detachItems();
157  int i = 0;
158  for (auto& elem : it->second)
159  {
160  Vector2fSeq points = elem.second;
161  QVector<QPointF> pointList;
162  pointList.reserve(points.size());
163 
164  for (Vector2f& point : points)
165  {
166  pointList.push_back({point.e0, point.e1});
167  }
168  QwtSeriesData<QPointF>* pointSeries = new QwtPointSeriesData(pointList);
169  QwtPlotCurve* curve = createCurve(QString::fromStdString(elem.first), QColor(Qt::GlobalColor(i % 15 + 7)));
170  curve->setData(pointSeries);
171  curve->attach(plotter);
172  showCurve(curve, true);
173  i++;
174  }
175  plotter->replot();
176  }
177 
178 
179 
180 
181 
183  {
184  }
185 
187  {
188  }
189 
190  void StaticPlotterWidgetController::addPlot(const std::string& plotName, const StringVector2fSeqDict& plotsData, const Ice::Current&)
191  {
192  ARMARX_CHECK_EXPRESSION(!plotName.empty());
193  ARMARX_CHECK_EXPRESSION(!plotsData.empty());
194  QString qplotName = QString::fromStdString(plotName);
195  {
196  std::unique_lock lock(dataMutex);
197  plotsMap[qplotName] = plotsData;
198  }
199  emit plotAdded(qplotName);
200 
201  }
202 
203 
204  void StaticPlotterWidgetController::addPlotWithTimestampVector(const std::string& plotName, const Ice::FloatSeq& timestamps, const StringFloatSeqDict& plotsData, const Ice::Current& c)
205  {
206  StringVector2fSeqDict plotsDataMap;
207  for (auto& elem : plotsData)
208  {
209  Vector2fSeq data;
210  ARMARX_CHECK_EQUAL(timestamps.size(), elem.second.size());
211  for (size_t i = 0; i < timestamps.size(); ++i)
212  {
213  data.push_back({timestamps.at(i), elem.second.at(i)});
214  }
215  plotsDataMap[elem.first] = data;
216  }
217  addPlot(plotName, plotsDataMap, c);
218  }
219 
220 
221  QwtPlotCurve* StaticPlotterWidgetController::createCurve(const QString& label, QColor color)
222  {
223  QwtPlotCurve* curve = new QwtPlotCurve(label);
224  curve->setRenderHint(QwtPlotItem::RenderAntialiased);
225  curve->setPen(color);
226  curve->setStyle(QwtPlotCurve::Lines);
227  curve->setPaintAttribute(QwtPlotCurve::ClipPolygons, true);
228 
229  // showCurve(curve, true);
230  return curve;
231  }
232 
233 
235  {
236  if (customToolbar)
237  {
238  if (parent != customToolbar->parent())
239  {
240  customToolbar->setParent(parent);
241  }
242 
243  return customToolbar;
244  }
245 
246  customToolbar = new QToolBar(parent);
247  customToolbar->setIconSize(QSize(16, 16));
248  customToolbar->addAction(QIcon(":/icons/Trash.svg"), "Delete Plots", this, SLOT(clearPlots()));
249 
250  return customToolbar;
251  }
252 
253 } // namespace armarx
armarx::StaticPlotterWidgetController::plotAdded
void plotAdded(QString plotName)
armarx::StaticPlotterWidgetController::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: StaticPlotterWidgetController.cpp:78
armarx::StaticPlotterWidgetController::addToPlotList
void addToPlotList(QString plotName)
Definition: StaticPlotterWidgetController.cpp:130
armarx::StaticPlotterWidgetController::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: StaticPlotterWidgetController.cpp:72
armarx::StaticPlotterWidgetController::changePlot
void changePlot(QString plotName)
Definition: StaticPlotterWidgetController.cpp:142
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
armarx::StaticPlotterWidgetController::addPlotWithTimestampVector
void addPlotWithTimestampVector(const std::string &plotName, const Ice::FloatSeq &timestamps, const StringFloatSeqDict &plotsData, const Ice::Current &) override
Definition: StaticPlotterWidgetController.cpp:204
armarx::StaticPlotterWidgetController::clearPlots
void clearPlots()
Definition: StaticPlotterWidgetController.cpp:121
armarx::StaticPlotterWidgetController::addPlot
void addPlot(const std::string &plotName, const StringVector2fSeqDict &plotsData, const Ice::Current &) override
Definition: StaticPlotterWidgetController.cpp:190
armarx::StaticPlotterWidgetController::getCustomTitlebarWidget
QPointer< QWidget > getCustomTitlebarWidget(QWidget *parent) override
getTitleToolbar returns a pointer to the a toolbar widget of this controller.
Definition: StaticPlotterWidgetController.cpp:234
armarx::StaticPlotterWidgetController::loadSettings
void loadSettings(QSettings *settings) override
Implement to load the settings that are part of the GUI configuration.
Definition: StaticPlotterWidgetController.cpp:182
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::StaticPlotterWidgetController::saveSettings
void saveSettings(QSettings *settings) override
Implement to save the settings as part of the GUI configuration.
Definition: StaticPlotterWidgetController.cpp:186
ExpressionException.h
armarx::ManagedIceObject::usingTopic
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Definition: ManagedIceObject.cpp:248
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
StaticPlotterWidgetController.h
armarx::ArmarXWidgetController::getWidget
virtual QPointer< QWidget > getWidget()
getWidget returns a pointer to the a widget of this controller.
Definition: ArmarXWidgetController.cpp:54
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
armarx::StaticPlotterWidgetController::StaticPlotterWidgetController
StaticPlotterWidgetController()
Definition: StaticPlotterWidgetController.cpp:21
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::StaticPlotterWidgetController::showCurve
void showCurve(QwtPlotItem *item, bool on)
Definition: StaticPlotterWidgetController.cpp:100