##// END OF EJS Templates
Rename axisLabelsChanged to axisRangeChanged
Michal Klocek -
r399:91816cf9fc5c
parent child
Show More
@@ -1,340 +1,340
1 #include "axisitem_p.h"
1 #include "axisitem_p.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include <QPainter>
4 #include <QPainter>
5 #include <QDebug>
5 #include <QDebug>
6
6
7 static int label_padding = 5;
7 static int label_padding = 5;
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) :
11 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) :
12 ChartItem(parent),
12 ChartItem(parent),
13 m_type(type),
13 m_type(type),
14 m_labelsAngle(0),
14 m_labelsAngle(0),
15 m_grid(parent),
15 m_grid(parent),
16 m_shades(parent),
16 m_shades(parent),
17 m_labels(parent),
17 m_labels(parent),
18 m_axis(parent)
18 m_axis(parent)
19 {
19 {
20 //initial initialization
20 //initial initialization
21 m_axis.setZValue(ChartPresenter::AxisZValue);
21 m_axis.setZValue(ChartPresenter::AxisZValue);
22 m_shades.setZValue(ChartPresenter::ShadesZValue);
22 m_shades.setZValue(ChartPresenter::ShadesZValue);
23 m_grid.setZValue(ChartPresenter::GridZValue);
23 m_grid.setZValue(ChartPresenter::GridZValue);
24 setFlags(QGraphicsItem::ItemHasNoContents);
24 setFlags(QGraphicsItem::ItemHasNoContents);
25 }
25 }
26
26
27 AxisItem::~AxisItem()
27 AxisItem::~AxisItem()
28 {
28 {
29 }
29 }
30
30
31 QRectF AxisItem::boundingRect() const
31 QRectF AxisItem::boundingRect() const
32 {
32 {
33 return QRectF();
33 return QRectF();
34 }
34 }
35
35
36 void AxisItem::createItems(int count)
36 void AxisItem::createItems(int count)
37 {
37 {
38 if(m_axis.children().size()==0)
38 if(m_axis.children().size()==0)
39 m_axis.addToGroup(new QGraphicsLineItem());
39 m_axis.addToGroup(new QGraphicsLineItem());
40 for (int i = 0; i < count; ++i) {
40 for (int i = 0; i < count; ++i) {
41 m_grid.addToGroup(new QGraphicsLineItem());
41 m_grid.addToGroup(new QGraphicsLineItem());
42 m_labels.addToGroup(new QGraphicsSimpleTextItem());
42 m_labels.addToGroup(new QGraphicsSimpleTextItem());
43 if(m_grid.childItems().size()%2) m_shades.addToGroup(new QGraphicsRectItem());
43 if(m_grid.childItems().size()%2) m_shades.addToGroup(new QGraphicsRectItem());
44 m_axis.addToGroup(new QGraphicsLineItem());
44 m_axis.addToGroup(new QGraphicsLineItem());
45 }
45 }
46 }
46 }
47
47
48 void AxisItem::clear(int count)
48 void AxisItem::clear(int count)
49 {
49 {
50 QList<QGraphicsItem *> lines = m_grid.childItems();
50 QList<QGraphicsItem *> lines = m_grid.childItems();
51 QList<QGraphicsItem *> labels = m_labels.childItems();
51 QList<QGraphicsItem *> labels = m_labels.childItems();
52 QList<QGraphicsItem *> shades = m_shades.childItems();
52 QList<QGraphicsItem *> shades = m_shades.childItems();
53 QList<QGraphicsItem *> axis = m_axis.childItems();
53 QList<QGraphicsItem *> axis = m_axis.childItems();
54
54
55 for (int i = 0; i < count; ++i) {
55 for (int i = 0; i < count; ++i) {
56 delete(lines.takeLast());
56 delete(lines.takeLast());
57 delete(labels.takeLast());
57 delete(labels.takeLast());
58 if(lines.size()%2) delete(shades.takeLast());
58 if(lines.size()%2) delete(shades.takeLast());
59 delete(axis.takeLast());
59 delete(axis.takeLast());
60 }
60 }
61 }
61 }
62
62
63 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
63 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
64 {
64 {
65 Q_UNUSED(painter);
65 Q_UNUSED(painter);
66 Q_UNUSED(option);
66 Q_UNUSED(option);
67 Q_UNUSED(widget);
67 Q_UNUSED(widget);
68 }
68 }
69
69
70 void AxisItem::updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout)
70 void AxisItem::updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout)
71 {
71 {
72 if(newLayout.count()==0) return;
72 if(newLayout.count()==0) return;
73 applyLayout(newLayout);
73 applyLayout(newLayout);
74 oldLayout=newLayout;
74 oldLayout=newLayout;
75 }
75 }
76
76
77 void AxisItem::handleAxisUpdate(QChartAxis* axis)
77 void AxisItem::handleAxisUpdate(QChartAxis* axis)
78 {
78 {
79 if(m_layoutVector.count()==0) return;
79 if(m_layoutVector.count()==0) return;
80
80
81 if(axis->isAxisVisible()) {
81 if(axis->isAxisVisible()) {
82 setAxisOpacity(100);
82 setAxisOpacity(100);
83 }
83 }
84 else {
84 else {
85 setAxisOpacity(0);
85 setAxisOpacity(0);
86 }
86 }
87
87
88 if(axis->isGridVisible()) {
88 if(axis->isGridVisible()) {
89 setGridOpacity(100);
89 setGridOpacity(100);
90 }
90 }
91 else {
91 else {
92 setGridOpacity(0);
92 setGridOpacity(0);
93 }
93 }
94
94
95 if(axis->labelsVisible())
95 if(axis->labelsVisible())
96 {
96 {
97 setLabelsOpacity(100);
97 setLabelsOpacity(100);
98 }
98 }
99 else {
99 else {
100 setLabelsOpacity(0);
100 setLabelsOpacity(0);
101 }
101 }
102
102
103 if(axis->shadesVisible()) {
103 if(axis->shadesVisible()) {
104 setShadesOpacity(axis->shadesOpacity());
104 setShadesOpacity(axis->shadesOpacity());
105 }
105 }
106 else {
106 else {
107 setShadesOpacity(0);
107 setShadesOpacity(0);
108 }
108 }
109
109
110 setLabelsAngle(axis->labelsAngle());
110 setLabelsAngle(axis->labelsAngle());
111 setAxisPen(axis->axisPen());
111 setAxisPen(axis->axisPen());
112 setLabelsPen(axis->labelsPen());
112 setLabelsPen(axis->labelsPen());
113 setLabelsBrush(axis->labelsBrush());
113 setLabelsBrush(axis->labelsBrush());
114 setLabelsFont(axis->labelsFont());
114 setLabelsFont(axis->labelsFont());
115 setGridPen(axis->gridPen());
115 setGridPen(axis->gridPen());
116 setShadesPen(axis->shadesPen());
116 setShadesPen(axis->shadesPen());
117 setShadesBrush(axis->shadesBrush());
117 setShadesBrush(axis->shadesBrush());
118 }
118 }
119
119
120 void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels)
120 void AxisItem::handleRangeChanged(QChartAxis* axis,const QStringList& labels)
121 {
121 {
122 int diff = m_thicksList.size() - labels.size();
122 int diff = m_thicksList.size() - labels.size();
123
123
124 if(diff>0){
124 if(diff>0){
125 clear(diff);
125 clear(diff);
126 }else if(diff<0){
126 }else if(diff<0){
127 createItems(-diff);
127 createItems(-diff);
128 }
128 }
129 m_thicksList=labels;
129 m_thicksList=labels;
130 QVector<qreal> vector = calculateLayout();
130 QVector<qreal> vector = calculateLayout();
131 updateItems(m_layoutVector,vector);
131 updateItems(m_layoutVector,vector);
132 if(diff!=0) handleAxisUpdate(axis);
132 if(diff!=0) handleAxisUpdate(axis);
133 }
133 }
134
134
135 void AxisItem::handleGeometryChanged(const QRectF& rect)
135 void AxisItem::handleGeometryChanged(const QRectF& rect)
136 {
136 {
137 m_rect = rect;
137 m_rect = rect;
138
138
139 if(m_thicksList.size()==0) return;
139 if(m_thicksList.size()==0) return;
140
140
141 QVector<qreal> vector = calculateLayout();
141 QVector<qreal> vector = calculateLayout();
142 updateItems(m_layoutVector,vector);
142 updateItems(m_layoutVector,vector);
143 }
143 }
144
144
145 void AxisItem::setAxisOpacity(qreal opacity)
145 void AxisItem::setAxisOpacity(qreal opacity)
146 {
146 {
147 m_axis.setOpacity(opacity);
147 m_axis.setOpacity(opacity);
148 }
148 }
149
149
150 qreal AxisItem::axisOpacity() const
150 qreal AxisItem::axisOpacity() const
151 {
151 {
152 return m_axis.opacity();
152 return m_axis.opacity();
153 }
153 }
154
154
155 void AxisItem::setGridOpacity(qreal opacity)
155 void AxisItem::setGridOpacity(qreal opacity)
156 {
156 {
157 m_grid.setOpacity(opacity);
157 m_grid.setOpacity(opacity);
158 }
158 }
159
159
160 qreal AxisItem::gridOpacity() const
160 qreal AxisItem::gridOpacity() const
161 {
161 {
162 return m_grid.opacity();
162 return m_grid.opacity();
163 }
163 }
164
164
165 void AxisItem::setLabelsOpacity(qreal opacity)
165 void AxisItem::setLabelsOpacity(qreal opacity)
166 {
166 {
167 m_labels.setOpacity(opacity);
167 m_labels.setOpacity(opacity);
168 }
168 }
169
169
170 qreal AxisItem::labelsOpacity() const
170 qreal AxisItem::labelsOpacity() const
171 {
171 {
172 return m_labels.opacity();
172 return m_labels.opacity();
173 }
173 }
174
174
175 void AxisItem::setShadesOpacity(qreal opacity)
175 void AxisItem::setShadesOpacity(qreal opacity)
176 {
176 {
177 m_shades.setOpacity(opacity);
177 m_shades.setOpacity(opacity);
178 }
178 }
179
179
180 qreal AxisItem::shadesOpacity() const
180 qreal AxisItem::shadesOpacity() const
181 {
181 {
182 return m_shades.opacity();
182 return m_shades.opacity();
183 }
183 }
184
184
185 void AxisItem::setLabelsAngle(int angle)
185 void AxisItem::setLabelsAngle(int angle)
186 {
186 {
187 foreach(QGraphicsItem* item , m_labels.childItems()) {
187 foreach(QGraphicsItem* item , m_labels.childItems()) {
188 QPointF center = item->boundingRect().center();
188 QPointF center = item->boundingRect().center();
189 item->setRotation(angle);
189 item->setRotation(angle);
190 }
190 }
191
191
192 m_labelsAngle=angle;
192 m_labelsAngle=angle;
193 }
193 }
194
194
195 void AxisItem::setLabelsPen(const QPen& pen)
195 void AxisItem::setLabelsPen(const QPen& pen)
196 {
196 {
197 foreach(QGraphicsItem* item , m_labels.childItems()) {
197 foreach(QGraphicsItem* item , m_labels.childItems()) {
198 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
198 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
199 }
199 }
200 }
200 }
201
201
202 void AxisItem::setLabelsBrush(const QBrush& brush)
202 void AxisItem::setLabelsBrush(const QBrush& brush)
203 {
203 {
204 foreach(QGraphicsItem* item , m_labels.childItems()) {
204 foreach(QGraphicsItem* item , m_labels.childItems()) {
205 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
205 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
206 }
206 }
207 }
207 }
208
208
209 void AxisItem::setLabelsFont(const QFont& font)
209 void AxisItem::setLabelsFont(const QFont& font)
210 {
210 {
211 foreach(QGraphicsItem* item , m_labels.childItems()) {
211 foreach(QGraphicsItem* item , m_labels.childItems()) {
212 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
212 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
213 }
213 }
214 }
214 }
215
215
216 void AxisItem::setShadesBrush(const QBrush& brush)
216 void AxisItem::setShadesBrush(const QBrush& brush)
217 {
217 {
218 foreach(QGraphicsItem* item , m_shades.childItems()) {
218 foreach(QGraphicsItem* item , m_shades.childItems()) {
219 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
219 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
220 }
220 }
221 }
221 }
222
222
223 void AxisItem::setShadesPen(const QPen& pen)
223 void AxisItem::setShadesPen(const QPen& pen)
224 {
224 {
225 foreach(QGraphicsItem* item , m_shades.childItems()) {
225 foreach(QGraphicsItem* item , m_shades.childItems()) {
226 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
226 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
227 }
227 }
228 }
228 }
229
229
230 void AxisItem::setAxisPen(const QPen& pen)
230 void AxisItem::setAxisPen(const QPen& pen)
231 {
231 {
232 foreach(QGraphicsItem* item , m_axis.childItems()) {
232 foreach(QGraphicsItem* item , m_axis.childItems()) {
233 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
233 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
234 }
234 }
235 }
235 }
236
236
237 void AxisItem::setGridPen(const QPen& pen)
237 void AxisItem::setGridPen(const QPen& pen)
238 {
238 {
239 foreach(QGraphicsItem* item , m_grid.childItems()) {
239 foreach(QGraphicsItem* item , m_grid.childItems()) {
240 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
240 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
241 }
241 }
242 }
242 }
243
243
244 QVector<qreal> AxisItem::calculateLayout() const
244 QVector<qreal> AxisItem::calculateLayout() const
245 {
245 {
246 QVector<qreal> points;
246 QVector<qreal> points;
247 points.resize(m_thicksList.size());
247 points.resize(m_thicksList.size());
248
248
249 switch (m_type)
249 switch (m_type)
250 {
250 {
251 case X_AXIS:
251 case X_AXIS:
252 {
252 {
253 const qreal deltaX = m_rect.width()/(m_thicksList.size()-1);
253 const qreal deltaX = m_rect.width()/(m_thicksList.size()-1);
254 for (int i = 0; i < m_thicksList.size(); ++i) {
254 for (int i = 0; i < m_thicksList.size(); ++i) {
255 int x = i * deltaX + m_rect.left();
255 int x = i * deltaX + m_rect.left();
256 points[i] = x;
256 points[i] = x;
257 }
257 }
258 }
258 }
259 break;
259 break;
260 case Y_AXIS:
260 case Y_AXIS:
261 {
261 {
262 const qreal deltaY = m_rect.height()/(m_thicksList.size()-1);
262 const qreal deltaY = m_rect.height()/(m_thicksList.size()-1);
263 for (int i = 0; i < m_thicksList.size(); ++i) {
263 for (int i = 0; i < m_thicksList.size(); ++i) {
264 int y = i * -deltaY + m_rect.bottom();
264 int y = i * -deltaY + m_rect.bottom();
265 points[i] = y;
265 points[i] = y;
266 }
266 }
267 }
267 }
268 break;
268 break;
269 }
269 }
270 return points;
270 return points;
271 }
271 }
272
272
273 void AxisItem::applyLayout(const QVector<qreal>& points)
273 void AxisItem::applyLayout(const QVector<qreal>& points)
274 {
274 {
275 Q_ASSERT(points.size() == m_thicksList.size());
275 Q_ASSERT(points.size() == m_thicksList.size());
276
276
277 QList<QGraphicsItem *> lines = m_grid.childItems();
277 QList<QGraphicsItem *> lines = m_grid.childItems();
278 QList<QGraphicsItem *> labels = m_labels.childItems();
278 QList<QGraphicsItem *> labels = m_labels.childItems();
279 QList<QGraphicsItem *> shades = m_shades.childItems();
279 QList<QGraphicsItem *> shades = m_shades.childItems();
280 QList<QGraphicsItem *> axis = m_axis.childItems();
280 QList<QGraphicsItem *> axis = m_axis.childItems();
281
281
282 Q_ASSERT(labels.size() == m_thicksList.size());
282 Q_ASSERT(labels.size() == m_thicksList.size());
283
283
284 switch (m_type)
284 switch (m_type)
285 {
285 {
286 case X_AXIS:
286 case X_AXIS:
287 {
287 {
288 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
288 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
289 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
289 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
290
290
291 for (int i = 0; i < points.size(); ++i) {
291 for (int i = 0; i < points.size(); ++i) {
292 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
292 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
293 lineItem->setLine(points[i], m_rect.top(), points[i], m_rect.bottom());
293 lineItem->setLine(points[i], m_rect.top(), points[i], m_rect.bottom());
294 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
294 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
295 labelItem->setText(m_thicksList.at(i));
295 labelItem->setText(m_thicksList.at(i));
296 QPointF center = labelItem->boundingRect().center();
296 QPointF center = labelItem->boundingRect().center();
297 labelItem->setTransformOriginPoint(center.x(), center.y());
297 labelItem->setTransformOriginPoint(center.x(), center.y());
298 labelItem->setPos(points[i] - center.x(), m_rect.bottom() + label_padding);
298 labelItem->setPos(points[i] - center.x(), m_rect.bottom() + label_padding);
299 if(i%2){
299 if(i%2){
300 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
300 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
301 rectItem->setRect(points[i],m_rect.top(),points[i+1]-points[i],m_rect.height());
301 rectItem->setRect(points[i],m_rect.top(),points[i+1]-points[i],m_rect.height());
302 }
302 }
303 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
303 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
304 lineItem->setLine(points[i],m_rect.bottom(),points[i],m_rect.bottom()+5);
304 lineItem->setLine(points[i],m_rect.bottom(),points[i],m_rect.bottom()+5);
305 }
305 }
306 }
306 }
307 break;
307 break;
308
308
309 case Y_AXIS:
309 case Y_AXIS:
310 {
310 {
311 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
311 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
312 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
312 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
313
313
314 for (int i = 0; i < points.size(); ++i) {
314 for (int i = 0; i < points.size(); ++i) {
315 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
315 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
316 lineItem->setLine(m_rect.left() , points[i], m_rect.right(), points[i]);
316 lineItem->setLine(m_rect.left() , points[i], m_rect.right(), points[i]);
317 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
317 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
318 labelItem->setText(m_thicksList.at(i));
318 labelItem->setText(m_thicksList.at(i));
319 QPointF center = labelItem->boundingRect().center();
319 QPointF center = labelItem->boundingRect().center();
320 labelItem->setTransformOriginPoint(center.x(), center.y());
320 labelItem->setTransformOriginPoint(center.x(), center.y());
321 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , points[i]-center.y());
321 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , points[i]-center.y());
322 if(i%2){
322 if(i%2){
323 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
323 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
324 rectItem->setRect(m_rect.left(),points[i],m_rect.width(),points[i]-points[i+1]);
324 rectItem->setRect(m_rect.left(),points[i],m_rect.width(),points[i]-points[i+1]);
325 }
325 }
326 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
326 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
327 lineItem->setLine(m_rect.left()-5,points[i],m_rect.left(),points[i]);
327 lineItem->setLine(m_rect.left()-5,points[i],m_rect.left(),points[i]);
328 }
328 }
329 }
329 }
330 break;
330 break;
331 default:
331 default:
332 qDebug()<<"Unknown axis type";
332 qDebug()<<"Unknown axis type";
333 break;
333 break;
334 }
334 }
335 }
335 }
336
336
337 //TODO "nice numbers algorithm"
337 //TODO "nice numbers algorithm"
338 #include "moc_axisitem_p.cpp"
338 #include "moc_axisitem_p.cpp"
339
339
340 QTCOMMERCIALCHART_END_NAMESPACE
340 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,79 +1,79
1 #ifndef AXISITEM_H_
1 #ifndef AXISITEM_H_
2 #define AXISITEM_H_
2 #define AXISITEM_H_
3
3
4 #include "domain_p.h"
4 #include "domain_p.h"
5 #include "chartitem_p.h"
5 #include "chartitem_p.h"
6 #include <QGraphicsItem>
6 #include <QGraphicsItem>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QChartAxis;
10 class QChartAxis;
11
11
12 class AxisItem : public QObject, public ChartItem
12 class AxisItem : public QObject, public ChartItem
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
16 enum AxisType{X_AXIS,Y_AXIS};
16 enum AxisType{X_AXIS,Y_AXIS};
17
17
18 AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0);
18 AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0);
19 ~AxisItem();
19 ~AxisItem();
20
20
21 //from QGraphicsItem
21 //from QGraphicsItem
22 QRectF boundingRect() const;
22 QRectF boundingRect() const;
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
23 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
24
24
25 AxisType axisType() const {return m_type;};
25 AxisType axisType() const {return m_type;};
26
26
27 void setAxisOpacity(qreal opacity);
27 void setAxisOpacity(qreal opacity);
28 qreal axisOpacity() const;
28 qreal axisOpacity() const;
29
29
30 void setGridOpacity(qreal opacity);
30 void setGridOpacity(qreal opacity);
31 qreal gridOpacity() const;
31 qreal gridOpacity() const;
32
32
33 void setLabelsOpacity(qreal opacity);
33 void setLabelsOpacity(qreal opacity);
34 qreal labelsOpacity() const;
34 qreal labelsOpacity() const;
35
35
36 void setShadesOpacity(qreal opacity);
36 void setShadesOpacity(qreal opacity);
37 qreal shadesOpacity() const;
37 qreal shadesOpacity() const;
38
38
39 void setLabelsAngle(int angle);
39 void setLabelsAngle(int angle);
40 int labelsAngle()const { return m_labelsAngle; }
40 int labelsAngle()const { return m_labelsAngle; }
41
41
42 void setShadesBrush(const QBrush& brush);
42 void setShadesBrush(const QBrush& brush);
43 void setShadesPen(const QPen& pen);
43 void setShadesPen(const QPen& pen);
44
44
45 void setAxisPen(const QPen& pen);
45 void setAxisPen(const QPen& pen);
46 void setGridPen(const QPen& pen);
46 void setGridPen(const QPen& pen);
47
47
48 void setLabelsPen(const QPen& pen);
48 void setLabelsPen(const QPen& pen);
49 void setLabelsBrush(const QBrush& brush);
49 void setLabelsBrush(const QBrush& brush);
50 void setLabelsFont(const QFont& font);
50 void setLabelsFont(const QFont& font);
51
51
52 public slots:
52 public slots:
53 void handleAxisUpdate(QChartAxis* axis); //look and feel
53 void handleAxisUpdate(QChartAxis* axis); //look and feel
54 void handleLabelsChanged(QChartAxis* axis,const QStringList& labels); //labels from dataset
54 void handleRangeChanged(QChartAxis* axis,const QStringList& labels); //labels from dataset
55 void handleGeometryChanged(const QRectF& size); // geometry from presenter
55 void handleGeometryChanged(const QRectF& size); // geometry from presenter
56 public:
56 public:
57 virtual void updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout);
57 virtual void updateItems(QVector<qreal>& oldLayout,QVector<qreal>& newLayout);
58 QVector<qreal> calculateLayout() const;
58 QVector<qreal> calculateLayout() const;
59 void applyLayout(const QVector<qreal>& points);
59 void applyLayout(const QVector<qreal>& points);
60
60
61 private:
61 private:
62 void clear(int count);
62 void clear(int count);
63 void createItems(int count);
63 void createItems(int count);
64 private:
64 private:
65 AxisType m_type;
65 AxisType m_type;
66 QRectF m_rect;
66 QRectF m_rect;
67 int m_labelsAngle;
67 int m_labelsAngle;
68 QGraphicsItemGroup m_grid;
68 QGraphicsItemGroup m_grid;
69 QGraphicsItemGroup m_shades;
69 QGraphicsItemGroup m_shades;
70 QGraphicsItemGroup m_labels;
70 QGraphicsItemGroup m_labels;
71 QGraphicsItemGroup m_axis;
71 QGraphicsItemGroup m_axis;
72 QStringList m_thicksList;
72 QStringList m_thicksList;
73 QVector<qreal> m_layoutVector;
73 QVector<qreal> m_layoutVector;
74
74
75 };
75 };
76
76
77 QTCOMMERCIALCHART_END_NAMESPACE
77 QTCOMMERCIALCHART_END_NAMESPACE
78
78
79 #endif /* AXISITEM_H_ */
79 #endif /* AXISITEM_H_ */
@@ -1,358 +1,358
1 #include "chartdataset_p.h"
1 #include "chartdataset_p.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 //series
3 //series
4 #include "qlineseries.h"
4 #include "qlineseries.h"
5 #include "qbarseries.h"
5 #include "qbarseries.h"
6 #include "qstackedbarseries.h"
6 #include "qstackedbarseries.h"
7 #include "qpercentbarseries.h"
7 #include "qpercentbarseries.h"
8 #include "qpieseries.h"
8 #include "qpieseries.h"
9 #include "qscatterseries.h"
9 #include "qscatterseries.h"
10
10
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
11 QTCOMMERCIALCHART_BEGIN_NAMESPACE
12
12
13 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
13 ChartDataSet::ChartDataSet(QObject *parent):QObject(parent),
14 m_axisX(new QChartAxis(this)),
14 m_axisX(new QChartAxis(this)),
15 m_axisY(new QChartAxis(this)),
15 m_axisY(new QChartAxis(this)),
16 m_domainIndex(0),
16 m_domainIndex(0),
17 m_axisXInitialized(false)
17 m_axisXInitialized(false)
18 {
18 {
19 }
19 }
20
20
21 ChartDataSet::~ChartDataSet()
21 ChartDataSet::~ChartDataSet()
22 {
22 {
23 // TODO Auto-generated destructor stub
23 // TODO Auto-generated destructor stub
24 }
24 }
25
25
26 const Domain ChartDataSet::domain(QChartAxis *axisY) const
26 const Domain ChartDataSet::domain(QChartAxis *axisY) const
27 {
27 {
28 int i = m_domainMap.count(axisY);
28 int i = m_domainMap.count(axisY);
29 if(i == 0){
29 if(i == 0){
30 return Domain();
30 return Domain();
31 }
31 }
32 i = i - m_domainIndex -1;
32 i = i - m_domainIndex -1;
33 return m_domainMap.values(axisY).at(i);
33 return m_domainMap.values(axisY).at(i);
34 }
34 }
35
35
36 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
36 void ChartDataSet::addSeries(QSeries* series, QChartAxis *axisY)
37 {
37 {
38 // TODO: we should check the series not already added
38 // TODO: we should check the series not already added
39
39
40 series->setParent(this); // take ownership
40 series->setParent(this); // take ownership
41 clearDomains();
41 clearDomains();
42
42
43 if(axisY==0) axisY = m_axisY;
43 if(axisY==0) axisY = m_axisY;
44 axisY->setParent(this); // take ownership
44 axisY->setParent(this); // take ownership
45
45
46 QList<QSeries*> seriesList = m_seriesMap.values(axisY);
46 QList<QSeries*> seriesList = m_seriesMap.values(axisY);
47
47
48 QList<Domain> domainList = m_domainMap.values(axisY);
48 QList<Domain> domainList = m_domainMap.values(axisY);
49
49
50 Q_ASSERT(domainList.size()<=1);
50 Q_ASSERT(domainList.size()<=1);
51
51
52 Domain domain;
52 Domain domain;
53
53
54 if(domainList.size()>0) domain = domainList.at(0);
54 if(domainList.size()>0) domain = domainList.at(0);
55
55
56 switch(series->type())
56 switch(series->type())
57 {
57 {
58 case QSeries::SeriesTypeLine: {
58 case QSeries::SeriesTypeLine: {
59
59
60 QLineSeries* xyseries = static_cast<QLineSeries*>(series);
60 QLineSeries* xyseries = static_cast<QLineSeries*>(series);
61
61
62 for (int i = 0; i < xyseries->count(); i++)
62 for (int i = 0; i < xyseries->count(); i++)
63 {
63 {
64 qreal x = xyseries->x(i);
64 qreal x = xyseries->x(i);
65 qreal y = xyseries->y(i);
65 qreal y = xyseries->y(i);
66 domain.m_minX = qMin(domain.m_minX,x);
66 domain.m_minX = qMin(domain.m_minX,x);
67 domain.m_minY = qMin(domain.m_minY,y);
67 domain.m_minY = qMin(domain.m_minY,y);
68 domain.m_maxX = qMax(domain.m_maxX,x);
68 domain.m_maxX = qMax(domain.m_maxX,x);
69 domain.m_maxY = qMax(domain.m_maxY,y);
69 domain.m_maxY = qMax(domain.m_maxY,y);
70 }
70 }
71 break;
71 break;
72 }
72 }
73 case QSeries::SeriesTypeBar: {
73 case QSeries::SeriesTypeBar: {
74 qDebug() << "QChartSeries::SeriesTypeBar";
74 qDebug() << "QChartSeries::SeriesTypeBar";
75 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
75 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
76 qreal x = barSeries->categoryCount();
76 qreal x = barSeries->categoryCount();
77 qreal y = barSeries->max();
77 qreal y = barSeries->max();
78 domain.m_minX = qMin(domain.m_minX,x);
78 domain.m_minX = qMin(domain.m_minX,x);
79 domain.m_minY = qMin(domain.m_minY,y);
79 domain.m_minY = qMin(domain.m_minY,y);
80 domain.m_maxX = qMax(domain.m_maxX,x);
80 domain.m_maxX = qMax(domain.m_maxX,x);
81 domain.m_maxY = qMax(domain.m_maxY,y);
81 domain.m_maxY = qMax(domain.m_maxY,y);
82 break;
82 break;
83 }
83 }
84 case QSeries::SeriesTypeStackedBar: {
84 case QSeries::SeriesTypeStackedBar: {
85 qDebug() << "QChartSeries::SeriesTypeStackedBar";
85 qDebug() << "QChartSeries::SeriesTypeStackedBar";
86
86
87 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
87 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
88 qreal x = stackedBarSeries->categoryCount();
88 qreal x = stackedBarSeries->categoryCount();
89 qreal y = stackedBarSeries->maxCategorySum();
89 qreal y = stackedBarSeries->maxCategorySum();
90 domain.m_minX = qMin(domain.m_minX,x);
90 domain.m_minX = qMin(domain.m_minX,x);
91 domain.m_minY = qMin(domain.m_minY,y);
91 domain.m_minY = qMin(domain.m_minY,y);
92 domain.m_maxX = qMax(domain.m_maxX,x);
92 domain.m_maxX = qMax(domain.m_maxX,x);
93 domain.m_maxY = qMax(domain.m_maxY,y);
93 domain.m_maxY = qMax(domain.m_maxY,y);
94 break;
94 break;
95 }
95 }
96 case QSeries::SeriesTypePercentBar: {
96 case QSeries::SeriesTypePercentBar: {
97 qDebug() << "QChartSeries::SeriesTypePercentBar";
97 qDebug() << "QChartSeries::SeriesTypePercentBar";
98
98
99 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
99 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
100 qreal x = percentBarSeries->categoryCount();
100 qreal x = percentBarSeries->categoryCount();
101 domain.m_minX = qMin(domain.m_minX,x);
101 domain.m_minX = qMin(domain.m_minX,x);
102 domain.m_minY = 0;
102 domain.m_minY = 0;
103 domain.m_maxX = qMax(domain.m_maxX,x);
103 domain.m_maxX = qMax(domain.m_maxX,x);
104 domain.m_maxY = 100;
104 domain.m_maxY = 100;
105 break;
105 break;
106 }
106 }
107
107
108 case QSeries::SeriesTypePie: {
108 case QSeries::SeriesTypePie: {
109 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
109 QPieSeries *pieSeries = static_cast<QPieSeries *>(series);
110 // TODO: domain stuff
110 // TODO: domain stuff
111 break;
111 break;
112 }
112 }
113
113
114 case QSeries::SeriesTypeScatter: {
114 case QSeries::SeriesTypeScatter: {
115 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
115 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
116 Q_ASSERT(scatterSeries);
116 Q_ASSERT(scatterSeries);
117 foreach (QPointF point, scatterSeries->data()) {
117 foreach (QPointF point, scatterSeries->data()) {
118 domain.m_minX = qMin(domain.m_minX, point.x());
118 domain.m_minX = qMin(domain.m_minX, point.x());
119 domain.m_maxX = qMax(domain.m_maxX, point.x());
119 domain.m_maxX = qMax(domain.m_maxX, point.x());
120 domain.m_minY = qMin(domain.m_minY, point.y());
120 domain.m_minY = qMin(domain.m_minY, point.y());
121 domain.m_maxY = qMax(domain.m_maxY, point.y());
121 domain.m_maxY = qMax(domain.m_maxY, point.y());
122 }
122 }
123 break;
123 break;
124 }
124 }
125
125
126 default: {
126 default: {
127 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
127 qDebug()<<__FUNCTION__<<"type" << series->type()<<"not supported";
128 return;
128 return;
129 break;
129 break;
130 }
130 }
131
131
132 }
132 }
133
133
134 if(!m_domainMap.contains(axisY))
134 if(!m_domainMap.contains(axisY))
135 {
135 {
136 emit axisAdded(axisY);
136 emit axisAdded(axisY);
137 QObject::connect(axisY,SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
137 QObject::connect(axisY,SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
138 QObject::connect(axisY,SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
138 QObject::connect(axisY,SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
139 QObject::connect(axisY,SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
139 QObject::connect(axisY,SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
140 }
140 }
141 m_domainMap.replace(axisY,domain);
141 m_domainMap.replace(axisY,domain);
142 m_seriesMap.insert(axisY,series);
142 m_seriesMap.insert(axisY,series);
143
143
144 if(!m_axisXInitialized)
144 if(!m_axisXInitialized)
145 {
145 {
146 emit axisAdded(axisX());
146 emit axisAdded(axisX());
147 QObject::connect(axisX(),SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
147 QObject::connect(axisX(),SIGNAL(minChanged(qreal)),this,SLOT(handleMinChanged(qreal)));
148 QObject::connect(axisX(),SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
148 QObject::connect(axisX(),SIGNAL(maxChanged(qreal)),this,SLOT(handleMaxChanged(qreal)));
149 QObject::connect(axisX(),SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
149 QObject::connect(axisX(),SIGNAL(ticksChanged(QChartAxis*)),this,SLOT(handleTickChanged(QChartAxis*)));
150 m_axisXInitialized=true;
150 m_axisXInitialized=true;
151 }
151 }
152
152
153 QStringList ylabels = createLabels(axisY,domain.m_minY,domain.m_maxY);
153 QStringList ylabels = createLabels(axisY,domain.m_minY,domain.m_maxY);
154 QStringList xlabels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
154 QStringList xlabels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
155 emit axisLabelsChanged(axisY,ylabels);
155 emit axisRangeChanged(axisY,ylabels);
156 emit axisLabelsChanged(axisX(),xlabels);
156 emit axisRangeChanged(axisX(),xlabels);
157 emit seriesAdded(series);
157 emit seriesAdded(series);
158 emit seriesDomainChanged(series,domain);
158 emit seriesDomainChanged(series,domain);
159
159
160 }
160 }
161
161
162 void ChartDataSet::removeSeries(QSeries* series)
162 void ChartDataSet::removeSeries(QSeries* series)
163 {
163 {
164 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
164 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
165 foreach(QChartAxis* axis , keys) {
165 foreach(QChartAxis* axis , keys) {
166 if(m_seriesMap.contains(axis,series)){
166 if(m_seriesMap.contains(axis,series)){
167 emit seriesRemoved(series);
167 emit seriesRemoved(series);
168 m_seriesMap.remove(axis,series);
168 m_seriesMap.remove(axis,series);
169 //remove axis if no longer there
169 //remove axis if no longer there
170 if(!m_seriesMap.contains(axis)){
170 if(!m_seriesMap.contains(axis)){
171 emit axisRemoved(axis);
171 emit axisRemoved(axis);
172 m_domainMap.remove(axis);
172 m_domainMap.remove(axis);
173 if(axis != m_axisY)
173 if(axis != m_axisY)
174 delete axis;
174 delete axis;
175 }
175 }
176 series->setParent(0);
176 series->setParent(0);
177 break;
177 break;
178 }
178 }
179 }
179 }
180 }
180 }
181
181
182 void ChartDataSet::removeAllSeries()
182 void ChartDataSet::removeAllSeries()
183 {
183 {
184 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
184 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
185 foreach(QChartAxis* axis , keys) {
185 foreach(QChartAxis* axis , keys) {
186 QList<QSeries*> seriesList = m_seriesMap.values(axis);
186 QList<QSeries*> seriesList = m_seriesMap.values(axis);
187 for(int i =0 ; i < seriesList.size();i++ )
187 for(int i =0 ; i < seriesList.size();i++ )
188 {
188 {
189 emit seriesRemoved(seriesList.at(i));
189 emit seriesRemoved(seriesList.at(i));
190 delete(seriesList.at(i));
190 delete(seriesList.at(i));
191 }
191 }
192 m_seriesMap.remove(axis);
192 m_seriesMap.remove(axis);
193 m_domainMap.remove(axis);
193 m_domainMap.remove(axis);
194 emit axisRemoved(axis);
194 emit axisRemoved(axis);
195 if(axis != m_axisY) delete axis;
195 if(axis != m_axisY) delete axis;
196 }
196 }
197 m_domainIndex=0;
197 m_domainIndex=0;
198 }
198 }
199
199
200 bool ChartDataSet::nextDomain()
200 bool ChartDataSet::nextDomain()
201 {
201 {
202 int limit = (m_domainMap.values().size()/m_domainMap.uniqueKeys().size())-1;
202 int limit = (m_domainMap.values().size()/m_domainMap.uniqueKeys().size())-1;
203
203
204 if (m_domainIndex < limit) {
204 if (m_domainIndex < limit) {
205 m_domainIndex++;
205 m_domainIndex++;
206 setDomain(m_domainIndex);
206 setDomain(m_domainIndex);
207 return true;
207 return true;
208 }
208 }
209 else {
209 else {
210 return false;
210 return false;
211 }
211 }
212 }
212 }
213
213
214 bool ChartDataSet::previousDomain()
214 bool ChartDataSet::previousDomain()
215 {
215 {
216 if (m_domainIndex > 0) {
216 if (m_domainIndex > 0) {
217 m_domainIndex--;
217 m_domainIndex--;
218 setDomain(m_domainIndex);
218 setDomain(m_domainIndex);
219 return true;
219 return true;
220 }
220 }
221 else {
221 else {
222 return false;
222 return false;
223 }
223 }
224 }
224 }
225
225
226 void ChartDataSet::setDomain(int index)
226 void ChartDataSet::setDomain(int index)
227 {
227 {
228 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
228 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
229
229
230 if(domainList.count()==0) return;
230 if(domainList.count()==0) return;
231
231
232 Domain domain;
232 Domain domain;
233
233
234 foreach (QChartAxis* axis , domainList) {
234 foreach (QChartAxis* axis , domainList) {
235 int i = m_domainMap.count(axis) - index -1;
235 int i = m_domainMap.count(axis) - index -1;
236 Q_ASSERT(i>=0);
236 Q_ASSERT(i>=0);
237 domain = m_domainMap.values(axis).at(i);
237 domain = m_domainMap.values(axis).at(i);
238 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
238 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
239 QList<QSeries*> seriesList = m_seriesMap.values(axis);
239 QList<QSeries*> seriesList = m_seriesMap.values(axis);
240 foreach(QSeries* series, seriesList) {
240 foreach(QSeries* series, seriesList) {
241 emit seriesDomainChanged(series,domain);
241 emit seriesDomainChanged(series,domain);
242 }
242 }
243 emit axisLabelsChanged(axis,labels);
243 emit axisRangeChanged(axis,labels);
244 }
244 }
245
245
246 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
246 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
247 emit axisLabelsChanged(axisX(),labels);
247 emit axisRangeChanged(axisX(),labels);
248 }
248 }
249
249
250 void ChartDataSet::clearDomains(int toIndex)
250 void ChartDataSet::clearDomains(int toIndex)
251 {
251 {
252 Q_ASSERT(toIndex>=0);
252 Q_ASSERT(toIndex>=0);
253
253
254 m_domainIndex = toIndex;
254 m_domainIndex = toIndex;
255
255
256 QList<QChartAxis*> keys = m_domainMap.uniqueKeys();
256 QList<QChartAxis*> keys = m_domainMap.uniqueKeys();
257
257
258 foreach (QChartAxis* key , keys)
258 foreach (QChartAxis* key , keys)
259 {
259 {
260 QList<Domain> domains = m_domainMap.values(key);
260 QList<Domain> domains = m_domainMap.values(key);
261 m_domainMap.remove(key);
261 m_domainMap.remove(key);
262 int i = domains.size() - toIndex - 1;
262 int i = domains.size() - toIndex - 1;
263 while(i--){
263 while(i--){
264 domains.removeFirst();
264 domains.removeFirst();
265 }
265 }
266 for(int j=domains.size()-1; j>=0 ;j--)
266 for(int j=domains.size()-1; j>=0 ;j--)
267 m_domainMap.insert(key,domains.at(j));
267 m_domainMap.insert(key,domains.at(j));
268 }
268 }
269 }
269 }
270
270
271 void ChartDataSet::addDomain(const QRectF& rect, const QRectF& viewport)
271 void ChartDataSet::addDomain(const QRectF& rect, const QRectF& viewport)
272 {
272 {
273 Q_ASSERT(rect.isValid());
273 Q_ASSERT(rect.isValid());
274 Q_ASSERT(viewport.isValid());
274 Q_ASSERT(viewport.isValid());
275
275
276 clearDomains(m_domainIndex);
276 clearDomains(m_domainIndex);
277
277
278 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
278 QList<QChartAxis*> domainList = m_domainMap.uniqueKeys();
279
279
280 Domain domain;
280 Domain domain;
281
281
282 foreach (QChartAxis* axis , domainList){
282 foreach (QChartAxis* axis , domainList){
283 domain = m_domainMap.value(axis).subDomain(rect,viewport.width(),viewport.height());
283 domain = m_domainMap.value(axis).subDomain(rect,viewport.width(),viewport.height());
284 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
284 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
285 QList<QSeries*> seriesList = m_seriesMap.values(axis);
285 QList<QSeries*> seriesList = m_seriesMap.values(axis);
286 foreach(QSeries* series, seriesList){
286 foreach(QSeries* series, seriesList){
287 emit seriesDomainChanged(series,domain);
287 emit seriesDomainChanged(series,domain);
288 }
288 }
289 emit axisLabelsChanged(axis,labels);
289 emit axisRangeChanged(axis,labels);
290 m_domainMap.insert(axis,domain);
290 m_domainMap.insert(axis,domain);
291 }
291 }
292
292
293 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
293 QStringList labels = createLabels(axisX(),domain.m_minX,domain.m_maxX);
294 emit axisLabelsChanged(axisX(),labels);
294 emit axisRangeChanged(axisX(),labels);
295
295
296 m_domainIndex++;
296 m_domainIndex++;
297 }
297 }
298
298
299 QChartAxis* ChartDataSet::axisY(QSeries* series) const
299 QChartAxis* ChartDataSet::axisY(QSeries* series) const
300 {
300 {
301 if(series == 0) return m_axisY;
301 if(series == 0) return m_axisY;
302
302
303 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
303 QList<QChartAxis*> keys = m_seriesMap.uniqueKeys();
304
304
305 foreach(QChartAxis* axis , keys) {
305 foreach(QChartAxis* axis , keys) {
306 if(m_seriesMap.contains(axis,series)){
306 if(m_seriesMap.contains(axis,series)){
307 return axis;
307 return axis;
308 }
308 }
309 }
309 }
310 return 0;
310 return 0;
311 }
311 }
312
312
313 QStringList ChartDataSet::createLabels(QChartAxis* axis,qreal min, qreal max)
313 QStringList ChartDataSet::createLabels(QChartAxis* axis,qreal min, qreal max)
314 {
314 {
315 Q_ASSERT(max>=min);
315 Q_ASSERT(max>=min);
316
316
317 QStringList labels;
317 QStringList labels;
318
318
319 int ticks = axis->ticksCount()-1;
319 int ticks = axis->ticksCount()-1;
320
320
321 for(int i=0; i<= ticks; i++){
321 for(int i=0; i<= ticks; i++){
322 qreal value = min + (i * (max - min)/ ticks);
322 qreal value = min + (i * (max - min)/ ticks);
323 QString label = axis->axisTickLabel(value);
323 QString label = axis->axisTickLabel(value);
324 if(label.isEmpty()){
324 if(label.isEmpty()){
325 labels << QString::number(value);
325 labels << QString::number(value);
326 }else{
326 }else{
327 labels << label;
327 labels << label;
328 }
328 }
329 }
329 }
330 return labels;
330 return labels;
331 }
331 }
332
332
333
333
334 void ChartDataSet::handleMinChanged(qreal min)
334 void ChartDataSet::handleMinChanged(qreal min)
335 {
335 {
336
336
337 }
337 }
338
338
339 void ChartDataSet::handleMaxChanged(qreal max)
339 void ChartDataSet::handleMaxChanged(qreal max)
340 {
340 {
341
341
342 }
342 }
343
343
344 void ChartDataSet::handleTickChanged(QChartAxis* axis)
344 void ChartDataSet::handleTickChanged(QChartAxis* axis)
345 {
345 {
346 Domain domain = m_domainMap.value(axisY());
346 Domain domain = m_domainMap.value(axisY());
347 if(axis==axisX()){
347 if(axis==axisX()){
348 QStringList labels = createLabels(axis,domain.m_minX,domain.m_maxX);
348 QStringList labels = createLabels(axis,domain.m_minX,domain.m_maxX);
349 emit axisLabelsChanged(axis,labels);
349 emit axisRangeChanged(axis,labels);
350 }else{
350 }else{
351 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
351 QStringList labels = createLabels(axis,domain.m_minY,domain.m_maxY);
352 emit axisLabelsChanged(axis,labels);
352 emit axisRangeChanged(axis,labels);
353 }
353 }
354 }
354 }
355
355
356 #include "moc_chartdataset_p.cpp"
356 #include "moc_chartdataset_p.cpp"
357
357
358 QTCOMMERCIALCHART_END_NAMESPACE
358 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,60 +1,60
1 #ifndef CHARTDATASET_P_H_
1 #ifndef CHARTDATASET_P_H_
2 #define CHARTDATASET_P_H_
2 #define CHARTDATASET_P_H_
3
3
4 #include "qseries.h"
4 #include "qseries.h"
5 #include "domain_p.h"
5 #include "domain_p.h"
6 #include <QVector>
6 #include <QVector>
7
7
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
9
10 class QChartAxis;
10 class QChartAxis;
11
11
12 class ChartDataSet : public QObject
12 class ChartDataSet : public QObject
13 {
13 {
14 Q_OBJECT
14 Q_OBJECT
15 public:
15 public:
16 ChartDataSet(QObject* parent=0);
16 ChartDataSet(QObject* parent=0);
17 virtual ~ChartDataSet();
17 virtual ~ChartDataSet();
18
18
19 void addSeries(QSeries* series,QChartAxis *axisY = 0);
19 void addSeries(QSeries* series,QChartAxis *axisY = 0);
20 void removeSeries(QSeries* series);
20 void removeSeries(QSeries* series);
21 void removeAllSeries();
21 void removeAllSeries();
22 void addDomain(const QRectF& rect, const QRectF& viewport);
22 void addDomain(const QRectF& rect, const QRectF& viewport);
23 bool nextDomain();
23 bool nextDomain();
24 bool previousDomain();
24 bool previousDomain();
25 void clearDomains(int toIndex =0);
25 void clearDomains(int toIndex =0);
26 const Domain domain(QChartAxis *axisY) const;
26 const Domain domain(QChartAxis *axisY) const;
27 int domainIndex() const {return m_domainIndex;}
27 int domainIndex() const {return m_domainIndex;}
28 void setDomain(int index);
28 void setDomain(int index);
29
29
30 QChartAxis* axisX() const { return m_axisX;};
30 QChartAxis* axisX() const { return m_axisX;};
31 QChartAxis* axisY(QSeries* series = 0) const;
31 QChartAxis* axisY(QSeries* series = 0) const;
32
32
33 signals:
33 signals:
34 void seriesAdded(QSeries* series);
34 void seriesAdded(QSeries* series);
35 void seriesRemoved(QSeries* series);
35 void seriesRemoved(QSeries* series);
36 void axisAdded(QChartAxis* axis);
36 void axisAdded(QChartAxis* axis);
37 void axisRemoved(QChartAxis* axis);
37 void axisRemoved(QChartAxis* axis);
38 void axisLabelsChanged(QChartAxis* axis, const QStringList& labels);
38 void axisRangeChanged(QChartAxis* axis, const QStringList& labels);
39 void seriesDomainChanged(QSeries* series,const Domain& domain);
39 void seriesDomainChanged(QSeries* series,const Domain& domain);
40
40
41 private slots:
41 private slots:
42 void handleMinChanged(qreal min);
42 void handleMinChanged(qreal min);
43 void handleMaxChanged(qreal max);
43 void handleMaxChanged(qreal max);
44 void handleTickChanged(QChartAxis*);
44 void handleTickChanged(QChartAxis*);
45
45
46 private:
46 private:
47 QStringList createLabels(QChartAxis* axis,qreal min, qreal max);
47 QStringList createLabels(QChartAxis* axis,qreal min, qreal max);
48
48
49 private:
49 private:
50 QMultiMap<QChartAxis*, Domain> m_domainMap;
50 QMultiMap<QChartAxis*, Domain> m_domainMap;
51 QMultiMap<QChartAxis*, QSeries*> m_seriesMap;
51 QMultiMap<QChartAxis*, QSeries*> m_seriesMap;
52 QChartAxis* m_axisX;
52 QChartAxis* m_axisX;
53 QChartAxis* m_axisY;
53 QChartAxis* m_axisY;
54 int m_domainIndex;
54 int m_domainIndex;
55 bool m_axisXInitialized;
55 bool m_axisXInitialized;
56 };
56 };
57
57
58 QTCOMMERCIALCHART_END_NAMESPACE
58 QTCOMMERCIALCHART_END_NAMESPACE
59
59
60 #endif /* CHARTENGINE_P_H_ */
60 #endif /* CHARTENGINE_P_H_ */
@@ -1,287 +1,287
1 #include "qchart.h"
1 #include "qchart.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include "chartdataset_p.h"
4 #include "chartdataset_p.h"
5 #include "charttheme_p.h"
5 #include "charttheme_p.h"
6 //series
6 //series
7 #include "qbarseries.h"
7 #include "qbarseries.h"
8 #include "qstackedbarseries.h"
8 #include "qstackedbarseries.h"
9 #include "qpercentbarseries.h"
9 #include "qpercentbarseries.h"
10 #include "qlineseries.h"
10 #include "qlineseries.h"
11 #include "qpieseries.h"
11 #include "qpieseries.h"
12 #include "qscatterseries.h"
12 #include "qscatterseries.h"
13 //items
13 //items
14 #include "axisitem_p.h"
14 #include "axisitem_p.h"
15 #include "axisanimationitem_p.h"
15 #include "axisanimationitem_p.h"
16 #include "barpresenter_p.h"
16 #include "barpresenter_p.h"
17 #include "stackedbarpresenter_p.h"
17 #include "stackedbarpresenter_p.h"
18 #include "percentbarpresenter_p.h"
18 #include "percentbarpresenter_p.h"
19 #include "linechartitem_p.h"
19 #include "linechartitem_p.h"
20 #include "linechartanimationitem_p.h"
20 #include "linechartanimationitem_p.h"
21 #include "piepresenter_p.h"
21 #include "piepresenter_p.h"
22 #include "scatterpresenter_p.h"
22 #include "scatterpresenter_p.h"
23
23
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
24 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25
25
26 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
26 ChartPresenter::ChartPresenter(QChart* chart,ChartDataSet* dataset):QObject(chart),
27 m_chart(chart),
27 m_chart(chart),
28 m_dataset(dataset),
28 m_dataset(dataset),
29 m_chartTheme(0),
29 m_chartTheme(0),
30 m_marginSize(0),
30 m_marginSize(0),
31 m_rect(QRectF(QPoint(0,0),m_chart->size())),
31 m_rect(QRectF(QPoint(0,0),m_chart->size())),
32 m_options(0)
32 m_options(0)
33 {
33 {
34 createConnections();
34 createConnections();
35 setChartTheme(QChart::ChartThemeDefault);
35 setChartTheme(QChart::ChartThemeDefault);
36
36
37 }
37 }
38
38
39 ChartPresenter::~ChartPresenter()
39 ChartPresenter::~ChartPresenter()
40 {
40 {
41 }
41 }
42
42
43 void ChartPresenter::createConnections()
43 void ChartPresenter::createConnections()
44 {
44 {
45 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
45 QObject::connect(m_chart,SIGNAL(geometryChanged()),this,SLOT(handleGeometryChanged()));
46 QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*)),this,SLOT(handleSeriesAdded(QSeries*)));
46 QObject::connect(m_dataset,SIGNAL(seriesAdded(QSeries*)),this,SLOT(handleSeriesAdded(QSeries*)));
47 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),this,SLOT(handleSeriesRemoved(QSeries*)));
47 QObject::connect(m_dataset,SIGNAL(seriesRemoved(QSeries*)),this,SLOT(handleSeriesRemoved(QSeries*)));
48 QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*)),this,SLOT(handleAxisAdded(QChartAxis*)));
48 QObject::connect(m_dataset,SIGNAL(axisAdded(QChartAxis*)),this,SLOT(handleAxisAdded(QChartAxis*)));
49 QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*)));
49 QObject::connect(m_dataset,SIGNAL(axisRemoved(QChartAxis*)),this,SLOT(handleAxisRemoved(QChartAxis*)));
50 QObject::connect(m_dataset,SIGNAL(seriesDomainChanged(QSeries*,const Domain&)),this,SLOT(handleSeriesDomainChanged(QSeries*,const Domain&)));
50 QObject::connect(m_dataset,SIGNAL(seriesDomainChanged(QSeries*,const Domain&)),this,SLOT(handleSeriesDomainChanged(QSeries*,const Domain&)));
51 QObject::connect(m_dataset,SIGNAL(axisLabelsChanged(QChartAxis*,const QStringList&)),this,SLOT(handleAxisLabelsChanged(QChartAxis*,const QStringList&)));
51 QObject::connect(m_dataset,SIGNAL(axisRangeChanged(QChartAxis*,const QStringList&)),this,SLOT(handleAxisRangeChanged(QChartAxis*,const QStringList&)));
52 }
52 }
53
53
54
54
55 QRectF ChartPresenter::geometry() const
55 QRectF ChartPresenter::geometry() const
56 {
56 {
57 return m_rect;
57 return m_rect;
58 }
58 }
59
59
60 void ChartPresenter::handleGeometryChanged()
60 void ChartPresenter::handleGeometryChanged()
61 {
61 {
62 m_rect = QRectF(QPoint(0,0),m_chart->size());
62 m_rect = QRectF(QPoint(0,0),m_chart->size());
63 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
63 m_rect.adjust(m_marginSize,m_marginSize, -m_marginSize, -m_marginSize);
64 Q_ASSERT(m_rect.isValid());
64 Q_ASSERT(m_rect.isValid());
65 emit geometryChanged(m_rect);
65 emit geometryChanged(m_rect);
66 }
66 }
67
67
68 int ChartPresenter::margin() const
68 int ChartPresenter::margin() const
69 {
69 {
70 return m_marginSize;
70 return m_marginSize;
71 }
71 }
72
72
73 void ChartPresenter::setMargin(int margin)
73 void ChartPresenter::setMargin(int margin)
74 {
74 {
75 m_marginSize = margin;
75 m_marginSize = margin;
76 }
76 }
77
77
78 void ChartPresenter::handleAxisAdded(QChartAxis* axis)
78 void ChartPresenter::handleAxisAdded(QChartAxis* axis)
79 {
79 {
80
80
81 AxisItem* item ;
81 AxisItem* item ;
82
82
83 if(!m_options.testFlag(QChart::GridAxisAnimations))
83 if(!m_options.testFlag(QChart::GridAxisAnimations))
84 {
84 {
85 item = new AxisItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
85 item = new AxisItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
86 }else{
86 }else{
87 item = new AxisAnimationItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
87 item = new AxisAnimationItem(axis==m_dataset->axisX()?AxisItem::X_AXIS : AxisItem::Y_AXIS,m_chart);
88 }
88 }
89
89
90 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
90 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
91 QObject::connect(axis,SIGNAL(update(QChartAxis*)),item,SLOT(handleAxisUpdate(QChartAxis*)));
91 QObject::connect(axis,SIGNAL(update(QChartAxis*)),item,SLOT(handleAxisUpdate(QChartAxis*)));
92
92
93 item->handleAxisUpdate(axis);
93 item->handleAxisUpdate(axis);
94 item->handleGeometryChanged(m_rect);
94 item->handleGeometryChanged(m_rect);
95 m_chartTheme->decorate(axis,item);
95 m_chartTheme->decorate(axis,item);
96 m_axisItems.insert(axis,item);
96 m_axisItems.insert(axis,item);
97 }
97 }
98
98
99 void ChartPresenter::handleAxisRemoved(QChartAxis* axis)
99 void ChartPresenter::handleAxisRemoved(QChartAxis* axis)
100 {
100 {
101 AxisItem* item = m_axisItems.take(axis);
101 AxisItem* item = m_axisItems.take(axis);
102 Q_ASSERT(item);
102 Q_ASSERT(item);
103 delete item;
103 delete item;
104 }
104 }
105
105
106
106
107 void ChartPresenter::handleSeriesAdded(QSeries* series)
107 void ChartPresenter::handleSeriesAdded(QSeries* series)
108 {
108 {
109 switch(series->type())
109 switch(series->type())
110 {
110 {
111 case QSeries::SeriesTypeLine: {
111 case QSeries::SeriesTypeLine: {
112 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
112 QLineSeries* lineSeries = static_cast<QLineSeries*>(series);
113 LineChartItem* item;
113 LineChartItem* item;
114 if(m_options.testFlag(QChart::SeriesAnimations)){
114 if(m_options.testFlag(QChart::SeriesAnimations)){
115 item = new LineChartAnimationItem(this,lineSeries,m_chart);
115 item = new LineChartAnimationItem(this,lineSeries,m_chart);
116 }else{
116 }else{
117 item = new LineChartItem(this,lineSeries,m_chart);
117 item = new LineChartItem(this,lineSeries,m_chart);
118 }
118 }
119 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
119 m_chartTheme->decorate(item,lineSeries,m_chartItems.count());
120 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
120 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
121 QObject::connect(lineSeries,SIGNAL(pointReplaced(int)),item,SLOT(handlePointReplaced(int)));
121 QObject::connect(lineSeries,SIGNAL(pointReplaced(int)),item,SLOT(handlePointReplaced(int)));
122 QObject::connect(lineSeries,SIGNAL(pointAdded(int)),item,SLOT(handlePointAdded(int)));
122 QObject::connect(lineSeries,SIGNAL(pointAdded(int)),item,SLOT(handlePointAdded(int)));
123 QObject::connect(lineSeries,SIGNAL(pointRemoved(int)),item,SLOT(handlePointRemoved(int)));
123 QObject::connect(lineSeries,SIGNAL(pointRemoved(int)),item,SLOT(handlePointRemoved(int)));
124 QObject::connect(lineSeries,SIGNAL(updated()),item,SLOT(handleUpdated()));
124 QObject::connect(lineSeries,SIGNAL(updated()),item,SLOT(handleUpdated()));
125 m_chartItems.insert(series,item);
125 m_chartItems.insert(series,item);
126 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
126 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
127 item->handleUpdated();
127 item->handleUpdated();
128 break;
128 break;
129 }
129 }
130
130
131 case QSeries::SeriesTypeBar: {
131 case QSeries::SeriesTypeBar: {
132 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
132 QBarSeries* barSeries = static_cast<QBarSeries*>(series);
133 BarPresenter* item = new BarPresenter(barSeries,m_chart);
133 BarPresenter* item = new BarPresenter(barSeries,m_chart);
134 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
134 m_chartTheme->decorate(item,barSeries,m_chartItems.count());
135 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
135 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
136 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
136 QObject::connect(barSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
137 m_chartItems.insert(series,item);
137 m_chartItems.insert(series,item);
138 // m_axisXItem->setVisible(false);
138 // m_axisXItem->setVisible(false);
139 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
139 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
140 break;
140 break;
141 }
141 }
142
142
143 case QSeries::SeriesTypeStackedBar: {
143 case QSeries::SeriesTypeStackedBar: {
144
144
145 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
145 QStackedBarSeries* stackedBarSeries = static_cast<QStackedBarSeries*>(series);
146 StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart);
146 StackedBarPresenter* item = new StackedBarPresenter(stackedBarSeries,m_chart);
147 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
147 m_chartTheme->decorate(item,stackedBarSeries,m_chartItems.count());
148 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
148 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
149 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
149 QObject::connect(stackedBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
150 m_chartItems.insert(series,item);
150 m_chartItems.insert(series,item);
151 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
151 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
152 break;
152 break;
153 }
153 }
154
154
155 case QSeries::SeriesTypePercentBar: {
155 case QSeries::SeriesTypePercentBar: {
156
156
157 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
157 QPercentBarSeries* percentBarSeries = static_cast<QPercentBarSeries*>(series);
158 PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart);
158 PercentBarPresenter* item = new PercentBarPresenter(percentBarSeries,m_chart);
159 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
159 m_chartTheme->decorate(item,percentBarSeries ,m_chartItems.count());
160 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
160 QObject::connect(this,SIGNAL(geometryChanged(const QRectF&)),item,SLOT(handleGeometryChanged(const QRectF&)));
161 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
161 QObject::connect(percentBarSeries,SIGNAL(changed(int)),item,SLOT(handleModelChanged(int)));
162 m_chartItems.insert(series,item);
162 m_chartItems.insert(series,item);
163 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
163 if(m_rect.isValid()) item->handleGeometryChanged(m_rect);
164 break;
164 break;
165 }
165 }
166 case QSeries::SeriesTypeScatter: {
166 case QSeries::SeriesTypeScatter: {
167 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
167 QScatterSeries *scatterSeries = qobject_cast<QScatterSeries *>(series);
168 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
168 ScatterPresenter *scatterPresenter = new ScatterPresenter(scatterSeries, m_chart);
169 QObject::connect(scatterPresenter, SIGNAL(clicked(QPointF)),
169 QObject::connect(scatterPresenter, SIGNAL(clicked(QPointF)),
170 scatterSeries, SIGNAL(clicked(QPointF)));
170 scatterSeries, SIGNAL(clicked(QPointF)));
171 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
171 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)),
172 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
172 scatterPresenter, SLOT(handleGeometryChanged(const QRectF&)));
173 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
173 m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count());
174 m_chartItems.insert(scatterSeries, scatterPresenter);
174 m_chartItems.insert(scatterSeries, scatterPresenter);
175 if(m_rect.isValid()) scatterPresenter->handleGeometryChanged(m_rect);
175 if(m_rect.isValid()) scatterPresenter->handleGeometryChanged(m_rect);
176 break;
176 break;
177 }
177 }
178 case QSeries::SeriesTypePie: {
178 case QSeries::SeriesTypePie: {
179 QPieSeries *s = qobject_cast<QPieSeries *>(series);
179 QPieSeries *s = qobject_cast<QPieSeries *>(series);
180 PiePresenter* pie = new PiePresenter(m_chart, s);
180 PiePresenter* pie = new PiePresenter(m_chart, s);
181 m_chartTheme->decorate(pie, s, m_chartItems.count());
181 m_chartTheme->decorate(pie, s, m_chartItems.count());
182 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
182 QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), pie, SLOT(handleGeometryChanged(const QRectF&)));
183
183
184 // Hide all from background when there is only piechart
184 // Hide all from background when there is only piechart
185 // TODO: refactor this ugly code... should be one setting for this
185 // TODO: refactor this ugly code... should be one setting for this
186 if (m_chartItems.count() == 0) {
186 if (m_chartItems.count() == 0) {
187 m_chart->axisX()->setAxisVisible(false);
187 m_chart->axisX()->setAxisVisible(false);
188 m_chart->axisY()->setAxisVisible(false);
188 m_chart->axisY()->setAxisVisible(false);
189 m_chart->axisX()->setGridVisible(false);
189 m_chart->axisX()->setGridVisible(false);
190 m_chart->axisY()->setGridVisible(false);
190 m_chart->axisY()->setGridVisible(false);
191 m_chart->axisX()->setLabelsVisible(false);
191 m_chart->axisX()->setLabelsVisible(false);
192 m_chart->axisY()->setLabelsVisible(false);
192 m_chart->axisY()->setLabelsVisible(false);
193 m_chart->axisX()->setShadesVisible(false);
193 m_chart->axisX()->setShadesVisible(false);
194 m_chart->axisY()->setShadesVisible(false);
194 m_chart->axisY()->setShadesVisible(false);
195 m_chart->setChartBackgroundBrush(Qt::transparent);
195 m_chart->setChartBackgroundBrush(Qt::transparent);
196 }
196 }
197
197
198 m_chartItems.insert(series, pie);
198 m_chartItems.insert(series, pie);
199 pie->handleGeometryChanged(m_rect);
199 pie->handleGeometryChanged(m_rect);
200 break;
200 break;
201 }
201 }
202 default: {
202 default: {
203 qDebug()<< "Series type" << series->type() << "not implemented.";
203 qDebug()<< "Series type" << series->type() << "not implemented.";
204 break;
204 break;
205 }
205 }
206 }
206 }
207 }
207 }
208
208
209 void ChartPresenter::handleSeriesRemoved(QSeries* series)
209 void ChartPresenter::handleSeriesRemoved(QSeries* series)
210 {
210 {
211 ChartItem* item = m_chartItems.take(series);
211 ChartItem* item = m_chartItems.take(series);
212 delete item;
212 delete item;
213 }
213 }
214
214
215 void ChartPresenter::handleSeriesDomainChanged(QSeries* series, const Domain& domain)
215 void ChartPresenter::handleSeriesDomainChanged(QSeries* series, const Domain& domain)
216 {
216 {
217 m_chartItems.value(series)->handleDomainChanged(domain);
217 m_chartItems.value(series)->handleDomainChanged(domain);
218 }
218 }
219
219
220 void ChartPresenter::handleAxisLabelsChanged(QChartAxis* axis,const QStringList& labels)
220 void ChartPresenter::handleAxisRangeChanged(QChartAxis* axis,const QStringList& labels)
221 {
221 {
222 m_axisItems.value(axis)->handleLabelsChanged(axis,labels);
222 m_axisItems.value(axis)->handleRangeChanged(axis,labels);
223 }
223 }
224
224
225 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
225 void ChartPresenter::setChartTheme(QChart::ChartTheme theme)
226 {
226 {
227 delete m_chartTheme;
227 delete m_chartTheme;
228
228
229 m_chartTheme = ChartTheme::createTheme(theme);
229 m_chartTheme = ChartTheme::createTheme(theme);
230
230
231 m_chartTheme->decorate(m_chart);
231 m_chartTheme->decorate(m_chart);
232 QMapIterator<QSeries*,ChartItem*> i(m_chartItems);
232 QMapIterator<QSeries*,ChartItem*> i(m_chartItems);
233
233
234 int index=0;
234 int index=0;
235 while (i.hasNext()) {
235 while (i.hasNext()) {
236 i.next();
236 i.next();
237 m_chartTheme->decorate(i.value(),i.key(),index);
237 m_chartTheme->decorate(i.value(),i.key(),index);
238 index++;
238 index++;
239 }
239 }
240
240
241 QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems);
241 QMapIterator<QChartAxis*,AxisItem*> j(m_axisItems);
242 while (j.hasNext()) {
242 while (j.hasNext()) {
243 j.next();
243 j.next();
244 m_chartTheme->decorate(j.key(),j.value());
244 m_chartTheme->decorate(j.key(),j.value());
245 }
245 }
246 }
246 }
247
247
248 QChart::ChartTheme ChartPresenter::chartTheme()
248 QChart::ChartTheme ChartPresenter::chartTheme()
249 {
249 {
250 return m_chartTheme->id();
250 return m_chartTheme->id();
251 }
251 }
252
252
253 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
253 void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options)
254 {
254 {
255 if(m_options!=options) {
255 if(m_options!=options) {
256
256
257 m_options=options;
257 m_options=options;
258
258
259 //recreate elements
259 //recreate elements
260
260
261 QList<QChartAxis*> axisList = m_axisItems.uniqueKeys();
261 QList<QChartAxis*> axisList = m_axisItems.uniqueKeys();
262 QList<QSeries*> seriesList = m_chartItems.uniqueKeys();
262 QList<QSeries*> seriesList = m_chartItems.uniqueKeys();
263
263
264 foreach(QChartAxis* axis, axisList) {
264 foreach(QChartAxis* axis, axisList) {
265 handleAxisRemoved(axis);
265 handleAxisRemoved(axis);
266 handleAxisAdded(axis);
266 handleAxisAdded(axis);
267 }
267 }
268 foreach(QSeries* series, seriesList) {
268 foreach(QSeries* series, seriesList) {
269 handleSeriesRemoved(series);
269 handleSeriesRemoved(series);
270 handleSeriesAdded(series);
270 handleSeriesAdded(series);
271 }
271 }
272
272
273 //now reintialize view data
273 //now reintialize view data
274 //TODO: make it more nice
274 //TODO: make it more nice
275 m_dataset->setDomain(m_dataset->domainIndex());
275 m_dataset->setDomain(m_dataset->domainIndex());
276 }
276 }
277 }
277 }
278
278
279 QChart::AnimationOptions ChartPresenter::animationOptions() const
279 QChart::AnimationOptions ChartPresenter::animationOptions() const
280 {
280 {
281 return m_options;
281 return m_options;
282 }
282 }
283
283
284
284
285 #include "moc_chartpresenter_p.cpp"
285 #include "moc_chartpresenter_p.cpp"
286
286
287 QTCOMMERCIALCHART_END_NAMESPACE
287 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,73 +1,73
1 #ifndef CHARTPRESENTER_H_
1 #ifndef CHARTPRESENTER_H_
2 #define CHARTPRESENTER_H_
2 #define CHARTPRESENTER_H_
3
3
4 #include "qchartglobal.h"
4 #include "qchartglobal.h"
5 #include "qchart.h" //becouse of QChart::ChartThemeId //TODO
5 #include "qchart.h" //becouse of QChart::ChartThemeId //TODO
6 #include "qchartaxis.h"
6 #include "qchartaxis.h"
7 #include <QRectF>
7 #include <QRectF>
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 class ChartItem;
11 class ChartItem;
12 class QSeries;
12 class QSeries;
13 class ChartDataSet;
13 class ChartDataSet;
14 //class QChart;
14 //class QChart;
15 class Domain;
15 class Domain;
16 class AxisItem;
16 class AxisItem;
17 class ChartTheme;
17 class ChartTheme;
18
18
19 class ChartPresenter: public QObject
19 class ChartPresenter: public QObject
20 {
20 {
21 Q_OBJECT
21 Q_OBJECT
22 public:
22 public:
23 enum ZValues {
23 enum ZValues {
24 BackgroundZValue = -1,
24 BackgroundZValue = -1,
25 ShadesZValue,
25 ShadesZValue,
26 GridZValue,
26 GridZValue,
27 AxisZValue,
27 AxisZValue,
28 LineChartZValue,
28 LineChartZValue,
29 ScatterSeriesZValue
29 ScatterSeriesZValue
30 };
30 };
31
31
32 ChartPresenter(QChart* chart,ChartDataSet *dataset);
32 ChartPresenter(QChart* chart,ChartDataSet *dataset);
33 virtual ~ChartPresenter();
33 virtual ~ChartPresenter();
34
34
35 void setMargin(int margin);
35 void setMargin(int margin);
36 int margin() const;
36 int margin() const;
37
37
38 QRectF geometry() const;
38 QRectF geometry() const;
39
39
40 void setChartTheme(QChart::ChartTheme theme);
40 void setChartTheme(QChart::ChartTheme theme);
41 QChart::ChartTheme chartTheme();
41 QChart::ChartTheme chartTheme();
42
42
43 void setAnimationOptions(QChart::AnimationOptions options);
43 void setAnimationOptions(QChart::AnimationOptions options);
44 QChart::AnimationOptions animationOptions() const;
44 QChart::AnimationOptions animationOptions() const;
45
45
46 private:
46 private:
47 void createConnections();
47 void createConnections();
48
48
49 public slots:
49 public slots:
50 void handleSeriesAdded(QSeries* series);
50 void handleSeriesAdded(QSeries* series);
51 void handleSeriesRemoved(QSeries* series);
51 void handleSeriesRemoved(QSeries* series);
52 void handleAxisAdded(QChartAxis* axis);
52 void handleAxisAdded(QChartAxis* axis);
53 void handleAxisRemoved(QChartAxis* axis);
53 void handleAxisRemoved(QChartAxis* axis);
54 void handleSeriesDomainChanged(QSeries* series, const Domain& domain);
54 void handleSeriesDomainChanged(QSeries* series, const Domain& domain);
55 void handleAxisLabelsChanged(QChartAxis* axis, const QStringList& labels);
55 void handleAxisRangeChanged(QChartAxis* axis, const QStringList& labels);
56 void handleGeometryChanged();
56 void handleGeometryChanged();
57 signals:
57 signals:
58 void geometryChanged(const QRectF& rect);
58 void geometryChanged(const QRectF& rect);
59 private:
59 private:
60 QMap<QSeries*,ChartItem*> m_chartItems;
60 QMap<QSeries*,ChartItem*> m_chartItems;
61 QMap<QChartAxis*,AxisItem*> m_axisItems;
61 QMap<QChartAxis*,AxisItem*> m_axisItems;
62 QChart* m_chart;
62 QChart* m_chart;
63 ChartDataSet* m_dataset;
63 ChartDataSet* m_dataset;
64 ChartTheme *m_chartTheme;
64 ChartTheme *m_chartTheme;
65 int m_marginSize;
65 int m_marginSize;
66 QRectF m_rect;
66 QRectF m_rect;
67 QChart::AnimationOptions m_options;
67 QChart::AnimationOptions m_options;
68
68
69 };
69 };
70
70
71 QTCOMMERCIALCHART_END_NAMESPACE
71 QTCOMMERCIALCHART_END_NAMESPACE
72
72
73 #endif /* CHARTPRESENTER_H_ */
73 #endif /* CHARTPRESENTER_H_ */
@@ -1,598 +1,598
1 #include <QtTest/QtTest>
1 #include <QtTest/QtTest>
2 #include <qchartaxis.h>
2 #include <qchartaxis.h>
3 #include <qlineseries.h>
3 #include <qlineseries.h>
4 #include <private/chartdataset_p.h>
4 #include <private/chartdataset_p.h>
5 #include <private/domain_p.h>
5 #include <private/domain_p.h>
6
6
7 QTCOMMERCIALCHART_USE_NAMESPACE
7 QTCOMMERCIALCHART_USE_NAMESPACE
8
8
9 Q_DECLARE_METATYPE(Domain)
9 Q_DECLARE_METATYPE(Domain)
10 Q_DECLARE_METATYPE(QChartAxis*)
10 Q_DECLARE_METATYPE(QChartAxis*)
11 Q_DECLARE_METATYPE(QSeries*)
11 Q_DECLARE_METATYPE(QSeries*)
12 Q_DECLARE_METATYPE(QLineSeries*)
12 Q_DECLARE_METATYPE(QLineSeries*)
13
13
14 class tst_ChartDataSet: public QObject {
14 class tst_ChartDataSet: public QObject {
15 Q_OBJECT
15 Q_OBJECT
16
16
17 public slots:
17 public slots:
18 void initTestCase();
18 void initTestCase();
19 void cleanupTestCase();
19 void cleanupTestCase();
20 void init();
20 void init();
21 void cleanup();
21 void cleanup();
22
22
23 private slots:
23 private slots:
24 void chartdataset_data();
24 void chartdataset_data();
25 void chartdataset();
25 void chartdataset();
26
26
27 void addDomain_data();
27 void addDomain_data();
28 void addDomain();
28 void addDomain();
29 void addSeries_data();
29 void addSeries_data();
30 void addSeries();
30 void addSeries();
31 void axisY_data();
31 void axisY_data();
32 void axisY();
32 void axisY();
33 void clearDomains_data();
33 void clearDomains_data();
34 void clearDomains();
34 void clearDomains();
35 void domain_data();
35 void domain_data();
36 void domain();
36 void domain();
37 void nextpreviousDomain_data();
37 void nextpreviousDomain_data();
38 void nextpreviousDomain();
38 void nextpreviousDomain();
39 void removeSeries_data();
39 void removeSeries_data();
40 void removeSeries();
40 void removeSeries();
41 void removeAllSeries_data();
41 void removeAllSeries_data();
42 void removeAllSeries();
42 void removeAllSeries();
43 };
43 };
44
44
45 void tst_ChartDataSet::initTestCase()
45 void tst_ChartDataSet::initTestCase()
46 {
46 {
47 qRegisterMetaType<Domain>("Domain");
47 qRegisterMetaType<Domain>("Domain");
48 qRegisterMetaType<QChartAxis*>();
48 qRegisterMetaType<QChartAxis*>();
49 qRegisterMetaType<QSeries*>();
49 qRegisterMetaType<QSeries*>();
50 }
50 }
51
51
52 void tst_ChartDataSet::cleanupTestCase()
52 void tst_ChartDataSet::cleanupTestCase()
53 {
53 {
54 }
54 }
55
55
56 void tst_ChartDataSet::init()
56 void tst_ChartDataSet::init()
57 {
57 {
58 }
58 }
59
59
60 void tst_ChartDataSet::cleanup()
60 void tst_ChartDataSet::cleanup()
61 {
61 {
62 }
62 }
63
63
64 void tst_ChartDataSet::chartdataset_data()
64 void tst_ChartDataSet::chartdataset_data()
65 {
65 {
66 }
66 }
67
67
68 void tst_ChartDataSet::chartdataset()
68 void tst_ChartDataSet::chartdataset()
69 {
69 {
70 ChartDataSet dataSet;
70 ChartDataSet dataSet;
71 QVERIFY2(dataSet.axisX(), "Missing axisX.");
71 QVERIFY2(dataSet.axisX(), "Missing axisX.");
72 QVERIFY2(dataSet.axisY(), "Missing axisY.");
72 QVERIFY2(dataSet.axisY(), "Missing axisY.");
73 //check if not dangling pointer
73 //check if not dangling pointer
74 dataSet.axisX()->objectName();
74 dataSet.axisX()->objectName();
75 dataSet.axisY()->objectName();
75 dataSet.axisY()->objectName();
76 QVERIFY(dataSet.domain(dataSet.axisX())==Domain());
76 QVERIFY(dataSet.domain(dataSet.axisX())==Domain());
77 QVERIFY(dataSet.domain(dataSet.axisY())==Domain());
77 QVERIFY(dataSet.domain(dataSet.axisY())==Domain());
78 QCOMPARE(dataSet.domainIndex(), 0);
78 QCOMPARE(dataSet.domainIndex(), 0);
79 }
79 }
80
80
81 void tst_ChartDataSet::addDomain_data()
81 void tst_ChartDataSet::addDomain_data()
82 {
82 {
83 QTest::addColumn<QRectF>("rect");
83 QTest::addColumn<QRectF>("rect");
84 QTest::addColumn<QRectF>("viewport");
84 QTest::addColumn<QRectF>("viewport");
85 QTest::newRow("400x400,1000x1000") << QRectF(200, 200, 600, 600)
85 QTest::newRow("400x400,1000x1000") << QRectF(200, 200, 600, 600)
86 << QRectF(0, 0, 1000, 1000);
86 << QRectF(0, 0, 1000, 1000);
87 QTest::newRow("600x600,1000x1000") << QRectF(100, 100, 700, 700)
87 QTest::newRow("600x600,1000x1000") << QRectF(100, 100, 700, 700)
88 << QRectF(0, 0, 1000, 1000);
88 << QRectF(0, 0, 1000, 1000);
89 QTest::newRow("200x200,1000x1000") << QRectF(400, 400, 600, 600)
89 QTest::newRow("200x200,1000x1000") << QRectF(400, 400, 600, 600)
90 << QRectF(0, 0, 1000, 1000);
90 << QRectF(0, 0, 1000, 1000);
91 }
91 }
92
92
93 void tst_ChartDataSet::addDomain()
93 void tst_ChartDataSet::addDomain()
94 {
94 {
95 QFETCH(QRectF, rect);
95 QFETCH(QRectF, rect);
96 QFETCH(QRectF, viewport);
96 QFETCH(QRectF, viewport);
97
97
98 ChartDataSet dataSet;
98 ChartDataSet dataSet;
99
99
100 Domain domain1(0, 1000, 0, 1000);
100 Domain domain1(0, 1000, 0, 1000);
101 QLineSeries series;
101 QLineSeries series;
102 series.add(0, 0);
102 series.add(0, 0);
103 series.add(1000, 1000);
103 series.add(1000, 1000);
104
104
105 dataSet.addSeries(&series);
105 dataSet.addSeries(&series);
106
106
107 QCOMPARE(dataSet.domainIndex(), 0);
107 QCOMPARE(dataSet.domainIndex(), 0);
108
108
109 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
109 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
110 QSignalSpy spy1(&dataSet,
110 QSignalSpy spy1(&dataSet,
111 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
111 SIGNAL(axisRangeChanged(QChartAxis*, QStringList const&)));
112 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
112 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
113 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QSeries*)));
113 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QSeries*)));
114 QSignalSpy spy4(&dataSet,
114 QSignalSpy spy4(&dataSet,
115 SIGNAL(seriesDomainChanged(QSeries*, Domain const&)));
115 SIGNAL(seriesDomainChanged(QSeries*, Domain const&)));
116 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
116 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
117
117
118 Domain domain2 = dataSet.domain(dataSet.axisY());
118 Domain domain2 = dataSet.domain(dataSet.axisY());
119 QVERIFY(domain1 == domain2);
119 QVERIFY(domain1 == domain2);
120
120
121 dataSet.addDomain(rect, viewport);
121 dataSet.addDomain(rect, viewport);
122 QCOMPARE(dataSet.domainIndex(), 1);
122 QCOMPARE(dataSet.domainIndex(), 1);
123 Domain domain3 = dataSet.domain(dataSet.axisY());
123 Domain domain3 = dataSet.domain(dataSet.axisY());
124 Domain domain4 = domain1.subDomain(rect, viewport.width(),
124 Domain domain4 = domain1.subDomain(rect, viewport.width(),
125 viewport.height());
125 viewport.height());
126 QVERIFY(domain3 == domain4);
126 QVERIFY(domain3 == domain4);
127
127
128 QCOMPARE(spy0.count(), 0);
128 QCOMPARE(spy0.count(), 0);
129 QCOMPARE(spy1.count(), 2);
129 QCOMPARE(spy1.count(), 2);
130 QCOMPARE(spy2.count(), 0);
130 QCOMPARE(spy2.count(), 0);
131 QCOMPARE(spy3.count(), 0);
131 QCOMPARE(spy3.count(), 0);
132 QCOMPARE(spy4.count(), 1);
132 QCOMPARE(spy4.count(), 1);
133 QCOMPARE(spy5.count(), 0);
133 QCOMPARE(spy5.count(), 0);
134 }
134 }
135
135
136 void tst_ChartDataSet::addSeries_data()
136 void tst_ChartDataSet::addSeries_data()
137 {
137 {
138 QTest::addColumn<int>("seriesCount");
138 QTest::addColumn<int>("seriesCount");
139 QTest::addColumn<int>("axisYCount");
139 QTest::addColumn<int>("axisYCount");
140 QTest::newRow("2 series, default axis") << 2 << 0;
140 QTest::newRow("2 series, default axis") << 2 << 0;
141 QTest::newRow("2 series, 2 new axis") << 2 << 2;
141 QTest::newRow("2 series, 2 new axis") << 2 << 2;
142 QTest::newRow("2 series, 1 new axis") << 2 << 2;
142 QTest::newRow("2 series, 1 new axis") << 2 << 2;
143 QTest::newRow("3 series, 3 new axis") << 3 << 3;
143 QTest::newRow("3 series, 3 new axis") << 3 << 3;
144 QTest::newRow("3 series, 2 new axis") << 3 << 2;
144 QTest::newRow("3 series, 2 new axis") << 3 << 2;
145 QTest::newRow("3 series, 1 new axis") << 3 << 1;
145 QTest::newRow("3 series, 1 new axis") << 3 << 1;
146 }
146 }
147
147
148 void tst_ChartDataSet::addSeries()
148 void tst_ChartDataSet::addSeries()
149 {
149 {
150 QFETCH(int, seriesCount);
150 QFETCH(int, seriesCount);
151 QFETCH(int, axisYCount);
151 QFETCH(int, axisYCount);
152
152
153 ChartDataSet dataSet;
153 ChartDataSet dataSet;
154
154
155 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
155 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
156 QSignalSpy spy1(&dataSet,
156 QSignalSpy spy1(&dataSet,
157 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
157 SIGNAL(axisRangeChanged(QChartAxis*, QStringList const&)));
158 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
158 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
159 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QSeries*)));
159 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QSeries*)));
160 QSignalSpy spy4(&dataSet,
160 QSignalSpy spy4(&dataSet,
161 SIGNAL(seriesDomainChanged(QSeries*, Domain const&)));
161 SIGNAL(seriesDomainChanged(QSeries*, Domain const&)));
162 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
162 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
163
163
164 QList<QChartAxis*> axisList;
164 QList<QChartAxis*> axisList;
165
165
166 for (int i = 0; i < axisYCount; i++) {
166 for (int i = 0; i < axisYCount; i++) {
167 QChartAxis* axis = new QChartAxis();
167 QChartAxis* axis = new QChartAxis();
168 axisList << axis;
168 axisList << axis;
169 }
169 }
170
170
171 QList<QChartAxis*>::iterator iterator = axisList.begin();
171 QList<QChartAxis*>::iterator iterator = axisList.begin();
172
172
173 for (int i = 0; i < seriesCount; i++) {
173 for (int i = 0; i < seriesCount; i++) {
174 QChartAxis* axisY = 0;
174 QChartAxis* axisY = 0;
175 QLineSeries* series = new QLineSeries();
175 QLineSeries* series = new QLineSeries();
176 if (iterator != axisList.end()) {
176 if (iterator != axisList.end()) {
177 axisY = *iterator;
177 axisY = *iterator;
178 iterator++;
178 iterator++;
179 } else if (axisList.count() > 0) {
179 } else if (axisList.count() > 0) {
180 iterator--;
180 iterator--;
181 axisY = *iterator;
181 axisY = *iterator;
182 iterator++;
182 iterator++;
183 }
183 }
184 dataSet.addSeries(series, axisY);
184 dataSet.addSeries(series, axisY);
185 }
185 }
186
186
187 //default axis
187 //default axis
188 if (axisYCount == 0)
188 if (axisYCount == 0)
189 axisYCount+=2;
189 axisYCount+=2;
190 else
190 else
191 axisYCount++;
191 axisYCount++;
192
192
193 QCOMPARE(spy0.count(), axisYCount);
193 QCOMPARE(spy0.count(), axisYCount);
194 QCOMPARE(spy1.count(), seriesCount*2);
194 QCOMPARE(spy1.count(), seriesCount*2);
195 QCOMPARE(spy2.count(), 0);
195 QCOMPARE(spy2.count(), 0);
196 QCOMPARE(spy3.count(), seriesCount);
196 QCOMPARE(spy3.count(), seriesCount);
197 QCOMPARE(spy4.count(), seriesCount);
197 QCOMPARE(spy4.count(), seriesCount);
198 QCOMPARE(spy5.count(), 0);
198 QCOMPARE(spy5.count(), 0);
199
199
200 QCOMPARE(dataSet.domainIndex(), 0);
200 QCOMPARE(dataSet.domainIndex(), 0);
201 }
201 }
202
202
203 void tst_ChartDataSet::axisY_data()
203 void tst_ChartDataSet::axisY_data()
204 {
204 {
205 QTest::addColumn<QChartAxis*>("axisY");
205 QTest::addColumn<QChartAxis*>("axisY");
206 QTest::newRow("axisY1") << new QChartAxis();
206 QTest::newRow("axisY1") << new QChartAxis();
207 QTest::newRow("axisY2") << new QChartAxis();
207 QTest::newRow("axisY2") << new QChartAxis();
208 }
208 }
209
209
210 void tst_ChartDataSet::axisY()
210 void tst_ChartDataSet::axisY()
211 {
211 {
212 QFETCH(QChartAxis*, axisY);
212 QFETCH(QChartAxis*, axisY);
213
213
214 ChartDataSet dataSet;
214 ChartDataSet dataSet;
215
215
216 QChartAxis* defaultAxisY = dataSet.axisY();
216 QChartAxis* defaultAxisY = dataSet.axisY();
217
217
218 QVERIFY2(defaultAxisY, "Missing axisY.");
218 QVERIFY2(defaultAxisY, "Missing axisY.");
219
219
220 QLineSeries* series1 = new QLineSeries();
220 QLineSeries* series1 = new QLineSeries();
221 dataSet.addSeries(series1);
221 dataSet.addSeries(series1);
222
222
223 QLineSeries* series2 = new QLineSeries();
223 QLineSeries* series2 = new QLineSeries();
224 dataSet.addSeries(series2, axisY);
224 dataSet.addSeries(series2, axisY);
225
225
226 QVERIFY(dataSet.axisY(series1) == defaultAxisY);
226 QVERIFY(dataSet.axisY(series1) == defaultAxisY);
227 QVERIFY(dataSet.axisY(series2) == axisY);
227 QVERIFY(dataSet.axisY(series2) == axisY);
228
228
229
229
230 }
230 }
231
231
232 void tst_ChartDataSet::clearDomains_data()
232 void tst_ChartDataSet::clearDomains_data()
233 {
233 {
234 QTest::addColumn<int>("indexCount");
234 QTest::addColumn<int>("indexCount");
235 QTest::newRow("0") << 0;
235 QTest::newRow("0") << 0;
236 QTest::newRow("1") << 1;
236 QTest::newRow("1") << 1;
237 QTest::newRow("5") << 2;
237 QTest::newRow("5") << 2;
238 QTest::newRow("8") << 3;
238 QTest::newRow("8") << 3;
239 }
239 }
240
240
241 void tst_ChartDataSet::clearDomains()
241 void tst_ChartDataSet::clearDomains()
242 {
242 {
243 QFETCH(int, indexCount);
243 QFETCH(int, indexCount);
244
244
245 Domain domain1(0, 100, 0, 100);
245 Domain domain1(0, 100, 0, 100);
246 QLineSeries* series = new QLineSeries();
246 QLineSeries* series = new QLineSeries();
247 series->add(0, 0);
247 series->add(0, 0);
248 series->add(100, 100);
248 series->add(100, 100);
249
249
250 ChartDataSet dataSet;
250 ChartDataSet dataSet;
251
251
252 QCOMPARE(dataSet.domainIndex(), 0);
252 QCOMPARE(dataSet.domainIndex(), 0);
253
253
254 dataSet.addSeries(series);
254 dataSet.addSeries(series);
255
255
256 Domain domain2 = dataSet.domain(dataSet.axisY());
256 Domain domain2 = dataSet.domain(dataSet.axisY());
257
257
258 QVERIFY(domain2 == domain1);
258 QVERIFY(domain2 == domain1);
259
259
260 QList<Domain> domains;
260 QList<Domain> domains;
261
261
262 domains << domain1;
262 domains << domain1;
263
263
264 for (int i = 0; i < indexCount; i++) {
264 for (int i = 0; i < indexCount; i++) {
265 dataSet.addDomain(QRect(0, 0, 10, 10), QRect(0, 0, 100, 100));
265 dataSet.addDomain(QRect(0, 0, 10, 10), QRect(0, 0, 100, 100));
266 domains << dataSet.domain(dataSet.axisY());
266 domains << dataSet.domain(dataSet.axisY());
267 }
267 }
268
268
269 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
269 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
270 QSignalSpy spy1(&dataSet,
270 QSignalSpy spy1(&dataSet,
271 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
271 SIGNAL(axisRangeChanged(QChartAxis*, QStringList const&)));
272 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
272 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
273 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QSeries*)));
273 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QSeries*)));
274 QSignalSpy spy4(&dataSet,
274 QSignalSpy spy4(&dataSet,
275 SIGNAL(seriesDomainChanged(QSeries*, Domain const&)));
275 SIGNAL(seriesDomainChanged(QSeries*, Domain const&)));
276 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
276 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
277
277
278 dataSet.clearDomains(indexCount);
278 dataSet.clearDomains(indexCount);
279
279
280 QCOMPARE(dataSet.domainIndex(), indexCount);
280 QCOMPARE(dataSet.domainIndex(), indexCount);
281
281
282 domain2 = dataSet.domain(dataSet.axisY());
282 domain2 = dataSet.domain(dataSet.axisY());
283
283
284 QVERIFY(domain2 == domains.at(indexCount));
284 QVERIFY(domain2 == domains.at(indexCount));
285
285
286 QCOMPARE(spy0.count(), 0);
286 QCOMPARE(spy0.count(), 0);
287 QCOMPARE(spy1.count(), 0);
287 QCOMPARE(spy1.count(), 0);
288 QCOMPARE(spy2.count(), 0);
288 QCOMPARE(spy2.count(), 0);
289 QCOMPARE(spy3.count(), 0);
289 QCOMPARE(spy3.count(), 0);
290 QCOMPARE(spy4.count(), 0);
290 QCOMPARE(spy4.count(), 0);
291 QCOMPARE(spy5.count(), 0);
291 QCOMPARE(spy5.count(), 0);
292 }
292 }
293
293
294 void tst_ChartDataSet::domain_data()
294 void tst_ChartDataSet::domain_data()
295 {
295 {
296 QTest::addColumn<Domain>("domain1");
296 QTest::addColumn<Domain>("domain1");
297 QTest::addColumn<Domain>("domain2");
297 QTest::addColumn<Domain>("domain2");
298 QTest::addColumn<Domain>("domain3");
298 QTest::addColumn<Domain>("domain3");
299 QTest::addColumn<Domain>("domain");
299 QTest::addColumn<Domain>("domain");
300 QTest::newRow("Domain(0,10,0,10)") << Domain(0, 10, 0, 10)
300 QTest::newRow("Domain(0,10,0,10)") << Domain(0, 10, 0, 10)
301 << Domain(0, 5, 0, 5) << Domain(0, 3, 0, 3) << Domain(0, 10, 0, 10);
301 << Domain(0, 5, 0, 5) << Domain(0, 3, 0, 3) << Domain(0, 10, 0, 10);
302 QTest::newRow("Domain(-1,11,0,11)") << Domain(-1, 10, 0, 10)
302 QTest::newRow("Domain(-1,11,0,11)") << Domain(-1, 10, 0, 10)
303 << Domain(0, 11, 0, 11) << Domain(0, 3, 0, 3)
303 << Domain(0, 11, 0, 11) << Domain(0, 3, 0, 3)
304 << Domain(-1, 11, 0, 11);
304 << Domain(-1, 11, 0, 11);
305 QTest::newRow("Domain(-5,5,1,8)") << Domain(-5, 0, 1, 1)
305 QTest::newRow("Domain(-5,5,1,8)") << Domain(-5, 0, 1, 1)
306 << Domain(0, 5, 0, 8) << Domain(1, 2, 1, 2) << Domain(-5, 5, 0, 8);
306 << Domain(0, 5, 0, 8) << Domain(1, 2, 1, 2) << Domain(-5, 5, 0, 8);
307 }
307 }
308
308
309 void tst_ChartDataSet::domain()
309 void tst_ChartDataSet::domain()
310 {
310 {
311 QFETCH(Domain, domain1);
311 QFETCH(Domain, domain1);
312 QFETCH(Domain, domain2);
312 QFETCH(Domain, domain2);
313 QFETCH(Domain, domain3);
313 QFETCH(Domain, domain3);
314 QFETCH(Domain, domain);
314 QFETCH(Domain, domain);
315
315
316 ChartDataSet dataSet;
316 ChartDataSet dataSet;
317 QLineSeries* series1 = new QLineSeries();
317 QLineSeries* series1 = new QLineSeries();
318 series1->add(domain1.m_minX, domain1.m_minY);
318 series1->add(domain1.m_minX, domain1.m_minY);
319 series1->add(domain1.m_maxX, domain1.m_maxY);
319 series1->add(domain1.m_maxX, domain1.m_maxY);
320 QLineSeries* series2 = new QLineSeries();
320 QLineSeries* series2 = new QLineSeries();
321 series2->add(domain2.m_minX, domain2.m_minY);
321 series2->add(domain2.m_minX, domain2.m_minY);
322 series2->add(domain2.m_maxX, domain2.m_maxY);
322 series2->add(domain2.m_maxX, domain2.m_maxY);
323 QLineSeries* series3 = new QLineSeries();
323 QLineSeries* series3 = new QLineSeries();
324 series3->add(domain3.m_minX, domain3.m_minY);
324 series3->add(domain3.m_minX, domain3.m_minY);
325 series3->add(domain3.m_maxX, domain3.m_maxY);
325 series3->add(domain3.m_maxX, domain3.m_maxY);
326
326
327 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
327 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
328 QSignalSpy spy1(&dataSet,
328 QSignalSpy spy1(&dataSet,
329 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
329 SIGNAL(axisRangeChanged(QChartAxis*, QStringList const&)));
330 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
330 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
331 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QSeries*)));
331 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QSeries*)));
332 QSignalSpy spy4(&dataSet,
332 QSignalSpy spy4(&dataSet,
333 SIGNAL(seriesDomainChanged(QSeries*, Domain const&)));
333 SIGNAL(seriesDomainChanged(QSeries*, Domain const&)));
334 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
334 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
335
335
336 dataSet.addSeries(series1);
336 dataSet.addSeries(series1);
337 dataSet.addSeries(series2);
337 dataSet.addSeries(series2);
338 dataSet.addSeries(series3);
338 dataSet.addSeries(series3);
339
339
340 QCOMPARE(dataSet.domainIndex(), 0);
340 QCOMPARE(dataSet.domainIndex(), 0);
341 QVERIFY2(dataSet.domain(dataSet.axisY()) == domain, "Domain not equal");
341 QVERIFY2(dataSet.domain(dataSet.axisY()) == domain, "Domain not equal");
342
342
343 QCOMPARE(spy0.count(), 2);
343 QCOMPARE(spy0.count(), 2);
344 QCOMPARE(spy1.count(), 6);
344 QCOMPARE(spy1.count(), 6);
345 QCOMPARE(spy2.count(), 0);
345 QCOMPARE(spy2.count(), 0);
346 QCOMPARE(spy3.count(), 3);
346 QCOMPARE(spy3.count(), 3);
347 QCOMPARE(spy4.count(), 3);
347 QCOMPARE(spy4.count(), 3);
348 QCOMPARE(spy5.count(), 0);
348 QCOMPARE(spy5.count(), 0);
349 }
349 }
350
350
351 void tst_ChartDataSet::nextpreviousDomain_data()
351 void tst_ChartDataSet::nextpreviousDomain_data()
352 {
352 {
353 QTest::addColumn<QRectF>("rect");
353 QTest::addColumn<QRectF>("rect");
354 QTest::addColumn<QRectF>("viewport");
354 QTest::addColumn<QRectF>("viewport");
355 QTest::newRow("400x400,1000x1000") << QRectF(200, 200, 600, 600)
355 QTest::newRow("400x400,1000x1000") << QRectF(200, 200, 600, 600)
356 << QRectF(0, 0, 1000, 1000);
356 << QRectF(0, 0, 1000, 1000);
357 QTest::newRow("600x600,1000x1000") << QRectF(100, 100, 700, 700)
357 QTest::newRow("600x600,1000x1000") << QRectF(100, 100, 700, 700)
358 << QRectF(0, 0, 1000, 1000);
358 << QRectF(0, 0, 1000, 1000);
359 QTest::newRow("200x200,1000x1000") << QRectF(400, 400, 600, 600)
359 QTest::newRow("200x200,1000x1000") << QRectF(400, 400, 600, 600)
360 << QRectF(0, 0, 1000, 1000);
360 << QRectF(0, 0, 1000, 1000);
361 }
361 }
362
362
363 void tst_ChartDataSet::nextpreviousDomain()
363 void tst_ChartDataSet::nextpreviousDomain()
364 {
364 {
365
365
366 QFETCH(QRectF, rect);
366 QFETCH(QRectF, rect);
367 QFETCH(QRectF, viewport);
367 QFETCH(QRectF, viewport);
368
368
369 ChartDataSet dataSet;
369 ChartDataSet dataSet;
370
370
371 Domain domain1(0, 1000, 0, 1000);
371 Domain domain1(0, 1000, 0, 1000);
372 QLineSeries* series = new QLineSeries();
372 QLineSeries* series = new QLineSeries();
373 series->add(0, 0);
373 series->add(0, 0);
374 series->add(1000, 1000);
374 series->add(1000, 1000);
375
375
376 dataSet.addSeries(series);
376 dataSet.addSeries(series);
377
377
378 QCOMPARE(dataSet.domainIndex(), 0);
378 QCOMPARE(dataSet.domainIndex(), 0);
379
379
380 Domain domain2 = dataSet.domain(dataSet.axisY());
380 Domain domain2 = dataSet.domain(dataSet.axisY());
381 QVERIFY(domain1 == domain2);
381 QVERIFY(domain1 == domain2);
382
382
383 dataSet.addDomain(rect, viewport);
383 dataSet.addDomain(rect, viewport);
384 QCOMPARE(dataSet.domainIndex(), 1);
384 QCOMPARE(dataSet.domainIndex(), 1);
385 Domain domain3 = dataSet.domain(dataSet.axisY());
385 Domain domain3 = dataSet.domain(dataSet.axisY());
386 Domain domain4 = domain1.subDomain(rect, viewport.width(),
386 Domain domain4 = domain1.subDomain(rect, viewport.width(),
387 viewport.height());
387 viewport.height());
388 QVERIFY(domain3 == domain4);
388 QVERIFY(domain3 == domain4);
389
389
390 dataSet.addDomain(rect, viewport);
390 dataSet.addDomain(rect, viewport);
391 QCOMPARE(dataSet.domainIndex(), 2);
391 QCOMPARE(dataSet.domainIndex(), 2);
392 Domain domain5 = dataSet.domain(dataSet.axisY());
392 Domain domain5 = dataSet.domain(dataSet.axisY());
393 Domain domain6 = domain3.subDomain(rect, viewport.width(),
393 Domain domain6 = domain3.subDomain(rect, viewport.width(),
394 viewport.height());
394 viewport.height());
395 QVERIFY(domain5 == domain6);
395 QVERIFY(domain5 == domain6);
396
396
397 dataSet.addDomain(rect, viewport);
397 dataSet.addDomain(rect, viewport);
398 QCOMPARE(dataSet.domainIndex(), 3);
398 QCOMPARE(dataSet.domainIndex(), 3);
399 Domain domain7 = dataSet.domain(dataSet.axisY());
399 Domain domain7 = dataSet.domain(dataSet.axisY());
400 Domain domain8 = domain5.subDomain(rect, viewport.width(),
400 Domain domain8 = domain5.subDomain(rect, viewport.width(),
401 viewport.height());
401 viewport.height());
402 QVERIFY(domain7 == domain8);
402 QVERIFY(domain7 == domain8);
403
403
404 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
404 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
405 QSignalSpy spy1(&dataSet,
405 QSignalSpy spy1(&dataSet,
406 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
406 SIGNAL(axisRangeChanged(QChartAxis*, QStringList const&)));
407 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
407 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
408 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QSeries*)));
408 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QSeries*)));
409 QSignalSpy spy4(&dataSet,
409 QSignalSpy spy4(&dataSet,
410 SIGNAL(seriesDomainChanged(QSeries*, Domain const&)));
410 SIGNAL(seriesDomainChanged(QSeries*, Domain const&)));
411 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
411 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
412
412
413 Domain domain;
413 Domain domain;
414
414
415 bool previous = dataSet.previousDomain();
415 bool previous = dataSet.previousDomain();
416 QCOMPARE(previous, true);
416 QCOMPARE(previous, true);
417 QCOMPARE(dataSet.domainIndex(), 2);
417 QCOMPARE(dataSet.domainIndex(), 2);
418 domain = dataSet.domain(dataSet.axisY());
418 domain = dataSet.domain(dataSet.axisY());
419 QVERIFY(domain == domain5);
419 QVERIFY(domain == domain5);
420 previous = dataSet.previousDomain();
420 previous = dataSet.previousDomain();
421 QCOMPARE(previous, true);
421 QCOMPARE(previous, true);
422 QCOMPARE(dataSet.domainIndex(), 1);
422 QCOMPARE(dataSet.domainIndex(), 1);
423 domain = dataSet.domain(dataSet.axisY());
423 domain = dataSet.domain(dataSet.axisY());
424 QVERIFY(domain == domain3);
424 QVERIFY(domain == domain3);
425 previous = dataSet.previousDomain();
425 previous = dataSet.previousDomain();
426 QCOMPARE(previous, true);
426 QCOMPARE(previous, true);
427 QCOMPARE(dataSet.domainIndex(), 0);
427 QCOMPARE(dataSet.domainIndex(), 0);
428 domain = dataSet.domain(dataSet.axisY());
428 domain = dataSet.domain(dataSet.axisY());
429 QVERIFY(domain == domain1);
429 QVERIFY(domain == domain1);
430 previous = dataSet.previousDomain();
430 previous = dataSet.previousDomain();
431 QCOMPARE(previous, false);
431 QCOMPARE(previous, false);
432 QCOMPARE(dataSet.domainIndex(), 0);
432 QCOMPARE(dataSet.domainIndex(), 0);
433 domain = dataSet.domain(dataSet.axisY());
433 domain = dataSet.domain(dataSet.axisY());
434 QVERIFY(domain == domain1);
434 QVERIFY(domain == domain1);
435
435
436 bool next = dataSet.nextDomain();
436 bool next = dataSet.nextDomain();
437 QCOMPARE(next, true);
437 QCOMPARE(next, true);
438 QCOMPARE(dataSet.domainIndex(),1);
438 QCOMPARE(dataSet.domainIndex(),1);
439 next = dataSet.nextDomain();
439 next = dataSet.nextDomain();
440 QCOMPARE(next, true);
440 QCOMPARE(next, true);
441 QCOMPARE(dataSet.domainIndex(),2);
441 QCOMPARE(dataSet.domainIndex(),2);
442 next = dataSet.nextDomain();
442 next = dataSet.nextDomain();
443 QCOMPARE(next, true);
443 QCOMPARE(next, true);
444 QCOMPARE(dataSet.domainIndex(),3);
444 QCOMPARE(dataSet.domainIndex(),3);
445 next = dataSet.nextDomain();
445 next = dataSet.nextDomain();
446 QCOMPARE(next, false);
446 QCOMPARE(next, false);
447 QCOMPARE(dataSet.domainIndex(),3);
447 QCOMPARE(dataSet.domainIndex(),3);
448
448
449
449
450 QCOMPARE(spy0.count(), 0);
450 QCOMPARE(spy0.count(), 0);
451 QCOMPARE(spy1.count(), 12);
451 QCOMPARE(spy1.count(), 12);
452 QCOMPARE(spy2.count(), 0);
452 QCOMPARE(spy2.count(), 0);
453 QCOMPARE(spy3.count(), 0);
453 QCOMPARE(spy3.count(), 0);
454 QCOMPARE(spy4.count(), 6);
454 QCOMPARE(spy4.count(), 6);
455 QCOMPARE(spy5.count(), 0);
455 QCOMPARE(spy5.count(), 0);
456 }
456 }
457
457
458 void tst_ChartDataSet::removeSeries_data()
458 void tst_ChartDataSet::removeSeries_data()
459 {
459 {
460 QTest::addColumn<int>("seriesCount");
460 QTest::addColumn<int>("seriesCount");
461 QTest::addColumn<int>("axisYCount");
461 QTest::addColumn<int>("axisYCount");
462 QTest::newRow("2 series, default axis") << 2 << 0;
462 QTest::newRow("2 series, default axis") << 2 << 0;
463 QTest::newRow("2 series, 2 new axis") << 2 << 2;
463 QTest::newRow("2 series, 2 new axis") << 2 << 2;
464 QTest::newRow("2 series, 1 new axis") << 2 << 2;
464 QTest::newRow("2 series, 1 new axis") << 2 << 2;
465 QTest::newRow("3 series, 3 new axis") << 3 << 3;
465 QTest::newRow("3 series, 3 new axis") << 3 << 3;
466 QTest::newRow("3 series, 2 new axis") << 3 << 2;
466 QTest::newRow("3 series, 2 new axis") << 3 << 2;
467 QTest::newRow("3 series, 1 new axis") << 3 << 1;
467 QTest::newRow("3 series, 1 new axis") << 3 << 1;
468 }
468 }
469
469
470 void tst_ChartDataSet::removeSeries()
470 void tst_ChartDataSet::removeSeries()
471 {
471 {
472 QFETCH(int, seriesCount);
472 QFETCH(int, seriesCount);
473 QFETCH(int, axisYCount);
473 QFETCH(int, axisYCount);
474
474
475 ChartDataSet dataSet;
475 ChartDataSet dataSet;
476
476
477 QList<QChartAxis*> axisList;
477 QList<QChartAxis*> axisList;
478 QList<QSeries*> seriesList;
478 QList<QSeries*> seriesList;
479
479
480 for (int i = 0; i < axisYCount; i++) {
480 for (int i = 0; i < axisYCount; i++) {
481 QChartAxis* axis = new QChartAxis();
481 QChartAxis* axis = new QChartAxis();
482 axisList << axis;
482 axisList << axis;
483 }
483 }
484
484
485 QList<QChartAxis*>::iterator iterator = axisList.begin();
485 QList<QChartAxis*>::iterator iterator = axisList.begin();
486
486
487 for (int i = 0; i < seriesCount; i++) {
487 for (int i = 0; i < seriesCount; i++) {
488 QChartAxis* axisY = 0;
488 QChartAxis* axisY = 0;
489 QLineSeries* series = new QLineSeries();
489 QLineSeries* series = new QLineSeries();
490 if (iterator != axisList.end()) {
490 if (iterator != axisList.end()) {
491 axisY = *iterator;
491 axisY = *iterator;
492 iterator++;
492 iterator++;
493 } else if (axisList.count() > 0) {
493 } else if (axisList.count() > 0) {
494 iterator--;
494 iterator--;
495 axisY = *iterator;
495 axisY = *iterator;
496 iterator++;
496 iterator++;
497 }
497 }
498 dataSet.addSeries(series, axisY);
498 dataSet.addSeries(series, axisY);
499 seriesList << series;
499 seriesList << series;
500 }
500 }
501
501
502 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
502 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
503 QSignalSpy spy1(&dataSet,
503 QSignalSpy spy1(&dataSet,
504 SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
504 SIGNAL(axisRangeChanged(QChartAxis*, QStringList const&)));
505 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
505 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
506 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QSeries*)));
506 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QSeries*)));
507 QSignalSpy spy4(&dataSet,
507 QSignalSpy spy4(&dataSet,
508 SIGNAL(seriesDomainChanged(QSeries*, Domain const&)));
508 SIGNAL(seriesDomainChanged(QSeries*, Domain const&)));
509 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
509 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
510
510
511 for (int i = 0; i < seriesCount; i++) {
511 for (int i = 0; i < seriesCount; i++) {
512 dataSet.removeSeries(seriesList.at(i));
512 dataSet.removeSeries(seriesList.at(i));
513 }
513 }
514
514
515 //default axis
515 //default axis
516 if (axisYCount == 0)
516 if (axisYCount == 0)
517 axisYCount++;
517 axisYCount++;
518
518
519 QCOMPARE(spy0.count(), 0);
519 QCOMPARE(spy0.count(), 0);
520 QCOMPARE(spy1.count(), 0);
520 QCOMPARE(spy1.count(), 0);
521 QCOMPARE(spy2.count(), axisYCount);
521 QCOMPARE(spy2.count(), axisYCount);
522 QCOMPARE(spy3.count(), 0);
522 QCOMPARE(spy3.count(), 0);
523 QCOMPARE(spy4.count(), 0);
523 QCOMPARE(spy4.count(), 0);
524 QCOMPARE(spy5.count(), seriesCount);
524 QCOMPARE(spy5.count(), seriesCount);
525
525
526 QCOMPARE(dataSet.domainIndex(), 0);
526 QCOMPARE(dataSet.domainIndex(), 0);
527
527
528 qDeleteAll(seriesList);
528 qDeleteAll(seriesList);
529 }
529 }
530
530
531 void tst_ChartDataSet::removeAllSeries_data()
531 void tst_ChartDataSet::removeAllSeries_data()
532 {
532 {
533 QTest::addColumn<int>("seriesCount");
533 QTest::addColumn<int>("seriesCount");
534 QTest::addColumn<int>("axisYCount");
534 QTest::addColumn<int>("axisYCount");
535 QTest::newRow("2 series, default axis") << 2 << 0;
535 QTest::newRow("2 series, default axis") << 2 << 0;
536 QTest::newRow("2 series, 2 new axis") << 2 << 2;
536 QTest::newRow("2 series, 2 new axis") << 2 << 2;
537 QTest::newRow("2 series, 1 new axis") << 2 << 2;
537 QTest::newRow("2 series, 1 new axis") << 2 << 2;
538 QTest::newRow("3 series, 3 new axis") << 3 << 3;
538 QTest::newRow("3 series, 3 new axis") << 3 << 3;
539 QTest::newRow("3 series, 2 new axis") << 3 << 2;
539 QTest::newRow("3 series, 2 new axis") << 3 << 2;
540 QTest::newRow("3 series, 1 new axis") << 3 << 1;
540 QTest::newRow("3 series, 1 new axis") << 3 << 1;
541 }
541 }
542
542
543 void tst_ChartDataSet::removeAllSeries()
543 void tst_ChartDataSet::removeAllSeries()
544 {
544 {
545 QFETCH(int, seriesCount);
545 QFETCH(int, seriesCount);
546 QFETCH(int, axisYCount);
546 QFETCH(int, axisYCount);
547
547
548 ChartDataSet dataSet;
548 ChartDataSet dataSet;
549
549
550 QList<QChartAxis*> axisList;
550 QList<QChartAxis*> axisList;
551
551
552 for (int i = 0; i < axisYCount; i++) {
552 for (int i = 0; i < axisYCount; i++) {
553 QChartAxis* axis = new QChartAxis();
553 QChartAxis* axis = new QChartAxis();
554 axisList << axis;
554 axisList << axis;
555 }
555 }
556
556
557 QList<QChartAxis*>::iterator iterator = axisList.begin();
557 QList<QChartAxis*>::iterator iterator = axisList.begin();
558
558
559 for (int i = 0; i < seriesCount; i++) {
559 for (int i = 0; i < seriesCount; i++) {
560 QChartAxis* axisY = 0;
560 QChartAxis* axisY = 0;
561 QLineSeries* series = new QLineSeries();
561 QLineSeries* series = new QLineSeries();
562 if (iterator != axisList.end()) {
562 if (iterator != axisList.end()) {
563 axisY = *iterator;
563 axisY = *iterator;
564 iterator++;
564 iterator++;
565 } else if (axisList.count() > 0) {
565 } else if (axisList.count() > 0) {
566 iterator--;
566 iterator--;
567 axisY = *iterator;
567 axisY = *iterator;
568 iterator++;
568 iterator++;
569 }
569 }
570 dataSet.addSeries(series, axisY);
570 dataSet.addSeries(series, axisY);
571 }
571 }
572
572
573 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
573 QSignalSpy spy0(&dataSet, SIGNAL(axisAdded(QChartAxis*)));
574 QSignalSpy spy1(&dataSet, SIGNAL(axisLabelsChanged(QChartAxis*, QStringList const&)));
574 QSignalSpy spy1(&dataSet, SIGNAL(axisRangeChanged(QChartAxis*, QStringList const&)));
575 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
575 QSignalSpy spy2(&dataSet, SIGNAL(axisRemoved(QChartAxis*)));
576 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QSeries*)));
576 QSignalSpy spy3(&dataSet, SIGNAL(seriesAdded(QSeries*)));
577 QSignalSpy spy4(&dataSet, SIGNAL(seriesDomainChanged(QSeries*, Domain const&)));
577 QSignalSpy spy4(&dataSet, SIGNAL(seriesDomainChanged(QSeries*, Domain const&)));
578 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
578 QSignalSpy spy5(&dataSet, SIGNAL(seriesRemoved(QSeries*)));
579
579
580 dataSet.removeAllSeries();
580 dataSet.removeAllSeries();
581 //default axis
581 //default axis
582 if (axisYCount == 0)
582 if (axisYCount == 0)
583 axisYCount++;
583 axisYCount++;
584
584
585 QCOMPARE(spy0.count(), 0);
585 QCOMPARE(spy0.count(), 0);
586 QCOMPARE(spy1.count(), 0);
586 QCOMPARE(spy1.count(), 0);
587 QCOMPARE(spy2.count(), axisYCount);
587 QCOMPARE(spy2.count(), axisYCount);
588 QCOMPARE(spy3.count(), 0);
588 QCOMPARE(spy3.count(), 0);
589 QCOMPARE(spy4.count(), 0);
589 QCOMPARE(spy4.count(), 0);
590 QCOMPARE(spy5.count(), seriesCount);
590 QCOMPARE(spy5.count(), seriesCount);
591
591
592 QCOMPARE(dataSet.domainIndex(), 0);
592 QCOMPARE(dataSet.domainIndex(), 0);
593 }
593 }
594
594
595
595
596 QTEST_MAIN(tst_ChartDataSet)
596 QTEST_MAIN(tst_ChartDataSet)
597 #include "tst_chartdataset.moc"
597 #include "tst_chartdataset.moc"
598
598
General Comments 0
You need to be logged in to leave comments. Login now