1 #ifndef GRAPHITEM_H
2 #define GRAPHITEM_H
3 
4 #include <QGraphicsObject>
5 #include <QPen>
6 #include "data/graph.h"
7 #include "units.h"
8 #include "graphicsscene.h"
9 
10 class GraphItem : public QObject, public GraphicsItem
11 {
12 	Q_OBJECT
13 
14 public:
15 	GraphItem(const Graph &graph, GraphType type, int width,
16 	  const QColor &color, Qt::PenStyle style, QGraphicsItem *parent = 0);
~GraphItem()17 	virtual ~GraphItem() {}
18 
19 	virtual QString info() const = 0;
20 
shape()21 	QPainterPath shape() const {return _shape;}
boundingRect()22 	QRectF boundingRect() const {return _shape.boundingRect();}
23 	void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
24 	  QWidget *widget);
25 
bounds()26 	const QRectF &bounds() const {return _bounds;}
27 
28 	qreal max() const;
29 	qreal min() const;
30 	qreal avg() const;
31 
32 	void setScale(qreal sx, qreal sy);
33 	void setGraphType(GraphType type);
34 	void setColor(const QColor &color);
35 	void setWidth(int width);
setUnits(Units units)36 	void setUnits(Units units) {_units = units;}
37 
secondaryGraph()38 	GraphItem *secondaryGraph() const {return _secondaryGraph;}
setSecondaryGraph(GraphItem * graph)39 	void setSecondaryGraph(GraphItem *graph) {_secondaryGraph = graph;}
40 
41 	qreal yAtX(qreal x);
42 	qreal distanceAtTime(qreal time);
43 
44 	void redraw();
45 
46 signals:
47 	void sliderPositionChanged(qreal);
48 	void selected(bool);
49 
50 public slots:
51 	void emitSliderPositionChanged(qreal);
52 	void hover(bool hover);
53 
54 protected:
55 	void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
56 	void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
57 	void mousePressEvent(QGraphicsSceneMouseEvent *event);
58 
59 	Units _units;
60 
61 private:
62 	const GraphSegment *segment(qreal x, GraphType type) const;
63 	void updatePath();
64 	void updateShape();
65 	void updateBounds();
66 
67 	Graph _graph;
68 	GraphType _type;
69 	QPainterPath _path;
70 	QPainterPath _shape;
71 	QRectF _bounds;
72 	qreal _sx, _sy;
73 	QPen _pen;
74 	bool _time;
75 
76 	GraphItem *_secondaryGraph;
77 };
78 
79 #endif // GRAPHITEM_H
80