##// END OF EJS Templates
Added support for bar series value label angle...
Miikka Heikkinen -
r2802:458692a5a594
parent child
Show More
@@ -1,274 +1,356
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd
3 ** Copyright (C) 2015 The Qt Company Ltd
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 **
6 **
7 ** This file is part of the Qt Charts module.
7 ** This file is part of the Qt Charts module.
8 **
8 **
9 ** Licensees holding valid commercial license for Qt may use this file in
9 ** Licensees holding valid commercial license for Qt may use this file in
10 ** accordance with the Qt License Agreement provided with the Software
10 ** accordance with the Qt License Agreement provided with the Software
11 ** or, alternatively, in accordance with the terms contained in a written
11 ** or, alternatively, in accordance with the terms contained in a written
12 ** agreement between you and The Qt Company.
12 ** agreement between you and The Qt Company.
13 **
13 **
14 ** If you have questions regarding the use of this file, please use
14 ** If you have questions regarding the use of this file, please use
15 ** contact form at http://qt.io
15 ** contact form at http://qt.io
16 **
16 **
17 ****************************************************************************/
17 ****************************************************************************/
18
18
19 #include <private/abstractbarchartitem_p.h>
19 #include <private/abstractbarchartitem_p.h>
20 #include <private/bar_p.h>
20 #include <private/bar_p.h>
21 #include <QtCharts/QBarSet>
21 #include <QtCharts/QBarSet>
22 #include <private/qbarset_p.h>
22 #include <private/qbarset_p.h>
23 #include <QtCharts/QAbstractBarSeries>
23 #include <QtCharts/QAbstractBarSeries>
24 #include <private/qabstractbarseries_p.h>
24 #include <private/qabstractbarseries_p.h>
25 #include <QtCharts/QChart>
25 #include <QtCharts/QChart>
26 #include <private/chartpresenter_p.h>
26 #include <private/chartpresenter_p.h>
27 #include <private/charttheme_p.h>
27 #include <private/charttheme_p.h>
28 #include <private/baranimation_p.h>
28 #include <private/baranimation_p.h>
29
29
30 #include <private/chartdataset_p.h>
30 #include <private/chartdataset_p.h>
31 #include <QtGui/QPainter>
31 #include <QtGui/QPainter>
32 #include <QtGui/QTextDocument>
32 #include <QtGui/QTextDocument>
33
33
34 QT_CHARTS_BEGIN_NAMESPACE
34 QT_CHARTS_BEGIN_NAMESPACE
35
35
36 AbstractBarChartItem::AbstractBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
36 AbstractBarChartItem::AbstractBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
37 ChartItem(series->d_func(),item),
37 ChartItem(series->d_func(),item),
38 m_animation(0),
38 m_animation(0),
39 m_series(series)
39 m_series(series)
40 {
40 {
41
41
42 setFlag(ItemClipsChildrenToShape);
42 setFlag(ItemClipsChildrenToShape);
43 setFlag(QGraphicsItem::ItemIsSelectable);
43 setFlag(QGraphicsItem::ItemIsSelectable);
44 connect(series->d_func(), SIGNAL(updatedLayout()), this, SLOT(handleLayoutChanged()));
44 connect(series->d_func(), SIGNAL(updatedLayout()), this, SLOT(handleLayoutChanged()));
45 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleUpdatedBars()));
45 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleUpdatedBars()));
46 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool)));
46 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool)));
47 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
47 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
48 connect(series, SIGNAL(visibleChanged()), this, SLOT(handleVisibleChanged()));
48 connect(series, SIGNAL(visibleChanged()), this, SLOT(handleVisibleChanged()));
49 connect(series, SIGNAL(opacityChanged()), this, SLOT(handleOpacityChanged()));
49 connect(series, SIGNAL(opacityChanged()), this, SLOT(handleOpacityChanged()));
50 connect(series, SIGNAL(labelsFormatChanged(QString)), this, SLOT(handleUpdatedBars()));
50 connect(series, SIGNAL(labelsFormatChanged(QString)), this, SLOT(handleUpdatedBars()));
51 connect(series, SIGNAL(labelsFormatChanged(QString)), this, SLOT(positionLabels()));
51 connect(series, SIGNAL(labelsFormatChanged(QString)), this, SLOT(positionLabels()));
52 connect(series, SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)),
52 connect(series, SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)),
53 this, SLOT(handleLabelsPositionChanged()));
53 this, SLOT(handleLabelsPositionChanged()));
54 connect(series, SIGNAL(labelsAngleChanged(qreal)), this, SLOT(positionLabels()));
54 setZValue(ChartPresenter::BarSeriesZValue);
55 setZValue(ChartPresenter::BarSeriesZValue);
55 handleDataStructureChanged();
56 handleDataStructureChanged();
56 handleVisibleChanged();
57 handleVisibleChanged();
57 handleUpdatedBars();
58 handleUpdatedBars();
58 }
59 }
59
60
60 AbstractBarChartItem::~AbstractBarChartItem()
61 AbstractBarChartItem::~AbstractBarChartItem()
61 {
62 {
62 }
63 }
63
64
64 void AbstractBarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
65 void AbstractBarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
65 {
66 {
66 Q_UNUSED(painter);
67 Q_UNUSED(painter);
67 Q_UNUSED(option);
68 Q_UNUSED(option);
68 Q_UNUSED(widget);
69 Q_UNUSED(widget);
69 }
70 }
70
71
71 QRectF AbstractBarChartItem::boundingRect() const
72 QRectF AbstractBarChartItem::boundingRect() const
72 {
73 {
73 return m_rect;
74 return m_rect;
74 }
75 }
75
76
76 void AbstractBarChartItem::applyLayout(const QVector<QRectF> &layout)
77 void AbstractBarChartItem::applyLayout(const QVector<QRectF> &layout)
77 {
78 {
78 QSizeF size = geometry().size();
79 QSizeF size = geometry().size();
79 if (geometry().size().isValid()) {
80 if (geometry().size().isValid()) {
80 if (m_animation) {
81 if (m_animation) {
81 if (m_layout.count() == 0 || m_oldSize != size) {
82 if (m_layout.count() == 0 || m_oldSize != size) {
82 initializeLayout();
83 initializeLayout();
83 m_oldSize = size;
84 m_oldSize = size;
84 }
85 }
85 m_animation->setup(m_layout, layout);
86 m_animation->setup(m_layout, layout);
86 presenter()->startAnimation(m_animation);
87 presenter()->startAnimation(m_animation);
87 } else {
88 } else {
88 setLayout(layout);
89 setLayout(layout);
89 update();
90 update();
90 }
91 }
91 }
92 }
92 }
93 }
93
94
94 void AbstractBarChartItem::setAnimation(BarAnimation *animation)
95 void AbstractBarChartItem::setAnimation(BarAnimation *animation)
95 {
96 {
96 m_animation = animation;
97 m_animation = animation;
97 }
98 }
98
99
99 void AbstractBarChartItem::setLayout(const QVector<QRectF> &layout)
100 void AbstractBarChartItem::setLayout(const QVector<QRectF> &layout)
100 {
101 {
101 if (layout.count() != m_bars.count())
102 if (layout.count() != m_bars.count())
102 return;
103 return;
103
104
104 m_layout = layout;
105 m_layout = layout;
105
106
106 for (int i = 0; i < m_bars.count(); i++)
107 for (int i = 0; i < m_bars.count(); i++)
107 m_bars.at(i)->setRect(layout.at(i));
108 m_bars.at(i)->setRect(layout.at(i));
108
109
109 positionLabels();
110 positionLabels();
110 }
111 }
111 //handlers
112 //handlers
112
113
113 void AbstractBarChartItem::handleDomainUpdated()
114 void AbstractBarChartItem::handleDomainUpdated()
114 {
115 {
115 m_domainMinX = domain()->minX();
116 m_domainMinX = domain()->minX();
116 m_domainMaxX = domain()->maxX();
117 m_domainMaxX = domain()->maxX();
117 m_domainMinY = domain()->minY();
118 m_domainMinY = domain()->minY();
118 m_domainMaxY = domain()->maxY();
119 m_domainMaxY = domain()->maxY();
119
120
120 QRectF rect(QPointF(0,0),domain()->size());
121 QRectF rect(QPointF(0,0),domain()->size());
121
122
122 if(m_rect != rect){
123 if(m_rect != rect){
123 prepareGeometryChange();
124 prepareGeometryChange();
124 m_rect = rect;
125 m_rect = rect;
125 }
126 }
126
127
127 handleLayoutChanged();
128 handleLayoutChanged();
128 }
129 }
129
130
130 void AbstractBarChartItem::handleLayoutChanged()
131 void AbstractBarChartItem::handleLayoutChanged()
131 {
132 {
132 if ((m_rect.width() <= 0) || (m_rect.height() <= 0))
133 if ((m_rect.width() <= 0) || (m_rect.height() <= 0))
133 return; // rect size zero.
134 return; // rect size zero.
134 QVector<QRectF> layout = calculateLayout();
135 QVector<QRectF> layout = calculateLayout();
135 applyLayout(layout);
136 applyLayout(layout);
136 handleUpdatedBars();
137 handleUpdatedBars();
137 }
138 }
138
139
139 void AbstractBarChartItem::handleLabelsVisibleChanged(bool visible)
140 void AbstractBarChartItem::handleLabelsVisibleChanged(bool visible)
140 {
141 {
141 foreach (QGraphicsTextItem *label, m_labels)
142 foreach (QGraphicsTextItem *label, m_labels)
142 label->setVisible(visible);
143 label->setVisible(visible);
143 update();
144 update();
144 }
145 }
145
146
146 void AbstractBarChartItem::handleDataStructureChanged()
147 void AbstractBarChartItem::handleDataStructureChanged()
147 {
148 {
148 foreach (QGraphicsItem *item, childItems())
149 foreach (QGraphicsItem *item, childItems())
149 delete item;
150 delete item;
150
151
151 m_bars.clear();
152 m_bars.clear();
152 m_labels.clear();
153 m_labels.clear();
153 m_layout.clear();
154 m_layout.clear();
154
155
155 // Create new graphic items for bars
156 // Create new graphic items for bars
156 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
157 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
157 for (int s = 0; s < m_series->count(); s++) {
158 for (int s = 0; s < m_series->count(); s++) {
158 QBarSet *set = m_series->d_func()->barsetAt(s);
159 QBarSet *set = m_series->d_func()->barsetAt(s);
159
160
160 // Bars
161 // Bars
161 Bar *bar = new Bar(set, c, this);
162 Bar *bar = new Bar(set, c, this);
162 m_bars.append(bar);
163 m_bars.append(bar);
163 connect(bar, SIGNAL(clicked(int,QBarSet*)), m_series, SIGNAL(clicked(int,QBarSet*)));
164 connect(bar, SIGNAL(clicked(int,QBarSet*)), m_series, SIGNAL(clicked(int,QBarSet*)));
164 connect(bar, SIGNAL(hovered(bool, int, QBarSet*)), m_series, SIGNAL(hovered(bool, int, QBarSet*)));
165 connect(bar, SIGNAL(hovered(bool, int, QBarSet*)), m_series, SIGNAL(hovered(bool, int, QBarSet*)));
165 connect(bar, SIGNAL(pressed(int, QBarSet*)), m_series, SIGNAL(pressed(int, QBarSet*)));
166 connect(bar, SIGNAL(pressed(int, QBarSet*)), m_series, SIGNAL(pressed(int, QBarSet*)));
166 connect(bar, SIGNAL(released(int, QBarSet*)),
167 connect(bar, SIGNAL(released(int, QBarSet*)),
167 m_series, SIGNAL(released(int, QBarSet*)));
168 m_series, SIGNAL(released(int, QBarSet*)));
168 connect(bar, SIGNAL(doubleClicked(int, QBarSet*)),
169 connect(bar, SIGNAL(doubleClicked(int, QBarSet*)),
169 m_series, SIGNAL(doubleClicked(int, QBarSet*)));
170 m_series, SIGNAL(doubleClicked(int, QBarSet*)));
170 connect(bar, SIGNAL(clicked(int,QBarSet*)), set, SIGNAL(clicked(int)));
171 connect(bar, SIGNAL(clicked(int,QBarSet*)), set, SIGNAL(clicked(int)));
171 connect(bar, SIGNAL(hovered(bool, int, QBarSet*)), set, SIGNAL(hovered(bool, int)));
172 connect(bar, SIGNAL(hovered(bool, int, QBarSet*)), set, SIGNAL(hovered(bool, int)));
172 connect(bar, SIGNAL(pressed(int, QBarSet*)), set, SIGNAL(pressed(int)));
173 connect(bar, SIGNAL(pressed(int, QBarSet*)), set, SIGNAL(pressed(int)));
173 connect(bar, SIGNAL(released(int, QBarSet*)), set, SIGNAL(released(int)));
174 connect(bar, SIGNAL(released(int, QBarSet*)), set, SIGNAL(released(int)));
174 connect(bar, SIGNAL(doubleClicked(int, QBarSet*)), set, SIGNAL(doubleClicked(int)));
175 connect(bar, SIGNAL(doubleClicked(int, QBarSet*)), set, SIGNAL(doubleClicked(int)));
175 // m_layout.append(QRectF(0, 0, 1, 1));
176 // m_layout.append(QRectF(0, 0, 1, 1));
176
177
177 // Labels
178 // Labels
178 QGraphicsTextItem *newLabel = new QGraphicsTextItem(this);
179 QGraphicsTextItem *newLabel = new QGraphicsTextItem(this);
179 newLabel->document()->setDocumentMargin(ChartPresenter::textMargin());
180 newLabel->document()->setDocumentMargin(ChartPresenter::textMargin());
180 m_labels.append(newLabel);
181 m_labels.append(newLabel);
181 }
182 }
182 }
183 }
183
184
184 if(themeManager()) themeManager()->updateSeries(m_series);
185 if(themeManager()) themeManager()->updateSeries(m_series);
185 handleLayoutChanged();
186 handleLayoutChanged();
186 handleVisibleChanged();
187 handleVisibleChanged();
187 }
188 }
188
189
189 void AbstractBarChartItem::handleVisibleChanged()
190 void AbstractBarChartItem::handleVisibleChanged()
190 {
191 {
191 bool visible = m_series->isVisible();
192 bool visible = m_series->isVisible();
192 if (visible)
193 if (visible)
193 handleLabelsVisibleChanged(m_series->isLabelsVisible());
194 handleLabelsVisibleChanged(m_series->isLabelsVisible());
194 else
195 else
195 handleLabelsVisibleChanged(visible);
196 handleLabelsVisibleChanged(visible);
196
197
197 foreach (QGraphicsItem *bar, m_bars)
198 foreach (QGraphicsItem *bar, m_bars)
198 bar->setVisible(visible);
199 bar->setVisible(visible);
199 }
200 }
200
201
201 void AbstractBarChartItem::handleOpacityChanged()
202 void AbstractBarChartItem::handleOpacityChanged()
202 {
203 {
203 foreach (QGraphicsItem *item, childItems())
204 foreach (QGraphicsItem *item, childItems())
204 item->setOpacity(m_series->opacity());
205 item->setOpacity(m_series->opacity());
205 }
206 }
206
207
207 void AbstractBarChartItem::handleUpdatedBars()
208 void AbstractBarChartItem::handleUpdatedBars()
208 {
209 {
209 if (!m_series->d_func()->blockBarUpdate()) {
210 if (!m_series->d_func()->blockBarUpdate()) {
210 // Handle changes in pen, brush, labels etc.
211 // Handle changes in pen, brush, labels etc.
211 int categoryCount = m_series->d_func()->categoryCount();
212 int categoryCount = m_series->d_func()->categoryCount();
212 int setCount = m_series->count();
213 int setCount = m_series->count();
213 int itemIndex(0);
214 int itemIndex(0);
214 static const QString valueTag(QLatin1String("@value"));
215 static const QString valueTag(QLatin1String("@value"));
215
216
216 for (int category = 0; category < categoryCount; category++) {
217 for (int category = 0; category < categoryCount; category++) {
217 for (int set = 0; set < setCount; set++) {
218 for (int set = 0; set < setCount; set++) {
218 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
219 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
219 Bar *bar = m_bars.at(itemIndex);
220 Bar *bar = m_bars.at(itemIndex);
220 bar->setPen(barSet->m_pen);
221 bar->setPen(barSet->m_pen);
221 bar->setBrush(barSet->m_brush);
222 bar->setBrush(barSet->m_brush);
222 bar->update();
223 bar->update();
223
224
224 QGraphicsTextItem *label = m_labels.at(itemIndex);
225 QGraphicsTextItem *label = m_labels.at(itemIndex);
225 QString valueLabel;
226 QString valueLabel;
226 if (presenter()) { // At startup presenter is not yet set, yet somehow update comes
227 if (presenter()) { // At startup presenter is not yet set, yet somehow update comes
227 if (m_series->labelsFormat().isEmpty()) {
228 if (m_series->labelsFormat().isEmpty()) {
228 valueLabel = presenter()->numberToString(barSet->value(category));
229 valueLabel = presenter()->numberToString(barSet->value(category));
229 } else {
230 } else {
230 valueLabel = m_series->labelsFormat();
231 valueLabel = m_series->labelsFormat();
231 valueLabel.replace(valueTag,
232 valueLabel.replace(valueTag,
232 presenter()->numberToString(barSet->value(category)));
233 presenter()->numberToString(barSet->value(category)));
233 }
234 }
234 }
235 }
235 label->setHtml(valueLabel);
236 label->setHtml(valueLabel);
236 label->setFont(barSet->m_labelFont);
237 label->setFont(barSet->m_labelFont);
237 label->setDefaultTextColor(barSet->m_labelBrush.color());
238 label->setDefaultTextColor(barSet->m_labelBrush.color());
238 label->update();
239 label->update();
239 itemIndex++;
240 itemIndex++;
240 }
241 }
241 }
242 }
242 }
243 }
243 }
244 }
244
245
245 void AbstractBarChartItem::handleLabelsPositionChanged()
246 void AbstractBarChartItem::handleLabelsPositionChanged()
246 {
247 {
247 positionLabels();
248 positionLabels();
248 }
249 }
249
250
250 void AbstractBarChartItem::positionLabels()
251 void AbstractBarChartItem::positionLabels()
251 {
252 {
253 // By default position labels on horizontal bar series
254 // Vertical bar series overload positionLabels() to call positionLabelsVertical()
255
256 QTransform transform;
257 const qreal angle = m_series->d_func()->labelsAngle();
258 if (angle != 0.0)
259 transform.rotate(angle);
260
252 for (int i = 0; i < m_layout.count(); i++) {
261 for (int i = 0; i < m_layout.count(); i++) {
253 QGraphicsTextItem *label = m_labels.at(i);
262 QGraphicsTextItem *label = m_labels.at(i);
263
264 QRectF labelRect = label->boundingRect();
265 QPointF center = labelRect.center();
266
254 qreal xPos = 0;
267 qreal xPos = 0;
255 qreal yPos = m_layout.at(i).center().y() - label->boundingRect().center().y();
268 qreal yPos = m_layout.at(i).center().y() - center.y();
269
270 int xDiff = 0;
271 if (angle != 0.0) {
272 label->setTransformOriginPoint(center.x(), center.y());
273 label->setRotation(m_series->d_func()->labelsAngle());
274 qreal oldWidth = labelRect.width();
275 labelRect = transform.mapRect(labelRect);
276 xDiff = (labelRect.width() - oldWidth) / 2;
277 }
256
278
257 int offset = m_bars.at(i)->pen().width() / 2 + 2;
279 int offset = m_bars.at(i)->pen().width() / 2 + 2;
258 if (m_series->labelsPosition() == QAbstractBarSeries::LabelsCenter)
280
259 xPos = m_layout.at(i).center().x() - label->boundingRect().center().x();
281 switch (m_series->labelsPosition()) {
260 else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsInsideEnd)
282 case QAbstractBarSeries::LabelsCenter:
261 xPos = m_layout.at(i).right() - label->boundingRect().width() - offset;
283 xPos = m_layout.at(i).center().x() - center.x();
262 else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsInsideBase)
284 break;
263 xPos = m_layout.at(i).left() + offset;
285 case QAbstractBarSeries::LabelsInsideEnd:
264 else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsOutsideEnd)
286 xPos = m_layout.at(i).right() - labelRect.width() - offset + xDiff;
265 xPos = m_layout.at(i).right() + offset;
287 break;
288 case QAbstractBarSeries::LabelsInsideBase:
289 xPos = m_layout.at(i).left() + offset + xDiff;
290 break;
291 case QAbstractBarSeries::LabelsOutsideEnd:
292 xPos = m_layout.at(i).right() + offset + xDiff;
293 break;
294 default:
295 // Invalid position, never comes here
296 break;
297 }
298
299 label->setPos(xPos, yPos);
300 label->setZValue(zValue() + 1);
301 }
302 }
303
304 void AbstractBarChartItem::positionLabelsVertical()
305 {
306 QTransform transform;
307 const qreal angle = m_series->d_func()->labelsAngle();
308 if (angle != 0.0)
309 transform.rotate(angle);
310
311 for (int i = 0; i < m_layout.count(); i++) {
312 QGraphicsTextItem *label = m_labels.at(i);
313
314 QRectF labelRect = label->boundingRect();
315 QPointF center = labelRect.center();
316
317 qreal xPos = m_layout.at(i).center().x() - center.x();
318 qreal yPos = 0;
319
320 int yDiff = 0;
321 if (angle != 0.0) {
322 label->setTransformOriginPoint(center.x(), center.y());
323 label->setRotation(m_series->d_func()->labelsAngle());
324 qreal oldHeight = labelRect.height();
325 labelRect = transform.mapRect(labelRect);
326 yDiff = (labelRect.height() - oldHeight) / 2;
327 }
328
329 int offset = m_bars.at(i)->pen().width() / 2 + 2;
330
331 switch (m_series->labelsPosition()) {
332 case QAbstractBarSeries::LabelsCenter:
333 yPos = m_layout.at(i).center().y() - center.y();
334 break;
335 case QAbstractBarSeries::LabelsInsideEnd:
336 yPos = m_layout.at(i).top() + offset + yDiff;
337 break;
338 case QAbstractBarSeries::LabelsInsideBase:
339 yPos = m_layout.at(i).bottom() - labelRect.height() - offset + yDiff;
340 break;
341 case QAbstractBarSeries::LabelsOutsideEnd:
342 yPos = m_layout.at(i).top() - labelRect.height() - offset + yDiff;
343 break;
344 default:
345 // Invalid position, never comes here
346 break;
347 }
266
348
267 label->setPos(xPos, yPos);
349 label->setPos(xPos, yPos);
268 label->setZValue(zValue() + 1);
350 label->setZValue(zValue() + 1);
269 }
351 }
270 }
352 }
271
353
272 #include "moc_abstractbarchartitem_p.cpp"
354 #include "moc_abstractbarchartitem_p.cpp"
273
355
274 QT_CHARTS_END_NAMESPACE
356 QT_CHARTS_END_NAMESPACE
@@ -1,94 +1,95
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd
3 ** Copyright (C) 2015 The Qt Company Ltd
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 **
6 **
7 ** This file is part of the Qt Charts module.
7 ** This file is part of the Qt Charts module.
8 **
8 **
9 ** Licensees holding valid commercial license for Qt may use this file in
9 ** Licensees holding valid commercial license for Qt may use this file in
10 ** accordance with the Qt License Agreement provided with the Software
10 ** accordance with the Qt License Agreement provided with the Software
11 ** or, alternatively, in accordance with the terms contained in a written
11 ** or, alternatively, in accordance with the terms contained in a written
12 ** agreement between you and The Qt Company.
12 ** agreement between you and The Qt Company.
13 **
13 **
14 ** If you have questions regarding the use of this file, please use
14 ** If you have questions regarding the use of this file, please use
15 ** contact form at http://qt.io
15 ** contact form at http://qt.io
16 **
16 **
17 ****************************************************************************/
17 ****************************************************************************/
18
18
19 // W A R N I N G
19 // W A R N I N G
20 // -------------
20 // -------------
21 //
21 //
22 // This file is not part of the Qt Enterprise Chart API. It exists purely as an
22 // This file is not part of the Qt Enterprise Chart API. It exists purely as an
23 // implementation detail. This header file may change from version to
23 // implementation detail. This header file may change from version to
24 // version without notice, or even be removed.
24 // version without notice, or even be removed.
25 //
25 //
26 // We mean it.
26 // We mean it.
27
27
28
28
29 #ifndef ABSTRACTBARCHARTITEM_H
29 #ifndef ABSTRACTBARCHARTITEM_H
30 #define ABSTRACTBARCHARTITEM_H
30 #define ABSTRACTBARCHARTITEM_H
31
31
32 #include <private/chartitem_p.h>
32 #include <private/chartitem_p.h>
33 #include <QtCharts/QAbstractBarSeries>
33 #include <QtCharts/QAbstractBarSeries>
34 #include <QtGui/QPen>
34 #include <QtGui/QPen>
35 #include <QtGui/QBrush>
35 #include <QtGui/QBrush>
36
36
37 QT_CHARTS_BEGIN_NAMESPACE
37 QT_CHARTS_BEGIN_NAMESPACE
38
38
39 class Bar;
39 class Bar;
40 class QAxisCategories;
40 class QAxisCategories;
41 class QChart;
41 class QChart;
42 class BarAnimation;
42 class BarAnimation;
43
43
44 class AbstractBarChartItem : public ChartItem
44 class AbstractBarChartItem : public ChartItem
45 {
45 {
46 Q_OBJECT
46 Q_OBJECT
47 public:
47 public:
48 AbstractBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item = 0);
48 AbstractBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item = 0);
49 virtual ~AbstractBarChartItem();
49 virtual ~AbstractBarChartItem();
50
50
51 public:
51 public:
52 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
52 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
53 QRectF boundingRect() const;
53 QRectF boundingRect() const;
54
54
55 virtual QVector<QRectF> calculateLayout() = 0;
55 virtual QVector<QRectF> calculateLayout() = 0;
56 virtual void initializeLayout() = 0;
56 virtual void initializeLayout() = 0;
57 virtual void applyLayout(const QVector<QRectF> &layout);
57 virtual void applyLayout(const QVector<QRectF> &layout);
58 virtual void setAnimation(BarAnimation *animation);
58 virtual void setAnimation(BarAnimation *animation);
59 void setLayout(const QVector<QRectF> &layout);
59 void setLayout(const QVector<QRectF> &layout);
60 void updateLayout(const QVector<QRectF> &layout);
60 void updateLayout(const QVector<QRectF> &layout);
61 QRectF geometry() const { return m_rect;}
61 QRectF geometry() const { return m_rect;}
62
62
63 public Q_SLOTS:
63 public Q_SLOTS:
64 void handleDomainUpdated();
64 void handleDomainUpdated();
65 void handleLayoutChanged();
65 void handleLayoutChanged();
66 void handleLabelsVisibleChanged(bool visible);
66 void handleLabelsVisibleChanged(bool visible);
67 void handleDataStructureChanged(); // structure of of series has changed, recreate graphic items
67 void handleDataStructureChanged(); // structure of of series has changed, recreate graphic items
68 void handleVisibleChanged();
68 void handleVisibleChanged();
69 void handleOpacityChanged();
69 void handleOpacityChanged();
70 virtual void handleUpdatedBars();
70 virtual void handleUpdatedBars();
71 void handleLabelsPositionChanged();
71 void handleLabelsPositionChanged();
72 virtual void positionLabels();
72 virtual void positionLabels();
73
73
74 protected:
74 protected:
75 void positionLabelsVertical();
75
76
76 qreal m_domainMinX;
77 qreal m_domainMinX;
77 qreal m_domainMaxX;
78 qreal m_domainMaxX;
78 qreal m_domainMinY;
79 qreal m_domainMinY;
79 qreal m_domainMaxY;
80 qreal m_domainMaxY;
80
81
81 QRectF m_rect;
82 QRectF m_rect;
82 QVector<QRectF> m_layout;
83 QVector<QRectF> m_layout;
83
84
84 BarAnimation *m_animation;
85 BarAnimation *m_animation;
85
86
86 QAbstractBarSeries *m_series; // Not owned.
87 QAbstractBarSeries *m_series; // Not owned.
87 QList<Bar *> m_bars;
88 QList<Bar *> m_bars;
88 QList<QGraphicsTextItem *> m_labels;
89 QList<QGraphicsTextItem *> m_labels;
89 QSizeF m_oldSize;
90 QSizeF m_oldSize;
90 };
91 };
91
92
92 QT_CHARTS_END_NAMESPACE
93 QT_CHARTS_END_NAMESPACE
93
94
94 #endif // ABSTRACTBARCHARTITEM_H
95 #endif // ABSTRACTBARCHARTITEM_H
@@ -1,1054 +1,1092
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd
3 ** Copyright (C) 2015 The Qt Company Ltd
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 **
6 **
7 ** This file is part of the Qt Charts module.
7 ** This file is part of the Qt Charts module.
8 **
8 **
9 ** Licensees holding valid commercial license for Qt may use this file in
9 ** Licensees holding valid commercial license for Qt may use this file in
10 ** accordance with the Qt License Agreement provided with the Software
10 ** accordance with the Qt License Agreement provided with the Software
11 ** or, alternatively, in accordance with the terms contained in a written
11 ** or, alternatively, in accordance with the terms contained in a written
12 ** agreement between you and The Qt Company.
12 ** agreement between you and The Qt Company.
13 **
13 **
14 ** If you have questions regarding the use of this file, please use
14 ** If you have questions regarding the use of this file, please use
15 ** contact form at http://qt.io
15 ** contact form at http://qt.io
16 **
16 **
17 ****************************************************************************/
17 ****************************************************************************/
18
18
19 #include <QtCharts/QAbstractBarSeries>
19 #include <QtCharts/QAbstractBarSeries>
20 #include <private/qabstractbarseries_p.h>
20 #include <private/qabstractbarseries_p.h>
21 #include <QtCharts/QBarSet>
21 #include <QtCharts/QBarSet>
22 #include <private/qbarset_p.h>
22 #include <private/qbarset_p.h>
23 #include <private/abstractdomain_p.h>
23 #include <private/abstractdomain_p.h>
24 #include <private/chartdataset_p.h>
24 #include <private/chartdataset_p.h>
25 #include <private/charttheme_p.h>
25 #include <private/charttheme_p.h>
26 #include <QtCharts/QValueAxis>
26 #include <QtCharts/QValueAxis>
27 #include <QtCharts/QBarCategoryAxis>
27 #include <QtCharts/QBarCategoryAxis>
28 #include <QtCharts/QBarLegendMarker>
28 #include <QtCharts/QBarLegendMarker>
29 #include <private/baranimation_p.h>
29 #include <private/baranimation_p.h>
30 #include <private/abstractbarchartitem_p.h>
30 #include <private/abstractbarchartitem_p.h>
31 #include <private/qchart_p.h>
31 #include <private/qchart_p.h>
32
32
33 QT_CHARTS_BEGIN_NAMESPACE
33 QT_CHARTS_BEGIN_NAMESPACE
34
34
35 /*!
35 /*!
36 \class QAbstractBarSeries
36 \class QAbstractBarSeries
37 \inmodule Qt Charts
37 \inmodule Qt Charts
38 \brief Series for creating a bar chart.
38 \brief Series for creating a bar chart.
39
39
40 QAbstractBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
40 QAbstractBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
41 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
41 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
42 and y-value is the height of the bar. The category names are ignored with this series and x-axis
42 and y-value is the height of the bar. The category names are ignored with this series and x-axis
43 shows the x-values.
43 shows the x-values.
44
44
45 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
45 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
46 \image examples_barchart.png
46 \image examples_barchart.png
47
47
48 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
48 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
49 */
49 */
50 /*!
50 /*!
51 \qmltype AbstractBarSeries
51 \qmltype AbstractBarSeries
52 \instantiates QAbstractBarSeries
52 \instantiates QAbstractBarSeries
53 \inqmlmodule QtCharts
53 \inqmlmodule QtCharts
54
54
55 \inherits AbstractSeries
55 \inherits AbstractSeries
56
56
57 \brief Series type for creating a bar chart.
57 \brief Series type for creating a bar chart.
58
58
59 The following QML shows how to create a simple bar chart:
59 The following QML shows how to create a simple bar chart:
60 \snippet qmlchart/qml/qmlchart/View6.qml 1
60 \snippet qmlchart/qml/qmlchart/View6.qml 1
61
61
62 \beginfloatleft
62 \beginfloatleft
63 \image examples_qmlchart6.png
63 \image examples_qmlchart6.png
64 \endfloat
64 \endfloat
65 \clearfloat
65 \clearfloat
66 */
66 */
67
67
68 /*!
68 /*!
69 \qmlproperty AbstractAxis AbstractBarSeries::axisX
69 \qmlproperty AbstractAxis AbstractBarSeries::axisX
70 The x axis used for the series. If you leave both axisX and axisXTop undefined, a BarCategoriesAxis is created for
70 The x axis used for the series. If you leave both axisX and axisXTop undefined, a BarCategoriesAxis is created for
71 the series.
71 the series.
72 \sa axisXTop
72 \sa axisXTop
73 */
73 */
74
74
75 /*!
75 /*!
76 \qmlproperty AbstractAxis AbstractBarSeries::axisY
76 \qmlproperty AbstractAxis AbstractBarSeries::axisY
77 The y axis used for the series. If you leave both axisY and axisYRight undefined, a ValueAxis is created for
77 The y axis used for the series. If you leave both axisY and axisYRight undefined, a ValueAxis is created for
78 the series.
78 the series.
79 \sa axisYRight
79 \sa axisYRight
80 */
80 */
81
81
82 /*!
82 /*!
83 \qmlproperty AbstractAxis AbstractBarSeries::axisXTop
83 \qmlproperty AbstractAxis AbstractBarSeries::axisXTop
84 The x axis used for the series, drawn on top of the chart view. Note that you can only provide either axisX or
84 The x axis used for the series, drawn on top of the chart view. Note that you can only provide either axisX or
85 axisXTop, but not both.
85 axisXTop, but not both.
86 \sa axisX
86 \sa axisX
87 */
87 */
88
88
89 /*!
89 /*!
90 \qmlproperty AbstractAxis AbstractBarSeries::axisYRight
90 \qmlproperty AbstractAxis AbstractBarSeries::axisYRight
91 The y axis used for the series, drawn to the right on the chart view. Note that you can only provide either axisY
91 The y axis used for the series, drawn to the right on the chart view. Note that you can only provide either axisY
92 or axisYRight, but not both.
92 or axisYRight, but not both.
93 \sa axisY
93 \sa axisY
94 */
94 */
95
95
96 /*!
96 /*!
97 \property QAbstractBarSeries::barWidth
97 \property QAbstractBarSeries::barWidth
98 The width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
98 The width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
99 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
99 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
100 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
100 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
101 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
101 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
102 \sa QBarSeries
102 \sa QBarSeries
103 */
103 */
104 /*!
104 /*!
105 \qmlproperty real AbstractBarSeries::barWidth
105 \qmlproperty real AbstractBarSeries::barWidth
106 The width of the bars of the series. The unit of width is the unit of x-axis. The minimum width for bars
106 The width of the bars of the series. The unit of width is the unit of x-axis. The minimum width for bars
107 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
107 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
108 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
108 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
109 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
109 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
110 */
110 */
111
111
112 /*!
112 /*!
113 \property QAbstractBarSeries::count
113 \property QAbstractBarSeries::count
114 Holds the number of sets in series.
114 Holds the number of sets in series.
115 */
115 */
116 /*!
116 /*!
117 \qmlproperty int AbstractBarSeries::count
117 \qmlproperty int AbstractBarSeries::count
118 Holds the number of sets in series.
118 Holds the number of sets in series.
119 */
119 */
120
120
121 /*!
121 /*!
122 \property QAbstractBarSeries::labelsVisible
122 \property QAbstractBarSeries::labelsVisible
123 Defines the visibility of the labels in series
123 Defines the visibility of the labels in series
124 */
124 */
125 /*!
125 /*!
126 \qmlproperty bool AbstractBarSeries::labelsVisible
126 \qmlproperty bool AbstractBarSeries::labelsVisible
127 Defines the visibility of the labels in series
127 Defines the visibility of the labels in series
128 */
128 */
129
129
130 /*!
130 /*!
131 \property QAbstractBarSeries::labelsFormat
131 \property QAbstractBarSeries::labelsFormat
132 The \a format used for showing labels in series.
132 The \a format used for showing labels in series.
133
133
134 QAbstractBarSeries supports the following format tag:
134 QAbstractBarSeries supports the following format tag:
135 \table
135 \table
136 \row
136 \row
137 \li @value \li The value of the bar
137 \li @value \li The value of the bar
138 \endtable
138 \endtable
139
139
140 For example, the following usage of the format tags would produce labels that show the value
140 For example, the following usage of the format tags would produce labels that show the value
141 followed by unit ('u'):
141 followed by unit ('u'):
142 \code
142 \code
143 series->setLabelsFormat("@value u");
143 series->setLabelsFormat("@value u");
144 \endcode
144 \endcode
145
145
146 By default, the labels shows the value of the bar. For percent bar series '%' is added after
146 By default, the labels shows the value of the bar. For percent bar series '%' is added after
147 the value. The labels are shown on the plot area, labels on the edge of the plot area are cut.
147 the value. The labels are shown on the plot area, labels on the edge of the plot area are cut.
148 If the bars are close to each other the labels may overlap.
148 If the bars are close to each other the labels may overlap.
149
149
150 \sa QAbstractBarSeries::labelsVisible, QAbstractBarSeries::labelsPosition
150 \sa QAbstractBarSeries::labelsVisible, QAbstractBarSeries::labelsPosition
151 */
151 */
152 /*!
152 /*!
153 \qmlproperty string AbstractBarSeries::labelsFormat
153 \qmlproperty string AbstractBarSeries::labelsFormat
154 The format used for showing labels in series.
154 The format used for showing labels in series.
155
155
156 \sa QAbstractBarSeries::labelsFormat, labelsVisible, labelsPosition
156 \sa QAbstractBarSeries::labelsFormat, labelsVisible, labelsPosition
157 */
157 */
158 /*!
158 /*!
159 \fn void QAbstractBarSeries::labelsFormatChanged(const QString &format)
159 \fn void QAbstractBarSeries::labelsFormatChanged(const QString &format)
160 Signal is emitted when the \a format of data value labels is changed.
160 Signal is emitted when the \a format of data value labels is changed.
161 */
161 */
162 /*!
162 /*!
163 \qmlsignal XYSeries::onLabelsFormatChanged(string format)
163 \qmlsignal XYSeries::onLabelsFormatChanged(string format)
164 Signal is emitted when the \a format of data value labels is changed.
164 Signal is emitted when the \a format of data value labels is changed.
165 */
165 */
166
166
167 /*!
167 /*!
168 \enum QAbstractBarSeries::LabelsPosition
168 \enum QAbstractBarSeries::LabelsPosition
169
169
170 This enum describes the position of the data value labels.
170 This enum describes the position of the data value labels.
171
171
172 \value LabelsCenter Label is in the center of the bar.
172 \value LabelsCenter Label is in the center of the bar.
173 \value LabelsInsideEnd Label is inside the bar at the high end of it.
173 \value LabelsInsideEnd Label is inside the bar at the high end of it.
174 \value LabelsInsideBase Label is inside the bar at the low end of it.
174 \value LabelsInsideBase Label is inside the bar at the low end of it.
175 \value LabelsOutsideEnd Label is outside the bar at the high end of it.
175 \value LabelsOutsideEnd Label is outside the bar at the high end of it.
176 */
176 */
177
177
178 /*!
178 /*!
179 \property QAbstractBarSeries::labelsPosition
179 \property QAbstractBarSeries::labelsPosition
180 Defines the \a position of value labels.
180 Defines the \a position of value labels.
181
181
182 \sa QAbstractBarSeries::labelsVisible, QAbstractBarSeries::labelsFormat
182 \sa QAbstractBarSeries::labelsVisible, QAbstractBarSeries::labelsFormat
183 */
183 */
184 /*!
184 /*!
185 \qmlproperty string AbstractBarSeries::labelsPosition
185 \qmlproperty string AbstractBarSeries::labelsPosition
186 Defines the \a position of value labels.
186 Defines the \a position of value labels.
187
187
188 \sa labelsVisible, labelsFormat
188 \sa labelsVisible, labelsFormat
189 */
189 */
190 /*!
190 /*!
191 \fn void QAbstractBarSeries::labelsPositionChanged(QAbstractBarSeries::LabelsPosition position)
191 \fn void QAbstractBarSeries::labelsPositionChanged(QAbstractBarSeries::LabelsPosition position)
192 Signal is emitted when the \a position of value labels is changed.
192 Signal is emitted when the \a position of value labels is changed.
193 */
193 */
194 /*!
194 /*!
195 \qmlsignal AbstractBarSeries::onLabelsPositionChanged(LabelsPosition position)
195 \qmlsignal AbstractBarSeries::onLabelsPositionChanged(LabelsPosition position)
196 Signal is emitted when the \a position of value labels is changed.
196 Signal is emitted when the \a position of value labels is changed.
197 */
197 */
198
198
199 /*!
199 /*!
200 \property QAbstractBarSeries::labelsAngle
201 The angle of the value labels in degrees.
202 */
203 /*!
204 \qmlproperty qreal QAbstractBarSeries::labelsAngle
205 The angle of the value labels in degrees.
206 */
207 /*!
208 \fn void QAbstractBarSeries::labelsAngleChanged(qreal angle)
209 Signal is emitted when the \a angle of the value labels is changed.
210 */
211 /*!
212 \qmlsignal AbstractBarSeries::onLabelsAngleChanged(qreal angle)
213 Signal is emitted when the \a angle of the value labels is changed.
214 */
215
216 /*!
200 \fn void QAbstractBarSeries::clicked(int index, QBarSet *barset)
217 \fn void QAbstractBarSeries::clicked(int index, QBarSet *barset)
201 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
218 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
202 Clicked bar inside set is indexed by \a index
219 Clicked bar inside set is indexed by \a index
203 */
220 */
204 /*!
221 /*!
205 \qmlsignal AbstractBarSeries::onClicked(int index, BarSet barset)
222 \qmlsignal AbstractBarSeries::onClicked(int index, BarSet barset)
206 The signal is emitted if the user clicks with a mouse on top of BarSet.
223 The signal is emitted if the user clicks with a mouse on top of BarSet.
207 Clicked bar inside set is indexed by \a index
224 Clicked bar inside set is indexed by \a index
208 */
225 */
209
226
210 /*!
227 /*!
211 \fn void QAbstractBarSeries::pressed(int index, QBarSet *barset)
228 \fn void QAbstractBarSeries::pressed(int index, QBarSet *barset)
212 The signal is emitted if the user presses with a mouse on top of QBarSet \a barset.
229 The signal is emitted if the user presses with a mouse on top of QBarSet \a barset.
213 Pressed bar inside set is indexed by \a index
230 Pressed bar inside set is indexed by \a index
214 */
231 */
215 /*!
232 /*!
216 \qmlsignal AbstractBarSeries::onPressed(int index, BarSet barset)
233 \qmlsignal AbstractBarSeries::onPressed(int index, BarSet barset)
217 The signal is emitted if the user presses with a mouse on top of BarSet.
234 The signal is emitted if the user presses with a mouse on top of BarSet.
218 Pressed bar inside set is indexed by \a index
235 Pressed bar inside set is indexed by \a index
219 */
236 */
220
237
221 /*!
238 /*!
222 \fn void QAbstractBarSeries::released(int index, QBarSet *barset)
239 \fn void QAbstractBarSeries::released(int index, QBarSet *barset)
223 The signal is emitted if the user releases with a mouse on top of QBarSet \a barset.
240 The signal is emitted if the user releases with a mouse on top of QBarSet \a barset.
224 Released bar inside set is indexed by \a index
241 Released bar inside set is indexed by \a index
225 */
242 */
226 /*!
243 /*!
227 \qmlsignal AbstractBarSeries::onReleased(int index, BarSet barset)
244 \qmlsignal AbstractBarSeries::onReleased(int index, BarSet barset)
228 The signal is emitted if the user releases with a mouse on top of BarSet.
245 The signal is emitted if the user releases with a mouse on top of BarSet.
229 Released bar inside set is indexed by \a index
246 Released bar inside set is indexed by \a index
230 */
247 */
231
248
232 /*!
249 /*!
233 \fn void QAbstractBarSeries::doubleClicked(int index, QBarSet *barset)
250 \fn void QAbstractBarSeries::doubleClicked(int index, QBarSet *barset)
234 The signal is emitted if the user doubleclicks with a mouse on top of QBarSet \a barset.
251 The signal is emitted if the user doubleclicks with a mouse on top of QBarSet \a barset.
235 DoubleClicked bar inside set is indexed by \a index
252 DoubleClicked bar inside set is indexed by \a index
236 */
253 */
237 /*!
254 /*!
238 \qmlsignal AbstractBarSeries::onDoubleClicked(int index, BarSet barset)
255 \qmlsignal AbstractBarSeries::onDoubleClicked(int index, BarSet barset)
239 The signal is emitted if the user doubleclicks with a mouse on top of BarSet.
256 The signal is emitted if the user doubleclicks with a mouse on top of BarSet.
240 Doubleclicked bar inside set is indexed by \a index
257 Doubleclicked bar inside set is indexed by \a index
241 */
258 */
242
259
243 /*!
260 /*!
244 \fn void QAbstractBarSeries::hovered(bool status, int index, QBarSet* barset)
261 \fn void QAbstractBarSeries::hovered(bool status, int index, QBarSet* barset)
245
262
246 The signal is emitted if mouse is hovered on top of series.
263 The signal is emitted if mouse is hovered on top of series.
247 Parameter \a barset is the pointer of barset, where hover happened.
264 Parameter \a barset is the pointer of barset, where hover happened.
248 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
265 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
249 Hovered bar inside the set is indexed by \a index.
266 Hovered bar inside the set is indexed by \a index.
250 */
267 */
251 /*!
268 /*!
252 \qmlsignal AbstractBarSeries::onHovered(bool status, int index, BarSet barset)
269 \qmlsignal AbstractBarSeries::onHovered(bool status, int index, BarSet barset)
253
270
254 The signal is emitted if mouse is hovered on top of series.
271 The signal is emitted if mouse is hovered on top of series.
255 Parameter \a barset is the pointer of barset, where hover happened.
272 Parameter \a barset is the pointer of barset, where hover happened.
256 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
273 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
257 Hovered bar inside the set is indexed by \a index.
274 Hovered bar inside the set is indexed by \a index.
258 */
275 */
259
276
260 /*!
277 /*!
261 \fn void QAbstractBarSeries::countChanged()
278 \fn void QAbstractBarSeries::countChanged()
262 This signal is emitted when barset count has been changed, for example by append or remove.
279 This signal is emitted when barset count has been changed, for example by append or remove.
263 */
280 */
264 /*!
281 /*!
265 \qmlsignal AbstractBarSeries::onCountChanged()
282 \qmlsignal AbstractBarSeries::onCountChanged()
266 This signal is emitted when barset count has been changed, for example by append or remove.
283 This signal is emitted when barset count has been changed, for example by append or remove.
267 */
284 */
268
285
269 /*!
286 /*!
270 \fn void QAbstractBarSeries::labelsVisibleChanged()
287 \fn void QAbstractBarSeries::labelsVisibleChanged()
271 This signal is emitted when labels visibility have changed.
288 This signal is emitted when labels visibility have changed.
272 \sa isLabelsVisible(), setLabelsVisible()
289 \sa isLabelsVisible(), setLabelsVisible()
273 */
290 */
274
291
275 /*!
292 /*!
276 \fn void QAbstractBarSeries::barsetsAdded(QList<QBarSet*> sets)
293 \fn void QAbstractBarSeries::barsetsAdded(QList<QBarSet*> sets)
277 This signal is emitted when \a sets have been added to the series.
294 This signal is emitted when \a sets have been added to the series.
278 \sa append(), insert()
295 \sa append(), insert()
279 */
296 */
280 /*!
297 /*!
281 \qmlsignal AbstractBarSeries::onBarsetsAdded(BarSet barset)
298 \qmlsignal AbstractBarSeries::onBarsetsAdded(BarSet barset)
282 Emitted when \a barset has been added to the series.
299 Emitted when \a barset has been added to the series.
283 */
300 */
284
301
285 /*!
302 /*!
286 \fn void QAbstractBarSeries::barsetsRemoved(QList<QBarSet*> sets)
303 \fn void QAbstractBarSeries::barsetsRemoved(QList<QBarSet*> sets)
287 This signal is emitted when \a sets have been removed from the series.
304 This signal is emitted when \a sets have been removed from the series.
288 \sa remove()
305 \sa remove()
289 */
306 */
290 /*!
307 /*!
291 \qmlsignal AbstractBarSeries::onBarsetsRemoved(BarSet barset)
308 \qmlsignal AbstractBarSeries::onBarsetsRemoved(BarSet barset)
292 Emitted when \a barset has been removed from the series.
309 Emitted when \a barset has been removed from the series.
293 */
310 */
294
311
295 /*!
312 /*!
296 \qmlmethod BarSet AbstractBarSeries::at(int index)
313 \qmlmethod BarSet AbstractBarSeries::at(int index)
297 Returns bar set at \a index. Returns null if the index is not valid.
314 Returns bar set at \a index. Returns null if the index is not valid.
298 */
315 */
299
316
300 /*!
317 /*!
301 \qmlmethod BarSet AbstractBarSeries::append(string label, VariantList values)
318 \qmlmethod BarSet AbstractBarSeries::append(string label, VariantList values)
302 Adds a new bar set with \a label and \a values to \a index. Values is a list of reals.
319 Adds a new bar set with \a label and \a values to \a index. Values is a list of reals.
303 For example:
320 For example:
304 \code
321 \code
305 myBarSeries.append("set 1", [0, 0.2, 0.2, 0.5, 0.4, 1.5, 0.9]);
322 myBarSeries.append("set 1", [0, 0.2, 0.2, 0.5, 0.4, 1.5, 0.9]);
306 \endcode
323 \endcode
307 */
324 */
308
325
309 /*!
326 /*!
310 \qmlmethod BarSet AbstractBarSeries::insert(int index, string label, VariantList values)
327 \qmlmethod BarSet AbstractBarSeries::insert(int index, string label, VariantList values)
311 Inserts a new bar set with \a label and \a values to \a index. Values can be a list of reals or a list of XYPoints.
328 Inserts a new bar set with \a label and \a values to \a index. Values can be a list of reals or a list of XYPoints.
312 If index is zero or smaller, the new barset is prepended. If the index is count or bigger, the new barset is
329 If index is zero or smaller, the new barset is prepended. If the index is count or bigger, the new barset is
313 appended.
330 appended.
314 \sa AbstractBarSeries::append()
331 \sa AbstractBarSeries::append()
315 */
332 */
316
333
317 /*!
334 /*!
318 \qmlmethod bool AbstractBarSeries::remove(BarSet barset)
335 \qmlmethod bool AbstractBarSeries::remove(BarSet barset)
319 Removes the barset from the series. Returns true if successful, false otherwise.
336 Removes the barset from the series. Returns true if successful, false otherwise.
320 */
337 */
321
338
322 /*!
339 /*!
323 \qmlmethod AbstractBarSeries::clear()
340 \qmlmethod AbstractBarSeries::clear()
324 Removes all barsets from the series.
341 Removes all barsets from the series.
325 */
342 */
326
343
327 /*!
344 /*!
328 Destructs abstractbarseries and owned barsets.
345 Destructs abstractbarseries and owned barsets.
329 */
346 */
330 QAbstractBarSeries::~QAbstractBarSeries()
347 QAbstractBarSeries::~QAbstractBarSeries()
331 {
348 {
332
349
333 }
350 }
334
351
335 /*!
352 /*!
336 \internal
353 \internal
337 */
354 */
338 QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &o, QObject *parent)
355 QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &o, QObject *parent)
339 : QAbstractSeries(o, parent)
356 : QAbstractSeries(o, parent)
340 {
357 {
341 Q_D(QAbstractSeries);
358 Q_D(QAbstractSeries);
342 QObject::connect(this, SIGNAL(countChanged()), d, SIGNAL(countChanged()));
359 QObject::connect(this, SIGNAL(countChanged()), d, SIGNAL(countChanged()));
343 }
360 }
344
361
345 /*!
362 /*!
346 Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
363 Sets the width of the bars of the series. The unit of \a width is the unit of x-axis. The minimum width for bars
347 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
364 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
348 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
365 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
349 Note that with \link QBarSeries \endlink this value means the width of one group of bars instead of just one bar.
366 Note that with \link QBarSeries \endlink this value means the width of one group of bars instead of just one bar.
350 */
367 */
351 void QAbstractBarSeries::setBarWidth(qreal width)
368 void QAbstractBarSeries::setBarWidth(qreal width)
352 {
369 {
353 Q_D(QAbstractBarSeries);
370 Q_D(QAbstractBarSeries);
354 d->setBarWidth(width);
371 d->setBarWidth(width);
355 }
372 }
356
373
357 /*!
374 /*!
358 Returns the width of the bars of the series.
375 Returns the width of the bars of the series.
359 \sa setBarWidth()
376 \sa setBarWidth()
360 */
377 */
361 qreal QAbstractBarSeries::barWidth() const
378 qreal QAbstractBarSeries::barWidth() const
362 {
379 {
363 Q_D(const QAbstractBarSeries);
380 Q_D(const QAbstractBarSeries);
364 return d->barWidth();
381 return d->barWidth();
365 }
382 }
366
383
367 /*!
384 /*!
368 Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
385 Adds a set of bars to series. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
369 Returns true, if appending succeeded.
386 Returns true, if appending succeeded.
370 */
387 */
371 bool QAbstractBarSeries::append(QBarSet *set)
388 bool QAbstractBarSeries::append(QBarSet *set)
372 {
389 {
373 Q_D(QAbstractBarSeries);
390 Q_D(QAbstractBarSeries);
374 bool success = d->append(set);
391 bool success = d->append(set);
375 if (success) {
392 if (success) {
376 QList<QBarSet *> sets;
393 QList<QBarSet *> sets;
377 sets.append(set);
394 sets.append(set);
378 set->setParent(this);
395 set->setParent(this);
379 emit barsetsAdded(sets);
396 emit barsetsAdded(sets);
380 emit countChanged();
397 emit countChanged();
381 }
398 }
382 return success;
399 return success;
383 }
400 }
384
401
385 /*!
402 /*!
386 Removes barset from series. Releases ownership of \a set. Deletes the set, if remove
403 Removes barset from series. Releases ownership of \a set. Deletes the set, if remove
387 was successful.
404 was successful.
388 Returns true, if set was removed.
405 Returns true, if set was removed.
389 */
406 */
390 bool QAbstractBarSeries::remove(QBarSet *set)
407 bool QAbstractBarSeries::remove(QBarSet *set)
391 {
408 {
392 Q_D(QAbstractBarSeries);
409 Q_D(QAbstractBarSeries);
393 bool success = d->remove(set);
410 bool success = d->remove(set);
394 if (success) {
411 if (success) {
395 QList<QBarSet *> sets;
412 QList<QBarSet *> sets;
396 sets.append(set);
413 sets.append(set);
397 set->setParent(0);
414 set->setParent(0);
398 emit barsetsRemoved(sets);
415 emit barsetsRemoved(sets);
399 emit countChanged();
416 emit countChanged();
400 delete set;
417 delete set;
401 set = 0;
418 set = 0;
402 }
419 }
403 return success;
420 return success;
404 }
421 }
405
422
406 /*!
423 /*!
407 Takes a single \a set from the series. Does not delete the barset object.
424 Takes a single \a set from the series. Does not delete the barset object.
408
425
409 NOTE: The series remains as the barset's parent object. You must set the
426 NOTE: The series remains as the barset's parent object. You must set the
410 parent object to take full ownership.
427 parent object to take full ownership.
411
428
412 Returns true if take was successful.
429 Returns true if take was successful.
413 */
430 */
414 bool QAbstractBarSeries::take(QBarSet *set)
431 bool QAbstractBarSeries::take(QBarSet *set)
415 {
432 {
416 Q_D(QAbstractBarSeries);
433 Q_D(QAbstractBarSeries);
417 bool success = d->remove(set);
434 bool success = d->remove(set);
418 if (success) {
435 if (success) {
419 QList<QBarSet *> sets;
436 QList<QBarSet *> sets;
420 sets.append(set);
437 sets.append(set);
421 emit barsetsRemoved(sets);
438 emit barsetsRemoved(sets);
422 emit countChanged();
439 emit countChanged();
423 }
440 }
424 return success;
441 return success;
425 }
442 }
426
443
427 /*!
444 /*!
428 Adds a list of barsets to series. Takes ownership of \a sets.
445 Adds a list of barsets to series. Takes ownership of \a sets.
429 Returns true, if all sets were appended successfully. If any of the sets is null or is already appended to series,
446 Returns true, if all sets were appended successfully. If any of the sets is null or is already appended to series,
430 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
447 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
431 and function returns false.
448 and function returns false.
432 */
449 */
433 bool QAbstractBarSeries::append(QList<QBarSet *> sets)
450 bool QAbstractBarSeries::append(QList<QBarSet *> sets)
434 {
451 {
435 Q_D(QAbstractBarSeries);
452 Q_D(QAbstractBarSeries);
436 bool success = d->append(sets);
453 bool success = d->append(sets);
437 if (success) {
454 if (success) {
438 foreach (QBarSet *set, sets)
455 foreach (QBarSet *set, sets)
439 set->setParent(this);
456 set->setParent(this);
440 emit barsetsAdded(sets);
457 emit barsetsAdded(sets);
441 emit countChanged();
458 emit countChanged();
442 }
459 }
443 return success;
460 return success;
444 }
461 }
445
462
446 /*!
463 /*!
447 Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
464 Insert a set of bars to series at \a index postion. Takes ownership of \a set. If the set is null or is already in series, it won't be appended.
448 Returns true, if inserting succeeded.
465 Returns true, if inserting succeeded.
449
466
450 */
467 */
451 bool QAbstractBarSeries::insert(int index, QBarSet *set)
468 bool QAbstractBarSeries::insert(int index, QBarSet *set)
452 {
469 {
453 Q_D(QAbstractBarSeries);
470 Q_D(QAbstractBarSeries);
454 bool success = d->insert(index, set);
471 bool success = d->insert(index, set);
455 if (success) {
472 if (success) {
456 QList<QBarSet *> sets;
473 QList<QBarSet *> sets;
457 sets.append(set);
474 sets.append(set);
458 emit barsetsAdded(sets);
475 emit barsetsAdded(sets);
459 emit countChanged();
476 emit countChanged();
460 }
477 }
461 return success;
478 return success;
462 }
479 }
463
480
464 /*!
481 /*!
465 Removes all barsets from the series. Deletes removed sets.
482 Removes all barsets from the series. Deletes removed sets.
466 */
483 */
467 void QAbstractBarSeries::clear()
484 void QAbstractBarSeries::clear()
468 {
485 {
469 Q_D(QAbstractBarSeries);
486 Q_D(QAbstractBarSeries);
470 QList<QBarSet *> sets = barSets();
487 QList<QBarSet *> sets = barSets();
471 bool success = d->remove(sets);
488 bool success = d->remove(sets);
472 if (success) {
489 if (success) {
473 emit barsetsRemoved(sets);
490 emit barsetsRemoved(sets);
474 emit countChanged();
491 emit countChanged();
475 foreach (QBarSet *set, sets)
492 foreach (QBarSet *set, sets)
476 delete set;
493 delete set;
477 }
494 }
478 }
495 }
479
496
480 /*!
497 /*!
481 Returns number of sets in series.
498 Returns number of sets in series.
482 */
499 */
483 int QAbstractBarSeries::count() const
500 int QAbstractBarSeries::count() const
484 {
501 {
485 Q_D(const QAbstractBarSeries);
502 Q_D(const QAbstractBarSeries);
486 return d->m_barSets.count();
503 return d->m_barSets.count();
487 }
504 }
488
505
489 /*!
506 /*!
490 Returns a list of sets in series. Keeps ownership of sets.
507 Returns a list of sets in series. Keeps ownership of sets.
491 */
508 */
492 QList<QBarSet *> QAbstractBarSeries::barSets() const
509 QList<QBarSet *> QAbstractBarSeries::barSets() const
493 {
510 {
494 Q_D(const QAbstractBarSeries);
511 Q_D(const QAbstractBarSeries);
495 return d->m_barSets;
512 return d->m_barSets;
496 }
513 }
497
514
498 /*!
515 /*!
499 Sets the visibility of labels in series to \a visible
516 Sets the visibility of labels in series to \a visible
500 */
517 */
501 void QAbstractBarSeries::setLabelsVisible(bool visible)
518 void QAbstractBarSeries::setLabelsVisible(bool visible)
502 {
519 {
503 Q_D(QAbstractBarSeries);
520 Q_D(QAbstractBarSeries);
504 if (d->m_labelsVisible != visible) {
521 if (d->m_labelsVisible != visible) {
505 d->setLabelsVisible(visible);
522 d->setLabelsVisible(visible);
506 emit labelsVisibleChanged();
523 emit labelsVisibleChanged();
507 }
524 }
508 }
525 }
509
526
510 /*!
527 /*!
511 Returns the visibility of labels
528 Returns the visibility of labels
512 */
529 */
513 bool QAbstractBarSeries::isLabelsVisible() const
530 bool QAbstractBarSeries::isLabelsVisible() const
514 {
531 {
515 Q_D(const QAbstractBarSeries);
532 Q_D(const QAbstractBarSeries);
516 return d->m_labelsVisible;
533 return d->m_labelsVisible;
517 }
534 }
518
535
519 void QAbstractBarSeries::setLabelsFormat(const QString &format)
536 void QAbstractBarSeries::setLabelsFormat(const QString &format)
520 {
537 {
521 Q_D(QAbstractBarSeries);
538 Q_D(QAbstractBarSeries);
522 if (d->m_labelsFormat != format) {
539 if (d->m_labelsFormat != format) {
523 d->m_labelsFormat = format;
540 d->m_labelsFormat = format;
524 emit labelsFormatChanged(format);
541 emit labelsFormatChanged(format);
525 }
542 }
526 }
543 }
527
544
528 QString QAbstractBarSeries::labelsFormat() const
545 QString QAbstractBarSeries::labelsFormat() const
529 {
546 {
530 Q_D(const QAbstractBarSeries);
547 Q_D(const QAbstractBarSeries);
531 return d->m_labelsFormat;
548 return d->m_labelsFormat;
532 }
549 }
533
550
551 void QAbstractBarSeries::setLabelsAngle(qreal angle)
552 {
553 Q_D(QAbstractBarSeries);
554 if (d->m_labelsAngle != angle) {
555 d->m_labelsAngle = angle;
556 emit labelsAngleChanged(angle);
557 }
558 }
559
560 qreal QAbstractBarSeries::labelsAngle() const
561 {
562 Q_D(const QAbstractBarSeries);
563 return d->m_labelsAngle;
564 }
565
534 void QAbstractBarSeries::setLabelsPosition(QAbstractBarSeries::LabelsPosition position)
566 void QAbstractBarSeries::setLabelsPosition(QAbstractBarSeries::LabelsPosition position)
535 {
567 {
536 Q_D(QAbstractBarSeries);
568 Q_D(QAbstractBarSeries);
537 if (d->m_labelsPosition != position) {
569 if (d->m_labelsPosition != position) {
538 d->m_labelsPosition = position;
570 d->m_labelsPosition = position;
539 emit labelsPositionChanged(position);
571 emit labelsPositionChanged(position);
540 }
572 }
541 }
573 }
542
574
543 QAbstractBarSeries::LabelsPosition QAbstractBarSeries::labelsPosition() const
575 QAbstractBarSeries::LabelsPosition QAbstractBarSeries::labelsPosition() const
544 {
576 {
545 Q_D(const QAbstractBarSeries);
577 Q_D(const QAbstractBarSeries);
546 return d->m_labelsPosition;
578 return d->m_labelsPosition;
547 }
579 }
548
580
549 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
581 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
550
582
551 QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) :
583 QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) :
552 QAbstractSeriesPrivate(q),
584 QAbstractSeriesPrivate(q),
553 m_barWidth(0.5), // Default value is 50% of category width
585 m_barWidth(0.5), // Default value is 50% of category width
554 m_labelsVisible(false),
586 m_labelsVisible(false),
555 m_visible(true),
587 m_visible(true),
556 m_blockBarUpdate(false),
588 m_blockBarUpdate(false),
557 m_labelsFormat(),
589 m_labelsFormat(),
558 m_labelsPosition(QAbstractBarSeries::LabelsCenter)
590 m_labelsPosition(QAbstractBarSeries::LabelsCenter),
591 m_labelsAngle(0)
559 {
592 {
560 }
593 }
561
594
562 int QAbstractBarSeriesPrivate::categoryCount() const
595 int QAbstractBarSeriesPrivate::categoryCount() const
563 {
596 {
564 // No categories defined. return count of longest set.
597 // No categories defined. return count of longest set.
565 int count = 0;
598 int count = 0;
566 for (int i = 0; i < m_barSets.count(); i++) {
599 for (int i = 0; i < m_barSets.count(); i++) {
567 if (m_barSets.at(i)->count() > count)
600 if (m_barSets.at(i)->count() > count)
568 count = m_barSets.at(i)->count();
601 count = m_barSets.at(i)->count();
569 }
602 }
570
603
571 return count;
604 return count;
572 }
605 }
573
606
574 void QAbstractBarSeriesPrivate::setBarWidth(qreal width)
607 void QAbstractBarSeriesPrivate::setBarWidth(qreal width)
575 {
608 {
576 if (width < 0.0)
609 if (width < 0.0)
577 width = 0.0;
610 width = 0.0;
578 m_barWidth = width;
611 m_barWidth = width;
579 emit updatedLayout();
612 emit updatedLayout();
580 }
613 }
581
614
582 qreal QAbstractBarSeriesPrivate::barWidth() const
615 qreal QAbstractBarSeriesPrivate::barWidth() const
583 {
616 {
584 return m_barWidth;
617 return m_barWidth;
585 }
618 }
586
619
587 QBarSet *QAbstractBarSeriesPrivate::barsetAt(int index)
620 QBarSet *QAbstractBarSeriesPrivate::barsetAt(int index)
588 {
621 {
589 return m_barSets.at(index);
622 return m_barSets.at(index);
590 }
623 }
591
624
592 void QAbstractBarSeriesPrivate::setVisible(bool visible)
625 void QAbstractBarSeriesPrivate::setVisible(bool visible)
593 {
626 {
594 m_visible = visible;
627 m_visible = visible;
595 emit visibleChanged();
628 emit visibleChanged();
596 }
629 }
597
630
598 void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible)
631 void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible)
599 {
632 {
600 m_labelsVisible = visible;
633 m_labelsVisible = visible;
601 emit labelsVisibleChanged(visible);
634 emit labelsVisibleChanged(visible);
602 }
635 }
603
636
604 qreal QAbstractBarSeriesPrivate::min()
637 qreal QAbstractBarSeriesPrivate::min()
605 {
638 {
606 if (m_barSets.count() <= 0)
639 if (m_barSets.count() <= 0)
607 return 0;
640 return 0;
608
641
609 qreal min = INT_MAX;
642 qreal min = INT_MAX;
610
643
611 for (int i = 0; i < m_barSets.count(); i++) {
644 for (int i = 0; i < m_barSets.count(); i++) {
612 int categoryCount = m_barSets.at(i)->count();
645 int categoryCount = m_barSets.at(i)->count();
613 for (int j = 0; j < categoryCount; j++) {
646 for (int j = 0; j < categoryCount; j++) {
614 qreal temp = m_barSets.at(i)->at(j);
647 qreal temp = m_barSets.at(i)->at(j);
615 if (temp < min)
648 if (temp < min)
616 min = temp;
649 min = temp;
617 }
650 }
618 }
651 }
619 return min;
652 return min;
620 }
653 }
621
654
622 qreal QAbstractBarSeriesPrivate::max()
655 qreal QAbstractBarSeriesPrivate::max()
623 {
656 {
624 if (m_barSets.count() <= 0)
657 if (m_barSets.count() <= 0)
625 return 0;
658 return 0;
626
659
627 qreal max = INT_MIN;
660 qreal max = INT_MIN;
628
661
629 for (int i = 0; i < m_barSets.count(); i++) {
662 for (int i = 0; i < m_barSets.count(); i++) {
630 int categoryCount = m_barSets.at(i)->count();
663 int categoryCount = m_barSets.at(i)->count();
631 for (int j = 0; j < categoryCount; j++) {
664 for (int j = 0; j < categoryCount; j++) {
632 qreal temp = m_barSets.at(i)->at(j);
665 qreal temp = m_barSets.at(i)->at(j);
633 if (temp > max)
666 if (temp > max)
634 max = temp;
667 max = temp;
635 }
668 }
636 }
669 }
637
670
638 return max;
671 return max;
639 }
672 }
640
673
641 qreal QAbstractBarSeriesPrivate::valueAt(int set, int category)
674 qreal QAbstractBarSeriesPrivate::valueAt(int set, int category)
642 {
675 {
643 if ((set < 0) || (set >= m_barSets.count()))
676 if ((set < 0) || (set >= m_barSets.count()))
644 return 0; // No set, no value.
677 return 0; // No set, no value.
645 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
678 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
646 return 0; // No category, no value.
679 return 0; // No category, no value.
647
680
648 return m_barSets.at(set)->at(category);
681 return m_barSets.at(set)->at(category);
649 }
682 }
650
683
651 qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category)
684 qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category)
652 {
685 {
653 if ((set < 0) || (set >= m_barSets.count()))
686 if ((set < 0) || (set >= m_barSets.count()))
654 return 0; // No set, no value.
687 return 0; // No set, no value.
655 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
688 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
656 return 0; // No category, no value.
689 return 0; // No category, no value.
657
690
658 qreal value = m_barSets.at(set)->at(category);
691 qreal value = m_barSets.at(set)->at(category);
659 qreal sum = categorySum(category);
692 qreal sum = categorySum(category);
660 if (qFuzzyCompare(sum, 0))
693 if (qFuzzyCompare(sum, 0))
661 return 0;
694 return 0;
662
695
663 return value / sum;
696 return value / sum;
664 }
697 }
665
698
666 qreal QAbstractBarSeriesPrivate::categorySum(int category)
699 qreal QAbstractBarSeriesPrivate::categorySum(int category)
667 {
700 {
668 qreal sum(0);
701 qreal sum(0);
669 int count = m_barSets.count(); // Count sets
702 int count = m_barSets.count(); // Count sets
670 for (int set = 0; set < count; set++) {
703 for (int set = 0; set < count; set++) {
671 if (category < m_barSets.at(set)->count())
704 if (category < m_barSets.at(set)->count())
672 sum += m_barSets.at(set)->at(category);
705 sum += m_barSets.at(set)->at(category);
673 }
706 }
674 return sum;
707 return sum;
675 }
708 }
676
709
677 qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category)
710 qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category)
678 {
711 {
679 qreal sum(0);
712 qreal sum(0);
680 int count = m_barSets.count(); // Count sets
713 int count = m_barSets.count(); // Count sets
681 for (int set = 0; set < count; set++) {
714 for (int set = 0; set < count; set++) {
682 if (category < m_barSets.at(set)->count())
715 if (category < m_barSets.at(set)->count())
683 sum += qAbs(m_barSets.at(set)->at(category));
716 sum += qAbs(m_barSets.at(set)->at(category));
684 }
717 }
685 return sum;
718 return sum;
686 }
719 }
687
720
688 qreal QAbstractBarSeriesPrivate::maxCategorySum()
721 qreal QAbstractBarSeriesPrivate::maxCategorySum()
689 {
722 {
690 qreal max = INT_MIN;
723 qreal max = INT_MIN;
691 int count = categoryCount();
724 int count = categoryCount();
692 for (int i = 0; i < count; i++) {
725 for (int i = 0; i < count; i++) {
693 qreal sum = categorySum(i);
726 qreal sum = categorySum(i);
694 if (sum > max)
727 if (sum > max)
695 max = sum;
728 max = sum;
696 }
729 }
697 return max;
730 return max;
698 }
731 }
699
732
700 qreal QAbstractBarSeriesPrivate::minX()
733 qreal QAbstractBarSeriesPrivate::minX()
701 {
734 {
702 if (m_barSets.count() <= 0)
735 if (m_barSets.count() <= 0)
703 return 0;
736 return 0;
704
737
705 qreal min = INT_MAX;
738 qreal min = INT_MAX;
706
739
707 for (int i = 0; i < m_barSets.count(); i++) {
740 for (int i = 0; i < m_barSets.count(); i++) {
708 int categoryCount = m_barSets.at(i)->count();
741 int categoryCount = m_barSets.at(i)->count();
709 for (int j = 0; j < categoryCount; j++) {
742 for (int j = 0; j < categoryCount; j++) {
710 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
743 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
711 if (temp < min)
744 if (temp < min)
712 min = temp;
745 min = temp;
713 }
746 }
714 }
747 }
715 return min;
748 return min;
716 }
749 }
717
750
718 qreal QAbstractBarSeriesPrivate::maxX()
751 qreal QAbstractBarSeriesPrivate::maxX()
719 {
752 {
720 if (m_barSets.count() <= 0)
753 if (m_barSets.count() <= 0)
721 return 0;
754 return 0;
722
755
723 qreal max = INT_MIN;
756 qreal max = INT_MIN;
724
757
725 for (int i = 0; i < m_barSets.count(); i++) {
758 for (int i = 0; i < m_barSets.count(); i++) {
726 int categoryCount = m_barSets.at(i)->count();
759 int categoryCount = m_barSets.at(i)->count();
727 for (int j = 0; j < categoryCount; j++) {
760 for (int j = 0; j < categoryCount; j++) {
728 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
761 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
729 if (temp > max)
762 if (temp > max)
730 max = temp;
763 max = temp;
731 }
764 }
732 }
765 }
733
766
734 return max;
767 return max;
735 }
768 }
736
769
737 qreal QAbstractBarSeriesPrivate::categoryTop(int category)
770 qreal QAbstractBarSeriesPrivate::categoryTop(int category)
738 {
771 {
739 // Returns top (sum of all positive values) of category.
772 // Returns top (sum of all positive values) of category.
740 // Returns 0, if all values are negative
773 // Returns 0, if all values are negative
741 qreal top(0);
774 qreal top(0);
742 int count = m_barSets.count();
775 int count = m_barSets.count();
743 for (int set = 0; set < count; set++) {
776 for (int set = 0; set < count; set++) {
744 if (category < m_barSets.at(set)->count()) {
777 if (category < m_barSets.at(set)->count()) {
745 qreal temp = m_barSets.at(set)->at(category);
778 qreal temp = m_barSets.at(set)->at(category);
746 if (temp > 0) {
779 if (temp > 0) {
747 top += temp;
780 top += temp;
748 }
781 }
749 }
782 }
750 }
783 }
751 return top;
784 return top;
752 }
785 }
753
786
754 qreal QAbstractBarSeriesPrivate::categoryBottom(int category)
787 qreal QAbstractBarSeriesPrivate::categoryBottom(int category)
755 {
788 {
756 // Returns bottom (sum of all negative values) of category
789 // Returns bottom (sum of all negative values) of category
757 // Returns 0, if all values are positive
790 // Returns 0, if all values are positive
758 qreal bottom(0);
791 qreal bottom(0);
759 int count = m_barSets.count();
792 int count = m_barSets.count();
760 for (int set = 0; set < count; set++) {
793 for (int set = 0; set < count; set++) {
761 if (category < m_barSets.at(set)->count()) {
794 if (category < m_barSets.at(set)->count()) {
762 qreal temp = m_barSets.at(set)->at(category);
795 qreal temp = m_barSets.at(set)->at(category);
763 if (temp < 0) {
796 if (temp < 0) {
764 bottom += temp;
797 bottom += temp;
765 }
798 }
766 }
799 }
767 }
800 }
768 return bottom;
801 return bottom;
769 }
802 }
770
803
771 qreal QAbstractBarSeriesPrivate::top()
804 qreal QAbstractBarSeriesPrivate::top()
772 {
805 {
773 // Returns top of all categories
806 // Returns top of all categories
774 qreal top(0);
807 qreal top(0);
775 int count = categoryCount();
808 int count = categoryCount();
776 for (int i = 0; i < count; i++) {
809 for (int i = 0; i < count; i++) {
777 qreal temp = categoryTop(i);
810 qreal temp = categoryTop(i);
778 if (temp > top)
811 if (temp > top)
779 top = temp;
812 top = temp;
780 }
813 }
781 return top;
814 return top;
782 }
815 }
783
816
784 qreal QAbstractBarSeriesPrivate::bottom()
817 qreal QAbstractBarSeriesPrivate::bottom()
785 {
818 {
786 // Returns bottom of all categories
819 // Returns bottom of all categories
787 qreal bottom(0);
820 qreal bottom(0);
788 int count = categoryCount();
821 int count = categoryCount();
789 for (int i = 0; i < count; i++) {
822 for (int i = 0; i < count; i++) {
790 qreal temp = categoryBottom(i);
823 qreal temp = categoryBottom(i);
791 if (temp < bottom)
824 if (temp < bottom)
792 bottom = temp;
825 bottom = temp;
793 }
826 }
794 return bottom;
827 return bottom;
795 }
828 }
796
829
797 bool QAbstractBarSeriesPrivate::blockBarUpdate()
830 bool QAbstractBarSeriesPrivate::blockBarUpdate()
798 {
831 {
799 return m_blockBarUpdate;
832 return m_blockBarUpdate;
800 }
833 }
801
834
835 qreal QAbstractBarSeriesPrivate::labelsAngle() const
836 {
837 return m_labelsAngle;
838 }
839
802 void QAbstractBarSeriesPrivate::initializeDomain()
840 void QAbstractBarSeriesPrivate::initializeDomain()
803 {
841 {
804 qreal minX(domain()->minX());
842 qreal minX(domain()->minX());
805 qreal minY(domain()->minY());
843 qreal minY(domain()->minY());
806 qreal maxX(domain()->maxX());
844 qreal maxX(domain()->maxX());
807 qreal maxY(domain()->maxY());
845 qreal maxY(domain()->maxY());
808
846
809 qreal seriesMinX = this->minX();
847 qreal seriesMinX = this->minX();
810 qreal seriesMaxX = this->maxX();
848 qreal seriesMaxX = this->maxX();
811 qreal y = max();
849 qreal y = max();
812 minX = qMin(minX, seriesMinX - (qreal)0.5);
850 minX = qMin(minX, seriesMinX - (qreal)0.5);
813 minY = qMin(minY, y);
851 minY = qMin(minY, y);
814 maxX = qMax(maxX, seriesMaxX + (qreal)0.5);
852 maxX = qMax(maxX, seriesMaxX + (qreal)0.5);
815 maxY = qMax(maxY, y);
853 maxY = qMax(maxY, y);
816
854
817 domain()->setRange(minX, maxX, minY, maxY);
855 domain()->setRange(minX, maxX, minY, maxY);
818 }
856 }
819
857
820 QList<QLegendMarker*> QAbstractBarSeriesPrivate::createLegendMarkers(QLegend* legend)
858 QList<QLegendMarker*> QAbstractBarSeriesPrivate::createLegendMarkers(QLegend* legend)
821 {
859 {
822 Q_Q(QAbstractBarSeries);
860 Q_Q(QAbstractBarSeries);
823 QList<QLegendMarker*> markers;
861 QList<QLegendMarker*> markers;
824
862
825 foreach(QBarSet* set, q->barSets()) {
863 foreach(QBarSet* set, q->barSets()) {
826 QBarLegendMarker* marker = new QBarLegendMarker(q,set,legend);
864 QBarLegendMarker* marker = new QBarLegendMarker(q,set,legend);
827 markers << marker;
865 markers << marker;
828 }
866 }
829 return markers;
867 return markers;
830 }
868 }
831
869
832
870
833 bool QAbstractBarSeriesPrivate::append(QBarSet *set)
871 bool QAbstractBarSeriesPrivate::append(QBarSet *set)
834 {
872 {
835 if ((m_barSets.contains(set)) || (set == 0))
873 if ((m_barSets.contains(set)) || (set == 0))
836 return false; // Fail if set is already in list or set is null.
874 return false; // Fail if set is already in list or set is null.
837
875
838 m_barSets.append(set);
876 m_barSets.append(set);
839 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
877 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
840 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
878 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
841 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
879 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
842
880
843 emit restructuredBars(); // this notifies barchartitem
881 emit restructuredBars(); // this notifies barchartitem
844 return true;
882 return true;
845 }
883 }
846
884
847 bool QAbstractBarSeriesPrivate::remove(QBarSet *set)
885 bool QAbstractBarSeriesPrivate::remove(QBarSet *set)
848 {
886 {
849 if (!m_barSets.contains(set))
887 if (!m_barSets.contains(set))
850 return false; // Fail if set is not in list
888 return false; // Fail if set is not in list
851
889
852 m_barSets.removeOne(set);
890 m_barSets.removeOne(set);
853 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
891 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
854 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
892 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
855 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
893 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
856
894
857 emit restructuredBars(); // this notifies barchartitem
895 emit restructuredBars(); // this notifies barchartitem
858 return true;
896 return true;
859 }
897 }
860
898
861 bool QAbstractBarSeriesPrivate::append(QList<QBarSet * > sets)
899 bool QAbstractBarSeriesPrivate::append(QList<QBarSet * > sets)
862 {
900 {
863 foreach (QBarSet *set, sets) {
901 foreach (QBarSet *set, sets) {
864 if ((set == 0) || (m_barSets.contains(set)))
902 if ((set == 0) || (m_barSets.contains(set)))
865 return false; // Fail if any of the sets is null or is already appended.
903 return false; // Fail if any of the sets is null or is already appended.
866 if (sets.count(set) != 1)
904 if (sets.count(set) != 1)
867 return false; // Also fail if same set is more than once in given list.
905 return false; // Also fail if same set is more than once in given list.
868 }
906 }
869
907
870 foreach (QBarSet *set, sets) {
908 foreach (QBarSet *set, sets) {
871 m_barSets.append(set);
909 m_barSets.append(set);
872 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
910 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
873 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
911 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
874 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
912 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
875 }
913 }
876
914
877 emit restructuredBars(); // this notifies barchartitem
915 emit restructuredBars(); // this notifies barchartitem
878 return true;
916 return true;
879 }
917 }
880
918
881 bool QAbstractBarSeriesPrivate::remove(QList<QBarSet * > sets)
919 bool QAbstractBarSeriesPrivate::remove(QList<QBarSet * > sets)
882 {
920 {
883 if (sets.count() == 0)
921 if (sets.count() == 0)
884 return false;
922 return false;
885
923
886 foreach (QBarSet *set, sets) {
924 foreach (QBarSet *set, sets) {
887 if ((set == 0) || (!m_barSets.contains(set)))
925 if ((set == 0) || (!m_barSets.contains(set)))
888 return false; // Fail if any of the sets is null or is not in series
926 return false; // Fail if any of the sets is null or is not in series
889 if (sets.count(set) != 1)
927 if (sets.count(set) != 1)
890 return false; // Also fail if same set is more than once in given list.
928 return false; // Also fail if same set is more than once in given list.
891 }
929 }
892
930
893 foreach (QBarSet *set, sets) {
931 foreach (QBarSet *set, sets) {
894 m_barSets.removeOne(set);
932 m_barSets.removeOne(set);
895 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
933 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
896 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
934 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
897 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
935 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
898 }
936 }
899
937
900 emit restructuredBars(); // this notifies barchartitem
938 emit restructuredBars(); // this notifies barchartitem
901
939
902 return true;
940 return true;
903 }
941 }
904
942
905 bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set)
943 bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set)
906 {
944 {
907 if ((m_barSets.contains(set)) || (set == 0))
945 if ((m_barSets.contains(set)) || (set == 0))
908 return false; // Fail if set is already in list or set is null.
946 return false; // Fail if set is already in list or set is null.
909
947
910 m_barSets.insert(index, set);
948 m_barSets.insert(index, set);
911 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
949 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
912 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
950 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
913 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
951 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
914
952
915 emit restructuredBars(); // this notifies barchartitem
953 emit restructuredBars(); // this notifies barchartitem
916 return true;
954 return true;
917 }
955 }
918
956
919 void QAbstractBarSeriesPrivate::initializeAxes()
957 void QAbstractBarSeriesPrivate::initializeAxes()
920 {
958 {
921 Q_Q(QAbstractBarSeries);
959 Q_Q(QAbstractBarSeries);
922
960
923 foreach(QAbstractAxis* axis, m_axes) {
961 foreach(QAbstractAxis* axis, m_axes) {
924
962
925 if (axis->type() == QAbstractAxis::AxisTypeBarCategory) {
963 if (axis->type() == QAbstractAxis::AxisTypeBarCategory) {
926 switch (q->type()) {
964 switch (q->type()) {
927 case QAbstractSeries::SeriesTypeHorizontalBar:
965 case QAbstractSeries::SeriesTypeHorizontalBar:
928 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
966 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
929 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
967 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
930 if (axis->orientation() == Qt::Vertical)
968 if (axis->orientation() == Qt::Vertical)
931 populateCategories(qobject_cast<QBarCategoryAxis *>(axis));
969 populateCategories(qobject_cast<QBarCategoryAxis *>(axis));
932 break;
970 break;
933 case QAbstractSeries::SeriesTypeBar:
971 case QAbstractSeries::SeriesTypeBar:
934 case QAbstractSeries::SeriesTypePercentBar:
972 case QAbstractSeries::SeriesTypePercentBar:
935 case QAbstractSeries::SeriesTypeStackedBar:
973 case QAbstractSeries::SeriesTypeStackedBar:
936 case QAbstractSeries::SeriesTypeBoxPlot:
974 case QAbstractSeries::SeriesTypeBoxPlot:
937 if (axis->orientation() == Qt::Horizontal)
975 if (axis->orientation() == Qt::Horizontal)
938 populateCategories(qobject_cast<QBarCategoryAxis *>(axis));
976 populateCategories(qobject_cast<QBarCategoryAxis *>(axis));
939 break;
977 break;
940 default:
978 default:
941 qWarning() << "Unexpected series type";
979 qWarning() << "Unexpected series type";
942 break;
980 break;
943 }
981 }
944 }
982 }
945 }
983 }
946 }
984 }
947
985
948 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
986 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
949 {
987 {
950 Q_Q(const QAbstractBarSeries);
988 Q_Q(const QAbstractBarSeries);
951
989
952 switch (q->type()) {
990 switch (q->type()) {
953 case QAbstractSeries::SeriesTypeHorizontalBar:
991 case QAbstractSeries::SeriesTypeHorizontalBar:
954 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
992 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
955 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
993 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
956 if (orientation == Qt::Vertical)
994 if (orientation == Qt::Vertical)
957 return QAbstractAxis::AxisTypeBarCategory;
995 return QAbstractAxis::AxisTypeBarCategory;
958 break;
996 break;
959 case QAbstractSeries::SeriesTypeBar:
997 case QAbstractSeries::SeriesTypeBar:
960 case QAbstractSeries::SeriesTypePercentBar:
998 case QAbstractSeries::SeriesTypePercentBar:
961 case QAbstractSeries::SeriesTypeStackedBar:
999 case QAbstractSeries::SeriesTypeStackedBar:
962 case QAbstractSeries::SeriesTypeBoxPlot:
1000 case QAbstractSeries::SeriesTypeBoxPlot:
963 if (orientation == Qt::Horizontal)
1001 if (orientation == Qt::Horizontal)
964 return QAbstractAxis::AxisTypeBarCategory;
1002 return QAbstractAxis::AxisTypeBarCategory;
965 break;
1003 break;
966 default:
1004 default:
967 qWarning() << "Unexpected series type";
1005 qWarning() << "Unexpected series type";
968 break;
1006 break;
969 }
1007 }
970 return QAbstractAxis::AxisTypeValue;
1008 return QAbstractAxis::AxisTypeValue;
971
1009
972 }
1010 }
973
1011
974 void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis *axis)
1012 void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis *axis)
975 {
1013 {
976 QStringList categories;
1014 QStringList categories;
977 if (axis->categories().isEmpty()) {
1015 if (axis->categories().isEmpty()) {
978 for (int i(1); i < categoryCount() + 1; i++)
1016 for (int i(1); i < categoryCount() + 1; i++)
979 categories << presenter()->numberToString(i);
1017 categories << presenter()->numberToString(i);
980 axis->append(categories);
1018 axis->append(categories);
981 }
1019 }
982 }
1020 }
983
1021
984 QAbstractAxis* QAbstractBarSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const
1022 QAbstractAxis* QAbstractBarSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const
985 {
1023 {
986 if (defaultAxisType(orientation) == QAbstractAxis::AxisTypeBarCategory)
1024 if (defaultAxisType(orientation) == QAbstractAxis::AxisTypeBarCategory)
987 return new QBarCategoryAxis;
1025 return new QBarCategoryAxis;
988 else
1026 else
989 return new QValueAxis;
1027 return new QValueAxis;
990 }
1028 }
991
1029
992 void QAbstractBarSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
1030 void QAbstractBarSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
993 {
1031 {
994 m_blockBarUpdate = true; // Ensures that the bars are not updated before the theme is ready
1032 m_blockBarUpdate = true; // Ensures that the bars are not updated before the theme is ready
995
1033
996 const QList<QGradient> gradients = theme->seriesGradients();
1034 const QList<QGradient> gradients = theme->seriesGradients();
997
1035
998 qreal takeAtPos = 0.5;
1036 qreal takeAtPos = 0.5;
999 qreal step = 0.2;
1037 qreal step = 0.2;
1000 if (m_barSets.count() > 1) {
1038 if (m_barSets.count() > 1) {
1001 step = 1.0 / (qreal) m_barSets.count();
1039 step = 1.0 / (qreal) m_barSets.count();
1002 if (m_barSets.count() % gradients.count())
1040 if (m_barSets.count() % gradients.count())
1003 step *= gradients.count();
1041 step *= gradients.count();
1004 else
1042 else
1005 step *= (gradients.count() - 1);
1043 step *= (gradients.count() - 1);
1006 }
1044 }
1007
1045
1008 for (int i(0); i < m_barSets.count(); i++) {
1046 for (int i(0); i < m_barSets.count(); i++) {
1009 int colorIndex = (index + i) % gradients.count();
1047 int colorIndex = (index + i) % gradients.count();
1010 if (i > 0 && i %gradients.count() == 0) {
1048 if (i > 0 && i %gradients.count() == 0) {
1011 // There is no dedicated base color for each sets, generate more colors
1049 // There is no dedicated base color for each sets, generate more colors
1012 takeAtPos += step;
1050 takeAtPos += step;
1013 if (takeAtPos == 1.0)
1051 if (takeAtPos == 1.0)
1014 takeAtPos += step;
1052 takeAtPos += step;
1015 takeAtPos -= (int) takeAtPos;
1053 takeAtPos -= (int) takeAtPos;
1016 }
1054 }
1017 if (forced || QChartPrivate::defaultBrush() == m_barSets.at(i)->d_ptr->m_brush)
1055 if (forced || QChartPrivate::defaultBrush() == m_barSets.at(i)->d_ptr->m_brush)
1018 m_barSets.at(i)->setBrush(ChartThemeManager::colorAt(gradients.at(colorIndex), takeAtPos));
1056 m_barSets.at(i)->setBrush(ChartThemeManager::colorAt(gradients.at(colorIndex), takeAtPos));
1019
1057
1020 // Pick label color from the opposite end of the gradient.
1058 // Pick label color from the opposite end of the gradient.
1021 // 0.3 as a boundary seems to work well.
1059 // 0.3 as a boundary seems to work well.
1022 if (forced || QChartPrivate::defaultBrush() == m_barSets.at(i)->d_ptr->m_labelBrush) {
1060 if (forced || QChartPrivate::defaultBrush() == m_barSets.at(i)->d_ptr->m_labelBrush) {
1023 if (takeAtPos < 0.3)
1061 if (takeAtPos < 0.3)
1024 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 1));
1062 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 1));
1025 else
1063 else
1026 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0));
1064 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0));
1027 }
1065 }
1028 if (forced || QChartPrivate::defaultPen() == m_barSets.at(i)->d_ptr->m_pen) {
1066 if (forced || QChartPrivate::defaultPen() == m_barSets.at(i)->d_ptr->m_pen) {
1029 QColor c = ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0);
1067 QColor c = ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0);
1030 m_barSets.at(i)->setPen(c);
1068 m_barSets.at(i)->setPen(c);
1031 }
1069 }
1032 }
1070 }
1033 m_blockBarUpdate = false;
1071 m_blockBarUpdate = false;
1034 emit updatedBars();
1072 emit updatedBars();
1035 }
1073 }
1036
1074
1037 void QAbstractBarSeriesPrivate::initializeAnimations(QChart::AnimationOptions options)
1075 void QAbstractBarSeriesPrivate::initializeAnimations(QChart::AnimationOptions options)
1038 {
1076 {
1039 AbstractBarChartItem *bar = static_cast<AbstractBarChartItem *>(m_item.data());
1077 AbstractBarChartItem *bar = static_cast<AbstractBarChartItem *>(m_item.data());
1040 Q_ASSERT(bar);
1078 Q_ASSERT(bar);
1041 if (bar->animation())
1079 if (bar->animation())
1042 bar->animation()->stopAndDestroyLater();
1080 bar->animation()->stopAndDestroyLater();
1043
1081
1044 if (options.testFlag(QChart::SeriesAnimations))
1082 if (options.testFlag(QChart::SeriesAnimations))
1045 bar->setAnimation(new BarAnimation(bar));
1083 bar->setAnimation(new BarAnimation(bar));
1046 else
1084 else
1047 bar->setAnimation(0);
1085 bar->setAnimation(0);
1048 QAbstractSeriesPrivate::initializeAnimations(options);
1086 QAbstractSeriesPrivate::initializeAnimations(options);
1049 }
1087 }
1050
1088
1051 #include "moc_qabstractbarseries.cpp"
1089 #include "moc_qabstractbarseries.cpp"
1052 #include "moc_qabstractbarseries_p.cpp"
1090 #include "moc_qabstractbarseries_p.cpp"
1053
1091
1054 QT_CHARTS_END_NAMESPACE
1092 QT_CHARTS_END_NAMESPACE
@@ -1,105 +1,110
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd
3 ** Copyright (C) 2015 The Qt Company Ltd
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 **
6 **
7 ** This file is part of the Qt Charts module.
7 ** This file is part of the Qt Charts module.
8 **
8 **
9 ** Licensees holding valid commercial license for Qt may use this file in
9 ** Licensees holding valid commercial license for Qt may use this file in
10 ** accordance with the Qt License Agreement provided with the Software
10 ** accordance with the Qt License Agreement provided with the Software
11 ** or, alternatively, in accordance with the terms contained in a written
11 ** or, alternatively, in accordance with the terms contained in a written
12 ** agreement between you and The Qt Company.
12 ** agreement between you and The Qt Company.
13 **
13 **
14 ** If you have questions regarding the use of this file, please use
14 ** If you have questions regarding the use of this file, please use
15 ** contact form at http://qt.io
15 ** contact form at http://qt.io
16 **
16 **
17 ****************************************************************************/
17 ****************************************************************************/
18
18
19 #ifndef QABSTRACTBARSERIES_H
19 #ifndef QABSTRACTBARSERIES_H
20 #define QABSTRACTBARSERIES_H
20 #define QABSTRACTBARSERIES_H
21
21
22 #include <QtCharts/QAbstractSeries>
22 #include <QtCharts/QAbstractSeries>
23 #include <QtCore/QStringList>
23 #include <QtCore/QStringList>
24
24
25 QT_CHARTS_BEGIN_NAMESPACE
25 QT_CHARTS_BEGIN_NAMESPACE
26
26
27 class QBarSet;
27 class QBarSet;
28 class QAbstractBarSeriesPrivate;
28 class QAbstractBarSeriesPrivate;
29
29
30 // Container for series
30 // Container for series
31 class QT_CHARTS_EXPORT QAbstractBarSeries : public QAbstractSeries
31 class QT_CHARTS_EXPORT QAbstractBarSeries : public QAbstractSeries
32 {
32 {
33 Q_OBJECT
33 Q_OBJECT
34 Q_PROPERTY(qreal barWidth READ barWidth WRITE setBarWidth)
34 Q_PROPERTY(qreal barWidth READ barWidth WRITE setBarWidth)
35 Q_PROPERTY(int count READ count NOTIFY countChanged)
35 Q_PROPERTY(int count READ count NOTIFY countChanged)
36 Q_PROPERTY(bool labelsVisible READ isLabelsVisible WRITE setLabelsVisible NOTIFY labelsVisibleChanged)
36 Q_PROPERTY(bool labelsVisible READ isLabelsVisible WRITE setLabelsVisible NOTIFY labelsVisibleChanged)
37 Q_PROPERTY(QString labelsFormat READ labelsFormat WRITE setLabelsFormat NOTIFY labelsFormatChanged)
37 Q_PROPERTY(QString labelsFormat READ labelsFormat WRITE setLabelsFormat NOTIFY labelsFormatChanged)
38 Q_PROPERTY(LabelsPosition labelsPosition READ labelsPosition WRITE setLabelsPosition NOTIFY labelsPositionChanged)
38 Q_PROPERTY(LabelsPosition labelsPosition READ labelsPosition WRITE setLabelsPosition NOTIFY labelsPositionChanged)
39 Q_PROPERTY(qreal labelsAngle READ labelsAngle WRITE setLabelsAngle NOTIFY labelsAngleChanged)
39 Q_ENUMS(LabelsPosition)
40 Q_ENUMS(LabelsPosition)
40
41
41 public:
42 public:
42 enum LabelsPosition {
43 enum LabelsPosition {
43 LabelsCenter = 0,
44 LabelsCenter = 0,
44 LabelsInsideEnd,
45 LabelsInsideEnd,
45 LabelsInsideBase,
46 LabelsInsideBase,
46 LabelsOutsideEnd
47 LabelsOutsideEnd
47 };
48 };
48
49
49 public:
50 public:
50 virtual ~QAbstractBarSeries();
51 virtual ~QAbstractBarSeries();
51
52
52 void setBarWidth(qreal width);
53 void setBarWidth(qreal width);
53 qreal barWidth() const;
54 qreal barWidth() const;
54
55
55 bool append(QBarSet *set);
56 bool append(QBarSet *set);
56 bool remove(QBarSet *set);
57 bool remove(QBarSet *set);
57 bool take(QBarSet *set);
58 bool take(QBarSet *set);
58 bool append(QList<QBarSet *> sets);
59 bool append(QList<QBarSet *> sets);
59 bool insert(int index, QBarSet *set);
60 bool insert(int index, QBarSet *set);
60 int count() const;
61 int count() const;
61 QList<QBarSet *> barSets() const;
62 QList<QBarSet *> barSets() const;
62 void clear();
63 void clear();
63
64
64 void setLabelsVisible(bool visible = true);
65 void setLabelsVisible(bool visible = true);
65 bool isLabelsVisible() const;
66 bool isLabelsVisible() const;
66
67
67 void setLabelsFormat(const QString &format);
68 void setLabelsFormat(const QString &format);
68 QString labelsFormat() const;
69 QString labelsFormat() const;
69
70
71 void setLabelsAngle(qreal angle);
72 qreal labelsAngle() const;
73
70 void setLabelsPosition(QAbstractBarSeries::LabelsPosition position);
74 void setLabelsPosition(QAbstractBarSeries::LabelsPosition position);
71 QAbstractBarSeries::LabelsPosition labelsPosition() const;
75 QAbstractBarSeries::LabelsPosition labelsPosition() const;
72
76
73 protected:
77 protected:
74 explicit QAbstractBarSeries(QAbstractBarSeriesPrivate &d, QObject *parent = 0);
78 explicit QAbstractBarSeries(QAbstractBarSeriesPrivate &d, QObject *parent = 0);
75
79
76 Q_SIGNALS:
80 Q_SIGNALS:
77 void clicked(int index, QBarSet *barset);
81 void clicked(int index, QBarSet *barset);
78 void hovered(bool status, int index, QBarSet *barset);
82 void hovered(bool status, int index, QBarSet *barset);
79 void pressed(int index, QBarSet *barset);
83 void pressed(int index, QBarSet *barset);
80 void released(int index, QBarSet *barset);
84 void released(int index, QBarSet *barset);
81 void doubleClicked(int index, QBarSet *barset);
85 void doubleClicked(int index, QBarSet *barset);
82 void countChanged();
86 void countChanged();
83 void labelsVisibleChanged();
87 void labelsVisibleChanged();
84 void labelsFormatChanged(const QString &format);
88 void labelsFormatChanged(const QString &format);
85 void labelsPositionChanged(QAbstractBarSeries::LabelsPosition position);
89 void labelsPositionChanged(QAbstractBarSeries::LabelsPosition position);
90 void labelsAngleChanged(qreal angle);
86
91
87 void barsetsAdded(QList<QBarSet *> sets);
92 void barsetsAdded(QList<QBarSet *> sets);
88 void barsetsRemoved(QList<QBarSet *> sets);
93 void barsetsRemoved(QList<QBarSet *> sets);
89
94
90 protected:
95 protected:
91 Q_DECLARE_PRIVATE(QAbstractBarSeries)
96 Q_DECLARE_PRIVATE(QAbstractBarSeries)
92 friend class AbstractBarChartItem;
97 friend class AbstractBarChartItem;
93 friend class PercentBarChartItem;
98 friend class PercentBarChartItem;
94 friend class StackedBarChartItem;
99 friend class StackedBarChartItem;
95 friend class BoxPlotChartItem;
100 friend class BoxPlotChartItem;
96 friend class BarChartItem;
101 friend class BarChartItem;
97 friend class HorizontalBarChartItem;
102 friend class HorizontalBarChartItem;
98 friend class HorizontalStackedBarChartItem;
103 friend class HorizontalStackedBarChartItem;
99 friend class HorizontalPercentBarChartItem;
104 friend class HorizontalPercentBarChartItem;
100 friend class BarSet;
105 friend class BarSet;
101 };
106 };
102
107
103 QT_CHARTS_END_NAMESPACE
108 QT_CHARTS_END_NAMESPACE
104
109
105 #endif // QABSTRACTBARSERIES_H
110 #endif // QABSTRACTBARSERIES_H
@@ -1,119 +1,122
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd
3 ** Copyright (C) 2015 The Qt Company Ltd
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 **
6 **
7 ** This file is part of the Qt Charts module.
7 ** This file is part of the Qt Charts module.
8 **
8 **
9 ** Licensees holding valid commercial license for Qt may use this file in
9 ** Licensees holding valid commercial license for Qt may use this file in
10 ** accordance with the Qt License Agreement provided with the Software
10 ** accordance with the Qt License Agreement provided with the Software
11 ** or, alternatively, in accordance with the terms contained in a written
11 ** or, alternatively, in accordance with the terms contained in a written
12 ** agreement between you and The Qt Company.
12 ** agreement between you and The Qt Company.
13 **
13 **
14 ** If you have questions regarding the use of this file, please use
14 ** If you have questions regarding the use of this file, please use
15 ** contact form at http://qt.io
15 ** contact form at http://qt.io
16 **
16 **
17 ****************************************************************************/
17 ****************************************************************************/
18
18
19 // W A R N I N G
19 // W A R N I N G
20 // -------------
20 // -------------
21 //
21 //
22 // This file is not part of the Qt Enterprise Chart API. It exists purely as an
22 // This file is not part of the Qt Enterprise Chart API. It exists purely as an
23 // implementation detail. This header file may change from version to
23 // implementation detail. This header file may change from version to
24 // version without notice, or even be removed.
24 // version without notice, or even be removed.
25 //
25 //
26 // We mean it.
26 // We mean it.
27
27
28 #ifndef QABSTRACTBARSERIES_P_H
28 #ifndef QABSTRACTBARSERIES_P_H
29 #define QABSTRACTBARSERIES_P_H
29 #define QABSTRACTBARSERIES_P_H
30
30
31 #include <QtCharts/QAbstractBarSeries>
31 #include <QtCharts/QAbstractBarSeries>
32 #include <private/qabstractseries_p.h>
32 #include <private/qabstractseries_p.h>
33 #include <QtCore/QStringList>
33 #include <QtCore/QStringList>
34 #include <QtCharts/QAbstractSeries>
34 #include <QtCharts/QAbstractSeries>
35
35
36 QT_CHARTS_BEGIN_NAMESPACE
36 QT_CHARTS_BEGIN_NAMESPACE
37
37
38 class QBarModelMapper;
38 class QBarModelMapper;
39 class QBarCategoryAxis;
39 class QBarCategoryAxis;
40 class QLegendMarker;
40 class QLegendMarker;
41
41
42 class QAbstractBarSeriesPrivate : public QAbstractSeriesPrivate
42 class QAbstractBarSeriesPrivate : public QAbstractSeriesPrivate
43 {
43 {
44 Q_OBJECT
44 Q_OBJECT
45 public:
45 public:
46 QAbstractBarSeriesPrivate(QAbstractBarSeries *parent);
46 QAbstractBarSeriesPrivate(QAbstractBarSeries *parent);
47 int categoryCount() const;
47 int categoryCount() const;
48
48
49 void setBarWidth(qreal width);
49 void setBarWidth(qreal width);
50 qreal barWidth() const;
50 qreal barWidth() const;
51
51
52 void setVisible(bool visible);
52 void setVisible(bool visible);
53 void setLabelsVisible(bool visible);
53 void setLabelsVisible(bool visible);
54
54
55 void initializeDomain();
55 void initializeDomain();
56 void initializeAxes();
56 void initializeAxes();
57 void initializeAnimations(QChart::AnimationOptions options);
57 void initializeAnimations(QChart::AnimationOptions options);
58 void initializeTheme(int index, ChartTheme* theme, bool forced = false);
58 void initializeTheme(int index, ChartTheme* theme, bool forced = false);
59
59
60 QList<QLegendMarker*> createLegendMarkers(QLegend *legend);
60 QList<QLegendMarker*> createLegendMarkers(QLegend *legend);
61
61
62 virtual QAbstractAxis::AxisType defaultAxisType(Qt::Orientation orientation) const;
62 virtual QAbstractAxis::AxisType defaultAxisType(Qt::Orientation orientation) const;
63 QAbstractAxis* createDefaultAxis(Qt::Orientation orientation) const;
63 QAbstractAxis* createDefaultAxis(Qt::Orientation orientation) const;
64
64
65 bool append(QBarSet *set);
65 bool append(QBarSet *set);
66 bool remove(QBarSet *set);
66 bool remove(QBarSet *set);
67 bool append(QList<QBarSet *> sets);
67 bool append(QList<QBarSet *> sets);
68 bool remove(QList<QBarSet *> sets);
68 bool remove(QList<QBarSet *> sets);
69 bool insert(int index, QBarSet *set);
69 bool insert(int index, QBarSet *set);
70
70
71 QBarSet *barsetAt(int index);
71 QBarSet *barsetAt(int index);
72 qreal min();
72 qreal min();
73 qreal max();
73 qreal max();
74 qreal valueAt(int set, int category);
74 qreal valueAt(int set, int category);
75 qreal percentageAt(int set, int category);
75 qreal percentageAt(int set, int category);
76 qreal categorySum(int category);
76 qreal categorySum(int category);
77 qreal absoluteCategorySum(int category);
77 qreal absoluteCategorySum(int category);
78 qreal maxCategorySum();
78 qreal maxCategorySum();
79 qreal minX();
79 qreal minX();
80 qreal maxX();
80 qreal maxX();
81 qreal categoryTop(int category);
81 qreal categoryTop(int category);
82 qreal categoryBottom(int category);
82 qreal categoryBottom(int category);
83 qreal top();
83 qreal top();
84 qreal bottom();
84 qreal bottom();
85
85
86 bool blockBarUpdate();
86 bool blockBarUpdate();
87
87
88 qreal labelsAngle() const;
89
88 Q_SIGNALS:
90 Q_SIGNALS:
89 void clicked(int index, QBarSet *barset);
91 void clicked(int index, QBarSet *barset);
90 void pressed(int index, QBarSet *barset);
92 void pressed(int index, QBarSet *barset);
91 void released(int index, QBarSet *barset);
93 void released(int index, QBarSet *barset);
92 void doubleClicked(int index, QBarSet *barset);
94 void doubleClicked(int index, QBarSet *barset);
93 void updatedBars();
95 void updatedBars();
94 void updatedLayout();
96 void updatedLayout();
95 void restructuredBars();
97 void restructuredBars();
96 void labelsVisibleChanged(bool visible);
98 void labelsVisibleChanged(bool visible);
97 void visibleChanged();
99 void visibleChanged();
98
100
99 private:
101 private:
100 void populateCategories(QBarCategoryAxis *axis);
102 void populateCategories(QBarCategoryAxis *axis);
101
103
102 protected:
104 protected:
103 QList<QBarSet *> m_barSets;
105 QList<QBarSet *> m_barSets;
104 qreal m_barWidth;
106 qreal m_barWidth;
105 bool m_labelsVisible;
107 bool m_labelsVisible;
106 bool m_visible;
108 bool m_visible;
107 bool m_blockBarUpdate;
109 bool m_blockBarUpdate;
108 QString m_labelsFormat;
110 QString m_labelsFormat;
109 QAbstractBarSeries::LabelsPosition m_labelsPosition;
111 QAbstractBarSeries::LabelsPosition m_labelsPosition;
112 qreal m_labelsAngle;
110
113
111 private:
114 private:
112 Q_DECLARE_PUBLIC(QAbstractBarSeries)
115 Q_DECLARE_PUBLIC(QAbstractBarSeries)
113 friend class HorizontalBarChartItem;
116 friend class HorizontalBarChartItem;
114 friend class BarChartItem;
117 friend class BarChartItem;
115 };
118 };
116
119
117 QT_CHARTS_END_NAMESPACE
120 QT_CHARTS_END_NAMESPACE
118
121
119 #endif // QABSTRACTBARSERIES_P_H
122 #endif // QABSTRACTBARSERIES_P_H
@@ -1,123 +1,106
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd
3 ** Copyright (C) 2015 The Qt Company Ltd
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 **
6 **
7 ** This file is part of the Qt Charts module.
7 ** This file is part of the Qt Charts module.
8 **
8 **
9 ** Licensees holding valid commercial license for Qt may use this file in
9 ** Licensees holding valid commercial license for Qt may use this file in
10 ** accordance with the Qt License Agreement provided with the Software
10 ** accordance with the Qt License Agreement provided with the Software
11 ** or, alternatively, in accordance with the terms contained in a written
11 ** or, alternatively, in accordance with the terms contained in a written
12 ** agreement between you and The Qt Company.
12 ** agreement between you and The Qt Company.
13 **
13 **
14 ** If you have questions regarding the use of this file, please use
14 ** If you have questions regarding the use of this file, please use
15 ** contact form at http://qt.io
15 ** contact form at http://qt.io
16 **
16 **
17 ****************************************************************************/
17 ****************************************************************************/
18
18
19 #include <private/barchartitem_p.h>
19 #include <private/barchartitem_p.h>
20 #include <private/bar_p.h>
20 #include <private/bar_p.h>
21 #include <private/qabstractbarseries_p.h>
21 #include <private/qabstractbarseries_p.h>
22 #include <QtCharts/QBarSet>
22 #include <QtCharts/QBarSet>
23 #include <private/qbarset_p.h>
23 #include <private/qbarset_p.h>
24
24
25 QT_CHARTS_BEGIN_NAMESPACE
25 QT_CHARTS_BEGIN_NAMESPACE
26
26
27 BarChartItem::BarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
27 BarChartItem::BarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
28 AbstractBarChartItem(series, item)
28 AbstractBarChartItem(series, item)
29 {
29 {
30 connect(series, SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)),
30 connect(series, SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)),
31 this, SLOT(handleLabelsPositionChanged()));
31 this, SLOT(handleLabelsPositionChanged()));
32 connect(series, SIGNAL(labelsFormatChanged(QString)), this, SLOT(positionLabels()));
32 connect(series, SIGNAL(labelsFormatChanged(QString)), this, SLOT(positionLabels()));
33 }
33 }
34
34
35 void BarChartItem::initializeLayout()
35 void BarChartItem::initializeLayout()
36 {
36 {
37 qreal categoryCount = m_series->d_func()->categoryCount();
37 qreal categoryCount = m_series->d_func()->categoryCount();
38 qreal setCount = m_series->count();
38 qreal setCount = m_series->count();
39 qreal barWidth = m_series->d_func()->barWidth();
39 qreal barWidth = m_series->d_func()->barWidth();
40
40
41 m_layout.clear();
41 m_layout.clear();
42 for(int category = 0; category < categoryCount; category++) {
42 for(int category = 0; category < categoryCount; category++) {
43 for (int set = 0; set < setCount; set++) {
43 for (int set = 0; set < setCount; set++) {
44 QRectF rect;
44 QRectF rect;
45 QPointF topLeft;
45 QPointF topLeft;
46 QPointF bottomRight;
46 QPointF bottomRight;
47
47
48 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) {
48 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) {
49 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + set/setCount * barWidth, domain()->minY()), m_validData);
49 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + set/setCount * barWidth, domain()->minY()), m_validData);
50 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/setCount * barWidth, domain()->minY()), m_validData);
50 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/setCount * barWidth, domain()->minY()), m_validData);
51 } else {
51 } else {
52 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + set/setCount * barWidth, 0), m_validData);
52 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + set/setCount * barWidth, 0), m_validData);
53 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/setCount * barWidth, 0), m_validData);
53 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/setCount * barWidth, 0), m_validData);
54 }
54 }
55
55
56 if (!m_validData)
56 if (!m_validData)
57 return;
57 return;
58 rect.setTopLeft(topLeft);
58 rect.setTopLeft(topLeft);
59 rect.setBottomRight(bottomRight);
59 rect.setBottomRight(bottomRight);
60 m_layout.append(rect.normalized());
60 m_layout.append(rect.normalized());
61 }
61 }
62 }
62 }
63 }
63 }
64
64
65 QVector<QRectF> BarChartItem::calculateLayout()
65 QVector<QRectF> BarChartItem::calculateLayout()
66 {
66 {
67 QVector<QRectF> layout;
67 QVector<QRectF> layout;
68
68
69 // Use temporary qreals for accuracy
69 // Use temporary qreals for accuracy
70 qreal categoryCount = m_series->d_func()->categoryCount();
70 qreal categoryCount = m_series->d_func()->categoryCount();
71 qreal setCount = m_series->count();
71 qreal setCount = m_series->count();
72 qreal barWidth = m_series->d_func()->barWidth();
72 qreal barWidth = m_series->d_func()->barWidth();
73
73
74 for(int category = 0; category < categoryCount; category++) {
74 for(int category = 0; category < categoryCount; category++) {
75 for (int set = 0; set < setCount; set++) {
75 for (int set = 0; set < setCount; set++) {
76 qreal value = m_series->barSets().at(set)->at(category);
76 qreal value = m_series->barSets().at(set)->at(category);
77 QRectF rect;
77 QRectF rect;
78 QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set)/(setCount) * barWidth, value), m_validData);
78 QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set)/(setCount) * barWidth, value), m_validData);
79 QPointF bottomRight;
79 QPointF bottomRight;
80 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
80 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
81 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/(setCount) * barWidth, domain()->minY()), m_validData);
81 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/(setCount) * barWidth, domain()->minY()), m_validData);
82 else
82 else
83 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/(setCount) * barWidth, 0), m_validData);
83 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/(setCount) * barWidth, 0), m_validData);
84
84
85 rect.setTopLeft(topLeft);
85 rect.setTopLeft(topLeft);
86 rect.setBottomRight(bottomRight);
86 rect.setBottomRight(bottomRight);
87 layout.append(rect.normalized());
87 layout.append(rect.normalized());
88 }
88 }
89 }
89 }
90
90
91 return layout;
91 return layout;
92 }
92 }
93
93
94 void BarChartItem::handleLabelsPositionChanged()
94 void BarChartItem::handleLabelsPositionChanged()
95 {
95 {
96 positionLabels();
96 positionLabels();
97 }
97 }
98
98
99 void BarChartItem::positionLabels()
99 void BarChartItem::positionLabels()
100 {
100 {
101 for (int i = 0; i < m_layout.count(); i++) {
101 positionLabelsVertical();
102 QGraphicsTextItem *label = m_labels.at(i);
103 qreal xPos = m_layout.at(i).center().x() - label->boundingRect().center().x();
104 qreal yPos = 0;
105
106 int offset = m_bars.at(i)->pen().width() / 2 + 2;
107 if (m_series->labelsPosition() == QAbstractBarSeries::LabelsCenter)
108 yPos = m_layout.at(i).center().y() - label->boundingRect().center().y();
109 else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsInsideEnd)
110 yPos = m_layout.at(i).top() - offset;
111 else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsInsideBase)
112 yPos = m_layout.at(i).bottom() - label->boundingRect().height() + offset;
113 else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsOutsideEnd)
114 yPos = m_layout.at(i).top() - label->boundingRect().height() + offset;
115
116 label->setPos(xPos, yPos);
117 label->setZValue(zValue() + 1);
118 }
119 }
102 }
120
103
121 #include "moc_barchartitem_p.cpp"
104 #include "moc_barchartitem_p.cpp"
122
105
123 QT_CHARTS_END_NAMESPACE
106 QT_CHARTS_END_NAMESPACE
@@ -1,169 +1,152
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd
3 ** Copyright (C) 2015 The Qt Company Ltd
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 **
6 **
7 ** This file is part of the Qt Charts module.
7 ** This file is part of the Qt Charts module.
8 **
8 **
9 ** Licensees holding valid commercial license for Qt may use this file in
9 ** Licensees holding valid commercial license for Qt may use this file in
10 ** accordance with the Qt License Agreement provided with the Software
10 ** accordance with the Qt License Agreement provided with the Software
11 ** or, alternatively, in accordance with the terms contained in a written
11 ** or, alternatively, in accordance with the terms contained in a written
12 ** agreement between you and The Qt Company.
12 ** agreement between you and The Qt Company.
13 **
13 **
14 ** If you have questions regarding the use of this file, please use
14 ** If you have questions regarding the use of this file, please use
15 ** contact form at http://qt.io
15 ** contact form at http://qt.io
16 **
16 **
17 ****************************************************************************/
17 ****************************************************************************/
18
18
19 #include <private/percentbarchartitem_p.h>
19 #include <private/percentbarchartitem_p.h>
20 #include <private/bar_p.h>
20 #include <private/bar_p.h>
21 #include <private/qabstractbarseries_p.h>
21 #include <private/qabstractbarseries_p.h>
22 #include <QtCharts/QBarSet>
22 #include <QtCharts/QBarSet>
23 #include <private/qbarset_p.h>
23 #include <private/qbarset_p.h>
24
24
25 QT_CHARTS_BEGIN_NAMESPACE
25 QT_CHARTS_BEGIN_NAMESPACE
26
26
27 PercentBarChartItem::PercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
27 PercentBarChartItem::PercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
28 AbstractBarChartItem(series, item)
28 AbstractBarChartItem(series, item)
29 {
29 {
30 connect(series, SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)),
30 connect(series, SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)),
31 this, SLOT(handleLabelsPositionChanged()));
31 this, SLOT(handleLabelsPositionChanged()));
32 connect(series, SIGNAL(labelsFormatChanged(QString)), this, SLOT(positionLabels()));
32 connect(series, SIGNAL(labelsFormatChanged(QString)), this, SLOT(positionLabels()));
33 }
33 }
34
34
35 void PercentBarChartItem::initializeLayout()
35 void PercentBarChartItem::initializeLayout()
36 {
36 {
37 qreal categoryCount = m_series->d_func()->categoryCount();
37 qreal categoryCount = m_series->d_func()->categoryCount();
38 qreal setCount = m_series->count();
38 qreal setCount = m_series->count();
39 qreal barWidth = m_series->d_func()->barWidth();
39 qreal barWidth = m_series->d_func()->barWidth();
40
40
41 m_layout.clear();
41 m_layout.clear();
42 for(int category = 0; category < categoryCount; category++) {
42 for(int category = 0; category < categoryCount; category++) {
43 for (int set = 0; set < setCount; set++) {
43 for (int set = 0; set < setCount; set++) {
44 QRectF rect;
44 QRectF rect;
45 QPointF topLeft;
45 QPointF topLeft;
46 QPointF bottomRight;
46 QPointF bottomRight;
47
47
48 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) {
48 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) {
49 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, domain()->minY()), m_validData);
49 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, domain()->minY()), m_validData);
50 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, domain()->minY()), m_validData);
50 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, domain()->minY()), m_validData);
51 } else {
51 } else {
52 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, 0), m_validData);
52 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, 0), m_validData);
53 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, 0), m_validData);
53 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, 0), m_validData);
54 }
54 }
55
55
56 if (!m_validData)
56 if (!m_validData)
57 return;
57 return;
58
58
59 rect.setTopLeft(topLeft);
59 rect.setTopLeft(topLeft);
60 rect.setBottomRight(bottomRight);
60 rect.setBottomRight(bottomRight);
61 m_layout.append(rect.normalized());
61 m_layout.append(rect.normalized());
62 }
62 }
63 }
63 }
64 }
64 }
65
65
66 QVector<QRectF> PercentBarChartItem::calculateLayout()
66 QVector<QRectF> PercentBarChartItem::calculateLayout()
67 {
67 {
68 QVector<QRectF> layout;
68 QVector<QRectF> layout;
69
69
70 // Use temporary qreals for accuracy
70 // Use temporary qreals for accuracy
71 qreal categoryCount = m_series->d_func()->categoryCount();
71 qreal categoryCount = m_series->d_func()->categoryCount();
72 qreal setCount = m_series->count();
72 qreal setCount = m_series->count();
73 qreal barWidth = m_series->d_func()->barWidth();
73 qreal barWidth = m_series->d_func()->barWidth();
74
74
75 for(int category = 0; category < categoryCount; category++) {
75 for(int category = 0; category < categoryCount; category++) {
76 qreal sum = 0;
76 qreal sum = 0;
77 qreal categorySum = m_series->d_func()->categorySum(category);
77 qreal categorySum = m_series->d_func()->categorySum(category);
78 for (int set = 0; set < setCount; set++) {
78 for (int set = 0; set < setCount; set++) {
79 qreal value = m_series->barSets().at(set)->at(category);
79 qreal value = m_series->barSets().at(set)->at(category);
80 QRectF rect;
80 QRectF rect;
81 qreal topY = 0;
81 qreal topY = 0;
82 qreal newSum = value + sum;
82 qreal newSum = value + sum;
83 if (newSum > 0)
83 if (newSum > 0)
84 topY = 100 * newSum / categorySum;
84 topY = 100 * newSum / categorySum;
85 qreal bottomY = 0;
85 qreal bottomY = 0;
86 if (sum > 0)
86 if (sum > 0)
87 bottomY = 100 * sum / categorySum;
87 bottomY = 100 * sum / categorySum;
88 QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth/2, topY), m_validData);
88 QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth/2, topY), m_validData);
89 QPointF bottomRight;
89 QPointF bottomRight;
90 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
90 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
91 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? bottomY : domain()->minY()), m_validData);
91 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? bottomY : domain()->minY()), m_validData);
92 else
92 else
93 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? bottomY : 0), m_validData);
93 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? bottomY : 0), m_validData);
94
94
95 rect.setTopLeft(topLeft);
95 rect.setTopLeft(topLeft);
96 rect.setBottomRight(bottomRight);
96 rect.setBottomRight(bottomRight);
97 layout.append(rect.normalized());
97 layout.append(rect.normalized());
98 sum = newSum;
98 sum = newSum;
99 }
99 }
100 }
100 }
101 return layout;
101 return layout;
102 }
102 }
103
103
104 void PercentBarChartItem::handleUpdatedBars()
104 void PercentBarChartItem::handleUpdatedBars()
105 {
105 {
106 // Handle changes in pen, brush, labels etc.
106 // Handle changes in pen, brush, labels etc.
107 int categoryCount = m_series->d_func()->categoryCount();
107 int categoryCount = m_series->d_func()->categoryCount();
108 int setCount = m_series->count();
108 int setCount = m_series->count();
109 int itemIndex(0);
109 int itemIndex(0);
110 static const QString valueTag(QLatin1String("@value"));
110 static const QString valueTag(QLatin1String("@value"));
111
111
112 for (int category = 0; category < categoryCount; category++) {
112 for (int category = 0; category < categoryCount; category++) {
113 for (int set = 0; set < setCount; set++) {
113 for (int set = 0; set < setCount; set++) {
114 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
114 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
115 Bar *bar = m_bars.at(itemIndex);
115 Bar *bar = m_bars.at(itemIndex);
116 bar->setPen(barSet->m_pen);
116 bar->setPen(barSet->m_pen);
117 bar->setBrush(barSet->m_brush);
117 bar->setBrush(barSet->m_brush);
118 bar->update();
118 bar->update();
119
119
120 QGraphicsTextItem *label = m_labels.at(itemIndex);
120 QGraphicsTextItem *label = m_labels.at(itemIndex);
121 qreal p = m_series->d_func()->percentageAt(set, category) * 100.0;
121 qreal p = m_series->d_func()->percentageAt(set, category) * 100.0;
122 QString vString(presenter()->numberToString(p, 'f', 0));
122 QString vString(presenter()->numberToString(p, 'f', 0));
123 QString valueLabel;
123 QString valueLabel;
124 if (m_series->labelsFormat().isEmpty()) {
124 if (m_series->labelsFormat().isEmpty()) {
125 vString.append(QStringLiteral("%"));
125 vString.append(QStringLiteral("%"));
126 valueLabel = vString;
126 valueLabel = vString;
127 } else {
127 } else {
128 valueLabel = m_series->labelsFormat();
128 valueLabel = m_series->labelsFormat();
129 valueLabel.replace(valueTag, vString);
129 valueLabel.replace(valueTag, vString);
130 }
130 }
131 label->setHtml(valueLabel);
131 label->setHtml(valueLabel);
132 label->setFont(barSet->m_labelFont);
132 label->setFont(barSet->m_labelFont);
133 label->setDefaultTextColor(barSet->m_labelBrush.color());
133 label->setDefaultTextColor(barSet->m_labelBrush.color());
134 label->update();
134 label->update();
135 itemIndex++;
135 itemIndex++;
136 }
136 }
137 }
137 }
138 }
138 }
139
139
140 void PercentBarChartItem::handleLabelsPositionChanged()
140 void PercentBarChartItem::handleLabelsPositionChanged()
141 {
141 {
142 positionLabels();
142 positionLabels();
143 }
143 }
144
144
145 void PercentBarChartItem::positionLabels()
145 void PercentBarChartItem::positionLabels()
146 {
146 {
147 for (int i = 0; i < m_layout.count(); i++) {
147 positionLabelsVertical();
148 QGraphicsTextItem *label = m_labels.at(i);
149 qreal xPos = m_layout.at(i).center().x() - label->boundingRect().center().x();
150 qreal yPos = 0;
151
152 int offset = m_bars.at(i)->pen().width() / 2 + 2;
153 if (m_series->labelsPosition() == QAbstractBarSeries::LabelsCenter)
154 yPos = m_layout.at(i).center().y() - label->boundingRect().center().y();
155 else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsInsideEnd)
156 yPos = m_layout.at(i).top() - offset;
157 else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsInsideBase)
158 yPos = m_layout.at(i).bottom() - label->boundingRect().height() + offset;
159 else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsOutsideEnd)
160 yPos = m_layout.at(i).top() - label->boundingRect().height() + offset;
161
162 label->setPos(xPos, yPos);
163 label->setZValue(zValue() + 1);
164 }
165 }
148 }
166
149
167 #include "moc_percentbarchartitem_p.cpp"
150 #include "moc_percentbarchartitem_p.cpp"
168
151
169 QT_CHARTS_END_NAMESPACE
152 QT_CHARTS_END_NAMESPACE
@@ -1,135 +1,118
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd
3 ** Copyright (C) 2015 The Qt Company Ltd
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 **
6 **
7 ** This file is part of the Qt Charts module.
7 ** This file is part of the Qt Charts module.
8 **
8 **
9 ** Licensees holding valid commercial license for Qt may use this file in
9 ** Licensees holding valid commercial license for Qt may use this file in
10 ** accordance with the Qt License Agreement provided with the Software
10 ** accordance with the Qt License Agreement provided with the Software
11 ** or, alternatively, in accordance with the terms contained in a written
11 ** or, alternatively, in accordance with the terms contained in a written
12 ** agreement between you and The Qt Company.
12 ** agreement between you and The Qt Company.
13 **
13 **
14 ** If you have questions regarding the use of this file, please use
14 ** If you have questions regarding the use of this file, please use
15 ** contact form at http://qt.io
15 ** contact form at http://qt.io
16 **
16 **
17 ****************************************************************************/
17 ****************************************************************************/
18
18
19 #include <private/stackedbarchartitem_p.h>
19 #include <private/stackedbarchartitem_p.h>
20 #include <private/bar_p.h>
20 #include <private/bar_p.h>
21 #include <private/qbarset_p.h>
21 #include <private/qbarset_p.h>
22 #include <private/qabstractbarseries_p.h>
22 #include <private/qabstractbarseries_p.h>
23 #include <QtCharts/QBarSet>
23 #include <QtCharts/QBarSet>
24
24
25 QT_CHARTS_BEGIN_NAMESPACE
25 QT_CHARTS_BEGIN_NAMESPACE
26
26
27 StackedBarChartItem::StackedBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
27 StackedBarChartItem::StackedBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
28 AbstractBarChartItem(series, item)
28 AbstractBarChartItem(series, item)
29 {
29 {
30 connect(series, SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)),
30 connect(series, SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)),
31 this, SLOT(handleLabelsPositionChanged()));
31 this, SLOT(handleLabelsPositionChanged()));
32 connect(series, SIGNAL(labelsFormatChanged(QString)), this, SLOT(positionLabels()));
32 connect(series, SIGNAL(labelsFormatChanged(QString)), this, SLOT(positionLabels()));
33 }
33 }
34
34
35 void StackedBarChartItem::initializeLayout()
35 void StackedBarChartItem::initializeLayout()
36 {
36 {
37 qreal categoryCount = m_series->d_func()->categoryCount();
37 qreal categoryCount = m_series->d_func()->categoryCount();
38 qreal setCount = m_series->count();
38 qreal setCount = m_series->count();
39 qreal barWidth = m_series->d_func()->barWidth();
39 qreal barWidth = m_series->d_func()->barWidth();
40
40
41 m_layout.clear();
41 m_layout.clear();
42 for(int category = 0; category < categoryCount; category++) {
42 for(int category = 0; category < categoryCount; category++) {
43 for (int set = 0; set < setCount; set++) {
43 for (int set = 0; set < setCount; set++) {
44 QRectF rect;
44 QRectF rect;
45 QPointF topLeft;
45 QPointF topLeft;
46 QPointF bottomRight;
46 QPointF bottomRight;
47
47
48 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) {
48 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) {
49 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, domain()->minY()), m_validData);
49 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, domain()->minY()), m_validData);
50 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, domain()->minY()), m_validData);
50 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, domain()->minY()), m_validData);
51 } else {
51 } else {
52 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, 0), m_validData);
52 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, 0), m_validData);
53 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, 0), m_validData);
53 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, 0), m_validData);
54 }
54 }
55
55
56 if (!m_validData)
56 if (!m_validData)
57 return;
57 return;
58
58
59 rect.setTopLeft(topLeft);
59 rect.setTopLeft(topLeft);
60 rect.setBottomRight(bottomRight);
60 rect.setBottomRight(bottomRight);
61 m_layout.append(rect.normalized());
61 m_layout.append(rect.normalized());
62 }
62 }
63 }
63 }
64 }
64 }
65
65
66 QVector<QRectF> StackedBarChartItem::calculateLayout()
66 QVector<QRectF> StackedBarChartItem::calculateLayout()
67 {
67 {
68 QVector<QRectF> layout;
68 QVector<QRectF> layout;
69 // Use temporary qreals for accuracy
69 // Use temporary qreals for accuracy
70 qreal categoryCount = m_series->d_func()->categoryCount();
70 qreal categoryCount = m_series->d_func()->categoryCount();
71 qreal setCount = m_series->count();
71 qreal setCount = m_series->count();
72 qreal barWidth = m_series->d_func()->barWidth();
72 qreal barWidth = m_series->d_func()->barWidth();
73
73
74 for(int category = 0; category < categoryCount; category++) {
74 for(int category = 0; category < categoryCount; category++) {
75 qreal positiveSum = 0;
75 qreal positiveSum = 0;
76 qreal negativeSum = 0;
76 qreal negativeSum = 0;
77 for (int set = 0; set < setCount; set++) {
77 for (int set = 0; set < setCount; set++) {
78 qreal value = m_series->barSets().at(set)->at(category);
78 qreal value = m_series->barSets().at(set)->at(category);
79 QRectF rect;
79 QRectF rect;
80 QPointF topLeft;
80 QPointF topLeft;
81 QPointF bottomRight;
81 QPointF bottomRight;
82 if (value < 0) {
82 if (value < 0) {
83 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + negativeSum), m_validData);
83 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + negativeSum), m_validData);
84 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
84 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
85 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : domain()->minY()), m_validData);
85 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : domain()->minY()), m_validData);
86 else
86 else
87 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : 0), m_validData);
87 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : 0), m_validData);
88 negativeSum += value;
88 negativeSum += value;
89 } else {
89 } else {
90 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + positiveSum), m_validData);
90 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + positiveSum), m_validData);
91 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
91 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
92 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : domain()->minY()), m_validData);
92 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : domain()->minY()), m_validData);
93 else
93 else
94 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : 0), m_validData);
94 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : 0), m_validData);
95 positiveSum += value;
95 positiveSum += value;
96 }
96 }
97
97
98 rect.setTopLeft(topLeft);
98 rect.setTopLeft(topLeft);
99 rect.setBottomRight(bottomRight);
99 rect.setBottomRight(bottomRight);
100 layout.append(rect.normalized());
100 layout.append(rect.normalized());
101 }
101 }
102 }
102 }
103 return layout;
103 return layout;
104 }
104 }
105
105
106 void StackedBarChartItem::handleLabelsPositionChanged()
106 void StackedBarChartItem::handleLabelsPositionChanged()
107 {
107 {
108 positionLabels();
108 positionLabels();
109 }
109 }
110
110
111 void StackedBarChartItem::positionLabels()
111 void StackedBarChartItem::positionLabels()
112 {
112 {
113 for (int i = 0; i < m_layout.count(); i++) {
113 positionLabelsVertical();
114 QGraphicsTextItem *label = m_labels.at(i);
115 qreal xPos = m_layout.at(i).center().x() - label->boundingRect().center().x();
116 qreal yPos = 0;
117
118 int offset = m_bars.at(i)->pen().width() / 2 + 2;
119 if (m_series->labelsPosition() == QAbstractBarSeries::LabelsCenter)
120 yPos = m_layout.at(i).center().y() - label->boundingRect().center().y();
121 else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsInsideEnd)
122 yPos = m_layout.at(i).top() - offset;
123 else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsInsideBase)
124 yPos = m_layout.at(i).bottom() - label->boundingRect().height() + offset;
125 else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsOutsideEnd)
126 yPos = m_layout.at(i).top() - label->boundingRect().height() + offset;
127
128 label->setPos(xPos, yPos);
129 label->setZValue(zValue() + 1);
130 }
131 }
114 }
132
115
133 #include "moc_stackedbarchartitem_p.cpp"
116 #include "moc_stackedbarchartitem_p.cpp"
134
117
135 QT_CHARTS_END_NAMESPACE
118 QT_CHARTS_END_NAMESPACE
@@ -1,1337 +1,1351
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd
3 ** Copyright (C) 2015 The Qt Company Ltd
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 **
6 **
7 ** This file is part of the Qt Charts module.
7 ** This file is part of the Qt Charts module.
8 **
8 **
9 ** Licensees holding valid commercial license for Qt may use this file in
9 ** Licensees holding valid commercial license for Qt may use this file in
10 ** accordance with the Qt License Agreement provided with the Software
10 ** accordance with the Qt License Agreement provided with the Software
11 ** or, alternatively, in accordance with the terms contained in a written
11 ** or, alternatively, in accordance with the terms contained in a written
12 ** agreement between you and The Qt Company.
12 ** agreement between you and The Qt Company.
13 **
13 **
14 ** If you have questions regarding the use of this file, please use
14 ** If you have questions regarding the use of this file, please use
15 ** contact form at http://qt.io
15 ** contact form at http://qt.io
16 **
16 **
17 ****************************************************************************/
17 ****************************************************************************/
18
18
19 #include <QtTest/QtTest>
19 #include <QtTest/QtTest>
20 #include <QtCharts/QBarSeries>
20 #include <QtCharts/QBarSeries>
21 #include <QtCharts/QBarSet>
21 #include <QtCharts/QBarSet>
22 #include <QtCharts/QChartView>
22 #include <QtCharts/QChartView>
23 #include <QtCharts/QChart>
23 #include <QtCharts/QChart>
24 #include "tst_definitions.h"
24 #include "tst_definitions.h"
25
25
26 QT_CHARTS_USE_NAMESPACE
26 QT_CHARTS_USE_NAMESPACE
27
27
28 Q_DECLARE_METATYPE(QBarSet*)
28 Q_DECLARE_METATYPE(QBarSet*)
29 Q_DECLARE_METATYPE(QList<QBarSet*>)
29 Q_DECLARE_METATYPE(QList<QBarSet*>)
30 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
30 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
31
31
32 class tst_QBarSeries : public QObject
32 class tst_QBarSeries : public QObject
33 {
33 {
34 Q_OBJECT
34 Q_OBJECT
35
35
36 public slots:
36 public slots:
37 void initTestCase();
37 void initTestCase();
38 void cleanupTestCase();
38 void cleanupTestCase();
39 void init();
39 void init();
40 void cleanup();
40 void cleanup();
41
41
42 private slots:
42 private slots:
43 void qbarseries_data();
43 void qbarseries_data();
44 void qbarseries();
44 void qbarseries();
45 void type_data();
45 void type_data();
46 void type();
46 void type();
47 void append_data();
47 void append_data();
48 void append();
48 void append();
49 void remove_data();
49 void remove_data();
50 void remove();
50 void remove();
51 void take_data();
51 void take_data();
52 void take();
52 void take();
53 void appendList_data();
53 void appendList_data();
54 void appendList();
54 void appendList();
55 void count_data();
55 void count_data();
56 void count();
56 void count();
57 void barSets_data();
57 void barSets_data();
58 void barSets();
58 void barSets();
59 void setLabelsVisible_data();
59 void setLabelsVisible_data();
60 void setLabelsVisible();
60 void setLabelsVisible();
61 void setLabelsFormat();
61 void setLabelsFormat();
62 void setLabelsPosition();
62 void setLabelsPosition();
63 void setLabelsAngle();
63 void opacity();
64 void opacity();
64 void mouseclicked_data();
65 void mouseclicked_data();
65 void mouseclicked();
66 void mouseclicked();
66 void mousehovered_data();
67 void mousehovered_data();
67 void mousehovered();
68 void mousehovered();
68 void clearWithAnimations();
69 void clearWithAnimations();
69 void destruction();
70 void destruction();
70 void mousePressed();
71 void mousePressed();
71 void mouseReleased();
72 void mouseReleased();
72 void mouseDoubleClicked();
73 void mouseDoubleClicked();
73
74
74 private:
75 private:
75 QBarSeries* m_barseries;
76 QBarSeries* m_barseries;
76 QBarSeries* m_barseries_with_sets;
77 QBarSeries* m_barseries_with_sets;
77
78
78 QList<QBarSet*> m_testSets;
79 QList<QBarSet*> m_testSets;
79
80
80 };
81 };
81
82
82 void tst_QBarSeries::initTestCase()
83 void tst_QBarSeries::initTestCase()
83 {
84 {
84 qRegisterMetaType<QBarSet*>("QBarSet*");
85 qRegisterMetaType<QBarSet*>("QBarSet*");
85 qRegisterMetaType<QList<QBarSet*> >("QList<QBarSet*>");
86 qRegisterMetaType<QList<QBarSet*> >("QList<QBarSet*>");
86 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
87 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
87 }
88 }
88
89
89 void tst_QBarSeries::cleanupTestCase()
90 void tst_QBarSeries::cleanupTestCase()
90 {
91 {
91 QTest::qWait(1); // Allow final deleteLaters to run
92 QTest::qWait(1); // Allow final deleteLaters to run
92 }
93 }
93
94
94 void tst_QBarSeries::init()
95 void tst_QBarSeries::init()
95 {
96 {
96 m_barseries = new QBarSeries();
97 m_barseries = new QBarSeries();
97 m_barseries_with_sets = new QBarSeries();
98 m_barseries_with_sets = new QBarSeries();
98
99
99 for (int i=0; i<5; i++) {
100 for (int i=0; i<5; i++) {
100 m_testSets.append(new QBarSet("testset"));
101 m_testSets.append(new QBarSet("testset"));
101 m_barseries_with_sets->append(m_testSets.at(i));
102 m_barseries_with_sets->append(m_testSets.at(i));
102 }
103 }
103 }
104 }
104
105
105 void tst_QBarSeries::cleanup()
106 void tst_QBarSeries::cleanup()
106 {
107 {
107 foreach (QBarSet* s, m_testSets) {
108 foreach (QBarSet* s, m_testSets) {
108 m_barseries_with_sets->remove(s);
109 m_barseries_with_sets->remove(s);
109 }
110 }
110 m_testSets.clear();
111 m_testSets.clear();
111
112
112 delete m_barseries;
113 delete m_barseries;
113 m_barseries = 0;
114 m_barseries = 0;
114 delete m_barseries_with_sets;
115 delete m_barseries_with_sets;
115 m_barseries_with_sets = 0;
116 m_barseries_with_sets = 0;
116 }
117 }
117
118
118 void tst_QBarSeries::qbarseries_data()
119 void tst_QBarSeries::qbarseries_data()
119 {
120 {
120 }
121 }
121
122
122 void tst_QBarSeries::qbarseries()
123 void tst_QBarSeries::qbarseries()
123 {
124 {
124 QBarSeries *barseries = new QBarSeries();
125 QBarSeries *barseries = new QBarSeries();
125 QVERIFY(barseries != 0);
126 QVERIFY(barseries != 0);
126 delete barseries;
127 delete barseries;
127 }
128 }
128
129
129 void tst_QBarSeries::type_data()
130 void tst_QBarSeries::type_data()
130 {
131 {
131
132
132 }
133 }
133
134
134 void tst_QBarSeries::type()
135 void tst_QBarSeries::type()
135 {
136 {
136 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeBar);
137 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeBar);
137 }
138 }
138
139
139 void tst_QBarSeries::append_data()
140 void tst_QBarSeries::append_data()
140 {
141 {
141 }
142 }
142
143
143 void tst_QBarSeries::append()
144 void tst_QBarSeries::append()
144 {
145 {
145 QVERIFY(m_barseries->count() == 0);
146 QVERIFY(m_barseries->count() == 0);
146
147
147 bool ret = false;
148 bool ret = false;
148
149
149 // Try adding barset
150 // Try adding barset
150 QBarSet *barset = new QBarSet("testset");
151 QBarSet *barset = new QBarSet("testset");
151 ret = m_barseries->append(barset);
152 ret = m_barseries->append(barset);
152
153
153 QVERIFY(ret == true);
154 QVERIFY(ret == true);
154 QVERIFY(m_barseries->count() == 1);
155 QVERIFY(m_barseries->count() == 1);
155
156
156 // Try adding another set
157 // Try adding another set
157 QBarSet *barset2 = new QBarSet("testset2");
158 QBarSet *barset2 = new QBarSet("testset2");
158 ret = m_barseries->append(barset2);
159 ret = m_barseries->append(barset2);
159
160
160 QVERIFY(ret == true);
161 QVERIFY(ret == true);
161 QVERIFY(m_barseries->count() == 2);
162 QVERIFY(m_barseries->count() == 2);
162
163
163 // Try adding same set again
164 // Try adding same set again
164 ret = m_barseries->append(barset2);
165 ret = m_barseries->append(barset2);
165 QVERIFY(ret == false);
166 QVERIFY(ret == false);
166 QVERIFY(m_barseries->count() == 2);
167 QVERIFY(m_barseries->count() == 2);
167
168
168 // Try adding null set
169 // Try adding null set
169 ret = m_barseries->append(0);
170 ret = m_barseries->append(0);
170 QVERIFY(ret == false);
171 QVERIFY(ret == false);
171 QVERIFY(m_barseries->count() == 2);
172 QVERIFY(m_barseries->count() == 2);
172
173
173 }
174 }
174
175
175 void tst_QBarSeries::remove_data()
176 void tst_QBarSeries::remove_data()
176 {
177 {
177 }
178 }
178
179
179 void tst_QBarSeries::remove()
180 void tst_QBarSeries::remove()
180 {
181 {
181 int count = m_testSets.count();
182 int count = m_testSets.count();
182 QVERIFY(m_barseries_with_sets->count() == count);
183 QVERIFY(m_barseries_with_sets->count() == count);
183
184
184 // Try to remove null pointer (should not remove, should not crash)
185 // Try to remove null pointer (should not remove, should not crash)
185 bool ret = false;
186 bool ret = false;
186 ret = m_barseries_with_sets->remove(0);
187 ret = m_barseries_with_sets->remove(0);
187 QVERIFY(ret == false);
188 QVERIFY(ret == false);
188 QVERIFY(m_barseries_with_sets->count() == count);
189 QVERIFY(m_barseries_with_sets->count() == count);
189
190
190 // Try to remove invalid pointer (should not remove, should not crash)
191 // Try to remove invalid pointer (should not remove, should not crash)
191 ret = m_barseries_with_sets->remove((QBarSet*) (m_testSets.at(0) + 1) );
192 ret = m_barseries_with_sets->remove((QBarSet*) (m_testSets.at(0) + 1) );
192 QVERIFY(ret == false);
193 QVERIFY(ret == false);
193 QVERIFY(m_barseries_with_sets->count() == count);
194 QVERIFY(m_barseries_with_sets->count() == count);
194
195
195 // remove some sets
196 // remove some sets
196 ret = m_barseries_with_sets->remove(m_testSets.at(2));
197 ret = m_barseries_with_sets->remove(m_testSets.at(2));
197 QVERIFY(ret == true);
198 QVERIFY(ret == true);
198 ret = m_barseries_with_sets->remove(m_testSets.at(3));
199 ret = m_barseries_with_sets->remove(m_testSets.at(3));
199 QVERIFY(ret == true);
200 QVERIFY(ret == true);
200 ret = m_barseries_with_sets->remove(m_testSets.at(4));
201 ret = m_barseries_with_sets->remove(m_testSets.at(4));
201 QVERIFY(ret == true);
202 QVERIFY(ret == true);
202
203
203 QVERIFY(m_barseries_with_sets->count() == 2);
204 QVERIFY(m_barseries_with_sets->count() == 2);
204
205
205 QList<QBarSet*> verifysets = m_barseries_with_sets->barSets();
206 QList<QBarSet*> verifysets = m_barseries_with_sets->barSets();
206
207
207 QVERIFY(verifysets.at(0) == m_testSets.at(0));
208 QVERIFY(verifysets.at(0) == m_testSets.at(0));
208 QVERIFY(verifysets.at(1) == m_testSets.at(1));
209 QVERIFY(verifysets.at(1) == m_testSets.at(1));
209
210
210 // Try removing all sets again (should be ok, even if some sets have already been removed)
211 // Try removing all sets again (should be ok, even if some sets have already been removed)
211 ret = false;
212 ret = false;
212 for (int i=0; i<count; i++) {
213 for (int i=0; i<count; i++) {
213 ret |= m_barseries_with_sets->remove(m_testSets.at(i));
214 ret |= m_barseries_with_sets->remove(m_testSets.at(i));
214 }
215 }
215
216
216 QVERIFY(ret == true);
217 QVERIFY(ret == true);
217 QVERIFY(m_barseries_with_sets->count() == 0);
218 QVERIFY(m_barseries_with_sets->count() == 0);
218 }
219 }
219
220
220 void tst_QBarSeries::take_data()
221 void tst_QBarSeries::take_data()
221 {
222 {
222
223
223 }
224 }
224
225
225 void tst_QBarSeries::take()
226 void tst_QBarSeries::take()
226 {
227 {
227 int count = m_testSets.count();
228 int count = m_testSets.count();
228 QVERIFY(m_barseries_with_sets->count() == count);
229 QVERIFY(m_barseries_with_sets->count() == count);
229
230
230 QSignalSpy countSpy(m_barseries_with_sets,SIGNAL(countChanged()));
231 QSignalSpy countSpy(m_barseries_with_sets,SIGNAL(countChanged()));
231 QSignalSpy removedSpy(m_barseries_with_sets,SIGNAL(barsetsRemoved(QList<QBarSet*>)));
232 QSignalSpy removedSpy(m_barseries_with_sets,SIGNAL(barsetsRemoved(QList<QBarSet*>)));
232
233
233 for (int i=0; i<m_testSets.count(); i++) {
234 for (int i=0; i<m_testSets.count(); i++) {
234 QBarSet* set = m_testSets.at(i);
235 QBarSet* set = m_testSets.at(i);
235 bool success = m_barseries_with_sets->take(set);
236 bool success = m_barseries_with_sets->take(set);
236 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
237 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
237 QVERIFY(success);
238 QVERIFY(success);
238 TRY_COMPARE(countSpy.count(),1);
239 TRY_COMPARE(countSpy.count(),1);
239 TRY_COMPARE(removedSpy.count(),1);
240 TRY_COMPARE(removedSpy.count(),1);
240
241
241 QList<QVariant> removedSpyArg = removedSpy.takeFirst();
242 QList<QVariant> removedSpyArg = removedSpy.takeFirst();
242 QList<QBarSet*> removedSets = qvariant_cast<QList<QBarSet*> > (removedSpyArg.at(0));
243 QList<QBarSet*> removedSets = qvariant_cast<QList<QBarSet*> > (removedSpyArg.at(0));
243 QCOMPARE(removedSets.at(0), m_testSets.at(i));
244 QCOMPARE(removedSets.at(0), m_testSets.at(i));
244 countSpy.takeFirst();
245 countSpy.takeFirst();
245 }
246 }
246 }
247 }
247
248
248
249
249 void tst_QBarSeries::appendList_data()
250 void tst_QBarSeries::appendList_data()
250 {
251 {
251
252
252 }
253 }
253
254
254 void tst_QBarSeries::appendList()
255 void tst_QBarSeries::appendList()
255 {
256 {
256 int count = 5;
257 int count = 5;
257 QVERIFY(m_barseries->count() == 0);
258 QVERIFY(m_barseries->count() == 0);
258
259
259 QList<QBarSet*> sets;
260 QList<QBarSet*> sets;
260 for (int i=0; i<count; i++) {
261 for (int i=0; i<count; i++) {
261 sets.append(new QBarSet("testset"));
262 sets.append(new QBarSet("testset"));
262 }
263 }
263
264
264 // Append new sets (should succeed, count should match the count of sets)
265 // Append new sets (should succeed, count should match the count of sets)
265 bool ret = false;
266 bool ret = false;
266 ret = m_barseries->append(sets);
267 ret = m_barseries->append(sets);
267 QVERIFY(ret == true);
268 QVERIFY(ret == true);
268 QVERIFY(m_barseries->count() == count);
269 QVERIFY(m_barseries->count() == count);
269
270
270 // Append same sets again (should fail, count should remain same)
271 // Append same sets again (should fail, count should remain same)
271 ret = m_barseries->append(sets);
272 ret = m_barseries->append(sets);
272 QVERIFY(ret == false);
273 QVERIFY(ret == false);
273 QVERIFY(m_barseries->count() == count);
274 QVERIFY(m_barseries->count() == count);
274
275
275 // Try append empty list (should succeed, but count should remain same)
276 // Try append empty list (should succeed, but count should remain same)
276 QList<QBarSet*> invalidList;
277 QList<QBarSet*> invalidList;
277 ret = m_barseries->append(invalidList);
278 ret = m_barseries->append(invalidList);
278 QVERIFY(ret == true);
279 QVERIFY(ret == true);
279 QVERIFY(m_barseries->count() == count);
280 QVERIFY(m_barseries->count() == count);
280
281
281 // Try append list with one new and one existing set (should fail, count remains same)
282 // Try append list with one new and one existing set (should fail, count remains same)
282 invalidList.append(new QBarSet("ok set"));
283 invalidList.append(new QBarSet("ok set"));
283 invalidList.append(sets.at(0));
284 invalidList.append(sets.at(0));
284 ret = m_barseries->append(invalidList);
285 ret = m_barseries->append(invalidList);
285 QVERIFY(ret == false);
286 QVERIFY(ret == false);
286 QVERIFY(m_barseries->count() == count);
287 QVERIFY(m_barseries->count() == count);
287 delete invalidList.at(0);
288 delete invalidList.at(0);
288
289
289 // Try append list with null pointers (should fail, count remains same)
290 // Try append list with null pointers (should fail, count remains same)
290 QList<QBarSet*> invalidList2;
291 QList<QBarSet*> invalidList2;
291 invalidList2.append(0);
292 invalidList2.append(0);
292 invalidList2.append(0);
293 invalidList2.append(0);
293 invalidList2.append(0);
294 invalidList2.append(0);
294 ret = m_barseries->append(invalidList2);
295 ret = m_barseries->append(invalidList2);
295 QVERIFY(ret == false);
296 QVERIFY(ret == false);
296 QVERIFY(m_barseries->count() == count);
297 QVERIFY(m_barseries->count() == count);
297 }
298 }
298
299
299 void tst_QBarSeries::count_data()
300 void tst_QBarSeries::count_data()
300 {
301 {
301
302
302 }
303 }
303
304
304 void tst_QBarSeries::count()
305 void tst_QBarSeries::count()
305 {
306 {
306 QVERIFY(m_barseries->count() == 0);
307 QVERIFY(m_barseries->count() == 0);
307 QVERIFY(m_barseries_with_sets->count() == m_testSets.count());
308 QVERIFY(m_barseries_with_sets->count() == m_testSets.count());
308 }
309 }
309
310
310 void tst_QBarSeries::barSets_data()
311 void tst_QBarSeries::barSets_data()
311 {
312 {
312
313
313 }
314 }
314
315
315 void tst_QBarSeries::barSets()
316 void tst_QBarSeries::barSets()
316 {
317 {
317 QVERIFY(m_barseries->barSets().count() == 0);
318 QVERIFY(m_barseries->barSets().count() == 0);
318
319
319 QList<QBarSet*> sets = m_barseries_with_sets->barSets();
320 QList<QBarSet*> sets = m_barseries_with_sets->barSets();
320 QVERIFY(sets.count() == m_testSets.count());
321 QVERIFY(sets.count() == m_testSets.count());
321
322
322 for (int i=0; i<m_testSets.count(); i++) {
323 for (int i=0; i<m_testSets.count(); i++) {
323 QVERIFY(sets.at(i) == m_testSets.at(i));
324 QVERIFY(sets.at(i) == m_testSets.at(i));
324 }
325 }
325 }
326 }
326
327
327 void tst_QBarSeries::setLabelsVisible_data()
328 void tst_QBarSeries::setLabelsVisible_data()
328 {
329 {
329
330
330 }
331 }
331
332
332 void tst_QBarSeries::setLabelsVisible()
333 void tst_QBarSeries::setLabelsVisible()
333 {
334 {
334 // labels should be invisible by default
335 // labels should be invisible by default
335 QVERIFY(m_barseries->isLabelsVisible() == false);
336 QVERIFY(m_barseries->isLabelsVisible() == false);
336 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
337 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
337
338
338 // turn labels to visible
339 // turn labels to visible
339 m_barseries_with_sets->setLabelsVisible(true);
340 m_barseries_with_sets->setLabelsVisible(true);
340 // TODO: test the signal
341 // TODO: test the signal
341 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
342 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
342
343
343 // turn labels to invisible
344 // turn labels to invisible
344 m_barseries_with_sets->setLabelsVisible(false);
345 m_barseries_with_sets->setLabelsVisible(false);
345 // TODO: test the signal
346 // TODO: test the signal
346 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
347 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
347
348
348 // without parameter, should turn labels to visible
349 // without parameter, should turn labels to visible
349 m_barseries_with_sets->setLabelsVisible();
350 m_barseries_with_sets->setLabelsVisible();
350 // TODO: test the signal
351 // TODO: test the signal
351 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
352 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
352 }
353 }
353
354
354 void tst_QBarSeries::setLabelsFormat()
355 void tst_QBarSeries::setLabelsFormat()
355 {
356 {
356 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
357 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
357 QCOMPARE(m_barseries->labelsFormat(), QString());
358 QCOMPARE(m_barseries->labelsFormat(), QString());
358
359
359 QString format("(@value)");
360 QString format("(@value)");
360 m_barseries->setLabelsFormat(format);
361 m_barseries->setLabelsFormat(format);
361 TRY_COMPARE(labelsFormatSpy.count(), 1);
362 TRY_COMPARE(labelsFormatSpy.count(), 1);
362 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
363 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
363 QVERIFY(arguments.at(0).toString() == format);
364 QVERIFY(arguments.at(0).toString() == format);
364 QCOMPARE(m_barseries->labelsFormat(), format);
365 QCOMPARE(m_barseries->labelsFormat(), format);
365
366
366 m_barseries->setLabelsFormat(QString());
367 m_barseries->setLabelsFormat(QString());
367 TRY_COMPARE(labelsFormatSpy.count(), 1);
368 TRY_COMPARE(labelsFormatSpy.count(), 1);
368 arguments = labelsFormatSpy.takeFirst();
369 arguments = labelsFormatSpy.takeFirst();
369 QVERIFY(arguments.at(0).toString() == QString());
370 QVERIFY(arguments.at(0).toString() == QString());
370 QCOMPARE(m_barseries->labelsFormat(), QString());
371 QCOMPARE(m_barseries->labelsFormat(), QString());
371 }
372 }
372
373
373 void tst_QBarSeries::setLabelsPosition()
374 void tst_QBarSeries::setLabelsPosition()
374 {
375 {
375 QSignalSpy labelsPositionSpy(m_barseries,
376 QSignalSpy labelsPositionSpy(m_barseries,
376 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
377 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
377 QCOMPARE(m_barseries->labelsPosition(), QBarSeries::LabelsCenter);
378 QCOMPARE(m_barseries->labelsPosition(), QBarSeries::LabelsCenter);
378
379
379 m_barseries->setLabelsPosition(QBarSeries::LabelsInsideEnd);
380 m_barseries->setLabelsPosition(QBarSeries::LabelsInsideEnd);
380 TRY_COMPARE(labelsPositionSpy.count(), 1);
381 TRY_COMPARE(labelsPositionSpy.count(), 1);
381 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
382 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
382 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
383 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
383 == QBarSeries::LabelsInsideEnd);
384 == QBarSeries::LabelsInsideEnd);
384 QCOMPARE(m_barseries->labelsPosition(), QBarSeries::LabelsInsideEnd);
385 QCOMPARE(m_barseries->labelsPosition(), QBarSeries::LabelsInsideEnd);
385
386
386 m_barseries->setLabelsPosition(QBarSeries::LabelsInsideBase);
387 m_barseries->setLabelsPosition(QBarSeries::LabelsInsideBase);
387 TRY_COMPARE(labelsPositionSpy.count(), 1);
388 TRY_COMPARE(labelsPositionSpy.count(), 1);
388 arguments = labelsPositionSpy.takeFirst();
389 arguments = labelsPositionSpy.takeFirst();
389 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
390 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
390 == QBarSeries::LabelsInsideBase);
391 == QBarSeries::LabelsInsideBase);
391 QCOMPARE(m_barseries->labelsPosition(), QBarSeries::LabelsInsideBase);
392 QCOMPARE(m_barseries->labelsPosition(), QBarSeries::LabelsInsideBase);
392
393
393 m_barseries->setLabelsPosition(QBarSeries::LabelsOutsideEnd);
394 m_barseries->setLabelsPosition(QBarSeries::LabelsOutsideEnd);
394 TRY_COMPARE(labelsPositionSpy.count(), 1);
395 TRY_COMPARE(labelsPositionSpy.count(), 1);
395 arguments = labelsPositionSpy.takeFirst();
396 arguments = labelsPositionSpy.takeFirst();
396 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
397 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
397 == QBarSeries::LabelsOutsideEnd);
398 == QBarSeries::LabelsOutsideEnd);
398 QCOMPARE(m_barseries->labelsPosition(), QBarSeries::LabelsOutsideEnd);
399 QCOMPARE(m_barseries->labelsPosition(), QBarSeries::LabelsOutsideEnd);
399
400
400 m_barseries->setLabelsPosition(QBarSeries::LabelsCenter);
401 m_barseries->setLabelsPosition(QBarSeries::LabelsCenter);
401 TRY_COMPARE(labelsPositionSpy.count(), 1);
402 TRY_COMPARE(labelsPositionSpy.count(), 1);
402 arguments = labelsPositionSpy.takeFirst();
403 arguments = labelsPositionSpy.takeFirst();
403 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
404 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
404 == QBarSeries::LabelsCenter);
405 == QBarSeries::LabelsCenter);
405 QCOMPARE(m_barseries->labelsPosition(), QBarSeries::LabelsCenter);
406 QCOMPARE(m_barseries->labelsPosition(), QBarSeries::LabelsCenter);
406 }
407 }
407
408
409 void tst_QBarSeries::setLabelsAngle()
410 {
411 QSignalSpy labelsAngleSpy(m_barseries,
412 SIGNAL(labelsAngleChanged(qreal)));
413 QCOMPARE(m_barseries->labelsAngle(), 0.0);
414
415 m_barseries->setLabelsAngle(55.0);
416 TRY_COMPARE(labelsAngleSpy.count(), 1);
417 QList<QVariant> arguments = labelsAngleSpy.takeFirst();
418 QVERIFY(arguments.at(0).value<qreal>() == 55.0);
419 QCOMPARE(m_barseries->labelsAngle(), 55.0);
420 }
421
408 void tst_QBarSeries::opacity()
422 void tst_QBarSeries::opacity()
409 {
423 {
410 QSignalSpy opacitySpy(m_barseries, SIGNAL(opacityChanged()));
424 QSignalSpy opacitySpy(m_barseries, SIGNAL(opacityChanged()));
411
425
412 QCOMPARE(m_barseries->opacity(), 1.0);
426 QCOMPARE(m_barseries->opacity(), 1.0);
413
427
414 m_barseries->setOpacity(0.5);
428 m_barseries->setOpacity(0.5);
415 QCOMPARE(m_barseries->opacity(), 0.5);
429 QCOMPARE(m_barseries->opacity(), 0.5);
416 QCOMPARE(opacitySpy.count(), 1);
430 QCOMPARE(opacitySpy.count(), 1);
417
431
418 m_barseries->setOpacity(0.0);
432 m_barseries->setOpacity(0.0);
419 QCOMPARE(m_barseries->opacity(), 0.0);
433 QCOMPARE(m_barseries->opacity(), 0.0);
420 QCOMPARE(opacitySpy.count(), 2);
434 QCOMPARE(opacitySpy.count(), 2);
421
435
422 m_barseries->setOpacity(1.0);
436 m_barseries->setOpacity(1.0);
423 QCOMPARE(m_barseries->opacity(), 1.0);
437 QCOMPARE(m_barseries->opacity(), 1.0);
424 QCOMPARE(opacitySpy.count(), 3);
438 QCOMPARE(opacitySpy.count(), 3);
425 }
439 }
426
440
427 void tst_QBarSeries::mouseclicked_data()
441 void tst_QBarSeries::mouseclicked_data()
428 {
442 {
429
443
430 }
444 }
431
445
432 void tst_QBarSeries::mouseclicked()
446 void tst_QBarSeries::mouseclicked()
433 {
447 {
434 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
448 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
435
449
436 QBarSeries* series = new QBarSeries();
450 QBarSeries* series = new QBarSeries();
437
451
438 QBarSet* set1 = new QBarSet(QString("set 1"));
452 QBarSet* set1 = new QBarSet(QString("set 1"));
439 *set1 << 10 << 10 << 10;
453 *set1 << 10 << 10 << 10;
440 series->append(set1);
454 series->append(set1);
441
455
442 QBarSet* set2 = new QBarSet(QString("set 2"));
456 QBarSet* set2 = new QBarSet(QString("set 2"));
443 *set2 << 10 << 10 << 10;
457 *set2 << 10 << 10 << 10;
444 series->append(set2);
458 series->append(set2);
445 QList<QBarSet*> barSets = series->barSets();
459 QList<QBarSet*> barSets = series->barSets();
446
460
447 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
461 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
448 QSignalSpy setSpy1(set1, SIGNAL(clicked(int)));
462 QSignalSpy setSpy1(set1, SIGNAL(clicked(int)));
449 QSignalSpy setSpy2(set2, SIGNAL(clicked(int)));
463 QSignalSpy setSpy2(set2, SIGNAL(clicked(int)));
450
464
451 QChartView view(new QChart());
465 QChartView view(new QChart());
452 view.resize(400,300);
466 view.resize(400,300);
453 view.chart()->addSeries(series);
467 view.chart()->addSeries(series);
454 view.show();
468 view.show();
455 QTest::qWaitForWindowShown(&view);
469 QTest::qWaitForWindowShown(&view);
456
470
457 // Calculate expected layout for bars
471 // Calculate expected layout for bars
458 QRectF plotArea = view.chart()->plotArea();
472 QRectF plotArea = view.chart()->plotArea();
459 qreal width = plotArea.width();
473 qreal width = plotArea.width();
460 qreal height = plotArea.height();
474 qreal height = plotArea.height();
461 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
475 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
462 qreal rangeX = 3; // 3 values per set
476 qreal rangeX = 3; // 3 values per set
463 qreal scaleY = (height / rangeY);
477 qreal scaleY = (height / rangeY);
464 qreal scaleX = (width / rangeX);
478 qreal scaleX = (width / rangeX);
465
479
466 qreal setCount = series->count();
480 qreal setCount = series->count();
467 qreal domainMinY = 0; // These come from internal domain used by barseries.
481 qreal domainMinY = 0; // These come from internal domain used by barseries.
468 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
482 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
469 qreal rectWidth = (scaleX / setCount) * series->barWidth();
483 qreal rectWidth = (scaleX / setCount) * series->barWidth();
470
484
471 QVector<QRectF> layout;
485 QVector<QRectF> layout;
472
486
473 // 3 = count of values in set
487 // 3 = count of values in set
474 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
488 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
475 for (int i = 0; i < 3; i++) {
489 for (int i = 0; i < 3; i++) {
476 qreal yPos = height + scaleY * domainMinY + plotArea.top();
490 qreal yPos = height + scaleY * domainMinY + plotArea.top();
477 for (int set = 0; set < setCount; set++) {
491 for (int set = 0; set < setCount; set++) {
478 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
492 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
479 xPos -= series->count()*rectWidth/2;
493 xPos -= series->count()*rectWidth/2;
480 xPos += set*rectWidth;
494 xPos += set*rectWidth;
481
495
482 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
496 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
483 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
497 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
484 layout.append(rect);
498 layout.append(rect);
485 }
499 }
486 }
500 }
487
501
488 //====================================================================================
502 //====================================================================================
489 // barset 1, bar 0
503 // barset 1, bar 0
490 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
504 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
491 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
505 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
492
506
493 QCOMPARE(seriesSpy.count(), 1);
507 QCOMPARE(seriesSpy.count(), 1);
494 QCOMPARE(setSpy1.count(), 1);
508 QCOMPARE(setSpy1.count(), 1);
495 QCOMPARE(setSpy2.count(), 0);
509 QCOMPARE(setSpy2.count(), 0);
496
510
497 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
511 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
498 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
512 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
499 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
513 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
500 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
514 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
501
515
502 QList<QVariant> setSpyArg = setSpy1.takeFirst();
516 QList<QVariant> setSpyArg = setSpy1.takeFirst();
503 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
517 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
504 QVERIFY(setSpyArg.at(0).toInt() == 0);
518 QVERIFY(setSpyArg.at(0).toInt() == 0);
505
519
506 //====================================================================================
520 //====================================================================================
507 // barset 1, bar 1
521 // barset 1, bar 1
508 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
522 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
509 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
523 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
510
524
511 QCOMPARE(seriesSpy.count(), 1);
525 QCOMPARE(seriesSpy.count(), 1);
512 QCOMPARE(setSpy1.count(), 1);
526 QCOMPARE(setSpy1.count(), 1);
513 QCOMPARE(setSpy2.count(), 0);
527 QCOMPARE(setSpy2.count(), 0);
514
528
515 seriesSpyArg = seriesSpy.takeFirst();
529 seriesSpyArg = seriesSpy.takeFirst();
516 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
530 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
517 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
531 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
518 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
532 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
519
533
520 setSpyArg = setSpy1.takeFirst();
534 setSpyArg = setSpy1.takeFirst();
521 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
535 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
522 QVERIFY(setSpyArg.at(0).toInt() == 1);
536 QVERIFY(setSpyArg.at(0).toInt() == 1);
523
537
524 //====================================================================================
538 //====================================================================================
525 // barset 1, bar 2
539 // barset 1, bar 2
526 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
540 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
527 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
541 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
528
542
529 QCOMPARE(seriesSpy.count(), 1);
543 QCOMPARE(seriesSpy.count(), 1);
530 QCOMPARE(setSpy1.count(), 1);
544 QCOMPARE(setSpy1.count(), 1);
531 QCOMPARE(setSpy2.count(), 0);
545 QCOMPARE(setSpy2.count(), 0);
532
546
533 seriesSpyArg = seriesSpy.takeFirst();
547 seriesSpyArg = seriesSpy.takeFirst();
534 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
548 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
535 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
549 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
536 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
550 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
537
551
538 setSpyArg = setSpy1.takeFirst();
552 setSpyArg = setSpy1.takeFirst();
539 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
553 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
540 QVERIFY(setSpyArg.at(0).toInt() == 2);
554 QVERIFY(setSpyArg.at(0).toInt() == 2);
541
555
542 //====================================================================================
556 //====================================================================================
543 // barset 2, bar 0
557 // barset 2, bar 0
544 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
558 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
545 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
559 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
546
560
547 QCOMPARE(seriesSpy.count(), 1);
561 QCOMPARE(seriesSpy.count(), 1);
548 QCOMPARE(setSpy1.count(), 0);
562 QCOMPARE(setSpy1.count(), 0);
549 QCOMPARE(setSpy2.count(), 1);
563 QCOMPARE(setSpy2.count(), 1);
550
564
551 seriesSpyArg = seriesSpy.takeFirst();
565 seriesSpyArg = seriesSpy.takeFirst();
552 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
566 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
553 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
567 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
554 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
568 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
555
569
556 setSpyArg = setSpy2.takeFirst();
570 setSpyArg = setSpy2.takeFirst();
557 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
571 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
558 QVERIFY(setSpyArg.at(0).toInt() == 0);
572 QVERIFY(setSpyArg.at(0).toInt() == 0);
559
573
560 //====================================================================================
574 //====================================================================================
561 // barset 2, bar 1
575 // barset 2, bar 1
562 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
576 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
563 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
577 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
564
578
565 QCOMPARE(seriesSpy.count(), 1);
579 QCOMPARE(seriesSpy.count(), 1);
566 QCOMPARE(setSpy1.count(), 0);
580 QCOMPARE(setSpy1.count(), 0);
567 QCOMPARE(setSpy2.count(), 1);
581 QCOMPARE(setSpy2.count(), 1);
568
582
569 seriesSpyArg = seriesSpy.takeFirst();
583 seriesSpyArg = seriesSpy.takeFirst();
570 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
584 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
571 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
585 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
572 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
586 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
573
587
574 setSpyArg = setSpy2.takeFirst();
588 setSpyArg = setSpy2.takeFirst();
575 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
589 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
576 QVERIFY(setSpyArg.at(0).toInt() == 1);
590 QVERIFY(setSpyArg.at(0).toInt() == 1);
577
591
578 //====================================================================================
592 //====================================================================================
579 // barset 2, bar 2
593 // barset 2, bar 2
580 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
594 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
581 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
595 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
582
596
583 QCOMPARE(seriesSpy.count(), 1);
597 QCOMPARE(seriesSpy.count(), 1);
584 QCOMPARE(setSpy1.count(), 0);
598 QCOMPARE(setSpy1.count(), 0);
585 QCOMPARE(setSpy2.count(), 1);
599 QCOMPARE(setSpy2.count(), 1);
586
600
587 seriesSpyArg = seriesSpy.takeFirst();
601 seriesSpyArg = seriesSpy.takeFirst();
588 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
602 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
589 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
603 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
590 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
604 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
591
605
592 setSpyArg = setSpy2.takeFirst();
606 setSpyArg = setSpy2.takeFirst();
593 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
607 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
594 QVERIFY(setSpyArg.at(0).toInt() == 2);
608 QVERIFY(setSpyArg.at(0).toInt() == 2);
595 }
609 }
596
610
597 void tst_QBarSeries::mousehovered_data()
611 void tst_QBarSeries::mousehovered_data()
598 {
612 {
599
613
600 }
614 }
601
615
602 void tst_QBarSeries::mousehovered()
616 void tst_QBarSeries::mousehovered()
603 {
617 {
604 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
618 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
605
619
606 QBarSeries* series = new QBarSeries();
620 QBarSeries* series = new QBarSeries();
607
621
608 QBarSet* set1 = new QBarSet(QString("set 1"));
622 QBarSet* set1 = new QBarSet(QString("set 1"));
609 *set1 << 10 << 10 << 10;
623 *set1 << 10 << 10 << 10;
610 series->append(set1);
624 series->append(set1);
611
625
612 QBarSet* set2 = new QBarSet(QString("set 2"));
626 QBarSet* set2 = new QBarSet(QString("set 2"));
613 *set2 << 10 << 10 << 10;
627 *set2 << 10 << 10 << 10;
614 series->append(set2);
628 series->append(set2);
615 QList<QBarSet*> barSets = series->barSets();
629 QList<QBarSet*> barSets = series->barSets();
616
630
617 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
631 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
618 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
632 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
619 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
633 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
620
634
621 QChartView view(new QChart());
635 QChartView view(new QChart());
622 view.resize(400,300);
636 view.resize(400,300);
623 view.chart()->addSeries(series);
637 view.chart()->addSeries(series);
624 view.show();
638 view.show();
625 QTest::qWaitForWindowShown(&view);
639 QTest::qWaitForWindowShown(&view);
626
640
627 //this is hack since view does not get events otherwise
641 //this is hack since view does not get events otherwise
628 view.setMouseTracking(true);
642 view.setMouseTracking(true);
629
643
630 // Calculate expected layout for bars
644 // Calculate expected layout for bars
631 QRectF plotArea = view.chart()->plotArea();
645 QRectF plotArea = view.chart()->plotArea();
632 qreal width = plotArea.width();
646 qreal width = plotArea.width();
633 qreal height = plotArea.height();
647 qreal height = plotArea.height();
634 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
648 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
635 qreal rangeX = 3; // 3 values per set
649 qreal rangeX = 3; // 3 values per set
636 qreal scaleY = (height / rangeY);
650 qreal scaleY = (height / rangeY);
637 qreal scaleX = (width / rangeX);
651 qreal scaleX = (width / rangeX);
638
652
639 qreal setCount = series->count();
653 qreal setCount = series->count();
640 qreal domainMinY = 0; // These come from internal domain used by barseries.
654 qreal domainMinY = 0; // These come from internal domain used by barseries.
641 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
655 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
642 qreal rectWidth = (scaleX / setCount) * series->barWidth();
656 qreal rectWidth = (scaleX / setCount) * series->barWidth();
643
657
644 QVector<QRectF> layout;
658 QVector<QRectF> layout;
645
659
646 // 3 = count of values in set
660 // 3 = count of values in set
647 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
661 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
648 for (int i = 0; i < 3; i++) {
662 for (int i = 0; i < 3; i++) {
649 qreal yPos = height + scaleY * domainMinY + plotArea.top();
663 qreal yPos = height + scaleY * domainMinY + plotArea.top();
650 for (int set = 0; set < setCount; set++) {
664 for (int set = 0; set < setCount; set++) {
651 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
665 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
652 xPos -= series->count()*rectWidth/2;
666 xPos -= series->count()*rectWidth/2;
653 xPos += set*rectWidth;
667 xPos += set*rectWidth;
654
668
655 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
669 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
656 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
670 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
657 layout.append(rect);
671 layout.append(rect);
658 }
672 }
659 }
673 }
660
674
661 //=======================================================================
675 //=======================================================================
662 // move mouse to left border
676 // move mouse to left border
663 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(0).center().y()));
677 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(0).center().y()));
664 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
678 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
665 TRY_COMPARE(seriesIndexSpy.count(), 0);
679 TRY_COMPARE(seriesIndexSpy.count(), 0);
666 TRY_COMPARE(setIndexSpy1.count(), 0);
680 TRY_COMPARE(setIndexSpy1.count(), 0);
667 TRY_COMPARE(setIndexSpy2.count(), 0);
681 TRY_COMPARE(setIndexSpy2.count(), 0);
668
682
669 //=======================================================================
683 //=======================================================================
670 // move mouse on top of set1
684 // move mouse on top of set1
671 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
685 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
672 TRY_COMPARE(seriesIndexSpy.count(), 1);
686 TRY_COMPARE(seriesIndexSpy.count(), 1);
673 TRY_COMPARE(setIndexSpy1.count(), 1);
687 TRY_COMPARE(setIndexSpy1.count(), 1);
674 TRY_COMPARE(setIndexSpy2.count(), 0);
688 TRY_COMPARE(setIndexSpy2.count(), 0);
675
689
676 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
690 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
677 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
691 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
678 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
692 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
679 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
693 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
680
694
681 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
695 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
682 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
696 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
683 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
697 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
684
698
685 //=======================================================================
699 //=======================================================================
686 // move mouse from top of set1 to top of set2
700 // move mouse from top of set1 to top of set2
687 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
701 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
688 TRY_COMPARE(seriesIndexSpy.count(), 2);
702 TRY_COMPARE(seriesIndexSpy.count(), 2);
689 TRY_COMPARE(setIndexSpy1.count(), 1);
703 TRY_COMPARE(setIndexSpy1.count(), 1);
690 TRY_COMPARE(setIndexSpy2.count(), 1);
704 TRY_COMPARE(setIndexSpy2.count(), 1);
691
705
692 // should leave set1
706 // should leave set1
693 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
707 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
694 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
708 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
695 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
709 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
696 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
710 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
697
711
698 setIndexSpyArg = setIndexSpy1.takeFirst();
712 setIndexSpyArg = setIndexSpy1.takeFirst();
699 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
713 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
700 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
714 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
701
715
702 // should enter set2
716 // should enter set2
703 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
717 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
704 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
718 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
705 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
719 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
706 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
720 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
707
721
708 setIndexSpyArg = setIndexSpy2.takeFirst();
722 setIndexSpyArg = setIndexSpy2.takeFirst();
709 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
723 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
710 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
724 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
711
725
712 //=======================================================================
726 //=======================================================================
713 // move mouse from top of set2 to background
727 // move mouse from top of set2 to background
714 QTest::mouseMove(view.viewport(), QPoint(layout.at(1).center().x(), 0));
728 QTest::mouseMove(view.viewport(), QPoint(layout.at(1).center().x(), 0));
715 TRY_COMPARE(seriesIndexSpy.count(), 1);
729 TRY_COMPARE(seriesIndexSpy.count(), 1);
716 TRY_COMPARE(setIndexSpy1.count(), 0);
730 TRY_COMPARE(setIndexSpy1.count(), 0);
717 TRY_COMPARE(setIndexSpy2.count(), 1);
731 TRY_COMPARE(setIndexSpy2.count(), 1);
718
732
719 // should leave set2
733 // should leave set2
720 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
734 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
721 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
735 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
722 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
736 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
723 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
737 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
724
738
725 setIndexSpyArg = setIndexSpy2.takeFirst();
739 setIndexSpyArg = setIndexSpy2.takeFirst();
726 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
740 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
727 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
741 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
728
742
729 //=======================================================================
743 //=======================================================================
730 // move mouse on top of set1, bar0 to check the index (hover into set1)
744 // move mouse on top of set1, bar0 to check the index (hover into set1)
731 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
745 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
732
746
733 TRY_COMPARE(seriesIndexSpy.count(), 1);
747 TRY_COMPARE(seriesIndexSpy.count(), 1);
734 TRY_COMPARE(setIndexSpy1.count(), 1);
748 TRY_COMPARE(setIndexSpy1.count(), 1);
735 TRY_COMPARE(setIndexSpy2.count(), 0);
749 TRY_COMPARE(setIndexSpy2.count(), 0);
736
750
737 //should enter set1, bar0
751 //should enter set1, bar0
738 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
752 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
739 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
753 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
740 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
754 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
741 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
755 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
742 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
756 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
743 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
757 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
744
758
745 setIndexSpyArg = setIndexSpy1.takeFirst();
759 setIndexSpyArg = setIndexSpy1.takeFirst();
746 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
760 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
747 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
761 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
748 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
762 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
749 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
763 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
750
764
751 //=======================================================================
765 //=======================================================================
752 // move mouse on top of set2, bar0 to check the index (hover out set1,
766 // move mouse on top of set2, bar0 to check the index (hover out set1,
753 // hover in set1)
767 // hover in set1)
754 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
768 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
755
769
756 TRY_COMPARE(seriesIndexSpy.count(), 2);
770 TRY_COMPARE(seriesIndexSpy.count(), 2);
757 TRY_COMPARE(setIndexSpy1.count(), 1);
771 TRY_COMPARE(setIndexSpy1.count(), 1);
758 TRY_COMPARE(setIndexSpy2.count(), 1);
772 TRY_COMPARE(setIndexSpy2.count(), 1);
759
773
760 // should leave set1, bar0
774 // should leave set1, bar0
761 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
775 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
762 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
776 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
763 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
777 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
764 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
778 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
765 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
779 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
766 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
780 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
767
781
768 setIndexSpyArg = setIndexSpy1.takeFirst();
782 setIndexSpyArg = setIndexSpy1.takeFirst();
769 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
783 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
770 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
784 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
771 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
785 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
772 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
786 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
773
787
774 // should enter set2, bar0
788 // should enter set2, bar0
775 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
789 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
776 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
790 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
777 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
791 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
778 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
792 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
779 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
793 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
780 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
794 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
781
795
782 setIndexSpyArg = setIndexSpy2.takeFirst();
796 setIndexSpyArg = setIndexSpy2.takeFirst();
783 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
797 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
784 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
798 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
785 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
799 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
786 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
800 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
787
801
788 //=======================================================================
802 //=======================================================================
789 // move mouse on top of set1, bar1 to check the index (hover out set 2,
803 // move mouse on top of set1, bar1 to check the index (hover out set 2,
790 // hover in set1)
804 // hover in set1)
791 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
805 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
792
806
793 TRY_COMPARE(seriesIndexSpy.count(), 2);
807 TRY_COMPARE(seriesIndexSpy.count(), 2);
794 TRY_COMPARE(setIndexSpy1.count(), 1);
808 TRY_COMPARE(setIndexSpy1.count(), 1);
795 TRY_COMPARE(setIndexSpy2.count(), 1);
809 TRY_COMPARE(setIndexSpy2.count(), 1);
796
810
797 // should leave set2, bar0
811 // should leave set2, bar0
798 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
812 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
799 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
813 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
800 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
814 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
801 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
815 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
802 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
816 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
803 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
817 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
804
818
805 setIndexSpyArg = setIndexSpy2.takeFirst();
819 setIndexSpyArg = setIndexSpy2.takeFirst();
806 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
820 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
807 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
821 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
808 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
822 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
809 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
823 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
810
824
811 // should enter set1, bar1
825 // should enter set1, bar1
812 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
826 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
813 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
827 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
814 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
828 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
815 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
829 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
816 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
830 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
817 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
831 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
818
832
819 setIndexSpyArg = setIndexSpy1.takeFirst();
833 setIndexSpyArg = setIndexSpy1.takeFirst();
820 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
834 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
821 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
835 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
822 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
836 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
823 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
837 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
824
838
825 //=======================================================================
839 //=======================================================================
826 // move mouse on top of view between set1 and set2 to check the index
840 // move mouse on top of view between set1 and set2 to check the index
827 // (hover out set1)
841 // (hover out set1)
828 QTest::mouseMove(view.viewport(), QPoint((layout.at(2).right() + layout.at(3).left()) / 2, 0));
842 QTest::mouseMove(view.viewport(), QPoint((layout.at(2).right() + layout.at(3).left()) / 2, 0));
829
843
830 TRY_COMPARE(seriesIndexSpy.count(), 1);
844 TRY_COMPARE(seriesIndexSpy.count(), 1);
831 TRY_COMPARE(setIndexSpy1.count(), 1);
845 TRY_COMPARE(setIndexSpy1.count(), 1);
832 TRY_COMPARE(setIndexSpy2.count(), 0);
846 TRY_COMPARE(setIndexSpy2.count(), 0);
833
847
834 // should leave set1, bar1
848 // should leave set1, bar1
835 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
849 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
836 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
850 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
837 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
851 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
838 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
852 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
839 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
853 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
840 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
854 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
841
855
842 setIndexSpyArg = setIndexSpy1.takeFirst();
856 setIndexSpyArg = setIndexSpy1.takeFirst();
843 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
857 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
844 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
858 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
845 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
859 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
846 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
860 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
847
861
848 //=======================================================================
862 //=======================================================================
849 // move mouse on top of set2, bar1 to check the index (hover in set2)
863 // move mouse on top of set2, bar1 to check the index (hover in set2)
850 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
864 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
851
865
852 TRY_COMPARE(seriesIndexSpy.count(), 1);
866 TRY_COMPARE(seriesIndexSpy.count(), 1);
853 TRY_COMPARE(setIndexSpy1.count(), 0);
867 TRY_COMPARE(setIndexSpy1.count(), 0);
854 TRY_COMPARE(setIndexSpy2.count(), 1);
868 TRY_COMPARE(setIndexSpy2.count(), 1);
855
869
856 // should enter set2, bar1
870 // should enter set2, bar1
857 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
871 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
858 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
872 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
859 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
873 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
860 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
874 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
861 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
875 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
862 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
876 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
863
877
864 setIndexSpyArg = setIndexSpy2.takeFirst();
878 setIndexSpyArg = setIndexSpy2.takeFirst();
865 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
879 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
866 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
880 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
867 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
881 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
868 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
882 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
869
883
870 //=======================================================================
884 //=======================================================================
871 // move mouse on top of the view between bar1 and bar2 to check the index
885 // move mouse on top of the view between bar1 and bar2 to check the index
872 //(hover out set2)
886 //(hover out set2)
873 QTest::mouseMove(view.viewport(), QPoint((layout.at(3).right() + layout.at(3).left()) / 2, 0));
887 QTest::mouseMove(view.viewport(), QPoint((layout.at(3).right() + layout.at(3).left()) / 2, 0));
874
888
875 TRY_COMPARE(seriesIndexSpy.count(), 1);
889 TRY_COMPARE(seriesIndexSpy.count(), 1);
876 TRY_COMPARE(setIndexSpy1.count(), 0);
890 TRY_COMPARE(setIndexSpy1.count(), 0);
877 TRY_COMPARE(setIndexSpy2.count(), 1);
891 TRY_COMPARE(setIndexSpy2.count(), 1);
878
892
879 // should leave set2, bar1
893 // should leave set2, bar1
880 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
894 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
881 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
895 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
882 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
896 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
883 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
897 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
884 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
898 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
885 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
899 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
886
900
887 setIndexSpyArg = setIndexSpy2.takeFirst();
901 setIndexSpyArg = setIndexSpy2.takeFirst();
888 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
902 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
889 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
903 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
890 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
904 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
891 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
905 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
892 }
906 }
893
907
894 void tst_QBarSeries::clearWithAnimations()
908 void tst_QBarSeries::clearWithAnimations()
895 {
909 {
896 QBarSeries* series = new QBarSeries();
910 QBarSeries* series = new QBarSeries();
897
911
898 QBarSet* set1 = new QBarSet(QString("set 1"));
912 QBarSet* set1 = new QBarSet(QString("set 1"));
899 *set1 << 10 << 10 << 10;
913 *set1 << 10 << 10 << 10;
900 series->append(set1);
914 series->append(set1);
901
915
902 QBarSet* set2 = new QBarSet(QString("set 2"));
916 QBarSet* set2 = new QBarSet(QString("set 2"));
903 *set2 << 10 << 10 << 10;
917 *set2 << 10 << 10 << 10;
904 series->append(set2);
918 series->append(set2);
905
919
906 QChartView view(new QChart());
920 QChartView view(new QChart());
907 view.resize(400,300);
921 view.resize(400,300);
908 view.chart()->setAnimationOptions(QChart::SeriesAnimations);
922 view.chart()->setAnimationOptions(QChart::SeriesAnimations);
909 view.chart()->addSeries(series);
923 view.chart()->addSeries(series);
910 view.show();
924 view.show();
911
925
912 series->clear();
926 series->clear();
913 }
927 }
914
928
915 void tst_QBarSeries::destruction()
929 void tst_QBarSeries::destruction()
916 {
930 {
917 // add a barset
931 // add a barset
918 QBarSeries *series = new QBarSeries();
932 QBarSeries *series = new QBarSeries();
919 QBarSet *set = new QBarSet("testset");
933 QBarSet *set = new QBarSet("testset");
920 QSignalSpy spy1(set, SIGNAL(destroyed()));
934 QSignalSpy spy1(set, SIGNAL(destroyed()));
921 series->append(set);
935 series->append(set);
922
936
923 // delete the series
937 // delete the series
924 delete series;
938 delete series;
925
939
926 // check that series deletes the set
940 // check that series deletes the set
927 QCOMPARE(spy1.count(), 1);
941 QCOMPARE(spy1.count(), 1);
928 }
942 }
929
943
930 void tst_QBarSeries::mousePressed()
944 void tst_QBarSeries::mousePressed()
931 {
945 {
932 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
946 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
933
947
934 QBarSeries* series = new QBarSeries();
948 QBarSeries* series = new QBarSeries();
935
949
936 QBarSet* set1 = new QBarSet(QString("set 1"));
950 QBarSet* set1 = new QBarSet(QString("set 1"));
937 *set1 << 10 << 10 << 10;
951 *set1 << 10 << 10 << 10;
938 series->append(set1);
952 series->append(set1);
939
953
940 QBarSet* set2 = new QBarSet(QString("set 2"));
954 QBarSet* set2 = new QBarSet(QString("set 2"));
941 *set2 << 10 << 10 << 10;
955 *set2 << 10 << 10 << 10;
942 series->append(set2);
956 series->append(set2);
943 QList<QBarSet*> barSets = series->barSets();
957 QList<QBarSet*> barSets = series->barSets();
944
958
945 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
959 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
946 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
960 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
947 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
961 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
948
962
949 QChartView view(new QChart());
963 QChartView view(new QChart());
950 view.resize(400,300);
964 view.resize(400,300);
951 view.chart()->addSeries(series);
965 view.chart()->addSeries(series);
952 view.show();
966 view.show();
953 QTest::qWaitForWindowShown(&view);
967 QTest::qWaitForWindowShown(&view);
954
968
955 // Calculate expected layout for bars
969 // Calculate expected layout for bars
956 QRectF plotArea = view.chart()->plotArea();
970 QRectF plotArea = view.chart()->plotArea();
957 qreal width = plotArea.width();
971 qreal width = plotArea.width();
958 qreal height = plotArea.height();
972 qreal height = plotArea.height();
959 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
973 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
960 qreal rangeX = 3; // 3 values per set
974 qreal rangeX = 3; // 3 values per set
961 qreal scaleY = (height / rangeY);
975 qreal scaleY = (height / rangeY);
962 qreal scaleX = (width / rangeX);
976 qreal scaleX = (width / rangeX);
963
977
964 qreal setCount = series->count();
978 qreal setCount = series->count();
965 qreal domainMinY = 0; // These come from internal domain used by barseries.
979 qreal domainMinY = 0; // These come from internal domain used by barseries.
966 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
980 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
967 qreal rectWidth = (scaleX / setCount) * series->barWidth();
981 qreal rectWidth = (scaleX / setCount) * series->barWidth();
968
982
969 QVector<QRectF> layout;
983 QVector<QRectF> layout;
970
984
971 // 3 = count of values in set
985 // 3 = count of values in set
972 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
986 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
973 for (int i = 0; i < 3; i++) {
987 for (int i = 0; i < 3; i++) {
974 qreal yPos = height + scaleY * domainMinY + plotArea.top();
988 qreal yPos = height + scaleY * domainMinY + plotArea.top();
975 for (int set = 0; set < setCount; set++) {
989 for (int set = 0; set < setCount; set++) {
976 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
990 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
977 xPos -= series->count()*rectWidth/2;
991 xPos -= series->count()*rectWidth/2;
978 xPos += set*rectWidth;
992 xPos += set*rectWidth;
979
993
980 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
994 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
981 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
995 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
982 layout.append(rect);
996 layout.append(rect);
983 }
997 }
984 }
998 }
985
999
986 //====================================================================================
1000 //====================================================================================
987 // barset 1, bar 0
1001 // barset 1, bar 0
988 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1002 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
989 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1003 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
990
1004
991 QCOMPARE(seriesSpy.count(), 1);
1005 QCOMPARE(seriesSpy.count(), 1);
992 QCOMPARE(setSpy1.count(), 1);
1006 QCOMPARE(setSpy1.count(), 1);
993 QCOMPARE(setSpy2.count(), 0);
1007 QCOMPARE(setSpy2.count(), 0);
994
1008
995 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1009 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
996 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1010 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
997 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1011 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
998 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1012 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
999
1013
1000 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1014 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1001 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1015 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1002 QVERIFY(setSpyArg.at(0).toInt() == 0);
1016 QVERIFY(setSpyArg.at(0).toInt() == 0);
1003
1017
1004 //====================================================================================
1018 //====================================================================================
1005 // barset 1, bar 1
1019 // barset 1, bar 1
1006 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
1020 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
1007 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1021 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1008
1022
1009 QCOMPARE(seriesSpy.count(), 1);
1023 QCOMPARE(seriesSpy.count(), 1);
1010 QCOMPARE(setSpy1.count(), 1);
1024 QCOMPARE(setSpy1.count(), 1);
1011 QCOMPARE(setSpy2.count(), 0);
1025 QCOMPARE(setSpy2.count(), 0);
1012
1026
1013 seriesSpyArg = seriesSpy.takeFirst();
1027 seriesSpyArg = seriesSpy.takeFirst();
1014 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1028 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1015 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1029 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1016 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1030 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1017
1031
1018 setSpyArg = setSpy1.takeFirst();
1032 setSpyArg = setSpy1.takeFirst();
1019 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1033 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1020 QVERIFY(setSpyArg.at(0).toInt() == 1);
1034 QVERIFY(setSpyArg.at(0).toInt() == 1);
1021
1035
1022 //====================================================================================
1036 //====================================================================================
1023 // barset 1, bar 2
1037 // barset 1, bar 2
1024 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
1038 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
1025 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1039 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1026
1040
1027 QCOMPARE(seriesSpy.count(), 1);
1041 QCOMPARE(seriesSpy.count(), 1);
1028 QCOMPARE(setSpy1.count(), 1);
1042 QCOMPARE(setSpy1.count(), 1);
1029 QCOMPARE(setSpy2.count(), 0);
1043 QCOMPARE(setSpy2.count(), 0);
1030
1044
1031 seriesSpyArg = seriesSpy.takeFirst();
1045 seriesSpyArg = seriesSpy.takeFirst();
1032 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1046 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1033 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1047 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1034 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1048 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1035
1049
1036 setSpyArg = setSpy1.takeFirst();
1050 setSpyArg = setSpy1.takeFirst();
1037 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1051 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1038 QVERIFY(setSpyArg.at(0).toInt() == 2);
1052 QVERIFY(setSpyArg.at(0).toInt() == 2);
1039
1053
1040 //====================================================================================
1054 //====================================================================================
1041 // barset 2, bar 0
1055 // barset 2, bar 0
1042 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
1056 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
1043 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1057 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1044
1058
1045 QCOMPARE(seriesSpy.count(), 1);
1059 QCOMPARE(seriesSpy.count(), 1);
1046 QCOMPARE(setSpy1.count(), 0);
1060 QCOMPARE(setSpy1.count(), 0);
1047 QCOMPARE(setSpy2.count(), 1);
1061 QCOMPARE(setSpy2.count(), 1);
1048
1062
1049 seriesSpyArg = seriesSpy.takeFirst();
1063 seriesSpyArg = seriesSpy.takeFirst();
1050 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1064 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1051 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1065 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1052 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1066 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1053
1067
1054 setSpyArg = setSpy2.takeFirst();
1068 setSpyArg = setSpy2.takeFirst();
1055 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1069 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1056 QVERIFY(setSpyArg.at(0).toInt() == 0);
1070 QVERIFY(setSpyArg.at(0).toInt() == 0);
1057
1071
1058 //====================================================================================
1072 //====================================================================================
1059 // barset 2, bar 1
1073 // barset 2, bar 1
1060 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
1074 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
1061 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1075 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1062
1076
1063 QCOMPARE(seriesSpy.count(), 1);
1077 QCOMPARE(seriesSpy.count(), 1);
1064 QCOMPARE(setSpy1.count(), 0);
1078 QCOMPARE(setSpy1.count(), 0);
1065 QCOMPARE(setSpy2.count(), 1);
1079 QCOMPARE(setSpy2.count(), 1);
1066
1080
1067 seriesSpyArg = seriesSpy.takeFirst();
1081 seriesSpyArg = seriesSpy.takeFirst();
1068 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1082 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1069 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1083 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1070 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1084 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1071
1085
1072 setSpyArg = setSpy2.takeFirst();
1086 setSpyArg = setSpy2.takeFirst();
1073 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1087 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1074 QVERIFY(setSpyArg.at(0).toInt() == 1);
1088 QVERIFY(setSpyArg.at(0).toInt() == 1);
1075
1089
1076 //====================================================================================
1090 //====================================================================================
1077 // barset 2, bar 2
1091 // barset 2, bar 2
1078 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
1092 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
1079 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1093 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1080
1094
1081 QCOMPARE(seriesSpy.count(), 1);
1095 QCOMPARE(seriesSpy.count(), 1);
1082 QCOMPARE(setSpy1.count(), 0);
1096 QCOMPARE(setSpy1.count(), 0);
1083 QCOMPARE(setSpy2.count(), 1);
1097 QCOMPARE(setSpy2.count(), 1);
1084
1098
1085 seriesSpyArg = seriesSpy.takeFirst();
1099 seriesSpyArg = seriesSpy.takeFirst();
1086 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1100 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1087 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1101 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1088 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1102 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1089
1103
1090 setSpyArg = setSpy2.takeFirst();
1104 setSpyArg = setSpy2.takeFirst();
1091 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1105 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1092 QVERIFY(setSpyArg.at(0).toInt() == 2);
1106 QVERIFY(setSpyArg.at(0).toInt() == 2);
1093 }
1107 }
1094
1108
1095 void tst_QBarSeries::mouseReleased()
1109 void tst_QBarSeries::mouseReleased()
1096 {
1110 {
1097 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
1111 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
1098
1112
1099 QBarSeries* series = new QBarSeries();
1113 QBarSeries* series = new QBarSeries();
1100
1114
1101 QBarSet* set1 = new QBarSet(QString("set 1"));
1115 QBarSet* set1 = new QBarSet(QString("set 1"));
1102 *set1 << 10 << 10 << 10;
1116 *set1 << 10 << 10 << 10;
1103 series->append(set1);
1117 series->append(set1);
1104
1118
1105 QBarSet* set2 = new QBarSet(QString("set 2"));
1119 QBarSet* set2 = new QBarSet(QString("set 2"));
1106 *set2 << 10 << 10 << 10;
1120 *set2 << 10 << 10 << 10;
1107 series->append(set2);
1121 series->append(set2);
1108 QList<QBarSet*> barSets = series->barSets();
1122 QList<QBarSet*> barSets = series->barSets();
1109
1123
1110 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
1124 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
1111 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
1125 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
1112 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
1126 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
1113
1127
1114 QChartView view(new QChart());
1128 QChartView view(new QChart());
1115 view.resize(400,300);
1129 view.resize(400,300);
1116 view.chart()->addSeries(series);
1130 view.chart()->addSeries(series);
1117 view.show();
1131 view.show();
1118 QTest::qWaitForWindowShown(&view);
1132 QTest::qWaitForWindowShown(&view);
1119
1133
1120 // Calculate expected layout for bars
1134 // Calculate expected layout for bars
1121 QRectF plotArea = view.chart()->plotArea();
1135 QRectF plotArea = view.chart()->plotArea();
1122 qreal width = plotArea.width();
1136 qreal width = plotArea.width();
1123 qreal height = plotArea.height();
1137 qreal height = plotArea.height();
1124 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
1138 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
1125 qreal rangeX = 3; // 3 values per set
1139 qreal rangeX = 3; // 3 values per set
1126 qreal scaleY = (height / rangeY);
1140 qreal scaleY = (height / rangeY);
1127 qreal scaleX = (width / rangeX);
1141 qreal scaleX = (width / rangeX);
1128
1142
1129 qreal setCount = series->count();
1143 qreal setCount = series->count();
1130 qreal domainMinY = 0; // These come from internal domain used by barseries.
1144 qreal domainMinY = 0; // These come from internal domain used by barseries.
1131 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
1145 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
1132 qreal rectWidth = (scaleX / setCount) * series->barWidth();
1146 qreal rectWidth = (scaleX / setCount) * series->barWidth();
1133
1147
1134 QVector<QRectF> layout;
1148 QVector<QRectF> layout;
1135
1149
1136 // 3 = count of values in set
1150 // 3 = count of values in set
1137 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
1151 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
1138 for (int i = 0; i < 3; i++) {
1152 for (int i = 0; i < 3; i++) {
1139 qreal yPos = height + scaleY * domainMinY + plotArea.top();
1153 qreal yPos = height + scaleY * domainMinY + plotArea.top();
1140 for (int set = 0; set < setCount; set++) {
1154 for (int set = 0; set < setCount; set++) {
1141 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
1155 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
1142 xPos -= series->count()*rectWidth/2;
1156 xPos -= series->count()*rectWidth/2;
1143 xPos += set*rectWidth;
1157 xPos += set*rectWidth;
1144
1158
1145 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
1159 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
1146 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
1160 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
1147 layout.append(rect);
1161 layout.append(rect);
1148 }
1162 }
1149 }
1163 }
1150
1164
1151 //====================================================================================
1165 //====================================================================================
1152 // barset 1, bar 0
1166 // barset 1, bar 0
1153 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1167 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1154 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1168 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1155
1169
1156 QCOMPARE(seriesSpy.count(), 1);
1170 QCOMPARE(seriesSpy.count(), 1);
1157 QCOMPARE(setSpy1.count(), 1);
1171 QCOMPARE(setSpy1.count(), 1);
1158 QCOMPARE(setSpy2.count(), 0);
1172 QCOMPARE(setSpy2.count(), 0);
1159
1173
1160 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1174 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1161 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1175 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1162 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1176 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1163 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1177 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1164
1178
1165 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1179 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1166 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1180 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1167 QVERIFY(setSpyArg.at(0).toInt() == 0);
1181 QVERIFY(setSpyArg.at(0).toInt() == 0);
1168
1182
1169 //====================================================================================
1183 //====================================================================================
1170 // barset 1, bar 1
1184 // barset 1, bar 1
1171 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
1185 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
1172 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1186 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1173
1187
1174 QCOMPARE(seriesSpy.count(), 1);
1188 QCOMPARE(seriesSpy.count(), 1);
1175 QCOMPARE(setSpy1.count(), 1);
1189 QCOMPARE(setSpy1.count(), 1);
1176 QCOMPARE(setSpy2.count(), 0);
1190 QCOMPARE(setSpy2.count(), 0);
1177
1191
1178 seriesSpyArg = seriesSpy.takeFirst();
1192 seriesSpyArg = seriesSpy.takeFirst();
1179 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1193 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1180 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1194 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1181 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1195 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1182
1196
1183 setSpyArg = setSpy1.takeFirst();
1197 setSpyArg = setSpy1.takeFirst();
1184 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1198 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1185 QVERIFY(setSpyArg.at(0).toInt() == 1);
1199 QVERIFY(setSpyArg.at(0).toInt() == 1);
1186
1200
1187 //====================================================================================
1201 //====================================================================================
1188 // barset 1, bar 2
1202 // barset 1, bar 2
1189 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
1203 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
1190 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1204 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1191
1205
1192 QCOMPARE(seriesSpy.count(), 1);
1206 QCOMPARE(seriesSpy.count(), 1);
1193 QCOMPARE(setSpy1.count(), 1);
1207 QCOMPARE(setSpy1.count(), 1);
1194 QCOMPARE(setSpy2.count(), 0);
1208 QCOMPARE(setSpy2.count(), 0);
1195
1209
1196 seriesSpyArg = seriesSpy.takeFirst();
1210 seriesSpyArg = seriesSpy.takeFirst();
1197 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1211 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1198 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1212 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1199 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1213 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1200
1214
1201 setSpyArg = setSpy1.takeFirst();
1215 setSpyArg = setSpy1.takeFirst();
1202 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1216 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1203 QVERIFY(setSpyArg.at(0).toInt() == 2);
1217 QVERIFY(setSpyArg.at(0).toInt() == 2);
1204
1218
1205 //====================================================================================
1219 //====================================================================================
1206 // barset 2, bar 0
1220 // barset 2, bar 0
1207 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
1221 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
1208 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1222 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1209
1223
1210 QCOMPARE(seriesSpy.count(), 1);
1224 QCOMPARE(seriesSpy.count(), 1);
1211 QCOMPARE(setSpy1.count(), 0);
1225 QCOMPARE(setSpy1.count(), 0);
1212 QCOMPARE(setSpy2.count(), 1);
1226 QCOMPARE(setSpy2.count(), 1);
1213
1227
1214 seriesSpyArg = seriesSpy.takeFirst();
1228 seriesSpyArg = seriesSpy.takeFirst();
1215 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1229 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1216 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1230 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1217 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1231 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1218
1232
1219 setSpyArg = setSpy2.takeFirst();
1233 setSpyArg = setSpy2.takeFirst();
1220 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1234 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1221 QVERIFY(setSpyArg.at(0).toInt() == 0);
1235 QVERIFY(setSpyArg.at(0).toInt() == 0);
1222
1236
1223 //====================================================================================
1237 //====================================================================================
1224 // barset 2, bar 1
1238 // barset 2, bar 1
1225 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
1239 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
1226 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1240 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1227
1241
1228 QCOMPARE(seriesSpy.count(), 1);
1242 QCOMPARE(seriesSpy.count(), 1);
1229 QCOMPARE(setSpy1.count(), 0);
1243 QCOMPARE(setSpy1.count(), 0);
1230 QCOMPARE(setSpy2.count(), 1);
1244 QCOMPARE(setSpy2.count(), 1);
1231
1245
1232 seriesSpyArg = seriesSpy.takeFirst();
1246 seriesSpyArg = seriesSpy.takeFirst();
1233 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1247 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1234 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1248 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1235 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1249 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1236
1250
1237 setSpyArg = setSpy2.takeFirst();
1251 setSpyArg = setSpy2.takeFirst();
1238 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1252 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1239 QVERIFY(setSpyArg.at(0).toInt() == 1);
1253 QVERIFY(setSpyArg.at(0).toInt() == 1);
1240
1254
1241 //====================================================================================
1255 //====================================================================================
1242 // barset 2, bar 2
1256 // barset 2, bar 2
1243 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
1257 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
1244 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1258 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1245
1259
1246 QCOMPARE(seriesSpy.count(), 1);
1260 QCOMPARE(seriesSpy.count(), 1);
1247 QCOMPARE(setSpy1.count(), 0);
1261 QCOMPARE(setSpy1.count(), 0);
1248 QCOMPARE(setSpy2.count(), 1);
1262 QCOMPARE(setSpy2.count(), 1);
1249
1263
1250 seriesSpyArg = seriesSpy.takeFirst();
1264 seriesSpyArg = seriesSpy.takeFirst();
1251 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1265 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1252 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1266 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1253 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1267 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1254
1268
1255 setSpyArg = setSpy2.takeFirst();
1269 setSpyArg = setSpy2.takeFirst();
1256 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1270 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1257 QVERIFY(setSpyArg.at(0).toInt() == 2);
1271 QVERIFY(setSpyArg.at(0).toInt() == 2);
1258 }
1272 }
1259
1273
1260 void tst_QBarSeries::mouseDoubleClicked()
1274 void tst_QBarSeries::mouseDoubleClicked()
1261 {
1275 {
1262 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
1276 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
1263
1277
1264 QBarSeries* series = new QBarSeries();
1278 QBarSeries* series = new QBarSeries();
1265
1279
1266 QBarSet* set1 = new QBarSet(QString("set 1"));
1280 QBarSet* set1 = new QBarSet(QString("set 1"));
1267 *set1 << 10 << 10 << 10;
1281 *set1 << 10 << 10 << 10;
1268 series->append(set1);
1282 series->append(set1);
1269
1283
1270 QBarSet* set2 = new QBarSet(QString("set 2"));
1284 QBarSet* set2 = new QBarSet(QString("set 2"));
1271 *set2 << 10 << 10 << 10;
1285 *set2 << 10 << 10 << 10;
1272 series->append(set2);
1286 series->append(set2);
1273 QList<QBarSet*> barSets = series->barSets();
1287 QList<QBarSet*> barSets = series->barSets();
1274
1288
1275 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
1289 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
1276 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
1290 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
1277 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
1291 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
1278
1292
1279 QChartView view(new QChart());
1293 QChartView view(new QChart());
1280 view.resize(400,300);
1294 view.resize(400,300);
1281 view.chart()->addSeries(series);
1295 view.chart()->addSeries(series);
1282 view.show();
1296 view.show();
1283 QTest::qWaitForWindowShown(&view);
1297 QTest::qWaitForWindowShown(&view);
1284
1298
1285 // Calculate expected layout for bars
1299 // Calculate expected layout for bars
1286 QRectF plotArea = view.chart()->plotArea();
1300 QRectF plotArea = view.chart()->plotArea();
1287 qreal width = plotArea.width();
1301 qreal width = plotArea.width();
1288 qreal height = plotArea.height();
1302 qreal height = plotArea.height();
1289 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
1303 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
1290 qreal rangeX = 3; // 3 values per set
1304 qreal rangeX = 3; // 3 values per set
1291 qreal scaleY = (height / rangeY);
1305 qreal scaleY = (height / rangeY);
1292 qreal scaleX = (width / rangeX);
1306 qreal scaleX = (width / rangeX);
1293
1307
1294 qreal setCount = series->count();
1308 qreal setCount = series->count();
1295 qreal domainMinY = 0; // These come from internal domain used by barseries.
1309 qreal domainMinY = 0; // These come from internal domain used by barseries.
1296 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
1310 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
1297 qreal rectWidth = (scaleX / setCount) * series->barWidth();
1311 qreal rectWidth = (scaleX / setCount) * series->barWidth();
1298
1312
1299 QVector<QRectF> layout;
1313 QVector<QRectF> layout;
1300
1314
1301 // 3 = count of values in set
1315 // 3 = count of values in set
1302 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
1316 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
1303 for (int i = 0; i < 3; i++) {
1317 for (int i = 0; i < 3; i++) {
1304 qreal yPos = height + scaleY * domainMinY + plotArea.top();
1318 qreal yPos = height + scaleY * domainMinY + plotArea.top();
1305 for (int set = 0; set < setCount; set++) {
1319 for (int set = 0; set < setCount; set++) {
1306 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
1320 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
1307 xPos -= series->count()*rectWidth/2;
1321 xPos -= series->count()*rectWidth/2;
1308 xPos += set*rectWidth;
1322 xPos += set*rectWidth;
1309
1323
1310 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
1324 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
1311 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
1325 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
1312 layout.append(rect);
1326 layout.append(rect);
1313 }
1327 }
1314 }
1328 }
1315
1329
1316 // barset 1, bar 0
1330 // barset 1, bar 0
1317 QTest::mouseDClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1331 QTest::mouseDClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1318 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1332 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1319
1333
1320 QCOMPARE(seriesSpy.count(), 1);
1334 QCOMPARE(seriesSpy.count(), 1);
1321 QCOMPARE(setSpy1.count(), 1);
1335 QCOMPARE(setSpy1.count(), 1);
1322 QCOMPARE(setSpy2.count(), 0);
1336 QCOMPARE(setSpy2.count(), 0);
1323
1337
1324 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1338 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1325 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1339 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1326 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1340 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1327 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1341 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1328
1342
1329 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1343 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1330 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1344 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1331 QVERIFY(setSpyArg.at(0).toInt() == 0);
1345 QVERIFY(setSpyArg.at(0).toInt() == 0);
1332 }
1346 }
1333
1347
1334 QTEST_MAIN(tst_QBarSeries)
1348 QTEST_MAIN(tst_QBarSeries)
1335
1349
1336 #include "tst_qbarseries.moc"
1350 #include "tst_qbarseries.moc"
1337
1351
@@ -1,1269 +1,1283
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd
3 ** Copyright (C) 2015 The Qt Company Ltd
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 **
6 **
7 ** This file is part of the Qt Charts module.
7 ** This file is part of the Qt Charts module.
8 **
8 **
9 ** Licensees holding valid commercial license for Qt may use this file in
9 ** Licensees holding valid commercial license for Qt may use this file in
10 ** accordance with the Qt License Agreement provided with the Software
10 ** accordance with the Qt License Agreement provided with the Software
11 ** or, alternatively, in accordance with the terms contained in a written
11 ** or, alternatively, in accordance with the terms contained in a written
12 ** agreement between you and The Qt Company.
12 ** agreement between you and The Qt Company.
13 **
13 **
14 ** If you have questions regarding the use of this file, please use
14 ** If you have questions regarding the use of this file, please use
15 ** contact form at http://qt.io
15 ** contact form at http://qt.io
16 **
16 **
17 ****************************************************************************/
17 ****************************************************************************/
18
18
19 #include <QtTest/QtTest>
19 #include <QtTest/QtTest>
20 #include <QtCharts/QHorizontalBarSeries>
20 #include <QtCharts/QHorizontalBarSeries>
21 #include <QtCharts/QBarSet>
21 #include <QtCharts/QBarSet>
22 #include <QtCharts/QChartView>
22 #include <QtCharts/QChartView>
23 #include <QtCharts/QChart>
23 #include <QtCharts/QChart>
24 #include "tst_definitions.h"
24 #include "tst_definitions.h"
25
25
26 QT_CHARTS_USE_NAMESPACE
26 QT_CHARTS_USE_NAMESPACE
27
27
28 Q_DECLARE_METATYPE(QBarSet*)
28 Q_DECLARE_METATYPE(QBarSet*)
29 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
29 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
30
30
31 class tst_QHorizontalBarSeries : public QObject
31 class tst_QHorizontalBarSeries : public QObject
32 {
32 {
33 Q_OBJECT
33 Q_OBJECT
34
34
35 public slots:
35 public slots:
36 void initTestCase();
36 void initTestCase();
37 void cleanupTestCase();
37 void cleanupTestCase();
38 void init();
38 void init();
39 void cleanup();
39 void cleanup();
40
40
41 private slots:
41 private slots:
42 void qhorizontalbarseries_data();
42 void qhorizontalbarseries_data();
43 void qhorizontalbarseries();
43 void qhorizontalbarseries();
44 void type_data();
44 void type_data();
45 void type();
45 void type();
46 void append_data();
46 void append_data();
47 void append();
47 void append();
48 void remove_data();
48 void remove_data();
49 void remove();
49 void remove();
50 void appendList_data();
50 void appendList_data();
51 void appendList();
51 void appendList();
52 void count_data();
52 void count_data();
53 void count();
53 void count();
54 void barSets_data();
54 void barSets_data();
55 void barSets();
55 void barSets();
56 void setLabelsVisible_data();
56 void setLabelsVisible_data();
57 void setLabelsVisible();
57 void setLabelsVisible();
58 void setLabelsFormat();
58 void setLabelsFormat();
59 void setLabelsPosition();
59 void setLabelsPosition();
60 void setLabelsAngle();
60 void mouseclicked_data();
61 void mouseclicked_data();
61 void mouseclicked();
62 void mouseclicked();
62 void mousehovered_data();
63 void mousehovered_data();
63 void mousehovered();
64 void mousehovered();
64 void clearWithAnimations();
65 void clearWithAnimations();
65 void mousePressed();
66 void mousePressed();
66 void mouseReleased();
67 void mouseReleased();
67 void mouseDoubleClicked();
68 void mouseDoubleClicked();
68
69
69 private:
70 private:
70 QHorizontalBarSeries* m_barseries;
71 QHorizontalBarSeries* m_barseries;
71 QHorizontalBarSeries* m_barseries_with_sets;
72 QHorizontalBarSeries* m_barseries_with_sets;
72
73
73 QList<QBarSet*> m_testSets;
74 QList<QBarSet*> m_testSets;
74
75
75 };
76 };
76
77
77 void tst_QHorizontalBarSeries::initTestCase()
78 void tst_QHorizontalBarSeries::initTestCase()
78 {
79 {
79 qRegisterMetaType<QBarSet*>("QBarSet*");
80 qRegisterMetaType<QBarSet*>("QBarSet*");
80 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
81 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
81 }
82 }
82
83
83 void tst_QHorizontalBarSeries::cleanupTestCase()
84 void tst_QHorizontalBarSeries::cleanupTestCase()
84 {
85 {
85 QTest::qWait(1); // Allow final deleteLaters to run
86 QTest::qWait(1); // Allow final deleteLaters to run
86 }
87 }
87
88
88 void tst_QHorizontalBarSeries::init()
89 void tst_QHorizontalBarSeries::init()
89 {
90 {
90 m_barseries = new QHorizontalBarSeries();
91 m_barseries = new QHorizontalBarSeries();
91 m_barseries_with_sets = new QHorizontalBarSeries();
92 m_barseries_with_sets = new QHorizontalBarSeries();
92
93
93 for (int i=0; i<5; i++) {
94 for (int i=0; i<5; i++) {
94 m_testSets.append(new QBarSet("testset"));
95 m_testSets.append(new QBarSet("testset"));
95 m_barseries_with_sets->append(m_testSets.at(i));
96 m_barseries_with_sets->append(m_testSets.at(i));
96 }
97 }
97 }
98 }
98
99
99 void tst_QHorizontalBarSeries::cleanup()
100 void tst_QHorizontalBarSeries::cleanup()
100 {
101 {
101 foreach (QBarSet* s, m_testSets) {
102 foreach (QBarSet* s, m_testSets) {
102 m_barseries_with_sets->remove(s);
103 m_barseries_with_sets->remove(s);
103 }
104 }
104 m_testSets.clear();
105 m_testSets.clear();
105
106
106 delete m_barseries;
107 delete m_barseries;
107 m_barseries = 0;
108 m_barseries = 0;
108 delete m_barseries_with_sets;
109 delete m_barseries_with_sets;
109 m_barseries_with_sets = 0;
110 m_barseries_with_sets = 0;
110 }
111 }
111
112
112 void tst_QHorizontalBarSeries::qhorizontalbarseries_data()
113 void tst_QHorizontalBarSeries::qhorizontalbarseries_data()
113 {
114 {
114 }
115 }
115
116
116 void tst_QHorizontalBarSeries::qhorizontalbarseries()
117 void tst_QHorizontalBarSeries::qhorizontalbarseries()
117 {
118 {
118 QHorizontalBarSeries *barseries = new QHorizontalBarSeries();
119 QHorizontalBarSeries *barseries = new QHorizontalBarSeries();
119 QVERIFY(barseries != 0);
120 QVERIFY(barseries != 0);
120 delete barseries;
121 delete barseries;
121 }
122 }
122
123
123 void tst_QHorizontalBarSeries::type_data()
124 void tst_QHorizontalBarSeries::type_data()
124 {
125 {
125
126
126 }
127 }
127
128
128 void tst_QHorizontalBarSeries::type()
129 void tst_QHorizontalBarSeries::type()
129 {
130 {
130 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeHorizontalBar);
131 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeHorizontalBar);
131 }
132 }
132
133
133 void tst_QHorizontalBarSeries::append_data()
134 void tst_QHorizontalBarSeries::append_data()
134 {
135 {
135 }
136 }
136
137
137 void tst_QHorizontalBarSeries::append()
138 void tst_QHorizontalBarSeries::append()
138 {
139 {
139 QVERIFY(m_barseries->count() == 0);
140 QVERIFY(m_barseries->count() == 0);
140
141
141 bool ret = false;
142 bool ret = false;
142
143
143 // Try adding barset
144 // Try adding barset
144 QBarSet *barset = new QBarSet("testset");
145 QBarSet *barset = new QBarSet("testset");
145 ret = m_barseries->append(barset);
146 ret = m_barseries->append(barset);
146
147
147 QVERIFY(ret == true);
148 QVERIFY(ret == true);
148 QVERIFY(m_barseries->count() == 1);
149 QVERIFY(m_barseries->count() == 1);
149
150
150 // Try adding another set
151 // Try adding another set
151 QBarSet *barset2 = new QBarSet("testset2");
152 QBarSet *barset2 = new QBarSet("testset2");
152 ret = m_barseries->append(barset2);
153 ret = m_barseries->append(barset2);
153
154
154 QVERIFY(ret == true);
155 QVERIFY(ret == true);
155 QVERIFY(m_barseries->count() == 2);
156 QVERIFY(m_barseries->count() == 2);
156
157
157 // Try adding same set again
158 // Try adding same set again
158 ret = m_barseries->append(barset2);
159 ret = m_barseries->append(barset2);
159 QVERIFY(ret == false);
160 QVERIFY(ret == false);
160 QVERIFY(m_barseries->count() == 2);
161 QVERIFY(m_barseries->count() == 2);
161
162
162 // Try adding null set
163 // Try adding null set
163 ret = m_barseries->append(0);
164 ret = m_barseries->append(0);
164 QVERIFY(ret == false);
165 QVERIFY(ret == false);
165 QVERIFY(m_barseries->count() == 2);
166 QVERIFY(m_barseries->count() == 2);
166
167
167 }
168 }
168
169
169 void tst_QHorizontalBarSeries::remove_data()
170 void tst_QHorizontalBarSeries::remove_data()
170 {
171 {
171 }
172 }
172
173
173 void tst_QHorizontalBarSeries::remove()
174 void tst_QHorizontalBarSeries::remove()
174 {
175 {
175 int count = m_testSets.count();
176 int count = m_testSets.count();
176 QVERIFY(m_barseries_with_sets->count() == count);
177 QVERIFY(m_barseries_with_sets->count() == count);
177
178
178 // Try to remove null pointer (should not remove, should not crash)
179 // Try to remove null pointer (should not remove, should not crash)
179 bool ret = false;
180 bool ret = false;
180 ret = m_barseries_with_sets->remove(0);
181 ret = m_barseries_with_sets->remove(0);
181 QVERIFY(ret == false);
182 QVERIFY(ret == false);
182 QVERIFY(m_barseries_with_sets->count() == count);
183 QVERIFY(m_barseries_with_sets->count() == count);
183
184
184 // Try to remove invalid pointer (should not remove, should not crash)
185 // Try to remove invalid pointer (should not remove, should not crash)
185 ret = m_barseries_with_sets->remove((QBarSet*) (m_testSets.at(0) + 1) );
186 ret = m_barseries_with_sets->remove((QBarSet*) (m_testSets.at(0) + 1) );
186 QVERIFY(ret == false);
187 QVERIFY(ret == false);
187 QVERIFY(m_barseries_with_sets->count() == count);
188 QVERIFY(m_barseries_with_sets->count() == count);
188
189
189 // remove some sets
190 // remove some sets
190 ret = m_barseries_with_sets->remove(m_testSets.at(2));
191 ret = m_barseries_with_sets->remove(m_testSets.at(2));
191 QVERIFY(ret == true);
192 QVERIFY(ret == true);
192 ret = m_barseries_with_sets->remove(m_testSets.at(3));
193 ret = m_barseries_with_sets->remove(m_testSets.at(3));
193 QVERIFY(ret == true);
194 QVERIFY(ret == true);
194 ret = m_barseries_with_sets->remove(m_testSets.at(4));
195 ret = m_barseries_with_sets->remove(m_testSets.at(4));
195 QVERIFY(ret == true);
196 QVERIFY(ret == true);
196
197
197 QVERIFY(m_barseries_with_sets->count() == 2);
198 QVERIFY(m_barseries_with_sets->count() == 2);
198
199
199 QList<QBarSet*> verifysets = m_barseries_with_sets->barSets();
200 QList<QBarSet*> verifysets = m_barseries_with_sets->barSets();
200
201
201 QVERIFY(verifysets.at(0) == m_testSets.at(0));
202 QVERIFY(verifysets.at(0) == m_testSets.at(0));
202 QVERIFY(verifysets.at(1) == m_testSets.at(1));
203 QVERIFY(verifysets.at(1) == m_testSets.at(1));
203
204
204 // Try removing all sets again (should be ok, even if some sets have already been removed)
205 // Try removing all sets again (should be ok, even if some sets have already been removed)
205 ret = false;
206 ret = false;
206 for (int i=0; i<count; i++) {
207 for (int i=0; i<count; i++) {
207 ret |= m_barseries_with_sets->remove(m_testSets.at(i));
208 ret |= m_barseries_with_sets->remove(m_testSets.at(i));
208 }
209 }
209
210
210 QVERIFY(ret == true);
211 QVERIFY(ret == true);
211 QVERIFY(m_barseries_with_sets->count() == 0);
212 QVERIFY(m_barseries_with_sets->count() == 0);
212 }
213 }
213
214
214 void tst_QHorizontalBarSeries::appendList_data()
215 void tst_QHorizontalBarSeries::appendList_data()
215 {
216 {
216
217
217 }
218 }
218
219
219 void tst_QHorizontalBarSeries::appendList()
220 void tst_QHorizontalBarSeries::appendList()
220 {
221 {
221 int count = 5;
222 int count = 5;
222 QVERIFY(m_barseries->count() == 0);
223 QVERIFY(m_barseries->count() == 0);
223
224
224 QList<QBarSet*> sets;
225 QList<QBarSet*> sets;
225 for (int i=0; i<count; i++) {
226 for (int i=0; i<count; i++) {
226 sets.append(new QBarSet("testset"));
227 sets.append(new QBarSet("testset"));
227 }
228 }
228
229
229 // Append new sets (should succeed, count should match the count of sets)
230 // Append new sets (should succeed, count should match the count of sets)
230 bool ret = false;
231 bool ret = false;
231 ret = m_barseries->append(sets);
232 ret = m_barseries->append(sets);
232 QVERIFY(ret == true);
233 QVERIFY(ret == true);
233 QVERIFY(m_barseries->count() == count);
234 QVERIFY(m_barseries->count() == count);
234
235
235 // Append same sets again (should fail, count should remain same)
236 // Append same sets again (should fail, count should remain same)
236 ret = m_barseries->append(sets);
237 ret = m_barseries->append(sets);
237 QVERIFY(ret == false);
238 QVERIFY(ret == false);
238 QVERIFY(m_barseries->count() == count);
239 QVERIFY(m_barseries->count() == count);
239
240
240 // Try append empty list (should succeed, but count should remain same)
241 // Try append empty list (should succeed, but count should remain same)
241 QList<QBarSet*> invalidList;
242 QList<QBarSet*> invalidList;
242 ret = m_barseries->append(invalidList);
243 ret = m_barseries->append(invalidList);
243 QVERIFY(ret == true);
244 QVERIFY(ret == true);
244 QVERIFY(m_barseries->count() == count);
245 QVERIFY(m_barseries->count() == count);
245
246
246 // Try append list with one new and one existing set (should fail, count remains same)
247 // Try append list with one new and one existing set (should fail, count remains same)
247 invalidList.append(new QBarSet("ok set"));
248 invalidList.append(new QBarSet("ok set"));
248 invalidList.append(sets.at(0));
249 invalidList.append(sets.at(0));
249 ret = m_barseries->append(invalidList);
250 ret = m_barseries->append(invalidList);
250 QVERIFY(ret == false);
251 QVERIFY(ret == false);
251 QVERIFY(m_barseries->count() == count);
252 QVERIFY(m_barseries->count() == count);
252 delete invalidList.at(0);
253 delete invalidList.at(0);
253
254
254 // Try append list with null pointers (should fail, count remains same)
255 // Try append list with null pointers (should fail, count remains same)
255 QList<QBarSet*> invalidList2;
256 QList<QBarSet*> invalidList2;
256 invalidList2.append(0);
257 invalidList2.append(0);
257 invalidList2.append(0);
258 invalidList2.append(0);
258 invalidList2.append(0);
259 invalidList2.append(0);
259 ret = m_barseries->append(invalidList2);
260 ret = m_barseries->append(invalidList2);
260 QVERIFY(ret == false);
261 QVERIFY(ret == false);
261 QVERIFY(m_barseries->count() == count);
262 QVERIFY(m_barseries->count() == count);
262 }
263 }
263
264
264 void tst_QHorizontalBarSeries::count_data()
265 void tst_QHorizontalBarSeries::count_data()
265 {
266 {
266
267
267 }
268 }
268
269
269 void tst_QHorizontalBarSeries::count()
270 void tst_QHorizontalBarSeries::count()
270 {
271 {
271 QVERIFY(m_barseries->count() == 0);
272 QVERIFY(m_barseries->count() == 0);
272 QVERIFY(m_barseries_with_sets->count() == m_testSets.count());
273 QVERIFY(m_barseries_with_sets->count() == m_testSets.count());
273 }
274 }
274
275
275 void tst_QHorizontalBarSeries::barSets_data()
276 void tst_QHorizontalBarSeries::barSets_data()
276 {
277 {
277
278
278 }
279 }
279
280
280 void tst_QHorizontalBarSeries::barSets()
281 void tst_QHorizontalBarSeries::barSets()
281 {
282 {
282 QVERIFY(m_barseries->barSets().count() == 0);
283 QVERIFY(m_barseries->barSets().count() == 0);
283
284
284 QList<QBarSet*> sets = m_barseries_with_sets->barSets();
285 QList<QBarSet*> sets = m_barseries_with_sets->barSets();
285 QVERIFY(sets.count() == m_testSets.count());
286 QVERIFY(sets.count() == m_testSets.count());
286
287
287 for (int i=0; i<m_testSets.count(); i++) {
288 for (int i=0; i<m_testSets.count(); i++) {
288 QVERIFY(sets.at(i) == m_testSets.at(i));
289 QVERIFY(sets.at(i) == m_testSets.at(i));
289 }
290 }
290 }
291 }
291
292
292 void tst_QHorizontalBarSeries::setLabelsVisible_data()
293 void tst_QHorizontalBarSeries::setLabelsVisible_data()
293 {
294 {
294
295
295 }
296 }
296
297
297 void tst_QHorizontalBarSeries::setLabelsVisible()
298 void tst_QHorizontalBarSeries::setLabelsVisible()
298 {
299 {
299 // labels should be invisible by default
300 // labels should be invisible by default
300 QVERIFY(m_barseries->isLabelsVisible() == false);
301 QVERIFY(m_barseries->isLabelsVisible() == false);
301 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
302 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
302
303
303 // turn labels to visible
304 // turn labels to visible
304 m_barseries_with_sets->setLabelsVisible(true);
305 m_barseries_with_sets->setLabelsVisible(true);
305 // TODO: test the signal
306 // TODO: test the signal
306 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
307 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
307
308
308 // turn labels to invisible
309 // turn labels to invisible
309 m_barseries_with_sets->setLabelsVisible(false);
310 m_barseries_with_sets->setLabelsVisible(false);
310 // TODO: test the signal
311 // TODO: test the signal
311 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
312 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
312
313
313 // without parameter, should turn labels to visible
314 // without parameter, should turn labels to visible
314 m_barseries_with_sets->setLabelsVisible();
315 m_barseries_with_sets->setLabelsVisible();
315 // TODO: test the signal
316 // TODO: test the signal
316 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
317 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
317 }
318 }
318
319
319 void tst_QHorizontalBarSeries::setLabelsFormat()
320 void tst_QHorizontalBarSeries::setLabelsFormat()
320 {
321 {
321 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
322 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
322 QCOMPARE(m_barseries->labelsFormat(), QString());
323 QCOMPARE(m_barseries->labelsFormat(), QString());
323
324
324 QString format("(@value)");
325 QString format("(@value)");
325 m_barseries->setLabelsFormat(format);
326 m_barseries->setLabelsFormat(format);
326 TRY_COMPARE(labelsFormatSpy.count(), 1);
327 TRY_COMPARE(labelsFormatSpy.count(), 1);
327 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
328 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
328 QVERIFY(arguments.at(0).toString() == format);
329 QVERIFY(arguments.at(0).toString() == format);
329 QCOMPARE(m_barseries->labelsFormat(), format);
330 QCOMPARE(m_barseries->labelsFormat(), format);
330
331
331 m_barseries->setLabelsFormat(QString());
332 m_barseries->setLabelsFormat(QString());
332 TRY_COMPARE(labelsFormatSpy.count(), 1);
333 TRY_COMPARE(labelsFormatSpy.count(), 1);
333 arguments = labelsFormatSpy.takeFirst();
334 arguments = labelsFormatSpy.takeFirst();
334 QVERIFY(arguments.at(0).toString() == QString());
335 QVERIFY(arguments.at(0).toString() == QString());
335 QCOMPARE(m_barseries->labelsFormat(), QString());
336 QCOMPARE(m_barseries->labelsFormat(), QString());
336 }
337 }
337
338
338 void tst_QHorizontalBarSeries::setLabelsPosition()
339 void tst_QHorizontalBarSeries::setLabelsPosition()
339 {
340 {
340 QSignalSpy labelsPositionSpy(m_barseries,
341 QSignalSpy labelsPositionSpy(m_barseries,
341 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
342 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
342 QCOMPARE(m_barseries->labelsPosition(), QHorizontalBarSeries::LabelsCenter);
343 QCOMPARE(m_barseries->labelsPosition(), QHorizontalBarSeries::LabelsCenter);
343
344
344 m_barseries->setLabelsPosition(QHorizontalBarSeries::LabelsInsideEnd);
345 m_barseries->setLabelsPosition(QHorizontalBarSeries::LabelsInsideEnd);
345 TRY_COMPARE(labelsPositionSpy.count(), 1);
346 TRY_COMPARE(labelsPositionSpy.count(), 1);
346 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
347 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
347 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
348 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
348 == QHorizontalBarSeries::LabelsInsideEnd);
349 == QHorizontalBarSeries::LabelsInsideEnd);
349 QCOMPARE(m_barseries->labelsPosition(), QHorizontalBarSeries::LabelsInsideEnd);
350 QCOMPARE(m_barseries->labelsPosition(), QHorizontalBarSeries::LabelsInsideEnd);
350
351
351 m_barseries->setLabelsPosition(QHorizontalBarSeries::LabelsInsideBase);
352 m_barseries->setLabelsPosition(QHorizontalBarSeries::LabelsInsideBase);
352 TRY_COMPARE(labelsPositionSpy.count(), 1);
353 TRY_COMPARE(labelsPositionSpy.count(), 1);
353 arguments = labelsPositionSpy.takeFirst();
354 arguments = labelsPositionSpy.takeFirst();
354 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
355 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
355 == QHorizontalBarSeries::LabelsInsideBase);
356 == QHorizontalBarSeries::LabelsInsideBase);
356 QCOMPARE(m_barseries->labelsPosition(), QHorizontalBarSeries::LabelsInsideBase);
357 QCOMPARE(m_barseries->labelsPosition(), QHorizontalBarSeries::LabelsInsideBase);
357
358
358 m_barseries->setLabelsPosition(QHorizontalBarSeries::LabelsOutsideEnd);
359 m_barseries->setLabelsPosition(QHorizontalBarSeries::LabelsOutsideEnd);
359 TRY_COMPARE(labelsPositionSpy.count(), 1);
360 TRY_COMPARE(labelsPositionSpy.count(), 1);
360 arguments = labelsPositionSpy.takeFirst();
361 arguments = labelsPositionSpy.takeFirst();
361 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
362 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
362 == QHorizontalBarSeries::LabelsOutsideEnd);
363 == QHorizontalBarSeries::LabelsOutsideEnd);
363 QCOMPARE(m_barseries->labelsPosition(), QHorizontalBarSeries::LabelsOutsideEnd);
364 QCOMPARE(m_barseries->labelsPosition(), QHorizontalBarSeries::LabelsOutsideEnd);
364
365
365 m_barseries->setLabelsPosition(QHorizontalBarSeries::LabelsCenter);
366 m_barseries->setLabelsPosition(QHorizontalBarSeries::LabelsCenter);
366 TRY_COMPARE(labelsPositionSpy.count(), 1);
367 TRY_COMPARE(labelsPositionSpy.count(), 1);
367 arguments = labelsPositionSpy.takeFirst();
368 arguments = labelsPositionSpy.takeFirst();
368 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
369 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
369 == QHorizontalBarSeries::LabelsCenter);
370 == QHorizontalBarSeries::LabelsCenter);
370 QCOMPARE(m_barseries->labelsPosition(), QHorizontalBarSeries::LabelsCenter);
371 QCOMPARE(m_barseries->labelsPosition(), QHorizontalBarSeries::LabelsCenter);
371 }
372 }
372
373
374 void tst_QHorizontalBarSeries::setLabelsAngle()
375 {
376 QSignalSpy labelsAngleSpy(m_barseries,
377 SIGNAL(labelsAngleChanged(qreal)));
378 QCOMPARE(m_barseries->labelsAngle(), 0.0);
379
380 m_barseries->setLabelsAngle(55.0);
381 TRY_COMPARE(labelsAngleSpy.count(), 1);
382 QList<QVariant> arguments = labelsAngleSpy.takeFirst();
383 QVERIFY(arguments.at(0).value<qreal>() == 55.0);
384 QCOMPARE(m_barseries->labelsAngle(), 55.0);
385 }
386
373 void tst_QHorizontalBarSeries::mouseclicked_data()
387 void tst_QHorizontalBarSeries::mouseclicked_data()
374 {
388 {
375
389
376 }
390 }
377
391
378 void tst_QHorizontalBarSeries::mouseclicked()
392 void tst_QHorizontalBarSeries::mouseclicked()
379 {
393 {
380 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
394 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
381
395
382 QHorizontalBarSeries* series = new QHorizontalBarSeries();
396 QHorizontalBarSeries* series = new QHorizontalBarSeries();
383
397
384 QBarSet* set1 = new QBarSet(QString("set 1"));
398 QBarSet* set1 = new QBarSet(QString("set 1"));
385 *set1 << 10 << 10 << 10;
399 *set1 << 10 << 10 << 10;
386 series->append(set1);
400 series->append(set1);
387
401
388 QBarSet* set2 = new QBarSet(QString("set 2"));
402 QBarSet* set2 = new QBarSet(QString("set 2"));
389 *set2 << 10 << 10 << 10;
403 *set2 << 10 << 10 << 10;
390 series->append(set2);
404 series->append(set2);
391
405
392 QList<QBarSet*> barSets = series->barSets();
406 QList<QBarSet*> barSets = series->barSets();
393
407
394 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
408 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
395 QSignalSpy setSpy1(set1, SIGNAL(clicked(int)));
409 QSignalSpy setSpy1(set1, SIGNAL(clicked(int)));
396 QSignalSpy setSpy2(set2, SIGNAL(clicked(int)));
410 QSignalSpy setSpy2(set2, SIGNAL(clicked(int)));
397
411
398 QChartView view(new QChart());
412 QChartView view(new QChart());
399 view.resize(400,300);
413 view.resize(400,300);
400 view.chart()->addSeries(series);
414 view.chart()->addSeries(series);
401 view.show();
415 view.show();
402 QTest::qWaitForWindowShown(&view);
416 QTest::qWaitForWindowShown(&view);
403
417
404 // Calculate expected layout for bars
418 // Calculate expected layout for bars
405 QRectF plotArea = view.chart()->plotArea();
419 QRectF plotArea = view.chart()->plotArea();
406 qreal width = plotArea.width();
420 qreal width = plotArea.width();
407 qreal height = plotArea.height();
421 qreal height = plotArea.height();
408 qreal rangeX = 10; // From 0 to 10 because of maximum value in set is 10
422 qreal rangeX = 10; // From 0 to 10 because of maximum value in set is 10
409 qreal rangeY = 3; // 3 values per set
423 qreal rangeY = 3; // 3 values per set
410 qreal scaleY = (height / rangeY);
424 qreal scaleY = (height / rangeY);
411 qreal scaleX = (width / rangeX);
425 qreal scaleX = (width / rangeX);
412
426
413 qreal setCount = series->count();
427 qreal setCount = series->count();
414 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
428 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
415 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
429 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
416 qreal rectHeight = (scaleY / setCount) * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
430 qreal rectHeight = (scaleY / setCount) * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
417
431
418 QVector<QRectF> layout;
432 QVector<QRectF> layout;
419
433
420 // 3 = count of values in set
434 // 3 = count of values in set
421 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
435 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
422 for (int i = 0; i < 3; i++) {
436 for (int i = 0; i < 3; i++) {
423 qreal xPos = -scaleX * domainMinX + plotArea.left();
437 qreal xPos = -scaleX * domainMinX + plotArea.left();
424 for (int set = 0; set < setCount; set++) {
438 for (int set = 0; set < setCount; set++) {
425 qreal yPos = plotArea.bottom() + (domainMinY - i) * scaleY;
439 qreal yPos = plotArea.bottom() + (domainMinY - i) * scaleY;
426 yPos += setCount*rectHeight/2;
440 yPos += setCount*rectHeight/2;
427 yPos -= set*rectHeight;
441 yPos -= set*rectHeight;
428
442
429 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
443 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
430 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
444 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
431 layout.append(rect);
445 layout.append(rect);
432 }
446 }
433 }
447 }
434
448
435 //====================================================================================
449 //====================================================================================
436 // barset 1, bar 0
450 // barset 1, bar 0
437 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
451 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
438 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
452 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
439
453
440 QCOMPARE(seriesSpy.count(), 1);
454 QCOMPARE(seriesSpy.count(), 1);
441 QCOMPARE(setSpy1.count(), 1);
455 QCOMPARE(setSpy1.count(), 1);
442 QCOMPARE(setSpy2.count(), 0);
456 QCOMPARE(setSpy2.count(), 0);
443
457
444 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
458 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
445 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
459 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
446 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
460 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
447 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
461 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
448
462
449 QList<QVariant> setSpyArg = setSpy1.takeFirst();
463 QList<QVariant> setSpyArg = setSpy1.takeFirst();
450 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
464 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
451 QVERIFY(setSpyArg.at(0).toInt() == 0);
465 QVERIFY(setSpyArg.at(0).toInt() == 0);
452
466
453 //====================================================================================
467 //====================================================================================
454 // barset 1, bar 1
468 // barset 1, bar 1
455 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
469 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
456 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
470 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
457
471
458 QCOMPARE(seriesSpy.count(), 1);
472 QCOMPARE(seriesSpy.count(), 1);
459 QCOMPARE(setSpy1.count(), 1);
473 QCOMPARE(setSpy1.count(), 1);
460 QCOMPARE(setSpy2.count(), 0);
474 QCOMPARE(setSpy2.count(), 0);
461
475
462 seriesSpyArg = seriesSpy.takeFirst();
476 seriesSpyArg = seriesSpy.takeFirst();
463 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
477 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
464 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
478 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
465 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
479 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
466
480
467 setSpyArg = setSpy1.takeFirst();
481 setSpyArg = setSpy1.takeFirst();
468 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
482 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
469 QVERIFY(setSpyArg.at(0).toInt() == 1);
483 QVERIFY(setSpyArg.at(0).toInt() == 1);
470
484
471 //====================================================================================
485 //====================================================================================
472 // barset 1, bar 2
486 // barset 1, bar 2
473 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
487 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
474 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
488 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
475
489
476 QCOMPARE(seriesSpy.count(), 1);
490 QCOMPARE(seriesSpy.count(), 1);
477 QCOMPARE(setSpy1.count(), 1);
491 QCOMPARE(setSpy1.count(), 1);
478 QCOMPARE(setSpy2.count(), 0);
492 QCOMPARE(setSpy2.count(), 0);
479
493
480 seriesSpyArg = seriesSpy.takeFirst();
494 seriesSpyArg = seriesSpy.takeFirst();
481 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
495 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
482 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
496 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
483 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
497 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
484
498
485 setSpyArg = setSpy1.takeFirst();
499 setSpyArg = setSpy1.takeFirst();
486 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
500 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
487 QVERIFY(setSpyArg.at(0).toInt() == 2);
501 QVERIFY(setSpyArg.at(0).toInt() == 2);
488
502
489 //====================================================================================
503 //====================================================================================
490 // barset 2, bar 0
504 // barset 2, bar 0
491 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
505 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
492 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
506 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
493
507
494 QCOMPARE(seriesSpy.count(), 1);
508 QCOMPARE(seriesSpy.count(), 1);
495 QCOMPARE(setSpy1.count(), 0);
509 QCOMPARE(setSpy1.count(), 0);
496 QCOMPARE(setSpy2.count(), 1);
510 QCOMPARE(setSpy2.count(), 1);
497
511
498 seriesSpyArg = seriesSpy.takeFirst();
512 seriesSpyArg = seriesSpy.takeFirst();
499 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
513 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
500 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
514 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
501 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
515 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
502
516
503 setSpyArg = setSpy2.takeFirst();
517 setSpyArg = setSpy2.takeFirst();
504 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
518 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
505 QVERIFY(setSpyArg.at(0).toInt() == 0);
519 QVERIFY(setSpyArg.at(0).toInt() == 0);
506
520
507 //====================================================================================
521 //====================================================================================
508 // barset 2, bar 1
522 // barset 2, bar 1
509 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
523 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
510 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
524 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
511
525
512 QCOMPARE(seriesSpy.count(), 1);
526 QCOMPARE(seriesSpy.count(), 1);
513 QCOMPARE(setSpy1.count(), 0);
527 QCOMPARE(setSpy1.count(), 0);
514 QCOMPARE(setSpy2.count(), 1);
528 QCOMPARE(setSpy2.count(), 1);
515
529
516 seriesSpyArg = seriesSpy.takeFirst();
530 seriesSpyArg = seriesSpy.takeFirst();
517 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
531 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
518 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
532 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
519 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
533 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
520
534
521 setSpyArg = setSpy2.takeFirst();
535 setSpyArg = setSpy2.takeFirst();
522 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
536 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
523 QVERIFY(setSpyArg.at(0).toInt() == 1);
537 QVERIFY(setSpyArg.at(0).toInt() == 1);
524
538
525 //====================================================================================
539 //====================================================================================
526 // barset 2, bar 2
540 // barset 2, bar 2
527 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
541 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
528 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
542 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
529
543
530 QCOMPARE(seriesSpy.count(), 1);
544 QCOMPARE(seriesSpy.count(), 1);
531 QCOMPARE(setSpy1.count(), 0);
545 QCOMPARE(setSpy1.count(), 0);
532 QCOMPARE(setSpy2.count(), 1);
546 QCOMPARE(setSpy2.count(), 1);
533
547
534 seriesSpyArg = seriesSpy.takeFirst();
548 seriesSpyArg = seriesSpy.takeFirst();
535 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
549 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
536 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
550 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
537 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
551 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
538
552
539 setSpyArg = setSpy2.takeFirst();
553 setSpyArg = setSpy2.takeFirst();
540 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
554 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
541 QVERIFY(setSpyArg.at(0).toInt() == 2);
555 QVERIFY(setSpyArg.at(0).toInt() == 2);
542 }
556 }
543
557
544 void tst_QHorizontalBarSeries::mousehovered_data()
558 void tst_QHorizontalBarSeries::mousehovered_data()
545 {
559 {
546
560
547 }
561 }
548
562
549 void tst_QHorizontalBarSeries::mousehovered()
563 void tst_QHorizontalBarSeries::mousehovered()
550 {
564 {
551 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
565 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
552
566
553 QHorizontalBarSeries* series = new QHorizontalBarSeries();
567 QHorizontalBarSeries* series = new QHorizontalBarSeries();
554
568
555 QBarSet* set1 = new QBarSet(QString("set 1"));
569 QBarSet* set1 = new QBarSet(QString("set 1"));
556 *set1 << 10 << 10 << 10;
570 *set1 << 10 << 10 << 10;
557 series->append(set1);
571 series->append(set1);
558
572
559 QBarSet* set2 = new QBarSet(QString("set 2"));
573 QBarSet* set2 = new QBarSet(QString("set 2"));
560 *set2 << 10 << 10 << 10;
574 *set2 << 10 << 10 << 10;
561 series->append(set2);
575 series->append(set2);
562
576
563 QList<QBarSet*> barSets = series->barSets();
577 QList<QBarSet*> barSets = series->barSets();
564
578
565 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
579 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
566 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
580 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
567 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
581 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
568
582
569 QChartView view(new QChart());
583 QChartView view(new QChart());
570 view.resize(400,300);
584 view.resize(400,300);
571 view.chart()->addSeries(series);
585 view.chart()->addSeries(series);
572 view.show();
586 view.show();
573 QTest::qWaitForWindowShown(&view);
587 QTest::qWaitForWindowShown(&view);
574
588
575 //this is hack since view does not get events otherwise
589 //this is hack since view does not get events otherwise
576 view.setMouseTracking(true);
590 view.setMouseTracking(true);
577
591
578 // Calculate expected layout for bars
592 // Calculate expected layout for bars
579 QRectF plotArea = view.chart()->plotArea();
593 QRectF plotArea = view.chart()->plotArea();
580 qreal width = plotArea.width();
594 qreal width = plotArea.width();
581 qreal height = plotArea.height();
595 qreal height = plotArea.height();
582 qreal rangeX = 10; // From 0 to 10 because of maximum value in set is 10
596 qreal rangeX = 10; // From 0 to 10 because of maximum value in set is 10
583 qreal rangeY = 3; // 3 values per set
597 qreal rangeY = 3; // 3 values per set
584 qreal scaleY = (height / rangeY);
598 qreal scaleY = (height / rangeY);
585 qreal scaleX = (width / rangeX);
599 qreal scaleX = (width / rangeX);
586
600
587 qreal setCount = series->count();
601 qreal setCount = series->count();
588 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
602 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
589 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
603 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
590 qreal rectHeight = (scaleY / setCount) * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
604 qreal rectHeight = (scaleY / setCount) * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
591
605
592 QVector<QRectF> layout;
606 QVector<QRectF> layout;
593
607
594 // 3 = count of values in set
608 // 3 = count of values in set
595 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
609 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
596 for (int i = 0; i < 3; i++) {
610 for (int i = 0; i < 3; i++) {
597 qreal xPos = -scaleX * domainMinX + plotArea.left();
611 qreal xPos = -scaleX * domainMinX + plotArea.left();
598 for (int set = 0; set < setCount; set++) {
612 for (int set = 0; set < setCount; set++) {
599 qreal yPos = plotArea.bottom() + (domainMinY - i) * scaleY;
613 qreal yPos = plotArea.bottom() + (domainMinY - i) * scaleY;
600 yPos += setCount*rectHeight/2;
614 yPos += setCount*rectHeight/2;
601 yPos -= set*rectHeight;
615 yPos -= set*rectHeight;
602
616
603 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
617 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
604 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
618 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
605 layout.append(rect);
619 layout.append(rect);
606 }
620 }
607 }
621 }
608
622
609 //=======================================================================
623 //=======================================================================
610 // move mouse to bottom border
624 // move mouse to bottom border
611 QTest::mouseMove(view.viewport(), QPoint(layout.at(0).center().x(), 300));
625 QTest::mouseMove(view.viewport(), QPoint(layout.at(0).center().x(), 300));
612 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
626 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
613 TRY_COMPARE(seriesIndexSpy.count(), 0);
627 TRY_COMPARE(seriesIndexSpy.count(), 0);
614 TRY_COMPARE(setIndexSpy1.count(), 0);
628 TRY_COMPARE(setIndexSpy1.count(), 0);
615 TRY_COMPARE(setIndexSpy2.count(), 0);
629 TRY_COMPARE(setIndexSpy2.count(), 0);
616
630
617 //=======================================================================
631 //=======================================================================
618 // move mouse on top of set1
632 // move mouse on top of set1
619 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
633 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
620 TRY_COMPARE(seriesIndexSpy.count(), 1);
634 TRY_COMPARE(seriesIndexSpy.count(), 1);
621 TRY_COMPARE(setIndexSpy1.count(), 1);
635 TRY_COMPARE(setIndexSpy1.count(), 1);
622 TRY_COMPARE(setIndexSpy2.count(), 0);
636 TRY_COMPARE(setIndexSpy2.count(), 0);
623
637
624 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
638 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
625 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
639 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
626 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
640 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
627 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
641 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
628
642
629 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
643 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
630 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
644 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
631 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
645 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
632
646
633 //=======================================================================
647 //=======================================================================
634 // move mouse from top of set1 to top of set2
648 // move mouse from top of set1 to top of set2
635 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
649 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
636 TRY_COMPARE(seriesIndexSpy.count(), 2);
650 TRY_COMPARE(seriesIndexSpy.count(), 2);
637 TRY_COMPARE(setIndexSpy1.count(), 1);
651 TRY_COMPARE(setIndexSpy1.count(), 1);
638 TRY_COMPARE(setIndexSpy2.count(), 1);
652 TRY_COMPARE(setIndexSpy2.count(), 1);
639
653
640 // should leave set1
654 // should leave set1
641 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
655 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
642 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
656 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
643 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
657 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
644 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
658 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
645
659
646 setIndexSpyArg = setIndexSpy1.takeFirst();
660 setIndexSpyArg = setIndexSpy1.takeFirst();
647 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
661 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
648 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
662 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
649
663
650 // should enter set2
664 // should enter set2
651 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
665 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
652 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
666 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
653 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
667 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
654 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
668 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
655
669
656 setIndexSpyArg = setIndexSpy2.takeFirst();
670 setIndexSpyArg = setIndexSpy2.takeFirst();
657 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
671 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
658 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
672 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
659
673
660 //=======================================================================
674 //=======================================================================
661 // move mouse from top of set2 to background
675 // move mouse from top of set2 to background
662 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(0).center().y()));
676 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(0).center().y()));
663 TRY_COMPARE(seriesIndexSpy.count(), 1);
677 TRY_COMPARE(seriesIndexSpy.count(), 1);
664 TRY_COMPARE(setIndexSpy1.count(), 0);
678 TRY_COMPARE(setIndexSpy1.count(), 0);
665 TRY_COMPARE(setIndexSpy2.count(), 1);
679 TRY_COMPARE(setIndexSpy2.count(), 1);
666
680
667 // should leave set2
681 // should leave set2
668 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
682 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
669 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
683 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
670 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
684 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
671 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
685 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
672
686
673 setIndexSpyArg = setIndexSpy2.takeFirst();
687 setIndexSpyArg = setIndexSpy2.takeFirst();
674 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
688 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
675 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
689 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
676
690
677 //=======================================================================
691 //=======================================================================
678 // move mouse on top of set1, bar0 to check the index (hover into set1)
692 // move mouse on top of set1, bar0 to check the index (hover into set1)
679 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
693 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
680
694
681 TRY_COMPARE(seriesIndexSpy.count(), 1);
695 TRY_COMPARE(seriesIndexSpy.count(), 1);
682 TRY_COMPARE(setIndexSpy1.count(), 1);
696 TRY_COMPARE(setIndexSpy1.count(), 1);
683 TRY_COMPARE(setIndexSpy2.count(), 0);
697 TRY_COMPARE(setIndexSpy2.count(), 0);
684
698
685 //should enter set1, bar0
699 //should enter set1, bar0
686 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
700 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
687 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
701 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
688 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
702 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
689 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
703 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
690 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
704 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
691 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
705 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
692
706
693 setIndexSpyArg = setIndexSpy1.takeFirst();
707 setIndexSpyArg = setIndexSpy1.takeFirst();
694 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
708 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
695 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
709 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
696 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
710 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
697 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
711 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
698
712
699 //=======================================================================
713 //=======================================================================
700 // move mouse on top of set2, bar0 to check the index (hover out set1,
714 // move mouse on top of set2, bar0 to check the index (hover out set1,
701 // hover in set1)
715 // hover in set1)
702 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
716 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
703
717
704 TRY_COMPARE(seriesIndexSpy.count(), 2);
718 TRY_COMPARE(seriesIndexSpy.count(), 2);
705 TRY_COMPARE(setIndexSpy1.count(), 1);
719 TRY_COMPARE(setIndexSpy1.count(), 1);
706 TRY_COMPARE(setIndexSpy2.count(), 1);
720 TRY_COMPARE(setIndexSpy2.count(), 1);
707
721
708 // should leave set1, bar0
722 // should leave set1, bar0
709 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
723 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
710 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
724 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
711 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
725 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
712 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
726 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
713 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
727 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
714 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
728 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
715
729
716 setIndexSpyArg = setIndexSpy1.takeFirst();
730 setIndexSpyArg = setIndexSpy1.takeFirst();
717 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
731 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
718 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
732 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
719 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
733 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
720 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
734 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
721
735
722 // should enter set2, bar0
736 // should enter set2, bar0
723 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
737 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
724 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
738 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
725 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
739 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
726 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
740 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
727 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
741 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
728 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
742 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
729
743
730 setIndexSpyArg = setIndexSpy2.takeFirst();
744 setIndexSpyArg = setIndexSpy2.takeFirst();
731 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
745 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
732 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
746 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
733 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
747 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
734 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
748 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
735
749
736 //=======================================================================
750 //=======================================================================
737 // move mouse on top of set1, bar1 to check the index (hover out set 2,
751 // move mouse on top of set1, bar1 to check the index (hover out set 2,
738 // hover in set1)
752 // hover in set1)
739 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
753 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
740
754
741 TRY_COMPARE(seriesIndexSpy.count(), 2);
755 TRY_COMPARE(seriesIndexSpy.count(), 2);
742 TRY_COMPARE(setIndexSpy1.count(), 1);
756 TRY_COMPARE(setIndexSpy1.count(), 1);
743 TRY_COMPARE(setIndexSpy2.count(), 1);
757 TRY_COMPARE(setIndexSpy2.count(), 1);
744
758
745 // should leave set2, bar0
759 // should leave set2, bar0
746 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
760 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
747 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
761 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
748 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
762 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
749 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
763 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
750 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
764 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
751 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
765 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
752
766
753 setIndexSpyArg = setIndexSpy2.takeFirst();
767 setIndexSpyArg = setIndexSpy2.takeFirst();
754 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
768 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
755 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
769 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
756 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
770 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
757 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
771 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
758
772
759 // should enter set1, bar1
773 // should enter set1, bar1
760 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
774 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
761 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
775 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
762 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
776 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
763 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
777 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
764 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
778 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
765 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
779 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
766
780
767 setIndexSpyArg = setIndexSpy1.takeFirst();
781 setIndexSpyArg = setIndexSpy1.takeFirst();
768 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
782 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
769 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
783 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
770 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
784 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
771 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
785 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
772
786
773 //=======================================================================
787 //=======================================================================
774 // move mouse on top of view between set1 and set2 to check the index
788 // move mouse on top of view between set1 and set2 to check the index
775 // (hover out set1)
789 // (hover out set1)
776 QTest::mouseMove(view.viewport(), QPoint(0, (layout.at(2).top() + layout.at(3).bottom()) / 2));
790 QTest::mouseMove(view.viewport(), QPoint(0, (layout.at(2).top() + layout.at(3).bottom()) / 2));
777
791
778 TRY_COMPARE(seriesIndexSpy.count(), 1);
792 TRY_COMPARE(seriesIndexSpy.count(), 1);
779 TRY_COMPARE(setIndexSpy1.count(), 1);
793 TRY_COMPARE(setIndexSpy1.count(), 1);
780 TRY_COMPARE(setIndexSpy2.count(), 0);
794 TRY_COMPARE(setIndexSpy2.count(), 0);
781
795
782 // should leave set1, bar1
796 // should leave set1, bar1
783 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
797 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
784 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
798 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
785 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
799 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
786 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
800 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
787 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
801 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
788 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
802 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
789
803
790 setIndexSpyArg = setIndexSpy1.takeFirst();
804 setIndexSpyArg = setIndexSpy1.takeFirst();
791 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
805 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
792 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
806 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
793 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
807 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
794 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
808 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
795
809
796 //=======================================================================
810 //=======================================================================
797 // move mouse on top of set2, bar1 to check the index (hover in set2)
811 // move mouse on top of set2, bar1 to check the index (hover in set2)
798 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
812 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
799
813
800 TRY_COMPARE(seriesIndexSpy.count(), 1);
814 TRY_COMPARE(seriesIndexSpy.count(), 1);
801 TRY_COMPARE(setIndexSpy1.count(), 0);
815 TRY_COMPARE(setIndexSpy1.count(), 0);
802 TRY_COMPARE(setIndexSpy2.count(), 1);
816 TRY_COMPARE(setIndexSpy2.count(), 1);
803
817
804 // should enter set2, bar1
818 // should enter set2, bar1
805 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
819 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
806 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
820 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
807 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
821 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
808 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
822 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
809 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
823 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
810 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
824 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
811
825
812 setIndexSpyArg = setIndexSpy2.takeFirst();
826 setIndexSpyArg = setIndexSpy2.takeFirst();
813 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
827 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
814 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
828 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
815 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
829 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
816 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
830 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
817
831
818 //=======================================================================
832 //=======================================================================
819 // move mouse on top of the view between bar1 and bar2 to check the index
833 // move mouse on top of the view between bar1 and bar2 to check the index
820 //(hover out set2)
834 //(hover out set2)
821 QTest::mouseMove(view.viewport(), QPoint(0, (layout.at(3).top() + layout.at(3).bottom()) / 2));
835 QTest::mouseMove(view.viewport(), QPoint(0, (layout.at(3).top() + layout.at(3).bottom()) / 2));
822
836
823 TRY_COMPARE(seriesIndexSpy.count(), 1);
837 TRY_COMPARE(seriesIndexSpy.count(), 1);
824 TRY_COMPARE(setIndexSpy1.count(), 0);
838 TRY_COMPARE(setIndexSpy1.count(), 0);
825 TRY_COMPARE(setIndexSpy2.count(), 1);
839 TRY_COMPARE(setIndexSpy2.count(), 1);
826
840
827 // should leave set2, bar1
841 // should leave set2, bar1
828 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
842 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
829 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
843 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
830 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
844 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
831 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
845 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
832 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
846 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
833 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
847 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
834
848
835 setIndexSpyArg = setIndexSpy2.takeFirst();
849 setIndexSpyArg = setIndexSpy2.takeFirst();
836 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
850 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
837 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
851 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
838 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
852 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
839 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
853 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
840 }
854 }
841
855
842 void tst_QHorizontalBarSeries::clearWithAnimations()
856 void tst_QHorizontalBarSeries::clearWithAnimations()
843 {
857 {
844 QHorizontalBarSeries* series = new QHorizontalBarSeries();
858 QHorizontalBarSeries* series = new QHorizontalBarSeries();
845
859
846 QBarSet* set1 = new QBarSet(QString("set 1"));
860 QBarSet* set1 = new QBarSet(QString("set 1"));
847 *set1 << 10 << 10 << 10;
861 *set1 << 10 << 10 << 10;
848 series->append(set1);
862 series->append(set1);
849
863
850 QBarSet* set2 = new QBarSet(QString("set 2"));
864 QBarSet* set2 = new QBarSet(QString("set 2"));
851 *set2 << 10 << 10 << 10;
865 *set2 << 10 << 10 << 10;
852 series->append(set2);
866 series->append(set2);
853
867
854 QChartView view(new QChart());
868 QChartView view(new QChart());
855 view.resize(400,300);
869 view.resize(400,300);
856 view.chart()->setAnimationOptions(QChart::SeriesAnimations);
870 view.chart()->setAnimationOptions(QChart::SeriesAnimations);
857 view.chart()->addSeries(series);
871 view.chart()->addSeries(series);
858 view.show();
872 view.show();
859
873
860 series->clear();
874 series->clear();
861 }
875 }
862
876
863 void tst_QHorizontalBarSeries::mousePressed()
877 void tst_QHorizontalBarSeries::mousePressed()
864 {
878 {
865 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
879 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
866
880
867 QHorizontalBarSeries* series = new QHorizontalBarSeries();
881 QHorizontalBarSeries* series = new QHorizontalBarSeries();
868
882
869 QBarSet* set1 = new QBarSet(QString("set 1"));
883 QBarSet* set1 = new QBarSet(QString("set 1"));
870 *set1 << 10 << 10 << 10;
884 *set1 << 10 << 10 << 10;
871 series->append(set1);
885 series->append(set1);
872
886
873 QBarSet* set2 = new QBarSet(QString("set 2"));
887 QBarSet* set2 = new QBarSet(QString("set 2"));
874 *set2 << 10 << 10 << 10;
888 *set2 << 10 << 10 << 10;
875 series->append(set2);
889 series->append(set2);
876 QList<QBarSet*> barSets = series->barSets();
890 QList<QBarSet*> barSets = series->barSets();
877
891
878 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
892 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
879 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
893 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
880 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
894 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
881
895
882 QChartView view(new QChart());
896 QChartView view(new QChart());
883 view.resize(400,300);
897 view.resize(400,300);
884 view.chart()->addSeries(series);
898 view.chart()->addSeries(series);
885 view.show();
899 view.show();
886 QTest::qWaitForWindowShown(&view);
900 QTest::qWaitForWindowShown(&view);
887
901
888 // Calculate expected layout for bars
902 // Calculate expected layout for bars
889 QRectF plotArea = view.chart()->plotArea();
903 QRectF plotArea = view.chart()->plotArea();
890 qreal width = plotArea.width();
904 qreal width = plotArea.width();
891 qreal height = plotArea.height();
905 qreal height = plotArea.height();
892 qreal rangeX = 10; // From 0 to 10 because of maximum value in set is 10
906 qreal rangeX = 10; // From 0 to 10 because of maximum value in set is 10
893 qreal rangeY = 3; // 3 values per set
907 qreal rangeY = 3; // 3 values per set
894 qreal scaleY = (height / rangeY);
908 qreal scaleY = (height / rangeY);
895 qreal scaleX = (width / rangeX);
909 qreal scaleX = (width / rangeX);
896
910
897 qreal setCount = series->count();
911 qreal setCount = series->count();
898 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
912 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
899 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
913 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
900 qreal rectHeight = (scaleY / setCount) * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
914 qreal rectHeight = (scaleY / setCount) * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
901
915
902 QVector<QRectF> layout;
916 QVector<QRectF> layout;
903
917
904 // 3 = count of values in set
918 // 3 = count of values in set
905 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
919 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
906 for (int i = 0; i < 3; i++) {
920 for (int i = 0; i < 3; i++) {
907 qreal xPos = -scaleX * domainMinX + plotArea.left();
921 qreal xPos = -scaleX * domainMinX + plotArea.left();
908 for (int set = 0; set < setCount; set++) {
922 for (int set = 0; set < setCount; set++) {
909 qreal yPos = plotArea.bottom() + (domainMinY - i) * scaleY;
923 qreal yPos = plotArea.bottom() + (domainMinY - i) * scaleY;
910 yPos += setCount*rectHeight/2;
924 yPos += setCount*rectHeight/2;
911 yPos -= set*rectHeight;
925 yPos -= set*rectHeight;
912
926
913 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
927 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
914 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
928 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
915 layout.append(rect);
929 layout.append(rect);
916 }
930 }
917 }
931 }
918
932
919 //====================================================================================
933 //====================================================================================
920 // barset 1, bar 0
934 // barset 1, bar 0
921 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
935 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
922 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
936 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
923
937
924 QCOMPARE(seriesSpy.count(), 1);
938 QCOMPARE(seriesSpy.count(), 1);
925 QCOMPARE(setSpy1.count(), 1);
939 QCOMPARE(setSpy1.count(), 1);
926 QCOMPARE(setSpy2.count(), 0);
940 QCOMPARE(setSpy2.count(), 0);
927
941
928 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
942 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
929 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
943 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
930 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
944 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
931 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
945 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
932
946
933 QList<QVariant> setSpyArg = setSpy1.takeFirst();
947 QList<QVariant> setSpyArg = setSpy1.takeFirst();
934 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
948 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
935 QVERIFY(setSpyArg.at(0).toInt() == 0);
949 QVERIFY(setSpyArg.at(0).toInt() == 0);
936
950
937 //====================================================================================
951 //====================================================================================
938 // barset 1, bar 1
952 // barset 1, bar 1
939 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
953 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
940 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
954 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
941
955
942 QCOMPARE(seriesSpy.count(), 1);
956 QCOMPARE(seriesSpy.count(), 1);
943 QCOMPARE(setSpy1.count(), 1);
957 QCOMPARE(setSpy1.count(), 1);
944 QCOMPARE(setSpy2.count(), 0);
958 QCOMPARE(setSpy2.count(), 0);
945
959
946 seriesSpyArg = seriesSpy.takeFirst();
960 seriesSpyArg = seriesSpy.takeFirst();
947 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
961 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
948 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
962 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
949 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
963 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
950
964
951 setSpyArg = setSpy1.takeFirst();
965 setSpyArg = setSpy1.takeFirst();
952 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
966 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
953 QVERIFY(setSpyArg.at(0).toInt() == 1);
967 QVERIFY(setSpyArg.at(0).toInt() == 1);
954
968
955 //====================================================================================
969 //====================================================================================
956 // barset 1, bar 2
970 // barset 1, bar 2
957 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
971 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
958 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
972 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
959
973
960 QCOMPARE(seriesSpy.count(), 1);
974 QCOMPARE(seriesSpy.count(), 1);
961 QCOMPARE(setSpy1.count(), 1);
975 QCOMPARE(setSpy1.count(), 1);
962 QCOMPARE(setSpy2.count(), 0);
976 QCOMPARE(setSpy2.count(), 0);
963
977
964 seriesSpyArg = seriesSpy.takeFirst();
978 seriesSpyArg = seriesSpy.takeFirst();
965 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
979 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
966 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
980 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
967 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
981 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
968
982
969 setSpyArg = setSpy1.takeFirst();
983 setSpyArg = setSpy1.takeFirst();
970 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
984 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
971 QVERIFY(setSpyArg.at(0).toInt() == 2);
985 QVERIFY(setSpyArg.at(0).toInt() == 2);
972
986
973 //====================================================================================
987 //====================================================================================
974 // barset 2, bar 0
988 // barset 2, bar 0
975 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
989 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
976 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
990 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
977
991
978 QCOMPARE(seriesSpy.count(), 1);
992 QCOMPARE(seriesSpy.count(), 1);
979 QCOMPARE(setSpy1.count(), 0);
993 QCOMPARE(setSpy1.count(), 0);
980 QCOMPARE(setSpy2.count(), 1);
994 QCOMPARE(setSpy2.count(), 1);
981
995
982 seriesSpyArg = seriesSpy.takeFirst();
996 seriesSpyArg = seriesSpy.takeFirst();
983 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
997 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
984 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
998 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
985 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
999 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
986
1000
987 setSpyArg = setSpy2.takeFirst();
1001 setSpyArg = setSpy2.takeFirst();
988 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1002 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
989 QVERIFY(setSpyArg.at(0).toInt() == 0);
1003 QVERIFY(setSpyArg.at(0).toInt() == 0);
990
1004
991 //====================================================================================
1005 //====================================================================================
992 // barset 2, bar 1
1006 // barset 2, bar 1
993 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
1007 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
994 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1008 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
995
1009
996 QCOMPARE(seriesSpy.count(), 1);
1010 QCOMPARE(seriesSpy.count(), 1);
997 QCOMPARE(setSpy1.count(), 0);
1011 QCOMPARE(setSpy1.count(), 0);
998 QCOMPARE(setSpy2.count(), 1);
1012 QCOMPARE(setSpy2.count(), 1);
999
1013
1000 seriesSpyArg = seriesSpy.takeFirst();
1014 seriesSpyArg = seriesSpy.takeFirst();
1001 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1015 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1002 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1016 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1003 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1017 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1004
1018
1005 setSpyArg = setSpy2.takeFirst();
1019 setSpyArg = setSpy2.takeFirst();
1006 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1020 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1007 QVERIFY(setSpyArg.at(0).toInt() == 1);
1021 QVERIFY(setSpyArg.at(0).toInt() == 1);
1008
1022
1009 //====================================================================================
1023 //====================================================================================
1010 // barset 2, bar 2
1024 // barset 2, bar 2
1011 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
1025 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
1012 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1026 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1013
1027
1014 QCOMPARE(seriesSpy.count(), 1);
1028 QCOMPARE(seriesSpy.count(), 1);
1015 QCOMPARE(setSpy1.count(), 0);
1029 QCOMPARE(setSpy1.count(), 0);
1016 QCOMPARE(setSpy2.count(), 1);
1030 QCOMPARE(setSpy2.count(), 1);
1017
1031
1018 seriesSpyArg = seriesSpy.takeFirst();
1032 seriesSpyArg = seriesSpy.takeFirst();
1019 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1033 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1020 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1034 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1021 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1035 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1022
1036
1023 setSpyArg = setSpy2.takeFirst();
1037 setSpyArg = setSpy2.takeFirst();
1024 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1038 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1025 QVERIFY(setSpyArg.at(0).toInt() == 2);
1039 QVERIFY(setSpyArg.at(0).toInt() == 2);
1026 }
1040 }
1027
1041
1028 void tst_QHorizontalBarSeries::mouseReleased()
1042 void tst_QHorizontalBarSeries::mouseReleased()
1029 {
1043 {
1030 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
1044 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
1031
1045
1032 QHorizontalBarSeries* series = new QHorizontalBarSeries();
1046 QHorizontalBarSeries* series = new QHorizontalBarSeries();
1033
1047
1034 QBarSet* set1 = new QBarSet(QString("set 1"));
1048 QBarSet* set1 = new QBarSet(QString("set 1"));
1035 *set1 << 10 << 10 << 10;
1049 *set1 << 10 << 10 << 10;
1036 series->append(set1);
1050 series->append(set1);
1037
1051
1038 QBarSet* set2 = new QBarSet(QString("set 2"));
1052 QBarSet* set2 = new QBarSet(QString("set 2"));
1039 *set2 << 10 << 10 << 10;
1053 *set2 << 10 << 10 << 10;
1040 series->append(set2);
1054 series->append(set2);
1041 QList<QBarSet*> barSets = series->barSets();
1055 QList<QBarSet*> barSets = series->barSets();
1042
1056
1043 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
1057 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
1044 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
1058 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
1045 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
1059 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
1046
1060
1047 QChartView view(new QChart());
1061 QChartView view(new QChart());
1048 view.resize(400,300);
1062 view.resize(400,300);
1049 view.chart()->addSeries(series);
1063 view.chart()->addSeries(series);
1050 view.show();
1064 view.show();
1051 QTest::qWaitForWindowShown(&view);
1065 QTest::qWaitForWindowShown(&view);
1052
1066
1053 // Calculate expected layout for bars
1067 // Calculate expected layout for bars
1054 QRectF plotArea = view.chart()->plotArea();
1068 QRectF plotArea = view.chart()->plotArea();
1055 qreal width = plotArea.width();
1069 qreal width = plotArea.width();
1056 qreal height = plotArea.height();
1070 qreal height = plotArea.height();
1057 qreal rangeX = 10; // From 0 to 10 because of maximum value in set is 10
1071 qreal rangeX = 10; // From 0 to 10 because of maximum value in set is 10
1058 qreal rangeY = 3; // 3 values per set
1072 qreal rangeY = 3; // 3 values per set
1059 qreal scaleY = (height / rangeY);
1073 qreal scaleY = (height / rangeY);
1060 qreal scaleX = (width / rangeX);
1074 qreal scaleX = (width / rangeX);
1061
1075
1062 qreal setCount = series->count();
1076 qreal setCount = series->count();
1063 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
1077 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
1064 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
1078 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
1065 qreal rectHeight = (scaleY / setCount) * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
1079 qreal rectHeight = (scaleY / setCount) * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
1066
1080
1067 QVector<QRectF> layout;
1081 QVector<QRectF> layout;
1068
1082
1069 // 3 = count of values in set
1083 // 3 = count of values in set
1070 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
1084 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
1071 for (int i = 0; i < 3; i++) {
1085 for (int i = 0; i < 3; i++) {
1072 qreal xPos = -scaleX * domainMinX + plotArea.left();
1086 qreal xPos = -scaleX * domainMinX + plotArea.left();
1073 for (int set = 0; set < setCount; set++) {
1087 for (int set = 0; set < setCount; set++) {
1074 qreal yPos = plotArea.bottom() + (domainMinY - i) * scaleY;
1088 qreal yPos = plotArea.bottom() + (domainMinY - i) * scaleY;
1075 yPos += setCount*rectHeight/2;
1089 yPos += setCount*rectHeight/2;
1076 yPos -= set*rectHeight;
1090 yPos -= set*rectHeight;
1077
1091
1078 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
1092 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
1079 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
1093 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
1080 layout.append(rect);
1094 layout.append(rect);
1081 }
1095 }
1082 }
1096 }
1083
1097
1084 //====================================================================================
1098 //====================================================================================
1085 // barset 1, bar 0
1099 // barset 1, bar 0
1086 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1100 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1087 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1101 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1088
1102
1089 QCOMPARE(seriesSpy.count(), 1);
1103 QCOMPARE(seriesSpy.count(), 1);
1090 QCOMPARE(setSpy1.count(), 1);
1104 QCOMPARE(setSpy1.count(), 1);
1091 QCOMPARE(setSpy2.count(), 0);
1105 QCOMPARE(setSpy2.count(), 0);
1092
1106
1093 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1107 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1094 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1108 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1095 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1109 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1096 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1110 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1097
1111
1098 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1112 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1099 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1113 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1100 QVERIFY(setSpyArg.at(0).toInt() == 0);
1114 QVERIFY(setSpyArg.at(0).toInt() == 0);
1101
1115
1102 //====================================================================================
1116 //====================================================================================
1103 // barset 1, bar 1
1117 // barset 1, bar 1
1104 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
1118 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
1105 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1119 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1106
1120
1107 QCOMPARE(seriesSpy.count(), 1);
1121 QCOMPARE(seriesSpy.count(), 1);
1108 QCOMPARE(setSpy1.count(), 1);
1122 QCOMPARE(setSpy1.count(), 1);
1109 QCOMPARE(setSpy2.count(), 0);
1123 QCOMPARE(setSpy2.count(), 0);
1110
1124
1111 seriesSpyArg = seriesSpy.takeFirst();
1125 seriesSpyArg = seriesSpy.takeFirst();
1112 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1126 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1113 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1127 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1114 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1128 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1115
1129
1116 setSpyArg = setSpy1.takeFirst();
1130 setSpyArg = setSpy1.takeFirst();
1117 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1131 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1118 QVERIFY(setSpyArg.at(0).toInt() == 1);
1132 QVERIFY(setSpyArg.at(0).toInt() == 1);
1119
1133
1120 //====================================================================================
1134 //====================================================================================
1121 // barset 1, bar 2
1135 // barset 1, bar 2
1122 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
1136 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
1123 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1137 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1124
1138
1125 QCOMPARE(seriesSpy.count(), 1);
1139 QCOMPARE(seriesSpy.count(), 1);
1126 QCOMPARE(setSpy1.count(), 1);
1140 QCOMPARE(setSpy1.count(), 1);
1127 QCOMPARE(setSpy2.count(), 0);
1141 QCOMPARE(setSpy2.count(), 0);
1128
1142
1129 seriesSpyArg = seriesSpy.takeFirst();
1143 seriesSpyArg = seriesSpy.takeFirst();
1130 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1144 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1131 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1145 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1132 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1146 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1133
1147
1134 setSpyArg = setSpy1.takeFirst();
1148 setSpyArg = setSpy1.takeFirst();
1135 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1149 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1136 QVERIFY(setSpyArg.at(0).toInt() == 2);
1150 QVERIFY(setSpyArg.at(0).toInt() == 2);
1137
1151
1138 //====================================================================================
1152 //====================================================================================
1139 // barset 2, bar 0
1153 // barset 2, bar 0
1140 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
1154 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
1141 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1155 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1142
1156
1143 QCOMPARE(seriesSpy.count(), 1);
1157 QCOMPARE(seriesSpy.count(), 1);
1144 QCOMPARE(setSpy1.count(), 0);
1158 QCOMPARE(setSpy1.count(), 0);
1145 QCOMPARE(setSpy2.count(), 1);
1159 QCOMPARE(setSpy2.count(), 1);
1146
1160
1147 seriesSpyArg = seriesSpy.takeFirst();
1161 seriesSpyArg = seriesSpy.takeFirst();
1148 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1162 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1149 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1163 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1150 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1164 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1151
1165
1152 setSpyArg = setSpy2.takeFirst();
1166 setSpyArg = setSpy2.takeFirst();
1153 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1167 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1154 QVERIFY(setSpyArg.at(0).toInt() == 0);
1168 QVERIFY(setSpyArg.at(0).toInt() == 0);
1155
1169
1156 //====================================================================================
1170 //====================================================================================
1157 // barset 2, bar 1
1171 // barset 2, bar 1
1158 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
1172 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
1159 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1173 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1160
1174
1161 QCOMPARE(seriesSpy.count(), 1);
1175 QCOMPARE(seriesSpy.count(), 1);
1162 QCOMPARE(setSpy1.count(), 0);
1176 QCOMPARE(setSpy1.count(), 0);
1163 QCOMPARE(setSpy2.count(), 1);
1177 QCOMPARE(setSpy2.count(), 1);
1164
1178
1165 seriesSpyArg = seriesSpy.takeFirst();
1179 seriesSpyArg = seriesSpy.takeFirst();
1166 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1180 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1167 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1181 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1168 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1182 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1169
1183
1170 setSpyArg = setSpy2.takeFirst();
1184 setSpyArg = setSpy2.takeFirst();
1171 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1185 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1172 QVERIFY(setSpyArg.at(0).toInt() == 1);
1186 QVERIFY(setSpyArg.at(0).toInt() == 1);
1173
1187
1174 //====================================================================================
1188 //====================================================================================
1175 // barset 2, bar 2
1189 // barset 2, bar 2
1176 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
1190 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
1177 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1191 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1178
1192
1179 QCOMPARE(seriesSpy.count(), 1);
1193 QCOMPARE(seriesSpy.count(), 1);
1180 QCOMPARE(setSpy1.count(), 0);
1194 QCOMPARE(setSpy1.count(), 0);
1181 QCOMPARE(setSpy2.count(), 1);
1195 QCOMPARE(setSpy2.count(), 1);
1182
1196
1183 seriesSpyArg = seriesSpy.takeFirst();
1197 seriesSpyArg = seriesSpy.takeFirst();
1184 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1198 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1185 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1199 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1186 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1200 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1187
1201
1188 setSpyArg = setSpy2.takeFirst();
1202 setSpyArg = setSpy2.takeFirst();
1189 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1203 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1190 QVERIFY(setSpyArg.at(0).toInt() == 2);
1204 QVERIFY(setSpyArg.at(0).toInt() == 2);
1191 }
1205 }
1192
1206
1193 void tst_QHorizontalBarSeries::mouseDoubleClicked()
1207 void tst_QHorizontalBarSeries::mouseDoubleClicked()
1194 {
1208 {
1195 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
1209 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
1196
1210
1197 QHorizontalBarSeries* series = new QHorizontalBarSeries();
1211 QHorizontalBarSeries* series = new QHorizontalBarSeries();
1198
1212
1199 QBarSet* set1 = new QBarSet(QString("set 1"));
1213 QBarSet* set1 = new QBarSet(QString("set 1"));
1200 *set1 << 10 << 10 << 10;
1214 *set1 << 10 << 10 << 10;
1201 series->append(set1);
1215 series->append(set1);
1202
1216
1203 QBarSet* set2 = new QBarSet(QString("set 2"));
1217 QBarSet* set2 = new QBarSet(QString("set 2"));
1204 *set2 << 10 << 10 << 10;
1218 *set2 << 10 << 10 << 10;
1205 series->append(set2);
1219 series->append(set2);
1206 QList<QBarSet*> barSets = series->barSets();
1220 QList<QBarSet*> barSets = series->barSets();
1207
1221
1208 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
1222 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
1209 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
1223 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
1210 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
1224 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
1211
1225
1212 QChartView view(new QChart());
1226 QChartView view(new QChart());
1213 view.resize(400,300);
1227 view.resize(400,300);
1214 view.chart()->addSeries(series);
1228 view.chart()->addSeries(series);
1215 view.show();
1229 view.show();
1216 QTest::qWaitForWindowShown(&view);
1230 QTest::qWaitForWindowShown(&view);
1217
1231
1218 // Calculate expected layout for bars
1232 // Calculate expected layout for bars
1219 QRectF plotArea = view.chart()->plotArea();
1233 QRectF plotArea = view.chart()->plotArea();
1220 qreal width = plotArea.width();
1234 qreal width = plotArea.width();
1221 qreal height = plotArea.height();
1235 qreal height = plotArea.height();
1222 qreal rangeX = 10; // From 0 to 10 because of maximum value in set is 10
1236 qreal rangeX = 10; // From 0 to 10 because of maximum value in set is 10
1223 qreal rangeY = 3; // 3 values per set
1237 qreal rangeY = 3; // 3 values per set
1224 qreal scaleY = (height / rangeY);
1238 qreal scaleY = (height / rangeY);
1225 qreal scaleX = (width / rangeX);
1239 qreal scaleX = (width / rangeX);
1226
1240
1227 qreal setCount = series->count();
1241 qreal setCount = series->count();
1228 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
1242 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
1229 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
1243 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
1230 qreal rectHeight = (scaleY / setCount) * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
1244 qreal rectHeight = (scaleY / setCount) * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
1231
1245
1232 QVector<QRectF> layout;
1246 QVector<QRectF> layout;
1233
1247
1234 // 3 = count of values in set
1248 // 3 = count of values in set
1235 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
1249 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
1236 for (int i = 0; i < 3; i++) {
1250 for (int i = 0; i < 3; i++) {
1237 qreal xPos = -scaleX * domainMinX + plotArea.left();
1251 qreal xPos = -scaleX * domainMinX + plotArea.left();
1238 for (int set = 0; set < setCount; set++) {
1252 for (int set = 0; set < setCount; set++) {
1239 qreal yPos = plotArea.bottom() + (domainMinY - i) * scaleY;
1253 qreal yPos = plotArea.bottom() + (domainMinY - i) * scaleY;
1240 yPos += setCount*rectHeight/2;
1254 yPos += setCount*rectHeight/2;
1241 yPos -= set*rectHeight;
1255 yPos -= set*rectHeight;
1242
1256
1243 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
1257 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
1244 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
1258 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
1245 layout.append(rect);
1259 layout.append(rect);
1246 }
1260 }
1247 }
1261 }
1248
1262
1249 // barset 1, bar 0
1263 // barset 1, bar 0
1250 QTest::mouseDClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1264 QTest::mouseDClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1251 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1265 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1252
1266
1253 QCOMPARE(seriesSpy.count(), 1);
1267 QCOMPARE(seriesSpy.count(), 1);
1254 QCOMPARE(setSpy1.count(), 1);
1268 QCOMPARE(setSpy1.count(), 1);
1255 QCOMPARE(setSpy2.count(), 0);
1269 QCOMPARE(setSpy2.count(), 0);
1256
1270
1257 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1271 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1258 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1272 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1259 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1273 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1260 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1274 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1261
1275
1262 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1276 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1263 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1277 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1264 QVERIFY(setSpyArg.at(0).toInt() == 0);
1278 QVERIFY(setSpyArg.at(0).toInt() == 0);
1265 }
1279 }
1266 QTEST_MAIN(tst_QHorizontalBarSeries)
1280 QTEST_MAIN(tst_QHorizontalBarSeries)
1267
1281
1268 #include "tst_qhorizontalbarseries.moc"
1282 #include "tst_qhorizontalbarseries.moc"
1269
1283
@@ -1,1016 +1,1030
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd
3 ** Copyright (C) 2015 The Qt Company Ltd
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 **
6 **
7 ** This file is part of the Qt Charts module.
7 ** This file is part of the Qt Charts module.
8 **
8 **
9 ** Licensees holding valid commercial license for Qt may use this file in
9 ** Licensees holding valid commercial license for Qt may use this file in
10 ** accordance with the Qt License Agreement provided with the Software
10 ** accordance with the Qt License Agreement provided with the Software
11 ** or, alternatively, in accordance with the terms contained in a written
11 ** or, alternatively, in accordance with the terms contained in a written
12 ** agreement between you and The Qt Company.
12 ** agreement between you and The Qt Company.
13 **
13 **
14 ** If you have questions regarding the use of this file, please use
14 ** If you have questions regarding the use of this file, please use
15 ** contact form at http://qt.io
15 ** contact form at http://qt.io
16 **
16 **
17 ****************************************************************************/
17 ****************************************************************************/
18
18
19 #include <QtTest/QtTest>
19 #include <QtTest/QtTest>
20 #include <QtCharts/QHorizontalPercentBarSeries>
20 #include <QtCharts/QHorizontalPercentBarSeries>
21 #include <QtCharts/QBarSet>
21 #include <QtCharts/QBarSet>
22 #include <QtCharts/QChartView>
22 #include <QtCharts/QChartView>
23 #include <QtCharts/QChart>
23 #include <QtCharts/QChart>
24 #include "tst_definitions.h"
24 #include "tst_definitions.h"
25
25
26 QT_CHARTS_USE_NAMESPACE
26 QT_CHARTS_USE_NAMESPACE
27
27
28 Q_DECLARE_METATYPE(QBarSet*)
28 Q_DECLARE_METATYPE(QBarSet*)
29 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
29 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
30
30
31 class tst_QHorizontalPercentBarSeries : public QObject
31 class tst_QHorizontalPercentBarSeries : public QObject
32 {
32 {
33 Q_OBJECT
33 Q_OBJECT
34
34
35 public slots:
35 public slots:
36 void initTestCase();
36 void initTestCase();
37 void cleanupTestCase();
37 void cleanupTestCase();
38 void init();
38 void init();
39 void cleanup();
39 void cleanup();
40
40
41 private slots:
41 private slots:
42 void qhorizontalpercentbarseries_data();
42 void qhorizontalpercentbarseries_data();
43 void qhorizontalpercentbarseries();
43 void qhorizontalpercentbarseries();
44 void type_data();
44 void type_data();
45 void type();
45 void type();
46 void setLabelsFormat();
46 void setLabelsFormat();
47 void setLabelsPosition();
47 void setLabelsPosition();
48 void setLabelsAngle();
48 void mouseclicked_data();
49 void mouseclicked_data();
49 void mouseclicked();
50 void mouseclicked();
50 void mousehovered_data();
51 void mousehovered_data();
51 void mousehovered();
52 void mousehovered();
52 void zeroValuesInSeries();
53 void zeroValuesInSeries();
53 void mousePressed();
54 void mousePressed();
54 void mouseReleased();
55 void mouseReleased();
55 void mouseDoubleClicked();
56 void mouseDoubleClicked();
56
57
57 private:
58 private:
58 QHorizontalPercentBarSeries* m_barseries;
59 QHorizontalPercentBarSeries* m_barseries;
59 };
60 };
60
61
61 void tst_QHorizontalPercentBarSeries::initTestCase()
62 void tst_QHorizontalPercentBarSeries::initTestCase()
62 {
63 {
63 qRegisterMetaType<QBarSet*>("QBarSet*");
64 qRegisterMetaType<QBarSet*>("QBarSet*");
64 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
65 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
65 }
66 }
66
67
67 void tst_QHorizontalPercentBarSeries::cleanupTestCase()
68 void tst_QHorizontalPercentBarSeries::cleanupTestCase()
68 {
69 {
69 QTest::qWait(1); // Allow final deleteLaters to run
70 QTest::qWait(1); // Allow final deleteLaters to run
70 }
71 }
71
72
72 void tst_QHorizontalPercentBarSeries::init()
73 void tst_QHorizontalPercentBarSeries::init()
73 {
74 {
74 m_barseries = new QHorizontalPercentBarSeries();
75 m_barseries = new QHorizontalPercentBarSeries();
75 }
76 }
76
77
77 void tst_QHorizontalPercentBarSeries::cleanup()
78 void tst_QHorizontalPercentBarSeries::cleanup()
78 {
79 {
79 delete m_barseries;
80 delete m_barseries;
80 m_barseries = 0;
81 m_barseries = 0;
81 }
82 }
82
83
83 void tst_QHorizontalPercentBarSeries::qhorizontalpercentbarseries_data()
84 void tst_QHorizontalPercentBarSeries::qhorizontalpercentbarseries_data()
84 {
85 {
85 }
86 }
86
87
87 void tst_QHorizontalPercentBarSeries::qhorizontalpercentbarseries()
88 void tst_QHorizontalPercentBarSeries::qhorizontalpercentbarseries()
88 {
89 {
89 QHorizontalPercentBarSeries *barseries = new QHorizontalPercentBarSeries();
90 QHorizontalPercentBarSeries *barseries = new QHorizontalPercentBarSeries();
90 QVERIFY(barseries != 0);
91 QVERIFY(barseries != 0);
91 delete barseries;
92 delete barseries;
92 }
93 }
93
94
94 void tst_QHorizontalPercentBarSeries::type_data()
95 void tst_QHorizontalPercentBarSeries::type_data()
95 {
96 {
96
97
97 }
98 }
98
99
99 void tst_QHorizontalPercentBarSeries::type()
100 void tst_QHorizontalPercentBarSeries::type()
100 {
101 {
101 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeHorizontalPercentBar);
102 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeHorizontalPercentBar);
102 }
103 }
103
104
104 void tst_QHorizontalPercentBarSeries::setLabelsFormat()
105 void tst_QHorizontalPercentBarSeries::setLabelsFormat()
105 {
106 {
106 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
107 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
107 QCOMPARE(m_barseries->labelsFormat(), QString());
108 QCOMPARE(m_barseries->labelsFormat(), QString());
108
109
109 QString format("(@value)");
110 QString format("(@value)");
110 m_barseries->setLabelsFormat(format);
111 m_barseries->setLabelsFormat(format);
111 TRY_COMPARE(labelsFormatSpy.count(), 1);
112 TRY_COMPARE(labelsFormatSpy.count(), 1);
112 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
113 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
113 QVERIFY(arguments.at(0).toString() == format);
114 QVERIFY(arguments.at(0).toString() == format);
114 QCOMPARE(m_barseries->labelsFormat(), format);
115 QCOMPARE(m_barseries->labelsFormat(), format);
115
116
116 m_barseries->setLabelsFormat(QString());
117 m_barseries->setLabelsFormat(QString());
117 TRY_COMPARE(labelsFormatSpy.count(), 1);
118 TRY_COMPARE(labelsFormatSpy.count(), 1);
118 arguments = labelsFormatSpy.takeFirst();
119 arguments = labelsFormatSpy.takeFirst();
119 QVERIFY(arguments.at(0).toString() == QString());
120 QVERIFY(arguments.at(0).toString() == QString());
120 QCOMPARE(m_barseries->labelsFormat(), QString());
121 QCOMPARE(m_barseries->labelsFormat(), QString());
121 }
122 }
122
123
123 void tst_QHorizontalPercentBarSeries::setLabelsPosition()
124 void tst_QHorizontalPercentBarSeries::setLabelsPosition()
124 {
125 {
125 QSignalSpy labelsPositionSpy(m_barseries,
126 QSignalSpy labelsPositionSpy(m_barseries,
126 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
127 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
127 QCOMPARE(m_barseries->labelsPosition(), QHorizontalPercentBarSeries::LabelsCenter);
128 QCOMPARE(m_barseries->labelsPosition(), QHorizontalPercentBarSeries::LabelsCenter);
128
129
129 m_barseries->setLabelsPosition(QHorizontalPercentBarSeries::LabelsInsideEnd);
130 m_barseries->setLabelsPosition(QHorizontalPercentBarSeries::LabelsInsideEnd);
130 TRY_COMPARE(labelsPositionSpy.count(), 1);
131 TRY_COMPARE(labelsPositionSpy.count(), 1);
131 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
132 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
132 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
133 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
133 == QHorizontalPercentBarSeries::LabelsInsideEnd);
134 == QHorizontalPercentBarSeries::LabelsInsideEnd);
134 QCOMPARE(m_barseries->labelsPosition(), QHorizontalPercentBarSeries::LabelsInsideEnd);
135 QCOMPARE(m_barseries->labelsPosition(), QHorizontalPercentBarSeries::LabelsInsideEnd);
135
136
136 m_barseries->setLabelsPosition(QHorizontalPercentBarSeries::LabelsInsideBase);
137 m_barseries->setLabelsPosition(QHorizontalPercentBarSeries::LabelsInsideBase);
137 TRY_COMPARE(labelsPositionSpy.count(), 1);
138 TRY_COMPARE(labelsPositionSpy.count(), 1);
138 arguments = labelsPositionSpy.takeFirst();
139 arguments = labelsPositionSpy.takeFirst();
139 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
140 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
140 == QHorizontalPercentBarSeries::LabelsInsideBase);
141 == QHorizontalPercentBarSeries::LabelsInsideBase);
141 QCOMPARE(m_barseries->labelsPosition(), QHorizontalPercentBarSeries::LabelsInsideBase);
142 QCOMPARE(m_barseries->labelsPosition(), QHorizontalPercentBarSeries::LabelsInsideBase);
142
143
143 m_barseries->setLabelsPosition(QHorizontalPercentBarSeries::LabelsOutsideEnd);
144 m_barseries->setLabelsPosition(QHorizontalPercentBarSeries::LabelsOutsideEnd);
144 TRY_COMPARE(labelsPositionSpy.count(), 1);
145 TRY_COMPARE(labelsPositionSpy.count(), 1);
145 arguments = labelsPositionSpy.takeFirst();
146 arguments = labelsPositionSpy.takeFirst();
146 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
147 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
147 == QHorizontalPercentBarSeries::LabelsOutsideEnd);
148 == QHorizontalPercentBarSeries::LabelsOutsideEnd);
148 QCOMPARE(m_barseries->labelsPosition(), QHorizontalPercentBarSeries::LabelsOutsideEnd);
149 QCOMPARE(m_barseries->labelsPosition(), QHorizontalPercentBarSeries::LabelsOutsideEnd);
149
150
150 m_barseries->setLabelsPosition(QHorizontalPercentBarSeries::LabelsCenter);
151 m_barseries->setLabelsPosition(QHorizontalPercentBarSeries::LabelsCenter);
151 TRY_COMPARE(labelsPositionSpy.count(), 1);
152 TRY_COMPARE(labelsPositionSpy.count(), 1);
152 arguments = labelsPositionSpy.takeFirst();
153 arguments = labelsPositionSpy.takeFirst();
153 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
154 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
154 == QHorizontalPercentBarSeries::LabelsCenter);
155 == QHorizontalPercentBarSeries::LabelsCenter);
155 QCOMPARE(m_barseries->labelsPosition(), QHorizontalPercentBarSeries::LabelsCenter);
156 QCOMPARE(m_barseries->labelsPosition(), QHorizontalPercentBarSeries::LabelsCenter);
156 }
157 }
157
158
159 void tst_QHorizontalPercentBarSeries::setLabelsAngle()
160 {
161 QSignalSpy labelsAngleSpy(m_barseries,
162 SIGNAL(labelsAngleChanged(qreal)));
163 QCOMPARE(m_barseries->labelsAngle(), 0.0);
164
165 m_barseries->setLabelsAngle(55.0);
166 TRY_COMPARE(labelsAngleSpy.count(), 1);
167 QList<QVariant> arguments = labelsAngleSpy.takeFirst();
168 QVERIFY(arguments.at(0).value<qreal>() == 55.0);
169 QCOMPARE(m_barseries->labelsAngle(), 55.0);
170 }
171
158 void tst_QHorizontalPercentBarSeries::mouseclicked_data()
172 void tst_QHorizontalPercentBarSeries::mouseclicked_data()
159 {
173 {
160
174
161 }
175 }
162
176
163 void tst_QHorizontalPercentBarSeries::mouseclicked()
177 void tst_QHorizontalPercentBarSeries::mouseclicked()
164 {
178 {
165 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
179 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
166
180
167 QHorizontalPercentBarSeries* series = new QHorizontalPercentBarSeries();
181 QHorizontalPercentBarSeries* series = new QHorizontalPercentBarSeries();
168
182
169 QBarSet* set1 = new QBarSet(QString("set 1"));
183 QBarSet* set1 = new QBarSet(QString("set 1"));
170 *set1 << 10 << 10 << 10;
184 *set1 << 10 << 10 << 10;
171 series->append(set1);
185 series->append(set1);
172
186
173 QBarSet* set2 = new QBarSet(QString("set 2"));
187 QBarSet* set2 = new QBarSet(QString("set 2"));
174 *set2 << 10 << 10 << 10;
188 *set2 << 10 << 10 << 10;
175 series->append(set2);
189 series->append(set2);
176
190
177 QList<QBarSet*> barSets = series->barSets();
191 QList<QBarSet*> barSets = series->barSets();
178
192
179 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
193 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
180
194
181 QChartView view(new QChart());
195 QChartView view(new QChart());
182 view.resize(400,300);
196 view.resize(400,300);
183 view.chart()->addSeries(series);
197 view.chart()->addSeries(series);
184 view.show();
198 view.show();
185 QTest::qWaitForWindowShown(&view);
199 QTest::qWaitForWindowShown(&view);
186
200
187 // Calculate expected layout for bars
201 // Calculate expected layout for bars
188 QRectF plotArea = view.chart()->plotArea();
202 QRectF plotArea = view.chart()->plotArea();
189 qreal width = plotArea.width();
203 qreal width = plotArea.width();
190 qreal height = plotArea.height();
204 qreal height = plotArea.height();
191 qreal rangeY = 3; // 3 values per set
205 qreal rangeY = 3; // 3 values per set
192 qreal rangeX = 100; // From 0 to 100 because of scaling to 100%
206 qreal rangeX = 100; // From 0 to 100 because of scaling to 100%
193 qreal scaleY = (height / rangeY);
207 qreal scaleY = (height / rangeY);
194 qreal scaleX = (width / rangeX);
208 qreal scaleX = (width / rangeX);
195
209
196 qreal setCount = series->count();
210 qreal setCount = series->count();
197 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
211 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
198 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
212 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
199 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
213 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
200
214
201 QVector<QRectF> layout;
215 QVector<QRectF> layout;
202
216
203 // 3 = count of values in set
217 // 3 = count of values in set
204 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
218 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
205 for (int i = 0; i < 3; i++) {
219 for (int i = 0; i < 3; i++) {
206 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
220 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
207 qreal percentage = (100 / colSum);
221 qreal percentage = (100 / colSum);
208 qreal xPos = -scaleX * domainMinX + plotArea.left();
222 qreal xPos = -scaleX * domainMinX + plotArea.left();
209 for (int set = 0; set < setCount; set++) {
223 for (int set = 0; set < setCount; set++) {
210 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
224 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
211 qreal rectWidth = barSets.at(set)->at(i) * percentage * scaleX;
225 qreal rectWidth = barSets.at(set)->at(i) * percentage * scaleX;
212 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
226 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
213 layout.append(rect);
227 layout.append(rect);
214 xPos += rectWidth;
228 xPos += rectWidth;
215 }
229 }
216 }
230 }
217
231
218 //====================================================================================
232 //====================================================================================
219 // barset 1, bar 0
233 // barset 1, bar 0
220 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
234 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
221 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
235 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
222
236
223 QCOMPARE(seriesSpy.count(), 1);
237 QCOMPARE(seriesSpy.count(), 1);
224
238
225 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
239 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
226 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
240 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
227 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
241 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
228 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
242 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
229
243
230 //====================================================================================
244 //====================================================================================
231 // barset 1, bar 1
245 // barset 1, bar 1
232 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
246 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
233 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
247 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
234
248
235 QCOMPARE(seriesSpy.count(), 1);
249 QCOMPARE(seriesSpy.count(), 1);
236
250
237 seriesSpyArg = seriesSpy.takeFirst();
251 seriesSpyArg = seriesSpy.takeFirst();
238 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
252 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
239 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
253 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
240 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
254 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
241
255
242 //====================================================================================
256 //====================================================================================
243 // barset 1, bar 2
257 // barset 1, bar 2
244 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
258 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
245 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
259 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
246
260
247 QCOMPARE(seriesSpy.count(), 1);
261 QCOMPARE(seriesSpy.count(), 1);
248
262
249 seriesSpyArg = seriesSpy.takeFirst();
263 seriesSpyArg = seriesSpy.takeFirst();
250 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
264 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
251 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
265 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
252 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
266 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
253
267
254 //====================================================================================
268 //====================================================================================
255 // barset 2, bar 0
269 // barset 2, bar 0
256 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
270 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
257 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
271 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
258
272
259 QCOMPARE(seriesSpy.count(), 1);
273 QCOMPARE(seriesSpy.count(), 1);
260
274
261 seriesSpyArg = seriesSpy.takeFirst();
275 seriesSpyArg = seriesSpy.takeFirst();
262 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
276 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
263 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
277 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
264 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
278 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
265
279
266 //====================================================================================
280 //====================================================================================
267 // barset 2, bar 1
281 // barset 2, bar 1
268 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
282 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
269 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
283 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
270
284
271 QCOMPARE(seriesSpy.count(), 1);
285 QCOMPARE(seriesSpy.count(), 1);
272
286
273 seriesSpyArg = seriesSpy.takeFirst();
287 seriesSpyArg = seriesSpy.takeFirst();
274 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
288 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
275 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
289 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
276 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
290 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
277
291
278 //====================================================================================
292 //====================================================================================
279 // barset 2, bar 2
293 // barset 2, bar 2
280 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
294 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
281 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
295 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
282
296
283 QCOMPARE(seriesSpy.count(), 1);
297 QCOMPARE(seriesSpy.count(), 1);
284
298
285 seriesSpyArg = seriesSpy.takeFirst();
299 seriesSpyArg = seriesSpy.takeFirst();
286 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
300 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
287 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
301 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
288 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
302 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
289 }
303 }
290
304
291 void tst_QHorizontalPercentBarSeries::mousehovered_data()
305 void tst_QHorizontalPercentBarSeries::mousehovered_data()
292 {
306 {
293
307
294 }
308 }
295
309
296 void tst_QHorizontalPercentBarSeries::mousehovered()
310 void tst_QHorizontalPercentBarSeries::mousehovered()
297 {
311 {
298 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
312 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
299
313
300 QHorizontalPercentBarSeries* series = new QHorizontalPercentBarSeries();
314 QHorizontalPercentBarSeries* series = new QHorizontalPercentBarSeries();
301
315
302 QBarSet* set1 = new QBarSet(QString("set 1"));
316 QBarSet* set1 = new QBarSet(QString("set 1"));
303 *set1 << 10 << 10 << 10;
317 *set1 << 10 << 10 << 10;
304 series->append(set1);
318 series->append(set1);
305
319
306 QBarSet* set2 = new QBarSet(QString("set 2"));
320 QBarSet* set2 = new QBarSet(QString("set 2"));
307 *set2 << 10 << 10 << 10;
321 *set2 << 10 << 10 << 10;
308 series->append(set2);
322 series->append(set2);
309
323
310 QList<QBarSet*> barSets = series->barSets();
324 QList<QBarSet*> barSets = series->barSets();
311
325
312 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
326 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
313 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
327 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
314 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
328 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
315
329
316 QChartView view(new QChart());
330 QChartView view(new QChart());
317 view.resize(400,300);
331 view.resize(400,300);
318 view.chart()->addSeries(series);
332 view.chart()->addSeries(series);
319 view.show();
333 view.show();
320 QTest::qWaitForWindowShown(&view);
334 QTest::qWaitForWindowShown(&view);
321
335
322 //this is hack since view does not get events otherwise
336 //this is hack since view does not get events otherwise
323 view.setMouseTracking(true);
337 view.setMouseTracking(true);
324
338
325 // Calculate expected layout for bars
339 // Calculate expected layout for bars
326 QRectF plotArea = view.chart()->plotArea();
340 QRectF plotArea = view.chart()->plotArea();
327 qreal width = plotArea.width();
341 qreal width = plotArea.width();
328 qreal height = plotArea.height();
342 qreal height = plotArea.height();
329 qreal rangeY = 3; // 3 values per set
343 qreal rangeY = 3; // 3 values per set
330 qreal rangeX = 100; // From 0 to 100 because of scaling to 100%
344 qreal rangeX = 100; // From 0 to 100 because of scaling to 100%
331 qreal scaleY = (height / rangeY);
345 qreal scaleY = (height / rangeY);
332 qreal scaleX = (width / rangeX);
346 qreal scaleX = (width / rangeX);
333
347
334 qreal setCount = series->count();
348 qreal setCount = series->count();
335 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
349 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
336 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
350 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
337 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
351 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
338
352
339 QVector<QRectF> layout;
353 QVector<QRectF> layout;
340
354
341 // 3 = count of values in set
355 // 3 = count of values in set
342 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
356 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
343 for (int i = 0; i < 3; i++) {
357 for (int i = 0; i < 3; i++) {
344 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
358 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
345 qreal percentage = (100 / colSum);
359 qreal percentage = (100 / colSum);
346 qreal xPos = -scaleX * domainMinX + plotArea.left();
360 qreal xPos = -scaleX * domainMinX + plotArea.left();
347 for (int set = 0; set < setCount; set++) {
361 for (int set = 0; set < setCount; set++) {
348 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
362 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
349 qreal rectWidth = barSets.at(set)->at(i) * percentage * scaleX;
363 qreal rectWidth = barSets.at(set)->at(i) * percentage * scaleX;
350 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
364 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
351 layout.append(rect);
365 layout.append(rect);
352 xPos += rectWidth;
366 xPos += rectWidth;
353 }
367 }
354 }
368 }
355
369
356 //=======================================================================
370 //=======================================================================
357 // move mouse to left border
371 // move mouse to left border
358 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(4).center().y()));
372 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(4).center().y()));
359 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
373 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
360 TRY_COMPARE(seriesIndexSpy.count(), 0);
374 TRY_COMPARE(seriesIndexSpy.count(), 0);
361
375
362 //=======================================================================
376 //=======================================================================
363 // move mouse on top of set1
377 // move mouse on top of set1
364 QTest::mouseMove(view.viewport(), layout.at(4).center().toPoint());
378 QTest::mouseMove(view.viewport(), layout.at(4).center().toPoint());
365 TRY_COMPARE(seriesIndexSpy.count(), 1);
379 TRY_COMPARE(seriesIndexSpy.count(), 1);
366 TRY_COMPARE(setIndexSpy1.count(), 1);
380 TRY_COMPARE(setIndexSpy1.count(), 1);
367 TRY_COMPARE(setIndexSpy2.count(), 0);
381 TRY_COMPARE(setIndexSpy2.count(), 0);
368
382
369 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
383 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
370 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
384 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
371 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
385 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
372 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
386 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
373
387
374 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
388 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
375 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
389 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
376 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
390 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
377
391
378 //=======================================================================
392 //=======================================================================
379 // move mouse from top of set1 to top of set2
393 // move mouse from top of set1 to top of set2
380 QTest::mouseMove(view.viewport(), layout.at(5).center().toPoint());
394 QTest::mouseMove(view.viewport(), layout.at(5).center().toPoint());
381 TRY_COMPARE(seriesIndexSpy.count(), 2);
395 TRY_COMPARE(seriesIndexSpy.count(), 2);
382 TRY_COMPARE(setIndexSpy1.count(), 1);
396 TRY_COMPARE(setIndexSpy1.count(), 1);
383 TRY_COMPARE(setIndexSpy2.count(), 1);
397 TRY_COMPARE(setIndexSpy2.count(), 1);
384
398
385 // should leave set1
399 // should leave set1
386 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
400 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
387 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
401 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
388 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
402 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
389 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
403 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
390
404
391 setIndexSpyArg = setIndexSpy1.takeFirst();
405 setIndexSpyArg = setIndexSpy1.takeFirst();
392 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
406 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
393 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
407 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
394
408
395 // should enter set2
409 // should enter set2
396 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
410 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
397 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
411 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
398 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
412 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
399 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
413 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
400
414
401 setIndexSpyArg = setIndexSpy2.takeFirst();
415 setIndexSpyArg = setIndexSpy2.takeFirst();
402 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
416 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
403 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
417 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
404
418
405 //=======================================================================
419 //=======================================================================
406 // move mouse from top of set2 to background
420 // move mouse from top of set2 to background
407 QTest::mouseMove(view.viewport(), QPoint(layout.at(5).center().x(), 0));
421 QTest::mouseMove(view.viewport(), QPoint(layout.at(5).center().x(), 0));
408 TRY_COMPARE(seriesIndexSpy.count(), 1);
422 TRY_COMPARE(seriesIndexSpy.count(), 1);
409 TRY_COMPARE(setIndexSpy1.count(), 0);
423 TRY_COMPARE(setIndexSpy1.count(), 0);
410 TRY_COMPARE(setIndexSpy2.count(), 1);
424 TRY_COMPARE(setIndexSpy2.count(), 1);
411
425
412 // should leave set2
426 // should leave set2
413 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
427 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
414 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
428 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
415 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
429 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
416 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
430 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
417
431
418 setIndexSpyArg = setIndexSpy2.takeFirst();
432 setIndexSpyArg = setIndexSpy2.takeFirst();
419 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
433 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
420 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
434 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
421
435
422 //=======================================================================
436 //=======================================================================
423 // move mouse on top of set1, bar0 to check the index (hover into set1)
437 // move mouse on top of set1, bar0 to check the index (hover into set1)
424 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
438 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
425
439
426 TRY_COMPARE(seriesIndexSpy.count(), 1);
440 TRY_COMPARE(seriesIndexSpy.count(), 1);
427 TRY_COMPARE(setIndexSpy1.count(), 1);
441 TRY_COMPARE(setIndexSpy1.count(), 1);
428 TRY_COMPARE(setIndexSpy2.count(), 0);
442 TRY_COMPARE(setIndexSpy2.count(), 0);
429
443
430 //should enter set1, bar0
444 //should enter set1, bar0
431 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
445 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
432 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
446 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
433 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
447 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
434 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
448 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
435 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
449 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
436 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
450 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
437
451
438 setIndexSpyArg = setIndexSpy1.takeFirst();
452 setIndexSpyArg = setIndexSpy1.takeFirst();
439 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
453 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
440 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
454 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
441 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
455 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
442 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
456 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
443
457
444 //=======================================================================
458 //=======================================================================
445 // move mouse on top of set2, bar0 to check the index (hover out set1,
459 // move mouse on top of set2, bar0 to check the index (hover out set1,
446 // hover in set2)
460 // hover in set2)
447 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
461 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
448 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
462 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
449
463
450 TRY_COMPARE(seriesIndexSpy.count(), 2);
464 TRY_COMPARE(seriesIndexSpy.count(), 2);
451 TRY_COMPARE(setIndexSpy1.count(), 1);
465 TRY_COMPARE(setIndexSpy1.count(), 1);
452 TRY_COMPARE(setIndexSpy2.count(), 1);
466 TRY_COMPARE(setIndexSpy2.count(), 1);
453
467
454 //should leave set1, bar0
468 //should leave set1, bar0
455 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
469 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
456 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
470 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
457 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
471 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
458 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
472 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
459 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
473 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
460 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
474 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
461
475
462 setIndexSpyArg = setIndexSpy1.takeFirst();
476 setIndexSpyArg = setIndexSpy1.takeFirst();
463 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
477 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
464 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
478 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
465 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
479 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
466 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
480 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
467
481
468 //should enter set2, bar0
482 //should enter set2, bar0
469 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
483 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
470 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
484 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
471 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
485 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
472 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
486 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
473 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
487 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
474 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
488 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
475
489
476 setIndexSpyArg = setIndexSpy2.takeFirst();
490 setIndexSpyArg = setIndexSpy2.takeFirst();
477 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
491 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
478 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
492 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
479 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
493 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
480 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
494 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
481
495
482 //=======================================================================
496 //=======================================================================
483 // move mouse on top of set1, bar1 to check the index (hover out set2,
497 // move mouse on top of set1, bar1 to check the index (hover out set2,
484 // hover in set1)
498 // hover in set1)
485 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
499 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
486
500
487 TRY_COMPARE(seriesIndexSpy.count(), 2);
501 TRY_COMPARE(seriesIndexSpy.count(), 2);
488 TRY_COMPARE(setIndexSpy1.count(), 1);
502 TRY_COMPARE(setIndexSpy1.count(), 1);
489 TRY_COMPARE(setIndexSpy2.count(), 1);
503 TRY_COMPARE(setIndexSpy2.count(), 1);
490
504
491 //should leave set2, bar0
505 //should leave set2, bar0
492 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
506 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
493 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
507 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
494 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
508 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
495 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
509 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
496 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
510 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
497 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
511 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
498
512
499 setIndexSpyArg = setIndexSpy2.takeFirst();
513 setIndexSpyArg = setIndexSpy2.takeFirst();
500 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
514 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
501 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
515 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
502 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
516 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
503 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
517 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
504
518
505 //should enter set1, bar1
519 //should enter set1, bar1
506 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
520 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
507 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
521 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
508 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
522 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
509 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
523 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
510 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
524 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
511 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
525 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
512
526
513 setIndexSpyArg = setIndexSpy1.takeFirst();
527 setIndexSpyArg = setIndexSpy1.takeFirst();
514 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
528 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
515 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
529 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
516 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
530 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
517 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
531 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
518
532
519 //=======================================================================
533 //=======================================================================
520 // move mouse between set1 and set2 (hover out set1)
534 // move mouse between set1 and set2 (hover out set1)
521 QTest::mouseMove(view.viewport(), QPoint(layout.at(3).left(),
535 QTest::mouseMove(view.viewport(), QPoint(layout.at(3).left(),
522 (layout.at(3).top() + layout.at(4).bottom()) / 2));
536 (layout.at(3).top() + layout.at(4).bottom()) / 2));
523
537
524 TRY_COMPARE(seriesIndexSpy.count(), 1);
538 TRY_COMPARE(seriesIndexSpy.count(), 1);
525 TRY_COMPARE(setIndexSpy1.count(), 1);
539 TRY_COMPARE(setIndexSpy1.count(), 1);
526 TRY_COMPARE(setIndexSpy2.count(), 0);
540 TRY_COMPARE(setIndexSpy2.count(), 0);
527
541
528 //should leave set1, bar1
542 //should leave set1, bar1
529 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
543 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
530 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
544 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
531 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
545 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
532 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
546 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
533 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
547 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
534 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
548 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
535
549
536 setIndexSpyArg = setIndexSpy1.takeFirst();
550 setIndexSpyArg = setIndexSpy1.takeFirst();
537 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
551 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
538 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
552 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
539 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
553 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
540 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
554 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
541
555
542 //=======================================================================
556 //=======================================================================
543 // move mouse on top of set2, bar1 to check the index (hover in set2)
557 // move mouse on top of set2, bar1 to check the index (hover in set2)
544 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
558 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
545
559
546 TRY_COMPARE(seriesIndexSpy.count(), 1);
560 TRY_COMPARE(seriesIndexSpy.count(), 1);
547 TRY_COMPARE(setIndexSpy1.count(), 0);
561 TRY_COMPARE(setIndexSpy1.count(), 0);
548 TRY_COMPARE(setIndexSpy2.count(), 1);
562 TRY_COMPARE(setIndexSpy2.count(), 1);
549
563
550 //should enter set2, bar1
564 //should enter set2, bar1
551 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
565 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
552 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
566 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
553 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
567 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
554 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
568 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
555 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
569 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
556 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
570 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
557
571
558 setIndexSpyArg = setIndexSpy2.takeFirst();
572 setIndexSpyArg = setIndexSpy2.takeFirst();
559 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
573 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
560 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
574 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
561 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
575 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
562 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
576 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
563
577
564 //=======================================================================
578 //=======================================================================
565 // move mouse between set1 and set2 (hover out set2)
579 // move mouse between set1 and set2 (hover out set2)
566 QTest::mouseMove(view.viewport(), QPoint(layout.at(3).left(),
580 QTest::mouseMove(view.viewport(), QPoint(layout.at(3).left(),
567 (layout.at(3).top() + layout.at(4).bottom()) / 2));
581 (layout.at(3).top() + layout.at(4).bottom()) / 2));
568
582
569 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
583 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
570 TRY_COMPARE(seriesIndexSpy.count(), 1);
584 TRY_COMPARE(seriesIndexSpy.count(), 1);
571 TRY_COMPARE(setIndexSpy1.count(), 0);
585 TRY_COMPARE(setIndexSpy1.count(), 0);
572 TRY_COMPARE(setIndexSpy2.count(), 1);
586 TRY_COMPARE(setIndexSpy2.count(), 1);
573
587
574 //should leave set1, bar1
588 //should leave set1, bar1
575 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
589 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
576 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
590 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
577 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
591 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
578 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
592 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
579 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
593 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
580 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
594 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
581
595
582 setIndexSpyArg = setIndexSpy2.takeFirst();
596 setIndexSpyArg = setIndexSpy2.takeFirst();
583 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
597 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
584 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
598 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
585 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
599 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
586 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
600 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
587 }
601 }
588
602
589 void tst_QHorizontalPercentBarSeries::zeroValuesInSeries()
603 void tst_QHorizontalPercentBarSeries::zeroValuesInSeries()
590 {
604 {
591 QHorizontalPercentBarSeries *series = new QHorizontalPercentBarSeries();
605 QHorizontalPercentBarSeries *series = new QHorizontalPercentBarSeries();
592 QBarSet *set1 = new QBarSet(QString("set 1"));
606 QBarSet *set1 = new QBarSet(QString("set 1"));
593 *set1 << 100 << 0.0 << 10;
607 *set1 << 100 << 0.0 << 10;
594 series->append(set1);
608 series->append(set1);
595
609
596 QBarSet *set2 = new QBarSet(QString("set 2"));
610 QBarSet *set2 = new QBarSet(QString("set 2"));
597 *set2 << 0.0 << 0.0 << 70;
611 *set2 << 0.0 << 0.0 << 70;
598 series->append(set2);
612 series->append(set2);
599
613
600 QChartView view(new QChart());
614 QChartView view(new QChart());
601 view.chart()->addSeries(series);
615 view.chart()->addSeries(series);
602 view.chart()->createDefaultAxes();
616 view.chart()->createDefaultAxes();
603 view.show();
617 view.show();
604
618
605 QTest::qWaitForWindowShown(&view);
619 QTest::qWaitForWindowShown(&view);
606 }
620 }
607
621
608
622
609 void tst_QHorizontalPercentBarSeries::mousePressed()
623 void tst_QHorizontalPercentBarSeries::mousePressed()
610 {
624 {
611 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
625 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
612
626
613 QHorizontalPercentBarSeries* series = new QHorizontalPercentBarSeries();
627 QHorizontalPercentBarSeries* series = new QHorizontalPercentBarSeries();
614
628
615 QBarSet* set1 = new QBarSet(QString("set 1"));
629 QBarSet* set1 = new QBarSet(QString("set 1"));
616 *set1 << 10 << 10 << 10;
630 *set1 << 10 << 10 << 10;
617 series->append(set1);
631 series->append(set1);
618
632
619 QBarSet* set2 = new QBarSet(QString("set 2"));
633 QBarSet* set2 = new QBarSet(QString("set 2"));
620 *set2 << 10 << 10 << 10;
634 *set2 << 10 << 10 << 10;
621 series->append(set2);
635 series->append(set2);
622 QList<QBarSet*> barSets = series->barSets();
636 QList<QBarSet*> barSets = series->barSets();
623
637
624 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
638 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
625 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
639 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
626 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
640 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
627
641
628 QChartView view(new QChart());
642 QChartView view(new QChart());
629 view.resize(400,300);
643 view.resize(400,300);
630 view.chart()->addSeries(series);
644 view.chart()->addSeries(series);
631 view.show();
645 view.show();
632 QTest::qWaitForWindowShown(&view);
646 QTest::qWaitForWindowShown(&view);
633
647
634 // Calculate expected layout for bars
648 // Calculate expected layout for bars
635 QRectF plotArea = view.chart()->plotArea();
649 QRectF plotArea = view.chart()->plotArea();
636 qreal width = plotArea.width();
650 qreal width = plotArea.width();
637 qreal height = plotArea.height();
651 qreal height = plotArea.height();
638 qreal rangeX = 100; // From 0 to 100 because of scaling to 100%
652 qreal rangeX = 100; // From 0 to 100 because of scaling to 100%
639 qreal rangeY = 3; // 3 values per set
653 qreal rangeY = 3; // 3 values per set
640 qreal scaleY = (height / rangeY);
654 qreal scaleY = (height / rangeY);
641 qreal scaleX = (width / rangeX);
655 qreal scaleX = (width / rangeX);
642
656
643 qreal setCount = series->count();
657 qreal setCount = series->count();
644 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
658 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
645 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
659 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
646 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
660 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
647
661
648 QVector<QRectF> layout;
662 QVector<QRectF> layout;
649
663
650 // 3 = count of values in set
664 // 3 = count of values in set
651 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
665 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
652 for (int i = 0; i < 3; i++) {
666 for (int i = 0; i < 3; i++) {
653 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
667 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
654 qreal percentage = (100 / colSum);
668 qreal percentage = (100 / colSum);
655 qreal xPos = -scaleX * domainMinX + plotArea.left();
669 qreal xPos = -scaleX * domainMinX + plotArea.left();
656 for (int set = 0; set < setCount; set++) {
670 for (int set = 0; set < setCount; set++) {
657 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
671 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
658 qreal rectWidth = barSets.at(set)->at(i) * percentage * scaleX;
672 qreal rectWidth = barSets.at(set)->at(i) * percentage * scaleX;
659 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
673 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
660 layout.append(rect);
674 layout.append(rect);
661 xPos += rectWidth;
675 xPos += rectWidth;
662 }
676 }
663 }
677 }
664
678
665 //====================================================================================
679 //====================================================================================
666 // barset 1, bar 0
680 // barset 1, bar 0
667 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
681 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
668 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
682 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
669
683
670 QCOMPARE(seriesSpy.count(), 1);
684 QCOMPARE(seriesSpy.count(), 1);
671 QCOMPARE(setSpy1.count(), 1);
685 QCOMPARE(setSpy1.count(), 1);
672 QCOMPARE(setSpy2.count(), 0);
686 QCOMPARE(setSpy2.count(), 0);
673
687
674 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
688 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
675 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
689 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
676 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
690 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
677 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
691 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
678
692
679 QList<QVariant> setSpyArg = setSpy1.takeFirst();
693 QList<QVariant> setSpyArg = setSpy1.takeFirst();
680 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
694 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
681 QVERIFY(setSpyArg.at(0).toInt() == 0);
695 QVERIFY(setSpyArg.at(0).toInt() == 0);
682
696
683 //====================================================================================
697 //====================================================================================
684 // barset 1, bar 1
698 // barset 1, bar 1
685 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
699 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
686 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
700 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
687
701
688 QCOMPARE(seriesSpy.count(), 1);
702 QCOMPARE(seriesSpy.count(), 1);
689 QCOMPARE(setSpy1.count(), 1);
703 QCOMPARE(setSpy1.count(), 1);
690 QCOMPARE(setSpy2.count(), 0);
704 QCOMPARE(setSpy2.count(), 0);
691
705
692 seriesSpyArg = seriesSpy.takeFirst();
706 seriesSpyArg = seriesSpy.takeFirst();
693 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
707 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
694 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
708 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
695 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
709 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
696
710
697 setSpyArg = setSpy1.takeFirst();
711 setSpyArg = setSpy1.takeFirst();
698 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
712 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
699 QVERIFY(setSpyArg.at(0).toInt() == 1);
713 QVERIFY(setSpyArg.at(0).toInt() == 1);
700
714
701 //====================================================================================
715 //====================================================================================
702 // barset 1, bar 2
716 // barset 1, bar 2
703 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
717 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
704 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
718 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
705
719
706 QCOMPARE(seriesSpy.count(), 1);
720 QCOMPARE(seriesSpy.count(), 1);
707 QCOMPARE(setSpy1.count(), 1);
721 QCOMPARE(setSpy1.count(), 1);
708 QCOMPARE(setSpy2.count(), 0);
722 QCOMPARE(setSpy2.count(), 0);
709
723
710 seriesSpyArg = seriesSpy.takeFirst();
724 seriesSpyArg = seriesSpy.takeFirst();
711 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
725 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
712 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
726 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
713 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
727 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
714
728
715 setSpyArg = setSpy1.takeFirst();
729 setSpyArg = setSpy1.takeFirst();
716 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
730 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
717 QVERIFY(setSpyArg.at(0).toInt() == 2);
731 QVERIFY(setSpyArg.at(0).toInt() == 2);
718
732
719 //====================================================================================
733 //====================================================================================
720 // barset 2, bar 0
734 // barset 2, bar 0
721 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
735 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
722 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
736 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
723
737
724 QCOMPARE(seriesSpy.count(), 1);
738 QCOMPARE(seriesSpy.count(), 1);
725 QCOMPARE(setSpy1.count(), 0);
739 QCOMPARE(setSpy1.count(), 0);
726 QCOMPARE(setSpy2.count(), 1);
740 QCOMPARE(setSpy2.count(), 1);
727
741
728 seriesSpyArg = seriesSpy.takeFirst();
742 seriesSpyArg = seriesSpy.takeFirst();
729 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
743 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
730 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
744 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
731 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
745 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
732
746
733 setSpyArg = setSpy2.takeFirst();
747 setSpyArg = setSpy2.takeFirst();
734 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
748 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
735 QVERIFY(setSpyArg.at(0).toInt() == 0);
749 QVERIFY(setSpyArg.at(0).toInt() == 0);
736
750
737 //====================================================================================
751 //====================================================================================
738 // barset 2, bar 1
752 // barset 2, bar 1
739 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
753 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
740 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
754 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
741
755
742 QCOMPARE(seriesSpy.count(), 1);
756 QCOMPARE(seriesSpy.count(), 1);
743 QCOMPARE(setSpy1.count(), 0);
757 QCOMPARE(setSpy1.count(), 0);
744 QCOMPARE(setSpy2.count(), 1);
758 QCOMPARE(setSpy2.count(), 1);
745
759
746 seriesSpyArg = seriesSpy.takeFirst();
760 seriesSpyArg = seriesSpy.takeFirst();
747 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
761 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
748 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
762 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
749 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
763 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
750
764
751 setSpyArg = setSpy2.takeFirst();
765 setSpyArg = setSpy2.takeFirst();
752 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
766 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
753 QVERIFY(setSpyArg.at(0).toInt() == 1);
767 QVERIFY(setSpyArg.at(0).toInt() == 1);
754
768
755 //====================================================================================
769 //====================================================================================
756 // barset 2, bar 2
770 // barset 2, bar 2
757 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
771 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
758 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
772 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
759
773
760 QCOMPARE(seriesSpy.count(), 1);
774 QCOMPARE(seriesSpy.count(), 1);
761 QCOMPARE(setSpy1.count(), 0);
775 QCOMPARE(setSpy1.count(), 0);
762 QCOMPARE(setSpy2.count(), 1);
776 QCOMPARE(setSpy2.count(), 1);
763
777
764 seriesSpyArg = seriesSpy.takeFirst();
778 seriesSpyArg = seriesSpy.takeFirst();
765 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
779 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
766 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
780 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
767 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
781 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
768
782
769 setSpyArg = setSpy2.takeFirst();
783 setSpyArg = setSpy2.takeFirst();
770 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
784 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
771 QVERIFY(setSpyArg.at(0).toInt() == 2);
785 QVERIFY(setSpyArg.at(0).toInt() == 2);
772 }
786 }
773
787
774 void tst_QHorizontalPercentBarSeries::mouseReleased()
788 void tst_QHorizontalPercentBarSeries::mouseReleased()
775 {
789 {
776 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
790 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
777
791
778 QHorizontalPercentBarSeries* series = new QHorizontalPercentBarSeries();
792 QHorizontalPercentBarSeries* series = new QHorizontalPercentBarSeries();
779
793
780 QBarSet* set1 = new QBarSet(QString("set 1"));
794 QBarSet* set1 = new QBarSet(QString("set 1"));
781 *set1 << 10 << 10 << 10;
795 *set1 << 10 << 10 << 10;
782 series->append(set1);
796 series->append(set1);
783
797
784 QBarSet* set2 = new QBarSet(QString("set 2"));
798 QBarSet* set2 = new QBarSet(QString("set 2"));
785 *set2 << 10 << 10 << 10;
799 *set2 << 10 << 10 << 10;
786 series->append(set2);
800 series->append(set2);
787 QList<QBarSet*> barSets = series->barSets();
801 QList<QBarSet*> barSets = series->barSets();
788
802
789 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
803 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
790 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
804 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
791 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
805 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
792
806
793 QChartView view(new QChart());
807 QChartView view(new QChart());
794 view.resize(400,300);
808 view.resize(400,300);
795 view.chart()->addSeries(series);
809 view.chart()->addSeries(series);
796 view.show();
810 view.show();
797 QTest::qWaitForWindowShown(&view);
811 QTest::qWaitForWindowShown(&view);
798
812
799 // Calculate expected layout for bars
813 // Calculate expected layout for bars
800 QRectF plotArea = view.chart()->plotArea();
814 QRectF plotArea = view.chart()->plotArea();
801 qreal width = plotArea.width();
815 qreal width = plotArea.width();
802 qreal height = plotArea.height();
816 qreal height = plotArea.height();
803 qreal rangeX = 100; // From 0 to 100 because of scaling to 100%
817 qreal rangeX = 100; // From 0 to 100 because of scaling to 100%
804 qreal rangeY = 3; // 3 values per set
818 qreal rangeY = 3; // 3 values per set
805 qreal scaleY = (height / rangeY);
819 qreal scaleY = (height / rangeY);
806 qreal scaleX = (width / rangeX);
820 qreal scaleX = (width / rangeX);
807
821
808 qreal setCount = series->count();
822 qreal setCount = series->count();
809 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
823 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
810 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
824 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
811 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
825 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
812
826
813 QVector<QRectF> layout;
827 QVector<QRectF> layout;
814
828
815 // 3 = count of values in set
829 // 3 = count of values in set
816 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
830 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
817 for (int i = 0; i < 3; i++) {
831 for (int i = 0; i < 3; i++) {
818 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
832 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
819 qreal percentage = (100 / colSum);
833 qreal percentage = (100 / colSum);
820 qreal xPos = -scaleX * domainMinX + plotArea.left();
834 qreal xPos = -scaleX * domainMinX + plotArea.left();
821 for (int set = 0; set < setCount; set++) {
835 for (int set = 0; set < setCount; set++) {
822 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
836 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
823 qreal rectWidth = barSets.at(set)->at(i) * percentage * scaleX;
837 qreal rectWidth = barSets.at(set)->at(i) * percentage * scaleX;
824 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
838 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
825 layout.append(rect);
839 layout.append(rect);
826 xPos += rectWidth;
840 xPos += rectWidth;
827 }
841 }
828 }
842 }
829
843
830 //====================================================================================
844 //====================================================================================
831 // barset 1, bar 0
845 // barset 1, bar 0
832 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
846 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
833 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
847 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
834
848
835 QCOMPARE(seriesSpy.count(), 1);
849 QCOMPARE(seriesSpy.count(), 1);
836 QCOMPARE(setSpy1.count(), 1);
850 QCOMPARE(setSpy1.count(), 1);
837 QCOMPARE(setSpy2.count(), 0);
851 QCOMPARE(setSpy2.count(), 0);
838
852
839 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
853 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
840 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
854 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
841 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
855 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
842 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
856 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
843
857
844 QList<QVariant> setSpyArg = setSpy1.takeFirst();
858 QList<QVariant> setSpyArg = setSpy1.takeFirst();
845 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
859 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
846 QVERIFY(setSpyArg.at(0).toInt() == 0);
860 QVERIFY(setSpyArg.at(0).toInt() == 0);
847
861
848 //====================================================================================
862 //====================================================================================
849 // barset 1, bar 1
863 // barset 1, bar 1
850 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
864 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
851 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
865 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
852
866
853 QCOMPARE(seriesSpy.count(), 1);
867 QCOMPARE(seriesSpy.count(), 1);
854 QCOMPARE(setSpy1.count(), 1);
868 QCOMPARE(setSpy1.count(), 1);
855 QCOMPARE(setSpy2.count(), 0);
869 QCOMPARE(setSpy2.count(), 0);
856
870
857 seriesSpyArg = seriesSpy.takeFirst();
871 seriesSpyArg = seriesSpy.takeFirst();
858 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
872 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
859 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
873 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
860 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
874 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
861
875
862 setSpyArg = setSpy1.takeFirst();
876 setSpyArg = setSpy1.takeFirst();
863 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
877 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
864 QVERIFY(setSpyArg.at(0).toInt() == 1);
878 QVERIFY(setSpyArg.at(0).toInt() == 1);
865
879
866 //====================================================================================
880 //====================================================================================
867 // barset 1, bar 2
881 // barset 1, bar 2
868 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
882 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
869 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
883 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
870
884
871 QCOMPARE(seriesSpy.count(), 1);
885 QCOMPARE(seriesSpy.count(), 1);
872 QCOMPARE(setSpy1.count(), 1);
886 QCOMPARE(setSpy1.count(), 1);
873 QCOMPARE(setSpy2.count(), 0);
887 QCOMPARE(setSpy2.count(), 0);
874
888
875 seriesSpyArg = seriesSpy.takeFirst();
889 seriesSpyArg = seriesSpy.takeFirst();
876 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
890 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
877 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
891 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
878 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
892 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
879
893
880 setSpyArg = setSpy1.takeFirst();
894 setSpyArg = setSpy1.takeFirst();
881 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
895 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
882 QVERIFY(setSpyArg.at(0).toInt() == 2);
896 QVERIFY(setSpyArg.at(0).toInt() == 2);
883
897
884 //====================================================================================
898 //====================================================================================
885 // barset 2, bar 0
899 // barset 2, bar 0
886 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
900 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
887 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
901 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
888
902
889 QCOMPARE(seriesSpy.count(), 1);
903 QCOMPARE(seriesSpy.count(), 1);
890 QCOMPARE(setSpy1.count(), 0);
904 QCOMPARE(setSpy1.count(), 0);
891 QCOMPARE(setSpy2.count(), 1);
905 QCOMPARE(setSpy2.count(), 1);
892
906
893 seriesSpyArg = seriesSpy.takeFirst();
907 seriesSpyArg = seriesSpy.takeFirst();
894 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
908 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
895 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
909 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
896 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
910 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
897
911
898 setSpyArg = setSpy2.takeFirst();
912 setSpyArg = setSpy2.takeFirst();
899 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
913 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
900 QVERIFY(setSpyArg.at(0).toInt() == 0);
914 QVERIFY(setSpyArg.at(0).toInt() == 0);
901
915
902 //====================================================================================
916 //====================================================================================
903 // barset 2, bar 1
917 // barset 2, bar 1
904 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
918 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
905 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
919 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
906
920
907 QCOMPARE(seriesSpy.count(), 1);
921 QCOMPARE(seriesSpy.count(), 1);
908 QCOMPARE(setSpy1.count(), 0);
922 QCOMPARE(setSpy1.count(), 0);
909 QCOMPARE(setSpy2.count(), 1);
923 QCOMPARE(setSpy2.count(), 1);
910
924
911 seriesSpyArg = seriesSpy.takeFirst();
925 seriesSpyArg = seriesSpy.takeFirst();
912 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
926 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
913 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
927 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
914 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
928 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
915
929
916 setSpyArg = setSpy2.takeFirst();
930 setSpyArg = setSpy2.takeFirst();
917 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
931 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
918 QVERIFY(setSpyArg.at(0).toInt() == 1);
932 QVERIFY(setSpyArg.at(0).toInt() == 1);
919
933
920 //====================================================================================
934 //====================================================================================
921 // barset 2, bar 2
935 // barset 2, bar 2
922 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
936 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
923 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
937 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
924
938
925 QCOMPARE(seriesSpy.count(), 1);
939 QCOMPARE(seriesSpy.count(), 1);
926 QCOMPARE(setSpy1.count(), 0);
940 QCOMPARE(setSpy1.count(), 0);
927 QCOMPARE(setSpy2.count(), 1);
941 QCOMPARE(setSpy2.count(), 1);
928
942
929 seriesSpyArg = seriesSpy.takeFirst();
943 seriesSpyArg = seriesSpy.takeFirst();
930 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
944 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
931 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
945 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
932 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
946 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
933
947
934 setSpyArg = setSpy2.takeFirst();
948 setSpyArg = setSpy2.takeFirst();
935 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
949 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
936 QVERIFY(setSpyArg.at(0).toInt() == 2);
950 QVERIFY(setSpyArg.at(0).toInt() == 2);
937 }
951 }
938
952
939 void tst_QHorizontalPercentBarSeries::mouseDoubleClicked()
953 void tst_QHorizontalPercentBarSeries::mouseDoubleClicked()
940 {
954 {
941 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
955 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
942
956
943 QHorizontalPercentBarSeries* series = new QHorizontalPercentBarSeries();
957 QHorizontalPercentBarSeries* series = new QHorizontalPercentBarSeries();
944
958
945 QBarSet* set1 = new QBarSet(QString("set 1"));
959 QBarSet* set1 = new QBarSet(QString("set 1"));
946 *set1 << 10 << 10 << 10;
960 *set1 << 10 << 10 << 10;
947 series->append(set1);
961 series->append(set1);
948
962
949 QBarSet* set2 = new QBarSet(QString("set 2"));
963 QBarSet* set2 = new QBarSet(QString("set 2"));
950 *set2 << 10 << 10 << 10;
964 *set2 << 10 << 10 << 10;
951 series->append(set2);
965 series->append(set2);
952 QList<QBarSet*> barSets = series->barSets();
966 QList<QBarSet*> barSets = series->barSets();
953
967
954 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
968 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
955 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
969 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
956 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
970 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
957
971
958 QChartView view(new QChart());
972 QChartView view(new QChart());
959 view.resize(400,300);
973 view.resize(400,300);
960 view.chart()->addSeries(series);
974 view.chart()->addSeries(series);
961 view.show();
975 view.show();
962 QTest::qWaitForWindowShown(&view);
976 QTest::qWaitForWindowShown(&view);
963
977
964 // Calculate expected layout for bars
978 // Calculate expected layout for bars
965 QRectF plotArea = view.chart()->plotArea();
979 QRectF plotArea = view.chart()->plotArea();
966 qreal width = plotArea.width();
980 qreal width = plotArea.width();
967 qreal height = plotArea.height();
981 qreal height = plotArea.height();
968 qreal rangeX = 100; // From 0 to 100 because of scaling to 100%
982 qreal rangeX = 100; // From 0 to 100 because of scaling to 100%
969 qreal rangeY = 3; // 3 values per set
983 qreal rangeY = 3; // 3 values per set
970 qreal scaleY = (height / rangeY);
984 qreal scaleY = (height / rangeY);
971 qreal scaleX = (width / rangeX);
985 qreal scaleX = (width / rangeX);
972
986
973 qreal setCount = series->count();
987 qreal setCount = series->count();
974 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
988 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
975 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
989 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
976 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
990 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
977
991
978 QVector<QRectF> layout;
992 QVector<QRectF> layout;
979
993
980 // 3 = count of values in set
994 // 3 = count of values in set
981 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
995 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
982 for (int i = 0; i < 3; i++) {
996 for (int i = 0; i < 3; i++) {
983 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
997 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
984 qreal percentage = (100 / colSum);
998 qreal percentage = (100 / colSum);
985 qreal xPos = -scaleX * domainMinX + plotArea.left();
999 qreal xPos = -scaleX * domainMinX + plotArea.left();
986 for (int set = 0; set < setCount; set++) {
1000 for (int set = 0; set < setCount; set++) {
987 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
1001 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
988 qreal rectWidth = barSets.at(set)->at(i) * percentage * scaleX;
1002 qreal rectWidth = barSets.at(set)->at(i) * percentage * scaleX;
989 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
1003 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
990 layout.append(rect);
1004 layout.append(rect);
991 xPos += rectWidth;
1005 xPos += rectWidth;
992 }
1006 }
993 }
1007 }
994
1008
995 // barset 1, bar 0
1009 // barset 1, bar 0
996 QTest::mouseDClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1010 QTest::mouseDClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
997 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1011 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
998
1012
999 QCOMPARE(seriesSpy.count(), 1);
1013 QCOMPARE(seriesSpy.count(), 1);
1000 QCOMPARE(setSpy1.count(), 1);
1014 QCOMPARE(setSpy1.count(), 1);
1001 QCOMPARE(setSpy2.count(), 0);
1015 QCOMPARE(setSpy2.count(), 0);
1002
1016
1003 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1017 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1004 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1018 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1005 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1019 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1006 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1020 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1007
1021
1008 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1022 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1009 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1023 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1010 QVERIFY(setSpyArg.at(0).toInt() == 0);
1024 QVERIFY(setSpyArg.at(0).toInt() == 0);
1011 }
1025 }
1012
1026
1013 QTEST_MAIN(tst_QHorizontalPercentBarSeries)
1027 QTEST_MAIN(tst_QHorizontalPercentBarSeries)
1014
1028
1015 #include "tst_qhorizontalpercentbarseries.moc"
1029 #include "tst_qhorizontalpercentbarseries.moc"
1016
1030
@@ -1,1019 +1,1033
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd
3 ** Copyright (C) 2015 The Qt Company Ltd
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 **
6 **
7 ** This file is part of the Qt Charts module.
7 ** This file is part of the Qt Charts module.
8 **
8 **
9 ** Licensees holding valid commercial license for Qt may use this file in
9 ** Licensees holding valid commercial license for Qt may use this file in
10 ** accordance with the Qt License Agreement provided with the Software
10 ** accordance with the Qt License Agreement provided with the Software
11 ** or, alternatively, in accordance with the terms contained in a written
11 ** or, alternatively, in accordance with the terms contained in a written
12 ** agreement between you and The Qt Company.
12 ** agreement between you and The Qt Company.
13 **
13 **
14 ** If you have questions regarding the use of this file, please use
14 ** If you have questions regarding the use of this file, please use
15 ** contact form at http://qt.io
15 ** contact form at http://qt.io
16 **
16 **
17 ****************************************************************************/
17 ****************************************************************************/
18
18
19 #include <QtTest/QtTest>
19 #include <QtTest/QtTest>
20 #include <QtCharts/QHorizontalStackedBarSeries>
20 #include <QtCharts/QHorizontalStackedBarSeries>
21 #include <QtCharts/QBarSet>
21 #include <QtCharts/QBarSet>
22 #include <QtCharts/QChartView>
22 #include <QtCharts/QChartView>
23 #include <QtCharts/QChart>
23 #include <QtCharts/QChart>
24 #include "tst_definitions.h"
24 #include "tst_definitions.h"
25
25
26 QT_CHARTS_USE_NAMESPACE
26 QT_CHARTS_USE_NAMESPACE
27
27
28 Q_DECLARE_METATYPE(QBarSet*)
28 Q_DECLARE_METATYPE(QBarSet*)
29 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
29 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
30
30
31 class tst_QHorizontalStackedBarSeries : public QObject
31 class tst_QHorizontalStackedBarSeries : public QObject
32 {
32 {
33 Q_OBJECT
33 Q_OBJECT
34
34
35 public slots:
35 public slots:
36 void initTestCase();
36 void initTestCase();
37 void cleanupTestCase();
37 void cleanupTestCase();
38 void init();
38 void init();
39 void cleanup();
39 void cleanup();
40
40
41 private slots:
41 private slots:
42 void qhorizontalstackedbarseries_data();
42 void qhorizontalstackedbarseries_data();
43 void qhorizontalstackedbarseries();
43 void qhorizontalstackedbarseries();
44 void type_data();
44 void type_data();
45 void type();
45 void type();
46 void setLabelsFormat();
46 void setLabelsFormat();
47 void setLabelsPosition();
47 void setLabelsPosition();
48 void setLabelsAngle();
48 void mouseclicked_data();
49 void mouseclicked_data();
49 void mouseclicked();
50 void mouseclicked();
50 void mousehovered_data();
51 void mousehovered_data();
51 void mousehovered();
52 void mousehovered();
52 void mousePressed();
53 void mousePressed();
53 void mouseReleased();
54 void mouseReleased();
54 void mouseDoubleClicked();
55 void mouseDoubleClicked();
55
56
56 private:
57 private:
57 QHorizontalStackedBarSeries* m_barseries;
58 QHorizontalStackedBarSeries* m_barseries;
58 };
59 };
59
60
60 void tst_QHorizontalStackedBarSeries::initTestCase()
61 void tst_QHorizontalStackedBarSeries::initTestCase()
61 {
62 {
62 qRegisterMetaType<QBarSet*>("QBarSet*");
63 qRegisterMetaType<QBarSet*>("QBarSet*");
63 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
64 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
64 }
65 }
65
66
66 void tst_QHorizontalStackedBarSeries::cleanupTestCase()
67 void tst_QHorizontalStackedBarSeries::cleanupTestCase()
67 {
68 {
68 QTest::qWait(1); // Allow final deleteLaters to run
69 QTest::qWait(1); // Allow final deleteLaters to run
69 }
70 }
70
71
71 void tst_QHorizontalStackedBarSeries::init()
72 void tst_QHorizontalStackedBarSeries::init()
72 {
73 {
73 m_barseries = new QHorizontalStackedBarSeries();
74 m_barseries = new QHorizontalStackedBarSeries();
74 }
75 }
75
76
76 void tst_QHorizontalStackedBarSeries::cleanup()
77 void tst_QHorizontalStackedBarSeries::cleanup()
77 {
78 {
78 delete m_barseries;
79 delete m_barseries;
79 m_barseries = 0;
80 m_barseries = 0;
80 }
81 }
81
82
82 void tst_QHorizontalStackedBarSeries::qhorizontalstackedbarseries_data()
83 void tst_QHorizontalStackedBarSeries::qhorizontalstackedbarseries_data()
83 {
84 {
84 }
85 }
85
86
86 void tst_QHorizontalStackedBarSeries::qhorizontalstackedbarseries()
87 void tst_QHorizontalStackedBarSeries::qhorizontalstackedbarseries()
87 {
88 {
88 QHorizontalStackedBarSeries *barseries = new QHorizontalStackedBarSeries();
89 QHorizontalStackedBarSeries *barseries = new QHorizontalStackedBarSeries();
89 QVERIFY(barseries != 0);
90 QVERIFY(barseries != 0);
90 delete barseries;
91 delete barseries;
91 }
92 }
92
93
93 void tst_QHorizontalStackedBarSeries::type_data()
94 void tst_QHorizontalStackedBarSeries::type_data()
94 {
95 {
95
96
96 }
97 }
97
98
98 void tst_QHorizontalStackedBarSeries::type()
99 void tst_QHorizontalStackedBarSeries::type()
99 {
100 {
100 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeHorizontalStackedBar);
101 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeHorizontalStackedBar);
101 }
102 }
102
103
103 void tst_QHorizontalStackedBarSeries::setLabelsFormat()
104 void tst_QHorizontalStackedBarSeries::setLabelsFormat()
104 {
105 {
105 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
106 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
106 QCOMPARE(m_barseries->labelsFormat(), QString());
107 QCOMPARE(m_barseries->labelsFormat(), QString());
107
108
108 QString format("(@value)");
109 QString format("(@value)");
109 m_barseries->setLabelsFormat(format);
110 m_barseries->setLabelsFormat(format);
110 TRY_COMPARE(labelsFormatSpy.count(), 1);
111 TRY_COMPARE(labelsFormatSpy.count(), 1);
111 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
112 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
112 QVERIFY(arguments.at(0).toString() == format);
113 QVERIFY(arguments.at(0).toString() == format);
113 QCOMPARE(m_barseries->labelsFormat(), format);
114 QCOMPARE(m_barseries->labelsFormat(), format);
114
115
115 m_barseries->setLabelsFormat(QString());
116 m_barseries->setLabelsFormat(QString());
116 TRY_COMPARE(labelsFormatSpy.count(), 1);
117 TRY_COMPARE(labelsFormatSpy.count(), 1);
117 arguments = labelsFormatSpy.takeFirst();
118 arguments = labelsFormatSpy.takeFirst();
118 QVERIFY(arguments.at(0).toString() == QString());
119 QVERIFY(arguments.at(0).toString() == QString());
119 QCOMPARE(m_barseries->labelsFormat(), QString());
120 QCOMPARE(m_barseries->labelsFormat(), QString());
120 }
121 }
121
122
122 void tst_QHorizontalStackedBarSeries::setLabelsPosition()
123 void tst_QHorizontalStackedBarSeries::setLabelsPosition()
123 {
124 {
124 QSignalSpy labelsPositionSpy(m_barseries,
125 QSignalSpy labelsPositionSpy(m_barseries,
125 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
126 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
126 QCOMPARE(m_barseries->labelsPosition(), QHorizontalStackedBarSeries::LabelsCenter);
127 QCOMPARE(m_barseries->labelsPosition(), QHorizontalStackedBarSeries::LabelsCenter);
127
128
128 m_barseries->setLabelsPosition(QHorizontalStackedBarSeries::LabelsInsideEnd);
129 m_barseries->setLabelsPosition(QHorizontalStackedBarSeries::LabelsInsideEnd);
129 TRY_COMPARE(labelsPositionSpy.count(), 1);
130 TRY_COMPARE(labelsPositionSpy.count(), 1);
130 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
131 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
131 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
132 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
132 == QHorizontalStackedBarSeries::LabelsInsideEnd);
133 == QHorizontalStackedBarSeries::LabelsInsideEnd);
133 QCOMPARE(m_barseries->labelsPosition(), QHorizontalStackedBarSeries::LabelsInsideEnd);
134 QCOMPARE(m_barseries->labelsPosition(), QHorizontalStackedBarSeries::LabelsInsideEnd);
134
135
135 m_barseries->setLabelsPosition(QHorizontalStackedBarSeries::LabelsInsideBase);
136 m_barseries->setLabelsPosition(QHorizontalStackedBarSeries::LabelsInsideBase);
136 TRY_COMPARE(labelsPositionSpy.count(), 1);
137 TRY_COMPARE(labelsPositionSpy.count(), 1);
137 arguments = labelsPositionSpy.takeFirst();
138 arguments = labelsPositionSpy.takeFirst();
138 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
139 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
139 == QHorizontalStackedBarSeries::LabelsInsideBase);
140 == QHorizontalStackedBarSeries::LabelsInsideBase);
140 QCOMPARE(m_barseries->labelsPosition(), QHorizontalStackedBarSeries::LabelsInsideBase);
141 QCOMPARE(m_barseries->labelsPosition(), QHorizontalStackedBarSeries::LabelsInsideBase);
141
142
142 m_barseries->setLabelsPosition(QHorizontalStackedBarSeries::LabelsOutsideEnd);
143 m_barseries->setLabelsPosition(QHorizontalStackedBarSeries::LabelsOutsideEnd);
143 TRY_COMPARE(labelsPositionSpy.count(), 1);
144 TRY_COMPARE(labelsPositionSpy.count(), 1);
144 arguments = labelsPositionSpy.takeFirst();
145 arguments = labelsPositionSpy.takeFirst();
145 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
146 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
146 == QHorizontalStackedBarSeries::LabelsOutsideEnd);
147 == QHorizontalStackedBarSeries::LabelsOutsideEnd);
147 QCOMPARE(m_barseries->labelsPosition(), QHorizontalStackedBarSeries::LabelsOutsideEnd);
148 QCOMPARE(m_barseries->labelsPosition(), QHorizontalStackedBarSeries::LabelsOutsideEnd);
148
149
149 m_barseries->setLabelsPosition(QHorizontalStackedBarSeries::LabelsCenter);
150 m_barseries->setLabelsPosition(QHorizontalStackedBarSeries::LabelsCenter);
150 TRY_COMPARE(labelsPositionSpy.count(), 1);
151 TRY_COMPARE(labelsPositionSpy.count(), 1);
151 arguments = labelsPositionSpy.takeFirst();
152 arguments = labelsPositionSpy.takeFirst();
152 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
153 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
153 == QHorizontalStackedBarSeries::LabelsCenter);
154 == QHorizontalStackedBarSeries::LabelsCenter);
154 QCOMPARE(m_barseries->labelsPosition(), QHorizontalStackedBarSeries::LabelsCenter);
155 QCOMPARE(m_barseries->labelsPosition(), QHorizontalStackedBarSeries::LabelsCenter);
155 }
156 }
156
157
158 void tst_QHorizontalStackedBarSeries::setLabelsAngle()
159 {
160 QSignalSpy labelsAngleSpy(m_barseries,
161 SIGNAL(labelsAngleChanged(qreal)));
162 QCOMPARE(m_barseries->labelsAngle(), 0.0);
163
164 m_barseries->setLabelsAngle(55.0);
165 TRY_COMPARE(labelsAngleSpy.count(), 1);
166 QList<QVariant> arguments = labelsAngleSpy.takeFirst();
167 QVERIFY(arguments.at(0).value<qreal>() == 55.0);
168 QCOMPARE(m_barseries->labelsAngle(), 55.0);
169 }
170
157 void tst_QHorizontalStackedBarSeries::mouseclicked_data()
171 void tst_QHorizontalStackedBarSeries::mouseclicked_data()
158 {
172 {
159
173
160 }
174 }
161
175
162 void tst_QHorizontalStackedBarSeries::mouseclicked()
176 void tst_QHorizontalStackedBarSeries::mouseclicked()
163 {
177 {
164 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
178 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
165
179
166 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
180 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
167
181
168 QBarSet* set1 = new QBarSet(QString("set 1"));
182 QBarSet* set1 = new QBarSet(QString("set 1"));
169 *set1 << 10 << 10 << 10;
183 *set1 << 10 << 10 << 10;
170 series->append(set1);
184 series->append(set1);
171
185
172 QBarSet* set2 = new QBarSet(QString("set 2"));
186 QBarSet* set2 = new QBarSet(QString("set 2"));
173 *set2 << 10 << 10 << 10;
187 *set2 << 10 << 10 << 10;
174 series->append(set2);
188 series->append(set2);
175
189
176 QList<QBarSet*> barSets = series->barSets();
190 QList<QBarSet*> barSets = series->barSets();
177
191
178 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
192 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
179
193
180 QChartView view(new QChart());
194 QChartView view(new QChart());
181 view.resize(400,300);
195 view.resize(400,300);
182 view.chart()->addSeries(series);
196 view.chart()->addSeries(series);
183 view.show();
197 view.show();
184 QTest::qWaitForWindowShown(&view);
198 QTest::qWaitForWindowShown(&view);
185
199
186 // Calculate expected layout for bars
200 // Calculate expected layout for bars
187 QRectF plotArea = view.chart()->plotArea();
201 QRectF plotArea = view.chart()->plotArea();
188 qreal width = plotArea.width();
202 qreal width = plotArea.width();
189 qreal height = plotArea.height();
203 qreal height = plotArea.height();
190 qreal rangeY = 3; // 3 values per set
204 qreal rangeY = 3; // 3 values per set
191 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
205 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
192 qreal scaleY = (height / rangeY);
206 qreal scaleY = (height / rangeY);
193 qreal scaleX = (width / rangeX);
207 qreal scaleX = (width / rangeX);
194
208
195 qreal setCount = series->count();
209 qreal setCount = series->count();
196 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
210 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
197 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
211 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
198 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
212 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
199
213
200 QVector<QRectF> layout;
214 QVector<QRectF> layout;
201
215
202 // 3 = count of values in set
216 // 3 = count of values in set
203 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
217 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
204 for (int i = 0; i < 3; i++) {
218 for (int i = 0; i < 3; i++) {
205 qreal xMax = -scaleX * domainMinX + plotArea.left();
219 qreal xMax = -scaleX * domainMinX + plotArea.left();
206 qreal xMin = -scaleX * domainMinX + plotArea.left();
220 qreal xMin = -scaleX * domainMinX + plotArea.left();
207 for (int set = 0; set < setCount; set++) {
221 for (int set = 0; set < setCount; set++) {
208 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
222 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
209 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
223 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
210 if (rectWidth > 0) {
224 if (rectWidth > 0) {
211 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
225 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
212 layout.append(rect);
226 layout.append(rect);
213 xMax += rectWidth;
227 xMax += rectWidth;
214 } else {
228 } else {
215 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
229 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
216 layout.append(rect);
230 layout.append(rect);
217 xMin += rectWidth;
231 xMin += rectWidth;
218 }
232 }
219 }
233 }
220 }
234 }
221
235
222 //====================================================================================
236 //====================================================================================
223 // barset 1, bar 0
237 // barset 1, bar 0
224 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
238 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
225 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
239 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
226
240
227 QCOMPARE(seriesSpy.count(), 1);
241 QCOMPARE(seriesSpy.count(), 1);
228
242
229 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
243 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
230 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
244 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
231 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
245 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
232 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
246 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
233
247
234 //====================================================================================
248 //====================================================================================
235 // barset 1, bar 1
249 // barset 1, bar 1
236 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
250 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
237 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
251 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
238
252
239 QCOMPARE(seriesSpy.count(), 1);
253 QCOMPARE(seriesSpy.count(), 1);
240
254
241 seriesSpyArg = seriesSpy.takeFirst();
255 seriesSpyArg = seriesSpy.takeFirst();
242 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
256 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
243 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
257 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
244 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
258 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
245
259
246 //====================================================================================
260 //====================================================================================
247 // barset 1, bar 2
261 // barset 1, bar 2
248 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
262 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
249 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
263 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
250
264
251 QCOMPARE(seriesSpy.count(), 1);
265 QCOMPARE(seriesSpy.count(), 1);
252
266
253 seriesSpyArg = seriesSpy.takeFirst();
267 seriesSpyArg = seriesSpy.takeFirst();
254 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
268 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
255 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
269 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
256 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
270 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
257
271
258 //====================================================================================
272 //====================================================================================
259 // barset 2, bar 0
273 // barset 2, bar 0
260 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
274 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
261 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
275 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
262
276
263 QCOMPARE(seriesSpy.count(), 1);
277 QCOMPARE(seriesSpy.count(), 1);
264
278
265 seriesSpyArg = seriesSpy.takeFirst();
279 seriesSpyArg = seriesSpy.takeFirst();
266 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
280 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
267 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
281 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
268 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
282 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
269
283
270 //====================================================================================
284 //====================================================================================
271 // barset 2, bar 1
285 // barset 2, bar 1
272 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
286 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
273 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
287 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
274
288
275 QCOMPARE(seriesSpy.count(), 1);
289 QCOMPARE(seriesSpy.count(), 1);
276
290
277 seriesSpyArg = seriesSpy.takeFirst();
291 seriesSpyArg = seriesSpy.takeFirst();
278 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
292 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
279 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
293 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
280 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
294 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
281
295
282 //====================================================================================
296 //====================================================================================
283 // barset 2, bar 2
297 // barset 2, bar 2
284 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
298 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
285 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
299 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
286
300
287 QCOMPARE(seriesSpy.count(), 1);
301 QCOMPARE(seriesSpy.count(), 1);
288
302
289 seriesSpyArg = seriesSpy.takeFirst();
303 seriesSpyArg = seriesSpy.takeFirst();
290 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
304 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
291 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
305 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
292 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
306 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
293 }
307 }
294
308
295 void tst_QHorizontalStackedBarSeries::mousehovered_data()
309 void tst_QHorizontalStackedBarSeries::mousehovered_data()
296 {
310 {
297
311
298 }
312 }
299
313
300 void tst_QHorizontalStackedBarSeries::mousehovered()
314 void tst_QHorizontalStackedBarSeries::mousehovered()
301 {
315 {
302 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
316 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
303
317
304 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
318 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
305
319
306 QBarSet* set1 = new QBarSet(QString("set 1"));
320 QBarSet* set1 = new QBarSet(QString("set 1"));
307 *set1 << 10 << 10 << 10;
321 *set1 << 10 << 10 << 10;
308 series->append(set1);
322 series->append(set1);
309
323
310 QBarSet* set2 = new QBarSet(QString("set 2"));
324 QBarSet* set2 = new QBarSet(QString("set 2"));
311 *set2 << 10 << 10 << 10;
325 *set2 << 10 << 10 << 10;
312 series->append(set2);
326 series->append(set2);
313
327
314 QList<QBarSet*> barSets = series->barSets();
328 QList<QBarSet*> barSets = series->barSets();
315
329
316 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
330 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
317 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
331 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
318 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
332 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
319
333
320 QChartView view(new QChart());
334 QChartView view(new QChart());
321 view.resize(400,300);
335 view.resize(400,300);
322 view.chart()->addSeries(series);
336 view.chart()->addSeries(series);
323 view.show();
337 view.show();
324 QTest::qWaitForWindowShown(&view);
338 QTest::qWaitForWindowShown(&view);
325
339
326 //this is hack since view does not get events otherwise
340 //this is hack since view does not get events otherwise
327 view.setMouseTracking(true);
341 view.setMouseTracking(true);
328
342
329 // Calculate expected layout for bars
343 // Calculate expected layout for bars
330 QRectF plotArea = view.chart()->plotArea();
344 QRectF plotArea = view.chart()->plotArea();
331 qreal width = plotArea.width();
345 qreal width = plotArea.width();
332 qreal height = plotArea.height();
346 qreal height = plotArea.height();
333 qreal rangeY = 3; // 3 values per set
347 qreal rangeY = 3; // 3 values per set
334 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
348 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
335 qreal scaleY = (height / rangeY);
349 qreal scaleY = (height / rangeY);
336 qreal scaleX = (width / rangeX);
350 qreal scaleX = (width / rangeX);
337
351
338 qreal setCount = series->count();
352 qreal setCount = series->count();
339 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
353 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
340 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
354 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
341 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
355 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
342
356
343 QVector<QRectF> layout;
357 QVector<QRectF> layout;
344
358
345 // 3 = count of values in set
359 // 3 = count of values in set
346 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
360 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
347 for (int i = 0; i < 3; i++) {
361 for (int i = 0; i < 3; i++) {
348 qreal xMax = -scaleX * domainMinX + plotArea.left();
362 qreal xMax = -scaleX * domainMinX + plotArea.left();
349 qreal xMin = -scaleX * domainMinX + plotArea.left();
363 qreal xMin = -scaleX * domainMinX + plotArea.left();
350 for (int set = 0; set < setCount; set++) {
364 for (int set = 0; set < setCount; set++) {
351 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
365 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
352 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
366 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
353 if (rectWidth > 0) {
367 if (rectWidth > 0) {
354 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
368 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
355 layout.append(rect);
369 layout.append(rect);
356 xMax += rectWidth;
370 xMax += rectWidth;
357 } else {
371 } else {
358 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
372 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
359 layout.append(rect);
373 layout.append(rect);
360 xMin += rectWidth;
374 xMin += rectWidth;
361 }
375 }
362 }
376 }
363 }
377 }
364
378
365 //=======================================================================
379 //=======================================================================
366 // move mouse to left border
380 // move mouse to left border
367 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(4).center().y()));
381 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(4).center().y()));
368 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
382 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
369 TRY_COMPARE(seriesIndexSpy.count(), 0);
383 TRY_COMPARE(seriesIndexSpy.count(), 0);
370
384
371 //=======================================================================
385 //=======================================================================
372 // move mouse on top of set1
386 // move mouse on top of set1
373 QTest::mouseMove(view.viewport(), layout.at(4).center().toPoint());
387 QTest::mouseMove(view.viewport(), layout.at(4).center().toPoint());
374 TRY_COMPARE(seriesIndexSpy.count(), 1);
388 TRY_COMPARE(seriesIndexSpy.count(), 1);
375 TRY_COMPARE(setIndexSpy1.count(), 1);
389 TRY_COMPARE(setIndexSpy1.count(), 1);
376 TRY_COMPARE(setIndexSpy2.count(), 0);
390 TRY_COMPARE(setIndexSpy2.count(), 0);
377
391
378 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
392 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
379 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
393 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
380 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
394 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
381 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
395 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
382
396
383 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
397 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
384 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
398 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
385 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
399 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
386
400
387 //=======================================================================
401 //=======================================================================
388 // move mouse from top of set1 to top of set2
402 // move mouse from top of set1 to top of set2
389 QTest::mouseMove(view.viewport(), layout.at(5).center().toPoint());
403 QTest::mouseMove(view.viewport(), layout.at(5).center().toPoint());
390 TRY_COMPARE(seriesIndexSpy.count(), 2);
404 TRY_COMPARE(seriesIndexSpy.count(), 2);
391 TRY_COMPARE(setIndexSpy1.count(), 1);
405 TRY_COMPARE(setIndexSpy1.count(), 1);
392 TRY_COMPARE(setIndexSpy2.count(), 1);
406 TRY_COMPARE(setIndexSpy2.count(), 1);
393
407
394 // should leave set1
408 // should leave set1
395 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
409 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
396 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
410 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
397 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
411 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
398 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
412 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
399
413
400 setIndexSpyArg = setIndexSpy1.takeFirst();
414 setIndexSpyArg = setIndexSpy1.takeFirst();
401 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
415 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
402 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
416 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
403
417
404 // should enter set2
418 // should enter set2
405 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
419 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
406 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
420 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
407 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
421 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
408 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
422 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
409
423
410 setIndexSpyArg = setIndexSpy2.takeFirst();
424 setIndexSpyArg = setIndexSpy2.takeFirst();
411 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
425 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
412 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
426 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
413
427
414 //=======================================================================
428 //=======================================================================
415 // move mouse from top of set2 to background
429 // move mouse from top of set2 to background
416 QTest::mouseMove(view.viewport(), QPoint(layout.at(5).center().y(), 0));
430 QTest::mouseMove(view.viewport(), QPoint(layout.at(5).center().y(), 0));
417 TRY_COMPARE(seriesIndexSpy.count(), 1);
431 TRY_COMPARE(seriesIndexSpy.count(), 1);
418 TRY_COMPARE(setIndexSpy1.count(), 0);
432 TRY_COMPARE(setIndexSpy1.count(), 0);
419 TRY_COMPARE(setIndexSpy2.count(), 1);
433 TRY_COMPARE(setIndexSpy2.count(), 1);
420
434
421 // should leave set2
435 // should leave set2
422 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
436 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
423 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
437 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
424 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
438 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
425 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
439 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
426
440
427 setIndexSpyArg = setIndexSpy2.takeFirst();
441 setIndexSpyArg = setIndexSpy2.takeFirst();
428 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
442 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
429 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
443 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
430
444
431 //=======================================================================
445 //=======================================================================
432 // move mouse on top of set1, bar0 to check the index (hover into set1)
446 // move mouse on top of set1, bar0 to check the index (hover into set1)
433 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
447 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
434
448
435 TRY_COMPARE(seriesIndexSpy.count(), 1);
449 TRY_COMPARE(seriesIndexSpy.count(), 1);
436 TRY_COMPARE(setIndexSpy1.count(), 1);
450 TRY_COMPARE(setIndexSpy1.count(), 1);
437 TRY_COMPARE(setIndexSpy2.count(), 0);
451 TRY_COMPARE(setIndexSpy2.count(), 0);
438
452
439 //should enter set1, bar0
453 //should enter set1, bar0
440 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
454 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
441 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
455 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
442 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
456 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
443 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
457 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
444 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
458 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
445 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
459 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
446
460
447 setIndexSpyArg = setIndexSpy1.takeFirst();
461 setIndexSpyArg = setIndexSpy1.takeFirst();
448 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
462 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
449 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
463 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
450 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
464 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
451 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
465 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
452
466
453 //=======================================================================
467 //=======================================================================
454 // move mouse on top of set2, bar0 to check the index (hover out set1,
468 // move mouse on top of set2, bar0 to check the index (hover out set1,
455 // hover in set2)
469 // hover in set2)
456 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
470 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
457 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
471 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
458
472
459 TRY_COMPARE(seriesIndexSpy.count(), 2);
473 TRY_COMPARE(seriesIndexSpy.count(), 2);
460 TRY_COMPARE(setIndexSpy1.count(), 1);
474 TRY_COMPARE(setIndexSpy1.count(), 1);
461 TRY_COMPARE(setIndexSpy2.count(), 1);
475 TRY_COMPARE(setIndexSpy2.count(), 1);
462
476
463 //should leave set1, bar0
477 //should leave set1, bar0
464 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
478 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
465 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
479 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
466 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
480 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
467 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
481 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
468 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
482 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
469 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
483 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
470
484
471 setIndexSpyArg = setIndexSpy1.takeFirst();
485 setIndexSpyArg = setIndexSpy1.takeFirst();
472 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
486 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
473 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
487 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
474 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
488 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
475 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
489 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
476
490
477 //should enter set2, bar0
491 //should enter set2, bar0
478 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
492 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
479 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
493 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
480 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
494 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
481 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
495 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
482 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
496 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
483 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
497 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
484
498
485 setIndexSpyArg = setIndexSpy2.takeFirst();
499 setIndexSpyArg = setIndexSpy2.takeFirst();
486 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
500 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
487 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
501 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
488 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
502 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
489 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
503 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
490
504
491 //=======================================================================
505 //=======================================================================
492 // move mouse on top of set1, bar1 to check the index (hover out set2,
506 // move mouse on top of set1, bar1 to check the index (hover out set2,
493 // hover in set1)
507 // hover in set1)
494 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
508 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
495
509
496 TRY_COMPARE(seriesIndexSpy.count(), 2);
510 TRY_COMPARE(seriesIndexSpy.count(), 2);
497 TRY_COMPARE(setIndexSpy1.count(), 1);
511 TRY_COMPARE(setIndexSpy1.count(), 1);
498 TRY_COMPARE(setIndexSpy2.count(), 1);
512 TRY_COMPARE(setIndexSpy2.count(), 1);
499
513
500 //should leave set2, bar0
514 //should leave set2, bar0
501 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
515 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
502 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
516 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
503 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
517 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
504 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
518 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
505 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
519 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
506 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
520 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
507
521
508 setIndexSpyArg = setIndexSpy2.takeFirst();
522 setIndexSpyArg = setIndexSpy2.takeFirst();
509 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
523 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
510 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
524 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
511 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
525 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
512 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
526 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
513
527
514 //should enter set1, bar1
528 //should enter set1, bar1
515 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
529 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
516 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
530 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
517 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
531 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
518 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
532 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
519 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
533 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
520 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
534 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
521
535
522 setIndexSpyArg = setIndexSpy1.takeFirst();
536 setIndexSpyArg = setIndexSpy1.takeFirst();
523 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
537 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
524 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
538 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
525 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
539 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
526 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
540 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
527
541
528 //=======================================================================
542 //=======================================================================
529 // move mouse between set1 and set2 (hover out set1)
543 // move mouse between set1 and set2 (hover out set1)
530 QTest::mouseMove(view.viewport(), QPoint(layout.at(3).left(),
544 QTest::mouseMove(view.viewport(), QPoint(layout.at(3).left(),
531 (layout.at(3).top() + layout.at(4).bottom()) / 2));
545 (layout.at(3).top() + layout.at(4).bottom()) / 2));
532
546
533 TRY_COMPARE(seriesIndexSpy.count(), 1);
547 TRY_COMPARE(seriesIndexSpy.count(), 1);
534 TRY_COMPARE(setIndexSpy1.count(), 1);
548 TRY_COMPARE(setIndexSpy1.count(), 1);
535 TRY_COMPARE(setIndexSpy2.count(), 0);
549 TRY_COMPARE(setIndexSpy2.count(), 0);
536
550
537 //should leave set1, bar1
551 //should leave set1, bar1
538 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
552 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
539 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
553 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
540 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
554 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
541 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
555 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
542 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
556 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
543 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
557 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
544
558
545 setIndexSpyArg = setIndexSpy1.takeFirst();
559 setIndexSpyArg = setIndexSpy1.takeFirst();
546 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
560 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
547 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
561 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
548 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
562 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
549 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
563 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
550
564
551 //=======================================================================
565 //=======================================================================
552 // move mouse on top of set2, bar1 to check the index (hover in set2)
566 // move mouse on top of set2, bar1 to check the index (hover in set2)
553 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
567 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
554
568
555 TRY_COMPARE(seriesIndexSpy.count(), 1);
569 TRY_COMPARE(seriesIndexSpy.count(), 1);
556 TRY_COMPARE(setIndexSpy1.count(), 0);
570 TRY_COMPARE(setIndexSpy1.count(), 0);
557 TRY_COMPARE(setIndexSpy2.count(), 1);
571 TRY_COMPARE(setIndexSpy2.count(), 1);
558
572
559 //should enter set2, bar1
573 //should enter set2, bar1
560 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
574 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
561 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
575 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
562 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
576 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
563 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
577 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
564 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
578 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
565 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
579 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
566
580
567 setIndexSpyArg = setIndexSpy2.takeFirst();
581 setIndexSpyArg = setIndexSpy2.takeFirst();
568 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
582 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
569 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
583 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
570 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
584 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
571 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
585 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
572
586
573 //=======================================================================
587 //=======================================================================
574 // move mouse between set1 and set2 (hover out set2)
588 // move mouse between set1 and set2 (hover out set2)
575 QTest::mouseMove(view.viewport(), QPoint(layout.at(3).left(),
589 QTest::mouseMove(view.viewport(), QPoint(layout.at(3).left(),
576 (layout.at(3).top() + layout.at(4).bottom()) / 2));
590 (layout.at(3).top() + layout.at(4).bottom()) / 2));
577
591
578 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
592 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
579 TRY_COMPARE(seriesIndexSpy.count(), 1);
593 TRY_COMPARE(seriesIndexSpy.count(), 1);
580 TRY_COMPARE(setIndexSpy1.count(), 0);
594 TRY_COMPARE(setIndexSpy1.count(), 0);
581 TRY_COMPARE(setIndexSpy2.count(), 1);
595 TRY_COMPARE(setIndexSpy2.count(), 1);
582
596
583 //should leave set1, bar1
597 //should leave set1, bar1
584 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
598 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
585 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
599 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
586 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
600 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
587 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
601 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
588 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
602 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
589 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
603 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
590
604
591 setIndexSpyArg = setIndexSpy2.takeFirst();
605 setIndexSpyArg = setIndexSpy2.takeFirst();
592 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
606 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
593 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
607 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
594 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
608 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
595 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
609 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
596 }
610 }
597
611
598 void tst_QHorizontalStackedBarSeries::mousePressed()
612 void tst_QHorizontalStackedBarSeries::mousePressed()
599 {
613 {
600 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
614 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
601
615
602 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
616 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
603
617
604 QBarSet* set1 = new QBarSet(QString("set 1"));
618 QBarSet* set1 = new QBarSet(QString("set 1"));
605 *set1 << 10 << 10 << 10;
619 *set1 << 10 << 10 << 10;
606 series->append(set1);
620 series->append(set1);
607
621
608 QBarSet* set2 = new QBarSet(QString("set 2"));
622 QBarSet* set2 = new QBarSet(QString("set 2"));
609 *set2 << 10 << 10 << 10;
623 *set2 << 10 << 10 << 10;
610 series->append(set2);
624 series->append(set2);
611 QList<QBarSet*> barSets = series->barSets();
625 QList<QBarSet*> barSets = series->barSets();
612
626
613 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
627 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
614 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
628 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
615 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
629 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
616
630
617 QChartView view(new QChart());
631 QChartView view(new QChart());
618 view.resize(400,300);
632 view.resize(400,300);
619 view.chart()->addSeries(series);
633 view.chart()->addSeries(series);
620 view.show();
634 view.show();
621 QTest::qWaitForWindowShown(&view);
635 QTest::qWaitForWindowShown(&view);
622
636
623 // Calculate expected layout for bars
637 // Calculate expected layout for bars
624 QRectF plotArea = view.chart()->plotArea();
638 QRectF plotArea = view.chart()->plotArea();
625 qreal width = plotArea.width();
639 qreal width = plotArea.width();
626 qreal height = plotArea.height();
640 qreal height = plotArea.height();
627 qreal rangeY = 3; // 3 values per set
641 qreal rangeY = 3; // 3 values per set
628 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
642 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
629 qreal scaleY = (height / rangeY);
643 qreal scaleY = (height / rangeY);
630 qreal scaleX = (width / rangeX);
644 qreal scaleX = (width / rangeX);
631
645
632 qreal setCount = series->count();
646 qreal setCount = series->count();
633 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
647 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
634 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
648 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
635 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
649 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
636
650
637 QVector<QRectF> layout;
651 QVector<QRectF> layout;
638
652
639 // 3 = count of values in set
653 // 3 = count of values in set
640 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
654 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
641 for (int i = 0; i < 3; i++) {
655 for (int i = 0; i < 3; i++) {
642 qreal xMax = -scaleX * domainMinX + plotArea.left();
656 qreal xMax = -scaleX * domainMinX + plotArea.left();
643 qreal xMin = -scaleX * domainMinX + plotArea.left();
657 qreal xMin = -scaleX * domainMinX + plotArea.left();
644 for (int set = 0; set < setCount; set++) {
658 for (int set = 0; set < setCount; set++) {
645 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
659 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
646 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
660 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
647 if (rectWidth > 0) {
661 if (rectWidth > 0) {
648 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
662 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
649 layout.append(rect);
663 layout.append(rect);
650 xMax += rectWidth;
664 xMax += rectWidth;
651 } else {
665 } else {
652 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
666 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
653 layout.append(rect);
667 layout.append(rect);
654 xMin += rectWidth;
668 xMin += rectWidth;
655 }
669 }
656 }
670 }
657 }
671 }
658
672
659 //====================================================================================
673 //====================================================================================
660 // barset 1, bar 0
674 // barset 1, bar 0
661 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
675 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
662 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
676 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
663
677
664 QCOMPARE(seriesSpy.count(), 1);
678 QCOMPARE(seriesSpy.count(), 1);
665 QCOMPARE(setSpy1.count(), 1);
679 QCOMPARE(setSpy1.count(), 1);
666 QCOMPARE(setSpy2.count(), 0);
680 QCOMPARE(setSpy2.count(), 0);
667
681
668 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
682 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
669 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
683 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
670 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
684 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
671 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
685 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
672
686
673 QList<QVariant> setSpyArg = setSpy1.takeFirst();
687 QList<QVariant> setSpyArg = setSpy1.takeFirst();
674 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
688 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
675 QVERIFY(setSpyArg.at(0).toInt() == 0);
689 QVERIFY(setSpyArg.at(0).toInt() == 0);
676
690
677 //====================================================================================
691 //====================================================================================
678 // barset 1, bar 1
692 // barset 1, bar 1
679 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
693 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
680 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
694 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
681
695
682 QCOMPARE(seriesSpy.count(), 1);
696 QCOMPARE(seriesSpy.count(), 1);
683 QCOMPARE(setSpy1.count(), 1);
697 QCOMPARE(setSpy1.count(), 1);
684 QCOMPARE(setSpy2.count(), 0);
698 QCOMPARE(setSpy2.count(), 0);
685
699
686 seriesSpyArg = seriesSpy.takeFirst();
700 seriesSpyArg = seriesSpy.takeFirst();
687 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
701 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
688 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
702 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
689 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
703 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
690
704
691 setSpyArg = setSpy1.takeFirst();
705 setSpyArg = setSpy1.takeFirst();
692 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
706 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
693 QVERIFY(setSpyArg.at(0).toInt() == 1);
707 QVERIFY(setSpyArg.at(0).toInt() == 1);
694
708
695 //====================================================================================
709 //====================================================================================
696 // barset 1, bar 2
710 // barset 1, bar 2
697 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
711 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
698 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
712 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
699
713
700 QCOMPARE(seriesSpy.count(), 1);
714 QCOMPARE(seriesSpy.count(), 1);
701 QCOMPARE(setSpy1.count(), 1);
715 QCOMPARE(setSpy1.count(), 1);
702 QCOMPARE(setSpy2.count(), 0);
716 QCOMPARE(setSpy2.count(), 0);
703
717
704 seriesSpyArg = seriesSpy.takeFirst();
718 seriesSpyArg = seriesSpy.takeFirst();
705 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
719 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
706 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
720 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
707 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
721 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
708
722
709 setSpyArg = setSpy1.takeFirst();
723 setSpyArg = setSpy1.takeFirst();
710 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
724 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
711 QVERIFY(setSpyArg.at(0).toInt() == 2);
725 QVERIFY(setSpyArg.at(0).toInt() == 2);
712
726
713 //====================================================================================
727 //====================================================================================
714 // barset 2, bar 0
728 // barset 2, bar 0
715 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
729 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
716 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
730 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
717
731
718 QCOMPARE(seriesSpy.count(), 1);
732 QCOMPARE(seriesSpy.count(), 1);
719 QCOMPARE(setSpy1.count(), 0);
733 QCOMPARE(setSpy1.count(), 0);
720 QCOMPARE(setSpy2.count(), 1);
734 QCOMPARE(setSpy2.count(), 1);
721
735
722 seriesSpyArg = seriesSpy.takeFirst();
736 seriesSpyArg = seriesSpy.takeFirst();
723 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
737 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
724 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
738 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
725 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
739 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
726
740
727 setSpyArg = setSpy2.takeFirst();
741 setSpyArg = setSpy2.takeFirst();
728 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
742 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
729 QVERIFY(setSpyArg.at(0).toInt() == 0);
743 QVERIFY(setSpyArg.at(0).toInt() == 0);
730
744
731 //====================================================================================
745 //====================================================================================
732 // barset 2, bar 1
746 // barset 2, bar 1
733 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
747 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
734 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
748 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
735
749
736 QCOMPARE(seriesSpy.count(), 1);
750 QCOMPARE(seriesSpy.count(), 1);
737 QCOMPARE(setSpy1.count(), 0);
751 QCOMPARE(setSpy1.count(), 0);
738 QCOMPARE(setSpy2.count(), 1);
752 QCOMPARE(setSpy2.count(), 1);
739
753
740 seriesSpyArg = seriesSpy.takeFirst();
754 seriesSpyArg = seriesSpy.takeFirst();
741 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
755 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
742 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
756 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
743 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
757 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
744
758
745 setSpyArg = setSpy2.takeFirst();
759 setSpyArg = setSpy2.takeFirst();
746 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
760 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
747 QVERIFY(setSpyArg.at(0).toInt() == 1);
761 QVERIFY(setSpyArg.at(0).toInt() == 1);
748
762
749 //====================================================================================
763 //====================================================================================
750 // barset 2, bar 2
764 // barset 2, bar 2
751 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
765 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
752 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
766 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
753
767
754 QCOMPARE(seriesSpy.count(), 1);
768 QCOMPARE(seriesSpy.count(), 1);
755 QCOMPARE(setSpy1.count(), 0);
769 QCOMPARE(setSpy1.count(), 0);
756 QCOMPARE(setSpy2.count(), 1);
770 QCOMPARE(setSpy2.count(), 1);
757
771
758 seriesSpyArg = seriesSpy.takeFirst();
772 seriesSpyArg = seriesSpy.takeFirst();
759 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
773 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
760 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
774 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
761 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
775 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
762
776
763 setSpyArg = setSpy2.takeFirst();
777 setSpyArg = setSpy2.takeFirst();
764 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
778 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
765 QVERIFY(setSpyArg.at(0).toInt() == 2);
779 QVERIFY(setSpyArg.at(0).toInt() == 2);
766 }
780 }
767
781
768 void tst_QHorizontalStackedBarSeries::mouseReleased()
782 void tst_QHorizontalStackedBarSeries::mouseReleased()
769 {
783 {
770 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
784 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
771
785
772 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
786 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
773
787
774 QBarSet* set1 = new QBarSet(QString("set 1"));
788 QBarSet* set1 = new QBarSet(QString("set 1"));
775 *set1 << 10 << 10 << 10;
789 *set1 << 10 << 10 << 10;
776 series->append(set1);
790 series->append(set1);
777
791
778 QBarSet* set2 = new QBarSet(QString("set 2"));
792 QBarSet* set2 = new QBarSet(QString("set 2"));
779 *set2 << 10 << 10 << 10;
793 *set2 << 10 << 10 << 10;
780 series->append(set2);
794 series->append(set2);
781 QList<QBarSet*> barSets = series->barSets();
795 QList<QBarSet*> barSets = series->barSets();
782
796
783 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
797 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
784 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
798 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
785 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
799 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
786
800
787 QChartView view(new QChart());
801 QChartView view(new QChart());
788 view.resize(400,300);
802 view.resize(400,300);
789 view.chart()->addSeries(series);
803 view.chart()->addSeries(series);
790 view.show();
804 view.show();
791 QTest::qWaitForWindowShown(&view);
805 QTest::qWaitForWindowShown(&view);
792
806
793 // Calculate expected layout for bars
807 // Calculate expected layout for bars
794 QRectF plotArea = view.chart()->plotArea();
808 QRectF plotArea = view.chart()->plotArea();
795 qreal width = plotArea.width();
809 qreal width = plotArea.width();
796 qreal height = plotArea.height();
810 qreal height = plotArea.height();
797 qreal rangeY = 3; // 3 values per set
811 qreal rangeY = 3; // 3 values per set
798 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
812 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
799 qreal scaleY = (height / rangeY);
813 qreal scaleY = (height / rangeY);
800 qreal scaleX = (width / rangeX);
814 qreal scaleX = (width / rangeX);
801
815
802 qreal setCount = series->count();
816 qreal setCount = series->count();
803 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
817 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
804 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
818 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
805 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
819 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
806
820
807 QVector<QRectF> layout;
821 QVector<QRectF> layout;
808
822
809 // 3 = count of values in set
823 // 3 = count of values in set
810 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
824 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
811 for (int i = 0; i < 3; i++) {
825 for (int i = 0; i < 3; i++) {
812 qreal xMax = -scaleX * domainMinX + plotArea.left();
826 qreal xMax = -scaleX * domainMinX + plotArea.left();
813 qreal xMin = -scaleX * domainMinX + plotArea.left();
827 qreal xMin = -scaleX * domainMinX + plotArea.left();
814 for (int set = 0; set < setCount; set++) {
828 for (int set = 0; set < setCount; set++) {
815 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
829 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
816 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
830 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
817 if (rectWidth > 0) {
831 if (rectWidth > 0) {
818 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
832 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
819 layout.append(rect);
833 layout.append(rect);
820 xMax += rectWidth;
834 xMax += rectWidth;
821 } else {
835 } else {
822 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
836 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
823 layout.append(rect);
837 layout.append(rect);
824 xMin += rectWidth;
838 xMin += rectWidth;
825 }
839 }
826 }
840 }
827 }
841 }
828
842
829 //====================================================================================
843 //====================================================================================
830 // barset 1, bar 0
844 // barset 1, bar 0
831 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
845 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
832 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
846 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
833
847
834 QCOMPARE(seriesSpy.count(), 1);
848 QCOMPARE(seriesSpy.count(), 1);
835 QCOMPARE(setSpy1.count(), 1);
849 QCOMPARE(setSpy1.count(), 1);
836 QCOMPARE(setSpy2.count(), 0);
850 QCOMPARE(setSpy2.count(), 0);
837
851
838 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
852 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
839 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
853 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
840 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
854 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
841 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
855 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
842
856
843 QList<QVariant> setSpyArg = setSpy1.takeFirst();
857 QList<QVariant> setSpyArg = setSpy1.takeFirst();
844 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
858 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
845 QVERIFY(setSpyArg.at(0).toInt() == 0);
859 QVERIFY(setSpyArg.at(0).toInt() == 0);
846
860
847 //====================================================================================
861 //====================================================================================
848 // barset 1, bar 1
862 // barset 1, bar 1
849 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
863 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
850 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
864 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
851
865
852 QCOMPARE(seriesSpy.count(), 1);
866 QCOMPARE(seriesSpy.count(), 1);
853 QCOMPARE(setSpy1.count(), 1);
867 QCOMPARE(setSpy1.count(), 1);
854 QCOMPARE(setSpy2.count(), 0);
868 QCOMPARE(setSpy2.count(), 0);
855
869
856 seriesSpyArg = seriesSpy.takeFirst();
870 seriesSpyArg = seriesSpy.takeFirst();
857 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
871 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
858 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
872 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
859 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
873 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
860
874
861 setSpyArg = setSpy1.takeFirst();
875 setSpyArg = setSpy1.takeFirst();
862 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
876 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
863 QVERIFY(setSpyArg.at(0).toInt() == 1);
877 QVERIFY(setSpyArg.at(0).toInt() == 1);
864
878
865 //====================================================================================
879 //====================================================================================
866 // barset 1, bar 2
880 // barset 1, bar 2
867 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
881 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
868 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
882 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
869
883
870 QCOMPARE(seriesSpy.count(), 1);
884 QCOMPARE(seriesSpy.count(), 1);
871 QCOMPARE(setSpy1.count(), 1);
885 QCOMPARE(setSpy1.count(), 1);
872 QCOMPARE(setSpy2.count(), 0);
886 QCOMPARE(setSpy2.count(), 0);
873
887
874 seriesSpyArg = seriesSpy.takeFirst();
888 seriesSpyArg = seriesSpy.takeFirst();
875 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
889 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
876 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
890 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
877 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
891 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
878
892
879 setSpyArg = setSpy1.takeFirst();
893 setSpyArg = setSpy1.takeFirst();
880 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
894 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
881 QVERIFY(setSpyArg.at(0).toInt() == 2);
895 QVERIFY(setSpyArg.at(0).toInt() == 2);
882
896
883 //====================================================================================
897 //====================================================================================
884 // barset 2, bar 0
898 // barset 2, bar 0
885 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
899 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
886 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
900 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
887
901
888 QCOMPARE(seriesSpy.count(), 1);
902 QCOMPARE(seriesSpy.count(), 1);
889 QCOMPARE(setSpy1.count(), 0);
903 QCOMPARE(setSpy1.count(), 0);
890 QCOMPARE(setSpy2.count(), 1);
904 QCOMPARE(setSpy2.count(), 1);
891
905
892 seriesSpyArg = seriesSpy.takeFirst();
906 seriesSpyArg = seriesSpy.takeFirst();
893 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
907 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
894 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
908 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
895 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
909 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
896
910
897 setSpyArg = setSpy2.takeFirst();
911 setSpyArg = setSpy2.takeFirst();
898 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
912 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
899 QVERIFY(setSpyArg.at(0).toInt() == 0);
913 QVERIFY(setSpyArg.at(0).toInt() == 0);
900
914
901 //====================================================================================
915 //====================================================================================
902 // barset 2, bar 1
916 // barset 2, bar 1
903 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
917 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
904 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
918 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
905
919
906 QCOMPARE(seriesSpy.count(), 1);
920 QCOMPARE(seriesSpy.count(), 1);
907 QCOMPARE(setSpy1.count(), 0);
921 QCOMPARE(setSpy1.count(), 0);
908 QCOMPARE(setSpy2.count(), 1);
922 QCOMPARE(setSpy2.count(), 1);
909
923
910 seriesSpyArg = seriesSpy.takeFirst();
924 seriesSpyArg = seriesSpy.takeFirst();
911 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
925 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
912 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
926 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
913 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
927 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
914
928
915 setSpyArg = setSpy2.takeFirst();
929 setSpyArg = setSpy2.takeFirst();
916 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
930 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
917 QVERIFY(setSpyArg.at(0).toInt() == 1);
931 QVERIFY(setSpyArg.at(0).toInt() == 1);
918
932
919 //====================================================================================
933 //====================================================================================
920 // barset 2, bar 2
934 // barset 2, bar 2
921 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
935 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
922 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
936 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
923
937
924 QCOMPARE(seriesSpy.count(), 1);
938 QCOMPARE(seriesSpy.count(), 1);
925 QCOMPARE(setSpy1.count(), 0);
939 QCOMPARE(setSpy1.count(), 0);
926 QCOMPARE(setSpy2.count(), 1);
940 QCOMPARE(setSpy2.count(), 1);
927
941
928 seriesSpyArg = seriesSpy.takeFirst();
942 seriesSpyArg = seriesSpy.takeFirst();
929 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
943 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
930 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
944 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
931 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
945 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
932
946
933 setSpyArg = setSpy2.takeFirst();
947 setSpyArg = setSpy2.takeFirst();
934 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
948 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
935 QVERIFY(setSpyArg.at(0).toInt() == 2);
949 QVERIFY(setSpyArg.at(0).toInt() == 2);
936 }
950 }
937
951
938 void tst_QHorizontalStackedBarSeries::mouseDoubleClicked()
952 void tst_QHorizontalStackedBarSeries::mouseDoubleClicked()
939 {
953 {
940 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
954 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
941
955
942 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
956 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
943
957
944 QBarSet* set1 = new QBarSet(QString("set 1"));
958 QBarSet* set1 = new QBarSet(QString("set 1"));
945 *set1 << 10 << 10 << 10;
959 *set1 << 10 << 10 << 10;
946 series->append(set1);
960 series->append(set1);
947
961
948 QBarSet* set2 = new QBarSet(QString("set 2"));
962 QBarSet* set2 = new QBarSet(QString("set 2"));
949 *set2 << 10 << 10 << 10;
963 *set2 << 10 << 10 << 10;
950 series->append(set2);
964 series->append(set2);
951 QList<QBarSet*> barSets = series->barSets();
965 QList<QBarSet*> barSets = series->barSets();
952
966
953 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
967 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
954 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
968 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
955 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
969 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
956
970
957 QChartView view(new QChart());
971 QChartView view(new QChart());
958 view.resize(400,300);
972 view.resize(400,300);
959 view.chart()->addSeries(series);
973 view.chart()->addSeries(series);
960 view.show();
974 view.show();
961 QTest::qWaitForWindowShown(&view);
975 QTest::qWaitForWindowShown(&view);
962
976
963 // Calculate expected layout for bars
977 // Calculate expected layout for bars
964 QRectF plotArea = view.chart()->plotArea();
978 QRectF plotArea = view.chart()->plotArea();
965 qreal width = plotArea.width();
979 qreal width = plotArea.width();
966 qreal height = plotArea.height();
980 qreal height = plotArea.height();
967 qreal rangeY = 3; // 3 values per set
981 qreal rangeY = 3; // 3 values per set
968 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
982 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
969 qreal scaleY = (height / rangeY);
983 qreal scaleY = (height / rangeY);
970 qreal scaleX = (width / rangeX);
984 qreal scaleX = (width / rangeX);
971
985
972 qreal setCount = series->count();
986 qreal setCount = series->count();
973 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
987 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
974 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
988 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
975 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
989 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
976
990
977 QVector<QRectF> layout;
991 QVector<QRectF> layout;
978
992
979 // 3 = count of values in set
993 // 3 = count of values in set
980 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
994 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
981 for (int i = 0; i < 3; i++) {
995 for (int i = 0; i < 3; i++) {
982 qreal xMax = -scaleX * domainMinX + plotArea.left();
996 qreal xMax = -scaleX * domainMinX + plotArea.left();
983 qreal xMin = -scaleX * domainMinX + plotArea.left();
997 qreal xMin = -scaleX * domainMinX + plotArea.left();
984 for (int set = 0; set < setCount; set++) {
998 for (int set = 0; set < setCount; set++) {
985 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
999 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
986 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
1000 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
987 if (rectWidth > 0) {
1001 if (rectWidth > 0) {
988 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
1002 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
989 layout.append(rect);
1003 layout.append(rect);
990 xMax += rectWidth;
1004 xMax += rectWidth;
991 } else {
1005 } else {
992 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
1006 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
993 layout.append(rect);
1007 layout.append(rect);
994 xMin += rectWidth;
1008 xMin += rectWidth;
995 }
1009 }
996 }
1010 }
997 }
1011 }
998
1012
999 // barset 1, bar 0
1013 // barset 1, bar 0
1000 QTest::mouseDClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1014 QTest::mouseDClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1001 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1015 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1002
1016
1003 QCOMPARE(seriesSpy.count(), 1);
1017 QCOMPARE(seriesSpy.count(), 1);
1004 QCOMPARE(setSpy1.count(), 1);
1018 QCOMPARE(setSpy1.count(), 1);
1005 QCOMPARE(setSpy2.count(), 0);
1019 QCOMPARE(setSpy2.count(), 0);
1006
1020
1007 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1021 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1008 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1022 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1009 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1023 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1010 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1024 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1011
1025
1012 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1026 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1013 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1027 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1014 QVERIFY(setSpyArg.at(0).toInt() == 0);
1028 QVERIFY(setSpyArg.at(0).toInt() == 0);
1015 }
1029 }
1016 QTEST_MAIN(tst_QHorizontalStackedBarSeries)
1030 QTEST_MAIN(tst_QHorizontalStackedBarSeries)
1017
1031
1018 #include "tst_qhorizontalstackedbarseries.moc"
1032 #include "tst_qhorizontalstackedbarseries.moc"
1019
1033
@@ -1,1025 +1,1039
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd
3 ** Copyright (C) 2015 The Qt Company Ltd
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 **
6 **
7 ** This file is part of the Qt Charts module.
7 ** This file is part of the Qt Charts module.
8 **
8 **
9 ** Licensees holding valid commercial license for Qt may use this file in
9 ** Licensees holding valid commercial license for Qt may use this file in
10 ** accordance with the Qt License Agreement provided with the Software
10 ** accordance with the Qt License Agreement provided with the Software
11 ** or, alternatively, in accordance with the terms contained in a written
11 ** or, alternatively, in accordance with the terms contained in a written
12 ** agreement between you and The Qt Company.
12 ** agreement between you and The Qt Company.
13 **
13 **
14 ** If you have questions regarding the use of this file, please use
14 ** If you have questions regarding the use of this file, please use
15 ** contact form at http://qt.io
15 ** contact form at http://qt.io
16 **
16 **
17 ****************************************************************************/
17 ****************************************************************************/
18
18
19 #include <QtTest/QtTest>
19 #include <QtTest/QtTest>
20 #include <QtCharts/QPercentBarSeries>
20 #include <QtCharts/QPercentBarSeries>
21 #include <QtCharts/QBarSet>
21 #include <QtCharts/QBarSet>
22 #include <QtCharts/QChartView>
22 #include <QtCharts/QChartView>
23 #include <QtCharts/QChart>
23 #include <QtCharts/QChart>
24 #include "tst_definitions.h"
24 #include "tst_definitions.h"
25
25
26 QT_CHARTS_USE_NAMESPACE
26 QT_CHARTS_USE_NAMESPACE
27
27
28 Q_DECLARE_METATYPE(QBarSet*)
28 Q_DECLARE_METATYPE(QBarSet*)
29 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
29 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
30
30
31 class tst_QPercentBarSeries : public QObject
31 class tst_QPercentBarSeries : public QObject
32 {
32 {
33 Q_OBJECT
33 Q_OBJECT
34
34
35 public slots:
35 public slots:
36 void initTestCase();
36 void initTestCase();
37 void cleanupTestCase();
37 void cleanupTestCase();
38 void init();
38 void init();
39 void cleanup();
39 void cleanup();
40
40
41 private slots:
41 private slots:
42 void qpercentbarseries_data();
42 void qpercentbarseries_data();
43 void qpercentbarseries();
43 void qpercentbarseries();
44 void type_data();
44 void type_data();
45 void type();
45 void type();
46 void setLabelsFormat();
46 void setLabelsFormat();
47 void setLabelsPosition();
47 void setLabelsPosition();
48 void setLabelsAngle();
48 void mouseclicked_data();
49 void mouseclicked_data();
49 void mouseclicked();
50 void mouseclicked();
50 void mousehovered_data();
51 void mousehovered_data();
51 void mousehovered();
52 void mousehovered();
52 void zeroValuesInSeries();
53 void zeroValuesInSeries();
53 void mousePressed();
54 void mousePressed();
54 void mouseReleased();
55 void mouseReleased();
55 void mouseDoubleClicked();
56 void mouseDoubleClicked();
56
57
57 private:
58 private:
58 QPercentBarSeries* m_barseries;
59 QPercentBarSeries* m_barseries;
59 };
60 };
60
61
61 void tst_QPercentBarSeries::initTestCase()
62 void tst_QPercentBarSeries::initTestCase()
62 {
63 {
63 qRegisterMetaType<QBarSet*>("QBarSet*");
64 qRegisterMetaType<QBarSet*>("QBarSet*");
64 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
65 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
65 }
66 }
66
67
67 void tst_QPercentBarSeries::cleanupTestCase()
68 void tst_QPercentBarSeries::cleanupTestCase()
68 {
69 {
69 QTest::qWait(1); // Allow final deleteLaters to run
70 QTest::qWait(1); // Allow final deleteLaters to run
70 }
71 }
71
72
72 void tst_QPercentBarSeries::init()
73 void tst_QPercentBarSeries::init()
73 {
74 {
74 m_barseries = new QPercentBarSeries();
75 m_barseries = new QPercentBarSeries();
75 }
76 }
76
77
77 void tst_QPercentBarSeries::cleanup()
78 void tst_QPercentBarSeries::cleanup()
78 {
79 {
79 delete m_barseries;
80 delete m_barseries;
80 m_barseries = 0;
81 m_barseries = 0;
81 }
82 }
82
83
83 void tst_QPercentBarSeries::qpercentbarseries_data()
84 void tst_QPercentBarSeries::qpercentbarseries_data()
84 {
85 {
85 }
86 }
86
87
87 void tst_QPercentBarSeries::qpercentbarseries()
88 void tst_QPercentBarSeries::qpercentbarseries()
88 {
89 {
89 QPercentBarSeries *barseries = new QPercentBarSeries();
90 QPercentBarSeries *barseries = new QPercentBarSeries();
90 QVERIFY(barseries != 0);
91 QVERIFY(barseries != 0);
91 delete barseries;
92 delete barseries;
92 }
93 }
93
94
94 void tst_QPercentBarSeries::type_data()
95 void tst_QPercentBarSeries::type_data()
95 {
96 {
96
97
97 }
98 }
98
99
99 void tst_QPercentBarSeries::type()
100 void tst_QPercentBarSeries::type()
100 {
101 {
101 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypePercentBar);
102 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypePercentBar);
102 }
103 }
103
104
104 void tst_QPercentBarSeries::mouseclicked_data()
105 void tst_QPercentBarSeries::mouseclicked_data()
105 {
106 {
106
107
107 }
108 }
108
109
109 void tst_QPercentBarSeries::setLabelsFormat()
110 void tst_QPercentBarSeries::setLabelsFormat()
110 {
111 {
111 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
112 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
112 QCOMPARE(m_barseries->labelsFormat(), QString());
113 QCOMPARE(m_barseries->labelsFormat(), QString());
113
114
114 QString format("(@value)");
115 QString format("(@value)");
115 m_barseries->setLabelsFormat(format);
116 m_barseries->setLabelsFormat(format);
116 TRY_COMPARE(labelsFormatSpy.count(), 1);
117 TRY_COMPARE(labelsFormatSpy.count(), 1);
117 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
118 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
118 QVERIFY(arguments.at(0).toString() == format);
119 QVERIFY(arguments.at(0).toString() == format);
119 QCOMPARE(m_barseries->labelsFormat(), format);
120 QCOMPARE(m_barseries->labelsFormat(), format);
120
121
121 m_barseries->setLabelsFormat(QString());
122 m_barseries->setLabelsFormat(QString());
122 TRY_COMPARE(labelsFormatSpy.count(), 1);
123 TRY_COMPARE(labelsFormatSpy.count(), 1);
123 arguments = labelsFormatSpy.takeFirst();
124 arguments = labelsFormatSpy.takeFirst();
124 QVERIFY(arguments.at(0).toString() == QString());
125 QVERIFY(arguments.at(0).toString() == QString());
125 QCOMPARE(m_barseries->labelsFormat(), QString());
126 QCOMPARE(m_barseries->labelsFormat(), QString());
126 }
127 }
127
128
128 void tst_QPercentBarSeries::setLabelsPosition()
129 void tst_QPercentBarSeries::setLabelsPosition()
129 {
130 {
130 QSignalSpy labelsPositionSpy(m_barseries,
131 QSignalSpy labelsPositionSpy(m_barseries,
131 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
132 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
132 QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsCenter);
133 QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsCenter);
133
134
134 m_barseries->setLabelsPosition(QPercentBarSeries::LabelsInsideEnd);
135 m_barseries->setLabelsPosition(QPercentBarSeries::LabelsInsideEnd);
135 TRY_COMPARE(labelsPositionSpy.count(), 1);
136 TRY_COMPARE(labelsPositionSpy.count(), 1);
136 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
137 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
137 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
138 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
138 == QPercentBarSeries::LabelsInsideEnd);
139 == QPercentBarSeries::LabelsInsideEnd);
139 QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsInsideEnd);
140 QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsInsideEnd);
140
141
141 m_barseries->setLabelsPosition(QPercentBarSeries::LabelsInsideBase);
142 m_barseries->setLabelsPosition(QPercentBarSeries::LabelsInsideBase);
142 TRY_COMPARE(labelsPositionSpy.count(), 1);
143 TRY_COMPARE(labelsPositionSpy.count(), 1);
143 arguments = labelsPositionSpy.takeFirst();
144 arguments = labelsPositionSpy.takeFirst();
144 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
145 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
145 == QPercentBarSeries::LabelsInsideBase);
146 == QPercentBarSeries::LabelsInsideBase);
146 QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsInsideBase);
147 QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsInsideBase);
147
148
148 m_barseries->setLabelsPosition(QPercentBarSeries::LabelsOutsideEnd);
149 m_barseries->setLabelsPosition(QPercentBarSeries::LabelsOutsideEnd);
149 TRY_COMPARE(labelsPositionSpy.count(), 1);
150 TRY_COMPARE(labelsPositionSpy.count(), 1);
150 arguments = labelsPositionSpy.takeFirst();
151 arguments = labelsPositionSpy.takeFirst();
151 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
152 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
152 == QPercentBarSeries::LabelsOutsideEnd);
153 == QPercentBarSeries::LabelsOutsideEnd);
153 QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsOutsideEnd);
154 QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsOutsideEnd);
154
155
155 m_barseries->setLabelsPosition(QPercentBarSeries::LabelsCenter);
156 m_barseries->setLabelsPosition(QPercentBarSeries::LabelsCenter);
156 TRY_COMPARE(labelsPositionSpy.count(), 1);
157 TRY_COMPARE(labelsPositionSpy.count(), 1);
157 arguments = labelsPositionSpy.takeFirst();
158 arguments = labelsPositionSpy.takeFirst();
158 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
159 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
159 == QPercentBarSeries::LabelsCenter);
160 == QPercentBarSeries::LabelsCenter);
160 QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsCenter);
161 QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsCenter);
161 }
162 }
162
163
164 void tst_QPercentBarSeries::setLabelsAngle()
165 {
166 QSignalSpy labelsAngleSpy(m_barseries,
167 SIGNAL(labelsAngleChanged(qreal)));
168 QCOMPARE(m_barseries->labelsAngle(), 0.0);
169
170 m_barseries->setLabelsAngle(55.0);
171 TRY_COMPARE(labelsAngleSpy.count(), 1);
172 QList<QVariant> arguments = labelsAngleSpy.takeFirst();
173 QVERIFY(arguments.at(0).value<qreal>() == 55.0);
174 QCOMPARE(m_barseries->labelsAngle(), 55.0);
175 }
176
163 void tst_QPercentBarSeries::mouseclicked()
177 void tst_QPercentBarSeries::mouseclicked()
164 {
178 {
165 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
179 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
166
180
167 QPercentBarSeries* series = new QPercentBarSeries();
181 QPercentBarSeries* series = new QPercentBarSeries();
168
182
169 QBarSet* set1 = new QBarSet(QString("set 1"));
183 QBarSet* set1 = new QBarSet(QString("set 1"));
170 *set1 << 10 << 10 << 10;
184 *set1 << 10 << 10 << 10;
171 series->append(set1);
185 series->append(set1);
172
186
173 QBarSet* set2 = new QBarSet(QString("set 2"));
187 QBarSet* set2 = new QBarSet(QString("set 2"));
174 *set2 << 10 << 10 << 10;
188 *set2 << 10 << 10 << 10;
175 series->append(set2);
189 series->append(set2);
176
190
177 QList<QBarSet*> barSets = series->barSets();
191 QList<QBarSet*> barSets = series->barSets();
178
192
179 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
193 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
180
194
181 QChartView view(new QChart());
195 QChartView view(new QChart());
182 view.resize(400,300);
196 view.resize(400,300);
183 view.chart()->addSeries(series);
197 view.chart()->addSeries(series);
184 view.show();
198 view.show();
185 QTest::qWaitForWindowShown(&view);
199 QTest::qWaitForWindowShown(&view);
186
200
187 // Calculate expected layout for bars
201 // Calculate expected layout for bars
188 QRectF plotArea = view.chart()->plotArea();
202 QRectF plotArea = view.chart()->plotArea();
189 qreal width = plotArea.width();
203 qreal width = plotArea.width();
190 qreal height = plotArea.height();
204 qreal height = plotArea.height();
191 qreal rangeY = 100; // From 0 to 100 because range is scaled to 100%
205 qreal rangeY = 100; // From 0 to 100 because range is scaled to 100%
192 qreal rangeX = 3; // 3 values per set
206 qreal rangeX = 3; // 3 values per set
193 qreal scaleY = (height / rangeY);
207 qreal scaleY = (height / rangeY);
194 qreal scaleX = (width / rangeX);
208 qreal scaleX = (width / rangeX);
195
209
196 qreal setCount = series->count();
210 qreal setCount = series->count();
197 qreal domainMinY = 0; // These come from internal domain used by barseries.
211 qreal domainMinY = 0; // These come from internal domain used by barseries.
198 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
212 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
199 qreal rectWidth = scaleX * series->barWidth();
213 qreal rectWidth = scaleX * series->barWidth();
200
214
201 QVector<QRectF> layout;
215 QVector<QRectF> layout;
202
216
203 // 3 = count of values in set
217 // 3 = count of values in set
204 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
218 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
205 for (int i = 0; i < 3; i++) {
219 for (int i = 0; i < 3; i++) {
206 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
220 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
207 qreal percentage = (100 / colSum);
221 qreal percentage = (100 / colSum);
208 qreal yPos = height + scaleY * domainMinY + plotArea.top();
222 qreal yPos = height + scaleY * domainMinY + plotArea.top();
209
223
210 for (int set = 0; set < setCount; set++) {
224 for (int set = 0; set < setCount; set++) {
211 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
225 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
212 qreal rectHeigth = barSets.at(set)->at(i) * percentage * scaleY;
226 qreal rectHeigth = barSets.at(set)->at(i) * percentage * scaleY;
213
227
214 QRectF rect(xPos, yPos-rectHeigth, rectWidth, rectHeigth);
228 QRectF rect(xPos, yPos-rectHeigth, rectWidth, rectHeigth);
215 layout.append(rect);
229 layout.append(rect);
216 yPos -= rectHeigth;
230 yPos -= rectHeigth;
217 }
231 }
218 }
232 }
219
233
220 //====================================================================================
234 //====================================================================================
221 // barset 1, bar 0
235 // barset 1, bar 0
222 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
236 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
223 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
237 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
224
238
225 QCOMPARE(seriesSpy.count(), 1);
239 QCOMPARE(seriesSpy.count(), 1);
226
240
227 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
241 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
228 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
242 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
229 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
243 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
230 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
244 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
231
245
232 //====================================================================================
246 //====================================================================================
233 // barset 1, bar 1
247 // barset 1, bar 1
234 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
248 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
235 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
249 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
236
250
237 QCOMPARE(seriesSpy.count(), 1);
251 QCOMPARE(seriesSpy.count(), 1);
238
252
239 seriesSpyArg = seriesSpy.takeFirst();
253 seriesSpyArg = seriesSpy.takeFirst();
240 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
254 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
241 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
255 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
242 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
256 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
243
257
244 //====================================================================================
258 //====================================================================================
245 // barset 1, bar 2
259 // barset 1, bar 2
246 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
260 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
247 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
261 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
248
262
249 QCOMPARE(seriesSpy.count(), 1);
263 QCOMPARE(seriesSpy.count(), 1);
250
264
251 seriesSpyArg = seriesSpy.takeFirst();
265 seriesSpyArg = seriesSpy.takeFirst();
252 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
266 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
253 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
267 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
254 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
268 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
255
269
256 //====================================================================================
270 //====================================================================================
257 // barset 2, bar 0
271 // barset 2, bar 0
258 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
272 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
259 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
273 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
260
274
261 QCOMPARE(seriesSpy.count(), 1);
275 QCOMPARE(seriesSpy.count(), 1);
262
276
263 seriesSpyArg = seriesSpy.takeFirst();
277 seriesSpyArg = seriesSpy.takeFirst();
264 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
278 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
265 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
279 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
266 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
280 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
267
281
268 //====================================================================================
282 //====================================================================================
269 // barset 2, bar 1
283 // barset 2, bar 1
270 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
284 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
271 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
285 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
272
286
273 QCOMPARE(seriesSpy.count(), 1);
287 QCOMPARE(seriesSpy.count(), 1);
274
288
275 seriesSpyArg = seriesSpy.takeFirst();
289 seriesSpyArg = seriesSpy.takeFirst();
276 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
290 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
277 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
291 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
278 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
292 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
279
293
280 //====================================================================================
294 //====================================================================================
281 // barset 2, bar 2
295 // barset 2, bar 2
282 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
296 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
283 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
297 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
284
298
285 QCOMPARE(seriesSpy.count(), 1);
299 QCOMPARE(seriesSpy.count(), 1);
286
300
287 seriesSpyArg = seriesSpy.takeFirst();
301 seriesSpyArg = seriesSpy.takeFirst();
288 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
302 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
289 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
303 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
290 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
304 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
291 }
305 }
292
306
293 void tst_QPercentBarSeries::mousehovered_data()
307 void tst_QPercentBarSeries::mousehovered_data()
294 {
308 {
295
309
296 }
310 }
297
311
298 void tst_QPercentBarSeries::mousehovered()
312 void tst_QPercentBarSeries::mousehovered()
299 {
313 {
300 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
314 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
301
315
302 QPercentBarSeries* series = new QPercentBarSeries();
316 QPercentBarSeries* series = new QPercentBarSeries();
303
317
304 QBarSet* set1 = new QBarSet(QString("set 1"));
318 QBarSet* set1 = new QBarSet(QString("set 1"));
305 *set1 << 10 << 10 << 10;
319 *set1 << 10 << 10 << 10;
306 series->append(set1);
320 series->append(set1);
307
321
308 QBarSet* set2 = new QBarSet(QString("set 2"));
322 QBarSet* set2 = new QBarSet(QString("set 2"));
309 *set2 << 10 << 10 << 10;
323 *set2 << 10 << 10 << 10;
310 series->append(set2);
324 series->append(set2);
311
325
312 QList<QBarSet*> barSets = series->barSets();
326 QList<QBarSet*> barSets = series->barSets();
313
327
314 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
328 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
315 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
329 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
316 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
330 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
317
331
318 QChartView view(new QChart());
332 QChartView view(new QChart());
319 view.resize(400,300);
333 view.resize(400,300);
320 view.chart()->addSeries(series);
334 view.chart()->addSeries(series);
321 view.show();
335 view.show();
322 QTest::qWaitForWindowShown(&view);
336 QTest::qWaitForWindowShown(&view);
323
337
324 //this is hack since view does not get events otherwise
338 //this is hack since view does not get events otherwise
325 view.setMouseTracking(true);
339 view.setMouseTracking(true);
326
340
327 // Calculate expected layout for bars
341 // Calculate expected layout for bars
328 QRectF plotArea = view.chart()->plotArea();
342 QRectF plotArea = view.chart()->plotArea();
329 qreal width = plotArea.width();
343 qreal width = plotArea.width();
330 qreal height = plotArea.height();
344 qreal height = plotArea.height();
331 qreal rangeY = 100; // From 0 to 100 because range is scaled to 100%
345 qreal rangeY = 100; // From 0 to 100 because range is scaled to 100%
332 qreal rangeX = 3; // 3 values per set
346 qreal rangeX = 3; // 3 values per set
333 qreal scaleY = (height / rangeY);
347 qreal scaleY = (height / rangeY);
334 qreal scaleX = (width / rangeX);
348 qreal scaleX = (width / rangeX);
335
349
336 qreal setCount = series->count();
350 qreal setCount = series->count();
337 qreal domainMinY = 0; // These come from internal domain used by barseries.
351 qreal domainMinY = 0; // These come from internal domain used by barseries.
338 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
352 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
339 qreal rectWidth = scaleX * series->barWidth();
353 qreal rectWidth = scaleX * series->barWidth();
340
354
341 QVector<QRectF> layout;
355 QVector<QRectF> layout;
342
356
343 // 3 = count of values in set
357 // 3 = count of values in set
344 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
358 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
345 for (int i = 0; i < 3; i++) {
359 for (int i = 0; i < 3; i++) {
346 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
360 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
347 qreal percentage = (100 / colSum);
361 qreal percentage = (100 / colSum);
348 qreal yPos = height + scaleY * domainMinY + plotArea.top();
362 qreal yPos = height + scaleY * domainMinY + plotArea.top();
349
363
350 for (int set = 0; set < setCount; set++) {
364 for (int set = 0; set < setCount; set++) {
351 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
365 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
352 qreal rectHeight = barSets.at(set)->at(i) * percentage * scaleY;
366 qreal rectHeight = barSets.at(set)->at(i) * percentage * scaleY;
353
367
354 QRectF rect(xPos, yPos-rectHeight, rectWidth, rectHeight);
368 QRectF rect(xPos, yPos-rectHeight, rectWidth, rectHeight);
355 layout.append(rect);
369 layout.append(rect);
356 yPos -= rectHeight;
370 yPos -= rectHeight;
357 }
371 }
358 }
372 }
359
373
360 //=======================================================================
374 //=======================================================================
361 // move mouse to left border
375 // move mouse to left border
362 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(0).center().y()));
376 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(0).center().y()));
363 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
377 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
364 TRY_COMPARE(seriesIndexSpy.count(), 0);
378 TRY_COMPARE(seriesIndexSpy.count(), 0);
365
379
366 //=======================================================================
380 //=======================================================================
367 // move mouse on top of set1
381 // move mouse on top of set1
368 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
382 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
369 TRY_COMPARE(seriesIndexSpy.count(), 1);
383 TRY_COMPARE(seriesIndexSpy.count(), 1);
370 TRY_COMPARE(setIndexSpy1.count(), 1);
384 TRY_COMPARE(setIndexSpy1.count(), 1);
371 TRY_COMPARE(setIndexSpy2.count(), 0);
385 TRY_COMPARE(setIndexSpy2.count(), 0);
372
386
373 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
387 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
374 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
388 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
375 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
389 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
376 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
390 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
377
391
378 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
392 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
379 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
393 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
380 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
394 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
381
395
382 //=======================================================================
396 //=======================================================================
383 // move mouse from top of set1 to top of set2
397 // move mouse from top of set1 to top of set2
384 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
398 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
385 TRY_COMPARE(seriesIndexSpy.count(), 2);
399 TRY_COMPARE(seriesIndexSpy.count(), 2);
386 TRY_COMPARE(setIndexSpy1.count(), 1);
400 TRY_COMPARE(setIndexSpy1.count(), 1);
387 TRY_COMPARE(setIndexSpy2.count(), 1);
401 TRY_COMPARE(setIndexSpy2.count(), 1);
388
402
389 // should leave set1
403 // should leave set1
390 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
404 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
391 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
405 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
392 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
406 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
393 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
407 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
394
408
395 setIndexSpyArg = setIndexSpy1.takeFirst();
409 setIndexSpyArg = setIndexSpy1.takeFirst();
396 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
410 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
397 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
411 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
398
412
399 // should enter set2
413 // should enter set2
400 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
414 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
401 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
415 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
402 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
416 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
403 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
417 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
404
418
405 setIndexSpyArg = setIndexSpy2.takeFirst();
419 setIndexSpyArg = setIndexSpy2.takeFirst();
406 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
420 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
407 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
421 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
408
422
409 //=======================================================================
423 //=======================================================================
410 // move mouse from top of set2 to background
424 // move mouse from top of set2 to background
411 QTest::mouseMove(view.viewport(), QPoint(layout.at(1).center().x(), 0));
425 QTest::mouseMove(view.viewport(), QPoint(layout.at(1).center().x(), 0));
412 TRY_COMPARE(seriesIndexSpy.count(), 1);
426 TRY_COMPARE(seriesIndexSpy.count(), 1);
413 TRY_COMPARE(setIndexSpy1.count(), 0);
427 TRY_COMPARE(setIndexSpy1.count(), 0);
414 TRY_COMPARE(setIndexSpy2.count(), 1);
428 TRY_COMPARE(setIndexSpy2.count(), 1);
415
429
416 // should leave set2
430 // should leave set2
417 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
431 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
418 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
432 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
419 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
433 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
420 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
434 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
421
435
422 setIndexSpyArg = setIndexSpy2.takeFirst();
436 setIndexSpyArg = setIndexSpy2.takeFirst();
423 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
437 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
424 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
438 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
425
439
426 //=======================================================================
440 //=======================================================================
427 // move mouse on top of set1, bar0 to check the index (hover into set1)
441 // move mouse on top of set1, bar0 to check the index (hover into set1)
428 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
442 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
429
443
430 TRY_COMPARE(seriesIndexSpy.count(), 1);
444 TRY_COMPARE(seriesIndexSpy.count(), 1);
431 TRY_COMPARE(setIndexSpy1.count(), 1);
445 TRY_COMPARE(setIndexSpy1.count(), 1);
432 TRY_COMPARE(setIndexSpy2.count(), 0);
446 TRY_COMPARE(setIndexSpy2.count(), 0);
433
447
434 //should enter set1, bar0
448 //should enter set1, bar0
435 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
449 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
436 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
450 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
437 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
451 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
438 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
452 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
439 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
453 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
440 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
454 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
441
455
442 setIndexSpyArg = setIndexSpy1.takeFirst();
456 setIndexSpyArg = setIndexSpy1.takeFirst();
443 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
457 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
444 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
458 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
445 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
459 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
446 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
460 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
447
461
448 //=======================================================================
462 //=======================================================================
449 // move mouse on top of set2, bar0 to check the index (hover out set1,
463 // move mouse on top of set2, bar0 to check the index (hover out set1,
450 // hover in set2)
464 // hover in set2)
451 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
465 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
452
466
453 TRY_COMPARE(seriesIndexSpy.count(), 2);
467 TRY_COMPARE(seriesIndexSpy.count(), 2);
454 TRY_COMPARE(setIndexSpy1.count(), 1);
468 TRY_COMPARE(setIndexSpy1.count(), 1);
455 TRY_COMPARE(setIndexSpy2.count(), 1);
469 TRY_COMPARE(setIndexSpy2.count(), 1);
456
470
457 //should leave set1, bar0
471 //should leave set1, bar0
458 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
472 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
459 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
473 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
460 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
474 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
461 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
475 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
462 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
476 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
463 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
477 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
464
478
465 setIndexSpyArg = setIndexSpy1.takeFirst();
479 setIndexSpyArg = setIndexSpy1.takeFirst();
466 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
480 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
467 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
481 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
468 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
482 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
469 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
483 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
470
484
471 //should enter set2, bar0
485 //should enter set2, bar0
472 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
486 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
473 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
487 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
474 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
488 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
475 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
489 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
476 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
490 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
477 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
491 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
478
492
479 setIndexSpyArg = setIndexSpy2.takeFirst();
493 setIndexSpyArg = setIndexSpy2.takeFirst();
480 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
494 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
481 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
495 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
482 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
496 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
483 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
497 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
484
498
485 //=======================================================================
499 //=======================================================================
486 // move mouse on top of set1, bar1 to check the index (hover out set2,
500 // move mouse on top of set1, bar1 to check the index (hover out set2,
487 // hover in set1)
501 // hover in set1)
488 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
502 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
489
503
490 TRY_COMPARE(seriesIndexSpy.count(), 2);
504 TRY_COMPARE(seriesIndexSpy.count(), 2);
491 TRY_COMPARE(setIndexSpy1.count(), 1);
505 TRY_COMPARE(setIndexSpy1.count(), 1);
492 TRY_COMPARE(setIndexSpy2.count(), 1);
506 TRY_COMPARE(setIndexSpy2.count(), 1);
493
507
494 //should leave set2, bar0
508 //should leave set2, bar0
495 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
509 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
496 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
510 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
497 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
511 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
498 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
512 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
499 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
513 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
500 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
514 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
501
515
502 setIndexSpyArg = setIndexSpy2.takeFirst();
516 setIndexSpyArg = setIndexSpy2.takeFirst();
503 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
517 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
504 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
518 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
505 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
519 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
506 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
520 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
507
521
508 //should enter set1, bar1
522 //should enter set1, bar1
509 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
523 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
510 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
524 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
511 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
525 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
512 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
526 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
513 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
527 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
514 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
528 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
515
529
516 setIndexSpyArg = setIndexSpy1.takeFirst();
530 setIndexSpyArg = setIndexSpy1.takeFirst();
517 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
531 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
518 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
532 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
519 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
533 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
520 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
534 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
521
535
522 //=======================================================================
536 //=======================================================================
523 // move mouse between set1 and set2 (hover out set1)
537 // move mouse between set1 and set2 (hover out set1)
524 QTest::mouseMove(view.viewport(), QPoint((layout.at(3).right() + layout.at(4).left()) /2,
538 QTest::mouseMove(view.viewport(), QPoint((layout.at(3).right() + layout.at(4).left()) /2,
525 layout.at(2).top()));
539 layout.at(2).top()));
526
540
527 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
541 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
528 TRY_COMPARE(seriesIndexSpy.count(), 1);
542 TRY_COMPARE(seriesIndexSpy.count(), 1);
529 TRY_COMPARE(setIndexSpy1.count(), 1);
543 TRY_COMPARE(setIndexSpy1.count(), 1);
530 TRY_COMPARE(setIndexSpy2.count(), 0);
544 TRY_COMPARE(setIndexSpy2.count(), 0);
531
545
532 //should leave set1, bar1
546 //should leave set1, bar1
533 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
547 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
534 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
548 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
535 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
549 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
536 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
550 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
537 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
551 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
538 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
552 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
539
553
540 setIndexSpyArg = setIndexSpy1.takeFirst();
554 setIndexSpyArg = setIndexSpy1.takeFirst();
541 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
555 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
542 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
556 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
543 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
557 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
544 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
558 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
545
559
546 //=======================================================================
560 //=======================================================================
547 // move mouse on top of set2, bar1 to check the index (hover in set2)
561 // move mouse on top of set2, bar1 to check the index (hover in set2)
548 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
562 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
549
563
550 TRY_COMPARE(seriesIndexSpy.count(), 1);
564 TRY_COMPARE(seriesIndexSpy.count(), 1);
551 TRY_COMPARE(setIndexSpy1.count(), 0);
565 TRY_COMPARE(setIndexSpy1.count(), 0);
552 TRY_COMPARE(setIndexSpy2.count(), 1);
566 TRY_COMPARE(setIndexSpy2.count(), 1);
553
567
554 //should enter set2, bar1
568 //should enter set2, bar1
555 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
569 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
556 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
570 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
557 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
571 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
558 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
572 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
559 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
573 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
560 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
574 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
561
575
562 setIndexSpyArg = setIndexSpy2.takeFirst();
576 setIndexSpyArg = setIndexSpy2.takeFirst();
563 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
577 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
564 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
578 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
565 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
579 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
566 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
580 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
567
581
568 //=======================================================================
582 //=======================================================================
569 // move mouse between set1 and set2 (hover out set2)
583 // move mouse between set1 and set2 (hover out set2)
570 QTest::mouseMove(view.viewport(), QPoint((layout.at(3).right() + layout.at(4).left()) /2,
584 QTest::mouseMove(view.viewport(), QPoint((layout.at(3).right() + layout.at(4).left()) /2,
571 layout.at(2).top()));
585 layout.at(2).top()));
572
586
573 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
587 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
574 TRY_COMPARE(seriesIndexSpy.count(), 1);
588 TRY_COMPARE(seriesIndexSpy.count(), 1);
575 TRY_COMPARE(setIndexSpy1.count(), 0);
589 TRY_COMPARE(setIndexSpy1.count(), 0);
576 TRY_COMPARE(setIndexSpy2.count(), 1);
590 TRY_COMPARE(setIndexSpy2.count(), 1);
577
591
578 //should leave set1, bar1
592 //should leave set1, bar1
579 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
593 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
580 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
594 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
581 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
595 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
582 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
596 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
583 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
597 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
584 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
598 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
585
599
586 setIndexSpyArg = setIndexSpy2.takeFirst();
600 setIndexSpyArg = setIndexSpy2.takeFirst();
587 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
601 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
588 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
602 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
589 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
603 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
590 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
604 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
591 }
605 }
592
606
593 void tst_QPercentBarSeries::zeroValuesInSeries()
607 void tst_QPercentBarSeries::zeroValuesInSeries()
594 {
608 {
595 QPercentBarSeries *series = new QPercentBarSeries();
609 QPercentBarSeries *series = new QPercentBarSeries();
596 QBarSet *set1 = new QBarSet(QString("set 1"));
610 QBarSet *set1 = new QBarSet(QString("set 1"));
597 *set1 << 100 << 0.0 << 10;
611 *set1 << 100 << 0.0 << 10;
598 series->append(set1);
612 series->append(set1);
599
613
600 QBarSet *set2 = new QBarSet(QString("set 2"));
614 QBarSet *set2 = new QBarSet(QString("set 2"));
601 *set2 << 0.0 << 0.0 << 70;
615 *set2 << 0.0 << 0.0 << 70;
602 series->append(set2);
616 series->append(set2);
603
617
604 QChartView view(new QChart());
618 QChartView view(new QChart());
605 view.chart()->addSeries(series);
619 view.chart()->addSeries(series);
606 view.chart()->createDefaultAxes();
620 view.chart()->createDefaultAxes();
607 view.show();
621 view.show();
608
622
609 QTest::qWaitForWindowShown(&view);
623 QTest::qWaitForWindowShown(&view);
610 }
624 }
611
625
612 void tst_QPercentBarSeries::mousePressed()
626 void tst_QPercentBarSeries::mousePressed()
613 {
627 {
614 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
628 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
615
629
616 QPercentBarSeries* series = new QPercentBarSeries();
630 QPercentBarSeries* series = new QPercentBarSeries();
617
631
618 QBarSet* set1 = new QBarSet(QString("set 1"));
632 QBarSet* set1 = new QBarSet(QString("set 1"));
619 *set1 << 10 << 10 << 10;
633 *set1 << 10 << 10 << 10;
620 series->append(set1);
634 series->append(set1);
621
635
622 QBarSet* set2 = new QBarSet(QString("set 2"));
636 QBarSet* set2 = new QBarSet(QString("set 2"));
623 *set2 << 10 << 10 << 10;
637 *set2 << 10 << 10 << 10;
624 series->append(set2);
638 series->append(set2);
625 QList<QBarSet*> barSets = series->barSets();
639 QList<QBarSet*> barSets = series->barSets();
626
640
627 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
641 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
628 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
642 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
629 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
643 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
630
644
631 QChartView view(new QChart());
645 QChartView view(new QChart());
632 view.resize(400,300);
646 view.resize(400,300);
633 view.chart()->addSeries(series);
647 view.chart()->addSeries(series);
634 view.show();
648 view.show();
635 QTest::qWaitForWindowShown(&view);
649 QTest::qWaitForWindowShown(&view);
636
650
637 // Calculate expected layout for bars
651 // Calculate expected layout for bars
638 QRectF plotArea = view.chart()->plotArea();
652 QRectF plotArea = view.chart()->plotArea();
639 qreal width = plotArea.width();
653 qreal width = plotArea.width();
640 qreal height = plotArea.height();
654 qreal height = plotArea.height();
641 qreal rangeY = 100; // From 0 to 100 because range is scaled to 100%
655 qreal rangeY = 100; // From 0 to 100 because range is scaled to 100%
642 qreal rangeX = 3; // 3 values per set
656 qreal rangeX = 3; // 3 values per set
643 qreal scaleY = (height / rangeY);
657 qreal scaleY = (height / rangeY);
644 qreal scaleX = (width / rangeX);
658 qreal scaleX = (width / rangeX);
645
659
646 qreal setCount = series->count();
660 qreal setCount = series->count();
647 qreal domainMinY = 0; // These come from internal domain used by barseries.
661 qreal domainMinY = 0; // These come from internal domain used by barseries.
648 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
662 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
649 qreal rectWidth = scaleX * series->barWidth();
663 qreal rectWidth = scaleX * series->barWidth();
650
664
651 QVector<QRectF> layout;
665 QVector<QRectF> layout;
652
666
653 // 3 = count of values in set
667 // 3 = count of values in set
654 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
668 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
655 for (int i = 0; i < 3; i++) {
669 for (int i = 0; i < 3; i++) {
656 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
670 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
657 qreal percentage = (100 / colSum);
671 qreal percentage = (100 / colSum);
658 qreal yPos = height + scaleY * domainMinY + plotArea.top();
672 qreal yPos = height + scaleY * domainMinY + plotArea.top();
659
673
660 for (int set = 0; set < setCount; set++) {
674 for (int set = 0; set < setCount; set++) {
661 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
675 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
662 qreal rectHeigth = barSets.at(set)->at(i) * percentage * scaleY;
676 qreal rectHeigth = barSets.at(set)->at(i) * percentage * scaleY;
663
677
664 QRectF rect(xPos, yPos-rectHeigth, rectWidth, rectHeigth);
678 QRectF rect(xPos, yPos-rectHeigth, rectWidth, rectHeigth);
665 layout.append(rect);
679 layout.append(rect);
666 yPos -= rectHeigth;
680 yPos -= rectHeigth;
667 }
681 }
668 }
682 }
669
683
670 //====================================================================================
684 //====================================================================================
671 // barset 1, bar 0
685 // barset 1, bar 0
672 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
686 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
673 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
687 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
674
688
675 QCOMPARE(seriesSpy.count(), 1);
689 QCOMPARE(seriesSpy.count(), 1);
676 QCOMPARE(setSpy1.count(), 1);
690 QCOMPARE(setSpy1.count(), 1);
677 QCOMPARE(setSpy2.count(), 0);
691 QCOMPARE(setSpy2.count(), 0);
678
692
679 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
693 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
680 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
694 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
681 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
695 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
682 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
696 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
683
697
684 QList<QVariant> setSpyArg = setSpy1.takeFirst();
698 QList<QVariant> setSpyArg = setSpy1.takeFirst();
685 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
699 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
686 QVERIFY(setSpyArg.at(0).toInt() == 0);
700 QVERIFY(setSpyArg.at(0).toInt() == 0);
687
701
688 //====================================================================================
702 //====================================================================================
689 // barset 1, bar 1
703 // barset 1, bar 1
690 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
704 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
691 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
705 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
692
706
693 QCOMPARE(seriesSpy.count(), 1);
707 QCOMPARE(seriesSpy.count(), 1);
694 QCOMPARE(setSpy1.count(), 1);
708 QCOMPARE(setSpy1.count(), 1);
695 QCOMPARE(setSpy2.count(), 0);
709 QCOMPARE(setSpy2.count(), 0);
696
710
697 seriesSpyArg = seriesSpy.takeFirst();
711 seriesSpyArg = seriesSpy.takeFirst();
698 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
712 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
699 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
713 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
700 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
714 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
701
715
702 setSpyArg = setSpy1.takeFirst();
716 setSpyArg = setSpy1.takeFirst();
703 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
717 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
704 QVERIFY(setSpyArg.at(0).toInt() == 1);
718 QVERIFY(setSpyArg.at(0).toInt() == 1);
705
719
706 //====================================================================================
720 //====================================================================================
707 // barset 1, bar 2
721 // barset 1, bar 2
708 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
722 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
709 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
723 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
710
724
711 QCOMPARE(seriesSpy.count(), 1);
725 QCOMPARE(seriesSpy.count(), 1);
712 QCOMPARE(setSpy1.count(), 1);
726 QCOMPARE(setSpy1.count(), 1);
713 QCOMPARE(setSpy2.count(), 0);
727 QCOMPARE(setSpy2.count(), 0);
714
728
715 seriesSpyArg = seriesSpy.takeFirst();
729 seriesSpyArg = seriesSpy.takeFirst();
716 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
730 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
717 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
731 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
718 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
732 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
719
733
720 setSpyArg = setSpy1.takeFirst();
734 setSpyArg = setSpy1.takeFirst();
721 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
735 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
722 QVERIFY(setSpyArg.at(0).toInt() == 2);
736 QVERIFY(setSpyArg.at(0).toInt() == 2);
723
737
724 //====================================================================================
738 //====================================================================================
725 // barset 2, bar 0
739 // barset 2, bar 0
726 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
740 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
727 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
741 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
728
742
729 QCOMPARE(seriesSpy.count(), 1);
743 QCOMPARE(seriesSpy.count(), 1);
730 QCOMPARE(setSpy1.count(), 0);
744 QCOMPARE(setSpy1.count(), 0);
731 QCOMPARE(setSpy2.count(), 1);
745 QCOMPARE(setSpy2.count(), 1);
732
746
733 seriesSpyArg = seriesSpy.takeFirst();
747 seriesSpyArg = seriesSpy.takeFirst();
734 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
748 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
735 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
749 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
736 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
750 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
737
751
738 setSpyArg = setSpy2.takeFirst();
752 setSpyArg = setSpy2.takeFirst();
739 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
753 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
740 QVERIFY(setSpyArg.at(0).toInt() == 0);
754 QVERIFY(setSpyArg.at(0).toInt() == 0);
741
755
742 //====================================================================================
756 //====================================================================================
743 // barset 2, bar 1
757 // barset 2, bar 1
744 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
758 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
745 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
759 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
746
760
747 QCOMPARE(seriesSpy.count(), 1);
761 QCOMPARE(seriesSpy.count(), 1);
748 QCOMPARE(setSpy1.count(), 0);
762 QCOMPARE(setSpy1.count(), 0);
749 QCOMPARE(setSpy2.count(), 1);
763 QCOMPARE(setSpy2.count(), 1);
750
764
751 seriesSpyArg = seriesSpy.takeFirst();
765 seriesSpyArg = seriesSpy.takeFirst();
752 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
766 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
753 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
767 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
754 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
768 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
755
769
756 setSpyArg = setSpy2.takeFirst();
770 setSpyArg = setSpy2.takeFirst();
757 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
771 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
758 QVERIFY(setSpyArg.at(0).toInt() == 1);
772 QVERIFY(setSpyArg.at(0).toInt() == 1);
759
773
760 //====================================================================================
774 //====================================================================================
761 // barset 2, bar 2
775 // barset 2, bar 2
762 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
776 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
763 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
777 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
764
778
765 QCOMPARE(seriesSpy.count(), 1);
779 QCOMPARE(seriesSpy.count(), 1);
766 QCOMPARE(setSpy1.count(), 0);
780 QCOMPARE(setSpy1.count(), 0);
767 QCOMPARE(setSpy2.count(), 1);
781 QCOMPARE(setSpy2.count(), 1);
768
782
769 seriesSpyArg = seriesSpy.takeFirst();
783 seriesSpyArg = seriesSpy.takeFirst();
770 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
784 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
771 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
785 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
772 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
786 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
773
787
774 setSpyArg = setSpy2.takeFirst();
788 setSpyArg = setSpy2.takeFirst();
775 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
789 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
776 QVERIFY(setSpyArg.at(0).toInt() == 2);
790 QVERIFY(setSpyArg.at(0).toInt() == 2);
777 }
791 }
778
792
779 void tst_QPercentBarSeries::mouseReleased()
793 void tst_QPercentBarSeries::mouseReleased()
780 {
794 {
781 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
795 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
782
796
783 QPercentBarSeries* series = new QPercentBarSeries();
797 QPercentBarSeries* series = new QPercentBarSeries();
784
798
785 QBarSet* set1 = new QBarSet(QString("set 1"));
799 QBarSet* set1 = new QBarSet(QString("set 1"));
786 *set1 << 10 << 10 << 10;
800 *set1 << 10 << 10 << 10;
787 series->append(set1);
801 series->append(set1);
788
802
789 QBarSet* set2 = new QBarSet(QString("set 2"));
803 QBarSet* set2 = new QBarSet(QString("set 2"));
790 *set2 << 10 << 10 << 10;
804 *set2 << 10 << 10 << 10;
791 series->append(set2);
805 series->append(set2);
792 QList<QBarSet*> barSets = series->barSets();
806 QList<QBarSet*> barSets = series->barSets();
793
807
794 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
808 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
795 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
809 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
796 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
810 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
797
811
798 QChartView view(new QChart());
812 QChartView view(new QChart());
799 view.resize(400,300);
813 view.resize(400,300);
800 view.chart()->addSeries(series);
814 view.chart()->addSeries(series);
801 view.show();
815 view.show();
802 QTest::qWaitForWindowShown(&view);
816 QTest::qWaitForWindowShown(&view);
803
817
804 // Calculate expected layout for bars
818 // Calculate expected layout for bars
805 QRectF plotArea = view.chart()->plotArea();
819 QRectF plotArea = view.chart()->plotArea();
806 qreal width = plotArea.width();
820 qreal width = plotArea.width();
807 qreal height = plotArea.height();
821 qreal height = plotArea.height();
808 qreal rangeY = 100; // From 0 to 100 because range is scaled to 100%
822 qreal rangeY = 100; // From 0 to 100 because range is scaled to 100%
809 qreal rangeX = 3; // 3 values per set
823 qreal rangeX = 3; // 3 values per set
810 qreal scaleY = (height / rangeY);
824 qreal scaleY = (height / rangeY);
811 qreal scaleX = (width / rangeX);
825 qreal scaleX = (width / rangeX);
812
826
813 qreal setCount = series->count();
827 qreal setCount = series->count();
814 qreal domainMinY = 0; // These come from internal domain used by barseries.
828 qreal domainMinY = 0; // These come from internal domain used by barseries.
815 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
829 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
816 qreal rectWidth = scaleX * series->barWidth();
830 qreal rectWidth = scaleX * series->barWidth();
817
831
818 QVector<QRectF> layout;
832 QVector<QRectF> layout;
819
833
820 // 3 = count of values in set
834 // 3 = count of values in set
821 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
835 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
822 for (int i = 0; i < 3; i++) {
836 for (int i = 0; i < 3; i++) {
823 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
837 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
824 qreal percentage = (100 / colSum);
838 qreal percentage = (100 / colSum);
825 qreal yPos = height + scaleY * domainMinY + plotArea.top();
839 qreal yPos = height + scaleY * domainMinY + plotArea.top();
826
840
827 for (int set = 0; set < setCount; set++) {
841 for (int set = 0; set < setCount; set++) {
828 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
842 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
829 qreal rectHeigth = barSets.at(set)->at(i) * percentage * scaleY;
843 qreal rectHeigth = barSets.at(set)->at(i) * percentage * scaleY;
830
844
831 QRectF rect(xPos, yPos-rectHeigth, rectWidth, rectHeigth);
845 QRectF rect(xPos, yPos-rectHeigth, rectWidth, rectHeigth);
832 layout.append(rect);
846 layout.append(rect);
833 yPos -= rectHeigth;
847 yPos -= rectHeigth;
834 }
848 }
835 }
849 }
836
850
837 //====================================================================================
851 //====================================================================================
838 // barset 1, bar 0
852 // barset 1, bar 0
839 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
853 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
840 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
854 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
841
855
842 QCOMPARE(seriesSpy.count(), 1);
856 QCOMPARE(seriesSpy.count(), 1);
843 QCOMPARE(setSpy1.count(), 1);
857 QCOMPARE(setSpy1.count(), 1);
844 QCOMPARE(setSpy2.count(), 0);
858 QCOMPARE(setSpy2.count(), 0);
845
859
846 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
860 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
847 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
861 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
848 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
862 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
849 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
863 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
850
864
851 QList<QVariant> setSpyArg = setSpy1.takeFirst();
865 QList<QVariant> setSpyArg = setSpy1.takeFirst();
852 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
866 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
853 QVERIFY(setSpyArg.at(0).toInt() == 0);
867 QVERIFY(setSpyArg.at(0).toInt() == 0);
854
868
855 //====================================================================================
869 //====================================================================================
856 // barset 1, bar 1
870 // barset 1, bar 1
857 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
871 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
858 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
872 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
859
873
860 QCOMPARE(seriesSpy.count(), 1);
874 QCOMPARE(seriesSpy.count(), 1);
861 QCOMPARE(setSpy1.count(), 1);
875 QCOMPARE(setSpy1.count(), 1);
862 QCOMPARE(setSpy2.count(), 0);
876 QCOMPARE(setSpy2.count(), 0);
863
877
864 seriesSpyArg = seriesSpy.takeFirst();
878 seriesSpyArg = seriesSpy.takeFirst();
865 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
879 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
866 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
880 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
867 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
881 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
868
882
869 setSpyArg = setSpy1.takeFirst();
883 setSpyArg = setSpy1.takeFirst();
870 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
884 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
871 QVERIFY(setSpyArg.at(0).toInt() == 1);
885 QVERIFY(setSpyArg.at(0).toInt() == 1);
872
886
873 //====================================================================================
887 //====================================================================================
874 // barset 1, bar 2
888 // barset 1, bar 2
875 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
889 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
876 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
890 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
877
891
878 QCOMPARE(seriesSpy.count(), 1);
892 QCOMPARE(seriesSpy.count(), 1);
879 QCOMPARE(setSpy1.count(), 1);
893 QCOMPARE(setSpy1.count(), 1);
880 QCOMPARE(setSpy2.count(), 0);
894 QCOMPARE(setSpy2.count(), 0);
881
895
882 seriesSpyArg = seriesSpy.takeFirst();
896 seriesSpyArg = seriesSpy.takeFirst();
883 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
897 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
884 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
898 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
885 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
899 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
886
900
887 setSpyArg = setSpy1.takeFirst();
901 setSpyArg = setSpy1.takeFirst();
888 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
902 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
889 QVERIFY(setSpyArg.at(0).toInt() == 2);
903 QVERIFY(setSpyArg.at(0).toInt() == 2);
890
904
891 //====================================================================================
905 //====================================================================================
892 // barset 2, bar 0
906 // barset 2, bar 0
893 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
907 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
894 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
908 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
895
909
896 QCOMPARE(seriesSpy.count(), 1);
910 QCOMPARE(seriesSpy.count(), 1);
897 QCOMPARE(setSpy1.count(), 0);
911 QCOMPARE(setSpy1.count(), 0);
898 QCOMPARE(setSpy2.count(), 1);
912 QCOMPARE(setSpy2.count(), 1);
899
913
900 seriesSpyArg = seriesSpy.takeFirst();
914 seriesSpyArg = seriesSpy.takeFirst();
901 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
915 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
902 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
916 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
903 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
917 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
904
918
905 setSpyArg = setSpy2.takeFirst();
919 setSpyArg = setSpy2.takeFirst();
906 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
920 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
907 QVERIFY(setSpyArg.at(0).toInt() == 0);
921 QVERIFY(setSpyArg.at(0).toInt() == 0);
908
922
909 //====================================================================================
923 //====================================================================================
910 // barset 2, bar 1
924 // barset 2, bar 1
911 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
925 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
912 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
926 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
913
927
914 QCOMPARE(seriesSpy.count(), 1);
928 QCOMPARE(seriesSpy.count(), 1);
915 QCOMPARE(setSpy1.count(), 0);
929 QCOMPARE(setSpy1.count(), 0);
916 QCOMPARE(setSpy2.count(), 1);
930 QCOMPARE(setSpy2.count(), 1);
917
931
918 seriesSpyArg = seriesSpy.takeFirst();
932 seriesSpyArg = seriesSpy.takeFirst();
919 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
933 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
920 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
934 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
921 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
935 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
922
936
923 setSpyArg = setSpy2.takeFirst();
937 setSpyArg = setSpy2.takeFirst();
924 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
938 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
925 QVERIFY(setSpyArg.at(0).toInt() == 1);
939 QVERIFY(setSpyArg.at(0).toInt() == 1);
926
940
927 //====================================================================================
941 //====================================================================================
928 // barset 2, bar 2
942 // barset 2, bar 2
929 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
943 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
930 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
944 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
931
945
932 QCOMPARE(seriesSpy.count(), 1);
946 QCOMPARE(seriesSpy.count(), 1);
933 QCOMPARE(setSpy1.count(), 0);
947 QCOMPARE(setSpy1.count(), 0);
934 QCOMPARE(setSpy2.count(), 1);
948 QCOMPARE(setSpy2.count(), 1);
935
949
936 seriesSpyArg = seriesSpy.takeFirst();
950 seriesSpyArg = seriesSpy.takeFirst();
937 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
951 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
938 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
952 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
939 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
953 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
940
954
941 setSpyArg = setSpy2.takeFirst();
955 setSpyArg = setSpy2.takeFirst();
942 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
956 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
943 QVERIFY(setSpyArg.at(0).toInt() == 2);
957 QVERIFY(setSpyArg.at(0).toInt() == 2);
944 }
958 }
945
959
946 void tst_QPercentBarSeries::mouseDoubleClicked()
960 void tst_QPercentBarSeries::mouseDoubleClicked()
947 {
961 {
948 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
962 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
949
963
950 QPercentBarSeries* series = new QPercentBarSeries();
964 QPercentBarSeries* series = new QPercentBarSeries();
951
965
952 QBarSet* set1 = new QBarSet(QString("set 1"));
966 QBarSet* set1 = new QBarSet(QString("set 1"));
953 *set1 << 10 << 10 << 10;
967 *set1 << 10 << 10 << 10;
954 series->append(set1);
968 series->append(set1);
955
969
956 QBarSet* set2 = new QBarSet(QString("set 2"));
970 QBarSet* set2 = new QBarSet(QString("set 2"));
957 *set2 << 10 << 10 << 10;
971 *set2 << 10 << 10 << 10;
958 series->append(set2);
972 series->append(set2);
959 QList<QBarSet*> barSets = series->barSets();
973 QList<QBarSet*> barSets = series->barSets();
960
974
961 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
975 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
962 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
976 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
963 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
977 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
964
978
965 QChartView view(new QChart());
979 QChartView view(new QChart());
966 view.resize(400,300);
980 view.resize(400,300);
967 view.chart()->addSeries(series);
981 view.chart()->addSeries(series);
968 view.show();
982 view.show();
969 QTest::qWaitForWindowShown(&view);
983 QTest::qWaitForWindowShown(&view);
970
984
971 // Calculate expected layout for bars
985 // Calculate expected layout for bars
972 QRectF plotArea = view.chart()->plotArea();
986 QRectF plotArea = view.chart()->plotArea();
973 qreal width = plotArea.width();
987 qreal width = plotArea.width();
974 qreal height = plotArea.height();
988 qreal height = plotArea.height();
975 qreal rangeY = 100; // From 0 to 100 because range is scaled to 100%
989 qreal rangeY = 100; // From 0 to 100 because range is scaled to 100%
976 qreal rangeX = 3; // 3 values per set
990 qreal rangeX = 3; // 3 values per set
977 qreal scaleY = (height / rangeY);
991 qreal scaleY = (height / rangeY);
978 qreal scaleX = (width / rangeX);
992 qreal scaleX = (width / rangeX);
979
993
980 qreal setCount = series->count();
994 qreal setCount = series->count();
981 qreal domainMinY = 0; // These come from internal domain used by barseries.
995 qreal domainMinY = 0; // These come from internal domain used by barseries.
982 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
996 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
983 qreal rectWidth = scaleX * series->barWidth();
997 qreal rectWidth = scaleX * series->barWidth();
984
998
985 QVector<QRectF> layout;
999 QVector<QRectF> layout;
986
1000
987 // 3 = count of values in set
1001 // 3 = count of values in set
988 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
1002 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
989 for (int i = 0; i < 3; i++) {
1003 for (int i = 0; i < 3; i++) {
990 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
1004 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
991 qreal percentage = (100 / colSum);
1005 qreal percentage = (100 / colSum);
992 qreal yPos = height + scaleY * domainMinY + plotArea.top();
1006 qreal yPos = height + scaleY * domainMinY + plotArea.top();
993
1007
994 for (int set = 0; set < setCount; set++) {
1008 for (int set = 0; set < setCount; set++) {
995 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
1009 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
996 qreal rectHeigth = barSets.at(set)->at(i) * percentage * scaleY;
1010 qreal rectHeigth = barSets.at(set)->at(i) * percentage * scaleY;
997
1011
998 QRectF rect(xPos, yPos-rectHeigth, rectWidth, rectHeigth);
1012 QRectF rect(xPos, yPos-rectHeigth, rectWidth, rectHeigth);
999 layout.append(rect);
1013 layout.append(rect);
1000 yPos -= rectHeigth;
1014 yPos -= rectHeigth;
1001 }
1015 }
1002 }
1016 }
1003
1017
1004 // barset 1, bar 0
1018 // barset 1, bar 0
1005 QTest::mouseDClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1019 QTest::mouseDClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1006 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1020 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1007
1021
1008 QCOMPARE(seriesSpy.count(), 1);
1022 QCOMPARE(seriesSpy.count(), 1);
1009 QCOMPARE(setSpy1.count(), 1);
1023 QCOMPARE(setSpy1.count(), 1);
1010 QCOMPARE(setSpy2.count(), 0);
1024 QCOMPARE(setSpy2.count(), 0);
1011
1025
1012 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1026 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1013 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1027 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1014 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1028 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1015 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1029 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1016
1030
1017 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1031 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1018 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1032 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1019 QVERIFY(setSpyArg.at(0).toInt() == 0);
1033 QVERIFY(setSpyArg.at(0).toInt() == 0);
1020 }
1034 }
1021
1035
1022 QTEST_MAIN(tst_QPercentBarSeries)
1036 QTEST_MAIN(tst_QPercentBarSeries)
1023
1037
1024 #include "tst_qpercentbarseries.moc"
1038 #include "tst_qpercentbarseries.moc"
1025
1039
@@ -1,1020 +1,1034
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd
3 ** Copyright (C) 2015 The Qt Company Ltd
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 **
6 **
7 ** This file is part of the Qt Charts module.
7 ** This file is part of the Qt Charts module.
8 **
8 **
9 ** Licensees holding valid commercial license for Qt may use this file in
9 ** Licensees holding valid commercial license for Qt may use this file in
10 ** accordance with the Qt License Agreement provided with the Software
10 ** accordance with the Qt License Agreement provided with the Software
11 ** or, alternatively, in accordance with the terms contained in a written
11 ** or, alternatively, in accordance with the terms contained in a written
12 ** agreement between you and The Qt Company.
12 ** agreement between you and The Qt Company.
13 **
13 **
14 ** If you have questions regarding the use of this file, please use
14 ** If you have questions regarding the use of this file, please use
15 ** contact form at http://qt.io
15 ** contact form at http://qt.io
16 **
16 **
17 ****************************************************************************/
17 ****************************************************************************/
18
18
19 #include <QtTest/QtTest>
19 #include <QtTest/QtTest>
20 #include <QtCharts/QStackedBarSeries>
20 #include <QtCharts/QStackedBarSeries>
21 #include <QtCharts/QBarSet>
21 #include <QtCharts/QBarSet>
22 #include <QtCharts/QChartView>
22 #include <QtCharts/QChartView>
23 #include <QtCharts/QChart>
23 #include <QtCharts/QChart>
24 #include "tst_definitions.h"
24 #include "tst_definitions.h"
25
25
26 QT_CHARTS_USE_NAMESPACE
26 QT_CHARTS_USE_NAMESPACE
27
27
28 Q_DECLARE_METATYPE(QBarSet*)
28 Q_DECLARE_METATYPE(QBarSet*)
29 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
29 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
30
30
31 class tst_QStackedBarSeries : public QObject
31 class tst_QStackedBarSeries : public QObject
32 {
32 {
33 Q_OBJECT
33 Q_OBJECT
34
34
35 public slots:
35 public slots:
36 void initTestCase();
36 void initTestCase();
37 void cleanupTestCase();
37 void cleanupTestCase();
38 void init();
38 void init();
39 void cleanup();
39 void cleanup();
40
40
41 private slots:
41 private slots:
42 void qstackedbarseries_data();
42 void qstackedbarseries_data();
43 void qstackedbarseries();
43 void qstackedbarseries();
44 void type_data();
44 void type_data();
45 void type();
45 void type();
46 void setLabelsFormat();
46 void setLabelsFormat();
47 void setLabelsPosition();
47 void setLabelsPosition();
48 void setLabelsAngle();
48 void mouseclicked_data();
49 void mouseclicked_data();
49 void mouseclicked();
50 void mouseclicked();
50 void mousehovered_data();
51 void mousehovered_data();
51 void mousehovered();
52 void mousehovered();
52 void mousePressed();
53 void mousePressed();
53 void mouseReleased();
54 void mouseReleased();
54 void mouseDoubleClicked();
55 void mouseDoubleClicked();
55
56
56 private:
57 private:
57 QStackedBarSeries* m_barseries;
58 QStackedBarSeries* m_barseries;
58 };
59 };
59
60
60 void tst_QStackedBarSeries::initTestCase()
61 void tst_QStackedBarSeries::initTestCase()
61 {
62 {
62 qRegisterMetaType<QBarSet*>("QBarSet*");
63 qRegisterMetaType<QBarSet*>("QBarSet*");
63 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
64 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
64 }
65 }
65
66
66 void tst_QStackedBarSeries::cleanupTestCase()
67 void tst_QStackedBarSeries::cleanupTestCase()
67 {
68 {
68 QTest::qWait(1); // Allow final deleteLaters to run
69 QTest::qWait(1); // Allow final deleteLaters to run
69 }
70 }
70
71
71 void tst_QStackedBarSeries::init()
72 void tst_QStackedBarSeries::init()
72 {
73 {
73 m_barseries = new QStackedBarSeries();
74 m_barseries = new QStackedBarSeries();
74 }
75 }
75
76
76 void tst_QStackedBarSeries::cleanup()
77 void tst_QStackedBarSeries::cleanup()
77 {
78 {
78 delete m_barseries;
79 delete m_barseries;
79 m_barseries = 0;
80 m_barseries = 0;
80 }
81 }
81
82
82 void tst_QStackedBarSeries::qstackedbarseries_data()
83 void tst_QStackedBarSeries::qstackedbarseries_data()
83 {
84 {
84 }
85 }
85
86
86 void tst_QStackedBarSeries::qstackedbarseries()
87 void tst_QStackedBarSeries::qstackedbarseries()
87 {
88 {
88 QStackedBarSeries *barseries = new QStackedBarSeries();
89 QStackedBarSeries *barseries = new QStackedBarSeries();
89 QVERIFY(barseries != 0);
90 QVERIFY(barseries != 0);
90 delete barseries;
91 delete barseries;
91 }
92 }
92
93
93 void tst_QStackedBarSeries::type_data()
94 void tst_QStackedBarSeries::type_data()
94 {
95 {
95
96
96 }
97 }
97
98
98 void tst_QStackedBarSeries::type()
99 void tst_QStackedBarSeries::type()
99 {
100 {
100 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeStackedBar);
101 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeStackedBar);
101 }
102 }
102
103
103 void tst_QStackedBarSeries::setLabelsFormat()
104 void tst_QStackedBarSeries::setLabelsFormat()
104 {
105 {
105 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
106 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
106 QCOMPARE(m_barseries->labelsFormat(), QString());
107 QCOMPARE(m_barseries->labelsFormat(), QString());
107
108
108 QString format("(@value)");
109 QString format("(@value)");
109 m_barseries->setLabelsFormat(format);
110 m_barseries->setLabelsFormat(format);
110 TRY_COMPARE(labelsFormatSpy.count(), 1);
111 TRY_COMPARE(labelsFormatSpy.count(), 1);
111 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
112 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
112 QVERIFY(arguments.at(0).toString() == format);
113 QVERIFY(arguments.at(0).toString() == format);
113 QCOMPARE(m_barseries->labelsFormat(), format);
114 QCOMPARE(m_barseries->labelsFormat(), format);
114
115
115 m_barseries->setLabelsFormat(QString());
116 m_barseries->setLabelsFormat(QString());
116 TRY_COMPARE(labelsFormatSpy.count(), 1);
117 TRY_COMPARE(labelsFormatSpy.count(), 1);
117 arguments = labelsFormatSpy.takeFirst();
118 arguments = labelsFormatSpy.takeFirst();
118 QVERIFY(arguments.at(0).toString() == QString());
119 QVERIFY(arguments.at(0).toString() == QString());
119 QCOMPARE(m_barseries->labelsFormat(), QString());
120 QCOMPARE(m_barseries->labelsFormat(), QString());
120 }
121 }
121
122
122 void tst_QStackedBarSeries::setLabelsPosition()
123 void tst_QStackedBarSeries::setLabelsPosition()
123 {
124 {
124 QSignalSpy labelsPositionSpy(m_barseries,
125 QSignalSpy labelsPositionSpy(m_barseries,
125 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
126 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
126 QCOMPARE(m_barseries->labelsPosition(), QStackedBarSeries::LabelsCenter);
127 QCOMPARE(m_barseries->labelsPosition(), QStackedBarSeries::LabelsCenter);
127
128
128 m_barseries->setLabelsPosition(QStackedBarSeries::LabelsInsideEnd);
129 m_barseries->setLabelsPosition(QStackedBarSeries::LabelsInsideEnd);
129 TRY_COMPARE(labelsPositionSpy.count(), 1);
130 TRY_COMPARE(labelsPositionSpy.count(), 1);
130 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
131 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
131 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
132 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
132 == QStackedBarSeries::LabelsInsideEnd);
133 == QStackedBarSeries::LabelsInsideEnd);
133 QCOMPARE(m_barseries->labelsPosition(), QStackedBarSeries::LabelsInsideEnd);
134 QCOMPARE(m_barseries->labelsPosition(), QStackedBarSeries::LabelsInsideEnd);
134
135
135 m_barseries->setLabelsPosition(QStackedBarSeries::LabelsInsideBase);
136 m_barseries->setLabelsPosition(QStackedBarSeries::LabelsInsideBase);
136 TRY_COMPARE(labelsPositionSpy.count(), 1);
137 TRY_COMPARE(labelsPositionSpy.count(), 1);
137 arguments = labelsPositionSpy.takeFirst();
138 arguments = labelsPositionSpy.takeFirst();
138 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
139 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
139 == QStackedBarSeries::LabelsInsideBase);
140 == QStackedBarSeries::LabelsInsideBase);
140 QCOMPARE(m_barseries->labelsPosition(), QStackedBarSeries::LabelsInsideBase);
141 QCOMPARE(m_barseries->labelsPosition(), QStackedBarSeries::LabelsInsideBase);
141
142
142 m_barseries->setLabelsPosition(QStackedBarSeries::LabelsOutsideEnd);
143 m_barseries->setLabelsPosition(QStackedBarSeries::LabelsOutsideEnd);
143 TRY_COMPARE(labelsPositionSpy.count(), 1);
144 TRY_COMPARE(labelsPositionSpy.count(), 1);
144 arguments = labelsPositionSpy.takeFirst();
145 arguments = labelsPositionSpy.takeFirst();
145 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
146 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
146 == QStackedBarSeries::LabelsOutsideEnd);
147 == QStackedBarSeries::LabelsOutsideEnd);
147 QCOMPARE(m_barseries->labelsPosition(), QStackedBarSeries::LabelsOutsideEnd);
148 QCOMPARE(m_barseries->labelsPosition(), QStackedBarSeries::LabelsOutsideEnd);
148
149
149 m_barseries->setLabelsPosition(QStackedBarSeries::LabelsCenter);
150 m_barseries->setLabelsPosition(QStackedBarSeries::LabelsCenter);
150 TRY_COMPARE(labelsPositionSpy.count(), 1);
151 TRY_COMPARE(labelsPositionSpy.count(), 1);
151 arguments = labelsPositionSpy.takeFirst();
152 arguments = labelsPositionSpy.takeFirst();
152 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
153 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
153 == QStackedBarSeries::LabelsCenter);
154 == QStackedBarSeries::LabelsCenter);
154 QCOMPARE(m_barseries->labelsPosition(), QStackedBarSeries::LabelsCenter);
155 QCOMPARE(m_barseries->labelsPosition(), QStackedBarSeries::LabelsCenter);
155 }
156 }
156
157
158 void tst_QStackedBarSeries::setLabelsAngle()
159 {
160 QSignalSpy labelsAngleSpy(m_barseries,
161 SIGNAL(labelsAngleChanged(qreal)));
162 QCOMPARE(m_barseries->labelsAngle(), 0.0);
163
164 m_barseries->setLabelsAngle(55.0);
165 TRY_COMPARE(labelsAngleSpy.count(), 1);
166 QList<QVariant> arguments = labelsAngleSpy.takeFirst();
167 QVERIFY(arguments.at(0).value<qreal>() == 55.0);
168 QCOMPARE(m_barseries->labelsAngle(), 55.0);
169 }
170
157 void tst_QStackedBarSeries::mouseclicked_data()
171 void tst_QStackedBarSeries::mouseclicked_data()
158 {
172 {
159
173
160 }
174 }
161
175
162 void tst_QStackedBarSeries::mouseclicked()
176 void tst_QStackedBarSeries::mouseclicked()
163 {
177 {
164 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
178 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
165
179
166 QStackedBarSeries* series = new QStackedBarSeries();
180 QStackedBarSeries* series = new QStackedBarSeries();
167
181
168 QBarSet* set1 = new QBarSet(QString("set 1"));
182 QBarSet* set1 = new QBarSet(QString("set 1"));
169 *set1 << 10 << 10 << 10;
183 *set1 << 10 << 10 << 10;
170 series->append(set1);
184 series->append(set1);
171
185
172 QBarSet* set2 = new QBarSet(QString("set 2"));
186 QBarSet* set2 = new QBarSet(QString("set 2"));
173 *set2 << 10 << 10 << 10;
187 *set2 << 10 << 10 << 10;
174 series->append(set2);
188 series->append(set2);
175
189
176 QList<QBarSet*> barSets = series->barSets();
190 QList<QBarSet*> barSets = series->barSets();
177
191
178 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
192 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
179
193
180 QChartView view(new QChart());
194 QChartView view(new QChart());
181 view.resize(400,300);
195 view.resize(400,300);
182 view.chart()->addSeries(series);
196 view.chart()->addSeries(series);
183 view.show();
197 view.show();
184 QTest::qWaitForWindowShown(&view);
198 QTest::qWaitForWindowShown(&view);
185
199
186 // Calculate expected layout for bars
200 // Calculate expected layout for bars
187 QRectF plotArea = view.chart()->plotArea();
201 QRectF plotArea = view.chart()->plotArea();
188 qreal width = plotArea.width();
202 qreal width = plotArea.width();
189 qreal height = plotArea.height();
203 qreal height = plotArea.height();
190 qreal rangeY = 20; // From 0 to 20 because sets are stacked (this should be height of highest stack)
204 qreal rangeY = 20; // From 0 to 20 because sets are stacked (this should be height of highest stack)
191 qreal rangeX = 3; // 3 values per set
205 qreal rangeX = 3; // 3 values per set
192 qreal scaleY = (height / rangeY);
206 qreal scaleY = (height / rangeY);
193 qreal scaleX = (width / rangeX);
207 qreal scaleX = (width / rangeX);
194
208
195 qreal setCount = series->count();
209 qreal setCount = series->count();
196 qreal domainMinY = 0; // These come from internal domain used by barseries.
210 qreal domainMinY = 0; // These come from internal domain used by barseries.
197 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
211 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
198 qreal rectWidth = scaleX * series->barWidth();
212 qreal rectWidth = scaleX * series->barWidth();
199
213
200 QVector<QRectF> layout;
214 QVector<QRectF> layout;
201
215
202 // 3 = count of values in set
216 // 3 = count of values in set
203 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
217 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
204 for (int i = 0; i < 3; i++) {
218 for (int i = 0; i < 3; i++) {
205 qreal yMax = height + scaleY * domainMinY + plotArea.top();
219 qreal yMax = height + scaleY * domainMinY + plotArea.top();
206 qreal yMin = height + scaleY * domainMinY + plotArea.top();
220 qreal yMin = height + scaleY * domainMinY + plotArea.top();
207 for (int set = 0; set < setCount; set++) {
221 for (int set = 0; set < setCount; set++) {
208 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
222 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
209 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
223 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
210 if (rectHeight < 0) {
224 if (rectHeight < 0) {
211 QRectF rect(xPos, yMax-rectHeight, rectWidth, rectHeight);
225 QRectF rect(xPos, yMax-rectHeight, rectWidth, rectHeight);
212 layout.append(rect);
226 layout.append(rect);
213 yMax -= rectHeight;
227 yMax -= rectHeight;
214 } else {
228 } else {
215 QRectF rect(xPos, yMin-rectHeight, rectWidth, rectHeight);
229 QRectF rect(xPos, yMin-rectHeight, rectWidth, rectHeight);
216 layout.append(rect);
230 layout.append(rect);
217 yMin -= rectHeight;
231 yMin -= rectHeight;
218 }
232 }
219 }
233 }
220 }
234 }
221
235
222 //====================================================================================
236 //====================================================================================
223 // barset 1, bar 0
237 // barset 1, bar 0
224 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
238 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
225 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
239 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
226
240
227 QCOMPARE(seriesSpy.count(), 1);
241 QCOMPARE(seriesSpy.count(), 1);
228
242
229 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
243 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
230 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
244 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
231 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
245 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
232 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
246 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
233
247
234 //====================================================================================
248 //====================================================================================
235 // barset 1, bar 1
249 // barset 1, bar 1
236 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
250 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
237 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
251 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
238
252
239 QCOMPARE(seriesSpy.count(), 1);
253 QCOMPARE(seriesSpy.count(), 1);
240
254
241 seriesSpyArg = seriesSpy.takeFirst();
255 seriesSpyArg = seriesSpy.takeFirst();
242 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
256 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
243 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
257 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
244 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
258 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
245
259
246 //====================================================================================
260 //====================================================================================
247 // barset 1, bar 2
261 // barset 1, bar 2
248 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
262 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
249 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
263 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
250
264
251 QCOMPARE(seriesSpy.count(), 1);
265 QCOMPARE(seriesSpy.count(), 1);
252
266
253 seriesSpyArg = seriesSpy.takeFirst();
267 seriesSpyArg = seriesSpy.takeFirst();
254 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
268 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
255 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
269 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
256 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
270 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
257
271
258 //====================================================================================
272 //====================================================================================
259 // barset 2, bar 0
273 // barset 2, bar 0
260 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
274 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
261 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
275 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
262
276
263 QCOMPARE(seriesSpy.count(), 1);
277 QCOMPARE(seriesSpy.count(), 1);
264
278
265 seriesSpyArg = seriesSpy.takeFirst();
279 seriesSpyArg = seriesSpy.takeFirst();
266 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
280 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
267 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
281 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
268 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
282 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
269
283
270 //====================================================================================
284 //====================================================================================
271 // barset 2, bar 1
285 // barset 2, bar 1
272 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
286 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
273 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
287 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
274
288
275 QCOMPARE(seriesSpy.count(), 1);
289 QCOMPARE(seriesSpy.count(), 1);
276
290
277 seriesSpyArg = seriesSpy.takeFirst();
291 seriesSpyArg = seriesSpy.takeFirst();
278 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
292 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
279 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
293 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
280 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
294 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
281
295
282 //====================================================================================
296 //====================================================================================
283 // barset 2, bar 2
297 // barset 2, bar 2
284 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
298 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
285 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
299 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
286
300
287 QCOMPARE(seriesSpy.count(), 1);
301 QCOMPARE(seriesSpy.count(), 1);
288
302
289 seriesSpyArg = seriesSpy.takeFirst();
303 seriesSpyArg = seriesSpy.takeFirst();
290 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
304 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
291 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
305 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
292 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
306 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
293 }
307 }
294
308
295 void tst_QStackedBarSeries::mousehovered_data()
309 void tst_QStackedBarSeries::mousehovered_data()
296 {
310 {
297
311
298 }
312 }
299
313
300 void tst_QStackedBarSeries::mousehovered()
314 void tst_QStackedBarSeries::mousehovered()
301 {
315 {
302 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
316 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
303
317
304 QStackedBarSeries* series = new QStackedBarSeries();
318 QStackedBarSeries* series = new QStackedBarSeries();
305
319
306 QBarSet* set1 = new QBarSet(QString("set 1"));
320 QBarSet* set1 = new QBarSet(QString("set 1"));
307 *set1 << 10 << 10 << 10;
321 *set1 << 10 << 10 << 10;
308 series->append(set1);
322 series->append(set1);
309
323
310 QBarSet* set2 = new QBarSet(QString("set 2"));
324 QBarSet* set2 = new QBarSet(QString("set 2"));
311 *set2 << 10 << 10 << 10;
325 *set2 << 10 << 10 << 10;
312 series->append(set2);
326 series->append(set2);
313
327
314 QList<QBarSet*> barSets = series->barSets();
328 QList<QBarSet*> barSets = series->barSets();
315
329
316 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
330 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
317 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
331 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
318 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
332 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
319
333
320 QChartView view(new QChart());
334 QChartView view(new QChart());
321 view.resize(400,300);
335 view.resize(400,300);
322 view.chart()->addSeries(series);
336 view.chart()->addSeries(series);
323 view.show();
337 view.show();
324 QTest::qWaitForWindowShown(&view);
338 QTest::qWaitForWindowShown(&view);
325
339
326 //this is hack since view does not get events otherwise
340 //this is hack since view does not get events otherwise
327 view.setMouseTracking(true);
341 view.setMouseTracking(true);
328
342
329 // Calculate expected layout for bars
343 // Calculate expected layout for bars
330 QRectF plotArea = view.chart()->plotArea();
344 QRectF plotArea = view.chart()->plotArea();
331 qreal width = plotArea.width();
345 qreal width = plotArea.width();
332 qreal height = plotArea.height();
346 qreal height = plotArea.height();
333 qreal rangeY = 20; // From 0 to 20 because sets are stacked (this should be height of highest stack)
347 qreal rangeY = 20; // From 0 to 20 because sets are stacked (this should be height of highest stack)
334 qreal rangeX = 3; // 3 values per set
348 qreal rangeX = 3; // 3 values per set
335 qreal scaleY = (height / rangeY);
349 qreal scaleY = (height / rangeY);
336 qreal scaleX = (width / rangeX);
350 qreal scaleX = (width / rangeX);
337
351
338 qreal setCount = series->count();
352 qreal setCount = series->count();
339 qreal domainMinY = 0; // These come from internal domain used by barseries.
353 qreal domainMinY = 0; // These come from internal domain used by barseries.
340 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
354 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
341 qreal rectWidth = scaleX * series->barWidth();
355 qreal rectWidth = scaleX * series->barWidth();
342
356
343 QVector<QRectF> layout;
357 QVector<QRectF> layout;
344
358
345 // 3 = count of values in set
359 // 3 = count of values in set
346 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
360 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
347 for (int i = 0; i < 3; i++) {
361 for (int i = 0; i < 3; i++) {
348 qreal yMax = height + scaleY * domainMinY + plotArea.top();
362 qreal yMax = height + scaleY * domainMinY + plotArea.top();
349 qreal yMin = height + scaleY * domainMinY + plotArea.top();
363 qreal yMin = height + scaleY * domainMinY + plotArea.top();
350 for (int set = 0; set < setCount; set++) {
364 for (int set = 0; set < setCount; set++) {
351 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
365 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
352 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
366 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
353
367
354 if (rectHeight < 0) {
368 if (rectHeight < 0) {
355 QRectF rect(xPos, yMax-rectHeight, rectWidth, rectHeight);
369 QRectF rect(xPos, yMax-rectHeight, rectWidth, rectHeight);
356 layout.append(rect);
370 layout.append(rect);
357 yMax -= rectHeight;
371 yMax -= rectHeight;
358 } else {
372 } else {
359 QRectF rect(xPos, yMin-rectHeight, rectWidth, rectHeight);
373 QRectF rect(xPos, yMin-rectHeight, rectWidth, rectHeight);
360 layout.append(rect);
374 layout.append(rect);
361 yMin -= rectHeight;
375 yMin -= rectHeight;
362 }
376 }
363 }
377 }
364 }
378 }
365
379
366 //=======================================================================
380 //=======================================================================
367 // move mouse to left border
381 // move mouse to left border
368 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(0).center().y()));
382 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(0).center().y()));
369 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
383 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
370 TRY_COMPARE(seriesIndexSpy.count(), 0);
384 TRY_COMPARE(seriesIndexSpy.count(), 0);
371
385
372 //=======================================================================
386 //=======================================================================
373 // move mouse on top of set1
387 // move mouse on top of set1
374 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
388 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
375 TRY_COMPARE(seriesIndexSpy.count(), 1);
389 TRY_COMPARE(seriesIndexSpy.count(), 1);
376 TRY_COMPARE(setIndexSpy1.count(), 1);
390 TRY_COMPARE(setIndexSpy1.count(), 1);
377 TRY_COMPARE(setIndexSpy2.count(), 0);
391 TRY_COMPARE(setIndexSpy2.count(), 0);
378
392
379 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
393 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
380 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
394 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
381 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
395 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
382 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
396 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
383
397
384 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
398 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
385 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
399 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
386 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
400 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
387
401
388 //=======================================================================
402 //=======================================================================
389 // move mouse from top of set1 to top of set2
403 // move mouse from top of set1 to top of set2
390 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
404 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
391 TRY_COMPARE(seriesIndexSpy.count(), 2);
405 TRY_COMPARE(seriesIndexSpy.count(), 2);
392 TRY_COMPARE(setIndexSpy1.count(), 1);
406 TRY_COMPARE(setIndexSpy1.count(), 1);
393 TRY_COMPARE(setIndexSpy2.count(), 1);
407 TRY_COMPARE(setIndexSpy2.count(), 1);
394
408
395 // should leave set1
409 // should leave set1
396 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
410 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
397 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
411 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
398 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
412 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
399 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
413 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
400
414
401 setIndexSpyArg = setIndexSpy1.takeFirst();
415 setIndexSpyArg = setIndexSpy1.takeFirst();
402 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
416 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
403 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
417 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
404
418
405 // should enter set2
419 // should enter set2
406 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
420 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
407 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
421 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
408 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
422 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
409 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
423 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
410
424
411 setIndexSpyArg = setIndexSpy2.takeFirst();
425 setIndexSpyArg = setIndexSpy2.takeFirst();
412 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
426 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
413 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
427 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
414
428
415 //=======================================================================
429 //=======================================================================
416 // move mouse from top of set2 to background
430 // move mouse from top of set2 to background
417 QTest::mouseMove(view.viewport(), QPoint(layout.at(1).center().x(), 0));
431 QTest::mouseMove(view.viewport(), QPoint(layout.at(1).center().x(), 0));
418 TRY_COMPARE(seriesIndexSpy.count(), 1);
432 TRY_COMPARE(seriesIndexSpy.count(), 1);
419 TRY_COMPARE(setIndexSpy1.count(), 0);
433 TRY_COMPARE(setIndexSpy1.count(), 0);
420 TRY_COMPARE(setIndexSpy2.count(), 1);
434 TRY_COMPARE(setIndexSpy2.count(), 1);
421
435
422 // should leave set2
436 // should leave set2
423 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
437 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
424 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
438 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
425 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
439 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
426 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
440 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
427
441
428 setIndexSpyArg = setIndexSpy2.takeFirst();
442 setIndexSpyArg = setIndexSpy2.takeFirst();
429 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
443 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
430 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
444 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
431
445
432 //=======================================================================
446 //=======================================================================
433 // move mouse on top of set1, bar0 to check the index (hover into set1)
447 // move mouse on top of set1, bar0 to check the index (hover into set1)
434 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
448 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
435
449
436 TRY_COMPARE(seriesIndexSpy.count(), 1);
450 TRY_COMPARE(seriesIndexSpy.count(), 1);
437 TRY_COMPARE(setIndexSpy1.count(), 1);
451 TRY_COMPARE(setIndexSpy1.count(), 1);
438 TRY_COMPARE(setIndexSpy2.count(), 0);
452 TRY_COMPARE(setIndexSpy2.count(), 0);
439
453
440 //should enter set1, bar0
454 //should enter set1, bar0
441 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
455 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
442 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
456 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
443 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
457 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
444 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
458 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
445 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
459 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
446 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
460 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
447
461
448 setIndexSpyArg = setIndexSpy1.takeFirst();
462 setIndexSpyArg = setIndexSpy1.takeFirst();
449 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
463 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
450 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
464 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
451 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
465 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
452 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
466 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
453
467
454 //=======================================================================
468 //=======================================================================
455 // move mouse on top of set2, bar0 to check the index (hover out set1,
469 // move mouse on top of set2, bar0 to check the index (hover out set1,
456 // hover in set2)
470 // hover in set2)
457 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
471 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
458
472
459 TRY_COMPARE(seriesIndexSpy.count(), 2);
473 TRY_COMPARE(seriesIndexSpy.count(), 2);
460 TRY_COMPARE(setIndexSpy1.count(), 1);
474 TRY_COMPARE(setIndexSpy1.count(), 1);
461 TRY_COMPARE(setIndexSpy2.count(), 1);
475 TRY_COMPARE(setIndexSpy2.count(), 1);
462
476
463 //should leave set1, bar0
477 //should leave set1, bar0
464 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
478 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
465 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
479 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
466 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
480 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
467 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
481 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
468 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
482 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
469 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
483 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
470
484
471 setIndexSpyArg = setIndexSpy1.takeFirst();
485 setIndexSpyArg = setIndexSpy1.takeFirst();
472 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
486 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
473 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
487 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
474 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
488 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
475 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
489 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
476
490
477 //should enter set2, bar0
491 //should enter set2, bar0
478 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
492 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
479 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
493 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
480 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
494 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
481 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
495 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
482 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
496 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
483 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
497 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
484
498
485 setIndexSpyArg = setIndexSpy2.takeFirst();
499 setIndexSpyArg = setIndexSpy2.takeFirst();
486 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
500 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
487 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
501 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
488 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
502 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
489 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
503 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
490
504
491 //=======================================================================
505 //=======================================================================
492 // move mouse on top of set1, bar1 to check the index (hover out set2,
506 // move mouse on top of set1, bar1 to check the index (hover out set2,
493 // hover in set1)
507 // hover in set1)
494 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
508 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
495
509
496 TRY_COMPARE(seriesIndexSpy.count(), 2);
510 TRY_COMPARE(seriesIndexSpy.count(), 2);
497 TRY_COMPARE(setIndexSpy1.count(), 1);
511 TRY_COMPARE(setIndexSpy1.count(), 1);
498 TRY_COMPARE(setIndexSpy2.count(), 1);
512 TRY_COMPARE(setIndexSpy2.count(), 1);
499
513
500 //should leave set2, bar0
514 //should leave set2, bar0
501 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
515 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
502 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
516 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
503 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
517 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
504 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
518 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
505 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
519 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
506 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
520 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
507
521
508 setIndexSpyArg = setIndexSpy2.takeFirst();
522 setIndexSpyArg = setIndexSpy2.takeFirst();
509 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
523 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
510 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
524 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
511 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
525 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
512 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
526 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
513
527
514 //should enter set1, bar1
528 //should enter set1, bar1
515 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
529 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
516 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
530 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
517 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
531 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
518 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
532 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
519 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
533 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
520 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
534 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
521
535
522 setIndexSpyArg = setIndexSpy1.takeFirst();
536 setIndexSpyArg = setIndexSpy1.takeFirst();
523 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
537 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
524 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
538 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
525 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
539 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
526 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
540 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
527
541
528 //=======================================================================
542 //=======================================================================
529 // move mouse between set1 and set2 (hover out set1)
543 // move mouse between set1 and set2 (hover out set1)
530 QTest::mouseMove(view.viewport(), QPoint((layout.at(3).right() + layout.at(4).left()) /2,
544 QTest::mouseMove(view.viewport(), QPoint((layout.at(3).right() + layout.at(4).left()) /2,
531 layout.at(2).top()));
545 layout.at(2).top()));
532
546
533 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
547 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
534 TRY_COMPARE(seriesIndexSpy.count(), 1);
548 TRY_COMPARE(seriesIndexSpy.count(), 1);
535 TRY_COMPARE(setIndexSpy1.count(), 1);
549 TRY_COMPARE(setIndexSpy1.count(), 1);
536 TRY_COMPARE(setIndexSpy2.count(), 0);
550 TRY_COMPARE(setIndexSpy2.count(), 0);
537
551
538 //should leave set1, bar1
552 //should leave set1, bar1
539 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
553 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
540 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
554 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
541 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
555 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
542 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
556 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
543 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
557 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
544 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
558 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
545
559
546 setIndexSpyArg = setIndexSpy1.takeFirst();
560 setIndexSpyArg = setIndexSpy1.takeFirst();
547 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
561 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
548 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
562 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
549 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
563 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
550 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
564 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
551
565
552 //=======================================================================
566 //=======================================================================
553 // move mouse on top of set2, bar1 to check the index (hover in set2)
567 // move mouse on top of set2, bar1 to check the index (hover in set2)
554 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
568 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
555
569
556 TRY_COMPARE(seriesIndexSpy.count(), 1);
570 TRY_COMPARE(seriesIndexSpy.count(), 1);
557 TRY_COMPARE(setIndexSpy1.count(), 0);
571 TRY_COMPARE(setIndexSpy1.count(), 0);
558 TRY_COMPARE(setIndexSpy2.count(), 1);
572 TRY_COMPARE(setIndexSpy2.count(), 1);
559
573
560 //should enter set2, bar1
574 //should enter set2, bar1
561 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
575 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
562 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
576 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
563 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
577 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
564 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
578 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
565 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
579 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
566 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
580 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
567
581
568 setIndexSpyArg = setIndexSpy2.takeFirst();
582 setIndexSpyArg = setIndexSpy2.takeFirst();
569 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
583 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
570 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
584 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
571 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
585 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
572 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
586 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
573
587
574 //=======================================================================
588 //=======================================================================
575 // move mouse between set1 and set2 (hover out set2)
589 // move mouse between set1 and set2 (hover out set2)
576 QTest::mouseMove(view.viewport(), QPoint((layout.at(3).right() + layout.at(4).left()) /2,
590 QTest::mouseMove(view.viewport(), QPoint((layout.at(3).right() + layout.at(4).left()) /2,
577 layout.at(2).top()));
591 layout.at(2).top()));
578
592
579 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
593 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
580 TRY_COMPARE(seriesIndexSpy.count(), 1);
594 TRY_COMPARE(seriesIndexSpy.count(), 1);
581 TRY_COMPARE(setIndexSpy1.count(), 0);
595 TRY_COMPARE(setIndexSpy1.count(), 0);
582 TRY_COMPARE(setIndexSpy2.count(), 1);
596 TRY_COMPARE(setIndexSpy2.count(), 1);
583
597
584 //should leave set1, bar1
598 //should leave set1, bar1
585 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
599 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
586 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
600 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
587 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
601 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
588 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
602 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
589 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
603 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
590 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
604 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
591
605
592 setIndexSpyArg = setIndexSpy2.takeFirst();
606 setIndexSpyArg = setIndexSpy2.takeFirst();
593 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
607 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
594 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
608 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
595 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
609 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
596 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
610 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
597 }
611 }
598
612
599 void tst_QStackedBarSeries::mousePressed()
613 void tst_QStackedBarSeries::mousePressed()
600 {
614 {
601 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
615 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
602
616
603 QStackedBarSeries* series = new QStackedBarSeries();
617 QStackedBarSeries* series = new QStackedBarSeries();
604
618
605 QBarSet* set1 = new QBarSet(QString("set 1"));
619 QBarSet* set1 = new QBarSet(QString("set 1"));
606 *set1 << 10 << 10 << 10;
620 *set1 << 10 << 10 << 10;
607 series->append(set1);
621 series->append(set1);
608
622
609 QBarSet* set2 = new QBarSet(QString("set 2"));
623 QBarSet* set2 = new QBarSet(QString("set 2"));
610 *set2 << 10 << 10 << 10;
624 *set2 << 10 << 10 << 10;
611 series->append(set2);
625 series->append(set2);
612 QList<QBarSet*> barSets = series->barSets();
626 QList<QBarSet*> barSets = series->barSets();
613
627
614 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
628 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
615 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
629 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
616 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
630 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
617
631
618 QChartView view(new QChart());
632 QChartView view(new QChart());
619 view.resize(400,300);
633 view.resize(400,300);
620 view.chart()->addSeries(series);
634 view.chart()->addSeries(series);
621 view.show();
635 view.show();
622 QTest::qWaitForWindowShown(&view);
636 QTest::qWaitForWindowShown(&view);
623
637
624 // Calculate expected layout for bars
638 // Calculate expected layout for bars
625 QRectF plotArea = view.chart()->plotArea();
639 QRectF plotArea = view.chart()->plotArea();
626 qreal width = plotArea.width();
640 qreal width = plotArea.width();
627 qreal height = plotArea.height();
641 qreal height = plotArea.height();
628 qreal rangeY = 20; // From 0 to 20 because sets are stacked (this should be height of highest stack)
642 qreal rangeY = 20; // From 0 to 20 because sets are stacked (this should be height of highest stack)
629 qreal rangeX = 3; // 3 values per set
643 qreal rangeX = 3; // 3 values per set
630 qreal scaleY = (height / rangeY);
644 qreal scaleY = (height / rangeY);
631 qreal scaleX = (width / rangeX);
645 qreal scaleX = (width / rangeX);
632
646
633 qreal setCount = series->count();
647 qreal setCount = series->count();
634 qreal domainMinY = 0; // These come from internal domain used by barseries.
648 qreal domainMinY = 0; // These come from internal domain used by barseries.
635 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
649 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
636 qreal rectWidth = scaleX * series->barWidth();
650 qreal rectWidth = scaleX * series->barWidth();
637
651
638 QVector<QRectF> layout;
652 QVector<QRectF> layout;
639
653
640 // 3 = count of values in set
654 // 3 = count of values in set
641 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
655 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
642 for (int i = 0; i < 3; i++) {
656 for (int i = 0; i < 3; i++) {
643 qreal yMax = height + scaleY * domainMinY + plotArea.top();
657 qreal yMax = height + scaleY * domainMinY + plotArea.top();
644 qreal yMin = height + scaleY * domainMinY + plotArea.top();
658 qreal yMin = height + scaleY * domainMinY + plotArea.top();
645 for (int set = 0; set < setCount; set++) {
659 for (int set = 0; set < setCount; set++) {
646 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
660 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
647 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
661 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
648 if (rectHeight < 0) {
662 if (rectHeight < 0) {
649 QRectF rect(xPos, yMax-rectHeight, rectWidth, rectHeight);
663 QRectF rect(xPos, yMax-rectHeight, rectWidth, rectHeight);
650 layout.append(rect);
664 layout.append(rect);
651 yMax -= rectHeight;
665 yMax -= rectHeight;
652 } else {
666 } else {
653 QRectF rect(xPos, yMin-rectHeight, rectWidth, rectHeight);
667 QRectF rect(xPos, yMin-rectHeight, rectWidth, rectHeight);
654 layout.append(rect);
668 layout.append(rect);
655 yMin -= rectHeight;
669 yMin -= rectHeight;
656 }
670 }
657 }
671 }
658 }
672 }
659
673
660 //====================================================================================
674 //====================================================================================
661 // barset 1, bar 0
675 // barset 1, bar 0
662 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
676 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
663 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
677 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
664
678
665 QCOMPARE(seriesSpy.count(), 1);
679 QCOMPARE(seriesSpy.count(), 1);
666 QCOMPARE(setSpy1.count(), 1);
680 QCOMPARE(setSpy1.count(), 1);
667 QCOMPARE(setSpy2.count(), 0);
681 QCOMPARE(setSpy2.count(), 0);
668
682
669 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
683 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
670 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
684 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
671 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
685 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
672 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
686 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
673
687
674 QList<QVariant> setSpyArg = setSpy1.takeFirst();
688 QList<QVariant> setSpyArg = setSpy1.takeFirst();
675 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
689 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
676 QVERIFY(setSpyArg.at(0).toInt() == 0);
690 QVERIFY(setSpyArg.at(0).toInt() == 0);
677
691
678 //====================================================================================
692 //====================================================================================
679 // barset 1, bar 1
693 // barset 1, bar 1
680 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
694 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
681 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
695 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
682
696
683 QCOMPARE(seriesSpy.count(), 1);
697 QCOMPARE(seriesSpy.count(), 1);
684 QCOMPARE(setSpy1.count(), 1);
698 QCOMPARE(setSpy1.count(), 1);
685 QCOMPARE(setSpy2.count(), 0);
699 QCOMPARE(setSpy2.count(), 0);
686
700
687 seriesSpyArg = seriesSpy.takeFirst();
701 seriesSpyArg = seriesSpy.takeFirst();
688 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
702 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
689 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
703 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
690 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
704 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
691
705
692 setSpyArg = setSpy1.takeFirst();
706 setSpyArg = setSpy1.takeFirst();
693 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
707 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
694 QVERIFY(setSpyArg.at(0).toInt() == 1);
708 QVERIFY(setSpyArg.at(0).toInt() == 1);
695
709
696 //====================================================================================
710 //====================================================================================
697 // barset 1, bar 2
711 // barset 1, bar 2
698 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
712 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
699 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
713 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
700
714
701 QCOMPARE(seriesSpy.count(), 1);
715 QCOMPARE(seriesSpy.count(), 1);
702 QCOMPARE(setSpy1.count(), 1);
716 QCOMPARE(setSpy1.count(), 1);
703 QCOMPARE(setSpy2.count(), 0);
717 QCOMPARE(setSpy2.count(), 0);
704
718
705 seriesSpyArg = seriesSpy.takeFirst();
719 seriesSpyArg = seriesSpy.takeFirst();
706 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
720 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
707 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
721 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
708 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
722 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
709
723
710 setSpyArg = setSpy1.takeFirst();
724 setSpyArg = setSpy1.takeFirst();
711 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
725 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
712 QVERIFY(setSpyArg.at(0).toInt() == 2);
726 QVERIFY(setSpyArg.at(0).toInt() == 2);
713
727
714 //====================================================================================
728 //====================================================================================
715 // barset 2, bar 0
729 // barset 2, bar 0
716 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
730 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
717 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
731 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
718
732
719 QCOMPARE(seriesSpy.count(), 1);
733 QCOMPARE(seriesSpy.count(), 1);
720 QCOMPARE(setSpy1.count(), 0);
734 QCOMPARE(setSpy1.count(), 0);
721 QCOMPARE(setSpy2.count(), 1);
735 QCOMPARE(setSpy2.count(), 1);
722
736
723 seriesSpyArg = seriesSpy.takeFirst();
737 seriesSpyArg = seriesSpy.takeFirst();
724 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
738 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
725 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
739 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
726 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
740 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
727
741
728 setSpyArg = setSpy2.takeFirst();
742 setSpyArg = setSpy2.takeFirst();
729 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
743 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
730 QVERIFY(setSpyArg.at(0).toInt() == 0);
744 QVERIFY(setSpyArg.at(0).toInt() == 0);
731
745
732 //====================================================================================
746 //====================================================================================
733 // barset 2, bar 1
747 // barset 2, bar 1
734 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
748 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
735 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
749 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
736
750
737 QCOMPARE(seriesSpy.count(), 1);
751 QCOMPARE(seriesSpy.count(), 1);
738 QCOMPARE(setSpy1.count(), 0);
752 QCOMPARE(setSpy1.count(), 0);
739 QCOMPARE(setSpy2.count(), 1);
753 QCOMPARE(setSpy2.count(), 1);
740
754
741 seriesSpyArg = seriesSpy.takeFirst();
755 seriesSpyArg = seriesSpy.takeFirst();
742 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
756 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
743 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
757 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
744 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
758 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
745
759
746 setSpyArg = setSpy2.takeFirst();
760 setSpyArg = setSpy2.takeFirst();
747 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
761 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
748 QVERIFY(setSpyArg.at(0).toInt() == 1);
762 QVERIFY(setSpyArg.at(0).toInt() == 1);
749
763
750 //====================================================================================
764 //====================================================================================
751 // barset 2, bar 2
765 // barset 2, bar 2
752 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
766 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
753 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
767 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
754
768
755 QCOMPARE(seriesSpy.count(), 1);
769 QCOMPARE(seriesSpy.count(), 1);
756 QCOMPARE(setSpy1.count(), 0);
770 QCOMPARE(setSpy1.count(), 0);
757 QCOMPARE(setSpy2.count(), 1);
771 QCOMPARE(setSpy2.count(), 1);
758
772
759 seriesSpyArg = seriesSpy.takeFirst();
773 seriesSpyArg = seriesSpy.takeFirst();
760 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
774 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
761 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
775 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
762 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
776 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
763
777
764 setSpyArg = setSpy2.takeFirst();
778 setSpyArg = setSpy2.takeFirst();
765 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
779 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
766 QVERIFY(setSpyArg.at(0).toInt() == 2);
780 QVERIFY(setSpyArg.at(0).toInt() == 2);
767 }
781 }
768
782
769 void tst_QStackedBarSeries::mouseReleased()
783 void tst_QStackedBarSeries::mouseReleased()
770 {
784 {
771 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
785 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
772
786
773 QStackedBarSeries* series = new QStackedBarSeries();
787 QStackedBarSeries* series = new QStackedBarSeries();
774
788
775 QBarSet* set1 = new QBarSet(QString("set 1"));
789 QBarSet* set1 = new QBarSet(QString("set 1"));
776 *set1 << 10 << 10 << 10;
790 *set1 << 10 << 10 << 10;
777 series->append(set1);
791 series->append(set1);
778
792
779 QBarSet* set2 = new QBarSet(QString("set 2"));
793 QBarSet* set2 = new QBarSet(QString("set 2"));
780 *set2 << 10 << 10 << 10;
794 *set2 << 10 << 10 << 10;
781 series->append(set2);
795 series->append(set2);
782 QList<QBarSet*> barSets = series->barSets();
796 QList<QBarSet*> barSets = series->barSets();
783
797
784 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
798 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
785 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
799 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
786 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
800 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
787
801
788 QChartView view(new QChart());
802 QChartView view(new QChart());
789 view.resize(400,300);
803 view.resize(400,300);
790 view.chart()->addSeries(series);
804 view.chart()->addSeries(series);
791 view.show();
805 view.show();
792 QTest::qWaitForWindowShown(&view);
806 QTest::qWaitForWindowShown(&view);
793
807
794 // Calculate expected layout for bars
808 // Calculate expected layout for bars
795 QRectF plotArea = view.chart()->plotArea();
809 QRectF plotArea = view.chart()->plotArea();
796 qreal width = plotArea.width();
810 qreal width = plotArea.width();
797 qreal height = plotArea.height();
811 qreal height = plotArea.height();
798 qreal rangeY = 20; // From 0 to 20 because sets are stacked (this should be height of highest stack)
812 qreal rangeY = 20; // From 0 to 20 because sets are stacked (this should be height of highest stack)
799 qreal rangeX = 3; // 3 values per set
813 qreal rangeX = 3; // 3 values per set
800 qreal scaleY = (height / rangeY);
814 qreal scaleY = (height / rangeY);
801 qreal scaleX = (width / rangeX);
815 qreal scaleX = (width / rangeX);
802
816
803 qreal setCount = series->count();
817 qreal setCount = series->count();
804 qreal domainMinY = 0; // These come from internal domain used by barseries.
818 qreal domainMinY = 0; // These come from internal domain used by barseries.
805 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
819 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
806 qreal rectWidth = scaleX * series->barWidth();
820 qreal rectWidth = scaleX * series->barWidth();
807
821
808 QVector<QRectF> layout;
822 QVector<QRectF> layout;
809
823
810 // 3 = count of values in set
824 // 3 = count of values in set
811 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
825 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
812 for (int i = 0; i < 3; i++) {
826 for (int i = 0; i < 3; i++) {
813 qreal yMax = height + scaleY * domainMinY + plotArea.top();
827 qreal yMax = height + scaleY * domainMinY + plotArea.top();
814 qreal yMin = height + scaleY * domainMinY + plotArea.top();
828 qreal yMin = height + scaleY * domainMinY + plotArea.top();
815 for (int set = 0; set < setCount; set++) {
829 for (int set = 0; set < setCount; set++) {
816 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
830 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
817 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
831 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
818 if (rectHeight < 0) {
832 if (rectHeight < 0) {
819 QRectF rect(xPos, yMax-rectHeight, rectWidth, rectHeight);
833 QRectF rect(xPos, yMax-rectHeight, rectWidth, rectHeight);
820 layout.append(rect);
834 layout.append(rect);
821 yMax -= rectHeight;
835 yMax -= rectHeight;
822 } else {
836 } else {
823 QRectF rect(xPos, yMin-rectHeight, rectWidth, rectHeight);
837 QRectF rect(xPos, yMin-rectHeight, rectWidth, rectHeight);
824 layout.append(rect);
838 layout.append(rect);
825 yMin -= rectHeight;
839 yMin -= rectHeight;
826 }
840 }
827 }
841 }
828 }
842 }
829
843
830 //====================================================================================
844 //====================================================================================
831 // barset 1, bar 0
845 // barset 1, bar 0
832 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
846 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
833 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
847 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
834
848
835 QCOMPARE(seriesSpy.count(), 1);
849 QCOMPARE(seriesSpy.count(), 1);
836 QCOMPARE(setSpy1.count(), 1);
850 QCOMPARE(setSpy1.count(), 1);
837 QCOMPARE(setSpy2.count(), 0);
851 QCOMPARE(setSpy2.count(), 0);
838
852
839 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
853 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
840 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
854 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
841 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
855 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
842 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
856 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
843
857
844 QList<QVariant> setSpyArg = setSpy1.takeFirst();
858 QList<QVariant> setSpyArg = setSpy1.takeFirst();
845 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
859 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
846 QVERIFY(setSpyArg.at(0).toInt() == 0);
860 QVERIFY(setSpyArg.at(0).toInt() == 0);
847
861
848 //====================================================================================
862 //====================================================================================
849 // barset 1, bar 1
863 // barset 1, bar 1
850 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
864 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
851 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
865 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
852
866
853 QCOMPARE(seriesSpy.count(), 1);
867 QCOMPARE(seriesSpy.count(), 1);
854 QCOMPARE(setSpy1.count(), 1);
868 QCOMPARE(setSpy1.count(), 1);
855 QCOMPARE(setSpy2.count(), 0);
869 QCOMPARE(setSpy2.count(), 0);
856
870
857 seriesSpyArg = seriesSpy.takeFirst();
871 seriesSpyArg = seriesSpy.takeFirst();
858 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
872 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
859 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
873 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
860 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
874 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
861
875
862 setSpyArg = setSpy1.takeFirst();
876 setSpyArg = setSpy1.takeFirst();
863 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
877 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
864 QVERIFY(setSpyArg.at(0).toInt() == 1);
878 QVERIFY(setSpyArg.at(0).toInt() == 1);
865
879
866 //====================================================================================
880 //====================================================================================
867 // barset 1, bar 2
881 // barset 1, bar 2
868 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
882 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
869 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
883 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
870
884
871 QCOMPARE(seriesSpy.count(), 1);
885 QCOMPARE(seriesSpy.count(), 1);
872 QCOMPARE(setSpy1.count(), 1);
886 QCOMPARE(setSpy1.count(), 1);
873 QCOMPARE(setSpy2.count(), 0);
887 QCOMPARE(setSpy2.count(), 0);
874
888
875 seriesSpyArg = seriesSpy.takeFirst();
889 seriesSpyArg = seriesSpy.takeFirst();
876 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
890 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
877 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
891 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
878 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
892 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
879
893
880 setSpyArg = setSpy1.takeFirst();
894 setSpyArg = setSpy1.takeFirst();
881 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
895 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
882 QVERIFY(setSpyArg.at(0).toInt() == 2);
896 QVERIFY(setSpyArg.at(0).toInt() == 2);
883
897
884 //====================================================================================
898 //====================================================================================
885 // barset 2, bar 0
899 // barset 2, bar 0
886 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
900 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
887 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
901 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
888
902
889 QCOMPARE(seriesSpy.count(), 1);
903 QCOMPARE(seriesSpy.count(), 1);
890 QCOMPARE(setSpy1.count(), 0);
904 QCOMPARE(setSpy1.count(), 0);
891 QCOMPARE(setSpy2.count(), 1);
905 QCOMPARE(setSpy2.count(), 1);
892
906
893 seriesSpyArg = seriesSpy.takeFirst();
907 seriesSpyArg = seriesSpy.takeFirst();
894 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
908 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
895 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
909 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
896 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
910 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
897
911
898 setSpyArg = setSpy2.takeFirst();
912 setSpyArg = setSpy2.takeFirst();
899 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
913 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
900 QVERIFY(setSpyArg.at(0).toInt() == 0);
914 QVERIFY(setSpyArg.at(0).toInt() == 0);
901
915
902 //====================================================================================
916 //====================================================================================
903 // barset 2, bar 1
917 // barset 2, bar 1
904 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
918 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
905 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
919 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
906
920
907 QCOMPARE(seriesSpy.count(), 1);
921 QCOMPARE(seriesSpy.count(), 1);
908 QCOMPARE(setSpy1.count(), 0);
922 QCOMPARE(setSpy1.count(), 0);
909 QCOMPARE(setSpy2.count(), 1);
923 QCOMPARE(setSpy2.count(), 1);
910
924
911 seriesSpyArg = seriesSpy.takeFirst();
925 seriesSpyArg = seriesSpy.takeFirst();
912 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
926 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
913 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
927 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
914 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
928 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
915
929
916 setSpyArg = setSpy2.takeFirst();
930 setSpyArg = setSpy2.takeFirst();
917 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
931 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
918 QVERIFY(setSpyArg.at(0).toInt() == 1);
932 QVERIFY(setSpyArg.at(0).toInt() == 1);
919
933
920 //====================================================================================
934 //====================================================================================
921 // barset 2, bar 2
935 // barset 2, bar 2
922 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
936 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
923 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
937 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
924
938
925 QCOMPARE(seriesSpy.count(), 1);
939 QCOMPARE(seriesSpy.count(), 1);
926 QCOMPARE(setSpy1.count(), 0);
940 QCOMPARE(setSpy1.count(), 0);
927 QCOMPARE(setSpy2.count(), 1);
941 QCOMPARE(setSpy2.count(), 1);
928
942
929 seriesSpyArg = seriesSpy.takeFirst();
943 seriesSpyArg = seriesSpy.takeFirst();
930 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
944 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
931 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
945 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
932 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
946 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
933
947
934 setSpyArg = setSpy2.takeFirst();
948 setSpyArg = setSpy2.takeFirst();
935 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
949 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
936 QVERIFY(setSpyArg.at(0).toInt() == 2);
950 QVERIFY(setSpyArg.at(0).toInt() == 2);
937 }
951 }
938
952
939 void tst_QStackedBarSeries::mouseDoubleClicked()
953 void tst_QStackedBarSeries::mouseDoubleClicked()
940 {
954 {
941 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
955 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
942
956
943 QStackedBarSeries* series = new QStackedBarSeries();
957 QStackedBarSeries* series = new QStackedBarSeries();
944
958
945 QBarSet* set1 = new QBarSet(QString("set 1"));
959 QBarSet* set1 = new QBarSet(QString("set 1"));
946 *set1 << 10 << 10 << 10;
960 *set1 << 10 << 10 << 10;
947 series->append(set1);
961 series->append(set1);
948
962
949 QBarSet* set2 = new QBarSet(QString("set 2"));
963 QBarSet* set2 = new QBarSet(QString("set 2"));
950 *set2 << 10 << 10 << 10;
964 *set2 << 10 << 10 << 10;
951 series->append(set2);
965 series->append(set2);
952 QList<QBarSet*> barSets = series->barSets();
966 QList<QBarSet*> barSets = series->barSets();
953
967
954 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
968 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
955 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
969 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
956 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
970 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
957
971
958 QChartView view(new QChart());
972 QChartView view(new QChart());
959 view.resize(400,300);
973 view.resize(400,300);
960 view.chart()->addSeries(series);
974 view.chart()->addSeries(series);
961 view.show();
975 view.show();
962 QTest::qWaitForWindowShown(&view);
976 QTest::qWaitForWindowShown(&view);
963
977
964 // Calculate expected layout for bars
978 // Calculate expected layout for bars
965 QRectF plotArea = view.chart()->plotArea();
979 QRectF plotArea = view.chart()->plotArea();
966 qreal width = plotArea.width();
980 qreal width = plotArea.width();
967 qreal height = plotArea.height();
981 qreal height = plotArea.height();
968 qreal rangeY = 20; // From 0 to 20 because sets are stacked (this should be height of highest stack)
982 qreal rangeY = 20; // From 0 to 20 because sets are stacked (this should be height of highest stack)
969 qreal rangeX = 3; // 3 values per set
983 qreal rangeX = 3; // 3 values per set
970 qreal scaleY = (height / rangeY);
984 qreal scaleY = (height / rangeY);
971 qreal scaleX = (width / rangeX);
985 qreal scaleX = (width / rangeX);
972
986
973 qreal setCount = series->count();
987 qreal setCount = series->count();
974 qreal domainMinY = 0; // These come from internal domain used by barseries.
988 qreal domainMinY = 0; // These come from internal domain used by barseries.
975 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
989 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
976 qreal rectWidth = scaleX * series->barWidth();
990 qreal rectWidth = scaleX * series->barWidth();
977
991
978 QVector<QRectF> layout;
992 QVector<QRectF> layout;
979
993
980 // 3 = count of values in set
994 // 3 = count of values in set
981 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
995 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
982 for (int i = 0; i < 3; i++) {
996 for (int i = 0; i < 3; i++) {
983 qreal yMax = height + scaleY * domainMinY + plotArea.top();
997 qreal yMax = height + scaleY * domainMinY + plotArea.top();
984 qreal yMin = height + scaleY * domainMinY + plotArea.top();
998 qreal yMin = height + scaleY * domainMinY + plotArea.top();
985 for (int set = 0; set < setCount; set++) {
999 for (int set = 0; set < setCount; set++) {
986 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
1000 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
987 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
1001 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
988 if (rectHeight < 0) {
1002 if (rectHeight < 0) {
989 QRectF rect(xPos, yMax-rectHeight, rectWidth, rectHeight);
1003 QRectF rect(xPos, yMax-rectHeight, rectWidth, rectHeight);
990 layout.append(rect);
1004 layout.append(rect);
991 yMax -= rectHeight;
1005 yMax -= rectHeight;
992 } else {
1006 } else {
993 QRectF rect(xPos, yMin-rectHeight, rectWidth, rectHeight);
1007 QRectF rect(xPos, yMin-rectHeight, rectWidth, rectHeight);
994 layout.append(rect);
1008 layout.append(rect);
995 yMin -= rectHeight;
1009 yMin -= rectHeight;
996 }
1010 }
997 }
1011 }
998 }
1012 }
999
1013
1000 // barset 1, bar 0
1014 // barset 1, bar 0
1001 QTest::mouseDClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1015 QTest::mouseDClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1002 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1016 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1003
1017
1004 QCOMPARE(seriesSpy.count(), 1);
1018 QCOMPARE(seriesSpy.count(), 1);
1005 QCOMPARE(setSpy1.count(), 1);
1019 QCOMPARE(setSpy1.count(), 1);
1006 QCOMPARE(setSpy2.count(), 0);
1020 QCOMPARE(setSpy2.count(), 0);
1007
1021
1008 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1022 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1009 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1023 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1010 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1024 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1011 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1025 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1012
1026
1013 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1027 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1014 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1028 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1015 QVERIFY(setSpyArg.at(0).toInt() == 0);
1029 QVERIFY(setSpyArg.at(0).toInt() == 0);
1016 }
1030 }
1017 QTEST_MAIN(tst_QStackedBarSeries)
1031 QTEST_MAIN(tst_QStackedBarSeries)
1018
1032
1019 #include "tst_qstackedbarseries.moc"
1033 #include "tst_qstackedbarseries.moc"
1020
1034
@@ -1,197 +1,205
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd
3 ** Copyright (C) 2015 The Qt Company Ltd
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 **
6 **
7 ** This file is part of the Qt Charts module.
7 ** This file is part of the Qt Charts module.
8 **
8 **
9 ** Licensees holding valid commercial license for Qt may use this file in
9 ** Licensees holding valid commercial license for Qt may use this file in
10 ** accordance with the Qt License Agreement provided with the Software
10 ** accordance with the Qt License Agreement provided with the Software
11 ** or, alternatively, in accordance with the terms contained in a written
11 ** or, alternatively, in accordance with the terms contained in a written
12 ** agreement between you and The Qt Company.
12 ** agreement between you and The Qt Company.
13 **
13 **
14 ** If you have questions regarding the use of this file, please use
14 ** If you have questions regarding the use of this file, please use
15 ** contact form at http://qt.io
15 ** contact form at http://qt.io
16 **
16 **
17 ****************************************************************************/
17 ****************************************************************************/
18
18
19 import QtQuick 2.0
19 import QtQuick 2.0
20
20
21 Row {
21 Row {
22 anchors.fill: parent
22 anchors.fill: parent
23 spacing: 5
23 spacing: 5
24 property variant series
24 property variant series
25
25
26 // buttons for selecting the edited object: series, barset or label
26 // buttons for selecting the edited object: series, barset or label
27 Flow {
27 Flow {
28 spacing: 5
28 spacing: 5
29 flow: Flow.TopToBottom
29 flow: Flow.TopToBottom
30 Button {
30 Button {
31 id: seriesButton
31 id: seriesButton
32 text: "series"
32 text: "series"
33 unpressedColor: "#79bd8f"
33 unpressedColor: "#79bd8f"
34 onClicked: {
34 onClicked: {
35 seriesFlow.visible = true;
35 seriesFlow.visible = true;
36 setFlow.visible = false;
36 setFlow.visible = false;
37 labelsFlow.visible = false;
37 labelsFlow.visible = false;
38 color = "#00a388";
38 color = "#00a388";
39 setButton.color = "#79bd8f";
39 setButton.color = "#79bd8f";
40 labelButton.color = "#79bd8f";
40 labelButton.color = "#79bd8f";
41 }
41 }
42 }
42 }
43 Button {
43 Button {
44 id: setButton
44 id: setButton
45 text: "BarSet"
45 text: "BarSet"
46 unpressedColor: "#79bd8f"
46 unpressedColor: "#79bd8f"
47 onClicked: {
47 onClicked: {
48 seriesFlow.visible = false;
48 seriesFlow.visible = false;
49 setFlow.visible = true;
49 setFlow.visible = true;
50 labelsFlow.visible = false;
50 labelsFlow.visible = false;
51 color = "#00a388";
51 color = "#00a388";
52 seriesButton.color = "#79bd8f";
52 seriesButton.color = "#79bd8f";
53 labelButton.color = "#79bd8f";
53 labelButton.color = "#79bd8f";
54 }
54 }
55 }
55 }
56 Button {
56 Button {
57 id: labelButton
57 id: labelButton
58 text: "label"
58 text: "label"
59 unpressedColor: "#79bd8f"
59 unpressedColor: "#79bd8f"
60 onClicked: {
60 onClicked: {
61 seriesFlow.visible = false;
61 seriesFlow.visible = false;
62 setFlow.visible = false;
62 setFlow.visible = false;
63 labelsFlow.visible = true;
63 labelsFlow.visible = true;
64 color = "#00a388";
64 color = "#00a388";
65 seriesButton.color = "#79bd8f";
65 seriesButton.color = "#79bd8f";
66 setButton.color = "#79bd8f";
66 setButton.color = "#79bd8f";
67 }
67 }
68 }
68 }
69 }
69 }
70
70
71 // Buttons for editing series
71 // Buttons for editing series
72 Flow {
72 Flow {
73 id: seriesFlow
73 id: seriesFlow
74 spacing: 5
74 spacing: 5
75 flow: Flow.TopToBottom
75 flow: Flow.TopToBottom
76 visible: false
76 visible: false
77
77
78 Button {
78 Button {
79 text: "visible"
79 text: "visible"
80 onClicked: series.visible = !series.visible;
80 onClicked: series.visible = !series.visible;
81 }
81 }
82 Button {
82 Button {
83 text: "opacity +"
83 text: "opacity +"
84 onClicked: series.opacity += 0.1;
84 onClicked: series.opacity += 0.1;
85 }
85 }
86 Button {
86 Button {
87 text: "opacity -"
87 text: "opacity -"
88 onClicked: series.opacity -= 0.1;
88 onClicked: series.opacity -= 0.1;
89 }
89 }
90 Button {
90 Button {
91 text: "bar width +"
91 text: "bar width +"
92 onClicked: series.barWidth += 0.1;
92 onClicked: series.barWidth += 0.1;
93 }
93 }
94 Button {
94 Button {
95 text: "bar width -"
95 text: "bar width -"
96 onClicked: series.barWidth -= 0.1;
96 onClicked: series.barWidth -= 0.1;
97 }
97 }
98 }
98 }
99
99
100 // Buttons for editing sets
100 // Buttons for editing sets
101 Flow {
101 Flow {
102 id: setFlow
102 id: setFlow
103 spacing: 5
103 spacing: 5
104 flow: Flow.TopToBottom
104 flow: Flow.TopToBottom
105 visible: false
105 visible: false
106
106
107 Button {
107 Button {
108 text: "append set"
108 text: "append set"
109 onClicked: {
109 onClicked: {
110 var count = series.count;
110 var count = series.count;
111 series.append("set" + count, [0, 0.1 * count, 0.2 * count, 0.3 * count, 0.4 * count, 0.5 * count, 0.6 * count]);
111 series.append("set" + count, [0, 0.1 * count, 0.2 * count, 0.3 * count, 0.4 * count, 0.5 * count, 0.6 * count]);
112 }
112 }
113 }
113 }
114 Button {
114 Button {
115 text: "insert set"
115 text: "insert set"
116 onClicked: {
116 onClicked: {
117 var count = series.count;
117 var count = series.count;
118 series.insert(count - 1, "set" + count, [0, 0.1 * count, 0.2 * count, 0.3 * count, 0.4 * count, 0.5 * count, 0.6 * count]);
118 series.insert(count - 1, "set" + count, [0, 0.1 * count, 0.2 * count, 0.3 * count, 0.4 * count, 0.5 * count, 0.6 * count]);
119 }
119 }
120 }
120 }
121 Button {
121 Button {
122 text: "remove set"
122 text: "remove set"
123 onClicked: series.remove(series.at(series.count - 1));
123 onClicked: series.remove(series.at(series.count - 1));
124 }
124 }
125 Button {
125 Button {
126 text: "clear sets"
126 text: "clear sets"
127 onClicked: series.clear();
127 onClicked: series.clear();
128 }
128 }
129
129
130 Button {
130 Button {
131 text: "set 1 append"
131 text: "set 1 append"
132 onClicked: series.at(0).append(series.at(0).count + 1);
132 onClicked: series.at(0).append(series.at(0).count + 1);
133 }
133 }
134 Button {
134 Button {
135 text: "set 1 replace"
135 text: "set 1 replace"
136 onClicked: series.at(0).replace(series.at(0).count - 1, series.at(0).at(series.at(0).count - 1) + 1.5);
136 onClicked: series.at(0).replace(series.at(0).count - 1, series.at(0).at(series.at(0).count - 1) + 1.5);
137 }
137 }
138 Button {
138 Button {
139 text: "set 1 remove"
139 text: "set 1 remove"
140 onClicked: series.at(0).remove(series.at(0).count - 1);
140 onClicked: series.at(0).remove(series.at(0).count - 1);
141 }
141 }
142
142
143 Button {
143 Button {
144 text: "set 1 color"
144 text: "set 1 color"
145 onClicked: series.at(0).color = main.nextColor();
145 onClicked: series.at(0).color = main.nextColor();
146 }
146 }
147 Button {
147 Button {
148 text: "set 1 border color"
148 text: "set 1 border color"
149 onClicked: series.at(0).borderColor = main.nextColor();
149 onClicked: series.at(0).borderColor = main.nextColor();
150 }
150 }
151 Button {
151 Button {
152 text: "set 1 borderWidth +"
152 text: "set 1 borderWidth +"
153 onClicked: series.at(0).borderWidth += 0.5;
153 onClicked: series.at(0).borderWidth += 0.5;
154 }
154 }
155 Button {
155 Button {
156 text: "set 1 borderWidth -"
156 text: "set 1 borderWidth -"
157 onClicked: series.at(0).borderWidth -= 0.5;
157 onClicked: series.at(0).borderWidth -= 0.5;
158 }
158 }
159 }
159 }
160
160
161
161
162 Flow {
162 Flow {
163 id: labelsFlow
163 id: labelsFlow
164 spacing: 5
164 spacing: 5
165 flow: Flow.TopToBottom
165 flow: Flow.TopToBottom
166 visible: false
166 visible: false
167
167
168 Button {
168 Button {
169 text: "labels visible"
169 text: "labels visible"
170 onClicked: series.labelsVisible = !series.labelsVisible;
170 onClicked: series.labelsVisible = !series.labelsVisible;
171 }
171 }
172 Button {
172 Button {
173 text: "labels format"
173 text: "labels format"
174 onClicked: {
174 onClicked: {
175 if (series.labelsFormat === "@value")
175 if (series.labelsFormat === "@value")
176 series.labelsFormat = "@value%"
176 series.labelsFormat = "@value%"
177 else
177 else
178 series.labelsFormat = "@value"
178 series.labelsFormat = "@value"
179 }
179 }
180 }
180 }
181 Button {
181 Button {
182 text: "labels position"
182 text: "labels position"
183 onClicked: series.changeLabelsPosition();
183 onClicked: series.changeLabelsPosition();
184 }
184 }
185 Button {
185 Button {
186 text: "set 1 label color"
186 text: "set 1 label color"
187 onClicked: series.at(0).labelColor = main.nextColor();
187 onClicked: series.at(0).labelColor = main.nextColor();
188 }
188 }
189 Button {
190 text: "labels angle +"
191 onClicked: series.labelsAngle = series.labelsAngle + 5;
192 }
193 Button {
194 text: "labels angle -"
195 onClicked: series.labelsAngle = series.labelsAngle - 5;
196 }
189 FontEditor {
197 FontEditor {
190 id: fontEditor
198 id: fontEditor
191 fontDescription: "label"
199 fontDescription: "label"
192 function editedFont() {
200 function editedFont() {
193 return series.at(0).labelFont;
201 return series.at(0).labelFont;
194 }
202 }
195 }
203 }
196 }
204 }
197 }
205 }
General Comments 0
You need to be logged in to leave comments. Login now