##// END OF EJS Templates
API review changes for pie
Jani Honkonen -
r1009:983f422506c5
parent child
Show More
@@ -1,50 +1,48
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "customslice.h"
21 #include "customslice.h"
22
22
23 QTCOMMERCIALCHART_USE_NAMESPACE
23 QTCOMMERCIALCHART_USE_NAMESPACE
24
24
25 CustomSlice::CustomSlice(qreal value, QString label)
25 CustomSlice::CustomSlice(qreal value, QString label)
26 :QPieSlice(value, label)
26 :QPieSlice(value, label)
27 {
27 {
28 connect(this, SIGNAL(hoverEnter()), this, SLOT(handleHoverEnter()));
28 connect(this, SIGNAL(hovered(bool)), this, SLOT(showHighlight(bool)));
29 connect(this, SIGNAL(hoverLeave()), this, SLOT(handleHoverLeave()));
30 }
29 }
31
30
32 QBrush CustomSlice::originalBrush()
31 QBrush CustomSlice::originalBrush()
33 {
32 {
34 return m_originalBrush;
33 return m_originalBrush;
35 }
34 }
36
35
37 void CustomSlice::handleHoverEnter()
36 void CustomSlice::showHighlight(bool show)
38 {
37 {
39 QBrush brush = this->brush();
38 if (show) {
40 m_originalBrush = brush;
39 QBrush brush = this->brush();
41 brush.setColor(brush.color().lighter());
40 m_originalBrush = brush;
42 setBrush(brush);
41 brush.setColor(brush.color().lighter());
43 }
42 setBrush(brush);
44
43 } else {
45 void CustomSlice::handleHoverLeave()
44 setBrush(m_originalBrush);
46 {
45 }
47 setBrush(m_originalBrush);
48 }
46 }
49
47
50 #include "moc_customslice.cpp"
48 #include "moc_customslice.cpp"
@@ -1,45 +1,44
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20 #ifndef CUSTOMSLICE_H
20 #ifndef CUSTOMSLICE_H
21 #define CUSTOMSLICE_H
21 #define CUSTOMSLICE_H
22
22
23 #include <QPieSlice>
23 #include <QPieSlice>
24
24
25 QTCOMMERCIALCHART_USE_NAMESPACE
25 QTCOMMERCIALCHART_USE_NAMESPACE
26
26
27 class CustomSlice : public QPieSlice
27 class CustomSlice : public QPieSlice
28 {
28 {
29 Q_OBJECT
29 Q_OBJECT
30
30
31 public:
31 public:
32 CustomSlice(qreal value, QString label);
32 CustomSlice(qreal value, QString label);
33
33
34 public:
34 public:
35 QBrush originalBrush();
35 QBrush originalBrush();
36
36
37 public Q_SLOTS:
37 public Q_SLOTS:
38 void handleHoverEnter();
38 void showHighlight(bool show);
39 void handleHoverLeave();
40
39
41 private:
40 private:
42 QBrush m_originalBrush;
41 QBrush m_originalBrush;
43 };
42 };
44
43
45 #endif // CUSTOMSLICE_H
44 #endif // CUSTOMSLICE_H
@@ -1,324 +1,331
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20 #include "mainwidget.h"
20 #include "mainwidget.h"
21 #include "customslice.h"
21 #include "customslice.h"
22 #include "pentool.h"
22 #include "pentool.h"
23 #include "brushtool.h"
23 #include "brushtool.h"
24 #include <QPushButton>
24 #include <QPushButton>
25 #include <QComboBox>
25 #include <QComboBox>
26 #include <QCheckBox>
26 #include <QCheckBox>
27 #include <QLabel>
27 #include <QLabel>
28 #include <QGroupBox>
28 #include <QGroupBox>
29 #include <QDoubleSpinBox>
29 #include <QDoubleSpinBox>
30 #include <QFormLayout>
30 #include <QFormLayout>
31 #include <QFontDialog>
31 #include <QFontDialog>
32 #include <QChartView>
32 #include <QChartView>
33 #include <QPieSeries>
33 #include <QPieSeries>
34
34
35 QTCOMMERCIALCHART_USE_NAMESPACE
35 QTCOMMERCIALCHART_USE_NAMESPACE
36
36
37 MainWidget::MainWidget(QWidget* parent)
37 MainWidget::MainWidget(QWidget* parent)
38 :QWidget(parent),
38 :QWidget(parent),
39 m_slice(0)
39 m_slice(0)
40 {
40 {
41 // create chart
41 // create chart
42 QChart *chart = new QChart;
42 QChart *chart = new QChart;
43 chart->setTitle("Piechart customization");
43 chart->setTitle("Piechart customization");
44 chart->setAnimationOptions(QChart::AllAnimations);
44 chart->setAnimationOptions(QChart::AllAnimations);
45
45
46 // create series
46 // create series
47 m_series = new QPieSeries();
47 m_series = new QPieSeries();
48 *m_series << new CustomSlice(10.0, "Slice 1");
48 *m_series << new CustomSlice(10.0, "Slice 1");
49 *m_series << new CustomSlice(20.0, "Slice 2");
49 *m_series << new CustomSlice(20.0, "Slice 2");
50 *m_series << new CustomSlice(30.0, "Slice 3");
50 *m_series << new CustomSlice(30.0, "Slice 3");
51 *m_series << new CustomSlice(40.0, "Slice 4");
51 *m_series << new CustomSlice(40.0, "Slice 4");
52 *m_series << new CustomSlice(50.0, "Slice 5");
52 *m_series << new CustomSlice(50.0, "Slice 5");
53 m_series->setLabelsVisible();
53 m_series->setLabelsVisible();
54 chart->addSeries(m_series);
54 chart->addSeries(m_series);
55
55
56 connect(m_series, SIGNAL(clicked(QPieSlice*,Qt::MouseButtons)), this, SLOT(handleSliceClicked(QPieSlice*,Qt::MouseButtons)));
56 connect(m_series, SIGNAL(clicked(QPieSlice*)), this, SLOT(handleSliceClicked(QPieSlice*)));
57
57
58 // chart settings
58 // chart settings
59 m_themeComboBox = new QComboBox();
59 m_themeComboBox = new QComboBox();
60 m_themeComboBox->addItem("Light", QChart::ChartThemeLight);
60 m_themeComboBox->addItem("Light", QChart::ChartThemeLight);
61 m_themeComboBox->addItem("BlueCerulean", QChart::ChartThemeBlueCerulean);
61 m_themeComboBox->addItem("BlueCerulean", QChart::ChartThemeBlueCerulean);
62 m_themeComboBox->addItem("Dark", QChart::ChartThemeDark);
62 m_themeComboBox->addItem("Dark", QChart::ChartThemeDark);
63 m_themeComboBox->addItem("BrownSand", QChart::ChartThemeBrownSand);
63 m_themeComboBox->addItem("BrownSand", QChart::ChartThemeBrownSand);
64 m_themeComboBox->addItem("BlueNcs", QChart::ChartThemeBlueNcs);
64 m_themeComboBox->addItem("BlueNcs", QChart::ChartThemeBlueNcs);
65 m_themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
65 m_themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
66 m_themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
66 m_themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
67
67
68 m_aaCheckBox = new QCheckBox();
68 m_aaCheckBox = new QCheckBox();
69 m_animationsCheckBox = new QCheckBox();
69 m_animationsCheckBox = new QCheckBox();
70 m_animationsCheckBox->setCheckState(Qt::Checked);
70 m_animationsCheckBox->setCheckState(Qt::Checked);
71
71
72 m_legendCheckBox = new QCheckBox();
73
72 QFormLayout* chartSettingsLayout = new QFormLayout();
74 QFormLayout* chartSettingsLayout = new QFormLayout();
73 chartSettingsLayout->addRow("Theme", m_themeComboBox);
75 chartSettingsLayout->addRow("Theme", m_themeComboBox);
74 chartSettingsLayout->addRow("Antialiasing", m_aaCheckBox);
76 chartSettingsLayout->addRow("Antialiasing", m_aaCheckBox);
75 chartSettingsLayout->addRow("Animations", m_animationsCheckBox);
77 chartSettingsLayout->addRow("Animations", m_animationsCheckBox);
78 chartSettingsLayout->addRow("Legend", m_legendCheckBox);
76 QGroupBox* chartSettings = new QGroupBox("Chart");
79 QGroupBox* chartSettings = new QGroupBox("Chart");
77 chartSettings->setLayout(chartSettingsLayout);
80 chartSettings->setLayout(chartSettingsLayout);
78
81
79 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this ,SLOT(updateChartSettings()));
82 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this ,SLOT(updateChartSettings()));
80 connect(m_aaCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
83 connect(m_aaCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
81 connect(m_animationsCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
84 connect(m_animationsCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
85 connect(m_legendCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
82
86
83 // series settings
87 // series settings
84 m_hPosition = new QDoubleSpinBox();
88 m_hPosition = new QDoubleSpinBox();
85 m_hPosition->setMinimum(0.0);
89 m_hPosition->setMinimum(0.0);
86 m_hPosition->setMaximum(1.0);
90 m_hPosition->setMaximum(1.0);
87 m_hPosition->setSingleStep(0.1);
91 m_hPosition->setSingleStep(0.1);
88 m_hPosition->setValue(m_series->horizontalPosition());
92 m_hPosition->setValue(m_series->horizontalPosition());
89
93
90 m_vPosition = new QDoubleSpinBox();
94 m_vPosition = new QDoubleSpinBox();
91 m_vPosition->setMinimum(0.0);
95 m_vPosition->setMinimum(0.0);
92 m_vPosition->setMaximum(1.0);
96 m_vPosition->setMaximum(1.0);
93 m_vPosition->setSingleStep(0.1);
97 m_vPosition->setSingleStep(0.1);
94 m_vPosition->setValue(m_series->verticalPosition());
98 m_vPosition->setValue(m_series->verticalPosition());
95
99
96 m_sizeFactor = new QDoubleSpinBox();
100 m_sizeFactor = new QDoubleSpinBox();
97 m_sizeFactor->setMinimum(0.0);
101 m_sizeFactor->setMinimum(0.0);
98 m_sizeFactor->setMaximum(1.0);
102 m_sizeFactor->setMaximum(1.0);
99 m_sizeFactor->setSingleStep(0.1);
103 m_sizeFactor->setSingleStep(0.1);
100 m_sizeFactor->setValue(m_series->pieSize());
104 m_sizeFactor->setValue(m_series->pieSize());
101
105
102 m_startAngle = new QDoubleSpinBox();
106 m_startAngle = new QDoubleSpinBox();
103 m_startAngle->setMinimum(0.0);
107 m_startAngle->setMinimum(0.0);
104 m_startAngle->setMaximum(360);
108 m_startAngle->setMaximum(360);
105 m_startAngle->setValue(m_series->pieStartAngle());
109 m_startAngle->setValue(m_series->pieStartAngle());
106 m_startAngle->setSingleStep(1);
110 m_startAngle->setSingleStep(1);
107
111
108 m_endAngle = new QDoubleSpinBox();
112 m_endAngle = new QDoubleSpinBox();
109 m_endAngle->setMinimum(0.0);
113 m_endAngle->setMinimum(0.0);
110 m_endAngle->setMaximum(360);
114 m_endAngle->setMaximum(360);
111 m_endAngle->setValue(m_series->pieEndAngle());
115 m_endAngle->setValue(m_series->pieEndAngle());
112 m_endAngle->setSingleStep(1);
116 m_endAngle->setSingleStep(1);
113
117
114 QPushButton *appendSlice = new QPushButton("Append slice");
118 QPushButton *appendSlice = new QPushButton("Append slice");
115 QPushButton *insertSlice = new QPushButton("Insert slice");
119 QPushButton *insertSlice = new QPushButton("Insert slice");
116
120
117 QFormLayout* seriesSettingsLayout = new QFormLayout();
121 QFormLayout* seriesSettingsLayout = new QFormLayout();
118 seriesSettingsLayout->addRow("Horizontal position", m_hPosition);
122 seriesSettingsLayout->addRow("Horizontal position", m_hPosition);
119 seriesSettingsLayout->addRow("Vertical position", m_vPosition);
123 seriesSettingsLayout->addRow("Vertical position", m_vPosition);
120 seriesSettingsLayout->addRow("Size factor", m_sizeFactor);
124 seriesSettingsLayout->addRow("Size factor", m_sizeFactor);
121 seriesSettingsLayout->addRow("Start angle", m_startAngle);
125 seriesSettingsLayout->addRow("Start angle", m_startAngle);
122 seriesSettingsLayout->addRow("End angle", m_endAngle);
126 seriesSettingsLayout->addRow("End angle", m_endAngle);
123 seriesSettingsLayout->addRow(appendSlice);
127 seriesSettingsLayout->addRow(appendSlice);
124 seriesSettingsLayout->addRow(insertSlice);
128 seriesSettingsLayout->addRow(insertSlice);
125 QGroupBox* seriesSettings = new QGroupBox("Series");
129 QGroupBox* seriesSettings = new QGroupBox("Series");
126 seriesSettings->setLayout(seriesSettingsLayout);
130 seriesSettings->setLayout(seriesSettingsLayout);
127
131
128 connect(m_vPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
132 connect(m_vPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
129 connect(m_hPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
133 connect(m_hPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
130 connect(m_sizeFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
134 connect(m_sizeFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
131 connect(m_startAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
135 connect(m_startAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
132 connect(m_endAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
136 connect(m_endAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
133 connect(appendSlice, SIGNAL(clicked()), this, SLOT(appendSlice()));
137 connect(appendSlice, SIGNAL(clicked()), this, SLOT(appendSlice()));
134 connect(insertSlice, SIGNAL(clicked()), this, SLOT(insertSlice()));
138 connect(insertSlice, SIGNAL(clicked()), this, SLOT(insertSlice()));
135
139
136 // slice settings
140 // slice settings
137 m_sliceName = new QLabel("<click a slice>");
141 m_sliceName = new QLabel("<click a slice>");
138 m_sliceValue = new QDoubleSpinBox();
142 m_sliceValue = new QDoubleSpinBox();
139 m_sliceValue->setMaximum(1000);
143 m_sliceValue->setMaximum(1000);
140 m_sliceLabelVisible = new QCheckBox();
144 m_sliceLabelVisible = new QCheckBox();
141 m_sliceLabelArmFactor = new QDoubleSpinBox();
145 m_sliceLabelArmFactor = new QDoubleSpinBox();
142 m_sliceLabelArmFactor->setSingleStep(0.01);
146 m_sliceLabelArmFactor->setSingleStep(0.01);
143 m_sliceExploded = new QCheckBox();
147 m_sliceExploded = new QCheckBox();
144 m_sliceExplodedFactor = new QDoubleSpinBox();
148 m_sliceExplodedFactor = new QDoubleSpinBox();
145 m_sliceExplodedFactor->setSingleStep(0.01);
149 m_sliceExplodedFactor->setSingleStep(0.01);
146 m_pen = new QPushButton();
150 m_pen = new QPushButton();
147 m_penTool = new PenTool("Slice pen", this);
151 m_penTool = new PenTool("Slice pen", this);
148 m_brush = new QPushButton();
152 m_brush = new QPushButton();
149 m_brushTool = new BrushTool("Slice brush", this);
153 m_brushTool = new BrushTool("Slice brush", this);
150 m_font = new QPushButton();
154 m_font = new QPushButton();
151 m_labelPen = new QPushButton();
155 m_labelPen = new QPushButton();
152 m_labelPenTool = new PenTool("Label pen", this);
156 m_labelPenTool = new PenTool("Label pen", this);
153 QPushButton *removeSlice = new QPushButton("Remove slice");
157 QPushButton *removeSlice = new QPushButton("Remove slice");
154
158
155 QFormLayout* sliceSettingsLayout = new QFormLayout();
159 QFormLayout* sliceSettingsLayout = new QFormLayout();
156 sliceSettingsLayout->addRow("Selected", m_sliceName);
160 sliceSettingsLayout->addRow("Selected", m_sliceName);
157 sliceSettingsLayout->addRow("Value", m_sliceValue);
161 sliceSettingsLayout->addRow("Value", m_sliceValue);
158 sliceSettingsLayout->addRow("Pen", m_pen);
162 sliceSettingsLayout->addRow("Pen", m_pen);
159 sliceSettingsLayout->addRow("Brush", m_brush);
163 sliceSettingsLayout->addRow("Brush", m_brush);
160 sliceSettingsLayout->addRow("Label visible", m_sliceLabelVisible);
164 sliceSettingsLayout->addRow("Label visible", m_sliceLabelVisible);
161 sliceSettingsLayout->addRow("Label font", m_font);
165 sliceSettingsLayout->addRow("Label font", m_font);
162 sliceSettingsLayout->addRow("Label pen", m_labelPen);
166 sliceSettingsLayout->addRow("Label pen", m_labelPen);
163 sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor);
167 sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor);
164 sliceSettingsLayout->addRow("Exploded", m_sliceExploded);
168 sliceSettingsLayout->addRow("Exploded", m_sliceExploded);
165 sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor);
169 sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor);
166 sliceSettingsLayout->addRow(removeSlice);
170 sliceSettingsLayout->addRow(removeSlice);
167 QGroupBox* sliceSettings = new QGroupBox("Slice");
171 QGroupBox* sliceSettings = new QGroupBox("Slice");
168 sliceSettings->setLayout(sliceSettingsLayout);
172 sliceSettings->setLayout(sliceSettingsLayout);
169
173
170 connect(m_sliceValue, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
174 connect(m_sliceValue, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
171 connect(m_pen, SIGNAL(clicked()), m_penTool, SLOT(show()));
175 connect(m_pen, SIGNAL(clicked()), m_penTool, SLOT(show()));
172 connect(m_penTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
176 connect(m_penTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
173 connect(m_brush, SIGNAL(clicked()), m_brushTool, SLOT(show()));
177 connect(m_brush, SIGNAL(clicked()), m_brushTool, SLOT(show()));
174 connect(m_brushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
178 connect(m_brushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
175 connect(m_font, SIGNAL(clicked()), this, SLOT(showFontDialog()));
179 connect(m_font, SIGNAL(clicked()), this, SLOT(showFontDialog()));
176 connect(m_labelPen, SIGNAL(clicked()), m_labelPenTool, SLOT(show()));
180 connect(m_labelPen, SIGNAL(clicked()), m_labelPenTool, SLOT(show()));
177 connect(m_labelPenTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
181 connect(m_labelPenTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
178 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
182 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
179 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
183 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
180 connect(m_sliceLabelArmFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
184 connect(m_sliceLabelArmFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
181 connect(m_sliceExploded, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
185 connect(m_sliceExploded, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
182 connect(m_sliceExplodedFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
186 connect(m_sliceExplodedFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
183 connect(removeSlice, SIGNAL(clicked()), this, SLOT(removeSlice()));
187 connect(removeSlice, SIGNAL(clicked()), this, SLOT(removeSlice()));
184
188
185 // create chart view
189 // create chart view
186 m_chartView = new QChartView(chart);
190 m_chartView = new QChartView(chart);
187
191
188 // create main layout
192 // create main layout
189 QVBoxLayout *settingsLayout = new QVBoxLayout();
193 QVBoxLayout *settingsLayout = new QVBoxLayout();
190 settingsLayout->addWidget(chartSettings);
194 settingsLayout->addWidget(chartSettings);
191 settingsLayout->addWidget(seriesSettings);
195 settingsLayout->addWidget(seriesSettings);
192 settingsLayout->addWidget(sliceSettings);
196 settingsLayout->addWidget(sliceSettings);
193 settingsLayout->addStretch();
197 settingsLayout->addStretch();
194
198
195 QGridLayout* baseLayout = new QGridLayout();
199 QGridLayout* baseLayout = new QGridLayout();
196 baseLayout->addLayout(settingsLayout, 0, 0);
200 baseLayout->addLayout(settingsLayout, 0, 0);
197 baseLayout->addWidget(m_chartView, 0, 1);
201 baseLayout->addWidget(m_chartView, 0, 1);
198 setLayout(baseLayout);
202 setLayout(baseLayout);
199
203
200 updateSerieSettings();
204 updateSerieSettings();
201 }
205 }
202
206
203
207
204 void MainWidget::updateChartSettings()
208 void MainWidget::updateChartSettings()
205 {
209 {
206 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
210 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
207 m_chartView->chart()->setTheme(theme);
211 m_chartView->chart()->setTheme(theme);
208 m_chartView->setRenderHint(QPainter::Antialiasing, m_aaCheckBox->isChecked());
212 m_chartView->setRenderHint(QPainter::Antialiasing, m_aaCheckBox->isChecked());
209
213
210 if (m_animationsCheckBox->checkState() == Qt::Checked)
214 if (m_animationsCheckBox->checkState() == Qt::Checked)
211 m_chartView->chart()->setAnimationOptions(QChart::AllAnimations);
215 m_chartView->chart()->setAnimationOptions(QChart::AllAnimations);
212 else
216 else
213 m_chartView->chart()->setAnimationOptions(QChart::NoAnimation);
217 m_chartView->chart()->setAnimationOptions(QChart::NoAnimation);
218
219 if (m_legendCheckBox->checkState() == Qt::Checked)
220 m_chartView->chart()->legend()->show();
221 else
222 m_chartView->chart()->legend()->hide();
214 }
223 }
215
224
216 void MainWidget::updateSerieSettings()
225 void MainWidget::updateSerieSettings()
217 {
226 {
218 m_series->setHorizontalPosition(m_hPosition->value());
227 m_series->setHorizontalPosition(m_hPosition->value());
219 m_series->setVerticalPosition(m_vPosition->value());
228 m_series->setVerticalPosition(m_vPosition->value());
220 m_series->setPieSize(m_sizeFactor->value());
229 m_series->setPieSize(m_sizeFactor->value());
221 m_series->setPieStartAngle(m_startAngle->value());
230 m_series->setPieStartAngle(m_startAngle->value());
222 m_series->setPieEndAngle(m_endAngle->value());
231 m_series->setPieEndAngle(m_endAngle->value());
223 }
232 }
224
233
225 void MainWidget::updateSliceSettings()
234 void MainWidget::updateSliceSettings()
226 {
235 {
227 if (!m_slice)
236 if (!m_slice)
228 return;
237 return;
229
238
230 m_slice->setValue(m_sliceValue->value());
239 m_slice->setValue(m_sliceValue->value());
231
240
232 m_slice->setPen(m_penTool->pen());
241 m_slice->setPen(m_penTool->pen());
233 m_slice->setBrush(m_brushTool->brush());
242 m_slice->setBrush(m_brushTool->brush());
234
243
235 m_slice->setLabelPen(m_labelPenTool->pen());
244 m_slice->setLabelPen(m_labelPenTool->pen());
236 m_slice->setLabelVisible(m_sliceLabelVisible->isChecked());
245 m_slice->setLabelVisible(m_sliceLabelVisible->isChecked());
237 m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value());
246 m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value());
238
247
239 m_slice->setExploded(m_sliceExploded->isChecked());
248 m_slice->setExploded(m_sliceExploded->isChecked());
240 m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value());
249 m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value());
241 }
250 }
242
251
243 void MainWidget::handleSliceClicked(QPieSlice* slice, Qt::MouseButtons buttons)
252 void MainWidget::handleSliceClicked(QPieSlice* slice)
244 {
253 {
245 Q_UNUSED(buttons);
246
247 m_slice = static_cast<CustomSlice*>(slice);
254 m_slice = static_cast<CustomSlice*>(slice);
248
255
249 // name
256 // name
250 m_sliceName->setText(slice->label());
257 m_sliceName->setText(slice->label());
251
258
252 // value
259 // value
253 m_sliceValue->blockSignals(true);
260 m_sliceValue->blockSignals(true);
254 m_sliceValue->setValue(slice->value());
261 m_sliceValue->setValue(slice->value());
255 m_sliceValue->blockSignals(false);
262 m_sliceValue->blockSignals(false);
256
263
257 // pen
264 // pen
258 m_pen->setText(PenTool::name(m_slice->pen()));
265 m_pen->setText(PenTool::name(m_slice->pen()));
259 m_penTool->setPen(m_slice->pen());
266 m_penTool->setPen(m_slice->pen());
260
267
261 // brush
268 // brush
262 m_brush->setText(m_slice->originalBrush().color().name());
269 m_brush->setText(m_slice->originalBrush().color().name());
263 m_brushTool->setBrush(m_slice->originalBrush());
270 m_brushTool->setBrush(m_slice->originalBrush());
264
271
265 // label
272 // label
266 m_labelPen->setText(PenTool::name(m_slice->labelPen()));
273 m_labelPen->setText(PenTool::name(m_slice->labelPen()));
267 m_labelPenTool->setPen(m_slice->labelPen());
274 m_labelPenTool->setPen(m_slice->labelPen());
268 m_font->setText(slice->labelFont().toString());
275 m_font->setText(slice->labelFont().toString());
269 m_sliceLabelVisible->blockSignals(true);
276 m_sliceLabelVisible->blockSignals(true);
270 m_sliceLabelVisible->setChecked(slice->isLabelVisible());
277 m_sliceLabelVisible->setChecked(slice->isLabelVisible());
271 m_sliceLabelVisible->blockSignals(false);
278 m_sliceLabelVisible->blockSignals(false);
272 m_sliceLabelArmFactor->blockSignals(true);
279 m_sliceLabelArmFactor->blockSignals(true);
273 m_sliceLabelArmFactor->setValue(slice->labelArmLengthFactor());
280 m_sliceLabelArmFactor->setValue(slice->labelArmLengthFactor());
274 m_sliceLabelArmFactor->blockSignals(false);
281 m_sliceLabelArmFactor->blockSignals(false);
275
282
276 // exploded
283 // exploded
277 m_sliceExploded->blockSignals(true);
284 m_sliceExploded->blockSignals(true);
278 m_sliceExploded->setChecked(slice->isExploded());
285 m_sliceExploded->setChecked(slice->isExploded());
279 m_sliceExploded->blockSignals(false);
286 m_sliceExploded->blockSignals(false);
280 m_sliceExplodedFactor->blockSignals(true);
287 m_sliceExplodedFactor->blockSignals(true);
281 m_sliceExplodedFactor->setValue(slice->explodeDistanceFactor());
288 m_sliceExplodedFactor->setValue(slice->explodeDistanceFactor());
282 m_sliceExplodedFactor->blockSignals(false);
289 m_sliceExplodedFactor->blockSignals(false);
283 }
290 }
284
291
285 void MainWidget::showFontDialog()
292 void MainWidget::showFontDialog()
286 {
293 {
287 if (!m_slice)
294 if (!m_slice)
288 return;
295 return;
289
296
290 QFontDialog dialog(m_slice->labelFont());
297 QFontDialog dialog(m_slice->labelFont());
291 dialog.show();
298 dialog.show();
292 dialog.exec();
299 dialog.exec();
293
300
294 m_slice->setLabelFont(dialog.currentFont());
301 m_slice->setLabelFont(dialog.currentFont());
295 m_font->setText(dialog.currentFont().toString());
302 m_font->setText(dialog.currentFont().toString());
296 }
303 }
297
304
298 void MainWidget::appendSlice()
305 void MainWidget::appendSlice()
299 {
306 {
300 *m_series << new CustomSlice(10.0, "Slice " + QString::number(m_series->count()+1));
307 *m_series << new CustomSlice(10.0, "Slice " + QString::number(m_series->count()+1));
301 }
308 }
302
309
303 void MainWidget::insertSlice()
310 void MainWidget::insertSlice()
304 {
311 {
305 if (!m_slice)
312 if (!m_slice)
306 return;
313 return;
307
314
308 int i = m_series->slices().indexOf(m_slice);
315 int i = m_series->slices().indexOf(m_slice);
309
316
310 m_series->insert(i, new CustomSlice(10.0, "Slice " + QString::number(m_series->count()+1)));
317 m_series->insert(i, new CustomSlice(10.0, "Slice " + QString::number(m_series->count()+1)));
311 }
318 }
312
319
313 void MainWidget::removeSlice()
320 void MainWidget::removeSlice()
314 {
321 {
315 if (!m_slice)
322 if (!m_slice)
316 return;
323 return;
317
324
318 m_sliceName->setText("<click a slice>");
325 m_sliceName->setText("<click a slice>");
319
326
320 m_series->remove(m_slice);
327 m_series->remove(m_slice);
321 m_slice = 0;
328 m_slice = 0;
322 }
329 }
323
330
324 #include "moc_mainwidget.cpp"
331 #include "moc_mainwidget.cpp"
@@ -1,90 +1,91
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20 #ifndef MAINWIDGET_H
20 #ifndef MAINWIDGET_H
21 #define MAINWIDGET_H
21 #define MAINWIDGET_H
22
22
23 #include <QWidget>
23 #include <QWidget>
24 #include <QChartGlobal>
24 #include <QChartGlobal>
25
25
26 class QLabel;
26 class QLabel;
27 class QPushButton;
27 class QPushButton;
28 class QCheckBox;
28 class QCheckBox;
29 class QComboBox;
29 class QComboBox;
30 class QDoubleSpinBox;
30 class QDoubleSpinBox;
31 class PenTool;
31 class PenTool;
32 class BrushTool;
32 class BrushTool;
33 class CustomSlice;
33 class CustomSlice;
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 class QChartView;
36 class QChartView;
37 class QPieSeries;
37 class QPieSeries;
38 class QPieSlice;
38 class QPieSlice;
39 QTCOMMERCIALCHART_END_NAMESPACE
39 QTCOMMERCIALCHART_END_NAMESPACE
40
40
41 QTCOMMERCIALCHART_USE_NAMESPACE
41 QTCOMMERCIALCHART_USE_NAMESPACE
42
42
43 class MainWidget : public QWidget
43 class MainWidget : public QWidget
44 {
44 {
45 Q_OBJECT
45 Q_OBJECT
46
46
47 public:
47 public:
48 explicit MainWidget(QWidget* parent = 0);
48 explicit MainWidget(QWidget* parent = 0);
49
49
50 public Q_SLOTS:
50 public Q_SLOTS:
51 void updateChartSettings();
51 void updateChartSettings();
52 void updateSerieSettings();
52 void updateSerieSettings();
53 void updateSliceSettings();
53 void updateSliceSettings();
54 void handleSliceClicked(QPieSlice* slice, Qt::MouseButtons buttons);
54 void handleSliceClicked(QPieSlice* slice);
55 void showFontDialog();
55 void showFontDialog();
56 void appendSlice();
56 void appendSlice();
57 void insertSlice();
57 void insertSlice();
58 void removeSlice();
58 void removeSlice();
59
59
60 private:
60 private:
61 QComboBox *m_themeComboBox;
61 QComboBox *m_themeComboBox;
62 QCheckBox *m_aaCheckBox;
62 QCheckBox *m_aaCheckBox;
63 QCheckBox *m_animationsCheckBox;
63 QCheckBox *m_animationsCheckBox;
64 QCheckBox *m_legendCheckBox;
64
65
65 QChartView* m_chartView;
66 QChartView* m_chartView;
66 QPieSeries* m_series;
67 QPieSeries* m_series;
67 CustomSlice* m_slice;
68 CustomSlice* m_slice;
68
69
69 QDoubleSpinBox* m_hPosition;
70 QDoubleSpinBox* m_hPosition;
70 QDoubleSpinBox* m_vPosition;
71 QDoubleSpinBox* m_vPosition;
71 QDoubleSpinBox* m_sizeFactor;
72 QDoubleSpinBox* m_sizeFactor;
72 QDoubleSpinBox* m_startAngle;
73 QDoubleSpinBox* m_startAngle;
73 QDoubleSpinBox* m_endAngle;
74 QDoubleSpinBox* m_endAngle;
74
75
75 QLabel* m_sliceName;
76 QLabel* m_sliceName;
76 QDoubleSpinBox* m_sliceValue;
77 QDoubleSpinBox* m_sliceValue;
77 QCheckBox* m_sliceLabelVisible;
78 QCheckBox* m_sliceLabelVisible;
78 QDoubleSpinBox* m_sliceLabelArmFactor;
79 QDoubleSpinBox* m_sliceLabelArmFactor;
79 QCheckBox* m_sliceExploded;
80 QCheckBox* m_sliceExploded;
80 QDoubleSpinBox* m_sliceExplodedFactor;
81 QDoubleSpinBox* m_sliceExplodedFactor;
81 QPushButton *m_brush;
82 QPushButton *m_brush;
82 BrushTool *m_brushTool;
83 BrushTool *m_brushTool;
83 QPushButton *m_pen;
84 QPushButton *m_pen;
84 PenTool *m_penTool;
85 PenTool *m_penTool;
85 QPushButton *m_font;
86 QPushButton *m_font;
86 QPushButton *m_labelPen;
87 QPushButton *m_labelPen;
87 PenTool *m_labelPenTool;
88 PenTool *m_labelPenTool;
88 };
89 };
89
90
90 #endif // MAINWIDGET_H
91 #endif // MAINWIDGET_H
@@ -1,70 +1,63
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "drilldownslice.h"
21 #include "drilldownslice.h"
22
22
23 QTCOMMERCIALCHART_USE_NAMESPACE
23 QTCOMMERCIALCHART_USE_NAMESPACE
24
24
25 DrilldownSlice::DrilldownSlice(qreal value, QString prefix, QAbstractSeries* drilldownSeries)
25 DrilldownSlice::DrilldownSlice(qreal value, QString prefix, QAbstractSeries* drilldownSeries)
26 :m_drilldownSeries(drilldownSeries),
26 :m_drilldownSeries(drilldownSeries),
27 m_prefix(prefix)
27 m_prefix(prefix)
28 {
28 {
29 setValue(value);
29 setValue(value);
30 updateLabel();
30 updateLabel();
31 setLabelFont(QFont("Arial", 8));
31 setLabelFont(QFont("Arial", 8));
32 connect(this, SIGNAL(changed()), this, SLOT(updateLabel()));
32 connect(this, SIGNAL(changed()), this, SLOT(updateLabel()));
33 connect(this, SIGNAL(hoverEnter()), this, SLOT(showHighlight()));
33 connect(this, SIGNAL(hovered(bool)), this, SLOT(showHighlight(bool)));
34 connect(this, SIGNAL(hoverLeave()), this, SLOT(hideHighlight()));
35 }
34 }
36
35
37 DrilldownSlice::~DrilldownSlice()
36 DrilldownSlice::~DrilldownSlice()
38 {
37 {
39
38
40 }
39 }
41
40
42 QAbstractSeries* DrilldownSlice::drilldownSeries() const
41 QAbstractSeries* DrilldownSlice::drilldownSeries() const
43 {
42 {
44 return m_drilldownSeries;
43 return m_drilldownSeries;
45 }
44 }
46
45
47 void DrilldownSlice::updateLabel()
46 void DrilldownSlice::updateLabel()
48 {
47 {
49 QString label = m_prefix;
48 QString label = m_prefix;
50 label += " $";
49 label += " $";
51 label += QString::number(this->value());
50 label += QString::number(this->value());
52 label += ", ";
51 label += ", ";
53 label += QString::number(this->percentage()*100, 'f', 1);
52 label += QString::number(this->percentage()*100, 'f', 1);
54 label += "%";
53 label += "%";
55 setLabel(label);
54 setLabel(label);
56 }
55 }
57
56
58 void DrilldownSlice::showHighlight()
57 void DrilldownSlice::showHighlight(bool show)
59 {
58 {
60 setExploded();
59 setExploded(show);
61 setLabelVisible();
60 setLabelVisible(show);
62 }
63
64 void DrilldownSlice::hideHighlight()
65 {
66 setExploded(false);
67 setLabelVisible(false);
68 }
61 }
69
62
70 #include "moc_drilldownslice.cpp"
63 #include "moc_drilldownslice.cpp"
@@ -1,50 +1,49
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20 #ifndef DRILLDOWNSLICE_H
20 #ifndef DRILLDOWNSLICE_H
21 #define DRILLDOWNSLICE_H
21 #define DRILLDOWNSLICE_H
22
22
23 #include <QPieSlice>
23 #include <QPieSlice>
24
24
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 class QAbstractSeries;
26 class QAbstractSeries;
27 QTCOMMERCIALCHART_END_NAMESPACE
27 QTCOMMERCIALCHART_END_NAMESPACE
28
28
29 QTCOMMERCIALCHART_USE_NAMESPACE
29 QTCOMMERCIALCHART_USE_NAMESPACE
30
30
31 class DrilldownSlice : public QPieSlice
31 class DrilldownSlice : public QPieSlice
32 {
32 {
33 Q_OBJECT
33 Q_OBJECT
34
34
35 public:
35 public:
36 DrilldownSlice(qreal value, QString prefix, QAbstractSeries* drilldownSeries);
36 DrilldownSlice(qreal value, QString prefix, QAbstractSeries* drilldownSeries);
37 virtual ~DrilldownSlice();
37 virtual ~DrilldownSlice();
38 QAbstractSeries* drilldownSeries() const;
38 QAbstractSeries* drilldownSeries() const;
39
39
40 public Q_SLOTS:
40 public Q_SLOTS:
41 void updateLabel();
41 void updateLabel();
42 void showHighlight();
42 void showHighlight(bool show);
43 void hideHighlight();
44
43
45 private:
44 private:
46 QAbstractSeries* m_drilldownSeries;
45 QAbstractSeries* m_drilldownSeries;
47 QString m_prefix;
46 QString m_prefix;
48 };
47 };
49
48
50 #endif // DRILLDOWNSLICE_H
49 #endif // DRILLDOWNSLICE_H
@@ -1,77 +1,77
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "drilldownchart.h"
21 #include "drilldownchart.h"
22 #include "drilldownslice.h"
22 #include "drilldownslice.h"
23 #include <QtGui/QApplication>
23 #include <QtGui/QApplication>
24 #include <QMainWindow>
24 #include <QMainWindow>
25 #include <QTime>
25 #include <QTime>
26 #include <QChartView>
26 #include <QChartView>
27 #include <QLegend>
27 #include <QLegend>
28 #include <QPieSeries>
28 #include <QPieSeries>
29
29
30 QTCOMMERCIALCHART_USE_NAMESPACE
30 QTCOMMERCIALCHART_USE_NAMESPACE
31
31
32 int main(int argc, char *argv[])
32 int main(int argc, char *argv[])
33 {
33 {
34 QApplication a(argc, argv);
34 QApplication a(argc, argv);
35
35
36 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
36 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
37
37
38 QMainWindow window;
38 QMainWindow window;
39
39
40 DrilldownChart* chart = new DrilldownChart();
40 DrilldownChart* chart = new DrilldownChart();
41 chart->setTheme(QChart::ChartThemeLight);
41 chart->setTheme(QChart::ChartThemeLight);
42 chart->setAnimationOptions(QChart::AllAnimations);
42 chart->setAnimationOptions(QChart::AllAnimations);
43 chart->legend()->setVisible(true);
43 chart->legend()->setVisible(true);
44 chart->legend()->setAlignment(QLegend::AlignmentRight);
44 chart->legend()->setAlignment(QLegend::AlignmentRight);
45
45
46 QPieSeries* yearSeries = new QPieSeries(&window);
46 QPieSeries* yearSeries = new QPieSeries(&window);
47 yearSeries->setName("Sales by year - All");
47 yearSeries->setName("Sales by year - All");
48
48
49 QList<QString> months;
49 QList<QString> months;
50 months << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
50 months << "Jan" << "Feb" << "Mar" << "Apr" << "May" << "Jun" << "Jul" << "Aug" << "Sep" << "Oct" << "Nov" << "Dec";
51 QList<QString> names;
51 QList<QString> names;
52 names << "Jane" << "John" << "Axel" << "Mary" << "Susan" << "Bob";
52 names << "Jane" << "John" << "Axel" << "Mary" << "Susan" << "Bob";
53
53
54 foreach (QString name, names) {
54 foreach (QString name, names) {
55 QPieSeries* series = new QPieSeries(&window);
55 QPieSeries* series = new QPieSeries(&window);
56 series->setName("Sales by month - " + name);
56 series->setName("Sales by month - " + name);
57
57
58 foreach (QString month, months)
58 foreach (QString month, months)
59 *series << new DrilldownSlice(qrand() % 1000, month, yearSeries);
59 *series << new DrilldownSlice(qrand() % 1000, month, yearSeries);
60
60
61 QObject::connect(series, SIGNAL(clicked(QPieSlice*,Qt::MouseButtons)), chart, SLOT(handleSliceClicked(QPieSlice*)));
61 QObject::connect(series, SIGNAL(clicked(QPieSlice*)), chart, SLOT(handleSliceClicked(QPieSlice*)));
62
62
63 *yearSeries << new DrilldownSlice(series->sum(), name, series);
63 *yearSeries << new DrilldownSlice(series->sum(), name, series);
64 }
64 }
65
65
66 QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*,Qt::MouseButtons)), chart, SLOT(handleSliceClicked(QPieSlice*)));
66 QObject::connect(yearSeries, SIGNAL(clicked(QPieSlice*)), chart, SLOT(handleSliceClicked(QPieSlice*)));
67
67
68 chart->changeSeries(yearSeries);
68 chart->changeSeries(yearSeries);
69
69
70 QChartView* chartView = new QChartView(chart);
70 QChartView* chartView = new QChartView(chart);
71 chartView->setRenderHint(QPainter::Antialiasing);
71 chartView->setRenderHint(QPainter::Antialiasing);
72 window.setCentralWidget(chartView);
72 window.setCentralWidget(chartView);
73 window.resize(800, 500);
73 window.resize(800, 500);
74 window.show();
74 window.show();
75
75
76 return a.exec();
76 return a.exec();
77 }
77 }
@@ -1,194 +1,194
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "legendmarker_p.h"
21 #include "legendmarker_p.h"
22 #include "qxyseries.h"
22 #include "qxyseries.h"
23 #include "qxyseries_p.h"
23 #include "qxyseries_p.h"
24 #include "qlegend.h"
24 #include "qlegend.h"
25 #include "qbarseries.h"
25 #include "qbarseries.h"
26 #include "qpieseries.h"
26 #include "qpieseries.h"
27 #include "qpieslice.h"
27 #include "qpieslice.h"
28 #include "qbarset.h"
28 #include "qbarset.h"
29 #include "qbarset_p.h"
29 #include "qbarset_p.h"
30 #include "qareaseries.h"
30 #include "qareaseries.h"
31 #include "qareaseries_p.h"
31 #include "qareaseries_p.h"
32 #include <QPainter>
32 #include <QPainter>
33 #include <QGraphicsSceneEvent>
33 #include <QGraphicsSceneEvent>
34 #include <QGraphicsSimpleTextItem>
34 #include <QGraphicsSimpleTextItem>
35
35
36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 QTCOMMERCIALCHART_BEGIN_NAMESPACE
37
37
38 LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) : QGraphicsObject(legend),
38 LegendMarker::LegendMarker(QAbstractSeries *series, QLegend *legend) : QGraphicsObject(legend),
39 m_series(series),
39 m_series(series),
40 m_markerRect(0,0,10.0,10.0),
40 m_markerRect(0,0,10.0,10.0),
41 m_boundingRect(0,0,0,0),
41 m_boundingRect(0,0,0,0),
42 m_legend(legend),
42 m_legend(legend),
43 m_textItem(new QGraphicsSimpleTextItem(this)),
43 m_textItem(new QGraphicsSimpleTextItem(this)),
44 m_rectItem(new QGraphicsRectItem(this))
44 m_rectItem(new QGraphicsRectItem(this))
45 {
45 {
46 //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
46 //setAcceptedMouseButtons(Qt::LeftButton|Qt::RightButton);
47 m_rectItem->setRect(m_markerRect);
47 m_rectItem->setRect(m_markerRect);
48 updateLayout();
48 updateLayout();
49 }
49 }
50
50
51 void LegendMarker::setPen(const QPen &pen)
51 void LegendMarker::setPen(const QPen &pen)
52 {
52 {
53 m_textItem->setPen(pen);
53 m_textItem->setPen(pen);
54 updateLayout();
54 updateLayout();
55 }
55 }
56
56
57 QPen LegendMarker::pen() const
57 QPen LegendMarker::pen() const
58 {
58 {
59 return m_textItem->pen();
59 return m_textItem->pen();
60 }
60 }
61
61
62 void LegendMarker::setBrush(const QBrush &brush)
62 void LegendMarker::setBrush(const QBrush &brush)
63 {
63 {
64 m_rectItem->setBrush(brush);
64 m_rectItem->setBrush(brush);
65 }
65 }
66
66
67 QBrush LegendMarker::brush() const
67 QBrush LegendMarker::brush() const
68 {
68 {
69 return m_rectItem->brush();
69 return m_rectItem->brush();
70 }
70 }
71
71
72 void LegendMarker::setLabel(const QString name)
72 void LegendMarker::setLabel(const QString name)
73 {
73 {
74 m_textItem->setText(name);
74 m_textItem->setText(name);
75 updateLayout();
75 updateLayout();
76 }
76 }
77
77
78 void LegendMarker::setSize(const QSize& size)
78 void LegendMarker::setSize(const QSize& size)
79 {
79 {
80 m_markerRect = QRectF(0,0,size.width(),size.height());
80 m_markerRect = QRectF(0,0,size.width(),size.height());
81 }
81 }
82
82
83 QString LegendMarker::label() const
83 QString LegendMarker::label() const
84 {
84 {
85 return m_textItem->text();
85 return m_textItem->text();
86 }
86 }
87
87
88 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
88 void LegendMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
89 {
89 {
90 Q_UNUSED(option)
90 Q_UNUSED(option)
91 Q_UNUSED(widget)
91 Q_UNUSED(widget)
92 Q_UNUSED(painter)
92 Q_UNUSED(painter)
93 }
93 }
94
94
95 QRectF LegendMarker::boundingRect() const
95 QRectF LegendMarker::boundingRect() const
96 {
96 {
97 return m_boundingRect;
97 return m_boundingRect;
98 }
98 }
99
99
100 void LegendMarker::updateLayout()
100 void LegendMarker::updateLayout()
101 {
101 {
102
102
103 static const qreal margin = 2;
103 static const qreal margin = 2;
104 static const qreal space = 4;
104 static const qreal space = 4;
105
105
106 const QRectF& textRect = m_textItem->boundingRect();
106 const QRectF& textRect = m_textItem->boundingRect();
107 prepareGeometryChange();
107 prepareGeometryChange();
108 m_boundingRect = QRectF(0,0,m_markerRect.width() + 2*margin + space + textRect.width(),qMax(m_markerRect.height()+2*margin,textRect.height()+2*margin));
108 m_boundingRect = QRectF(0,0,m_markerRect.width() + 2*margin + space + textRect.width(),qMax(m_markerRect.height()+2*margin,textRect.height()+2*margin));
109 m_textItem->setPos(m_markerRect.width() + space + margin,m_boundingRect.height()/2 - textRect.height()/2);
109 m_textItem->setPos(m_markerRect.width() + space + margin,m_boundingRect.height()/2 - textRect.height()/2);
110 m_rectItem->setPos(margin,m_boundingRect.height()/2 - m_markerRect.height()/2);
110 m_rectItem->setPos(margin,m_boundingRect.height()/2 - m_markerRect.height()/2);
111
111
112 }
112 }
113
113
114 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
114 void LegendMarker::mousePressEvent(QGraphicsSceneMouseEvent *event)
115 {
115 {
116 QGraphicsObject::mousePressEvent(event);
116 QGraphicsObject::mousePressEvent(event);
117 emit selected();
117 emit selected();
118 }
118 }
119
119
120 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
120 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
121
121
122 AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend),
122 AreaLegendMarker::AreaLegendMarker(QAreaSeries *series,QLegend *legend) : LegendMarker(series,legend),
123 m_series(series)
123 m_series(series)
124 {
124 {
125 QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
125 QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
126 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
126 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
127 updated();
127 updated();
128 }
128 }
129
129
130 void AreaLegendMarker::updated()
130 void AreaLegendMarker::updated()
131 {
131 {
132 setBrush(m_series->brush());
132 setBrush(m_series->brush());
133 setLabel(m_series->name());
133 setLabel(m_series->name());
134 }
134 }
135
135
136 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
136 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
137
137
138 BarLegendMarker::BarLegendMarker(QBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend),
138 BarLegendMarker::BarLegendMarker(QBarSeries *barseries,QBarSet *barset, QLegend *legend) : LegendMarker(barseries,legend),
139 m_barset(barset)
139 m_barset(barset)
140 {
140 {
141 QObject::connect(this, SIGNAL(selected()),barseries, SIGNAL(selected()));
141 QObject::connect(this, SIGNAL(selected()),barseries, SIGNAL(selected()));
142 QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated()));
142 QObject::connect(barset->d_ptr.data(), SIGNAL(updatedBars()), this, SLOT(updated()));
143 updated();
143 updated();
144 }
144 }
145
145
146 void BarLegendMarker::updated()
146 void BarLegendMarker::updated()
147 {
147 {
148 setBrush(m_barset->brush());
148 setBrush(m_barset->brush());
149 setLabel(m_barset->name());
149 setLabel(m_barset->name());
150 }
150 }
151
151
152 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
152 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
153
153
154 PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend),
154 PieLegendMarker::PieLegendMarker(QPieSeries* series,QPieSlice *pieslice, QLegend *legend) : LegendMarker(series,legend),
155 m_pieslice(pieslice)
155 m_pieslice(pieslice)
156 {
156 {
157 QObject::connect(this, SIGNAL(selected()),pieslice, SIGNAL(selected()));
157 QObject::connect(this, SIGNAL(selected()),pieslice, SIGNAL(clicked()));
158 QObject::connect(pieslice, SIGNAL(changed()), this, SLOT(updated()));
158 QObject::connect(pieslice, SIGNAL(changed()), this, SLOT(updated()));
159 updated();
159 updated();
160 }
160 }
161
161
162 void PieLegendMarker::updated()
162 void PieLegendMarker::updated()
163 {
163 {
164 setBrush(m_pieslice->brush());
164 setBrush(m_pieslice->brush());
165 setLabel(m_pieslice->label());
165 setLabel(m_pieslice->label());
166 }
166 }
167
167
168 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
168 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
169
169
170 XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend),
170 XYLegendMarker::XYLegendMarker(QXYSeries *series, QLegend *legend) : LegendMarker(series,legend),
171 m_series(series)
171 m_series(series)
172 {
172 {
173 QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
173 QObject::connect(this, SIGNAL(selected()), series, SIGNAL(selected()));
174 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
174 QObject::connect(series->d_func(),SIGNAL(updated()), this, SLOT(updated()));
175 updated();
175 updated();
176 }
176 }
177
177
178 void XYLegendMarker::updated()
178 void XYLegendMarker::updated()
179 {
179 {
180 setLabel(m_series->name());
180 setLabel(m_series->name());
181
181
182 if(m_series->type()== QAbstractSeries::SeriesTypeScatter)
182 if(m_series->type()== QAbstractSeries::SeriesTypeScatter)
183 {
183 {
184 setBrush(m_series->brush());
184 setBrush(m_series->brush());
185
185
186 }
186 }
187 else {
187 else {
188 setBrush(QBrush(m_series->pen().color()));
188 setBrush(QBrush(m_series->pen().color()));
189 }
189 }
190 }
190 }
191
191
192 #include "moc_legendmarker_p.cpp"
192 #include "moc_legendmarker_p.cpp"
193
193
194 QTCOMMERCIALCHART_END_NAMESPACE
194 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,460 +1,463
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qlegend.h"
21 #include "qlegend.h"
22 #include "qlegend_p.h"
22 #include "qlegend_p.h"
23 #include "qabstractseries.h"
23 #include "qabstractseries.h"
24 #include "qabstractseries_p.h"
24 #include "qabstractseries_p.h"
25 #include "qchart_p.h"
25 #include "qchart_p.h"
26
26
27 #include "legendmarker_p.h"
27 #include "legendmarker_p.h"
28 #include "qxyseries.h"
28 #include "qxyseries.h"
29 #include "qlineseries.h"
29 #include "qlineseries.h"
30 #include "qareaseries.h"
30 #include "qareaseries.h"
31 #include "qscatterseries.h"
31 #include "qscatterseries.h"
32 #include "qsplineseries.h"
32 #include "qsplineseries.h"
33 #include "qbarseries.h"
33 #include "qbarseries.h"
34 #include "qstackedbarseries.h"
34 #include "qstackedbarseries.h"
35 #include "qpercentbarseries.h"
35 #include "qpercentbarseries.h"
36 #include "qbarset.h"
36 #include "qbarset.h"
37 #include "qpieseries.h"
37 #include "qpieseries.h"
38 #include "qpieseries_p.h"
38 #include "qpieslice.h"
39 #include "qpieslice.h"
39 #include "chartpresenter_p.h"
40 #include "chartpresenter_p.h"
40 #include <QPainter>
41 #include <QPainter>
41 #include <QPen>
42 #include <QPen>
42 #include <QTimer>
43 #include <QTimer>
43
44
44 #include <QGraphicsSceneEvent>
45 #include <QGraphicsSceneEvent>
45
46
46 QTCOMMERCIALCHART_BEGIN_NAMESPACE
47 QTCOMMERCIALCHART_BEGIN_NAMESPACE
47
48
48 /*!
49 /*!
49 \class QLegend
50 \class QLegend
50 \brief part of QtCommercial chart API.
51 \brief part of QtCommercial chart API.
51 \mainclass
52 \mainclass
52
53
53 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
54 QLegend is a graphical object, whics displays legend of the chart. Legend state is updated by QChart, when
54 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
55 series have been changed. By default, legend is drawn by QChart, but user can set a new parent to legend and
55 handle the drawing manually.
56 handle the drawing manually.
56 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
57 User isn't supposed to create or delete legend objects, but can reference it via QChart class.
57
58
58 \image examples_percentbarchart_legend.png
59 \image examples_percentbarchart_legend.png
59
60
60 \sa QChart
61 \sa QChart
61 */
62 */
62
63
63 /*!
64 /*!
64 \enum QLegend::Alignment
65 \enum QLegend::Alignment
65
66
66 This enum describes the possible position for legend inside chart.
67 This enum describes the possible position for legend inside chart.
67
68
68 \value AlignmentTop
69 \value AlignmentTop
69 \value AlignmentBottom
70 \value AlignmentBottom
70 \value AlignmentLeft
71 \value AlignmentLeft
71 \value AlignmentRight
72 \value AlignmentRight
72 */
73 */
73
74
74 /*!
75 /*!
75 \fn qreal QLegend::minWidth() const
76 \fn qreal QLegend::minWidth() const
76 Returns minimum width of the legend
77 Returns minimum width of the legend
77 */
78 */
78
79
79 /*!
80 /*!
80 \fn qreal QLegend::minHeight() const
81 \fn qreal QLegend::minHeight() const
81 Returns minimum height of the legend
82 Returns minimum height of the legend
82 */
83 */
83
84
84 /*!
85 /*!
85 Constructs the legend object and sets the parent to \a parent
86 Constructs the legend object and sets the parent to \a parent
86 */
87 */
87
88
88 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
89 QLegend::QLegend(QChart *chart):QGraphicsWidget(chart),
89 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,this))
90 d_ptr(new QLegendPrivate(chart->d_ptr->m_presenter,this))
90 {
91 {
91 setZValue(ChartPresenter::LegendZValue);
92 setZValue(ChartPresenter::LegendZValue);
92 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
93 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
93 setEnabled(false); // By default legend is disabled
94 setEnabled(false); // By default legend is disabled
94 setVisible(false);
95 setVisible(false);
95 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries *, Domain *)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries *,Domain*)));
96 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesAdded(QAbstractSeries *, Domain *)),d_ptr.data(),SLOT(handleSeriesAdded(QAbstractSeries *,Domain*)));
96 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries *)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries *)));
97 QObject::connect(chart->d_ptr->m_dataset,SIGNAL(seriesRemoved(QAbstractSeries *)),d_ptr.data(),SLOT(handleSeriesRemoved(QAbstractSeries *)));
97 }
98 }
98
99
99 /*!
100 /*!
100 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
101 Destroys the legend object. Legend is always owned by a QChart, so an application should never call this.
101 */
102 */
102 QLegend::~QLegend()
103 QLegend::~QLegend()
103 {
104 {
104 }
105 }
105
106
106 /*!
107 /*!
107 Paints the legend to given \a painter. Paremeters \a option and \a widget arent used.
108 Paints the legend to given \a painter. Paremeters \a option and \a widget arent used.
108 */
109 */
109
110
110 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
111 void QLegend::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
111 {
112 {
112 Q_UNUSED(option)
113 Q_UNUSED(option)
113 Q_UNUSED(widget)
114 Q_UNUSED(widget)
114 if(!d_ptr->m_backgroundVisible) return;
115 if(!d_ptr->m_backgroundVisible) return;
115
116
116 painter->setOpacity(opacity());
117 painter->setOpacity(opacity());
117 painter->setPen(d_ptr->m_pen);
118 painter->setPen(d_ptr->m_pen);
118 painter->setBrush(d_ptr->m_brush);
119 painter->setBrush(d_ptr->m_brush);
119 painter->drawRect(boundingRect());
120 painter->drawRect(boundingRect());
120 }
121 }
121
122
122 /*!
123 /*!
123 Bounding rect of legend.
124 Bounding rect of legend.
124 */
125 */
125
126
126 QRectF QLegend::boundingRect() const
127 QRectF QLegend::boundingRect() const
127 {
128 {
128 return d_ptr->m_rect;
129 return d_ptr->m_rect;
129 }
130 }
130
131
131 /*!
132 /*!
132 Sets the \a brush of legend. Brush affects the background of legend.
133 Sets the \a brush of legend. Brush affects the background of legend.
133 */
134 */
134 void QLegend::setBrush(const QBrush &brush)
135 void QLegend::setBrush(const QBrush &brush)
135 {
136 {
136 if (d_ptr->m_brush != brush) {
137 if (d_ptr->m_brush != brush) {
137 d_ptr->m_brush = brush;
138 d_ptr->m_brush = brush;
138 update();
139 update();
139 }
140 }
140 }
141 }
141
142
142 /*!
143 /*!
143 Returns the brush used by legend.
144 Returns the brush used by legend.
144 */
145 */
145 QBrush QLegend::brush() const
146 QBrush QLegend::brush() const
146 {
147 {
147 return d_ptr->m_brush;
148 return d_ptr->m_brush;
148 }
149 }
149
150
150 /*!
151 /*!
151 Sets the \a pen of legend. Pen affects the legend borders.
152 Sets the \a pen of legend. Pen affects the legend borders.
152 */
153 */
153 void QLegend::setPen(const QPen &pen)
154 void QLegend::setPen(const QPen &pen)
154 {
155 {
155 if (d_ptr->m_pen != pen) {
156 if (d_ptr->m_pen != pen) {
156 d_ptr->m_pen = pen;
157 d_ptr->m_pen = pen;
157 update();
158 update();
158 }
159 }
159 }
160 }
160
161
161 /*!
162 /*!
162 Returns the pen used by legend
163 Returns the pen used by legend
163 */
164 */
164
165
165 QPen QLegend::pen() const
166 QPen QLegend::pen() const
166 {
167 {
167 return d_ptr->m_pen;
168 return d_ptr->m_pen;
168 }
169 }
169
170
170 /*!
171 /*!
171 Sets the \a alignment for legend. Legend tries to paint itself on the defined position in chart.
172 Sets the \a alignment for legend. Legend tries to paint itself on the defined position in chart.
172 \sa QLegend::Alignment
173 \sa QLegend::Alignment
173 */
174 */
174 void QLegend::setAlignment(QLegend::Alignments alignment)
175 void QLegend::setAlignment(QLegend::Alignments alignment)
175 {
176 {
176 if(d_ptr->m_alignment!=alignment && d_ptr->m_attachedToChart) {
177 if(d_ptr->m_alignment!=alignment && d_ptr->m_attachedToChart) {
177 d_ptr->m_alignment = alignment;
178 d_ptr->m_alignment = alignment;
178 d_ptr->updateLayout();
179 d_ptr->updateLayout();
179 }
180 }
180 }
181 }
181
182
182 /*!
183 /*!
183 Returns the preferred layout for legend
184 Returns the preferred layout for legend
184 */
185 */
185 QLegend::Alignments QLegend::alignment() const
186 QLegend::Alignments QLegend::alignment() const
186 {
187 {
187 return d_ptr->m_alignment;
188 return d_ptr->m_alignment;
188 }
189 }
189
190
190 /*!
191 /*!
191 Detaches the legend from chart. Chart won't change layout of the legend.
192 Detaches the legend from chart. Chart won't change layout of the legend.
192 */
193 */
193 void QLegend::detachFromChart()
194 void QLegend::detachFromChart()
194 {
195 {
195 d_ptr->m_attachedToChart = false;
196 d_ptr->m_attachedToChart = false;
196 }
197 }
197
198
198 /*!
199 /*!
199 Attaches the legend to chart. Chart may change layout of the legend.
200 Attaches the legend to chart. Chart may change layout of the legend.
200 */
201 */
201 void QLegend::attachToChart()
202 void QLegend::attachToChart()
202 {
203 {
203 d_ptr->m_attachedToChart = true;
204 d_ptr->m_attachedToChart = true;
204 }
205 }
205
206
206 /*!
207 /*!
207 Returns true, if legend is attached to chart.
208 Returns true, if legend is attached to chart.
208 */
209 */
209 bool QLegend::isAttachedToChart()
210 bool QLegend::isAttachedToChart()
210 {
211 {
211 return d_ptr->m_attachedToChart;
212 return d_ptr->m_attachedToChart;
212 }
213 }
213
214
214 /*!
215 /*!
215 Sets the legend's scrolling offset to value defined by \a point.
216 Sets the legend's scrolling offset to value defined by \a point.
216 */
217 */
217 void QLegend::setOffset(const QPointF& point)
218 void QLegend::setOffset(const QPointF& point)
218 {
219 {
219 d_ptr->setOffset(point.x(),point.y());
220 d_ptr->setOffset(point.x(),point.y());
220 }
221 }
221
222
222 /*!
223 /*!
223 Returns the legend's scrolling offset.
224 Returns the legend's scrolling offset.
224 */
225 */
225 QPointF QLegend::offset() const
226 QPointF QLegend::offset() const
226 {
227 {
227 return QPointF(d_ptr->m_offsetX,d_ptr->m_offsetY);
228 return QPointF(d_ptr->m_offsetX,d_ptr->m_offsetY);
228 }
229 }
229
230
230 /*!
231 /*!
231 Sets the visibility of legend background to \a visible
232 Sets the visibility of legend background to \a visible
232 */
233 */
233 void QLegend::setBackgroundVisible(bool visible)
234 void QLegend::setBackgroundVisible(bool visible)
234 {
235 {
235 if(d_ptr->m_backgroundVisible!=visible)
236 if(d_ptr->m_backgroundVisible!=visible)
236 {
237 {
237 d_ptr->m_backgroundVisible=visible;
238 d_ptr->m_backgroundVisible=visible;
238 update();
239 update();
239 }
240 }
240 }
241 }
241
242
242 /*!
243 /*!
243 Returns the visibility of legend background
244 Returns the visibility of legend background
244 */
245 */
245 bool QLegend::isBackgroundVisible() const
246 bool QLegend::isBackgroundVisible() const
246 {
247 {
247 return d_ptr->m_backgroundVisible;
248 return d_ptr->m_backgroundVisible;
248 }
249 }
249
250
250 /*!
251 /*!
251 \internal \a event see QGraphicsWidget for details
252 \internal \a event see QGraphicsWidget for details
252 */
253 */
253 void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event)
254 void QLegend::resizeEvent(QGraphicsSceneResizeEvent *event)
254 {
255 {
255 const QRectF& rect = QRectF(QPoint(0,0),event->newSize());
256 const QRectF& rect = QRectF(QPoint(0,0),event->newSize());
256 QGraphicsWidget::resizeEvent(event);
257 QGraphicsWidget::resizeEvent(event);
257 if(d_ptr->m_rect != rect) {
258 if(d_ptr->m_rect != rect) {
258 d_ptr->m_rect = rect;
259 d_ptr->m_rect = rect;
259 d_ptr->updateLayout();
260 d_ptr->updateLayout();
260 }
261 }
261 }
262 }
262
263
263 /*!
264 /*!
264 \internal \a event see QGraphicsWidget for details
265 \internal \a event see QGraphicsWidget for details
265 */
266 */
266 void QLegend::hideEvent(QHideEvent *event)
267 void QLegend::hideEvent(QHideEvent *event)
267 {
268 {
268 QGraphicsWidget::hideEvent(event);
269 QGraphicsWidget::hideEvent(event);
269 setEnabled(false);
270 setEnabled(false);
270 d_ptr->updateLayout();
271 d_ptr->updateLayout();
271 }
272 }
272
273
273 /*!
274 /*!
274 \internal \a event see QGraphicsWidget for details
275 \internal \a event see QGraphicsWidget for details
275 */
276 */
276 void QLegend::showEvent(QShowEvent *event)
277 void QLegend::showEvent(QShowEvent *event)
277 {
278 {
278 QGraphicsWidget::showEvent(event);
279 QGraphicsWidget::showEvent(event);
279 setEnabled(true);
280 setEnabled(true);
280 d_ptr->updateLayout();
281 d_ptr->updateLayout();
281 }
282 }
282
283
283 qreal QLegend::minWidth() const
284 qreal QLegend::minWidth() const
284 {
285 {
285 return d_ptr->m_minWidth;
286 return d_ptr->m_minWidth;
286 }
287 }
287
288
288 qreal QLegend::minHeight() const
289 qreal QLegend::minHeight() const
289 {
290 {
290 return d_ptr->m_minHeight;
291 return d_ptr->m_minHeight;
291 }
292 }
292
293
293 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
294 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
294
295
295 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter,QLegend *q):
296 QLegendPrivate::QLegendPrivate(ChartPresenter* presenter,QLegend *q):
296 q_ptr(q),
297 q_ptr(q),
297 m_presenter(presenter),
298 m_presenter(presenter),
298 m_markers(new QGraphicsItemGroup(q)),
299 m_markers(new QGraphicsItemGroup(q)),
299 m_alignment(QLegend::AlignmentTop),
300 m_alignment(QLegend::AlignmentTop),
300 m_offsetX(0),
301 m_offsetX(0),
301 m_offsetY(0),
302 m_offsetY(0),
302 m_minWidth(0),
303 m_minWidth(0),
303 m_minHeight(0),
304 m_minHeight(0),
304 m_width(0),
305 m_width(0),
305 m_height(0),
306 m_height(0),
306 m_attachedToChart(true),
307 m_attachedToChart(true),
307 m_backgroundVisible(false)
308 m_backgroundVisible(false)
308 {
309 {
309
310
310 }
311 }
311
312
312 QLegendPrivate::~QLegendPrivate()
313 QLegendPrivate::~QLegendPrivate()
313 {
314 {
314
315
315 }
316 }
316
317
317 void QLegendPrivate::setOffset(qreal x, qreal y)
318 void QLegendPrivate::setOffset(qreal x, qreal y)
318 {
319 {
319
320
320 switch(m_alignment) {
321 switch(m_alignment) {
321
322
322 case QLegend::AlignmentTop:
323 case QLegend::AlignmentTop:
323 case QLegend::AlignmentBottom: {
324 case QLegend::AlignmentBottom: {
324 if(m_width<=m_rect.width()) return;
325 if(m_width<=m_rect.width()) return;
325
326
326 if (x != m_offsetX) {
327 if (x != m_offsetX) {
327 m_offsetX = qBound(qreal(0), x, m_width - m_rect.width());
328 m_offsetX = qBound(qreal(0), x, m_width - m_rect.width());
328 m_markers->setPos(-m_offsetX,m_rect.top());
329 m_markers->setPos(-m_offsetX,m_rect.top());
329 }
330 }
330 break;
331 break;
331 }
332 }
332 case QLegend::AlignmentLeft:
333 case QLegend::AlignmentLeft:
333 case QLegend::AlignmentRight: {
334 case QLegend::AlignmentRight: {
334
335
335 if(m_height<=m_rect.height()) return;
336 if(m_height<=m_rect.height()) return;
336
337
337 if (y != m_offsetY) {
338 if (y != m_offsetY) {
338 m_offsetY = qBound(qreal(0), y, m_height - m_rect.height());
339 m_offsetY = qBound(qreal(0), y, m_height - m_rect.height());
339 m_markers->setPos(m_rect.left(),-m_offsetY);
340 m_markers->setPos(m_rect.left(),-m_offsetY);
340 }
341 }
341 break;
342 break;
342 }
343 }
343 }
344 }
344 }
345 }
345
346
346
347
347 void QLegendPrivate::updateLayout()
348 void QLegendPrivate::updateLayout()
348 {
349 {
349 m_offsetX=0;
350 m_offsetX=0;
350 QList<QGraphicsItem *> items = m_markers->childItems();
351 QList<QGraphicsItem *> items = m_markers->childItems();
351
352
352 if(items.isEmpty()) return;
353 if(items.isEmpty()) return;
353
354
354 m_minWidth=0;
355 m_minWidth=0;
355 m_minHeight=0;
356 m_minHeight=0;
356
357
357 switch(m_alignment) {
358 switch(m_alignment) {
358
359
359 case QLegend::AlignmentTop:
360 case QLegend::AlignmentTop:
360 case QLegend::AlignmentBottom: {
361 case QLegend::AlignmentBottom: {
361 QPointF point = m_rect.topLeft();
362 QPointF point = m_rect.topLeft();
362 m_width = 0;
363 m_width = 0;
363 foreach (QGraphicsItem *item, items) {
364 foreach (QGraphicsItem *item, items) {
364 item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2);
365 item->setPos(point.x(),m_rect.height()/2 -item->boundingRect().height()/2);
365 const QRectF& rect = item->boundingRect();
366 const QRectF& rect = item->boundingRect();
366 qreal w = rect.width();
367 qreal w = rect.width();
367 m_minWidth=qMax(m_minWidth,w);
368 m_minWidth=qMax(m_minWidth,w);
368 m_minHeight=qMax(m_minHeight,rect.height());
369 m_minHeight=qMax(m_minHeight,rect.height());
369 m_width+=w;
370 m_width+=w;
370 point.setX(point.x() + w);
371 point.setX(point.x() + w);
371 }
372 }
372 if(m_width<m_rect.width()) {
373 if(m_width<m_rect.width()) {
373 m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top());
374 m_markers->setPos(m_rect.width()/2-m_width/2,m_rect.top());
374 }
375 }
375 else {
376 else {
376 m_markers->setPos(m_rect.topLeft());
377 m_markers->setPos(m_rect.topLeft());
377 }
378 }
378 m_height=m_minHeight;
379 m_height=m_minHeight;
379 }
380 }
380 break;
381 break;
381 case QLegend::AlignmentLeft:
382 case QLegend::AlignmentLeft:
382 case QLegend::AlignmentRight: {
383 case QLegend::AlignmentRight: {
383 QPointF point = m_rect.topLeft();
384 QPointF point = m_rect.topLeft();
384 m_height = 0;
385 m_height = 0;
385 foreach (QGraphicsItem *item, items) {
386 foreach (QGraphicsItem *item, items) {
386 item->setPos(point);
387 item->setPos(point);
387 const QRectF& rect = item->boundingRect();
388 const QRectF& rect = item->boundingRect();
388 qreal h = rect.height();
389 qreal h = rect.height();
389 m_minWidth=qMax(m_minWidth,rect.width());
390 m_minWidth=qMax(m_minWidth,rect.width());
390 m_minHeight=qMax(m_minHeight,h);
391 m_minHeight=qMax(m_minHeight,h);
391 m_height+=h;
392 m_height+=h;
392 point.setY(point.y() + h);
393 point.setY(point.y() + h);
393 }
394 }
394 if(m_height<m_rect.height()) {
395 if(m_height<m_rect.height()) {
395 m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2);
396 m_markers->setPos(m_rect.left(),m_rect.height()/2-m_height/2);
396 }
397 }
397 else {
398 else {
398 m_markers->setPos(m_rect.topLeft());
399 m_markers->setPos(m_rect.topLeft());
399 }
400 }
400 m_width=m_minWidth;
401 m_width=m_minWidth;
401 }
402 }
402 break;
403 break;
403 }
404 }
404
405
405 m_presenter->updateLayout();
406 m_presenter->updateLayout();
406 }
407 }
407
408
408 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
409 void QLegendPrivate::handleSeriesAdded(QAbstractSeries *series, Domain *domain)
409 {
410 {
410 Q_UNUSED(domain)
411 Q_UNUSED(domain)
411
412
412 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
413 QList<LegendMarker*> markers = series->d_ptr->createLegendMarker(q_ptr);
413 foreach(LegendMarker* marker , markers)
414 foreach(LegendMarker* marker , markers)
414 m_markers->addToGroup(marker);
415 m_markers->addToGroup(marker);
415
416
416 if(series->type() == QAbstractSeries::SeriesTypePie)
417 if(series->type() == QAbstractSeries::SeriesTypePie)
417 {
418 {
418 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
419 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
419 QObject::connect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleUpdateSeries()));
420 QPieSeriesPrivate *d = QPieSeriesPrivate::seriesData(*pieSeries);
420 QObject::connect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleUpdateSeries()));
421 QObject::connect(d, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
422 QObject::connect(d, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
421 }
423 }
422
424
423 updateLayout();
425 updateLayout();
424 }
426 }
425
427
426 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
428 void QLegendPrivate::handleSeriesRemoved(QAbstractSeries *series)
427 {
429 {
428
430
429 QList<QGraphicsItem *> items = m_markers->childItems();
431 QList<QGraphicsItem *> items = m_markers->childItems();
430
432
431 foreach (QGraphicsItem *markers, items) {
433 foreach (QGraphicsItem *markers, items) {
432 LegendMarker *marker = static_cast<LegendMarker*>(markers);
434 LegendMarker *marker = static_cast<LegendMarker*>(markers);
433 if (marker->series() == series) {
435 if (marker->series() == series) {
434 delete marker;
436 delete marker;
435 }
437 }
436 }
438 }
437
439
438 if(series->type() == QAbstractSeries::SeriesTypePie)
440 if(series->type() == QAbstractSeries::SeriesTypePie)
439 {
441 {
440 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
442 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
441 QObject::disconnect(pieSeries,SIGNAL(added(QList<QPieSlice*>)),this,SLOT(handleUpdateSeries()));
443 QPieSeriesPrivate *d = QPieSeriesPrivate::seriesData(*pieSeries);
442 QObject::disconnect(pieSeries,SIGNAL(removed(QList<QPieSlice*>)),this,SLOT(handleUpdateSeries()));
444 QObject::disconnect(d, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
445 QObject::disconnect(d, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleUpdatePieSeries()));
443 }
446 }
444
447
445 updateLayout();
448 updateLayout();
446 }
449 }
447
450
448 void QLegendPrivate::handleUpdateSeries()
451 void QLegendPrivate::handleUpdatePieSeries()
449 {
452 {
450 //TODO: reimplement to be optimal
453 //TODO: reimplement to be optimal
451 QAbstractSeries* series = qobject_cast<QAbstractSeries *> (sender());
454 QPieSeriesPrivate* d = qobject_cast<QPieSeriesPrivate *> (sender());
452 Q_ASSERT(series);
455 Q_ASSERT(d->q_func());
453 handleSeriesRemoved(series);
456 handleSeriesRemoved(d->q_func());
454 handleSeriesAdded(series,0);
457 handleSeriesAdded(d->q_func(), 0);
455 }
458 }
456
459
457 #include "moc_qlegend.cpp"
460 #include "moc_qlegend.cpp"
458 #include "moc_qlegend_p.cpp"
461 #include "moc_qlegend_p.cpp"
459
462
460 QTCOMMERCIALCHART_END_NAMESPACE
463 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,78 +1,78
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef QLEGEND_P_H
30 #ifndef QLEGEND_P_H
31 #define QLEGEND_P_H
31 #define QLEGEND_P_H
32
32
33 #include "qlegend.h"
33 #include "qlegend.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class ChartPresenter;
37 class ChartPresenter;
38 class QAbstractSeries;
38 class QAbstractSeries;
39
39
40 class QLegendPrivate : public QObject
40 class QLegendPrivate : public QObject
41 {
41 {
42 Q_OBJECT
42 Q_OBJECT
43 public:
43 public:
44 QLegendPrivate(ChartPresenter *chart,QLegend *q);
44 QLegendPrivate(ChartPresenter *chart,QLegend *q);
45 ~QLegendPrivate();
45 ~QLegendPrivate();
46
46
47 void setOffset(qreal x, qreal y);
47 void setOffset(qreal x, qreal y);
48 void updateLayout();
48 void updateLayout();
49
49
50 public Q_SLOTS:
50 public Q_SLOTS:
51 void handleSeriesAdded(QAbstractSeries *series, Domain *domain);
51 void handleSeriesAdded(QAbstractSeries *series, Domain *domain);
52 void handleSeriesRemoved(QAbstractSeries *series);
52 void handleSeriesRemoved(QAbstractSeries *series);
53 void handleUpdateSeries(); //TODO remove this function
53 void handleUpdatePieSeries(); //TODO remove this function
54
54
55 private:
55 private:
56 QLegend *q_ptr;
56 QLegend *q_ptr;
57 ChartPresenter *m_presenter;
57 ChartPresenter *m_presenter;
58 QGraphicsItemGroup* m_markers;
58 QGraphicsItemGroup* m_markers;
59 QLegend::Alignments m_alignment;
59 QLegend::Alignments m_alignment;
60 QBrush m_brush;
60 QBrush m_brush;
61 QPen m_pen;
61 QPen m_pen;
62 QRectF m_rect;
62 QRectF m_rect;
63 qreal m_offsetX;
63 qreal m_offsetX;
64 qreal m_offsetY;
64 qreal m_offsetY;
65 qreal m_minWidth;
65 qreal m_minWidth;
66 qreal m_minHeight;
66 qreal m_minHeight;
67 qreal m_width;
67 qreal m_width;
68 qreal m_height;
68 qreal m_height;
69 bool m_attachedToChart;
69 bool m_attachedToChart;
70 bool m_backgroundVisible;
70 bool m_backgroundVisible;
71
71
72 friend class QLegend;
72 friend class QLegend;
73
73
74 };
74 };
75
75
76 QTCOMMERCIALCHART_END_NAMESPACE
76 QTCOMMERCIALCHART_END_NAMESPACE
77
77
78 #endif
78 #endif
@@ -1,210 +1,212
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "piechartitem_p.h"
21 #include "piechartitem_p.h"
22 #include "piesliceitem_p.h"
22 #include "piesliceitem_p.h"
23 #include "qpieslice.h"
23 #include "qpieslice.h"
24 #include "qpieseries.h"
24 #include "qpieseries.h"
25 #include "qpieseries_p.h"
25 #include "chartpresenter_p.h"
26 #include "chartpresenter_p.h"
26 #include "chartdataset_p.h"
27 #include "chartdataset_p.h"
27 #include "chartanimator_p.h"
28 #include "chartanimator_p.h"
28 #include <QPainter>
29 #include <QPainter>
29 #include <QTimer>
30 #include <QTimer>
30
31
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
33
33 PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter)
34 PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter)
34 :ChartItem(presenter),
35 :ChartItem(presenter),
35 m_series(series)
36 m_series(series)
36 {
37 {
37 Q_ASSERT(series);
38 Q_ASSERT(series);
38 connect(series, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>)));
39
39 connect(series, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleSlicesRemoved(QList<QPieSlice*>)));
40 QPieSeriesPrivate *d = QPieSeriesPrivate::seriesData(*series);
40 connect(series, SIGNAL(piePositionChanged()), this, SLOT(handlePieLayoutChanged()));
41 connect(d, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>)));
41 connect(series, SIGNAL(pieSizeChanged()), this, SLOT(handlePieLayoutChanged()));
42 connect(d, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleSlicesRemoved(QList<QPieSlice*>)));
43 connect(d, SIGNAL(piePositionChanged()), this, SLOT(handlePieLayoutChanged()));
44 connect(d, SIGNAL(pieSizeChanged()), this, SLOT(handlePieLayoutChanged()));
42
45
43 QTimer::singleShot(0, this, SLOT(initialize())); // TODO: get rid of this
46 QTimer::singleShot(0, this, SLOT(initialize())); // TODO: get rid of this
44
47
45 // Note: the following does not affect as long as the item does not have anything to paint
48 // Note: the following does not affect as long as the item does not have anything to paint
46 setZValue(ChartPresenter::PieSeriesZValue);
49 setZValue(ChartPresenter::PieSeriesZValue);
47 }
50 }
48
51
49 PieChartItem::~PieChartItem()
52 PieChartItem::~PieChartItem()
50 {
53 {
51 // slices deleted automatically through QGraphicsItem
54 // slices deleted automatically through QGraphicsItem
52 }
55 }
53
56
54 void PieChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
57 void PieChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
55 {
58 {
56 Q_UNUSED(painter)
59 Q_UNUSED(painter)
57 // TODO: paint shadows for all components
60 // TODO: paint shadows for all components
58 // - get paths from items & merge & offset and draw with shadow color?
61 // - get paths from items & merge & offset and draw with shadow color?
59 //painter->setBrush(QBrush(Qt::red));
62 //painter->setBrush(QBrush(Qt::red));
60 //painter->drawRect(m_debugRect);
63 //painter->drawRect(m_debugRect);
61 }
64 }
62
65
63 void PieChartItem::initialize()
66 void PieChartItem::initialize()
64 {
67 {
65 handleSlicesAdded(m_series->slices());
68 handleSlicesAdded(m_series->slices());
66 }
69 }
67
70
68 void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices)
71 void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices)
69 {
72 {
70 bool isEmpty = m_slices.isEmpty();
73 bool isEmpty = m_slices.isEmpty();
71
74
72 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
75 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
73
76
74 foreach (QPieSlice *s, slices) {
77 foreach (QPieSlice *s, slices) {
75 PieSliceItem* item = new PieSliceItem(this);
78 PieSliceItem* item = new PieSliceItem(this);
76 m_slices.insert(s, item);
79 m_slices.insert(s, item);
77 connect(s, SIGNAL(changed()), this, SLOT(handleSliceChanged()));
80 connect(s, SIGNAL(changed()), this, SLOT(handleSliceChanged()));
78 connect(item, SIGNAL(clicked(Qt::MouseButtons)), s, SIGNAL(clicked(Qt::MouseButtons)));
81 connect(item, SIGNAL(clicked(Qt::MouseButtons)), s, SIGNAL(clicked()));
79 connect(item, SIGNAL(hoverEnter()), s, SIGNAL(hoverEnter()));
82 connect(item, SIGNAL(hovered(bool)), s, SIGNAL(hovered(bool)));
80 connect(item, SIGNAL(hoverLeave()), s, SIGNAL(hoverLeave()));
81
83
82 PieSliceData data = sliceData(s);
84 PieSliceData data = sliceData(s);
83
85
84 if (animator())
86 if (animator())
85 animator()->addAnimation(this, s, data, isEmpty);
87 animator()->addAnimation(this, s, data, isEmpty);
86 else
88 else
87 setLayout(s, data);
89 setLayout(s, data);
88 }
90 }
89 }
91 }
90
92
91 void PieChartItem::handleSlicesRemoved(QList<QPieSlice*> slices)
93 void PieChartItem::handleSlicesRemoved(QList<QPieSlice*> slices)
92 {
94 {
93 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
95 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
94
96
95 foreach (QPieSlice *s, slices) {
97 foreach (QPieSlice *s, slices) {
96 if (animator())
98 if (animator())
97 animator()->removeAnimation(this, s);
99 animator()->removeAnimation(this, s);
98 else
100 else
99 destroySlice(s);
101 destroySlice(s);
100 }
102 }
101 }
103 }
102
104
103 void PieChartItem::handlePieLayoutChanged()
105 void PieChartItem::handlePieLayoutChanged()
104 {
106 {
105 PieLayout layout = calculateLayout();
107 PieLayout layout = calculateLayout();
106 applyLayout(layout);
108 applyLayout(layout);
107 update();
109 update();
108 }
110 }
109
111
110 void PieChartItem::handleSliceChanged()
112 void PieChartItem::handleSliceChanged()
111 {
113 {
112 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
114 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
113 Q_ASSERT(m_slices.contains(slice));
115 Q_ASSERT(m_slices.contains(slice));
114 PieSliceData data = sliceData(slice);
116 PieSliceData data = sliceData(slice);
115 updateLayout(slice, data);
117 updateLayout(slice, data);
116 update();
118 update();
117 }
119 }
118
120
119 void PieChartItem::handleDomainChanged(qreal, qreal, qreal, qreal)
121 void PieChartItem::handleDomainChanged(qreal, qreal, qreal, qreal)
120 {
122 {
121 // TODO
123 // TODO
122 }
124 }
123
125
124 void PieChartItem::handleGeometryChanged(const QRectF& rect)
126 void PieChartItem::handleGeometryChanged(const QRectF& rect)
125 {
127 {
126 prepareGeometryChange();
128 prepareGeometryChange();
127 m_rect = rect;
129 m_rect = rect;
128 handlePieLayoutChanged();
130 handlePieLayoutChanged();
129 }
131 }
130
132
131 void PieChartItem::calculatePieLayout()
133 void PieChartItem::calculatePieLayout()
132 {
134 {
133 // find pie center coordinates
135 // find pie center coordinates
134 m_pieCenter.setX(m_rect.left() + (m_rect.width() * m_series->horizontalPosition()));
136 m_pieCenter.setX(m_rect.left() + (m_rect.width() * m_series->horizontalPosition()));
135 m_pieCenter.setY(m_rect.top() + (m_rect.height() * m_series->verticalPosition()));
137 m_pieCenter.setY(m_rect.top() + (m_rect.height() * m_series->verticalPosition()));
136
138
137 // find maximum radius for pie
139 // find maximum radius for pie
138 m_pieRadius = m_rect.height() / 2;
140 m_pieRadius = m_rect.height() / 2;
139 if (m_rect.width() < m_rect.height())
141 if (m_rect.width() < m_rect.height())
140 m_pieRadius = m_rect.width() / 2;
142 m_pieRadius = m_rect.width() / 2;
141
143
142 // apply size factor
144 // apply size factor
143 m_pieRadius *= m_series->pieSize();
145 m_pieRadius *= m_series->pieSize();
144 }
146 }
145
147
146 PieSliceData PieChartItem::sliceData(QPieSlice *slice)
148 PieSliceData PieChartItem::sliceData(QPieSlice *slice)
147 {
149 {
148 // TODO: This function is kid of useless now. Refactor.
150 // TODO: This function is kid of useless now. Refactor.
149 PieSliceData sliceData = PieSliceData::data(slice);
151 PieSliceData sliceData = PieSliceData::data(slice);
150 sliceData.m_center = PieSliceItem::sliceCenter(m_pieCenter, m_pieRadius, slice);
152 sliceData.m_center = PieSliceItem::sliceCenter(m_pieCenter, m_pieRadius, slice);
151 sliceData.m_radius = m_pieRadius;
153 sliceData.m_radius = m_pieRadius;
152 return sliceData;
154 return sliceData;
153 }
155 }
154
156
155 PieLayout PieChartItem::calculateLayout()
157 PieLayout PieChartItem::calculateLayout()
156 {
158 {
157 calculatePieLayout();
159 calculatePieLayout();
158 PieLayout layout;
160 PieLayout layout;
159 foreach (QPieSlice* s, m_series->slices()) {
161 foreach (QPieSlice* s, m_series->slices()) {
160 if (m_slices.contains(s)) // calculate layout only for those slices that are already visible
162 if (m_slices.contains(s)) // calculate layout only for those slices that are already visible
161 layout.insert(s, sliceData(s));
163 layout.insert(s, sliceData(s));
162 }
164 }
163 return layout;
165 return layout;
164 }
166 }
165
167
166 void PieChartItem::applyLayout(const PieLayout &layout)
168 void PieChartItem::applyLayout(const PieLayout &layout)
167 {
169 {
168 if (animator())
170 if (animator())
169 animator()->updateLayout(this, layout);
171 animator()->updateLayout(this, layout);
170 else
172 else
171 setLayout(layout);
173 setLayout(layout);
172 }
174 }
173
175
174 void PieChartItem::updateLayout(QPieSlice *slice, const PieSliceData &sliceData)
176 void PieChartItem::updateLayout(QPieSlice *slice, const PieSliceData &sliceData)
175 {
177 {
176 if (animator())
178 if (animator())
177 animator()->updateLayout(this, slice, sliceData);
179 animator()->updateLayout(this, slice, sliceData);
178 else
180 else
179 setLayout(slice, sliceData);
181 setLayout(slice, sliceData);
180 }
182 }
181
183
182 void PieChartItem::setLayout(const PieLayout &layout)
184 void PieChartItem::setLayout(const PieLayout &layout)
183 {
185 {
184 foreach (QPieSlice *slice, layout.keys()) {
186 foreach (QPieSlice *slice, layout.keys()) {
185 PieSliceItem *item = m_slices.value(slice);
187 PieSliceItem *item = m_slices.value(slice);
186 Q_ASSERT(item);
188 Q_ASSERT(item);
187 item->setSliceData(layout.value(slice));
189 item->setSliceData(layout.value(slice));
188 item->updateGeometry();
190 item->updateGeometry();
189 item->update();
191 item->update();
190 }
192 }
191 }
193 }
192
194
193 void PieChartItem::setLayout(QPieSlice *slice, const PieSliceData &sliceData)
195 void PieChartItem::setLayout(QPieSlice *slice, const PieSliceData &sliceData)
194 {
196 {
195 // find slice
197 // find slice
196 PieSliceItem *item = m_slices.value(slice);
198 PieSliceItem *item = m_slices.value(slice);
197 Q_ASSERT(item);
199 Q_ASSERT(item);
198 item->setSliceData(sliceData);
200 item->setSliceData(sliceData);
199 item->updateGeometry();
201 item->updateGeometry();
200 item->update();
202 item->update();
201 }
203 }
202
204
203 void PieChartItem::destroySlice(QPieSlice *slice)
205 void PieChartItem::destroySlice(QPieSlice *slice)
204 {
206 {
205 delete m_slices.take(slice);
207 delete m_slices.take(slice);
206 }
208 }
207
209
208 #include "moc_piechartitem_p.cpp"
210 #include "moc_piechartitem_p.cpp"
209
211
210 QTCOMMERCIALCHART_END_NAMESPACE
212 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,212 +1,212
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "piesliceitem_p.h"
21 #include "piesliceitem_p.h"
22 #include "piechartitem_p.h"
22 #include "piechartitem_p.h"
23 #include "qpieseries.h"
23 #include "qpieseries.h"
24 #include "qpieslice.h"
24 #include "qpieslice.h"
25 #include "chartpresenter_p.h"
25 #include "chartpresenter_p.h"
26 #include <QPainter>
26 #include <QPainter>
27 #include <qmath.h>
27 #include <qmath.h>
28 #include <QGraphicsSceneEvent>
28 #include <QGraphicsSceneEvent>
29 #include <QTime>
29 #include <QTime>
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 #define PI 3.14159265 // TODO: is this defined in some header?
33 #define PI 3.14159265 // TODO: is this defined in some header?
34
34
35 QPointF offset(qreal angle, qreal length)
35 QPointF offset(qreal angle, qreal length)
36 {
36 {
37 qreal dx = qSin(angle*(PI/180)) * length;
37 qreal dx = qSin(angle*(PI/180)) * length;
38 qreal dy = qCos(angle*(PI/180)) * length;
38 qreal dy = qCos(angle*(PI/180)) * length;
39 return QPointF(dx, -dy);
39 return QPointF(dx, -dy);
40 }
40 }
41
41
42 PieSliceItem::PieSliceItem(QGraphicsItem* parent)
42 PieSliceItem::PieSliceItem(QGraphicsItem* parent)
43 :QGraphicsObject(parent)
43 :QGraphicsObject(parent)
44 {
44 {
45 setAcceptHoverEvents(true);
45 setAcceptHoverEvents(true);
46 setAcceptedMouseButtons(Qt::MouseButtonMask);
46 setAcceptedMouseButtons(Qt::MouseButtonMask);
47 setZValue(ChartPresenter::PieSeriesZValue);
47 setZValue(ChartPresenter::PieSeriesZValue);
48 }
48 }
49
49
50 PieSliceItem::~PieSliceItem()
50 PieSliceItem::~PieSliceItem()
51 {
51 {
52
52
53 }
53 }
54
54
55 QRectF PieSliceItem::boundingRect() const
55 QRectF PieSliceItem::boundingRect() const
56 {
56 {
57 return m_boundingRect;
57 return m_boundingRect;
58 }
58 }
59
59
60 QPainterPath PieSliceItem::shape() const
60 QPainterPath PieSliceItem::shape() const
61 {
61 {
62 // Don't include the label and label arm.
62 // Don't include the label and label arm.
63 // This is used to detect a mouse clicks. We do not want clicks from label.
63 // This is used to detect a mouse clicks. We do not want clicks from label.
64 return m_slicePath;
64 return m_slicePath;
65 }
65 }
66
66
67 void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
67 void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
68 {
68 {
69 painter->save();
69 painter->save();
70 painter->setClipRect(parentItem()->boundingRect());
70 painter->setClipRect(parentItem()->boundingRect());
71 painter->setPen(m_data.m_slicePen);
71 painter->setPen(m_data.m_slicePen);
72 painter->setBrush(m_data.m_sliceBrush);
72 painter->setBrush(m_data.m_sliceBrush);
73 painter->drawPath(m_slicePath);
73 painter->drawPath(m_slicePath);
74 painter->restore();
74 painter->restore();
75
75
76 if (m_data.m_isLabelVisible) {
76 if (m_data.m_isLabelVisible) {
77 painter->setClipRect(parentItem()->boundingRect());
77 painter->setClipRect(parentItem()->boundingRect());
78 painter->setPen(m_data.m_labelPen);
78 painter->setPen(m_data.m_labelPen);
79 painter->drawPath(m_labelArmPath);
79 painter->drawPath(m_labelArmPath);
80 // the pen color will affect the font color as well
80 // the pen color will affect the font color as well
81 painter->setFont(m_data.m_labelFont);
81 painter->setFont(m_data.m_labelFont);
82 painter->drawText(m_labelTextRect.bottomLeft(), m_data.m_labelText);
82 painter->drawText(m_labelTextRect.bottomLeft(), m_data.m_labelText);
83 }
83 }
84 }
84 }
85
85
86 void PieSliceItem::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
86 void PieSliceItem::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
87 {
87 {
88 emit hoverEnter(); // TODO: refactor emit hover(somebooleanvalue)
88 emit hovered(true);
89 }
89 }
90
90
91 void PieSliceItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
91 void PieSliceItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
92 {
92 {
93 emit hoverLeave(); // TODO: refactor emit hover(somebooleanvalue)
93 emit hovered(false);
94 }
94 }
95
95
96 void PieSliceItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
96 void PieSliceItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
97 {
97 {
98 emit clicked(event->buttons());
98 emit clicked(event->buttons());
99 }
99 }
100
100
101 void PieSliceItem::setSliceData(PieSliceData sliceData)
101 void PieSliceItem::setSliceData(PieSliceData sliceData)
102 {
102 {
103 m_data = sliceData;
103 m_data = sliceData;
104 }
104 }
105
105
106 void PieSliceItem::updateGeometry()
106 void PieSliceItem::updateGeometry()
107 {
107 {
108 if (m_data.m_radius <= 0)
108 if (m_data.m_radius <= 0)
109 return;
109 return;
110
110
111 prepareGeometryChange();
111 prepareGeometryChange();
112
112
113 // update slice path
113 // update slice path
114 qreal centerAngle;
114 qreal centerAngle;
115 QPointF armStart;
115 QPointF armStart;
116 m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, &centerAngle, &armStart);
116 m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, &centerAngle, &armStart);
117
117
118 // update text rect
118 // update text rect
119 m_labelTextRect = labelTextRect(m_data.m_labelFont, m_data.m_labelText);
119 m_labelTextRect = labelTextRect(m_data.m_labelFont, m_data.m_labelText);
120
120
121 // update label arm path
121 // update label arm path
122 QPointF labelTextStart;
122 QPointF labelTextStart;
123 m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart);
123 m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart);
124
124
125 // update text position
125 // update text position
126 m_labelTextRect.moveBottomLeft(labelTextStart);
126 m_labelTextRect.moveBottomLeft(labelTextStart);
127
127
128 // update bounding rect
128 // update bounding rect
129 if (m_data.m_isLabelVisible)
129 if (m_data.m_isLabelVisible)
130 m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect);
130 m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect);
131 else
131 else
132 m_boundingRect = m_slicePath.boundingRect();
132 m_boundingRect = m_slicePath.boundingRect();
133 }
133 }
134
134
135 QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice)
135 QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice)
136 {
136 {
137 if (slice->isExploded()) {
137 if (slice->isExploded()) {
138 qreal centerAngle = slice->startAngle() + ((slice->endAngle() - slice->startAngle())/2);
138 qreal centerAngle = slice->startAngle() + ((slice->endAngle() - slice->startAngle())/2);
139 qreal len = radius * slice->explodeDistanceFactor();
139 qreal len = radius * slice->explodeDistanceFactor();
140 qreal dx = qSin(centerAngle*(PI/180)) * len;
140 qreal dx = qSin(centerAngle*(PI/180)) * len;
141 qreal dy = -qCos(centerAngle*(PI/180)) * len;
141 qreal dy = -qCos(centerAngle*(PI/180)) * len;
142 point += QPointF(dx, dy);
142 point += QPointF(dx, dy);
143 }
143 }
144 return point;
144 return point;
145 }
145 }
146
146
147 QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF* armStart)
147 QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF* armStart)
148 {
148 {
149 // calculate center angle
149 // calculate center angle
150 *centerAngle = startAngle + (angleSpan/2);
150 *centerAngle = startAngle + (angleSpan/2);
151
151
152 // calculate slice rectangle
152 // calculate slice rectangle
153 QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2);
153 QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2);
154
154
155 // slice path
155 // slice path
156 // TODO: draw the shape so that it might have a hole in the center
156 // TODO: draw the shape so that it might have a hole in the center
157 QPainterPath path;
157 QPainterPath path;
158 path.moveTo(rect.center());
158 path.moveTo(rect.center());
159 path.arcTo(rect, -startAngle + 90, -angleSpan);
159 path.arcTo(rect, -startAngle + 90, -angleSpan);
160 path.closeSubpath();
160 path.closeSubpath();
161
161
162 // calculate label arm start point
162 // calculate label arm start point
163 *armStart = center;
163 *armStart = center;
164 *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP);
164 *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP);
165
165
166 return path;
166 return path;
167 }
167 }
168
168
169 QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart)
169 QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart)
170 {
170 {
171 // prevent label arm pointing straight down because it will look bad
171 // prevent label arm pointing straight down because it will look bad
172 if (angle < 180 && angle > 170)
172 if (angle < 180 && angle > 170)
173 angle = 170;
173 angle = 170;
174 if (angle > 180 && angle < 190)
174 if (angle > 180 && angle < 190)
175 angle = 190;
175 angle = 190;
176
176
177 // line from slice to label
177 // line from slice to label
178 qreal dx = qSin(angle*(PI/180)) * length;
178 qreal dx = qSin(angle*(PI/180)) * length;
179 qreal dy = -qCos(angle*(PI/180)) * length;
179 qreal dy = -qCos(angle*(PI/180)) * length;
180 QPointF parm1 = start + QPointF(dx, dy);
180 QPointF parm1 = start + QPointF(dx, dy);
181
181
182 // line to underline the label
182 // line to underline the label
183 QPointF parm2 = parm1;
183 QPointF parm2 = parm1;
184 if (angle < 180) { // arm swings the other way on the left side
184 if (angle < 180) { // arm swings the other way on the left side
185 parm2 += QPointF(textWidth, 0);
185 parm2 += QPointF(textWidth, 0);
186 *textStart = parm1;
186 *textStart = parm1;
187 }
187 }
188 else {
188 else {
189 parm2 += QPointF(-textWidth,0);
189 parm2 += QPointF(-textWidth,0);
190 *textStart = parm2;
190 *textStart = parm2;
191 }
191 }
192
192
193 // elevate the text position a bit so that it does not hit the line
193 // elevate the text position a bit so that it does not hit the line
194 *textStart += QPointF(0, -3);
194 *textStart += QPointF(0, -3);
195
195
196 QPainterPath path;
196 QPainterPath path;
197 path.moveTo(start);
197 path.moveTo(start);
198 path.lineTo(parm1);
198 path.lineTo(parm1);
199 path.lineTo(parm2);
199 path.lineTo(parm2);
200
200
201 return path;
201 return path;
202 }
202 }
203
203
204 QRectF PieSliceItem::labelTextRect(QFont font, QString text)
204 QRectF PieSliceItem::labelTextRect(QFont font, QString text)
205 {
205 {
206 QFontMetricsF fm(font);
206 QFontMetricsF fm(font);
207 return fm.boundingRect(text);
207 return fm.boundingRect(text);
208 }
208 }
209
209
210 #include "moc_piesliceitem_p.cpp"
210 #include "moc_piesliceitem_p.cpp"
211
211
212 QTCOMMERCIALCHART_END_NAMESPACE
212 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,79 +1,78
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef PIESLICEITEM_H
21 #ifndef PIESLICEITEM_H
22 #define PIESLICEITEM_H
22 #define PIESLICEITEM_H
23
23
24 #include "qchartglobal.h"
24 #include "qchartglobal.h"
25 #include "charttheme_p.h"
25 #include "charttheme_p.h"
26 #include "qpieseries.h"
26 #include "qpieseries.h"
27 #include "pieslicedata_p.h"
27 #include "pieslicedata_p.h"
28 #include <QGraphicsItem>
28 #include <QGraphicsItem>
29 #include <QRectF>
29 #include <QRectF>
30 #include <QColor>
30 #include <QColor>
31 #include <QPen>
31 #include <QPen>
32
32
33 #define PIESLICE_LABEL_GAP 5
33 #define PIESLICE_LABEL_GAP 5
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 class PieChartItem;
36 class PieChartItem;
37 class PieSliceLabel;
37 class PieSliceLabel;
38 class QPieSlice;
38 class QPieSlice;
39
39
40 class PieSliceItem : public QGraphicsObject
40 class PieSliceItem : public QGraphicsObject
41 {
41 {
42 Q_OBJECT
42 Q_OBJECT
43
43
44 public:
44 public:
45 PieSliceItem(QGraphicsItem* parent = 0);
45 PieSliceItem(QGraphicsItem* parent = 0);
46 ~PieSliceItem();
46 ~PieSliceItem();
47
47
48 public: // from QGraphicsItem
48 public: // from QGraphicsItem
49 QRectF boundingRect() const;
49 QRectF boundingRect() const;
50 QPainterPath shape() const;
50 QPainterPath shape() const;
51 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
51 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
52 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
52 void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
53 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
53 void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
54 void mousePressEvent(QGraphicsSceneMouseEvent *event);
54 void mousePressEvent(QGraphicsSceneMouseEvent *event);
55
55
56 Q_SIGNALS:
56 Q_SIGNALS:
57 void clicked(Qt::MouseButtons buttons);
57 void clicked(Qt::MouseButtons buttons);
58 void hoverEnter();
58 void hovered(bool state);
59 void hoverLeave();
60
59
61 public:
60 public:
62 void setSliceData(PieSliceData sliceData);
61 void setSliceData(PieSliceData sliceData);
63 void updateGeometry();
62 void updateGeometry();
64 static QPointF sliceCenter(QPointF point, qreal radius, QPieSlice *slice);
63 static QPointF sliceCenter(QPointF point, qreal radius, QPieSlice *slice);
65 QPainterPath slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF *armStart);
64 QPainterPath slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF *armStart);
66 QPainterPath labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart);
65 QPainterPath labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart);
67 QRectF labelTextRect(QFont font, QString text);
66 QRectF labelTextRect(QFont font, QString text);
68
67
69 private:
68 private:
70 PieSliceData m_data;
69 PieSliceData m_data;
71 QRectF m_boundingRect;
70 QRectF m_boundingRect;
72 QPainterPath m_slicePath;
71 QPainterPath m_slicePath;
73 QPainterPath m_labelArmPath;
72 QPainterPath m_labelArmPath;
74 QRectF m_labelTextRect;
73 QRectF m_labelTextRect;
75 };
74 };
76
75
77 QTCOMMERCIALCHART_END_NAMESPACE
76 QTCOMMERCIALCHART_END_NAMESPACE
78
77
79 #endif // PIESLICEITEM_H
78 #endif // PIESLICEITEM_H
@@ -1,740 +1,697
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qpieseries.h"
21 #include "qpieseries.h"
22 #include "qpieseries_p.h"
22 #include "qpieseries_p.h"
23 #include "qpieslice.h"
23 #include "qpieslice.h"
24 #include "pieslicedata_p.h"
24 #include "pieslicedata_p.h"
25 #include "chartdataset_p.h"
25 #include "chartdataset_p.h"
26 #include "charttheme_p.h"
26 #include "charttheme_p.h"
27 #include "chartanimator_p.h"
27 #include "chartanimator_p.h"
28 #include "legendmarker_p.h"
28 #include "legendmarker_p.h"
29 #include <QAbstractItemModel>
29 #include <QAbstractItemModel>
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 /*!
33 /*!
34 \class QPieSeries
34 \class QPieSeries
35 \brief Pie series API for QtCommercial Charts
35 \brief Pie series API for QtCommercial Charts
36
36
37 The pie series defines a pie chart which consists of pie slices which are defined as QPieSlice objects.
37 The pie series defines a pie chart which consists of pie slices which are defined as QPieSlice objects.
38 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
38 The slices can have any values as the QPieSeries will calculate its relative value to the sum of all slices.
39 The actual slice size is determined by that relative value.
39 The actual slice size is determined by that relative value.
40
40
41 Pie size and position on the chart is controlled by using relative values which range from 0.0 to 1.0
41 Pie size and position on the chart is controlled by using relative values which range from 0.0 to 1.0
42 These relate to the actual chart rectangle.
42 These relate to the actual chart rectangle.
43
43
44 By default the pie is defined as a full pie but it can also be a partial pie.
44 By default the pie is defined as a full pie but it can also be a partial pie.
45 This can be done by setting a starting angle and angle span to the series.
45 This can be done by setting a starting angle and angle span to the series.
46 Full pie is 360 degrees where 0 is at 12 a'clock.
46 Full pie is 360 degrees where 0 is at 12 a'clock.
47
47
48 See the \l {PieChart Example} {pie chart example} to learn how to create a simple pie chart.
48 See the \l {PieChart Example} {pie chart example} to learn how to create a simple pie chart.
49 \image examples_piechart.png
49 \image examples_piechart.png
50 */
50 */
51
51
52 /*!
52 /*!
53 \property QPieSeries::horizontalPosition
53 \property QPieSeries::horizontalPosition
54 \brief Defines the horizontal position of the pie.
54 \brief Defines the horizontal position of the pie.
55
55
56 The value is a relative value to the chart rectangle where:
56 The value is a relative value to the chart rectangle where:
57
57
58 \list
58 \list
59 \o 0.0 is the absolute left.
59 \o 0.0 is the absolute left.
60 \o 1.0 is the absolute right.
60 \o 1.0 is the absolute right.
61 \endlist
61 \endlist
62
62
63 Default value is 0.5 (center).
63 Default value is 0.5 (center).
64 */
64 */
65
65
66 /*!
66 /*!
67 \property QPieSeries::verticalPosition
67 \property QPieSeries::verticalPosition
68 \brief Defines the vertical position of the pie.
68 \brief Defines the vertical position of the pie.
69
69
70 The value is a relative value to the chart rectangle where:
70 The value is a relative value to the chart rectangle where:
71
71
72 \list
72 \list
73 \o 0.0 is the absolute top.
73 \o 0.0 is the absolute top.
74 \o 1.0 is the absolute bottom.
74 \o 1.0 is the absolute bottom.
75 \endlist
75 \endlist
76
76
77 Default value is 0.5 (center).
77 Default value is 0.5 (center).
78 */
78 */
79
79
80 /*!
80 /*!
81 \property QPieSeries::size
81 \property QPieSeries::size
82 \brief Defines the pie size.
82 \brief Defines the pie size.
83
83
84 The value is a relative value to the chart rectangle where:
84 The value is a relative value to the chart rectangle where:
85
85
86 \list
86 \list
87 \o 0.0 is the minimum size (pie not drawn).
87 \o 0.0 is the minimum size (pie not drawn).
88 \o 1.0 is the maximum size that can fit the chart.
88 \o 1.0 is the maximum size that can fit the chart.
89 \endlist
89 \endlist
90
90
91 Default value is 0.7.
91 Default value is 0.7.
92 */
92 */
93
93
94 /*!
94 /*!
95 \property QPieSeries::startAngle
95 \property QPieSeries::startAngle
96 \brief Defines the starting angle of the pie.
96 \brief Defines the starting angle of the pie.
97
97
98 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
98 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
99
99
100 Default is value is 0.
100 Default is value is 0.
101 */
101 */
102
102
103 /*!
103 /*!
104 \property QPieSeries::endAngle
104 \property QPieSeries::endAngle
105 \brief Defines the ending angle of the pie.
105 \brief Defines the ending angle of the pie.
106
106
107 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
107 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
108
108
109 Default is value is 360.
109 Default is value is 360.
110 */
110 */
111
111
112
112
113 /*!
113 /*!
114 Constructs a series object which is a child of \a parent.
114 Constructs a series object which is a child of \a parent.
115 */
115 */
116 QPieSeries::QPieSeries(QObject *parent) :
116 QPieSeries::QPieSeries(QObject *parent) :
117 QAbstractSeries(*new QPieSeriesPrivate(this),parent)
117 QAbstractSeries(*new QPieSeriesPrivate(this),parent)
118 {
118 {
119
119
120 }
120 }
121
121
122 /*!
122 /*!
123 Destroys the series and its slices.
123 Destroys the series and its slices.
124 */
124 */
125 QPieSeries::~QPieSeries()
125 QPieSeries::~QPieSeries()
126 {
126 {
127 // NOTE: d_prt destroyed by QObject
127 // NOTE: d_prt destroyed by QObject
128 }
128 }
129
129
130 /*!
130 /*!
131 Returns QChartSeries::SeriesTypePie.
131 Returns QChartSeries::SeriesTypePie.
132 */
132 */
133 QAbstractSeries::QSeriesType QPieSeries::type() const
133 QAbstractSeries::QSeriesType QPieSeries::type() const
134 {
134 {
135 return QAbstractSeries::SeriesTypePie;
135 return QAbstractSeries::SeriesTypePie;
136 }
136 }
137
137
138 /*!
138 /*!
139 Sets an array of \a slices to the series replacing the existing slices.
139 Sets an array of \a slices to the series replacing the existing slices.
140 Slice ownership is passed to the series.
140 Slice ownership is passed to the series.
141 */
141 */
142 void QPieSeries::replace(QList<QPieSlice*> slices)
142 void QPieSeries::replace(QList<QPieSlice*> slices)
143 {
143 {
144 clear();
144 clear();
145 append(slices);
145 append(slices);
146 }
146 }
147
147
148 /*!
148 /*!
149 Appends an array of \a slices to the series.
149 Appends an array of \a slices to the series.
150 Slice ownership is passed to the series.
150 Slice ownership is passed to the series.
151 */
151 */
152 void QPieSeries::append(QList<QPieSlice*> slices)
152 void QPieSeries::append(QList<QPieSlice*> slices)
153 {
153 {
154 Q_D(QPieSeries);
154 Q_D(QPieSeries);
155
155
156 foreach (QPieSlice* s, slices) {
156 foreach (QPieSlice* s, slices) {
157 s->setParent(this);
157 s->setParent(this);
158 d->m_slices << s;
158 d->m_slices << s;
159 }
159 }
160
160
161 d->updateDerivativeData();
161 d->updateDerivativeData();
162
162
163 foreach (QPieSlice* s, slices) {
163 foreach (QPieSlice* s, slices) {
164 connect(s, SIGNAL(changed()), d, SLOT(sliceChanged()));
164 connect(s, SIGNAL(changed()), d, SLOT(sliceChanged()));
165 connect(s, SIGNAL(clicked(Qt::MouseButtons)), d, SLOT(sliceClicked(Qt::MouseButtons)));
165 connect(s, SIGNAL(clicked()), d, SLOT(sliceClicked()));
166 connect(s, SIGNAL(hoverEnter()), d, SLOT(sliceHoverEnter()));
166 connect(s, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
167 connect(s, SIGNAL(hoverLeave()), d, SLOT(sliceHoverLeave()));
168 }
167 }
169
168
170 emit added(slices);
169 emit d->added(slices);
171 }
170 }
172
171
173 /*!
172 /*!
174 Appends a single \a slice to the series.
173 Appends a single \a slice to the series.
175 Slice ownership is passed to the series.
174 Slice ownership is passed to the series.
176 */
175 */
177 void QPieSeries::append(QPieSlice* slice)
176 void QPieSeries::append(QPieSlice* slice)
178 {
177 {
179 append(QList<QPieSlice*>() << slice);
178 append(QList<QPieSlice*>() << slice);
180 }
179 }
181
180
182 /*!
181 /*!
183 Appends a single \a slice to the series and returns a reference to the series.
182 Appends a single \a slice to the series and returns a reference to the series.
184 Slice ownership is passed to the series.
183 Slice ownership is passed to the series.
185 */
184 */
186 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
185 QPieSeries& QPieSeries::operator << (QPieSlice* slice)
187 {
186 {
188 append(slice);
187 append(slice);
189 return *this;
188 return *this;
190 }
189 }
191
190
192
191
193 /*!
192 /*!
194 Appends a single slice to the series with give \a value and \a name.
193 Appends a single slice to the series with give \a value and \a name.
195 Slice ownership is passed to the series.
194 Slice ownership is passed to the series.
196 */
195 */
197 QPieSlice* QPieSeries::append(qreal value, QString name)
196 QPieSlice* QPieSeries::append(qreal value, QString name)
198 {
197 {
199 QPieSlice* slice = new QPieSlice(value, name);
198 QPieSlice* slice = new QPieSlice(value, name);
200 append(slice);
199 append(slice);
201 return slice;
200 return slice;
202 }
201 }
203
202
204 /*!
203 /*!
205 Inserts a single \a slice to the series before the slice at \a index position.
204 Inserts a single \a slice to the series before the slice at \a index position.
206 Slice ownership is passed to the series.
205 Slice ownership is passed to the series.
207 */
206 */
208 void QPieSeries::insert(int index, QPieSlice* slice)
207 void QPieSeries::insert(int index, QPieSlice* slice)
209 {
208 {
210 Q_D(QPieSeries);
209 Q_D(QPieSeries);
211 Q_ASSERT(index <= d->m_slices.count());
210 Q_ASSERT(index <= d->m_slices.count());
212 slice->setParent(this);
211 slice->setParent(this);
213 d->m_slices.insert(index, slice);
212 d->m_slices.insert(index, slice);
214
213
215 d->updateDerivativeData();
214 d->updateDerivativeData();
216
215
217 connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
216 connect(slice, SIGNAL(changed()), d, SLOT(sliceChanged()));
218 connect(slice, SIGNAL(clicked(Qt::MouseButtons)), d, SLOT(sliceClicked(Qt::MouseButtons)));
217 connect(slice, SIGNAL(clicked()), d, SLOT(sliceClicked()));
219 connect(slice, SIGNAL(hoverEnter()), d, SLOT(sliceHoverEnter()));
218 connect(slice, SIGNAL(hovered(bool)), d, SLOT(sliceHovered(bool)));
220 connect(slice, SIGNAL(hoverLeave()), d, SLOT(sliceHoverLeave()));
221
219
222 emit added(QList<QPieSlice*>() << slice);
220 emit d->added(QList<QPieSlice*>() << slice);
223 }
221 }
224
222
225 /*!
223 /*!
226 Removes a single \a slice from the series and deletes the slice.
224 Removes a single \a slice from the series and deletes the slice.
227
225
228 Do not reference the pointer after this call.
226 Do not reference the pointer after this call.
229 */
227 */
230 void QPieSeries::remove(QPieSlice* slice)
228 void QPieSeries::remove(QPieSlice* slice)
231 {
229 {
232 Q_D(QPieSeries);
230 Q_D(QPieSeries);
233 if (!d->m_slices.removeOne(slice)) {
231 if (!d->m_slices.removeOne(slice)) {
234 Q_ASSERT(0); // TODO: how should this be reported?
232 Q_ASSERT(0); // TODO: how should this be reported?
235 return;
233 return;
236 }
234 }
237
235
238 d->updateDerivativeData();
236 d->updateDerivativeData();
239
237
240 emit removed(QList<QPieSlice*>() << slice);
238 emit d->removed(QList<QPieSlice*>() << slice);
241
239
242 delete slice;
240 delete slice;
243 slice = 0;
241 slice = 0;
244 }
242 }
245
243
246 /*!
244 /*!
247 Clears all slices from the series.
245 Clears all slices from the series.
248 */
246 */
249 void QPieSeries::clear()
247 void QPieSeries::clear()
250 {
248 {
251 Q_D(QPieSeries);
249 Q_D(QPieSeries);
252 if (d->m_slices.count() == 0)
250 if (d->m_slices.count() == 0)
253 return;
251 return;
254
252
255 QList<QPieSlice*> slices = d->m_slices;
253 QList<QPieSlice*> slices = d->m_slices;
256 foreach (QPieSlice* s, d->m_slices) {
254 foreach (QPieSlice* s, d->m_slices) {
257 d->m_slices.removeOne(s);
255 d->m_slices.removeOne(s);
258 delete s;
256 delete s;
259 }
257 }
260
258
261 d->updateDerivativeData();
259 d->updateDerivativeData();
262
260
263 emit removed(slices);
261 emit d->removed(slices);
264 }
262 }
265
263
266 /*!
264 /*!
267 returns the number of the slices in this series.
265 returns the number of the slices in this series.
268 */
266 */
269 int QPieSeries::count() const
267 int QPieSeries::count() const
270 {
268 {
271 Q_D(const QPieSeries);
269 Q_D(const QPieSeries);
272 return d->m_slices.count();
270 return d->m_slices.count();
273 }
271 }
274
272
275 /*!
273 /*!
276 Returns true is the series is empty.
274 Returns true is the series is empty.
277 */
275 */
278 bool QPieSeries::isEmpty() const
276 bool QPieSeries::isEmpty() const
279 {
277 {
280 Q_D(const QPieSeries);
278 Q_D(const QPieSeries);
281 return d->m_slices.isEmpty();
279 return d->m_slices.isEmpty();
282 }
280 }
283
281
284 /*!
282 /*!
285 Returns a list of slices that belong to this series.
283 Returns a list of slices that belong to this series.
286 */
284 */
287 QList<QPieSlice*> QPieSeries::slices() const
285 QList<QPieSlice*> QPieSeries::slices() const
288 {
286 {
289 Q_D(const QPieSeries);
287 Q_D(const QPieSeries);
290 return d->m_slices;
288 return d->m_slices;
291 }
289 }
292
290
293 void QPieSeries::setHorizontalPosition(qreal relativePosition)
291 void QPieSeries::setHorizontalPosition(qreal relativePosition)
294 {
292 {
295 Q_D(QPieSeries);
293 Q_D(QPieSeries);
296 if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0))
294 if (d->setRealValue(d->m_pieRelativeHorPos, relativePosition, 1.0))
297 emit piePositionChanged();
295 emit d->piePositionChanged();
298 }
296 }
299
297
300 void QPieSeries::setVerticalPosition(qreal relativePosition)
298 void QPieSeries::setVerticalPosition(qreal relativePosition)
301 {
299 {
302 Q_D(QPieSeries);
300 Q_D(QPieSeries);
303 if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0))
301 if (d->setRealValue(d->m_pieRelativeVerPos, relativePosition, 1.0))
304 emit piePositionChanged();
302 emit d->piePositionChanged();
305 }
303 }
306
304
307 qreal QPieSeries::horizontalPosition() const
305 qreal QPieSeries::horizontalPosition() const
308 {
306 {
309 Q_D(const QPieSeries);
307 Q_D(const QPieSeries);
310 return d->m_pieRelativeHorPos;
308 return d->m_pieRelativeHorPos;
311 }
309 }
312
310
313 qreal QPieSeries::verticalPosition() const
311 qreal QPieSeries::verticalPosition() const
314 {
312 {
315 Q_D(const QPieSeries);
313 Q_D(const QPieSeries);
316 return d->m_pieRelativeVerPos;
314 return d->m_pieRelativeVerPos;
317 }
315 }
318
316
319 void QPieSeries::setPieSize(qreal relativeSize)
317 void QPieSeries::setPieSize(qreal relativeSize)
320 {
318 {
321 Q_D(QPieSeries);
319 Q_D(QPieSeries);
322 if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0))
320 if (d->setRealValue(d->m_pieRelativeSize, relativeSize, 1.0))
323 emit pieSizeChanged();
321 emit d->pieSizeChanged();
324 }
322 }
325
323
326 qreal QPieSeries::pieSize() const
324 qreal QPieSeries::pieSize() const
327 {
325 {
328 Q_D(const QPieSeries);
326 Q_D(const QPieSeries);
329 return d->m_pieRelativeSize;
327 return d->m_pieRelativeSize;
330 }
328 }
331
329
332
330
333 void QPieSeries::setPieStartAngle(qreal angle)
331 void QPieSeries::setPieStartAngle(qreal angle)
334 {
332 {
335 Q_D(QPieSeries);
333 Q_D(QPieSeries);
336 if (d->setRealValue(d->m_pieStartAngle, angle, d->m_pieEndAngle))
334 if (d->setRealValue(d->m_pieStartAngle, angle, d->m_pieEndAngle))
337 d->updateDerivativeData();
335 d->updateDerivativeData();
338 }
336 }
339
337
340 qreal QPieSeries::pieStartAngle() const
338 qreal QPieSeries::pieStartAngle() const
341 {
339 {
342 Q_D(const QPieSeries);
340 Q_D(const QPieSeries);
343 return d->m_pieStartAngle;
341 return d->m_pieStartAngle;
344 }
342 }
345
343
346 /*!
344 /*!
347 Sets the end angle of the pie.
345 Sets the end angle of the pie.
348
346
349 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
347 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
350
348
351 \a angle must be greater than start angle.
349 \a angle must be greater than start angle.
352
350
353 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
351 \sa pieEndAngle(), pieStartAngle(), setPieStartAngle()
354 */
352 */
355 void QPieSeries::setPieEndAngle(qreal angle)
353 void QPieSeries::setPieEndAngle(qreal angle)
356 {
354 {
357 Q_D(QPieSeries);
355 Q_D(QPieSeries);
358
356
359 if (d->setRealValue(d->m_pieEndAngle, angle, 360.0, d->m_pieStartAngle))
357 if (d->setRealValue(d->m_pieEndAngle, angle, 360.0, d->m_pieStartAngle))
360 d->updateDerivativeData();
358 d->updateDerivativeData();
361 }
359 }
362
360
363 /*!
361 /*!
364 Returns the end angle of the pie.
362 Returns the end angle of the pie.
365
363
366 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
364 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
367
365
368 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
366 \sa setPieEndAngle(), pieStartAngle(), setPieStartAngle()
369 */
367 */
370 qreal QPieSeries::pieEndAngle() const
368 qreal QPieSeries::pieEndAngle() const
371 {
369 {
372 Q_D(const QPieSeries);
370 Q_D(const QPieSeries);
373 return d->m_pieEndAngle;
371 return d->m_pieEndAngle;
374 }
372 }
375
373
376 /*!
374 /*!
377 Sets the all the slice labels \a visible or invisible.
375 Sets the all the slice labels \a visible or invisible.
378
376
379 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
377 \sa QPieSlice::isLabelVisible(), QPieSlice::setLabelVisible()
380 */
378 */
381 void QPieSeries::setLabelsVisible(bool visible)
379 void QPieSeries::setLabelsVisible(bool visible)
382 {
380 {
383 Q_D(QPieSeries);
381 Q_D(QPieSeries);
384 foreach (QPieSlice* s, d->m_slices)
382 foreach (QPieSlice* s, d->m_slices)
385 s->setLabelVisible(visible);
383 s->setLabelVisible(visible);
386 }
384 }
387
385
388 /*!
386 /*!
389 Returns the sum of all slice values in this series.
387 Returns the sum of all slice values in this series.
390
388
391 \sa QPieSlice::value(), QPieSlice::setValue(), QPieSlice::percentage()
389 \sa QPieSlice::value(), QPieSlice::setValue(), QPieSlice::percentage()
392 */
390 */
393 qreal QPieSeries::sum() const
391 qreal QPieSeries::sum() const
394 {
392 {
395 Q_D(const QPieSeries);
393 Q_D(const QPieSeries);
396 return d->m_sum;
394 return d->m_sum;
397 }
395 }
398
396
399 /*!
397 /*!
400 \fn void QPieSeries::clicked(QPieSlice* slice, Qt::MouseButtons buttons)
398 \fn void QPieSeries::clicked(QPieSlice* slice)
401
399
402 This signal is emitted when a \a slice has been clicked with mouse \a buttons.
400 This signal is emitted when a \a slice has been clicked.
403
401
404 \sa QPieSlice::clicked()
402 \sa QPieSlice::clicked()
405 */
403 */
406
404
407 /*!
405 /*!
408 \fn void QPieSeries::hoverEnter(QPieSlice* slice)
406 \fn void QPieSeries::hovered(QPieSlice* slice, bool state)
409
407
410 This signal is emitted when user has hovered over a \a slice.
408 This signal is emitted when user has hovered over or away from the \a slice.
411
409
412 \sa QPieSlice::hoverEnter()
410 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
413 */
414
415 /*!
416 \fn void QPieSeries::hoverLeave(QPieSlice* slice)
417
418 This signal is emitted when user has hovered away from a \a slice.
419
420 \sa QPieSlice::hoverLeave()
421 */
422
423 /*!
424 \fn void QPieSeries::added(QList<QPieSlice*> slices)
425
426 This signal is emitted when \a slices has been added to the series.
427
411
428 \sa append(), insert()
412 \sa QPieSlice::hovered()
429 */
430
431 /*!
432 \fn void QPieSeries::removed(QList<QPieSlice*> slices)
433
434 This signal is emitted when \a slices has been removed from the series.
435
436 \sa remove(), clear()
437 */
438
439 /*!
440 \fn void QPieSeries::piePositionChanged()
441
442 This signal is emitted when pie position has changed.
443
444 \sa verticalPosition(), setVerticalPosition(), horizontalPosition(), setHorizontalPosition()
445 */
446
447 /*!
448 \fn void QPieSeries::pieSizeChanged()
449
450 This signal is emitted when pie size has changed.
451
452 \sa pieSize(), setPieSize()
453 */
413 */
454
414
455 /*!
415 /*!
456 \fn bool QPieSeries::setModel(QAbstractItemModel *model)
416 \fn bool QPieSeries::setModel(QAbstractItemModel *model)
457 Sets the \a model to be used as a data source
417 Sets the \a model to be used as a data source
458 */
418 */
459 bool QPieSeries::setModel(QAbstractItemModel* model)
419 bool QPieSeries::setModel(QAbstractItemModel* model)
460 {
420 {
461 Q_D(QPieSeries);
421 Q_D(QPieSeries);
462 // disconnect signals from old model
422 // disconnect signals from old model
463 if(d->m_model)
423 if(d->m_model)
464 {
424 {
465 disconnect(d->m_model, 0, this, 0);
425 disconnect(d->m_model, 0, this, 0);
466 d->m_mapValues = -1;
426 d->m_mapValues = -1;
467 d->m_mapLabels = -1;
427 d->m_mapLabels = -1;
468 d->m_mapOrientation = Qt::Vertical;
428 d->m_mapOrientation = Qt::Vertical;
469 }
429 }
470
430
471 // set new model
431 // set new model
472 if(model)
432 if(model)
473 {
433 {
474 d->m_model = model;
434 d->m_model = model;
475 return true;
435 return true;
476 }
436 }
477 else
437 else
478 {
438 {
479 d->m_model = 0;
439 d->m_model = 0;
480 return false;
440 return false;
481 }
441 }
482 }
442 }
483
443
484 /*!
444 /*!
485 \fn bool QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
445 \fn bool QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
486 Sets column/row specified by \a modelValuesLine to be used as a list of pie slice values for the pie.
446 Sets column/row specified by \a modelValuesLine to be used as a list of pie slice values for the pie.
487 Parameter \a modelValuesLine indicates the column/row where the values for the pie slices are located in the model.
447 Parameter \a modelValuesLine indicates the column/row where the values for the pie slices are located in the model.
488 Parameter \a modelLabelsLine indicates the column/row where the labels for the pie slices are located in the model.
448 Parameter \a modelLabelsLine indicates the column/row where the labels for the pie slices are located in the model.
489 The \a orientation parameter specifies whether the data is in columns or in rows.
449 The \a orientation parameter specifies whether the data is in columns or in rows.
490 */
450 */
491 void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
451 void QPieSeries::setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation)
492 {
452 {
493 Q_D(QPieSeries);
453 Q_D(QPieSeries);
494
454
495 if (d->m_model == 0)
455 if (d->m_model == 0)
496 return;
456 return;
497
457
498 d->m_mapValues = modelValuesLine;
458 d->m_mapValues = modelValuesLine;
499 d->m_mapLabels = modelLabelsLine;
459 d->m_mapLabels = modelLabelsLine;
500 d->m_mapOrientation = orientation;
460 d->m_mapOrientation = orientation;
501
461
502 // connect the signals
462 // connect the signals
503 if (d->m_mapOrientation == Qt::Vertical) {
463 if (d->m_mapOrientation == Qt::Vertical) {
504 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
464 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
505 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
465 connect(d->m_model, SIGNAL(rowsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
506 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
466 connect(d->m_model, SIGNAL(rowsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
507 } else {
467 } else {
508 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
468 connect(d->m_model, SIGNAL(dataChanged(QModelIndex,QModelIndex)), d, SLOT(modelUpdated(QModelIndex,QModelIndex)));
509 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
469 connect(d->m_model, SIGNAL(columnsInserted(QModelIndex,int,int)), d, SLOT(modelDataAdded(QModelIndex,int,int)));
510 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
470 connect(d->m_model, SIGNAL(columnsRemoved(QModelIndex,int,int)), d, SLOT(modelDataRemoved(QModelIndex,int,int)));
511 }
471 }
512
472
513 // create the initial slices set
473 // create the initial slices set
514 if (d->m_mapOrientation == Qt::Vertical) {
474 if (d->m_mapOrientation == Qt::Vertical) {
515 for (int i = 0; i < d->m_model->rowCount(); i++)
475 for (int i = 0; i < d->m_model->rowCount(); i++)
516 append(d->m_model->data(d->m_model->index(i, d->m_mapValues), Qt::DisplayRole).toDouble(), d->m_model->data(d->m_model->index(i, d->m_mapLabels), Qt::DisplayRole).toString());
476 append(d->m_model->data(d->m_model->index(i, d->m_mapValues), Qt::DisplayRole).toDouble(), d->m_model->data(d->m_model->index(i, d->m_mapLabels), Qt::DisplayRole).toString());
517 } else {
477 } else {
518 for (int i = 0; i < d->m_model->columnCount(); i++)
478 for (int i = 0; i < d->m_model->columnCount(); i++)
519 append(d->m_model->data(d->m_model->index(d->m_mapValues, i), Qt::DisplayRole).toDouble(), d->m_model->data(d->m_model->index(d->m_mapLabels, i), Qt::DisplayRole).toString());
479 append(d->m_model->data(d->m_model->index(d->m_mapValues, i), Qt::DisplayRole).toDouble(), d->m_model->data(d->m_model->index(d->m_mapLabels, i), Qt::DisplayRole).toString());
520 }
480 }
521 }
481 }
522
482
523 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
483 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
524
484
525
485
526 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
486 QPieSeriesPrivate::QPieSeriesPrivate(QPieSeries *parent) :
527 QAbstractSeriesPrivate(parent),
487 QAbstractSeriesPrivate(parent),
528 m_pieRelativeHorPos(0.5),
488 m_pieRelativeHorPos(0.5),
529 m_pieRelativeVerPos(0.5),
489 m_pieRelativeVerPos(0.5),
530 m_pieRelativeSize(0.7),
490 m_pieRelativeSize(0.7),
531 m_pieStartAngle(0),
491 m_pieStartAngle(0),
532 m_pieEndAngle(360),
492 m_pieEndAngle(360),
533 m_sum(0),
493 m_sum(0),
534 m_mapValues(0),
494 m_mapValues(0),
535 m_mapLabels(0),
495 m_mapLabels(0),
536 m_mapOrientation(Qt::Horizontal)
496 m_mapOrientation(Qt::Horizontal)
537 {
497 {
538
498
539 }
499 }
540
500
541 QPieSeriesPrivate::~QPieSeriesPrivate()
501 QPieSeriesPrivate::~QPieSeriesPrivate()
542 {
502 {
543
503
544 }
504 }
545
505
546 void QPieSeriesPrivate::updateDerivativeData()
506 void QPieSeriesPrivate::updateDerivativeData()
547 {
507 {
548 m_sum = 0;
508 m_sum = 0;
549
509
550 // nothing to do?
510 // nothing to do?
551 if (m_slices.count() == 0)
511 if (m_slices.count() == 0)
552 return;
512 return;
553
513
554 // calculate sum of all slices
514 // calculate sum of all slices
555 foreach (QPieSlice* s, m_slices)
515 foreach (QPieSlice* s, m_slices)
556 m_sum += s->value();
516 m_sum += s->value();
557
517
558 // nothing to show..
518 // nothing to show..
559 if (qFuzzyIsNull(m_sum))
519 if (qFuzzyIsNull(m_sum))
560 return;
520 return;
561
521
562 // update slice attributes
522 // update slice attributes
563 qreal sliceAngle = m_pieStartAngle;
523 qreal sliceAngle = m_pieStartAngle;
564 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
524 qreal pieSpan = m_pieEndAngle - m_pieStartAngle;
565 QVector<QPieSlice*> changed;
525 QVector<QPieSlice*> changed;
566 foreach (QPieSlice* s, m_slices) {
526 foreach (QPieSlice* s, m_slices) {
567
527
568 PieSliceData data = PieSliceData::data(s);
528 PieSliceData data = PieSliceData::data(s);
569 data.m_percentage = s->value() / m_sum;
529 data.m_percentage = s->value() / m_sum;
570 data.m_angleSpan = pieSpan * data.m_percentage;
530 data.m_angleSpan = pieSpan * data.m_percentage;
571 data.m_startAngle = sliceAngle;
531 data.m_startAngle = sliceAngle;
572 sliceAngle += data.m_angleSpan;
532 sliceAngle += data.m_angleSpan;
573
533
574 if (PieSliceData::data(s) != data) {
534 if (PieSliceData::data(s) != data) {
575 PieSliceData::data(s) = data;
535 PieSliceData::data(s) = data;
576 changed << s;
536 changed << s;
577 }
537 }
578 }
538 }
579
539
580 // emit signals
540 // emit signals
581 foreach (QPieSlice* s, changed)
541 foreach (QPieSlice* s, changed)
582 PieSliceData::data(s).emitChangedSignal(s);
542 PieSliceData::data(s).emitChangedSignal(s);
583 }
543 }
584
544
585 void QPieSeriesPrivate::sliceChanged()
545 QPieSeriesPrivate* QPieSeriesPrivate::seriesData(QPieSeries &series)
586 {
546 {
587 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
547 return series.d_func();
588 updateDerivativeData();
589 }
548 }
590
549
591 void QPieSeriesPrivate::sliceClicked(Qt::MouseButtons buttons)
550 void QPieSeriesPrivate::sliceChanged()
592 {
551 {
593 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
552 Q_ASSERT(m_slices.contains(qobject_cast<QPieSlice *>(sender())));
594 Q_ASSERT(m_slices.contains(slice));
553 updateDerivativeData();
595 Q_Q(QPieSeries);
596 emit q->clicked(slice, buttons);
597 }
554 }
598
555
599 void QPieSeriesPrivate::sliceHoverEnter()
556 void QPieSeriesPrivate::sliceClicked()
600 {
557 {
601 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
558 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
602 Q_ASSERT(m_slices.contains(slice));
559 Q_ASSERT(m_slices.contains(slice));
603 Q_Q(QPieSeries);
560 Q_Q(QPieSeries);
604 emit q->hoverEnter(slice);
561 emit q->clicked(slice);
605 }
562 }
606
563
607 void QPieSeriesPrivate::sliceHoverLeave()
564 void QPieSeriesPrivate::sliceHovered(bool state)
608 {
565 {
609 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
566 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
610 Q_ASSERT(m_slices.contains(slice));
567 Q_ASSERT(m_slices.contains(slice));
611 Q_Q(QPieSeries);
568 Q_Q(QPieSeries);
612 emit q->hoverLeave(slice);
569 emit q->hovered(slice, state);
613 }
570 }
614
571
615 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
572 void QPieSeriesPrivate::modelUpdated(QModelIndex topLeft, QModelIndex bottomRight)
616 {
573 {
617 Q_UNUSED(bottomRight)
574 Q_UNUSED(bottomRight)
618
575
619 if (m_mapOrientation == Qt::Vertical)
576 if (m_mapOrientation == Qt::Vertical)
620 {
577 {
621 if (topLeft.column() == m_mapValues)
578 if (topLeft.column() == m_mapValues)
622 if (m_mapValues == m_mapLabels)
579 if (m_mapValues == m_mapLabels)
623 {
580 {
624 m_slices.at(topLeft.row())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
581 m_slices.at(topLeft.row())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
625 m_slices.at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
582 m_slices.at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
626 }
583 }
627 else
584 else
628 {
585 {
629 m_slices.at(topLeft.row())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
586 m_slices.at(topLeft.row())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
630 }
587 }
631 else if (topLeft.column() == m_mapLabels)
588 else if (topLeft.column() == m_mapLabels)
632 m_slices.at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
589 m_slices.at(topLeft.row())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
633 }
590 }
634 else
591 else
635 {
592 {
636 if (topLeft.row() == m_mapValues)
593 if (topLeft.row() == m_mapValues)
637 if (m_mapValues == m_mapLabels)
594 if (m_mapValues == m_mapLabels)
638 {
595 {
639 m_slices.at(topLeft.column())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
596 m_slices.at(topLeft.column())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
640 m_slices.at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
597 m_slices.at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
641 }
598 }
642 else
599 else
643 {
600 {
644 m_slices.at(topLeft.column())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
601 m_slices.at(topLeft.column())->setValue(m_model->data(topLeft, Qt::DisplayRole).toDouble());
645 }
602 }
646 else if (topLeft.row() == m_mapLabels)
603 else if (topLeft.row() == m_mapLabels)
647 m_slices.at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
604 m_slices.at(topLeft.column())->setLabel(m_model->data(topLeft, Qt::DisplayRole).toString());
648 }
605 }
649 }
606 }
650
607
651 void QPieSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
608 void QPieSeriesPrivate::modelDataAdded(QModelIndex parent, int start, int end)
652 {
609 {
653 Q_UNUSED(parent)
610 Q_UNUSED(parent)
654 Q_UNUSED(end)
611 Q_UNUSED(end)
655 Q_Q(QPieSeries);
612 Q_Q(QPieSeries);
656
613
657 QPieSlice* newSlice = new QPieSlice;
614 QPieSlice* newSlice = new QPieSlice;
658 newSlice->setLabelVisible(true);
615 newSlice->setLabelVisible(true);
659 if (m_mapOrientation == Qt::Vertical)
616 if (m_mapOrientation == Qt::Vertical)
660 {
617 {
661 newSlice->setValue(m_model->data(m_model->index(start, m_mapValues), Qt::DisplayRole).toDouble());
618 newSlice->setValue(m_model->data(m_model->index(start, m_mapValues), Qt::DisplayRole).toDouble());
662 newSlice->setLabel(m_model->data(m_model->index(start, m_mapLabels), Qt::DisplayRole).toString());
619 newSlice->setLabel(m_model->data(m_model->index(start, m_mapLabels), Qt::DisplayRole).toString());
663 }
620 }
664 else
621 else
665 {
622 {
666 newSlice->setValue(m_model->data(m_model->index(m_mapValues, start), Qt::DisplayRole).toDouble());
623 newSlice->setValue(m_model->data(m_model->index(m_mapValues, start), Qt::DisplayRole).toDouble());
667 newSlice->setLabel(m_model->data(m_model->index(m_mapLabels, start), Qt::DisplayRole).toString());
624 newSlice->setLabel(m_model->data(m_model->index(m_mapLabels, start), Qt::DisplayRole).toString());
668 }
625 }
669
626
670 q->insert(start, newSlice);
627 q->insert(start, newSlice);
671 }
628 }
672
629
673 void QPieSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
630 void QPieSeriesPrivate::modelDataRemoved(QModelIndex parent, int start, int end)
674 {
631 {
675 Q_UNUSED(parent)
632 Q_UNUSED(parent)
676 Q_UNUSED(end)
633 Q_UNUSED(end)
677 Q_Q(QPieSeries);
634 Q_Q(QPieSeries);
678 q->remove(m_slices.at(start));
635 q->remove(m_slices.at(start));
679 }
636 }
680
637
681 bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min)
638 bool QPieSeriesPrivate::setRealValue(qreal &value, qreal newValue, qreal max, qreal min)
682 {
639 {
683 // Remove rounding errors
640 // Remove rounding errors
684 qreal roundedValue = newValue;
641 qreal roundedValue = newValue;
685 if (qFuzzyIsNull(min) && qFuzzyIsNull(newValue))
642 if (qFuzzyIsNull(min) && qFuzzyIsNull(newValue))
686 roundedValue = 0.0;
643 roundedValue = 0.0;
687 else if (qFuzzyCompare(newValue, max))
644 else if (qFuzzyCompare(newValue, max))
688 roundedValue = max;
645 roundedValue = max;
689 else if (qFuzzyCompare(newValue, min))
646 else if (qFuzzyCompare(newValue, min))
690 roundedValue = min;
647 roundedValue = min;
691
648
692 // Check if the position is valid after removing the rounding errors
649 // Check if the position is valid after removing the rounding errors
693 if (roundedValue < min || roundedValue > max) {
650 if (roundedValue < min || roundedValue > max) {
694 qWarning("QPieSeries: Illegal value");
651 qWarning("QPieSeries: Illegal value");
695 return false;
652 return false;
696 }
653 }
697
654
698 if (!qFuzzyIsNull(value - roundedValue)) {
655 if (!qFuzzyIsNull(value - roundedValue)) {
699 value = roundedValue;
656 value = roundedValue;
700 return true;
657 return true;
701 }
658 }
702
659
703 // The change was so small it is considered a rounding error
660 // The change was so small it is considered a rounding error
704 return false;
661 return false;
705 }
662 }
706
663
707 void QPieSeriesPrivate::scaleDomain(Domain& domain)
664 void QPieSeriesPrivate::scaleDomain(Domain& domain)
708 {
665 {
709 Q_UNUSED(domain);
666 Q_UNUSED(domain);
710 #ifndef QT_NO_DEBUG
667 #ifndef QT_NO_DEBUG
711 qWarning() << __FILE__<<__FUNCTION__<<"not implemented";
668 qWarning() << __FILE__<<__FUNCTION__<<"not implemented";
712 #endif
669 #endif
713 }
670 }
714
671
715 Chart* QPieSeriesPrivate::createGraphics(ChartPresenter* presenter)
672 Chart* QPieSeriesPrivate::createGraphics(ChartPresenter* presenter)
716 {
673 {
717 Q_Q(QPieSeries);
674 Q_Q(QPieSeries);
718 PieChartItem* pie = new PieChartItem(q,presenter);
675 PieChartItem* pie = new PieChartItem(q,presenter);
719 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
676 if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) {
720 presenter->animator()->addAnimation(pie);
677 presenter->animator()->addAnimation(pie);
721 }
678 }
722 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
679 presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q));
723 return pie;
680 return pie;
724 }
681 }
725
682
726 QList<LegendMarker*> QPieSeriesPrivate::createLegendMarker(QLegend* legend)
683 QList<LegendMarker*> QPieSeriesPrivate::createLegendMarker(QLegend* legend)
727 {
684 {
728 Q_Q(QPieSeries);
685 Q_Q(QPieSeries);
729 QList<LegendMarker*> markers;
686 QList<LegendMarker*> markers;
730 foreach(QPieSlice* slice, q->slices()) {
687 foreach(QPieSlice* slice, q->slices()) {
731 PieLegendMarker* marker = new PieLegendMarker(q,slice,legend);
688 PieLegendMarker* marker = new PieLegendMarker(q,slice,legend);
732 markers << marker;
689 markers << marker;
733 }
690 }
734 return markers;
691 return markers;
735 }
692 }
736
693
737 #include "moc_qpieseries.cpp"
694 #include "moc_qpieseries.cpp"
738 #include "moc_qpieseries_p.cpp"
695 #include "moc_qpieseries_p.cpp"
739
696
740 QTCOMMERCIALCHART_END_NAMESPACE
697 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,101 +1,89
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef PIESERIES_H
21 #ifndef PIESERIES_H
22 #define PIESERIES_H
22 #define PIESERIES_H
23
23
24 #include <qabstractseries.h>
24 #include <qabstractseries.h>
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 class QPieSeriesPrivate;
27 class QPieSeriesPrivate;
28 class QPieSlice;
28 class QPieSlice;
29
29
30 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QAbstractSeries
30 class QTCOMMERCIALCHART_EXPORT QPieSeries : public QAbstractSeries
31 {
31 {
32 Q_OBJECT
32 Q_OBJECT
33 Q_PROPERTY(qreal horizontalPosition READ horizontalPosition WRITE setHorizontalPosition)
33 Q_PROPERTY(qreal horizontalPosition READ horizontalPosition WRITE setHorizontalPosition)
34 Q_PROPERTY(qreal verticalPosition READ verticalPosition WRITE setVerticalPosition)
34 Q_PROPERTY(qreal verticalPosition READ verticalPosition WRITE setVerticalPosition)
35 Q_PROPERTY(qreal size READ pieSize WRITE setPieSize)
35 Q_PROPERTY(qreal size READ pieSize WRITE setPieSize)
36 Q_PROPERTY(qreal startAngle READ pieStartAngle WRITE setPieStartAngle)
36 Q_PROPERTY(qreal startAngle READ pieStartAngle WRITE setPieStartAngle)
37 Q_PROPERTY(qreal endAngle READ pieEndAngle WRITE setPieEndAngle)
37 Q_PROPERTY(qreal endAngle READ pieEndAngle WRITE setPieEndAngle)
38
38
39 public:
39 public:
40 explicit QPieSeries(QObject *parent = 0);
40 explicit QPieSeries(QObject *parent = 0);
41 virtual ~QPieSeries();
41 virtual ~QPieSeries();
42
42
43 public: // from QChartSeries
44 QSeriesType type() const;
43 QSeriesType type() const;
45
44
46 public:
47
48 // slice setters
49 void append(QPieSlice* slice);
45 void append(QPieSlice* slice);
50 void append(QList<QPieSlice*> slices);
46 void append(QList<QPieSlice*> slices);
47 QPieSeries& operator << (QPieSlice* slice);
48 QPieSlice* append(qreal value, QString name);
51 void insert(int index, QPieSlice* slice);
49 void insert(int index, QPieSlice* slice);
52 void replace(QList<QPieSlice*> slices);
50 void replace(QList<QPieSlice*> slices);
53 void remove(QPieSlice* slice);
51 void remove(QPieSlice* slice);
54 void clear();
52 void clear();
55
53
56 // slice getters
57 QList<QPieSlice*> slices() const;
54 QList<QPieSlice*> slices() const;
58
59 // calculated data
60 int count() const;
55 int count() const;
61 bool isEmpty() const;
56 bool isEmpty() const;
57
62 qreal sum() const;
58 qreal sum() const;
63
59
64 // pie customization
65 void setHorizontalPosition(qreal relativePosition);
60 void setHorizontalPosition(qreal relativePosition);
66 qreal horizontalPosition() const;
61 qreal horizontalPosition() const;
67 void setVerticalPosition(qreal relativePosition);
62 void setVerticalPosition(qreal relativePosition);
68 qreal verticalPosition() const;
63 qreal verticalPosition() const;
64
69 void setPieSize(qreal relativeSize);
65 void setPieSize(qreal relativeSize);
70 qreal pieSize() const;
66 qreal pieSize() const;
67
71 void setPieStartAngle(qreal startAngle);
68 void setPieStartAngle(qreal startAngle);
72 qreal pieStartAngle() const;
69 qreal pieStartAngle() const;
73 void setPieEndAngle(qreal endAngle);
70 void setPieEndAngle(qreal endAngle);
74 qreal pieEndAngle() const;
71 qreal pieEndAngle() const;
75
72
76 // convenience function
77 QPieSeries& operator << (QPieSlice* slice);
78 QPieSlice* append(qreal value, QString name);
79 void setLabelsVisible(bool visible = true);
73 void setLabelsVisible(bool visible = true);
80
74
81 // data from model
82 bool setModel(QAbstractItemModel* model);
75 bool setModel(QAbstractItemModel* model);
83 void setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation = Qt::Vertical);
76 void setModelMapping(int modelValuesLine, int modelLabelsLine, Qt::Orientation orientation = Qt::Vertical);
84
77
85 Q_SIGNALS:
78 Q_SIGNALS:
86 void clicked(QPieSlice* slice, Qt::MouseButtons buttons);
79 void clicked(QPieSlice* slice);
87 void hoverEnter(QPieSlice* slice);
80 void hovered(QPieSlice* slice, bool state);
88 void hoverLeave(QPieSlice* slice);
89 void added(QList<QPieSlice*> slices);
90 void removed(QList<QPieSlice*> slices);
91 void piePositionChanged();
92 void pieSizeChanged();
93
81
94 private:
82 private:
95 Q_DECLARE_PRIVATE(QPieSeries)
83 Q_DECLARE_PRIVATE(QPieSeries)
96 Q_DISABLE_COPY(QPieSeries)
84 Q_DISABLE_COPY(QPieSeries)
97 };
85 };
98
86
99 QTCOMMERCIALCHART_END_NAMESPACE
87 QTCOMMERCIALCHART_END_NAMESPACE
100
88
101 #endif // PIESERIES_H
89 #endif // PIESERIES_H
@@ -1,74 +1,84
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef QPIESERIES_P_H
21 #ifndef QPIESERIES_P_H
22 #define QPIESERIES_P_H
22 #define QPIESERIES_P_H
23
23
24 #include "qpieseries.h"
24 #include "qpieseries.h"
25 #include "qabstractseries_p.h"
25 #include "qabstractseries_p.h"
26
26
27 class QModelIndex;
27 class QModelIndex;
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 class QLegendPrivate;
30
31
31 class QPieSeriesPrivate : public QAbstractSeriesPrivate
32 class QPieSeriesPrivate : public QAbstractSeriesPrivate
32 {
33 {
33 Q_OBJECT
34 Q_OBJECT
34
35
35 public:
36 public:
36 QPieSeriesPrivate(QPieSeries *parent);
37 QPieSeriesPrivate(QPieSeries *parent);
37 ~QPieSeriesPrivate();
38 ~QPieSeriesPrivate();
38
39
39 void scaleDomain(Domain& domain);
40 void scaleDomain(Domain& domain);
40 Chart* createGraphics(ChartPresenter* presenter);
41 Chart* createGraphics(ChartPresenter *presenter);
41 QList<LegendMarker*> createLegendMarker(QLegend* legend);
42 QList<LegendMarker*> createLegendMarker(QLegend *legend);
42
43
43 void updateDerivativeData();
44 void updateDerivativeData();
44
45
46 static QPieSeriesPrivate* seriesData(QPieSeries &series);
47
48 Q_SIGNALS:
49 void added(QList<QPieSlice*> slices);
50 void removed(QList<QPieSlice*> slices);
51 void piePositionChanged();
52 void pieSizeChanged();
53
45 public Q_SLOTS:
54 public Q_SLOTS:
46 void sliceChanged();
55 void sliceChanged();
47 void sliceClicked(Qt::MouseButtons buttons);
56 void sliceClicked();
48 void sliceHoverEnter();
57 void sliceHovered(bool state);
49 void sliceHoverLeave();
50 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
58 void modelUpdated(QModelIndex topLeft, QModelIndex bottomRight);
51 void modelDataAdded(QModelIndex parent, int start, int end);
59 void modelDataAdded(QModelIndex parent, int start, int end);
52 void modelDataRemoved(QModelIndex parent, int start, int end);
60 void modelDataRemoved(QModelIndex parent, int start, int end);
53 bool setRealValue(qreal &value, qreal newValue, qreal max, qreal min = 0.0);
61 bool setRealValue(qreal &value, qreal newValue, qreal max, qreal min = 0.0);
54
62
55 public:
63 public:
56 QList<QPieSlice*> m_slices;
64 QList<QPieSlice*> m_slices;
57 qreal m_pieRelativeHorPos;
65 qreal m_pieRelativeHorPos;
58 qreal m_pieRelativeVerPos;
66 qreal m_pieRelativeVerPos;
59 qreal m_pieRelativeSize;
67 qreal m_pieRelativeSize;
60 qreal m_pieStartAngle;
68 qreal m_pieStartAngle;
61 qreal m_pieEndAngle;
69 qreal m_pieEndAngle;
62 qreal m_sum;
70 qreal m_sum;
63
71
64 // model map
72 // model map
65 int m_mapValues;
73 int m_mapValues;
66 int m_mapLabels;
74 int m_mapLabels;
67 Qt::Orientation m_mapOrientation;
75 Qt::Orientation m_mapOrientation;
76
68 private:
77 private:
78 friend class QLegendPrivate;
69 Q_DECLARE_PUBLIC(QPieSeries)
79 Q_DECLARE_PUBLIC(QPieSeries)
70 };
80 };
71
81
72 QTCOMMERCIALCHART_END_NAMESPACE
82 QTCOMMERCIALCHART_END_NAMESPACE
73
83
74 #endif // QPIESERIES_P_H
84 #endif // QPIESERIES_P_H
@@ -1,423 +1,410
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "qpieslice.h"
21 #include "qpieslice.h"
22 #include "pieslicedata_p.h"
22 #include "pieslicedata_p.h"
23
23
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25
25
26 /*!
26 /*!
27 \class QPieSlice
27 \class QPieSlice
28 \brief Defines a slice in pie series.
28 \brief Defines a slice in pie series.
29
29
30 This object defines the properties of a single slice in a QPieSeries.
30 This object defines the properties of a single slice in a QPieSeries.
31
31
32 In addition to the obvious value and label properties the user can also control
32 In addition to the obvious value and label properties the user can also control
33 the visual appearance of a slice. By modifying the visual appearance also means that
33 the visual appearance of a slice. By modifying the visual appearance also means that
34 the user is overriding the default appearance set by the theme. Even if the theme is
34 the user is overriding the default appearance set by the theme. Even if the theme is
35 changed users settings will persist.
35 changed users settings will persist.
36
36
37 To enable user interaction customization with the slices some basic signals
37 To enable user interaction customization with the slices some basic signals
38 are provided about clicking and hovering.
38 are provided about clicking and hovering.
39 */
39 */
40
40
41 /*!
41 /*!
42 \property QPieSlice::label
42 \property QPieSlice::label
43
43
44 Label of the slice.
44 Label of the slice.
45 */
45 */
46
46
47 /*!
47 /*!
48 \property QPieSlice::value
48 \property QPieSlice::value
49
49
50 Value of the slice.
50 Value of the slice.
51
51
52 \sa percentage(), QPieSeries::sum()
52 \sa percentage(), QPieSeries::sum()
53 */
53 */
54
54
55 /*!
55 /*!
56 Constructs an empty slice with a \a parent.
56 Constructs an empty slice with a \a parent.
57
57
58 \sa QPieSeries::append(), QPieSeries::insert(), QPieSeries::replace()
58 \sa QPieSeries::append(), QPieSeries::insert(), QPieSeries::replace()
59 */
59 */
60 QPieSlice::QPieSlice(QObject *parent)
60 QPieSlice::QPieSlice(QObject *parent)
61 :QObject(parent),
61 :QObject(parent),
62 d(new PieSliceData())
62 d(new PieSliceData())
63 {
63 {
64
64
65 }
65 }
66
66
67 /*!
67 /*!
68 Constructs an empty slice with given \a value, \a label and a \a parent.
68 Constructs an empty slice with given \a value, \a label and a \a parent.
69 \sa QPieSeries::append(), QPieSeries::insert(), QPieSeries::replace()
69 \sa QPieSeries::append(), QPieSeries::insert(), QPieSeries::replace()
70 */
70 */
71 QPieSlice::QPieSlice(qreal value, QString label, QObject *parent)
71 QPieSlice::QPieSlice(qreal value, QString label, QObject *parent)
72 :QObject(parent),
72 :QObject(parent),
73 d(new PieSliceData())
73 d(new PieSliceData())
74 {
74 {
75 d->m_value = value;
75 d->m_value = value;
76 d->m_labelText = label;
76 d->m_labelText = label;
77 }
77 }
78
78
79 /*!
79 /*!
80 Destroys the slice.
80 Destroys the slice.
81 User should not delete the slice if it has been added to the series.
81 User should not delete the slice if it has been added to the series.
82 */
82 */
83 QPieSlice::~QPieSlice()
83 QPieSlice::~QPieSlice()
84 {
84 {
85 delete d;
85 delete d;
86 }
86 }
87
87
88 /*!
88 /*!
89 Gets the value of the slice.
89 Gets the value of the slice.
90 Note that all values in the series
90 Note that all values in the series
91 \sa setValue()
91 \sa setValue()
92 */
92 */
93 qreal QPieSlice::value() const
93 qreal QPieSlice::value() const
94 {
94 {
95 return d->m_value;
95 return d->m_value;
96 }
96 }
97
97
98 /*!
98 /*!
99 Gets the label of the slice.
99 Gets the label of the slice.
100 \sa setLabel()
100 \sa setLabel()
101 */
101 */
102 QString QPieSlice::label() const
102 QString QPieSlice::label() const
103 {
103 {
104 return d->m_labelText;
104 return d->m_labelText;
105 }
105 }
106
106
107 /*!
107 /*!
108 Returns true if label is set as visible.
108 Returns true if label is set as visible.
109 \sa setLabelVisible()
109 \sa setLabelVisible()
110 */
110 */
111 bool QPieSlice::isLabelVisible() const
111 bool QPieSlice::isLabelVisible() const
112 {
112 {
113 return d->m_isLabelVisible;
113 return d->m_isLabelVisible;
114 }
114 }
115
115
116 /*!
116 /*!
117 Returns true if slice is exloded from the pie.
117 Returns true if slice is exloded from the pie.
118 \sa setExploded(), explodeDistanceFactor(), setExplodeDistanceFactor()
118 \sa setExploded(), explodeDistanceFactor(), setExplodeDistanceFactor()
119 */
119 */
120 bool QPieSlice::isExploded() const
120 bool QPieSlice::isExploded() const
121 {
121 {
122 return d->m_isExploded;
122 return d->m_isExploded;
123 }
123 }
124
124
125 /*!
125 /*!
126 Returns the explode distance factor.
126 Returns the explode distance factor.
127
127
128 The factor is relative to pie radius. For example:
128 The factor is relative to pie radius. For example:
129 1.0 means the distance is the same as the radius.
129 1.0 means the distance is the same as the radius.
130 0.5 means the distance is half of the radius.
130 0.5 means the distance is half of the radius.
131
131
132 Default value is 0.15.
132 Default value is 0.15.
133
133
134 \sa setExplodeDistanceFactor(), isExploded(), setExploded()
134 \sa setExplodeDistanceFactor(), isExploded(), setExploded()
135 */
135 */
136 qreal QPieSlice::explodeDistanceFactor() const
136 qreal QPieSlice::explodeDistanceFactor() const
137 {
137 {
138 return d->m_explodeDistanceFactor;
138 return d->m_explodeDistanceFactor;
139 }
139 }
140
140
141 /*!
141 /*!
142 Returns the percentage of this slice compared to the sum of all slices in the same series.
142 Returns the percentage of this slice compared to the sum of all slices in the same series.
143 The returned value ranges from 0 to 1.0.
143 The returned value ranges from 0 to 1.0.
144
144
145 Updated internally after the slice is added to the series.
145 Updated internally after the slice is added to the series.
146
146
147 \sa QPieSeries::sum()
147 \sa QPieSeries::sum()
148 */
148 */
149 qreal QPieSlice::percentage() const
149 qreal QPieSlice::percentage() const
150 {
150 {
151 return d->m_percentage;
151 return d->m_percentage;
152 }
152 }
153
153
154 /*!
154 /*!
155 Returns the starting angle of this slice in the series it belongs to.
155 Returns the starting angle of this slice in the series it belongs to.
156
156
157 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
157 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
158
158
159 Updated internally after the slice is added to the series.
159 Updated internally after the slice is added to the series.
160 */
160 */
161 qreal QPieSlice::startAngle() const
161 qreal QPieSlice::startAngle() const
162 {
162 {
163 return d->m_startAngle;
163 return d->m_startAngle;
164 }
164 }
165
165
166 /*!
166 /*!
167 Returns the end angle of this slice in the series it belongs to.
167 Returns the end angle of this slice in the series it belongs to.
168
168
169 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
169 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
170
170
171 Updated internally after the slice is added to the series.
171 Updated internally after the slice is added to the series.
172 */
172 */
173 qreal QPieSlice::endAngle() const
173 qreal QPieSlice::endAngle() const
174 {
174 {
175 return d->m_startAngle + d->m_angleSpan;
175 return d->m_startAngle + d->m_angleSpan;
176 }
176 }
177
177
178 /*!
178 /*!
179 Returns the pen used to draw this slice.
179 Returns the pen used to draw this slice.
180 \sa setPen()
180 \sa setPen()
181 */
181 */
182 QPen QPieSlice::pen() const
182 QPen QPieSlice::pen() const
183 {
183 {
184 return d->m_slicePen;
184 return d->m_slicePen;
185 }
185 }
186
186
187 /*!
187 /*!
188 Returns the brush used to draw this slice.
188 Returns the brush used to draw this slice.
189 \sa setBrush()
189 \sa setBrush()
190 */
190 */
191 QBrush QPieSlice::brush() const
191 QBrush QPieSlice::brush() const
192 {
192 {
193 return d->m_sliceBrush;
193 return d->m_sliceBrush;
194 }
194 }
195
195
196 /*!
196 /*!
197 Returns the pen used to draw the label in this slice.
197 Returns the pen used to draw the label in this slice.
198 \sa setLabelPen()
198 \sa setLabelPen()
199 */
199 */
200 QPen QPieSlice::labelPen() const
200 QPen QPieSlice::labelPen() const
201 {
201 {
202 return d->m_labelPen;
202 return d->m_labelPen;
203 }
203 }
204
204
205 /*!
205 /*!
206 Returns the font used to draw label in this slice.
206 Returns the font used to draw label in this slice.
207 \sa setLabelFont()
207 \sa setLabelFont()
208 */
208 */
209 QFont QPieSlice::labelFont() const
209 QFont QPieSlice::labelFont() const
210 {
210 {
211 return d->m_labelFont;
211 return d->m_labelFont;
212 }
212 }
213
213
214 /*!
214 /*!
215 Gets the label arm length factor.
215 Gets the label arm length factor.
216
216
217 The factor is relative to pie radius. For example:
217 The factor is relative to pie radius. For example:
218 1.0 means the length is the same as the radius.
218 1.0 means the length is the same as the radius.
219 0.5 means the length is half of the radius.
219 0.5 means the length is half of the radius.
220
220
221 Default value is 0.15
221 Default value is 0.15
222
222
223 \sa setLabelArmLengthFactor()
223 \sa setLabelArmLengthFactor()
224 */
224 */
225 qreal QPieSlice::labelArmLengthFactor() const
225 qreal QPieSlice::labelArmLengthFactor() const
226 {
226 {
227 return d->m_labelArmLengthFactor;
227 return d->m_labelArmLengthFactor;
228 }
228 }
229
229
230 /*!
230 /*!
231 \fn void QPieSlice::clicked(Qt::MouseButtons buttons)
231 \fn void QPieSlice::clicked()
232
232
233 This signal is emitted when user has clicked the slice.
233 This signal is emitted when user has clicked the slice.
234 Parameter \a buttons hold the information about the clicked mouse buttons.
235
234
236 \sa QPieSeries::clicked()
235 \sa QPieSeries::clicked()
237 */
236 */
238
237
239 /*!
238 /*!
240 \fn void QPieSlice::hoverEnter()
239 \fn void QPieSlice::hovered(bool state)
241
240
242 This signal is emitted when user has hovered over the slice.
241 This signal is emitted when user has hovered over or away from the slice.
243
242
244 \sa QPieSeries::hoverEnter()
243 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
245 */
246
247 /*!
248 \fn void QPieSlice::hoverLeave()
249
250 This signal is emitted when user has hovered away from the slice.
251
244
252 \sa QPieSeries::hoverLeave()
245 \sa QPieSeries::hovered()
253 */
246 */
254
247
255 /*!
248 /*!
256 \fn void QPieSlice::changed()
249 \fn void QPieSlice::changed()
257
250
258 This signal emitted when something has changed in the slice.
251 This signal emitted when something has changed in the slice.
259 */
252 */
260
253
261 /*!
254 /*!
262 \fn void QPieSlice::selected()
263
264 This signal emitted when this slice has been clicked in the legend.
265 */
266
267 /*!
268 Sets the \a value of this slice.
255 Sets the \a value of this slice.
269 \sa value()
256 \sa value()
270 */
257 */
271 void QPieSlice::setValue(qreal value)
258 void QPieSlice::setValue(qreal value)
272 {
259 {
273 if (!qFuzzyIsNull(d->m_value - value)) {
260 if (!qFuzzyIsNull(d->m_value - value)) {
274 d->m_value = value;
261 d->m_value = value;
275 emit changed();
262 emit changed();
276 }
263 }
277 }
264 }
278
265
279 /*!
266 /*!
280 Sets the \a label of the slice.
267 Sets the \a label of the slice.
281 \sa label()
268 \sa label()
282 */
269 */
283 void QPieSlice::setLabel(QString label)
270 void QPieSlice::setLabel(QString label)
284 {
271 {
285 if (d->m_labelText != label) {
272 if (d->m_labelText != label) {
286 d->m_labelText = label;
273 d->m_labelText = label;
287 emit changed();
274 emit changed();
288 }
275 }
289 }
276 }
290
277
291 /*!
278 /*!
292 Sets the label \a visible in this slice.
279 Sets the label \a visible in this slice.
293 \sa isLabelVisible(), QPieSeries::setLabelsVisible()
280 \sa isLabelVisible(), QPieSeries::setLabelsVisible()
294 */
281 */
295 void QPieSlice::setLabelVisible(bool visible)
282 void QPieSlice::setLabelVisible(bool visible)
296 {
283 {
297 if (d->m_isLabelVisible != visible) {
284 if (d->m_isLabelVisible != visible) {
298 d->m_isLabelVisible = visible;
285 d->m_isLabelVisible = visible;
299 emit changed();
286 emit changed();
300 }
287 }
301 }
288 }
302
289
303 /*!
290 /*!
304 Sets this slices \a exploded state.
291 Sets this slices \a exploded state.
305
292
306 If the slice is exploded it is moved away from the pie center. The distance is defined by the explode distance factor.
293 If the slice is exploded it is moved away from the pie center. The distance is defined by the explode distance factor.
307
294
308 \sa isExploded(), explodeDistanceFactor(), setExplodeDistanceFactor()
295 \sa isExploded(), explodeDistanceFactor(), setExplodeDistanceFactor()
309 */
296 */
310 void QPieSlice::setExploded(bool exploded)
297 void QPieSlice::setExploded(bool exploded)
311 {
298 {
312 if (d->m_isExploded != exploded) {
299 if (d->m_isExploded != exploded) {
313 d->m_isExploded = exploded;
300 d->m_isExploded = exploded;
314 emit changed();
301 emit changed();
315 }
302 }
316 }
303 }
317
304
318 /*!
305 /*!
319 Sets the explode distance \a factor.
306 Sets the explode distance \a factor.
320
307
321 The factor is relative to pie radius. For example:
308 The factor is relative to pie radius. For example:
322 1.0 means the distance is the same as the radius.
309 1.0 means the distance is the same as the radius.
323 0.5 means the distance is half of the radius.
310 0.5 means the distance is half of the radius.
324
311
325 Default value is 0.15
312 Default value is 0.15
326
313
327 \sa explodeDistanceFactor(), isExploded(), setExploded()
314 \sa explodeDistanceFactor(), isExploded(), setExploded()
328 */
315 */
329 void QPieSlice::setExplodeDistanceFactor(qreal factor)
316 void QPieSlice::setExplodeDistanceFactor(qreal factor)
330 {
317 {
331 if (!qFuzzyIsNull(d->m_explodeDistanceFactor - factor)) {
318 if (!qFuzzyIsNull(d->m_explodeDistanceFactor - factor)) {
332 d->m_explodeDistanceFactor = factor;
319 d->m_explodeDistanceFactor = factor;
333 emit changed();
320 emit changed();
334 }
321 }
335 }
322 }
336
323
337 /*!
324 /*!
338 Sets the \a pen used to draw this slice.
325 Sets the \a pen used to draw this slice.
339
326
340 Overrides the pen set by the theme.
327 Overrides the pen set by the theme.
341
328
342 \sa pen()
329 \sa pen()
343 */
330 */
344 void QPieSlice::setPen(const QPen &pen)
331 void QPieSlice::setPen(const QPen &pen)
345 {
332 {
346 if (d->m_slicePen != pen) {
333 if (d->m_slicePen != pen) {
347 d->m_slicePen = pen;
334 d->m_slicePen = pen;
348 d->m_slicePen.setThemed(false);
335 d->m_slicePen.setThemed(false);
349 emit changed();
336 emit changed();
350 }
337 }
351 }
338 }
352
339
353 /*!
340 /*!
354 Sets the \a brush used to draw this slice.
341 Sets the \a brush used to draw this slice.
355
342
356 Overrides the brush set by the theme.
343 Overrides the brush set by the theme.
357
344
358 \sa brush()
345 \sa brush()
359 */
346 */
360 void QPieSlice::setBrush(const QBrush &brush)
347 void QPieSlice::setBrush(const QBrush &brush)
361 {
348 {
362 if (d->m_sliceBrush != brush) {
349 if (d->m_sliceBrush != brush) {
363 d->m_sliceBrush = brush;
350 d->m_sliceBrush = brush;
364 d->m_sliceBrush.setThemed(false);
351 d->m_sliceBrush.setThemed(false);
365 emit changed();
352 emit changed();
366 }
353 }
367 }
354 }
368
355
369 /*!
356 /*!
370 Sets the \a pen used to draw the label in this slice.
357 Sets the \a pen used to draw the label in this slice.
371
358
372 Overrides the pen set by the theme.
359 Overrides the pen set by the theme.
373
360
374 \sa labelPen()
361 \sa labelPen()
375 */
362 */
376 void QPieSlice::setLabelPen(const QPen &pen)
363 void QPieSlice::setLabelPen(const QPen &pen)
377 {
364 {
378 if (d->m_labelPen != pen) {
365 if (d->m_labelPen != pen) {
379 d->m_labelPen = pen;
366 d->m_labelPen = pen;
380 d->m_labelPen.setThemed(false);
367 d->m_labelPen.setThemed(false);
381 emit changed();
368 emit changed();
382 }
369 }
383 }
370 }
384
371
385 /*!
372 /*!
386 Sets the \a font used to draw the label in this slice.
373 Sets the \a font used to draw the label in this slice.
387
374
388 Overrides the font set by the theme.
375 Overrides the font set by the theme.
389
376
390 \sa labelFont()
377 \sa labelFont()
391 */
378 */
392 void QPieSlice::setLabelFont(const QFont &font)
379 void QPieSlice::setLabelFont(const QFont &font)
393 {
380 {
394 if (d->m_labelFont != font) {
381 if (d->m_labelFont != font) {
395 d->m_labelFont = font;
382 d->m_labelFont = font;
396 d->m_labelFont.setThemed(false);
383 d->m_labelFont.setThemed(false);
397 emit changed();
384 emit changed();
398 }
385 }
399 }
386 }
400
387
401 /*!
388 /*!
402 Sets the label arm length \a factor.
389 Sets the label arm length \a factor.
403
390
404 The factor is relative to pie radius. For example:
391 The factor is relative to pie radius. For example:
405 1.0 means the length is the same as the radius.
392 1.0 means the length is the same as the radius.
406 0.5 means the length is half of the radius.
393 0.5 means the length is half of the radius.
407
394
408 Default value is 0.15
395 Default value is 0.15
409
396
410 \sa labelArmLengthFactor()
397 \sa labelArmLengthFactor()
411 */
398 */
412 void QPieSlice::setLabelArmLengthFactor(qreal factor)
399 void QPieSlice::setLabelArmLengthFactor(qreal factor)
413 {
400 {
414 if (!qFuzzyIsNull(d->m_labelArmLengthFactor - factor)) {
401 if (!qFuzzyIsNull(d->m_labelArmLengthFactor - factor)) {
415 d->m_labelArmLengthFactor = factor;
402 d->m_labelArmLengthFactor = factor;
416 emit changed();
403 emit changed();
417 }
404 }
418 }
405 }
419
406
420 QTCOMMERCIALCHART_END_NAMESPACE
407 QTCOMMERCIALCHART_END_NAMESPACE
421
408
422 QTCOMMERCIALCHART_USE_NAMESPACE
409 QTCOMMERCIALCHART_USE_NAMESPACE
423 #include "moc_qpieslice.cpp"
410 #include "moc_qpieslice.cpp"
@@ -1,88 +1,84
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #ifndef QPIESLICE_H
21 #ifndef QPIESLICE_H
22 #define QPIESLICE_H
22 #define QPIESLICE_H
23
23
24 #include <qchartglobal.h>
24 #include <qchartglobal.h>
25 #include <QObject>
25 #include <QObject>
26 #include <QPen>
26 #include <QPen>
27 #include <QBrush>
27 #include <QBrush>
28 #include <QFont>
28 #include <QFont>
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 class PieSliceData;
31 class PieSliceData;
32
32
33 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
33 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
34 {
34 {
35 Q_OBJECT
35 Q_OBJECT
36 Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY changed)
36 Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY changed)
37 Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY changed)
37 Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY changed)
38
38
39 public:
39 public:
40 explicit QPieSlice(QObject *parent = 0);
40 explicit QPieSlice(QObject *parent = 0);
41 QPieSlice(qreal value, QString label, QObject *parent = 0);
41 QPieSlice(qreal value, QString label, QObject *parent = 0);
42 virtual ~QPieSlice();
42 virtual ~QPieSlice();
43
43
44 // data
45 void setValue(qreal value);
44 void setValue(qreal value);
46 qreal value() const;
45 qreal value() const;
47 void setLabel(QString label);
46 void setLabel(QString label);
48 QString label() const;
47 QString label() const;
49 void setLabelVisible(bool visible = true);
48 void setLabelVisible(bool visible = true);
50 bool isLabelVisible() const;
49 bool isLabelVisible() const;
51 void setExploded(bool exploded = true);
50 void setExploded(bool exploded = true);
52 bool isExploded() const;
51 bool isExploded() const;
53
52
54 // generated data
55 qreal percentage() const;
56 qreal startAngle() const;
57 qreal endAngle() const;
58
59 // customization
60 void setPen(const QPen &pen);
53 void setPen(const QPen &pen);
61 QPen pen() const;
54 QPen pen() const;
62 void setBrush(const QBrush &brush);
55 void setBrush(const QBrush &brush);
63 QBrush brush() const;
56 QBrush brush() const;
64 void setLabelPen(const QPen &pen);
57 void setLabelPen(const QPen &pen);
65 QPen labelPen() const;
58 QPen labelPen() const;
66 void setLabelFont(const QFont &font);
59 void setLabelFont(const QFont &font);
67 QFont labelFont() const;
60 QFont labelFont() const;
61
68 void setLabelArmLengthFactor(qreal factor);
62 void setLabelArmLengthFactor(qreal factor);
69 qreal labelArmLengthFactor() const;
63 qreal labelArmLengthFactor() const;
70 void setExplodeDistanceFactor(qreal factor);
64 void setExplodeDistanceFactor(qreal factor);
71 qreal explodeDistanceFactor() const;
65 qreal explodeDistanceFactor() const;
72
66
67 qreal percentage() const;
68 qreal startAngle() const;
69 qreal endAngle() const;
70
73 Q_SIGNALS:
71 Q_SIGNALS:
74 void clicked(Qt::MouseButtons buttons);
72 void clicked();
75 void hoverEnter();
73 void hovered(bool state);
76 void hoverLeave();
77 void changed();
74 void changed();
78 void selected();
79
75
80 private:
76 private:
81 friend class PieSliceData;
77 friend class PieSliceData;
82 PieSliceData * const d;
78 PieSliceData * const d;
83 Q_DISABLE_COPY(QPieSlice)
79 Q_DISABLE_COPY(QPieSlice)
84 };
80 };
85
81
86 QTCOMMERCIALCHART_END_NAMESPACE
82 QTCOMMERCIALCHART_END_NAMESPACE
87
83
88 #endif // QPIESLICE_H
84 #endif // QPIESLICE_H
General Comments 0
You need to be logged in to leave comments. Login now