UPlot.h
Go to the documentation of this file.
1 // Taken from UtiLite library r186 [www.utilite.googlecode.com]
2 
3 /*
4 * utilite is a cross-platform library with
5 * useful utilities for fast and small developing.
6 * Copyright (C) 2010 Mathieu Labbe
7 *
8 * utilite is free library: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * utilite is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #ifndef UPLOT_H_
23 #define UPLOT_H_
24 
25 //#include "utilite/UtiLiteExp.h" // DLL export/import defines
26 
27 #include <QFrame>
28 #include <QtCore/QList>
29 #include <QtCore/QMap>
30 #include <QtGui/QPen>
31 #include <QtGui/QBrush>
32 #include <QGraphicsEllipseItem>
33 #include <QtCore/QMutex>
34 #include <QLabel>
35 #include <QPushButton>
36 #include <QtCore/QTime>
37 
38 class QGraphicsView;
39 class QGraphicsScene;
40 class QGraphicsItem;
41 class QFormLayout;
42 
47 class UPlotItem : public QGraphicsEllipseItem
48 {
49 public:
53  UPlotItem(qreal dataX, qreal dataY, qreal width=2);
57  UPlotItem(const QPointF & data, qreal width=2);
58  virtual ~UPlotItem();
59 
60 public:
63  void setData(const QPointF & data);
64 
65  UPlotItem * nextItem() const {return _nextItem;}
66  UPlotItem * previousItem() const {return _previousItem;};
67  const QPointF & data() const {return _data;}
68 
69 protected:
70  virtual void hoverEnterEvent(QGraphicsSceneHoverEvent * event);
71  virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent * event);
72  virtual void focusInEvent(QFocusEvent * event);
73  virtual void focusOutEvent(QFocusEvent * event);
74  virtual void keyReleaseEvent(QKeyEvent * keyEvent);
75 
76  virtual void showDescription(bool shown);
77 
78 private:
79  QPointF _data;
80  QGraphicsTextItem * _text;
83 };
84 
85 class UPlot;
86 
90 class UPlotCurve : public QObject
91 {
92  Q_OBJECT
93 
94 public:
98  UPlotCurve(const QString & name, QObject * parent = 0);
102  UPlotCurve(const QString & name, const QVector<UPlotItem *> data, QObject * parent = 0);
106  UPlotCurve(const QString & name, const QVector<float> & x, const QVector<float> & y, QObject * parent = 0);
107  virtual ~UPlotCurve();
108 
112  const QPen & pen() const {return _pen;}
116  const QBrush & brush() const {return _brush;}
117 
121  void setPen(const QPen & pen);
125  void setBrush(const QBrush & brush);
126 
130  QString name() const {return _name;}
134  int itemsSize() const;
135  QPointF getItemData(int index);
136  bool isVisible() const {return _visible;}
137  void setData(QVector<UPlotItem*> & data); // take the ownership
138  void setData(const QVector<float> & x, const QVector<float> & y);
139  void setData(const std::vector<float> & x, const std::vector<float> & y);
140  void setData(const QVector<float> & y);
141  void setData(const std::vector<float> & y);
142  void getData(QVector<float> & x, QVector<float> & y) const; // only call in Qt MainThread
143  void draw(QPainter * painter);
144 
145 public Q_SLOTS:
150  virtual void clear();
155  void setVisible(bool visible);
160  void setXIncrement(float increment);
165  void setXStart(float val);
170  void addValue(UPlotItem * data); // take the ownership
176  void addValue(float y);
181  void addValue(float x, float y);
188  void addValue(const QString & y);
194  void addValues(QVector<UPlotItem *> & data); // take the ownership
199  void addValues(const QVector<float> & xs, const QVector<float> & ys);
205  void addValues(const QVector<float> & ys);
206  void addValues(const QVector<int> & ys); // for convenience
212  void addValues(const std::vector<float> & ys); // for convenience
213  void addValues(const std::vector<int> & ys); // for convenience
214 
215 Q_SIGNALS:
220  void dataChanged(const UPlotCurve *);
221 
222 protected:
223  friend class UPlot;
224  void attach(UPlot * plot);
225  void detach(UPlot * plot);
226  void updateMinMax();
227  const QVector<float> & getMinMax() const {return _minMax;}
228  int removeItem(int index);
229  void _addValue(UPlotItem * data);;
230  virtual bool isMinMaxValid() const {return _minMax.size();}
231  virtual void update(float scaleX, float scaleY, float offsetX, float offsetY, float xDir, float yDir, bool allDataKept);
232  QList<QGraphicsItem *> _items;
234 
235 private:
236  void removeItem(UPlotItem * item);
237 
238 private:
239  QString _name;
240  QPen _pen;
241  QBrush _brush;
242  float _xIncrement;
243  float _xStart;
244  bool _visible;
246  QVector<float> _minMax; // minX, maxX, minY, maxY
247 };
248 
249 
254 {
255  Q_OBJECT
256 
257 public:
261  UPlotCurveThreshold(const QString & name, float thesholdValue, Qt::Orientation orientation = Qt::Horizontal, QObject * parent = 0);
262  virtual ~UPlotCurveThreshold();
263 
264 public Q_SLOTS:
268  void setThreshold(float threshold);
272  void setOrientation(Qt::Orientation orientation);
273 
274 protected:
275  friend class UPlot;
276  virtual void update(float scaleX, float scaleY, float offsetX, float offsetY, float xDir, float yDir, bool allDataKept);
277  virtual bool isMinMaxValid() const {return false;}
278 
279 private:
280  Qt::Orientation _orientation;
281 };
282 
286 class UPlotAxis : public QWidget
287 {
288 public:
292  UPlotAxis(Qt::Orientation orientation = Qt::Horizontal, float min=0, float max=1, QWidget * parent = 0);
293  virtual ~UPlotAxis();
294 
295 public:
300  void setAxis(float & min, float & max);
304  int border() const {return _border;}
308  int step() const {return _step;}
312  int count() const {return _count;}
316  void setReversed(bool reversed); // Vertical :bottom->up, horizontal :right->left
317 
318 protected:
319  virtual void paintEvent(QPaintEvent * event);
320 
321 private:
322  Qt::Orientation _orientation;
323  float _min;
324  float _max;
325  int _count;
326  int _step;
327  bool _reversed;
329  int _border;
330 };
331 
332 
336 class UPlotLegendItem : public QPushButton
337 {
338  Q_OBJECT
339 
340 public:
344  UPlotLegendItem(const UPlotCurve * curve, QWidget * parent = 0);
345  virtual ~UPlotLegendItem();
346  const UPlotCurve * curve() const {return _curve;}
347 
348 Q_SIGNALS:
349  void legendItemRemoved(const UPlotCurve *);
350 
351 protected:
352  virtual void contextMenuEvent(QContextMenuEvent * event);
353 
354 private:
356  QMenu * _menu;
357  QAction * _aChangeText;
358  QAction * _aResetText;
359  QAction * _aRemoveCurve;
360  QAction * _aCopyToClipboard;
361 };
362 
366 class UPlotLegend : public QWidget
367 {
368  Q_OBJECT
369 
370 public:
374  UPlotLegend(QWidget * parent = 0);
375  virtual ~UPlotLegend();
376 
377  void setFlat(bool on);
378  bool isFlat() const {return _flat;}
379  void addItem(const UPlotCurve * curve);
380  QPixmap createSymbol(const QPen & pen, const QBrush & brush);
381  bool remove(const UPlotCurve * curve);
382 
383 public Q_SLOTS:
384  void removeLegendItem(const UPlotCurve * curve);
385 
386 Q_SIGNALS:
387  void legendItemRemoved(const UPlotCurve * curve);
388  void legendItemToggled(const UPlotCurve * curve, bool toggled);
389 
390 protected:
391  virtual void contextMenuEvent(QContextMenuEvent * event);
392 
393 private Q_SLOTS:
394  void redirectToggled(bool);
395 
396 private:
397  bool _flat;
398  QMenu * _menu;
399  QAction * _aUseFlatButtons;
400 };
401 
402 
406 class UOrientableLabel : public QLabel
407 {
408  Q_OBJECT
409 
410 public:
414  UOrientableLabel(const QString & text, Qt::Orientation orientation = Qt::Horizontal, QWidget * parent = 0);
415  virtual ~UOrientableLabel();
419  Qt::Orientation orientation() const {return _orientation;}
423  void setOrientation(Qt::Orientation orientation);
424  QSize sizeHint() const;
425  QSize minimumSizeHint() const;
426 protected:
427  virtual void paintEvent(QPaintEvent* event);
428 private:
429  Qt::Orientation _orientation;
430 };
431 
462 class UPlot : public QWidget
463 {
464  Q_OBJECT
465 
466 public:
470  UPlot(QWidget * parent = 0);
471  virtual ~UPlot();
472 
476  UPlotCurve * addCurve(const QString & curveName, const QColor & color = QColor());
480  bool addCurve(UPlotCurve * curve, bool ownershipTransferred = true);
484  QStringList curveNames();
485  bool contains(const QString & curveName);
486  void removeCurves();
490  UPlotCurveThreshold * addThreshold(const QString & name, float value, Qt::Orientation orientation = Qt::Horizontal);
491  QString title() const {return this->objectName();}
492  QPen getRandomPenColored();
493  void showLegend(bool shown);
494  void showGrid(bool shown);
495  void showRefreshRate(bool shown);
496  void keepAllData(bool kept);
497  void showXAxis(bool shown) {_horizontalAxis->setVisible(shown);}
498  void showYAxis(bool shown) {_verticalAxis->setVisible(shown);}
499  void setVariableXAxis() {_fixedAxis[0] = false;}
500  void setVariableYAxis() {_fixedAxis[1] = false;}
501  void setFixedXAxis(float x1, float x2);
502  void setFixedYAxis(float y1, float y2);
503  void setMaxVisibleItems(int maxVisibleItems);
504  void setTitle(const QString & text);
505  void setXLabel(const QString & text);
506  void setYLabel(const QString & text, Qt::Orientation orientation = Qt::Vertical);
507  void setWorkingDirectory(const QString & workingDirectory);
508  void setGraphicsView(bool on);
509  QRectF sceneRect() const;
510 
511 public Q_SLOTS:
516  void removeCurve(const UPlotCurve * curve);
517  void showCurve(const UPlotCurve * curve, bool shown);
518  void updateAxis(); //reset axis and recompute it with all curves minMax
523  void clearData();
524 
525 private Q_SLOTS:
526  void captureScreen();
527  void updateAxis(const UPlotCurve * curve);
528 
529 protected:
530  virtual void contextMenuEvent(QContextMenuEvent * event);
531  virtual void paintEvent(QPaintEvent * event);
532  virtual void resizeEvent(QResizeEvent * event);
533 
534 private:
535  friend class UPlotCurve;
536  void addItem(QGraphicsItem * item);
537 
538 private:
539  void replot(QPainter * painter);
540  bool updateAxis(float x, float y);
541  bool updateAxis(float x1, float x2, float y1, float y2);
542  void setupUi();
543  void createActions();
544  void createMenus();
545  void selectScreenCaptureFormat();
546 
547 private:
549  QGraphicsView * _view;
550  QGraphicsItem * _sceneRoot;
552  float _axisMaximums[4]; // {x1->x2, y1->y2}
553  bool _axisMaximumsSet[4]; // {x1->x2, y1->y2}
554  bool _fixedAxis[2];
559  QList<QGraphicsLineItem *> hGridLines;
560  QList<QGraphicsLineItem *> vGridLines;
561  QList<UPlotCurve*> _curves;
562  QLabel * _title;
563  QLabel * _xLabel;
565  QLabel * _refreshRate;
571 
572  QMenu * _menu;
573  QAction * _aShowLegend;
574  QAction * _aShowGrid;
575  QAction * _aKeepAllData;
576  QAction * _aLimit0;
577  QAction * _aLimit10;
578  QAction * _aLimit50;
579  QAction * _aLimit100;
580  QAction * _aLimit500;
581  QAction * _aLimit1000;
582  QAction * _aLimitCustom;
583  QAction * _aAddVerticalLine;
585  QAction * _aChangeTitle;
586  QAction * _aChangeXLabel;
587  QAction * _aChangeYLabel;
588  QAction * _aYLabelVertical;
589  QAction * _aShowRefreshRate;
590  QAction * _aSaveFigure;
592  QAction * _aClearData;
593  QAction * _aGraphicsView;
594 };
595 
596 #endif /* UPLOT_H_ */
QString _autoScreenCaptureFormat
Definition: UPlot.h:570
QGraphicsTextItem * _text
Definition: UPlot.h:80
UPlot * _plot
Definition: UPlot.h:233
const QVector< float > & getMinMax() const
Definition: UPlot.h:227
QAction * _aChangeYLabel
Definition: UPlot.h:587
QGraphicsItem * _sceneRoot
Definition: UPlot.h:550
UPlotAxis * _verticalAxis
Definition: UPlot.h:555
QList< UPlotCurve * > _curves
Definition: UPlot.h:561
QPen _pen
Definition: UPlot.h:240
float _max
Definition: UPlot.h:324
QAction * _aKeepAllData
Definition: UPlot.h:575
QAction * _aLimit500
Definition: UPlot.h:580
QLabel * _refreshRate
Definition: UPlot.h:565
bool _flat
Definition: UPlot.h:397
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
Definition: UPlot.cpp:175
QAction * _aLimit100
Definition: UPlot.h:579
QAction * _aGraphicsView
Definition: UPlot.h:593
QLabel * _xLabel
Definition: UPlot.h:563
const UPlotCurve * _curve
Definition: UPlot.h:355
int step() const
Definition: UPlot.h:308
QAction * _aAddVerticalLine
Definition: UPlot.h:583
UPlotItem * _nextItem
Definition: UPlot.h:82
UPlotItem * _previousItem
Definition: UPlot.h:81
QAction * _aClearData
Definition: UPlot.h:592
UOrientableLabel * _yLabel
Definition: UPlot.h:564
QWidget * _graphicsViewHolder
Definition: UPlot.h:551
Qt::Orientation _orientation
Definition: UPlot.h:322
QTime _refreshIntervalTime
Definition: UPlot.h:567
TFSIMD_FORCE_INLINE const tfScalar & y() const
UPlotItem * previousItem() const
Definition: UPlot.h:66
QAction * _aShowGrid
Definition: UPlot.h:574
QString title() const
Definition: UPlot.h:491
QString _workingDirectory
Definition: UPlot.h:566
QAction * _aLimitCustom
Definition: UPlot.h:582
const QBrush & brush() const
Definition: UPlot.h:116
Definition: UPlot.h:462
bool _visible
Definition: UPlot.h:244
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
QAction * _aLimit10
Definition: UPlot.h:577
void showYAxis(bool shown)
Definition: UPlot.h:498
UPlotAxis * _horizontalAxis
Definition: UPlot.h:556
int _count
Definition: UPlot.h:325
virtual void showDescription(bool shown)
Definition: UPlot.cpp:128
const QPointF & data() const
Definition: UPlot.h:67
QAction * _aRemoveCurve
Definition: UPlot.h:359
void setVariableYAxis()
Definition: UPlot.h:500
virtual bool isMinMaxValid() const
Definition: UPlot.h:230
Qt::Orientation _orientation
Definition: UPlot.h:429
QMenu * _menu
Definition: UPlot.h:356
QAction * _aChangeText
Definition: UPlot.h:357
int count() const
Definition: UPlot.h:312
QAction * _aShowRefreshRate
Definition: UPlot.h:589
QAction * _aLimit1000
Definition: UPlot.h:581
UPlotItem(qreal dataX, qreal dataY, qreal width=2)
Definition: UPlot.cpp:53
bool _valuesShown
Definition: UPlot.h:245
QAction * _aAddHorizontalLine
Definition: UPlot.h:584
QMenu * _menu
Definition: UPlot.h:398
QGraphicsView * _view
Definition: UPlot.h:549
TFSIMD_FORCE_INLINE const tfScalar & x() const
int _maxVisibleItems
Definition: UPlot.h:558
const QPen & pen() const
Definition: UPlot.h:112
virtual void keyReleaseEvent(QKeyEvent *keyEvent)
Definition: UPlot.cpp:210
QList< QGraphicsItem * > _items
Definition: UPlot.h:232
virtual bool isMinMaxValid() const
Definition: UPlot.h:277
QMenu * _menu
Definition: UPlot.h:572
float _xIncrement
Definition: UPlot.h:242
QAction * _aYLabelVertical
Definition: UPlot.h:588
virtual ~UPlotItem()
Definition: UPlot.cpp:82
int border() const
Definition: UPlot.h:304
QList< QGraphicsLineItem * > vGridLines
Definition: UPlot.h:560
QAction * _aCopyToClipboard
Definition: UPlot.h:360
int _step
Definition: UPlot.h:326
QAction * _aAutoScreenCapture
Definition: UPlot.h:591
QAction * _aChangeXLabel
Definition: UPlot.h:586
int _penStyleCount
Definition: UPlot.h:557
bool isFlat() const
Definition: UPlot.h:378
int _gradMaxDigits
Definition: UPlot.h:328
int min(int a, int b)
UPlotLegend * _legend
Definition: UPlot.h:548
QAction * _aLimit0
Definition: UPlot.h:576
QLabel * _title
Definition: UPlot.h:562
QAction * _aSaveFigure
Definition: UPlot.h:590
virtual void focusOutEvent(QFocusEvent *event)
Definition: UPlot.cpp:204
QString _name
Definition: UPlot.h:239
void setData(const QPointF &data)
Definition: UPlot.cpp:99
Qt::Orientation orientation() const
Definition: UPlot.h:419
bool isVisible() const
Definition: UPlot.h:136
QAction * _aChangeTitle
Definition: UPlot.h:585
QTime _refreshStartTime
Definition: UPlot.h:569
UPlotItem * nextItem() const
Definition: UPlot.h:65
virtual void focusInEvent(QFocusEvent *event)
Definition: UPlot.cpp:198
float _xStart
Definition: UPlot.h:243
QBrush _brush
Definition: UPlot.h:241
QAction * _aUseFlatButtons
Definition: UPlot.h:399
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
Definition: UPlot.cpp:189
int _border
Definition: UPlot.h:329
void setVariableXAxis()
Definition: UPlot.h:499
QVector< float > _minMax
Definition: UPlot.h:246
bool _reversed
Definition: UPlot.h:327
const UPlotCurve * curve() const
Definition: UPlot.h:346
QString name() const
Definition: UPlot.h:130
int _lowestRefreshRate
Definition: UPlot.h:568
QAction * _aShowLegend
Definition: UPlot.h:573
QAction * _aResetText
Definition: UPlot.h:358
void showXAxis(bool shown)
Definition: UPlot.h:497
QPointF _data
Definition: UPlot.h:79
float _min
Definition: UPlot.h:323
void setNextItem(UPlotItem *nextItem)
Definition: UPlot.cpp:104
QList< QGraphicsLineItem * > hGridLines
Definition: UPlot.h:559
Qt::Orientation _orientation
Definition: UPlot.h:280
QAction * _aLimit50
Definition: UPlot.h:578
void increment(int *value)
void setPreviousItem(UPlotItem *previousItem)
Definition: UPlot.cpp:116


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Thu Jun 6 2019 19:22:26