@@ -1,42 +1,43 | |||
|
1 | 1 | #include <QtGui/QApplication> |
|
2 | 2 | #include <QMainWindow> |
|
3 | 3 | #include <qchartglobal.h> |
|
4 | 4 | #include <qchartview.h> |
|
5 | 5 | #include <qpieseries.h> |
|
6 | 6 | #include <qpieslice.h> |
|
7 | 7 | #include "customslice.h" |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | int main(int argc, char *argv[]) |
|
12 | 12 | { |
|
13 | 13 | QApplication a(argc, argv); |
|
14 | 14 | |
|
15 | 15 | QMainWindow window; |
|
16 | 16 | |
|
17 | 17 | QPieSeries *series = new QPieSeries(); |
|
18 | 18 | series->add(5, "Slice 1"); |
|
19 | 19 | series->add(2, "Slice 2"); |
|
20 | 20 | series->add(3, "Slice 3"); |
|
21 | 21 | series->add(4, "Slice 4"); |
|
22 | 22 | series->add(5, "Slice 5"); |
|
23 | 23 | series->add(6, "Slice 6"); |
|
24 | 24 | series->add(7, "Slice 7"); |
|
25 | 25 | series->add(new CustomSlice(8)); |
|
26 | 26 | series->enableClickExplodes(true); |
|
27 | 27 | series->enableHoverHighlight(true); |
|
28 | 28 | |
|
29 | 29 | foreach (QPieSlice*s, series->slices()) |
|
30 | 30 | qDebug() << s->angle() << s->span() << s->percentage(); |
|
31 | 31 | |
|
32 | 32 | QChartView* chartView = new QChartView(&window); |
|
33 | chartView->setRenderHint(QPainter::Antialiasing); | |
|
33 | 34 | chartView->addSeries(series); |
|
34 | 35 | chartView->setChartTitle("simple piechart"); |
|
35 | 36 | chartView->setChartTheme(QChart::ChartThemeIcy); |
|
36 | 37 | |
|
37 | 38 | window.setCentralWidget(chartView); |
|
38 | 39 | window.resize(600, 600); |
|
39 | 40 | window.show(); |
|
40 | 41 | |
|
41 | 42 | return a.exec(); |
|
42 | 43 | } |
@@ -1,141 +1,140 | |||
|
1 | 1 | #include "pieslice.h" |
|
2 | 2 | #include "pieslicelabel.h" |
|
3 | 3 | #include "piepresenter.h" |
|
4 | 4 | #include "qpieseries.h" |
|
5 | 5 | #include "qpieslice.h" |
|
6 | 6 | #include <QPainter> |
|
7 | 7 | #include <QDebug> |
|
8 | 8 | #include <qmath.h> |
|
9 | 9 | #include <QGraphicsSceneEvent> |
|
10 | 10 | #include <QTime> |
|
11 | 11 | |
|
12 | 12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
13 | 13 | |
|
14 | 14 | #define PI 3.14159265 |
|
15 | 15 | #define EXPLODE_OFFSET 20 |
|
16 | 16 | |
|
17 | 17 | QPointF offset(qreal angle, qreal length) |
|
18 | 18 | { |
|
19 | 19 | qreal dx = qSin(angle*(PI/180)) * length; |
|
20 | 20 | qreal dy = qCos(angle*(PI/180)) * length; |
|
21 | 21 | return QPointF(dx, -dy); |
|
22 | 22 | } |
|
23 | 23 | |
|
24 | 24 | PieSlice::PieSlice(QGraphicsItem* parent) |
|
25 | 25 | :QGraphicsObject(parent), |
|
26 | 26 | m_slicelabel(new PieSliceLabel(this)), |
|
27 | 27 | m_angle(0), |
|
28 | 28 | m_span(0), |
|
29 | 29 | m_isExploded(false) |
|
30 | 30 | { |
|
31 | 31 | setAcceptHoverEvents(true); |
|
32 | 32 | setAcceptedMouseButtons(Qt::LeftButton); |
|
33 | 33 | } |
|
34 | 34 | |
|
35 | 35 | PieSlice::~PieSlice() |
|
36 | 36 | { |
|
37 | 37 | |
|
38 | 38 | } |
|
39 | 39 | |
|
40 | 40 | QRectF PieSlice::boundingRect() const |
|
41 | 41 | { |
|
42 | 42 | return m_path.boundingRect(); |
|
43 | 43 | } |
|
44 | 44 | |
|
45 | 45 | QPainterPath PieSlice::shape() const |
|
46 | 46 | { |
|
47 | 47 | return m_path; |
|
48 | 48 | } |
|
49 | 49 | |
|
50 | 50 | void PieSlice::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) |
|
51 | 51 | { |
|
52 | painter->setRenderHint(QPainter::Antialiasing); | |
|
53 | 52 | painter->setPen(m_pen); |
|
54 | 53 | painter->setBrush(m_brush); |
|
55 | 54 | painter->drawPath(m_path); |
|
56 | 55 | } |
|
57 | 56 | |
|
58 | 57 | void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/) |
|
59 | 58 | { |
|
60 | 59 | emit hoverEnter(); |
|
61 | 60 | } |
|
62 | 61 | |
|
63 | 62 | void PieSlice::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) |
|
64 | 63 | { |
|
65 | 64 | emit hoverLeave(); |
|
66 | 65 | } |
|
67 | 66 | |
|
68 | 67 | void PieSlice::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/) |
|
69 | 68 | { |
|
70 | 69 | emit clicked(); |
|
71 | 70 | } |
|
72 | 71 | |
|
73 | 72 | void PieSlice::setPieRect(QRectF rect) |
|
74 | 73 | { |
|
75 | 74 | m_pieRect = rect; |
|
76 | 75 | } |
|
77 | 76 | |
|
78 | 77 | void PieSlice::updateGeometry() |
|
79 | 78 | { |
|
80 | 79 | prepareGeometryChange(); |
|
81 | 80 | |
|
82 | 81 | // calculate center angle |
|
83 | 82 | qreal centerAngle = m_angle + (m_span/2); |
|
84 | 83 | |
|
85 | 84 | // adjust rect for exploding |
|
86 | 85 | QRectF rect = m_pieRect; |
|
87 | 86 | rect.adjust(EXPLODE_OFFSET, EXPLODE_OFFSET, -EXPLODE_OFFSET ,-EXPLODE_OFFSET); |
|
88 | 87 | if (m_isExploded) { |
|
89 | 88 | QPointF d = offset((centerAngle), EXPLODE_OFFSET); |
|
90 | 89 | rect.translate(d.x(), d.y()); |
|
91 | 90 | } |
|
92 | 91 | |
|
93 | 92 | // update slice path |
|
94 | 93 | // TODO: draw the shape so that it might have a hole in the center |
|
95 | 94 | QPainterPath path; |
|
96 | 95 | path.moveTo(rect.center()); |
|
97 | 96 | path.arcTo(rect, -m_angle + 90, -m_span); |
|
98 | 97 | path.closeSubpath(); |
|
99 | 98 | m_path = path; |
|
100 | 99 | |
|
101 | 100 | // update label position |
|
102 | 101 | qreal radius = rect.height() / 2; |
|
103 | 102 | QPointF edgeCenter = rect.center() + offset(centerAngle, radius + 5); |
|
104 | 103 | m_slicelabel->setArmStartPoint(edgeCenter); |
|
105 | 104 | m_slicelabel->setArmAngle(centerAngle); |
|
106 | 105 | m_slicelabel->updateGeometry(); |
|
107 | 106 | |
|
108 | 107 | //qDebug() << "PieSlice::updateGeometry" << m_slicelabel->text() << boundingRect() << m_angle << m_span; |
|
109 | 108 | } |
|
110 | 109 | |
|
111 | 110 | void PieSlice::handleSliceDataChanged() |
|
112 | 111 | { |
|
113 | 112 | QPieSlice *slice = qobject_cast<QPieSlice*>(sender()); |
|
114 | 113 | Q_ASSERT(slice); |
|
115 | 114 | updateData(slice); |
|
116 | 115 | } |
|
117 | 116 | |
|
118 | 117 | void PieSlice::updateData(const QPieSlice* sliceData) |
|
119 | 118 | { |
|
120 | 119 | // TODO: compare what has changes to avoid unneccesary geometry updates |
|
121 | 120 | |
|
122 | 121 | m_angle = sliceData->angle(); |
|
123 | 122 | m_span = sliceData->span(); |
|
124 | 123 | m_isExploded = sliceData->isExploded(); |
|
125 | 124 | m_pen = sliceData->pen(); |
|
126 | 125 | m_brush = sliceData->brush(); |
|
127 | 126 | |
|
128 | 127 | m_slicelabel->setVisible(sliceData->isLabelVisible()); |
|
129 | 128 | m_slicelabel->setText(sliceData->label()); |
|
130 | 129 | m_slicelabel->setPen(sliceData->labelPen()); |
|
131 | 130 | m_slicelabel->setFont(sliceData->labelFont()); |
|
132 | 131 | m_slicelabel->setArmLength(sliceData->labelArmLenght()); |
|
133 | 132 | |
|
134 | 133 | updateGeometry(); |
|
135 | 134 | update(); |
|
136 | 135 | m_slicelabel->update(); |
|
137 | 136 | } |
|
138 | 137 | |
|
139 | 138 | #include "moc_pieslice.cpp" |
|
140 | 139 | |
|
141 | 140 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,74 +1,72 | |||
|
1 | 1 | #include "pieslicelabel.h" |
|
2 | 2 | #include <QPainter> |
|
3 | 3 | #include <qmath.h> |
|
4 | 4 | #include <QGraphicsTextItem> |
|
5 | 5 | #include <QDebug> |
|
6 | 6 | |
|
7 | 7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | 8 | |
|
9 | 9 | #define PI 3.14159265 |
|
10 | 10 | |
|
11 | 11 | PieSliceLabel::PieSliceLabel(QGraphicsItem* parent) |
|
12 | 12 | :QGraphicsItem(parent), |
|
13 | 13 | m_armAngle(0), |
|
14 | 14 | m_armLength(0) |
|
15 | 15 | { |
|
16 | 16 | |
|
17 | 17 | } |
|
18 | 18 | |
|
19 | 19 | void PieSliceLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) |
|
20 | 20 | { |
|
21 | painter->setRenderHint(QPainter::Antialiasing); | |
|
22 | ||
|
23 | 21 | painter->setPen(m_pen); |
|
24 | 22 | painter->drawPath(m_armPath); |
|
25 | 23 | |
|
26 | 24 | // TODO: do we need a pen for text? |
|
27 | 25 | painter->setFont(m_font); |
|
28 | 26 | painter->drawText(m_textRect.bottomLeft(), m_text); |
|
29 | 27 | |
|
30 | 28 | //qDebug() << "PieSliceLabel::paint" << m_text << m_textRect; |
|
31 | 29 | } |
|
32 | 30 | |
|
33 | 31 | void PieSliceLabel::updateGeometry() |
|
34 | 32 | { |
|
35 | 33 | prepareGeometryChange(); |
|
36 | 34 | |
|
37 | 35 | // calculate text size |
|
38 | 36 | QFontMetricsF fm(m_font); |
|
39 | 37 | QRectF textRect = fm.boundingRect(m_text); |
|
40 | 38 | |
|
41 | 39 | // calculate path for arm and text start point |
|
42 | 40 | qreal dx = qSin(m_armAngle*(PI/180)) * m_armLength; |
|
43 | 41 | qreal dy = -qCos(m_armAngle*(PI/180)) * m_armLength; |
|
44 | 42 | QPointF parm1 = m_armStartPoint + QPointF(dx, dy); |
|
45 | 43 | |
|
46 | 44 | // calculate horizontal arm and text position |
|
47 | 45 | QPointF parm2 = parm1; |
|
48 | 46 | textRect.moveBottomLeft(parm1); |
|
49 | 47 | if (m_armAngle < 180) { // arm swings the other way on the left side |
|
50 | 48 | parm2 += QPointF(m_textRect.width(), 0); |
|
51 | 49 | } else { |
|
52 | 50 | parm2 += QPointF(-m_textRect.width(),0); |
|
53 | 51 | textRect.moveBottomLeft(parm2); |
|
54 | 52 | } |
|
55 | 53 | |
|
56 | 54 | // add a little offset to text so that it does not touch the arm |
|
57 | 55 | qreal yOffset = m_pen.widthF() ? m_pen.widthF() : 2; |
|
58 | 56 | textRect.translate(0, -yOffset); |
|
59 | 57 | |
|
60 | 58 | // update arm path |
|
61 | 59 | QPainterPath path; |
|
62 | 60 | path.moveTo(m_armStartPoint); |
|
63 | 61 | path.lineTo(parm1); |
|
64 | 62 | path.lineTo(parm2); |
|
65 | 63 | |
|
66 | 64 | // update paths & rects |
|
67 | 65 | m_armPath = path; |
|
68 | 66 | m_textRect = textRect; |
|
69 | 67 | m_rect = path.boundingRect().united(m_textRect); |
|
70 | 68 | |
|
71 | 69 | //qDebug() << "PieSliceLabel::updateGeometry" << m_text << m_armStartPoint << m_armLength << m_armAngle << m_textRect; |
|
72 | 70 | } |
|
73 | 71 | |
|
74 | 72 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now