@@ -0,0 +1,168 | |||
|
1 | #include <QtGui/QApplication> | |
|
2 | #include <QtGui> | |
|
3 | #include <QDebug> | |
|
4 | ||
|
5 | class Slice : public QGraphicsItem | |
|
6 | { | |
|
7 | public: | |
|
8 | Slice(const QColor& color, qreal startAngle, qreal span) | |
|
9 | :m_color(color), | |
|
10 | m_startAngle(startAngle), | |
|
11 | m_span(span) | |
|
12 | { | |
|
13 | setAcceptHoverEvents(true); | |
|
14 | } | |
|
15 | ||
|
16 | ~Slice() | |
|
17 | { | |
|
18 | ||
|
19 | } | |
|
20 | ||
|
21 | virtual QRectF boundingRect() const | |
|
22 | { | |
|
23 | return parentItem()->boundingRect(); | |
|
24 | } | |
|
25 | ||
|
26 | QPainterPath shape() const | |
|
27 | { | |
|
28 | qreal angle = (-m_startAngle) + (90); | |
|
29 | qreal span = -m_span; | |
|
30 | ||
|
31 | QPainterPath path; | |
|
32 | path.moveTo(boundingRect().center()); | |
|
33 | path.arcTo(boundingRect(), angle, span); | |
|
34 | ||
|
35 | // TODO: draw the shape so that it might have a hole in the center | |
|
36 | // - Sin & Cos will be needed to find inner/outer arc endpoints | |
|
37 | ||
|
38 | // dx, dy are offsets from the center | |
|
39 | //qreal l = boundingRect().height(); | |
|
40 | //qreal dx = qSin(angle*(M_PI/180)) * l; | |
|
41 | //qreal dy = qCos(angle*(M_PI/180)) * l; | |
|
42 | ||
|
43 | // TODO: exploded slice? | |
|
44 | ||
|
45 | return path; | |
|
46 | } | |
|
47 | ||
|
48 | virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
|
49 | { | |
|
50 | // Setup painter | |
|
51 | painter->setBrush(m_color); | |
|
52 | QPen pen; | |
|
53 | //pen.setColor(m_color.darker()); | |
|
54 | pen.setColor(Qt::gray); | |
|
55 | pen.setWidth(1); | |
|
56 | painter->setPen(pen); | |
|
57 | ||
|
58 | // From Qt docs: | |
|
59 | // The startAngle and spanAngle must be specified in 1/16th of a degree, i.e. a full circle equals 5760 (16 * 360). | |
|
60 | // Positive values for the angles mean counter-clockwise while negative values mean the clockwise direction. | |
|
61 | // Zero degrees is at the 3 o'clock position. | |
|
62 | // | |
|
63 | // For sake of simplicity convert this so that zero degrees is at 12 o'clock and full circle is 360. | |
|
64 | //qreal angle = (-m_startAngle*16) + (90*16); | |
|
65 | //qreal span = -m_span*16; | |
|
66 | //painter->drawPie(boundingRect(), angle, span); | |
|
67 | ||
|
68 | painter->drawPath(shape()); | |
|
69 | } | |
|
70 | ||
|
71 | virtual void hoverEnterEvent( QGraphicsSceneHoverEvent * event) | |
|
72 | { | |
|
73 | QGraphicsItem::hoverEnterEvent(event); | |
|
74 | qDebug() << "hover" << m_color << m_startAngle << m_span; | |
|
75 | } | |
|
76 | ||
|
77 | private: | |
|
78 | QColor m_color; | |
|
79 | qreal m_startAngle; | |
|
80 | qreal m_span; | |
|
81 | }; | |
|
82 | ||
|
83 | class PieChart : public QGraphicsItem | |
|
84 | { | |
|
85 | public: | |
|
86 | PieChart() | |
|
87 | { | |
|
88 | qsrand(QTime::currentTime().msec()); | |
|
89 | } | |
|
90 | ||
|
91 | ~PieChart() | |
|
92 | { | |
|
93 | ||
|
94 | } | |
|
95 | ||
|
96 | QColor randomColor() | |
|
97 | { | |
|
98 | QColor c; | |
|
99 | c.setRed(qrand() % 255); | |
|
100 | c.setGreen(qrand() % 255); | |
|
101 | c.setBlue(qrand() % 255); | |
|
102 | return c; | |
|
103 | } | |
|
104 | ||
|
105 | void setData(QList<int> data) | |
|
106 | { | |
|
107 | qreal fullPie = 360; | |
|
108 | ||
|
109 | qreal total = 0; | |
|
110 | foreach (qreal value, data) | |
|
111 | total += value; | |
|
112 | ||
|
113 | qreal angle = 0; | |
|
114 | foreach (qreal value, data) { | |
|
115 | qreal span = value / total * fullPie; | |
|
116 | Slice *slice = new Slice(randomColor(), angle, span); | |
|
117 | slice->setParentItem(this); | |
|
118 | m_slices.append(slice); | |
|
119 | angle += span; | |
|
120 | } | |
|
121 | } | |
|
122 | ||
|
123 | virtual QRectF boundingRect() const | |
|
124 | { | |
|
125 | return QRectF(-200, -200, 200, 200); | |
|
126 | } | |
|
127 | ||
|
128 | virtual void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
|
129 | { | |
|
130 | //painter->setBrush(Qt::red); | |
|
131 | //painter->drawRect(boundingRect()); | |
|
132 | } | |
|
133 | ||
|
134 | private: | |
|
135 | QList<Slice*> m_slices; | |
|
136 | QSizeF m_size; | |
|
137 | }; | |
|
138 | ||
|
139 | ||
|
140 | int main(int argc, char *argv[]) | |
|
141 | { | |
|
142 | QApplication a(argc, argv); | |
|
143 | ||
|
144 | QGraphicsScene scene; | |
|
145 | ||
|
146 | PieChart *pieChart = new PieChart(); | |
|
147 | scene.addItem(pieChart); | |
|
148 | ||
|
149 | QGraphicsView view(&scene); | |
|
150 | view.setRenderHint(QPainter::Antialiasing); | |
|
151 | view.resize(400, 300); | |
|
152 | view.show(); | |
|
153 | ||
|
154 | QList<int> data; | |
|
155 | data << 1; | |
|
156 | data << 2; | |
|
157 | data << 3; | |
|
158 | data << 20; | |
|
159 | data << 2; | |
|
160 | data << 3; | |
|
161 | data << 10; | |
|
162 | data << 2; | |
|
163 | data << 1; | |
|
164 | data << 5; | |
|
165 | pieChart->setData(data); | |
|
166 | ||
|
167 | return a.exec(); | |
|
168 | } |
General Comments 0
You need to be logged in to leave comments.
Login now