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