##// END OF EJS Templates
Use labelBrush instead of labelPen for text labels
Tero Ahola -
r1307:7b3a3ea4ca65
parent child
Show More
@@ -1,331 +1,331
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20 #include "mainwidget.h"
21 21 #include "customslice.h"
22 22 #include "pentool.h"
23 23 #include "brushtool.h"
24 24 #include <QPushButton>
25 25 #include <QComboBox>
26 26 #include <QCheckBox>
27 27 #include <QLabel>
28 28 #include <QGroupBox>
29 29 #include <QDoubleSpinBox>
30 30 #include <QFormLayout>
31 31 #include <QFontDialog>
32 32 #include <QChartView>
33 33 #include <QPieSeries>
34 34
35 35 QTCOMMERCIALCHART_USE_NAMESPACE
36 36
37 37 MainWidget::MainWidget(QWidget* parent)
38 38 :QWidget(parent),
39 39 m_slice(0)
40 40 {
41 41 // create chart
42 42 QChart *chart = new QChart;
43 43 chart->setTitle("Piechart customization");
44 44 chart->setAnimationOptions(QChart::AllAnimations);
45 45
46 46 // create series
47 47 m_series = new QPieSeries();
48 48 *m_series << new CustomSlice("Slice 1", 10.0);
49 49 *m_series << new CustomSlice("Slice 2", 20.0);
50 50 *m_series << new CustomSlice("Slice 3", 30.0);
51 51 *m_series << new CustomSlice("Slice 4", 40.0);
52 52 *m_series << new CustomSlice("Slice 5", 50.0);
53 53 m_series->setLabelsVisible();
54 54 chart->addSeries(m_series);
55 55
56 56 connect(m_series, SIGNAL(clicked(QPieSlice*)), this, SLOT(handleSliceClicked(QPieSlice*)));
57 57
58 58 // chart settings
59 59 m_themeComboBox = new QComboBox();
60 60 m_themeComboBox->addItem("Light", QChart::ChartThemeLight);
61 61 m_themeComboBox->addItem("BlueCerulean", QChart::ChartThemeBlueCerulean);
62 62 m_themeComboBox->addItem("Dark", QChart::ChartThemeDark);
63 63 m_themeComboBox->addItem("BrownSand", QChart::ChartThemeBrownSand);
64 64 m_themeComboBox->addItem("BlueNcs", QChart::ChartThemeBlueNcs);
65 65 m_themeComboBox->addItem("High Contrast", QChart::ChartThemeHighContrast);
66 66 m_themeComboBox->addItem("Blue Icy", QChart::ChartThemeBlueIcy);
67 67
68 68 m_aaCheckBox = new QCheckBox();
69 69 m_animationsCheckBox = new QCheckBox();
70 70 m_animationsCheckBox->setCheckState(Qt::Checked);
71 71
72 72 m_legendCheckBox = new QCheckBox();
73 73
74 74 QFormLayout* chartSettingsLayout = new QFormLayout();
75 75 chartSettingsLayout->addRow("Theme", m_themeComboBox);
76 76 chartSettingsLayout->addRow("Antialiasing", m_aaCheckBox);
77 77 chartSettingsLayout->addRow("Animations", m_animationsCheckBox);
78 78 chartSettingsLayout->addRow("Legend", m_legendCheckBox);
79 79 QGroupBox* chartSettings = new QGroupBox("Chart");
80 80 chartSettings->setLayout(chartSettingsLayout);
81 81
82 82 connect(m_themeComboBox, SIGNAL(currentIndexChanged(int)), this ,SLOT(updateChartSettings()));
83 83 connect(m_aaCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
84 84 connect(m_animationsCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
85 85 connect(m_legendCheckBox, SIGNAL(toggled(bool)), this ,SLOT(updateChartSettings()));
86 86
87 87 // series settings
88 88 m_hPosition = new QDoubleSpinBox();
89 89 m_hPosition->setMinimum(0.0);
90 90 m_hPosition->setMaximum(1.0);
91 91 m_hPosition->setSingleStep(0.1);
92 92 m_hPosition->setValue(m_series->horizontalPosition());
93 93
94 94 m_vPosition = new QDoubleSpinBox();
95 95 m_vPosition->setMinimum(0.0);
96 96 m_vPosition->setMaximum(1.0);
97 97 m_vPosition->setSingleStep(0.1);
98 98 m_vPosition->setValue(m_series->verticalPosition());
99 99
100 100 m_sizeFactor = new QDoubleSpinBox();
101 101 m_sizeFactor->setMinimum(0.0);
102 102 m_sizeFactor->setMaximum(1.0);
103 103 m_sizeFactor->setSingleStep(0.1);
104 104 m_sizeFactor->setValue(m_series->pieSize());
105 105
106 106 m_startAngle = new QDoubleSpinBox();
107 107 m_startAngle->setMinimum(-720);
108 108 m_startAngle->setMaximum(720);
109 109 m_startAngle->setValue(m_series->pieStartAngle());
110 110 m_startAngle->setSingleStep(1);
111 111
112 112 m_endAngle = new QDoubleSpinBox();
113 113 m_endAngle->setMinimum(-720);
114 114 m_endAngle->setMaximum(720);
115 115 m_endAngle->setValue(m_series->pieEndAngle());
116 116 m_endAngle->setSingleStep(1);
117 117
118 118 QPushButton *appendSlice = new QPushButton("Append slice");
119 119 QPushButton *insertSlice = new QPushButton("Insert slice");
120 120
121 121 QFormLayout* seriesSettingsLayout = new QFormLayout();
122 122 seriesSettingsLayout->addRow("Horizontal position", m_hPosition);
123 123 seriesSettingsLayout->addRow("Vertical position", m_vPosition);
124 124 seriesSettingsLayout->addRow("Size factor", m_sizeFactor);
125 125 seriesSettingsLayout->addRow("Start angle", m_startAngle);
126 126 seriesSettingsLayout->addRow("End angle", m_endAngle);
127 127 seriesSettingsLayout->addRow(appendSlice);
128 128 seriesSettingsLayout->addRow(insertSlice);
129 129 QGroupBox* seriesSettings = new QGroupBox("Series");
130 130 seriesSettings->setLayout(seriesSettingsLayout);
131 131
132 132 connect(m_vPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
133 133 connect(m_hPosition, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
134 134 connect(m_sizeFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
135 135 connect(m_startAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
136 136 connect(m_endAngle, SIGNAL(valueChanged(double)), this, SLOT(updateSerieSettings()));
137 137 connect(appendSlice, SIGNAL(clicked()), this, SLOT(appendSlice()));
138 138 connect(insertSlice, SIGNAL(clicked()), this, SLOT(insertSlice()));
139 139
140 140 // slice settings
141 141 m_sliceName = new QLabel("<click a slice>");
142 142 m_sliceValue = new QDoubleSpinBox();
143 143 m_sliceValue->setMaximum(1000);
144 144 m_sliceLabelVisible = new QCheckBox();
145 145 m_sliceLabelArmFactor = new QDoubleSpinBox();
146 146 m_sliceLabelArmFactor->setSingleStep(0.01);
147 147 m_sliceExploded = new QCheckBox();
148 148 m_sliceExplodedFactor = new QDoubleSpinBox();
149 149 m_sliceExplodedFactor->setSingleStep(0.01);
150 150 m_pen = new QPushButton();
151 151 m_penTool = new PenTool("Slice pen", this);
152 152 m_brush = new QPushButton();
153 153 m_brushTool = new BrushTool("Slice brush", this);
154 154 m_font = new QPushButton();
155 m_labelPen = new QPushButton();
156 m_labelPenTool = new PenTool("Label pen", this);
155 m_labelBrush = new QPushButton();
156 m_labelBrushTool = new BrushTool("Label brush", this);
157 157 QPushButton *removeSlice = new QPushButton("Remove slice");
158 158
159 159 QFormLayout* sliceSettingsLayout = new QFormLayout();
160 160 sliceSettingsLayout->addRow("Selected", m_sliceName);
161 161 sliceSettingsLayout->addRow("Value", m_sliceValue);
162 162 sliceSettingsLayout->addRow("Pen", m_pen);
163 163 sliceSettingsLayout->addRow("Brush", m_brush);
164 164 sliceSettingsLayout->addRow("Label visible", m_sliceLabelVisible);
165 165 sliceSettingsLayout->addRow("Label font", m_font);
166 sliceSettingsLayout->addRow("Label pen", m_labelPen);
166 sliceSettingsLayout->addRow("Label pen", m_labelBrush);
167 167 sliceSettingsLayout->addRow("Label arm length", m_sliceLabelArmFactor);
168 168 sliceSettingsLayout->addRow("Exploded", m_sliceExploded);
169 169 sliceSettingsLayout->addRow("Explode distance", m_sliceExplodedFactor);
170 170 sliceSettingsLayout->addRow(removeSlice);
171 171 QGroupBox* sliceSettings = new QGroupBox("Slice");
172 172 sliceSettings->setLayout(sliceSettingsLayout);
173 173
174 174 connect(m_sliceValue, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
175 175 connect(m_pen, SIGNAL(clicked()), m_penTool, SLOT(show()));
176 176 connect(m_penTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
177 177 connect(m_brush, SIGNAL(clicked()), m_brushTool, SLOT(show()));
178 178 connect(m_brushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
179 179 connect(m_font, SIGNAL(clicked()), this, SLOT(showFontDialog()));
180 connect(m_labelPen, SIGNAL(clicked()), m_labelPenTool, SLOT(show()));
181 connect(m_labelPenTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
180 connect(m_labelBrush, SIGNAL(clicked()), m_labelBrushTool, SLOT(show()));
181 connect(m_labelBrushTool, SIGNAL(changed()), this, SLOT(updateSliceSettings()));
182 182 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
183 183 connect(m_sliceLabelVisible, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
184 184 connect(m_sliceLabelArmFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
185 185 connect(m_sliceExploded, SIGNAL(toggled(bool)), this, SLOT(updateSliceSettings()));
186 186 connect(m_sliceExplodedFactor, SIGNAL(valueChanged(double)), this, SLOT(updateSliceSettings()));
187 187 connect(removeSlice, SIGNAL(clicked()), this, SLOT(removeSlice()));
188 188
189 189 // create chart view
190 190 m_chartView = new QChartView(chart);
191 191
192 192 // create main layout
193 193 QVBoxLayout *settingsLayout = new QVBoxLayout();
194 194 settingsLayout->addWidget(chartSettings);
195 195 settingsLayout->addWidget(seriesSettings);
196 196 settingsLayout->addWidget(sliceSettings);
197 197 settingsLayout->addStretch();
198 198
199 199 QGridLayout* baseLayout = new QGridLayout();
200 200 baseLayout->addLayout(settingsLayout, 0, 0);
201 201 baseLayout->addWidget(m_chartView, 0, 1);
202 202 setLayout(baseLayout);
203 203
204 204 updateSerieSettings();
205 205 }
206 206
207 207
208 208 void MainWidget::updateChartSettings()
209 209 {
210 210 QChart::ChartTheme theme = (QChart::ChartTheme) m_themeComboBox->itemData(m_themeComboBox->currentIndex()).toInt();
211 211 m_chartView->chart()->setTheme(theme);
212 212 m_chartView->setRenderHint(QPainter::Antialiasing, m_aaCheckBox->isChecked());
213 213
214 214 if (m_animationsCheckBox->checkState() == Qt::Checked)
215 215 m_chartView->chart()->setAnimationOptions(QChart::AllAnimations);
216 216 else
217 217 m_chartView->chart()->setAnimationOptions(QChart::NoAnimation);
218 218
219 219 if (m_legendCheckBox->checkState() == Qt::Checked)
220 220 m_chartView->chart()->legend()->show();
221 221 else
222 222 m_chartView->chart()->legend()->hide();
223 223 }
224 224
225 225 void MainWidget::updateSerieSettings()
226 226 {
227 227 m_series->setHorizontalPosition(m_hPosition->value());
228 228 m_series->setVerticalPosition(m_vPosition->value());
229 229 m_series->setPieSize(m_sizeFactor->value());
230 230 m_series->setPieStartAngle(m_startAngle->value());
231 231 m_series->setPieEndAngle(m_endAngle->value());
232 232 }
233 233
234 234 void MainWidget::updateSliceSettings()
235 235 {
236 236 if (!m_slice)
237 237 return;
238 238
239 239 m_slice->setValue(m_sliceValue->value());
240 240
241 241 m_slice->setPen(m_penTool->pen());
242 242 m_slice->setBrush(m_brushTool->brush());
243 243
244 m_slice->setLabelPen(m_labelPenTool->pen());
244 m_slice->setLabelBrush(m_labelBrushTool->brush());
245 245 m_slice->setLabelVisible(m_sliceLabelVisible->isChecked());
246 246 m_slice->setLabelArmLengthFactor(m_sliceLabelArmFactor->value());
247 247
248 248 m_slice->setExploded(m_sliceExploded->isChecked());
249 249 m_slice->setExplodeDistanceFactor(m_sliceExplodedFactor->value());
250 250 }
251 251
252 252 void MainWidget::handleSliceClicked(QPieSlice* slice)
253 253 {
254 254 m_slice = static_cast<CustomSlice*>(slice);
255 255
256 256 // name
257 257 m_sliceName->setText(slice->label());
258 258
259 259 // value
260 260 m_sliceValue->blockSignals(true);
261 261 m_sliceValue->setValue(slice->value());
262 262 m_sliceValue->blockSignals(false);
263 263
264 264 // pen
265 265 m_pen->setText(PenTool::name(m_slice->pen()));
266 266 m_penTool->setPen(m_slice->pen());
267 267
268 268 // brush
269 269 m_brush->setText(m_slice->originalBrush().color().name());
270 270 m_brushTool->setBrush(m_slice->originalBrush());
271 271
272 272 // label
273 m_labelPen->setText(PenTool::name(m_slice->labelPen()));
274 m_labelPenTool->setPen(m_slice->labelPen());
273 m_labelBrush->setText(BrushTool::name(m_slice->labelBrush()));
274 m_labelBrushTool->setBrush(m_slice->labelBrush());
275 275 m_font->setText(slice->labelFont().toString());
276 276 m_sliceLabelVisible->blockSignals(true);
277 277 m_sliceLabelVisible->setChecked(slice->isLabelVisible());
278 278 m_sliceLabelVisible->blockSignals(false);
279 279 m_sliceLabelArmFactor->blockSignals(true);
280 280 m_sliceLabelArmFactor->setValue(slice->labelArmLengthFactor());
281 281 m_sliceLabelArmFactor->blockSignals(false);
282 282
283 283 // exploded
284 284 m_sliceExploded->blockSignals(true);
285 285 m_sliceExploded->setChecked(slice->isExploded());
286 286 m_sliceExploded->blockSignals(false);
287 287 m_sliceExplodedFactor->blockSignals(true);
288 288 m_sliceExplodedFactor->setValue(slice->explodeDistanceFactor());
289 289 m_sliceExplodedFactor->blockSignals(false);
290 290 }
291 291
292 292 void MainWidget::showFontDialog()
293 293 {
294 294 if (!m_slice)
295 295 return;
296 296
297 297 QFontDialog dialog(m_slice->labelFont());
298 298 dialog.show();
299 299 dialog.exec();
300 300
301 301 m_slice->setLabelFont(dialog.currentFont());
302 302 m_font->setText(dialog.currentFont().toString());
303 303 }
304 304
305 305 void MainWidget::appendSlice()
306 306 {
307 307 *m_series << new CustomSlice("Slice " + QString::number(m_series->count()+1), 10.0);
308 308 }
309 309
310 310 void MainWidget::insertSlice()
311 311 {
312 312 if (!m_slice)
313 313 return;
314 314
315 315 int i = m_series->slices().indexOf(m_slice);
316 316
317 317 m_series->insert(i, new CustomSlice("Slice " + QString::number(m_series->count()+1), 10.0));
318 318 }
319 319
320 320 void MainWidget::removeSlice()
321 321 {
322 322 if (!m_slice)
323 323 return;
324 324
325 325 m_sliceName->setText("<click a slice>");
326 326
327 327 m_series->remove(m_slice);
328 328 m_slice = 0;
329 329 }
330 330
331 331 #include "moc_mainwidget.cpp"
@@ -1,91 +1,91
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20 #ifndef MAINWIDGET_H
21 21 #define MAINWIDGET_H
22 22
23 23 #include <QWidget>
24 24 #include <QChartGlobal>
25 25
26 26 class QLabel;
27 27 class QPushButton;
28 28 class QCheckBox;
29 29 class QComboBox;
30 30 class QDoubleSpinBox;
31 31 class PenTool;
32 32 class BrushTool;
33 33 class CustomSlice;
34 34
35 35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36 36 class QChartView;
37 37 class QPieSeries;
38 38 class QPieSlice;
39 39 QTCOMMERCIALCHART_END_NAMESPACE
40 40
41 41 QTCOMMERCIALCHART_USE_NAMESPACE
42 42
43 43 class MainWidget : public QWidget
44 44 {
45 45 Q_OBJECT
46 46
47 47 public:
48 48 explicit MainWidget(QWidget* parent = 0);
49 49
50 50 public Q_SLOTS:
51 51 void updateChartSettings();
52 52 void updateSerieSettings();
53 53 void updateSliceSettings();
54 54 void handleSliceClicked(QPieSlice* slice);
55 55 void showFontDialog();
56 56 void appendSlice();
57 57 void insertSlice();
58 58 void removeSlice();
59 59
60 60 private:
61 61 QComboBox *m_themeComboBox;
62 62 QCheckBox *m_aaCheckBox;
63 63 QCheckBox *m_animationsCheckBox;
64 64 QCheckBox *m_legendCheckBox;
65 65
66 66 QChartView* m_chartView;
67 67 QPieSeries* m_series;
68 68 CustomSlice* m_slice;
69 69
70 70 QDoubleSpinBox* m_hPosition;
71 71 QDoubleSpinBox* m_vPosition;
72 72 QDoubleSpinBox* m_sizeFactor;
73 73 QDoubleSpinBox* m_startAngle;
74 74 QDoubleSpinBox* m_endAngle;
75 75
76 76 QLabel* m_sliceName;
77 77 QDoubleSpinBox* m_sliceValue;
78 78 QCheckBox* m_sliceLabelVisible;
79 79 QDoubleSpinBox* m_sliceLabelArmFactor;
80 80 QCheckBox* m_sliceExploded;
81 81 QDoubleSpinBox* m_sliceExplodedFactor;
82 82 QPushButton *m_brush;
83 83 BrushTool *m_brushTool;
84 84 QPushButton *m_pen;
85 85 PenTool *m_penTool;
86 86 QPushButton *m_font;
87 QPushButton *m_labelPen;
88 PenTool *m_labelPenTool;
87 QPushButton *m_labelBrush;
88 BrushTool *m_labelBrushTool;
89 89 };
90 90
91 91 #endif // MAINWIDGET_H
@@ -1,149 +1,150
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "declarativepieseries.h"
22 22 #include "declarativechart.h"
23 23 #include "qchart.h"
24 24 #include <qdeclarativelist.h>
25 25 #include <QVPieModelMapper>
26 26 #include <QHPieModelMapper>
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
30 30 DeclarativePieSlice::DeclarativePieSlice(QObject *parent) :
31 31 QPieSlice(parent)
32 32 {
33 33 }
34 34
35 35 QColor DeclarativePieSlice::color()
36 36 {
37 37 return brush().color();
38 38 }
39 39
40 40 void DeclarativePieSlice::setColor(QColor color)
41 41 {
42 42 QBrush b = brush();
43 43 b.setColor(color);
44 44 setBrush(b);
45 45 }
46 46
47 47 QColor DeclarativePieSlice::borderColor()
48 48 {
49 49 return pen().color();
50 50 }
51 51
52 52 void DeclarativePieSlice::setBorderColor(QColor color)
53 53 {
54 54 QPen p = pen();
55 55 p.setColor(color);
56 56 setPen(p);
57 57 }
58 58
59 59 int DeclarativePieSlice::borderWidth()
60 60 {
61 61 return pen().width();
62 62 }
63 63
64 64 void DeclarativePieSlice::setBorderWidth(int width)
65 65 {
66 66 QPen p = pen();
67 67 p.setWidth(width);
68 68 setPen(p);
69 69 }
70 70
71 71 QColor DeclarativePieSlice::labelColor()
72 72 {
73 return labelPen().color();
73 return labelBrush().color();
74 74 }
75 75
76 76 void DeclarativePieSlice::setLabelColor(QColor color)
77 77 {
78 QPen p = labelPen();
79 p.setColor(color);
80 setLabelPen(p);
78 // TODO: use brush instead for label color
79 QBrush b = labelBrush();
80 b.setColor(color);
81 setLabelBrush(b);
81 82 }
82 83
83 84 DeclarativePieSeries::DeclarativePieSeries(QObject *parent) :
84 85 QPieSeries(parent)
85 86 {
86 87 }
87 88
88 89 void DeclarativePieSeries::classBegin()
89 90 {
90 91 }
91 92
92 93 void DeclarativePieSeries::componentComplete()
93 94 {
94 95 foreach(QObject *child, children()) {
95 96 if (qobject_cast<DeclarativePieSlice *>(child)) {
96 97 QPieSeries::append(qobject_cast<DeclarativePieSlice *>(child));
97 98 } else if(qobject_cast<QVPieModelMapper *>(child)) {
98 99 QVPieModelMapper *mapper = qobject_cast<QVPieModelMapper *>(child);
99 100 mapper->setSeries(this);
100 101 } else if(qobject_cast<QHPieModelMapper *>(child)) {
101 102 QHPieModelMapper *mapper = qobject_cast<QHPieModelMapper *>(child);
102 103 mapper->setSeries(this);
103 104 }
104 105 }
105 106 }
106 107
107 108 QDeclarativeListProperty<QObject> DeclarativePieSeries::seriesChildren()
108 109 {
109 110 return QDeclarativeListProperty<QObject>(this, 0, &DeclarativePieSeries::appendSeriesChildren);
110 111 }
111 112
112 113 void DeclarativePieSeries::appendSeriesChildren(QDeclarativeListProperty<QObject> * list, QObject *element)
113 114 {
114 115 // Empty implementation; the children are parsed in componentComplete instead
115 116 Q_UNUSED(list);
116 117 Q_UNUSED(element);
117 118 }
118 119
119 120 DeclarativePieSlice *DeclarativePieSeries::at(int index)
120 121 {
121 122 QList<QPieSlice*> sliceList = slices();
122 123 if (index < sliceList.count())
123 124 return qobject_cast<DeclarativePieSlice *>(sliceList[index]);
124 125
125 126 return 0;
126 127 }
127 128
128 129 DeclarativePieSlice* DeclarativePieSeries::find(QString label)
129 130 {
130 131 foreach (QPieSlice *slice, slices()) {
131 132 if (slice->label() == label)
132 133 return qobject_cast<DeclarativePieSlice *>(slice);
133 134 }
134 135 return 0;
135 136 }
136 137
137 138 DeclarativePieSlice* DeclarativePieSeries::append(QString label, qreal value)
138 139 {
139 140 // TODO: parameter order is wrong, switch it:
140 141 DeclarativePieSlice *slice = new DeclarativePieSlice(this);
141 142 slice->setLabel(label);
142 143 slice->setValue(value);
143 144 QPieSeries::append(slice);
144 145 return slice;
145 146 }
146 147
147 148 #include "moc_declarativepieseries.cpp"
148 149
149 150 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,342 +1,323
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qbarset.h"
22 22 #include "qbarset_p.h"
23 23
24 24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 25
26 26 /*!
27 27 \class QBarSet
28 28 \brief part of QtCommercial chart API.
29 29
30 30 QBarSet represents one set of bars. Set of bars contains one data value for each category.
31 31 First value of set is assumed to belong to first category, second to second category and so on.
32 32 If set has fewer values than there are categories, then the missing values are assumed to be
33 33 at the end of set. For missing values in middle of a set, numerical value of zero is used.
34 34
35 35 \mainclass
36 36
37 37 \sa QBarSeries, QGroupedBarSeries, QStackedBarSeries, QPercentBarSeries
38 38 */
39 39
40 40 /*!
41 41 \fn void QBarSet::clicked(QString category)
42 42 \brief signals that set has been clicked
43 43 Parameter \a category describes on which category was clicked
44 44 */
45 45
46 46 /*!
47 47 \fn void QBarSet::hovered(bool status)
48 48 \brief signals that mouse has hovered over the set. If \a status is true, then mouse was entered. If \a status is false, then mouse was left.
49 49
50 50 The signal is emitted if mouse is hovered on top of set
51 51 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
52 52 */
53 53
54 54 /*!
55 55 Constructs QBarSet with a name of \a name and with parent of \a parent
56 56 */
57 57 QBarSet::QBarSet(const QString name, QObject *parent)
58 58 : QObject(parent)
59 59 ,d_ptr(new QBarSetPrivate(name,this))
60 60 {
61 61 }
62 62
63 63 /*!
64 64 Destroys the barset
65 65 */
66 66 QBarSet::~QBarSet()
67 67 {
68 68 // NOTE: d_ptr destroyed by QObject
69 69 }
70 70
71 71 /*!
72 72 Sets new \a name for set.
73 73 */
74 74 void QBarSet::setName(const QString name)
75 75 {
76 76 d_ptr->m_name = name;
77 77 }
78 78
79 79 /*!
80 80 Returns name of the set.
81 81 */
82 82 QString QBarSet::name() const
83 83 {
84 84 return d_ptr->m_name;
85 85 }
86 86
87 87 /*!
88 88 Appends a point to set. Parameter \a value x coordinate defines the
89 89 position in x-axis and y coordinate defines the height of bar.
90 90 Depending on presentation (QBarSeries, QGroupedBarSeries, QStackedBarSeries, QPercentBarSeries)
91 91 the x values are used or ignored.
92 92 */
93 93 void QBarSet::append(const QPointF value)
94 94 {
95 95 d_ptr->m_values.append(value);
96 96 emit d_ptr->restructuredBars();
97 97 }
98 98
99 99 /*!
100 100 Appends a list of \a values to set. Works like append with single point.
101 101 \sa append()
102 102 */
103 103 void QBarSet::append(const QList<QPointF> values)
104 104 {
105 105 for (int i=0; i<values.count(); i++) {
106 106 d_ptr->m_values.append(values.at(i));
107 107 }
108 108 emit d_ptr->restructuredBars();
109 109 }
110 110
111 111 /*!
112 112 Appends new value \a value to the end of set. Internally the value is converted to QPointF,
113 113 with x coordinate being the index of appended value and y coordinate is the value.
114 114 */
115 115 void QBarSet::append(const qreal value)
116 116 {
117 117 append(QPointF(d_ptr->m_values.count(), value));
118 118 }
119 119
120 120 /*!
121 121 Appends a list of reals to set. Works like append with single real value. The values in list
122 122 are converted to QPointF, where x coordinate is the index of point and y coordinate is the value.
123 123 \sa append()
124 124 */
125 125 void QBarSet::append(const QList<qreal> values)
126 126 {
127 127 int index = d_ptr->m_values.count();
128 128 for (int i=0; i<values.count(); i++) {
129 129 d_ptr->m_values.append(QPointF(index,values.at(i)));
130 130 index++;
131 131 }
132 132 emit d_ptr->restructuredBars();
133 133 }
134 134
135 135 /*!
136 136 Convinience operator. Same as append, with real \a value.
137 137 \sa append()
138 138 */
139 139 QBarSet& QBarSet::operator << (const qreal &value)
140 140 {
141 141 append(value);
142 142 return *this;
143 143 }
144 144
145 145 /*!
146 146 Convinience operator. Same as append, with QPointF \a value.
147 147 \sa append()
148 148 */
149 149 QBarSet& QBarSet::operator << (const QPointF &value)
150 150 {
151 151 append(value);
152 152 return *this;
153 153 }
154 154
155 155 /*!
156 156 Inserts new \a value on the \a index position.
157 157 The value that is currently at this postion is moved to postion index + 1
158 158 \sa remove()
159 159 */
160 160 void QBarSet::insert(const int index, const qreal value)
161 161 {
162 162 d_ptr->m_values.insert(index, QPointF(index, value));
163 163 // emit d_ptr->updatedBars();
164 164 }
165 165
166 166 /*!
167 167 Removes the value specified by \a index
168 168 \sa insert()
169 169 */
170 170 void QBarSet::remove(const int index)
171 171 {
172 172 d_ptr->m_values.removeAt(index);
173 173 // emit d_ptr->updatedBars();
174 174 }
175 175
176 176 /*!
177 177 Sets a new value \a value to set, indexed by \a index
178 178 */
179 179 void QBarSet::replace(const int index, const qreal value)
180 180 {
181 181 d_ptr->m_values.replace(index,QPointF(index,value));
182 182 emit d_ptr->updatedBars();
183 183 }
184 184
185 185 /*!
186 186 Returns value of set indexed by \a index. Note that all appended values are stored internally as QPointF.
187 187 The returned QPointF has x coordinate, which is index (if appended with qreal append) or the x value
188 188 of the QPointF (if appended with QPointF append).
189 189 If the index is out of bounds QPointF(0, 0.0) is returned.
190 190 */
191 191 QPointF QBarSet::at(const int index) const
192 192 {
193 193 if (index < 0 || index >= d_ptr->m_values.count()) {
194 194 return QPointF(index, 0.0);
195 195 }
196 196
197 197 return d_ptr->m_values.at(index);
198 198 }
199 199
200 200 /*!
201 201 Returns value of set indexed by \a index. ote that all appended values are stored internally as QPointF.
202 202 The returned QPointF has x coordinate, which is index (if appended with qreal append) or the x value
203 203 of the QPointF (if appended with QPointF append).
204 204 */
205 205 QPointF QBarSet::operator [](const int index) const
206 206 {
207 207 return d_ptr->m_values.at(index);
208 208 }
209 209
210 210 /*!
211 211 Returns count of values in set.
212 212 */
213 213 int QBarSet::count() const
214 214 {
215 215 return d_ptr->m_values.count();
216 216 }
217 217
218 218 /*!
219 219 Returns sum of all values in barset. The sum is sum of y coordinates in the QPointF representation.
220 220 */
221 221 qreal QBarSet::sum() const
222 222 {
223 223 qreal total(0);
224 224 for (int i=0; i < d_ptr->m_values.count(); i++) {
225 225 //total += d_ptr->m_values.at(i);
226 226 total += d_ptr->m_values.at(i).y();
227 227 }
228 228 return total;
229 229 }
230 230
231 231 /*!
232 232 Sets pen for set. Bars of this set are drawn using \a pen
233 233 */
234 234 void QBarSet::setPen(const QPen &pen)
235 235 {
236 236 if(d_ptr->m_pen!=pen){
237 237 d_ptr->m_pen = pen;
238 238 emit d_ptr->updatedBars();
239 239 }
240 240 }
241 241
242 242 /*!
243 243 Returns pen of the set.
244 244 */
245 245 QPen QBarSet::pen() const
246 246 {
247 247 return d_ptr->m_pen;
248 248 }
249 249
250 250 /*!
251 251 Sets brush for the set. Bars of this set are drawn using \a brush
252 252 */
253 253 void QBarSet::setBrush(const QBrush &brush)
254 254 {
255 255 if(d_ptr->m_brush!=brush){
256 256 d_ptr->m_brush = brush;
257 257 emit d_ptr->updatedBars();
258 258 }
259 259 }
260 260
261 261 /*!
262 262 Returns brush of the set.
263 263 */
264 264 QBrush QBarSet::brush() const
265 265 {
266 266 return d_ptr->m_brush;
267 267 }
268 268
269 269 /*!
270 Sets \a pen of the values that are drawn on top of this barset
271 */
272 void QBarSet::setLabelPen(const QPen &pen)
273 {
274 if(d_ptr->m_labelPen!=pen){
275 d_ptr->m_labelPen = pen;
276 emit d_ptr->updatedBars();
277 }
278 }
279
280 /*!
281 Returns pen of the values that are drawn on top of this barset
282 */
283 QPen QBarSet::labelPen() const
284 {
285 return d_ptr->m_labelPen;
286 }
287
288 /*!
289 270 Sets \a brush of the values that are drawn on top of this barset
290 271 */
291 272 void QBarSet::setLabelBrush(const QBrush &brush)
292 273 {
293 274 if(d_ptr->m_labelBrush!=brush){
294 275 d_ptr->m_labelBrush = brush;
295 276 emit d_ptr->updatedBars();
296 277 }
297 278 }
298 279
299 280 /*!
300 281 Returns brush of the values that are drawn on top of this barset
301 282 */
302 283 QBrush QBarSet::labelBrush() const
303 284 {
304 285 return d_ptr->m_labelBrush;
305 286 }
306 287
307 288 /*!
308 289 Sets the \a font for values that are drawn on top of this barset
309 290 */
310 291 void QBarSet::setLabelFont(const QFont &font)
311 292 {
312 293 if(d_ptr->m_labelFont!=font) {
313 294 d_ptr->m_labelFont = font;
314 295 emit d_ptr->updatedBars();
315 296 }
316 297
317 298 }
318 299
319 300 /*!
320 301 Returns the pen for values that are drawn on top of this set
321 302 */
322 303 QFont QBarSet::labelFont() const
323 304 {
324 305 return d_ptr->m_labelFont;
325 306 }
326 307
327 308 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
328 309
329 310 QBarSetPrivate::QBarSetPrivate(const QString name, QBarSet *parent) : QObject(parent),
330 311 q_ptr(parent),
331 312 m_name(name)
332 313 {
333 314 }
334 315
335 316 QBarSetPrivate::~QBarSetPrivate()
336 317 {
337 318 }
338 319
339 320 #include "moc_qbarset.cpp"
340 321 #include "moc_qbarset_p.cpp"
341 322
342 323 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,90 +1,87
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef QBARSET_H
22 22 #define QBARSET_H
23 23
24 24 #include <qchartglobal.h>
25 25 #include <QPen>
26 26 #include <QBrush>
27 27 #include <QFont>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30 class QBarSetPrivate;
31 31
32 32 class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject
33 33 {
34 34 Q_OBJECT
35 35 Q_PROPERTY(QString name READ name WRITE setName)
36 36
37 37 public:
38 38 explicit QBarSet(const QString name, QObject *parent = 0);
39 39 virtual ~QBarSet();
40 40
41 41 void setName(const QString name);
42 42 QString name() const;
43 43
44 44 void append(const QPointF value);
45 45 void append(const QList<QPointF> values);
46 46 void append(const qreal value);
47 47 void append(const QList<qreal> values);
48 48
49 49 QBarSet& operator << (const qreal &value);
50 50 QBarSet& operator << (const QPointF &value);
51 51
52 52 void insert(const int index, const qreal value);
53 53 void remove(const int index);
54 54 void replace(const int index, const qreal value);
55 55 QPointF at(const int index) const;
56 56 QPointF operator [] (const int index) const;
57 57 int count() const;
58 58 qreal sum() const;
59 59
60 60 void setPen(const QPen &pen);
61 61 QPen pen() const;
62 62
63 63 void setBrush(const QBrush &brush);
64 64 QBrush brush() const;
65 65
66 void setLabelPen(const QPen &pen);
67 QPen labelPen() const;
68
69 66 void setLabelBrush(const QBrush &brush);
70 67 QBrush labelBrush() const;
71 68
72 69 void setLabelFont(const QFont &font);
73 70 QFont labelFont() const;
74 71
75 72 //Q_SIGNALS:
76 73 // void clicked(QString category);
77 74 // void hovered(bool status);
78 75
79 76 private:
80 77 QScopedPointer<QBarSetPrivate> d_ptr;
81 78 Q_DISABLE_COPY(QBarSet)
82 79 friend class QBarSeries;
83 80 friend class BarLegendMarker;
84 81 friend class BarChartItem;
85 82 friend class QBarSeriesPrivate;
86 83 };
87 84
88 85 QTCOMMERCIALCHART_END_NAMESPACE
89 86
90 87 #endif // QBARSET_H
@@ -1,69 +1,68
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 // W A R N I N G
22 22 // -------------
23 23 //
24 24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 25 // implementation detail. This header file may change from version to
26 26 // version without notice, or even be removed.
27 27 //
28 28 // We mean it.
29 29
30 30 #ifndef QBARSET_P_H
31 31 #define QBARSET_P_H
32 32
33 33 #include "qbarset.h"
34 34 #include <QMap>
35 35 #include <QPen>
36 36 #include <QBrush>
37 37 #include <QFont>
38 38
39 39 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40 40
41 41 class QBarSetPrivate : public QObject
42 42 {
43 43 Q_OBJECT
44 44
45 45 public:
46 46 QBarSetPrivate(const QString name, QBarSet *parent);
47 47 ~QBarSetPrivate();
48 48
49 49 Q_SIGNALS:
50 50 void clicked(QString category);
51 51 void restructuredBars();
52 52 void updatedBars();
53 53
54 54 public:
55 55 QBarSet * const q_ptr;
56 56 QString m_name;
57 57 QList<QPointF> m_values;
58 58 QPen m_pen;
59 59 QBrush m_brush;
60 QPen m_labelPen;
61 60 QBrush m_labelBrush;
62 61 QFont m_labelFont;
63 62
64 63 friend class QBarSet;
65 64 };
66 65
67 66 QTCOMMERCIALCHART_END_NAMESPACE
68 67
69 68 #endif // QBARSETPRIVATE_P_H
@@ -1,385 +1,385
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "charttheme_p.h"
22 22 #include "qchart.h"
23 23 #include "qchart_p.h"
24 24 #include "qchartview.h"
25 25 #include "qlegend.h"
26 26 #include "qaxis.h"
27 27 #include <QTime>
28 28
29 29 //series
30 30 #include "qbarset.h"
31 31 #include "qbarseries.h"
32 32 #include "qstackedbarseries.h"
33 33 #include "qpercentbarseries.h"
34 34 #include "qlineseries.h"
35 35 #include "qareaseries.h"
36 36 #include "qscatterseries.h"
37 37 #include "qpieseries.h"
38 38 #include "qpieslice.h"
39 39 #include "qpieslice_p.h"
40 40 #include "qsplineseries.h"
41 41
42 42 //items
43 43 #include "chartaxis_p.h"
44 44 #include "barchartitem_p.h"
45 45 #include "stackedbarchartitem_p.h"
46 46 #include "percentbarchartitem_p.h"
47 47 #include "linechartitem_p.h"
48 48 #include "areachartitem_p.h"
49 49 #include "scatterchartitem_p.h"
50 50 #include "piechartitem_p.h"
51 51 #include "splinechartitem_p.h"
52 52
53 53 //themes
54 54 #include "chartthemesystem_p.h"
55 55 #include "chartthemelight_p.h"
56 56 #include "chartthemebluecerulean_p.h"
57 57 #include "chartthemedark_p.h"
58 58 #include "chartthemebrownsand_p.h"
59 59 #include "chartthemebluencs_p.h"
60 60 #include "chartthemehighcontrast_p.h"
61 61 #include "chartthemeblueicy_p.h"
62 62
63 63 QTCOMMERCIALCHART_BEGIN_NAMESPACE
64 64
65 65 ChartTheme::ChartTheme(QChart::ChartTheme id) :
66 66 m_masterFont(QFont("arial", 14)),
67 67 m_labelFont(QFont("arial", 10)),
68 68 m_titleBrush(QColor(QRgb(0x000000))),
69 69 m_axisLinePen(QPen(QRgb(0x000000))),
70 70 m_axisLabelBrush(QColor(QRgb(0x000000))),
71 71 m_backgroundShadesPen(Qt::NoPen),
72 72 m_backgroundShadesBrush(Qt::NoBrush),
73 73 m_backgroundShades(BackgroundShadesNone),
74 74 m_backgroundDropShadowEnabled(false),
75 75 m_gridLinePen(QPen(QRgb(0x000000))),
76 76 m_force(false)
77 77 {
78 78 m_id = id;
79 79 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
80 80 }
81 81
82 82
83 83 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
84 84 {
85 85 switch(theme) {
86 86 case QChart::ChartThemeLight:
87 87 return new ChartThemeLight();
88 88 case QChart::ChartThemeBlueCerulean:
89 89 return new ChartThemeBlueCerulean();
90 90 case QChart::ChartThemeDark:
91 91 return new ChartThemeDark();
92 92 case QChart::ChartThemeBrownSand:
93 93 return new ChartThemeBrownSand();
94 94 case QChart::ChartThemeBlueNcs:
95 95 return new ChartThemeBlueNcs();
96 96 case QChart::ChartThemeHighContrast:
97 97 return new ChartThemeHighContrast();
98 98 case QChart::ChartThemeBlueIcy:
99 99 return new ChartThemeBlueIcy();
100 100 default:
101 101 return new ChartThemeSystem();
102 102 }
103 103 }
104 104
105 105 void ChartTheme::decorate(QChart *chart)
106 106 {
107 107 QBrush brush;
108 108
109 109 if(brush == chart->backgroundBrush() || m_force)
110 110 chart->setBackgroundBrush(m_chartBackgroundGradient);
111 111 chart->setTitleFont(m_masterFont);
112 112 chart->setTitleBrush(m_titleBrush);
113 113 chart->setBackgroundDropShadowEnabled(m_backgroundDropShadowEnabled);
114 114 }
115 115
116 116 void ChartTheme::decorate(QLegend *legend)
117 117 {
118 118 QPen pen;
119 119 QBrush brush;
120 120
121 121 if (pen == legend->pen() || m_force){
122 122 legend->setPen(Qt::NoPen);
123 123 }
124 124
125 125 if (brush == legend->brush() || m_force) {
126 126 legend->setBrush(m_chartBackgroundGradient);
127 127 }
128 128 }
129 129
130 130 void ChartTheme::decorate(QAreaSeries *series, int index)
131 131 {
132 132 QPen pen;
133 133 QBrush brush;
134 134
135 135 if (pen == series->pen() || m_force){
136 136 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
137 137 pen.setWidthF(2);
138 138 series->setPen(pen);
139 139 }
140 140
141 141 if (brush == series->brush() || m_force) {
142 142 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
143 143 series->setBrush(brush);
144 144 }
145 145 }
146 146
147 147
148 148 void ChartTheme::decorate(QLineSeries *series,int index)
149 149 {
150 150 QPen pen;
151 151 if(pen == series->pen() || m_force ){
152 152 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
153 153 pen.setWidthF(2);
154 154 series->setPen(pen);
155 155 }
156 156 }
157 157
158 158 void ChartTheme::decorate(QBarSeries *series, int index)
159 159 {
160 160 QBrush brush;
161 161 QPen pen;
162 162 QList<QBarSet *> sets = series->barSets();
163 163
164 164 qreal takeAtPos = 0.5;
165 165 qreal step = 0.2;
166 166 if (sets.count() > 1 ) {
167 167 step = 1.0 / (qreal) sets.count();
168 168 if (sets.count() % m_seriesGradients.count())
169 169 step *= m_seriesGradients.count();
170 170 else
171 171 step *= (m_seriesGradients.count() - 1);
172 172 }
173 173
174 174 for (int i(0); i < sets.count(); i++) {
175 175 int colorIndex = (index + i) % m_seriesGradients.count();
176 176 if (i > 0 && i % m_seriesGradients.count() == 0) {
177 177 // There is no dedicated base color for each sets, generate more colors
178 178 takeAtPos += step;
179 179 if (takeAtPos == 1.0)
180 180 takeAtPos += step;
181 181 takeAtPos -= (int) takeAtPos;
182 182 }
183 183 if (brush == sets.at(i)->brush() || m_force )
184 184 sets.at(i)->setBrush(colorAt(m_seriesGradients.at(colorIndex), takeAtPos));
185 185
186 186 // Pick label color from the opposite end of the gradient.
187 187 // 0.3 as a boundary seems to work well.
188 188 if (takeAtPos < 0.3)
189 189 sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1));
190 190 else
191 191 sets.at(i)->setLabelBrush(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0));
192 192
193 193 if (pen == sets.at(i)->pen() || m_force) {
194 194 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
195 195 sets.at(i)->setPen(c);
196 196 }
197 197 }
198 198 }
199 199
200 200 void ChartTheme::decorate(QScatterSeries *series, int index)
201 201 {
202 202 QPen pen;
203 203 QBrush brush;
204 204
205 205 if (pen == series->pen() || m_force) {
206 206 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0));
207 207 pen.setWidthF(2);
208 208 series->setPen(pen);
209 209 }
210 210
211 211 if (brush == series->brush() || m_force) {
212 212 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
213 213 series->setBrush(brush);
214 214 }
215 215 }
216 216
217 217 void ChartTheme::decorate(QPieSeries *series, int index)
218 218 {
219 219
220 220 for (int i(0); i < series->slices().count(); i++) {
221 221
222 222 QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.0);
223 223
224 224 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
225 225 qreal pos = (qreal) (i + 1) / (qreal) series->count();
226 226 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
227 227
228 228 QPieSlice *s = series->slices().at(i);
229 229 QPieSlicePrivate *d = QPieSlicePrivate::fromSlice(s);
230 230
231 231 if (d->m_data.m_slicePen.isThemed() || m_force)
232 232 d->setPen(penColor, true);
233 233
234 234 if (d->m_data.m_sliceBrush.isThemed() || m_force)
235 235 d->setBrush(brushColor, true);
236 236
237 if (d->m_data.m_labelPen.isThemed() || m_force)
238 d->setLabelPen(QPen(m_titleBrush.color()), true);
237 if (d->m_data.m_labelBrush.isThemed() || m_force)
238 d->setLabelBrush(QBrush(m_titleBrush.color()), true);
239 239
240 240 if (d->m_data.m_labelFont.isThemed() || m_force)
241 241 d->setLabelFont(m_labelFont, true);
242 242 }
243 243 }
244 244
245 245 void ChartTheme::decorate(QSplineSeries *series, int index)
246 246 {
247 247 QPen pen;
248 248 if(pen == series->pen() || m_force){
249 249 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
250 250 pen.setWidthF(2);
251 251 series->setPen(pen);
252 252 }
253 253 }
254 254
255 255 void ChartTheme::decorate(QAxis *axis,bool axisX)
256 256 {
257 257 QPen pen;
258 258 QBrush brush;
259 259 QFont font;
260 260
261 261 if (axis->isAxisVisible()) {
262 262
263 263 if(brush == axis->labelsBrush() || m_force){
264 264 axis->setLabelsBrush(m_axisLabelBrush);
265 265 }
266 266 if(pen == axis->labelsPen() || m_force){
267 267 axis->setLabelsPen(Qt::NoPen); // NoPen for performance reasons
268 268 }
269 269
270 270
271 271 if (axis->shadesVisible() || m_force) {
272 272
273 273 if(brush == axis->shadesBrush() || m_force){
274 274 axis->setShadesBrush(m_backgroundShadesBrush);
275 275 }
276 276
277 277 if(pen == axis->shadesPen() || m_force){
278 278 axis->setShadesPen(m_backgroundShadesPen);
279 279 }
280 280
281 281 if( m_force && (m_backgroundShades == BackgroundShadesBoth
282 282 || (m_backgroundShades == BackgroundShadesVertical && axisX)
283 283 || (m_backgroundShades == BackgroundShadesHorizontal && !axisX))){
284 284 axis->setShadesVisible(true);
285 285
286 286 }
287 287 }
288 288
289 289 if(pen == axis->axisPen() || m_force){
290 290 axis->setAxisPen(m_axisLinePen);
291 291 }
292 292
293 293 if(pen == axis->gridLinePen() || m_force){
294 294 axis->setGridLinePen(m_gridLinePen);
295 295 }
296 296
297 297 if(font == axis->labelsFont() || m_force){
298 298 axis->setLabelsFont(m_labelFont);
299 299 }
300 300 }
301 301 }
302 302
303 303 void ChartTheme::generateSeriesGradients()
304 304 {
305 305 // Generate gradients in HSV color space
306 306 foreach (const QColor& color, m_seriesColors) {
307 307 QLinearGradient g;
308 308 qreal h = color.hsvHueF();
309 309 qreal s = color.hsvSaturationF();
310 310
311 311 // TODO: tune the algorithm to give nice results with most base colors defined in
312 312 // most themes. The rest of the gradients we can define manually in theme specific
313 313 // implementation.
314 314 QColor start = color;
315 315 start.setHsvF(h, 0.0, 1.0);
316 316 g.setColorAt(0.0, start);
317 317
318 318 g.setColorAt(0.5, color);
319 319
320 320 QColor end = color;
321 321 end.setHsvF(h, s, 0.25);
322 322 g.setColorAt(1.0, end);
323 323
324 324 m_seriesGradients << g;
325 325 }
326 326 }
327 327
328 328
329 329 QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos)
330 330 {
331 331 Q_ASSERT(pos >= 0.0 && pos <= 1.0);
332 332 qreal r = start.redF() + ((end.redF() - start.redF()) * pos);
333 333 qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos);
334 334 qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos);
335 335 QColor c;
336 336 c.setRgbF(r, g, b);
337 337 return c;
338 338 }
339 339
340 340 QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos)
341 341 {
342 342 Q_ASSERT(pos >= 0 && pos <= 1.0);
343 343
344 344 QGradientStops stops = gradient.stops();
345 345 int count = stops.count();
346 346
347 347 // find previous stop relative to position
348 348 QGradientStop prev = stops.first();
349 349 for (int i = 0; i < count; i++) {
350 350 QGradientStop stop = stops.at(i);
351 351 if (pos > stop.first)
352 352 prev = stop;
353 353
354 354 // given position is actually a stop position?
355 355 if (pos == stop.first) {
356 356 //qDebug() << "stop color" << pos;
357 357 return stop.second;
358 358 }
359 359 }
360 360
361 361 // find next stop relative to position
362 362 QGradientStop next = stops.last();
363 363 for (int i = count - 1; i >= 0; i--) {
364 364 QGradientStop stop = stops.at(i);
365 365 if (pos < stop.first)
366 366 next = stop;
367 367 }
368 368
369 369 //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first;
370 370
371 371 qreal range = next.first - prev.first;
372 372 qreal posDelta = pos - prev.first;
373 373 qreal relativePos = posDelta / range;
374 374
375 375 //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos;
376 376
377 377 return colorAt(prev.second, next.second, relativePos);
378 378 }
379 379
380 380 void ChartTheme::setForced(bool enabled)
381 381 {
382 382 m_force=enabled;
383 383 }
384 384
385 385 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,208 +1,208
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "piechartitem_p.h"
22 22 #include "piesliceitem_p.h"
23 23 #include "qpieslice.h"
24 24 #include "qpieslice_p.h"
25 25 #include "qpieseries.h"
26 26 #include "qpieseries_p.h"
27 27 #include "chartpresenter_p.h"
28 28 #include "chartdataset_p.h"
29 29 #include "chartanimator_p.h"
30 30 #include <QPainter>
31 31 #include <QTimer>
32 32
33 33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 34
35 35 PieChartItem::PieChartItem(QPieSeries *series, ChartPresenter* presenter)
36 36 :ChartItem(presenter),
37 37 m_series(series)
38 38 {
39 39 Q_ASSERT(series);
40 40
41 41 connect(series, SIGNAL(added(QList<QPieSlice*>)), this, SLOT(handleSlicesAdded(QList<QPieSlice*>)));
42 42 connect(series, SIGNAL(removed(QList<QPieSlice*>)), this, SLOT(handleSlicesRemoved(QList<QPieSlice*>)));
43 43 connect(series, SIGNAL(horizontalPositionChanged()), this, SLOT(updateLayout()));
44 44 connect(series, SIGNAL(verticalPositionChanged()), this, SLOT(updateLayout()));
45 45 connect(series, SIGNAL(pieSizeChanged()), this, SLOT(updateLayout()));
46 46 connect(QPieSeriesPrivate::fromSeries(series), SIGNAL(calculatedDataChanged()), this, SLOT(updateLayout()));
47 47
48 48 // Note: the following does not affect as long as the item does not have anything to paint
49 49 setZValue(ChartPresenter::PieSeriesZValue);
50 50
51 51 // Note: will not create slice items until we have a proper rectangle to draw on.
52 52 }
53 53
54 54 PieChartItem::~PieChartItem()
55 55 {
56 56 // slices deleted automatically through QGraphicsItem
57 57 }
58 58
59 59 void PieChartItem::handleGeometryChanged(const QRectF& rect)
60 60 {
61 61 prepareGeometryChange();
62 62 m_rect = rect;
63 63 updateLayout();
64 64
65 65 // This is for delayed initialization of the slice items during startup.
66 66 // It ensures that startup animation originates from the correct position.
67 67 if (m_sliceItems.isEmpty())
68 68 handleSlicesAdded(m_series->slices());
69 69 }
70 70
71 71 void PieChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
72 72 {
73 73 Q_UNUSED(minX);
74 74 Q_UNUSED(maxX);
75 75 Q_UNUSED(minY);
76 76 Q_UNUSED(maxY);
77 77 // does not apply to pie
78 78 }
79 79
80 80 void PieChartItem::rangeXChanged(qreal min, qreal max, int tickXCount)
81 81 {
82 82 Q_UNUSED(min);
83 83 Q_UNUSED(max);
84 84 Q_UNUSED(tickXCount);
85 85 // does not apply to pie
86 86 }
87 87
88 88 void PieChartItem::rangeYChanged(qreal min, qreal max, int tickYCount)
89 89 {
90 90 Q_UNUSED(min);
91 91 Q_UNUSED(max);
92 92 Q_UNUSED(tickYCount);
93 93 // does not apply to pie
94 94 }
95 95
96 96 void PieChartItem::updateLayout()
97 97 {
98 98 // find pie center coordinates
99 99 m_pieCenter.setX(m_rect.left() + (m_rect.width() * m_series->horizontalPosition()));
100 100 m_pieCenter.setY(m_rect.top() + (m_rect.height() * m_series->verticalPosition()));
101 101
102 102 // find maximum radius for pie
103 103 m_pieRadius = m_rect.height() / 2;
104 104 if (m_rect.width() < m_rect.height())
105 105 m_pieRadius = m_rect.width() / 2;
106 106
107 107 // apply size factor
108 108 m_pieRadius *= m_series->pieSize();
109 109
110 110 // set layouts for existing slice items
111 111 foreach (QPieSlice* slice, m_series->slices()) {
112 112 PieSliceItem *sliceItem = m_sliceItems.value(slice);
113 113 if (sliceItem) {
114 114 PieSliceData sliceData = updateSliceGeometry(slice);
115 115 if (animator())
116 116 animator()->updateAnimation(this, sliceItem, sliceData);
117 117 else
118 118 sliceItem->setLayout(sliceData);
119 119 }
120 120 }
121 121
122 122 update();
123 123 }
124 124
125 125 void PieChartItem::handleSlicesAdded(QList<QPieSlice*> slices)
126 126 {
127 127 // delay creating slice items until there is a proper rectangle
128 128 if (!m_rect.isValid() && m_sliceItems.isEmpty())
129 129 return;
130 130
131 131 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
132 132
133 133 bool startupAnimation = m_sliceItems.isEmpty();
134 134
135 135 foreach (QPieSlice *slice, slices) {
136 136 PieSliceItem* sliceItem = new PieSliceItem(this);
137 137 m_sliceItems.insert(slice, sliceItem);
138 138
139 139 // Note: do need to connect to slice valueChanged() etc.
140 140 // This is handled through calculatedDataChanged signal.
141 141 connect(slice, SIGNAL(labelChanged()), this, SLOT(handleSliceChanged()));
142 142 connect(slice, SIGNAL(labelVisibleChanged()), this, SLOT(handleSliceChanged()));
143 143 connect(slice, SIGNAL(explodedChanged()), this, SLOT(handleSliceChanged()));
144 144 connect(slice, SIGNAL(penChanged()), this, SLOT(handleSliceChanged()));
145 145 connect(slice, SIGNAL(brushChanged()), this, SLOT(handleSliceChanged()));
146 connect(slice, SIGNAL(labelPenChanged()), this, SLOT(handleSliceChanged()));
146 connect(slice, SIGNAL(labelBrushChanged()), this, SLOT(handleSliceChanged()));
147 147 connect(slice, SIGNAL(labelFontChanged()), this, SLOT(handleSliceChanged()));
148 148 connect(slice, SIGNAL(labelArmLengthFactorChanged()), this, SLOT(handleSliceChanged()));
149 149 connect(slice, SIGNAL(explodeDistanceFactorChanged()), this, SLOT(handleSliceChanged()));
150 150
151 151 connect(sliceItem, SIGNAL(clicked(Qt::MouseButtons)), slice, SIGNAL(clicked()));
152 152 connect(sliceItem, SIGNAL(hovered(bool)), slice, SIGNAL(hovered(bool)));
153 153
154 154 PieSliceData sliceData = updateSliceGeometry(slice);
155 155 if (animator())
156 156 animator()->addAnimation(this, sliceItem, sliceData, startupAnimation);
157 157 else
158 158 sliceItem->setLayout(sliceData);
159 159 }
160 160 }
161 161
162 162 void PieChartItem::handleSlicesRemoved(QList<QPieSlice*> slices)
163 163 {
164 164 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
165 165
166 166 foreach (QPieSlice *slice, slices) {
167 167
168 168 PieSliceItem *sliceItem = m_sliceItems.value(slice);
169 169
170 170 // this can happen if you call append() & remove() in a row so that PieSliceItem is not even created
171 171 if (!sliceItem)
172 172 continue;
173 173
174 174 m_sliceItems.remove(slice);
175 175
176 176 if (animator())
177 177 animator()->removeAnimation(this, sliceItem); // animator deletes the PieSliceItem
178 178 else
179 179 delete sliceItem;
180 180 }
181 181 }
182 182
183 183 void PieChartItem::handleSliceChanged()
184 184 {
185 185 QPieSlice* slice = qobject_cast<QPieSlice *>(sender());
186 186 Q_ASSERT(m_sliceItems.contains(slice));
187 187
188 188 PieSliceItem *sliceItem = m_sliceItems.value(slice);
189 189 PieSliceData sliceData = updateSliceGeometry(slice);
190 190 if (animator())
191 191 animator()->updateAnimation(this, sliceItem, sliceData);
192 192 else
193 193 sliceItem->setLayout(sliceData);
194 194
195 195 update();
196 196 }
197 197
198 198 PieSliceData PieChartItem::updateSliceGeometry(QPieSlice *slice)
199 199 {
200 200 PieSliceData &sliceData = QPieSlicePrivate::fromSlice(slice)->m_data;
201 201 sliceData.m_center = PieSliceItem::sliceCenter(m_pieCenter, m_pieRadius, slice);
202 202 sliceData.m_radius = m_pieRadius;
203 203 return sliceData;
204 204 }
205 205
206 206 #include "moc_piechartitem_p.cpp"
207 207
208 208 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,130 +1,130
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef PIESLICEDATA_P_H
22 22 #define PIESLICEDATA_P_H
23 23
24 24 #include "qchartglobal.h"
25 25 #include "qpieslice.h"
26 26 #include <QPen>
27 27 #include <QBrush>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 template <class T>
32 32 class Themed : public T
33 33 {
34 34 public:
35 35 Themed():m_isThemed(true) {}
36 36
37 37 inline T &operator=(const T &other) { return T::operator =(other); }
38 38
39 39 inline bool operator!=(const T &other) const { return T::operator !=(other); }
40 40 inline bool operator!=(const Themed &other) const
41 41 {
42 42 if (T::operator !=(other))
43 43 return true;
44 44
45 45 if (m_isThemed != other.m_isThemed)
46 46 return true;
47 47
48 48 return false;
49 49 }
50 50
51 51 inline void setThemed(bool state) { m_isThemed = state; }
52 52 inline bool isThemed() const { return m_isThemed; }
53 53
54 54 private:
55 55 bool m_isThemed;
56 56 };
57 57
58 58 class PieSliceData
59 59 {
60 60 public:
61 61 PieSliceData()
62 62 {
63 63 m_value = 0;
64 64
65 65 m_isExploded = false;
66 66 m_explodeDistanceFactor = 0.15;
67 67
68 68 m_isLabelVisible = false;
69 69 m_labelArmLengthFactor = 0.15;
70 70
71 71 m_percentage = 0;
72 72 m_radius = 0;
73 73 m_startAngle = 0;
74 74 m_angleSpan = 0;
75 75 }
76 76
77 77 bool operator!=(const PieSliceData &other) const
78 78 {
79 79 if (!qFuzzyIsNull(m_value - other.m_value))
80 80 return true;
81 81
82 82 if (m_slicePen != other.m_slicePen ||
83 83 m_sliceBrush != other.m_sliceBrush)
84 84 return true;
85 85
86 86 if (m_isExploded != other.m_isExploded ||
87 87 !qFuzzyIsNull(m_explodeDistanceFactor - other.m_explodeDistanceFactor))
88 88 return true;
89 89
90 90 if (m_isLabelVisible != other.m_isLabelVisible ||
91 91 m_labelText != other.m_labelText ||
92 92 m_labelFont != other.m_labelFont ||
93 93 !qFuzzyIsNull(m_labelArmLengthFactor - other.m_labelArmLengthFactor) ||
94 m_labelPen != other.m_labelPen)
94 m_labelBrush != other.m_labelBrush)
95 95 return true;
96 96
97 97 if (!qFuzzyIsNull(m_percentage - other.m_percentage) ||
98 98 m_center != other.m_center ||
99 99 !qFuzzyIsNull(m_radius - other.m_radius) ||
100 100 !qFuzzyIsNull(m_startAngle - other.m_startAngle) ||
101 101 !qFuzzyIsNull(m_angleSpan - other.m_angleSpan))
102 102 return true;
103 103
104 104 return false;
105 105 }
106 106
107 107 qreal m_value;
108 108
109 109 Themed<QPen> m_slicePen;
110 110 Themed<QBrush> m_sliceBrush;
111 111
112 112 bool m_isExploded;
113 113 qreal m_explodeDistanceFactor;
114 114
115 115 bool m_isLabelVisible;
116 116 QString m_labelText;
117 117 Themed<QFont> m_labelFont;
118 118 qreal m_labelArmLengthFactor;
119 Themed<QPen> m_labelPen;
119 Themed<QBrush> m_labelBrush;
120 120
121 121 qreal m_percentage;
122 122 QPointF m_center;
123 123 qreal m_radius;
124 124 qreal m_startAngle;
125 125 qreal m_angleSpan;
126 126 };
127 127
128 128 QTCOMMERCIALCHART_END_NAMESPACE
129 129
130 130 #endif // PIESLICEDATA_P_H
@@ -1,223 +1,224
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "piesliceitem_p.h"
22 22 #include "piechartitem_p.h"
23 23 #include "qpieseries.h"
24 24 #include "qpieslice.h"
25 25 #include "chartpresenter_p.h"
26 26 #include <QPainter>
27 27 #include <qmath.h>
28 28 #include <QGraphicsSceneEvent>
29 29 #include <QTime>
30 30 #include <QDebug>
31 31
32 32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 33
34 34 #define PI 3.14159265 // TODO: is this defined in some header?
35 35
36 36 QPointF offset(qreal angle, qreal length)
37 37 {
38 38 qreal dx = qSin(angle*(PI/180)) * length;
39 39 qreal dy = qCos(angle*(PI/180)) * length;
40 40 return QPointF(dx, -dy);
41 41 }
42 42
43 43 PieSliceItem::PieSliceItem(QGraphicsItem* parent)
44 44 :QGraphicsObject(parent),
45 45 m_hovered(false)
46 46 {
47 47 setAcceptHoverEvents(true);
48 48 setAcceptedMouseButtons(Qt::MouseButtonMask);
49 49 setZValue(ChartPresenter::PieSeriesZValue);
50 50 }
51 51
52 52 PieSliceItem::~PieSliceItem()
53 53 {
54 54 // If user is hovering over the slice and it gets destroyed we do
55 55 // not get a hover leave event. So we must emit the signal here.
56 56 if (m_hovered)
57 57 emit hovered(false);
58 58 }
59 59
60 60 QRectF PieSliceItem::boundingRect() const
61 61 {
62 62 return m_boundingRect;
63 63 }
64 64
65 65 QPainterPath PieSliceItem::shape() const
66 66 {
67 67 // Don't include the label and label arm.
68 68 // This is used to detect a mouse clicks. We do not want clicks from label.
69 69 return m_slicePath;
70 70 }
71 71
72 72 void PieSliceItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* /*option*/, QWidget* /*widget*/)
73 73 {
74 74 painter->save();
75 75 painter->setClipRect(parentItem()->boundingRect());
76 76 painter->setPen(m_data.m_slicePen);
77 77 painter->setBrush(m_data.m_sliceBrush);
78 78 painter->drawPath(m_slicePath);
79 79 painter->restore();
80 80
81 81 if (m_data.m_isLabelVisible) {
82 82 painter->save();
83 83 painter->setClipRect(parentItem()->boundingRect());
84 painter->setPen(m_data.m_labelPen);
84 // Pen for label arm not defined in the QPieSeries api, let's use brush's color instead
85 // Also, the drawText actually uses the pen color for the text color (unlike QGraphicsSimpleTextItem)
86 painter->setPen(m_data.m_labelBrush.color());
85 87 painter->drawPath(m_labelArmPath);
86 // the pen color will affect the font color as well
87 88 painter->setFont(m_data.m_labelFont);
88 89 painter->drawText(m_labelTextRect.bottomLeft(), m_data.m_labelText);
89 90 painter->restore();
90 91 }
91 92 }
92 93
93 94 void PieSliceItem::hoverEnterEvent(QGraphicsSceneHoverEvent* /*event*/)
94 95 {
95 96 m_hovered = true;
96 97 emit hovered(true);
97 98 }
98 99
99 100 void PieSliceItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* /*event*/)
100 101 {
101 102 m_hovered = false;
102 103 emit hovered(false);
103 104 }
104 105
105 106 void PieSliceItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
106 107 {
107 108 emit clicked(event->buttons());
108 109 }
109 110
110 111 void PieSliceItem::setLayout(const PieSliceData &sliceData)
111 112 {
112 113 m_data = sliceData;
113 114 updateGeometry();
114 115 update();
115 116 }
116 117
117 118 void PieSliceItem::updateGeometry()
118 119 {
119 120 if (m_data.m_radius <= 0)
120 121 return;
121 122
122 123 prepareGeometryChange();
123 124
124 125 // update slice path
125 126 qreal centerAngle;
126 127 QPointF armStart;
127 128 m_slicePath = slicePath(m_data.m_center, m_data.m_radius, m_data.m_startAngle, m_data.m_angleSpan, &centerAngle, &armStart);
128 129
129 130 // update text rect
130 131 m_labelTextRect = labelTextRect(m_data.m_labelFont, m_data.m_labelText);
131 132
132 133 // update label arm path
133 134 QPointF labelTextStart;
134 135 m_labelArmPath = labelArmPath(armStart, centerAngle, m_data.m_radius * m_data.m_labelArmLengthFactor, m_labelTextRect.width(), &labelTextStart);
135 136
136 137 // update text position
137 138 m_labelTextRect.moveBottomLeft(labelTextStart);
138 139
139 140 // update bounding rect
140 141 if (m_data.m_isLabelVisible)
141 142 m_boundingRect = m_slicePath.boundingRect().united(m_labelArmPath.boundingRect()).united(m_labelTextRect);
142 143 else
143 144 m_boundingRect = m_slicePath.boundingRect();
144 145 }
145 146
146 147 QPointF PieSliceItem::sliceCenter(QPointF point, qreal radius, QPieSlice *slice)
147 148 {
148 149 if (slice->isExploded()) {
149 150 qreal centerAngle = slice->startAngle() + (slice->angleSpan()/2);
150 151 qreal len = radius * slice->explodeDistanceFactor();
151 152 qreal dx = qSin(centerAngle*(PI/180)) * len;
152 153 qreal dy = -qCos(centerAngle*(PI/180)) * len;
153 154 point += QPointF(dx, dy);
154 155 }
155 156 return point;
156 157 }
157 158
158 159 QPainterPath PieSliceItem::slicePath(QPointF center, qreal radius, qreal startAngle, qreal angleSpan, qreal *centerAngle, QPointF* armStart)
159 160 {
160 161 // calculate center angle
161 162 *centerAngle = startAngle + (angleSpan/2);
162 163
163 164 // calculate slice rectangle
164 165 QRectF rect(center.x()-radius, center.y()-radius, radius*2, radius*2);
165 166
166 167 // slice path
167 168 // TODO: draw the shape so that it might have a hole in the center
168 169 QPainterPath path;
169 170 path.moveTo(rect.center());
170 171 path.arcTo(rect, -startAngle + 90, -angleSpan);
171 172 path.closeSubpath();
172 173
173 174 // calculate label arm start point
174 175 *armStart = center;
175 176 *armStart += offset(*centerAngle, radius + PIESLICE_LABEL_GAP);
176 177
177 178 return path;
178 179 }
179 180
180 181 QPainterPath PieSliceItem::labelArmPath(QPointF start, qreal angle, qreal length, qreal textWidth, QPointF *textStart)
181 182 {
182 183 // prevent label arm pointing straight down because it will look bad
183 184 if (angle < 180 && angle > 170)
184 185 angle = 170;
185 186 if (angle > 180 && angle < 190)
186 187 angle = 190;
187 188
188 189 // line from slice to label
189 190 qreal dx = qSin(angle*(PI/180)) * length;
190 191 qreal dy = -qCos(angle*(PI/180)) * length;
191 192 QPointF parm1 = start + QPointF(dx, dy);
192 193
193 194 // line to underline the label
194 195 QPointF parm2 = parm1;
195 196 if (angle < 180) { // arm swings the other way on the left side
196 197 parm2 += QPointF(textWidth, 0);
197 198 *textStart = parm1;
198 199 }
199 200 else {
200 201 parm2 += QPointF(-textWidth,0);
201 202 *textStart = parm2;
202 203 }
203 204
204 205 // elevate the text position a bit so that it does not hit the line
205 206 *textStart += QPointF(0, -3);
206 207
207 208 QPainterPath path;
208 209 path.moveTo(start);
209 210 path.lineTo(parm1);
210 211 path.lineTo(parm2);
211 212
212 213 return path;
213 214 }
214 215
215 216 QRectF PieSliceItem::labelTextRect(QFont font, QString text)
216 217 {
217 218 QFontMetricsF fm(font);
218 219 return fm.boundingRect(text);
219 220 }
220 221
221 222 #include "moc_piesliceitem_p.cpp"
222 223
223 224 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,536 +1,536
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qpieslice.h"
22 22 #include "qpieslice_p.h"
23 23
24 24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 25
26 26 /*!
27 27 \class QPieSlice
28 28 \brief Defines a slice in pie series.
29 29
30 30 This object defines the properties of a single slice in a QPieSeries.
31 31
32 32 In addition to the obvious value and label properties the user can also control
33 33 the visual appearance of a slice. By modifying the visual appearance also means that
34 34 the user is overriding the default appearance set by the theme.
35 35
36 36 Note that if the user has customized slices and theme is changed all customizations will be lost.
37 37
38 38 To enable user interaction with the pie some basic signals are provided about clicking and hovering.
39 39 */
40 40
41 41 /*!
42 42 \property QPieSlice::label
43 43
44 44 Label of the slice.
45 45
46 \sa labelVisible, labelPen, labelFont, labelArmLengthFactor
46 \sa labelVisible, labelBrush, labelFont, labelArmLengthFactor
47 47 */
48 48
49 49 /*!
50 50 \fn void QPieSlice::labelChanged()
51 51
52 52 This signal emitted when the slice label has been changed.
53 53
54 54 \sa label
55 55 */
56 56
57 57 /*!
58 58 \property QPieSlice::value
59 59
60 60 Value of the slice.
61 61
62 62 Note that if users sets a negative value it is converted to a positive value.
63 63
64 64 \sa percentage(), QPieSeries::sum()
65 65 */
66 66
67 67 /*!
68 68 \fn void QPieSlice::valueChanged()
69 69
70 70 This signal is emitted when the slice value changes.
71 71
72 72 \sa value
73 73 */
74 74
75 75 /*!
76 76 \property QPieSlice::labelVisible
77 77
78 78 Defienes the visibility of the slice label.
79 79
80 80 Default is not visible.
81 81
82 \sa label, labelPen, labelFont, labelArmLengthFactor
82 \sa label, labelBrush, labelFont, labelArmLengthFactor
83 83 */
84 84
85 85 /*!
86 86 \fn void QPieSlice::labelVisibleChanged()
87 87
88 88 This signal emitted when visibility of the slice label has changed.
89 89
90 90 \sa labelVisible
91 91 */
92 92
93 93 /*!
94 94 \property QPieSlice::exploded
95 95
96 96 Defines if the slice is exploded from the pie.
97 97
98 98 \sa explodeDistanceFactor
99 99 */
100 100
101 101 /*!
102 102 \fn void QPieSlice::explodedChanged()
103 103
104 104 This signal is emitted the the slice has been exploded from the pie or is returned back to the pie.
105 105
106 106 \sa exploded
107 107 */
108 108
109 109 /*!
110 110 \property QPieSlice::pen
111 111
112 112 Pen used to draw the slice border.
113 113 */
114 114
115 115 /*!
116 116 \fn void QPieSlice::penChanged()
117 117
118 118 This signal is emitted when the pen of the slice has changed.
119 119
120 120 \sa pen
121 121 */
122 122
123 123 /*!
124 124 \property QPieSlice::brush
125 125
126 126 Brush used to draw the slice.
127 127 */
128 128
129 129 /*!
130 130 \fn void QPieSlice::brushChanged()
131 131
132 132 This signal is emitted when the brush of the slice has changed.
133 133
134 134 \sa brush
135 135 */
136 136
137 137 /*!
138 \property QPieSlice::labelPen
138 \property QPieSlice::labelBrush
139 139
140 140 Pen used to draw label and label arm of the slice.
141 141
142 142 \sa label, labelVisible, labelFont, labelArmLengthFactor
143 143 */
144 144
145 145 /*!
146 \fn void QPieSlice::labelPenChanged()
146 \fn void QPieSlice::labelBrushChanged()
147 147
148 148 This signal is emitted when the label pen of the slice has changed.
149 149
150 \sa labelPen
150 \sa labelBrush
151 151 */
152 152
153 153 /*!
154 154 \property QPieSlice::labelFont
155 155
156 156 Font used for drawing label text.
157 157
158 158 \sa label, labelVisible, labelArmLengthFactor
159 159 */
160 160
161 161 /*!
162 162 \fn void QPieSlice::labelFontChanged()
163 163
164 164 This signal is emitted when the label font of the slice has changed.
165 165
166 166 \sa labelFont
167 167 */
168 168
169 169 /*!
170 170 \property QPieSlice::labelArmLengthFactor
171 171
172 172 Defines the length of the label arm.
173 173
174 174 The factor is relative to pie radius. For example:
175 175 1.0 means the length is the same as the radius.
176 176 0.5 means the length is half of the radius.
177 177
178 178 Default value is 0.15
179 179
180 \sa label, labelVisible, labelPen, labelFont
180 \sa label, labelVisible, labelBrush, labelFont
181 181 */
182 182
183 183 /*!
184 184 \fn void QPieSlice::labelArmLengthFactorChanged()
185 185
186 186 This signal is emitted when the label arm factor of the slice has changed.
187 187
188 188 \sa labelArmLengthFactor
189 189 */
190 190
191 191 /*!
192 192 \property QPieSlice::explodeDistanceFactor
193 193
194 194 When the slice is exploded this factor defines how far the slice is exploded away from the pie.
195 195
196 196 The factor is relative to pie radius. For example:
197 197 1.0 means the distance is the same as the radius.
198 198 0.5 means the distance is half of the radius.
199 199
200 200 Default value is 0.15
201 201
202 202 \sa exploded
203 203 */
204 204
205 205 /*!
206 206 \fn void QPieSlice::explodeDistanceFactorChanged()
207 207
208 208 This signal is emitted when the explode distance factor of the slice has changed.
209 209
210 210 \sa explodeDistanceFactor
211 211 */
212 212
213 213 /*!
214 214 \property QPieSlice::percentage
215 215
216 216 Percentage of the slice compared to the sum of all slices in the series.
217 217
218 218 The actual value ranges from 0.0 to 1.0.
219 219
220 220 Updated automatically once the slice is added to the series.
221 221
222 222 \sa value, QPieSeries::sum
223 223 */
224 224
225 225 /*!
226 226 \fn void QPieSlice::percentageChanged()
227 227
228 228 This signal is emitted when the percentage of the slice has changed.
229 229
230 230 \sa percentage
231 231 */
232 232
233 233 /*!
234 234 \property QPieSlice::startAngle
235 235
236 236 Defines the starting angle of this slice in the series it belongs to.
237 237
238 238 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
239 239
240 240 Updated automatically once the slice is added to the series.
241 241 */
242 242
243 243 /*!
244 244 \fn void QPieSlice::startAngleChanged()
245 245
246 246 This signal is emitted when the starting angle f the slice has changed.
247 247
248 248 \sa startAngle
249 249 */
250 250
251 251 /*!
252 252 \property QPieSlice::angleSpan
253 253
254 254 Span of the slice in degrees.
255 255
256 256 Full pie is 360 degrees where 0 degrees is at 12 a'clock.
257 257
258 258 Updated automatically once the slice is added to the series.
259 259 */
260 260
261 261 /*!
262 262 \fn void QPieSlice::angleSpanChanged()
263 263
264 264 This signal is emitted when the angle span of the slice has changed.
265 265
266 266 \sa angleSpan
267 267 */
268 268
269 269
270 270 /*!
271 271 \fn void QPieSlice::clicked()
272 272
273 273 This signal is emitted when user has clicked the slice.
274 274
275 275 \sa QPieSeries::clicked()
276 276 */
277 277
278 278 /*!
279 279 \fn void QPieSlice::hovered(bool state)
280 280
281 281 This signal is emitted when user has hovered over or away from the slice.
282 282
283 283 \a state is true when user has hovered over the slice and false when hover has moved away from the slice.
284 284
285 285 \sa QPieSeries::hovered()
286 286 */
287 287
288 288 /*!
289 289 Constructs an empty slice with a \a parent.
290 290
291 291 \sa QPieSeries::append(), QPieSeries::insert()
292 292 */
293 293 QPieSlice::QPieSlice(QObject *parent)
294 294 :QObject(parent),
295 295 d_ptr(new QPieSlicePrivate(this))
296 296 {
297 297
298 298 }
299 299
300 300 /*!
301 301 Constructs an empty slice with given \a value, \a label and a \a parent.
302 302 \sa QPieSeries::append(), QPieSeries::insert()
303 303 */
304 304 QPieSlice::QPieSlice(QString label, qreal value, QObject *parent)
305 305 :QObject(parent),
306 306 d_ptr(new QPieSlicePrivate(this))
307 307 {
308 308 setValue(value);
309 309 setLabel(label);
310 310 }
311 311
312 312 /*!
313 313 Destroys the slice.
314 314 User should not delete the slice if it has been added to the series.
315 315 */
316 316 QPieSlice::~QPieSlice()
317 317 {
318 318
319 319 }
320 320
321 321 void QPieSlice::setLabel(QString label)
322 322 {
323 323 if (d_ptr->m_data.m_labelText != label) {
324 324 d_ptr->m_data.m_labelText = label;
325 325 emit labelChanged();
326 326 }
327 327 }
328 328
329 329 QString QPieSlice::label() const
330 330 {
331 331 return d_ptr->m_data.m_labelText;
332 332 }
333 333
334 334 void QPieSlice::setValue(qreal value)
335 335 {
336 336 value = qAbs(value); // negative values not allowed
337 337 if (!qFuzzyIsNull(d_ptr->m_data.m_value - value)) {
338 338 d_ptr->m_data.m_value = value;
339 339 emit valueChanged();
340 340 }
341 341 }
342 342
343 343 qreal QPieSlice::value() const
344 344 {
345 345 return d_ptr->m_data.m_value;
346 346 }
347 347
348 348 void QPieSlice::setLabelVisible(bool visible)
349 349 {
350 350 if (d_ptr->m_data.m_isLabelVisible != visible) {
351 351 d_ptr->m_data.m_isLabelVisible = visible;
352 352 emit labelVisibleChanged();
353 353 }
354 354 }
355 355
356 356 bool QPieSlice::isLabelVisible() const
357 357 {
358 358 return d_ptr->m_data.m_isLabelVisible;
359 359 }
360 360
361 361 void QPieSlice::setExploded(bool exploded)
362 362 {
363 363 if (d_ptr->m_data.m_isExploded != exploded) {
364 364 d_ptr->m_data.m_isExploded = exploded;
365 365 emit explodedChanged();
366 366 }
367 367 }
368 368
369 369 bool QPieSlice::isExploded() const
370 370 {
371 371 return d_ptr->m_data.m_isExploded;
372 372 }
373 373
374 374 void QPieSlice::setPen(const QPen &pen)
375 375 {
376 376 d_ptr->setPen(pen, false);
377 377 }
378 378
379 379 QPen QPieSlice::pen() const
380 380 {
381 381 return d_ptr->m_data.m_slicePen;
382 382 }
383 383
384 384 void QPieSlice::setBrush(const QBrush &brush)
385 385 {
386 386 d_ptr->setBrush(brush, false);
387 387 }
388 388
389 389 QBrush QPieSlice::brush() const
390 390 {
391 391 return d_ptr->m_data.m_sliceBrush;
392 392 }
393 393
394 void QPieSlice::setLabelPen(const QPen &pen)
394 void QPieSlice::setLabelBrush(const QBrush &brush)
395 395 {
396 d_ptr->setLabelPen(pen, false);
396 d_ptr->setLabelBrush(brush, false);
397 397 }
398 398
399 QPen QPieSlice::labelPen() const
399 QBrush QPieSlice::labelBrush() const
400 400 {
401 return d_ptr->m_data.m_labelPen;
401 return d_ptr->m_data.m_labelBrush;
402 402 }
403 403
404 404 void QPieSlice::setLabelFont(const QFont &font)
405 405 {
406 406 d_ptr->setLabelFont(font, false);
407 407 }
408 408
409 409 QFont QPieSlice::labelFont() const
410 410 {
411 411 return d_ptr->m_data.m_labelFont;
412 412 }
413 413
414 414 void QPieSlice::setLabelArmLengthFactor(qreal factor)
415 415 {
416 416 if (!qFuzzyIsNull(d_ptr->m_data.m_labelArmLengthFactor - factor)) {
417 417 d_ptr->m_data.m_labelArmLengthFactor = factor;
418 418 emit labelArmLengthFactorChanged();
419 419 }
420 420 }
421 421
422 422 qreal QPieSlice::labelArmLengthFactor() const
423 423 {
424 424 return d_ptr->m_data.m_labelArmLengthFactor;
425 425 }
426 426
427 427 void QPieSlice::setExplodeDistanceFactor(qreal factor)
428 428 {
429 429 if (!qFuzzyIsNull(d_ptr->m_data.m_explodeDistanceFactor - factor)) {
430 430 d_ptr->m_data.m_explodeDistanceFactor = factor;
431 431 emit explodeDistanceFactorChanged();
432 432 }
433 433 }
434 434
435 435 qreal QPieSlice::explodeDistanceFactor() const
436 436 {
437 437 return d_ptr->m_data.m_explodeDistanceFactor;
438 438 }
439 439
440 440 qreal QPieSlice::percentage() const
441 441 {
442 442 return d_ptr->m_data.m_percentage;
443 443 }
444 444
445 445 qreal QPieSlice::startAngle() const
446 446 {
447 447 return d_ptr->m_data.m_startAngle;
448 448 }
449 449
450 450 qreal QPieSlice::angleSpan() const
451 451 {
452 452 return d_ptr->m_data.m_angleSpan;
453 453 }
454 454
455 455 QPieSlicePrivate::QPieSlicePrivate(QPieSlice *parent)
456 456 :QObject(parent),
457 457 q_ptr(parent)
458 458 {
459 459
460 460 }
461 461
462 462 QPieSlicePrivate::~QPieSlicePrivate()
463 463 {
464 464
465 465 }
466 466
467 467 QPieSlicePrivate *QPieSlicePrivate::fromSlice(QPieSlice *slice)
468 468 {
469 469 return slice->d_func();
470 470 }
471 471
472 472 void QPieSlicePrivate::setPen(const QPen &pen, bool themed)
473 473 {
474 474 if (m_data.m_slicePen != pen) {
475 475 m_data.m_slicePen = pen;
476 476 m_data.m_slicePen.setThemed(themed);
477 477 emit q_ptr->penChanged();
478 478 }
479 479 }
480 480
481 481 void QPieSlicePrivate::setBrush(const QBrush &brush, bool themed)
482 482 {
483 483 if (m_data.m_sliceBrush != brush) {
484 484 m_data.m_sliceBrush = brush;
485 485 m_data.m_sliceBrush.setThemed(themed);
486 486 emit q_ptr->brushChanged();
487 487 }
488 488 }
489 489
490 void QPieSlicePrivate::setLabelPen(const QPen &pen, bool themed)
490 void QPieSlicePrivate::setLabelBrush(const QBrush &brush, bool themed)
491 491 {
492 if (m_data.m_labelPen != pen) {
493 m_data.m_labelPen = pen;
494 m_data.m_labelPen.setThemed(themed);
495 emit q_ptr->labelPenChanged();
492 if (m_data.m_labelBrush != brush) {
493 m_data.m_labelBrush = brush;
494 m_data.m_labelBrush.setThemed(themed);
495 emit q_ptr->labelBrushChanged();
496 496 }
497 497 }
498 498
499 499 void QPieSlicePrivate::setLabelFont(const QFont &font, bool themed)
500 500 {
501 501 if (m_data.m_labelFont != font) {
502 502 m_data.m_labelFont = font;
503 503 m_data.m_labelFont.setThemed(themed);
504 504 emit q_ptr->labelFontChanged();
505 505 }
506 506 }
507 507
508 508 void QPieSlicePrivate::setPercentage(qreal percentage)
509 509 {
510 510 if (!qFuzzyIsNull(m_data.m_percentage - percentage)) {
511 511 m_data.m_percentage = percentage;
512 512 emit q_ptr->percentageChanged();
513 513 }
514 514 }
515 515
516 516 void QPieSlicePrivate::setStartAngle(qreal angle)
517 517 {
518 518 if (!qFuzzyIsNull(m_data.m_startAngle - angle)) {
519 519 m_data.m_startAngle = angle;
520 520 emit q_ptr->startAngleChanged();
521 521 }
522 522 }
523 523
524 524 void QPieSlicePrivate::setAngleSpan(qreal span)
525 525 {
526 526 if (!qFuzzyIsNull(m_data.m_angleSpan - span)) {
527 527 m_data.m_angleSpan = span;
528 528 emit q_ptr->angleSpanChanged();
529 529 }
530 530 }
531 531
532 532 QTCOMMERCIALCHART_END_NAMESPACE
533 533
534 534 QTCOMMERCIALCHART_USE_NAMESPACE
535 535 #include "moc_qpieslice.cpp"
536 536 #include "moc_qpieslice_p.cpp"
@@ -1,114 +1,114
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #ifndef QPIESLICE_H
22 22 #define QPIESLICE_H
23 23
24 24 #include <qchartglobal.h>
25 25 #include <QObject>
26 26 #include <QPen>
27 27 #include <QBrush>
28 28 #include <QFont>
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31 class QPieSlicePrivate;
32 32
33 33 class QTCOMMERCIALCHART_EXPORT QPieSlice : public QObject
34 34 {
35 35 Q_OBJECT
36 36 Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
37 37 Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY valueChanged)
38 38 Q_PROPERTY(bool labelVisible READ isLabelVisible WRITE setLabelVisible NOTIFY labelVisibleChanged)
39 39 Q_PROPERTY(bool exploded READ isExploded WRITE setExploded NOTIFY explodedChanged)
40 40 Q_PROPERTY(QPen pen READ pen WRITE setPen NOTIFY penChanged)
41 41 Q_PROPERTY(QBrush brush READ brush WRITE setBrush NOTIFY brushChanged)
42 Q_PROPERTY(QPen labelPen READ labelPen WRITE setLabelPen NOTIFY labelPenChanged)
42 Q_PROPERTY(QBrush labelBrush READ labelBrush WRITE setLabelBrush NOTIFY labelBrushChanged)
43 43 Q_PROPERTY(QFont labelFont READ labelFont WRITE setLabelFont NOTIFY labelFontChanged)
44 44 Q_PROPERTY(qreal labelArmLengthFactor READ labelArmLengthFactor WRITE setLabelArmLengthFactor)
45 45 Q_PROPERTY(qreal explodeDistanceFactor READ explodeDistanceFactor WRITE setExplodeDistanceFactor)
46 46 Q_PROPERTY(qreal percentage READ percentage NOTIFY percentageChanged)
47 47 Q_PROPERTY(qreal startAngle READ startAngle NOTIFY startAngleChanged)
48 48 Q_PROPERTY(qreal angleSpan READ angleSpan NOTIFY angleSpanChanged)
49 49
50 50 public:
51 51 explicit QPieSlice(QObject *parent = 0);
52 52 QPieSlice(QString label, qreal value, QObject *parent = 0);
53 53 virtual ~QPieSlice();
54 54
55 55 void setLabel(QString label);
56 56 QString label() const;
57 57
58 58 void setValue(qreal value);
59 59 qreal value() const;
60 60
61 61 void setLabelVisible(bool visible = true);
62 62 bool isLabelVisible() const;
63 63
64 64 void setExploded(bool exploded = true);
65 65 bool isExploded() const;
66 66
67 67 void setPen(const QPen &pen);
68 68 QPen pen() const;
69 69
70 70 void setBrush(const QBrush &brush);
71 71 QBrush brush() const;
72 72
73 void setLabelPen(const QPen &pen);
74 QPen labelPen() const;
73 void setLabelBrush(const QBrush &brush);
74 QBrush labelBrush() const;
75 75
76 76 void setLabelFont(const QFont &font);
77 77 QFont labelFont() const;
78 78
79 79 void setLabelArmLengthFactor(qreal factor);
80 80 qreal labelArmLengthFactor() const;
81 81
82 82 void setExplodeDistanceFactor(qreal factor);
83 83 qreal explodeDistanceFactor() const;
84 84
85 85 qreal percentage() const;
86 86 qreal startAngle() const;
87 87 qreal angleSpan() const;
88 88
89 89 Q_SIGNALS:
90 90 void labelChanged();
91 91 void valueChanged();
92 92 void labelVisibleChanged();
93 93 void explodedChanged();
94 94 void penChanged();
95 95 void brushChanged();
96 void labelPenChanged();
96 void labelBrushChanged();
97 97 void labelFontChanged();
98 98 void labelArmLengthFactorChanged();
99 99 void explodeDistanceFactorChanged();
100 100 void percentageChanged();
101 101 void startAngleChanged();
102 102 void angleSpanChanged();
103 103 void clicked();
104 104 void hovered(bool state);
105 105
106 106 private:
107 107 QPieSlicePrivate * const d_ptr;
108 108 Q_DECLARE_PRIVATE(QPieSlice)
109 109 Q_DISABLE_COPY(QPieSlice)
110 110 };
111 111
112 112 QTCOMMERCIALCHART_END_NAMESPACE
113 113
114 114 #endif // QPIESLICE_H
@@ -1,43 +1,43
1 1 #ifndef QPIESLICE_P_H
2 2 #define QPIESLICE_P_H
3 3
4 4 #include <QObject>
5 5 #include "qpieslice.h"
6 6 #include "pieslicedata_p.h"
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class QPieSlicePrivate : public QObject
11 11 {
12 12 Q_OBJECT
13 13
14 14 public:
15 15 QPieSlicePrivate(QPieSlice *parent);
16 16 ~QPieSlicePrivate();
17 17
18 18 static QPieSlicePrivate* fromSlice(QPieSlice *slice);
19 19
20 20 void setPen(const QPen &pen, bool themed);
21 21 void setBrush(const QBrush &brush, bool themed);
22 void setLabelPen(const QPen &pen, bool themed);
22 void setLabelBrush(const QBrush &brush, bool themed);
23 23 void setLabelFont(const QFont &font, bool themed);
24 24
25 25 void setPercentage(qreal percentage);
26 26 void setStartAngle(qreal angle);
27 27 void setAngleSpan(qreal span);
28 28
29 29 private:
30 30 PieSliceData m_data;
31 31
32 32 private:
33 33 friend class QPieSeriesPrivate;
34 34 friend class ChartTheme;
35 35 friend class PieChartItem;
36 36
37 37 QPieSlice * const q_ptr;
38 38 Q_DECLARE_PUBLIC(QPieSlice)
39 39 };
40 40
41 41 QTCOMMERCIALCHART_END_NAMESPACE
42 42
43 43 #endif // QPIESLICE_P_H
@@ -1,436 +1,418
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include <QtTest/QtTest>
22 22 #include <qbarset.h>
23 23
24 24 QTCOMMERCIALCHART_USE_NAMESPACE
25 25
26 26 class tst_QBarSet : public QObject
27 27 {
28 28 Q_OBJECT
29 29
30 30 public slots:
31 31 void initTestCase();
32 32 void cleanupTestCase();
33 33 void init();
34 34 void cleanup();
35 35
36 36 private slots:
37 37 void qbarset_data();
38 38 void qbarset();
39 39 void name_data();
40 40 void name();
41 41 void append_data();
42 42 void append();
43 43 void appendOperator_data();
44 44 void appendOperator();
45 45 void insert_data();
46 46 void insert();
47 47 void remove_data();
48 48 void remove();
49 49 void replace_data();
50 50 void replace();
51 51 void at_data();
52 52 void at();
53 53 void atOperator_data();
54 54 void atOperator();
55 55 void count_data();
56 56 void count();
57 57 void sum_data();
58 58 void sum();
59 59 void setPen_data();
60 60 void setPen();
61 61 void setBrush_data();
62 62 void setBrush();
63 void setLabelPen_data();
64 void setLabelPen();
65 63 void setLabelBrush_data();
66 64 void setLabelBrush();
67 65 void setLabelFont_data();
68 66 void setLabelFont();
69 67
70 68 private:
71 69 QBarSet* m_barset;
72 70 };
73 71
74 72 void tst_QBarSet::initTestCase()
75 73 {
76 74 }
77 75
78 76 void tst_QBarSet::cleanupTestCase()
79 77 {
80 78 }
81 79
82 80 void tst_QBarSet::init()
83 81 {
84 82 m_barset = new QBarSet(QString("Name"));
85 83 }
86 84
87 85 void tst_QBarSet::cleanup()
88 86 {
89 87 delete m_barset;
90 88 m_barset = 0;
91 89 }
92 90
93 91 void tst_QBarSet::qbarset_data()
94 92 {
95 93 }
96 94
97 95 void tst_QBarSet::qbarset()
98 96 {
99 97 QBarSet barset(QString("Name"));
100 98 QCOMPARE(barset.name(), QString("Name"));
101 99 QCOMPARE(barset.count(), 0);
102 100 QVERIFY(qFuzzyIsNull(barset.sum()));
103 101 }
104 102
105 103 void tst_QBarSet::name_data()
106 104 {
107 105 QTest::addColumn<QString> ("name");
108 106 QTest::addColumn<QString> ("result");
109 107 QTest::newRow("name0") << QString("name0") << QString("name0");
110 108 QTest::newRow("name1") << QString("name1") << QString("name1");
111 109 }
112 110
113 111 void tst_QBarSet::name()
114 112 {
115 113 QFETCH(QString, name);
116 114 QFETCH(QString, result);
117 115
118 116 m_barset->setName(name);
119 117 QCOMPARE(m_barset->name(), result);
120 118 }
121 119
122 120 void tst_QBarSet::append_data()
123 121 {
124 122 QTest::addColumn<int> ("count");
125 123 QTest::newRow("0") << 0;
126 124 QTest::newRow("5") << 5;
127 125 QTest::newRow("100") << 100;
128 126 QTest::newRow("1000") << 1000;
129 127 }
130 128
131 129 void tst_QBarSet::append()
132 130 {
133 131 QFETCH(int, count);
134 132
135 133 QCOMPARE(m_barset->count(), 0);
136 134 QVERIFY(qFuzzyIsNull(m_barset->sum()));
137 135
138 136 qreal sum(0.0);
139 137 qreal value(0.0);
140 138
141 139 for (int i=0; i<count; i++) {
142 140 m_barset->append(value);
143 141 QCOMPARE(m_barset->at(i).y(), value);
144 142 sum += value;
145 143 value += 1.0;
146 144 }
147 145
148 146 QCOMPARE(m_barset->count(), count);
149 147 QVERIFY(qFuzzyCompare(m_barset->sum(), sum));
150 148 }
151 149
152 150 void tst_QBarSet::appendOperator_data()
153 151 {
154 152 append_data();
155 153 }
156 154
157 155 void tst_QBarSet::appendOperator()
158 156 {
159 157 QFETCH(int, count);
160 158
161 159 QCOMPARE(m_barset->count(), 0);
162 160 QVERIFY(qFuzzyIsNull(m_barset->sum()));
163 161
164 162 qreal sum(0.0);
165 163 qreal value(0.0);
166 164
167 165 for (int i=0; i<count; i++) {
168 166 *m_barset << value;
169 167 QCOMPARE(m_barset->at(i).y(), value);
170 168 sum += value;
171 169 value += 1.0;
172 170 }
173 171
174 172 QCOMPARE(m_barset->count(), count);
175 173 QVERIFY(qFuzzyCompare(m_barset->sum(), sum));
176 174 }
177 175
178 176 void tst_QBarSet::insert_data()
179 177 {
180 178 }
181 179
182 180 void tst_QBarSet::insert()
183 181 {
184 182 QCOMPARE(m_barset->count(), 0);
185 183 QVERIFY(qFuzzyIsNull(m_barset->sum()));
186 184
187 185 m_barset->insert(0, 1.0); // 1.0
188 186 QCOMPARE(m_barset->at(0).y(), 1.0);
189 187 QCOMPARE(m_barset->count(), 1);
190 188 QVERIFY(qFuzzyCompare(m_barset->sum(), 1.0));
191 189
192 190 m_barset->insert(0, 2.0); // 2.0 1.0
193 191 QCOMPARE(m_barset->at(0).y(), 2.0);
194 192 QCOMPARE(m_barset->at(1).y(), 1.0);
195 193 QCOMPARE(m_barset->count(), 2);
196 194 QVERIFY(qFuzzyCompare(m_barset->sum(), 3.0));
197 195
198 196 m_barset->insert(1, 3.0); // 2.0 3.0 1.0
199 197 QCOMPARE(m_barset->at(1).y(), 3.0);
200 198 QCOMPARE(m_barset->at(0).y(), 2.0);
201 199 QCOMPARE(m_barset->at(2).y(), 1.0);
202 200 QCOMPARE(m_barset->count(), 3);
203 201 QVERIFY(qFuzzyCompare(m_barset->sum(), 6.0));
204 202 }
205 203
206 204 void tst_QBarSet::remove_data()
207 205 {
208 206 }
209 207
210 208 void tst_QBarSet::remove()
211 209 {
212 210 QCOMPARE(m_barset->count(), 0);
213 211 QVERIFY(qFuzzyIsNull(m_barset->sum()));
214 212
215 213 m_barset->append(1.0);
216 214 m_barset->append(2.0);
217 215 m_barset->append(3.0);
218 216 m_barset->append(4.0);
219 217
220 218 QCOMPARE(m_barset->count(), 4);
221 219 QCOMPARE(m_barset->sum(), 10.0);
222 220
223 221 m_barset->remove(2); // 1.0 2.0 4.0
224 222 QCOMPARE(m_barset->at(0).y(), 1.0);
225 223 QCOMPARE(m_barset->at(1).y(), 2.0);
226 224 QCOMPARE(m_barset->at(2).y(), 4.0);
227 225 QCOMPARE(m_barset->count(), 3);
228 226 QCOMPARE(m_barset->sum(), 7.0);
229 227
230 228 m_barset->remove(0); // 2.0 4.0
231 229 QCOMPARE(m_barset->at(0).y(), 2.0);
232 230 QCOMPARE(m_barset->at(1).y(), 4.0);
233 231 QCOMPARE(m_barset->count(), 2);
234 232 QCOMPARE(m_barset->sum(), 6.0);
235 233 }
236 234
237 235 void tst_QBarSet::replace_data()
238 236 {
239 237
240 238 }
241 239
242 240 void tst_QBarSet::replace()
243 241 {
244 242 QCOMPARE(m_barset->count(), 0);
245 243 QVERIFY(qFuzzyIsNull(m_barset->sum()));
246 244
247 245 m_barset->append(1.0);
248 246 m_barset->append(2.0);
249 247 m_barset->append(3.0);
250 248 m_barset->append(4.0);
251 249
252 250 QCOMPARE(m_barset->count(), 4);
253 251 QCOMPARE(m_barset->sum(), 10.0);
254 252
255 253 m_barset->replace(0, 5.0); // 5.0 2.0 3.0 4.0
256 254 QCOMPARE(m_barset->count(), 4);
257 255 QCOMPARE(m_barset->sum(), 14.0);
258 256 QCOMPARE(m_barset->at(0).y(), 5.0);
259 257
260 258 m_barset->replace(3, 6.0);
261 259 QCOMPARE(m_barset->count(), 4); // 5.0 2.0 3.0 6.0
262 260 QCOMPARE(m_barset->sum(), 16.0);
263 261 QCOMPARE(m_barset->at(0).y(), 5.0);
264 262 QCOMPARE(m_barset->at(1).y(), 2.0);
265 263 QCOMPARE(m_barset->at(2).y(), 3.0);
266 264 QCOMPARE(m_barset->at(3).y(), 6.0);
267 265 }
268 266
269 267 void tst_QBarSet::at_data()
270 268 {
271 269
272 270 }
273 271
274 272 void tst_QBarSet::at()
275 273 {
276 274 QCOMPARE(m_barset->count(), 0);
277 275 QVERIFY(qFuzzyIsNull(m_barset->sum()));
278 276
279 277 m_barset->append(1.0);
280 278 m_barset->append(2.0);
281 279 m_barset->append(3.0);
282 280 m_barset->append(4.0);
283 281
284 282 QCOMPARE(m_barset->at(0).y(), 1.0);
285 283 QCOMPARE(m_barset->at(1).y(), 2.0);
286 284 QCOMPARE(m_barset->at(2).y(), 3.0);
287 285 QCOMPARE(m_barset->at(3).y(), 4.0);
288 286 }
289 287
290 288 void tst_QBarSet::atOperator_data()
291 289 {
292 290
293 291 }
294 292
295 293 void tst_QBarSet::atOperator()
296 294 {
297 295 QCOMPARE(m_barset->count(), 0);
298 296 QVERIFY(qFuzzyIsNull(m_barset->sum()));
299 297
300 298 m_barset->append(1.0);
301 299 m_barset->append(2.0);
302 300 m_barset->append(3.0);
303 301 m_barset->append(4.0);
304 302
305 303 QCOMPARE(m_barset->operator [](0).y(), 1.0);
306 304 QCOMPARE(m_barset->operator [](1).y(), 2.0);
307 305 QCOMPARE(m_barset->operator [](2).y(), 3.0);
308 306 QCOMPARE(m_barset->operator [](3).y(), 4.0);
309 307 }
310 308
311 309 void tst_QBarSet::count_data()
312 310 {
313 311
314 312 }
315 313
316 314 void tst_QBarSet::count()
317 315 {
318 316 QCOMPARE(m_barset->count(), 0);
319 317 QVERIFY(qFuzzyIsNull(m_barset->sum()));
320 318
321 319 m_barset->append(1.0);
322 320 QCOMPARE(m_barset->count(),1);
323 321 m_barset->append(2.0);
324 322 QCOMPARE(m_barset->count(),2);
325 323 m_barset->append(3.0);
326 324 QCOMPARE(m_barset->count(),3);
327 325 m_barset->append(4.0);
328 326 QCOMPARE(m_barset->count(),4);
329 327 }
330 328
331 329 void tst_QBarSet::sum_data()
332 330 {
333 331
334 332 }
335 333
336 334 void tst_QBarSet::sum()
337 335 {
338 336 QCOMPARE(m_barset->count(), 0);
339 337 QVERIFY(qFuzzyIsNull(m_barset->sum()));
340 338
341 339 m_barset->append(1.0);
342 340 QVERIFY(qFuzzyCompare(m_barset->sum(),1.0));
343 341 m_barset->append(2.0);
344 342 QVERIFY(qFuzzyCompare(m_barset->sum(),3.0));
345 343 m_barset->append(3.0);
346 344 QVERIFY(qFuzzyCompare(m_barset->sum(),6.0));
347 345 m_barset->append(4.0);
348 346 QVERIFY(qFuzzyCompare(m_barset->sum(),10.0));
349 347 }
350 348
351 349 void tst_QBarSet::setPen_data()
352 350 {
353 351
354 352 }
355 353
356 354 void tst_QBarSet::setPen()
357 355 {
358 356 QVERIFY(m_barset->pen() == QPen());
359 357
360 358 QPen pen;
361 359 pen.setColor(QColor(128,128,128,128));
362 360 m_barset->setPen(pen);
363 361
364 362 QVERIFY(m_barset->pen() == pen);
365 363 }
366 364
367 365 void tst_QBarSet::setBrush_data()
368 366 {
369 367
370 368 }
371 369
372 370 void tst_QBarSet::setBrush()
373 371 {
374 372 QVERIFY(m_barset->brush() == QBrush());
375 373
376 374 QBrush brush;
377 375 brush.setColor(QColor(128,128,128,128));
378 376 m_barset->setBrush(brush);
379 377
380 378 QVERIFY(m_barset->brush() == brush);
381 379 }
382 380
383 void tst_QBarSet::setLabelPen_data()
384 {
385
386 }
387
388 void tst_QBarSet::setLabelPen()
389 {
390 QVERIFY(m_barset->labelPen() == QPen());
391
392 QPen pen;
393 pen.setColor(QColor(128,128,128,128));
394 m_barset->setLabelPen(pen);
395
396 QVERIFY(m_barset->labelPen() == pen);
397 }
398
399 381 void tst_QBarSet::setLabelBrush_data()
400 382 {
401 383
402 384 }
403 385
404 386 void tst_QBarSet::setLabelBrush()
405 387 {
406 388 QVERIFY(m_barset->labelBrush() == QBrush());
407 389
408 390 QBrush brush;
409 391 brush.setColor(QColor(128,128,128,128));
410 392 m_barset->setLabelBrush(brush);
411 393
412 394 QVERIFY(m_barset->labelBrush() == brush);
413 395 }
414 396
415 397 void tst_QBarSet::setLabelFont_data()
416 398 {
417 399
418 400 }
419 401
420 402 void tst_QBarSet::setLabelFont()
421 403 {
422 404 QVERIFY(m_barset->labelFont() == QFont());
423 405
424 406 QFont font;
425 407 font.setBold(true);
426 408 font.setItalic(true);
427 409 m_barset->setLabelFont(font);
428 410
429 411 QVERIFY(m_barset->labelFont() == font);
430 412 }
431 413
432 414
433 415 QTEST_MAIN(tst_QBarSet)
434 416
435 417 #include "tst_qbarset.moc"
436 418
@@ -1,292 +1,292
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include <QtTest/QtTest>
22 22 #include <tst_definitions.h>
23 23 #include <qchartview.h>
24 24 #include <qchart.h>
25 25 #include <qpieslice.h>
26 26 #include <qpieseries.h>
27 27
28 28 QTCOMMERCIALCHART_USE_NAMESPACE
29 29
30 30 class tst_qpieslice : public QObject
31 31 {
32 32 Q_OBJECT
33 33
34 34 public slots:
35 35 void initTestCase();
36 36 void cleanupTestCase();
37 37 void init();
38 38 void cleanup();
39 39
40 40 private slots:
41 41 void construction();
42 42 void changedSignals();
43 43 void customize();
44 44 void mouseClick();
45 45 void mouseHover();
46 46
47 47 private:
48 48
49 49
50 50 private:
51 51
52 52 };
53 53
54 54 void tst_qpieslice::initTestCase()
55 55 {
56 56 }
57 57
58 58 void tst_qpieslice::cleanupTestCase()
59 59 {
60 60 }
61 61
62 62 void tst_qpieslice::init()
63 63 {
64 64
65 65 }
66 66
67 67 void tst_qpieslice::cleanup()
68 68 {
69 69
70 70 }
71 71
72 72 void tst_qpieslice::construction()
73 73 {
74 74 // no params
75 75 QPieSlice slice1;
76 76 QCOMPARE(slice1.value(), 0.0);
77 77 QVERIFY(slice1.label().isEmpty());
78 78 QVERIFY(!slice1.isLabelVisible());
79 79 QVERIFY(!slice1.isExploded());
80 80 QCOMPARE(slice1.pen(), QPen());
81 81 QCOMPARE(slice1.brush(), QBrush());
82 QCOMPARE(slice1.labelPen(), QPen());
82 QCOMPARE(slice1.labelBrush(), QBrush());
83 83 QCOMPARE(slice1.labelFont(), QFont());
84 84 QCOMPARE(slice1.labelArmLengthFactor(), 0.15); // default value
85 85 QCOMPARE(slice1.explodeDistanceFactor(), 0.15); // default value
86 86 QCOMPARE(slice1.percentage(), 0.0);
87 87 QCOMPARE(slice1.startAngle(), 0.0);
88 88 QCOMPARE(slice1.angleSpan(), 0.0);
89 89
90 90 // value and label params
91 91 QPieSlice slice2("foobar", 1.0);
92 92 QCOMPARE(slice2.value(), 1.0);
93 93 QCOMPARE(slice2.label(), QString("foobar"));
94 94 QVERIFY(!slice2.isLabelVisible());
95 95 QVERIFY(!slice2.isExploded());
96 96 QCOMPARE(slice2.pen(), QPen());
97 97 QCOMPARE(slice2.brush(), QBrush());
98 QCOMPARE(slice2.labelPen(), QPen());
98 QCOMPARE(slice2.labelBrush(), QBrush());
99 99 QCOMPARE(slice2.labelFont(), QFont());
100 100 QCOMPARE(slice2.labelArmLengthFactor(), 0.15); // default value
101 101 QCOMPARE(slice2.explodeDistanceFactor(), 0.15); // default value
102 102 QCOMPARE(slice2.percentage(), 0.0);
103 103 QCOMPARE(slice2.startAngle(), 0.0);
104 104 QCOMPARE(slice2.angleSpan(), 0.0);
105 105 }
106 106
107 107 void tst_qpieslice::changedSignals()
108 108 {
109 109 QPieSlice slice;
110 110
111 111 QSignalSpy valueSpy(&slice, SIGNAL(valueChanged()));
112 112 QSignalSpy labelSpy(&slice, SIGNAL(labelChanged()));
113 113 QSignalSpy explodedSpy(&slice, SIGNAL(explodedChanged()));
114 114 QSignalSpy penSpy(&slice, SIGNAL(penChanged()));
115 115 QSignalSpy brushSpy(&slice, SIGNAL(brushChanged()));
116 QSignalSpy labelPenSpy(&slice, SIGNAL(labelPenChanged()));
116 QSignalSpy labelBrushSpy(&slice, SIGNAL(labelBrushChanged()));
117 117 QSignalSpy labelFontSpy(&slice, SIGNAL(labelFontChanged()));
118 118 QSignalSpy labelArmLengthFactorSpy(&slice, SIGNAL(labelArmLengthFactorChanged()));
119 119 QSignalSpy explodeDistanceFactorSpy(&slice, SIGNAL(explodeDistanceFactorChanged()));
120 120
121 121 // percentageChanged(), startAngleChanged() and angleSpanChanged() signals tested at tst_qpieseries::calculatedValues()
122 122
123 123 // set everything twice to see we do not get unnecessary signals
124 124 slice.setValue(1.0);
125 125 slice.setValue(-1.0);
126 126 QCOMPARE(slice.value(), 1.0);
127 127 slice.setLabel("foobar");
128 128 slice.setLabel("foobar");
129 129 slice.setLabelVisible();
130 130 slice.setLabelVisible();
131 131 slice.setExploded();
132 132 slice.setExploded();
133 133 slice.setPen(QPen(Qt::red));
134 134 slice.setPen(QPen(Qt::red));
135 135 slice.setBrush(QBrush(Qt::red));
136 136 slice.setBrush(QBrush(Qt::red));
137 slice.setLabelPen(QPen(Qt::green));
138 slice.setLabelPen(QPen(Qt::green));
137 slice.setLabelBrush(QBrush(Qt::green));
138 slice.setLabelBrush(QBrush(Qt::green));
139 139 slice.setLabelFont(QFont("Tahoma"));
140 140 slice.setLabelFont(QFont("Tahoma"));
141 141 slice.setLabelArmLengthFactor(0.1);
142 142 slice.setLabelArmLengthFactor(0.1);
143 143 slice.setExplodeDistanceFactor(0.1);
144 144 slice.setExplodeDistanceFactor(0.1);
145 145
146 146 TRY_COMPARE(valueSpy.count(), 1);
147 147 TRY_COMPARE(labelSpy.count(), 1);
148 148 TRY_COMPARE(explodedSpy.count(), 1);
149 149 TRY_COMPARE(penSpy.count(), 1);
150 150 TRY_COMPARE(brushSpy.count(), 1);
151 TRY_COMPARE(labelPenSpy.count(), 1);
151 TRY_COMPARE(labelBrushSpy.count(), 1);
152 152 TRY_COMPARE(labelFontSpy.count(), 1);
153 153 TRY_COMPARE(labelArmLengthFactorSpy.count(), 1);
154 154 TRY_COMPARE(explodeDistanceFactorSpy.count(), 1);
155 155 }
156 156
157 157 void tst_qpieslice::customize()
158 158 {
159 159 // create a pie series
160 160 QPieSeries *series = new QPieSeries();
161 161 QPieSlice *s1 = series->append("slice 1", 1);
162 162 QPieSlice *s2 = series->append("slice 2", 2);
163 163 series->append("slice 3", 3);
164 164
165 165 // customize a slice
166 166 QPen p1(Qt::red);
167 167 s1->setPen(p1);
168 168 QBrush b1(Qt::red);
169 169 s1->setBrush(b1);
170 s1->setLabelPen(p1);
170 s1->setLabelBrush(b1);
171 171 QFont f1("Consolas");
172 172 s1->setLabelFont(f1);
173 173
174 174 // add series to the chart
175 175 QChartView view(new QChart());
176 176 view.resize(200, 200);
177 177 view.chart()->addSeries(series);
178 178 view.show();
179 179 QTest::qWaitForWindowShown(&view);
180 180 //QTest::qWait(1000);
181 181
182 182 // check that customizations persist
183 183 QCOMPARE(s1->pen(), p1);
184 184 QCOMPARE(s1->brush(), b1);
185 QCOMPARE(s1->labelPen(), p1);
185 QCOMPARE(s1->labelBrush(), b1);
186 186 QCOMPARE(s1->labelFont(), f1);
187 187
188 188 // remove a slice
189 189 series->remove(s2);
190 190 QCOMPARE(s1->pen(), p1);
191 191 QCOMPARE(s1->brush(), b1);
192 QCOMPARE(s1->labelPen(), p1);
192 QCOMPARE(s1->labelBrush(), b1);
193 193 QCOMPARE(s1->labelFont(), f1);
194 194
195 195 // add a slice
196 196 series->append("slice 4", 4);
197 197 QCOMPARE(s1->pen(), p1);
198 198 QCOMPARE(s1->brush(), b1);
199 QCOMPARE(s1->labelPen(), p1);
199 QCOMPARE(s1->labelBrush(), b1);
200 200 QCOMPARE(s1->labelFont(), f1);
201 201
202 202 // insert a slice
203 203 series->insert(0, new QPieSlice("slice 5", 5));
204 204 QCOMPARE(s1->pen(), p1);
205 205 QCOMPARE(s1->brush(), b1);
206 QCOMPARE(s1->labelPen(), p1);
206 QCOMPARE(s1->labelBrush(), b1);
207 207 QCOMPARE(s1->labelFont(), f1);
208 208
209 209 // change theme
210 210 // theme will overwrite customizations
211 211 view.chart()->setTheme(QChart::ChartThemeHighContrast);
212 212 QVERIFY(s1->pen() != p1);
213 213 QVERIFY(s1->brush() != b1);
214 QVERIFY(s1->labelPen() != p1);
214 QVERIFY(s1->labelBrush() != b1);
215 215 QVERIFY(s1->labelFont() != f1);
216 216 }
217 217
218 218 void tst_qpieslice::mouseClick()
219 219 {
220 220 // create a pie series
221 221 QPieSeries *series = new QPieSeries();
222 222 series->setPieSize(1.0);
223 223 QPieSlice *s1 = series->append("slice 1", 1);
224 224 QPieSlice *s2 = series->append("slice 2", 2);
225 225 QPieSlice *s3 = series->append("slice 3", 3);
226 226 QSignalSpy clickSpy1(s1, SIGNAL(clicked()));
227 227 QSignalSpy clickSpy2(s2, SIGNAL(clicked()));
228 228 QSignalSpy clickSpy3(s3, SIGNAL(clicked()));
229 229
230 230 // add series to the chart
231 231 QChartView view(new QChart());
232 232 view.chart()->legend()->setVisible(false);
233 233 view.resize(200, 200);
234 234 view.chart()->addSeries(series);
235 235 view.show();
236 236 QTest::qWaitForWindowShown(&view);
237 237
238 238 // simulate clicks
239 239 // pie rectangle: QRectF(60,60 121x121)
240 240 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(139, 85)); // inside slice 1
241 241 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(146, 136)); // inside slice 2
242 242 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(91, 119)); // inside slice 3
243 243 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(70, 70)); // inside pie rectangle but not inside a slice
244 244 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(170, 170)); // inside pie rectangle but not inside a slice
245 245 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
246 246 QCOMPARE(clickSpy1.count(), 1);
247 247 QCOMPARE(clickSpy2.count(), 1);
248 248 QCOMPARE(clickSpy3.count(), 1);
249 249 }
250 250
251 251 void tst_qpieslice::mouseHover()
252 252 {
253 253 // create a pie series
254 254 QPieSeries *series = new QPieSeries();
255 255 series->setPieSize(1.0);
256 256 QPieSlice *s1 = series->append("slice 1", 1);
257 257 series->append("slice 2", 2);
258 258 series->append("slice 3", 3);
259 259
260 260 // add series to the chart
261 261 QChartView view(new QChart());
262 262 view.chart()->legend()->setVisible(false);
263 263 view.resize(200, 200);
264 264 view.chart()->addSeries(series);
265 265 view.show();
266 266 QTest::qWaitForWindowShown(&view);
267 267
268 268 // first move to right top corner
269 269 QTest::mouseMove(view.viewport(), QPoint(200, 0));
270 270 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
271 271
272 272 // move inside slice rectangle but NOT the actual slice
273 273 // pie rectangle: QRectF(60,60 121x121)
274 274 QSignalSpy hoverSpy(s1, SIGNAL(hovered(bool)));
275 275 QTest::mouseMove(view.viewport(), QPoint(170, 70));
276 276 TRY_COMPARE(hoverSpy.count(), 0);
277 277
278 278 // move inside the slice
279 279 QTest::mouseMove(view.viewport(), QPoint(139, 85));
280 280 TRY_COMPARE(hoverSpy.count(), 1);
281 281 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(0).at(0)), true);
282 282
283 283 // move outside the slice
284 284 QTest::mouseMove(view.viewport(), QPoint(200, 0));
285 285 TRY_COMPARE(hoverSpy.count(), 2);
286 286 QCOMPARE(qvariant_cast<bool>(hoverSpy.at(1).at(0)), false);
287 287 }
288 288
289 289 QTEST_MAIN(tst_qpieslice)
290 290
291 291 #include "tst_qpieslice.moc"
292 292
General Comments 0
You need to be logged in to leave comments. Login now