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