UPlot.h
Go to the documentation of this file.
00001 // Taken from UtiLite library r186 [www.utilite.googlecode.com]
00002 
00003 /*
00004 *  utilite is a cross-platform library with
00005 *  useful utilities for fast and small developing.
00006 *  Copyright (C) 2010  Mathieu Labbe
00007 *
00008 *  utilite is free library: you can redistribute it and/or modify
00009 *  it under the terms of the GNU Lesser General Public License as published by
00010 *  the Free Software Foundation, either version 3 of the License, or
00011 *  (at your option) any later version.
00012 *
00013 *  utilite is distributed in the hope that it will be useful,
00014 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 *  GNU Lesser General Public License for more details.
00017 *
00018 *  You should have received a copy of the GNU Lesser General Public License
00019 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 
00022 #ifndef UPLOT_H_
00023 #define UPLOT_H_
00024 
00025 //#include "utilite/UtiLiteExp.h" // DLL export/import defines
00026 
00027 #include <QtGui/QFrame>
00028 #include <QtCore/QList>
00029 #include <QtCore/QMap>
00030 #include <QtGui/QPen>
00031 #include <QtGui/QBrush>
00032 #include <QtGui/QGraphicsEllipseItem>
00033 #include <QtCore/QMutex>
00034 #include <QtGui/QLabel>
00035 #include <QtGui/QPushButton>
00036 #include <QtCore/QTime>
00037 
00038 class QGraphicsView;
00039 class QGraphicsScene;
00040 class QGraphicsItem;
00041 class QFormLayout;
00042 
00047 class UPlotItem : public QGraphicsEllipseItem
00048 {
00049 public:
00053         UPlotItem(qreal dataX, qreal dataY, qreal width=2);
00057         UPlotItem(const QPointF & data, qreal width=2);
00058         virtual ~UPlotItem();
00059 
00060 public:
00061         void setNextItem(UPlotItem * nextItem);
00062         void setPreviousItem(UPlotItem * previousItem);
00063         void setData(const QPointF & data);
00064 
00065         UPlotItem * nextItem() const {return _nextItem;}
00066         UPlotItem * previousItem() const {return _previousItem;};
00067         const QPointF & data() const {return _data;}
00068 
00069 protected:
00070         virtual void hoverEnterEvent(QGraphicsSceneHoverEvent * event);
00071         virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent * event);
00072         virtual void focusInEvent(QFocusEvent * event);
00073         virtual void focusOutEvent(QFocusEvent * event);
00074         virtual void keyReleaseEvent(QKeyEvent * keyEvent);
00075 
00076         virtual void showDescription(bool shown);
00077 
00078 private:
00079         QPointF _data;
00080         QGraphicsTextItem * _text;
00081         UPlotItem * _previousItem;
00082         UPlotItem * _nextItem;
00083 };
00084 
00085 class UPlot;
00086 
00090 class UPlotCurve : public QObject
00091 {
00092         Q_OBJECT
00093 
00094 public:
00098         UPlotCurve(const QString & name, QObject * parent = 0);
00102         UPlotCurve(const QString & name, const QVector<UPlotItem *> data, QObject * parent = 0);
00106         UPlotCurve(const QString & name, const QVector<float> & x, const QVector<float> & y, QObject * parent = 0);
00107         virtual ~UPlotCurve();
00108 
00112         const QPen & pen() const {return _pen;}
00116         const QBrush & brush() const {return _brush;}
00117 
00121         void setPen(const QPen & pen);
00125         void setBrush(const QBrush & brush);
00126 
00130         QString name() const {return _name;}
00134         int itemsSize() const;
00135         QPointF getItemData(int index);
00136         bool isVisible() const {return _visible;}
00137         void setData(QVector<UPlotItem*> & data); // take the ownership
00138         void setData(const QVector<float> & x, const QVector<float> & y);
00139         void setData(const std::vector<float> & x, const std::vector<float> & y);
00140         void setData(const QVector<float> & y);
00141         void setData(const std::vector<float> & y);
00142         void getData(QVector<float> & x, QVector<float> & y) const; // only call in Qt MainThread
00143         void draw(QPainter * painter);
00144 
00145 public Q_SLOTS:
00150         virtual void clear();
00155     void setVisible(bool visible);
00160     void setXIncrement(float increment);
00165     void setXStart(float val);
00170         void addValue(UPlotItem * data); // take the ownership
00176         void addValue(float y);
00181         void addValue(float x, float y);
00188         void addValue(const QString & y);
00194         void addValues(QVector<UPlotItem *> & data); // take the ownership
00199         void addValues(const QVector<float> & xs, const QVector<float> & ys);
00205         void addValues(const QVector<float> & ys);
00206         void addValues(const QVector<int> & ys); // for convenience
00212         void addValues(const std::vector<float> & ys); // for convenience
00213         void addValues(const std::vector<int> & ys); // for convenience
00214 
00215 Q_SIGNALS:
00220         void dataChanged(const UPlotCurve *);
00221 
00222 protected:
00223         friend class UPlot;
00224         void attach(UPlot * plot);
00225         void detach(UPlot * plot);
00226         void updateMinMax();
00227         const QVector<float> & getMinMax() const {return _minMax;}
00228         int removeItem(int index);
00229         void _addValue(UPlotItem * data);;
00230         virtual bool isMinMaxValid() const {return _minMax.size();}
00231         virtual void update(float scaleX, float scaleY, float offsetX, float offsetY, float xDir, float yDir, bool allDataKept);
00232         QList<QGraphicsItem *> _items;
00233         UPlot * _plot;
00234 
00235 private:
00236         void removeItem(UPlotItem * item);
00237 
00238 private:
00239         QString _name;
00240         QPen _pen;
00241         QBrush _brush;
00242         float _xIncrement;
00243         float _xStart;
00244         bool _visible;
00245         bool _valuesShown;
00246         QVector<float> _minMax; // minX, maxX, minY, maxY
00247 };
00248 
00249 
00253 class UPlotCurveThreshold : public UPlotCurve
00254 {
00255         Q_OBJECT
00256 
00257 public:
00261         UPlotCurveThreshold(const QString & name, float thesholdValue, Qt::Orientation orientation = Qt::Horizontal, QObject * parent = 0);
00262         virtual ~UPlotCurveThreshold();
00263 
00264 public Q_SLOTS:
00268         void setThreshold(float threshold);
00272         void setOrientation(Qt::Orientation orientation);
00273 
00274 protected:
00275         friend class UPlot;
00276         virtual void update(float scaleX, float scaleY, float offsetX, float offsetY, float xDir, float yDir, bool allDataKept);
00277         virtual bool isMinMaxValid() const {return false;}
00278 
00279 private:
00280         Qt::Orientation _orientation;
00281 };
00282 
00286 class UPlotAxis : public QWidget
00287 {
00288 public:
00292         UPlotAxis(Qt::Orientation orientation = Qt::Horizontal, float min=0, float max=1, QWidget * parent = 0);
00293         virtual ~UPlotAxis();
00294 
00295 public:
00300         void setAxis(float & min, float & max);
00304         int border() const {return _border;}
00308         int step() const {return _step;}
00312         int count() const {return _count;}
00316         void setReversed(bool reversed); // Vertical :bottom->up, horizontal :right->left
00317 
00318 protected:
00319         virtual void paintEvent(QPaintEvent * event);
00320 
00321 private:
00322         Qt::Orientation _orientation;
00323         float _min;
00324         float _max;
00325         int _count;
00326         int _step;
00327         bool _reversed;
00328         int _gradMaxDigits;
00329         int _border;
00330 };
00331 
00332 
00336 class UPlotLegendItem : public QPushButton
00337 {
00338         Q_OBJECT
00339 
00340 public:
00344         UPlotLegendItem(const UPlotCurve * curve, QWidget * parent = 0);
00345         virtual ~UPlotLegendItem();
00346         const UPlotCurve * curve() const {return _curve;}
00347 
00348 Q_SIGNALS:
00349         void legendItemRemoved(const UPlotCurve *);
00350 
00351 protected:
00352         virtual void contextMenuEvent(QContextMenuEvent * event);
00353 
00354 private:
00355         const UPlotCurve * _curve;
00356         QMenu * _menu;
00357         QAction * _aChangeText;
00358         QAction * _aResetText;
00359         QAction * _aRemoveCurve;
00360         QAction * _aCopyToClipboard;
00361 };
00362 
00366 class UPlotLegend : public QWidget
00367 {
00368         Q_OBJECT
00369 
00370 public:
00374         UPlotLegend(QWidget * parent = 0);
00375         virtual ~UPlotLegend();
00376 
00377         void setFlat(bool on);
00378         bool isFlat() const {return _flat;}
00379         void addItem(const UPlotCurve * curve);
00380         QPixmap createSymbol(const QPen & pen, const QBrush & brush);
00381         bool remove(const UPlotCurve * curve);
00382 
00383 public Q_SLOTS:
00384         void removeLegendItem(const UPlotCurve * curve);
00385 
00386 Q_SIGNALS:
00387         void legendItemRemoved(const UPlotCurve * curve);
00388         void legendItemToggled(const UPlotCurve * curve, bool toggled);
00389 
00390 protected:
00391         virtual void contextMenuEvent(QContextMenuEvent * event);
00392 
00393 private Q_SLOTS:
00394         void redirectToggled(bool);
00395 
00396 private:
00397         bool _flat;
00398         QMenu * _menu;
00399         QAction * _aUseFlatButtons;
00400 };
00401 
00402 
00406 class UOrientableLabel : public QLabel
00407 {
00408         Q_OBJECT
00409 
00410 public:
00414         UOrientableLabel(const QString & text, Qt::Orientation orientation = Qt::Horizontal, QWidget * parent = 0);
00415         virtual ~UOrientableLabel();
00419         Qt::Orientation orientation() const {return _orientation;}
00423         void setOrientation(Qt::Orientation orientation);
00424         QSize sizeHint() const;
00425         QSize minimumSizeHint() const;
00426 protected:
00427     virtual void paintEvent(QPaintEvent* event);
00428 private:
00429     Qt::Orientation _orientation;
00430 };
00431 
00462 class UPlot : public QWidget
00463 {
00464         Q_OBJECT
00465 
00466 public:
00470         UPlot(QWidget * parent = 0);
00471         virtual ~UPlot();
00472 
00476         UPlotCurve * addCurve(const QString & curveName, const QColor & color = QColor());
00480         bool addCurve(UPlotCurve * curve, bool ownershipTransferred = true);
00484         QStringList curveNames();
00485         bool contains(const QString & curveName);
00486         void removeCurves();
00490         UPlotCurveThreshold * addThreshold(const QString & name, float value, Qt::Orientation orientation = Qt::Horizontal);
00491         QString title() const {return this->objectName();}
00492         QPen getRandomPenColored();
00493         void showLegend(bool shown);
00494         void showGrid(bool shown);
00495         void showRefreshRate(bool shown);
00496         void keepAllData(bool kept);
00497         void showXAxis(bool shown) {_horizontalAxis->setVisible(shown);}
00498         void showYAxis(bool shown) {_verticalAxis->setVisible(shown);}
00499         void setVariableXAxis() {_fixedAxis[0] = false;}
00500         void setVariableYAxis() {_fixedAxis[1] = false;}
00501         void setFixedXAxis(float x1, float x2);
00502         void setFixedYAxis(float y1, float y2);
00503         void setMaxVisibleItems(int maxVisibleItems);
00504         void setTitle(const QString & text);
00505         void setXLabel(const QString & text);
00506         void setYLabel(const QString & text, Qt::Orientation orientation = Qt::Vertical);
00507         void setWorkingDirectory(const QString & workingDirectory);
00508         void setGraphicsView(bool on);
00509         QRectF sceneRect() const;
00510 
00511 public Q_SLOTS:
00516         void removeCurve(const UPlotCurve * curve);
00517         void showCurve(const UPlotCurve * curve, bool shown);
00518         void updateAxis(); //reset axis and recompute it with all curves minMax
00523         void clearData();
00524 
00525 private Q_SLOTS:
00526         void captureScreen();
00527         void updateAxis(const UPlotCurve * curve);
00528 
00529 protected:
00530         virtual void contextMenuEvent(QContextMenuEvent * event);
00531         virtual void paintEvent(QPaintEvent * event);
00532         virtual void resizeEvent(QResizeEvent * event);
00533 
00534 private:
00535         friend class UPlotCurve;
00536         void addItem(QGraphicsItem * item);
00537 
00538 private:
00539         void replot(QPainter * painter);
00540         bool updateAxis(float x, float y);
00541         bool updateAxis(float x1, float x2, float y1, float y2);
00542         void setupUi();
00543         void createActions();
00544         void createMenus();
00545         void selectScreenCaptureFormat();
00546 
00547 private:
00548         UPlotLegend * _legend;
00549         QGraphicsView * _view;
00550         QGraphicsItem * _sceneRoot;
00551         QWidget * _graphicsViewHolder;
00552         float _axisMaximums[4]; // {x1->x2, y1->y2}
00553         bool _axisMaximumsSet[4]; // {x1->x2, y1->y2}
00554         bool _fixedAxis[2];
00555         UPlotAxis * _verticalAxis;
00556         UPlotAxis * _horizontalAxis;
00557         int _penStyleCount;
00558         int _maxVisibleItems;
00559         QList<QGraphicsLineItem *> hGridLines;
00560         QList<QGraphicsLineItem *> vGridLines;
00561         QList<UPlotCurve*> _curves;
00562         QLabel * _title;
00563         QLabel * _xLabel;
00564         UOrientableLabel * _yLabel;
00565         QLabel * _refreshRate;
00566         QString _workingDirectory;
00567         QTime _refreshIntervalTime;
00568         int _lowestRefreshRate;
00569         QTime _refreshStartTime;
00570         QString _autoScreenCaptureFormat;
00571 
00572         QMenu * _menu;
00573         QAction * _aShowLegend;
00574         QAction * _aShowGrid;
00575         QAction * _aKeepAllData;
00576         QAction * _aLimit0;
00577         QAction * _aLimit10;
00578         QAction * _aLimit50;
00579         QAction * _aLimit100;
00580         QAction * _aLimit500;
00581         QAction * _aLimit1000;
00582         QAction * _aLimitCustom;
00583         QAction * _aAddVerticalLine;
00584         QAction * _aAddHorizontalLine;
00585         QAction * _aChangeTitle;
00586         QAction * _aChangeXLabel;
00587         QAction * _aChangeYLabel;
00588         QAction * _aYLabelVertical;
00589         QAction * _aShowRefreshRate;
00590         QAction * _aSaveFigure;
00591         QAction * _aAutoScreenCapture;
00592         QAction * _aClearData;
00593         QAction * _aGraphicsView;
00594 };
00595 
00596 #endif /* UPLOT_H_ */


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Thu Feb 11 2016 22:57:56