##// 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 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include <private/abstractbarchartitem_p.h>
20 20 #include <private/bar_p.h>
21 21 #include <QtCharts/QBarSet>
22 22 #include <private/qbarset_p.h>
23 23 #include <QtCharts/QAbstractBarSeries>
24 24 #include <private/qabstractbarseries_p.h>
25 25 #include <QtCharts/QChart>
26 26 #include <private/chartpresenter_p.h>
27 27 #include <private/charttheme_p.h>
28 28 #include <private/baranimation_p.h>
29 29
30 30 #include <private/chartdataset_p.h>
31 31 #include <QtGui/QPainter>
32 32 #include <QtGui/QTextDocument>
33 33
34 34 QT_CHARTS_BEGIN_NAMESPACE
35 35
36 36 AbstractBarChartItem::AbstractBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
37 37 ChartItem(series->d_func(),item),
38 38 m_animation(0),
39 39 m_series(series)
40 40 {
41 41
42 42 setFlag(ItemClipsChildrenToShape);
43 43 setFlag(QGraphicsItem::ItemIsSelectable);
44 44 connect(series->d_func(), SIGNAL(updatedLayout()), this, SLOT(handleLayoutChanged()));
45 45 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleUpdatedBars()));
46 46 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool)));
47 47 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
48 48 connect(series, SIGNAL(visibleChanged()), this, SLOT(handleVisibleChanged()));
49 49 connect(series, SIGNAL(opacityChanged()), this, SLOT(handleOpacityChanged()));
50 50 connect(series, SIGNAL(labelsFormatChanged(QString)), this, SLOT(handleUpdatedBars()));
51 51 connect(series, SIGNAL(labelsFormatChanged(QString)), this, SLOT(positionLabels()));
52 52 connect(series, SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)),
53 53 this, SLOT(handleLabelsPositionChanged()));
54 connect(series, SIGNAL(labelsAngleChanged(qreal)), this, SLOT(positionLabels()));
54 55 setZValue(ChartPresenter::BarSeriesZValue);
55 56 handleDataStructureChanged();
56 57 handleVisibleChanged();
57 58 handleUpdatedBars();
58 59 }
59 60
60 61 AbstractBarChartItem::~AbstractBarChartItem()
61 62 {
62 63 }
63 64
64 65 void AbstractBarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
65 66 {
66 67 Q_UNUSED(painter);
67 68 Q_UNUSED(option);
68 69 Q_UNUSED(widget);
69 70 }
70 71
71 72 QRectF AbstractBarChartItem::boundingRect() const
72 73 {
73 74 return m_rect;
74 75 }
75 76
76 77 void AbstractBarChartItem::applyLayout(const QVector<QRectF> &layout)
77 78 {
78 79 QSizeF size = geometry().size();
79 80 if (geometry().size().isValid()) {
80 81 if (m_animation) {
81 82 if (m_layout.count() == 0 || m_oldSize != size) {
82 83 initializeLayout();
83 84 m_oldSize = size;
84 85 }
85 86 m_animation->setup(m_layout, layout);
86 87 presenter()->startAnimation(m_animation);
87 88 } else {
88 89 setLayout(layout);
89 90 update();
90 91 }
91 92 }
92 93 }
93 94
94 95 void AbstractBarChartItem::setAnimation(BarAnimation *animation)
95 96 {
96 97 m_animation = animation;
97 98 }
98 99
99 100 void AbstractBarChartItem::setLayout(const QVector<QRectF> &layout)
100 101 {
101 102 if (layout.count() != m_bars.count())
102 103 return;
103 104
104 105 m_layout = layout;
105 106
106 107 for (int i = 0; i < m_bars.count(); i++)
107 108 m_bars.at(i)->setRect(layout.at(i));
108 109
109 110 positionLabels();
110 111 }
111 112 //handlers
112 113
113 114 void AbstractBarChartItem::handleDomainUpdated()
114 115 {
115 116 m_domainMinX = domain()->minX();
116 117 m_domainMaxX = domain()->maxX();
117 118 m_domainMinY = domain()->minY();
118 119 m_domainMaxY = domain()->maxY();
119 120
120 121 QRectF rect(QPointF(0,0),domain()->size());
121 122
122 123 if(m_rect != rect){
123 124 prepareGeometryChange();
124 125 m_rect = rect;
125 126 }
126 127
127 128 handleLayoutChanged();
128 129 }
129 130
130 131 void AbstractBarChartItem::handleLayoutChanged()
131 132 {
132 133 if ((m_rect.width() <= 0) || (m_rect.height() <= 0))
133 134 return; // rect size zero.
134 135 QVector<QRectF> layout = calculateLayout();
135 136 applyLayout(layout);
136 137 handleUpdatedBars();
137 138 }
138 139
139 140 void AbstractBarChartItem::handleLabelsVisibleChanged(bool visible)
140 141 {
141 142 foreach (QGraphicsTextItem *label, m_labels)
142 143 label->setVisible(visible);
143 144 update();
144 145 }
145 146
146 147 void AbstractBarChartItem::handleDataStructureChanged()
147 148 {
148 149 foreach (QGraphicsItem *item, childItems())
149 150 delete item;
150 151
151 152 m_bars.clear();
152 153 m_labels.clear();
153 154 m_layout.clear();
154 155
155 156 // Create new graphic items for bars
156 157 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
157 158 for (int s = 0; s < m_series->count(); s++) {
158 159 QBarSet *set = m_series->d_func()->barsetAt(s);
159 160
160 161 // Bars
161 162 Bar *bar = new Bar(set, c, this);
162 163 m_bars.append(bar);
163 164 connect(bar, SIGNAL(clicked(int,QBarSet*)), m_series, SIGNAL(clicked(int,QBarSet*)));
164 165 connect(bar, SIGNAL(hovered(bool, int, QBarSet*)), m_series, SIGNAL(hovered(bool, int, QBarSet*)));
165 166 connect(bar, SIGNAL(pressed(int, QBarSet*)), m_series, SIGNAL(pressed(int, QBarSet*)));
166 167 connect(bar, SIGNAL(released(int, QBarSet*)),
167 168 m_series, SIGNAL(released(int, QBarSet*)));
168 169 connect(bar, SIGNAL(doubleClicked(int, QBarSet*)),
169 170 m_series, SIGNAL(doubleClicked(int, QBarSet*)));
170 171 connect(bar, SIGNAL(clicked(int,QBarSet*)), set, SIGNAL(clicked(int)));
171 172 connect(bar, SIGNAL(hovered(bool, int, QBarSet*)), set, SIGNAL(hovered(bool, int)));
172 173 connect(bar, SIGNAL(pressed(int, QBarSet*)), set, SIGNAL(pressed(int)));
173 174 connect(bar, SIGNAL(released(int, QBarSet*)), set, SIGNAL(released(int)));
174 175 connect(bar, SIGNAL(doubleClicked(int, QBarSet*)), set, SIGNAL(doubleClicked(int)));
175 176 // m_layout.append(QRectF(0, 0, 1, 1));
176 177
177 178 // Labels
178 179 QGraphicsTextItem *newLabel = new QGraphicsTextItem(this);
179 180 newLabel->document()->setDocumentMargin(ChartPresenter::textMargin());
180 181 m_labels.append(newLabel);
181 182 }
182 183 }
183 184
184 185 if(themeManager()) themeManager()->updateSeries(m_series);
185 186 handleLayoutChanged();
186 187 handleVisibleChanged();
187 188 }
188 189
189 190 void AbstractBarChartItem::handleVisibleChanged()
190 191 {
191 192 bool visible = m_series->isVisible();
192 193 if (visible)
193 194 handleLabelsVisibleChanged(m_series->isLabelsVisible());
194 195 else
195 196 handleLabelsVisibleChanged(visible);
196 197
197 198 foreach (QGraphicsItem *bar, m_bars)
198 199 bar->setVisible(visible);
199 200 }
200 201
201 202 void AbstractBarChartItem::handleOpacityChanged()
202 203 {
203 204 foreach (QGraphicsItem *item, childItems())
204 205 item->setOpacity(m_series->opacity());
205 206 }
206 207
207 208 void AbstractBarChartItem::handleUpdatedBars()
208 209 {
209 210 if (!m_series->d_func()->blockBarUpdate()) {
210 211 // Handle changes in pen, brush, labels etc.
211 212 int categoryCount = m_series->d_func()->categoryCount();
212 213 int setCount = m_series->count();
213 214 int itemIndex(0);
214 215 static const QString valueTag(QLatin1String("@value"));
215 216
216 217 for (int category = 0; category < categoryCount; category++) {
217 218 for (int set = 0; set < setCount; set++) {
218 219 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
219 220 Bar *bar = m_bars.at(itemIndex);
220 221 bar->setPen(barSet->m_pen);
221 222 bar->setBrush(barSet->m_brush);
222 223 bar->update();
223 224
224 225 QGraphicsTextItem *label = m_labels.at(itemIndex);
225 226 QString valueLabel;
226 227 if (presenter()) { // At startup presenter is not yet set, yet somehow update comes
227 228 if (m_series->labelsFormat().isEmpty()) {
228 229 valueLabel = presenter()->numberToString(barSet->value(category));
229 230 } else {
230 231 valueLabel = m_series->labelsFormat();
231 232 valueLabel.replace(valueTag,
232 233 presenter()->numberToString(barSet->value(category)));
233 234 }
234 235 }
235 236 label->setHtml(valueLabel);
236 237 label->setFont(barSet->m_labelFont);
237 238 label->setDefaultTextColor(barSet->m_labelBrush.color());
238 239 label->update();
239 240 itemIndex++;
240 241 }
241 242 }
242 243 }
243 244 }
244 245
245 246 void AbstractBarChartItem::handleLabelsPositionChanged()
246 247 {
247 248 positionLabels();
248 249 }
249 250
250 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 261 for (int i = 0; i < m_layout.count(); i++) {
253 262 QGraphicsTextItem *label = m_labels.at(i);
263
264 QRectF labelRect = label->boundingRect();
265 QPointF center = labelRect.center();
266
254 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 279 int offset = m_bars.at(i)->pen().width() / 2 + 2;
258 if (m_series->labelsPosition() == QAbstractBarSeries::LabelsCenter)
259 xPos = m_layout.at(i).center().x() - label->boundingRect().center().x();
260 else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsInsideEnd)
261 xPos = m_layout.at(i).right() - label->boundingRect().width() - offset;
262 else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsInsideBase)
263 xPos = m_layout.at(i).left() + offset;
264 else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsOutsideEnd)
265 xPos = m_layout.at(i).right() + offset;
280
281 switch (m_series->labelsPosition()) {
282 case QAbstractBarSeries::LabelsCenter:
283 xPos = m_layout.at(i).center().x() - center.x();
284 break;
285 case QAbstractBarSeries::LabelsInsideEnd:
286 xPos = m_layout.at(i).right() - labelRect.width() - offset + xDiff;
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 349 label->setPos(xPos, yPos);
268 350 label->setZValue(zValue() + 1);
269 351 }
270 352 }
271 353
272 354 #include "moc_abstractbarchartitem_p.cpp"
273 355
274 356 QT_CHARTS_END_NAMESPACE
@@ -1,94 +1,95
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 // W A R N I N G
20 20 // -------------
21 21 //
22 22 // This file is not part of the Qt Enterprise Chart API. It exists purely as an
23 23 // implementation detail. This header file may change from version to
24 24 // version without notice, or even be removed.
25 25 //
26 26 // We mean it.
27 27
28 28
29 29 #ifndef ABSTRACTBARCHARTITEM_H
30 30 #define ABSTRACTBARCHARTITEM_H
31 31
32 32 #include <private/chartitem_p.h>
33 33 #include <QtCharts/QAbstractBarSeries>
34 34 #include <QtGui/QPen>
35 35 #include <QtGui/QBrush>
36 36
37 37 QT_CHARTS_BEGIN_NAMESPACE
38 38
39 39 class Bar;
40 40 class QAxisCategories;
41 41 class QChart;
42 42 class BarAnimation;
43 43
44 44 class AbstractBarChartItem : public ChartItem
45 45 {
46 46 Q_OBJECT
47 47 public:
48 48 AbstractBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item = 0);
49 49 virtual ~AbstractBarChartItem();
50 50
51 51 public:
52 52 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
53 53 QRectF boundingRect() const;
54 54
55 55 virtual QVector<QRectF> calculateLayout() = 0;
56 56 virtual void initializeLayout() = 0;
57 57 virtual void applyLayout(const QVector<QRectF> &layout);
58 58 virtual void setAnimation(BarAnimation *animation);
59 59 void setLayout(const QVector<QRectF> &layout);
60 60 void updateLayout(const QVector<QRectF> &layout);
61 61 QRectF geometry() const { return m_rect;}
62 62
63 63 public Q_SLOTS:
64 64 void handleDomainUpdated();
65 65 void handleLayoutChanged();
66 66 void handleLabelsVisibleChanged(bool visible);
67 67 void handleDataStructureChanged(); // structure of of series has changed, recreate graphic items
68 68 void handleVisibleChanged();
69 69 void handleOpacityChanged();
70 70 virtual void handleUpdatedBars();
71 71 void handleLabelsPositionChanged();
72 72 virtual void positionLabels();
73 73
74 74 protected:
75 void positionLabelsVertical();
75 76
76 77 qreal m_domainMinX;
77 78 qreal m_domainMaxX;
78 79 qreal m_domainMinY;
79 80 qreal m_domainMaxY;
80 81
81 82 QRectF m_rect;
82 83 QVector<QRectF> m_layout;
83 84
84 85 BarAnimation *m_animation;
85 86
86 87 QAbstractBarSeries *m_series; // Not owned.
87 88 QList<Bar *> m_bars;
88 89 QList<QGraphicsTextItem *> m_labels;
89 90 QSizeF m_oldSize;
90 91 };
91 92
92 93 QT_CHARTS_END_NAMESPACE
93 94
94 95 #endif // ABSTRACTBARCHARTITEM_H
@@ -1,1054 +1,1092
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include <QtCharts/QAbstractBarSeries>
20 20 #include <private/qabstractbarseries_p.h>
21 21 #include <QtCharts/QBarSet>
22 22 #include <private/qbarset_p.h>
23 23 #include <private/abstractdomain_p.h>
24 24 #include <private/chartdataset_p.h>
25 25 #include <private/charttheme_p.h>
26 26 #include <QtCharts/QValueAxis>
27 27 #include <QtCharts/QBarCategoryAxis>
28 28 #include <QtCharts/QBarLegendMarker>
29 29 #include <private/baranimation_p.h>
30 30 #include <private/abstractbarchartitem_p.h>
31 31 #include <private/qchart_p.h>
32 32
33 33 QT_CHARTS_BEGIN_NAMESPACE
34 34
35 35 /*!
36 36 \class QAbstractBarSeries
37 37 \inmodule Qt Charts
38 38 \brief Series for creating a bar chart.
39 39
40 40 QAbstractBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to
41 41 the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar
42 42 and y-value is the height of the bar. The category names are ignored with this series and x-axis
43 43 shows the x-values.
44 44
45 45 See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart.
46 46 \image examples_barchart.png
47 47
48 48 \sa QBarSet, QStackedBarSeries, QPercentBarSeries
49 49 */
50 50 /*!
51 51 \qmltype AbstractBarSeries
52 52 \instantiates QAbstractBarSeries
53 53 \inqmlmodule QtCharts
54 54
55 55 \inherits AbstractSeries
56 56
57 57 \brief Series type for creating a bar chart.
58 58
59 59 The following QML shows how to create a simple bar chart:
60 60 \snippet qmlchart/qml/qmlchart/View6.qml 1
61 61
62 62 \beginfloatleft
63 63 \image examples_qmlchart6.png
64 64 \endfloat
65 65 \clearfloat
66 66 */
67 67
68 68 /*!
69 69 \qmlproperty AbstractAxis AbstractBarSeries::axisX
70 70 The x axis used for the series. If you leave both axisX and axisXTop undefined, a BarCategoriesAxis is created for
71 71 the series.
72 72 \sa axisXTop
73 73 */
74 74
75 75 /*!
76 76 \qmlproperty AbstractAxis AbstractBarSeries::axisY
77 77 The y axis used for the series. If you leave both axisY and axisYRight undefined, a ValueAxis is created for
78 78 the series.
79 79 \sa axisYRight
80 80 */
81 81
82 82 /*!
83 83 \qmlproperty AbstractAxis AbstractBarSeries::axisXTop
84 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 85 axisXTop, but not both.
86 86 \sa axisX
87 87 */
88 88
89 89 /*!
90 90 \qmlproperty AbstractAxis AbstractBarSeries::axisYRight
91 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 92 or axisYRight, but not both.
93 93 \sa axisY
94 94 */
95 95
96 96 /*!
97 97 \property QAbstractBarSeries::barWidth
98 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 99 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
100 100 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
101 101 Note that with QBarSeries this value means the width of one group of bars instead of just one bar.
102 102 \sa QBarSeries
103 103 */
104 104 /*!
105 105 \qmlproperty real AbstractBarSeries::barWidth
106 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 107 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
108 108 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
109 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 113 \property QAbstractBarSeries::count
114 114 Holds the number of sets in series.
115 115 */
116 116 /*!
117 117 \qmlproperty int AbstractBarSeries::count
118 118 Holds the number of sets in series.
119 119 */
120 120
121 121 /*!
122 122 \property QAbstractBarSeries::labelsVisible
123 123 Defines the visibility of the labels in series
124 124 */
125 125 /*!
126 126 \qmlproperty bool AbstractBarSeries::labelsVisible
127 127 Defines the visibility of the labels in series
128 128 */
129 129
130 130 /*!
131 131 \property QAbstractBarSeries::labelsFormat
132 132 The \a format used for showing labels in series.
133 133
134 134 QAbstractBarSeries supports the following format tag:
135 135 \table
136 136 \row
137 137 \li @value \li The value of the bar
138 138 \endtable
139 139
140 140 For example, the following usage of the format tags would produce labels that show the value
141 141 followed by unit ('u'):
142 142 \code
143 143 series->setLabelsFormat("@value u");
144 144 \endcode
145 145
146 146 By default, the labels shows the value of the bar. For percent bar series '%' is added after
147 147 the value. The labels are shown on the plot area, labels on the edge of the plot area are cut.
148 148 If the bars are close to each other the labels may overlap.
149 149
150 150 \sa QAbstractBarSeries::labelsVisible, QAbstractBarSeries::labelsPosition
151 151 */
152 152 /*!
153 153 \qmlproperty string AbstractBarSeries::labelsFormat
154 154 The format used for showing labels in series.
155 155
156 156 \sa QAbstractBarSeries::labelsFormat, labelsVisible, labelsPosition
157 157 */
158 158 /*!
159 159 \fn void QAbstractBarSeries::labelsFormatChanged(const QString &format)
160 160 Signal is emitted when the \a format of data value labels is changed.
161 161 */
162 162 /*!
163 163 \qmlsignal XYSeries::onLabelsFormatChanged(string format)
164 164 Signal is emitted when the \a format of data value labels is changed.
165 165 */
166 166
167 167 /*!
168 168 \enum QAbstractBarSeries::LabelsPosition
169 169
170 170 This enum describes the position of the data value labels.
171 171
172 172 \value LabelsCenter Label is in the center of the bar.
173 173 \value LabelsInsideEnd Label is inside the bar at the high end of it.
174 174 \value LabelsInsideBase Label is inside the bar at the low end of it.
175 175 \value LabelsOutsideEnd Label is outside the bar at the high end of it.
176 176 */
177 177
178 178 /*!
179 179 \property QAbstractBarSeries::labelsPosition
180 180 Defines the \a position of value labels.
181 181
182 182 \sa QAbstractBarSeries::labelsVisible, QAbstractBarSeries::labelsFormat
183 183 */
184 184 /*!
185 185 \qmlproperty string AbstractBarSeries::labelsPosition
186 186 Defines the \a position of value labels.
187 187
188 188 \sa labelsVisible, labelsFormat
189 189 */
190 190 /*!
191 191 \fn void QAbstractBarSeries::labelsPositionChanged(QAbstractBarSeries::LabelsPosition position)
192 192 Signal is emitted when the \a position of value labels is changed.
193 193 */
194 194 /*!
195 195 \qmlsignal AbstractBarSeries::onLabelsPositionChanged(LabelsPosition position)
196 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 217 \fn void QAbstractBarSeries::clicked(int index, QBarSet *barset)
201 218 The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset.
202 219 Clicked bar inside set is indexed by \a index
203 220 */
204 221 /*!
205 222 \qmlsignal AbstractBarSeries::onClicked(int index, BarSet barset)
206 223 The signal is emitted if the user clicks with a mouse on top of BarSet.
207 224 Clicked bar inside set is indexed by \a index
208 225 */
209 226
210 227 /*!
211 228 \fn void QAbstractBarSeries::pressed(int index, QBarSet *barset)
212 229 The signal is emitted if the user presses with a mouse on top of QBarSet \a barset.
213 230 Pressed bar inside set is indexed by \a index
214 231 */
215 232 /*!
216 233 \qmlsignal AbstractBarSeries::onPressed(int index, BarSet barset)
217 234 The signal is emitted if the user presses with a mouse on top of BarSet.
218 235 Pressed bar inside set is indexed by \a index
219 236 */
220 237
221 238 /*!
222 239 \fn void QAbstractBarSeries::released(int index, QBarSet *barset)
223 240 The signal is emitted if the user releases with a mouse on top of QBarSet \a barset.
224 241 Released bar inside set is indexed by \a index
225 242 */
226 243 /*!
227 244 \qmlsignal AbstractBarSeries::onReleased(int index, BarSet barset)
228 245 The signal is emitted if the user releases with a mouse on top of BarSet.
229 246 Released bar inside set is indexed by \a index
230 247 */
231 248
232 249 /*!
233 250 \fn void QAbstractBarSeries::doubleClicked(int index, QBarSet *barset)
234 251 The signal is emitted if the user doubleclicks with a mouse on top of QBarSet \a barset.
235 252 DoubleClicked bar inside set is indexed by \a index
236 253 */
237 254 /*!
238 255 \qmlsignal AbstractBarSeries::onDoubleClicked(int index, BarSet barset)
239 256 The signal is emitted if the user doubleclicks with a mouse on top of BarSet.
240 257 Doubleclicked bar inside set is indexed by \a index
241 258 */
242 259
243 260 /*!
244 261 \fn void QAbstractBarSeries::hovered(bool status, int index, QBarSet* barset)
245 262
246 263 The signal is emitted if mouse is hovered on top of series.
247 264 Parameter \a barset is the pointer of barset, where hover happened.
248 265 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
249 266 Hovered bar inside the set is indexed by \a index.
250 267 */
251 268 /*!
252 269 \qmlsignal AbstractBarSeries::onHovered(bool status, int index, BarSet barset)
253 270
254 271 The signal is emitted if mouse is hovered on top of series.
255 272 Parameter \a barset is the pointer of barset, where hover happened.
256 273 Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series.
257 274 Hovered bar inside the set is indexed by \a index.
258 275 */
259 276
260 277 /*!
261 278 \fn void QAbstractBarSeries::countChanged()
262 279 This signal is emitted when barset count has been changed, for example by append or remove.
263 280 */
264 281 /*!
265 282 \qmlsignal AbstractBarSeries::onCountChanged()
266 283 This signal is emitted when barset count has been changed, for example by append or remove.
267 284 */
268 285
269 286 /*!
270 287 \fn void QAbstractBarSeries::labelsVisibleChanged()
271 288 This signal is emitted when labels visibility have changed.
272 289 \sa isLabelsVisible(), setLabelsVisible()
273 290 */
274 291
275 292 /*!
276 293 \fn void QAbstractBarSeries::barsetsAdded(QList<QBarSet*> sets)
277 294 This signal is emitted when \a sets have been added to the series.
278 295 \sa append(), insert()
279 296 */
280 297 /*!
281 298 \qmlsignal AbstractBarSeries::onBarsetsAdded(BarSet barset)
282 299 Emitted when \a barset has been added to the series.
283 300 */
284 301
285 302 /*!
286 303 \fn void QAbstractBarSeries::barsetsRemoved(QList<QBarSet*> sets)
287 304 This signal is emitted when \a sets have been removed from the series.
288 305 \sa remove()
289 306 */
290 307 /*!
291 308 \qmlsignal AbstractBarSeries::onBarsetsRemoved(BarSet barset)
292 309 Emitted when \a barset has been removed from the series.
293 310 */
294 311
295 312 /*!
296 313 \qmlmethod BarSet AbstractBarSeries::at(int index)
297 314 Returns bar set at \a index. Returns null if the index is not valid.
298 315 */
299 316
300 317 /*!
301 318 \qmlmethod BarSet AbstractBarSeries::append(string label, VariantList values)
302 319 Adds a new bar set with \a label and \a values to \a index. Values is a list of reals.
303 320 For example:
304 321 \code
305 322 myBarSeries.append("set 1", [0, 0.2, 0.2, 0.5, 0.4, 1.5, 0.9]);
306 323 \endcode
307 324 */
308 325
309 326 /*!
310 327 \qmlmethod BarSet AbstractBarSeries::insert(int index, string label, VariantList values)
311 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 329 If index is zero or smaller, the new barset is prepended. If the index is count or bigger, the new barset is
313 330 appended.
314 331 \sa AbstractBarSeries::append()
315 332 */
316 333
317 334 /*!
318 335 \qmlmethod bool AbstractBarSeries::remove(BarSet barset)
319 336 Removes the barset from the series. Returns true if successful, false otherwise.
320 337 */
321 338
322 339 /*!
323 340 \qmlmethod AbstractBarSeries::clear()
324 341 Removes all barsets from the series.
325 342 */
326 343
327 344 /*!
328 345 Destructs abstractbarseries and owned barsets.
329 346 */
330 347 QAbstractBarSeries::~QAbstractBarSeries()
331 348 {
332 349
333 350 }
334 351
335 352 /*!
336 353 \internal
337 354 */
338 355 QAbstractBarSeries::QAbstractBarSeries(QAbstractBarSeriesPrivate &o, QObject *parent)
339 356 : QAbstractSeries(o, parent)
340 357 {
341 358 Q_D(QAbstractSeries);
342 359 QObject::connect(this, SIGNAL(countChanged()), d, SIGNAL(countChanged()));
343 360 }
344 361
345 362 /*!
346 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 364 is zero and negative values are treated as zero. Setting the width to zero means that width of the bar on screen
348 365 is one pixel no matter what the scale of x-axis is. Bars wider than zero are scaled with x-axis.
349 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 368 void QAbstractBarSeries::setBarWidth(qreal width)
352 369 {
353 370 Q_D(QAbstractBarSeries);
354 371 d->setBarWidth(width);
355 372 }
356 373
357 374 /*!
358 375 Returns the width of the bars of the series.
359 376 \sa setBarWidth()
360 377 */
361 378 qreal QAbstractBarSeries::barWidth() const
362 379 {
363 380 Q_D(const QAbstractBarSeries);
364 381 return d->barWidth();
365 382 }
366 383
367 384 /*!
368 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 386 Returns true, if appending succeeded.
370 387 */
371 388 bool QAbstractBarSeries::append(QBarSet *set)
372 389 {
373 390 Q_D(QAbstractBarSeries);
374 391 bool success = d->append(set);
375 392 if (success) {
376 393 QList<QBarSet *> sets;
377 394 sets.append(set);
378 395 set->setParent(this);
379 396 emit barsetsAdded(sets);
380 397 emit countChanged();
381 398 }
382 399 return success;
383 400 }
384 401
385 402 /*!
386 403 Removes barset from series. Releases ownership of \a set. Deletes the set, if remove
387 404 was successful.
388 405 Returns true, if set was removed.
389 406 */
390 407 bool QAbstractBarSeries::remove(QBarSet *set)
391 408 {
392 409 Q_D(QAbstractBarSeries);
393 410 bool success = d->remove(set);
394 411 if (success) {
395 412 QList<QBarSet *> sets;
396 413 sets.append(set);
397 414 set->setParent(0);
398 415 emit barsetsRemoved(sets);
399 416 emit countChanged();
400 417 delete set;
401 418 set = 0;
402 419 }
403 420 return success;
404 421 }
405 422
406 423 /*!
407 424 Takes a single \a set from the series. Does not delete the barset object.
408 425
409 426 NOTE: The series remains as the barset's parent object. You must set the
410 427 parent object to take full ownership.
411 428
412 429 Returns true if take was successful.
413 430 */
414 431 bool QAbstractBarSeries::take(QBarSet *set)
415 432 {
416 433 Q_D(QAbstractBarSeries);
417 434 bool success = d->remove(set);
418 435 if (success) {
419 436 QList<QBarSet *> sets;
420 437 sets.append(set);
421 438 emit barsetsRemoved(sets);
422 439 emit countChanged();
423 440 }
424 441 return success;
425 442 }
426 443
427 444 /*!
428 445 Adds a list of barsets to series. Takes ownership of \a sets.
429 446 Returns true, if all sets were appended successfully. If any of the sets is null or is already appended to series,
430 447 nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended
431 448 and function returns false.
432 449 */
433 450 bool QAbstractBarSeries::append(QList<QBarSet *> sets)
434 451 {
435 452 Q_D(QAbstractBarSeries);
436 453 bool success = d->append(sets);
437 454 if (success) {
438 455 foreach (QBarSet *set, sets)
439 456 set->setParent(this);
440 457 emit barsetsAdded(sets);
441 458 emit countChanged();
442 459 }
443 460 return success;
444 461 }
445 462
446 463 /*!
447 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 465 Returns true, if inserting succeeded.
449 466
450 467 */
451 468 bool QAbstractBarSeries::insert(int index, QBarSet *set)
452 469 {
453 470 Q_D(QAbstractBarSeries);
454 471 bool success = d->insert(index, set);
455 472 if (success) {
456 473 QList<QBarSet *> sets;
457 474 sets.append(set);
458 475 emit barsetsAdded(sets);
459 476 emit countChanged();
460 477 }
461 478 return success;
462 479 }
463 480
464 481 /*!
465 482 Removes all barsets from the series. Deletes removed sets.
466 483 */
467 484 void QAbstractBarSeries::clear()
468 485 {
469 486 Q_D(QAbstractBarSeries);
470 487 QList<QBarSet *> sets = barSets();
471 488 bool success = d->remove(sets);
472 489 if (success) {
473 490 emit barsetsRemoved(sets);
474 491 emit countChanged();
475 492 foreach (QBarSet *set, sets)
476 493 delete set;
477 494 }
478 495 }
479 496
480 497 /*!
481 498 Returns number of sets in series.
482 499 */
483 500 int QAbstractBarSeries::count() const
484 501 {
485 502 Q_D(const QAbstractBarSeries);
486 503 return d->m_barSets.count();
487 504 }
488 505
489 506 /*!
490 507 Returns a list of sets in series. Keeps ownership of sets.
491 508 */
492 509 QList<QBarSet *> QAbstractBarSeries::barSets() const
493 510 {
494 511 Q_D(const QAbstractBarSeries);
495 512 return d->m_barSets;
496 513 }
497 514
498 515 /*!
499 516 Sets the visibility of labels in series to \a visible
500 517 */
501 518 void QAbstractBarSeries::setLabelsVisible(bool visible)
502 519 {
503 520 Q_D(QAbstractBarSeries);
504 521 if (d->m_labelsVisible != visible) {
505 522 d->setLabelsVisible(visible);
506 523 emit labelsVisibleChanged();
507 524 }
508 525 }
509 526
510 527 /*!
511 528 Returns the visibility of labels
512 529 */
513 530 bool QAbstractBarSeries::isLabelsVisible() const
514 531 {
515 532 Q_D(const QAbstractBarSeries);
516 533 return d->m_labelsVisible;
517 534 }
518 535
519 536 void QAbstractBarSeries::setLabelsFormat(const QString &format)
520 537 {
521 538 Q_D(QAbstractBarSeries);
522 539 if (d->m_labelsFormat != format) {
523 540 d->m_labelsFormat = format;
524 541 emit labelsFormatChanged(format);
525 542 }
526 543 }
527 544
528 545 QString QAbstractBarSeries::labelsFormat() const
529 546 {
530 547 Q_D(const QAbstractBarSeries);
531 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 566 void QAbstractBarSeries::setLabelsPosition(QAbstractBarSeries::LabelsPosition position)
535 567 {
536 568 Q_D(QAbstractBarSeries);
537 569 if (d->m_labelsPosition != position) {
538 570 d->m_labelsPosition = position;
539 571 emit labelsPositionChanged(position);
540 572 }
541 573 }
542 574
543 575 QAbstractBarSeries::LabelsPosition QAbstractBarSeries::labelsPosition() const
544 576 {
545 577 Q_D(const QAbstractBarSeries);
546 578 return d->m_labelsPosition;
547 579 }
548 580
549 581 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
550 582
551 583 QAbstractBarSeriesPrivate::QAbstractBarSeriesPrivate(QAbstractBarSeries *q) :
552 584 QAbstractSeriesPrivate(q),
553 585 m_barWidth(0.5), // Default value is 50% of category width
554 586 m_labelsVisible(false),
555 587 m_visible(true),
556 588 m_blockBarUpdate(false),
557 589 m_labelsFormat(),
558 m_labelsPosition(QAbstractBarSeries::LabelsCenter)
590 m_labelsPosition(QAbstractBarSeries::LabelsCenter),
591 m_labelsAngle(0)
559 592 {
560 593 }
561 594
562 595 int QAbstractBarSeriesPrivate::categoryCount() const
563 596 {
564 597 // No categories defined. return count of longest set.
565 598 int count = 0;
566 599 for (int i = 0; i < m_barSets.count(); i++) {
567 600 if (m_barSets.at(i)->count() > count)
568 601 count = m_barSets.at(i)->count();
569 602 }
570 603
571 604 return count;
572 605 }
573 606
574 607 void QAbstractBarSeriesPrivate::setBarWidth(qreal width)
575 608 {
576 609 if (width < 0.0)
577 610 width = 0.0;
578 611 m_barWidth = width;
579 612 emit updatedLayout();
580 613 }
581 614
582 615 qreal QAbstractBarSeriesPrivate::barWidth() const
583 616 {
584 617 return m_barWidth;
585 618 }
586 619
587 620 QBarSet *QAbstractBarSeriesPrivate::barsetAt(int index)
588 621 {
589 622 return m_barSets.at(index);
590 623 }
591 624
592 625 void QAbstractBarSeriesPrivate::setVisible(bool visible)
593 626 {
594 627 m_visible = visible;
595 628 emit visibleChanged();
596 629 }
597 630
598 631 void QAbstractBarSeriesPrivate::setLabelsVisible(bool visible)
599 632 {
600 633 m_labelsVisible = visible;
601 634 emit labelsVisibleChanged(visible);
602 635 }
603 636
604 637 qreal QAbstractBarSeriesPrivate::min()
605 638 {
606 639 if (m_barSets.count() <= 0)
607 640 return 0;
608 641
609 642 qreal min = INT_MAX;
610 643
611 644 for (int i = 0; i < m_barSets.count(); i++) {
612 645 int categoryCount = m_barSets.at(i)->count();
613 646 for (int j = 0; j < categoryCount; j++) {
614 647 qreal temp = m_barSets.at(i)->at(j);
615 648 if (temp < min)
616 649 min = temp;
617 650 }
618 651 }
619 652 return min;
620 653 }
621 654
622 655 qreal QAbstractBarSeriesPrivate::max()
623 656 {
624 657 if (m_barSets.count() <= 0)
625 658 return 0;
626 659
627 660 qreal max = INT_MIN;
628 661
629 662 for (int i = 0; i < m_barSets.count(); i++) {
630 663 int categoryCount = m_barSets.at(i)->count();
631 664 for (int j = 0; j < categoryCount; j++) {
632 665 qreal temp = m_barSets.at(i)->at(j);
633 666 if (temp > max)
634 667 max = temp;
635 668 }
636 669 }
637 670
638 671 return max;
639 672 }
640 673
641 674 qreal QAbstractBarSeriesPrivate::valueAt(int set, int category)
642 675 {
643 676 if ((set < 0) || (set >= m_barSets.count()))
644 677 return 0; // No set, no value.
645 678 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
646 679 return 0; // No category, no value.
647 680
648 681 return m_barSets.at(set)->at(category);
649 682 }
650 683
651 684 qreal QAbstractBarSeriesPrivate::percentageAt(int set, int category)
652 685 {
653 686 if ((set < 0) || (set >= m_barSets.count()))
654 687 return 0; // No set, no value.
655 688 else if ((category < 0) || (category >= m_barSets.at(set)->count()))
656 689 return 0; // No category, no value.
657 690
658 691 qreal value = m_barSets.at(set)->at(category);
659 692 qreal sum = categorySum(category);
660 693 if (qFuzzyCompare(sum, 0))
661 694 return 0;
662 695
663 696 return value / sum;
664 697 }
665 698
666 699 qreal QAbstractBarSeriesPrivate::categorySum(int category)
667 700 {
668 701 qreal sum(0);
669 702 int count = m_barSets.count(); // Count sets
670 703 for (int set = 0; set < count; set++) {
671 704 if (category < m_barSets.at(set)->count())
672 705 sum += m_barSets.at(set)->at(category);
673 706 }
674 707 return sum;
675 708 }
676 709
677 710 qreal QAbstractBarSeriesPrivate::absoluteCategorySum(int category)
678 711 {
679 712 qreal sum(0);
680 713 int count = m_barSets.count(); // Count sets
681 714 for (int set = 0; set < count; set++) {
682 715 if (category < m_barSets.at(set)->count())
683 716 sum += qAbs(m_barSets.at(set)->at(category));
684 717 }
685 718 return sum;
686 719 }
687 720
688 721 qreal QAbstractBarSeriesPrivate::maxCategorySum()
689 722 {
690 723 qreal max = INT_MIN;
691 724 int count = categoryCount();
692 725 for (int i = 0; i < count; i++) {
693 726 qreal sum = categorySum(i);
694 727 if (sum > max)
695 728 max = sum;
696 729 }
697 730 return max;
698 731 }
699 732
700 733 qreal QAbstractBarSeriesPrivate::minX()
701 734 {
702 735 if (m_barSets.count() <= 0)
703 736 return 0;
704 737
705 738 qreal min = INT_MAX;
706 739
707 740 for (int i = 0; i < m_barSets.count(); i++) {
708 741 int categoryCount = m_barSets.at(i)->count();
709 742 for (int j = 0; j < categoryCount; j++) {
710 743 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
711 744 if (temp < min)
712 745 min = temp;
713 746 }
714 747 }
715 748 return min;
716 749 }
717 750
718 751 qreal QAbstractBarSeriesPrivate::maxX()
719 752 {
720 753 if (m_barSets.count() <= 0)
721 754 return 0;
722 755
723 756 qreal max = INT_MIN;
724 757
725 758 for (int i = 0; i < m_barSets.count(); i++) {
726 759 int categoryCount = m_barSets.at(i)->count();
727 760 for (int j = 0; j < categoryCount; j++) {
728 761 qreal temp = m_barSets.at(i)->d_ptr.data()->m_values.at(j).x();
729 762 if (temp > max)
730 763 max = temp;
731 764 }
732 765 }
733 766
734 767 return max;
735 768 }
736 769
737 770 qreal QAbstractBarSeriesPrivate::categoryTop(int category)
738 771 {
739 772 // Returns top (sum of all positive values) of category.
740 773 // Returns 0, if all values are negative
741 774 qreal top(0);
742 775 int count = m_barSets.count();
743 776 for (int set = 0; set < count; set++) {
744 777 if (category < m_barSets.at(set)->count()) {
745 778 qreal temp = m_barSets.at(set)->at(category);
746 779 if (temp > 0) {
747 780 top += temp;
748 781 }
749 782 }
750 783 }
751 784 return top;
752 785 }
753 786
754 787 qreal QAbstractBarSeriesPrivate::categoryBottom(int category)
755 788 {
756 789 // Returns bottom (sum of all negative values) of category
757 790 // Returns 0, if all values are positive
758 791 qreal bottom(0);
759 792 int count = m_barSets.count();
760 793 for (int set = 0; set < count; set++) {
761 794 if (category < m_barSets.at(set)->count()) {
762 795 qreal temp = m_barSets.at(set)->at(category);
763 796 if (temp < 0) {
764 797 bottom += temp;
765 798 }
766 799 }
767 800 }
768 801 return bottom;
769 802 }
770 803
771 804 qreal QAbstractBarSeriesPrivate::top()
772 805 {
773 806 // Returns top of all categories
774 807 qreal top(0);
775 808 int count = categoryCount();
776 809 for (int i = 0; i < count; i++) {
777 810 qreal temp = categoryTop(i);
778 811 if (temp > top)
779 812 top = temp;
780 813 }
781 814 return top;
782 815 }
783 816
784 817 qreal QAbstractBarSeriesPrivate::bottom()
785 818 {
786 819 // Returns bottom of all categories
787 820 qreal bottom(0);
788 821 int count = categoryCount();
789 822 for (int i = 0; i < count; i++) {
790 823 qreal temp = categoryBottom(i);
791 824 if (temp < bottom)
792 825 bottom = temp;
793 826 }
794 827 return bottom;
795 828 }
796 829
797 830 bool QAbstractBarSeriesPrivate::blockBarUpdate()
798 831 {
799 832 return m_blockBarUpdate;
800 833 }
801 834
835 qreal QAbstractBarSeriesPrivate::labelsAngle() const
836 {
837 return m_labelsAngle;
838 }
839
802 840 void QAbstractBarSeriesPrivate::initializeDomain()
803 841 {
804 842 qreal minX(domain()->minX());
805 843 qreal minY(domain()->minY());
806 844 qreal maxX(domain()->maxX());
807 845 qreal maxY(domain()->maxY());
808 846
809 847 qreal seriesMinX = this->minX();
810 848 qreal seriesMaxX = this->maxX();
811 849 qreal y = max();
812 850 minX = qMin(minX, seriesMinX - (qreal)0.5);
813 851 minY = qMin(minY, y);
814 852 maxX = qMax(maxX, seriesMaxX + (qreal)0.5);
815 853 maxY = qMax(maxY, y);
816 854
817 855 domain()->setRange(minX, maxX, minY, maxY);
818 856 }
819 857
820 858 QList<QLegendMarker*> QAbstractBarSeriesPrivate::createLegendMarkers(QLegend* legend)
821 859 {
822 860 Q_Q(QAbstractBarSeries);
823 861 QList<QLegendMarker*> markers;
824 862
825 863 foreach(QBarSet* set, q->barSets()) {
826 864 QBarLegendMarker* marker = new QBarLegendMarker(q,set,legend);
827 865 markers << marker;
828 866 }
829 867 return markers;
830 868 }
831 869
832 870
833 871 bool QAbstractBarSeriesPrivate::append(QBarSet *set)
834 872 {
835 873 if ((m_barSets.contains(set)) || (set == 0))
836 874 return false; // Fail if set is already in list or set is null.
837 875
838 876 m_barSets.append(set);
839 877 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
840 878 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
841 879 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
842 880
843 881 emit restructuredBars(); // this notifies barchartitem
844 882 return true;
845 883 }
846 884
847 885 bool QAbstractBarSeriesPrivate::remove(QBarSet *set)
848 886 {
849 887 if (!m_barSets.contains(set))
850 888 return false; // Fail if set is not in list
851 889
852 890 m_barSets.removeOne(set);
853 891 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
854 892 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
855 893 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
856 894
857 895 emit restructuredBars(); // this notifies barchartitem
858 896 return true;
859 897 }
860 898
861 899 bool QAbstractBarSeriesPrivate::append(QList<QBarSet * > sets)
862 900 {
863 901 foreach (QBarSet *set, sets) {
864 902 if ((set == 0) || (m_barSets.contains(set)))
865 903 return false; // Fail if any of the sets is null or is already appended.
866 904 if (sets.count(set) != 1)
867 905 return false; // Also fail if same set is more than once in given list.
868 906 }
869 907
870 908 foreach (QBarSet *set, sets) {
871 909 m_barSets.append(set);
872 910 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
873 911 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
874 912 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
875 913 }
876 914
877 915 emit restructuredBars(); // this notifies barchartitem
878 916 return true;
879 917 }
880 918
881 919 bool QAbstractBarSeriesPrivate::remove(QList<QBarSet * > sets)
882 920 {
883 921 if (sets.count() == 0)
884 922 return false;
885 923
886 924 foreach (QBarSet *set, sets) {
887 925 if ((set == 0) || (!m_barSets.contains(set)))
888 926 return false; // Fail if any of the sets is null or is not in series
889 927 if (sets.count(set) != 1)
890 928 return false; // Also fail if same set is more than once in given list.
891 929 }
892 930
893 931 foreach (QBarSet *set, sets) {
894 932 m_barSets.removeOne(set);
895 933 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
896 934 QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
897 935 QObject::disconnect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
898 936 }
899 937
900 938 emit restructuredBars(); // this notifies barchartitem
901 939
902 940 return true;
903 941 }
904 942
905 943 bool QAbstractBarSeriesPrivate::insert(int index, QBarSet *set)
906 944 {
907 945 if ((m_barSets.contains(set)) || (set == 0))
908 946 return false; // Fail if set is already in list or set is null.
909 947
910 948 m_barSets.insert(index, set);
911 949 QObject::connect(set->d_ptr.data(), SIGNAL(updatedLayout()), this, SIGNAL(updatedLayout()));
912 950 QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), this, SIGNAL(updatedBars()));
913 951 QObject::connect(set->d_ptr.data(), SIGNAL(restructuredBars()), this, SIGNAL(restructuredBars()));
914 952
915 953 emit restructuredBars(); // this notifies barchartitem
916 954 return true;
917 955 }
918 956
919 957 void QAbstractBarSeriesPrivate::initializeAxes()
920 958 {
921 959 Q_Q(QAbstractBarSeries);
922 960
923 961 foreach(QAbstractAxis* axis, m_axes) {
924 962
925 963 if (axis->type() == QAbstractAxis::AxisTypeBarCategory) {
926 964 switch (q->type()) {
927 965 case QAbstractSeries::SeriesTypeHorizontalBar:
928 966 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
929 967 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
930 968 if (axis->orientation() == Qt::Vertical)
931 969 populateCategories(qobject_cast<QBarCategoryAxis *>(axis));
932 970 break;
933 971 case QAbstractSeries::SeriesTypeBar:
934 972 case QAbstractSeries::SeriesTypePercentBar:
935 973 case QAbstractSeries::SeriesTypeStackedBar:
936 974 case QAbstractSeries::SeriesTypeBoxPlot:
937 975 if (axis->orientation() == Qt::Horizontal)
938 976 populateCategories(qobject_cast<QBarCategoryAxis *>(axis));
939 977 break;
940 978 default:
941 979 qWarning() << "Unexpected series type";
942 980 break;
943 981 }
944 982 }
945 983 }
946 984 }
947 985
948 986 QAbstractAxis::AxisType QAbstractBarSeriesPrivate::defaultAxisType(Qt::Orientation orientation) const
949 987 {
950 988 Q_Q(const QAbstractBarSeries);
951 989
952 990 switch (q->type()) {
953 991 case QAbstractSeries::SeriesTypeHorizontalBar:
954 992 case QAbstractSeries::SeriesTypeHorizontalPercentBar:
955 993 case QAbstractSeries::SeriesTypeHorizontalStackedBar:
956 994 if (orientation == Qt::Vertical)
957 995 return QAbstractAxis::AxisTypeBarCategory;
958 996 break;
959 997 case QAbstractSeries::SeriesTypeBar:
960 998 case QAbstractSeries::SeriesTypePercentBar:
961 999 case QAbstractSeries::SeriesTypeStackedBar:
962 1000 case QAbstractSeries::SeriesTypeBoxPlot:
963 1001 if (orientation == Qt::Horizontal)
964 1002 return QAbstractAxis::AxisTypeBarCategory;
965 1003 break;
966 1004 default:
967 1005 qWarning() << "Unexpected series type";
968 1006 break;
969 1007 }
970 1008 return QAbstractAxis::AxisTypeValue;
971 1009
972 1010 }
973 1011
974 1012 void QAbstractBarSeriesPrivate::populateCategories(QBarCategoryAxis *axis)
975 1013 {
976 1014 QStringList categories;
977 1015 if (axis->categories().isEmpty()) {
978 1016 for (int i(1); i < categoryCount() + 1; i++)
979 1017 categories << presenter()->numberToString(i);
980 1018 axis->append(categories);
981 1019 }
982 1020 }
983 1021
984 1022 QAbstractAxis* QAbstractBarSeriesPrivate::createDefaultAxis(Qt::Orientation orientation) const
985 1023 {
986 1024 if (defaultAxisType(orientation) == QAbstractAxis::AxisTypeBarCategory)
987 1025 return new QBarCategoryAxis;
988 1026 else
989 1027 return new QValueAxis;
990 1028 }
991 1029
992 1030 void QAbstractBarSeriesPrivate::initializeTheme(int index, ChartTheme* theme, bool forced)
993 1031 {
994 1032 m_blockBarUpdate = true; // Ensures that the bars are not updated before the theme is ready
995 1033
996 1034 const QList<QGradient> gradients = theme->seriesGradients();
997 1035
998 1036 qreal takeAtPos = 0.5;
999 1037 qreal step = 0.2;
1000 1038 if (m_barSets.count() > 1) {
1001 1039 step = 1.0 / (qreal) m_barSets.count();
1002 1040 if (m_barSets.count() % gradients.count())
1003 1041 step *= gradients.count();
1004 1042 else
1005 1043 step *= (gradients.count() - 1);
1006 1044 }
1007 1045
1008 1046 for (int i(0); i < m_barSets.count(); i++) {
1009 1047 int colorIndex = (index + i) % gradients.count();
1010 1048 if (i > 0 && i %gradients.count() == 0) {
1011 1049 // There is no dedicated base color for each sets, generate more colors
1012 1050 takeAtPos += step;
1013 1051 if (takeAtPos == 1.0)
1014 1052 takeAtPos += step;
1015 1053 takeAtPos -= (int) takeAtPos;
1016 1054 }
1017 1055 if (forced || QChartPrivate::defaultBrush() == m_barSets.at(i)->d_ptr->m_brush)
1018 1056 m_barSets.at(i)->setBrush(ChartThemeManager::colorAt(gradients.at(colorIndex), takeAtPos));
1019 1057
1020 1058 // Pick label color from the opposite end of the gradient.
1021 1059 // 0.3 as a boundary seems to work well.
1022 1060 if (forced || QChartPrivate::defaultBrush() == m_barSets.at(i)->d_ptr->m_labelBrush) {
1023 1061 if (takeAtPos < 0.3)
1024 1062 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 1));
1025 1063 else
1026 1064 m_barSets.at(i)->setLabelBrush(ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0));
1027 1065 }
1028 1066 if (forced || QChartPrivate::defaultPen() == m_barSets.at(i)->d_ptr->m_pen) {
1029 1067 QColor c = ChartThemeManager::colorAt(gradients.at(index % gradients.size()), 0.0);
1030 1068 m_barSets.at(i)->setPen(c);
1031 1069 }
1032 1070 }
1033 1071 m_blockBarUpdate = false;
1034 1072 emit updatedBars();
1035 1073 }
1036 1074
1037 1075 void QAbstractBarSeriesPrivate::initializeAnimations(QChart::AnimationOptions options)
1038 1076 {
1039 1077 AbstractBarChartItem *bar = static_cast<AbstractBarChartItem *>(m_item.data());
1040 1078 Q_ASSERT(bar);
1041 1079 if (bar->animation())
1042 1080 bar->animation()->stopAndDestroyLater();
1043 1081
1044 1082 if (options.testFlag(QChart::SeriesAnimations))
1045 1083 bar->setAnimation(new BarAnimation(bar));
1046 1084 else
1047 1085 bar->setAnimation(0);
1048 1086 QAbstractSeriesPrivate::initializeAnimations(options);
1049 1087 }
1050 1088
1051 1089 #include "moc_qabstractbarseries.cpp"
1052 1090 #include "moc_qabstractbarseries_p.cpp"
1053 1091
1054 1092 QT_CHARTS_END_NAMESPACE
@@ -1,105 +1,110
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #ifndef QABSTRACTBARSERIES_H
20 20 #define QABSTRACTBARSERIES_H
21 21
22 22 #include <QtCharts/QAbstractSeries>
23 23 #include <QtCore/QStringList>
24 24
25 25 QT_CHARTS_BEGIN_NAMESPACE
26 26
27 27 class QBarSet;
28 28 class QAbstractBarSeriesPrivate;
29 29
30 30 // Container for series
31 31 class QT_CHARTS_EXPORT QAbstractBarSeries : public QAbstractSeries
32 32 {
33 33 Q_OBJECT
34 34 Q_PROPERTY(qreal barWidth READ barWidth WRITE setBarWidth)
35 35 Q_PROPERTY(int count READ count NOTIFY countChanged)
36 36 Q_PROPERTY(bool labelsVisible READ isLabelsVisible WRITE setLabelsVisible NOTIFY labelsVisibleChanged)
37 37 Q_PROPERTY(QString labelsFormat READ labelsFormat WRITE setLabelsFormat NOTIFY labelsFormatChanged)
38 38 Q_PROPERTY(LabelsPosition labelsPosition READ labelsPosition WRITE setLabelsPosition NOTIFY labelsPositionChanged)
39 Q_PROPERTY(qreal labelsAngle READ labelsAngle WRITE setLabelsAngle NOTIFY labelsAngleChanged)
39 40 Q_ENUMS(LabelsPosition)
40 41
41 42 public:
42 43 enum LabelsPosition {
43 44 LabelsCenter = 0,
44 45 LabelsInsideEnd,
45 46 LabelsInsideBase,
46 47 LabelsOutsideEnd
47 48 };
48 49
49 50 public:
50 51 virtual ~QAbstractBarSeries();
51 52
52 53 void setBarWidth(qreal width);
53 54 qreal barWidth() const;
54 55
55 56 bool append(QBarSet *set);
56 57 bool remove(QBarSet *set);
57 58 bool take(QBarSet *set);
58 59 bool append(QList<QBarSet *> sets);
59 60 bool insert(int index, QBarSet *set);
60 61 int count() const;
61 62 QList<QBarSet *> barSets() const;
62 63 void clear();
63 64
64 65 void setLabelsVisible(bool visible = true);
65 66 bool isLabelsVisible() const;
66 67
67 68 void setLabelsFormat(const QString &format);
68 69 QString labelsFormat() const;
69 70
71 void setLabelsAngle(qreal angle);
72 qreal labelsAngle() const;
73
70 74 void setLabelsPosition(QAbstractBarSeries::LabelsPosition position);
71 75 QAbstractBarSeries::LabelsPosition labelsPosition() const;
72 76
73 77 protected:
74 78 explicit QAbstractBarSeries(QAbstractBarSeriesPrivate &d, QObject *parent = 0);
75 79
76 80 Q_SIGNALS:
77 81 void clicked(int index, QBarSet *barset);
78 82 void hovered(bool status, int index, QBarSet *barset);
79 83 void pressed(int index, QBarSet *barset);
80 84 void released(int index, QBarSet *barset);
81 85 void doubleClicked(int index, QBarSet *barset);
82 86 void countChanged();
83 87 void labelsVisibleChanged();
84 88 void labelsFormatChanged(const QString &format);
85 89 void labelsPositionChanged(QAbstractBarSeries::LabelsPosition position);
90 void labelsAngleChanged(qreal angle);
86 91
87 92 void barsetsAdded(QList<QBarSet *> sets);
88 93 void barsetsRemoved(QList<QBarSet *> sets);
89 94
90 95 protected:
91 96 Q_DECLARE_PRIVATE(QAbstractBarSeries)
92 97 friend class AbstractBarChartItem;
93 98 friend class PercentBarChartItem;
94 99 friend class StackedBarChartItem;
95 100 friend class BoxPlotChartItem;
96 101 friend class BarChartItem;
97 102 friend class HorizontalBarChartItem;
98 103 friend class HorizontalStackedBarChartItem;
99 104 friend class HorizontalPercentBarChartItem;
100 105 friend class BarSet;
101 106 };
102 107
103 108 QT_CHARTS_END_NAMESPACE
104 109
105 110 #endif // QABSTRACTBARSERIES_H
@@ -1,119 +1,122
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 // W A R N I N G
20 20 // -------------
21 21 //
22 22 // This file is not part of the Qt Enterprise Chart API. It exists purely as an
23 23 // implementation detail. This header file may change from version to
24 24 // version without notice, or even be removed.
25 25 //
26 26 // We mean it.
27 27
28 28 #ifndef QABSTRACTBARSERIES_P_H
29 29 #define QABSTRACTBARSERIES_P_H
30 30
31 31 #include <QtCharts/QAbstractBarSeries>
32 32 #include <private/qabstractseries_p.h>
33 33 #include <QtCore/QStringList>
34 34 #include <QtCharts/QAbstractSeries>
35 35
36 36 QT_CHARTS_BEGIN_NAMESPACE
37 37
38 38 class QBarModelMapper;
39 39 class QBarCategoryAxis;
40 40 class QLegendMarker;
41 41
42 42 class QAbstractBarSeriesPrivate : public QAbstractSeriesPrivate
43 43 {
44 44 Q_OBJECT
45 45 public:
46 46 QAbstractBarSeriesPrivate(QAbstractBarSeries *parent);
47 47 int categoryCount() const;
48 48
49 49 void setBarWidth(qreal width);
50 50 qreal barWidth() const;
51 51
52 52 void setVisible(bool visible);
53 53 void setLabelsVisible(bool visible);
54 54
55 55 void initializeDomain();
56 56 void initializeAxes();
57 57 void initializeAnimations(QChart::AnimationOptions options);
58 58 void initializeTheme(int index, ChartTheme* theme, bool forced = false);
59 59
60 60 QList<QLegendMarker*> createLegendMarkers(QLegend *legend);
61 61
62 62 virtual QAbstractAxis::AxisType defaultAxisType(Qt::Orientation orientation) const;
63 63 QAbstractAxis* createDefaultAxis(Qt::Orientation orientation) const;
64 64
65 65 bool append(QBarSet *set);
66 66 bool remove(QBarSet *set);
67 67 bool append(QList<QBarSet *> sets);
68 68 bool remove(QList<QBarSet *> sets);
69 69 bool insert(int index, QBarSet *set);
70 70
71 71 QBarSet *barsetAt(int index);
72 72 qreal min();
73 73 qreal max();
74 74 qreal valueAt(int set, int category);
75 75 qreal percentageAt(int set, int category);
76 76 qreal categorySum(int category);
77 77 qreal absoluteCategorySum(int category);
78 78 qreal maxCategorySum();
79 79 qreal minX();
80 80 qreal maxX();
81 81 qreal categoryTop(int category);
82 82 qreal categoryBottom(int category);
83 83 qreal top();
84 84 qreal bottom();
85 85
86 86 bool blockBarUpdate();
87 87
88 qreal labelsAngle() const;
89
88 90 Q_SIGNALS:
89 91 void clicked(int index, QBarSet *barset);
90 92 void pressed(int index, QBarSet *barset);
91 93 void released(int index, QBarSet *barset);
92 94 void doubleClicked(int index, QBarSet *barset);
93 95 void updatedBars();
94 96 void updatedLayout();
95 97 void restructuredBars();
96 98 void labelsVisibleChanged(bool visible);
97 99 void visibleChanged();
98 100
99 101 private:
100 102 void populateCategories(QBarCategoryAxis *axis);
101 103
102 104 protected:
103 105 QList<QBarSet *> m_barSets;
104 106 qreal m_barWidth;
105 107 bool m_labelsVisible;
106 108 bool m_visible;
107 109 bool m_blockBarUpdate;
108 110 QString m_labelsFormat;
109 111 QAbstractBarSeries::LabelsPosition m_labelsPosition;
112 qreal m_labelsAngle;
110 113
111 114 private:
112 115 Q_DECLARE_PUBLIC(QAbstractBarSeries)
113 116 friend class HorizontalBarChartItem;
114 117 friend class BarChartItem;
115 118 };
116 119
117 120 QT_CHARTS_END_NAMESPACE
118 121
119 122 #endif // QABSTRACTBARSERIES_P_H
@@ -1,123 +1,106
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include <private/barchartitem_p.h>
20 20 #include <private/bar_p.h>
21 21 #include <private/qabstractbarseries_p.h>
22 22 #include <QtCharts/QBarSet>
23 23 #include <private/qbarset_p.h>
24 24
25 25 QT_CHARTS_BEGIN_NAMESPACE
26 26
27 27 BarChartItem::BarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
28 28 AbstractBarChartItem(series, item)
29 29 {
30 30 connect(series, SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)),
31 31 this, SLOT(handleLabelsPositionChanged()));
32 32 connect(series, SIGNAL(labelsFormatChanged(QString)), this, SLOT(positionLabels()));
33 33 }
34 34
35 35 void BarChartItem::initializeLayout()
36 36 {
37 37 qreal categoryCount = m_series->d_func()->categoryCount();
38 38 qreal setCount = m_series->count();
39 39 qreal barWidth = m_series->d_func()->barWidth();
40 40
41 41 m_layout.clear();
42 42 for(int category = 0; category < categoryCount; category++) {
43 43 for (int set = 0; set < setCount; set++) {
44 44 QRectF rect;
45 45 QPointF topLeft;
46 46 QPointF bottomRight;
47 47
48 48 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) {
49 49 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + set/setCount * barWidth, domain()->minY()), m_validData);
50 50 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/setCount * barWidth, domain()->minY()), m_validData);
51 51 } else {
52 52 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + set/setCount * barWidth, 0), m_validData);
53 53 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/setCount * barWidth, 0), m_validData);
54 54 }
55 55
56 56 if (!m_validData)
57 57 return;
58 58 rect.setTopLeft(topLeft);
59 59 rect.setBottomRight(bottomRight);
60 60 m_layout.append(rect.normalized());
61 61 }
62 62 }
63 63 }
64 64
65 65 QVector<QRectF> BarChartItem::calculateLayout()
66 66 {
67 67 QVector<QRectF> layout;
68 68
69 69 // Use temporary qreals for accuracy
70 70 qreal categoryCount = m_series->d_func()->categoryCount();
71 71 qreal setCount = m_series->count();
72 72 qreal barWidth = m_series->d_func()->barWidth();
73 73
74 74 for(int category = 0; category < categoryCount; category++) {
75 75 for (int set = 0; set < setCount; set++) {
76 76 qreal value = m_series->barSets().at(set)->at(category);
77 77 QRectF rect;
78 78 QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set)/(setCount) * barWidth, value), m_validData);
79 79 QPointF bottomRight;
80 80 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
81 81 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/(setCount) * barWidth, domain()->minY()), m_validData);
82 82 else
83 83 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2 + (set + 1)/(setCount) * barWidth, 0), m_validData);
84 84
85 85 rect.setTopLeft(topLeft);
86 86 rect.setBottomRight(bottomRight);
87 87 layout.append(rect.normalized());
88 88 }
89 89 }
90 90
91 91 return layout;
92 92 }
93 93
94 94 void BarChartItem::handleLabelsPositionChanged()
95 95 {
96 96 positionLabels();
97 97 }
98 98
99 99 void BarChartItem::positionLabels()
100 100 {
101 for (int i = 0; i < m_layout.count(); i++) {
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 }
101 positionLabelsVertical();
119 102 }
120 103
121 104 #include "moc_barchartitem_p.cpp"
122 105
123 106 QT_CHARTS_END_NAMESPACE
@@ -1,169 +1,152
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include <private/percentbarchartitem_p.h>
20 20 #include <private/bar_p.h>
21 21 #include <private/qabstractbarseries_p.h>
22 22 #include <QtCharts/QBarSet>
23 23 #include <private/qbarset_p.h>
24 24
25 25 QT_CHARTS_BEGIN_NAMESPACE
26 26
27 27 PercentBarChartItem::PercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
28 28 AbstractBarChartItem(series, item)
29 29 {
30 30 connect(series, SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)),
31 31 this, SLOT(handleLabelsPositionChanged()));
32 32 connect(series, SIGNAL(labelsFormatChanged(QString)), this, SLOT(positionLabels()));
33 33 }
34 34
35 35 void PercentBarChartItem::initializeLayout()
36 36 {
37 37 qreal categoryCount = m_series->d_func()->categoryCount();
38 38 qreal setCount = m_series->count();
39 39 qreal barWidth = m_series->d_func()->barWidth();
40 40
41 41 m_layout.clear();
42 42 for(int category = 0; category < categoryCount; category++) {
43 43 for (int set = 0; set < setCount; set++) {
44 44 QRectF rect;
45 45 QPointF topLeft;
46 46 QPointF bottomRight;
47 47
48 48 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) {
49 49 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, domain()->minY()), m_validData);
50 50 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, domain()->minY()), m_validData);
51 51 } else {
52 52 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, 0), m_validData);
53 53 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, 0), m_validData);
54 54 }
55 55
56 56 if (!m_validData)
57 57 return;
58 58
59 59 rect.setTopLeft(topLeft);
60 60 rect.setBottomRight(bottomRight);
61 61 m_layout.append(rect.normalized());
62 62 }
63 63 }
64 64 }
65 65
66 66 QVector<QRectF> PercentBarChartItem::calculateLayout()
67 67 {
68 68 QVector<QRectF> layout;
69 69
70 70 // Use temporary qreals for accuracy
71 71 qreal categoryCount = m_series->d_func()->categoryCount();
72 72 qreal setCount = m_series->count();
73 73 qreal barWidth = m_series->d_func()->barWidth();
74 74
75 75 for(int category = 0; category < categoryCount; category++) {
76 76 qreal sum = 0;
77 77 qreal categorySum = m_series->d_func()->categorySum(category);
78 78 for (int set = 0; set < setCount; set++) {
79 79 qreal value = m_series->barSets().at(set)->at(category);
80 80 QRectF rect;
81 81 qreal topY = 0;
82 82 qreal newSum = value + sum;
83 83 if (newSum > 0)
84 84 topY = 100 * newSum / categorySum;
85 85 qreal bottomY = 0;
86 86 if (sum > 0)
87 87 bottomY = 100 * sum / categorySum;
88 88 QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth/2, topY), m_validData);
89 89 QPointF bottomRight;
90 90 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
91 91 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? bottomY : domain()->minY()), m_validData);
92 92 else
93 93 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? bottomY : 0), m_validData);
94 94
95 95 rect.setTopLeft(topLeft);
96 96 rect.setBottomRight(bottomRight);
97 97 layout.append(rect.normalized());
98 98 sum = newSum;
99 99 }
100 100 }
101 101 return layout;
102 102 }
103 103
104 104 void PercentBarChartItem::handleUpdatedBars()
105 105 {
106 106 // Handle changes in pen, brush, labels etc.
107 107 int categoryCount = m_series->d_func()->categoryCount();
108 108 int setCount = m_series->count();
109 109 int itemIndex(0);
110 110 static const QString valueTag(QLatin1String("@value"));
111 111
112 112 for (int category = 0; category < categoryCount; category++) {
113 113 for (int set = 0; set < setCount; set++) {
114 114 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
115 115 Bar *bar = m_bars.at(itemIndex);
116 116 bar->setPen(barSet->m_pen);
117 117 bar->setBrush(barSet->m_brush);
118 118 bar->update();
119 119
120 120 QGraphicsTextItem *label = m_labels.at(itemIndex);
121 121 qreal p = m_series->d_func()->percentageAt(set, category) * 100.0;
122 122 QString vString(presenter()->numberToString(p, 'f', 0));
123 123 QString valueLabel;
124 124 if (m_series->labelsFormat().isEmpty()) {
125 125 vString.append(QStringLiteral("%"));
126 126 valueLabel = vString;
127 127 } else {
128 128 valueLabel = m_series->labelsFormat();
129 129 valueLabel.replace(valueTag, vString);
130 130 }
131 131 label->setHtml(valueLabel);
132 132 label->setFont(barSet->m_labelFont);
133 133 label->setDefaultTextColor(barSet->m_labelBrush.color());
134 134 label->update();
135 135 itemIndex++;
136 136 }
137 137 }
138 138 }
139 139
140 140 void PercentBarChartItem::handleLabelsPositionChanged()
141 141 {
142 142 positionLabels();
143 143 }
144 144
145 145 void PercentBarChartItem::positionLabels()
146 146 {
147 for (int i = 0; i < m_layout.count(); i++) {
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 }
147 positionLabelsVertical();
165 148 }
166 149
167 150 #include "moc_percentbarchartitem_p.cpp"
168 151
169 152 QT_CHARTS_END_NAMESPACE
@@ -1,135 +1,118
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include <private/stackedbarchartitem_p.h>
20 20 #include <private/bar_p.h>
21 21 #include <private/qbarset_p.h>
22 22 #include <private/qabstractbarseries_p.h>
23 23 #include <QtCharts/QBarSet>
24 24
25 25 QT_CHARTS_BEGIN_NAMESPACE
26 26
27 27 StackedBarChartItem::StackedBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
28 28 AbstractBarChartItem(series, item)
29 29 {
30 30 connect(series, SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)),
31 31 this, SLOT(handleLabelsPositionChanged()));
32 32 connect(series, SIGNAL(labelsFormatChanged(QString)), this, SLOT(positionLabels()));
33 33 }
34 34
35 35 void StackedBarChartItem::initializeLayout()
36 36 {
37 37 qreal categoryCount = m_series->d_func()->categoryCount();
38 38 qreal setCount = m_series->count();
39 39 qreal barWidth = m_series->d_func()->barWidth();
40 40
41 41 m_layout.clear();
42 42 for(int category = 0; category < categoryCount; category++) {
43 43 for (int set = 0; set < setCount; set++) {
44 44 QRectF rect;
45 45 QPointF topLeft;
46 46 QPointF bottomRight;
47 47
48 48 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) {
49 49 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, domain()->minY()), m_validData);
50 50 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, domain()->minY()), m_validData);
51 51 } else {
52 52 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, 0), m_validData);
53 53 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, 0), m_validData);
54 54 }
55 55
56 56 if (!m_validData)
57 57 return;
58 58
59 59 rect.setTopLeft(topLeft);
60 60 rect.setBottomRight(bottomRight);
61 61 m_layout.append(rect.normalized());
62 62 }
63 63 }
64 64 }
65 65
66 66 QVector<QRectF> StackedBarChartItem::calculateLayout()
67 67 {
68 68 QVector<QRectF> layout;
69 69 // Use temporary qreals for accuracy
70 70 qreal categoryCount = m_series->d_func()->categoryCount();
71 71 qreal setCount = m_series->count();
72 72 qreal barWidth = m_series->d_func()->barWidth();
73 73
74 74 for(int category = 0; category < categoryCount; category++) {
75 75 qreal positiveSum = 0;
76 76 qreal negativeSum = 0;
77 77 for (int set = 0; set < setCount; set++) {
78 78 qreal value = m_series->barSets().at(set)->at(category);
79 79 QRectF rect;
80 80 QPointF topLeft;
81 81 QPointF bottomRight;
82 82 if (value < 0) {
83 83 bottomRight = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + negativeSum), m_validData);
84 84 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
85 85 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : domain()->minY()), m_validData);
86 86 else
87 87 topLeft = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? negativeSum : 0), m_validData);
88 88 negativeSum += value;
89 89 } else {
90 90 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, value + positiveSum), m_validData);
91 91 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
92 92 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : domain()->minY()), m_validData);
93 93 else
94 94 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, set ? positiveSum : 0), m_validData);
95 95 positiveSum += value;
96 96 }
97 97
98 98 rect.setTopLeft(topLeft);
99 99 rect.setBottomRight(bottomRight);
100 100 layout.append(rect.normalized());
101 101 }
102 102 }
103 103 return layout;
104 104 }
105 105
106 106 void StackedBarChartItem::handleLabelsPositionChanged()
107 107 {
108 108 positionLabels();
109 109 }
110 110
111 111 void StackedBarChartItem::positionLabels()
112 112 {
113 for (int i = 0; i < m_layout.count(); i++) {
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 }
113 positionLabelsVertical();
131 114 }
132 115
133 116 #include "moc_stackedbarchartitem_p.cpp"
134 117
135 118 QT_CHARTS_END_NAMESPACE
@@ -1,1337 +1,1351
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include <QtTest/QtTest>
20 20 #include <QtCharts/QBarSeries>
21 21 #include <QtCharts/QBarSet>
22 22 #include <QtCharts/QChartView>
23 23 #include <QtCharts/QChart>
24 24 #include "tst_definitions.h"
25 25
26 26 QT_CHARTS_USE_NAMESPACE
27 27
28 28 Q_DECLARE_METATYPE(QBarSet*)
29 29 Q_DECLARE_METATYPE(QList<QBarSet*>)
30 30 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
31 31
32 32 class tst_QBarSeries : public QObject
33 33 {
34 34 Q_OBJECT
35 35
36 36 public slots:
37 37 void initTestCase();
38 38 void cleanupTestCase();
39 39 void init();
40 40 void cleanup();
41 41
42 42 private slots:
43 43 void qbarseries_data();
44 44 void qbarseries();
45 45 void type_data();
46 46 void type();
47 47 void append_data();
48 48 void append();
49 49 void remove_data();
50 50 void remove();
51 51 void take_data();
52 52 void take();
53 53 void appendList_data();
54 54 void appendList();
55 55 void count_data();
56 56 void count();
57 57 void barSets_data();
58 58 void barSets();
59 59 void setLabelsVisible_data();
60 60 void setLabelsVisible();
61 61 void setLabelsFormat();
62 62 void setLabelsPosition();
63 void setLabelsAngle();
63 64 void opacity();
64 65 void mouseclicked_data();
65 66 void mouseclicked();
66 67 void mousehovered_data();
67 68 void mousehovered();
68 69 void clearWithAnimations();
69 70 void destruction();
70 71 void mousePressed();
71 72 void mouseReleased();
72 73 void mouseDoubleClicked();
73 74
74 75 private:
75 76 QBarSeries* m_barseries;
76 77 QBarSeries* m_barseries_with_sets;
77 78
78 79 QList<QBarSet*> m_testSets;
79 80
80 81 };
81 82
82 83 void tst_QBarSeries::initTestCase()
83 84 {
84 85 qRegisterMetaType<QBarSet*>("QBarSet*");
85 86 qRegisterMetaType<QList<QBarSet*> >("QList<QBarSet*>");
86 87 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
87 88 }
88 89
89 90 void tst_QBarSeries::cleanupTestCase()
90 91 {
91 92 QTest::qWait(1); // Allow final deleteLaters to run
92 93 }
93 94
94 95 void tst_QBarSeries::init()
95 96 {
96 97 m_barseries = new QBarSeries();
97 98 m_barseries_with_sets = new QBarSeries();
98 99
99 100 for (int i=0; i<5; i++) {
100 101 m_testSets.append(new QBarSet("testset"));
101 102 m_barseries_with_sets->append(m_testSets.at(i));
102 103 }
103 104 }
104 105
105 106 void tst_QBarSeries::cleanup()
106 107 {
107 108 foreach (QBarSet* s, m_testSets) {
108 109 m_barseries_with_sets->remove(s);
109 110 }
110 111 m_testSets.clear();
111 112
112 113 delete m_barseries;
113 114 m_barseries = 0;
114 115 delete m_barseries_with_sets;
115 116 m_barseries_with_sets = 0;
116 117 }
117 118
118 119 void tst_QBarSeries::qbarseries_data()
119 120 {
120 121 }
121 122
122 123 void tst_QBarSeries::qbarseries()
123 124 {
124 125 QBarSeries *barseries = new QBarSeries();
125 126 QVERIFY(barseries != 0);
126 127 delete barseries;
127 128 }
128 129
129 130 void tst_QBarSeries::type_data()
130 131 {
131 132
132 133 }
133 134
134 135 void tst_QBarSeries::type()
135 136 {
136 137 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeBar);
137 138 }
138 139
139 140 void tst_QBarSeries::append_data()
140 141 {
141 142 }
142 143
143 144 void tst_QBarSeries::append()
144 145 {
145 146 QVERIFY(m_barseries->count() == 0);
146 147
147 148 bool ret = false;
148 149
149 150 // Try adding barset
150 151 QBarSet *barset = new QBarSet("testset");
151 152 ret = m_barseries->append(barset);
152 153
153 154 QVERIFY(ret == true);
154 155 QVERIFY(m_barseries->count() == 1);
155 156
156 157 // Try adding another set
157 158 QBarSet *barset2 = new QBarSet("testset2");
158 159 ret = m_barseries->append(barset2);
159 160
160 161 QVERIFY(ret == true);
161 162 QVERIFY(m_barseries->count() == 2);
162 163
163 164 // Try adding same set again
164 165 ret = m_barseries->append(barset2);
165 166 QVERIFY(ret == false);
166 167 QVERIFY(m_barseries->count() == 2);
167 168
168 169 // Try adding null set
169 170 ret = m_barseries->append(0);
170 171 QVERIFY(ret == false);
171 172 QVERIFY(m_barseries->count() == 2);
172 173
173 174 }
174 175
175 176 void tst_QBarSeries::remove_data()
176 177 {
177 178 }
178 179
179 180 void tst_QBarSeries::remove()
180 181 {
181 182 int count = m_testSets.count();
182 183 QVERIFY(m_barseries_with_sets->count() == count);
183 184
184 185 // Try to remove null pointer (should not remove, should not crash)
185 186 bool ret = false;
186 187 ret = m_barseries_with_sets->remove(0);
187 188 QVERIFY(ret == false);
188 189 QVERIFY(m_barseries_with_sets->count() == count);
189 190
190 191 // Try to remove invalid pointer (should not remove, should not crash)
191 192 ret = m_barseries_with_sets->remove((QBarSet*) (m_testSets.at(0) + 1) );
192 193 QVERIFY(ret == false);
193 194 QVERIFY(m_barseries_with_sets->count() == count);
194 195
195 196 // remove some sets
196 197 ret = m_barseries_with_sets->remove(m_testSets.at(2));
197 198 QVERIFY(ret == true);
198 199 ret = m_barseries_with_sets->remove(m_testSets.at(3));
199 200 QVERIFY(ret == true);
200 201 ret = m_barseries_with_sets->remove(m_testSets.at(4));
201 202 QVERIFY(ret == true);
202 203
203 204 QVERIFY(m_barseries_with_sets->count() == 2);
204 205
205 206 QList<QBarSet*> verifysets = m_barseries_with_sets->barSets();
206 207
207 208 QVERIFY(verifysets.at(0) == m_testSets.at(0));
208 209 QVERIFY(verifysets.at(1) == m_testSets.at(1));
209 210
210 211 // Try removing all sets again (should be ok, even if some sets have already been removed)
211 212 ret = false;
212 213 for (int i=0; i<count; i++) {
213 214 ret |= m_barseries_with_sets->remove(m_testSets.at(i));
214 215 }
215 216
216 217 QVERIFY(ret == true);
217 218 QVERIFY(m_barseries_with_sets->count() == 0);
218 219 }
219 220
220 221 void tst_QBarSeries::take_data()
221 222 {
222 223
223 224 }
224 225
225 226 void tst_QBarSeries::take()
226 227 {
227 228 int count = m_testSets.count();
228 229 QVERIFY(m_barseries_with_sets->count() == count);
229 230
230 231 QSignalSpy countSpy(m_barseries_with_sets,SIGNAL(countChanged()));
231 232 QSignalSpy removedSpy(m_barseries_with_sets,SIGNAL(barsetsRemoved(QList<QBarSet*>)));
232 233
233 234 for (int i=0; i<m_testSets.count(); i++) {
234 235 QBarSet* set = m_testSets.at(i);
235 236 bool success = m_barseries_with_sets->take(set);
236 237 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
237 238 QVERIFY(success);
238 239 TRY_COMPARE(countSpy.count(),1);
239 240 TRY_COMPARE(removedSpy.count(),1);
240 241
241 242 QList<QVariant> removedSpyArg = removedSpy.takeFirst();
242 243 QList<QBarSet*> removedSets = qvariant_cast<QList<QBarSet*> > (removedSpyArg.at(0));
243 244 QCOMPARE(removedSets.at(0), m_testSets.at(i));
244 245 countSpy.takeFirst();
245 246 }
246 247 }
247 248
248 249
249 250 void tst_QBarSeries::appendList_data()
250 251 {
251 252
252 253 }
253 254
254 255 void tst_QBarSeries::appendList()
255 256 {
256 257 int count = 5;
257 258 QVERIFY(m_barseries->count() == 0);
258 259
259 260 QList<QBarSet*> sets;
260 261 for (int i=0; i<count; i++) {
261 262 sets.append(new QBarSet("testset"));
262 263 }
263 264
264 265 // Append new sets (should succeed, count should match the count of sets)
265 266 bool ret = false;
266 267 ret = m_barseries->append(sets);
267 268 QVERIFY(ret == true);
268 269 QVERIFY(m_barseries->count() == count);
269 270
270 271 // Append same sets again (should fail, count should remain same)
271 272 ret = m_barseries->append(sets);
272 273 QVERIFY(ret == false);
273 274 QVERIFY(m_barseries->count() == count);
274 275
275 276 // Try append empty list (should succeed, but count should remain same)
276 277 QList<QBarSet*> invalidList;
277 278 ret = m_barseries->append(invalidList);
278 279 QVERIFY(ret == true);
279 280 QVERIFY(m_barseries->count() == count);
280 281
281 282 // Try append list with one new and one existing set (should fail, count remains same)
282 283 invalidList.append(new QBarSet("ok set"));
283 284 invalidList.append(sets.at(0));
284 285 ret = m_barseries->append(invalidList);
285 286 QVERIFY(ret == false);
286 287 QVERIFY(m_barseries->count() == count);
287 288 delete invalidList.at(0);
288 289
289 290 // Try append list with null pointers (should fail, count remains same)
290 291 QList<QBarSet*> invalidList2;
291 292 invalidList2.append(0);
292 293 invalidList2.append(0);
293 294 invalidList2.append(0);
294 295 ret = m_barseries->append(invalidList2);
295 296 QVERIFY(ret == false);
296 297 QVERIFY(m_barseries->count() == count);
297 298 }
298 299
299 300 void tst_QBarSeries::count_data()
300 301 {
301 302
302 303 }
303 304
304 305 void tst_QBarSeries::count()
305 306 {
306 307 QVERIFY(m_barseries->count() == 0);
307 308 QVERIFY(m_barseries_with_sets->count() == m_testSets.count());
308 309 }
309 310
310 311 void tst_QBarSeries::barSets_data()
311 312 {
312 313
313 314 }
314 315
315 316 void tst_QBarSeries::barSets()
316 317 {
317 318 QVERIFY(m_barseries->barSets().count() == 0);
318 319
319 320 QList<QBarSet*> sets = m_barseries_with_sets->barSets();
320 321 QVERIFY(sets.count() == m_testSets.count());
321 322
322 323 for (int i=0; i<m_testSets.count(); i++) {
323 324 QVERIFY(sets.at(i) == m_testSets.at(i));
324 325 }
325 326 }
326 327
327 328 void tst_QBarSeries::setLabelsVisible_data()
328 329 {
329 330
330 331 }
331 332
332 333 void tst_QBarSeries::setLabelsVisible()
333 334 {
334 335 // labels should be invisible by default
335 336 QVERIFY(m_barseries->isLabelsVisible() == false);
336 337 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
337 338
338 339 // turn labels to visible
339 340 m_barseries_with_sets->setLabelsVisible(true);
340 341 // TODO: test the signal
341 342 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
342 343
343 344 // turn labels to invisible
344 345 m_barseries_with_sets->setLabelsVisible(false);
345 346 // TODO: test the signal
346 347 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
347 348
348 349 // without parameter, should turn labels to visible
349 350 m_barseries_with_sets->setLabelsVisible();
350 351 // TODO: test the signal
351 352 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
352 353 }
353 354
354 355 void tst_QBarSeries::setLabelsFormat()
355 356 {
356 357 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
357 358 QCOMPARE(m_barseries->labelsFormat(), QString());
358 359
359 360 QString format("(@value)");
360 361 m_barseries->setLabelsFormat(format);
361 362 TRY_COMPARE(labelsFormatSpy.count(), 1);
362 363 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
363 364 QVERIFY(arguments.at(0).toString() == format);
364 365 QCOMPARE(m_barseries->labelsFormat(), format);
365 366
366 367 m_barseries->setLabelsFormat(QString());
367 368 TRY_COMPARE(labelsFormatSpy.count(), 1);
368 369 arguments = labelsFormatSpy.takeFirst();
369 370 QVERIFY(arguments.at(0).toString() == QString());
370 371 QCOMPARE(m_barseries->labelsFormat(), QString());
371 372 }
372 373
373 374 void tst_QBarSeries::setLabelsPosition()
374 375 {
375 376 QSignalSpy labelsPositionSpy(m_barseries,
376 377 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
377 378 QCOMPARE(m_barseries->labelsPosition(), QBarSeries::LabelsCenter);
378 379
379 380 m_barseries->setLabelsPosition(QBarSeries::LabelsInsideEnd);
380 381 TRY_COMPARE(labelsPositionSpy.count(), 1);
381 382 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
382 383 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
383 384 == QBarSeries::LabelsInsideEnd);
384 385 QCOMPARE(m_barseries->labelsPosition(), QBarSeries::LabelsInsideEnd);
385 386
386 387 m_barseries->setLabelsPosition(QBarSeries::LabelsInsideBase);
387 388 TRY_COMPARE(labelsPositionSpy.count(), 1);
388 389 arguments = labelsPositionSpy.takeFirst();
389 390 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
390 391 == QBarSeries::LabelsInsideBase);
391 392 QCOMPARE(m_barseries->labelsPosition(), QBarSeries::LabelsInsideBase);
392 393
393 394 m_barseries->setLabelsPosition(QBarSeries::LabelsOutsideEnd);
394 395 TRY_COMPARE(labelsPositionSpy.count(), 1);
395 396 arguments = labelsPositionSpy.takeFirst();
396 397 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
397 398 == QBarSeries::LabelsOutsideEnd);
398 399 QCOMPARE(m_barseries->labelsPosition(), QBarSeries::LabelsOutsideEnd);
399 400
400 401 m_barseries->setLabelsPosition(QBarSeries::LabelsCenter);
401 402 TRY_COMPARE(labelsPositionSpy.count(), 1);
402 403 arguments = labelsPositionSpy.takeFirst();
403 404 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
404 405 == QBarSeries::LabelsCenter);
405 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 422 void tst_QBarSeries::opacity()
409 423 {
410 424 QSignalSpy opacitySpy(m_barseries, SIGNAL(opacityChanged()));
411 425
412 426 QCOMPARE(m_barseries->opacity(), 1.0);
413 427
414 428 m_barseries->setOpacity(0.5);
415 429 QCOMPARE(m_barseries->opacity(), 0.5);
416 430 QCOMPARE(opacitySpy.count(), 1);
417 431
418 432 m_barseries->setOpacity(0.0);
419 433 QCOMPARE(m_barseries->opacity(), 0.0);
420 434 QCOMPARE(opacitySpy.count(), 2);
421 435
422 436 m_barseries->setOpacity(1.0);
423 437 QCOMPARE(m_barseries->opacity(), 1.0);
424 438 QCOMPARE(opacitySpy.count(), 3);
425 439 }
426 440
427 441 void tst_QBarSeries::mouseclicked_data()
428 442 {
429 443
430 444 }
431 445
432 446 void tst_QBarSeries::mouseclicked()
433 447 {
434 448 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
435 449
436 450 QBarSeries* series = new QBarSeries();
437 451
438 452 QBarSet* set1 = new QBarSet(QString("set 1"));
439 453 *set1 << 10 << 10 << 10;
440 454 series->append(set1);
441 455
442 456 QBarSet* set2 = new QBarSet(QString("set 2"));
443 457 *set2 << 10 << 10 << 10;
444 458 series->append(set2);
445 459 QList<QBarSet*> barSets = series->barSets();
446 460
447 461 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
448 462 QSignalSpy setSpy1(set1, SIGNAL(clicked(int)));
449 463 QSignalSpy setSpy2(set2, SIGNAL(clicked(int)));
450 464
451 465 QChartView view(new QChart());
452 466 view.resize(400,300);
453 467 view.chart()->addSeries(series);
454 468 view.show();
455 469 QTest::qWaitForWindowShown(&view);
456 470
457 471 // Calculate expected layout for bars
458 472 QRectF plotArea = view.chart()->plotArea();
459 473 qreal width = plotArea.width();
460 474 qreal height = plotArea.height();
461 475 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
462 476 qreal rangeX = 3; // 3 values per set
463 477 qreal scaleY = (height / rangeY);
464 478 qreal scaleX = (width / rangeX);
465 479
466 480 qreal setCount = series->count();
467 481 qreal domainMinY = 0; // These come from internal domain used by barseries.
468 482 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
469 483 qreal rectWidth = (scaleX / setCount) * series->barWidth();
470 484
471 485 QVector<QRectF> layout;
472 486
473 487 // 3 = count of values in set
474 488 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
475 489 for (int i = 0; i < 3; i++) {
476 490 qreal yPos = height + scaleY * domainMinY + plotArea.top();
477 491 for (int set = 0; set < setCount; set++) {
478 492 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
479 493 xPos -= series->count()*rectWidth/2;
480 494 xPos += set*rectWidth;
481 495
482 496 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
483 497 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
484 498 layout.append(rect);
485 499 }
486 500 }
487 501
488 502 //====================================================================================
489 503 // barset 1, bar 0
490 504 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
491 505 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
492 506
493 507 QCOMPARE(seriesSpy.count(), 1);
494 508 QCOMPARE(setSpy1.count(), 1);
495 509 QCOMPARE(setSpy2.count(), 0);
496 510
497 511 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
498 512 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
499 513 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
500 514 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
501 515
502 516 QList<QVariant> setSpyArg = setSpy1.takeFirst();
503 517 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
504 518 QVERIFY(setSpyArg.at(0).toInt() == 0);
505 519
506 520 //====================================================================================
507 521 // barset 1, bar 1
508 522 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
509 523 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
510 524
511 525 QCOMPARE(seriesSpy.count(), 1);
512 526 QCOMPARE(setSpy1.count(), 1);
513 527 QCOMPARE(setSpy2.count(), 0);
514 528
515 529 seriesSpyArg = seriesSpy.takeFirst();
516 530 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
517 531 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
518 532 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
519 533
520 534 setSpyArg = setSpy1.takeFirst();
521 535 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
522 536 QVERIFY(setSpyArg.at(0).toInt() == 1);
523 537
524 538 //====================================================================================
525 539 // barset 1, bar 2
526 540 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
527 541 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
528 542
529 543 QCOMPARE(seriesSpy.count(), 1);
530 544 QCOMPARE(setSpy1.count(), 1);
531 545 QCOMPARE(setSpy2.count(), 0);
532 546
533 547 seriesSpyArg = seriesSpy.takeFirst();
534 548 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
535 549 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
536 550 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
537 551
538 552 setSpyArg = setSpy1.takeFirst();
539 553 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
540 554 QVERIFY(setSpyArg.at(0).toInt() == 2);
541 555
542 556 //====================================================================================
543 557 // barset 2, bar 0
544 558 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
545 559 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
546 560
547 561 QCOMPARE(seriesSpy.count(), 1);
548 562 QCOMPARE(setSpy1.count(), 0);
549 563 QCOMPARE(setSpy2.count(), 1);
550 564
551 565 seriesSpyArg = seriesSpy.takeFirst();
552 566 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
553 567 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
554 568 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
555 569
556 570 setSpyArg = setSpy2.takeFirst();
557 571 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
558 572 QVERIFY(setSpyArg.at(0).toInt() == 0);
559 573
560 574 //====================================================================================
561 575 // barset 2, bar 1
562 576 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
563 577 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
564 578
565 579 QCOMPARE(seriesSpy.count(), 1);
566 580 QCOMPARE(setSpy1.count(), 0);
567 581 QCOMPARE(setSpy2.count(), 1);
568 582
569 583 seriesSpyArg = seriesSpy.takeFirst();
570 584 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
571 585 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
572 586 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
573 587
574 588 setSpyArg = setSpy2.takeFirst();
575 589 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
576 590 QVERIFY(setSpyArg.at(0).toInt() == 1);
577 591
578 592 //====================================================================================
579 593 // barset 2, bar 2
580 594 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
581 595 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
582 596
583 597 QCOMPARE(seriesSpy.count(), 1);
584 598 QCOMPARE(setSpy1.count(), 0);
585 599 QCOMPARE(setSpy2.count(), 1);
586 600
587 601 seriesSpyArg = seriesSpy.takeFirst();
588 602 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
589 603 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
590 604 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
591 605
592 606 setSpyArg = setSpy2.takeFirst();
593 607 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
594 608 QVERIFY(setSpyArg.at(0).toInt() == 2);
595 609 }
596 610
597 611 void tst_QBarSeries::mousehovered_data()
598 612 {
599 613
600 614 }
601 615
602 616 void tst_QBarSeries::mousehovered()
603 617 {
604 618 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
605 619
606 620 QBarSeries* series = new QBarSeries();
607 621
608 622 QBarSet* set1 = new QBarSet(QString("set 1"));
609 623 *set1 << 10 << 10 << 10;
610 624 series->append(set1);
611 625
612 626 QBarSet* set2 = new QBarSet(QString("set 2"));
613 627 *set2 << 10 << 10 << 10;
614 628 series->append(set2);
615 629 QList<QBarSet*> barSets = series->barSets();
616 630
617 631 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
618 632 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
619 633 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
620 634
621 635 QChartView view(new QChart());
622 636 view.resize(400,300);
623 637 view.chart()->addSeries(series);
624 638 view.show();
625 639 QTest::qWaitForWindowShown(&view);
626 640
627 641 //this is hack since view does not get events otherwise
628 642 view.setMouseTracking(true);
629 643
630 644 // Calculate expected layout for bars
631 645 QRectF plotArea = view.chart()->plotArea();
632 646 qreal width = plotArea.width();
633 647 qreal height = plotArea.height();
634 648 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
635 649 qreal rangeX = 3; // 3 values per set
636 650 qreal scaleY = (height / rangeY);
637 651 qreal scaleX = (width / rangeX);
638 652
639 653 qreal setCount = series->count();
640 654 qreal domainMinY = 0; // These come from internal domain used by barseries.
641 655 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
642 656 qreal rectWidth = (scaleX / setCount) * series->barWidth();
643 657
644 658 QVector<QRectF> layout;
645 659
646 660 // 3 = count of values in set
647 661 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
648 662 for (int i = 0; i < 3; i++) {
649 663 qreal yPos = height + scaleY * domainMinY + plotArea.top();
650 664 for (int set = 0; set < setCount; set++) {
651 665 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
652 666 xPos -= series->count()*rectWidth/2;
653 667 xPos += set*rectWidth;
654 668
655 669 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
656 670 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
657 671 layout.append(rect);
658 672 }
659 673 }
660 674
661 675 //=======================================================================
662 676 // move mouse to left border
663 677 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(0).center().y()));
664 678 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
665 679 TRY_COMPARE(seriesIndexSpy.count(), 0);
666 680 TRY_COMPARE(setIndexSpy1.count(), 0);
667 681 TRY_COMPARE(setIndexSpy2.count(), 0);
668 682
669 683 //=======================================================================
670 684 // move mouse on top of set1
671 685 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
672 686 TRY_COMPARE(seriesIndexSpy.count(), 1);
673 687 TRY_COMPARE(setIndexSpy1.count(), 1);
674 688 TRY_COMPARE(setIndexSpy2.count(), 0);
675 689
676 690 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
677 691 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
678 692 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
679 693 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
680 694
681 695 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
682 696 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
683 697 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
684 698
685 699 //=======================================================================
686 700 // move mouse from top of set1 to top of set2
687 701 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
688 702 TRY_COMPARE(seriesIndexSpy.count(), 2);
689 703 TRY_COMPARE(setIndexSpy1.count(), 1);
690 704 TRY_COMPARE(setIndexSpy2.count(), 1);
691 705
692 706 // should leave set1
693 707 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
694 708 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
695 709 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
696 710 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
697 711
698 712 setIndexSpyArg = setIndexSpy1.takeFirst();
699 713 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
700 714 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
701 715
702 716 // should enter set2
703 717 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
704 718 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
705 719 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
706 720 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
707 721
708 722 setIndexSpyArg = setIndexSpy2.takeFirst();
709 723 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
710 724 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
711 725
712 726 //=======================================================================
713 727 // move mouse from top of set2 to background
714 728 QTest::mouseMove(view.viewport(), QPoint(layout.at(1).center().x(), 0));
715 729 TRY_COMPARE(seriesIndexSpy.count(), 1);
716 730 TRY_COMPARE(setIndexSpy1.count(), 0);
717 731 TRY_COMPARE(setIndexSpy2.count(), 1);
718 732
719 733 // should leave set2
720 734 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
721 735 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
722 736 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
723 737 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
724 738
725 739 setIndexSpyArg = setIndexSpy2.takeFirst();
726 740 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
727 741 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
728 742
729 743 //=======================================================================
730 744 // move mouse on top of set1, bar0 to check the index (hover into set1)
731 745 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
732 746
733 747 TRY_COMPARE(seriesIndexSpy.count(), 1);
734 748 TRY_COMPARE(setIndexSpy1.count(), 1);
735 749 TRY_COMPARE(setIndexSpy2.count(), 0);
736 750
737 751 //should enter set1, bar0
738 752 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
739 753 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
740 754 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
741 755 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
742 756 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
743 757 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
744 758
745 759 setIndexSpyArg = setIndexSpy1.takeFirst();
746 760 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
747 761 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
748 762 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
749 763 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
750 764
751 765 //=======================================================================
752 766 // move mouse on top of set2, bar0 to check the index (hover out set1,
753 767 // hover in set1)
754 768 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
755 769
756 770 TRY_COMPARE(seriesIndexSpy.count(), 2);
757 771 TRY_COMPARE(setIndexSpy1.count(), 1);
758 772 TRY_COMPARE(setIndexSpy2.count(), 1);
759 773
760 774 // should leave set1, bar0
761 775 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
762 776 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
763 777 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
764 778 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
765 779 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
766 780 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
767 781
768 782 setIndexSpyArg = setIndexSpy1.takeFirst();
769 783 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
770 784 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
771 785 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
772 786 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
773 787
774 788 // should enter set2, bar0
775 789 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
776 790 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
777 791 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
778 792 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
779 793 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
780 794 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
781 795
782 796 setIndexSpyArg = setIndexSpy2.takeFirst();
783 797 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
784 798 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
785 799 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
786 800 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
787 801
788 802 //=======================================================================
789 803 // move mouse on top of set1, bar1 to check the index (hover out set 2,
790 804 // hover in set1)
791 805 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
792 806
793 807 TRY_COMPARE(seriesIndexSpy.count(), 2);
794 808 TRY_COMPARE(setIndexSpy1.count(), 1);
795 809 TRY_COMPARE(setIndexSpy2.count(), 1);
796 810
797 811 // should leave set2, bar0
798 812 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
799 813 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
800 814 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
801 815 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
802 816 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
803 817 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
804 818
805 819 setIndexSpyArg = setIndexSpy2.takeFirst();
806 820 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
807 821 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
808 822 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
809 823 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
810 824
811 825 // should enter set1, bar1
812 826 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
813 827 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
814 828 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
815 829 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
816 830 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
817 831 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
818 832
819 833 setIndexSpyArg = setIndexSpy1.takeFirst();
820 834 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
821 835 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
822 836 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
823 837 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
824 838
825 839 //=======================================================================
826 840 // move mouse on top of view between set1 and set2 to check the index
827 841 // (hover out set1)
828 842 QTest::mouseMove(view.viewport(), QPoint((layout.at(2).right() + layout.at(3).left()) / 2, 0));
829 843
830 844 TRY_COMPARE(seriesIndexSpy.count(), 1);
831 845 TRY_COMPARE(setIndexSpy1.count(), 1);
832 846 TRY_COMPARE(setIndexSpy2.count(), 0);
833 847
834 848 // should leave set1, bar1
835 849 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
836 850 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
837 851 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
838 852 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
839 853 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
840 854 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
841 855
842 856 setIndexSpyArg = setIndexSpy1.takeFirst();
843 857 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
844 858 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
845 859 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
846 860 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
847 861
848 862 //=======================================================================
849 863 // move mouse on top of set2, bar1 to check the index (hover in set2)
850 864 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
851 865
852 866 TRY_COMPARE(seriesIndexSpy.count(), 1);
853 867 TRY_COMPARE(setIndexSpy1.count(), 0);
854 868 TRY_COMPARE(setIndexSpy2.count(), 1);
855 869
856 870 // should enter set2, bar1
857 871 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
858 872 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
859 873 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
860 874 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
861 875 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
862 876 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
863 877
864 878 setIndexSpyArg = setIndexSpy2.takeFirst();
865 879 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
866 880 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
867 881 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
868 882 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
869 883
870 884 //=======================================================================
871 885 // move mouse on top of the view between bar1 and bar2 to check the index
872 886 //(hover out set2)
873 887 QTest::mouseMove(view.viewport(), QPoint((layout.at(3).right() + layout.at(3).left()) / 2, 0));
874 888
875 889 TRY_COMPARE(seriesIndexSpy.count(), 1);
876 890 TRY_COMPARE(setIndexSpy1.count(), 0);
877 891 TRY_COMPARE(setIndexSpy2.count(), 1);
878 892
879 893 // should leave set2, bar1
880 894 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
881 895 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
882 896 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
883 897 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
884 898 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
885 899 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
886 900
887 901 setIndexSpyArg = setIndexSpy2.takeFirst();
888 902 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
889 903 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
890 904 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
891 905 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
892 906 }
893 907
894 908 void tst_QBarSeries::clearWithAnimations()
895 909 {
896 910 QBarSeries* series = new QBarSeries();
897 911
898 912 QBarSet* set1 = new QBarSet(QString("set 1"));
899 913 *set1 << 10 << 10 << 10;
900 914 series->append(set1);
901 915
902 916 QBarSet* set2 = new QBarSet(QString("set 2"));
903 917 *set2 << 10 << 10 << 10;
904 918 series->append(set2);
905 919
906 920 QChartView view(new QChart());
907 921 view.resize(400,300);
908 922 view.chart()->setAnimationOptions(QChart::SeriesAnimations);
909 923 view.chart()->addSeries(series);
910 924 view.show();
911 925
912 926 series->clear();
913 927 }
914 928
915 929 void tst_QBarSeries::destruction()
916 930 {
917 931 // add a barset
918 932 QBarSeries *series = new QBarSeries();
919 933 QBarSet *set = new QBarSet("testset");
920 934 QSignalSpy spy1(set, SIGNAL(destroyed()));
921 935 series->append(set);
922 936
923 937 // delete the series
924 938 delete series;
925 939
926 940 // check that series deletes the set
927 941 QCOMPARE(spy1.count(), 1);
928 942 }
929 943
930 944 void tst_QBarSeries::mousePressed()
931 945 {
932 946 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
933 947
934 948 QBarSeries* series = new QBarSeries();
935 949
936 950 QBarSet* set1 = new QBarSet(QString("set 1"));
937 951 *set1 << 10 << 10 << 10;
938 952 series->append(set1);
939 953
940 954 QBarSet* set2 = new QBarSet(QString("set 2"));
941 955 *set2 << 10 << 10 << 10;
942 956 series->append(set2);
943 957 QList<QBarSet*> barSets = series->barSets();
944 958
945 959 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
946 960 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
947 961 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
948 962
949 963 QChartView view(new QChart());
950 964 view.resize(400,300);
951 965 view.chart()->addSeries(series);
952 966 view.show();
953 967 QTest::qWaitForWindowShown(&view);
954 968
955 969 // Calculate expected layout for bars
956 970 QRectF plotArea = view.chart()->plotArea();
957 971 qreal width = plotArea.width();
958 972 qreal height = plotArea.height();
959 973 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
960 974 qreal rangeX = 3; // 3 values per set
961 975 qreal scaleY = (height / rangeY);
962 976 qreal scaleX = (width / rangeX);
963 977
964 978 qreal setCount = series->count();
965 979 qreal domainMinY = 0; // These come from internal domain used by barseries.
966 980 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
967 981 qreal rectWidth = (scaleX / setCount) * series->barWidth();
968 982
969 983 QVector<QRectF> layout;
970 984
971 985 // 3 = count of values in set
972 986 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
973 987 for (int i = 0; i < 3; i++) {
974 988 qreal yPos = height + scaleY * domainMinY + plotArea.top();
975 989 for (int set = 0; set < setCount; set++) {
976 990 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
977 991 xPos -= series->count()*rectWidth/2;
978 992 xPos += set*rectWidth;
979 993
980 994 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
981 995 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
982 996 layout.append(rect);
983 997 }
984 998 }
985 999
986 1000 //====================================================================================
987 1001 // barset 1, bar 0
988 1002 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
989 1003 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
990 1004
991 1005 QCOMPARE(seriesSpy.count(), 1);
992 1006 QCOMPARE(setSpy1.count(), 1);
993 1007 QCOMPARE(setSpy2.count(), 0);
994 1008
995 1009 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
996 1010 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
997 1011 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
998 1012 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
999 1013
1000 1014 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1001 1015 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1002 1016 QVERIFY(setSpyArg.at(0).toInt() == 0);
1003 1017
1004 1018 //====================================================================================
1005 1019 // barset 1, bar 1
1006 1020 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
1007 1021 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1008 1022
1009 1023 QCOMPARE(seriesSpy.count(), 1);
1010 1024 QCOMPARE(setSpy1.count(), 1);
1011 1025 QCOMPARE(setSpy2.count(), 0);
1012 1026
1013 1027 seriesSpyArg = seriesSpy.takeFirst();
1014 1028 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1015 1029 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1016 1030 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1017 1031
1018 1032 setSpyArg = setSpy1.takeFirst();
1019 1033 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1020 1034 QVERIFY(setSpyArg.at(0).toInt() == 1);
1021 1035
1022 1036 //====================================================================================
1023 1037 // barset 1, bar 2
1024 1038 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
1025 1039 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1026 1040
1027 1041 QCOMPARE(seriesSpy.count(), 1);
1028 1042 QCOMPARE(setSpy1.count(), 1);
1029 1043 QCOMPARE(setSpy2.count(), 0);
1030 1044
1031 1045 seriesSpyArg = seriesSpy.takeFirst();
1032 1046 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1033 1047 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1034 1048 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1035 1049
1036 1050 setSpyArg = setSpy1.takeFirst();
1037 1051 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1038 1052 QVERIFY(setSpyArg.at(0).toInt() == 2);
1039 1053
1040 1054 //====================================================================================
1041 1055 // barset 2, bar 0
1042 1056 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
1043 1057 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1044 1058
1045 1059 QCOMPARE(seriesSpy.count(), 1);
1046 1060 QCOMPARE(setSpy1.count(), 0);
1047 1061 QCOMPARE(setSpy2.count(), 1);
1048 1062
1049 1063 seriesSpyArg = seriesSpy.takeFirst();
1050 1064 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1051 1065 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1052 1066 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1053 1067
1054 1068 setSpyArg = setSpy2.takeFirst();
1055 1069 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1056 1070 QVERIFY(setSpyArg.at(0).toInt() == 0);
1057 1071
1058 1072 //====================================================================================
1059 1073 // barset 2, bar 1
1060 1074 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
1061 1075 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1062 1076
1063 1077 QCOMPARE(seriesSpy.count(), 1);
1064 1078 QCOMPARE(setSpy1.count(), 0);
1065 1079 QCOMPARE(setSpy2.count(), 1);
1066 1080
1067 1081 seriesSpyArg = seriesSpy.takeFirst();
1068 1082 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1069 1083 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1070 1084 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1071 1085
1072 1086 setSpyArg = setSpy2.takeFirst();
1073 1087 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1074 1088 QVERIFY(setSpyArg.at(0).toInt() == 1);
1075 1089
1076 1090 //====================================================================================
1077 1091 // barset 2, bar 2
1078 1092 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
1079 1093 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1080 1094
1081 1095 QCOMPARE(seriesSpy.count(), 1);
1082 1096 QCOMPARE(setSpy1.count(), 0);
1083 1097 QCOMPARE(setSpy2.count(), 1);
1084 1098
1085 1099 seriesSpyArg = seriesSpy.takeFirst();
1086 1100 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1087 1101 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1088 1102 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1089 1103
1090 1104 setSpyArg = setSpy2.takeFirst();
1091 1105 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1092 1106 QVERIFY(setSpyArg.at(0).toInt() == 2);
1093 1107 }
1094 1108
1095 1109 void tst_QBarSeries::mouseReleased()
1096 1110 {
1097 1111 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
1098 1112
1099 1113 QBarSeries* series = new QBarSeries();
1100 1114
1101 1115 QBarSet* set1 = new QBarSet(QString("set 1"));
1102 1116 *set1 << 10 << 10 << 10;
1103 1117 series->append(set1);
1104 1118
1105 1119 QBarSet* set2 = new QBarSet(QString("set 2"));
1106 1120 *set2 << 10 << 10 << 10;
1107 1121 series->append(set2);
1108 1122 QList<QBarSet*> barSets = series->barSets();
1109 1123
1110 1124 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
1111 1125 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
1112 1126 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
1113 1127
1114 1128 QChartView view(new QChart());
1115 1129 view.resize(400,300);
1116 1130 view.chart()->addSeries(series);
1117 1131 view.show();
1118 1132 QTest::qWaitForWindowShown(&view);
1119 1133
1120 1134 // Calculate expected layout for bars
1121 1135 QRectF plotArea = view.chart()->plotArea();
1122 1136 qreal width = plotArea.width();
1123 1137 qreal height = plotArea.height();
1124 1138 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
1125 1139 qreal rangeX = 3; // 3 values per set
1126 1140 qreal scaleY = (height / rangeY);
1127 1141 qreal scaleX = (width / rangeX);
1128 1142
1129 1143 qreal setCount = series->count();
1130 1144 qreal domainMinY = 0; // These come from internal domain used by barseries.
1131 1145 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
1132 1146 qreal rectWidth = (scaleX / setCount) * series->barWidth();
1133 1147
1134 1148 QVector<QRectF> layout;
1135 1149
1136 1150 // 3 = count of values in set
1137 1151 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
1138 1152 for (int i = 0; i < 3; i++) {
1139 1153 qreal yPos = height + scaleY * domainMinY + plotArea.top();
1140 1154 for (int set = 0; set < setCount; set++) {
1141 1155 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
1142 1156 xPos -= series->count()*rectWidth/2;
1143 1157 xPos += set*rectWidth;
1144 1158
1145 1159 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
1146 1160 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
1147 1161 layout.append(rect);
1148 1162 }
1149 1163 }
1150 1164
1151 1165 //====================================================================================
1152 1166 // barset 1, bar 0
1153 1167 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1154 1168 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1155 1169
1156 1170 QCOMPARE(seriesSpy.count(), 1);
1157 1171 QCOMPARE(setSpy1.count(), 1);
1158 1172 QCOMPARE(setSpy2.count(), 0);
1159 1173
1160 1174 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1161 1175 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1162 1176 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1163 1177 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1164 1178
1165 1179 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1166 1180 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1167 1181 QVERIFY(setSpyArg.at(0).toInt() == 0);
1168 1182
1169 1183 //====================================================================================
1170 1184 // barset 1, bar 1
1171 1185 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
1172 1186 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1173 1187
1174 1188 QCOMPARE(seriesSpy.count(), 1);
1175 1189 QCOMPARE(setSpy1.count(), 1);
1176 1190 QCOMPARE(setSpy2.count(), 0);
1177 1191
1178 1192 seriesSpyArg = seriesSpy.takeFirst();
1179 1193 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1180 1194 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1181 1195 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1182 1196
1183 1197 setSpyArg = setSpy1.takeFirst();
1184 1198 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1185 1199 QVERIFY(setSpyArg.at(0).toInt() == 1);
1186 1200
1187 1201 //====================================================================================
1188 1202 // barset 1, bar 2
1189 1203 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
1190 1204 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1191 1205
1192 1206 QCOMPARE(seriesSpy.count(), 1);
1193 1207 QCOMPARE(setSpy1.count(), 1);
1194 1208 QCOMPARE(setSpy2.count(), 0);
1195 1209
1196 1210 seriesSpyArg = seriesSpy.takeFirst();
1197 1211 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1198 1212 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1199 1213 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1200 1214
1201 1215 setSpyArg = setSpy1.takeFirst();
1202 1216 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1203 1217 QVERIFY(setSpyArg.at(0).toInt() == 2);
1204 1218
1205 1219 //====================================================================================
1206 1220 // barset 2, bar 0
1207 1221 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
1208 1222 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1209 1223
1210 1224 QCOMPARE(seriesSpy.count(), 1);
1211 1225 QCOMPARE(setSpy1.count(), 0);
1212 1226 QCOMPARE(setSpy2.count(), 1);
1213 1227
1214 1228 seriesSpyArg = seriesSpy.takeFirst();
1215 1229 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1216 1230 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1217 1231 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1218 1232
1219 1233 setSpyArg = setSpy2.takeFirst();
1220 1234 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1221 1235 QVERIFY(setSpyArg.at(0).toInt() == 0);
1222 1236
1223 1237 //====================================================================================
1224 1238 // barset 2, bar 1
1225 1239 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
1226 1240 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1227 1241
1228 1242 QCOMPARE(seriesSpy.count(), 1);
1229 1243 QCOMPARE(setSpy1.count(), 0);
1230 1244 QCOMPARE(setSpy2.count(), 1);
1231 1245
1232 1246 seriesSpyArg = seriesSpy.takeFirst();
1233 1247 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1234 1248 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1235 1249 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1236 1250
1237 1251 setSpyArg = setSpy2.takeFirst();
1238 1252 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1239 1253 QVERIFY(setSpyArg.at(0).toInt() == 1);
1240 1254
1241 1255 //====================================================================================
1242 1256 // barset 2, bar 2
1243 1257 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
1244 1258 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1245 1259
1246 1260 QCOMPARE(seriesSpy.count(), 1);
1247 1261 QCOMPARE(setSpy1.count(), 0);
1248 1262 QCOMPARE(setSpy2.count(), 1);
1249 1263
1250 1264 seriesSpyArg = seriesSpy.takeFirst();
1251 1265 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1252 1266 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1253 1267 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1254 1268
1255 1269 setSpyArg = setSpy2.takeFirst();
1256 1270 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1257 1271 QVERIFY(setSpyArg.at(0).toInt() == 2);
1258 1272 }
1259 1273
1260 1274 void tst_QBarSeries::mouseDoubleClicked()
1261 1275 {
1262 1276 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
1263 1277
1264 1278 QBarSeries* series = new QBarSeries();
1265 1279
1266 1280 QBarSet* set1 = new QBarSet(QString("set 1"));
1267 1281 *set1 << 10 << 10 << 10;
1268 1282 series->append(set1);
1269 1283
1270 1284 QBarSet* set2 = new QBarSet(QString("set 2"));
1271 1285 *set2 << 10 << 10 << 10;
1272 1286 series->append(set2);
1273 1287 QList<QBarSet*> barSets = series->barSets();
1274 1288
1275 1289 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
1276 1290 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
1277 1291 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
1278 1292
1279 1293 QChartView view(new QChart());
1280 1294 view.resize(400,300);
1281 1295 view.chart()->addSeries(series);
1282 1296 view.show();
1283 1297 QTest::qWaitForWindowShown(&view);
1284 1298
1285 1299 // Calculate expected layout for bars
1286 1300 QRectF plotArea = view.chart()->plotArea();
1287 1301 qreal width = plotArea.width();
1288 1302 qreal height = plotArea.height();
1289 1303 qreal rangeY = 10; // From 0 to 10 because of maximum value in set is 10
1290 1304 qreal rangeX = 3; // 3 values per set
1291 1305 qreal scaleY = (height / rangeY);
1292 1306 qreal scaleX = (width / rangeX);
1293 1307
1294 1308 qreal setCount = series->count();
1295 1309 qreal domainMinY = 0; // These come from internal domain used by barseries.
1296 1310 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
1297 1311 qreal rectWidth = (scaleX / setCount) * series->barWidth();
1298 1312
1299 1313 QVector<QRectF> layout;
1300 1314
1301 1315 // 3 = count of values in set
1302 1316 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
1303 1317 for (int i = 0; i < 3; i++) {
1304 1318 qreal yPos = height + scaleY * domainMinY + plotArea.top();
1305 1319 for (int set = 0; set < setCount; set++) {
1306 1320 qreal xPos = (i - domainMinX) * scaleX + plotArea.left();
1307 1321 xPos -= series->count()*rectWidth/2;
1308 1322 xPos += set*rectWidth;
1309 1323
1310 1324 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
1311 1325 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
1312 1326 layout.append(rect);
1313 1327 }
1314 1328 }
1315 1329
1316 1330 // barset 1, bar 0
1317 1331 QTest::mouseDClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1318 1332 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1319 1333
1320 1334 QCOMPARE(seriesSpy.count(), 1);
1321 1335 QCOMPARE(setSpy1.count(), 1);
1322 1336 QCOMPARE(setSpy2.count(), 0);
1323 1337
1324 1338 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1325 1339 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1326 1340 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1327 1341 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1328 1342
1329 1343 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1330 1344 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1331 1345 QVERIFY(setSpyArg.at(0).toInt() == 0);
1332 1346 }
1333 1347
1334 1348 QTEST_MAIN(tst_QBarSeries)
1335 1349
1336 1350 #include "tst_qbarseries.moc"
1337 1351
@@ -1,1269 +1,1283
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include <QtTest/QtTest>
20 20 #include <QtCharts/QHorizontalBarSeries>
21 21 #include <QtCharts/QBarSet>
22 22 #include <QtCharts/QChartView>
23 23 #include <QtCharts/QChart>
24 24 #include "tst_definitions.h"
25 25
26 26 QT_CHARTS_USE_NAMESPACE
27 27
28 28 Q_DECLARE_METATYPE(QBarSet*)
29 29 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
30 30
31 31 class tst_QHorizontalBarSeries : public QObject
32 32 {
33 33 Q_OBJECT
34 34
35 35 public slots:
36 36 void initTestCase();
37 37 void cleanupTestCase();
38 38 void init();
39 39 void cleanup();
40 40
41 41 private slots:
42 42 void qhorizontalbarseries_data();
43 43 void qhorizontalbarseries();
44 44 void type_data();
45 45 void type();
46 46 void append_data();
47 47 void append();
48 48 void remove_data();
49 49 void remove();
50 50 void appendList_data();
51 51 void appendList();
52 52 void count_data();
53 53 void count();
54 54 void barSets_data();
55 55 void barSets();
56 56 void setLabelsVisible_data();
57 57 void setLabelsVisible();
58 58 void setLabelsFormat();
59 59 void setLabelsPosition();
60 void setLabelsAngle();
60 61 void mouseclicked_data();
61 62 void mouseclicked();
62 63 void mousehovered_data();
63 64 void mousehovered();
64 65 void clearWithAnimations();
65 66 void mousePressed();
66 67 void mouseReleased();
67 68 void mouseDoubleClicked();
68 69
69 70 private:
70 71 QHorizontalBarSeries* m_barseries;
71 72 QHorizontalBarSeries* m_barseries_with_sets;
72 73
73 74 QList<QBarSet*> m_testSets;
74 75
75 76 };
76 77
77 78 void tst_QHorizontalBarSeries::initTestCase()
78 79 {
79 80 qRegisterMetaType<QBarSet*>("QBarSet*");
80 81 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
81 82 }
82 83
83 84 void tst_QHorizontalBarSeries::cleanupTestCase()
84 85 {
85 86 QTest::qWait(1); // Allow final deleteLaters to run
86 87 }
87 88
88 89 void tst_QHorizontalBarSeries::init()
89 90 {
90 91 m_barseries = new QHorizontalBarSeries();
91 92 m_barseries_with_sets = new QHorizontalBarSeries();
92 93
93 94 for (int i=0; i<5; i++) {
94 95 m_testSets.append(new QBarSet("testset"));
95 96 m_barseries_with_sets->append(m_testSets.at(i));
96 97 }
97 98 }
98 99
99 100 void tst_QHorizontalBarSeries::cleanup()
100 101 {
101 102 foreach (QBarSet* s, m_testSets) {
102 103 m_barseries_with_sets->remove(s);
103 104 }
104 105 m_testSets.clear();
105 106
106 107 delete m_barseries;
107 108 m_barseries = 0;
108 109 delete m_barseries_with_sets;
109 110 m_barseries_with_sets = 0;
110 111 }
111 112
112 113 void tst_QHorizontalBarSeries::qhorizontalbarseries_data()
113 114 {
114 115 }
115 116
116 117 void tst_QHorizontalBarSeries::qhorizontalbarseries()
117 118 {
118 119 QHorizontalBarSeries *barseries = new QHorizontalBarSeries();
119 120 QVERIFY(barseries != 0);
120 121 delete barseries;
121 122 }
122 123
123 124 void tst_QHorizontalBarSeries::type_data()
124 125 {
125 126
126 127 }
127 128
128 129 void tst_QHorizontalBarSeries::type()
129 130 {
130 131 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeHorizontalBar);
131 132 }
132 133
133 134 void tst_QHorizontalBarSeries::append_data()
134 135 {
135 136 }
136 137
137 138 void tst_QHorizontalBarSeries::append()
138 139 {
139 140 QVERIFY(m_barseries->count() == 0);
140 141
141 142 bool ret = false;
142 143
143 144 // Try adding barset
144 145 QBarSet *barset = new QBarSet("testset");
145 146 ret = m_barseries->append(barset);
146 147
147 148 QVERIFY(ret == true);
148 149 QVERIFY(m_barseries->count() == 1);
149 150
150 151 // Try adding another set
151 152 QBarSet *barset2 = new QBarSet("testset2");
152 153 ret = m_barseries->append(barset2);
153 154
154 155 QVERIFY(ret == true);
155 156 QVERIFY(m_barseries->count() == 2);
156 157
157 158 // Try adding same set again
158 159 ret = m_barseries->append(barset2);
159 160 QVERIFY(ret == false);
160 161 QVERIFY(m_barseries->count() == 2);
161 162
162 163 // Try adding null set
163 164 ret = m_barseries->append(0);
164 165 QVERIFY(ret == false);
165 166 QVERIFY(m_barseries->count() == 2);
166 167
167 168 }
168 169
169 170 void tst_QHorizontalBarSeries::remove_data()
170 171 {
171 172 }
172 173
173 174 void tst_QHorizontalBarSeries::remove()
174 175 {
175 176 int count = m_testSets.count();
176 177 QVERIFY(m_barseries_with_sets->count() == count);
177 178
178 179 // Try to remove null pointer (should not remove, should not crash)
179 180 bool ret = false;
180 181 ret = m_barseries_with_sets->remove(0);
181 182 QVERIFY(ret == false);
182 183 QVERIFY(m_barseries_with_sets->count() == count);
183 184
184 185 // Try to remove invalid pointer (should not remove, should not crash)
185 186 ret = m_barseries_with_sets->remove((QBarSet*) (m_testSets.at(0) + 1) );
186 187 QVERIFY(ret == false);
187 188 QVERIFY(m_barseries_with_sets->count() == count);
188 189
189 190 // remove some sets
190 191 ret = m_barseries_with_sets->remove(m_testSets.at(2));
191 192 QVERIFY(ret == true);
192 193 ret = m_barseries_with_sets->remove(m_testSets.at(3));
193 194 QVERIFY(ret == true);
194 195 ret = m_barseries_with_sets->remove(m_testSets.at(4));
195 196 QVERIFY(ret == true);
196 197
197 198 QVERIFY(m_barseries_with_sets->count() == 2);
198 199
199 200 QList<QBarSet*> verifysets = m_barseries_with_sets->barSets();
200 201
201 202 QVERIFY(verifysets.at(0) == m_testSets.at(0));
202 203 QVERIFY(verifysets.at(1) == m_testSets.at(1));
203 204
204 205 // Try removing all sets again (should be ok, even if some sets have already been removed)
205 206 ret = false;
206 207 for (int i=0; i<count; i++) {
207 208 ret |= m_barseries_with_sets->remove(m_testSets.at(i));
208 209 }
209 210
210 211 QVERIFY(ret == true);
211 212 QVERIFY(m_barseries_with_sets->count() == 0);
212 213 }
213 214
214 215 void tst_QHorizontalBarSeries::appendList_data()
215 216 {
216 217
217 218 }
218 219
219 220 void tst_QHorizontalBarSeries::appendList()
220 221 {
221 222 int count = 5;
222 223 QVERIFY(m_barseries->count() == 0);
223 224
224 225 QList<QBarSet*> sets;
225 226 for (int i=0; i<count; i++) {
226 227 sets.append(new QBarSet("testset"));
227 228 }
228 229
229 230 // Append new sets (should succeed, count should match the count of sets)
230 231 bool ret = false;
231 232 ret = m_barseries->append(sets);
232 233 QVERIFY(ret == true);
233 234 QVERIFY(m_barseries->count() == count);
234 235
235 236 // Append same sets again (should fail, count should remain same)
236 237 ret = m_barseries->append(sets);
237 238 QVERIFY(ret == false);
238 239 QVERIFY(m_barseries->count() == count);
239 240
240 241 // Try append empty list (should succeed, but count should remain same)
241 242 QList<QBarSet*> invalidList;
242 243 ret = m_barseries->append(invalidList);
243 244 QVERIFY(ret == true);
244 245 QVERIFY(m_barseries->count() == count);
245 246
246 247 // Try append list with one new and one existing set (should fail, count remains same)
247 248 invalidList.append(new QBarSet("ok set"));
248 249 invalidList.append(sets.at(0));
249 250 ret = m_barseries->append(invalidList);
250 251 QVERIFY(ret == false);
251 252 QVERIFY(m_barseries->count() == count);
252 253 delete invalidList.at(0);
253 254
254 255 // Try append list with null pointers (should fail, count remains same)
255 256 QList<QBarSet*> invalidList2;
256 257 invalidList2.append(0);
257 258 invalidList2.append(0);
258 259 invalidList2.append(0);
259 260 ret = m_barseries->append(invalidList2);
260 261 QVERIFY(ret == false);
261 262 QVERIFY(m_barseries->count() == count);
262 263 }
263 264
264 265 void tst_QHorizontalBarSeries::count_data()
265 266 {
266 267
267 268 }
268 269
269 270 void tst_QHorizontalBarSeries::count()
270 271 {
271 272 QVERIFY(m_barseries->count() == 0);
272 273 QVERIFY(m_barseries_with_sets->count() == m_testSets.count());
273 274 }
274 275
275 276 void tst_QHorizontalBarSeries::barSets_data()
276 277 {
277 278
278 279 }
279 280
280 281 void tst_QHorizontalBarSeries::barSets()
281 282 {
282 283 QVERIFY(m_barseries->barSets().count() == 0);
283 284
284 285 QList<QBarSet*> sets = m_barseries_with_sets->barSets();
285 286 QVERIFY(sets.count() == m_testSets.count());
286 287
287 288 for (int i=0; i<m_testSets.count(); i++) {
288 289 QVERIFY(sets.at(i) == m_testSets.at(i));
289 290 }
290 291 }
291 292
292 293 void tst_QHorizontalBarSeries::setLabelsVisible_data()
293 294 {
294 295
295 296 }
296 297
297 298 void tst_QHorizontalBarSeries::setLabelsVisible()
298 299 {
299 300 // labels should be invisible by default
300 301 QVERIFY(m_barseries->isLabelsVisible() == false);
301 302 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
302 303
303 304 // turn labels to visible
304 305 m_barseries_with_sets->setLabelsVisible(true);
305 306 // TODO: test the signal
306 307 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
307 308
308 309 // turn labels to invisible
309 310 m_barseries_with_sets->setLabelsVisible(false);
310 311 // TODO: test the signal
311 312 QVERIFY(m_barseries_with_sets->isLabelsVisible() == false);
312 313
313 314 // without parameter, should turn labels to visible
314 315 m_barseries_with_sets->setLabelsVisible();
315 316 // TODO: test the signal
316 317 QVERIFY(m_barseries_with_sets->isLabelsVisible() == true);
317 318 }
318 319
319 320 void tst_QHorizontalBarSeries::setLabelsFormat()
320 321 {
321 322 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
322 323 QCOMPARE(m_barseries->labelsFormat(), QString());
323 324
324 325 QString format("(@value)");
325 326 m_barseries->setLabelsFormat(format);
326 327 TRY_COMPARE(labelsFormatSpy.count(), 1);
327 328 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
328 329 QVERIFY(arguments.at(0).toString() == format);
329 330 QCOMPARE(m_barseries->labelsFormat(), format);
330 331
331 332 m_barseries->setLabelsFormat(QString());
332 333 TRY_COMPARE(labelsFormatSpy.count(), 1);
333 334 arguments = labelsFormatSpy.takeFirst();
334 335 QVERIFY(arguments.at(0).toString() == QString());
335 336 QCOMPARE(m_barseries->labelsFormat(), QString());
336 337 }
337 338
338 339 void tst_QHorizontalBarSeries::setLabelsPosition()
339 340 {
340 341 QSignalSpy labelsPositionSpy(m_barseries,
341 342 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
342 343 QCOMPARE(m_barseries->labelsPosition(), QHorizontalBarSeries::LabelsCenter);
343 344
344 345 m_barseries->setLabelsPosition(QHorizontalBarSeries::LabelsInsideEnd);
345 346 TRY_COMPARE(labelsPositionSpy.count(), 1);
346 347 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
347 348 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
348 349 == QHorizontalBarSeries::LabelsInsideEnd);
349 350 QCOMPARE(m_barseries->labelsPosition(), QHorizontalBarSeries::LabelsInsideEnd);
350 351
351 352 m_barseries->setLabelsPosition(QHorizontalBarSeries::LabelsInsideBase);
352 353 TRY_COMPARE(labelsPositionSpy.count(), 1);
353 354 arguments = labelsPositionSpy.takeFirst();
354 355 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
355 356 == QHorizontalBarSeries::LabelsInsideBase);
356 357 QCOMPARE(m_barseries->labelsPosition(), QHorizontalBarSeries::LabelsInsideBase);
357 358
358 359 m_barseries->setLabelsPosition(QHorizontalBarSeries::LabelsOutsideEnd);
359 360 TRY_COMPARE(labelsPositionSpy.count(), 1);
360 361 arguments = labelsPositionSpy.takeFirst();
361 362 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
362 363 == QHorizontalBarSeries::LabelsOutsideEnd);
363 364 QCOMPARE(m_barseries->labelsPosition(), QHorizontalBarSeries::LabelsOutsideEnd);
364 365
365 366 m_barseries->setLabelsPosition(QHorizontalBarSeries::LabelsCenter);
366 367 TRY_COMPARE(labelsPositionSpy.count(), 1);
367 368 arguments = labelsPositionSpy.takeFirst();
368 369 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
369 370 == QHorizontalBarSeries::LabelsCenter);
370 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 387 void tst_QHorizontalBarSeries::mouseclicked_data()
374 388 {
375 389
376 390 }
377 391
378 392 void tst_QHorizontalBarSeries::mouseclicked()
379 393 {
380 394 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
381 395
382 396 QHorizontalBarSeries* series = new QHorizontalBarSeries();
383 397
384 398 QBarSet* set1 = new QBarSet(QString("set 1"));
385 399 *set1 << 10 << 10 << 10;
386 400 series->append(set1);
387 401
388 402 QBarSet* set2 = new QBarSet(QString("set 2"));
389 403 *set2 << 10 << 10 << 10;
390 404 series->append(set2);
391 405
392 406 QList<QBarSet*> barSets = series->barSets();
393 407
394 408 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
395 409 QSignalSpy setSpy1(set1, SIGNAL(clicked(int)));
396 410 QSignalSpy setSpy2(set2, SIGNAL(clicked(int)));
397 411
398 412 QChartView view(new QChart());
399 413 view.resize(400,300);
400 414 view.chart()->addSeries(series);
401 415 view.show();
402 416 QTest::qWaitForWindowShown(&view);
403 417
404 418 // Calculate expected layout for bars
405 419 QRectF plotArea = view.chart()->plotArea();
406 420 qreal width = plotArea.width();
407 421 qreal height = plotArea.height();
408 422 qreal rangeX = 10; // From 0 to 10 because of maximum value in set is 10
409 423 qreal rangeY = 3; // 3 values per set
410 424 qreal scaleY = (height / rangeY);
411 425 qreal scaleX = (width / rangeX);
412 426
413 427 qreal setCount = series->count();
414 428 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
415 429 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
416 430 qreal rectHeight = (scaleY / setCount) * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
417 431
418 432 QVector<QRectF> layout;
419 433
420 434 // 3 = count of values in set
421 435 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
422 436 for (int i = 0; i < 3; i++) {
423 437 qreal xPos = -scaleX * domainMinX + plotArea.left();
424 438 for (int set = 0; set < setCount; set++) {
425 439 qreal yPos = plotArea.bottom() + (domainMinY - i) * scaleY;
426 440 yPos += setCount*rectHeight/2;
427 441 yPos -= set*rectHeight;
428 442
429 443 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
430 444 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
431 445 layout.append(rect);
432 446 }
433 447 }
434 448
435 449 //====================================================================================
436 450 // barset 1, bar 0
437 451 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
438 452 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
439 453
440 454 QCOMPARE(seriesSpy.count(), 1);
441 455 QCOMPARE(setSpy1.count(), 1);
442 456 QCOMPARE(setSpy2.count(), 0);
443 457
444 458 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
445 459 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
446 460 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
447 461 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
448 462
449 463 QList<QVariant> setSpyArg = setSpy1.takeFirst();
450 464 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
451 465 QVERIFY(setSpyArg.at(0).toInt() == 0);
452 466
453 467 //====================================================================================
454 468 // barset 1, bar 1
455 469 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
456 470 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
457 471
458 472 QCOMPARE(seriesSpy.count(), 1);
459 473 QCOMPARE(setSpy1.count(), 1);
460 474 QCOMPARE(setSpy2.count(), 0);
461 475
462 476 seriesSpyArg = seriesSpy.takeFirst();
463 477 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
464 478 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
465 479 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
466 480
467 481 setSpyArg = setSpy1.takeFirst();
468 482 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
469 483 QVERIFY(setSpyArg.at(0).toInt() == 1);
470 484
471 485 //====================================================================================
472 486 // barset 1, bar 2
473 487 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
474 488 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
475 489
476 490 QCOMPARE(seriesSpy.count(), 1);
477 491 QCOMPARE(setSpy1.count(), 1);
478 492 QCOMPARE(setSpy2.count(), 0);
479 493
480 494 seriesSpyArg = seriesSpy.takeFirst();
481 495 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
482 496 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
483 497 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
484 498
485 499 setSpyArg = setSpy1.takeFirst();
486 500 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
487 501 QVERIFY(setSpyArg.at(0).toInt() == 2);
488 502
489 503 //====================================================================================
490 504 // barset 2, bar 0
491 505 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
492 506 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
493 507
494 508 QCOMPARE(seriesSpy.count(), 1);
495 509 QCOMPARE(setSpy1.count(), 0);
496 510 QCOMPARE(setSpy2.count(), 1);
497 511
498 512 seriesSpyArg = seriesSpy.takeFirst();
499 513 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
500 514 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
501 515 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
502 516
503 517 setSpyArg = setSpy2.takeFirst();
504 518 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
505 519 QVERIFY(setSpyArg.at(0).toInt() == 0);
506 520
507 521 //====================================================================================
508 522 // barset 2, bar 1
509 523 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
510 524 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
511 525
512 526 QCOMPARE(seriesSpy.count(), 1);
513 527 QCOMPARE(setSpy1.count(), 0);
514 528 QCOMPARE(setSpy2.count(), 1);
515 529
516 530 seriesSpyArg = seriesSpy.takeFirst();
517 531 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
518 532 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
519 533 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
520 534
521 535 setSpyArg = setSpy2.takeFirst();
522 536 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
523 537 QVERIFY(setSpyArg.at(0).toInt() == 1);
524 538
525 539 //====================================================================================
526 540 // barset 2, bar 2
527 541 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
528 542 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
529 543
530 544 QCOMPARE(seriesSpy.count(), 1);
531 545 QCOMPARE(setSpy1.count(), 0);
532 546 QCOMPARE(setSpy2.count(), 1);
533 547
534 548 seriesSpyArg = seriesSpy.takeFirst();
535 549 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
536 550 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
537 551 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
538 552
539 553 setSpyArg = setSpy2.takeFirst();
540 554 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
541 555 QVERIFY(setSpyArg.at(0).toInt() == 2);
542 556 }
543 557
544 558 void tst_QHorizontalBarSeries::mousehovered_data()
545 559 {
546 560
547 561 }
548 562
549 563 void tst_QHorizontalBarSeries::mousehovered()
550 564 {
551 565 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
552 566
553 567 QHorizontalBarSeries* series = new QHorizontalBarSeries();
554 568
555 569 QBarSet* set1 = new QBarSet(QString("set 1"));
556 570 *set1 << 10 << 10 << 10;
557 571 series->append(set1);
558 572
559 573 QBarSet* set2 = new QBarSet(QString("set 2"));
560 574 *set2 << 10 << 10 << 10;
561 575 series->append(set2);
562 576
563 577 QList<QBarSet*> barSets = series->barSets();
564 578
565 579 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
566 580 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
567 581 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
568 582
569 583 QChartView view(new QChart());
570 584 view.resize(400,300);
571 585 view.chart()->addSeries(series);
572 586 view.show();
573 587 QTest::qWaitForWindowShown(&view);
574 588
575 589 //this is hack since view does not get events otherwise
576 590 view.setMouseTracking(true);
577 591
578 592 // Calculate expected layout for bars
579 593 QRectF plotArea = view.chart()->plotArea();
580 594 qreal width = plotArea.width();
581 595 qreal height = plotArea.height();
582 596 qreal rangeX = 10; // From 0 to 10 because of maximum value in set is 10
583 597 qreal rangeY = 3; // 3 values per set
584 598 qreal scaleY = (height / rangeY);
585 599 qreal scaleX = (width / rangeX);
586 600
587 601 qreal setCount = series->count();
588 602 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
589 603 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
590 604 qreal rectHeight = (scaleY / setCount) * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
591 605
592 606 QVector<QRectF> layout;
593 607
594 608 // 3 = count of values in set
595 609 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
596 610 for (int i = 0; i < 3; i++) {
597 611 qreal xPos = -scaleX * domainMinX + plotArea.left();
598 612 for (int set = 0; set < setCount; set++) {
599 613 qreal yPos = plotArea.bottom() + (domainMinY - i) * scaleY;
600 614 yPos += setCount*rectHeight/2;
601 615 yPos -= set*rectHeight;
602 616
603 617 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
604 618 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
605 619 layout.append(rect);
606 620 }
607 621 }
608 622
609 623 //=======================================================================
610 624 // move mouse to bottom border
611 625 QTest::mouseMove(view.viewport(), QPoint(layout.at(0).center().x(), 300));
612 626 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
613 627 TRY_COMPARE(seriesIndexSpy.count(), 0);
614 628 TRY_COMPARE(setIndexSpy1.count(), 0);
615 629 TRY_COMPARE(setIndexSpy2.count(), 0);
616 630
617 631 //=======================================================================
618 632 // move mouse on top of set1
619 633 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
620 634 TRY_COMPARE(seriesIndexSpy.count(), 1);
621 635 TRY_COMPARE(setIndexSpy1.count(), 1);
622 636 TRY_COMPARE(setIndexSpy2.count(), 0);
623 637
624 638 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
625 639 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
626 640 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
627 641 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
628 642
629 643 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
630 644 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
631 645 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
632 646
633 647 //=======================================================================
634 648 // move mouse from top of set1 to top of set2
635 649 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
636 650 TRY_COMPARE(seriesIndexSpy.count(), 2);
637 651 TRY_COMPARE(setIndexSpy1.count(), 1);
638 652 TRY_COMPARE(setIndexSpy2.count(), 1);
639 653
640 654 // should leave set1
641 655 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
642 656 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
643 657 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
644 658 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
645 659
646 660 setIndexSpyArg = setIndexSpy1.takeFirst();
647 661 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
648 662 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
649 663
650 664 // should enter set2
651 665 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
652 666 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
653 667 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
654 668 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
655 669
656 670 setIndexSpyArg = setIndexSpy2.takeFirst();
657 671 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
658 672 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
659 673
660 674 //=======================================================================
661 675 // move mouse from top of set2 to background
662 676 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(0).center().y()));
663 677 TRY_COMPARE(seriesIndexSpy.count(), 1);
664 678 TRY_COMPARE(setIndexSpy1.count(), 0);
665 679 TRY_COMPARE(setIndexSpy2.count(), 1);
666 680
667 681 // should leave set2
668 682 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
669 683 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
670 684 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
671 685 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
672 686
673 687 setIndexSpyArg = setIndexSpy2.takeFirst();
674 688 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
675 689 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
676 690
677 691 //=======================================================================
678 692 // move mouse on top of set1, bar0 to check the index (hover into set1)
679 693 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
680 694
681 695 TRY_COMPARE(seriesIndexSpy.count(), 1);
682 696 TRY_COMPARE(setIndexSpy1.count(), 1);
683 697 TRY_COMPARE(setIndexSpy2.count(), 0);
684 698
685 699 //should enter set1, bar0
686 700 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
687 701 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
688 702 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
689 703 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
690 704 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
691 705 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
692 706
693 707 setIndexSpyArg = setIndexSpy1.takeFirst();
694 708 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
695 709 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
696 710 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
697 711 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
698 712
699 713 //=======================================================================
700 714 // move mouse on top of set2, bar0 to check the index (hover out set1,
701 715 // hover in set1)
702 716 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
703 717
704 718 TRY_COMPARE(seriesIndexSpy.count(), 2);
705 719 TRY_COMPARE(setIndexSpy1.count(), 1);
706 720 TRY_COMPARE(setIndexSpy2.count(), 1);
707 721
708 722 // should leave set1, bar0
709 723 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
710 724 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
711 725 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
712 726 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
713 727 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
714 728 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
715 729
716 730 setIndexSpyArg = setIndexSpy1.takeFirst();
717 731 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
718 732 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
719 733 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
720 734 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
721 735
722 736 // should enter set2, bar0
723 737 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
724 738 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
725 739 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
726 740 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
727 741 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
728 742 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
729 743
730 744 setIndexSpyArg = setIndexSpy2.takeFirst();
731 745 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
732 746 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
733 747 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
734 748 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
735 749
736 750 //=======================================================================
737 751 // move mouse on top of set1, bar1 to check the index (hover out set 2,
738 752 // hover in set1)
739 753 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
740 754
741 755 TRY_COMPARE(seriesIndexSpy.count(), 2);
742 756 TRY_COMPARE(setIndexSpy1.count(), 1);
743 757 TRY_COMPARE(setIndexSpy2.count(), 1);
744 758
745 759 // should leave set2, bar0
746 760 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
747 761 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
748 762 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
749 763 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
750 764 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
751 765 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
752 766
753 767 setIndexSpyArg = setIndexSpy2.takeFirst();
754 768 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
755 769 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
756 770 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
757 771 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
758 772
759 773 // should enter set1, bar1
760 774 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
761 775 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
762 776 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
763 777 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
764 778 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
765 779 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
766 780
767 781 setIndexSpyArg = setIndexSpy1.takeFirst();
768 782 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
769 783 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
770 784 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
771 785 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
772 786
773 787 //=======================================================================
774 788 // move mouse on top of view between set1 and set2 to check the index
775 789 // (hover out set1)
776 790 QTest::mouseMove(view.viewport(), QPoint(0, (layout.at(2).top() + layout.at(3).bottom()) / 2));
777 791
778 792 TRY_COMPARE(seriesIndexSpy.count(), 1);
779 793 TRY_COMPARE(setIndexSpy1.count(), 1);
780 794 TRY_COMPARE(setIndexSpy2.count(), 0);
781 795
782 796 // should leave set1, bar1
783 797 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
784 798 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
785 799 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
786 800 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
787 801 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
788 802 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
789 803
790 804 setIndexSpyArg = setIndexSpy1.takeFirst();
791 805 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
792 806 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
793 807 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
794 808 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
795 809
796 810 //=======================================================================
797 811 // move mouse on top of set2, bar1 to check the index (hover in set2)
798 812 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
799 813
800 814 TRY_COMPARE(seriesIndexSpy.count(), 1);
801 815 TRY_COMPARE(setIndexSpy1.count(), 0);
802 816 TRY_COMPARE(setIndexSpy2.count(), 1);
803 817
804 818 // should enter set2, bar1
805 819 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
806 820 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
807 821 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
808 822 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
809 823 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
810 824 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
811 825
812 826 setIndexSpyArg = setIndexSpy2.takeFirst();
813 827 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
814 828 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
815 829 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
816 830 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
817 831
818 832 //=======================================================================
819 833 // move mouse on top of the view between bar1 and bar2 to check the index
820 834 //(hover out set2)
821 835 QTest::mouseMove(view.viewport(), QPoint(0, (layout.at(3).top() + layout.at(3).bottom()) / 2));
822 836
823 837 TRY_COMPARE(seriesIndexSpy.count(), 1);
824 838 TRY_COMPARE(setIndexSpy1.count(), 0);
825 839 TRY_COMPARE(setIndexSpy2.count(), 1);
826 840
827 841 // should leave set2, bar1
828 842 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
829 843 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
830 844 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
831 845 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
832 846 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
833 847 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
834 848
835 849 setIndexSpyArg = setIndexSpy2.takeFirst();
836 850 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
837 851 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
838 852 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
839 853 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
840 854 }
841 855
842 856 void tst_QHorizontalBarSeries::clearWithAnimations()
843 857 {
844 858 QHorizontalBarSeries* series = new QHorizontalBarSeries();
845 859
846 860 QBarSet* set1 = new QBarSet(QString("set 1"));
847 861 *set1 << 10 << 10 << 10;
848 862 series->append(set1);
849 863
850 864 QBarSet* set2 = new QBarSet(QString("set 2"));
851 865 *set2 << 10 << 10 << 10;
852 866 series->append(set2);
853 867
854 868 QChartView view(new QChart());
855 869 view.resize(400,300);
856 870 view.chart()->setAnimationOptions(QChart::SeriesAnimations);
857 871 view.chart()->addSeries(series);
858 872 view.show();
859 873
860 874 series->clear();
861 875 }
862 876
863 877 void tst_QHorizontalBarSeries::mousePressed()
864 878 {
865 879 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
866 880
867 881 QHorizontalBarSeries* series = new QHorizontalBarSeries();
868 882
869 883 QBarSet* set1 = new QBarSet(QString("set 1"));
870 884 *set1 << 10 << 10 << 10;
871 885 series->append(set1);
872 886
873 887 QBarSet* set2 = new QBarSet(QString("set 2"));
874 888 *set2 << 10 << 10 << 10;
875 889 series->append(set2);
876 890 QList<QBarSet*> barSets = series->barSets();
877 891
878 892 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
879 893 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
880 894 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
881 895
882 896 QChartView view(new QChart());
883 897 view.resize(400,300);
884 898 view.chart()->addSeries(series);
885 899 view.show();
886 900 QTest::qWaitForWindowShown(&view);
887 901
888 902 // Calculate expected layout for bars
889 903 QRectF plotArea = view.chart()->plotArea();
890 904 qreal width = plotArea.width();
891 905 qreal height = plotArea.height();
892 906 qreal rangeX = 10; // From 0 to 10 because of maximum value in set is 10
893 907 qreal rangeY = 3; // 3 values per set
894 908 qreal scaleY = (height / rangeY);
895 909 qreal scaleX = (width / rangeX);
896 910
897 911 qreal setCount = series->count();
898 912 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
899 913 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
900 914 qreal rectHeight = (scaleY / setCount) * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
901 915
902 916 QVector<QRectF> layout;
903 917
904 918 // 3 = count of values in set
905 919 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
906 920 for (int i = 0; i < 3; i++) {
907 921 qreal xPos = -scaleX * domainMinX + plotArea.left();
908 922 for (int set = 0; set < setCount; set++) {
909 923 qreal yPos = plotArea.bottom() + (domainMinY - i) * scaleY;
910 924 yPos += setCount*rectHeight/2;
911 925 yPos -= set*rectHeight;
912 926
913 927 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
914 928 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
915 929 layout.append(rect);
916 930 }
917 931 }
918 932
919 933 //====================================================================================
920 934 // barset 1, bar 0
921 935 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
922 936 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
923 937
924 938 QCOMPARE(seriesSpy.count(), 1);
925 939 QCOMPARE(setSpy1.count(), 1);
926 940 QCOMPARE(setSpy2.count(), 0);
927 941
928 942 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
929 943 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
930 944 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
931 945 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
932 946
933 947 QList<QVariant> setSpyArg = setSpy1.takeFirst();
934 948 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
935 949 QVERIFY(setSpyArg.at(0).toInt() == 0);
936 950
937 951 //====================================================================================
938 952 // barset 1, bar 1
939 953 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
940 954 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
941 955
942 956 QCOMPARE(seriesSpy.count(), 1);
943 957 QCOMPARE(setSpy1.count(), 1);
944 958 QCOMPARE(setSpy2.count(), 0);
945 959
946 960 seriesSpyArg = seriesSpy.takeFirst();
947 961 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
948 962 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
949 963 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
950 964
951 965 setSpyArg = setSpy1.takeFirst();
952 966 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
953 967 QVERIFY(setSpyArg.at(0).toInt() == 1);
954 968
955 969 //====================================================================================
956 970 // barset 1, bar 2
957 971 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
958 972 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
959 973
960 974 QCOMPARE(seriesSpy.count(), 1);
961 975 QCOMPARE(setSpy1.count(), 1);
962 976 QCOMPARE(setSpy2.count(), 0);
963 977
964 978 seriesSpyArg = seriesSpy.takeFirst();
965 979 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
966 980 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
967 981 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
968 982
969 983 setSpyArg = setSpy1.takeFirst();
970 984 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
971 985 QVERIFY(setSpyArg.at(0).toInt() == 2);
972 986
973 987 //====================================================================================
974 988 // barset 2, bar 0
975 989 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
976 990 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
977 991
978 992 QCOMPARE(seriesSpy.count(), 1);
979 993 QCOMPARE(setSpy1.count(), 0);
980 994 QCOMPARE(setSpy2.count(), 1);
981 995
982 996 seriesSpyArg = seriesSpy.takeFirst();
983 997 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
984 998 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
985 999 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
986 1000
987 1001 setSpyArg = setSpy2.takeFirst();
988 1002 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
989 1003 QVERIFY(setSpyArg.at(0).toInt() == 0);
990 1004
991 1005 //====================================================================================
992 1006 // barset 2, bar 1
993 1007 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
994 1008 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
995 1009
996 1010 QCOMPARE(seriesSpy.count(), 1);
997 1011 QCOMPARE(setSpy1.count(), 0);
998 1012 QCOMPARE(setSpy2.count(), 1);
999 1013
1000 1014 seriesSpyArg = seriesSpy.takeFirst();
1001 1015 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1002 1016 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1003 1017 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1004 1018
1005 1019 setSpyArg = setSpy2.takeFirst();
1006 1020 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1007 1021 QVERIFY(setSpyArg.at(0).toInt() == 1);
1008 1022
1009 1023 //====================================================================================
1010 1024 // barset 2, bar 2
1011 1025 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
1012 1026 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1013 1027
1014 1028 QCOMPARE(seriesSpy.count(), 1);
1015 1029 QCOMPARE(setSpy1.count(), 0);
1016 1030 QCOMPARE(setSpy2.count(), 1);
1017 1031
1018 1032 seriesSpyArg = seriesSpy.takeFirst();
1019 1033 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1020 1034 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1021 1035 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1022 1036
1023 1037 setSpyArg = setSpy2.takeFirst();
1024 1038 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1025 1039 QVERIFY(setSpyArg.at(0).toInt() == 2);
1026 1040 }
1027 1041
1028 1042 void tst_QHorizontalBarSeries::mouseReleased()
1029 1043 {
1030 1044 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
1031 1045
1032 1046 QHorizontalBarSeries* series = new QHorizontalBarSeries();
1033 1047
1034 1048 QBarSet* set1 = new QBarSet(QString("set 1"));
1035 1049 *set1 << 10 << 10 << 10;
1036 1050 series->append(set1);
1037 1051
1038 1052 QBarSet* set2 = new QBarSet(QString("set 2"));
1039 1053 *set2 << 10 << 10 << 10;
1040 1054 series->append(set2);
1041 1055 QList<QBarSet*> barSets = series->barSets();
1042 1056
1043 1057 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
1044 1058 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
1045 1059 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
1046 1060
1047 1061 QChartView view(new QChart());
1048 1062 view.resize(400,300);
1049 1063 view.chart()->addSeries(series);
1050 1064 view.show();
1051 1065 QTest::qWaitForWindowShown(&view);
1052 1066
1053 1067 // Calculate expected layout for bars
1054 1068 QRectF plotArea = view.chart()->plotArea();
1055 1069 qreal width = plotArea.width();
1056 1070 qreal height = plotArea.height();
1057 1071 qreal rangeX = 10; // From 0 to 10 because of maximum value in set is 10
1058 1072 qreal rangeY = 3; // 3 values per set
1059 1073 qreal scaleY = (height / rangeY);
1060 1074 qreal scaleX = (width / rangeX);
1061 1075
1062 1076 qreal setCount = series->count();
1063 1077 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
1064 1078 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
1065 1079 qreal rectHeight = (scaleY / setCount) * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
1066 1080
1067 1081 QVector<QRectF> layout;
1068 1082
1069 1083 // 3 = count of values in set
1070 1084 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
1071 1085 for (int i = 0; i < 3; i++) {
1072 1086 qreal xPos = -scaleX * domainMinX + plotArea.left();
1073 1087 for (int set = 0; set < setCount; set++) {
1074 1088 qreal yPos = plotArea.bottom() + (domainMinY - i) * scaleY;
1075 1089 yPos += setCount*rectHeight/2;
1076 1090 yPos -= set*rectHeight;
1077 1091
1078 1092 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
1079 1093 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
1080 1094 layout.append(rect);
1081 1095 }
1082 1096 }
1083 1097
1084 1098 //====================================================================================
1085 1099 // barset 1, bar 0
1086 1100 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1087 1101 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1088 1102
1089 1103 QCOMPARE(seriesSpy.count(), 1);
1090 1104 QCOMPARE(setSpy1.count(), 1);
1091 1105 QCOMPARE(setSpy2.count(), 0);
1092 1106
1093 1107 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1094 1108 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1095 1109 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1096 1110 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1097 1111
1098 1112 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1099 1113 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1100 1114 QVERIFY(setSpyArg.at(0).toInt() == 0);
1101 1115
1102 1116 //====================================================================================
1103 1117 // barset 1, bar 1
1104 1118 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
1105 1119 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1106 1120
1107 1121 QCOMPARE(seriesSpy.count(), 1);
1108 1122 QCOMPARE(setSpy1.count(), 1);
1109 1123 QCOMPARE(setSpy2.count(), 0);
1110 1124
1111 1125 seriesSpyArg = seriesSpy.takeFirst();
1112 1126 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1113 1127 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1114 1128 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1115 1129
1116 1130 setSpyArg = setSpy1.takeFirst();
1117 1131 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1118 1132 QVERIFY(setSpyArg.at(0).toInt() == 1);
1119 1133
1120 1134 //====================================================================================
1121 1135 // barset 1, bar 2
1122 1136 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
1123 1137 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1124 1138
1125 1139 QCOMPARE(seriesSpy.count(), 1);
1126 1140 QCOMPARE(setSpy1.count(), 1);
1127 1141 QCOMPARE(setSpy2.count(), 0);
1128 1142
1129 1143 seriesSpyArg = seriesSpy.takeFirst();
1130 1144 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1131 1145 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1132 1146 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1133 1147
1134 1148 setSpyArg = setSpy1.takeFirst();
1135 1149 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1136 1150 QVERIFY(setSpyArg.at(0).toInt() == 2);
1137 1151
1138 1152 //====================================================================================
1139 1153 // barset 2, bar 0
1140 1154 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
1141 1155 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1142 1156
1143 1157 QCOMPARE(seriesSpy.count(), 1);
1144 1158 QCOMPARE(setSpy1.count(), 0);
1145 1159 QCOMPARE(setSpy2.count(), 1);
1146 1160
1147 1161 seriesSpyArg = seriesSpy.takeFirst();
1148 1162 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1149 1163 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1150 1164 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1151 1165
1152 1166 setSpyArg = setSpy2.takeFirst();
1153 1167 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1154 1168 QVERIFY(setSpyArg.at(0).toInt() == 0);
1155 1169
1156 1170 //====================================================================================
1157 1171 // barset 2, bar 1
1158 1172 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
1159 1173 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1160 1174
1161 1175 QCOMPARE(seriesSpy.count(), 1);
1162 1176 QCOMPARE(setSpy1.count(), 0);
1163 1177 QCOMPARE(setSpy2.count(), 1);
1164 1178
1165 1179 seriesSpyArg = seriesSpy.takeFirst();
1166 1180 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1167 1181 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1168 1182 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
1169 1183
1170 1184 setSpyArg = setSpy2.takeFirst();
1171 1185 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1172 1186 QVERIFY(setSpyArg.at(0).toInt() == 1);
1173 1187
1174 1188 //====================================================================================
1175 1189 // barset 2, bar 2
1176 1190 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
1177 1191 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1178 1192
1179 1193 QCOMPARE(seriesSpy.count(), 1);
1180 1194 QCOMPARE(setSpy1.count(), 0);
1181 1195 QCOMPARE(setSpy2.count(), 1);
1182 1196
1183 1197 seriesSpyArg = seriesSpy.takeFirst();
1184 1198 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
1185 1199 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1186 1200 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
1187 1201
1188 1202 setSpyArg = setSpy2.takeFirst();
1189 1203 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1190 1204 QVERIFY(setSpyArg.at(0).toInt() == 2);
1191 1205 }
1192 1206
1193 1207 void tst_QHorizontalBarSeries::mouseDoubleClicked()
1194 1208 {
1195 1209 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
1196 1210
1197 1211 QHorizontalBarSeries* series = new QHorizontalBarSeries();
1198 1212
1199 1213 QBarSet* set1 = new QBarSet(QString("set 1"));
1200 1214 *set1 << 10 << 10 << 10;
1201 1215 series->append(set1);
1202 1216
1203 1217 QBarSet* set2 = new QBarSet(QString("set 2"));
1204 1218 *set2 << 10 << 10 << 10;
1205 1219 series->append(set2);
1206 1220 QList<QBarSet*> barSets = series->barSets();
1207 1221
1208 1222 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
1209 1223 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
1210 1224 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
1211 1225
1212 1226 QChartView view(new QChart());
1213 1227 view.resize(400,300);
1214 1228 view.chart()->addSeries(series);
1215 1229 view.show();
1216 1230 QTest::qWaitForWindowShown(&view);
1217 1231
1218 1232 // Calculate expected layout for bars
1219 1233 QRectF plotArea = view.chart()->plotArea();
1220 1234 qreal width = plotArea.width();
1221 1235 qreal height = plotArea.height();
1222 1236 qreal rangeX = 10; // From 0 to 10 because of maximum value in set is 10
1223 1237 qreal rangeY = 3; // 3 values per set
1224 1238 qreal scaleY = (height / rangeY);
1225 1239 qreal scaleX = (width / rangeX);
1226 1240
1227 1241 qreal setCount = series->count();
1228 1242 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
1229 1243 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
1230 1244 qreal rectHeight = (scaleY / setCount) * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
1231 1245
1232 1246 QVector<QRectF> layout;
1233 1247
1234 1248 // 3 = count of values in set
1235 1249 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
1236 1250 for (int i = 0; i < 3; i++) {
1237 1251 qreal xPos = -scaleX * domainMinX + plotArea.left();
1238 1252 for (int set = 0; set < setCount; set++) {
1239 1253 qreal yPos = plotArea.bottom() + (domainMinY - i) * scaleY;
1240 1254 yPos += setCount*rectHeight/2;
1241 1255 yPos -= set*rectHeight;
1242 1256
1243 1257 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
1244 1258 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
1245 1259 layout.append(rect);
1246 1260 }
1247 1261 }
1248 1262
1249 1263 // barset 1, bar 0
1250 1264 QTest::mouseDClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1251 1265 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1252 1266
1253 1267 QCOMPARE(seriesSpy.count(), 1);
1254 1268 QCOMPARE(setSpy1.count(), 1);
1255 1269 QCOMPARE(setSpy2.count(), 0);
1256 1270
1257 1271 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1258 1272 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1259 1273 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1260 1274 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1261 1275
1262 1276 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1263 1277 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1264 1278 QVERIFY(setSpyArg.at(0).toInt() == 0);
1265 1279 }
1266 1280 QTEST_MAIN(tst_QHorizontalBarSeries)
1267 1281
1268 1282 #include "tst_qhorizontalbarseries.moc"
1269 1283
@@ -1,1016 +1,1030
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include <QtTest/QtTest>
20 20 #include <QtCharts/QHorizontalPercentBarSeries>
21 21 #include <QtCharts/QBarSet>
22 22 #include <QtCharts/QChartView>
23 23 #include <QtCharts/QChart>
24 24 #include "tst_definitions.h"
25 25
26 26 QT_CHARTS_USE_NAMESPACE
27 27
28 28 Q_DECLARE_METATYPE(QBarSet*)
29 29 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
30 30
31 31 class tst_QHorizontalPercentBarSeries : public QObject
32 32 {
33 33 Q_OBJECT
34 34
35 35 public slots:
36 36 void initTestCase();
37 37 void cleanupTestCase();
38 38 void init();
39 39 void cleanup();
40 40
41 41 private slots:
42 42 void qhorizontalpercentbarseries_data();
43 43 void qhorizontalpercentbarseries();
44 44 void type_data();
45 45 void type();
46 46 void setLabelsFormat();
47 47 void setLabelsPosition();
48 void setLabelsAngle();
48 49 void mouseclicked_data();
49 50 void mouseclicked();
50 51 void mousehovered_data();
51 52 void mousehovered();
52 53 void zeroValuesInSeries();
53 54 void mousePressed();
54 55 void mouseReleased();
55 56 void mouseDoubleClicked();
56 57
57 58 private:
58 59 QHorizontalPercentBarSeries* m_barseries;
59 60 };
60 61
61 62 void tst_QHorizontalPercentBarSeries::initTestCase()
62 63 {
63 64 qRegisterMetaType<QBarSet*>("QBarSet*");
64 65 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
65 66 }
66 67
67 68 void tst_QHorizontalPercentBarSeries::cleanupTestCase()
68 69 {
69 70 QTest::qWait(1); // Allow final deleteLaters to run
70 71 }
71 72
72 73 void tst_QHorizontalPercentBarSeries::init()
73 74 {
74 75 m_barseries = new QHorizontalPercentBarSeries();
75 76 }
76 77
77 78 void tst_QHorizontalPercentBarSeries::cleanup()
78 79 {
79 80 delete m_barseries;
80 81 m_barseries = 0;
81 82 }
82 83
83 84 void tst_QHorizontalPercentBarSeries::qhorizontalpercentbarseries_data()
84 85 {
85 86 }
86 87
87 88 void tst_QHorizontalPercentBarSeries::qhorizontalpercentbarseries()
88 89 {
89 90 QHorizontalPercentBarSeries *barseries = new QHorizontalPercentBarSeries();
90 91 QVERIFY(barseries != 0);
91 92 delete barseries;
92 93 }
93 94
94 95 void tst_QHorizontalPercentBarSeries::type_data()
95 96 {
96 97
97 98 }
98 99
99 100 void tst_QHorizontalPercentBarSeries::type()
100 101 {
101 102 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeHorizontalPercentBar);
102 103 }
103 104
104 105 void tst_QHorizontalPercentBarSeries::setLabelsFormat()
105 106 {
106 107 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
107 108 QCOMPARE(m_barseries->labelsFormat(), QString());
108 109
109 110 QString format("(@value)");
110 111 m_barseries->setLabelsFormat(format);
111 112 TRY_COMPARE(labelsFormatSpy.count(), 1);
112 113 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
113 114 QVERIFY(arguments.at(0).toString() == format);
114 115 QCOMPARE(m_barseries->labelsFormat(), format);
115 116
116 117 m_barseries->setLabelsFormat(QString());
117 118 TRY_COMPARE(labelsFormatSpy.count(), 1);
118 119 arguments = labelsFormatSpy.takeFirst();
119 120 QVERIFY(arguments.at(0).toString() == QString());
120 121 QCOMPARE(m_barseries->labelsFormat(), QString());
121 122 }
122 123
123 124 void tst_QHorizontalPercentBarSeries::setLabelsPosition()
124 125 {
125 126 QSignalSpy labelsPositionSpy(m_barseries,
126 127 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
127 128 QCOMPARE(m_barseries->labelsPosition(), QHorizontalPercentBarSeries::LabelsCenter);
128 129
129 130 m_barseries->setLabelsPosition(QHorizontalPercentBarSeries::LabelsInsideEnd);
130 131 TRY_COMPARE(labelsPositionSpy.count(), 1);
131 132 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
132 133 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
133 134 == QHorizontalPercentBarSeries::LabelsInsideEnd);
134 135 QCOMPARE(m_barseries->labelsPosition(), QHorizontalPercentBarSeries::LabelsInsideEnd);
135 136
136 137 m_barseries->setLabelsPosition(QHorizontalPercentBarSeries::LabelsInsideBase);
137 138 TRY_COMPARE(labelsPositionSpy.count(), 1);
138 139 arguments = labelsPositionSpy.takeFirst();
139 140 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
140 141 == QHorizontalPercentBarSeries::LabelsInsideBase);
141 142 QCOMPARE(m_barseries->labelsPosition(), QHorizontalPercentBarSeries::LabelsInsideBase);
142 143
143 144 m_barseries->setLabelsPosition(QHorizontalPercentBarSeries::LabelsOutsideEnd);
144 145 TRY_COMPARE(labelsPositionSpy.count(), 1);
145 146 arguments = labelsPositionSpy.takeFirst();
146 147 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
147 148 == QHorizontalPercentBarSeries::LabelsOutsideEnd);
148 149 QCOMPARE(m_barseries->labelsPosition(), QHorizontalPercentBarSeries::LabelsOutsideEnd);
149 150
150 151 m_barseries->setLabelsPosition(QHorizontalPercentBarSeries::LabelsCenter);
151 152 TRY_COMPARE(labelsPositionSpy.count(), 1);
152 153 arguments = labelsPositionSpy.takeFirst();
153 154 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
154 155 == QHorizontalPercentBarSeries::LabelsCenter);
155 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 172 void tst_QHorizontalPercentBarSeries::mouseclicked_data()
159 173 {
160 174
161 175 }
162 176
163 177 void tst_QHorizontalPercentBarSeries::mouseclicked()
164 178 {
165 179 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
166 180
167 181 QHorizontalPercentBarSeries* series = new QHorizontalPercentBarSeries();
168 182
169 183 QBarSet* set1 = new QBarSet(QString("set 1"));
170 184 *set1 << 10 << 10 << 10;
171 185 series->append(set1);
172 186
173 187 QBarSet* set2 = new QBarSet(QString("set 2"));
174 188 *set2 << 10 << 10 << 10;
175 189 series->append(set2);
176 190
177 191 QList<QBarSet*> barSets = series->barSets();
178 192
179 193 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
180 194
181 195 QChartView view(new QChart());
182 196 view.resize(400,300);
183 197 view.chart()->addSeries(series);
184 198 view.show();
185 199 QTest::qWaitForWindowShown(&view);
186 200
187 201 // Calculate expected layout for bars
188 202 QRectF plotArea = view.chart()->plotArea();
189 203 qreal width = plotArea.width();
190 204 qreal height = plotArea.height();
191 205 qreal rangeY = 3; // 3 values per set
192 206 qreal rangeX = 100; // From 0 to 100 because of scaling to 100%
193 207 qreal scaleY = (height / rangeY);
194 208 qreal scaleX = (width / rangeX);
195 209
196 210 qreal setCount = series->count();
197 211 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
198 212 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
199 213 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
200 214
201 215 QVector<QRectF> layout;
202 216
203 217 // 3 = count of values in set
204 218 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
205 219 for (int i = 0; i < 3; i++) {
206 220 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
207 221 qreal percentage = (100 / colSum);
208 222 qreal xPos = -scaleX * domainMinX + plotArea.left();
209 223 for (int set = 0; set < setCount; set++) {
210 224 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
211 225 qreal rectWidth = barSets.at(set)->at(i) * percentage * scaleX;
212 226 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
213 227 layout.append(rect);
214 228 xPos += rectWidth;
215 229 }
216 230 }
217 231
218 232 //====================================================================================
219 233 // barset 1, bar 0
220 234 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
221 235 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
222 236
223 237 QCOMPARE(seriesSpy.count(), 1);
224 238
225 239 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
226 240 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
227 241 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
228 242 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
229 243
230 244 //====================================================================================
231 245 // barset 1, bar 1
232 246 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
233 247 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
234 248
235 249 QCOMPARE(seriesSpy.count(), 1);
236 250
237 251 seriesSpyArg = seriesSpy.takeFirst();
238 252 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
239 253 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
240 254 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
241 255
242 256 //====================================================================================
243 257 // barset 1, bar 2
244 258 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
245 259 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
246 260
247 261 QCOMPARE(seriesSpy.count(), 1);
248 262
249 263 seriesSpyArg = seriesSpy.takeFirst();
250 264 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
251 265 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
252 266 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
253 267
254 268 //====================================================================================
255 269 // barset 2, bar 0
256 270 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
257 271 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
258 272
259 273 QCOMPARE(seriesSpy.count(), 1);
260 274
261 275 seriesSpyArg = seriesSpy.takeFirst();
262 276 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
263 277 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
264 278 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
265 279
266 280 //====================================================================================
267 281 // barset 2, bar 1
268 282 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
269 283 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
270 284
271 285 QCOMPARE(seriesSpy.count(), 1);
272 286
273 287 seriesSpyArg = seriesSpy.takeFirst();
274 288 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
275 289 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
276 290 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
277 291
278 292 //====================================================================================
279 293 // barset 2, bar 2
280 294 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
281 295 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
282 296
283 297 QCOMPARE(seriesSpy.count(), 1);
284 298
285 299 seriesSpyArg = seriesSpy.takeFirst();
286 300 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
287 301 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
288 302 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
289 303 }
290 304
291 305 void tst_QHorizontalPercentBarSeries::mousehovered_data()
292 306 {
293 307
294 308 }
295 309
296 310 void tst_QHorizontalPercentBarSeries::mousehovered()
297 311 {
298 312 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
299 313
300 314 QHorizontalPercentBarSeries* series = new QHorizontalPercentBarSeries();
301 315
302 316 QBarSet* set1 = new QBarSet(QString("set 1"));
303 317 *set1 << 10 << 10 << 10;
304 318 series->append(set1);
305 319
306 320 QBarSet* set2 = new QBarSet(QString("set 2"));
307 321 *set2 << 10 << 10 << 10;
308 322 series->append(set2);
309 323
310 324 QList<QBarSet*> barSets = series->barSets();
311 325
312 326 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
313 327 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
314 328 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
315 329
316 330 QChartView view(new QChart());
317 331 view.resize(400,300);
318 332 view.chart()->addSeries(series);
319 333 view.show();
320 334 QTest::qWaitForWindowShown(&view);
321 335
322 336 //this is hack since view does not get events otherwise
323 337 view.setMouseTracking(true);
324 338
325 339 // Calculate expected layout for bars
326 340 QRectF plotArea = view.chart()->plotArea();
327 341 qreal width = plotArea.width();
328 342 qreal height = plotArea.height();
329 343 qreal rangeY = 3; // 3 values per set
330 344 qreal rangeX = 100; // From 0 to 100 because of scaling to 100%
331 345 qreal scaleY = (height / rangeY);
332 346 qreal scaleX = (width / rangeX);
333 347
334 348 qreal setCount = series->count();
335 349 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
336 350 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
337 351 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
338 352
339 353 QVector<QRectF> layout;
340 354
341 355 // 3 = count of values in set
342 356 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
343 357 for (int i = 0; i < 3; i++) {
344 358 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
345 359 qreal percentage = (100 / colSum);
346 360 qreal xPos = -scaleX * domainMinX + plotArea.left();
347 361 for (int set = 0; set < setCount; set++) {
348 362 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
349 363 qreal rectWidth = barSets.at(set)->at(i) * percentage * scaleX;
350 364 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
351 365 layout.append(rect);
352 366 xPos += rectWidth;
353 367 }
354 368 }
355 369
356 370 //=======================================================================
357 371 // move mouse to left border
358 372 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(4).center().y()));
359 373 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
360 374 TRY_COMPARE(seriesIndexSpy.count(), 0);
361 375
362 376 //=======================================================================
363 377 // move mouse on top of set1
364 378 QTest::mouseMove(view.viewport(), layout.at(4).center().toPoint());
365 379 TRY_COMPARE(seriesIndexSpy.count(), 1);
366 380 TRY_COMPARE(setIndexSpy1.count(), 1);
367 381 TRY_COMPARE(setIndexSpy2.count(), 0);
368 382
369 383 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
370 384 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
371 385 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
372 386 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
373 387
374 388 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
375 389 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
376 390 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
377 391
378 392 //=======================================================================
379 393 // move mouse from top of set1 to top of set2
380 394 QTest::mouseMove(view.viewport(), layout.at(5).center().toPoint());
381 395 TRY_COMPARE(seriesIndexSpy.count(), 2);
382 396 TRY_COMPARE(setIndexSpy1.count(), 1);
383 397 TRY_COMPARE(setIndexSpy2.count(), 1);
384 398
385 399 // should leave set1
386 400 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
387 401 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
388 402 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
389 403 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
390 404
391 405 setIndexSpyArg = setIndexSpy1.takeFirst();
392 406 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
393 407 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
394 408
395 409 // should enter set2
396 410 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
397 411 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
398 412 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
399 413 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
400 414
401 415 setIndexSpyArg = setIndexSpy2.takeFirst();
402 416 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
403 417 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
404 418
405 419 //=======================================================================
406 420 // move mouse from top of set2 to background
407 421 QTest::mouseMove(view.viewport(), QPoint(layout.at(5).center().x(), 0));
408 422 TRY_COMPARE(seriesIndexSpy.count(), 1);
409 423 TRY_COMPARE(setIndexSpy1.count(), 0);
410 424 TRY_COMPARE(setIndexSpy2.count(), 1);
411 425
412 426 // should leave set2
413 427 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
414 428 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
415 429 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
416 430 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
417 431
418 432 setIndexSpyArg = setIndexSpy2.takeFirst();
419 433 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
420 434 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
421 435
422 436 //=======================================================================
423 437 // move mouse on top of set1, bar0 to check the index (hover into set1)
424 438 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
425 439
426 440 TRY_COMPARE(seriesIndexSpy.count(), 1);
427 441 TRY_COMPARE(setIndexSpy1.count(), 1);
428 442 TRY_COMPARE(setIndexSpy2.count(), 0);
429 443
430 444 //should enter set1, bar0
431 445 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
432 446 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
433 447 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
434 448 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
435 449 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
436 450 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
437 451
438 452 setIndexSpyArg = setIndexSpy1.takeFirst();
439 453 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
440 454 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
441 455 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
442 456 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
443 457
444 458 //=======================================================================
445 459 // move mouse on top of set2, bar0 to check the index (hover out set1,
446 460 // hover in set2)
447 461 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
448 462 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
449 463
450 464 TRY_COMPARE(seriesIndexSpy.count(), 2);
451 465 TRY_COMPARE(setIndexSpy1.count(), 1);
452 466 TRY_COMPARE(setIndexSpy2.count(), 1);
453 467
454 468 //should leave set1, bar0
455 469 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
456 470 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
457 471 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
458 472 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
459 473 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
460 474 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
461 475
462 476 setIndexSpyArg = setIndexSpy1.takeFirst();
463 477 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
464 478 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
465 479 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
466 480 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
467 481
468 482 //should enter set2, bar0
469 483 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
470 484 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
471 485 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
472 486 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
473 487 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
474 488 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
475 489
476 490 setIndexSpyArg = setIndexSpy2.takeFirst();
477 491 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
478 492 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
479 493 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
480 494 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
481 495
482 496 //=======================================================================
483 497 // move mouse on top of set1, bar1 to check the index (hover out set2,
484 498 // hover in set1)
485 499 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
486 500
487 501 TRY_COMPARE(seriesIndexSpy.count(), 2);
488 502 TRY_COMPARE(setIndexSpy1.count(), 1);
489 503 TRY_COMPARE(setIndexSpy2.count(), 1);
490 504
491 505 //should leave set2, bar0
492 506 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
493 507 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
494 508 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
495 509 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
496 510 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
497 511 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
498 512
499 513 setIndexSpyArg = setIndexSpy2.takeFirst();
500 514 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
501 515 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
502 516 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
503 517 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
504 518
505 519 //should enter set1, bar1
506 520 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
507 521 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
508 522 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
509 523 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
510 524 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
511 525 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
512 526
513 527 setIndexSpyArg = setIndexSpy1.takeFirst();
514 528 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
515 529 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
516 530 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
517 531 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
518 532
519 533 //=======================================================================
520 534 // move mouse between set1 and set2 (hover out set1)
521 535 QTest::mouseMove(view.viewport(), QPoint(layout.at(3).left(),
522 536 (layout.at(3).top() + layout.at(4).bottom()) / 2));
523 537
524 538 TRY_COMPARE(seriesIndexSpy.count(), 1);
525 539 TRY_COMPARE(setIndexSpy1.count(), 1);
526 540 TRY_COMPARE(setIndexSpy2.count(), 0);
527 541
528 542 //should leave set1, bar1
529 543 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
530 544 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
531 545 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
532 546 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
533 547 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
534 548 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
535 549
536 550 setIndexSpyArg = setIndexSpy1.takeFirst();
537 551 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
538 552 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
539 553 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
540 554 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
541 555
542 556 //=======================================================================
543 557 // move mouse on top of set2, bar1 to check the index (hover in set2)
544 558 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
545 559
546 560 TRY_COMPARE(seriesIndexSpy.count(), 1);
547 561 TRY_COMPARE(setIndexSpy1.count(), 0);
548 562 TRY_COMPARE(setIndexSpy2.count(), 1);
549 563
550 564 //should enter set2, bar1
551 565 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
552 566 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
553 567 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
554 568 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
555 569 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
556 570 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
557 571
558 572 setIndexSpyArg = setIndexSpy2.takeFirst();
559 573 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
560 574 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
561 575 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
562 576 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
563 577
564 578 //=======================================================================
565 579 // move mouse between set1 and set2 (hover out set2)
566 580 QTest::mouseMove(view.viewport(), QPoint(layout.at(3).left(),
567 581 (layout.at(3).top() + layout.at(4).bottom()) / 2));
568 582
569 583 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
570 584 TRY_COMPARE(seriesIndexSpy.count(), 1);
571 585 TRY_COMPARE(setIndexSpy1.count(), 0);
572 586 TRY_COMPARE(setIndexSpy2.count(), 1);
573 587
574 588 //should leave set1, bar1
575 589 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
576 590 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
577 591 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
578 592 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
579 593 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
580 594 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
581 595
582 596 setIndexSpyArg = setIndexSpy2.takeFirst();
583 597 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
584 598 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
585 599 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
586 600 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
587 601 }
588 602
589 603 void tst_QHorizontalPercentBarSeries::zeroValuesInSeries()
590 604 {
591 605 QHorizontalPercentBarSeries *series = new QHorizontalPercentBarSeries();
592 606 QBarSet *set1 = new QBarSet(QString("set 1"));
593 607 *set1 << 100 << 0.0 << 10;
594 608 series->append(set1);
595 609
596 610 QBarSet *set2 = new QBarSet(QString("set 2"));
597 611 *set2 << 0.0 << 0.0 << 70;
598 612 series->append(set2);
599 613
600 614 QChartView view(new QChart());
601 615 view.chart()->addSeries(series);
602 616 view.chart()->createDefaultAxes();
603 617 view.show();
604 618
605 619 QTest::qWaitForWindowShown(&view);
606 620 }
607 621
608 622
609 623 void tst_QHorizontalPercentBarSeries::mousePressed()
610 624 {
611 625 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
612 626
613 627 QHorizontalPercentBarSeries* series = new QHorizontalPercentBarSeries();
614 628
615 629 QBarSet* set1 = new QBarSet(QString("set 1"));
616 630 *set1 << 10 << 10 << 10;
617 631 series->append(set1);
618 632
619 633 QBarSet* set2 = new QBarSet(QString("set 2"));
620 634 *set2 << 10 << 10 << 10;
621 635 series->append(set2);
622 636 QList<QBarSet*> barSets = series->barSets();
623 637
624 638 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
625 639 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
626 640 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
627 641
628 642 QChartView view(new QChart());
629 643 view.resize(400,300);
630 644 view.chart()->addSeries(series);
631 645 view.show();
632 646 QTest::qWaitForWindowShown(&view);
633 647
634 648 // Calculate expected layout for bars
635 649 QRectF plotArea = view.chart()->plotArea();
636 650 qreal width = plotArea.width();
637 651 qreal height = plotArea.height();
638 652 qreal rangeX = 100; // From 0 to 100 because of scaling to 100%
639 653 qreal rangeY = 3; // 3 values per set
640 654 qreal scaleY = (height / rangeY);
641 655 qreal scaleX = (width / rangeX);
642 656
643 657 qreal setCount = series->count();
644 658 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
645 659 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
646 660 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
647 661
648 662 QVector<QRectF> layout;
649 663
650 664 // 3 = count of values in set
651 665 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
652 666 for (int i = 0; i < 3; i++) {
653 667 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
654 668 qreal percentage = (100 / colSum);
655 669 qreal xPos = -scaleX * domainMinX + plotArea.left();
656 670 for (int set = 0; set < setCount; set++) {
657 671 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
658 672 qreal rectWidth = barSets.at(set)->at(i) * percentage * scaleX;
659 673 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
660 674 layout.append(rect);
661 675 xPos += rectWidth;
662 676 }
663 677 }
664 678
665 679 //====================================================================================
666 680 // barset 1, bar 0
667 681 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
668 682 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
669 683
670 684 QCOMPARE(seriesSpy.count(), 1);
671 685 QCOMPARE(setSpy1.count(), 1);
672 686 QCOMPARE(setSpy2.count(), 0);
673 687
674 688 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
675 689 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
676 690 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
677 691 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
678 692
679 693 QList<QVariant> setSpyArg = setSpy1.takeFirst();
680 694 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
681 695 QVERIFY(setSpyArg.at(0).toInt() == 0);
682 696
683 697 //====================================================================================
684 698 // barset 1, bar 1
685 699 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
686 700 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
687 701
688 702 QCOMPARE(seriesSpy.count(), 1);
689 703 QCOMPARE(setSpy1.count(), 1);
690 704 QCOMPARE(setSpy2.count(), 0);
691 705
692 706 seriesSpyArg = seriesSpy.takeFirst();
693 707 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
694 708 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
695 709 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
696 710
697 711 setSpyArg = setSpy1.takeFirst();
698 712 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
699 713 QVERIFY(setSpyArg.at(0).toInt() == 1);
700 714
701 715 //====================================================================================
702 716 // barset 1, bar 2
703 717 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
704 718 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
705 719
706 720 QCOMPARE(seriesSpy.count(), 1);
707 721 QCOMPARE(setSpy1.count(), 1);
708 722 QCOMPARE(setSpy2.count(), 0);
709 723
710 724 seriesSpyArg = seriesSpy.takeFirst();
711 725 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
712 726 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
713 727 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
714 728
715 729 setSpyArg = setSpy1.takeFirst();
716 730 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
717 731 QVERIFY(setSpyArg.at(0).toInt() == 2);
718 732
719 733 //====================================================================================
720 734 // barset 2, bar 0
721 735 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
722 736 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
723 737
724 738 QCOMPARE(seriesSpy.count(), 1);
725 739 QCOMPARE(setSpy1.count(), 0);
726 740 QCOMPARE(setSpy2.count(), 1);
727 741
728 742 seriesSpyArg = seriesSpy.takeFirst();
729 743 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
730 744 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
731 745 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
732 746
733 747 setSpyArg = setSpy2.takeFirst();
734 748 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
735 749 QVERIFY(setSpyArg.at(0).toInt() == 0);
736 750
737 751 //====================================================================================
738 752 // barset 2, bar 1
739 753 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
740 754 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
741 755
742 756 QCOMPARE(seriesSpy.count(), 1);
743 757 QCOMPARE(setSpy1.count(), 0);
744 758 QCOMPARE(setSpy2.count(), 1);
745 759
746 760 seriesSpyArg = seriesSpy.takeFirst();
747 761 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
748 762 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
749 763 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
750 764
751 765 setSpyArg = setSpy2.takeFirst();
752 766 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
753 767 QVERIFY(setSpyArg.at(0).toInt() == 1);
754 768
755 769 //====================================================================================
756 770 // barset 2, bar 2
757 771 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
758 772 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
759 773
760 774 QCOMPARE(seriesSpy.count(), 1);
761 775 QCOMPARE(setSpy1.count(), 0);
762 776 QCOMPARE(setSpy2.count(), 1);
763 777
764 778 seriesSpyArg = seriesSpy.takeFirst();
765 779 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
766 780 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
767 781 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
768 782
769 783 setSpyArg = setSpy2.takeFirst();
770 784 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
771 785 QVERIFY(setSpyArg.at(0).toInt() == 2);
772 786 }
773 787
774 788 void tst_QHorizontalPercentBarSeries::mouseReleased()
775 789 {
776 790 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
777 791
778 792 QHorizontalPercentBarSeries* series = new QHorizontalPercentBarSeries();
779 793
780 794 QBarSet* set1 = new QBarSet(QString("set 1"));
781 795 *set1 << 10 << 10 << 10;
782 796 series->append(set1);
783 797
784 798 QBarSet* set2 = new QBarSet(QString("set 2"));
785 799 *set2 << 10 << 10 << 10;
786 800 series->append(set2);
787 801 QList<QBarSet*> barSets = series->barSets();
788 802
789 803 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
790 804 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
791 805 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
792 806
793 807 QChartView view(new QChart());
794 808 view.resize(400,300);
795 809 view.chart()->addSeries(series);
796 810 view.show();
797 811 QTest::qWaitForWindowShown(&view);
798 812
799 813 // Calculate expected layout for bars
800 814 QRectF plotArea = view.chart()->plotArea();
801 815 qreal width = plotArea.width();
802 816 qreal height = plotArea.height();
803 817 qreal rangeX = 100; // From 0 to 100 because of scaling to 100%
804 818 qreal rangeY = 3; // 3 values per set
805 819 qreal scaleY = (height / rangeY);
806 820 qreal scaleX = (width / rangeX);
807 821
808 822 qreal setCount = series->count();
809 823 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
810 824 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
811 825 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
812 826
813 827 QVector<QRectF> layout;
814 828
815 829 // 3 = count of values in set
816 830 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
817 831 for (int i = 0; i < 3; i++) {
818 832 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
819 833 qreal percentage = (100 / colSum);
820 834 qreal xPos = -scaleX * domainMinX + plotArea.left();
821 835 for (int set = 0; set < setCount; set++) {
822 836 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
823 837 qreal rectWidth = barSets.at(set)->at(i) * percentage * scaleX;
824 838 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
825 839 layout.append(rect);
826 840 xPos += rectWidth;
827 841 }
828 842 }
829 843
830 844 //====================================================================================
831 845 // barset 1, bar 0
832 846 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
833 847 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
834 848
835 849 QCOMPARE(seriesSpy.count(), 1);
836 850 QCOMPARE(setSpy1.count(), 1);
837 851 QCOMPARE(setSpy2.count(), 0);
838 852
839 853 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
840 854 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
841 855 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
842 856 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
843 857
844 858 QList<QVariant> setSpyArg = setSpy1.takeFirst();
845 859 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
846 860 QVERIFY(setSpyArg.at(0).toInt() == 0);
847 861
848 862 //====================================================================================
849 863 // barset 1, bar 1
850 864 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
851 865 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
852 866
853 867 QCOMPARE(seriesSpy.count(), 1);
854 868 QCOMPARE(setSpy1.count(), 1);
855 869 QCOMPARE(setSpy2.count(), 0);
856 870
857 871 seriesSpyArg = seriesSpy.takeFirst();
858 872 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
859 873 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
860 874 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
861 875
862 876 setSpyArg = setSpy1.takeFirst();
863 877 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
864 878 QVERIFY(setSpyArg.at(0).toInt() == 1);
865 879
866 880 //====================================================================================
867 881 // barset 1, bar 2
868 882 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
869 883 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
870 884
871 885 QCOMPARE(seriesSpy.count(), 1);
872 886 QCOMPARE(setSpy1.count(), 1);
873 887 QCOMPARE(setSpy2.count(), 0);
874 888
875 889 seriesSpyArg = seriesSpy.takeFirst();
876 890 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
877 891 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
878 892 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
879 893
880 894 setSpyArg = setSpy1.takeFirst();
881 895 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
882 896 QVERIFY(setSpyArg.at(0).toInt() == 2);
883 897
884 898 //====================================================================================
885 899 // barset 2, bar 0
886 900 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
887 901 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
888 902
889 903 QCOMPARE(seriesSpy.count(), 1);
890 904 QCOMPARE(setSpy1.count(), 0);
891 905 QCOMPARE(setSpy2.count(), 1);
892 906
893 907 seriesSpyArg = seriesSpy.takeFirst();
894 908 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
895 909 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
896 910 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
897 911
898 912 setSpyArg = setSpy2.takeFirst();
899 913 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
900 914 QVERIFY(setSpyArg.at(0).toInt() == 0);
901 915
902 916 //====================================================================================
903 917 // barset 2, bar 1
904 918 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
905 919 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
906 920
907 921 QCOMPARE(seriesSpy.count(), 1);
908 922 QCOMPARE(setSpy1.count(), 0);
909 923 QCOMPARE(setSpy2.count(), 1);
910 924
911 925 seriesSpyArg = seriesSpy.takeFirst();
912 926 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
913 927 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
914 928 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
915 929
916 930 setSpyArg = setSpy2.takeFirst();
917 931 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
918 932 QVERIFY(setSpyArg.at(0).toInt() == 1);
919 933
920 934 //====================================================================================
921 935 // barset 2, bar 2
922 936 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
923 937 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
924 938
925 939 QCOMPARE(seriesSpy.count(), 1);
926 940 QCOMPARE(setSpy1.count(), 0);
927 941 QCOMPARE(setSpy2.count(), 1);
928 942
929 943 seriesSpyArg = seriesSpy.takeFirst();
930 944 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
931 945 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
932 946 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
933 947
934 948 setSpyArg = setSpy2.takeFirst();
935 949 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
936 950 QVERIFY(setSpyArg.at(0).toInt() == 2);
937 951 }
938 952
939 953 void tst_QHorizontalPercentBarSeries::mouseDoubleClicked()
940 954 {
941 955 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
942 956
943 957 QHorizontalPercentBarSeries* series = new QHorizontalPercentBarSeries();
944 958
945 959 QBarSet* set1 = new QBarSet(QString("set 1"));
946 960 *set1 << 10 << 10 << 10;
947 961 series->append(set1);
948 962
949 963 QBarSet* set2 = new QBarSet(QString("set 2"));
950 964 *set2 << 10 << 10 << 10;
951 965 series->append(set2);
952 966 QList<QBarSet*> barSets = series->barSets();
953 967
954 968 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
955 969 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
956 970 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
957 971
958 972 QChartView view(new QChart());
959 973 view.resize(400,300);
960 974 view.chart()->addSeries(series);
961 975 view.show();
962 976 QTest::qWaitForWindowShown(&view);
963 977
964 978 // Calculate expected layout for bars
965 979 QRectF plotArea = view.chart()->plotArea();
966 980 qreal width = plotArea.width();
967 981 qreal height = plotArea.height();
968 982 qreal rangeX = 100; // From 0 to 100 because of scaling to 100%
969 983 qreal rangeY = 3; // 3 values per set
970 984 qreal scaleY = (height / rangeY);
971 985 qreal scaleX = (width / rangeX);
972 986
973 987 qreal setCount = series->count();
974 988 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
975 989 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
976 990 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
977 991
978 992 QVector<QRectF> layout;
979 993
980 994 // 3 = count of values in set
981 995 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
982 996 for (int i = 0; i < 3; i++) {
983 997 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
984 998 qreal percentage = (100 / colSum);
985 999 qreal xPos = -scaleX * domainMinX + plotArea.left();
986 1000 for (int set = 0; set < setCount; set++) {
987 1001 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
988 1002 qreal rectWidth = barSets.at(set)->at(i) * percentage * scaleX;
989 1003 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
990 1004 layout.append(rect);
991 1005 xPos += rectWidth;
992 1006 }
993 1007 }
994 1008
995 1009 // barset 1, bar 0
996 1010 QTest::mouseDClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
997 1011 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
998 1012
999 1013 QCOMPARE(seriesSpy.count(), 1);
1000 1014 QCOMPARE(setSpy1.count(), 1);
1001 1015 QCOMPARE(setSpy2.count(), 0);
1002 1016
1003 1017 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1004 1018 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1005 1019 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1006 1020 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1007 1021
1008 1022 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1009 1023 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1010 1024 QVERIFY(setSpyArg.at(0).toInt() == 0);
1011 1025 }
1012 1026
1013 1027 QTEST_MAIN(tst_QHorizontalPercentBarSeries)
1014 1028
1015 1029 #include "tst_qhorizontalpercentbarseries.moc"
1016 1030
@@ -1,1019 +1,1033
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include <QtTest/QtTest>
20 20 #include <QtCharts/QHorizontalStackedBarSeries>
21 21 #include <QtCharts/QBarSet>
22 22 #include <QtCharts/QChartView>
23 23 #include <QtCharts/QChart>
24 24 #include "tst_definitions.h"
25 25
26 26 QT_CHARTS_USE_NAMESPACE
27 27
28 28 Q_DECLARE_METATYPE(QBarSet*)
29 29 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
30 30
31 31 class tst_QHorizontalStackedBarSeries : public QObject
32 32 {
33 33 Q_OBJECT
34 34
35 35 public slots:
36 36 void initTestCase();
37 37 void cleanupTestCase();
38 38 void init();
39 39 void cleanup();
40 40
41 41 private slots:
42 42 void qhorizontalstackedbarseries_data();
43 43 void qhorizontalstackedbarseries();
44 44 void type_data();
45 45 void type();
46 46 void setLabelsFormat();
47 47 void setLabelsPosition();
48 void setLabelsAngle();
48 49 void mouseclicked_data();
49 50 void mouseclicked();
50 51 void mousehovered_data();
51 52 void mousehovered();
52 53 void mousePressed();
53 54 void mouseReleased();
54 55 void mouseDoubleClicked();
55 56
56 57 private:
57 58 QHorizontalStackedBarSeries* m_barseries;
58 59 };
59 60
60 61 void tst_QHorizontalStackedBarSeries::initTestCase()
61 62 {
62 63 qRegisterMetaType<QBarSet*>("QBarSet*");
63 64 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
64 65 }
65 66
66 67 void tst_QHorizontalStackedBarSeries::cleanupTestCase()
67 68 {
68 69 QTest::qWait(1); // Allow final deleteLaters to run
69 70 }
70 71
71 72 void tst_QHorizontalStackedBarSeries::init()
72 73 {
73 74 m_barseries = new QHorizontalStackedBarSeries();
74 75 }
75 76
76 77 void tst_QHorizontalStackedBarSeries::cleanup()
77 78 {
78 79 delete m_barseries;
79 80 m_barseries = 0;
80 81 }
81 82
82 83 void tst_QHorizontalStackedBarSeries::qhorizontalstackedbarseries_data()
83 84 {
84 85 }
85 86
86 87 void tst_QHorizontalStackedBarSeries::qhorizontalstackedbarseries()
87 88 {
88 89 QHorizontalStackedBarSeries *barseries = new QHorizontalStackedBarSeries();
89 90 QVERIFY(barseries != 0);
90 91 delete barseries;
91 92 }
92 93
93 94 void tst_QHorizontalStackedBarSeries::type_data()
94 95 {
95 96
96 97 }
97 98
98 99 void tst_QHorizontalStackedBarSeries::type()
99 100 {
100 101 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeHorizontalStackedBar);
101 102 }
102 103
103 104 void tst_QHorizontalStackedBarSeries::setLabelsFormat()
104 105 {
105 106 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
106 107 QCOMPARE(m_barseries->labelsFormat(), QString());
107 108
108 109 QString format("(@value)");
109 110 m_barseries->setLabelsFormat(format);
110 111 TRY_COMPARE(labelsFormatSpy.count(), 1);
111 112 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
112 113 QVERIFY(arguments.at(0).toString() == format);
113 114 QCOMPARE(m_barseries->labelsFormat(), format);
114 115
115 116 m_barseries->setLabelsFormat(QString());
116 117 TRY_COMPARE(labelsFormatSpy.count(), 1);
117 118 arguments = labelsFormatSpy.takeFirst();
118 119 QVERIFY(arguments.at(0).toString() == QString());
119 120 QCOMPARE(m_barseries->labelsFormat(), QString());
120 121 }
121 122
122 123 void tst_QHorizontalStackedBarSeries::setLabelsPosition()
123 124 {
124 125 QSignalSpy labelsPositionSpy(m_barseries,
125 126 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
126 127 QCOMPARE(m_barseries->labelsPosition(), QHorizontalStackedBarSeries::LabelsCenter);
127 128
128 129 m_barseries->setLabelsPosition(QHorizontalStackedBarSeries::LabelsInsideEnd);
129 130 TRY_COMPARE(labelsPositionSpy.count(), 1);
130 131 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
131 132 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
132 133 == QHorizontalStackedBarSeries::LabelsInsideEnd);
133 134 QCOMPARE(m_barseries->labelsPosition(), QHorizontalStackedBarSeries::LabelsInsideEnd);
134 135
135 136 m_barseries->setLabelsPosition(QHorizontalStackedBarSeries::LabelsInsideBase);
136 137 TRY_COMPARE(labelsPositionSpy.count(), 1);
137 138 arguments = labelsPositionSpy.takeFirst();
138 139 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
139 140 == QHorizontalStackedBarSeries::LabelsInsideBase);
140 141 QCOMPARE(m_barseries->labelsPosition(), QHorizontalStackedBarSeries::LabelsInsideBase);
141 142
142 143 m_barseries->setLabelsPosition(QHorizontalStackedBarSeries::LabelsOutsideEnd);
143 144 TRY_COMPARE(labelsPositionSpy.count(), 1);
144 145 arguments = labelsPositionSpy.takeFirst();
145 146 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
146 147 == QHorizontalStackedBarSeries::LabelsOutsideEnd);
147 148 QCOMPARE(m_barseries->labelsPosition(), QHorizontalStackedBarSeries::LabelsOutsideEnd);
148 149
149 150 m_barseries->setLabelsPosition(QHorizontalStackedBarSeries::LabelsCenter);
150 151 TRY_COMPARE(labelsPositionSpy.count(), 1);
151 152 arguments = labelsPositionSpy.takeFirst();
152 153 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
153 154 == QHorizontalStackedBarSeries::LabelsCenter);
154 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 171 void tst_QHorizontalStackedBarSeries::mouseclicked_data()
158 172 {
159 173
160 174 }
161 175
162 176 void tst_QHorizontalStackedBarSeries::mouseclicked()
163 177 {
164 178 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
165 179
166 180 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
167 181
168 182 QBarSet* set1 = new QBarSet(QString("set 1"));
169 183 *set1 << 10 << 10 << 10;
170 184 series->append(set1);
171 185
172 186 QBarSet* set2 = new QBarSet(QString("set 2"));
173 187 *set2 << 10 << 10 << 10;
174 188 series->append(set2);
175 189
176 190 QList<QBarSet*> barSets = series->barSets();
177 191
178 192 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
179 193
180 194 QChartView view(new QChart());
181 195 view.resize(400,300);
182 196 view.chart()->addSeries(series);
183 197 view.show();
184 198 QTest::qWaitForWindowShown(&view);
185 199
186 200 // Calculate expected layout for bars
187 201 QRectF plotArea = view.chart()->plotArea();
188 202 qreal width = plotArea.width();
189 203 qreal height = plotArea.height();
190 204 qreal rangeY = 3; // 3 values per set
191 205 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
192 206 qreal scaleY = (height / rangeY);
193 207 qreal scaleX = (width / rangeX);
194 208
195 209 qreal setCount = series->count();
196 210 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
197 211 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
198 212 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
199 213
200 214 QVector<QRectF> layout;
201 215
202 216 // 3 = count of values in set
203 217 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
204 218 for (int i = 0; i < 3; i++) {
205 219 qreal xMax = -scaleX * domainMinX + plotArea.left();
206 220 qreal xMin = -scaleX * domainMinX + plotArea.left();
207 221 for (int set = 0; set < setCount; set++) {
208 222 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
209 223 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
210 224 if (rectWidth > 0) {
211 225 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
212 226 layout.append(rect);
213 227 xMax += rectWidth;
214 228 } else {
215 229 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
216 230 layout.append(rect);
217 231 xMin += rectWidth;
218 232 }
219 233 }
220 234 }
221 235
222 236 //====================================================================================
223 237 // barset 1, bar 0
224 238 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
225 239 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
226 240
227 241 QCOMPARE(seriesSpy.count(), 1);
228 242
229 243 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
230 244 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
231 245 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
232 246 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
233 247
234 248 //====================================================================================
235 249 // barset 1, bar 1
236 250 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
237 251 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
238 252
239 253 QCOMPARE(seriesSpy.count(), 1);
240 254
241 255 seriesSpyArg = seriesSpy.takeFirst();
242 256 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
243 257 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
244 258 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
245 259
246 260 //====================================================================================
247 261 // barset 1, bar 2
248 262 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
249 263 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
250 264
251 265 QCOMPARE(seriesSpy.count(), 1);
252 266
253 267 seriesSpyArg = seriesSpy.takeFirst();
254 268 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
255 269 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
256 270 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
257 271
258 272 //====================================================================================
259 273 // barset 2, bar 0
260 274 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
261 275 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
262 276
263 277 QCOMPARE(seriesSpy.count(), 1);
264 278
265 279 seriesSpyArg = seriesSpy.takeFirst();
266 280 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
267 281 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
268 282 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
269 283
270 284 //====================================================================================
271 285 // barset 2, bar 1
272 286 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
273 287 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
274 288
275 289 QCOMPARE(seriesSpy.count(), 1);
276 290
277 291 seriesSpyArg = seriesSpy.takeFirst();
278 292 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
279 293 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
280 294 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
281 295
282 296 //====================================================================================
283 297 // barset 2, bar 2
284 298 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
285 299 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
286 300
287 301 QCOMPARE(seriesSpy.count(), 1);
288 302
289 303 seriesSpyArg = seriesSpy.takeFirst();
290 304 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
291 305 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
292 306 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
293 307 }
294 308
295 309 void tst_QHorizontalStackedBarSeries::mousehovered_data()
296 310 {
297 311
298 312 }
299 313
300 314 void tst_QHorizontalStackedBarSeries::mousehovered()
301 315 {
302 316 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
303 317
304 318 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
305 319
306 320 QBarSet* set1 = new QBarSet(QString("set 1"));
307 321 *set1 << 10 << 10 << 10;
308 322 series->append(set1);
309 323
310 324 QBarSet* set2 = new QBarSet(QString("set 2"));
311 325 *set2 << 10 << 10 << 10;
312 326 series->append(set2);
313 327
314 328 QList<QBarSet*> barSets = series->barSets();
315 329
316 330 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
317 331 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
318 332 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
319 333
320 334 QChartView view(new QChart());
321 335 view.resize(400,300);
322 336 view.chart()->addSeries(series);
323 337 view.show();
324 338 QTest::qWaitForWindowShown(&view);
325 339
326 340 //this is hack since view does not get events otherwise
327 341 view.setMouseTracking(true);
328 342
329 343 // Calculate expected layout for bars
330 344 QRectF plotArea = view.chart()->plotArea();
331 345 qreal width = plotArea.width();
332 346 qreal height = plotArea.height();
333 347 qreal rangeY = 3; // 3 values per set
334 348 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
335 349 qreal scaleY = (height / rangeY);
336 350 qreal scaleX = (width / rangeX);
337 351
338 352 qreal setCount = series->count();
339 353 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
340 354 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
341 355 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
342 356
343 357 QVector<QRectF> layout;
344 358
345 359 // 3 = count of values in set
346 360 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
347 361 for (int i = 0; i < 3; i++) {
348 362 qreal xMax = -scaleX * domainMinX + plotArea.left();
349 363 qreal xMin = -scaleX * domainMinX + plotArea.left();
350 364 for (int set = 0; set < setCount; set++) {
351 365 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
352 366 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
353 367 if (rectWidth > 0) {
354 368 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
355 369 layout.append(rect);
356 370 xMax += rectWidth;
357 371 } else {
358 372 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
359 373 layout.append(rect);
360 374 xMin += rectWidth;
361 375 }
362 376 }
363 377 }
364 378
365 379 //=======================================================================
366 380 // move mouse to left border
367 381 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(4).center().y()));
368 382 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
369 383 TRY_COMPARE(seriesIndexSpy.count(), 0);
370 384
371 385 //=======================================================================
372 386 // move mouse on top of set1
373 387 QTest::mouseMove(view.viewport(), layout.at(4).center().toPoint());
374 388 TRY_COMPARE(seriesIndexSpy.count(), 1);
375 389 TRY_COMPARE(setIndexSpy1.count(), 1);
376 390 TRY_COMPARE(setIndexSpy2.count(), 0);
377 391
378 392 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
379 393 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
380 394 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
381 395 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
382 396
383 397 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
384 398 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
385 399 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
386 400
387 401 //=======================================================================
388 402 // move mouse from top of set1 to top of set2
389 403 QTest::mouseMove(view.viewport(), layout.at(5).center().toPoint());
390 404 TRY_COMPARE(seriesIndexSpy.count(), 2);
391 405 TRY_COMPARE(setIndexSpy1.count(), 1);
392 406 TRY_COMPARE(setIndexSpy2.count(), 1);
393 407
394 408 // should leave set1
395 409 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
396 410 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
397 411 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
398 412 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
399 413
400 414 setIndexSpyArg = setIndexSpy1.takeFirst();
401 415 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
402 416 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
403 417
404 418 // should enter set2
405 419 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
406 420 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
407 421 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
408 422 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
409 423
410 424 setIndexSpyArg = setIndexSpy2.takeFirst();
411 425 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
412 426 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
413 427
414 428 //=======================================================================
415 429 // move mouse from top of set2 to background
416 430 QTest::mouseMove(view.viewport(), QPoint(layout.at(5).center().y(), 0));
417 431 TRY_COMPARE(seriesIndexSpy.count(), 1);
418 432 TRY_COMPARE(setIndexSpy1.count(), 0);
419 433 TRY_COMPARE(setIndexSpy2.count(), 1);
420 434
421 435 // should leave set2
422 436 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
423 437 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
424 438 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
425 439 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
426 440
427 441 setIndexSpyArg = setIndexSpy2.takeFirst();
428 442 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
429 443 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
430 444
431 445 //=======================================================================
432 446 // move mouse on top of set1, bar0 to check the index (hover into set1)
433 447 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
434 448
435 449 TRY_COMPARE(seriesIndexSpy.count(), 1);
436 450 TRY_COMPARE(setIndexSpy1.count(), 1);
437 451 TRY_COMPARE(setIndexSpy2.count(), 0);
438 452
439 453 //should enter set1, bar0
440 454 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
441 455 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
442 456 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
443 457 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
444 458 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
445 459 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
446 460
447 461 setIndexSpyArg = setIndexSpy1.takeFirst();
448 462 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
449 463 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
450 464 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
451 465 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
452 466
453 467 //=======================================================================
454 468 // move mouse on top of set2, bar0 to check the index (hover out set1,
455 469 // hover in set2)
456 470 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
457 471 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
458 472
459 473 TRY_COMPARE(seriesIndexSpy.count(), 2);
460 474 TRY_COMPARE(setIndexSpy1.count(), 1);
461 475 TRY_COMPARE(setIndexSpy2.count(), 1);
462 476
463 477 //should leave set1, bar0
464 478 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
465 479 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
466 480 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
467 481 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
468 482 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
469 483 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
470 484
471 485 setIndexSpyArg = setIndexSpy1.takeFirst();
472 486 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
473 487 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
474 488 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
475 489 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
476 490
477 491 //should enter set2, bar0
478 492 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
479 493 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
480 494 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
481 495 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
482 496 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
483 497 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
484 498
485 499 setIndexSpyArg = setIndexSpy2.takeFirst();
486 500 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
487 501 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
488 502 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
489 503 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
490 504
491 505 //=======================================================================
492 506 // move mouse on top of set1, bar1 to check the index (hover out set2,
493 507 // hover in set1)
494 508 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
495 509
496 510 TRY_COMPARE(seriesIndexSpy.count(), 2);
497 511 TRY_COMPARE(setIndexSpy1.count(), 1);
498 512 TRY_COMPARE(setIndexSpy2.count(), 1);
499 513
500 514 //should leave set2, bar0
501 515 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
502 516 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
503 517 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
504 518 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
505 519 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
506 520 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
507 521
508 522 setIndexSpyArg = setIndexSpy2.takeFirst();
509 523 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
510 524 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
511 525 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
512 526 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
513 527
514 528 //should enter set1, bar1
515 529 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
516 530 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
517 531 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
518 532 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
519 533 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
520 534 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
521 535
522 536 setIndexSpyArg = setIndexSpy1.takeFirst();
523 537 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
524 538 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
525 539 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
526 540 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
527 541
528 542 //=======================================================================
529 543 // move mouse between set1 and set2 (hover out set1)
530 544 QTest::mouseMove(view.viewport(), QPoint(layout.at(3).left(),
531 545 (layout.at(3).top() + layout.at(4).bottom()) / 2));
532 546
533 547 TRY_COMPARE(seriesIndexSpy.count(), 1);
534 548 TRY_COMPARE(setIndexSpy1.count(), 1);
535 549 TRY_COMPARE(setIndexSpy2.count(), 0);
536 550
537 551 //should leave set1, bar1
538 552 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
539 553 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
540 554 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
541 555 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
542 556 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
543 557 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
544 558
545 559 setIndexSpyArg = setIndexSpy1.takeFirst();
546 560 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
547 561 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
548 562 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
549 563 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
550 564
551 565 //=======================================================================
552 566 // move mouse on top of set2, bar1 to check the index (hover in set2)
553 567 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
554 568
555 569 TRY_COMPARE(seriesIndexSpy.count(), 1);
556 570 TRY_COMPARE(setIndexSpy1.count(), 0);
557 571 TRY_COMPARE(setIndexSpy2.count(), 1);
558 572
559 573 //should enter set2, bar1
560 574 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
561 575 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
562 576 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
563 577 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
564 578 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
565 579 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
566 580
567 581 setIndexSpyArg = setIndexSpy2.takeFirst();
568 582 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
569 583 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
570 584 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
571 585 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
572 586
573 587 //=======================================================================
574 588 // move mouse between set1 and set2 (hover out set2)
575 589 QTest::mouseMove(view.viewport(), QPoint(layout.at(3).left(),
576 590 (layout.at(3).top() + layout.at(4).bottom()) / 2));
577 591
578 592 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
579 593 TRY_COMPARE(seriesIndexSpy.count(), 1);
580 594 TRY_COMPARE(setIndexSpy1.count(), 0);
581 595 TRY_COMPARE(setIndexSpy2.count(), 1);
582 596
583 597 //should leave set1, bar1
584 598 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
585 599 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
586 600 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
587 601 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
588 602 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
589 603 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
590 604
591 605 setIndexSpyArg = setIndexSpy2.takeFirst();
592 606 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
593 607 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
594 608 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
595 609 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
596 610 }
597 611
598 612 void tst_QHorizontalStackedBarSeries::mousePressed()
599 613 {
600 614 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
601 615
602 616 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
603 617
604 618 QBarSet* set1 = new QBarSet(QString("set 1"));
605 619 *set1 << 10 << 10 << 10;
606 620 series->append(set1);
607 621
608 622 QBarSet* set2 = new QBarSet(QString("set 2"));
609 623 *set2 << 10 << 10 << 10;
610 624 series->append(set2);
611 625 QList<QBarSet*> barSets = series->barSets();
612 626
613 627 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
614 628 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
615 629 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
616 630
617 631 QChartView view(new QChart());
618 632 view.resize(400,300);
619 633 view.chart()->addSeries(series);
620 634 view.show();
621 635 QTest::qWaitForWindowShown(&view);
622 636
623 637 // Calculate expected layout for bars
624 638 QRectF plotArea = view.chart()->plotArea();
625 639 qreal width = plotArea.width();
626 640 qreal height = plotArea.height();
627 641 qreal rangeY = 3; // 3 values per set
628 642 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
629 643 qreal scaleY = (height / rangeY);
630 644 qreal scaleX = (width / rangeX);
631 645
632 646 qreal setCount = series->count();
633 647 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
634 648 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
635 649 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
636 650
637 651 QVector<QRectF> layout;
638 652
639 653 // 3 = count of values in set
640 654 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
641 655 for (int i = 0; i < 3; i++) {
642 656 qreal xMax = -scaleX * domainMinX + plotArea.left();
643 657 qreal xMin = -scaleX * domainMinX + plotArea.left();
644 658 for (int set = 0; set < setCount; set++) {
645 659 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
646 660 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
647 661 if (rectWidth > 0) {
648 662 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
649 663 layout.append(rect);
650 664 xMax += rectWidth;
651 665 } else {
652 666 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
653 667 layout.append(rect);
654 668 xMin += rectWidth;
655 669 }
656 670 }
657 671 }
658 672
659 673 //====================================================================================
660 674 // barset 1, bar 0
661 675 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
662 676 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
663 677
664 678 QCOMPARE(seriesSpy.count(), 1);
665 679 QCOMPARE(setSpy1.count(), 1);
666 680 QCOMPARE(setSpy2.count(), 0);
667 681
668 682 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
669 683 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
670 684 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
671 685 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
672 686
673 687 QList<QVariant> setSpyArg = setSpy1.takeFirst();
674 688 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
675 689 QVERIFY(setSpyArg.at(0).toInt() == 0);
676 690
677 691 //====================================================================================
678 692 // barset 1, bar 1
679 693 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
680 694 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
681 695
682 696 QCOMPARE(seriesSpy.count(), 1);
683 697 QCOMPARE(setSpy1.count(), 1);
684 698 QCOMPARE(setSpy2.count(), 0);
685 699
686 700 seriesSpyArg = seriesSpy.takeFirst();
687 701 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
688 702 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
689 703 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
690 704
691 705 setSpyArg = setSpy1.takeFirst();
692 706 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
693 707 QVERIFY(setSpyArg.at(0).toInt() == 1);
694 708
695 709 //====================================================================================
696 710 // barset 1, bar 2
697 711 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
698 712 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
699 713
700 714 QCOMPARE(seriesSpy.count(), 1);
701 715 QCOMPARE(setSpy1.count(), 1);
702 716 QCOMPARE(setSpy2.count(), 0);
703 717
704 718 seriesSpyArg = seriesSpy.takeFirst();
705 719 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
706 720 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
707 721 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
708 722
709 723 setSpyArg = setSpy1.takeFirst();
710 724 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
711 725 QVERIFY(setSpyArg.at(0).toInt() == 2);
712 726
713 727 //====================================================================================
714 728 // barset 2, bar 0
715 729 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
716 730 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
717 731
718 732 QCOMPARE(seriesSpy.count(), 1);
719 733 QCOMPARE(setSpy1.count(), 0);
720 734 QCOMPARE(setSpy2.count(), 1);
721 735
722 736 seriesSpyArg = seriesSpy.takeFirst();
723 737 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
724 738 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
725 739 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
726 740
727 741 setSpyArg = setSpy2.takeFirst();
728 742 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
729 743 QVERIFY(setSpyArg.at(0).toInt() == 0);
730 744
731 745 //====================================================================================
732 746 // barset 2, bar 1
733 747 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
734 748 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
735 749
736 750 QCOMPARE(seriesSpy.count(), 1);
737 751 QCOMPARE(setSpy1.count(), 0);
738 752 QCOMPARE(setSpy2.count(), 1);
739 753
740 754 seriesSpyArg = seriesSpy.takeFirst();
741 755 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
742 756 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
743 757 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
744 758
745 759 setSpyArg = setSpy2.takeFirst();
746 760 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
747 761 QVERIFY(setSpyArg.at(0).toInt() == 1);
748 762
749 763 //====================================================================================
750 764 // barset 2, bar 2
751 765 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
752 766 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
753 767
754 768 QCOMPARE(seriesSpy.count(), 1);
755 769 QCOMPARE(setSpy1.count(), 0);
756 770 QCOMPARE(setSpy2.count(), 1);
757 771
758 772 seriesSpyArg = seriesSpy.takeFirst();
759 773 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
760 774 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
761 775 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
762 776
763 777 setSpyArg = setSpy2.takeFirst();
764 778 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
765 779 QVERIFY(setSpyArg.at(0).toInt() == 2);
766 780 }
767 781
768 782 void tst_QHorizontalStackedBarSeries::mouseReleased()
769 783 {
770 784 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
771 785
772 786 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
773 787
774 788 QBarSet* set1 = new QBarSet(QString("set 1"));
775 789 *set1 << 10 << 10 << 10;
776 790 series->append(set1);
777 791
778 792 QBarSet* set2 = new QBarSet(QString("set 2"));
779 793 *set2 << 10 << 10 << 10;
780 794 series->append(set2);
781 795 QList<QBarSet*> barSets = series->barSets();
782 796
783 797 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
784 798 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
785 799 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
786 800
787 801 QChartView view(new QChart());
788 802 view.resize(400,300);
789 803 view.chart()->addSeries(series);
790 804 view.show();
791 805 QTest::qWaitForWindowShown(&view);
792 806
793 807 // Calculate expected layout for bars
794 808 QRectF plotArea = view.chart()->plotArea();
795 809 qreal width = plotArea.width();
796 810 qreal height = plotArea.height();
797 811 qreal rangeY = 3; // 3 values per set
798 812 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
799 813 qreal scaleY = (height / rangeY);
800 814 qreal scaleX = (width / rangeX);
801 815
802 816 qreal setCount = series->count();
803 817 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
804 818 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
805 819 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
806 820
807 821 QVector<QRectF> layout;
808 822
809 823 // 3 = count of values in set
810 824 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
811 825 for (int i = 0; i < 3; i++) {
812 826 qreal xMax = -scaleX * domainMinX + plotArea.left();
813 827 qreal xMin = -scaleX * domainMinX + plotArea.left();
814 828 for (int set = 0; set < setCount; set++) {
815 829 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
816 830 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
817 831 if (rectWidth > 0) {
818 832 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
819 833 layout.append(rect);
820 834 xMax += rectWidth;
821 835 } else {
822 836 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
823 837 layout.append(rect);
824 838 xMin += rectWidth;
825 839 }
826 840 }
827 841 }
828 842
829 843 //====================================================================================
830 844 // barset 1, bar 0
831 845 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
832 846 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
833 847
834 848 QCOMPARE(seriesSpy.count(), 1);
835 849 QCOMPARE(setSpy1.count(), 1);
836 850 QCOMPARE(setSpy2.count(), 0);
837 851
838 852 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
839 853 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
840 854 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
841 855 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
842 856
843 857 QList<QVariant> setSpyArg = setSpy1.takeFirst();
844 858 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
845 859 QVERIFY(setSpyArg.at(0).toInt() == 0);
846 860
847 861 //====================================================================================
848 862 // barset 1, bar 1
849 863 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
850 864 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
851 865
852 866 QCOMPARE(seriesSpy.count(), 1);
853 867 QCOMPARE(setSpy1.count(), 1);
854 868 QCOMPARE(setSpy2.count(), 0);
855 869
856 870 seriesSpyArg = seriesSpy.takeFirst();
857 871 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
858 872 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
859 873 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
860 874
861 875 setSpyArg = setSpy1.takeFirst();
862 876 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
863 877 QVERIFY(setSpyArg.at(0).toInt() == 1);
864 878
865 879 //====================================================================================
866 880 // barset 1, bar 2
867 881 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
868 882 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
869 883
870 884 QCOMPARE(seriesSpy.count(), 1);
871 885 QCOMPARE(setSpy1.count(), 1);
872 886 QCOMPARE(setSpy2.count(), 0);
873 887
874 888 seriesSpyArg = seriesSpy.takeFirst();
875 889 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
876 890 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
877 891 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
878 892
879 893 setSpyArg = setSpy1.takeFirst();
880 894 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
881 895 QVERIFY(setSpyArg.at(0).toInt() == 2);
882 896
883 897 //====================================================================================
884 898 // barset 2, bar 0
885 899 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
886 900 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
887 901
888 902 QCOMPARE(seriesSpy.count(), 1);
889 903 QCOMPARE(setSpy1.count(), 0);
890 904 QCOMPARE(setSpy2.count(), 1);
891 905
892 906 seriesSpyArg = seriesSpy.takeFirst();
893 907 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
894 908 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
895 909 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
896 910
897 911 setSpyArg = setSpy2.takeFirst();
898 912 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
899 913 QVERIFY(setSpyArg.at(0).toInt() == 0);
900 914
901 915 //====================================================================================
902 916 // barset 2, bar 1
903 917 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
904 918 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
905 919
906 920 QCOMPARE(seriesSpy.count(), 1);
907 921 QCOMPARE(setSpy1.count(), 0);
908 922 QCOMPARE(setSpy2.count(), 1);
909 923
910 924 seriesSpyArg = seriesSpy.takeFirst();
911 925 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
912 926 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
913 927 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
914 928
915 929 setSpyArg = setSpy2.takeFirst();
916 930 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
917 931 QVERIFY(setSpyArg.at(0).toInt() == 1);
918 932
919 933 //====================================================================================
920 934 // barset 2, bar 2
921 935 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
922 936 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
923 937
924 938 QCOMPARE(seriesSpy.count(), 1);
925 939 QCOMPARE(setSpy1.count(), 0);
926 940 QCOMPARE(setSpy2.count(), 1);
927 941
928 942 seriesSpyArg = seriesSpy.takeFirst();
929 943 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
930 944 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
931 945 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
932 946
933 947 setSpyArg = setSpy2.takeFirst();
934 948 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
935 949 QVERIFY(setSpyArg.at(0).toInt() == 2);
936 950 }
937 951
938 952 void tst_QHorizontalStackedBarSeries::mouseDoubleClicked()
939 953 {
940 954 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
941 955
942 956 QHorizontalStackedBarSeries* series = new QHorizontalStackedBarSeries();
943 957
944 958 QBarSet* set1 = new QBarSet(QString("set 1"));
945 959 *set1 << 10 << 10 << 10;
946 960 series->append(set1);
947 961
948 962 QBarSet* set2 = new QBarSet(QString("set 2"));
949 963 *set2 << 10 << 10 << 10;
950 964 series->append(set2);
951 965 QList<QBarSet*> barSets = series->barSets();
952 966
953 967 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
954 968 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
955 969 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
956 970
957 971 QChartView view(new QChart());
958 972 view.resize(400,300);
959 973 view.chart()->addSeries(series);
960 974 view.show();
961 975 QTest::qWaitForWindowShown(&view);
962 976
963 977 // Calculate expected layout for bars
964 978 QRectF plotArea = view.chart()->plotArea();
965 979 qreal width = plotArea.width();
966 980 qreal height = plotArea.height();
967 981 qreal rangeY = 3; // 3 values per set
968 982 qreal rangeX = 20; // From 0 to 20 because bars are stacked (this should be height of highest stack)
969 983 qreal scaleY = (height / rangeY);
970 984 qreal scaleX = (width / rangeX);
971 985
972 986 qreal setCount = series->count();
973 987 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
974 988 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
975 989 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
976 990
977 991 QVector<QRectF> layout;
978 992
979 993 // 3 = count of values in set
980 994 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
981 995 for (int i = 0; i < 3; i++) {
982 996 qreal xMax = -scaleX * domainMinX + plotArea.left();
983 997 qreal xMin = -scaleX * domainMinX + plotArea.left();
984 998 for (int set = 0; set < setCount; set++) {
985 999 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
986 1000 qreal rectWidth = barSets.at(set)->at(i) * scaleX;
987 1001 if (rectWidth > 0) {
988 1002 QRectF rect(xMax, yPos - rectHeight, rectWidth, rectHeight);
989 1003 layout.append(rect);
990 1004 xMax += rectWidth;
991 1005 } else {
992 1006 QRectF rect(xMin, yPos - rectHeight, rectWidth, rectHeight);
993 1007 layout.append(rect);
994 1008 xMin += rectWidth;
995 1009 }
996 1010 }
997 1011 }
998 1012
999 1013 // barset 1, bar 0
1000 1014 QTest::mouseDClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1001 1015 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1002 1016
1003 1017 QCOMPARE(seriesSpy.count(), 1);
1004 1018 QCOMPARE(setSpy1.count(), 1);
1005 1019 QCOMPARE(setSpy2.count(), 0);
1006 1020
1007 1021 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1008 1022 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1009 1023 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1010 1024 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1011 1025
1012 1026 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1013 1027 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1014 1028 QVERIFY(setSpyArg.at(0).toInt() == 0);
1015 1029 }
1016 1030 QTEST_MAIN(tst_QHorizontalStackedBarSeries)
1017 1031
1018 1032 #include "tst_qhorizontalstackedbarseries.moc"
1019 1033
@@ -1,1025 +1,1039
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include <QtTest/QtTest>
20 20 #include <QtCharts/QPercentBarSeries>
21 21 #include <QtCharts/QBarSet>
22 22 #include <QtCharts/QChartView>
23 23 #include <QtCharts/QChart>
24 24 #include "tst_definitions.h"
25 25
26 26 QT_CHARTS_USE_NAMESPACE
27 27
28 28 Q_DECLARE_METATYPE(QBarSet*)
29 29 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
30 30
31 31 class tst_QPercentBarSeries : public QObject
32 32 {
33 33 Q_OBJECT
34 34
35 35 public slots:
36 36 void initTestCase();
37 37 void cleanupTestCase();
38 38 void init();
39 39 void cleanup();
40 40
41 41 private slots:
42 42 void qpercentbarseries_data();
43 43 void qpercentbarseries();
44 44 void type_data();
45 45 void type();
46 46 void setLabelsFormat();
47 47 void setLabelsPosition();
48 void setLabelsAngle();
48 49 void mouseclicked_data();
49 50 void mouseclicked();
50 51 void mousehovered_data();
51 52 void mousehovered();
52 53 void zeroValuesInSeries();
53 54 void mousePressed();
54 55 void mouseReleased();
55 56 void mouseDoubleClicked();
56 57
57 58 private:
58 59 QPercentBarSeries* m_barseries;
59 60 };
60 61
61 62 void tst_QPercentBarSeries::initTestCase()
62 63 {
63 64 qRegisterMetaType<QBarSet*>("QBarSet*");
64 65 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
65 66 }
66 67
67 68 void tst_QPercentBarSeries::cleanupTestCase()
68 69 {
69 70 QTest::qWait(1); // Allow final deleteLaters to run
70 71 }
71 72
72 73 void tst_QPercentBarSeries::init()
73 74 {
74 75 m_barseries = new QPercentBarSeries();
75 76 }
76 77
77 78 void tst_QPercentBarSeries::cleanup()
78 79 {
79 80 delete m_barseries;
80 81 m_barseries = 0;
81 82 }
82 83
83 84 void tst_QPercentBarSeries::qpercentbarseries_data()
84 85 {
85 86 }
86 87
87 88 void tst_QPercentBarSeries::qpercentbarseries()
88 89 {
89 90 QPercentBarSeries *barseries = new QPercentBarSeries();
90 91 QVERIFY(barseries != 0);
91 92 delete barseries;
92 93 }
93 94
94 95 void tst_QPercentBarSeries::type_data()
95 96 {
96 97
97 98 }
98 99
99 100 void tst_QPercentBarSeries::type()
100 101 {
101 102 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypePercentBar);
102 103 }
103 104
104 105 void tst_QPercentBarSeries::mouseclicked_data()
105 106 {
106 107
107 108 }
108 109
109 110 void tst_QPercentBarSeries::setLabelsFormat()
110 111 {
111 112 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
112 113 QCOMPARE(m_barseries->labelsFormat(), QString());
113 114
114 115 QString format("(@value)");
115 116 m_barseries->setLabelsFormat(format);
116 117 TRY_COMPARE(labelsFormatSpy.count(), 1);
117 118 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
118 119 QVERIFY(arguments.at(0).toString() == format);
119 120 QCOMPARE(m_barseries->labelsFormat(), format);
120 121
121 122 m_barseries->setLabelsFormat(QString());
122 123 TRY_COMPARE(labelsFormatSpy.count(), 1);
123 124 arguments = labelsFormatSpy.takeFirst();
124 125 QVERIFY(arguments.at(0).toString() == QString());
125 126 QCOMPARE(m_barseries->labelsFormat(), QString());
126 127 }
127 128
128 129 void tst_QPercentBarSeries::setLabelsPosition()
129 130 {
130 131 QSignalSpy labelsPositionSpy(m_barseries,
131 132 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
132 133 QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsCenter);
133 134
134 135 m_barseries->setLabelsPosition(QPercentBarSeries::LabelsInsideEnd);
135 136 TRY_COMPARE(labelsPositionSpy.count(), 1);
136 137 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
137 138 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
138 139 == QPercentBarSeries::LabelsInsideEnd);
139 140 QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsInsideEnd);
140 141
141 142 m_barseries->setLabelsPosition(QPercentBarSeries::LabelsInsideBase);
142 143 TRY_COMPARE(labelsPositionSpy.count(), 1);
143 144 arguments = labelsPositionSpy.takeFirst();
144 145 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
145 146 == QPercentBarSeries::LabelsInsideBase);
146 147 QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsInsideBase);
147 148
148 149 m_barseries->setLabelsPosition(QPercentBarSeries::LabelsOutsideEnd);
149 150 TRY_COMPARE(labelsPositionSpy.count(), 1);
150 151 arguments = labelsPositionSpy.takeFirst();
151 152 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
152 153 == QPercentBarSeries::LabelsOutsideEnd);
153 154 QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsOutsideEnd);
154 155
155 156 m_barseries->setLabelsPosition(QPercentBarSeries::LabelsCenter);
156 157 TRY_COMPARE(labelsPositionSpy.count(), 1);
157 158 arguments = labelsPositionSpy.takeFirst();
158 159 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
159 160 == QPercentBarSeries::LabelsCenter);
160 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 177 void tst_QPercentBarSeries::mouseclicked()
164 178 {
165 179 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
166 180
167 181 QPercentBarSeries* series = new QPercentBarSeries();
168 182
169 183 QBarSet* set1 = new QBarSet(QString("set 1"));
170 184 *set1 << 10 << 10 << 10;
171 185 series->append(set1);
172 186
173 187 QBarSet* set2 = new QBarSet(QString("set 2"));
174 188 *set2 << 10 << 10 << 10;
175 189 series->append(set2);
176 190
177 191 QList<QBarSet*> barSets = series->barSets();
178 192
179 193 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
180 194
181 195 QChartView view(new QChart());
182 196 view.resize(400,300);
183 197 view.chart()->addSeries(series);
184 198 view.show();
185 199 QTest::qWaitForWindowShown(&view);
186 200
187 201 // Calculate expected layout for bars
188 202 QRectF plotArea = view.chart()->plotArea();
189 203 qreal width = plotArea.width();
190 204 qreal height = plotArea.height();
191 205 qreal rangeY = 100; // From 0 to 100 because range is scaled to 100%
192 206 qreal rangeX = 3; // 3 values per set
193 207 qreal scaleY = (height / rangeY);
194 208 qreal scaleX = (width / rangeX);
195 209
196 210 qreal setCount = series->count();
197 211 qreal domainMinY = 0; // These come from internal domain used by barseries.
198 212 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
199 213 qreal rectWidth = scaleX * series->barWidth();
200 214
201 215 QVector<QRectF> layout;
202 216
203 217 // 3 = count of values in set
204 218 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
205 219 for (int i = 0; i < 3; i++) {
206 220 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
207 221 qreal percentage = (100 / colSum);
208 222 qreal yPos = height + scaleY * domainMinY + plotArea.top();
209 223
210 224 for (int set = 0; set < setCount; set++) {
211 225 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
212 226 qreal rectHeigth = barSets.at(set)->at(i) * percentage * scaleY;
213 227
214 228 QRectF rect(xPos, yPos-rectHeigth, rectWidth, rectHeigth);
215 229 layout.append(rect);
216 230 yPos -= rectHeigth;
217 231 }
218 232 }
219 233
220 234 //====================================================================================
221 235 // barset 1, bar 0
222 236 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
223 237 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
224 238
225 239 QCOMPARE(seriesSpy.count(), 1);
226 240
227 241 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
228 242 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
229 243 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
230 244 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
231 245
232 246 //====================================================================================
233 247 // barset 1, bar 1
234 248 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
235 249 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
236 250
237 251 QCOMPARE(seriesSpy.count(), 1);
238 252
239 253 seriesSpyArg = seriesSpy.takeFirst();
240 254 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
241 255 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
242 256 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
243 257
244 258 //====================================================================================
245 259 // barset 1, bar 2
246 260 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
247 261 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
248 262
249 263 QCOMPARE(seriesSpy.count(), 1);
250 264
251 265 seriesSpyArg = seriesSpy.takeFirst();
252 266 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
253 267 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
254 268 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
255 269
256 270 //====================================================================================
257 271 // barset 2, bar 0
258 272 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
259 273 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
260 274
261 275 QCOMPARE(seriesSpy.count(), 1);
262 276
263 277 seriesSpyArg = seriesSpy.takeFirst();
264 278 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
265 279 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
266 280 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
267 281
268 282 //====================================================================================
269 283 // barset 2, bar 1
270 284 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
271 285 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
272 286
273 287 QCOMPARE(seriesSpy.count(), 1);
274 288
275 289 seriesSpyArg = seriesSpy.takeFirst();
276 290 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
277 291 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
278 292 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
279 293
280 294 //====================================================================================
281 295 // barset 2, bar 2
282 296 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
283 297 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
284 298
285 299 QCOMPARE(seriesSpy.count(), 1);
286 300
287 301 seriesSpyArg = seriesSpy.takeFirst();
288 302 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
289 303 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
290 304 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
291 305 }
292 306
293 307 void tst_QPercentBarSeries::mousehovered_data()
294 308 {
295 309
296 310 }
297 311
298 312 void tst_QPercentBarSeries::mousehovered()
299 313 {
300 314 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
301 315
302 316 QPercentBarSeries* series = new QPercentBarSeries();
303 317
304 318 QBarSet* set1 = new QBarSet(QString("set 1"));
305 319 *set1 << 10 << 10 << 10;
306 320 series->append(set1);
307 321
308 322 QBarSet* set2 = new QBarSet(QString("set 2"));
309 323 *set2 << 10 << 10 << 10;
310 324 series->append(set2);
311 325
312 326 QList<QBarSet*> barSets = series->barSets();
313 327
314 328 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
315 329 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
316 330 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
317 331
318 332 QChartView view(new QChart());
319 333 view.resize(400,300);
320 334 view.chart()->addSeries(series);
321 335 view.show();
322 336 QTest::qWaitForWindowShown(&view);
323 337
324 338 //this is hack since view does not get events otherwise
325 339 view.setMouseTracking(true);
326 340
327 341 // Calculate expected layout for bars
328 342 QRectF plotArea = view.chart()->plotArea();
329 343 qreal width = plotArea.width();
330 344 qreal height = plotArea.height();
331 345 qreal rangeY = 100; // From 0 to 100 because range is scaled to 100%
332 346 qreal rangeX = 3; // 3 values per set
333 347 qreal scaleY = (height / rangeY);
334 348 qreal scaleX = (width / rangeX);
335 349
336 350 qreal setCount = series->count();
337 351 qreal domainMinY = 0; // These come from internal domain used by barseries.
338 352 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
339 353 qreal rectWidth = scaleX * series->barWidth();
340 354
341 355 QVector<QRectF> layout;
342 356
343 357 // 3 = count of values in set
344 358 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
345 359 for (int i = 0; i < 3; i++) {
346 360 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
347 361 qreal percentage = (100 / colSum);
348 362 qreal yPos = height + scaleY * domainMinY + plotArea.top();
349 363
350 364 for (int set = 0; set < setCount; set++) {
351 365 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
352 366 qreal rectHeight = barSets.at(set)->at(i) * percentage * scaleY;
353 367
354 368 QRectF rect(xPos, yPos-rectHeight, rectWidth, rectHeight);
355 369 layout.append(rect);
356 370 yPos -= rectHeight;
357 371 }
358 372 }
359 373
360 374 //=======================================================================
361 375 // move mouse to left border
362 376 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(0).center().y()));
363 377 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
364 378 TRY_COMPARE(seriesIndexSpy.count(), 0);
365 379
366 380 //=======================================================================
367 381 // move mouse on top of set1
368 382 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
369 383 TRY_COMPARE(seriesIndexSpy.count(), 1);
370 384 TRY_COMPARE(setIndexSpy1.count(), 1);
371 385 TRY_COMPARE(setIndexSpy2.count(), 0);
372 386
373 387 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
374 388 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
375 389 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
376 390 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
377 391
378 392 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
379 393 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
380 394 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
381 395
382 396 //=======================================================================
383 397 // move mouse from top of set1 to top of set2
384 398 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
385 399 TRY_COMPARE(seriesIndexSpy.count(), 2);
386 400 TRY_COMPARE(setIndexSpy1.count(), 1);
387 401 TRY_COMPARE(setIndexSpy2.count(), 1);
388 402
389 403 // should leave set1
390 404 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
391 405 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
392 406 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
393 407 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
394 408
395 409 setIndexSpyArg = setIndexSpy1.takeFirst();
396 410 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
397 411 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
398 412
399 413 // should enter set2
400 414 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
401 415 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
402 416 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
403 417 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
404 418
405 419 setIndexSpyArg = setIndexSpy2.takeFirst();
406 420 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
407 421 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
408 422
409 423 //=======================================================================
410 424 // move mouse from top of set2 to background
411 425 QTest::mouseMove(view.viewport(), QPoint(layout.at(1).center().x(), 0));
412 426 TRY_COMPARE(seriesIndexSpy.count(), 1);
413 427 TRY_COMPARE(setIndexSpy1.count(), 0);
414 428 TRY_COMPARE(setIndexSpy2.count(), 1);
415 429
416 430 // should leave set2
417 431 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
418 432 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
419 433 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
420 434 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
421 435
422 436 setIndexSpyArg = setIndexSpy2.takeFirst();
423 437 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
424 438 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
425 439
426 440 //=======================================================================
427 441 // move mouse on top of set1, bar0 to check the index (hover into set1)
428 442 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
429 443
430 444 TRY_COMPARE(seriesIndexSpy.count(), 1);
431 445 TRY_COMPARE(setIndexSpy1.count(), 1);
432 446 TRY_COMPARE(setIndexSpy2.count(), 0);
433 447
434 448 //should enter set1, bar0
435 449 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
436 450 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
437 451 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
438 452 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
439 453 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
440 454 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
441 455
442 456 setIndexSpyArg = setIndexSpy1.takeFirst();
443 457 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
444 458 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
445 459 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
446 460 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
447 461
448 462 //=======================================================================
449 463 // move mouse on top of set2, bar0 to check the index (hover out set1,
450 464 // hover in set2)
451 465 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
452 466
453 467 TRY_COMPARE(seriesIndexSpy.count(), 2);
454 468 TRY_COMPARE(setIndexSpy1.count(), 1);
455 469 TRY_COMPARE(setIndexSpy2.count(), 1);
456 470
457 471 //should leave set1, bar0
458 472 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
459 473 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
460 474 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
461 475 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
462 476 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
463 477 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
464 478
465 479 setIndexSpyArg = setIndexSpy1.takeFirst();
466 480 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
467 481 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
468 482 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
469 483 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
470 484
471 485 //should enter set2, bar0
472 486 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
473 487 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
474 488 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
475 489 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
476 490 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
477 491 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
478 492
479 493 setIndexSpyArg = setIndexSpy2.takeFirst();
480 494 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
481 495 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
482 496 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
483 497 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
484 498
485 499 //=======================================================================
486 500 // move mouse on top of set1, bar1 to check the index (hover out set2,
487 501 // hover in set1)
488 502 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
489 503
490 504 TRY_COMPARE(seriesIndexSpy.count(), 2);
491 505 TRY_COMPARE(setIndexSpy1.count(), 1);
492 506 TRY_COMPARE(setIndexSpy2.count(), 1);
493 507
494 508 //should leave set2, bar0
495 509 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
496 510 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
497 511 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
498 512 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
499 513 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
500 514 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
501 515
502 516 setIndexSpyArg = setIndexSpy2.takeFirst();
503 517 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
504 518 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
505 519 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
506 520 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
507 521
508 522 //should enter set1, bar1
509 523 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
510 524 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
511 525 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
512 526 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
513 527 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
514 528 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
515 529
516 530 setIndexSpyArg = setIndexSpy1.takeFirst();
517 531 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
518 532 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
519 533 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
520 534 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
521 535
522 536 //=======================================================================
523 537 // move mouse between set1 and set2 (hover out set1)
524 538 QTest::mouseMove(view.viewport(), QPoint((layout.at(3).right() + layout.at(4).left()) /2,
525 539 layout.at(2).top()));
526 540
527 541 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
528 542 TRY_COMPARE(seriesIndexSpy.count(), 1);
529 543 TRY_COMPARE(setIndexSpy1.count(), 1);
530 544 TRY_COMPARE(setIndexSpy2.count(), 0);
531 545
532 546 //should leave set1, bar1
533 547 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
534 548 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
535 549 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
536 550 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
537 551 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
538 552 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
539 553
540 554 setIndexSpyArg = setIndexSpy1.takeFirst();
541 555 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
542 556 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
543 557 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
544 558 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
545 559
546 560 //=======================================================================
547 561 // move mouse on top of set2, bar1 to check the index (hover in set2)
548 562 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
549 563
550 564 TRY_COMPARE(seriesIndexSpy.count(), 1);
551 565 TRY_COMPARE(setIndexSpy1.count(), 0);
552 566 TRY_COMPARE(setIndexSpy2.count(), 1);
553 567
554 568 //should enter set2, bar1
555 569 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
556 570 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
557 571 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
558 572 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
559 573 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
560 574 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
561 575
562 576 setIndexSpyArg = setIndexSpy2.takeFirst();
563 577 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
564 578 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
565 579 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
566 580 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
567 581
568 582 //=======================================================================
569 583 // move mouse between set1 and set2 (hover out set2)
570 584 QTest::mouseMove(view.viewport(), QPoint((layout.at(3).right() + layout.at(4).left()) /2,
571 585 layout.at(2).top()));
572 586
573 587 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
574 588 TRY_COMPARE(seriesIndexSpy.count(), 1);
575 589 TRY_COMPARE(setIndexSpy1.count(), 0);
576 590 TRY_COMPARE(setIndexSpy2.count(), 1);
577 591
578 592 //should leave set1, bar1
579 593 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
580 594 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
581 595 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
582 596 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
583 597 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
584 598 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
585 599
586 600 setIndexSpyArg = setIndexSpy2.takeFirst();
587 601 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
588 602 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
589 603 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
590 604 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
591 605 }
592 606
593 607 void tst_QPercentBarSeries::zeroValuesInSeries()
594 608 {
595 609 QPercentBarSeries *series = new QPercentBarSeries();
596 610 QBarSet *set1 = new QBarSet(QString("set 1"));
597 611 *set1 << 100 << 0.0 << 10;
598 612 series->append(set1);
599 613
600 614 QBarSet *set2 = new QBarSet(QString("set 2"));
601 615 *set2 << 0.0 << 0.0 << 70;
602 616 series->append(set2);
603 617
604 618 QChartView view(new QChart());
605 619 view.chart()->addSeries(series);
606 620 view.chart()->createDefaultAxes();
607 621 view.show();
608 622
609 623 QTest::qWaitForWindowShown(&view);
610 624 }
611 625
612 626 void tst_QPercentBarSeries::mousePressed()
613 627 {
614 628 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
615 629
616 630 QPercentBarSeries* series = new QPercentBarSeries();
617 631
618 632 QBarSet* set1 = new QBarSet(QString("set 1"));
619 633 *set1 << 10 << 10 << 10;
620 634 series->append(set1);
621 635
622 636 QBarSet* set2 = new QBarSet(QString("set 2"));
623 637 *set2 << 10 << 10 << 10;
624 638 series->append(set2);
625 639 QList<QBarSet*> barSets = series->barSets();
626 640
627 641 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
628 642 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
629 643 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
630 644
631 645 QChartView view(new QChart());
632 646 view.resize(400,300);
633 647 view.chart()->addSeries(series);
634 648 view.show();
635 649 QTest::qWaitForWindowShown(&view);
636 650
637 651 // Calculate expected layout for bars
638 652 QRectF plotArea = view.chart()->plotArea();
639 653 qreal width = plotArea.width();
640 654 qreal height = plotArea.height();
641 655 qreal rangeY = 100; // From 0 to 100 because range is scaled to 100%
642 656 qreal rangeX = 3; // 3 values per set
643 657 qreal scaleY = (height / rangeY);
644 658 qreal scaleX = (width / rangeX);
645 659
646 660 qreal setCount = series->count();
647 661 qreal domainMinY = 0; // These come from internal domain used by barseries.
648 662 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
649 663 qreal rectWidth = scaleX * series->barWidth();
650 664
651 665 QVector<QRectF> layout;
652 666
653 667 // 3 = count of values in set
654 668 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
655 669 for (int i = 0; i < 3; i++) {
656 670 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
657 671 qreal percentage = (100 / colSum);
658 672 qreal yPos = height + scaleY * domainMinY + plotArea.top();
659 673
660 674 for (int set = 0; set < setCount; set++) {
661 675 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
662 676 qreal rectHeigth = barSets.at(set)->at(i) * percentage * scaleY;
663 677
664 678 QRectF rect(xPos, yPos-rectHeigth, rectWidth, rectHeigth);
665 679 layout.append(rect);
666 680 yPos -= rectHeigth;
667 681 }
668 682 }
669 683
670 684 //====================================================================================
671 685 // barset 1, bar 0
672 686 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
673 687 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
674 688
675 689 QCOMPARE(seriesSpy.count(), 1);
676 690 QCOMPARE(setSpy1.count(), 1);
677 691 QCOMPARE(setSpy2.count(), 0);
678 692
679 693 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
680 694 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
681 695 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
682 696 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
683 697
684 698 QList<QVariant> setSpyArg = setSpy1.takeFirst();
685 699 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
686 700 QVERIFY(setSpyArg.at(0).toInt() == 0);
687 701
688 702 //====================================================================================
689 703 // barset 1, bar 1
690 704 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
691 705 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
692 706
693 707 QCOMPARE(seriesSpy.count(), 1);
694 708 QCOMPARE(setSpy1.count(), 1);
695 709 QCOMPARE(setSpy2.count(), 0);
696 710
697 711 seriesSpyArg = seriesSpy.takeFirst();
698 712 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
699 713 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
700 714 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
701 715
702 716 setSpyArg = setSpy1.takeFirst();
703 717 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
704 718 QVERIFY(setSpyArg.at(0).toInt() == 1);
705 719
706 720 //====================================================================================
707 721 // barset 1, bar 2
708 722 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
709 723 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
710 724
711 725 QCOMPARE(seriesSpy.count(), 1);
712 726 QCOMPARE(setSpy1.count(), 1);
713 727 QCOMPARE(setSpy2.count(), 0);
714 728
715 729 seriesSpyArg = seriesSpy.takeFirst();
716 730 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
717 731 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
718 732 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
719 733
720 734 setSpyArg = setSpy1.takeFirst();
721 735 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
722 736 QVERIFY(setSpyArg.at(0).toInt() == 2);
723 737
724 738 //====================================================================================
725 739 // barset 2, bar 0
726 740 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
727 741 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
728 742
729 743 QCOMPARE(seriesSpy.count(), 1);
730 744 QCOMPARE(setSpy1.count(), 0);
731 745 QCOMPARE(setSpy2.count(), 1);
732 746
733 747 seriesSpyArg = seriesSpy.takeFirst();
734 748 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
735 749 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
736 750 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
737 751
738 752 setSpyArg = setSpy2.takeFirst();
739 753 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
740 754 QVERIFY(setSpyArg.at(0).toInt() == 0);
741 755
742 756 //====================================================================================
743 757 // barset 2, bar 1
744 758 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
745 759 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
746 760
747 761 QCOMPARE(seriesSpy.count(), 1);
748 762 QCOMPARE(setSpy1.count(), 0);
749 763 QCOMPARE(setSpy2.count(), 1);
750 764
751 765 seriesSpyArg = seriesSpy.takeFirst();
752 766 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
753 767 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
754 768 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
755 769
756 770 setSpyArg = setSpy2.takeFirst();
757 771 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
758 772 QVERIFY(setSpyArg.at(0).toInt() == 1);
759 773
760 774 //====================================================================================
761 775 // barset 2, bar 2
762 776 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
763 777 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
764 778
765 779 QCOMPARE(seriesSpy.count(), 1);
766 780 QCOMPARE(setSpy1.count(), 0);
767 781 QCOMPARE(setSpy2.count(), 1);
768 782
769 783 seriesSpyArg = seriesSpy.takeFirst();
770 784 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
771 785 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
772 786 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
773 787
774 788 setSpyArg = setSpy2.takeFirst();
775 789 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
776 790 QVERIFY(setSpyArg.at(0).toInt() == 2);
777 791 }
778 792
779 793 void tst_QPercentBarSeries::mouseReleased()
780 794 {
781 795 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
782 796
783 797 QPercentBarSeries* series = new QPercentBarSeries();
784 798
785 799 QBarSet* set1 = new QBarSet(QString("set 1"));
786 800 *set1 << 10 << 10 << 10;
787 801 series->append(set1);
788 802
789 803 QBarSet* set2 = new QBarSet(QString("set 2"));
790 804 *set2 << 10 << 10 << 10;
791 805 series->append(set2);
792 806 QList<QBarSet*> barSets = series->barSets();
793 807
794 808 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
795 809 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
796 810 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
797 811
798 812 QChartView view(new QChart());
799 813 view.resize(400,300);
800 814 view.chart()->addSeries(series);
801 815 view.show();
802 816 QTest::qWaitForWindowShown(&view);
803 817
804 818 // Calculate expected layout for bars
805 819 QRectF plotArea = view.chart()->plotArea();
806 820 qreal width = plotArea.width();
807 821 qreal height = plotArea.height();
808 822 qreal rangeY = 100; // From 0 to 100 because range is scaled to 100%
809 823 qreal rangeX = 3; // 3 values per set
810 824 qreal scaleY = (height / rangeY);
811 825 qreal scaleX = (width / rangeX);
812 826
813 827 qreal setCount = series->count();
814 828 qreal domainMinY = 0; // These come from internal domain used by barseries.
815 829 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
816 830 qreal rectWidth = scaleX * series->barWidth();
817 831
818 832 QVector<QRectF> layout;
819 833
820 834 // 3 = count of values in set
821 835 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
822 836 for (int i = 0; i < 3; i++) {
823 837 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
824 838 qreal percentage = (100 / colSum);
825 839 qreal yPos = height + scaleY * domainMinY + plotArea.top();
826 840
827 841 for (int set = 0; set < setCount; set++) {
828 842 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
829 843 qreal rectHeigth = barSets.at(set)->at(i) * percentage * scaleY;
830 844
831 845 QRectF rect(xPos, yPos-rectHeigth, rectWidth, rectHeigth);
832 846 layout.append(rect);
833 847 yPos -= rectHeigth;
834 848 }
835 849 }
836 850
837 851 //====================================================================================
838 852 // barset 1, bar 0
839 853 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
840 854 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
841 855
842 856 QCOMPARE(seriesSpy.count(), 1);
843 857 QCOMPARE(setSpy1.count(), 1);
844 858 QCOMPARE(setSpy2.count(), 0);
845 859
846 860 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
847 861 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
848 862 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
849 863 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
850 864
851 865 QList<QVariant> setSpyArg = setSpy1.takeFirst();
852 866 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
853 867 QVERIFY(setSpyArg.at(0).toInt() == 0);
854 868
855 869 //====================================================================================
856 870 // barset 1, bar 1
857 871 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
858 872 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
859 873
860 874 QCOMPARE(seriesSpy.count(), 1);
861 875 QCOMPARE(setSpy1.count(), 1);
862 876 QCOMPARE(setSpy2.count(), 0);
863 877
864 878 seriesSpyArg = seriesSpy.takeFirst();
865 879 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
866 880 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
867 881 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
868 882
869 883 setSpyArg = setSpy1.takeFirst();
870 884 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
871 885 QVERIFY(setSpyArg.at(0).toInt() == 1);
872 886
873 887 //====================================================================================
874 888 // barset 1, bar 2
875 889 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
876 890 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
877 891
878 892 QCOMPARE(seriesSpy.count(), 1);
879 893 QCOMPARE(setSpy1.count(), 1);
880 894 QCOMPARE(setSpy2.count(), 0);
881 895
882 896 seriesSpyArg = seriesSpy.takeFirst();
883 897 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
884 898 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
885 899 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
886 900
887 901 setSpyArg = setSpy1.takeFirst();
888 902 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
889 903 QVERIFY(setSpyArg.at(0).toInt() == 2);
890 904
891 905 //====================================================================================
892 906 // barset 2, bar 0
893 907 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
894 908 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
895 909
896 910 QCOMPARE(seriesSpy.count(), 1);
897 911 QCOMPARE(setSpy1.count(), 0);
898 912 QCOMPARE(setSpy2.count(), 1);
899 913
900 914 seriesSpyArg = seriesSpy.takeFirst();
901 915 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
902 916 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
903 917 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
904 918
905 919 setSpyArg = setSpy2.takeFirst();
906 920 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
907 921 QVERIFY(setSpyArg.at(0).toInt() == 0);
908 922
909 923 //====================================================================================
910 924 // barset 2, bar 1
911 925 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
912 926 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
913 927
914 928 QCOMPARE(seriesSpy.count(), 1);
915 929 QCOMPARE(setSpy1.count(), 0);
916 930 QCOMPARE(setSpy2.count(), 1);
917 931
918 932 seriesSpyArg = seriesSpy.takeFirst();
919 933 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
920 934 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
921 935 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
922 936
923 937 setSpyArg = setSpy2.takeFirst();
924 938 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
925 939 QVERIFY(setSpyArg.at(0).toInt() == 1);
926 940
927 941 //====================================================================================
928 942 // barset 2, bar 2
929 943 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
930 944 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
931 945
932 946 QCOMPARE(seriesSpy.count(), 1);
933 947 QCOMPARE(setSpy1.count(), 0);
934 948 QCOMPARE(setSpy2.count(), 1);
935 949
936 950 seriesSpyArg = seriesSpy.takeFirst();
937 951 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
938 952 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
939 953 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
940 954
941 955 setSpyArg = setSpy2.takeFirst();
942 956 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
943 957 QVERIFY(setSpyArg.at(0).toInt() == 2);
944 958 }
945 959
946 960 void tst_QPercentBarSeries::mouseDoubleClicked()
947 961 {
948 962 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
949 963
950 964 QPercentBarSeries* series = new QPercentBarSeries();
951 965
952 966 QBarSet* set1 = new QBarSet(QString("set 1"));
953 967 *set1 << 10 << 10 << 10;
954 968 series->append(set1);
955 969
956 970 QBarSet* set2 = new QBarSet(QString("set 2"));
957 971 *set2 << 10 << 10 << 10;
958 972 series->append(set2);
959 973 QList<QBarSet*> barSets = series->barSets();
960 974
961 975 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
962 976 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
963 977 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
964 978
965 979 QChartView view(new QChart());
966 980 view.resize(400,300);
967 981 view.chart()->addSeries(series);
968 982 view.show();
969 983 QTest::qWaitForWindowShown(&view);
970 984
971 985 // Calculate expected layout for bars
972 986 QRectF plotArea = view.chart()->plotArea();
973 987 qreal width = plotArea.width();
974 988 qreal height = plotArea.height();
975 989 qreal rangeY = 100; // From 0 to 100 because range is scaled to 100%
976 990 qreal rangeX = 3; // 3 values per set
977 991 qreal scaleY = (height / rangeY);
978 992 qreal scaleX = (width / rangeX);
979 993
980 994 qreal setCount = series->count();
981 995 qreal domainMinY = 0; // These come from internal domain used by barseries.
982 996 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
983 997 qreal rectWidth = scaleX * series->barWidth();
984 998
985 999 QVector<QRectF> layout;
986 1000
987 1001 // 3 = count of values in set
988 1002 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
989 1003 for (int i = 0; i < 3; i++) {
990 1004 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
991 1005 qreal percentage = (100 / colSum);
992 1006 qreal yPos = height + scaleY * domainMinY + plotArea.top();
993 1007
994 1008 for (int set = 0; set < setCount; set++) {
995 1009 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
996 1010 qreal rectHeigth = barSets.at(set)->at(i) * percentage * scaleY;
997 1011
998 1012 QRectF rect(xPos, yPos-rectHeigth, rectWidth, rectHeigth);
999 1013 layout.append(rect);
1000 1014 yPos -= rectHeigth;
1001 1015 }
1002 1016 }
1003 1017
1004 1018 // barset 1, bar 0
1005 1019 QTest::mouseDClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1006 1020 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1007 1021
1008 1022 QCOMPARE(seriesSpy.count(), 1);
1009 1023 QCOMPARE(setSpy1.count(), 1);
1010 1024 QCOMPARE(setSpy2.count(), 0);
1011 1025
1012 1026 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1013 1027 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1014 1028 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1015 1029 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1016 1030
1017 1031 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1018 1032 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1019 1033 QVERIFY(setSpyArg.at(0).toInt() == 0);
1020 1034 }
1021 1035
1022 1036 QTEST_MAIN(tst_QPercentBarSeries)
1023 1037
1024 1038 #include "tst_qpercentbarseries.moc"
1025 1039
@@ -1,1020 +1,1034
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include <QtTest/QtTest>
20 20 #include <QtCharts/QStackedBarSeries>
21 21 #include <QtCharts/QBarSet>
22 22 #include <QtCharts/QChartView>
23 23 #include <QtCharts/QChart>
24 24 #include "tst_definitions.h"
25 25
26 26 QT_CHARTS_USE_NAMESPACE
27 27
28 28 Q_DECLARE_METATYPE(QBarSet*)
29 29 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
30 30
31 31 class tst_QStackedBarSeries : public QObject
32 32 {
33 33 Q_OBJECT
34 34
35 35 public slots:
36 36 void initTestCase();
37 37 void cleanupTestCase();
38 38 void init();
39 39 void cleanup();
40 40
41 41 private slots:
42 42 void qstackedbarseries_data();
43 43 void qstackedbarseries();
44 44 void type_data();
45 45 void type();
46 46 void setLabelsFormat();
47 47 void setLabelsPosition();
48 void setLabelsAngle();
48 49 void mouseclicked_data();
49 50 void mouseclicked();
50 51 void mousehovered_data();
51 52 void mousehovered();
52 53 void mousePressed();
53 54 void mouseReleased();
54 55 void mouseDoubleClicked();
55 56
56 57 private:
57 58 QStackedBarSeries* m_barseries;
58 59 };
59 60
60 61 void tst_QStackedBarSeries::initTestCase()
61 62 {
62 63 qRegisterMetaType<QBarSet*>("QBarSet*");
63 64 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
64 65 }
65 66
66 67 void tst_QStackedBarSeries::cleanupTestCase()
67 68 {
68 69 QTest::qWait(1); // Allow final deleteLaters to run
69 70 }
70 71
71 72 void tst_QStackedBarSeries::init()
72 73 {
73 74 m_barseries = new QStackedBarSeries();
74 75 }
75 76
76 77 void tst_QStackedBarSeries::cleanup()
77 78 {
78 79 delete m_barseries;
79 80 m_barseries = 0;
80 81 }
81 82
82 83 void tst_QStackedBarSeries::qstackedbarseries_data()
83 84 {
84 85 }
85 86
86 87 void tst_QStackedBarSeries::qstackedbarseries()
87 88 {
88 89 QStackedBarSeries *barseries = new QStackedBarSeries();
89 90 QVERIFY(barseries != 0);
90 91 delete barseries;
91 92 }
92 93
93 94 void tst_QStackedBarSeries::type_data()
94 95 {
95 96
96 97 }
97 98
98 99 void tst_QStackedBarSeries::type()
99 100 {
100 101 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeStackedBar);
101 102 }
102 103
103 104 void tst_QStackedBarSeries::setLabelsFormat()
104 105 {
105 106 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
106 107 QCOMPARE(m_barseries->labelsFormat(), QString());
107 108
108 109 QString format("(@value)");
109 110 m_barseries->setLabelsFormat(format);
110 111 TRY_COMPARE(labelsFormatSpy.count(), 1);
111 112 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
112 113 QVERIFY(arguments.at(0).toString() == format);
113 114 QCOMPARE(m_barseries->labelsFormat(), format);
114 115
115 116 m_barseries->setLabelsFormat(QString());
116 117 TRY_COMPARE(labelsFormatSpy.count(), 1);
117 118 arguments = labelsFormatSpy.takeFirst();
118 119 QVERIFY(arguments.at(0).toString() == QString());
119 120 QCOMPARE(m_barseries->labelsFormat(), QString());
120 121 }
121 122
122 123 void tst_QStackedBarSeries::setLabelsPosition()
123 124 {
124 125 QSignalSpy labelsPositionSpy(m_barseries,
125 126 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
126 127 QCOMPARE(m_barseries->labelsPosition(), QStackedBarSeries::LabelsCenter);
127 128
128 129 m_barseries->setLabelsPosition(QStackedBarSeries::LabelsInsideEnd);
129 130 TRY_COMPARE(labelsPositionSpy.count(), 1);
130 131 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
131 132 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
132 133 == QStackedBarSeries::LabelsInsideEnd);
133 134 QCOMPARE(m_barseries->labelsPosition(), QStackedBarSeries::LabelsInsideEnd);
134 135
135 136 m_barseries->setLabelsPosition(QStackedBarSeries::LabelsInsideBase);
136 137 TRY_COMPARE(labelsPositionSpy.count(), 1);
137 138 arguments = labelsPositionSpy.takeFirst();
138 139 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
139 140 == QStackedBarSeries::LabelsInsideBase);
140 141 QCOMPARE(m_barseries->labelsPosition(), QStackedBarSeries::LabelsInsideBase);
141 142
142 143 m_barseries->setLabelsPosition(QStackedBarSeries::LabelsOutsideEnd);
143 144 TRY_COMPARE(labelsPositionSpy.count(), 1);
144 145 arguments = labelsPositionSpy.takeFirst();
145 146 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
146 147 == QStackedBarSeries::LabelsOutsideEnd);
147 148 QCOMPARE(m_barseries->labelsPosition(), QStackedBarSeries::LabelsOutsideEnd);
148 149
149 150 m_barseries->setLabelsPosition(QStackedBarSeries::LabelsCenter);
150 151 TRY_COMPARE(labelsPositionSpy.count(), 1);
151 152 arguments = labelsPositionSpy.takeFirst();
152 153 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
153 154 == QStackedBarSeries::LabelsCenter);
154 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 171 void tst_QStackedBarSeries::mouseclicked_data()
158 172 {
159 173
160 174 }
161 175
162 176 void tst_QStackedBarSeries::mouseclicked()
163 177 {
164 178 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
165 179
166 180 QStackedBarSeries* series = new QStackedBarSeries();
167 181
168 182 QBarSet* set1 = new QBarSet(QString("set 1"));
169 183 *set1 << 10 << 10 << 10;
170 184 series->append(set1);
171 185
172 186 QBarSet* set2 = new QBarSet(QString("set 2"));
173 187 *set2 << 10 << 10 << 10;
174 188 series->append(set2);
175 189
176 190 QList<QBarSet*> barSets = series->barSets();
177 191
178 192 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
179 193
180 194 QChartView view(new QChart());
181 195 view.resize(400,300);
182 196 view.chart()->addSeries(series);
183 197 view.show();
184 198 QTest::qWaitForWindowShown(&view);
185 199
186 200 // Calculate expected layout for bars
187 201 QRectF plotArea = view.chart()->plotArea();
188 202 qreal width = plotArea.width();
189 203 qreal height = plotArea.height();
190 204 qreal rangeY = 20; // From 0 to 20 because sets are stacked (this should be height of highest stack)
191 205 qreal rangeX = 3; // 3 values per set
192 206 qreal scaleY = (height / rangeY);
193 207 qreal scaleX = (width / rangeX);
194 208
195 209 qreal setCount = series->count();
196 210 qreal domainMinY = 0; // These come from internal domain used by barseries.
197 211 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
198 212 qreal rectWidth = scaleX * series->barWidth();
199 213
200 214 QVector<QRectF> layout;
201 215
202 216 // 3 = count of values in set
203 217 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
204 218 for (int i = 0; i < 3; i++) {
205 219 qreal yMax = height + scaleY * domainMinY + plotArea.top();
206 220 qreal yMin = height + scaleY * domainMinY + plotArea.top();
207 221 for (int set = 0; set < setCount; set++) {
208 222 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
209 223 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
210 224 if (rectHeight < 0) {
211 225 QRectF rect(xPos, yMax-rectHeight, rectWidth, rectHeight);
212 226 layout.append(rect);
213 227 yMax -= rectHeight;
214 228 } else {
215 229 QRectF rect(xPos, yMin-rectHeight, rectWidth, rectHeight);
216 230 layout.append(rect);
217 231 yMin -= rectHeight;
218 232 }
219 233 }
220 234 }
221 235
222 236 //====================================================================================
223 237 // barset 1, bar 0
224 238 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
225 239 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
226 240
227 241 QCOMPARE(seriesSpy.count(), 1);
228 242
229 243 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
230 244 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
231 245 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
232 246 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
233 247
234 248 //====================================================================================
235 249 // barset 1, bar 1
236 250 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
237 251 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
238 252
239 253 QCOMPARE(seriesSpy.count(), 1);
240 254
241 255 seriesSpyArg = seriesSpy.takeFirst();
242 256 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
243 257 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
244 258 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
245 259
246 260 //====================================================================================
247 261 // barset 1, bar 2
248 262 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
249 263 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
250 264
251 265 QCOMPARE(seriesSpy.count(), 1);
252 266
253 267 seriesSpyArg = seriesSpy.takeFirst();
254 268 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
255 269 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
256 270 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
257 271
258 272 //====================================================================================
259 273 // barset 2, bar 0
260 274 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
261 275 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
262 276
263 277 QCOMPARE(seriesSpy.count(), 1);
264 278
265 279 seriesSpyArg = seriesSpy.takeFirst();
266 280 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
267 281 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
268 282 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
269 283
270 284 //====================================================================================
271 285 // barset 2, bar 1
272 286 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
273 287 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
274 288
275 289 QCOMPARE(seriesSpy.count(), 1);
276 290
277 291 seriesSpyArg = seriesSpy.takeFirst();
278 292 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
279 293 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
280 294 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
281 295
282 296 //====================================================================================
283 297 // barset 2, bar 2
284 298 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
285 299 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
286 300
287 301 QCOMPARE(seriesSpy.count(), 1);
288 302
289 303 seriesSpyArg = seriesSpy.takeFirst();
290 304 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
291 305 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
292 306 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
293 307 }
294 308
295 309 void tst_QStackedBarSeries::mousehovered_data()
296 310 {
297 311
298 312 }
299 313
300 314 void tst_QStackedBarSeries::mousehovered()
301 315 {
302 316 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
303 317
304 318 QStackedBarSeries* series = new QStackedBarSeries();
305 319
306 320 QBarSet* set1 = new QBarSet(QString("set 1"));
307 321 *set1 << 10 << 10 << 10;
308 322 series->append(set1);
309 323
310 324 QBarSet* set2 = new QBarSet(QString("set 2"));
311 325 *set2 << 10 << 10 << 10;
312 326 series->append(set2);
313 327
314 328 QList<QBarSet*> barSets = series->barSets();
315 329
316 330 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
317 331 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
318 332 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
319 333
320 334 QChartView view(new QChart());
321 335 view.resize(400,300);
322 336 view.chart()->addSeries(series);
323 337 view.show();
324 338 QTest::qWaitForWindowShown(&view);
325 339
326 340 //this is hack since view does not get events otherwise
327 341 view.setMouseTracking(true);
328 342
329 343 // Calculate expected layout for bars
330 344 QRectF plotArea = view.chart()->plotArea();
331 345 qreal width = plotArea.width();
332 346 qreal height = plotArea.height();
333 347 qreal rangeY = 20; // From 0 to 20 because sets are stacked (this should be height of highest stack)
334 348 qreal rangeX = 3; // 3 values per set
335 349 qreal scaleY = (height / rangeY);
336 350 qreal scaleX = (width / rangeX);
337 351
338 352 qreal setCount = series->count();
339 353 qreal domainMinY = 0; // These come from internal domain used by barseries.
340 354 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
341 355 qreal rectWidth = scaleX * series->barWidth();
342 356
343 357 QVector<QRectF> layout;
344 358
345 359 // 3 = count of values in set
346 360 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
347 361 for (int i = 0; i < 3; i++) {
348 362 qreal yMax = height + scaleY * domainMinY + plotArea.top();
349 363 qreal yMin = height + scaleY * domainMinY + plotArea.top();
350 364 for (int set = 0; set < setCount; set++) {
351 365 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
352 366 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
353 367
354 368 if (rectHeight < 0) {
355 369 QRectF rect(xPos, yMax-rectHeight, rectWidth, rectHeight);
356 370 layout.append(rect);
357 371 yMax -= rectHeight;
358 372 } else {
359 373 QRectF rect(xPos, yMin-rectHeight, rectWidth, rectHeight);
360 374 layout.append(rect);
361 375 yMin -= rectHeight;
362 376 }
363 377 }
364 378 }
365 379
366 380 //=======================================================================
367 381 // move mouse to left border
368 382 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(0).center().y()));
369 383 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
370 384 TRY_COMPARE(seriesIndexSpy.count(), 0);
371 385
372 386 //=======================================================================
373 387 // move mouse on top of set1
374 388 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
375 389 TRY_COMPARE(seriesIndexSpy.count(), 1);
376 390 TRY_COMPARE(setIndexSpy1.count(), 1);
377 391 TRY_COMPARE(setIndexSpy2.count(), 0);
378 392
379 393 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
380 394 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
381 395 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
382 396 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
383 397
384 398 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
385 399 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
386 400 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
387 401
388 402 //=======================================================================
389 403 // move mouse from top of set1 to top of set2
390 404 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
391 405 TRY_COMPARE(seriesIndexSpy.count(), 2);
392 406 TRY_COMPARE(setIndexSpy1.count(), 1);
393 407 TRY_COMPARE(setIndexSpy2.count(), 1);
394 408
395 409 // should leave set1
396 410 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
397 411 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
398 412 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
399 413 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
400 414
401 415 setIndexSpyArg = setIndexSpy1.takeFirst();
402 416 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
403 417 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
404 418
405 419 // should enter set2
406 420 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
407 421 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
408 422 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
409 423 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
410 424
411 425 setIndexSpyArg = setIndexSpy2.takeFirst();
412 426 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
413 427 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
414 428
415 429 //=======================================================================
416 430 // move mouse from top of set2 to background
417 431 QTest::mouseMove(view.viewport(), QPoint(layout.at(1).center().x(), 0));
418 432 TRY_COMPARE(seriesIndexSpy.count(), 1);
419 433 TRY_COMPARE(setIndexSpy1.count(), 0);
420 434 TRY_COMPARE(setIndexSpy2.count(), 1);
421 435
422 436 // should leave set2
423 437 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
424 438 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
425 439 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
426 440 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
427 441
428 442 setIndexSpyArg = setIndexSpy2.takeFirst();
429 443 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
430 444 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
431 445
432 446 //=======================================================================
433 447 // move mouse on top of set1, bar0 to check the index (hover into set1)
434 448 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
435 449
436 450 TRY_COMPARE(seriesIndexSpy.count(), 1);
437 451 TRY_COMPARE(setIndexSpy1.count(), 1);
438 452 TRY_COMPARE(setIndexSpy2.count(), 0);
439 453
440 454 //should enter set1, bar0
441 455 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
442 456 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
443 457 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
444 458 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
445 459 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
446 460 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
447 461
448 462 setIndexSpyArg = setIndexSpy1.takeFirst();
449 463 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
450 464 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
451 465 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
452 466 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
453 467
454 468 //=======================================================================
455 469 // move mouse on top of set2, bar0 to check the index (hover out set1,
456 470 // hover in set2)
457 471 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
458 472
459 473 TRY_COMPARE(seriesIndexSpy.count(), 2);
460 474 TRY_COMPARE(setIndexSpy1.count(), 1);
461 475 TRY_COMPARE(setIndexSpy2.count(), 1);
462 476
463 477 //should leave set1, bar0
464 478 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
465 479 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
466 480 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
467 481 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
468 482 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
469 483 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
470 484
471 485 setIndexSpyArg = setIndexSpy1.takeFirst();
472 486 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
473 487 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
474 488 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
475 489 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
476 490
477 491 //should enter set2, bar0
478 492 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
479 493 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
480 494 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
481 495 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
482 496 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
483 497 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
484 498
485 499 setIndexSpyArg = setIndexSpy2.takeFirst();
486 500 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
487 501 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
488 502 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
489 503 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
490 504
491 505 //=======================================================================
492 506 // move mouse on top of set1, bar1 to check the index (hover out set2,
493 507 // hover in set1)
494 508 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
495 509
496 510 TRY_COMPARE(seriesIndexSpy.count(), 2);
497 511 TRY_COMPARE(setIndexSpy1.count(), 1);
498 512 TRY_COMPARE(setIndexSpy2.count(), 1);
499 513
500 514 //should leave set2, bar0
501 515 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
502 516 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
503 517 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
504 518 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
505 519 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
506 520 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
507 521
508 522 setIndexSpyArg = setIndexSpy2.takeFirst();
509 523 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
510 524 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
511 525 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
512 526 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
513 527
514 528 //should enter set1, bar1
515 529 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
516 530 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
517 531 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
518 532 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
519 533 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
520 534 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
521 535
522 536 setIndexSpyArg = setIndexSpy1.takeFirst();
523 537 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
524 538 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
525 539 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
526 540 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
527 541
528 542 //=======================================================================
529 543 // move mouse between set1 and set2 (hover out set1)
530 544 QTest::mouseMove(view.viewport(), QPoint((layout.at(3).right() + layout.at(4).left()) /2,
531 545 layout.at(2).top()));
532 546
533 547 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
534 548 TRY_COMPARE(seriesIndexSpy.count(), 1);
535 549 TRY_COMPARE(setIndexSpy1.count(), 1);
536 550 TRY_COMPARE(setIndexSpy2.count(), 0);
537 551
538 552 //should leave set1, bar1
539 553 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
540 554 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
541 555 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
542 556 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
543 557 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
544 558 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
545 559
546 560 setIndexSpyArg = setIndexSpy1.takeFirst();
547 561 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
548 562 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
549 563 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
550 564 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
551 565
552 566 //=======================================================================
553 567 // move mouse on top of set2, bar1 to check the index (hover in set2)
554 568 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
555 569
556 570 TRY_COMPARE(seriesIndexSpy.count(), 1);
557 571 TRY_COMPARE(setIndexSpy1.count(), 0);
558 572 TRY_COMPARE(setIndexSpy2.count(), 1);
559 573
560 574 //should enter set2, bar1
561 575 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
562 576 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
563 577 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
564 578 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
565 579 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
566 580 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
567 581
568 582 setIndexSpyArg = setIndexSpy2.takeFirst();
569 583 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
570 584 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
571 585 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
572 586 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
573 587
574 588 //=======================================================================
575 589 // move mouse between set1 and set2 (hover out set2)
576 590 QTest::mouseMove(view.viewport(), QPoint((layout.at(3).right() + layout.at(4).left()) /2,
577 591 layout.at(2).top()));
578 592
579 593 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
580 594 TRY_COMPARE(seriesIndexSpy.count(), 1);
581 595 TRY_COMPARE(setIndexSpy1.count(), 0);
582 596 TRY_COMPARE(setIndexSpy2.count(), 1);
583 597
584 598 //should leave set1, bar1
585 599 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
586 600 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
587 601 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
588 602 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
589 603 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
590 604 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
591 605
592 606 setIndexSpyArg = setIndexSpy2.takeFirst();
593 607 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
594 608 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
595 609 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
596 610 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
597 611 }
598 612
599 613 void tst_QStackedBarSeries::mousePressed()
600 614 {
601 615 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
602 616
603 617 QStackedBarSeries* series = new QStackedBarSeries();
604 618
605 619 QBarSet* set1 = new QBarSet(QString("set 1"));
606 620 *set1 << 10 << 10 << 10;
607 621 series->append(set1);
608 622
609 623 QBarSet* set2 = new QBarSet(QString("set 2"));
610 624 *set2 << 10 << 10 << 10;
611 625 series->append(set2);
612 626 QList<QBarSet*> barSets = series->barSets();
613 627
614 628 QSignalSpy seriesSpy(series,SIGNAL(pressed(int,QBarSet*)));
615 629 QSignalSpy setSpy1(set1, SIGNAL(pressed(int)));
616 630 QSignalSpy setSpy2(set2, SIGNAL(pressed(int)));
617 631
618 632 QChartView view(new QChart());
619 633 view.resize(400,300);
620 634 view.chart()->addSeries(series);
621 635 view.show();
622 636 QTest::qWaitForWindowShown(&view);
623 637
624 638 // Calculate expected layout for bars
625 639 QRectF plotArea = view.chart()->plotArea();
626 640 qreal width = plotArea.width();
627 641 qreal height = plotArea.height();
628 642 qreal rangeY = 20; // From 0 to 20 because sets are stacked (this should be height of highest stack)
629 643 qreal rangeX = 3; // 3 values per set
630 644 qreal scaleY = (height / rangeY);
631 645 qreal scaleX = (width / rangeX);
632 646
633 647 qreal setCount = series->count();
634 648 qreal domainMinY = 0; // These come from internal domain used by barseries.
635 649 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
636 650 qreal rectWidth = scaleX * series->barWidth();
637 651
638 652 QVector<QRectF> layout;
639 653
640 654 // 3 = count of values in set
641 655 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
642 656 for (int i = 0; i < 3; i++) {
643 657 qreal yMax = height + scaleY * domainMinY + plotArea.top();
644 658 qreal yMin = height + scaleY * domainMinY + plotArea.top();
645 659 for (int set = 0; set < setCount; set++) {
646 660 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
647 661 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
648 662 if (rectHeight < 0) {
649 663 QRectF rect(xPos, yMax-rectHeight, rectWidth, rectHeight);
650 664 layout.append(rect);
651 665 yMax -= rectHeight;
652 666 } else {
653 667 QRectF rect(xPos, yMin-rectHeight, rectWidth, rectHeight);
654 668 layout.append(rect);
655 669 yMin -= rectHeight;
656 670 }
657 671 }
658 672 }
659 673
660 674 //====================================================================================
661 675 // barset 1, bar 0
662 676 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
663 677 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
664 678
665 679 QCOMPARE(seriesSpy.count(), 1);
666 680 QCOMPARE(setSpy1.count(), 1);
667 681 QCOMPARE(setSpy2.count(), 0);
668 682
669 683 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
670 684 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
671 685 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
672 686 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
673 687
674 688 QList<QVariant> setSpyArg = setSpy1.takeFirst();
675 689 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
676 690 QVERIFY(setSpyArg.at(0).toInt() == 0);
677 691
678 692 //====================================================================================
679 693 // barset 1, bar 1
680 694 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
681 695 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
682 696
683 697 QCOMPARE(seriesSpy.count(), 1);
684 698 QCOMPARE(setSpy1.count(), 1);
685 699 QCOMPARE(setSpy2.count(), 0);
686 700
687 701 seriesSpyArg = seriesSpy.takeFirst();
688 702 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
689 703 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
690 704 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
691 705
692 706 setSpyArg = setSpy1.takeFirst();
693 707 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
694 708 QVERIFY(setSpyArg.at(0).toInt() == 1);
695 709
696 710 //====================================================================================
697 711 // barset 1, bar 2
698 712 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
699 713 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
700 714
701 715 QCOMPARE(seriesSpy.count(), 1);
702 716 QCOMPARE(setSpy1.count(), 1);
703 717 QCOMPARE(setSpy2.count(), 0);
704 718
705 719 seriesSpyArg = seriesSpy.takeFirst();
706 720 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
707 721 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
708 722 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
709 723
710 724 setSpyArg = setSpy1.takeFirst();
711 725 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
712 726 QVERIFY(setSpyArg.at(0).toInt() == 2);
713 727
714 728 //====================================================================================
715 729 // barset 2, bar 0
716 730 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
717 731 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
718 732
719 733 QCOMPARE(seriesSpy.count(), 1);
720 734 QCOMPARE(setSpy1.count(), 0);
721 735 QCOMPARE(setSpy2.count(), 1);
722 736
723 737 seriesSpyArg = seriesSpy.takeFirst();
724 738 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
725 739 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
726 740 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
727 741
728 742 setSpyArg = setSpy2.takeFirst();
729 743 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
730 744 QVERIFY(setSpyArg.at(0).toInt() == 0);
731 745
732 746 //====================================================================================
733 747 // barset 2, bar 1
734 748 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
735 749 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
736 750
737 751 QCOMPARE(seriesSpy.count(), 1);
738 752 QCOMPARE(setSpy1.count(), 0);
739 753 QCOMPARE(setSpy2.count(), 1);
740 754
741 755 seriesSpyArg = seriesSpy.takeFirst();
742 756 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
743 757 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
744 758 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
745 759
746 760 setSpyArg = setSpy2.takeFirst();
747 761 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
748 762 QVERIFY(setSpyArg.at(0).toInt() == 1);
749 763
750 764 //====================================================================================
751 765 // barset 2, bar 2
752 766 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
753 767 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
754 768
755 769 QCOMPARE(seriesSpy.count(), 1);
756 770 QCOMPARE(setSpy1.count(), 0);
757 771 QCOMPARE(setSpy2.count(), 1);
758 772
759 773 seriesSpyArg = seriesSpy.takeFirst();
760 774 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
761 775 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
762 776 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
763 777
764 778 setSpyArg = setSpy2.takeFirst();
765 779 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
766 780 QVERIFY(setSpyArg.at(0).toInt() == 2);
767 781 }
768 782
769 783 void tst_QStackedBarSeries::mouseReleased()
770 784 {
771 785 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
772 786
773 787 QStackedBarSeries* series = new QStackedBarSeries();
774 788
775 789 QBarSet* set1 = new QBarSet(QString("set 1"));
776 790 *set1 << 10 << 10 << 10;
777 791 series->append(set1);
778 792
779 793 QBarSet* set2 = new QBarSet(QString("set 2"));
780 794 *set2 << 10 << 10 << 10;
781 795 series->append(set2);
782 796 QList<QBarSet*> barSets = series->barSets();
783 797
784 798 QSignalSpy seriesSpy(series,SIGNAL(released(int,QBarSet*)));
785 799 QSignalSpy setSpy1(set1, SIGNAL(released(int)));
786 800 QSignalSpy setSpy2(set2, SIGNAL(released(int)));
787 801
788 802 QChartView view(new QChart());
789 803 view.resize(400,300);
790 804 view.chart()->addSeries(series);
791 805 view.show();
792 806 QTest::qWaitForWindowShown(&view);
793 807
794 808 // Calculate expected layout for bars
795 809 QRectF plotArea = view.chart()->plotArea();
796 810 qreal width = plotArea.width();
797 811 qreal height = plotArea.height();
798 812 qreal rangeY = 20; // From 0 to 20 because sets are stacked (this should be height of highest stack)
799 813 qreal rangeX = 3; // 3 values per set
800 814 qreal scaleY = (height / rangeY);
801 815 qreal scaleX = (width / rangeX);
802 816
803 817 qreal setCount = series->count();
804 818 qreal domainMinY = 0; // These come from internal domain used by barseries.
805 819 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
806 820 qreal rectWidth = scaleX * series->barWidth();
807 821
808 822 QVector<QRectF> layout;
809 823
810 824 // 3 = count of values in set
811 825 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
812 826 for (int i = 0; i < 3; i++) {
813 827 qreal yMax = height + scaleY * domainMinY + plotArea.top();
814 828 qreal yMin = height + scaleY * domainMinY + plotArea.top();
815 829 for (int set = 0; set < setCount; set++) {
816 830 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
817 831 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
818 832 if (rectHeight < 0) {
819 833 QRectF rect(xPos, yMax-rectHeight, rectWidth, rectHeight);
820 834 layout.append(rect);
821 835 yMax -= rectHeight;
822 836 } else {
823 837 QRectF rect(xPos, yMin-rectHeight, rectWidth, rectHeight);
824 838 layout.append(rect);
825 839 yMin -= rectHeight;
826 840 }
827 841 }
828 842 }
829 843
830 844 //====================================================================================
831 845 // barset 1, bar 0
832 846 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
833 847 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
834 848
835 849 QCOMPARE(seriesSpy.count(), 1);
836 850 QCOMPARE(setSpy1.count(), 1);
837 851 QCOMPARE(setSpy2.count(), 0);
838 852
839 853 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
840 854 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
841 855 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
842 856 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
843 857
844 858 QList<QVariant> setSpyArg = setSpy1.takeFirst();
845 859 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
846 860 QVERIFY(setSpyArg.at(0).toInt() == 0);
847 861
848 862 //====================================================================================
849 863 // barset 1, bar 1
850 864 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
851 865 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
852 866
853 867 QCOMPARE(seriesSpy.count(), 1);
854 868 QCOMPARE(setSpy1.count(), 1);
855 869 QCOMPARE(setSpy2.count(), 0);
856 870
857 871 seriesSpyArg = seriesSpy.takeFirst();
858 872 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
859 873 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
860 874 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
861 875
862 876 setSpyArg = setSpy1.takeFirst();
863 877 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
864 878 QVERIFY(setSpyArg.at(0).toInt() == 1);
865 879
866 880 //====================================================================================
867 881 // barset 1, bar 2
868 882 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
869 883 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
870 884
871 885 QCOMPARE(seriesSpy.count(), 1);
872 886 QCOMPARE(setSpy1.count(), 1);
873 887 QCOMPARE(setSpy2.count(), 0);
874 888
875 889 seriesSpyArg = seriesSpy.takeFirst();
876 890 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
877 891 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
878 892 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
879 893
880 894 setSpyArg = setSpy1.takeFirst();
881 895 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
882 896 QVERIFY(setSpyArg.at(0).toInt() == 2);
883 897
884 898 //====================================================================================
885 899 // barset 2, bar 0
886 900 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
887 901 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
888 902
889 903 QCOMPARE(seriesSpy.count(), 1);
890 904 QCOMPARE(setSpy1.count(), 0);
891 905 QCOMPARE(setSpy2.count(), 1);
892 906
893 907 seriesSpyArg = seriesSpy.takeFirst();
894 908 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
895 909 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
896 910 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
897 911
898 912 setSpyArg = setSpy2.takeFirst();
899 913 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
900 914 QVERIFY(setSpyArg.at(0).toInt() == 0);
901 915
902 916 //====================================================================================
903 917 // barset 2, bar 1
904 918 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
905 919 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
906 920
907 921 QCOMPARE(seriesSpy.count(), 1);
908 922 QCOMPARE(setSpy1.count(), 0);
909 923 QCOMPARE(setSpy2.count(), 1);
910 924
911 925 seriesSpyArg = seriesSpy.takeFirst();
912 926 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
913 927 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
914 928 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
915 929
916 930 setSpyArg = setSpy2.takeFirst();
917 931 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
918 932 QVERIFY(setSpyArg.at(0).toInt() == 1);
919 933
920 934 //====================================================================================
921 935 // barset 2, bar 2
922 936 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
923 937 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
924 938
925 939 QCOMPARE(seriesSpy.count(), 1);
926 940 QCOMPARE(setSpy1.count(), 0);
927 941 QCOMPARE(setSpy2.count(), 1);
928 942
929 943 seriesSpyArg = seriesSpy.takeFirst();
930 944 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
931 945 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
932 946 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
933 947
934 948 setSpyArg = setSpy2.takeFirst();
935 949 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
936 950 QVERIFY(setSpyArg.at(0).toInt() == 2);
937 951 }
938 952
939 953 void tst_QStackedBarSeries::mouseDoubleClicked()
940 954 {
941 955 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
942 956
943 957 QStackedBarSeries* series = new QStackedBarSeries();
944 958
945 959 QBarSet* set1 = new QBarSet(QString("set 1"));
946 960 *set1 << 10 << 10 << 10;
947 961 series->append(set1);
948 962
949 963 QBarSet* set2 = new QBarSet(QString("set 2"));
950 964 *set2 << 10 << 10 << 10;
951 965 series->append(set2);
952 966 QList<QBarSet*> barSets = series->barSets();
953 967
954 968 QSignalSpy seriesSpy(series,SIGNAL(doubleClicked(int,QBarSet*)));
955 969 QSignalSpy setSpy1(set1, SIGNAL(doubleClicked(int)));
956 970 QSignalSpy setSpy2(set2, SIGNAL(doubleClicked(int)));
957 971
958 972 QChartView view(new QChart());
959 973 view.resize(400,300);
960 974 view.chart()->addSeries(series);
961 975 view.show();
962 976 QTest::qWaitForWindowShown(&view);
963 977
964 978 // Calculate expected layout for bars
965 979 QRectF plotArea = view.chart()->plotArea();
966 980 qreal width = plotArea.width();
967 981 qreal height = plotArea.height();
968 982 qreal rangeY = 20; // From 0 to 20 because sets are stacked (this should be height of highest stack)
969 983 qreal rangeX = 3; // 3 values per set
970 984 qreal scaleY = (height / rangeY);
971 985 qreal scaleX = (width / rangeX);
972 986
973 987 qreal setCount = series->count();
974 988 qreal domainMinY = 0; // These come from internal domain used by barseries.
975 989 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
976 990 qreal rectWidth = scaleX * series->barWidth();
977 991
978 992 QVector<QRectF> layout;
979 993
980 994 // 3 = count of values in set
981 995 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
982 996 for (int i = 0; i < 3; i++) {
983 997 qreal yMax = height + scaleY * domainMinY + plotArea.top();
984 998 qreal yMin = height + scaleY * domainMinY + plotArea.top();
985 999 for (int set = 0; set < setCount; set++) {
986 1000 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
987 1001 qreal rectHeight = barSets.at(set)->at(i) * scaleY;
988 1002 if (rectHeight < 0) {
989 1003 QRectF rect(xPos, yMax-rectHeight, rectWidth, rectHeight);
990 1004 layout.append(rect);
991 1005 yMax -= rectHeight;
992 1006 } else {
993 1007 QRectF rect(xPos, yMin-rectHeight, rectWidth, rectHeight);
994 1008 layout.append(rect);
995 1009 yMin -= rectHeight;
996 1010 }
997 1011 }
998 1012 }
999 1013
1000 1014 // barset 1, bar 0
1001 1015 QTest::mouseDClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
1002 1016 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
1003 1017
1004 1018 QCOMPARE(seriesSpy.count(), 1);
1005 1019 QCOMPARE(setSpy1.count(), 1);
1006 1020 QCOMPARE(setSpy2.count(), 0);
1007 1021
1008 1022 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
1009 1023 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
1010 1024 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
1011 1025 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
1012 1026
1013 1027 QList<QVariant> setSpyArg = setSpy1.takeFirst();
1014 1028 QVERIFY(setSpyArg.at(0).type() == QVariant::Int);
1015 1029 QVERIFY(setSpyArg.at(0).toInt() == 0);
1016 1030 }
1017 1031 QTEST_MAIN(tst_QStackedBarSeries)
1018 1032
1019 1033 #include "tst_qstackedbarseries.moc"
1020 1034
@@ -1,197 +1,205
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2015 The Qt Company Ltd
4 4 ** All rights reserved.
5 5 ** For any questions to The Qt Company, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and The Qt Company.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 import QtQuick 2.0
20 20
21 21 Row {
22 22 anchors.fill: parent
23 23 spacing: 5
24 24 property variant series
25 25
26 26 // buttons for selecting the edited object: series, barset or label
27 27 Flow {
28 28 spacing: 5
29 29 flow: Flow.TopToBottom
30 30 Button {
31 31 id: seriesButton
32 32 text: "series"
33 33 unpressedColor: "#79bd8f"
34 34 onClicked: {
35 35 seriesFlow.visible = true;
36 36 setFlow.visible = false;
37 37 labelsFlow.visible = false;
38 38 color = "#00a388";
39 39 setButton.color = "#79bd8f";
40 40 labelButton.color = "#79bd8f";
41 41 }
42 42 }
43 43 Button {
44 44 id: setButton
45 45 text: "BarSet"
46 46 unpressedColor: "#79bd8f"
47 47 onClicked: {
48 48 seriesFlow.visible = false;
49 49 setFlow.visible = true;
50 50 labelsFlow.visible = false;
51 51 color = "#00a388";
52 52 seriesButton.color = "#79bd8f";
53 53 labelButton.color = "#79bd8f";
54 54 }
55 55 }
56 56 Button {
57 57 id: labelButton
58 58 text: "label"
59 59 unpressedColor: "#79bd8f"
60 60 onClicked: {
61 61 seriesFlow.visible = false;
62 62 setFlow.visible = false;
63 63 labelsFlow.visible = true;
64 64 color = "#00a388";
65 65 seriesButton.color = "#79bd8f";
66 66 setButton.color = "#79bd8f";
67 67 }
68 68 }
69 69 }
70 70
71 71 // Buttons for editing series
72 72 Flow {
73 73 id: seriesFlow
74 74 spacing: 5
75 75 flow: Flow.TopToBottom
76 76 visible: false
77 77
78 78 Button {
79 79 text: "visible"
80 80 onClicked: series.visible = !series.visible;
81 81 }
82 82 Button {
83 83 text: "opacity +"
84 84 onClicked: series.opacity += 0.1;
85 85 }
86 86 Button {
87 87 text: "opacity -"
88 88 onClicked: series.opacity -= 0.1;
89 89 }
90 90 Button {
91 91 text: "bar width +"
92 92 onClicked: series.barWidth += 0.1;
93 93 }
94 94 Button {
95 95 text: "bar width -"
96 96 onClicked: series.barWidth -= 0.1;
97 97 }
98 98 }
99 99
100 100 // Buttons for editing sets
101 101 Flow {
102 102 id: setFlow
103 103 spacing: 5
104 104 flow: Flow.TopToBottom
105 105 visible: false
106 106
107 107 Button {
108 108 text: "append set"
109 109 onClicked: {
110 110 var count = series.count;
111 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 114 Button {
115 115 text: "insert set"
116 116 onClicked: {
117 117 var count = series.count;
118 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 121 Button {
122 122 text: "remove set"
123 123 onClicked: series.remove(series.at(series.count - 1));
124 124 }
125 125 Button {
126 126 text: "clear sets"
127 127 onClicked: series.clear();
128 128 }
129 129
130 130 Button {
131 131 text: "set 1 append"
132 132 onClicked: series.at(0).append(series.at(0).count + 1);
133 133 }
134 134 Button {
135 135 text: "set 1 replace"
136 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 138 Button {
139 139 text: "set 1 remove"
140 140 onClicked: series.at(0).remove(series.at(0).count - 1);
141 141 }
142 142
143 143 Button {
144 144 text: "set 1 color"
145 145 onClicked: series.at(0).color = main.nextColor();
146 146 }
147 147 Button {
148 148 text: "set 1 border color"
149 149 onClicked: series.at(0).borderColor = main.nextColor();
150 150 }
151 151 Button {
152 152 text: "set 1 borderWidth +"
153 153 onClicked: series.at(0).borderWidth += 0.5;
154 154 }
155 155 Button {
156 156 text: "set 1 borderWidth -"
157 157 onClicked: series.at(0).borderWidth -= 0.5;
158 158 }
159 159 }
160 160
161 161
162 162 Flow {
163 163 id: labelsFlow
164 164 spacing: 5
165 165 flow: Flow.TopToBottom
166 166 visible: false
167 167
168 168 Button {
169 169 text: "labels visible"
170 170 onClicked: series.labelsVisible = !series.labelsVisible;
171 171 }
172 172 Button {
173 173 text: "labels format"
174 174 onClicked: {
175 175 if (series.labelsFormat === "@value")
176 176 series.labelsFormat = "@value%"
177 177 else
178 178 series.labelsFormat = "@value"
179 179 }
180 180 }
181 181 Button {
182 182 text: "labels position"
183 183 onClicked: series.changeLabelsPosition();
184 184 }
185 185 Button {
186 186 text: "set 1 label color"
187 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 197 FontEditor {
190 198 id: fontEditor
191 199 fontDescription: "label"
192 200 function editedFont() {
193 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