@@ -1,97 +1,97 | |||
|
1 | 1 | #include "mainwindow.h" |
|
2 | 2 | #include <qchartview.h> |
|
3 | 3 | #include <qpieseries.h> |
|
4 | 4 | #include <qpieslice.h> |
|
5 | 5 | #include <qlineseries.h> |
|
6 | 6 | #include <qscatterseries.h> |
|
7 | 7 | #include <qchartaxis.h> |
|
8 | 8 | #include <QDebug> |
|
9 | 9 | |
|
10 | 10 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
11 | 11 | |
|
12 | 12 | MainWindow::MainWindow(QWidget *parent) |
|
13 | 13 | : QMainWindow(parent) |
|
14 | 14 | { |
|
15 | 15 | // Here's the set of company's colors used throughout the example |
|
16 | 16 | m_companyColor1 = "#b90020"; |
|
17 | 17 | m_companyColor2 = "#6d0013"; |
|
18 | 18 | m_companyColor3 = "#d5d5f5"; |
|
19 | 19 | m_companyColor4 = "#fcfcfc"; |
|
20 | 20 | |
|
21 | 21 | resize(400, 300); |
|
22 | 22 | setWindowFlags(Qt::FramelessWindowHint); |
|
23 | 23 | |
|
24 | 24 | // Create chart view |
|
25 | 25 | m_chartView = new QChartView(this); |
|
26 | 26 | setCentralWidget(m_chartView); |
|
27 | 27 | m_chartView->setChartTitle("Custom colors example"); |
|
28 | 28 | m_chartView->setRenderHint(QPainter::Antialiasing); |
|
29 | 29 | |
|
30 | 30 | // Create line series |
|
31 | 31 | m_line = new QLineSeries(); |
|
32 | 32 | m_line->add(0.0, 1.1); |
|
33 | 33 | m_line->add(1.0, 2.3); |
|
34 | 34 | m_line->add(2.0, 2.1); |
|
35 | 35 | m_line->add(3.0, 3.3); |
|
36 | 36 | m_chartView->addSeries(m_line); |
|
37 | 37 | |
|
38 | 38 | // Create scatter series with the same data |
|
39 | 39 | m_scatter = new QScatterSeries(); |
|
40 | 40 | m_scatter->add(m_line->data()); |
|
41 | 41 | m_chartView->addSeries(m_scatter); |
|
42 | 42 | |
|
43 | 43 | // Create pie series with different data |
|
44 | 44 | m_pie = new QPieSeries(); |
|
45 | 45 | m_pie->add(1.1, "1"); |
|
46 | 46 | m_pie->add(2.1, "2"); |
|
47 | 47 | m_pie->add(3.0, "3"); |
|
48 |
m_pie->setPosition |
|
|
49 |
m_pie->setSize |
|
|
48 | m_pie->setPiePosition(0.7, 0.7); | |
|
49 | m_pie->setPieSize(0.5); | |
|
50 | 50 | m_chartView->addSeries(m_pie); |
|
51 | 51 | |
|
52 | 52 | connect(&m_timer, SIGNAL(timeout()), this, SLOT(customize())); |
|
53 | 53 | m_timer.setInterval(1500); |
|
54 | 54 | m_timer.setSingleShot(false); |
|
55 | 55 | m_timer.start(); |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | MainWindow::~MainWindow() |
|
59 | 59 | { |
|
60 | 60 | } |
|
61 | 61 | |
|
62 | 62 | void MainWindow::customize() |
|
63 | 63 | { |
|
64 | 64 | // Customize chart background |
|
65 | 65 | // Use a gradient from color 3 to color 4 for chart background |
|
66 | 66 | QLinearGradient chartGradient(0, 0, 0, 300); |
|
67 | 67 | chartGradient.setColorAt(0.0, m_companyColor3); |
|
68 | 68 | chartGradient.setColorAt(0.5, m_companyColor4); |
|
69 | 69 | chartGradient.setColorAt(1.0, m_companyColor3); |
|
70 | 70 | m_chartView->setChartBackgroundBrush(chartGradient); |
|
71 | 71 | m_chartView->setBackgroundBrush(m_companyColor4); |
|
72 | 72 | m_chartView->setChartTitleBrush(m_companyColor1); |
|
73 | 73 | |
|
74 | 74 | // Customize chart axis |
|
75 | 75 | QPen color1Pen(m_companyColor1, 4.0); |
|
76 | 76 | m_chartView->axisX()->setAxisPen(color1Pen); |
|
77 | 77 | m_chartView->axisY()->setAxisPen(color1Pen); |
|
78 | 78 | |
|
79 | 79 | // Customize series |
|
80 | 80 | m_line->setPen(color1Pen); |
|
81 | 81 | m_scatter->setPen(color1Pen); |
|
82 | 82 | m_scatter->setBrush(m_companyColor3); |
|
83 | 83 | for (int i(0); i < m_pie->slices().count(); i++) { |
|
84 | 84 | Qt::BrushStyle style = static_cast<Qt::BrushStyle>(i + 1); |
|
85 | 85 | m_pie->slices().at(i)->setSliceBrush(QBrush(m_companyColor2, style)); |
|
86 | 86 | m_pie->slices().at(i)->setSlicePen(color1Pen); |
|
87 | 87 | } |
|
88 | 88 | |
|
89 | 89 | |
|
90 | 90 | // Calculate new colors to be used on the next update for the series |
|
91 | 91 | m_companyColor1.setRed((m_companyColor1.red() + 25) % 255); |
|
92 | 92 | m_companyColor1.setGreen((m_companyColor1.green() + 25) % 255); |
|
93 | 93 | m_companyColor1.setBlue((m_companyColor1.blue() + 25) % 255); |
|
94 | 94 | m_companyColor2.setRed((m_companyColor2.red() + 25) % 255); |
|
95 | 95 | m_companyColor2.setGreen((m_companyColor2.green() + 25) % 255); |
|
96 | 96 | m_companyColor2.setBlue((m_companyColor2.blue() + 25) % 255); |
|
97 | 97 | } |
@@ -1,236 +1,234 | |||
|
1 | 1 | #include <QtGui/QApplication> |
|
2 | 2 | #include <QMainWindow> |
|
3 | 3 | #include <qchartglobal.h> |
|
4 | 4 | #include <qchartview.h> |
|
5 | 5 | #include <qpieseries.h> |
|
6 | 6 | #include <qpieslice.h> |
|
7 | 7 | #include <QGridLayout> |
|
8 | 8 | #include <QFormLayout> |
|
9 | 9 | #include <QComboBox> |
|
10 | 10 | #include <QSpinBox> |
|
11 | 11 | #include <QCheckBox> |
|
12 | 12 | #include <QGroupBox> |
|
13 | 13 | #include <QLabel> |
|
14 | 14 | |
|
15 | 15 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
16 | 16 | |
|
17 | 17 | class CustomSlice : public QPieSlice |
|
18 | 18 | { |
|
19 | 19 | Q_OBJECT |
|
20 | 20 | public: |
|
21 | 21 | CustomSlice(qreal value, QString label) |
|
22 | 22 | :QPieSlice(value, label) |
|
23 | 23 | { |
|
24 | 24 | connect(this, SIGNAL(hoverEnter()), this, SLOT(handleHoverEnter())); |
|
25 | 25 | connect(this, SIGNAL(hoverLeave()), this, SLOT(handleHoverLeave())); |
|
26 | 26 | } |
|
27 | 27 | |
|
28 | 28 | public Q_SLOTS: |
|
29 | 29 | |
|
30 | 30 | void handleHoverEnter() |
|
31 | 31 | { |
|
32 | 32 | QBrush brush = this->sliceBrush(); |
|
33 | 33 | m_originalBrush = brush; |
|
34 | 34 | brush.setColor(brush.color().lighter()); |
|
35 | 35 | setSliceBrush(brush); |
|
36 | 36 | } |
|
37 | 37 | |
|
38 | 38 | void handleHoverLeave() |
|
39 | 39 | { |
|
40 | 40 | setSliceBrush(m_originalBrush); |
|
41 | 41 | } |
|
42 | 42 | |
|
43 | 43 | private: |
|
44 | 44 | QBrush m_originalBrush; |
|
45 | 45 | }; |
|
46 | 46 | |
|
47 | 47 | class MainWidget : public QWidget |
|
48 | 48 | { |
|
49 | 49 | Q_OBJECT |
|
50 | 50 | |
|
51 | 51 | public: |
|
52 | 52 | explicit MainWidget(QWidget* parent = 0) |
|
53 | 53 | :QWidget(parent), |
|
54 | 54 | m_slice(0) |
|
55 | 55 | { |
|
56 | 56 | m_chartView = new QChartView(); |
|
57 | 57 | m_chartView->setChartTitle("Piechart customization"); |
|
58 | 58 | m_chartView->setRenderHint(QPainter::Antialiasing); |
|
59 | 59 | m_chartView->setChartTheme(QChart::ChartThemeIcy); |
|
60 | 60 | |
|
61 | 61 | m_series = new QPieSeries(); |
|
62 | 62 | *m_series << new CustomSlice(10.0, "Slice 1"); |
|
63 | 63 | *m_series << new CustomSlice(20.0, "Slice 2"); |
|
64 | 64 | *m_series << new CustomSlice(30.0, "Slice 3"); |
|
65 | 65 | *m_series << new CustomSlice(40.0, "Slice 4"); |
|
66 | 66 | *m_series << new CustomSlice(50.0, "Slice 5"); |
|
67 | 67 | m_chartView->addSeries(m_series); |
|
68 | 68 | |
|
69 | 69 | m_hPosition = new QDoubleSpinBox(); |
|
70 | 70 | m_hPosition->setMinimum(0.0); |
|
71 | 71 | m_hPosition->setMaximum(1.0); |
|
72 | 72 | m_hPosition->setSingleStep(0.1); |
|
73 |
m_hPosition->setValue(m_series-> |
|
|
73 | m_hPosition->setValue(m_series->pieHorizontalPosition()); | |
|
74 | 74 | |
|
75 | 75 | m_vPosition = new QDoubleSpinBox(); |
|
76 | 76 | m_vPosition->setMinimum(0.0); |
|
77 | 77 | m_vPosition->setMaximum(1.0); |
|
78 | 78 | m_vPosition->setSingleStep(0.1); |
|
79 |
m_vPosition->setValue(m_series-> |
|
|
79 | m_vPosition->setValue(m_series->pieVerticalPosition()); | |
|
80 | 80 | |
|
81 | 81 | m_sizeFactor = new QDoubleSpinBox(); |
|
82 | 82 | m_sizeFactor->setMinimum(0.0); |
|
83 | 83 | m_sizeFactor->setMaximum(1.0); |
|
84 | 84 | m_sizeFactor->setSingleStep(0.1); |
|
85 |
m_sizeFactor->setValue(m_series-> |
|
|
85 | m_sizeFactor->setValue(m_series->pieSize()); | |
|
86 | 86 | |
|
87 | 87 | m_startAngle = new QDoubleSpinBox(); |
|
88 | 88 | m_startAngle->setMinimum(0.0); |
|
89 | 89 | m_startAngle->setMaximum(360); |
|
90 |
m_startAngle->setValue(m_series-> |
|
|
90 | m_startAngle->setValue(m_series->pieStartAngle()); | |
|
91 | 91 | m_startAngle->setSingleStep(1); |
|
92 | 92 | |
|
93 | 93 | m_endAngle = new QDoubleSpinBox(); |
|
94 | 94 | m_endAngle->setMinimum(0.0); |
|
95 | 95 | m_endAngle->setMaximum(360); |
|
96 | m_endAngle->setValue(m_series->endAngle()); | |
|
96 | m_endAngle->setValue(m_series->pieEndAngle()); | |
|
97 | 97 | m_endAngle->setSingleStep(1); |
|
98 | 98 | |
|
99 | 99 | QFormLayout* seriesSettingsLayout = new QFormLayout(); |
|
100 | 100 | seriesSettingsLayout->addRow("Horizontal position", m_hPosition); |
|
101 | 101 | seriesSettingsLayout->addRow("Vertical position", m_vPosition); |
|
102 | 102 | seriesSettingsLayout->addRow("Size factor", m_sizeFactor); |
|
103 | 103 | seriesSettingsLayout->addRow("Start angle", m_startAngle); |
|
104 | 104 | seriesSettingsLayout->addRow("End angle", m_endAngle); |
|
105 | 105 | QGroupBox* seriesSettings = new QGroupBox("Series"); |
|
106 | 106 | seriesSettings->setLayout(seriesSettingsLayout); |
|
107 | 107 | |
|
108 | 108 | m_sliceName = new QLabel("<click a slice>"); |
|
109 | 109 | m_sliceValue = new QDoubleSpinBox(); |
|
110 | 110 | m_sliceValue->setMaximum(1000); |
|
111 | 111 | m_sliceLabelVisible = new QCheckBox(); |
|
112 | 112 | m_sliceLabelArmFactor = new QDoubleSpinBox(); |
|
113 | 113 | m_sliceLabelArmFactor->setSingleStep(0.01); |
|
114 | 114 | m_sliceExploded = new QCheckBox(); |
|
115 | 115 | m_sliceExplodedFactor = new QDoubleSpinBox(); |
|
116 | 116 | m_sliceExplodedFactor->setSingleStep(0.01); |
|
117 | 117 | |
|
118 | 118 | QFormLayout* sliceSettingsLayout = new QFormLayout(); |
|
119 | 119 | sliceSettingsLayout->addRow("Selected", m_sliceName); |
|
120 | 120 | sliceSettingsLayout->addRow("Value", m_sliceValue); |
|
121 | 121 | sliceSettingsLayout->addRow("Label visible", m_sliceLabelVisible); |
|
122 | 122 | sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor); |
|
123 | 123 | sliceSettingsLayout->addRow("Exploded", m_sliceExploded); |
|
124 | 124 | sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor); |
|
125 | 125 | |
|
126 | 126 | QGroupBox* sliceSettings = new QGroupBox("Slice"); |
|
127 | 127 | sliceSettings->setLayout(sliceSettingsLayout); |
|
128 | 128 | |
|
129 | 129 | QGridLayout* baseLayout = new QGridLayout(); |
|
130 | 130 | baseLayout->addWidget(seriesSettings, 0, 0); |
|
131 | 131 | baseLayout->addWidget(sliceSettings, 1, 0); |
|
132 | 132 | baseLayout->addWidget(m_chartView, 0, 1, 2, 1); |
|
133 | 133 | setLayout(baseLayout); |
|
134 | 134 | |
|
135 | 135 | connect(m_vPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); |
|
136 | 136 | connect(m_hPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); |
|
137 | 137 | connect(m_sizeFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); |
|
138 | 138 | connect(m_startAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); |
|
139 | 139 | connect(m_endAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings())); |
|
140 | 140 | |
|
141 | 141 | connect(m_sliceValue, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings())); |
|
142 | 142 | connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings())); |
|
143 | 143 | connect(m_sliceLabelArmFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings())); |
|
144 | 144 | connect(m_sliceExploded, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings())); |
|
145 | 145 | connect(m_sliceExplodedFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings())); |
|
146 | 146 | |
|
147 | 147 | connect(m_series, SIGNAL(clicked(QPieSlice*)), this, SLOT(handleSliceClicked(QPieSlice*))); |
|
148 | 148 | |
|
149 | 149 | updateSerieSettings(); |
|
150 | 150 | } |
|
151 | 151 | |
|
152 | 152 | public Q_SLOTS: |
|
153 | 153 | |
|
154 | 154 | void updateSerieSettings() |
|
155 | 155 | { |
|
156 |
m_series->setPosition |
|
|
157 | ||
|
158 |
m_series->set |
|
|
159 | ||
|
160 | m_series->setStartAngle(m_startAngle->value()); | |
|
161 | m_series->setEndAngle(m_endAngle->value()); | |
|
156 | m_series->setPiePosition(m_vPosition->value(), m_hPosition->value()); | |
|
157 | m_series->setPieSize(m_sizeFactor->value()); | |
|
158 | m_series->setPieStartAngle(m_startAngle->value()); | |
|
159 | m_series->setPieEndAngle(m_endAngle->value()); | |
|
162 | 160 | } |
|
163 | 161 | |
|
164 | 162 | void updateSliceSettings() |
|
165 | 163 | { |
|
166 | 164 | if (!m_slice) |
|
167 | 165 | return; |
|
168 | 166 | |
|
169 | 167 | m_slice->setValue(m_sliceValue->value()); |
|
170 | 168 | m_slice->setLabelVisible(m_sliceLabelVisible->isChecked()); |
|
171 | 169 | m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value()); |
|
172 | 170 | m_slice->setExploded(m_sliceExploded->isChecked()); |
|
173 | 171 | m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value()); |
|
174 | 172 | } |
|
175 | 173 | |
|
176 | 174 | void handleSliceClicked(QPieSlice* slice) |
|
177 | 175 | { |
|
178 | 176 | m_slice = slice; |
|
179 | 177 | m_sliceName->setText(slice->label()); |
|
180 | 178 | |
|
181 | 179 | m_sliceValue->blockSignals(true); |
|
182 | 180 | m_sliceValue->setValue(slice->value()); |
|
183 | 181 | m_sliceValue->blockSignals(false); |
|
184 | 182 | |
|
185 | 183 | m_sliceLabelVisible->blockSignals(true); |
|
186 | 184 | m_sliceLabelVisible->setChecked(slice->isLabelVisible()); |
|
187 | 185 | m_sliceLabelVisible->blockSignals(false); |
|
188 | 186 | |
|
189 | 187 | m_sliceLabelArmFactor->blockSignals(true); |
|
190 | 188 | m_sliceLabelArmFactor->setValue(slice->labelArmLengthFactor()); |
|
191 | 189 | m_sliceLabelArmFactor->blockSignals(false); |
|
192 | 190 | |
|
193 | 191 | m_sliceExploded->blockSignals(true); |
|
194 | 192 | m_sliceExploded->setChecked(slice->isExploded()); |
|
195 | 193 | m_sliceExploded->blockSignals(false); |
|
196 | 194 | |
|
197 | 195 | m_sliceExplodedFactor->blockSignals(true); |
|
198 | 196 | m_sliceExplodedFactor->setValue(slice->explodeDistanceFactor()); |
|
199 | 197 | m_sliceExplodedFactor->blockSignals(false); |
|
200 | 198 | } |
|
201 | 199 | |
|
202 | 200 | private: |
|
203 | 201 | QChartView* m_chartView; |
|
204 | 202 | QPieSeries* m_series; |
|
205 | 203 | QPieSlice* m_slice; |
|
206 | 204 | |
|
207 | 205 | QDoubleSpinBox* m_hPosition; |
|
208 | 206 | QDoubleSpinBox* m_vPosition; |
|
209 | 207 | QDoubleSpinBox* m_sizeFactor; |
|
210 | 208 | QDoubleSpinBox* m_startAngle; |
|
211 | 209 | QDoubleSpinBox* m_endAngle; |
|
212 | 210 | |
|
213 | 211 | QLabel* m_sliceName; |
|
214 | 212 | QDoubleSpinBox* m_sliceValue; |
|
215 | 213 | QCheckBox* m_sliceLabelVisible; |
|
216 | 214 | QDoubleSpinBox* m_sliceLabelArmFactor; |
|
217 | 215 | QCheckBox* m_sliceExploded; |
|
218 | 216 | QDoubleSpinBox* m_sliceExplodedFactor; |
|
219 | 217 | }; |
|
220 | 218 | |
|
221 | 219 | int main(int argc, char *argv[]) |
|
222 | 220 | { |
|
223 | 221 | QApplication a(argc, argv); |
|
224 | 222 | |
|
225 | 223 | QMainWindow window; |
|
226 | 224 | |
|
227 | 225 | MainWidget* widget = new MainWidget(); |
|
228 | 226 | |
|
229 | 227 | window.setCentralWidget(widget); |
|
230 | 228 | window.resize(900, 600); |
|
231 | 229 | window.show(); |
|
232 | 230 | |
|
233 | 231 | return a.exec(); |
|
234 | 232 | } |
|
235 | 233 | |
|
236 | 234 | #include "main.moc" |
@@ -1,158 +1,158 | |||
|
1 | 1 | #include "piepresenter_p.h" |
|
2 | 2 | #include "pieslice_p.h" |
|
3 | 3 | #include "qpieslice.h" |
|
4 | 4 | #include "qpieseries.h" |
|
5 | 5 | #include "chartpresenter_p.h" |
|
6 | 6 | #include <QDebug> |
|
7 | 7 | #include <QPainter> |
|
8 | 8 | |
|
9 | 9 | |
|
10 | 10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
11 | 11 | |
|
12 | 12 | PiePresenter::PiePresenter(QGraphicsItem *parent, QPieSeries *series) |
|
13 | 13 | :ChartItem(parent), |
|
14 | 14 | m_series(series) |
|
15 | 15 | { |
|
16 | 16 | Q_ASSERT(series); |
|
17 | 17 | connect(series, SIGNAL(changed(const QPieSeries::ChangeSet&)), this, SLOT(handleSeriesChanged(const QPieSeries::ChangeSet&))); |
|
18 |
connect(series, SIGNAL( |
|
|
19 |
connect(series, SIGNAL(p |
|
|
18 | connect(series, SIGNAL(piePositionChanged()), this, SLOT(updateGeometry())); | |
|
19 | connect(series, SIGNAL(pieSizeChanged()), this, SLOT(updateGeometry())); | |
|
20 | 20 | |
|
21 | 21 | if (m_series->count()) { |
|
22 | 22 | QPieSeries::ChangeSet changeSet; |
|
23 | 23 | changeSet.appendAdded(m_series->m_slices); |
|
24 | 24 | handleSeriesChanged(changeSet); |
|
25 | 25 | } |
|
26 | 26 | |
|
27 | 27 | // Note: the following does not affect as long as the item does not have anything to paint |
|
28 | 28 | setZValue(ChartPresenter::PieSeriesZValue); |
|
29 | 29 | } |
|
30 | 30 | |
|
31 | 31 | PiePresenter::~PiePresenter() |
|
32 | 32 | { |
|
33 | 33 | // slices deleted automatically through QGraphicsItem |
|
34 | 34 | } |
|
35 | 35 | |
|
36 | 36 | void PiePresenter::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) |
|
37 | 37 | { |
|
38 | 38 | // TODO: paint shadows for all components |
|
39 | 39 | // - get paths from items & merge & offset and draw with shadow color? |
|
40 | 40 | //painter->setBrush(QBrush(Qt::red)); |
|
41 | 41 | //painter->drawRect(m_debugRect); |
|
42 | 42 | } |
|
43 | 43 | |
|
44 | 44 | void PiePresenter::handleSeriesChanged(const QPieSeries::ChangeSet& changeSet) |
|
45 | 45 | { |
|
46 | 46 | //qDebug() << "PiePresenter::handleSeriesChanged()"; |
|
47 | 47 | //qDebug() << " added : " << changeSet.added(); |
|
48 | 48 | //qDebug() << " changed: " << changeSet.changed(); |
|
49 | 49 | //qDebug() << " removed: " << changeSet.removed(); |
|
50 | 50 | |
|
51 | 51 | foreach (QPieSlice* s, changeSet.added()) |
|
52 | 52 | addSlice(s); |
|
53 | 53 | |
|
54 | 54 | foreach (QPieSlice* s, changeSet.changed()) |
|
55 | 55 | updateSlice(s); |
|
56 | 56 | |
|
57 | 57 | foreach (QPieSlice* s, changeSet.removed()) |
|
58 | 58 | deleteSlice(s); |
|
59 | 59 | |
|
60 | 60 | // every change possibly changes the actual pie size |
|
61 | 61 | updateGeometry(); |
|
62 | 62 | } |
|
63 | 63 | |
|
64 | 64 | void PiePresenter::handleDomainChanged(const Domain& domain) |
|
65 | 65 | { |
|
66 | 66 | // TODO |
|
67 | 67 | } |
|
68 | 68 | |
|
69 | 69 | void PiePresenter::handleGeometryChanged(const QRectF& rect) |
|
70 | 70 | { |
|
71 | 71 | m_rect = rect; |
|
72 | 72 | prepareGeometryChange(); |
|
73 | 73 | updateGeometry(); |
|
74 | 74 | } |
|
75 | 75 | |
|
76 | 76 | void PiePresenter::updateGeometry() |
|
77 | 77 | { |
|
78 | 78 | if (!m_rect.isValid() || m_rect.isEmpty()) |
|
79 | 79 | return; |
|
80 | 80 | |
|
81 | 81 | // find pie center coordinates |
|
82 | 82 | QPointF center; |
|
83 |
center.setX(m_rect.left() + (m_rect.width() * m_series-> |
|
|
84 |
center.setY(m_rect.top() + (m_rect.height() * m_series-> |
|
|
83 | center.setX(m_rect.left() + (m_rect.width() * m_series->pieHorizontalPosition())); | |
|
84 | center.setY(m_rect.top() + (m_rect.height() * m_series->pieVerticalPosition())); | |
|
85 | 85 | |
|
86 | 86 | // find maximum radius for pie |
|
87 | 87 | qreal radius = m_rect.height() / 2; |
|
88 | 88 | if (m_rect.width() < m_rect.height()) |
|
89 | 89 | radius = m_rect.width() / 2; |
|
90 | 90 | |
|
91 | 91 | // apply size factor |
|
92 |
radius *= m_series-> |
|
|
92 | radius *= m_series->pieSize(); | |
|
93 | 93 | |
|
94 | 94 | // update slices |
|
95 | 95 | if (m_pieCenter != center || m_pieRadius != radius) { |
|
96 | 96 | m_pieCenter = center; |
|
97 | 97 | m_pieRadius = radius; |
|
98 | 98 | //qDebug() << "PiePresenter::updateGeometry()" << m_rect << m_pieCenter << m_pieRadius; |
|
99 | 99 | foreach (PieSlice* s, m_slices.values()) { |
|
100 | 100 | s->setPieCenterAndRadius(center, radius); |
|
101 | 101 | s->updateGeometry(); |
|
102 | 102 | s->update(); |
|
103 | 103 | } |
|
104 | 104 | } |
|
105 | 105 | |
|
106 | 106 | update(); |
|
107 | 107 | } |
|
108 | 108 | |
|
109 | 109 | void PiePresenter::addSlice(QPieSlice* sliceData) |
|
110 | 110 | { |
|
111 | 111 | //qDebug() << "PiePresenter::addSlice()" << sliceData; |
|
112 | 112 | |
|
113 | 113 | if (m_slices.keys().contains(sliceData)) { |
|
114 | 114 | Q_ASSERT(0); // TODO: how to handle this nicely? |
|
115 | 115 | return; |
|
116 | 116 | } |
|
117 | 117 | |
|
118 | 118 | // create slice |
|
119 | 119 | PieSlice *slice = new PieSlice(this); |
|
120 | 120 | slice->setPieCenterAndRadius(m_pieCenter, m_pieRadius); |
|
121 | 121 | slice->updateData(sliceData); |
|
122 | 122 | slice->updateGeometry(); |
|
123 | 123 | slice->update(); |
|
124 | 124 | m_slices.insert(sliceData, slice); |
|
125 | 125 | |
|
126 | 126 | // connect signals |
|
127 | 127 | connect(slice, SIGNAL(clicked()), sliceData, SIGNAL(clicked())); |
|
128 | 128 | connect(slice, SIGNAL(hoverEnter()), sliceData, SIGNAL(hoverEnter())); |
|
129 | 129 | connect(slice, SIGNAL(hoverLeave()), sliceData, SIGNAL(hoverLeave())); |
|
130 | 130 | } |
|
131 | 131 | |
|
132 | 132 | void PiePresenter::updateSlice(QPieSlice* sliceData) |
|
133 | 133 | { |
|
134 | 134 | //qDebug() << "PiePresenter::updateSlice()" << sliceData; |
|
135 | 135 | |
|
136 | 136 | if (!m_slices.contains(sliceData)) { |
|
137 | 137 | Q_ASSERT(0); // TODO: how to handle this nicely? |
|
138 | 138 | return; |
|
139 | 139 | } |
|
140 | 140 | |
|
141 | 141 | m_slices[sliceData]->updateData(sliceData); |
|
142 | 142 | } |
|
143 | 143 | |
|
144 | 144 | void PiePresenter::deleteSlice(QPieSlice* sliceData) |
|
145 | 145 | { |
|
146 | 146 | //qDebug() << "PiePresenter::deleteSlice()" << sliceData; |
|
147 | 147 | |
|
148 | 148 | if (!m_slices.contains(sliceData)) { |
|
149 | 149 | Q_ASSERT(0); // TODO: how to handle this nicely? |
|
150 | 150 | return; |
|
151 | 151 | } |
|
152 | 152 | |
|
153 | 153 | delete m_slices.take(sliceData); |
|
154 | 154 | } |
|
155 | 155 | |
|
156 | 156 | #include "moc_piepresenter_p.cpp" |
|
157 | 157 | |
|
158 | 158 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,562 +1,563 | |||
|
1 | 1 | #include "qpieseries.h" |
|
2 | 2 | #include "qpieslice.h" |
|
3 | 3 | #include <QDebug> |
|
4 | 4 | |
|
5 | 5 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
6 | 6 | |
|
7 | 7 | |
|
8 | 8 | /*! |
|
9 | 9 | \class QPieSeries::ChangeSet |
|
10 | 10 | \brief Defines the changes in the series. |
|
11 | 11 | |
|
12 | 12 | Contains the changes that have occurred in the series. Lists of added, changed and removed slices. |
|
13 | 13 | |
|
14 | 14 | \sa QPieSeries::changed() |
|
15 | 15 | */ |
|
16 | 16 | |
|
17 | 17 | /*! |
|
18 | 18 | \internal |
|
19 | 19 | */ |
|
20 | 20 | void QPieSeries::ChangeSet::appendAdded(QPieSlice* slice) |
|
21 | 21 | { |
|
22 | 22 | if (!m_added.contains(slice)) |
|
23 | 23 | m_added << slice; |
|
24 | 24 | } |
|
25 | 25 | |
|
26 | 26 | /*! |
|
27 | 27 | \internal |
|
28 | 28 | */ |
|
29 | 29 | void QPieSeries::ChangeSet::appendAdded(QList<QPieSlice*> slices) |
|
30 | 30 | { |
|
31 | 31 | foreach (QPieSlice* s, slices) |
|
32 | 32 | appendAdded(s); |
|
33 | 33 | } |
|
34 | 34 | |
|
35 | 35 | /*! |
|
36 | 36 | \internal |
|
37 | 37 | */ |
|
38 | 38 | void QPieSeries::ChangeSet::appendChanged(QPieSlice* slice) |
|
39 | 39 | { |
|
40 | 40 | if (!m_changed.contains(slice)) |
|
41 | 41 | m_changed << slice; |
|
42 | 42 | } |
|
43 | 43 | |
|
44 | 44 | /*! |
|
45 | 45 | \internal |
|
46 | 46 | */ |
|
47 | 47 | void QPieSeries::ChangeSet::appendRemoved(QPieSlice* slice) |
|
48 | 48 | { |
|
49 | 49 | if (!m_removed.contains(slice)) |
|
50 | 50 | m_removed << slice; |
|
51 | 51 | } |
|
52 | 52 | |
|
53 | 53 | /*! |
|
54 | 54 | Returns a list of slices that have been added to the series. |
|
55 | 55 | \sa QPieSeries::changed() |
|
56 | 56 | */ |
|
57 | 57 | QList<QPieSlice*> QPieSeries::ChangeSet::added() const |
|
58 | 58 | { |
|
59 | 59 | return m_added; |
|
60 | 60 | } |
|
61 | 61 | |
|
62 | 62 | /*! |
|
63 | 63 | Returns a list of slices that have been changed in the series. |
|
64 | 64 | \sa QPieSeries::changed() |
|
65 | 65 | */ |
|
66 | 66 | QList<QPieSlice*> QPieSeries::ChangeSet::changed() const |
|
67 | 67 | { |
|
68 | 68 | return m_changed; |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | /*! |
|
72 | 72 | Returns a list of slices that have been removed from the series. |
|
73 | 73 | \sa QPieSeries::changed() |
|
74 | 74 | */ |
|
75 | 75 | QList<QPieSlice*> QPieSeries::ChangeSet::removed() const |
|
76 | 76 | { |
|
77 | 77 | return m_removed; |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | |
|
81 | 81 | /*! |
|
82 | 82 | Returns true if there are no added/changed or removed slices in the change set. |
|
83 | 83 | */ |
|
84 | 84 | bool QPieSeries::ChangeSet::isEmpty() const |
|
85 | 85 | { |
|
86 | 86 | if (m_added.count() || m_changed.count() || m_removed.count()) |
|
87 | 87 | return false; |
|
88 | 88 | return true; |
|
89 | 89 | } |
|
90 | 90 | |
|
91 | 91 | /*! |
|
92 | 92 | \class QPieSeries |
|
93 | 93 | \brief Pie series API for QtCommercial Charts |
|
94 | 94 | |
|
95 | 95 | The pie series defines a pie chart which consists of pie slices which are QPieSlice objects. |
|
96 | 96 | The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices. |
|
97 | 97 | The actual slice size is determined by that relative value. |
|
98 | 98 | |
|
99 | 99 | By default the pie is defined as a full pie but it can be a partial pie. |
|
100 | 100 | This can be done by setting a starting angle and angle span to the series. |
|
101 | 101 | */ |
|
102 | 102 | |
|
103 | 103 | /*! |
|
104 | 104 | Constructs a series object which is a child of \a parent. |
|
105 | 105 | */ |
|
106 | 106 | QPieSeries::QPieSeries(QObject *parent) : |
|
107 | 107 | QSeries(parent), |
|
108 | m_hPositionFactor(0.5), | |
|
109 | m_vPositionFactor(0.5), | |
|
110 |
m_pieSize |
|
|
108 | m_pieRelativeHorPos(0.5), | |
|
109 | m_pieRelativeVerPos(0.5), | |
|
110 | m_pieRelativeSize(0.7), | |
|
111 | 111 | m_pieStartAngle(0), |
|
112 | 112 | m_pieEndAngle(360), |
|
113 | 113 | m_total(0) |
|
114 | 114 | { |
|
115 | 115 | |
|
116 | 116 | } |
|
117 | 117 | |
|
118 | 118 | /*! |
|
119 | 119 | Destroys the object. Note that adding series to QChart transfers the ownership to the chart. |
|
120 | 120 | */ |
|
121 | 121 | QPieSeries::~QPieSeries() |
|
122 | 122 | { |
|
123 | 123 | |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | /*! |
|
127 | 127 | Returns QChartSeries::SeriesTypePie. |
|
128 | 128 | */ |
|
129 | 129 | QSeries::QSeriesType QPieSeries::type() const |
|
130 | 130 | { |
|
131 | 131 | return QSeries::SeriesTypePie; |
|
132 | 132 | } |
|
133 | 133 | |
|
134 | 134 | /*! |
|
135 | 135 | Sets an array of \a slices to the series replacing the existing slices. |
|
136 | 136 | Slice ownership is passed to the series. |
|
137 | 137 | */ |
|
138 | 138 | void QPieSeries::replace(QList<QPieSlice*> slices) |
|
139 | 139 | { |
|
140 | 140 | clear(); |
|
141 | 141 | add(slices); |
|
142 | 142 | } |
|
143 | 143 | |
|
144 | 144 | /*! |
|
145 | 145 | Adds an array of \a slices to the series. |
|
146 | 146 | Slice ownership is passed to the series. |
|
147 | 147 | */ |
|
148 | 148 | void QPieSeries::add(QList<QPieSlice*> slices) |
|
149 | 149 | { |
|
150 | 150 | ChangeSet changeSet; |
|
151 | 151 | foreach (QPieSlice* s, slices) { |
|
152 | 152 | s->setParent(this); |
|
153 | 153 | m_slices << s; |
|
154 | 154 | changeSet.appendAdded(s); |
|
155 | 155 | } |
|
156 | 156 | |
|
157 | 157 | updateDerivativeData(); |
|
158 | 158 | |
|
159 | 159 | foreach (QPieSlice* s, slices) { |
|
160 | 160 | connect(s, SIGNAL(changed()), this, SLOT(sliceChanged())); |
|
161 | 161 | connect(s, SIGNAL(clicked()), this, SLOT(sliceClicked())); |
|
162 | 162 | connect(s, SIGNAL(hoverEnter()), this, SLOT(sliceHoverEnter())); |
|
163 | 163 | connect(s, SIGNAL(hoverLeave()), this, SLOT(sliceHoverLeave())); |
|
164 | 164 | } |
|
165 | 165 | |
|
166 | 166 | emit changed(changeSet); |
|
167 | 167 | } |
|
168 | 168 | |
|
169 | 169 | /*! |
|
170 | 170 | Adds a single \a slice to the series. |
|
171 | 171 | Slice ownership is passed to the series. |
|
172 | 172 | */ |
|
173 | 173 | void QPieSeries::add(QPieSlice* slice) |
|
174 | 174 | { |
|
175 | 175 | add(QList<QPieSlice*>() << slice); |
|
176 | 176 | } |
|
177 | 177 | |
|
178 | 178 | /*! |
|
179 | 179 | Adds a single \a slice to the series and returns a reference to the series. |
|
180 | 180 | Slice ownership is passed to the series. |
|
181 | 181 | */ |
|
182 | 182 | QPieSeries& QPieSeries::operator << (QPieSlice* slice) |
|
183 | 183 | { |
|
184 | 184 | add(slice); |
|
185 | 185 | return *this; |
|
186 | 186 | } |
|
187 | 187 | |
|
188 | 188 | |
|
189 | 189 | /*! |
|
190 | 190 | Adds a single slice to the series with give \a value and \a name. |
|
191 | 191 | Slice ownership is passed to the series. |
|
192 | 192 | */ |
|
193 | 193 | QPieSlice* QPieSeries::add(qreal value, QString name) |
|
194 | 194 | { |
|
195 | 195 | QPieSlice* slice = new QPieSlice(value, name); |
|
196 | 196 | add(slice); |
|
197 | 197 | return slice; |
|
198 | 198 | } |
|
199 | 199 | |
|
200 | 200 | /*! |
|
201 | 201 | Removes a single \a slice from the series and deletes the slice. |
|
202 | 202 | |
|
203 | 203 | Do not reference this pointer after this call. |
|
204 | 204 | */ |
|
205 | 205 | void QPieSeries::remove(QPieSlice* slice) |
|
206 | 206 | { |
|
207 | 207 | if (!m_slices.removeOne(slice)) { |
|
208 | 208 | Q_ASSERT(0); // TODO: how should this be reported? |
|
209 | 209 | return; |
|
210 | 210 | } |
|
211 | 211 | |
|
212 | 212 | ChangeSet changeSet; |
|
213 | 213 | changeSet.appendRemoved(slice); |
|
214 | 214 | emit changed(changeSet); |
|
215 | 215 | |
|
216 | 216 | delete slice; |
|
217 | 217 | slice = NULL; |
|
218 | 218 | |
|
219 | 219 | updateDerivativeData(); |
|
220 | 220 | } |
|
221 | 221 | |
|
222 | 222 | /*! |
|
223 | 223 | Clears all slices from the series. |
|
224 | 224 | */ |
|
225 | 225 | void QPieSeries::clear() |
|
226 | 226 | { |
|
227 | 227 | if (m_slices.count() == 0) |
|
228 | 228 | return; |
|
229 | 229 | |
|
230 | 230 | ChangeSet changeSet; |
|
231 | 231 | foreach (QPieSlice* s, m_slices) { |
|
232 | 232 | changeSet.appendRemoved(s); |
|
233 | 233 | m_slices.removeOne(s); |
|
234 | 234 | delete s; |
|
235 | 235 | } |
|
236 | 236 | emit changed(changeSet); |
|
237 | 237 | updateDerivativeData(); |
|
238 | 238 | } |
|
239 | 239 | |
|
240 | 240 | /*! |
|
241 | 241 | Counts the number of the slices in this series. |
|
242 | 242 | */ |
|
243 | 243 | int QPieSeries::count() const |
|
244 | 244 | { |
|
245 | 245 | return m_slices.count(); |
|
246 | 246 | } |
|
247 | 247 | |
|
248 | 248 | /*! |
|
249 | 249 | Returns a list of slices that belong to this series. |
|
250 | 250 | */ |
|
251 | 251 | QList<QPieSlice*> QPieSeries::slices() const |
|
252 | 252 | { |
|
253 | 253 | return m_slices; |
|
254 | 254 | } |
|
255 | 255 | |
|
256 | 256 | /*! |
|
257 |
Sets the center position of the pie by \a |
|
|
257 | Sets the center position of the pie by \a relativeHorizontalPosition and \a relativeVerticalPosition. | |
|
258 | 258 | |
|
259 | 259 | The factors are relative to the chart rectangle where: |
|
260 | 260 | |
|
261 |
\a |
|
|
262 |
\a |
|
|
263 |
\a vertical |
|
|
264 |
\a vertical |
|
|
261 | \a relativeHorizontalPosition 0.0 means the absolute left. | |
|
262 | \a relativeHorizontalPosition 1.0 means the absolute right. | |
|
263 | \a relativeVerticalPosition 0.0 means the absolute top. | |
|
264 | \a relativeVerticalPosition 1.0 means the absolute bottom. | |
|
265 | 265 | |
|
266 |
By default |
|
|
266 | By default both values are 0.5 which puts the pie in the middle of the chart rectangle. | |
|
267 | 267 | |
|
268 |
\sa |
|
|
268 | \sa pieHorizontalPosition(), pieVerticalPosition(), setPieSize() | |
|
269 | 269 | */ |
|
270 |
void QPieSeries::setPosition |
|
|
270 | void QPieSeries::setPiePosition(qreal relativeHorizontalPosition, qreal relativeVerticalPosition) | |
|
271 | 271 | { |
|
272 | if (horizontalFactor < 0.0 || horizontalFactor > 1.0 || verticalFactor < 0.0 || verticalFactor > 1.0) | |
|
272 | if (relativeHorizontalPosition < 0.0 || relativeHorizontalPosition > 1.0 || | |
|
273 | relativeVerticalPosition < 0.0 || relativeVerticalPosition > 1.0) | |
|
273 | 274 | return; |
|
274 | 275 | |
|
275 | if (m_hPositionFactor != horizontalFactor || m_vPositionFactor != verticalFactor) { | |
|
276 | m_hPositionFactor = horizontalFactor; | |
|
277 | m_vPositionFactor = verticalFactor; | |
|
278 | emit positionChanged(); | |
|
276 | if (m_pieRelativeHorPos != relativeHorizontalPosition || m_pieRelativeVerPos != relativeVerticalPosition) { | |
|
277 | m_pieRelativeHorPos = relativeHorizontalPosition; | |
|
278 | m_pieRelativeVerPos = relativeVerticalPosition; | |
|
279 | emit piePositionChanged(); | |
|
279 | 280 | } |
|
280 | 281 | } |
|
281 | 282 | |
|
282 | 283 | /*! |
|
283 |
Gets the horizontal position |
|
|
284 | Gets the horizontal position of the pie. | |
|
284 | 285 | |
|
285 |
The |
|
|
286 | The returned value is relative to the chart rectangle where: | |
|
286 | 287 | |
|
287 |
|
|
|
288 |
|
|
|
288 | 0.0 means the absolute left. | |
|
289 | 1.0 means the absolute right. | |
|
289 | 290 | |
|
290 |
By default |
|
|
291 | By default it is 0.5 which puts the pie in the horizontal middle of the chart rectangle. | |
|
291 | 292 | |
|
292 |
\sa setPosition |
|
|
293 | \sa setPiePosition(), pieVerticalPosition(), setPieSize() | |
|
293 | 294 | */ |
|
294 |
qreal QPieSeries:: |
|
|
295 | qreal QPieSeries::pieHorizontalPosition() const | |
|
295 | 296 | { |
|
296 | return m_hPositionFactor; | |
|
297 | return m_pieRelativeHorPos; | |
|
297 | 298 | } |
|
298 | 299 | |
|
299 | 300 | /*! |
|
300 |
Gets the vertical position |
|
|
301 | Gets the vertical position position of the pie. | |
|
301 | 302 | |
|
302 |
The |
|
|
303 | The returned value is relative to the chart rectangle where: | |
|
303 | 304 | |
|
304 |
|
|
|
305 |
|
|
|
305 | 0.0 means the absolute top. | |
|
306 | 1.0 means the absolute bottom. | |
|
306 | 307 | |
|
307 |
By default |
|
|
308 | By default it is 0.5 which puts the pie in the vertical middle of the chart rectangle. | |
|
308 | 309 | |
|
309 |
\sa setPosition |
|
|
310 | \sa setPiePosition(), pieHorizontalPosition(), setPieSize() | |
|
310 | 311 | */ |
|
311 |
qreal QPieSeries:: |
|
|
312 | qreal QPieSeries::pieVerticalPosition() const | |
|
312 | 313 | { |
|
313 |
return m_ |
|
|
314 | return m_pieRelativeVerPos; | |
|
314 | 315 | } |
|
315 | 316 | |
|
316 | 317 | /*! |
|
317 |
Sets the |
|
|
318 | Sets the relative size of the pie. | |
|
318 | 319 | |
|
319 |
The |
|
|
320 | The \a relativeSize is defined so that the 1.0 is the maximum that can fit the given chart rectangle. | |
|
320 | 321 | |
|
321 | 322 | Default value is 0.7. |
|
322 | 323 | |
|
323 |
\sa |
|
|
324 | \sa pieSize(), setPiePosition(), pieVerticalPosition(), pieHorizontalPosition() | |
|
324 | 325 | */ |
|
325 |
void QPieSeries::setSize |
|
|
326 | void QPieSeries::setPieSize(qreal relativeSize) | |
|
326 | 327 | { |
|
327 | if (sizeFactor < 0.0) | |
|
328 | if (relativeSize < 0.0 || relativeSize > 1.0) | |
|
328 | 329 | return; |
|
329 | 330 | |
|
330 |
if (m_pieSize |
|
|
331 |
m_pieSize |
|
|
332 |
emit |
|
|
331 | if (m_pieRelativeSize != relativeSize) { | |
|
332 | m_pieRelativeSize = relativeSize; | |
|
333 | emit pieSizeChanged(); | |
|
333 | 334 | } |
|
334 | 335 | } |
|
335 | 336 | |
|
336 | 337 | /*! |
|
337 |
Gets the size |
|
|
338 | Gets the relative size of the pie. | |
|
338 | 339 | |
|
339 |
The size |
|
|
340 | The size is defined so that the 1.0 is the maximum that can fit the given chart rectangle. | |
|
340 | 341 | |
|
341 | 342 | Default value is 0.7. |
|
342 | 343 | |
|
343 |
\sa setSize |
|
|
344 | \sa setPieSize(), setPiePosition(), pieVerticalPosition(), pieHorizontalPosition() | |
|
344 | 345 | */ |
|
345 |
qreal QPieSeries:: |
|
|
346 | qreal QPieSeries::pieSize() const | |
|
346 | 347 | { |
|
347 |
return m_pieSize |
|
|
348 | return m_pieRelativeSize; | |
|
348 | 349 | } |
|
349 | 350 | |
|
350 | 351 | |
|
351 | 352 | /*! |
|
352 | 353 | Sets the end angle of the pie. |
|
353 | 354 | |
|
354 | 355 | Full pie is 360 degrees where 0 degrees is at 12 a'clock. |
|
355 | 356 | |
|
356 |
\a |
|
|
357 | \a angle must be less than pie end angle. Default value is 0. | |
|
357 | 358 | |
|
358 |
\sa |
|
|
359 | \sa pieStartAngle(), pieEndAngle(), setPieEndAngle() | |
|
359 | 360 | */ |
|
360 |
void QPieSeries::setStartAngle(qreal |
|
|
361 | void QPieSeries::setPieStartAngle(qreal angle) | |
|
361 | 362 | { |
|
362 |
if ( |
|
|
363 |
m_pieStartAngle = |
|
|
363 | if (angle >= 0 && angle <= 360 && angle != m_pieStartAngle && angle <= m_pieEndAngle) { | |
|
364 | m_pieStartAngle = angle; | |
|
364 | 365 | updateDerivativeData(); |
|
365 | 366 | } |
|
366 | 367 | } |
|
367 | 368 | |
|
368 | 369 | /*! |
|
369 | 370 | Gets the start angle of the pie. |
|
370 | 371 | |
|
371 | 372 | Full pie is 360 degrees where 0 degrees is at 12 a'clock. Default value is 360. |
|
372 | 373 | |
|
373 | \sa setStartAngle(), endAngle(), setEndAngle() | |
|
374 | \sa setPieStartAngle(), pieEndAngle(), setPieEndAngle() | |
|
374 | 375 | */ |
|
375 |
qreal QPieSeries:: |
|
|
376 | qreal QPieSeries::pieStartAngle() const | |
|
376 | 377 | { |
|
377 | 378 | return m_pieStartAngle; |
|
378 | 379 | } |
|
379 | 380 | |
|
380 | 381 | /*! |
|
381 | 382 | Sets the end angle of the pie. |
|
382 | 383 | |
|
383 | 384 | Full pie is 360 degrees where 0 degrees is at 12 a'clock. |
|
384 | 385 | |
|
385 |
\a |
|
|
386 | \a angle must be greater than start angle. | |
|
386 | 387 | |
|
387 |
\sa endAngle(), |
|
|
388 | \sa pieEndAngle(), pieStartAngle(), setPieStartAngle() | |
|
388 | 389 | */ |
|
389 |
void QPieSeries::setEndAngle(qreal |
|
|
390 | void QPieSeries::setPieEndAngle(qreal angle) | |
|
390 | 391 | { |
|
391 |
if ( |
|
|
392 |
m_pieEndAngle = |
|
|
392 | if (angle >= 0 && angle <= 360 && angle != m_pieEndAngle && angle >= m_pieStartAngle) { | |
|
393 | m_pieEndAngle = angle; | |
|
393 | 394 | updateDerivativeData(); |
|
394 | 395 | } |
|
395 | 396 | } |
|
396 | 397 | |
|
397 | 398 | /*! |
|
398 | 399 | Returns the end angle of the pie. |
|
399 | 400 | |
|
400 | 401 | Full pie is 360 degrees where 0 degrees is at 12 a'clock. |
|
401 | 402 | |
|
402 |
\sa setEndAngle(), |
|
|
403 | \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle() | |
|
403 | 404 | */ |
|
404 | qreal QPieSeries::endAngle() const | |
|
405 | qreal QPieSeries::pieEndAngle() const | |
|
405 | 406 | { |
|
406 | 407 | return m_pieEndAngle; |
|
407 | 408 | } |
|
408 | 409 | |
|
409 | 410 | /*! |
|
410 | 411 | Sets the all the slice labels \a visible or invisible. |
|
411 | 412 | |
|
412 | 413 | \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible() |
|
413 | 414 | */ |
|
414 | 415 | void QPieSeries::setLabelsVisible(bool visible) |
|
415 | 416 | { |
|
416 | 417 | foreach (QPieSlice* s, m_slices) |
|
417 | 418 | s->setLabelVisible(visible); |
|
418 | 419 | } |
|
419 | 420 | |
|
420 | 421 | /*! |
|
421 | 422 | Returns the sum of all slice values in this series. |
|
422 | 423 | |
|
423 | 424 | \sa QPieSlice::value(), QPieSlice::setValue() |
|
424 | 425 | */ |
|
425 | 426 | qreal QPieSeries::total() const |
|
426 | 427 | { |
|
427 | 428 | return m_total; |
|
428 | 429 | } |
|
429 | 430 | |
|
430 | 431 | /*! |
|
431 | 432 | \fn void QPieSeries::changed(const QPieSeries::ChangeSet& changeSet) |
|
432 | 433 | |
|
433 | 434 | This signal emitted when something has changed in the series. |
|
434 | 435 | The \a changeSet contains the details of which slices have been added, changed or removed. |
|
435 | 436 | |
|
436 | 437 | \sa QPieSeries::ChangeSet, QPieSlice::changed() |
|
437 | 438 | */ |
|
438 | 439 | |
|
439 | 440 | /*! |
|
440 | 441 | \fn void QPieSeries::clicked(QPieSlice* slice) |
|
441 | 442 | |
|
442 | 443 | This signal is emitted when a \a slice has been clicked. |
|
443 | 444 | |
|
444 | 445 | \sa QPieSlice::clicked() |
|
445 | 446 | */ |
|
446 | 447 | |
|
447 | 448 | /*! |
|
448 | 449 | \fn void QPieSeries::hoverEnter(QPieSlice* slice) |
|
449 | 450 | |
|
450 | 451 | This signal is emitted when user has hovered over a \a slice. |
|
451 | 452 | |
|
452 | 453 | \sa QPieSlice::hoverEnter() |
|
453 | 454 | */ |
|
454 | 455 | |
|
455 | 456 | /*! |
|
456 | 457 | \fn void QPieSeries::hoverLeave(QPieSlice* slice) |
|
457 | 458 | |
|
458 | 459 | This signal is emitted when user has hovered away from a \a slice. |
|
459 | 460 | |
|
460 | 461 | \sa QPieSlice::hoverLeave() |
|
461 | 462 | */ |
|
462 | 463 | |
|
463 | 464 | /*! |
|
464 |
\fn void QPieSeries:: |
|
|
465 | \fn void QPieSeries::pieSizeChanged() | |
|
465 | 466 | |
|
466 | 467 | This signal is emitted when size factor has been changed. |
|
467 | 468 | |
|
468 |
\sa |
|
|
469 | \sa pieSize(), setPieSize() | |
|
469 | 470 | */ |
|
470 | 471 | |
|
471 | 472 | /*! |
|
472 | \fn void QPieSeries::positionChanged() | |
|
473 | \fn void QPieSeries::piePositionChanged() | |
|
473 | 474 | |
|
474 | 475 | This signal is emitted when position of the pie has been changed. |
|
475 | 476 | |
|
476 |
\sa |
|
|
477 | \sa pieHorizontalPosition(), pieVerticalPosition(), setPiePosition() | |
|
477 | 478 | */ |
|
478 | 479 | |
|
479 | 480 | void QPieSeries::sliceChanged() |
|
480 | 481 | { |
|
481 | 482 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); |
|
482 | 483 | Q_ASSERT(m_slices.contains(slice)); |
|
483 | 484 | |
|
484 | 485 | ChangeSet changeSet; |
|
485 | 486 | changeSet.appendChanged(slice); |
|
486 | 487 | emit changed(changeSet); |
|
487 | 488 | |
|
488 | 489 | updateDerivativeData(); |
|
489 | 490 | } |
|
490 | 491 | |
|
491 | 492 | void QPieSeries::sliceClicked() |
|
492 | 493 | { |
|
493 | 494 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); |
|
494 | 495 | Q_ASSERT(m_slices.contains(slice)); |
|
495 | 496 | emit clicked(slice); |
|
496 | 497 | } |
|
497 | 498 | |
|
498 | 499 | void QPieSeries::sliceHoverEnter() |
|
499 | 500 | { |
|
500 | 501 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); |
|
501 | 502 | Q_ASSERT(m_slices.contains(slice)); |
|
502 | 503 | emit hoverEnter(slice); |
|
503 | 504 | } |
|
504 | 505 | |
|
505 | 506 | void QPieSeries::sliceHoverLeave() |
|
506 | 507 | { |
|
507 | 508 | QPieSlice* slice = qobject_cast<QPieSlice *>(sender()); |
|
508 | 509 | Q_ASSERT(m_slices.contains(slice)); |
|
509 | 510 | emit hoverLeave(slice); |
|
510 | 511 | } |
|
511 | 512 | |
|
512 | 513 | void QPieSeries::updateDerivativeData() |
|
513 | 514 | { |
|
514 | 515 | m_total = 0; |
|
515 | 516 | |
|
516 | 517 | // nothing to do? |
|
517 | 518 | if (m_slices.count() == 0) |
|
518 | 519 | return; |
|
519 | 520 | |
|
520 | 521 | // calculate total |
|
521 | 522 | foreach (QPieSlice* s, m_slices) |
|
522 | 523 | m_total += s->value(); |
|
523 | 524 | |
|
524 | 525 | // we must have some values |
|
525 | 526 | if (m_total == 0) { |
|
526 | 527 | qDebug() << "QPieSeries::updateDerivativeData() total == 0"; |
|
527 | 528 | Q_ASSERT(m_total > 0); // TODO: is this the correct way to handle this? |
|
528 | 529 | } |
|
529 | 530 | |
|
530 | 531 | // update slice attributes |
|
531 | 532 | qreal sliceAngle = m_pieStartAngle; |
|
532 | 533 | qreal pieSpan = m_pieEndAngle - m_pieStartAngle; |
|
533 | 534 | foreach (QPieSlice* s, m_slices) { |
|
534 | 535 | |
|
535 | 536 | bool changed = false; |
|
536 | 537 | |
|
537 | 538 | qreal percentage = s->value() / m_total; |
|
538 | 539 | if (s->m_percentage != percentage) { |
|
539 | 540 | s->m_percentage = percentage; |
|
540 | 541 | changed = true; |
|
541 | 542 | } |
|
542 | 543 | |
|
543 | 544 | qreal sliceSpan = pieSpan * percentage; |
|
544 | 545 | if (s->m_angleSpan != sliceSpan) { |
|
545 | 546 | s->m_angleSpan = sliceSpan; |
|
546 | 547 | changed = true; |
|
547 | 548 | } |
|
548 | 549 | |
|
549 | 550 | if (s->m_startAngle != sliceAngle) { |
|
550 | 551 | s->m_startAngle = sliceAngle; |
|
551 | 552 | changed = true; |
|
552 | 553 | } |
|
553 | 554 | sliceAngle += sliceSpan; |
|
554 | 555 | |
|
555 | 556 | if (changed) |
|
556 | 557 | emit s->changed(); |
|
557 | 558 | } |
|
558 | 559 | } |
|
559 | 560 | |
|
560 | 561 | #include "moc_qpieseries.cpp" |
|
561 | 562 | |
|
562 | 563 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,134 +1,133 | |||
|
1 | 1 | #ifndef PIESERIES_H |
|
2 | 2 | #define PIESERIES_H |
|
3 | 3 | |
|
4 | 4 | #include "qseries.h" |
|
5 | 5 | #include <QObject> |
|
6 | 6 | #include <QRectF> |
|
7 | 7 | #include <QColor> |
|
8 | 8 | #include <QPen> |
|
9 | 9 | #include <QBrush> |
|
10 | 10 | #include <QSignalMapper> |
|
11 | 11 | |
|
12 | 12 | class QGraphicsObject; |
|
13 | 13 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
14 | 14 | class PiePresenter; |
|
15 | 15 | class PieSlice; |
|
16 | 16 | class QPieSlice; |
|
17 | 17 | |
|
18 | 18 | class QTCOMMERCIALCHART_EXPORT QPieSeries : public QSeries |
|
19 | 19 | { |
|
20 | 20 | Q_OBJECT |
|
21 | 21 | |
|
22 | 22 | public: |
|
23 | 23 | |
|
24 | 24 | class ChangeSet |
|
25 | 25 | { |
|
26 | 26 | public: |
|
27 | 27 | |
|
28 | 28 | // TODO: these should not really be exposed to the public API |
|
29 | 29 | void appendAdded(QPieSlice* slice); |
|
30 | 30 | void appendAdded(QList<QPieSlice*> slices); |
|
31 | 31 | void appendChanged(QPieSlice* slice); |
|
32 | 32 | void appendRemoved(QPieSlice* slice); |
|
33 | 33 | |
|
34 | 34 | QList<QPieSlice*> added() const; |
|
35 | 35 | QList<QPieSlice*> changed() const; |
|
36 | 36 | QList<QPieSlice*> removed() const; |
|
37 | 37 | |
|
38 | 38 | bool isEmpty() const; |
|
39 | 39 | |
|
40 | 40 | private: |
|
41 | 41 | QList<QPieSlice*> m_added; |
|
42 | 42 | QList<QPieSlice*> m_changed; |
|
43 | 43 | QList<QPieSlice*> m_removed; |
|
44 | 44 | }; |
|
45 | 45 | |
|
46 | 46 | public: |
|
47 | 47 | QPieSeries(QObject *parent = 0); |
|
48 | 48 | virtual ~QPieSeries(); |
|
49 | 49 | |
|
50 | 50 | public: // from QChartSeries |
|
51 | 51 | QSeriesType type() const; |
|
52 | 52 | |
|
53 | 53 | public: |
|
54 | 54 | |
|
55 | 55 | // slice setters |
|
56 | 56 | void add(QPieSlice* slice); |
|
57 | 57 | void add(QList<QPieSlice*> slices); |
|
58 | 58 | void replace(QList<QPieSlice*> slices); |
|
59 | 59 | void remove(QPieSlice* slice); |
|
60 | 60 | void clear(); |
|
61 | 61 | |
|
62 | 62 | // sluce getters |
|
63 | 63 | QList<QPieSlice*> slices() const; |
|
64 | 64 | |
|
65 | 65 | // calculated data |
|
66 | 66 | int count() const; |
|
67 | 67 | qreal total() const; |
|
68 | 68 | |
|
69 | 69 | // pie customization |
|
70 |
void setPosition |
|
|
71 |
qreal |
|
|
72 |
qreal |
|
|
73 |
void setSize |
|
|
74 |
qreal |
|
|
75 | void setStartAngle(qreal startAngle); | |
|
76 |
qreal |
|
|
77 | void setEndAngle(qreal endAngle); | |
|
78 | qreal endAngle() const; | |
|
70 | void setPiePosition(qreal relativeHorizontalPosition, qreal relativeVerticalPosition); | |
|
71 | qreal pieHorizontalPosition() const; | |
|
72 | qreal pieVerticalPosition() const; | |
|
73 | void setPieSize(qreal relativeSize); | |
|
74 | qreal pieSize() const; | |
|
75 | void setPieStartAngle(qreal startAngle); | |
|
76 | qreal pieStartAngle() const; | |
|
77 | void setPieEndAngle(qreal endAngle); | |
|
78 | qreal pieEndAngle() const; | |
|
79 | 79 | |
|
80 | 80 | // convenience function |
|
81 | 81 | QPieSeries& operator << (QPieSlice* slice); |
|
82 | 82 | QPieSlice* add(qreal value, QString name); |
|
83 | 83 | void setLabelsVisible(bool visible = true); |
|
84 | 84 | |
|
85 | 85 | // TODO: find slices? |
|
86 | 86 | // QList<QPieSlice*> findByValue(qreal value); |
|
87 | 87 | // ... |
|
88 | 88 | |
|
89 | 89 | // TODO: sorting slices? |
|
90 | 90 | // void sort(QPieSeries::SortByValue|label|??) |
|
91 | 91 | |
|
92 | 92 | // TODO: general graphics customization |
|
93 | 93 | // setDrawStyle(2d|3d) |
|
94 | 94 | // setDropShadows |
|
95 | 95 | |
|
96 | 96 | Q_SIGNALS: |
|
97 | ||
|
98 | void changed(const QPieSeries::ChangeSet& changeSet); | |
|
99 | ||
|
100 | 97 | void clicked(QPieSlice* slice); |
|
101 | 98 | void hoverEnter(QPieSlice* slice); |
|
102 | 99 | void hoverLeave(QPieSlice* slice); |
|
103 | 100 | |
|
104 |
void |
|
|
105 | void positionChanged(); | |
|
101 | void pieSizeChanged(); | |
|
102 | void piePositionChanged(); | |
|
103 | ||
|
104 | void changed(const QPieSeries::ChangeSet& changeSet); // TODO: hide this in PIMPL | |
|
106 | 105 | |
|
107 | 106 | private Q_SLOTS: // TODO: should be private and not visible in the interface at all |
|
108 | 107 | void sliceChanged(); |
|
109 | 108 | void sliceClicked(); |
|
110 | 109 | void sliceHoverEnter(); |
|
111 | 110 | void sliceHoverLeave(); |
|
112 | 111 | |
|
113 | 112 | private: |
|
114 | 113 | void updateDerivativeData(); |
|
115 | 114 | |
|
116 | 115 | private: |
|
117 | 116 | Q_DISABLE_COPY(QPieSeries) |
|
118 | 117 | |
|
119 | 118 | // TODO: use PIML |
|
120 | 119 | friend class PiePresenter; |
|
121 | 120 | friend class PieSlice; |
|
122 | 121 | |
|
123 | 122 | QList<QPieSlice*> m_slices; |
|
124 | qreal m_hPositionFactor; | |
|
125 | qreal m_vPositionFactor; | |
|
126 |
qreal m_pieSize |
|
|
123 | qreal m_pieRelativeHorPos; | |
|
124 | qreal m_pieRelativeVerPos; | |
|
125 | qreal m_pieRelativeSize; | |
|
127 | 126 | qreal m_pieStartAngle; |
|
128 | 127 | qreal m_pieEndAngle; |
|
129 | 128 | qreal m_total; |
|
130 | 129 | }; |
|
131 | 130 | |
|
132 | 131 | QTCOMMERCIALCHART_END_NAMESPACE |
|
133 | 132 | |
|
134 | 133 | #endif // PIESERIES_H |
General Comments 0
You need to be logged in to leave comments.
Login now