UPlot.h
Go to the documentation of this file.
00001 /*
00002 *  utilite is a cross-platform library with
00003 *  useful utilities for fast and small developing.
00004 *  Copyright (C) 2010  Mathieu Labbe
00005 *
00006 *  utilite is free library: you can redistribute it and/or modify
00007 *  it under the terms of the GNU Lesser General Public License as published by
00008 *  the Free Software Foundation, either version 3 of the License, or
00009 *  (at your option) any later version.
00010 *
00011 *  utilite is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 *  GNU Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public License
00017 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 #ifndef UPLOT_H_
00021 #define UPLOT_H_
00022 
00023 #include <QFrame>
00024 #include <QtCore/QList>
00025 #include <QtCore/QMap>
00026 #include <QtGui/QPen>
00027 #include <QtGui/QBrush>
00028 #include <QGraphicsEllipseItem>
00029 #include <QtCore/QMutex>
00030 #include <QLabel>
00031 #include <QPushButton>
00032 #include <QtCore/QTime>
00033 
00034 class QGraphicsView;
00035 class QGraphicsScene;
00036 class QGraphicsItem;
00037 class QFormLayout;
00038 
00043 class  UPlotItem : public QGraphicsEllipseItem
00044 {
00045 public:
00049         UPlotItem(qreal dataX, qreal dataY, qreal width=2);
00053         UPlotItem(const QPointF & data, qreal width=2);
00054         virtual ~UPlotItem();
00055 
00056 public:
00057         void setNextItem(UPlotItem * nextItem);
00058         void setPreviousItem(UPlotItem * previousItem);
00059         void setData(const QPointF & data);
00060 
00061         UPlotItem * nextItem() const {return _nextItem;}
00062         UPlotItem * previousItem() const {return _previousItem;};
00063         const QPointF & data() const {return _data;}
00064 
00065 protected:
00066         virtual void hoverEnterEvent(QGraphicsSceneHoverEvent * event);
00067         virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent * event);
00068         virtual void focusInEvent(QFocusEvent * event);
00069         virtual void focusOutEvent(QFocusEvent * event);
00070         virtual void keyReleaseEvent(QKeyEvent * keyEvent);
00071 
00072         virtual void showDescription(bool shown);
00073 private:
00074         void init(qreal dataX, qreal dataY);
00075 
00076 private:
00077         QPointF _data;
00078         UPlotItem * _previousItem;
00079         UPlotItem * _nextItem;
00080         QGraphicsTextItem * _text;
00081         QGraphicsRectItem * _textBackground;
00082 };
00083 
00084 class UPlot;
00085 
00089 class  UPlotCurve : public QObject
00090 {
00091         Q_OBJECT
00092 
00093 public:
00097         UPlotCurve(const QString & name, QObject * parent = 0);
00101         UPlotCurve(const QString & name, const QVector<UPlotItem *> data, QObject * parent = 0);
00105         UPlotCurve(const QString & name, const QVector<float> & x, const QVector<float> & y, QObject * parent = 0);
00106         virtual ~UPlotCurve();
00107 
00111         const QPen & pen() const {return _pen;}
00115         const QBrush & brush() const {return _brush;}
00116 
00120         void setPen(const QPen & pen);
00124         void setBrush(const QBrush & brush);
00125 
00126         void setItemsColor(const QColor & color);
00127         QColor itemsColor() const  {return _itemsColor;}
00128 
00132         QString name() const {return _name;}
00136         int itemsSize() const;
00137         QPointF getItemData(int index);
00138         bool isVisible() const {return _visible;}
00139         void setData(QVector<UPlotItem*> & data); // take the ownership
00140         void getData(QVector<float> & x, QVector<float> & y) const; // only call in Qt MainThread
00141         void draw(QPainter * painter, const QRect & limits);
00142 
00143 public slots:
00148         virtual void clear();
00153     void setVisible(bool visible);
00158     void setXIncrement(float increment);
00163     void setXStart(float val);
00168         void addValue(UPlotItem * data); // take the ownership
00174         void addValue(float y);
00179         void addValue(float x, float y);
00186         void addValue(const QString & y);
00192         void addValues(QVector<UPlotItem *> & data); // take the ownership
00197         void addValues(const QVector<float> & xs, const QVector<float> & ys);
00203         void addValues(const QVector<float> & ys);
00204         void addValues(const QVector<int> & ys); // for convenience
00210         void addValues(const std::vector<float> & ys); // for convenience
00211         void addValues(const std::vector<int> & ys); // for convenience
00212 
00213         void setData(const QVector<float> & x, const QVector<float> & y);
00214         void setData(const std::vector<float> & x, const std::vector<float> & y);
00215         void setData(const QVector<float> & y);
00216         void setData(const std::vector<float> & y);
00217 
00218 signals:
00223         void dataChanged(const UPlotCurve *);
00224 
00225 protected:
00226         friend class UPlot;
00227         void attach(UPlot * plot);
00228         void detach(UPlot * plot);
00229         void updateMinMax();
00230         const QVector<float> & getMinMax() const {return _minMax;}
00231         int removeItem(int index);
00232         void _addValue(UPlotItem * data);;
00233         virtual bool isMinMaxValid() const {return _minMax.size();}
00234         virtual void update(float scaleX, float scaleY, float offsetX, float offsetY, float xDir, float yDir, int maxItemsKept);
00235         QList<QGraphicsItem *> _items;
00236         UPlot * _plot;
00237 
00238 private:
00239         void removeItem(UPlotItem * item);
00240 
00241 private:
00242         QString _name;
00243         QPen _pen;
00244         QBrush _brush;
00245         float _xIncrement;
00246         float _xStart;
00247         bool _visible;
00248         bool _valuesShown;
00249         QVector<float> _minMax; // minX, maxX, minY, maxY
00250         QGraphicsRectItem * _rootItem;
00251         QColor _itemsColor;
00252 };
00253 
00254 
00258 class  UPlotCurveThreshold : public UPlotCurve
00259 {
00260         Q_OBJECT
00261 
00262 public:
00266         UPlotCurveThreshold(const QString & name, float thesholdValue, Qt::Orientation orientation = Qt::Horizontal, QObject * parent = 0);
00267         virtual ~UPlotCurveThreshold();
00268 
00269 public slots:
00273         void setThreshold(float threshold);
00277         void setOrientation(Qt::Orientation orientation);
00278 
00279 protected:
00280         friend class UPlot;
00281         virtual void update(float scaleX, float scaleY, float offsetX, float offsetY, float xDir, float yDir, int maxItemsKept);
00282         virtual bool isMinMaxValid() const {return false;}
00283 
00284 private:
00285         Qt::Orientation _orientation;
00286 };
00287 
00291 class  UPlotAxis : public QWidget
00292 {
00293 public:
00297         UPlotAxis(Qt::Orientation orientation = Qt::Horizontal, float min=0, float max=1, QWidget * parent = 0);
00298         virtual ~UPlotAxis();
00299 
00300 public:
00305         void setAxis(float & min, float & max);
00309         int border() const {return _border;}
00313         int step() const {return _step;}
00317         int count() const {return _count;}
00321         void setReversed(bool reversed); // Vertical :bottom->up, horizontal :right->left
00322 
00323 protected:
00324         virtual void paintEvent(QPaintEvent * event);
00325 
00326 private:
00327         Qt::Orientation _orientation;
00328         float _min;
00329         float _max;
00330         int _count;
00331         int _step;
00332         bool _reversed;
00333         int _gradMaxDigits;
00334         int _border;
00335 };
00336 
00337 
00341 class  UPlotLegendItem : public QPushButton
00342 {
00343         Q_OBJECT
00344 
00345 public:
00349         UPlotLegendItem(UPlotCurve * curve, QWidget * parent = 0);
00350         virtual ~UPlotLegendItem();
00351         const UPlotCurve * curve() const {return _curve;}
00352         QPixmap createSymbol(const QPen & pen, const QBrush & brush);
00353 
00354 signals:
00355         void legendItemRemoved(const UPlotCurve *);
00356         void moveUpRequest(UPlotLegendItem *);
00357         void moveDownRequest(UPlotLegendItem *);
00358 
00359 private slots:
00360         void updateStdDev();
00361 
00362 protected:
00363         virtual void contextMenuEvent(QContextMenuEvent * event);
00364 
00365 private:
00366         UPlotCurve * _curve;
00367         QMenu * _menu;
00368         QAction * _aChangeText;
00369         QAction * _aResetText;
00370         QAction * _aChangeColor;
00371         QAction * _aCopyToClipboard;
00372         QAction * _aShowStdDev;
00373         QAction * _aRemoveCurve;
00374         QAction * _aMoveUp;
00375         QAction * _aMoveDown;
00376 };
00377 
00381 class  UPlotLegend : public QWidget
00382 {
00383         Q_OBJECT
00384 
00385 public:
00389         UPlotLegend(QWidget * parent = 0);
00390         virtual ~UPlotLegend();
00391 
00392         void setFlat(bool on);
00393         bool isFlat() const {return _flat;}
00394         void addItem(UPlotCurve * curve);
00395         bool remove(const UPlotCurve * curve);
00396 
00397 private slots:
00398         void removeLegendItem(const UPlotCurve * curve);
00399         void moveUp(UPlotLegendItem * item);
00400         void moveDown(UPlotLegendItem * item);
00401 
00402 signals:
00403         void legendItemRemoved(const UPlotCurve * curve);
00404         void legendItemToggled(const UPlotCurve * curve, bool toggled);
00405         void legendItemMoved(const UPlotCurve * curve, int);
00406 
00407 protected:
00408         virtual void contextMenuEvent(QContextMenuEvent * event);
00409 
00410 private slots:
00411         void redirectToggled(bool);
00412 
00413 private:
00414         bool _flat;
00415         QMenu * _menu;
00416         QAction * _aUseFlatButtons;
00417 };
00418 
00419 
00423 class  UOrientableLabel : public QLabel
00424 {
00425         Q_OBJECT
00426 
00427 public:
00431         UOrientableLabel(const QString & text, Qt::Orientation orientation = Qt::Horizontal, QWidget * parent = 0);
00432         virtual ~UOrientableLabel();
00436         Qt::Orientation orientation() const {return _orientation;}
00440         void setOrientation(Qt::Orientation orientation);
00441         QSize sizeHint() const;
00442         QSize minimumSizeHint() const;
00443 protected:
00444     virtual void paintEvent(QPaintEvent* event);
00445 private:
00446     Qt::Orientation _orientation;
00447 };
00448 
00479 class  UPlot : public QWidget
00480 {
00481         Q_OBJECT
00482 
00483 public:
00487         UPlot(QWidget * parent = 0);
00488         virtual ~UPlot();
00489 
00493         UPlotCurve * addCurve(const QString & curveName, const QColor & color = QColor());
00497         bool addCurve(UPlotCurve * curve, bool ownershipTransferred = true);
00501         QStringList curveNames();
00502         bool contains(const QString & curveName);
00503         void removeCurves();
00507         UPlotCurveThreshold * addThreshold(const QString & name, float value, Qt::Orientation orientation = Qt::Horizontal);
00508         QString title() const {return this->objectName();}
00509         QPen getRandomPenColored();
00510         void showLegend(bool shown);
00511         void showGrid(bool shown);
00512         void showRefreshRate(bool shown);
00513         void trackMouse(bool tracking);
00514         void keepAllData(bool kept);
00515         void showXAxis(bool shown) {_horizontalAxis->setVisible(shown);}
00516         void showYAxis(bool shown) {_verticalAxis->setVisible(shown);}
00517         void setVariableXAxis() {_fixedAxis[0] = false;}
00518         void setVariableYAxis() {_fixedAxis[1] = false;}
00519         void setFixedXAxis(float x1, float x2);
00520         void setFixedYAxis(float y1, float y2);
00521         void setMaxVisibleItems(int maxVisibleItems);
00522         void setTitle(const QString & text);
00523         void setXLabel(const QString & text);
00524         void setYLabel(const QString & text, Qt::Orientation orientation = Qt::Vertical);
00525         void setWorkingDirectory(const QString & workingDirectory);
00526         void setGraphicsView(bool on);
00527         void setBackgroundColor(const QColor & color);
00528         QRectF sceneRect() const;
00529 
00530 public slots:
00535         void removeCurve(const UPlotCurve * curve);
00536         void showCurve(const UPlotCurve * curve, bool shown);
00537         void updateAxis(); //reset axis and recompute it with all curves minMax
00542         void clearData();
00543 
00544 private slots:
00545         void captureScreen();
00546         void updateAxis(const UPlotCurve * curve);
00547         void moveCurve(const UPlotCurve *, int index);
00548 
00549 protected:
00550         virtual void contextMenuEvent(QContextMenuEvent * event);
00551         virtual void paintEvent(QPaintEvent * event);
00552         virtual void resizeEvent(QResizeEvent * event);
00553         virtual void mousePressEvent(QMouseEvent * event);
00554         virtual void mouseMoveEvent(QMouseEvent * event);
00555         virtual void mouseReleaseEvent(QMouseEvent * event);
00556         virtual void mouseDoubleClickEvent(QMouseEvent * event);
00557 
00558 private:
00559         friend class UPlotCurve;
00560         void addItem(QGraphicsItem * item);
00561 
00562 private:
00563         void replot(QPainter * painter);
00564         bool updateAxis(float x, float y);
00565         bool updateAxis(float x1, float x2, float y1, float y2);
00566         void setupUi();
00567         void createActions();
00568         void createMenus();
00569         void selectScreenCaptureFormat();
00570         bool mousePosToValue(const QPoint & pos, float & x, float & y);
00571 
00572 private:
00573         UPlotLegend * _legend;
00574         QGraphicsView * _view;
00575         QGraphicsItem * _sceneRoot;
00576         QWidget * _graphicsViewHolder;
00577         float _axisMaximums[4]; // {x1->x2, y1->y2}
00578         bool _axisMaximumsSet[4]; // {x1->x2, y1->y2}
00579         bool _fixedAxis[2];
00580         UPlotAxis * _verticalAxis;
00581         UPlotAxis * _horizontalAxis;
00582         int _penStyleCount;
00583         int _maxVisibleItems;
00584         QList<QGraphicsLineItem *> hGridLines;
00585         QList<QGraphicsLineItem *> vGridLines;
00586         QList<UPlotCurve*> _curves;
00587         QLabel * _title;
00588         QLabel * _xLabel;
00589         UOrientableLabel * _yLabel;
00590         QLabel * _refreshRate;
00591         QString _workingDirectory;
00592         QTime _refreshIntervalTime;
00593         int _lowestRefreshRate;
00594         QTime _refreshStartTime;
00595         QString _autoScreenCaptureFormat;
00596         QPoint _mousePressedPos;
00597         QPoint _mouseCurrentPos;
00598         QColor _bgColor;
00599 
00600         QMenu * _menu;
00601         QAction * _aShowLegend;
00602         QAction * _aShowGrid;
00603         QAction * _aKeepAllData;
00604         QAction * _aLimit0;
00605         QAction * _aLimit10;
00606         QAction * _aLimit50;
00607         QAction * _aLimit100;
00608         QAction * _aLimit500;
00609         QAction * _aLimit1000;
00610         QAction * _aLimitCustom;
00611         QAction * _aAddVerticalLine;
00612         QAction * _aAddHorizontalLine;
00613         QAction * _aChangeTitle;
00614         QAction * _aChangeXLabel;
00615         QAction * _aChangeYLabel;
00616         QAction * _aChangeBackgroundColor;
00617         QAction * _aYLabelVertical;
00618         QAction * _aShowRefreshRate;
00619         QAction * _aMouseTracking;
00620         QAction * _aSaveFigure;
00621         QAction * _aAutoScreenCapture;
00622         QAction * _aClearData;
00623         QAction * _aGraphicsView;
00624 };
00625 
00626 #endif /* UPLOT_H_ */


rtabmap
Author(s): Mathieu Labbe
autogenerated on Fri Aug 28 2015 12:51:42