##// END OF EJS Templates
Renaming pen & brush functions for pie and adding const
Jani Honkonen -
r469:bfbe6f34c25e
parent child
Show More
@@ -1,236 +1,236
1 #include <QtGui/QApplication>
1 #include <QtGui/QApplication>
2 #include <QMainWindow>
2 #include <QMainWindow>
3 #include <qchartglobal.h>
3 #include <qchartglobal.h>
4 #include <qchartview.h>
4 #include <qchartview.h>
5 #include <qpieseries.h>
5 #include <qpieseries.h>
6 #include <qpieslice.h>
6 #include <qpieslice.h>
7 #include <QGridLayout>
7 #include <QGridLayout>
8 #include <QFormLayout>
8 #include <QFormLayout>
9 #include <QComboBox>
9 #include <QComboBox>
10 #include <QSpinBox>
10 #include <QSpinBox>
11 #include <QCheckBox>
11 #include <QCheckBox>
12 #include <QGroupBox>
12 #include <QGroupBox>
13 #include <QLabel>
13 #include <QLabel>
14
14
15 QTCOMMERCIALCHART_USE_NAMESPACE
15 QTCOMMERCIALCHART_USE_NAMESPACE
16
16
17 class CustomSlice : public QPieSlice
17 class CustomSlice : public QPieSlice
18 {
18 {
19 Q_OBJECT
19 Q_OBJECT
20 public:
20 public:
21 CustomSlice(qreal value, QString label)
21 CustomSlice(qreal value, QString label)
22 :QPieSlice(value, label)
22 :QPieSlice(value, label)
23 {
23 {
24 connect(this, SIGNAL(hoverEnter()), this, SLOT(handleHoverEnter()));
24 connect(this, SIGNAL(hoverEnter()), this, SLOT(handleHoverEnter()));
25 connect(this, SIGNAL(hoverLeave()), this, SLOT(handleHoverLeave()));
25 connect(this, SIGNAL(hoverLeave()), this, SLOT(handleHoverLeave()));
26 }
26 }
27
27
28 public Q_SLOTS:
28 public Q_SLOTS:
29
29
30 void handleHoverEnter()
30 void handleHoverEnter()
31 {
31 {
32 QBrush brush = this->brush();
32 QBrush brush = this->sliceBrush();
33 m_originalBrush = brush;
33 m_originalBrush = brush;
34 brush.setColor(brush.color().lighter());
34 brush.setColor(brush.color().lighter());
35 setBrush(brush);
35 setSliceBrush(brush);
36 }
36 }
37
37
38 void handleHoverLeave()
38 void handleHoverLeave()
39 {
39 {
40 setBrush(m_originalBrush);
40 setSliceBrush(m_originalBrush);
41 }
41 }
42
42
43 private:
43 private:
44 QBrush m_originalBrush;
44 QBrush m_originalBrush;
45 };
45 };
46
46
47 class MainWidget : public QWidget
47 class MainWidget : public QWidget
48 {
48 {
49 Q_OBJECT
49 Q_OBJECT
50
50
51 public:
51 public:
52 explicit MainWidget(QWidget* parent = 0)
52 explicit MainWidget(QWidget* parent = 0)
53 :QWidget(parent),
53 :QWidget(parent),
54 m_slice(0)
54 m_slice(0)
55 {
55 {
56 m_chartView = new QChartView();
56 m_chartView = new QChartView();
57 m_chartView->setChartTitle("Piechart customization");
57 m_chartView->setChartTitle("Piechart customization");
58 m_chartView->setRenderHint(QPainter::Antialiasing);
58 m_chartView->setRenderHint(QPainter::Antialiasing);
59 m_chartView->setChartTheme(QChart::ChartThemeIcy);
59 m_chartView->setChartTheme(QChart::ChartThemeIcy);
60
60
61 m_series = new QPieSeries();
61 m_series = new QPieSeries();
62 *m_series << new CustomSlice(10.0, "Slice 1");
62 *m_series << new CustomSlice(10.0, "Slice 1");
63 *m_series << new CustomSlice(20.0, "Slice 2");
63 *m_series << new CustomSlice(20.0, "Slice 2");
64 *m_series << new CustomSlice(30.0, "Slice 3");
64 *m_series << new CustomSlice(30.0, "Slice 3");
65 *m_series << new CustomSlice(40.0, "Slice 4");
65 *m_series << new CustomSlice(40.0, "Slice 4");
66 *m_series << new CustomSlice(50.0, "Slice 5");
66 *m_series << new CustomSlice(50.0, "Slice 5");
67 m_chartView->addSeries(m_series);
67 m_chartView->addSeries(m_series);
68
68
69 m_hPosition = new QDoubleSpinBox();
69 m_hPosition = new QDoubleSpinBox();
70 m_hPosition->setMinimum(0.0);
70 m_hPosition->setMinimum(0.0);
71 m_hPosition->setMaximum(1.0);
71 m_hPosition->setMaximum(1.0);
72 m_hPosition->setSingleStep(0.1);
72 m_hPosition->setSingleStep(0.1);
73 m_hPosition->setValue(m_series->horizontalPositionFactor());
73 m_hPosition->setValue(m_series->horizontalPositionFactor());
74
74
75 m_vPosition = new QDoubleSpinBox();
75 m_vPosition = new QDoubleSpinBox();
76 m_vPosition->setMinimum(0.0);
76 m_vPosition->setMinimum(0.0);
77 m_vPosition->setMaximum(1.0);
77 m_vPosition->setMaximum(1.0);
78 m_vPosition->setSingleStep(0.1);
78 m_vPosition->setSingleStep(0.1);
79 m_vPosition->setValue(m_series->verticalPositionFactor());
79 m_vPosition->setValue(m_series->verticalPositionFactor());
80
80
81 m_sizeFactor = new QDoubleSpinBox();
81 m_sizeFactor = new QDoubleSpinBox();
82 m_sizeFactor->setMinimum(0.0);
82 m_sizeFactor->setMinimum(0.0);
83 m_sizeFactor->setMaximum(1.0);
83 m_sizeFactor->setMaximum(1.0);
84 m_sizeFactor->setSingleStep(0.1);
84 m_sizeFactor->setSingleStep(0.1);
85 m_sizeFactor->setValue(m_series->sizeFactor());
85 m_sizeFactor->setValue(m_series->sizeFactor());
86
86
87 m_startAngle = new QDoubleSpinBox();
87 m_startAngle = new QDoubleSpinBox();
88 m_startAngle->setMinimum(0.0);
88 m_startAngle->setMinimum(0.0);
89 m_startAngle->setMaximum(360);
89 m_startAngle->setMaximum(360);
90 m_startAngle->setValue(m_series->startAngle());
90 m_startAngle->setValue(m_series->startAngle());
91 m_startAngle->setSingleStep(1);
91 m_startAngle->setSingleStep(1);
92
92
93 m_endAngle = new QDoubleSpinBox();
93 m_endAngle = new QDoubleSpinBox();
94 m_endAngle->setMinimum(0.0);
94 m_endAngle->setMinimum(0.0);
95 m_endAngle->setMaximum(360);
95 m_endAngle->setMaximum(360);
96 m_endAngle->setValue(m_series->endAngle());
96 m_endAngle->setValue(m_series->endAngle());
97 m_endAngle->setSingleStep(1);
97 m_endAngle->setSingleStep(1);
98
98
99 QFormLayout* seriesSettingsLayout = new QFormLayout();
99 QFormLayout* seriesSettingsLayout = new QFormLayout();
100 seriesSettingsLayout->addRow("Horizontal position", m_hPosition);
100 seriesSettingsLayout->addRow("Horizontal position", m_hPosition);
101 seriesSettingsLayout->addRow("Vertical position", m_vPosition);
101 seriesSettingsLayout->addRow("Vertical position", m_vPosition);
102 seriesSettingsLayout->addRow("Size factor", m_sizeFactor);
102 seriesSettingsLayout->addRow("Size factor", m_sizeFactor);
103 seriesSettingsLayout->addRow("Start angle", m_startAngle);
103 seriesSettingsLayout->addRow("Start angle", m_startAngle);
104 seriesSettingsLayout->addRow("End angle", m_endAngle);
104 seriesSettingsLayout->addRow("End angle", m_endAngle);
105 QGroupBox* seriesSettings = new QGroupBox("Series");
105 QGroupBox* seriesSettings = new QGroupBox("Series");
106 seriesSettings->setLayout(seriesSettingsLayout);
106 seriesSettings->setLayout(seriesSettingsLayout);
107
107
108 m_sliceName = new QLabel("<click a slice>");
108 m_sliceName = new QLabel("<click a slice>");
109 m_sliceValue = new QDoubleSpinBox();
109 m_sliceValue = new QDoubleSpinBox();
110 m_sliceValue->setMaximum(1000);
110 m_sliceValue->setMaximum(1000);
111 m_sliceLabelVisible = new QCheckBox();
111 m_sliceLabelVisible = new QCheckBox();
112 m_sliceLabelArmFactor = new QDoubleSpinBox();
112 m_sliceLabelArmFactor = new QDoubleSpinBox();
113 m_sliceLabelArmFactor->setSingleStep(0.01);
113 m_sliceLabelArmFactor->setSingleStep(0.01);
114 m_sliceExploded = new QCheckBox();
114 m_sliceExploded = new QCheckBox();
115 m_sliceExplodedFactor = new QDoubleSpinBox();
115 m_sliceExplodedFactor = new QDoubleSpinBox();
116 m_sliceExplodedFactor->setSingleStep(0.01);
116 m_sliceExplodedFactor->setSingleStep(0.01);
117
117
118 QFormLayout* sliceSettingsLayout = new QFormLayout();
118 QFormLayout* sliceSettingsLayout = new QFormLayout();
119 sliceSettingsLayout->addRow("Selected", m_sliceName);
119 sliceSettingsLayout->addRow("Selected", m_sliceName);
120 sliceSettingsLayout->addRow("Value", m_sliceValue);
120 sliceSettingsLayout->addRow("Value", m_sliceValue);
121 sliceSettingsLayout->addRow("Label visible", m_sliceLabelVisible);
121 sliceSettingsLayout->addRow("Label visible", m_sliceLabelVisible);
122 sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor);
122 sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor);
123 sliceSettingsLayout->addRow("Exploded", m_sliceExploded);
123 sliceSettingsLayout->addRow("Exploded", m_sliceExploded);
124 sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor);
124 sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor);
125
125
126 QGroupBox* sliceSettings = new QGroupBox("Slice");
126 QGroupBox* sliceSettings = new QGroupBox("Slice");
127 sliceSettings->setLayout(sliceSettingsLayout);
127 sliceSettings->setLayout(sliceSettingsLayout);
128
128
129 QGridLayout* baseLayout = new QGridLayout();
129 QGridLayout* baseLayout = new QGridLayout();
130 baseLayout->addWidget(seriesSettings, 0, 0);
130 baseLayout->addWidget(seriesSettings, 0, 0);
131 baseLayout->addWidget(sliceSettings, 1, 0);
131 baseLayout->addWidget(sliceSettings, 1, 0);
132 baseLayout->addWidget(m_chartView, 0, 1, 2, 1);
132 baseLayout->addWidget(m_chartView, 0, 1, 2, 1);
133 setLayout(baseLayout);
133 setLayout(baseLayout);
134
134
135 connect(m_vPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
135 connect(m_vPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
136 connect(m_hPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
136 connect(m_hPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
137 connect(m_sizeFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
137 connect(m_sizeFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
138 connect(m_startAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
138 connect(m_startAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
139 connect(m_endAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
139 connect(m_endAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
140
140
141 connect(m_sliceValue, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
141 connect(m_sliceValue, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
142 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
142 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
143 connect(m_sliceLabelArmFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
143 connect(m_sliceLabelArmFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
144 connect(m_sliceExploded, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
144 connect(m_sliceExploded, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
145 connect(m_sliceExplodedFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
145 connect(m_sliceExplodedFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
146
146
147 connect(m_series, SIGNAL(clicked(QPieSlice*)), this, SLOT(handleSliceClicked(QPieSlice*)));
147 connect(m_series, SIGNAL(clicked(QPieSlice*)), this, SLOT(handleSliceClicked(QPieSlice*)));
148
148
149 updateSerieSettings();
149 updateSerieSettings();
150 }
150 }
151
151
152 public Q_SLOTS:
152 public Q_SLOTS:
153
153
154 void updateSerieSettings()
154 void updateSerieSettings()
155 {
155 {
156 m_series->setPositionFactors(m_vPosition->value(), m_hPosition->value());
156 m_series->setPositionFactors(m_vPosition->value(), m_hPosition->value());
157
157
158 m_series->setSizeFactor(m_sizeFactor->value());
158 m_series->setSizeFactor(m_sizeFactor->value());
159
159
160 m_series->setStartAngle(m_startAngle->value());
160 m_series->setStartAngle(m_startAngle->value());
161 m_series->setEndAngle(m_endAngle->value());
161 m_series->setEndAngle(m_endAngle->value());
162 }
162 }
163
163
164 void updateSliceSettings()
164 void updateSliceSettings()
165 {
165 {
166 if (!m_slice)
166 if (!m_slice)
167 return;
167 return;
168
168
169 m_slice->setValue(m_sliceValue->value());
169 m_slice->setValue(m_sliceValue->value());
170 m_slice->setLabelVisible(m_sliceLabelVisible->isChecked());
170 m_slice->setLabelVisible(m_sliceLabelVisible->isChecked());
171 m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value());
171 m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value());
172 m_slice->setExploded(m_sliceExploded->isChecked());
172 m_slice->setExploded(m_sliceExploded->isChecked());
173 m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value());
173 m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value());
174 }
174 }
175
175
176 void handleSliceClicked(QPieSlice* slice)
176 void handleSliceClicked(QPieSlice* slice)
177 {
177 {
178 m_slice = slice;
178 m_slice = slice;
179 m_sliceName->setText(slice->label());
179 m_sliceName->setText(slice->label());
180
180
181 m_sliceValue->blockSignals(true);
181 m_sliceValue->blockSignals(true);
182 m_sliceValue->setValue(slice->value());
182 m_sliceValue->setValue(slice->value());
183 m_sliceValue->blockSignals(false);
183 m_sliceValue->blockSignals(false);
184
184
185 m_sliceLabelVisible->blockSignals(true);
185 m_sliceLabelVisible->blockSignals(true);
186 m_sliceLabelVisible->setChecked(slice->isLabelVisible());
186 m_sliceLabelVisible->setChecked(slice->isLabelVisible());
187 m_sliceLabelVisible->blockSignals(false);
187 m_sliceLabelVisible->blockSignals(false);
188
188
189 m_sliceLabelArmFactor->blockSignals(true);
189 m_sliceLabelArmFactor->blockSignals(true);
190 m_sliceLabelArmFactor->setValue(slice->labelArmLengthFactor());
190 m_sliceLabelArmFactor->setValue(slice->labelArmLengthFactor());
191 m_sliceLabelArmFactor->blockSignals(false);
191 m_sliceLabelArmFactor->blockSignals(false);
192
192
193 m_sliceExploded->blockSignals(true);
193 m_sliceExploded->blockSignals(true);
194 m_sliceExploded->setChecked(slice->isExploded());
194 m_sliceExploded->setChecked(slice->isExploded());
195 m_sliceExploded->blockSignals(false);
195 m_sliceExploded->blockSignals(false);
196
196
197 m_sliceExplodedFactor->blockSignals(true);
197 m_sliceExplodedFactor->blockSignals(true);
198 m_sliceExplodedFactor->setValue(slice->explodeDistanceFactor());
198 m_sliceExplodedFactor->setValue(slice->explodeDistanceFactor());
199 m_sliceExplodedFactor->blockSignals(false);
199 m_sliceExplodedFactor->blockSignals(false);
200 }
200 }
201
201
202 private:
202 private:
203 QChartView* m_chartView;
203 QChartView* m_chartView;
204 QPieSeries* m_series;
204 QPieSeries* m_series;
205 QPieSlice* m_slice;
205 QPieSlice* m_slice;
206
206
207 QDoubleSpinBox* m_hPosition;
207 QDoubleSpinBox* m_hPosition;
208 QDoubleSpinBox* m_vPosition;
208 QDoubleSpinBox* m_vPosition;
209 QDoubleSpinBox* m_sizeFactor;
209 QDoubleSpinBox* m_sizeFactor;
210 QDoubleSpinBox* m_startAngle;
210 QDoubleSpinBox* m_startAngle;
211 QDoubleSpinBox* m_endAngle;
211 QDoubleSpinBox* m_endAngle;
212
212
213 QLabel* m_sliceName;
213 QLabel* m_sliceName;
214 QDoubleSpinBox* m_sliceValue;
214 QDoubleSpinBox* m_sliceValue;
215 QCheckBox* m_sliceLabelVisible;
215 QCheckBox* m_sliceLabelVisible;
216 QDoubleSpinBox* m_sliceLabelArmFactor;
216 QDoubleSpinBox* m_sliceLabelArmFactor;
217 QCheckBox* m_sliceExploded;
217 QCheckBox* m_sliceExploded;
218 QDoubleSpinBox* m_sliceExplodedFactor;
218 QDoubleSpinBox* m_sliceExplodedFactor;
219 };
219 };
220
220
221 int main(int argc, char *argv[])
221 int main(int argc, char *argv[])
222 {
222 {
223 QApplication a(argc, argv);
223 QApplication a(argc, argv);
224
224
225 QMainWindow window;
225 QMainWindow window;
226
226
227 MainWidget* widget = new MainWidget();
227 MainWidget* widget = new MainWidget();
228
228
229 window.setCentralWidget(widget);
229 window.setCentralWidget(widget);
230 window.resize(900, 600);
230 window.resize(900, 600);
231 window.show();
231 window.show();
232
232
233 return a.exec();
233 return a.exec();
234 }
234 }
235
235
236 #include "main.moc"
236 #include "main.moc"
@@ -1,328 +1,328
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 #include <QTime>
4 #include <QTime>
5
5
6 //series
6 //series
7 #include "qbarset.h"
7 #include "qbarset.h"
8 #include "qbarseries.h"
8 #include "qbarseries.h"
9 #include "qstackedbarseries.h"
9 #include "qstackedbarseries.h"
10 #include "qpercentbarseries.h"
10 #include "qpercentbarseries.h"
11 #include "qlineseries.h"
11 #include "qlineseries.h"
12 #include "qareaseries.h"
12 #include "qareaseries.h"
13 #include "qscatterseries.h"
13 #include "qscatterseries.h"
14 #include "qpieseries.h"
14 #include "qpieseries.h"
15 #include "qpieslice.h"
15 #include "qpieslice.h"
16 #include "qsplineseries.h"
16 #include "qsplineseries.h"
17
17
18 //items
18 //items
19 #include "axisitem_p.h"
19 #include "axisitem_p.h"
20 #include "barpresenter_p.h"
20 #include "barpresenter_p.h"
21 #include "stackedbarpresenter_p.h"
21 #include "stackedbarpresenter_p.h"
22 #include "percentbarpresenter_p.h"
22 #include "percentbarpresenter_p.h"
23 #include "linechartitem_p.h"
23 #include "linechartitem_p.h"
24 #include "areachartitem_p.h"
24 #include "areachartitem_p.h"
25 #include "scatterpresenter_p.h"
25 #include "scatterpresenter_p.h"
26 #include "piepresenter_p.h"
26 #include "piepresenter_p.h"
27 #include "splinechartitem_p.h"
27 #include "splinechartitem_p.h"
28
28
29 //themes
29 //themes
30 #include "chartthemevanilla_p.h"
30 #include "chartthemevanilla_p.h"
31 #include "chartthemeicy_p.h"
31 #include "chartthemeicy_p.h"
32 #include "chartthemegrayscale_p.h"
32 #include "chartthemegrayscale_p.h"
33 #include "chartthemescientific_p.h"
33 #include "chartthemescientific_p.h"
34
34
35
35
36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37
37
38 /* TODO
38 /* TODO
39 case QChart::ChartThemeUnnamed1:
39 case QChart::ChartThemeUnnamed1:
40 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
40 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff3fa9f5)), 2));
41 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
41 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xff7AC943)), 2));
42 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
42 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF931E)), 2));
43 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
43 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF1D25)), 2));
44 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
44 m_seriesThemes.append(SeriesTheme(QColor(QRgb(0xffFF7BAC)), 2));
45
45
46 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
46 m_gradientStartColor = QColor(QRgb(0xfff3dc9e));
47 m_gradientEndColor = QColor(QRgb(0xffafafaf));
47 m_gradientEndColor = QColor(QRgb(0xffafafaf));
48 */
48 */
49
49
50 ChartTheme::ChartTheme(QChart::ChartTheme id)
50 ChartTheme::ChartTheme(QChart::ChartTheme id)
51 {
51 {
52 m_id = id;
52 m_id = id;
53 m_seriesColor.append(QRgb(0xff000000));
53 m_seriesColor.append(QRgb(0xff000000));
54 m_seriesColor.append(QRgb(0xff707070));
54 m_seriesColor.append(QRgb(0xff707070));
55 m_gradientStartColor = QColor(QRgb(0xffffffff));
55 m_gradientStartColor = QColor(QRgb(0xffffffff));
56 m_gradientEndColor = QColor(QRgb(0xffafafaf));
56 m_gradientEndColor = QColor(QRgb(0xffafafaf));
57
57
58 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
58 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
59 }
59 }
60
60
61
61
62 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
62 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
63 {
63 {
64 switch(theme) {
64 switch(theme) {
65 case QChart::ChartThemeDefault:
65 case QChart::ChartThemeDefault:
66 return new ChartTheme();
66 return new ChartTheme();
67 case QChart::ChartThemeVanilla:
67 case QChart::ChartThemeVanilla:
68 return new ChartThemeVanilla();
68 return new ChartThemeVanilla();
69 case QChart::ChartThemeIcy:
69 case QChart::ChartThemeIcy:
70 return new ChartThemeIcy();
70 return new ChartThemeIcy();
71 case QChart::ChartThemeGrayscale:
71 case QChart::ChartThemeGrayscale:
72 return new ChartThemeGrayscale();
72 return new ChartThemeGrayscale();
73 case QChart::ChartThemeScientific:
73 case QChart::ChartThemeScientific:
74 return new ChartThemeScientific();
74 return new ChartThemeScientific();
75 }
75 }
76 }
76 }
77
77
78 void ChartTheme::decorate(QChart* chart)
78 void ChartTheme::decorate(QChart* chart)
79 {
79 {
80 QLinearGradient backgroundGradient;
80 QLinearGradient backgroundGradient;
81 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
81 backgroundGradient.setColorAt(0.0, m_gradientStartColor);
82 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
82 backgroundGradient.setColorAt(1.0, m_gradientEndColor);
83 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
83 backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
84 chart->setChartBackgroundBrush(backgroundGradient);
84 chart->setChartBackgroundBrush(backgroundGradient);
85 }
85 }
86 //TODO helper to by removed later
86 //TODO helper to by removed later
87 void ChartTheme::decorate(ChartItem* item, QSeries* series,int count)
87 void ChartTheme::decorate(ChartItem* item, QSeries* series,int count)
88 {
88 {
89 switch(series->type())
89 switch(series->type())
90 {
90 {
91 case QSeries::SeriesTypeLine: {
91 case QSeries::SeriesTypeLine: {
92 QLineSeries* s = static_cast<QLineSeries*>(series);
92 QLineSeries* s = static_cast<QLineSeries*>(series);
93 LineChartItem* i = static_cast<LineChartItem*>(item);
93 LineChartItem* i = static_cast<LineChartItem*>(item);
94 decorate(i,s,count);
94 decorate(i,s,count);
95 break;
95 break;
96 }
96 }
97 case QSeries::SeriesTypeArea: {
97 case QSeries::SeriesTypeArea: {
98 QAreaSeries* s = static_cast<QAreaSeries*>(series);
98 QAreaSeries* s = static_cast<QAreaSeries*>(series);
99 AreaChartItem* i = static_cast<AreaChartItem*>(item);
99 AreaChartItem* i = static_cast<AreaChartItem*>(item);
100 decorate(i,s,count);
100 decorate(i,s,count);
101 break;
101 break;
102 }
102 }
103 case QSeries::SeriesTypeBar: {
103 case QSeries::SeriesTypeBar: {
104 QBarSeries* b = static_cast<QBarSeries*>(series);
104 QBarSeries* b = static_cast<QBarSeries*>(series);
105 BarPresenter* i = static_cast<BarPresenter*>(item);
105 BarPresenter* i = static_cast<BarPresenter*>(item);
106 decorate(i,b,count);
106 decorate(i,b,count);
107 break;
107 break;
108 }
108 }
109 case QSeries::SeriesTypeStackedBar: {
109 case QSeries::SeriesTypeStackedBar: {
110 QStackedBarSeries* s = static_cast<QStackedBarSeries*>(series);
110 QStackedBarSeries* s = static_cast<QStackedBarSeries*>(series);
111 StackedBarPresenter* i = static_cast<StackedBarPresenter*>(item);
111 StackedBarPresenter* i = static_cast<StackedBarPresenter*>(item);
112 decorate(i,s,count);
112 decorate(i,s,count);
113 break;
113 break;
114 }
114 }
115 case QSeries::SeriesTypePercentBar: {
115 case QSeries::SeriesTypePercentBar: {
116 QPercentBarSeries* s = static_cast<QPercentBarSeries*>(series);
116 QPercentBarSeries* s = static_cast<QPercentBarSeries*>(series);
117 PercentBarPresenter* i = static_cast<PercentBarPresenter*>(item);
117 PercentBarPresenter* i = static_cast<PercentBarPresenter*>(item);
118 decorate(i,s,count);
118 decorate(i,s,count);
119 break;
119 break;
120 }
120 }
121 case QSeries::SeriesTypeScatter: {
121 case QSeries::SeriesTypeScatter: {
122 QScatterSeries* s = qobject_cast<QScatterSeries*>(series);
122 QScatterSeries* s = qobject_cast<QScatterSeries*>(series);
123 Q_ASSERT(s);
123 Q_ASSERT(s);
124 ScatterPresenter* i = static_cast<ScatterPresenter*>(item);
124 ScatterPresenter* i = static_cast<ScatterPresenter*>(item);
125 Q_ASSERT(i);
125 Q_ASSERT(i);
126 decorate(i, s, count);
126 decorate(i, s, count);
127 break;
127 break;
128 }
128 }
129 case QSeries::SeriesTypePie: {
129 case QSeries::SeriesTypePie: {
130 QPieSeries* s = static_cast<QPieSeries*>(series);
130 QPieSeries* s = static_cast<QPieSeries*>(series);
131 PiePresenter* i = static_cast<PiePresenter*>(item);
131 PiePresenter* i = static_cast<PiePresenter*>(item);
132 decorate(i,s,count);
132 decorate(i,s,count);
133 break;
133 break;
134 }
134 }
135 default:
135 default:
136 qDebug()<<"Wrong item to be decorated by theme";
136 qDebug()<<"Wrong item to be decorated by theme";
137 break;
137 break;
138 }
138 }
139
139
140 }
140 }
141
141
142 void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series,int count)
142 void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series,int count)
143 {
143 {
144 QPen pen;
144 QPen pen;
145 QBrush brush;
145 QBrush brush;
146
146
147 if(pen != series->pen()){
147 if(pen != series->pen()){
148 item->setPen(series->pen());
148 item->setPen(series->pen());
149 }else{
149 }else{
150 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
150 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
151 pen.setWidthF(2);
151 pen.setWidthF(2);
152 item->setPen(pen);
152 item->setPen(pen);
153 }
153 }
154
154
155 if(brush != series->brush()){
155 if(brush != series->brush()){
156 item->setBrush(series->brush());
156 item->setBrush(series->brush());
157 }else{
157 }else{
158 QBrush brush(m_seriesColor.at(count%m_seriesColor.size()));
158 QBrush brush(m_seriesColor.at(count%m_seriesColor.size()));
159 item->setBrush(brush);
159 item->setBrush(brush);
160 }
160 }
161 }
161 }
162
162
163
163
164 void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int count)
164 void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int count)
165 {
165 {
166 QPen pen;
166 QPen pen;
167 if(pen != series->pen()){
167 if(pen != series->pen()){
168 item->setPen(series->pen());
168 item->setPen(series->pen());
169 return;
169 return;
170 }
170 }
171 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
171 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
172 pen.setWidthF(2);
172 pen.setWidthF(2);
173 item->setPen(pen);
173 item->setPen(pen);
174 }
174 }
175
175
176 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int count)
176 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int count)
177 {
177 {
178 QList<QBarSet*> sets = series->barSets();
178 QList<QBarSet*> sets = series->barSets();
179 for (int i=0; i<series->barsetCount(); i++) {
179 for (int i=0; i<series->barsetCount(); i++) {
180 sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
180 sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
181 }
181 }
182 }
182 }
183
183
184 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int count)
184 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int count)
185 {
185 {
186 QList<QBarSet*> sets = series->barSets();
186 QList<QBarSet*> sets = series->barSets();
187 for (int i=0; i<series->barsetCount(); i++) {
187 for (int i=0; i<series->barsetCount(); i++) {
188 sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
188 sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
189 }
189 }
190 }
190 }
191
191
192 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count)
192 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int count)
193 {
193 {
194 QList<QBarSet*> sets = series->barSets();
194 QList<QBarSet*> sets = series->barSets();
195 for (int i=0; i<series->barsetCount(); i++) {
195 for (int i=0; i<series->barsetCount(); i++) {
196 sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
196 sets.at(i)->setBrush(QBrush(m_seriesColor.at(i%m_seriesColor.count())));
197 }
197 }
198 }
198 }
199
199
200 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count)
200 void ChartTheme::decorate(ScatterPresenter* presenter, QScatterSeries* series, int count)
201 {
201 {
202 Q_ASSERT(presenter);
202 Q_ASSERT(presenter);
203 Q_ASSERT(series);
203 Q_ASSERT(series);
204
204
205 QColor color = m_seriesColor.at(count % m_seriesColor.size());
205 QColor color = m_seriesColor.at(count % m_seriesColor.size());
206 // TODO: define alpha in the theme? or in the series?
206 // TODO: define alpha in the theme? or in the series?
207 //color.setAlpha(120);
207 //color.setAlpha(120);
208
208
209 QBrush brush(color, Qt::SolidPattern);
209 QBrush brush(color, Qt::SolidPattern);
210 presenter->m_markerBrush = brush;
210 presenter->m_markerBrush = brush;
211
211
212 QPen pen(brush, 3);
212 QPen pen(brush, 3);
213 pen.setColor(color);
213 pen.setColor(color);
214 presenter->m_markerPen = pen;
214 presenter->m_markerPen = pen;
215 }
215 }
216
216
217 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
217 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int /*count*/)
218 {
218 {
219 // create a list of slice colors based on current theme
219 // create a list of slice colors based on current theme
220 int i = 0;
220 int i = 0;
221 QList<QColor> colors;
221 QList<QColor> colors;
222 while (colors.count() < series->count()) {
222 while (colors.count() < series->count()) {
223
223
224 // get base color
224 // get base color
225 QColor c = m_seriesColor[i++];
225 QColor c = m_seriesColor[i++];
226 i = i % m_seriesColor.count();
226 i = i % m_seriesColor.count();
227
227
228 // dont use black colors... looks bad
228 // dont use black colors... looks bad
229 if (c == Qt::black)
229 if (c == Qt::black)
230 continue;
230 continue;
231
231
232 // by default use the "raw" theme color
232 // by default use the "raw" theme color
233 if (!colors.contains(c)) {
233 if (!colors.contains(c)) {
234 colors << c;
234 colors << c;
235 continue;
235 continue;
236 }
236 }
237 // ...ok we need to generate something that looks like the same color
237 // ...ok we need to generate something that looks like the same color
238 // but different lightness
238 // but different lightness
239
239
240 int tryCount = 0;
240 int tryCount = 0;
241 while (tryCount++ < 100) {
241 while (tryCount++ < 100) {
242
242
243 // find maximum value we can raise the lightness
243 // find maximum value we can raise the lightness
244 int lMax = 255;
244 int lMax = 255;
245 if (lMax > 255 - c.red())
245 if (lMax > 255 - c.red())
246 lMax = 255 - c.red();
246 lMax = 255 - c.red();
247 if (lMax > 255 - c.green())
247 if (lMax > 255 - c.green())
248 lMax = 255 - c.green();
248 lMax = 255 - c.green();
249 if (lMax > 255 - c.blue())
249 if (lMax > 255 - c.blue())
250 lMax = 255 - c.blue();
250 lMax = 255 - c.blue();
251
251
252 // find maximum value we can make it darker
252 // find maximum value we can make it darker
253 int dMax = 255;
253 int dMax = 255;
254 if (dMax > c.red())
254 if (dMax > c.red())
255 dMax = c.red();
255 dMax = c.red();
256 if (dMax > c.green())
256 if (dMax > c.green())
257 dMax = c.green();
257 dMax = c.green();
258 if (dMax > c.blue())
258 if (dMax > c.blue())
259 dMax = c.blue();
259 dMax = c.blue();
260
260
261 int max = dMax + lMax;
261 int max = dMax + lMax;
262 if (max == 0) {
262 if (max == 0) {
263 // no room to make color lighter or darker...
263 // no room to make color lighter or darker...
264 qDebug() << "cannot generate a color for pie!";
264 qDebug() << "cannot generate a color for pie!";
265 break;
265 break;
266 }
266 }
267
267
268 // generate random color
268 // generate random color
269 int r = c.red() - dMax;
269 int r = c.red() - dMax;
270 int g = c.green() - dMax;
270 int g = c.green() - dMax;
271 int b = c.blue() - dMax;
271 int b = c.blue() - dMax;
272 int d = qrand() % max;
272 int d = qrand() % max;
273 c.setRgb(r+d, g+d, b+d);
273 c.setRgb(r+d, g+d, b+d);
274
274
275 // found a unique color?
275 // found a unique color?
276 if (!colors.contains(c))
276 if (!colors.contains(c))
277 break;
277 break;
278 }
278 }
279
279
280 qDebug() << "generated a color for pie" << c;
280 qDebug() << "generated a color for pie" << c;
281 colors << c;
281 colors << c;
282 }
282 }
283
283
284 // finally update colors
284 // finally update colors
285 foreach (QPieSlice* s, series->slices()) {
285 foreach (QPieSlice* s, series->slices()) {
286 QColor c = colors.takeFirst();
286 QColor c = colors.takeFirst();
287 s->setPen(c);
287 s->setSlicePen(c);
288 s->setBrush(c);
288 s->setSliceBrush(c);
289 }
289 }
290 }
290 }
291
291
292
292
293 void ChartTheme::decorate(QChartAxis* axis,AxisItem* item)
293 void ChartTheme::decorate(QChartAxis* axis,AxisItem* item)
294 {
294 {
295 //TODO: dummy defults for now
295 //TODO: dummy defults for now
296 axis->setLabelsBrush(Qt::black);
296 axis->setLabelsBrush(Qt::black);
297 axis->setLabelsPen(Qt::NoPen);
297 axis->setLabelsPen(Qt::NoPen);
298 axis->setShadesPen(Qt::NoPen);
298 axis->setShadesPen(Qt::NoPen);
299 axis->setShadesOpacity(0.5);
299 axis->setShadesOpacity(0.5);
300 }
300 }
301
301
302 void ChartTheme::decorate(SplineChartItem* item, QSplineSeries* series, int count)
302 void ChartTheme::decorate(SplineChartItem* item, QSplineSeries* series, int count)
303 {
303 {
304 Q_ASSERT(item);
304 Q_ASSERT(item);
305 Q_ASSERT(series);
305 Q_ASSERT(series);
306
306
307 QPen pen;
307 QPen pen;
308 if(pen != series->pen()){
308 if(pen != series->pen()){
309 item->setPen(series->pen());
309 item->setPen(series->pen());
310 return;
310 return;
311 }
311 }
312 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
312 pen.setColor(m_seriesColor.at(count%m_seriesColor.size()));
313 pen.setWidthF(series->pen().widthF());
313 pen.setWidthF(series->pen().widthF());
314 item->setPen(pen);
314 item->setPen(pen);
315
315
316 // QColor color = m_seriesColor.at(count % m_seriesColor.size());
316 // QColor color = m_seriesColor.at(count % m_seriesColor.size());
317 // TODO: define alpha in the theme? or in the series?
317 // TODO: define alpha in the theme? or in the series?
318 //color.setAlpha(120);
318 //color.setAlpha(120);
319
319
320 // QBrush brush(color, Qt::SolidPattern);
320 // QBrush brush(color, Qt::SolidPattern);
321 // presenter->m_markerBrush = brush;
321 // presenter->m_markerBrush = brush;
322
322
323 // QPen pen(brush, 3);
323 // QPen pen(brush, 3);
324 // pen.setColor(color);
324 // pen.setColor(color);
325 // presenter->m_markerPen = pen;
325 // presenter->m_markerPen = pen;
326 }
326 }
327
327
328 QTCOMMERCIALCHART_END_NAMESPACE
328 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,208 +1,208
1 #include "pieslice_p.h"
1 #include "pieslice_p.h"
2 #include "piepresenter_p.h"
2 #include "piepresenter_p.h"
3 #include "qpieseries.h"
3 #include "qpieseries.h"
4 #include "qpieslice.h"
4 #include "qpieslice.h"
5 #include <QPainter>
5 #include <QPainter>
6 #include <QDebug>
6 #include <QDebug>
7 #include <qmath.h>
7 #include <qmath.h>
8 #include <QGraphicsSceneEvent>
8 #include <QGraphicsSceneEvent>
9 #include <QTime>
9 #include <QTime>
10
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
12
13 #define PI 3.14159265 // TODO: is this defined in some header?
13 #define PI 3.14159265 // TODO: is this defined in some header?
14
14
15 QPointF offset(qreal angle, qreal length)
15 QPointF offset(qreal angle, qreal length)
16 {
16 {
17 qreal dx = qSin(angle*(PI/180)) * length;
17 qreal dx = qSin(angle*(PI/180)) * length;
18 qreal dy = qCos(angle*(PI/180)) * length;
18 qreal dy = qCos(angle*(PI/180)) * length;
19 return QPointF(dx, -dy);
19 return QPointF(dx, -dy);
20 }
20 }
21
21
22 PieSlice::PieSlice(QGraphicsItem* parent)
22 PieSlice::PieSlice(QGraphicsItem* parent)
23 :QGraphicsObject(parent),
23 :QGraphicsObject(parent),
24 m_pieRadius(0),
24 m_pieRadius(0),
25 m_startAngle(0),
25 m_startAngle(0),
26 m_angleSpan(0),
26 m_angleSpan(0),
27 m_isExploded(false),
27 m_isExploded(false),
28 m_explodeDistanceFactor(0),
28 m_explodeDistanceFactor(0),
29 m_labelVisible(false),
29 m_labelVisible(false),
30 m_labelArmLengthFactor(0)
30 m_labelArmLengthFactor(0)
31 {
31 {
32 setAcceptHoverEvents(true);
32 setAcceptHoverEvents(true);
33 setAcceptedMouseButtons(Qt::LeftButton);
33 setAcceptedMouseButtons(Qt::LeftButton);
34 }
34 }
35
35
36 PieSlice::~PieSlice()
36 PieSlice::~PieSlice()
37 {
37 {
38
38
39 }
39 }
40
40
41 QRectF PieSlice::boundingRect() const
41 QRectF PieSlice::boundingRect() const
42 {
42 {
43 return m_slicePath.boundingRect();
43 return m_slicePath.boundingRect();
44 }
44 }
45
45
46 QPainterPath PieSlice::shape() const
46 QPainterPath PieSlice::shape() const
47 {
47 {
48 // Don't include the label and label arm.
48 // Don't include the label and label arm.
49 // This is used to detect a mouse clicks. We do not want clicks from label.
49 // This is used to detect a mouse clicks. We do not want clicks from label.
50 return m_slicePath;
50 return m_slicePath;
51 }
51 }
52
52
53 void PieSlice::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
53 void PieSlice::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
54 {
54 {
55 painter->setClipRect(parentItem()->boundingRect());
55 painter->setClipRect(parentItem()->boundingRect());
56
56
57 painter->save();
57 painter->save();
58 painter->setPen(m_pen);
58 painter->setPen(m_slicePen);
59 painter->setBrush(m_brush);
59 painter->setBrush(m_sliceBrush);
60 painter->drawPath(m_slicePath);
60 painter->drawPath(m_slicePath);
61 painter->restore();
61 painter->restore();
62
62
63 if (m_labelVisible) {
63 if (m_labelVisible) {
64 painter->save();
64 painter->save();
65 painter->setPen(m_labelArmPen);
65 painter->setPen(m_labelArmPen);
66 painter->drawPath(m_labelArmPath);
66 painter->drawPath(m_labelArmPath);
67 painter->restore();
67 painter->restore();
68
68
69 painter->setFont(m_labelFont);
69 painter->setFont(m_labelFont);
70 painter->drawText(m_labelTextRect.bottomLeft(), m_labelText);
70 painter->drawText(m_labelTextRect.bottomLeft(), m_labelText);
71 }
71 }
72 }
72 }
73
73
74 void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
74 void PieSlice::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
75 {
75 {
76 emit hoverEnter();
76 emit hoverEnter();
77 }
77 }
78
78
79 void PieSlice::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
79 void PieSlice::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
80 {
80 {
81 emit hoverLeave();
81 emit hoverLeave();
82 }
82 }
83
83
84 void PieSlice::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/)
84 void PieSlice::mousePressEvent(QGraphicsSceneMouseEvent* /*event*/)
85 {
85 {
86 emit clicked();
86 emit clicked();
87 }
87 }
88
88
89 void PieSlice::setPieCenterAndRadius(QPointF center, qreal radius)
89 void PieSlice::setPieCenterAndRadius(QPointF center, qreal radius)
90 {
90 {
91 m_pieCenter = center;
91 m_pieCenter = center;
92 m_pieRadius = radius;
92 m_pieRadius = radius;
93 }
93 }
94
94
95 void PieSlice::updateGeometry()
95 void PieSlice::updateGeometry()
96 {
96 {
97 if (m_pieRadius <= 0)
97 if (m_pieRadius <= 0)
98 return;
98 return;
99
99
100 prepareGeometryChange();
100 prepareGeometryChange();
101
101
102 // update slice path
102 // update slice path
103 qreal centerAngle;
103 qreal centerAngle;
104 QPointF armStart;
104 QPointF armStart;
105 m_slicePath = slicePath(m_pieCenter, m_pieRadius, m_startAngle, m_angleSpan, m_isExploded, m_pieRadius * m_explodeDistanceFactor, &centerAngle, &armStart);
105 m_slicePath = slicePath(m_pieCenter, m_pieRadius, m_startAngle, m_angleSpan, m_isExploded, m_pieRadius * m_explodeDistanceFactor, &centerAngle, &armStart);
106
106
107 // update text rect
107 // update text rect
108 m_labelTextRect = labelTextRect(m_labelFont, m_labelText);
108 m_labelTextRect = labelTextRect(m_labelFont, m_labelText);
109
109
110 // update label arm path
110 // update label arm path
111 QPointF labelTextStart;
111 QPointF labelTextStart;
112 m_labelArmPath = labelArmPath(armStart, centerAngle, m_pieRadius * m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart);
112 m_labelArmPath = labelArmPath(armStart, centerAngle, m_pieRadius * m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart);
113
113
114 // update text position
114 // update text position
115 m_labelTextRect.moveBottomLeft(labelTextStart);
115 m_labelTextRect.moveBottomLeft(labelTextStart);
116
116
117 //qDebug() << "PieSlice::updateGeometry" << m_labelText << boundingRect() << m_startAngle << m_startAngle + m_angleSpan;
117 //qDebug() << "PieSlice::updateGeometry" << m_labelText << boundingRect() << m_startAngle << m_startAngle + m_angleSpan;
118 }
118 }
119
119
120 void PieSlice::updateData(const QPieSlice* sliceData)
120 void PieSlice::updateData(const QPieSlice* sliceData)
121 {
121 {
122 // TODO: compare what has changes to avoid unneccesary geometry updates
122 // TODO: compare what has changes to avoid unneccesary geometry updates
123
123
124 m_startAngle = sliceData->startAngle();
124 m_startAngle = sliceData->startAngle();
125 m_angleSpan = sliceData->m_angleSpan;
125 m_angleSpan = sliceData->m_angleSpan;
126 m_isExploded = sliceData->isExploded();
126 m_isExploded = sliceData->isExploded();
127 m_explodeDistanceFactor = sliceData->explodeDistanceFactor();
127 m_explodeDistanceFactor = sliceData->explodeDistanceFactor();
128 m_pen = sliceData->pen();
128 m_slicePen = sliceData->slicePen();
129 m_brush = sliceData->brush();
129 m_sliceBrush = sliceData->sliceBrush();
130
130
131 m_labelVisible = sliceData->isLabelVisible();
131 m_labelVisible = sliceData->isLabelVisible();
132 m_labelText = sliceData->label();
132 m_labelText = sliceData->label();
133 m_labelFont = sliceData->labelFont();
133 m_labelFont = sliceData->labelFont();
134 m_labelArmLengthFactor = sliceData->labelArmLengthFactor();
134 m_labelArmLengthFactor = sliceData->labelArmLengthFactor();
135 m_labelArmPen = sliceData->labelPen();
135 m_labelArmPen = sliceData->labelArmPen();
136
136
137 updateGeometry();
137 updateGeometry();
138 update();
138 update();
139 }
139 }
140
140
141 QPainterPath PieSlice::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, bool exploded, qreal explodeDistance, qreal* centerAngle, QPointF* armStart)
141 QPainterPath PieSlice::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, bool exploded, qreal explodeDistance, qreal* centerAngle, QPointF* armStart)
142 {
142 {
143 // calculate center angle
143 // calculate center angle
144 *centerAngle = startAngle + (angleSpan/2);
144 *centerAngle = startAngle + (angleSpan/2);
145
145
146 // calculate slice rectangle
146 // calculate slice rectangle
147 QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2);
147 QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2);
148
148
149 // adjust rect for exploding
149 // adjust rect for exploding
150 if (exploded) {
150 if (exploded) {
151 qreal dx = qSin(*centerAngle*(PI/180)) * explodeDistance;
151 qreal dx = qSin(*centerAngle*(PI/180)) * explodeDistance;
152 qreal dy = -qCos(*centerAngle*(PI/180)) * explodeDistance;
152 qreal dy = -qCos(*centerAngle*(PI/180)) * explodeDistance;
153 rect.translate(dx, dy);
153 rect.translate(dx, dy);
154 }
154 }
155
155
156 // slice path
156 // slice path
157 // TODO: draw the shape so that it might have a hole in the center
157 // TODO: draw the shape so that it might have a hole in the center
158 QPainterPath path;
158 QPainterPath path;
159 path.moveTo(rect.center());
159 path.moveTo(rect.center());
160 path.arcTo(rect, -startAngle + 90, -angleSpan);
160 path.arcTo(rect, -startAngle + 90, -angleSpan);
161 path.closeSubpath();
161 path.closeSubpath();
162
162
163 // calculate label arm start point
163 // calculate label arm start point
164 *armStart = center;
164 *armStart = center;
165 if (exploded)
165 if (exploded)
166 *armStart += offset(*centerAngle, explodeDistance + radius + PIESLICE_LABEL_GAP);
166 *armStart += offset(*centerAngle, explodeDistance + radius + PIESLICE_LABEL_GAP);
167 else
167 else
168 *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP);
168 *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP);
169
169
170 return path;
170 return path;
171 }
171 }
172
172
173 QPainterPath PieSlice::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF* textStart)
173 QPainterPath PieSlice::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF* textStart)
174 {
174 {
175 qreal dx = qSin(angle*(PI/180)) * length;
175 qreal dx = qSin(angle*(PI/180)) * length;
176 qreal dy = -qCos(angle*(PI/180)) * length;
176 qreal dy = -qCos(angle*(PI/180)) * length;
177 QPointF parm1 = start + QPointF(dx, dy);
177 QPointF parm1 = start + QPointF(dx, dy);
178
178
179 QPointF parm2 = parm1;
179 QPointF parm2 = parm1;
180 if (angle < 180) { // arm swings the other way on the left side
180 if (angle < 180) { // arm swings the other way on the left side
181 parm2 += QPointF(textWidth, 0);
181 parm2 += QPointF(textWidth, 0);
182 *textStart = parm1;
182 *textStart = parm1;
183 }
183 }
184 else {
184 else {
185 parm2 += QPointF(-textWidth,0);
185 parm2 += QPointF(-textWidth,0);
186 *textStart = parm2;
186 *textStart = parm2;
187 }
187 }
188
188
189 // elevate the text position a bit so that it does not hit the line
189 // elevate the text position a bit so that it does not hit the line
190 *textStart += QPointF(0, -5);
190 *textStart += QPointF(0, -5);
191
191
192 QPainterPath path;
192 QPainterPath path;
193 path.moveTo(start);
193 path.moveTo(start);
194 path.lineTo(parm1);
194 path.lineTo(parm1);
195 path.lineTo(parm2);
195 path.lineTo(parm2);
196
196
197 return path;
197 return path;
198 }
198 }
199
199
200 QRectF PieSlice::labelTextRect(QFont font, QString text)
200 QRectF PieSlice::labelTextRect(QFont font, QString text)
201 {
201 {
202 QFontMetricsF fm(font);
202 QFontMetricsF fm(font);
203 return fm.boundingRect(text);
203 return fm.boundingRect(text);
204 }
204 }
205
205
206 #include "moc_pieslice_p.cpp"
206 #include "moc_pieslice_p.cpp"
207
207
208 QTCOMMERCIALCHART_END_NAMESPACE
208 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,74 +1,74
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 #define PIESLICE_LABEL_GAP 5
12 #define PIESLICE_LABEL_GAP 5
13
13
14 QTCOMMERCIALCHART_BEGIN_NAMESPACE
14 QTCOMMERCIALCHART_BEGIN_NAMESPACE
15 class PiePresenter;
15 class PiePresenter;
16 class PieSliceLabel;
16 class PieSliceLabel;
17 class QPieSlice;
17 class QPieSlice;
18
18
19 class PieSlice : public QGraphicsObject
19 class PieSlice : public QGraphicsObject
20 {
20 {
21 Q_OBJECT
21 Q_OBJECT
22
22
23 public:
23 public:
24 PieSlice(QGraphicsItem* parent = 0);
24 PieSlice(QGraphicsItem* parent = 0);
25 ~PieSlice();
25 ~PieSlice();
26
26
27 public: // from QGraphicsItem
27 public: // from QGraphicsItem
28 QRectF boundingRect() const;
28 QRectF boundingRect() const;
29 QPainterPath shape() const;
29 QPainterPath shape() const;
30 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
30 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
31 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
31 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
32 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
32 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
33 void mousePressEvent(QGraphicsSceneMouseEvent *event);
33 void mousePressEvent(QGraphicsSceneMouseEvent *event);
34
34
35 Q_SIGNALS:
35 Q_SIGNALS:
36 void clicked();
36 void clicked();
37 void hoverEnter();
37 void hoverEnter();
38 void hoverLeave();
38 void hoverLeave();
39
39
40 public Q_SLOTS:
40 public Q_SLOTS:
41 void setPieCenterAndRadius(QPointF center, qreal radius);
41 void setPieCenterAndRadius(QPointF center, qreal radius);
42 void updateGeometry();
42 void updateGeometry();
43 void updateData(const QPieSlice *sliceData);
43 void updateData(const QPieSlice *sliceData);
44
44
45 public:
45 public:
46 static QPainterPath slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, bool exploded, qreal explodeDistance, qreal* centerAngle, QPointF* armStart);
46 static QPainterPath slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, bool exploded, qreal explodeDistance, qreal* centerAngle, QPointF* armStart);
47 static QPainterPath labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF* textStart);
47 static QPainterPath labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF* textStart);
48 static QRectF labelTextRect(QFont font, QString text);
48 static QRectF labelTextRect(QFont font, QString text);
49
49
50 private:
50 private:
51 QPointF m_pieCenter;
51 QPointF m_pieCenter;
52 qreal m_pieRadius;
52 qreal m_pieRadius;
53
53
54 QPainterPath m_slicePath;
54 QPainterPath m_slicePath;
55 qreal m_startAngle;
55 qreal m_startAngle;
56 qreal m_angleSpan;
56 qreal m_angleSpan;
57 bool m_isExploded;
57 bool m_isExploded;
58 qreal m_explodeDistanceFactor;
58 qreal m_explodeDistanceFactor;
59 bool m_labelVisible;
59 bool m_labelVisible;
60 QPen m_pen;
60 QPen m_slicePen;
61 QBrush m_brush;
61 QBrush m_sliceBrush;
62
62
63 QPainterPath m_labelArmPath;
63 QPainterPath m_labelArmPath;
64 qreal m_labelArmLengthFactor;
64 qreal m_labelArmLengthFactor;
65 QPen m_labelArmPen;
65 QPen m_labelArmPen;
66
66
67 QRectF m_labelTextRect;
67 QRectF m_labelTextRect;
68 QFont m_labelFont;
68 QFont m_labelFont;
69 QString m_labelText;
69 QString m_labelText;
70 };
70 };
71
71
72 QTCOMMERCIALCHART_END_NAMESPACE
72 QTCOMMERCIALCHART_END_NAMESPACE
73
73
74 #endif // PIESLICE_H
74 #endif // PIESLICE_H
@@ -1,401 +1,401
1 #include "qpieslice.h"
1 #include "qpieslice.h"
2
2
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
3 QTCOMMERCIALCHART_BEGIN_NAMESPACE
4
4
5 #define DEFAULT_PEN_COLOR Qt::black
5 #define DEFAULT_PEN_COLOR Qt::black
6 #define DEFAULT_BRUSH_COLOR Qt::white
6 #define DEFAULT_BRUSH_COLOR Qt::white
7 #define DEFAULT_LABEL_ARM_LENGTH_FACTOR 0.15
7 #define DEFAULT_LABEL_ARM_LENGTH_FACTOR 0.15
8 #define DEFAULT_EXPOLODE_DISTANCE_FACTOR 0.15
8 #define DEFAULT_EXPOLODE_DISTANCE_FACTOR 0.15
9
9
10 /*!
10 /*!
11 \class QPieSlice
11 \class QPieSlice
12 \brief Defines a slice in pie series.
12 \brief Defines a slice in pie series.
13
13
14 Holds all the data of a single slice in a QPieSeries and provides the means
14 Holds all the data of a single slice in a QPieSeries and provides the means
15 to modify slice data and customize the visual appearance of the slice.
15 to modify slice data and customize the visual appearance of the slice.
16
16
17 It also provides the means to customize user interaction with the slice by
17 It also provides the means to customize user interaction with the slice by
18 providing signals for clicking and hover events.
18 providing signals for clicking and hover events.
19 */
19 */
20
20
21 /*!
21 /*!
22 \property QPieSlice::label
22 \property QPieSlice::label
23
23
24 Label of the slice.
24 Label of the slice.
25 */
25 */
26
26
27 /*!
27 /*!
28 \property QPieSlice::value
28 \property QPieSlice::value
29
29
30 Value of the slice.
30 Value of the slice.
31 */
31 */
32
32
33 /*!
33 /*!
34 Constructs an empty slice with a \a parent.
34 Constructs an empty slice with a \a parent.
35
35
36 Note that QPieSeries takes ownership of the slice when it is set/added.
36 Note that QPieSeries takes ownership of the slice when it is set/added.
37
37
38 \sa QPieSeries::replace(), QPieSeries::add()
38 \sa QPieSeries::replace(), QPieSeries::add()
39 */
39 */
40 QPieSlice::QPieSlice(QObject *parent)
40 QPieSlice::QPieSlice(QObject *parent)
41 :QObject(parent),
41 :QObject(parent),
42 m_value(0),
42 m_value(0),
43 m_isLabelVisible(false),
43 m_isLabelVisible(false),
44 m_isExploded(false),
44 m_isExploded(false),
45 m_explodeDistanceFactor(DEFAULT_EXPOLODE_DISTANCE_FACTOR),
45 m_explodeDistanceFactor(DEFAULT_EXPOLODE_DISTANCE_FACTOR),
46 m_percentage(0),
46 m_percentage(0),
47 m_startAngle(0),
47 m_startAngle(0),
48 m_angleSpan(0),
48 m_angleSpan(0),
49 m_pen(DEFAULT_PEN_COLOR),
49 m_slicePen(DEFAULT_PEN_COLOR),
50 m_brush(DEFAULT_BRUSH_COLOR),
50 m_sliceBrush(DEFAULT_BRUSH_COLOR),
51 m_labelPen(DEFAULT_PEN_COLOR),
51 m_labelArmPen(DEFAULT_PEN_COLOR),
52 m_labelArmLengthFactor(DEFAULT_LABEL_ARM_LENGTH_FACTOR)
52 m_labelArmLengthFactor(DEFAULT_LABEL_ARM_LENGTH_FACTOR)
53 {
53 {
54
54
55 }
55 }
56
56
57 /*!
57 /*!
58 Constructs an empty slice with given \a value, \a label and a \a parent.
58 Constructs an empty slice with given \a value, \a label and a \a parent.
59 Note that QPieSeries takes ownership of the slice when it is set/added.
59 Note that QPieSeries takes ownership of the slice when it is set/added.
60 \sa QPieSeries::replace(), QPieSeries::add()
60 \sa QPieSeries::replace(), QPieSeries::add()
61 */
61 */
62 QPieSlice::QPieSlice(qreal value, QString label, QObject *parent)
62 QPieSlice::QPieSlice(qreal value, QString label, QObject *parent)
63 :QObject(parent),
63 :QObject(parent),
64 m_value(value),
64 m_value(value),
65 m_label(label),
65 m_label(label),
66 m_isLabelVisible(false),
66 m_isLabelVisible(false),
67 m_isExploded(false),
67 m_isExploded(false),
68 m_explodeDistanceFactor(DEFAULT_EXPOLODE_DISTANCE_FACTOR),
68 m_explodeDistanceFactor(DEFAULT_EXPOLODE_DISTANCE_FACTOR),
69 m_percentage(0),
69 m_percentage(0),
70 m_startAngle(0),
70 m_startAngle(0),
71 m_angleSpan(0),
71 m_angleSpan(0),
72 m_pen(DEFAULT_PEN_COLOR),
72 m_slicePen(DEFAULT_PEN_COLOR),
73 m_brush(DEFAULT_BRUSH_COLOR),
73 m_sliceBrush(DEFAULT_BRUSH_COLOR),
74 m_labelPen(DEFAULT_PEN_COLOR),
74 m_labelArmPen(DEFAULT_PEN_COLOR),
75 m_labelArmLengthFactor(DEFAULT_LABEL_ARM_LENGTH_FACTOR)
75 m_labelArmLengthFactor(DEFAULT_LABEL_ARM_LENGTH_FACTOR)
76 {
76 {
77
77
78 }
78 }
79
79
80 /*!
80 /*!
81 Destroys the slice.
81 Destroys the slice.
82 User should not delete the slice if it has been added to the series.
82 User should not delete the slice if it has been added to the series.
83 */
83 */
84 QPieSlice::~QPieSlice()
84 QPieSlice::~QPieSlice()
85 {
85 {
86
86
87 }
87 }
88
88
89 /*!
89 /*!
90 Gets the value of the slice.
90 Gets the value of the slice.
91 Note that all values in the series
91 Note that all values in the series
92 \sa setValue()
92 \sa setValue()
93 */
93 */
94 qreal QPieSlice::value() const
94 qreal QPieSlice::value() const
95 {
95 {
96 return m_value;
96 return m_value;
97 }
97 }
98
98
99 /*!
99 /*!
100 Gets the label of the slice.
100 Gets the label of the slice.
101 \sa setLabel()
101 \sa setLabel()
102 */
102 */
103 QString QPieSlice::label() const
103 QString QPieSlice::label() const
104 {
104 {
105 return m_label;
105 return m_label;
106 }
106 }
107
107
108 /*!
108 /*!
109 Returns true if label is set as visible.
109 Returns true if label is set as visible.
110 \sa setLabelVisible()
110 \sa setLabelVisible()
111 */
111 */
112 bool QPieSlice::isLabelVisible() const
112 bool QPieSlice::isLabelVisible() const
113 {
113 {
114 return m_isLabelVisible;
114 return m_isLabelVisible;
115 }
115 }
116
116
117 /*!
117 /*!
118 Returns true if slice is exloded from the pie.
118 Returns true if slice is exloded from the pie.
119 \sa setExploded(), setExplodeDistanceFactor()
119 \sa setExploded(), setExplodeDistanceFactor()
120 */
120 */
121 bool QPieSlice::isExploded() const
121 bool QPieSlice::isExploded() const
122 {
122 {
123 return m_isExploded;
123 return m_isExploded;
124 }
124 }
125
125
126 /*!
126 /*!
127 Returns the explode distance factor.
127 Returns the explode distance factor.
128
128
129 The factor is relative to pie radius. For example:
129 The factor is relative to pie radius. For example:
130 1.0 means the distance is the same as the radius.
130 1.0 means the distance is the same as the radius.
131 0.5 means the distance is half of the radius.
131 0.5 means the distance is half of the radius.
132
132
133 Default value is 0.15.
133 Default value is 0.15.
134
134
135 \sa setExplodeDistanceFactor()
135 \sa setExplodeDistanceFactor()
136 */
136 */
137 qreal QPieSlice::explodeDistanceFactor() const
137 qreal QPieSlice::explodeDistanceFactor() const
138 {
138 {
139 return m_explodeDistanceFactor;
139 return m_explodeDistanceFactor;
140 }
140 }
141
141
142 /*!
142 /*!
143 Returns the percentage of this slice compared to all slices in the same series.
143 Returns the percentage of this slice compared to all slices in the same series.
144 The returned value ranges from 0 to 1.0.
144 The returned value ranges from 0 to 1.0.
145
145
146 Updated internally after the slice is added to the series.
146 Updated internally after the slice is added to the series.
147 */
147 */
148 qreal QPieSlice::percentage() const
148 qreal QPieSlice::percentage() const
149 {
149 {
150 return m_percentage;
150 return m_percentage;
151 }
151 }
152
152
153 /*!
153 /*!
154 Returns the starting angle of this slice in the series it belongs to.
154 Returns the starting angle of this slice in the series it belongs to.
155
155
156 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
156 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
157
157
158 Updated internally after the slice is added to the series.
158 Updated internally after the slice is added to the series.
159 */
159 */
160 qreal QPieSlice::startAngle() const
160 qreal QPieSlice::startAngle() const
161 {
161 {
162 return m_startAngle;
162 return m_startAngle;
163 }
163 }
164
164
165 /*!
165 /*!
166 Returns the end angle of this slice in the series it belongs to.
166 Returns the end angle of this slice in the series it belongs to.
167
167
168 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
168 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
169
169
170 Updated internally after the slice is added to the series.
170 Updated internally after the slice is added to the series.
171 */
171 */
172 qreal QPieSlice::endAngle() const
172 qreal QPieSlice::endAngle() const
173 {
173 {
174 return m_startAngle + m_angleSpan;
174 return m_startAngle + m_angleSpan;
175 }
175 }
176
176
177 /*!
177 /*!
178 Returns the pen used to draw this slice.
178 Returns the pen used to draw this slice.
179 \sa setPen()
179 \sa setSlicePen()
180 */
180 */
181 QPen QPieSlice::pen() const
181 QPen QPieSlice::slicePen() const
182 {
182 {
183 return m_pen;
183 return m_slicePen;
184 }
184 }
185
185
186 /*!
186 /*!
187 Returns the brush used to draw this slice.
187 Returns the brush used to draw this slice.
188 \sa setBrush()
188 \sa setSliceBrush()
189 */
189 */
190 QBrush QPieSlice::brush() const
190 QBrush QPieSlice::sliceBrush() const
191 {
191 {
192 return m_brush;
192 return m_sliceBrush;
193 }
193 }
194
194
195 /*!
195 /*!
196 Returns the pen used to draw label in this slice.
196 Returns the pen used to draw label arm in this slice.
197 \sa setLabelPen()
197 \sa setLabelArmPen()
198 */
198 */
199 QPen QPieSlice::labelPen() const
199 QPen QPieSlice::labelArmPen() const
200 {
200 {
201 return m_labelPen;
201 return m_labelArmPen;
202 }
202 }
203
203
204 /*!
204 /*!
205 Returns the font used to draw label in this slice.
205 Returns the font used to draw label in this slice.
206 \sa setLabelFont()
206 \sa setLabelFont()
207 */
207 */
208 QFont QPieSlice::labelFont() const
208 QFont QPieSlice::labelFont() const
209 {
209 {
210 return m_labelFont;
210 return m_labelFont;
211 }
211 }
212
212
213 /*!
213 /*!
214 Gets the label arm lenght factor.
214 Gets the label arm lenght factor.
215
215
216 The factor is relative to pie radius. For example:
216 The factor is relative to pie radius. For example:
217 1.0 means the length is the same as the radius.
217 1.0 means the length is the same as the radius.
218 0.5 means the length is half of the radius.
218 0.5 means the length is half of the radius.
219
219
220 Default value is 0.15
220 Default value is 0.15
221
221
222 \sa setLabelArmLengthFactor()
222 \sa setLabelArmLengthFactor()
223 */
223 */
224 qreal QPieSlice::labelArmLengthFactor() const
224 qreal QPieSlice::labelArmLengthFactor() const
225 {
225 {
226 return m_labelArmLengthFactor;
226 return m_labelArmLengthFactor;
227 }
227 }
228
228
229 /*!
229 /*!
230 \fn void QPieSlice::clicked()
230 \fn void QPieSlice::clicked()
231
231
232 This signal is emitted when user has clicked the slice.
232 This signal is emitted when user has clicked the slice.
233
233
234 \sa QPieSeries::clicked()
234 \sa QPieSeries::clicked()
235 */
235 */
236
236
237 /*!
237 /*!
238 \fn void QPieSlice::hoverEnter()
238 \fn void QPieSlice::hoverEnter()
239
239
240 This signal is emitted when user has hovered over the slice.
240 This signal is emitted when user has hovered over the slice.
241
241
242 \sa QPieSeries::hoverEnter()
242 \sa QPieSeries::hoverEnter()
243 */
243 */
244
244
245 /*!
245 /*!
246 \fn void QPieSlice::hoverLeave()
246 \fn void QPieSlice::hoverLeave()
247
247
248 This signal is emitted when user has hovered away from the slice.
248 This signal is emitted when user has hovered away from the slice.
249
249
250 \sa QPieSeries::hoverLeave()
250 \sa QPieSeries::hoverLeave()
251 */
251 */
252
252
253 /*!
253 /*!
254 \fn void QPieSlice::changed()
254 \fn void QPieSlice::changed()
255
255
256 This signal emitted when something has changed in the slice.
256 This signal emitted when something has changed in the slice.
257
257
258 \sa QPieSeries::changed()
258 \sa QPieSeries::changed()
259 */
259 */
260
260
261 /*!
261 /*!
262 Sets the \a value of this slice.
262 Sets the \a value of this slice.
263 \sa value()
263 \sa value()
264 */
264 */
265 void QPieSlice::setValue(qreal value)
265 void QPieSlice::setValue(qreal value)
266 {
266 {
267 if (m_value != value) {
267 if (m_value != value) {
268 m_value = value;
268 m_value = value;
269 emit changed();
269 emit changed();
270 }
270 }
271 }
271 }
272
272
273 /*!
273 /*!
274 Sets the \a label of the slice.
274 Sets the \a label of the slice.
275 \sa label()
275 \sa label()
276 */
276 */
277 void QPieSlice::setLabel(QString label)
277 void QPieSlice::setLabel(QString label)
278 {
278 {
279 if (m_label != label) {
279 if (m_label != label) {
280 m_label = label;
280 m_label = label;
281 emit changed();
281 emit changed();
282 }
282 }
283 }
283 }
284
284
285 /*!
285 /*!
286 Sets the label \a visible in this slice.
286 Sets the label \a visible in this slice.
287 \sa isLabelVisible(), QPieSeries::setLabelsVisible()
287 \sa isLabelVisible(), QPieSeries::setLabelsVisible()
288 */
288 */
289 void QPieSlice::setLabelVisible(bool visible)
289 void QPieSlice::setLabelVisible(bool visible)
290 {
290 {
291 if (m_isLabelVisible != visible) {
291 if (m_isLabelVisible != visible) {
292 m_isLabelVisible = visible;
292 m_isLabelVisible = visible;
293 emit changed();
293 emit changed();
294 }
294 }
295 }
295 }
296
296
297 /*!
297 /*!
298 Sets this slice \a exploded.
298 Sets this slice \a exploded.
299 \sa isExploded(), explodeDistanceFactor()
299 \sa isExploded(), explodeDistanceFactor()
300 */
300 */
301 void QPieSlice::setExploded(bool exploded)
301 void QPieSlice::setExploded(bool exploded)
302 {
302 {
303 if (m_isExploded != exploded) {
303 if (m_isExploded != exploded) {
304 m_isExploded = exploded;
304 m_isExploded = exploded;
305 emit changed();
305 emit changed();
306 }
306 }
307 }
307 }
308
308
309 /*!
309 /*!
310 Sets the explode distance \a factor.
310 Sets the explode distance \a factor.
311
311
312 The factor is relative to pie radius. For example:
312 The factor is relative to pie radius. For example:
313 1.0 means the distance is the same as the radius.
313 1.0 means the distance is the same as the radius.
314 0.5 means the distance is half of the radius.
314 0.5 means the distance is half of the radius.
315
315
316 Default value is 0.15
316 Default value is 0.15
317
317
318 \sa explodeDistanceFactor()
318 \sa explodeDistanceFactor()
319 */
319 */
320 void QPieSlice::setExplodeDistanceFactor(qreal factor)
320 void QPieSlice::setExplodeDistanceFactor(qreal factor)
321 {
321 {
322 if (m_explodeDistanceFactor != factor) {
322 if (m_explodeDistanceFactor != factor) {
323 m_explodeDistanceFactor = factor;
323 m_explodeDistanceFactor = factor;
324 emit changed();
324 emit changed();
325 }
325 }
326 }
326 }
327
327
328 /*!
328 /*!
329 Sets the \a pen used to draw this slice.
329 Sets the \a pen used to draw this slice.
330 Note that applying a theme will override this.
330 Note that applying a theme will override this.
331 \sa pen()
331 \sa slicePen()
332 */
332 */
333 void QPieSlice::setPen(QPen pen)
333 void QPieSlice::setSlicePen(const QPen &pen)
334 {
334 {
335 if (m_pen != pen) {
335 if (m_slicePen != pen) {
336 m_pen = pen;
336 m_slicePen = pen;
337 emit changed();
337 emit changed();
338 }
338 }
339 }
339 }
340
340
341 /*!
341 /*!
342 Sets the \a brush used to draw this slice.
342 Sets the \a brush used to draw this slice.
343 Note that applying a theme will override this.
343 Note that applying a theme will override this.
344 \sa brush()
344 \sa sliceBrush()
345 */
345 */
346 void QPieSlice::setBrush(QBrush brush)
346 void QPieSlice::setSliceBrush(const QBrush &brush)
347 {
347 {
348 if (m_brush != brush) {
348 if (m_sliceBrush != brush) {
349 m_brush = brush;
349 m_sliceBrush = brush;
350 emit changed();
350 emit changed();
351 }
351 }
352 }
352 }
353
353
354 /*!
354 /*!
355 Sets the \a pen used to draw the label in this slice.
355 Sets the \a pen used to draw the label arm in this slice.
356 Note that applying a theme will override this.
356 Note that applying a theme will override this.
357 \sa labelPen()
357 \sa labelArmPen()
358 */
358 */
359 void QPieSlice::setLabelPen(QPen pen)
359 void QPieSlice::setLabelArmPen(const QPen &pen)
360 {
360 {
361 if (m_labelPen != pen) {
361 if (m_labelArmPen != pen) {
362 m_labelPen = pen;
362 m_labelArmPen = pen;
363 emit changed();
363 emit changed();
364 }
364 }
365 }
365 }
366
366
367 /*!
367 /*!
368 Sets the \a font used to draw the label in this slice.
368 Sets the \a font used to draw the label in this slice.
369 Note that applying a theme will override this.
369 Note that applying a theme will override this.
370 \sa labelFont()
370 \sa labelFont()
371 */
371 */
372 void QPieSlice::setLabelFont(QFont font)
372 void QPieSlice::setLabelFont(const QFont &font)
373 {
373 {
374 if (m_labelFont != font) {
374 if (m_labelFont != font) {
375 m_labelFont = font;
375 m_labelFont = font;
376 emit changed();
376 emit changed();
377 }
377 }
378 }
378 }
379
379
380 /*!
380 /*!
381 Sets the label arm lenght \a factor.
381 Sets the label arm lenght \a factor.
382
382
383 The factor is relative to pie radius. For example:
383 The factor is relative to pie radius. For example:
384 1.0 means the length is the same as the radius.
384 1.0 means the length is the same as the radius.
385 0.5 means the length is half of the radius.
385 0.5 means the length is half of the radius.
386
386
387 Default value is 0.15
387 Default value is 0.15
388
388
389 \sa labelArmLengthFactor()
389 \sa labelArmLengthFactor()
390 */
390 */
391 void QPieSlice::setLabelArmLengthFactor(qreal factor)
391 void QPieSlice::setLabelArmLengthFactor(qreal factor)
392 {
392 {
393 if (m_labelArmLengthFactor != factor) {
393 if (m_labelArmLengthFactor != factor) {
394 m_labelArmLengthFactor = factor;
394 m_labelArmLengthFactor = factor;
395 emit changed();
395 emit changed();
396 }
396 }
397 }
397 }
398
398
399 #include "moc_qpieslice.cpp"
399 #include "moc_qpieslice.cpp"
400
400
401 QTCOMMERCIALCHART_END_NAMESPACE
401 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,91 +1,91
1 #ifndef QPIESLICE_H
1 #ifndef QPIESLICE_H
2 #define QPIESLICE_H
2 #define QPIESLICE_H
3
3
4 #include <qchartglobal.h>
4 #include <qchartglobal.h>
5 #include <QObject>
5 #include <QObject>
6 #include <QPen>
6 #include <QPen>
7 #include <QBrush>
7 #include <QBrush>
8 #include <QFont>
8 #include <QFont>
9
9
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11
11
12 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
12 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY changed)
15 Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY changed)
16 Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY changed)
16 Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY changed)
17
17
18 public:
18 public:
19 QPieSlice(QObject *parent = 0);
19 QPieSlice(QObject *parent = 0);
20 QPieSlice(qreal value, QString label, QObject *parent = 0);
20 QPieSlice(qreal value, QString label, QObject *parent = 0);
21 virtual ~QPieSlice();
21 virtual ~QPieSlice();
22
22
23 // data
23 // data
24 void setValue(qreal value);
24 void setValue(qreal value);
25 qreal value() const;
25 qreal value() const;
26 void setLabel(QString label);
26 void setLabel(QString label);
27 QString label() const;
27 QString label() const;
28 void setLabelVisible(bool visible);
28 void setLabelVisible(bool visible);
29 bool isLabelVisible() const;
29 bool isLabelVisible() const;
30 void setExploded(bool exploded);
30 void setExploded(bool exploded);
31 bool isExploded() const;
31 bool isExploded() const;
32 void setExplodeDistanceFactor(qreal factor);
32 void setExplodeDistanceFactor(qreal factor);
33 qreal explodeDistanceFactor() const;
33 qreal explodeDistanceFactor() const;
34
34
35 // generated data
35 // generated data
36 qreal percentage() const;
36 qreal percentage() const;
37 qreal startAngle() const;
37 qreal startAngle() const;
38 qreal endAngle() const;
38 qreal endAngle() const;
39
39
40 // customization
40 // customization
41 void setPen(QPen pen);
41 void setSlicePen(const QPen &pen);
42 QPen pen() const;
42 QPen slicePen() const;
43 void setBrush(QBrush brush);
43 void setSliceBrush(const QBrush &brush);
44 QBrush brush() const;
44 QBrush sliceBrush() const;
45 void setLabelPen(QPen pen);
45 void setLabelArmPen(const QPen &pen);
46 QPen labelPen() const;
46 QPen labelArmPen() const;
47 void setLabelFont(QFont font);
47 void setLabelFont(const QFont &font);
48 QFont labelFont() const;
48 QFont labelFont() const;
49 void setLabelArmLengthFactor(qreal factor);
49 void setLabelArmLengthFactor(qreal factor);
50 qreal labelArmLengthFactor() const;
50 qreal labelArmLengthFactor() const;
51
51
52 // TODO: label position in general
52 // TODO: label position in general
53 // setLabelFlags(inside|outside|labelArmOn|labelArmOff|???)
53 // setLabelFlags(inside|outside|labelArmOn|labelArmOff|???)
54 // setLabelOrientation(horizontal|vertical|same as slice center angle|???)
54 // setLabelOrientation(horizontal|vertical|same as slice center angle|???)
55
55
56 Q_SIGNALS:
56 Q_SIGNALS:
57 void clicked();
57 void clicked();
58 void hoverEnter();
58 void hoverEnter();
59 void hoverLeave();
59 void hoverLeave();
60 void changed();
60 void changed();
61
61
62 private:
62 private:
63
63
64 // TODO: use private class
64 // TODO: use private class
65 friend class QPieSeries;
65 friend class QPieSeries;
66 friend class PiePresenter;
66 friend class PiePresenter;
67 friend class PieSlice;
67 friend class PieSlice;
68
68
69 // data
69 // data
70 qreal m_value;
70 qreal m_value;
71 QString m_label;
71 QString m_label;
72 bool m_isLabelVisible;
72 bool m_isLabelVisible;
73 bool m_isExploded;
73 bool m_isExploded;
74 qreal m_explodeDistanceFactor;
74 qreal m_explodeDistanceFactor;
75
75
76 // generated data
76 // generated data
77 qreal m_percentage;
77 qreal m_percentage;
78 qreal m_startAngle;
78 qreal m_startAngle;
79 qreal m_angleSpan;
79 qreal m_angleSpan;
80
80
81 // customization
81 // customization
82 QPen m_pen;
82 QPen m_slicePen;
83 QBrush m_brush;
83 QBrush m_sliceBrush;
84 QPen m_labelPen;
85 QFont m_labelFont;
84 QFont m_labelFont;
85 QPen m_labelArmPen;
86 qreal m_labelArmLengthFactor;
86 qreal m_labelArmLengthFactor;
87 };
87 };
88
88
89 QTCOMMERCIALCHART_END_NAMESPACE
89 QTCOMMERCIALCHART_END_NAMESPACE
90
90
91 #endif // QPIESLICE_H
91 #endif // QPIESLICE_H
General Comments 0
You need to be logged in to leave comments. Login now