##// END OF EJS Templates
Unify naming setGeometry -> setLayout
Michal Klocek -
r557:6b65c67e3745
parent child
Show More
@@ -1,96 +1,96
1 1 #include "xyanimation_p.h"
2 2 #include "xychartitem_p.h"
3 3
4 4 Q_DECLARE_METATYPE(QVector<QPointF>)
5 5
6 6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7 7
8 8 XYAnimation::XYAnimation(XYChartItem *item):ChartAnimation(item),
9 9 m_item(item),
10 10 m_type(MoveDownAnimation),
11 11 m_dirty(false)
12 12 {
13 13 }
14 14
15 15 XYAnimation::~XYAnimation()
16 16 {
17 17 }
18 18
19 19 void XYAnimation::setAnimationType(Animation type)
20 20 {
21 21 m_type=type;
22 22 }
23 23
24 24 void XYAnimation::setValues(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints)
25 25 {
26 26 setKeyValueAt(0.0, qVariantFromValue(oldPoints));
27 27 setKeyValueAt(1.0, qVariantFromValue(newPoints));
28 28 m_points = newPoints;
29 29 m_dirty=false;
30 30 }
31 31
32 32 void XYAnimation::updateValues(QVector<QPointF>& newPoints)
33 33 {
34 34 if(state()!=QAbstractAnimation::Stopped) {
35 35 stop();
36 36 m_dirty=true;
37 37 }
38 38
39 39 if(m_dirty) {
40 40 m_points=newPoints;
41 41 m_dirty=false;
42 42 }
43 43
44 44 setKeyValueAt(0.0, qVariantFromValue(m_points));
45 45 setKeyValueAt(1.0, qVariantFromValue(newPoints));
46 46 }
47 47
48 48 QVariant XYAnimation::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
49 49 {
50 50 QVector<QPointF> startVector = qVariantValue<QVector<QPointF> >(start);
51 51 QVector<QPointF> endVector = qVariantValue<QVector<QPointF> >(end);
52 52 QVector<QPointF> result;
53 53
54 54 switch(m_type) {
55 55
56 56 case MoveDownAnimation: {
57 57
58 58 Q_ASSERT(startVector.count() == endVector.count());
59 59 for(int i =0;i< startVector.count();i++) {
60 60 qreal x = startVector[i].x() + ((endVector[i].x()- startVector[i].x()) * progress);
61 61 qreal y = startVector[i].y() + ((endVector[i].y()- startVector[i].y()) * progress);
62 62 result << QPointF(x,y);
63 63 }
64 64
65 65 }
66 66 break;
67 67 case LineDrawAnimation:{
68 68 for(int i =0;i< endVector.count()* qBound(0.0, progress, 1.0);i++) {
69 69 result << endVector[i];
70 70 }
71 71 }
72 72 break;
73 73 default:
74 74 qWarning()<<"Unknow type of animation";
75 75 break;
76 76 }
77 77
78 78 return qVariantFromValue(result);
79 79 }
80 80
81 81 void XYAnimation::updateCurrentValue (const QVariant & value )
82 82 {
83 83 if(state()!=QAbstractAnimation::Stopped){ //workaround
84 84 QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value);
85 m_item->setGeometry(vector);
85 m_item->setLayout(vector);
86 86 }
87 87 }
88 88
89 89 void XYAnimation::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState)
90 90 {
91 91 Q_UNUSED(oldState)
92 92 if (newState==QAbstractAnimation::Running) m_dirty=true;
93 93 QVariantAnimation::updateState(newState,oldState);
94 94 }
95 95
96 96 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,112 +1,102
1 1 #include "areachartitem_p.h"
2 2 #include "qareaseries.h"
3 3 #include "qlineseries.h"
4 4 #include <QPainter>
5 5
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9 //TODO: optimize : remove points which are not visible
10 10
11 11 AreaChartItem::AreaChartItem(QAreaSeries* areaSeries,QGraphicsItem *parent):ChartItem(parent),
12 12 m_series(areaSeries),
13 13 m_upper(0),
14 14 m_lower(0)
15 15 {
16 16 //m_items.setZValue(ChartPresenter::LineChartZValue);
17 17 m_upper = new AreaBoundItem(this,m_series->upperSeries());
18 18 if(m_series->lowerSeries()){
19 19 m_lower = new AreaBoundItem(this,m_series->lowerSeries());
20 20 }
21 21
22 22 QObject::connect(areaSeries,SIGNAL(updated()),this,SLOT(handleUpdated()));
23 23
24 24 handleUpdated();
25 25 }
26 26
27 27 AreaChartItem::~AreaChartItem()
28 28 {
29 29 delete m_upper;
30 30 delete m_lower;
31 31 };
32 32
33 33 QRectF AreaChartItem::boundingRect() const
34 34 {
35 35 return m_rect;
36 36 }
37 37
38 38 QPainterPath AreaChartItem::shape() const
39 39 {
40 40 return m_path;
41 41 }
42 42
43 void AreaChartItem::setPen(const QPen& pen)
44 {
45 m_pen = pen;
46 }
47
48 void AreaChartItem::setBrush(const QBrush& brush)
49 {
50 m_brush = brush;
51 }
52
53 43 void AreaChartItem::updatePath()
54 44 {
55 45 QPainterPath path;
56 46
57 47 path.connectPath(m_upper->shape());
58 48 if(m_lower){
59 49 path.connectPath(m_lower->shape().toReversed());
60 50 }
61 51 else{
62 52 QPointF first = path.pointAtPercent(0);
63 53 QPointF last = path.pointAtPercent(1);
64 54 path.lineTo(last.x(),m_clipRect.bottom());
65 55 path.lineTo(first.x(),m_clipRect.bottom());
66 56 }
67 57 path.closeSubpath();
68 58 prepareGeometryChange();
69 59 m_path=path;
70 60 m_rect=path.boundingRect();
71 61 update();
72 62 }
73 63
74 64 void AreaChartItem::handleUpdated()
75 65 {
76 setPen(m_series->pen());
77 setBrush(m_series->brush());
66 m_pen = m_series->pen();
67 m_brush = m_series->brush();
78 68 update();
79 69 }
80 70
81 71 void AreaChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
82 72 {
83 73 m_upper->handleDomainChanged(minX,maxX,minY,maxY);
84 74 if(m_lower)
85 75 m_lower->handleDomainChanged(minX,maxX,minY,maxY);
86 76 }
87 77
88 78 void AreaChartItem::handleGeometryChanged(const QRectF& rect)
89 79 {
90 80 m_clipRect=rect.translated(-rect.topLeft());
91 81 setPos(rect.topLeft());
92 82 m_upper->handleGeometryChanged(rect);
93 83 if(m_lower)
94 84 m_lower->handleGeometryChanged(rect);
95 85 }
96 86 //painter
97 87
98 88 void AreaChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
99 89 {
100 90 Q_UNUSED(widget);
101 91 Q_UNUSED(option);
102 92 painter->save();
103 93 painter->setPen(m_pen);
104 94 painter->setBrush(m_brush);
105 95 painter->setClipRect(m_clipRect);
106 96 painter->drawPath(m_path);
107 97 painter->restore();
108 98 }
109 99
110 100 #include "moc_areachartitem_p.cpp"
111 101
112 102 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,65 +1,63
1 1 #ifndef AREACHARTITEM_H
2 2 #define AREACHARTITEM_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "linechartitem_p.h"
6 6 #include <QPen>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class QAreaSeries;
11 11 class AreaChartItem;
12 12
13 13 class AreaChartItem : public QObject ,public ChartItem
14 14 {
15 15 Q_OBJECT
16 16 public:
17 17 AreaChartItem(QAreaSeries* areaSeries, QGraphicsItem *parent = 0);
18 18 ~ AreaChartItem();
19 19
20 20 //from QGraphicsItem
21 21 QRectF boundingRect() const;
22 22 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 23 QPainterPath shape() const;
24 24
25 void setPen(const QPen& pen);
26 void setBrush(const QBrush& brush);
27 25 void setPointsVisible(bool visible);
28 26 void updatePath();
29 27 public slots:
30 28 void handleUpdated();
31 29 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
32 30 void handleGeometryChanged(const QRectF& size);
33 31
34 32 private:
35 33 QAreaSeries* m_series;
36 34 LineChartItem* m_upper;
37 35 LineChartItem* m_lower;
38 36 QPainterPath m_path;
39 37 QRectF m_rect;
40 38 QRectF m_clipRect;
41 39 QPen m_pen;
42 40 QBrush m_brush;
43 41 };
44 42
45 43 class AreaBoundItem : public LineChartItem
46 44 {
47 45 public:
48 46 AreaBoundItem(AreaChartItem* item,QLineSeries* lineSeries):LineChartItem(lineSeries),
49 47 m_item(item){};
50 48
51 49 ~AreaBoundItem(){};
52 50
53 void setGeometry(QVector<QPointF>& points){
54 LineChartItem::setGeometry(points);
51 void setLayout(QVector<QPointF>& points){
52 LineChartItem::setLayout(points);
55 53 m_item->updatePath();
56 54 }
57 55
58 56 private:
59 57 AreaChartItem* m_item;
60 58
61 59 };
62 60
63 61 QTCOMMERCIALCHART_END_NAMESPACE
64 62
65 63 #endif
@@ -1,333 +1,329
1 1 #include "charttheme_p.h"
2 2 #include "qchart.h"
3 3 #include "qlegend.h"
4 4 #include "qchartaxis.h"
5 5 #include <QTime>
6 6
7 7 //series
8 8 #include "qbarset.h"
9 9 #include "qbarseries.h"
10 10 #include "qstackedbarseries.h"
11 11 #include "qpercentbarseries.h"
12 12 #include "qlineseries.h"
13 13 #include "qareaseries.h"
14 14 #include "qscatterseries.h"
15 15 #include "qpieseries.h"
16 16 #include "qpieslice.h"
17 17 #include "qsplineseries.h"
18 18
19 19 //items
20 20 #include "axisitem_p.h"
21 21 #include "barpresenter_p.h"
22 22 #include "stackedbarpresenter_p.h"
23 23 #include "percentbarpresenter_p.h"
24 24 #include "linechartitem_p.h"
25 25 #include "areachartitem_p.h"
26 26 #include "scatterchartitem_p.h"
27 27 #include "piepresenter_p.h"
28 28 #include "splinechartitem_p.h"
29 29
30 30 //themes
31 31 #include "chartthemedefault_p.h"
32 32 #include "chartthemevanilla_p.h"
33 33 #include "chartthemeicy_p.h"
34 34 #include "chartthemegrayscale_p.h"
35 35 #include "chartthemescientific_p.h"
36 36
37 37
38 38 QTCOMMERCIALCHART_BEGIN_NAMESPACE
39 39
40 40 ChartTheme::ChartTheme(QChart::ChartTheme id)
41 41 {
42 42 m_id = id;
43 43 qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
44 44 }
45 45
46 46
47 47 ChartTheme* ChartTheme::createTheme(QChart::ChartTheme theme)
48 48 {
49 49 switch(theme) {
50 50 case QChart::ChartThemeVanilla:
51 51 return new ChartThemeVanilla();
52 52 case QChart::ChartThemeIcy:
53 53 return new ChartThemeIcy();
54 54 case QChart::ChartThemeGrayscale:
55 55 return new ChartThemeGrayscale();
56 56 case QChart::ChartThemeScientific:
57 57 return new ChartThemeScientific();
58 58 default:
59 59 return new ChartThemeDefault();
60 60 }
61 61 }
62 62
63 63 void ChartTheme::decorate(QChart* chart)
64 64 {
65 65 chart->setChartBackgroundBrush(m_backgroundGradient);
66 66 chart->setChartTitleFont(m_masterFont);
67 67 }
68 68
69 69 void ChartTheme::decorate(QLegend* legend)
70 70 {
71 71 legend->setBackgroundBrush(m_backgroundGradient);
72 72 }
73 73
74 74 void ChartTheme::decorate(AreaChartItem* item, QAreaSeries* series, int index)
75 75 {
76 76 QPen pen;
77 77 QBrush brush;
78 78
79 79 if (pen != series->pen()){
80 item->setPen(series->pen());
81 } else {
82 80 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0));
83 81 pen.setWidthF(2);
84 item->setPen(pen);
82 series->setPen(pen);
85 83 }
86 84
87 85 if (brush != series->brush()) {
88 item->setBrush(series->brush());
89 } else {
90 86 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
91 item->setBrush(brush);
87 series->setBrush(brush);
92 88 }
93 89 }
94 90
95 91
96 92 void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int index)
97 93 {
98 94 QPen pen;
99 95 if(pen != series->pen()){
100 96 series->setPen(series->pen());
101 97 return;
102 98 }else{
103 99 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
104 100 pen.setWidthF(2);
105 101 series->setPen(pen);
106 102 }
107 103 }
108 104
109 105 void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int index)
110 106 {
111 107 QList<QBarSet*> sets = series->barSets();
112 108 for (int i=0; i<sets.count(); i++) {
113 109 qreal pos = 0.5;
114 110 if (sets.count() > 1)
115 111 pos = (qreal) i / (qreal) (sets.count() - 1);
116 112 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
117 113 sets.at(i)->setBrush(QBrush(c));
118 114
119 115 // Pick label color as far as possible from bar color (within gradient).
120 116 // 0.3 is magic number that was picked as value that gave enough contrast with icy theme gradient :)
121 117 // TODO: better picking of label color?
122 118 if (pos < 0.3) {
123 119 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
124 120 } else {
125 121 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
126 122 }
127 123 sets.at(i)->setFloatingValuePen(QPen(c));
128 124 }
129 125 }
130 126
131 127 void ChartTheme::decorate(StackedBarPresenter* item, QStackedBarSeries* series,int index)
132 128 {
133 129 QList<QBarSet*> sets = series->barSets();
134 130 for (int i=0; i<sets.count(); i++) {
135 131 qreal pos = 0.5;
136 132 if (sets.count() > 1)
137 133 pos = (qreal) i / (qreal) (sets.count() - 1);
138 134 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
139 135 sets.at(i)->setBrush(QBrush(c));
140 136
141 137 if (pos < 0.3) {
142 138 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
143 139 } else {
144 140 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
145 141 }
146 142 sets.at(i)->setFloatingValuePen(QPen(c));
147 143 }
148 144 }
149 145
150 146 void ChartTheme::decorate(PercentBarPresenter* item, QPercentBarSeries* series,int index)
151 147 {
152 148 QList<QBarSet*> sets = series->barSets();
153 149 for (int i=0; i<sets.count(); i++) {
154 150 qreal pos = 0.5;
155 151 if (sets.count() > 1)
156 152 pos = (qreal) i / (qreal) (sets.count() - 1);
157 153 QColor c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
158 154 sets.at(i)->setBrush(QBrush(c));
159 155
160 156 if (pos < 0.3) {
161 157 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1);
162 158 } else {
163 159 c = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0);
164 160 }
165 161 sets.at(i)->setFloatingValuePen(QPen(c));
166 162 }
167 163 }
168 164
169 165 void ChartTheme::decorate(ScatterChartItem* item, QScatterSeries* series, int index)
170 166 {
171 167 Q_ASSERT(item);
172 168 Q_ASSERT(series);
173 169
174 170 QPen pen;
175 171 QBrush brush;
176 172
177 173 if (pen == series->pen()) {
178 174 pen.setColor(colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 1.0));
179 175 pen.setWidthF(2);
180 176 series->setPen(pen);
181 177 }
182 178
183 179 if (brush == series->brush()) {
184 180 QBrush brush(m_seriesColors.at(index % m_seriesColors.size()));
185 181 series->setBrush(brush);
186 182 }
187 183 }
188 184
189 185 void ChartTheme::decorate(PiePresenter* item, QPieSeries* series, int index)
190 186 {
191 187 // Get color for a slice from a gradient linearly, beginning from the start of the gradient
192 188 for (int i(0); i < series->slices().count(); i++) {
193 189 qreal pos = (qreal) i / (qreal) series->count();
194 190 QColor penColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), 0.1);
195 191 series->slices().at(i)->setSlicePen(penColor);
196 192 QColor brushColor = colorAt(m_seriesGradients.at(index % m_seriesGradients.size()), pos);
197 193 series->slices().at(i)->setSliceBrush(brushColor);
198 194 }
199 195 }
200 196
201 197 void ChartTheme::decorate(SplineChartItem* item, QSplineSeries* series, int index)
202 198 {
203 199 Q_ASSERT(item);
204 200 Q_ASSERT(series);
205 201
206 202 QPen pen;
207 203
208 204 if(pen != series->pen()){
209 205 item->setLinePen(series->pen());
210 206 }else{
211 207 pen.setColor(m_seriesColors.at(index%m_seriesColors.size()));
212 208 pen.setWidthF(series->pen().widthF());
213 209 item->setLinePen(series->pen());
214 210 }
215 211
216 212 // QColor color = m_seriesColors.at(index % m_seriesColors.size());
217 213 // TODO: define alpha in the theme? or in the series?
218 214 //color.setAlpha(120);
219 215
220 216 // QBrush brush(color, Qt::SolidPattern);
221 217 // presenter->m_markerBrush = brush;
222 218
223 219 // QPen pen(brush, 3);
224 220 // pen.setColor(color);
225 221 // presenter->m_markerPen = pen;
226 222 }
227 223
228 224 void ChartTheme::decorate(QChartAxis* axis, AxisItem* item)
229 225 {
230 226 Q_ASSERT(axis);
231 227 Q_ASSERT(item);
232 228
233 229 if (axis->isAxisVisible()) {
234 230 axis->setLabelsBrush(m_axisLabelBrush);
235 231 axis->setLabelsPen(m_axisLabelPen);
236 232 // TODO: check the axis type (x or y) should define whether to show the shades or not
237 233 if (m_backgroundShades == BackgroundShadesBoth
238 234 || m_backgroundShades == BackgroundShadesVertical /*&& x axis ?*/
239 235 || m_backgroundShades == BackgroundShadesHorizontal /* && y axis*/) {
240 236 axis->setShadesPen(m_backgroundShadesPen);
241 237 axis->setShadesBrush(m_backgroundShadesBrush);
242 238 } else {
243 239 // The shades not supposed to be shown for this axis, clear possible brush and pen
244 240 axis->setShadesPen(Qt::NoPen);
245 241 axis->setShadesBrush(Qt::NoBrush);
246 242 }
247 243 axis->setAxisPen(m_axisLinePen);
248 244 axis->setGridLinePen(m_gridLinePen);
249 245 axis->setLabelsFont(m_masterFont);
250 246 }
251 247 }
252 248
253 249 void ChartTheme::generateSeriesGradients()
254 250 {
255 251 // Generate gradients in HSV color space
256 252 foreach (QColor color, m_seriesColors) {
257 253 QLinearGradient g;
258 254 qreal h = color.hsvHueF();
259 255 qreal s = color.hsvSaturationF();
260 256
261 257 // TODO: tune the algorithm to give nice results with most base colors defined in
262 258 // most themes. The rest of the gradients we can define manually in theme specific
263 259 // implementation.
264 260 QColor start = color;
265 261 start.setHsvF(h, 0.05, 0.95);
266 262 g.setColorAt(0.0, start);
267 263
268 264 g.setColorAt(0.5, color);
269 265
270 266 QColor end = color;
271 267 end.setHsvF(h, s, 0.25);
272 268 g.setColorAt(1.0, end);
273 269
274 270 m_seriesGradients << g;
275 271 }
276 272 }
277 273
278 274
279 275 QColor ChartTheme::colorAt(const QColor &start, const QColor &end, qreal pos)
280 276 {
281 277 Q_ASSERT(pos >=0.0 && pos <= 1.0);
282 278 qreal r = start.redF() + ((end.redF() - start.redF()) * pos);
283 279 qreal g = start.greenF() + ((end.greenF() - start.greenF()) * pos);
284 280 qreal b = start.blueF() + ((end.blueF() - start.blueF()) * pos);
285 281 QColor c;
286 282 c.setRgbF(r, g, b);
287 283 return c;
288 284 }
289 285
290 286 QColor ChartTheme::colorAt(const QGradient &gradient, qreal pos)
291 287 {
292 288 Q_ASSERT(pos >=0 && pos <= 1.0);
293 289
294 290 // another possibility:
295 291 // http://stackoverflow.com/questions/3306786/get-intermediate-color-from-a-gradient
296 292
297 293 QGradientStops stops = gradient.stops();
298 294 int count = stops.count();
299 295
300 296 // find previous stop relative to position
301 297 QGradientStop prev = stops.first();
302 298 for (int i=0; i<count; i++) {
303 299 QGradientStop stop = stops.at(i);
304 300 if (pos > stop.first)
305 301 prev = stop;
306 302
307 303 // given position is actually a stop position?
308 304 if (pos == stop.first) {
309 305 //qDebug() << "stop color" << pos;
310 306 return stop.second;
311 307 }
312 308 }
313 309
314 310 // find next stop relative to position
315 311 QGradientStop next = stops.last();
316 312 for (int i=count-1; i>=0; i--) {
317 313 QGradientStop stop = stops.at(i);
318 314 if (pos < stop.first)
319 315 next = stop;
320 316 }
321 317
322 318 //qDebug() << "prev" << prev.first << "pos" << pos << "next" << next.first;
323 319
324 320 qreal range = next.first - prev.first;
325 321 qreal posDelta = pos - prev.first;
326 322 qreal relativePos = posDelta / range;
327 323
328 324 //qDebug() << "range" << range << "posDelta" << posDelta << "relativePos" << relativePos;
329 325
330 326 return colorAt(prev.second, next.second, relativePos);
331 327 }
332 328
333 329 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,88 +1,88
1 1 #include "linechartitem_p.h"
2 2 #include "qlineseries.h"
3 3 #include "chartpresenter_p.h"
4 4 #include <QPainter>
5 5 #include <QGraphicsSceneMouseEvent>
6 6
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 //TODO: optimize : remove points which are not visible
11 11
12 12 LineChartItem::LineChartItem(QLineSeries* series,QGraphicsItem *parent):XYChartItem(series,parent),
13 13 m_series(series),
14 14 m_pointsVisible(false)
15 15 {
16 16 setZValue(ChartPresenter::LineChartZValue);
17 17 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
18 18 QObject::connect(this,SIGNAL(clicked(const QPointF&)),series,SIGNAL(clicked(const QPointF&)));
19 19 handleUpdated();
20 20 }
21 21
22 22 QRectF LineChartItem::boundingRect() const
23 23 {
24 24 return m_rect;
25 25 }
26 26
27 27 QPainterPath LineChartItem::shape() const
28 28 {
29 29 return m_path;
30 30 }
31 31
32 void LineChartItem::setGeometry(QVector<QPointF>& points)
32 void LineChartItem::setLayout(QVector<QPointF>& points)
33 33 {
34 34 if(points.size()==0)
35 35 {
36 XYChartItem::setGeometry(points);
36 XYChartItem::setLayout(points);
37 37 return;
38 38 }
39 39
40 40 QList<QGraphicsItem*> items = m_items.childItems();
41 41
42 42 QPainterPath linePath(points.at(0));
43 43
44 44 for(int i=1; i< points.size();i++) {
45 45 linePath.lineTo(points.at(i));
46 46 }
47 47
48 48 prepareGeometryChange();
49 49 m_path = linePath;
50 50 m_rect = linePath.boundingRect();
51 51
52 XYChartItem::setGeometry(points);
52 XYChartItem::setLayout(points);
53 53 }
54 54
55 55 void LineChartItem::handleUpdated()
56 56 {
57 57 m_pointsVisible = m_series->pointsVisible();
58 58 m_linePen = m_series->pen();
59 59 m_pointPen = m_series->pen();
60 60 m_pointPen.setWidthF(2*m_pointPen.width());
61 61 update();
62 62 }
63 63
64 64 //painter
65 65
66 66 void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
67 67 {
68 68 Q_UNUSED(widget);
69 69 Q_UNUSED(option);
70 70 painter->save();
71 71 painter->setPen(m_linePen);
72 72 painter->setClipRect(clipRect());
73 73 painter->drawPath(m_path);
74 74 if(m_pointsVisible){
75 75 painter->setPen(m_pointPen);
76 76 painter->drawPoints(points());
77 77 }
78 78 painter->restore();
79 79 }
80 80
81 81 void LineChartItem::mousePressEvent( QGraphicsSceneMouseEvent * event )
82 82 {
83 83 emit clicked(calculateDomainPoint(event->pos()));
84 84 }
85 85
86 86 #include "moc_linechartitem_p.cpp"
87 87
88 88 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,47 +1,47
1 1 #ifndef LINECHARTITEM_H
2 2 #define LINECHARTITEM_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "xychartitem_p.h"
6 6 #include <QPen>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class QLineSeries;
11 11
12 12 class LineChartItem : public XYChartItem
13 13 {
14 14 Q_OBJECT
15 15 public:
16 16 explicit LineChartItem(QLineSeries* series,QGraphicsItem *parent = 0);
17 17 ~ LineChartItem(){};
18 18
19 19 //from QGraphicsItem
20 20 QRectF boundingRect() const;
21 21 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
22 22 QPainterPath shape() const;
23 23
24 24 public slots:
25 25 void handleUpdated();
26 26
27 27 signals:
28 28 void clicked(const QPointF& point);
29 29
30 30 protected:
31 virtual void setGeometry(QVector<QPointF>& points);
31 void setLayout(QVector<QPointF>& points);
32 32 void mousePressEvent( QGraphicsSceneMouseEvent * event );
33 33
34 34 private:
35 35 QLineSeries* m_series;
36 36 QGraphicsItemGroup m_items;
37 37 QPainterPath m_path;
38 38 QRectF m_rect;
39 39 QPen m_linePen;
40 40 QPen m_pointPen;
41 41 bool m_pointsVisible;
42 42
43 43 };
44 44
45 45 QTCOMMERCIALCHART_END_NAMESPACE
46 46
47 47 #endif
@@ -1,181 +1,181
1 1 #include "scatterchartitem_p.h"
2 2 #include "qscatterseries.h"
3 3 #include "chartpresenter_p.h"
4 4 #include <QPainter>
5 5 #include <QGraphicsScene>
6 6
7 7 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 8
9 9
10 10
11 11
12 12
13 13
14 14
15 15 ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent) :
16 16 XYChartItem(series,parent),
17 17 m_series(series),
18 18 m_items(this),
19 19 m_shape(QScatterSeries::MarkerShapeRectangle),
20 20 m_size(10)
21 21
22 22 {
23 23 Q_ASSERT(parent);
24 24 Q_ASSERT(series);
25 25
26 26 QObject::connect(m_series,SIGNAL(updated()), this, SLOT(handleUpdated()));
27 27 QObject::connect(this,SIGNAL(clicked(const QPointF&)), m_series, SIGNAL(clicked(const QPointF&)));
28 28
29 29 setZValue(ChartPresenter::ScatterSeriesZValue);
30 30 setFlags(QGraphicsItem::ItemHasNoContents);
31 31 setFlags(QGraphicsItem::ItemClipsChildrenToShape);
32 32
33 33 handleUpdated();
34 34
35 35 m_items.setHandlesChildEvents(false);
36 36
37 37 // TODO: how to draw a drop shadow?
38 38 // QGraphicsDropShadowEffect *dropShadow = new QGraphicsDropShadowEffect();
39 39 // dropShadow->setOffset(2.0);
40 40 // dropShadow->setBlurRadius(2.0);
41 41 // setGraphicsEffect(dropShadow);
42 42 }
43 43
44 44
45 45 QRectF ScatterChartItem::boundingRect() const
46 46 {
47 47 return m_rect;
48 48 }
49 49
50 50 void ScatterChartItem::createPoints(int count)
51 51 {
52 52 for (int i = 0; i < count; ++i) {
53 53
54 54 QGraphicsItem *item;
55 55
56 56 switch (m_shape) {
57 57 case QScatterSeries::MarkerShapeCircle:{
58 58 QGraphicsEllipseItem* i = new QGraphicsEllipseItem(0,0,m_size,m_size);
59 59 const QRectF& rect = i->boundingRect();
60 60 i->setPos(-rect.width()/2,-rect.height()/2);
61 61 item = new Marker(i,this);
62 62 break;
63 63 }
64 64 case QScatterSeries::MarkerShapeRectangle:{
65 65 QGraphicsRectItem* i = new QGraphicsRectItem(0,0,m_size,m_size);
66 66 i->setPos(-m_size/2,-m_size/2);
67 67 item = new Marker(i,this);
68 68 break;
69 69 }
70 70 default:
71 71 qWarning()<<"Unsupported marker type";
72 72 break;
73 73
74 74 }
75 75 m_items.addToGroup(item);
76 76 }
77 77 }
78 78
79 79 void ScatterChartItem::deletePoints(int count)
80 80 {
81 81 QList<QGraphicsItem *> items = m_items.childItems();
82 82
83 83 for (int i = 0; i < count; ++i) {
84 84 delete(items.takeLast());
85 85 }
86 86 }
87 87
88 88 void ScatterChartItem::markerSelected(Marker* marker)
89 89 {
90 90 emit clicked(QPointF(m_series->x(marker->index()), m_series->y(marker->index())));
91 91 }
92 92
93 void ScatterChartItem::setGeometry(QVector<QPointF>& points)
93 void ScatterChartItem::setLayout(QVector<QPointF>& points)
94 94 {
95 95 if(points.size()==0)
96 96 {
97 XYChartItem::setGeometry(points);
97 XYChartItem::setLayout(points);
98 98 return;
99 99 }
100 100
101 101 int diff = XYChartItem::points().size() - points.size();
102 102
103 103 if(diff>0) {
104 104 deletePoints(diff);
105 105 }
106 106 else if(diff<0) {
107 107 createPoints(-diff);
108 108 }
109 109
110 110 if(diff!=0) handleUpdated();
111 111
112 112 QList<QGraphicsItem*> items = m_items.childItems();
113 113
114 114 for(int i=0; i< points.size();i++) {
115 115 Marker* item = static_cast<Marker*>(items.at(i));
116 116 const QPointF& point = points.at(i);
117 117 const QRectF& rect = item->boundingRect();
118 118 item->setIndex(i);
119 119 item->setPos(point.x()-rect.width()/2,point.y()-rect.height()/2);
120 120 if(!clipRect().contains(point)) {
121 121 item->setVisible(false);
122 122 }
123 123 else {
124 124 item->setVisible(true);
125 125 }
126 126 }
127 127
128 128 prepareGeometryChange();
129 129 m_rect = clipRect();
130 XYChartItem::setGeometry(points);
130 XYChartItem::setLayout(points);
131 131 }
132 132
133 133
134 134 void ScatterChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
135 135 {
136 136 Q_UNUSED(painter);
137 137 Q_UNUSED(option);
138 138 Q_UNUSED(widget);
139 139 }
140 140
141 141 void ScatterChartItem::setPen(const QPen& pen)
142 142 {
143 143 foreach(QGraphicsItem* item , m_items.childItems()) {
144 144 static_cast<Marker*>(item)->setPen(pen);
145 145 }
146 146 }
147 147
148 148 void ScatterChartItem::setBrush(const QBrush& brush)
149 149 {
150 150 foreach(QGraphicsItem* item , m_items.childItems()) {
151 151 static_cast<Marker*>(item)->setBrush(brush);
152 152 }
153 153 }
154 154
155 155 void ScatterChartItem::handleUpdated()
156 156 {
157 157
158 158 int count = m_items.childItems().count();
159 159
160 160 if(count==0) return;
161 161
162 162 bool recreate = m_size != m_series->size() || m_shape != m_series->shape();
163 163
164 164 //TODO: only rewrite on size change
165 165
166 166 m_size = m_series->size();
167 167 m_shape = m_series->shape();
168 168
169 169 if(recreate){
170 170 deletePoints(count);
171 171 createPoints(count);
172 172 }
173 173
174 174 setPen(m_series->pen());
175 175 setBrush(m_series->brush());
176 176
177 177 }
178 178
179 179 #include "moc_scatterchartitem_p.cpp"
180 180
181 181 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,122 +1,122
1 1 #ifndef SCATTERPRESENTER_H
2 2 #define SCATTERPRESENTER_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "xychartitem_p.h"
6 6 #include <QGraphicsEllipseItem>
7 7 #include <QPen>
8 8
9 9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 10
11 11 class QScatterSeries;
12 12 class Marker;
13 13
14 14 class ScatterChartItem : public XYChartItem
15 15 {
16 16 Q_OBJECT
17 17 public:
18 18 explicit ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent = 0);
19 19
20 20 public:
21 21 //from QGraphicsItem
22 22 QRectF boundingRect() const;
23 23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
24 24
25 25 void setPen(const QPen& pen);
26 26 void setBrush(const QBrush& brush);
27 27
28 28 void markerSelected(Marker* item);
29 29
30 30 signals:
31 31 void clicked(const QPointF& point);
32 32
33 33 public slots:
34 34 void handleUpdated();
35 35
36 36 private:
37 37 void createPoints(int count);
38 38 void deletePoints(int count);
39 39
40 40 protected:
41 virtual void setGeometry(QVector<QPointF>& points);
41 void setLayout(QVector<QPointF>& points);
42 42
43 43 private:
44 44 QScatterSeries *m_series;
45 45 QGraphicsItemGroup m_items;
46 46 int m_shape;
47 47 int m_size;
48 48 QRectF m_rect;
49 49
50 50 };
51 51
52 52
53 53 class Marker: public QAbstractGraphicsShapeItem
54 54 {
55 55
56 56 public:
57 57
58 58 Marker(QAbstractGraphicsShapeItem* item , ScatterChartItem* parent):QAbstractGraphicsShapeItem(0),m_item(item),m_parent(parent)
59 59 {
60 60 };
61 61
62 62 ~Marker()
63 63 {
64 64 delete m_item;
65 65 }
66 66
67 67 void setIndex(int index)
68 68 {
69 69 m_index=index;
70 70 }
71 71
72 72 int index() const
73 73 {
74 74 return m_index;
75 75 }
76 76
77 77 QPainterPath shape() const
78 78 {
79 79 return m_item->shape();
80 80 }
81 81
82 82 QRectF boundingRect() const
83 83 {
84 84 return m_item->boundingRect();
85 85 }
86 86
87 87 bool contains(const QPointF &point) const
88 88 {
89 89 return m_item->contains(point);
90 90 }
91 91
92 92 void setPen(const QPen& pen)
93 93 {
94 94 m_item->setPen(pen);
95 95 }
96 96
97 97 void setBrush(const QBrush& brush)
98 98 {
99 99 m_item->setBrush(brush);
100 100 }
101 101
102 102 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
103 103 {
104 104 m_item->paint(painter,option,widget);
105 105 }
106 106
107 107 protected:
108 108
109 109 void mousePressEvent( QGraphicsSceneMouseEvent * event )
110 110 {
111 111 m_parent->markerSelected(this);
112 112 }
113 113
114 114 private:
115 115 QAbstractGraphicsShapeItem* m_item;
116 116 ScatterChartItem* m_parent;
117 117 int m_index;
118 118 };
119 119
120 120 QTCOMMERCIALCHART_END_NAMESPACE
121 121
122 122 #endif // SCATTERPRESENTER_H
@@ -1,106 +1,106
1 1 #include "splinechartitem_p.h"
2 2 #include "chartpresenter_p.h"
3 3 #include <QPainter>
4 4
5 5 QTCOMMERCIALCHART_BEGIN_NAMESPACE
6 6
7 7 SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphicsItem *parent) :
8 8 XYChartItem(series, parent),
9 9 m_series(series)
10 10 {
11 11 setZValue(ChartPresenter::LineChartZValue);
12 12 QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated()));
13 13 handleUpdated();
14 14 }
15 15
16 16 QRectF SplineChartItem::boundingRect() const
17 17 {
18 18 return m_rect;
19 19 }
20 20
21 21 QPainterPath SplineChartItem::shape() const
22 22 {
23 23 return m_path;
24 24 }
25 25
26 26 QPointF SplineChartItem::calculateGeometryControlPoint(int index) const
27 27 {
28 28 return XYChartItem::calculateGeometryPoint(m_series->controlPoint(index));
29 29 }
30 30
31 void SplineChartItem::setGeometry(QVector<QPointF>& points)
31 void SplineChartItem::setLayout(QVector<QPointF>& points)
32 32 {
33 33
34 34 if(points.size()==0)
35 35 {
36 XYChartItem::setGeometry(points);
36 XYChartItem::setLayout(points);
37 37 return;
38 38 }
39 39
40 40 QPainterPath splinePath;
41 41 const QPointF& point = points.at(0);
42 42 splinePath.moveTo(point);
43 43
44 44 for (int i = 0; i < points.size() - 1; i++)
45 45 {
46 46 const QPointF& point = points.at(i + 1);
47 47 splinePath.cubicTo(calculateGeometryControlPoint(2 * i), calculateGeometryControlPoint(2 * i + 1), point);
48 48 }
49 49
50 50 prepareGeometryChange();
51 51 m_path = splinePath;
52 52 m_rect = splinePath.boundingRect();
53 XYChartItem::setGeometry(points);
53 XYChartItem::setLayout(points);
54 54 }
55 55
56 56 void SplineChartItem::setLinePen(const QPen& pen)
57 57 {
58 58 m_pen = pen;
59 59 }
60 60
61 61 //handlers
62 62
63 63 void SplineChartItem::handleUpdated()
64 64 {
65 65 //m_items.setVisible(m_series->pointsVisible());
66 66 setLinePen(m_series->pen());
67 67 update();
68 68 }
69 69
70 70 //painter
71 71
72 72 void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
73 73 {
74 74 Q_UNUSED(widget);
75 75 Q_UNUSED(option);
76 76 painter->save();
77 77 painter->setClipRect(clipRect());
78 78 painter->setPen(m_pen);
79 79 painter->drawPath(m_path);
80 80
81 81 const QVector<QPointF> points = XYChartItem::points();
82 82
83 83 for (int i = 0; i < points.size() - 1; i++)
84 84 {
85 85 painter->setPen(Qt::red);
86 86 painter->drawEllipse(points[i], 2, 2);
87 87
88 88 painter->setPen(Qt::blue);
89 89 // painter->drawLine(m_series->at(i), m_series->controlPoint(2 * i));
90 90 // painter->drawLine(m_series->at(i + 1), m_series->controlPoint(2 * i + 1));
91 91 // painter->drawEllipse(calculateGeometryControlPoint(2 * i), 4, 4);
92 92 // painter->drawEllipse(calculateGeometryControlPoint(2 * i + 1), 4, 4);
93 93 }
94 94 if (points.count() > 0)
95 95 {
96 96 painter->setPen(Qt::red);
97 97 painter->drawEllipse(points[points.count() - 1], 2, 2);
98 98 }
99 99 painter->restore();
100 100 }
101 101
102 102
103 103
104 104 #include "moc_splinechartitem_p.cpp"
105 105
106 106 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,43 +1,43
1 1 #ifndef SPLINECHARTITEM_P_H
2 2 #define SPLINECHARTITEM_P_H
3 3
4 4 #include "qsplineseries.h"
5 5 #include "xychartitem_p.h"
6 6 #include <QGraphicsItem>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class SplineChartItem : public XYChartItem
11 11 {
12 12 Q_OBJECT
13 13 public:
14 14 SplineChartItem(QSplineSeries* series, QGraphicsItem *parent = 0);
15 15
16 16 //from QGraphicsItem
17 17 QRectF boundingRect() const;
18 18 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
19 19 QPainterPath shape() const;
20 20
21 21 void setLinePen(const QPen& pen);
22 22 void setPointsVisible(bool visible);
23 23
24 24 public slots:
25 25 void handleUpdated();
26 26
27 27 protected:
28 void setGeometry(QVector<QPointF>& points);
28 void setLayout(QVector<QPointF>& points);
29 29
30 30 private:
31 31 QPointF calculateGeometryControlPoint(int index) const;
32 32
33 33 private:
34 34 QSplineSeries* m_series;
35 35 QPainterPath m_path;
36 36 QRectF m_rect;
37 37 QPen m_pen;
38 38
39 39 };
40 40
41 41 QTCOMMERCIALCHART_END_NAMESPACE
42 42
43 43 #endif // SPLINECHARTITEM_P_H
@@ -1,156 +1,156
1 1 #include "xychartitem_p.h"
2 2 #include "qxyseries.h"
3 3 #include "chartpresenter_p.h"
4 4 #include "chartanimator_p.h"
5 5 #include <QPainter>
6 6
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 //TODO: optimize : remove points which are not visible
11 11
12 12 XYChartItem::XYChartItem(QXYSeries* series, QGraphicsItem *parent):ChartItem(parent),
13 13 m_minX(0),
14 14 m_maxX(0),
15 15 m_minY(0),
16 16 m_maxY(0),
17 17 m_series(series)
18 18 {
19 19 QObject::connect(series,SIGNAL(pointReplaced(int)),this,SLOT(handlePointReplaced(int)));
20 20 QObject::connect(series,SIGNAL(pointAdded(int)),this,SLOT(handlePointAdded(int)));
21 21 QObject::connect(series,SIGNAL(pointRemoved(int)),this,SLOT(handlePointRemoved(int)));
22 22
23 23 }
24 24
25 25 QPointF XYChartItem::calculateGeometryPoint(const QPointF& point) const
26 26 {
27 27 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
28 28 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
29 29 qreal x = (point.x() - m_minX)* deltaX;
30 30 qreal y = (point.y() - m_minY)*-deltaY + m_size.height();
31 31 return QPointF(x,y);
32 32 }
33 33
34 34
35 35 QPointF XYChartItem::calculateGeometryPoint(int index) const
36 36 {
37 37 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
38 38 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
39 39 qreal x = (m_series->x(index) - m_minX)* deltaX;
40 40 qreal y = (m_series->y(index) - m_minY)*-deltaY + m_size.height();
41 41 return QPointF(x,y);
42 42 }
43 43
44 44 QVector<QPointF> XYChartItem::calculateGeometryPoints() const
45 45 {
46 46 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
47 47 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
48 48
49 49 QVector<QPointF> points;
50 50 points.reserve(m_series->count());
51 51 for (int i = 0; i < m_series->count(); ++i) {
52 52 qreal x = (m_series->x(i) - m_minX)* deltaX;
53 53 qreal y = (m_series->y(i) - m_minY)*-deltaY + m_size.height();
54 54 points << QPointF(x,y);
55 55 }
56 56 return points;
57 57 }
58 58
59 59 QPointF XYChartItem::calculateDomainPoint(const QPointF& point) const
60 60 {
61 61 const qreal deltaX = m_size.width()/(m_maxX-m_minX);
62 62 const qreal deltaY = m_size.height()/(m_maxY-m_minY);
63 63 qreal x = point.x()/deltaX +m_minX;
64 64 qreal y = (point.y()-m_size.height())/(-deltaY)+ m_minY;
65 65 return QPointF(x,y);
66 66 }
67 67
68 void XYChartItem::updatePoints(QVector<QPointF>& points)
68 void XYChartItem::applyLayout(QVector<QPointF>& points)
69 69 {
70 70 if(m_animator){
71 71 m_animator->applyLayout(this,points);
72 72 }
73 else setGeometry(points);
73 else setLayout(points);
74 74
75 75 }
76 76
77 void XYChartItem::updatePoint(QVector<QPointF>& points)
77 void XYChartItem::updateLayout(QVector<QPointF>& points)
78 78 {
79 setGeometry(points);
79 setLayout(points);
80 80 }
81 81
82 void XYChartItem::setGeometry(QVector<QPointF>& points)
82 void XYChartItem::setLayout(QVector<QPointF>& points)
83 83 {
84 84 m_points = points;
85 85 }
86 86
87 87 //handlers
88 88
89 89 void XYChartItem::handlePointAdded(int index)
90 90 {
91 91 Q_ASSERT(index<m_series->count());
92 92 Q_ASSERT(index>=0);
93 93
94 94 QPointF point = calculateGeometryPoint(index);
95 95 QVector<QPointF> points = m_points;
96 96 points.insert(index,point);
97 updatePoints(points);
97 updateLayout(points);
98 98 update();
99 99 }
100 100 void XYChartItem::handlePointRemoved(int index)
101 101 {
102 102 Q_ASSERT(index<m_series->count() + 1);
103 103 Q_ASSERT(index>=0);
104 104 // QPointF point = calculateGeometryPoint(index);
105 105 QVector<QPointF> points = m_points;
106 106 points.remove(index);
107 updatePoints(points);
107 updateLayout(points);
108 108 update();
109 109 }
110 110
111 111 void XYChartItem::handlePointReplaced(int index)
112 112 {
113 113 Q_ASSERT(index<m_series->count());
114 114 Q_ASSERT(index>=0);
115 115 QPointF point = calculateGeometryPoint(index);
116 116 QVector<QPointF> points = m_points;
117 117 points.replace(index,point);
118 updatePoint(points);
118 updateLayout(points);
119 119 update();
120 120 }
121 121
122 122 void XYChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
123 123 {
124 124 m_minX=minX;
125 125 m_maxX=maxX;
126 126 m_minY=minY;
127 127 m_maxY=maxY;
128 128
129 129 if(isEmpty()) return;
130 130 QVector<QPointF> points = calculateGeometryPoints();
131 updatePoints(points);
131 applyLayout(points);
132 132 update();
133 133 }
134 134
135 135 void XYChartItem::handleGeometryChanged(const QRectF& rect)
136 136 {
137 137 Q_ASSERT(rect.isValid());
138 138 m_size=rect.size();
139 139 m_clipRect=rect.translated(-rect.topLeft());
140 140 setPos(rect.topLeft());
141 141
142 142 if(isEmpty()) return;
143 143 QVector<QPointF> points = calculateGeometryPoints();
144 updatePoints(points);
144 applyLayout(points);
145 145 update();
146 146 }
147 147
148 148
149 149 bool XYChartItem::isEmpty()
150 150 {
151 151 return !m_clipRect.isValid() || m_maxX - m_minX == 0 || m_maxY - m_minY ==0 ;
152 152 }
153 153
154 154 #include "moc_xychartitem_p.cpp"
155 155
156 156 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,58 +1,58
1 1 #ifndef XYCHARTITEM_H
2 2 #define XYCHARTITEM_H
3 3
4 4 #include "qchartglobal.h"
5 5 #include "chartitem_p.h"
6 6 #include <QPen>
7 7
8 8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 9
10 10 class ChartPresenter;
11 11 class QXYSeries;
12 12
13 13 class XYChartItem : public QObject , public ChartItem
14 14 {
15 15 Q_OBJECT
16 16 public:
17 17 explicit XYChartItem(QXYSeries* series, QGraphicsItem *parent = 0);
18 18 ~ XYChartItem(){};
19 19
20 20 QVector<QPointF> points() const {return m_points;}
21 21 QRectF clipRect() const { return m_clipRect;}
22 22
23 23 public slots:
24 24 void handlePointAdded(int index);
25 25 void handlePointRemoved(int index);
26 26 void handlePointReplaced(int index);
27 27 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
28 28 void handleGeometryChanged(const QRectF& size);
29 29
30 30 protected:
31 virtual void setGeometry(QVector<QPointF>& points);
31 virtual void setLayout(QVector<QPointF>& points);
32 32 QPointF calculateGeometryPoint(const QPointF& point) const;
33 33 QPointF calculateGeometryPoint(int index) const;
34 34 QPointF calculateDomainPoint(const QPointF& point) const;
35 35 QVector<QPointF> calculateGeometryPoints() const;
36 36
37 37 private:
38 void updatePoints(QVector<QPointF>& points);
39 void updatePoint(QVector<QPointF>& points);
38 void applyLayout(QVector<QPointF>& points);
39 void updateLayout(QVector<QPointF>& points);
40 40 inline bool isEmpty();
41 41
42 42 private:
43 43 qreal m_minX;
44 44 qreal m_maxX;
45 45 qreal m_minY;
46 46 qreal m_maxY;
47 47 QXYSeries* m_series;
48 48 QSizeF m_size;
49 49 QRectF m_clipRect;
50 50 QVector<QPointF> m_points;
51 51
52 52 friend class XYAnimation;
53 53
54 54 };
55 55
56 56 QTCOMMERCIALCHART_END_NAMESPACE
57 57
58 58 #endif
General Comments 0
You need to be logged in to leave comments. Login now