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