@@ -0,0 +1,20 | |||
|
1 | #include "customslice.h" | |
|
2 | ||
|
3 | CustomSlice::CustomSlice(qreal value, QObject* parent) | |
|
4 | :QPieSlice(parent) | |
|
5 | { | |
|
6 | setValue(value); | |
|
7 | connect(this, SIGNAL(changed()), this, SLOT(updateLabel())); | |
|
8 | connect(this, SIGNAL(hoverEnter()), this, SLOT(toggleExploded())); | |
|
9 | connect(this, SIGNAL(hoverLeave()), this, SLOT(toggleExploded())); | |
|
10 | } | |
|
11 | ||
|
12 | void CustomSlice::updateLabel() | |
|
13 | { | |
|
14 | setLabel(QString::number(this->percentage())); | |
|
15 | } | |
|
16 | ||
|
17 | void CustomSlice::toggleExploded() | |
|
18 | { | |
|
19 | setExploded(!isExploded()); | |
|
20 | } |
@@ -0,0 +1,15 | |||
|
1 | #include <qpieslice.h> | |
|
2 | ||
|
3 | QTCOMMERCIALCHART_USE_NAMESPACE | |
|
4 | ||
|
5 | class CustomSlice : public QPieSlice | |
|
6 | { | |
|
7 | Q_OBJECT | |
|
8 | ||
|
9 | public: | |
|
10 | CustomSlice(qreal value, QObject* parent = 0); | |
|
11 | ||
|
12 | public Q_SLOTS: | |
|
13 | void updateLabel(); | |
|
14 | void toggleExploded(); | |
|
15 | }; |
@@ -0,0 +1,181 | |||
|
1 | #include "qpieslice.h" | |
|
2 | ||
|
3 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
|
4 | ||
|
5 | #define DEFAULT_PEN_COLOR Qt::black | |
|
6 | #define DEFAULT_BRUSH_COLOR Qt::white | |
|
7 | #define DEFAULT_LABEL_ARM_LENGTH 50 | |
|
8 | ||
|
9 | QPieSlice::QPieSlice(QObject *parent) | |
|
10 | :QObject(parent), | |
|
11 | m_value(0), | |
|
12 | m_isLabelVisible(true), | |
|
13 | m_isExploded(false), | |
|
14 | m_percentage(0), | |
|
15 | m_angle(0), | |
|
16 | m_span(0), | |
|
17 | m_pen(DEFAULT_PEN_COLOR), | |
|
18 | m_brush(DEFAULT_BRUSH_COLOR), | |
|
19 | m_labelPen(DEFAULT_PEN_COLOR), | |
|
20 | m_labelArmLength(DEFAULT_LABEL_ARM_LENGTH) | |
|
21 | { | |
|
22 | ||
|
23 | } | |
|
24 | ||
|
25 | QPieSlice::QPieSlice(qreal value, QString label, bool labelVisible, QObject *parent) | |
|
26 | :QObject(parent), | |
|
27 | m_value(value), | |
|
28 | m_label(label), | |
|
29 | m_isLabelVisible(labelVisible), | |
|
30 | m_isExploded(false), | |
|
31 | m_percentage(0), | |
|
32 | m_angle(0), | |
|
33 | m_span(0), | |
|
34 | m_pen(DEFAULT_PEN_COLOR), | |
|
35 | m_brush(DEFAULT_BRUSH_COLOR), | |
|
36 | m_labelPen(DEFAULT_PEN_COLOR), | |
|
37 | m_labelArmLength(DEFAULT_LABEL_ARM_LENGTH) | |
|
38 | { | |
|
39 | ||
|
40 | } | |
|
41 | ||
|
42 | QPieSlice::~QPieSlice() | |
|
43 | { | |
|
44 | ||
|
45 | } | |
|
46 | ||
|
47 | qreal QPieSlice::value() const | |
|
48 | { | |
|
49 | return m_value; | |
|
50 | } | |
|
51 | ||
|
52 | QString QPieSlice::label() const | |
|
53 | { | |
|
54 | return m_label; | |
|
55 | } | |
|
56 | ||
|
57 | bool QPieSlice::isLabelVisible() const | |
|
58 | { | |
|
59 | return m_isLabelVisible; | |
|
60 | } | |
|
61 | ||
|
62 | bool QPieSlice::isExploded() const | |
|
63 | { | |
|
64 | return m_isExploded; | |
|
65 | } | |
|
66 | ||
|
67 | qreal QPieSlice::percentage() const | |
|
68 | { | |
|
69 | return m_percentage; | |
|
70 | } | |
|
71 | ||
|
72 | qreal QPieSlice::angle() const | |
|
73 | { | |
|
74 | return m_angle; | |
|
75 | } | |
|
76 | ||
|
77 | qreal QPieSlice::span() const | |
|
78 | { | |
|
79 | return m_span; | |
|
80 | } | |
|
81 | ||
|
82 | QPen QPieSlice::pen() const | |
|
83 | { | |
|
84 | return m_pen; | |
|
85 | } | |
|
86 | ||
|
87 | QBrush QPieSlice::brush() const | |
|
88 | { | |
|
89 | return m_brush; | |
|
90 | } | |
|
91 | ||
|
92 | QPen QPieSlice::labelPen() const | |
|
93 | { | |
|
94 | return m_labelPen; | |
|
95 | } | |
|
96 | ||
|
97 | QFont QPieSlice::labelFont() const | |
|
98 | { | |
|
99 | return m_labelFont; | |
|
100 | } | |
|
101 | ||
|
102 | qreal QPieSlice::labelArmLenght() const | |
|
103 | { | |
|
104 | return m_labelArmLength; | |
|
105 | } | |
|
106 | ||
|
107 | void QPieSlice::setValue(qreal value) | |
|
108 | { | |
|
109 | if (m_value != value) { | |
|
110 | m_value = value; | |
|
111 | emit changed(); | |
|
112 | } | |
|
113 | } | |
|
114 | ||
|
115 | void QPieSlice::setLabel(QString label) | |
|
116 | { | |
|
117 | if (m_label != label) { | |
|
118 | m_label = label; | |
|
119 | emit changed(); | |
|
120 | } | |
|
121 | } | |
|
122 | ||
|
123 | void QPieSlice::setLabelVisible(bool visible) | |
|
124 | { | |
|
125 | if (m_isLabelVisible != visible) { | |
|
126 | m_isLabelVisible = visible; | |
|
127 | emit changed(); | |
|
128 | } | |
|
129 | } | |
|
130 | ||
|
131 | void QPieSlice::setExploded(bool exploded) | |
|
132 | { | |
|
133 | if (m_isExploded != exploded) { | |
|
134 | m_isExploded = exploded; | |
|
135 | emit changed(); | |
|
136 | } | |
|
137 | } | |
|
138 | ||
|
139 | void QPieSlice::setPen(QPen pen) | |
|
140 | { | |
|
141 | if (m_pen != pen) { | |
|
142 | m_pen = pen; | |
|
143 | emit changed(); | |
|
144 | } | |
|
145 | } | |
|
146 | ||
|
147 | void QPieSlice::setBrush(QBrush brush) | |
|
148 | { | |
|
149 | if (m_brush != brush) { | |
|
150 | m_brush = brush; | |
|
151 | emit changed(); | |
|
152 | } | |
|
153 | } | |
|
154 | ||
|
155 | void QPieSlice::setLabelFont(QFont font) | |
|
156 | { | |
|
157 | if (m_labelFont != font) { | |
|
158 | m_labelFont = font; | |
|
159 | emit changed(); | |
|
160 | } | |
|
161 | } | |
|
162 | ||
|
163 | void QPieSlice::setLabelPen(QPen pen) | |
|
164 | { | |
|
165 | if (m_labelPen != pen) { | |
|
166 | m_labelPen = pen; | |
|
167 | emit changed(); | |
|
168 | } | |
|
169 | } | |
|
170 | ||
|
171 | void QPieSlice::setLabelArmLength(qreal len) | |
|
172 | { | |
|
173 | if (m_labelArmLength != len) { | |
|
174 | m_labelArmLength = len; | |
|
175 | emit changed(); | |
|
176 | } | |
|
177 | } | |
|
178 | ||
|
179 | #include "moc_qpieslice.cpp" | |
|
180 | ||
|
181 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -0,0 +1,86 | |||
|
1 | #ifndef QPIESLICE_H | |
|
2 | #define QPIESLICE_H | |
|
3 | ||
|
4 | #include <qchartglobal.h> | |
|
5 | #include <QObject> | |
|
6 | #include <QPen> | |
|
7 | #include <QBrush> | |
|
8 | #include <QFont> | |
|
9 | ||
|
10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
|
11 | ||
|
12 | class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject | |
|
13 | { | |
|
14 | Q_OBJECT | |
|
15 | ||
|
16 | public: | |
|
17 | QPieSlice(QObject *parent = 0); | |
|
18 | QPieSlice(qreal value, QString label, bool labelVisible = true, QObject *parent = 0); | |
|
19 | virtual ~QPieSlice(); | |
|
20 | ||
|
21 | // data | |
|
22 | qreal value() const; | |
|
23 | QString label() const; | |
|
24 | bool isLabelVisible() const; | |
|
25 | bool isExploded() const; | |
|
26 | ||
|
27 | // generated data | |
|
28 | qreal percentage() const; | |
|
29 | qreal angle() const; | |
|
30 | qreal span() const; | |
|
31 | ||
|
32 | // customization | |
|
33 | QPen pen() const; | |
|
34 | QBrush brush() const; | |
|
35 | QPen labelPen() const; | |
|
36 | QFont labelFont() const; | |
|
37 | qreal labelArmLenght() const; | |
|
38 | ||
|
39 | Q_SIGNALS: | |
|
40 | void clicked(); | |
|
41 | void hoverEnter(); | |
|
42 | void hoverLeave(); | |
|
43 | void changed(); | |
|
44 | ||
|
45 | public Q_SLOTS: | |
|
46 | ||
|
47 | // data | |
|
48 | void setLabel(QString label); | |
|
49 | void setLabelVisible(bool visible); | |
|
50 | void setValue(qreal value); | |
|
51 | void setExploded(bool exploded); | |
|
52 | ||
|
53 | // customization | |
|
54 | void setPen(QPen pen); | |
|
55 | void setBrush(QBrush brush); | |
|
56 | void setLabelFont(QFont font); | |
|
57 | void setLabelPen(QPen pen); | |
|
58 | void setLabelArmLength(qreal len); | |
|
59 | ||
|
60 | private: | |
|
61 | ||
|
62 | // TODO: use private class | |
|
63 | friend class QPieSeries; | |
|
64 | ||
|
65 | // data | |
|
66 | qreal m_value; | |
|
67 | QString m_label; | |
|
68 | bool m_isLabelVisible; | |
|
69 | bool m_isExploded; | |
|
70 | ||
|
71 | // generated data | |
|
72 | qreal m_percentage; | |
|
73 | qreal m_angle; | |
|
74 | qreal m_span; | |
|
75 | ||
|
76 | // customization | |
|
77 | QPen m_pen; | |
|
78 | QBrush m_brush; | |
|
79 | QPen m_labelPen; | |
|
80 | QFont m_labelFont; | |
|
81 | qreal m_labelArmLength; | |
|
82 | }; | |
|
83 | ||
|
84 | QTCOMMERCIALCHART_END_NAMESPACE | |
|
85 | ||
|
86 | #endif // QPIESLICE_H |
@@ -1,35 +1,35 | |||
|
1 | 1 | #include "multichartwidget.h" |
|
2 | 2 | #include <QVBoxLayout> |
|
3 | 3 | #include <qchartglobal.h> |
|
4 | 4 | #include <qchartview.h> |
|
5 | 5 | #include <qscatterseries.h> |
|
6 | 6 | #include <qpieseries.h> |
|
7 | 7 | |
|
8 | 8 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
9 | 9 | |
|
10 | 10 | MultiChartWidget::MultiChartWidget(QWidget *parent) : |
|
11 | 11 | QWidget(parent) |
|
12 | 12 | { |
|
13 | 13 | QVBoxLayout *l = new QVBoxLayout(this); |
|
14 | 14 | |
|
15 | 15 | // Create chart 1 and add a simple pie series onto it |
|
16 | 16 | QChartView *chartView1 = new QChartView(); |
|
17 | 17 | l->addWidget(chartView1); |
|
18 | 18 | QPieSeries *pie = new QPieSeries(); |
|
19 |
pie->add |
|
|
20 |
pie->add |
|
|
19 | pie->add(1.1, "label1"); | |
|
20 | pie->add(1.2, "label2"); | |
|
21 | 21 | chartView1->addSeries(pie); |
|
22 | 22 | |
|
23 | 23 | // Create chart 2 and add a simple scatter series onto it |
|
24 | 24 | QChartView *chartView2 = new QChartView(); |
|
25 | 25 | l->addWidget(chartView2); |
|
26 | 26 | QScatterSeries *scatter = new QScatterSeries(); |
|
27 | 27 | *scatter << QPointF(0.5, 5.0) |
|
28 | 28 | << QPointF(1.0, 4.5) |
|
29 | 29 | << QPointF(1.0, 5.5) |
|
30 | 30 | << QPointF(1.5, 5.0) |
|
31 | 31 | << QPointF(2.0, 4.5) |
|
32 | 32 | << QPointF(2.0, 5.5) |
|
33 | 33 | << QPointF(2.5, 5.0); |
|
34 | 34 | chartView2->addSeries(scatter); |
|
35 | 35 | } |
@@ -1,33 +1,42 | |||
|
1 | 1 | #include <QtGui/QApplication> |
|
2 | 2 | #include <QMainWindow> |
|
3 | #include <cmath> | |
|
4 | 3 | #include <qchartglobal.h> |
|
5 | 4 | #include <qchartview.h> |
|
6 | 5 | #include <qpieseries.h> |
|
6 | #include <qpieslice.h> | |
|
7 | #include "customslice.h" | |
|
7 | 8 | |
|
8 | 9 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
9 | 10 | |
|
10 | 11 | int main(int argc, char *argv[]) |
|
11 | 12 | { |
|
12 | 13 | QApplication a(argc, argv); |
|
13 | 14 | |
|
14 | // Create widget and scatter series | |
|
15 | QChartView *chartWidget = new QChartView(); | |
|
16 | QPieSeries *series = qobject_cast<QPieSeries *>(chartWidget->createSeries(QChartSeries::SeriesTypePie)); | |
|
17 | Q_ASSERT(series); | |
|
18 | ||
|
19 | // Add test data to the series | |
|
20 | series->add(QPieSlice(1, "test1", true, true, QPen(Qt::red, 2), QBrush(Qt::red))); | |
|
21 |
series->add( |
|
|
22 |
series->add( |
|
|
23 |
series->add( |
|
|
24 |
series->add( |
|
|
25 | ||
|
26 | // Use the chart widget as the central widget | |
|
27 | QMainWindow w; | |
|
28 | w.resize(640, 480); | |
|
29 | w.setCentralWidget(chartWidget); | |
|
30 | w.show(); | |
|
15 | QMainWindow window; | |
|
16 | ||
|
17 | QPieSeries *series = new QPieSeries(); | |
|
18 | series->add(5, "Slice 1"); | |
|
19 | series->add(2, "Slice 2"); | |
|
20 | series->add(3, "Slice 3"); | |
|
21 | series->add(4, "Slice 4"); | |
|
22 | series->add(5, "Slice 5"); | |
|
23 | series->add(6, "Slice 6"); | |
|
24 | series->add(7, "Slice 7"); | |
|
25 | series->add(new CustomSlice(8)); | |
|
26 | series->enableClickExplodes(true); | |
|
27 | series->enableHoverHighlight(true); | |
|
28 | ||
|
29 | foreach (QPieSlice*s, series->slices()) | |
|
30 | qDebug() << s->angle() << s->span() << s->percentage(); | |
|
31 | ||
|
32 | QChartView* chartView = new QChartView(&window); | |
|
33 | chartView->addSeries(series); | |
|
34 | chartView->setChartTitle("simple piechart"); | |
|
35 | chartView->setChartTheme(QChart::ChartThemeIcy); | |
|
36 | ||
|
37 | window.setCentralWidget(chartView); | |
|
38 | window.resize(600, 600); | |
|
39 | window.show(); | |
|
31 | 40 | |
|
32 | 41 | return a.exec(); |
|
33 | 42 | } |
@@ -1,17 +1,19 | |||
|
1 | 1 | !include( ../../common.pri ) { |
|
2 | 2 | error( "Couldn't find the common.pri file!" ) |
|
3 | 3 | } |
|
4 | 4 | !include( ../../integrated.pri ) { |
|
5 | 5 | error( "Couldn't find the integrated.pri file !") |
|
6 | 6 | } |
|
7 | 7 | |
|
8 | 8 | QT += core gui |
|
9 | 9 | |
|
10 | 10 | TARGET = piechart |
|
11 | 11 | TEMPLATE = app |
|
12 | 12 | |
|
13 | SOURCES += main.cpp | |
|
13 | SOURCES += main.cpp customslice.cpp | |
|
14 | HEADERS += customslice.h | |
|
14 | 15 | |
|
15 | HEADERS += | |
|
16 | OBJECTS_DIR = tmp | |
|
17 | MOC_DIR = tmp | |
|
16 | 18 | |
|
17 | 19 |
@@ -1,255 +1,254 | |||
|
1 | 1 | #include "charttheme_p.h" |
|
2 | 2 | #include "qchart.h" |
|
3 | 3 | #include "qchartaxis.h" |
|
4 | 4 | |
|
5 | 5 | |
|
6 | 6 | //series |
|
7 | 7 | #include "barchartseries.h" |
|
8 | 8 | #include "stackedbarchartseries.h" |
|
9 | 9 | #include "percentbarchartseries.h" |
|
10 | 10 | #include "qlinechartseries.h" |
|
11 | 11 | #include "qscatterseries.h" |
|
12 | 12 | #include "qpieseries.h" |
|
13 | #include "qpieslice.h" | |
|
13 | 14 | |
|
14 | 15 | //items |
|
15 | 16 | #include "axisitem_p.h" |
|
16 | 17 | #include "bargroup.h" |
|
17 | 18 | #include "stackedbargroup.h" |
|
18 | 19 | #include "linechartitem_p.h" |
|
19 | 20 | #include "percentbargroup.h" |
|
20 | 21 | #include "scatterpresenter_p.h" |
|
21 | 22 | #include "piepresenter.h" |
|
22 | 23 | |
|
23 | 24 | //themes |
|
24 | 25 | #include "chartthemevanilla_p.h" |
|
25 | 26 | #include "chartthemeicy_p.h" |
|
26 | 27 | #include "chartthemegrayscale_p.h" |
|
27 | 28 | #include "chartthemescientific_p.h" |
|
28 | 29 | |
|
29 | 30 | |
|
30 | 31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
31 | 32 | |
|
32 | 33 | /* TODO |
|
33 | 34 | case QChart::ChartThemeUnnamed1: |
|
34 | 35 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2)); |
|
35 | 36 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2)); |
|
36 | 37 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2)); |
|
37 | 38 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2)); |
|
38 | 39 | m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2)); |
|
39 | 40 | |
|
40 | 41 | m_gradientStartColor = QColor(QRgb(0xfff3dc9e)); |
|
41 | 42 | m_gradientEndColor = QColor(QRgb(0xffafafaf)); |
|
42 | 43 | */ |
|
43 | 44 | |
|
44 | 45 | ChartTheme::ChartTheme(QChart::ChartTheme id) |
|
45 | 46 | { |
|
46 | 47 | m_id = id; |
|
47 | 48 | m_seriesColor.append(QRgb(0xff000000)); |
|
48 | 49 | m_seriesColor.append(QRgb(0xff707070)); |
|
49 | 50 | m_gradientStartColor = QColor(QRgb(0xffffffff)); |
|
50 | 51 | m_gradientEndColor = QColor(QRgb(0xffafafaf)); |
|
51 | 52 | } |
|
52 | 53 | |
|
53 | 54 | |
|
54 | 55 | ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme) |
|
55 | 56 | { |
|
56 | 57 | switch(theme) { |
|
57 | 58 | case QChart::ChartThemeDefault: |
|
58 | 59 | return new ChartTheme(); |
|
59 | 60 | case QChart::ChartThemeVanilla: |
|
60 | 61 | return new ChartThemeVanilla(); |
|
61 | 62 | case QChart::ChartThemeIcy: |
|
62 | 63 | return new ChartThemeIcy(); |
|
63 | 64 | case QChart::ChartThemeGrayscale: |
|
64 | 65 | return new ChartThemeGrayscale(); |
|
65 | 66 | case QChart::ChartThemeScientific: |
|
66 | 67 | return new ChartThemeScientific(); |
|
67 | 68 | } |
|
68 | 69 | } |
|
69 | 70 | |
|
70 | 71 | void ChartTheme::decorate(QChart* chart) |
|
71 | 72 | { |
|
72 | 73 | QLinearGradient backgroundGradient; |
|
73 | 74 | backgroundGradient.setColorAt(0.0, m_gradientStartColor); |
|
74 | 75 | backgroundGradient.setColorAt(1.0, m_gradientEndColor); |
|
75 | 76 | backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode); |
|
76 | 77 | chart->setChartBackgroundBrush(backgroundGradient); |
|
77 | 78 | } |
|
78 | 79 | //TODO helper to by removed later |
|
79 | 80 | void ChartTheme::decorate(ChartItem* item, QChartSeries* series,int count) |
|
80 | 81 | { |
|
81 | 82 | switch(series->type()) |
|
82 | 83 | { |
|
83 | 84 | case QChartSeries::SeriesTypeLine: { |
|
84 | 85 | QLineChartSeries* s = static_cast<QLineChartSeries*>(series); |
|
85 | 86 | LineChartItem* i = static_cast<LineChartItem*>(item); |
|
86 | 87 | decorate(i,s,count); |
|
87 | 88 | break; |
|
88 | 89 | } |
|
89 | 90 | case QChartSeries::SeriesTypeBar: { |
|
90 | 91 | BarChartSeries* b = static_cast<BarChartSeries*>(series); |
|
91 | 92 | BarGroup* i = static_cast<BarGroup*>(item); |
|
92 | 93 | decorate(i,b,count); |
|
93 | 94 | break; |
|
94 | 95 | } |
|
95 | 96 | case QChartSeries::SeriesTypeStackedBar: { |
|
96 | 97 | StackedBarChartSeries* s = static_cast<StackedBarChartSeries*>(series); |
|
97 | 98 | StackedBarGroup* i = static_cast<StackedBarGroup*>(item); |
|
98 | 99 | decorate(i,s,count); |
|
99 | 100 | break; |
|
100 | 101 | } |
|
101 | 102 | case QChartSeries::SeriesTypePercentBar: { |
|
102 | 103 | PercentBarChartSeries* s = static_cast<PercentBarChartSeries*>(series); |
|
103 | 104 | PercentBarGroup* i = static_cast<PercentBarGroup*>(item); |
|
104 | 105 | decorate(i,s,count); |
|
105 | 106 | break; |
|
106 | 107 | } |
|
107 | 108 | case QChartSeries::SeriesTypeScatter: { |
|
108 | 109 | QScatterSeries* s = qobject_cast<QScatterSeries*>(series); |
|
109 | 110 | Q_ASSERT(s); |
|
110 | 111 | ScatterPresenter* i = static_cast<ScatterPresenter*>(item); |
|
111 | 112 | Q_ASSERT(i); |
|
112 | 113 | decorate(i, s, count); |
|
113 | 114 | break; |
|
114 | 115 | } |
|
115 | 116 | case QChartSeries::SeriesTypePie: { |
|
116 | 117 | QPieSeries* s = static_cast<QPieSeries*>(series); |
|
117 | 118 | PiePresenter* i = static_cast<PiePresenter*>(item); |
|
118 | 119 | decorate(i,s,count); |
|
119 | 120 | break; |
|
120 | 121 | } |
|
121 | 122 | default: |
|
122 | 123 | qDebug()<<"Wrong item to be decorated by theme"; |
|
123 | 124 | break; |
|
124 | 125 | } |
|
125 | 126 | |
|
126 | 127 | } |
|
127 | 128 | |
|
128 | 129 | void ChartTheme::decorate(LineChartItem* item, QLineChartSeries* series,int count) |
|
129 | 130 | { |
|
130 | 131 | QPen pen; |
|
131 | 132 | if(pen != series->pen()){ |
|
132 | 133 | item->setPen(series->pen()); |
|
133 | 134 | return; |
|
134 | 135 | } |
|
135 | 136 | pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); |
|
136 | 137 | pen.setWidthF(2); |
|
137 | 138 | item->setPen(pen); |
|
138 | 139 | } |
|
139 | 140 | |
|
140 | 141 | void ChartTheme::decorate(BarGroup* item, BarChartSeries* series,int count) |
|
141 | 142 | { |
|
142 | 143 | // TODO: better way to descide series color and remove hard coded colors. |
|
143 | 144 | item->resetBrushes(); |
|
144 | 145 | for (int i=0; i<m_seriesColor.count(); i++) { |
|
145 | 146 | QBrush brush(m_seriesColor.at(i)); |
|
146 | 147 | item->addBrush(brush); |
|
147 | 148 | } |
|
148 | 149 | item->addBrush(QBrush(QColor(255,0,0,128))); |
|
149 | 150 | item->addBrush(QBrush(QColor(255,255,0,128))); |
|
150 | 151 | item->addBrush(QBrush(QColor(0,255,0,128))); |
|
151 | 152 | item->addBrush(QBrush(QColor(0,0,255,128))); |
|
152 | 153 | item->addBrush(QBrush(QColor(255,128,0,128))); |
|
153 | 154 | } |
|
154 | 155 | |
|
155 | 156 | void ChartTheme::decorate(StackedBarGroup* item, StackedBarChartSeries* series,int count) |
|
156 | 157 | { |
|
157 | 158 | // TODO: better way to descide series color and remove hard coded colors. |
|
158 | 159 | item->resetBrushes(); |
|
159 | 160 | for (int i=0; i<m_seriesColor.count(); i++) { |
|
160 | 161 | QBrush brush(m_seriesColor.at(i)); |
|
161 | 162 | item->addBrush(brush); |
|
162 | 163 | } |
|
163 | 164 | item->addBrush(QBrush(QColor(255,0,0,128))); |
|
164 | 165 | item->addBrush(QBrush(QColor(255,255,0,128))); |
|
165 | 166 | item->addBrush(QBrush(QColor(0,255,0,128))); |
|
166 | 167 | item->addBrush(QBrush(QColor(0,0,255,128))); |
|
167 | 168 | item->addBrush(QBrush(QColor(255,128,0,128))); |
|
168 | 169 | } |
|
169 | 170 | |
|
170 | 171 | void ChartTheme::decorate(PercentBarGroup* item, PercentBarChartSeries* series,int count) |
|
171 | 172 | { |
|
172 | 173 | // TODO: better way to descide series color and remove hard coded colors. |
|
173 | 174 | item->resetBrushes(); |
|
174 | 175 | for (int i=0; i<m_seriesColor.count(); i++) { |
|
175 | 176 | QBrush brush(m_seriesColor.at(i)); |
|
176 | 177 | item->addBrush(brush); |
|
177 | 178 | } |
|
178 | 179 | item->addBrush(QBrush(QColor(255,0,0,128))); |
|
179 | 180 | item->addBrush(QBrush(QColor(255,255,0,128))); |
|
180 | 181 | item->addBrush(QBrush(QColor(0,255,0,128))); |
|
181 | 182 | item->addBrush(QBrush(QColor(0,0,255,128))); |
|
182 | 183 | item->addBrush(QBrush(QColor(255,128,0,128))); |
|
183 | 184 | } |
|
184 | 185 | |
|
185 | 186 | void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count) |
|
186 | 187 | { |
|
187 | 188 | Q_ASSERT(presenter); |
|
188 | 189 | Q_ASSERT(series); |
|
189 | 190 | |
|
190 | 191 | QColor color = m_seriesColor.at(count % m_seriesColor.size()); |
|
191 | 192 | // TODO: define alpha in the theme? or in the series? |
|
192 | 193 | color.setAlpha(120); |
|
193 | 194 | |
|
194 | 195 | QBrush brush(color, Qt::SolidPattern); |
|
195 | 196 | presenter->m_markerBrush = brush; |
|
196 | 197 | |
|
197 | 198 | QPen pen(brush, 1); |
|
198 | 199 | pen.setColor(color); |
|
199 | 200 | presenter->m_markerPen = pen; |
|
200 | 201 | } |
|
201 | 202 | |
|
202 | 203 | void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/) |
|
203 | 204 | { |
|
204 | 205 | // create a list of slice colors based on current theme |
|
205 | 206 | int i = 0; |
|
206 | 207 | QList<QColor> colors; |
|
207 | 208 | while (colors.count() < series->count()) { |
|
208 | 209 | |
|
209 | 210 | // get base color |
|
210 | 211 | QColor c = m_seriesColor[i++]; |
|
211 | 212 | i = i % m_seriesColor.count(); |
|
212 | 213 | |
|
213 | 214 | // -1 means achromatic color -> cannot manipulate lightness |
|
214 | 215 | // TODO: find a better way to randomize lightness |
|
215 | 216 | if (c.toHsv().hue() == -1) |
|
216 | 217 | qWarning() << "ChartTheme::decorate() warning: achromatic theme color"; |
|
217 | 218 | |
|
218 | 219 | // randomize lightness |
|
219 | 220 | qreal f = 50 + (qrand() % 100); // 50 is 50% darker, 100 is the same, 150 is 50% lighter |
|
220 | 221 | c = c.lighter(f); |
|
221 | 222 | |
|
222 | 223 | // find duplicates |
|
223 | 224 | bool isUnique = true; |
|
224 | 225 | foreach (QColor color, colors) { |
|
225 | 226 | if (c == color) |
|
226 | 227 | isUnique = false; |
|
227 | 228 | } |
|
228 | 229 | |
|
229 | 230 | // add to array if unique |
|
230 | 231 | //if (isUnique) |
|
231 | 232 | colors << c; |
|
232 | 233 | } |
|
233 | 234 | |
|
234 | 235 | // finally update colors |
|
235 |
foreach (QPieSlice |
|
|
236 | QPieSlice s = series->slice(id); | |
|
237 | s.setPen(QPen(Qt::black)); // TODO: get from theme | |
|
238 | s.setBrush(colors.takeFirst()); | |
|
239 | series->update(s); | |
|
236 | foreach (QPieSlice* s, series->slices()) { | |
|
237 | s->setPen(QPen(Qt::black)); // TODO: get from theme | |
|
238 | s->setBrush(colors.takeFirst()); | |
|
240 | 239 | } |
|
241 | 240 | } |
|
242 | 241 | |
|
243 | 242 | |
|
244 | 243 | void ChartTheme::decorate(QChartAxis& axis,AxisItem* item) |
|
245 | 244 | { |
|
246 | 245 | //TODO: dummy defults for now |
|
247 | 246 | |
|
248 | 247 | axis.setLabelsBrush(Qt::black); |
|
249 | 248 | axis.setLabelsPen(Qt::NoPen); |
|
250 | 249 | axis.setShadesPen(Qt::NoPen); |
|
251 | 250 | axis.setShadesOpacity(0.5); |
|
252 | 251 | item->handleAxisChanged(axis); |
|
253 | 252 | } |
|
254 | 253 | |
|
255 | 254 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,16 +1,18 | |||
|
1 | 1 | INCLUDEPATH += $$PWD |
|
2 | 2 | DEPENDPATH += $$PWD |
|
3 | 3 | |
|
4 | 4 | SOURCES += \ |
|
5 | 5 | $$PWD/qpieseries.cpp \ |
|
6 | 6 | $$PWD/pieslice.cpp \ |
|
7 | 7 | $$PWD/piepresenter.cpp \ |
|
8 | $$PWD/pieslicelabel.cpp | |
|
8 | $$PWD/pieslicelabel.cpp \ | |
|
9 | $$PWD/qpieslice.cpp | |
|
9 | 10 | |
|
10 | 11 | PRIVATE_HEADERS += \ |
|
11 | 12 | $$PWD/piepresenter.h \ |
|
12 | 13 | $$PWD/pieslice.h \ |
|
13 | 14 | $$PWD/pieslicelabel.h |
|
14 | 15 | |
|
15 | 16 | PUBLIC_HEADERS += \ |
|
16 | $$PWD/qpieseries.h | |
|
17 | $$PWD/qpieseries.h \ | |
|
18 | $$PWD/qpieslice.h |
@@ -1,170 +1,155 | |||
|
1 | 1 | |
|
2 | 2 | #include "piepresenter.h" |
|
3 | 3 | #include "pieslice.h" |
|
4 | #include "qpieslice.h" | |
|
4 | 5 | #include <QDebug> |
|
5 | 6 | #include <QTime> |
|
6 | 7 | |
|
7 | 8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | 9 | |
|
9 | 10 | PiePresenter::PiePresenter(QGraphicsItem *parent, QPieSeries *series) |
|
10 | 11 | :ChartItem(parent), |
|
11 | 12 | m_series(series) |
|
12 | 13 | { |
|
13 | 14 | Q_ASSERT(series); |
|
14 | 15 | connect(series, SIGNAL(changed(const QPieSeries::ChangeSet&)), this, SLOT(handleSeriesChanged(const QPieSeries::ChangeSet&))); |
|
15 | 16 | connect(series, SIGNAL(sizeFactorChanged()), this, SLOT(updateGeometry())); |
|
16 | 17 | connect(series, SIGNAL(positionChanged()), this, SLOT(updateGeometry())); |
|
17 | 18 | } |
|
18 | 19 | |
|
19 | 20 | PiePresenter::~PiePresenter() |
|
20 | 21 | { |
|
21 | 22 | // slices deleted automatically through QGraphicsItem |
|
22 | 23 | } |
|
23 | 24 | |
|
24 | 25 | void PiePresenter::paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) |
|
25 | 26 | { |
|
26 | 27 | // TODO: paint shadows for all components |
|
27 | 28 | // - get paths from items & merge & offset and draw with shadow color? |
|
28 | 29 | } |
|
29 | 30 | |
|
30 | 31 | void PiePresenter::handleSeriesChanged(const QPieSeries::ChangeSet& changeSet) |
|
31 | 32 | { |
|
32 | qDebug() << "PiePresenter::handleSeriesChanged()"; | |
|
33 |
qDebug() << " added : " << changeSet. |
|
|
34 |
qDebug() << " changed: " << changeSet. |
|
|
35 |
qDebug() << " removed: " << changeSet. |
|
|
33 | //qDebug() << "PiePresenter::handleSeriesChanged()"; | |
|
34 | //qDebug() << " added : " << changeSet.added(); | |
|
35 | //qDebug() << " changed: " << changeSet.changed(); | |
|
36 | //qDebug() << " removed: " << changeSet.removed(); | |
|
36 | 37 | |
|
37 | 38 | // ignore changeset when there are no visual slices |
|
38 | 39 | // changeset might not be valid about the added slices |
|
39 | 40 | if (m_slices.count() == 0) { |
|
40 |
foreach (QPieSlice |
|
|
41 |
addSlice( |
|
|
41 | foreach (QPieSlice* s, m_series->m_slices) | |
|
42 | addSlice(s); | |
|
42 | 43 | return; |
|
43 | 44 | } |
|
44 | 45 | |
|
45 |
foreach (QPieSlice |
|
|
46 |
deleteSlice( |
|
|
46 | foreach (QPieSlice* s, changeSet.removed()) | |
|
47 | deleteSlice(s); | |
|
47 | 48 | |
|
48 |
foreach (QPieSlice |
|
|
49 |
|
|
|
49 | foreach (QPieSlice* s, changeSet.added()) | |
|
50 | addSlice(s); | |
|
51 | } | |
|
50 | 52 | |
|
51 | foreach (QPieSliceId id, changeSet.m_added) | |
|
52 | addSlice(id); | |
|
53 | void PiePresenter::handleDomainChanged(const Domain& domain) | |
|
54 | { | |
|
55 | // TODO | |
|
56 | } | |
|
57 | ||
|
58 | void PiePresenter::handleGeometryChanged(const QRectF& rect) | |
|
59 | { | |
|
60 | m_rect = rect; | |
|
61 | updateGeometry(); | |
|
53 | 62 | } |
|
54 | 63 | |
|
55 | 64 | void PiePresenter::updateGeometry() |
|
56 | 65 | { |
|
57 | 66 | prepareGeometryChange(); |
|
58 | 67 | |
|
59 |
|
|
|
68 | QRectF pieRect = m_rect; | |
|
60 | 69 | |
|
61 |
if ( |
|
|
62 |
|
|
|
63 |
|
|
|
64 |
|
|
|
70 | if (pieRect.width() < pieRect.height()) { | |
|
71 | pieRect.setWidth(pieRect.width() * m_series->sizeFactor()); | |
|
72 | pieRect.setHeight(pieRect.width()); | |
|
73 | pieRect.moveCenter(m_rect.center()); | |
|
65 | 74 | } else { |
|
66 |
|
|
|
67 |
|
|
|
68 |
|
|
|
75 | pieRect.setHeight(pieRect.height() * m_series->sizeFactor()); | |
|
76 | pieRect.setWidth(pieRect.height()); | |
|
77 | pieRect.moveCenter(m_rect.center()); | |
|
69 | 78 | } |
|
70 | 79 | |
|
71 | 80 | switch (m_series->position()) { |
|
72 | 81 | case QPieSeries::PiePositionTopLeft: { |
|
73 |
|
|
|
74 |
|
|
|
75 |
|
|
|
82 | pieRect.setHeight(pieRect.height() / 2); | |
|
83 | pieRect.setWidth(pieRect.height()); | |
|
84 | pieRect.moveCenter(QPointF(m_rect.center().x() / 2, m_rect.center().y() / 2)); | |
|
76 | 85 | break; |
|
77 | 86 | } |
|
78 | 87 | case QPieSeries::PiePositionTopRight: { |
|
79 |
|
|
|
80 |
|
|
|
81 |
|
|
|
88 | pieRect.setHeight(pieRect.height() / 2); | |
|
89 | pieRect.setWidth(pieRect.height()); | |
|
90 | pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, m_rect.center().y() / 2)); | |
|
82 | 91 | break; |
|
83 | 92 | } |
|
84 | 93 | case QPieSeries::PiePositionBottomLeft: { |
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
|
94 | pieRect.setHeight(pieRect.height() / 2); | |
|
95 | pieRect.setWidth(pieRect.height()); | |
|
96 | pieRect.moveCenter(QPointF(m_rect.center().x() / 2, (m_rect.center().y() / 2) * 3)); | |
|
88 | 97 | break; |
|
89 | 98 | } |
|
90 | 99 | case QPieSeries::PiePositionBottomRight: { |
|
91 |
|
|
|
92 |
|
|
|
93 |
|
|
|
100 | pieRect.setHeight(pieRect.height() / 2); | |
|
101 | pieRect.setWidth(pieRect.height()); | |
|
102 | pieRect.moveCenter(QPointF((m_rect.center().x() / 2) * 3, (m_rect.center().y() / 2) * 3)); | |
|
94 | 103 | break; |
|
95 | 104 | } |
|
96 | 105 | default: |
|
97 | 106 | break; |
|
98 | 107 | } |
|
99 | 108 | |
|
100 | // update slice geometry | |
|
101 | const qreal fullPie = 360; | |
|
102 | qreal angle = 0; | |
|
103 |
foreach ( |
|
|
104 | qreal span = fullPie * m_series->slice(id).percentage(); | |
|
105 |
|
|
|
106 | angle += span; | |
|
109 | if (m_pieRect != pieRect) { | |
|
110 | m_pieRect = pieRect; | |
|
111 | //qDebug() << "PiePresenter::updateGeometry()" << m_pieRect; | |
|
112 | foreach (PieSlice* s, m_slices.values()) { | |
|
113 | s->setPieRect(m_pieRect); | |
|
114 | s->updateGeometry(); | |
|
115 | } | |
|
107 | 116 | } |
|
108 | ||
|
109 | //qDebug() << "PiePresenter::updateGeometry" << m_rect << m_pieRect; | |
|
110 | 117 | } |
|
111 | 118 | |
|
112 | void PiePresenter::handleDomainChanged(const Domain& domain) | |
|
119 | void PiePresenter::addSlice(QPieSlice* sliceData) | |
|
113 | 120 | { |
|
114 | // TODO | |
|
115 | } | |
|
121 | //qDebug() << "PiePresenter::addSlice()" << sliceData; | |
|
116 | 122 | |
|
117 | void PiePresenter::handleGeometryChanged(const QRectF& rect) | |
|
118 | { | |
|
119 | m_rect = rect; | |
|
120 | updateGeometry(); | |
|
121 | } | |
|
122 | ||
|
123 | void PiePresenter::addSlice(QPieSliceId id) | |
|
124 | { | |
|
125 | qDebug() << "PiePresenter::addSlice()" << id; | |
|
126 | ||
|
127 | if (m_slices.contains(id)) { | |
|
128 | qWarning() << "PiePresenter::addSlice(): slice already exists!" << id; | |
|
129 | updateSlice(id); | |
|
123 | if (m_slices.keys().contains(sliceData)) { | |
|
124 | //qWarning() << "PiePresenter::addSlice(): slice already exists!" << sliceData; | |
|
125 | Q_ASSERT(0); | |
|
130 | 126 | return; |
|
131 | 127 | } |
|
132 | 128 | |
|
133 | 129 | // create slice |
|
134 |
PieSlice *slice = new PieSlice( |
|
|
135 | m_slices.insert(id, slice); | |
|
136 | ||
|
137 | updateGeometry(); | |
|
130 | PieSlice *slice = new PieSlice(this); | |
|
131 | slice->updateData(sliceData); | |
|
132 | m_slices.insert(sliceData, slice); | |
|
133 | ||
|
134 | // connect signals | |
|
135 | connect(sliceData, SIGNAL(changed()), slice, SLOT(handleSliceDataChanged())); | |
|
136 | connect(slice, SIGNAL(clicked()), sliceData, SIGNAL(clicked())); | |
|
137 | connect(slice, SIGNAL(hoverEnter()), sliceData, SIGNAL(hoverEnter())); | |
|
138 | connect(slice, SIGNAL(hoverLeave()), sliceData, SIGNAL(hoverLeave())); | |
|
138 | 139 | } |
|
139 | 140 | |
|
140 |
void PiePresenter:: |
|
|
141 | void PiePresenter::deleteSlice(QPieSlice* sliceData) | |
|
141 | 142 | { |
|
142 |
qDebug() << "PiePresenter:: |
|
|
143 | //qDebug() << "PiePresenter::deleteSlice()" << sliceData; | |
|
143 | 144 | |
|
144 | // TODO: animation | |
|
145 | if (m_slices.contains(id)) | |
|
146 | m_slices.value(id)->updateData(); | |
|
145 | if (m_slices.contains(sliceData)) | |
|
146 | delete m_slices.take(sliceData); | |
|
147 | 147 | else { |
|
148 | qWarning() << "PiePresenter::updateSlice(): slice does not exist!" << id; | |
|
149 | addSlice(id); | |
|
148 | // nothing to remove | |
|
149 | Q_ASSERT(0); // TODO: remove before release | |
|
150 | 150 | } |
|
151 | ||
|
152 | updateGeometry(); | |
|
153 | } | |
|
154 | ||
|
155 | void PiePresenter::deleteSlice(QPieSliceId id) | |
|
156 | { | |
|
157 | qDebug() << "PiePresenter::deleteSlice()" << id; | |
|
158 | ||
|
159 | // TODO: animation | |
|
160 | if (m_slices.contains(id)) | |
|
161 | delete m_slices.take(id); | |
|
162 | else | |
|
163 | qWarning() << "PiePresenter::deleteSlice(): slice does not exist!" << id; | |
|
164 | ||
|
165 | updateGeometry(); | |
|
166 | 151 | } |
|
167 | 152 | |
|
168 | 153 | #include "moc_piepresenter.cpp" |
|
169 | 154 | |
|
170 | 155 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,49 +1,48 | |||
|
1 | 1 | #ifndef PIEPRESENTER_H |
|
2 | 2 | #define PIEPRESENTER_H |
|
3 | 3 | |
|
4 | 4 | #include "chartitem_p.h" |
|
5 | 5 | #include "qpieseries.h" |
|
6 | 6 | #include <QSignalMapper> |
|
7 | 7 | |
|
8 | 8 | class QGraphicsItem; |
|
9 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 10 | class PieSlice; |
|
11 | 11 | |
|
12 | 12 | class PiePresenter : public QObject, public ChartItem |
|
13 | 13 | { |
|
14 | 14 | Q_OBJECT |
|
15 | 15 | |
|
16 | 16 | public: |
|
17 | 17 | // TODO: use a generic data class instead of x and y |
|
18 | 18 | PiePresenter(QGraphicsItem *parent, QPieSeries *series); |
|
19 | 19 | ~PiePresenter(); |
|
20 | 20 | |
|
21 | 21 | public: // from QGraphicsItem |
|
22 | 22 | QRectF boundingRect() const { return m_rect; } |
|
23 | 23 | void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *); |
|
24 | 24 | |
|
25 | 25 | public: |
|
26 | 26 | QRectF pieRect() const { return m_pieRect; } |
|
27 | 27 | |
|
28 | 28 | public Q_SLOTS: |
|
29 | 29 | void handleSeriesChanged(const QPieSeries::ChangeSet& changeSet); |
|
30 | 30 | void handleDomainChanged(const Domain& domain); |
|
31 | 31 | void handleGeometryChanged(const QRectF& rect); |
|
32 | 32 | void updateGeometry(); |
|
33 | 33 | |
|
34 | 34 | private: |
|
35 |
void addSlice(QPieSlice |
|
|
36 |
void |
|
|
37 | void deleteSlice(QPieSliceId id); | |
|
35 | void addSlice(QPieSlice* sliceData); | |
|
36 | void deleteSlice(QPieSlice* sliceData); | |
|
38 | 37 | |
|
39 | 38 | private: |
|
40 | 39 | friend class PieSlice; |
|
41 |
QHash<QPieSlice |
|
|
40 | QHash<QPieSlice*, PieSlice*> m_slices; | |
|
42 | 41 | QPieSeries *m_series; |
|
43 | 42 | QRectF m_rect; |
|
44 | 43 | QRectF m_pieRect; |
|
45 | 44 | }; |
|
46 | 45 | |
|
47 | 46 | QTCOMMERCIALCHART_END_NAMESPACE |
|
48 | 47 | |
|
49 | 48 | #endif // PIEPRESENTER_H |
@@ -1,142 +1,143 | |||
|
1 | 1 | #include "pieslice.h" |
|
2 | 2 | #include "pieslicelabel.h" |
|
3 | 3 | #include "piepresenter.h" |
|
4 | 4 | #include "qpieseries.h" |
|
5 | #include "qpieslice.h" | |
|
5 | 6 | #include <QPainter> |
|
6 | 7 | #include <QDebug> |
|
7 | 8 | #include <qmath.h> |
|
8 | 9 | #include <QGraphicsSceneEvent> |
|
9 | 10 | #include <QTime> |
|
10 | 11 | |
|
11 | 12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
12 | 13 | |
|
13 | 14 | #define PI 3.14159265 |
|
14 | 15 | #define EXPLODE_OFFSET 20 |
|
15 | 16 | |
|
16 | 17 | QPointF offset(qreal angle, qreal length) |
|
17 | 18 | { |
|
18 | 19 | qreal dx = qSin(angle*(PI/180)) * length; |
|
19 | 20 | qreal dy = qCos(angle*(PI/180)) * length; |
|
20 | 21 | return QPointF(dx, -dy); |
|
21 | 22 | } |
|
22 | 23 | |
|
23 |
PieSlice::PieSlice( |
|
|
24 | PieSlice::PieSlice(QGraphicsItem* parent) | |
|
24 | 25 | :QGraphicsObject(parent), |
|
25 | m_id(id), | |
|
26 | m_series(series), | |
|
27 | 26 | m_slicelabel(new PieSliceLabel(this)), |
|
28 | m_isHovering(false) | |
|
27 | m_angle(0), | |
|
28 | m_span(0), | |
|
29 | m_isExploded(false) | |
|
29 | 30 | { |
|
30 | Q_ASSERT(series); | |
|
31 | 31 | setAcceptHoverEvents(true); |
|
32 | 32 | setAcceptedMouseButtons(Qt::LeftButton); |
|
33 | updateData(); | |
|
34 | 33 | } |
|
35 | 34 | |
|
36 | 35 | PieSlice::~PieSlice() |
|
37 | 36 | { |
|
38 | qDebug() << "~PieSlice()" << m_id; | |
|
37 | ||
|
39 | 38 | } |
|
40 | 39 | |
|
41 | 40 | QRectF PieSlice::boundingRect() const |
|
42 | 41 | { |
|
43 |
return m_ |
|
|
42 | return m_path.boundingRect(); | |
|
44 | 43 | } |
|
45 | 44 | |
|
46 | 45 | QPainterPath PieSlice::shape() const |
|
47 | 46 | { |
|
48 | 47 | return m_path; |
|
49 | 48 | } |
|
50 | 49 | |
|
51 | 50 | void PieSlice::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/) |
|
52 | 51 | { |
|
53 | // set hover brush | |
|
54 | // TODO: what if we are using gradients... | |
|
55 | QBrush brush = m_data.brush(); | |
|
56 | if (m_isHovering) | |
|
57 | brush.setColor(brush.color().lighter()); | |
|
58 | ||
|
59 | 52 | painter->setRenderHint(QPainter::Antialiasing); |
|
60 |
painter->setPen(m_ |
|
|
61 | painter->setBrush(brush); | |
|
53 | painter->setPen(m_pen); | |
|
54 | painter->setBrush(m_brush); | |
|
62 | 55 | painter->drawPath(m_path); |
|
63 | 56 | } |
|
64 | 57 | |
|
65 | 58 | void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/) |
|
66 | 59 | { |
|
67 |
|
|
|
68 | update(); | |
|
69 | // TODO: emit hoverEnter() | |
|
60 | emit hoverEnter(); | |
|
70 | 61 | } |
|
71 | 62 | |
|
72 | 63 | void PieSlice::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/) |
|
73 | 64 | { |
|
74 | m_isHovering = false; | |
|
75 | update(); | |
|
76 | // TODO: emit hoverLeave() | |
|
65 | emit hoverLeave(); | |
|
77 | 66 | } |
|
78 | 67 | |
|
79 | 68 | void PieSlice::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/) |
|
80 | 69 | { |
|
81 |
|
|
|
82 | // TODO: should we let the user decide if this can be exploded? | |
|
83 | m_data.setExploded(!m_data.isExploded()); | |
|
84 | m_series->update(m_data); | |
|
70 | emit clicked(); | |
|
71 | } | |
|
72 | ||
|
73 | void PieSlice::setPieRect(QRectF rect) | |
|
74 | { | |
|
75 | m_pieRect = rect; | |
|
85 | 76 | } |
|
86 | 77 | |
|
87 |
void PieSlice::updateGeometry( |
|
|
78 | void PieSlice::updateGeometry() | |
|
88 | 79 | { |
|
89 | 80 | prepareGeometryChange(); |
|
90 | 81 | |
|
91 | 82 | // calculate center angle |
|
92 |
qreal centerAngle = |
|
|
83 | qreal centerAngle = m_angle + (m_span/2); | |
|
93 | 84 | |
|
94 | 85 | // adjust rect for exploding |
|
86 | QRectF rect = m_pieRect; | |
|
95 | 87 | rect.adjust(EXPLODE_OFFSET, EXPLODE_OFFSET, -EXPLODE_OFFSET ,-EXPLODE_OFFSET); |
|
96 |
if (m_ |
|
|
88 | if (m_isExploded) { | |
|
97 | 89 | QPointF d = offset((centerAngle), EXPLODE_OFFSET); |
|
98 | 90 | rect.translate(d.x(), d.y()); |
|
99 | 91 | } |
|
100 | 92 | |
|
101 | 93 | // update slice path |
|
102 | 94 | // TODO: draw the shape so that it might have a hole in the center |
|
103 | 95 | QPainterPath path; |
|
104 | 96 | path.moveTo(rect.center()); |
|
105 |
path.arcTo(rect, - |
|
|
97 | path.arcTo(rect, -m_angle + 90, -m_span); | |
|
98 | path.closeSubpath(); | |
|
106 | 99 | m_path = path; |
|
107 | m_rect = path.boundingRect(); | |
|
108 | 100 | |
|
109 | 101 | // update label position |
|
110 | 102 | qreal radius = rect.height() / 2; |
|
111 | 103 | QPointF edgeCenter = rect.center() + offset(centerAngle, radius + 5); |
|
112 | 104 | |
|
113 | 105 | m_slicelabel->setArmStartPoint(edgeCenter); |
|
114 | 106 | m_slicelabel->setArmAngle(centerAngle); |
|
115 | m_slicelabel->setArmLength(50); | |
|
116 | 107 | m_slicelabel->updateGeometry(); |
|
117 | 108 | |
|
118 |
//qDebug() << "PieSlice::updateGeometry" << m_ |
|
|
109 | //qDebug() << "PieSlice::updateGeometry" << m_slicelabel->text() << boundingRect() << m_angle << m_span; | |
|
110 | } | |
|
111 | ||
|
112 | void PieSlice::handleSliceDataChanged() | |
|
113 | { | |
|
114 | QPieSlice *slice = qobject_cast<QPieSlice*>(sender()); | |
|
115 | Q_ASSERT(slice); | |
|
116 | updateData(slice); | |
|
119 | 117 | } |
|
120 | 118 | |
|
121 | void PieSlice::updateData() | |
|
119 | void PieSlice::updateData(const QPieSlice* sliceData) | |
|
122 | 120 | { |
|
123 | if (!m_series->m_slices.contains(m_id)) | |
|
124 | qWarning() << "PieSlice::updateData(): cannot find slice data!" << m_id; | |
|
121 | // TODO: compare what has changes to avoid unneccesary geometry updates | |
|
125 | 122 | |
|
126 | QPieSlice data = m_series->slice(m_id); | |
|
127 | // TODO: find out what has changed and trigger some animation | |
|
128 | m_data = data; | |
|
123 | m_angle = sliceData->angle(); | |
|
124 | m_span = sliceData->span(); | |
|
125 | m_isExploded = sliceData->isExploded(); | |
|
126 | m_pen = sliceData->pen(); | |
|
127 | m_brush = sliceData->brush(); | |
|
129 | 128 | |
|
129 | updateGeometry(); | |
|
130 | 130 | update(); |
|
131 | 131 | |
|
132 |
m_slicelabel->setVisible( |
|
|
133 |
m_slicelabel->setText( |
|
|
134 |
|
|
|
135 |
|
|
|
132 | m_slicelabel->setVisible(sliceData->isLabelVisible()); | |
|
133 | m_slicelabel->setText(sliceData->label()); | |
|
134 | m_slicelabel->setPen(sliceData->labelPen()); | |
|
135 | m_slicelabel->setFont(sliceData->labelFont()); | |
|
136 | m_slicelabel->setArmLength(sliceData->labelArmLenght()); | |
|
136 | 137 | m_slicelabel->updateGeometry(); // text size & font modifies the geometry |
|
137 | 138 | m_slicelabel->update(); |
|
138 | 139 | } |
|
139 | 140 | |
|
140 | 141 | #include "moc_pieslice.cpp" |
|
141 | 142 | |
|
142 | 143 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,51 +1,60 | |||
|
1 | 1 | #ifndef PIESLICE_H |
|
2 | 2 | #define PIESLICE_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartglobal.h" |
|
5 | 5 | #include "charttheme_p.h" |
|
6 | 6 | #include "qpieseries.h" |
|
7 | 7 | #include <QGraphicsItem> |
|
8 | 8 | #include <QRectF> |
|
9 | 9 | #include <QColor> |
|
10 | 10 | #include <QPen> |
|
11 | 11 | |
|
12 | 12 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
13 | 13 | class PiePresenter; |
|
14 | 14 | class PieSliceLabel; |
|
15 | class QPieSlice; | |
|
15 | 16 | |
|
16 | 17 | class PieSlice : public QGraphicsObject |
|
17 | 18 | { |
|
18 | 19 | Q_OBJECT |
|
19 | 20 | |
|
20 | 21 | public: |
|
21 |
PieSlice( |
|
|
22 | PieSlice(QGraphicsItem* parent = 0); | |
|
22 | 23 | ~PieSlice(); |
|
23 | 24 | |
|
24 | 25 | public: // from QGraphicsItem |
|
25 | 26 | QRectF boundingRect() const; |
|
26 | 27 | QPainterPath shape() const; |
|
27 | 28 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
28 | 29 | void hoverEnterEvent(QGraphicsSceneHoverEvent *event); |
|
29 | 30 | void hoverLeaveEvent(QGraphicsSceneHoverEvent *event); |
|
30 | 31 | void mousePressEvent(QGraphicsSceneMouseEvent *event); |
|
31 | 32 | |
|
32 | 33 | Q_SIGNALS: |
|
33 | 34 | void clicked(); |
|
35 | void hoverEnter(); | |
|
36 | void hoverLeave(); | |
|
34 | 37 | |
|
35 | public: | |
|
36 | void updateGeometry(QRectF rect, qreal startAngle, qreal span); | |
|
37 | void updateData(); | |
|
38 | public Q_SLOTS: | |
|
39 | void handleSliceDataChanged(); | |
|
40 | void setPieRect(QRectF rect); | |
|
41 | void updateGeometry(); | |
|
42 | void updateData(const QPieSlice *sliceData); | |
|
38 | 43 | |
|
39 | 44 | private: |
|
40 | QPieSliceId m_id; | |
|
41 | QPieSeries* m_series; | |
|
42 | QPieSlice m_data; | |
|
43 | 45 | PieSliceLabel* m_slicelabel; |
|
46 | ||
|
47 | QRectF m_pieRect; | |
|
44 | 48 | QPainterPath m_path; |
|
45 | QRectF m_rect; | |
|
46 | bool m_isHovering; | |
|
49 | ||
|
50 | qreal m_angle; | |
|
51 | qreal m_span; | |
|
52 | bool m_isExploded; | |
|
53 | ||
|
54 | QPen m_pen; | |
|
55 | QBrush m_brush; | |
|
47 | 56 | }; |
|
48 | 57 | |
|
49 | 58 | QTCOMMERCIALCHART_END_NAMESPACE |
|
50 | 59 | |
|
51 | 60 | #endif // PIESLICE_H |
@@ -1,152 +1,300 | |||
|
1 | 1 | #include "qpieseries.h" |
|
2 | #include "qpieslice.h" | |
|
2 | 3 | #include "piepresenter.h" |
|
3 | 4 | #include "pieslice.h" |
|
4 | 5 | #include <QDebug> |
|
5 | 6 | |
|
6 | 7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | 8 | |
|
8 | QPieSeries::QPieSeries(QObject *parent) : | |
|
9 | QChartSeries(parent), | |
|
10 | m_sizeFactor(1.0), | |
|
11 | m_position(PiePositionMaximized), | |
|
12 | m_sliceIdSeed(0) | |
|
9 | void QPieSeries::ChangeSet::appendAdded(QPieSlice* slice) | |
|
13 | 10 | { |
|
11 | if (!m_added.contains(slice)) | |
|
12 | m_added << slice; | |
|
14 | 13 | } |
|
15 | 14 | |
|
16 | QPieSeries::~QPieSeries() | |
|
15 | void QPieSeries::ChangeSet::appendChanged(QPieSlice* slice) | |
|
17 | 16 | { |
|
17 | if (!m_changed.contains(slice)) | |
|
18 | m_changed << slice; | |
|
19 | } | |
|
18 | 20 | |
|
21 | void QPieSeries::ChangeSet::appendRemoved(QPieSlice* slice) | |
|
22 | { | |
|
23 | if (!m_removed.contains(slice)) | |
|
24 | m_removed << slice; | |
|
19 | 25 | } |
|
20 | 26 | |
|
21 | bool QPieSeries::setData(QList<qreal> data) | |
|
27 | QList<QPieSlice*> QPieSeries::ChangeSet::added() const | |
|
22 | 28 | { |
|
23 | // TODO: remove this function | |
|
24 | QList<QPieSlice> slices; | |
|
25 | foreach (int value, data) | |
|
26 | slices << QPieSlice(value, QString::number(value)); | |
|
27 | return set(slices); | |
|
29 | return m_added; | |
|
28 | 30 | } |
|
29 | 31 | |
|
30 | bool QPieSeries::set(const QList<QPieSlice>& slices) | |
|
32 | QList<QPieSlice*> QPieSeries::ChangeSet::changed() const | |
|
31 | 33 | { |
|
32 | if (!slices.count()) | |
|
33 | return false; | |
|
34 | return m_changed; | |
|
35 | } | |
|
34 | 36 | |
|
35 | ChangeSet changeSet; | |
|
37 | QList<QPieSlice*> QPieSeries::ChangeSet::removed() const | |
|
38 | { | |
|
39 | return m_removed; | |
|
40 | } | |
|
36 | 41 | |
|
37 | foreach (QPieSlice s, m_slices.values()) | |
|
38 | changeSet.m_removed << s.id(); | |
|
42 | bool QPieSeries::ChangeSet::isEmpty() const | |
|
43 | { | |
|
44 | if (m_added.count() || m_changed.count() || m_removed.count()) | |
|
45 | return false; | |
|
46 | return true; | |
|
47 | } | |
|
39 | 48 | |
|
40 | m_slices.clear(); | |
|
41 | 49 | |
|
42 | foreach (QPieSlice s, slices) { | |
|
43 | s.m_id = generateSliceId(); | |
|
44 | m_slices.insert(s.id(), s); | |
|
45 | changeSet.m_added << s.id(); | |
|
46 | } | |
|
50 | QPieSeries::QPieSeries(QObject *parent) : | |
|
51 | QChartSeries(parent), | |
|
52 | m_sizeFactor(1.0), | |
|
53 | m_position(PiePositionMaximized), | |
|
54 | m_pieStartAngle(0), | |
|
55 | m_pieSpan(360) | |
|
56 | { | |
|
47 | 57 | |
|
48 | updateDerivativeData(); | |
|
49 | emit changed(changeSet); | |
|
58 | } | |
|
50 | 59 | |
|
60 | QPieSeries::~QPieSeries() | |
|
61 | { | |
|
62 | // slices destroyed by parent object | |
|
63 | qDebug() << "~QPieSeries"; | |
|
64 | } | |
|
65 | ||
|
66 | bool QPieSeries::setData(QList<qreal> data) | |
|
67 | { | |
|
68 | // TODO: remove this function | |
|
69 | QList<QPieSlice*> slices; | |
|
70 | foreach (int value, data) | |
|
71 | slices << new QPieSlice(value, QString::number(value)); | |
|
72 | set(slices); | |
|
51 | 73 | return true; |
|
52 | 74 | } |
|
53 | 75 | |
|
54 |
|
|
|
76 | void QPieSeries::set(QList<QPieSlice*> slices) | |
|
55 | 77 | { |
|
56 | if (!slices.count()) | |
|
57 | return false; | |
|
78 | clear(); | |
|
79 | add(slices); | |
|
80 | } | |
|
58 | 81 | |
|
82 | void QPieSeries::add(QList<QPieSlice*> slices) | |
|
83 | { | |
|
59 | 84 | ChangeSet changeSet; |
|
60 | foreach (QPieSlice s, slices) { | |
|
61 | s.m_id = generateSliceId(); | |
|
62 |
m_slices |
|
|
63 |
changeSet. |
|
|
85 | foreach (QPieSlice* s, slices) { | |
|
86 | s->setParent(this); | |
|
87 | m_slices << s; | |
|
88 | changeSet.appendAdded(s); | |
|
64 | 89 | } |
|
65 | 90 | |
|
66 | 91 | updateDerivativeData(); |
|
67 | emit changed(changeSet); | |
|
68 | 92 | |
|
69 | return true; | |
|
93 | foreach (QPieSlice* s, slices) { | |
|
94 | connect(s, SIGNAL(changed()), this, SLOT(sliceChanged())); | |
|
95 | connect(s, SIGNAL(clicked()), this, SLOT(sliceClicked())); | |
|
96 | connect(s, SIGNAL(hoverEnter()), this, SLOT(sliceHoverEnter())); | |
|
97 | connect(s, SIGNAL(hoverLeave()), this, SLOT(sliceHoverLeave())); | |
|
98 | } | |
|
99 | ||
|
100 | emit changed(changeSet); | |
|
70 | 101 | } |
|
71 | 102 | |
|
72 |
|
|
|
103 | void QPieSeries::add(QPieSlice* slice) | |
|
73 | 104 | { |
|
74 |
|
|
|
105 | add(QList<QPieSlice*>() << slice); | |
|
75 | 106 | } |
|
76 | 107 | |
|
77 | bool QPieSeries::update(const QPieSlice& slice) | |
|
108 | QPieSlice* QPieSeries::add(qreal value, QString name) | |
|
78 | 109 | { |
|
79 | if (!m_slices.contains(slice.id())) | |
|
80 | return false; // series does not contain this slice | |
|
110 | QPieSlice* slice = new QPieSlice(value, name); | |
|
111 | add(slice); | |
|
112 | return slice; | |
|
113 | } | |
|
81 | 114 | |
|
82 | m_slices[slice.id()] = slice; | |
|
115 | void QPieSeries::remove(QPieSlice* slice) | |
|
116 | { | |
|
117 | if (!m_slices.removeOne(slice)) { | |
|
118 | Q_ASSERT(0); // TODO: remove before release | |
|
119 | return; | |
|
120 | } | |
|
83 | 121 | |
|
84 | 122 | ChangeSet changeSet; |
|
85 |
changeSet. |
|
|
86 | updateDerivativeData(); | |
|
123 | changeSet.appendRemoved(slice); | |
|
87 | 124 | emit changed(changeSet); |
|
88 | 125 | |
|
89 | return true; | |
|
126 | delete slice; | |
|
127 | slice = NULL; | |
|
128 | ||
|
129 | updateDerivativeData(); | |
|
90 | 130 | } |
|
91 | 131 | |
|
92 | bool QPieSeries::remove(QPieSliceId id) | |
|
132 | void QPieSeries::clear() | |
|
93 | 133 | { |
|
94 |
if ( |
|
|
95 | return false; // series does not contain this slice | |
|
96 | ||
|
97 | m_slices.remove(id); | |
|
134 | if (m_slices.count() == 0) | |
|
135 | return; | |
|
98 | 136 | |
|
99 | 137 | ChangeSet changeSet; |
|
100 | changeSet.m_removed << id; | |
|
101 | updateDerivativeData(); | |
|
138 | foreach (QPieSlice* s, m_slices) { | |
|
139 | changeSet.appendRemoved(s); | |
|
140 | m_slices.removeOne(s); | |
|
141 | delete s; | |
|
142 | } | |
|
102 | 143 | emit changed(changeSet); |
|
103 | ||
|
104 | return true; | |
|
105 | } | |
|
106 | ||
|
107 | QPieSlice QPieSeries::slice(QPieSliceId id) const | |
|
108 | { | |
|
109 | return m_slices.value(id); | |
|
144 | updateDerivativeData(); | |
|
110 | 145 | } |
|
111 | 146 | |
|
112 | 147 | void QPieSeries::setSizeFactor(qreal factor) |
|
113 | 148 | { |
|
114 | 149 | if (factor < 0.0) |
|
115 | 150 | return; |
|
116 | 151 | |
|
117 | 152 | if (m_sizeFactor != factor) { |
|
118 | 153 | m_sizeFactor = factor; |
|
119 | 154 | emit sizeFactorChanged(); |
|
120 | 155 | } |
|
121 | 156 | } |
|
122 | 157 | |
|
123 | 158 | void QPieSeries::setPosition(PiePosition position) |
|
124 | 159 | { |
|
125 | 160 | if (m_position != position) { |
|
126 | 161 | m_position = position; |
|
127 | 162 | emit positionChanged(); |
|
128 | 163 | } |
|
129 | 164 | } |
|
130 | 165 | |
|
131 | QPieSliceId QPieSeries::generateSliceId() | |
|
166 | void QPieSeries::setSpan(qreal startAngle, qreal span) | |
|
167 | { | |
|
168 | if (startAngle >= 0 && startAngle < 360 && | |
|
169 | span > 0 && span <= 360) { | |
|
170 | m_pieStartAngle = startAngle; | |
|
171 | m_pieSpan = span; | |
|
172 | updateDerivativeData(); | |
|
173 | } | |
|
174 | } | |
|
175 | ||
|
176 | void QPieSeries::setLabelsVisible(bool visible) | |
|
177 | { | |
|
178 | foreach (QPieSlice* s, m_slices) | |
|
179 | s->setLabelVisible(visible); | |
|
180 | } | |
|
181 | ||
|
182 | void QPieSeries::enableClickExplodes(bool enable) | |
|
183 | { | |
|
184 | if (enable) | |
|
185 | connect(this, SIGNAL(clicked(QPieSlice*)), this, SLOT(toggleExploded(QPieSlice*))); | |
|
186 | else | |
|
187 | disconnect(this, SLOT(toggleExploded(QPieSlice*))); | |
|
188 | } | |
|
189 | ||
|
190 | void QPieSeries::enableHoverHighlight(bool enable) | |
|
191 | { | |
|
192 | if (enable) { | |
|
193 | connect(this, SIGNAL(hoverEnter(QPieSlice*)), this, SLOT(highlightOn(QPieSlice*))); | |
|
194 | connect(this, SIGNAL(hoverLeave(QPieSlice*)), this, SLOT(highlightOff(QPieSlice*))); | |
|
195 | } else { | |
|
196 | disconnect(this, SLOT(hoverEnter(QPieSlice*))); | |
|
197 | disconnect(this, SLOT(hoverLeave(QPieSlice*))); | |
|
198 | } | |
|
199 | } | |
|
200 | ||
|
201 | void QPieSeries::sliceChanged() | |
|
202 | { | |
|
203 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | |
|
204 | Q_ASSERT(m_slices.contains(slice)); | |
|
205 | ||
|
206 | ChangeSet changeSet; | |
|
207 | changeSet.appendChanged(slice); | |
|
208 | emit changed(changeSet); | |
|
209 | ||
|
210 | updateDerivativeData(); | |
|
211 | } | |
|
212 | ||
|
213 | void QPieSeries::sliceClicked() | |
|
214 | { | |
|
215 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | |
|
216 | Q_ASSERT(m_slices.contains(slice)); | |
|
217 | emit clicked(slice); | |
|
218 | } | |
|
219 | ||
|
220 | void QPieSeries::sliceHoverEnter() | |
|
221 | { | |
|
222 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | |
|
223 | Q_ASSERT(m_slices.contains(slice)); | |
|
224 | emit hoverEnter(slice); | |
|
225 | } | |
|
226 | ||
|
227 | void QPieSeries::sliceHoverLeave() | |
|
132 | 228 | { |
|
133 | // Id is quint64 so it should be enough for us. | |
|
134 | // Note that id is not unique between pie series. | |
|
135 | return m_sliceIdSeed++; | |
|
229 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); | |
|
230 | Q_ASSERT(m_slices.contains(slice)); | |
|
231 | emit hoverLeave(slice); | |
|
232 | } | |
|
233 | ||
|
234 | void QPieSeries::toggleExploded(QPieSlice* slice) | |
|
235 | { | |
|
236 | Q_ASSERT(slice); | |
|
237 | slice->setExploded(!slice->isExploded()); | |
|
238 | } | |
|
239 | ||
|
240 | void QPieSeries::highlightOn(QPieSlice* slice) | |
|
241 | { | |
|
242 | Q_ASSERT(slice); | |
|
243 | QColor c = slice->brush().color().lighter(); | |
|
244 | slice->setBrush(c); | |
|
245 | } | |
|
246 | ||
|
247 | void QPieSeries::highlightOff(QPieSlice* slice) | |
|
248 | { | |
|
249 | Q_ASSERT(slice); | |
|
250 | QColor c = slice->brush().color().darker(150); | |
|
251 | slice->setBrush(c); | |
|
136 | 252 | } |
|
137 | 253 | |
|
138 | 254 | void QPieSeries::updateDerivativeData() |
|
139 | 255 | { |
|
140 | 256 | m_total = 0; |
|
141 | foreach (const QPieSlice& s, m_slices.values()) | |
|
142 | m_total += s.value(); | |
|
143 | 257 | |
|
144 | Q_ASSERT(m_total > 0); // TODO: remove this before release | |
|
258 | // nothing to do? | |
|
259 | if (m_slices.count() == 0) | |
|
260 | return; | |
|
261 | ||
|
262 | // calculate total | |
|
263 | foreach (QPieSlice* s, m_slices) | |
|
264 | m_total += s->value(); | |
|
265 | ||
|
266 | // we must have some values | |
|
267 | Q_ASSERT(m_total > 0); // TODO | |
|
145 | 268 | |
|
146 | foreach (QPieSliceId id, m_slices.keys()) | |
|
147 | m_slices[id].m_percentage = m_slices.value(id).value() / m_total; | |
|
269 | // update slice attributes | |
|
270 | qreal sliceAngle = m_pieStartAngle; | |
|
271 | foreach (QPieSlice* s, m_slices) { | |
|
272 | ||
|
273 | bool changed = false; | |
|
274 | ||
|
275 | qreal percentage = s->value() / m_total; | |
|
276 | if (s->m_percentage != percentage) { | |
|
277 | s->m_percentage = percentage; | |
|
278 | changed = true; | |
|
279 | } | |
|
280 | ||
|
281 | qreal sliceSpan = m_pieSpan * percentage; | |
|
282 | if (s->m_span != sliceSpan) { | |
|
283 | s->m_span = sliceSpan; | |
|
284 | changed = true; | |
|
285 | } | |
|
286 | ||
|
287 | if (s->m_angle != sliceAngle) { | |
|
288 | s->m_angle = sliceAngle; | |
|
289 | changed = true; | |
|
290 | } | |
|
291 | sliceAngle += sliceSpan; | |
|
292 | ||
|
293 | if (changed) | |
|
294 | emit s->changed(); | |
|
295 | } | |
|
148 | 296 | } |
|
149 | 297 | |
|
150 | 298 | #include "moc_qpieseries.cpp" |
|
151 | 299 | |
|
152 | 300 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,163 +1,127 | |||
|
1 | 1 | #ifndef PIESERIES_H |
|
2 | 2 | #define PIESERIES_H |
|
3 | 3 | |
|
4 | 4 | #include "qchartseries.h" |
|
5 | 5 | #include <QObject> |
|
6 | 6 | #include <QRectF> |
|
7 | 7 | #include <QColor> |
|
8 | 8 | #include <QPen> |
|
9 | 9 | #include <QBrush> |
|
10 | #include <QSignalMapper> | |
|
10 | 11 | |
|
11 | 12 | class QGraphicsObject; |
|
12 | 13 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
13 | 14 | class PiePresenter; |
|
14 | 15 | class PieSlice; |
|
15 | ||
|
16 | typedef quint64 QPieSliceId; | |
|
17 | ||
|
18 | class QPieSlice | |
|
19 | { | |
|
20 | public: | |
|
21 | QPieSlice() | |
|
22 | :m_id(-1), m_value(0), m_isLabelVisible(true), m_isExploded(false), m_percentage(0) {} | |
|
23 | ||
|
24 | QPieSlice(qreal value, QString label = QString(), bool labelVisible = true, bool exploded = false, QPen pen = QPen(), QBrush brush = QBrush()) | |
|
25 | :m_id(-1), m_value(value), m_label(label), m_isLabelVisible(labelVisible), m_isExploded(exploded), m_pen(pen), m_brush(brush), m_percentage(0) {} | |
|
26 | ||
|
27 | QPieSliceId id() const { return m_id; } | |
|
28 | ||
|
29 | void setValue(qreal value) { m_value = value; } | |
|
30 | qreal value() const { return m_value; } | |
|
31 | ||
|
32 | void setLabel(QString label) { m_label = label; } | |
|
33 | QString label() const { return m_label; } | |
|
34 | ||
|
35 | void setLabelVisible(bool visible) { m_isLabelVisible = visible; } | |
|
36 | bool isLabelVisible() const { return m_isLabelVisible; } | |
|
37 | ||
|
38 | // TODO: | |
|
39 | //void setLabelPen(QPen pen) {}; | |
|
40 | //QPen labelPen() const {}; | |
|
41 | //void setLabelFont(QFont font); | |
|
42 | //QFont labelFont() const; | |
|
43 | //void setLabelArmLenght(qreal len) {}; | |
|
44 | //qreal labelArmLenght() const {}; | |
|
45 | ||
|
46 | void setExploded(bool exploded) { m_isExploded = exploded; } | |
|
47 | bool isExploded() const { return m_isExploded; } | |
|
48 | ||
|
49 | void setPen(QPen pen) { m_pen = pen; } | |
|
50 | QPen pen() const { return m_pen; } | |
|
51 | ||
|
52 | void setBrush(QBrush brush) { m_brush = brush; } | |
|
53 | QBrush brush() const { return m_brush; } | |
|
54 | ||
|
55 | qreal percentage() const { return m_percentage; } | |
|
56 | ||
|
57 | private: | |
|
58 | ||
|
59 | // TODO: use private class | |
|
60 | friend class QPieSeries; | |
|
61 | ||
|
62 | QPieSliceId m_id; | |
|
63 | qreal m_value; | |
|
64 | QString m_label; | |
|
65 | bool m_isLabelVisible; | |
|
66 | bool m_isExploded; | |
|
67 | ||
|
68 | QPen m_pen; | |
|
69 | QBrush m_brush; | |
|
70 | ||
|
71 | qreal m_percentage; // generated content | |
|
72 | }; | |
|
16 | class QPieSlice; | |
|
73 | 17 | |
|
74 | 18 | class QTCOMMERCIALCHART_EXPORT QPieSeries : public QChartSeries |
|
75 | 19 | { |
|
76 | 20 | Q_OBJECT |
|
77 | 21 | |
|
78 | 22 | public: |
|
79 | 23 | enum PiePosition { |
|
80 | 24 | PiePositionMaximized = 0, |
|
81 | 25 | PiePositionTopLeft, |
|
82 | 26 | PiePositionTopRight, |
|
83 | 27 | PiePositionBottomLeft, |
|
84 | 28 | PiePositionBottomRight |
|
85 | 29 | }; |
|
86 | 30 | |
|
87 | 31 | class ChangeSet |
|
88 | 32 | { |
|
89 | 33 | public: |
|
90 | QList<QPieSliceId> m_added; | |
|
91 | QList<QPieSliceId> m_removed; | |
|
92 | QList<QPieSliceId> m_changed; | |
|
34 | void appendAdded(QPieSlice* slice); | |
|
35 | void appendChanged(QPieSlice* slice); | |
|
36 | void appendRemoved(QPieSlice* slice); | |
|
37 | ||
|
38 | QList<QPieSlice*> added() const; | |
|
39 | QList<QPieSlice*> changed() const; | |
|
40 | QList<QPieSlice*> removed() const; | |
|
41 | ||
|
42 | bool isEmpty() const; | |
|
43 | ||
|
44 | private: | |
|
45 | QList<QPieSlice*> m_added; | |
|
46 | QList<QPieSlice*> m_changed; | |
|
47 | QList<QPieSlice*> m_removed; | |
|
93 | 48 | }; |
|
94 | 49 | |
|
95 | 50 | public: |
|
96 | 51 | QPieSeries(QObject *parent = 0); |
|
97 | ~QPieSeries(); | |
|
52 | virtual ~QPieSeries(); | |
|
98 | 53 | |
|
99 | 54 | public: // from QChartSeries |
|
100 | 55 | QChartSeriesType type() const { return QChartSeries::SeriesTypePie; } |
|
101 | 56 | virtual bool setData(QList<qreal> data); // TODO: remove this |
|
102 | 57 | |
|
103 | 58 | public: |
|
104 | // TODO: should we return id/bool or what? | |
|
105 | // TODO: should we prefer passing a modifiable reference? | |
|
106 |
|
|
|
107 | bool add(const QList<QPieSlice>& slices); | |
|
108 |
|
|
|
109 | bool update(const QPieSlice& slice); | |
|
110 | bool remove(QPieSliceId id); | |
|
59 | void set(QList<QPieSlice*> slices); | |
|
60 | void add(QList<QPieSlice*> slices); | |
|
61 | void add(QPieSlice* slice); | |
|
62 | QPieSlice* add(qreal value, QString name); | |
|
63 | void remove(QPieSlice* slice); | |
|
64 | void clear(); | |
|
111 | 65 | |
|
112 | 66 | int count() const { return m_slices.count(); } |
|
113 | 67 | |
|
114 |
QList<QPieSlice> slices() const { return m_slices |
|
|
115 | QList<QPieSliceId> ids() const { return m_slices.keys(); } | |
|
116 | QPieSlice slice(QPieSliceId id) const; | |
|
68 | QList<QPieSlice*> slices() const { return m_slices; } | |
|
117 | 69 | |
|
118 |
// TODO: |
|
|
70 | // TODO: find slices? | |
|
71 | // QList<QPieSlice*> findByValue(qreal value); | |
|
72 | // ... | |
|
119 | 73 | |
|
120 |
// TODO: |
|
|
121 |
//void s |
|
|
122 | //void setLabel(QPieSliceId id, QString label); | |
|
123 | //void setPen(QPieSliceId id, QPen pen); | |
|
124 | //void setBrush(QPieSliceId id, QBrush brush); | |
|
125 | //void setExploded(QPieSliceId id, bool exploded); | |
|
74 | // TODO: sorting slices? | |
|
75 | // void sort(QPieSeries::SortByValue) | |
|
126 | 76 | |
|
127 | 77 | void setSizeFactor(qreal sizeFactor); |
|
128 | 78 | qreal sizeFactor() const { return m_sizeFactor; } |
|
129 | 79 | |
|
130 | 80 | void setPosition(PiePosition position); |
|
131 | 81 | PiePosition position() const { return m_position; } |
|
132 | 82 | |
|
83 | void setSpan(qreal startAngle, qreal span); | |
|
84 | ||
|
85 | void setLabelsVisible(bool visible); | |
|
86 | void enableClickExplodes(bool enable); | |
|
87 | void enableHoverHighlight(bool enable); | |
|
88 | ||
|
133 | 89 | Q_SIGNALS: |
|
134 | 90 | void changed(const QPieSeries::ChangeSet& changeSet); |
|
91 | void clicked(QPieSlice* slice); | |
|
92 | void hoverEnter(QPieSlice* slice); | |
|
93 | void hoverLeave(QPieSlice* slice); | |
|
135 | 94 | void sizeFactorChanged(); |
|
136 | 95 | void positionChanged(); |
|
137 | 96 | |
|
138 | // TODO: | |
|
139 |
|
|
|
140 | // ?? void sliceHoverEnter(QPieSliceId id); | |
|
141 |
|
|
|
97 | private Q_SLOTS: // should be private and not in the interface | |
|
98 | void sliceChanged(); | |
|
99 | void sliceClicked(); | |
|
100 | void sliceHoverEnter(); | |
|
101 | void sliceHoverLeave(); | |
|
102 | ||
|
103 | void toggleExploded(QPieSlice* slice); | |
|
104 | void highlightOn(QPieSlice* slice); | |
|
105 | void highlightOff(QPieSlice* slice); | |
|
142 | 106 | |
|
143 | 107 | private: |
|
144 | QPieSliceId generateSliceId(); | |
|
145 | 108 | void updateDerivativeData(); |
|
146 | 109 | |
|
147 | 110 | private: |
|
148 | 111 | Q_DISABLE_COPY(QPieSeries) |
|
149 | 112 | |
|
150 | 113 | // TODO: use PIML |
|
151 | 114 | friend class PiePresenter; |
|
152 | 115 | friend class PieSlice; |
|
153 | 116 | |
|
154 |
Q |
|
|
117 | QList<QPieSlice*> m_slices; | |
|
155 | 118 | qreal m_sizeFactor; |
|
156 | 119 | PiePosition m_position; |
|
157 | 120 | qreal m_total; |
|
158 | QPieSliceId m_sliceIdSeed; | |
|
121 | qreal m_pieStartAngle; | |
|
122 | qreal m_pieSpan; | |
|
159 | 123 | }; |
|
160 | 124 | |
|
161 | 125 | QTCOMMERCIALCHART_END_NAMESPACE |
|
162 | 126 | |
|
163 | 127 | #endif // PIESERIES_H |
General Comments 0
You need to be logged in to leave comments.
Login now