##// END OF EJS Templates
Fix assert failure with percent bar series...
Titta Heikkala -
r2690:6c409268c150
parent child
Show More
@@ -1,134 +1,141
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2014 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "horizontalpercentbarchartitem_p.h"
22 22 #include "qabstractbarseries_p.h"
23 23 #include "qbarset_p.h"
24 24 #include "bar_p.h"
25 25
26 26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 27
28 28 HorizontalPercentBarChartItem::HorizontalPercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item)
29 29 : AbstractBarChartItem(series, item)
30 30 {
31 31 }
32 32
33 33 void HorizontalPercentBarChartItem::initializeLayout()
34 34 {
35 35 qreal categoryCount = m_series->d_func()->categoryCount();
36 36 qreal setCount = m_series->count();
37 37 qreal barWidth = m_series->d_func()->barWidth();
38 38
39 39 m_layout.clear();
40 40 for(int category = 0; category < categoryCount; category++) {
41 41 for (int set = 0; set < setCount; set++) {
42 42 QRectF rect;
43 43 QPointF topLeft;
44 44 QPointF bottomRight;
45 45 if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) {
46 46 topLeft = domain()->calculateGeometryPoint(QPointF(domain()->minX(), category - barWidth / 2), m_validData);
47 47 bottomRight = domain()->calculateGeometryPoint(QPointF(domain()->minX(), category + barWidth / 2), m_validData);
48 48 } else {
49 49 topLeft = domain()->calculateGeometryPoint(QPointF(0, category - barWidth / 2), m_validData);
50 50 bottomRight = domain()->calculateGeometryPoint(QPointF(0, category + barWidth / 2), m_validData);
51 51 }
52 52
53 53 if (!m_validData)
54 54 return;
55 55
56 56 rect.setTopLeft(topLeft);
57 57 rect.setBottomRight(bottomRight);
58 58 m_layout.append(rect.normalized());
59 59 }
60 60 }
61 61 }
62 62
63 63 QVector<QRectF> HorizontalPercentBarChartItem::calculateLayout()
64 64 {
65 65 QVector<QRectF> layout;
66 66
67 67 // Use temporary qreals for accuracy
68 68 qreal categoryCount = m_series->d_func()->categoryCount();
69 69 qreal setCount = m_series->count();
70 70 qreal barWidth = m_series->d_func()->barWidth();
71 71
72 72 for(int category = 0; category < categoryCount; category++) {
73 73 qreal sum = 0;
74 74 qreal categorySum = m_series->d_func()->categorySum(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 qreal topX = 0;
79 if (sum > 0)
80 topX = 100 * sum / categorySum;
81 qreal bottomX = 0;
82 qreal newSum = value + sum;
83 if (newSum > 0)
84 bottomX = 100 * newSum / categorySum;
78 85 QPointF topLeft;
79 86 if (domain()->type() == AbstractDomain::LogXYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
80 topLeft = domain()->calculateGeometryPoint(QPointF(set ? 100 * sum/categorySum : domain()->minX(), category - barWidth/2), m_validData);
87 topLeft = domain()->calculateGeometryPoint(QPointF(set ? topX : domain()->minX(), category - barWidth/2), m_validData);
81 88 else
82 topLeft = domain()->calculateGeometryPoint(QPointF(set ? 100 * sum/categorySum : 0, category - barWidth/2), m_validData);
83 QPointF bottomRight = domain()->calculateGeometryPoint(QPointF(100 * (value + sum)/categorySum, category + barWidth/2), m_validData);
89 topLeft = domain()->calculateGeometryPoint(QPointF(set ? topX : 0, category - barWidth/2), m_validData);
90 QPointF bottomRight = domain()->calculateGeometryPoint(QPointF(bottomX, category + barWidth/2), m_validData);
84 91
85 92 rect.setTopLeft(topLeft);
86 93 rect.setBottomRight(bottomRight);
87 94 layout.append(rect.normalized());
88 sum +=value;
95 sum = newSum;
89 96 }
90 97 }
91 98 return layout;
92 99 }
93 100
94 101 void HorizontalPercentBarChartItem::handleUpdatedBars()
95 102 {
96 103 // Handle changes in pen, brush, labels etc.
97 104 int categoryCount = m_series->d_func()->categoryCount();
98 105 int setCount = m_series->count();
99 106 int itemIndex(0);
100 107 static const QString valueTag(QLatin1String("@value"));
101 108
102 109 for (int category = 0; category < categoryCount; category++) {
103 110 for (int set = 0; set < setCount; set++) {
104 111 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
105 112 Bar *bar = m_bars.at(itemIndex);
106 113 bar->setPen(barSet->m_pen);
107 114 bar->setBrush(barSet->m_brush);
108 115 bar->update();
109 116
110 117 QGraphicsTextItem *label = m_labels.at(itemIndex);
111 118 int p = m_series->d_func()->percentageAt(set, category) * 100;
112 119 QString vString(QString::number(p));
113 120 vString.truncate(3);
114 121 vString.append("%");
115 122 QString valueLabel;
116 123 if (m_series->labelsFormat().isEmpty()) {
117 124 valueLabel = vString;
118 125 } else {
119 126 valueLabel = m_series->labelsFormat();
120 127 valueLabel.replace(valueTag, QString::number(barSet->value(category)));
121 128 }
122 129 label->setHtml(valueLabel);
123 130 label->setFont(barSet->m_labelFont);
124 131 label->setDefaultTextColor(barSet->m_labelBrush.color());
125 132 label->update();
126 133 itemIndex++;
127 134 }
128 135 }
129 136 }
130 137
131 138 #include "moc_horizontalpercentbarchartitem_p.cpp"
132 139
133 140 QTCOMMERCIALCHART_END_NAMESPACE
134 141
@@ -1,121 +1,124
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2014 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20 #include "qhorizontalpercentbarseries.h"
21 21 #include "qhorizontalpercentbarseries_p.h"
22 22 #include "horizontalpercentbarchartitem_p.h"
23 23
24 24 #include "chartdataset_p.h"
25 25 #include "charttheme_p.h"
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 /*!
30 30 \class QHorizontalPercentBarSeries
31 31 \inmodule Qt Charts
32 32 \brief Series for creating horizontal percent bar chart.
33 33 \mainclass
34 34
35 QHorizontalPercentBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars
36 as groups, where bars in same category are grouped next to each other. QHorizontalPercentBarSeries groups the data
37 from sets to categories, which are defined by a QStringList.
35 QHorizontalPercentBarSeries represents a series of data shown as bars. The purpose of this
36 class is to draw bars as groups, where bars in same category are grouped next to each other.
37 QHorizontalPercentBarSeries groups the data from sets to categories, which are defined by a
38 QStringList. Bars with zero value are not drawn.
38 39
39 See the \l {HorizontalPercentBarChart Example} {horizontal percent bar chart example} to learn how to create a horizontal percent bar chart.
40 See the \l {HorizontalPercentBarChart Example} {horizontal percent bar chart example} to learn
41 how to create a horizontal percent bar chart.
40 42 \image examples_horizontalpercentbarchart.png
41 43
42 \sa QBarSet, QBarSeries, QPercentBarSeries, QAbstractBarSeries, QStackedBarSeries, QHorizontalStackedBarSeries, QHorizontalBarSeries
44 \sa QBarSet, QBarSeries, QPercentBarSeries, QAbstractBarSeries, QStackedBarSeries,
45 QHorizontalStackedBarSeries, QHorizontalBarSeries
43 46 */
44 47 #ifdef QDOC_QT5
45 48 /*!
46 49 \qmltype HorizontalPercentBarSeries
47 50 \instantiates QHorizontalPercentBarSeries
48 51 \inqmlmodule QtCommercial.Chart
49 52
50 53 \include doc/src/horizontalpercentbarseries.qdocinc
51 54 */
52 55 #else
53 56 /*!
54 57 \qmlclass HorizontalPercentBarSeries QHorizontalPercentBarSeries
55 58
56 59 \include ../doc/src/horizontalpercentbarseries.qdocinc
57 60 */
58 61 #endif
59 62
60 63 /*!
61 64 Constructs empty QHorizontalPercentBarSeries.
62 65 QHorizontalPercentBarSeries is QObject which is a child of a \a parent.
63 66 */
64 67 QHorizontalPercentBarSeries::QHorizontalPercentBarSeries(QObject *parent) :
65 68 QAbstractBarSeries(*new QHorizontalPercentBarSeriesPrivate(this), parent)
66 69 {
67 70 }
68 71
69 72 /*!
70 73 Returns QAbstractSeries::SeriesTypeHorizontalPercentBar.
71 74 */
72 75 QAbstractSeries::SeriesType QHorizontalPercentBarSeries::type() const
73 76 {
74 77 return QAbstractSeries::SeriesTypeHorizontalPercentBar;
75 78 }
76 79
77 80 /*!
78 81 Destructor.
79 82 Removes series from chart.
80 83 */
81 84 QHorizontalPercentBarSeries::~QHorizontalPercentBarSeries()
82 85 {
83 86 Q_D(QHorizontalPercentBarSeries);
84 87 if (d->m_chart)
85 88 d->m_chart->removeSeries(this);
86 89 }
87 90
88 91 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
89 92
90 93 QHorizontalPercentBarSeriesPrivate::QHorizontalPercentBarSeriesPrivate(QHorizontalPercentBarSeries *q) : QAbstractBarSeriesPrivate(q)
91 94 {
92 95
93 96 }
94 97
95 98 void QHorizontalPercentBarSeriesPrivate::initializeDomain()
96 99 {
97 100 qreal minX(domain()->minX());
98 101 qreal minY(domain()->minY());
99 102 qreal maxX(domain()->maxX());
100 103 qreal maxY(domain()->maxY());
101 104
102 105 qreal y = categoryCount();
103 106 minX = 0;
104 107 maxX = 100;
105 108 minY = qMin(minY, - (qreal)0.5);
106 109 maxY = qMax(maxY, y - (qreal)0.5);
107 110
108 111 domain()->setRange(minX, maxX, minY, maxY);
109 112 }
110 113
111 114 void QHorizontalPercentBarSeriesPrivate::initializeGraphics(QGraphicsItem* parent)
112 115 {
113 116 Q_Q(QHorizontalPercentBarSeries);
114 117 HorizontalPercentBarChartItem *bar = new HorizontalPercentBarChartItem(q,parent);
115 118 m_item.reset(bar);
116 119 QAbstractSeriesPrivate::initializeGraphics(parent);
117 120 }
118 121
119 122 #include "moc_qhorizontalpercentbarseries.cpp"
120 123
121 124 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,164 +1,171
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2014 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "percentbarchartitem_p.h"
22 22 #include "bar_p.h"
23 23 #include "qabstractbarseries_p.h"
24 24 #include "qbarset.h"
25 25 #include "qbarset_p.h"
26 26
27 27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 28
29 29 PercentBarChartItem::PercentBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
30 30 AbstractBarChartItem(series, item)
31 31 {
32 32 connect(series, SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)),
33 33 this, SLOT(handleLabelsPositionChanged()));
34 34 connect(series, SIGNAL(labelsFormatChanged(QString)), this, SLOT(positionLabels()));
35 35 }
36 36
37 37 void PercentBarChartItem::initializeLayout()
38 38 {
39 39 qreal categoryCount = m_series->d_func()->categoryCount();
40 40 qreal setCount = m_series->count();
41 41 qreal barWidth = m_series->d_func()->barWidth();
42 42
43 43 m_layout.clear();
44 44 for(int category = 0; category < categoryCount; category++) {
45 45 for (int set = 0; set < setCount; set++) {
46 46 QRectF rect;
47 47 QPointF topLeft;
48 48 QPointF bottomRight;
49 49
50 50 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain) {
51 51 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, domain()->minY()), m_validData);
52 52 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, domain()->minY()), m_validData);
53 53 } else {
54 54 topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth / 2, 0), m_validData);
55 55 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth / 2, 0), m_validData);
56 56 }
57 57
58 58 if (!m_validData)
59 59 return;
60 60
61 61 rect.setTopLeft(topLeft);
62 62 rect.setBottomRight(bottomRight);
63 63 m_layout.append(rect.normalized());
64 64 }
65 65 }
66 66 }
67 67
68 68 QVector<QRectF> PercentBarChartItem::calculateLayout()
69 69 {
70 70 QVector<QRectF> layout;
71 71
72 72 // Use temporary qreals for accuracy
73 73 qreal categoryCount = m_series->d_func()->categoryCount();
74 74 qreal setCount = m_series->count();
75 75 qreal barWidth = m_series->d_func()->barWidth();
76 76
77 77 for(int category = 0; category < categoryCount; category++) {
78 78 qreal sum = 0;
79 79 qreal categorySum = m_series->d_func()->categorySum(category);
80 80 for (int set = 0; set < setCount; set++) {
81 81 qreal value = m_series->barSets().at(set)->at(category);
82 82 QRectF rect;
83 QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth/2, 100 * (value + sum)/categorySum), m_validData);
83 qreal topY = 0;
84 qreal newSum = value + sum;
85 if (newSum > 0)
86 topY = 100 * newSum / categorySum;
87 qreal bottomY = 0;
88 if (sum > 0)
89 bottomY = 100 * sum / categorySum;
90 QPointF topLeft = domain()->calculateGeometryPoint(QPointF(category - barWidth/2, topY), m_validData);
84 91 QPointF bottomRight;
85 92 if (domain()->type() == AbstractDomain::XLogYDomain || domain()->type() == AbstractDomain::LogXLogYDomain)
86 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? 100 * sum/categorySum : domain()->minY()), m_validData);
93 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? bottomY : domain()->minY()), m_validData);
87 94 else
88 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? 100 * sum/categorySum : 0), m_validData);
95 bottomRight = domain()->calculateGeometryPoint(QPointF(category + barWidth/2, set ? bottomY : 0), m_validData);
89 96
90 97 rect.setTopLeft(topLeft);
91 98 rect.setBottomRight(bottomRight);
92 99 layout.append(rect.normalized());
93 sum +=value;
100 sum = newSum;
94 101 }
95 102 }
96 103 return layout;
97 104 }
98 105
99 106 void PercentBarChartItem::handleUpdatedBars()
100 107 {
101 108 // Handle changes in pen, brush, labels etc.
102 109 int categoryCount = m_series->d_func()->categoryCount();
103 110 int setCount = m_series->count();
104 111 int itemIndex(0);
105 112 static const QString valueTag(QLatin1String("@value"));
106 113
107 114 for (int category = 0; category < categoryCount; category++) {
108 115 for (int set = 0; set < setCount; set++) {
109 116 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
110 117 Bar *bar = m_bars.at(itemIndex);
111 118 bar->setPen(barSet->m_pen);
112 119 bar->setBrush(barSet->m_brush);
113 120 bar->update();
114 121
115 122 QGraphicsTextItem *label = m_labels.at(itemIndex);
116 123 int p = m_series->d_func()->percentageAt(set, category) * 100;
117 124 QString vString(QString::number(p));
118 125 vString.truncate(3);
119 126 vString.append("%");
120 127 QString valueLabel;
121 128 if (m_series->labelsFormat().isEmpty()) {
122 129 valueLabel = vString;
123 130 } else {
124 131 valueLabel = m_series->labelsFormat();
125 132 valueLabel.replace(valueTag, QString::number(barSet->value(category)));
126 133 }
127 134 label->setHtml(valueLabel);
128 135 label->setFont(barSet->m_labelFont);
129 136 label->setDefaultTextColor(barSet->m_labelBrush.color());
130 137 label->update();
131 138 itemIndex++;
132 139 }
133 140 }
134 141 }
135 142
136 143 void PercentBarChartItem::handleLabelsPositionChanged()
137 144 {
138 145 positionLabels();
139 146 }
140 147
141 148 void PercentBarChartItem::positionLabels()
142 149 {
143 150 for (int i = 0; i < m_layout.count(); i++) {
144 151 QGraphicsTextItem *label = m_labels.at(i);
145 152 qreal xPos = m_layout.at(i).center().x() - label->boundingRect().center().x();
146 153 qreal yPos = 0;
147 154
148 155 if (m_series->labelsPosition() == QAbstractBarSeries::LabelsCenter)
149 156 yPos = m_layout.at(i).center().y() - label->boundingRect().center().y();
150 157 else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsInsideEnd)
151 158 yPos = m_layout.at(i).top();
152 159 else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsInsideBase)
153 160 yPos = m_layout.at(i).bottom() - label->boundingRect().height();
154 161 else if (m_series->labelsPosition() == QAbstractBarSeries::LabelsOutsideEnd)
155 162 yPos = m_layout.at(i).top() - label->boundingRect().height();
156 163
157 164 label->setPos(xPos, yPos);
158 165 label->setZValue(zValue() + 1);
159 166 }
160 167 }
161 168
162 169 #include "moc_percentbarchartitem_p.cpp"
163 170
164 171 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,123 +1,125
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2014 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "qpercentbarseries.h"
22 22 #include "qpercentbarseries_p.h"
23 23 #include "percentbarchartitem_p.h"
24 24 #include "chartdataset_p.h"
25 25 #include "charttheme_p.h"
26 26 #include "qvalueaxis.h"
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
30 30 /*!
31 31 \class QPercentBarSeries
32 32 \inmodule Qt Charts
33 33 \brief Series for creating percent bar chart.
34 34 \mainclass
35 35
36 QPercentBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars
37 as stacks, where each bar is shown as percentage of all bars in that category.
36 QPercentBarSeries represents a series of data shown as bars. The purpose of this class is to
37 draw bars as stacks, where each bar is shown as percentage of all bars in that category.
38 38 QPercentBarSeries groups the data from sets to categories, which are defined by a QStringList.
39 Bars with zero value are not drawn.
39 40
40 See the \l {PercentbarChart Example} {percent bar chart example} to learn how to create a percent bar chart.
41 See the \l {PercentbarChart Example} {percent bar chart example} to learn how to create a
42 percent bar chart.
41 43 \image examples_percentbarchart.png
42 44
43 45 \sa QBarSet, QStackedBarSeries, QAbstractBarSeries
44 46 */
45 47 #ifdef QDOC_QT5
46 48 /*!
47 49 \qmltype PercentBarSeries
48 50 \instantiates QPercentBarSeries
49 51 \inqmlmodule QtCommercial.Chart
50 52
51 53 \include doc/src/persentbarseries.qdocinc
52 54 */
53 55 #else
54 56 /*!
55 57 \qmlclass PercentBarSeries QPercentBarSeries
56 58
57 59 \include ../doc/src/persentbarseries.qdocinc
58 60 */
59 61 #endif
60 62
61 63 /*!
62 64 Constructs empty QPercentBarSeries.
63 65 QPercentBarSeries is QObject which is a child of a \a parent.
64 66 */
65 67 QPercentBarSeries::QPercentBarSeries(QObject *parent)
66 68 : QAbstractBarSeries(*new QPercentBarSeriesPrivate(this), parent)
67 69 {
68 70 }
69 71
70 72 /*!
71 73 Destructor. Removes series from chart.
72 74 */
73 75 QPercentBarSeries::~QPercentBarSeries()
74 76 {
75 77 Q_D(QPercentBarSeries);
76 78 if (d->m_chart)
77 79 d->m_chart->removeSeries(this);
78 80 }
79 81
80 82 /*!
81 83 Returns QAbstractSeries::SeriesTypePercentBar.
82 84 */
83 85 QAbstractSeries::SeriesType QPercentBarSeries::type() const
84 86 {
85 87 return QAbstractSeries::SeriesTypePercentBar;
86 88 }
87 89
88 90 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
89 91
90 92 QPercentBarSeriesPrivate::QPercentBarSeriesPrivate(QPercentBarSeries *q) : QAbstractBarSeriesPrivate(q)
91 93 {
92 94
93 95 }
94 96
95 97 void QPercentBarSeriesPrivate::initializeDomain()
96 98 {
97 99 qreal minX(domain()->minX());
98 100 qreal minY(domain()->minY());
99 101 qreal maxX(domain()->maxX());
100 102 qreal maxY(domain()->maxY());
101 103
102 104 qreal x = categoryCount();
103 105 minX = qMin(minX, - (qreal)0.5);
104 106 maxX = qMax(maxX, x - (qreal)0.5);
105 107 minY = 0;
106 108 maxY = 100;
107 109
108 110 domain()->setRange(minX, maxX, minY, maxY);
109 111 }
110 112
111 113
112 114 void QPercentBarSeriesPrivate::initializeGraphics(QGraphicsItem* parent)
113 115 {
114 116 Q_Q(QPercentBarSeries);
115 117 PercentBarChartItem *bar = new PercentBarChartItem(q,parent);
116 118 m_item.reset(bar);
117 119 QAbstractSeriesPrivate::initializeGraphics(parent);
118 120 }
119 121
120 122 #include "moc_qpercentbarseries.cpp"
121 123
122 124 QTCOMMERCIALCHART_END_NAMESPACE
123 125
@@ -1,659 +1,679
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2014 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include <QtTest/QtTest>
22 22 #include <qhorizontalpercentbarseries.h>
23 23 #include <qbarset.h>
24 24 #include <qchartview.h>
25 25 #include <qchart.h>
26 26 #include "tst_definitions.h"
27 27
28 28 QTCOMMERCIALCHART_USE_NAMESPACE
29 29
30 30 Q_DECLARE_METATYPE(QBarSet*)
31 31 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
32 32
33 33 class tst_QHorizontalPercentBarSeries : public QObject
34 34 {
35 35 Q_OBJECT
36 36
37 37 public slots:
38 38 void initTestCase();
39 39 void cleanupTestCase();
40 40 void init();
41 41 void cleanup();
42 42
43 43 private slots:
44 44 void qhorizontalpercentbarseries_data();
45 45 void qhorizontalpercentbarseries();
46 46 void type_data();
47 47 void type();
48 48 void setLabelsFormat();
49 49 void setLabelsPosition();
50 50 void mouseclicked_data();
51 51 void mouseclicked();
52 52 void mousehovered_data();
53 53 void mousehovered();
54 void zeroValuesInSeries();
54 55
55 56 private:
56 57 QHorizontalPercentBarSeries* m_barseries;
57 58 };
58 59
59 60 void tst_QHorizontalPercentBarSeries::initTestCase()
60 61 {
61 62 qRegisterMetaType<QBarSet*>("QBarSet*");
62 63 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
63 64 }
64 65
65 66 void tst_QHorizontalPercentBarSeries::cleanupTestCase()
66 67 {
67 68 }
68 69
69 70 void tst_QHorizontalPercentBarSeries::init()
70 71 {
71 72 m_barseries = new QHorizontalPercentBarSeries();
72 73 }
73 74
74 75 void tst_QHorizontalPercentBarSeries::cleanup()
75 76 {
76 77 delete m_barseries;
77 78 m_barseries = 0;
78 79 }
79 80
80 81 void tst_QHorizontalPercentBarSeries::qhorizontalpercentbarseries_data()
81 82 {
82 83 }
83 84
84 85 void tst_QHorizontalPercentBarSeries::qhorizontalpercentbarseries()
85 86 {
86 87 QHorizontalPercentBarSeries *barseries = new QHorizontalPercentBarSeries();
87 88 QVERIFY(barseries != 0);
88 89 }
89 90
90 91 void tst_QHorizontalPercentBarSeries::type_data()
91 92 {
92 93
93 94 }
94 95
95 96 void tst_QHorizontalPercentBarSeries::type()
96 97 {
97 98 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeHorizontalPercentBar);
98 99 }
99 100
100 101 void tst_QHorizontalPercentBarSeries::setLabelsFormat()
101 102 {
102 103 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
103 104 QCOMPARE(m_barseries->labelsFormat(), QString());
104 105
105 106 QString format("(@value)");
106 107 m_barseries->setLabelsFormat(format);
107 108 TRY_COMPARE(labelsFormatSpy.count(), 1);
108 109 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
109 110 QVERIFY(arguments.at(0).toString() == format);
110 111 QCOMPARE(m_barseries->labelsFormat(), format);
111 112
112 113 m_barseries->setLabelsFormat(QString());
113 114 TRY_COMPARE(labelsFormatSpy.count(), 1);
114 115 arguments = labelsFormatSpy.takeFirst();
115 116 QVERIFY(arguments.at(0).toString() == QString());
116 117 QCOMPARE(m_barseries->labelsFormat(), QString());
117 118 }
118 119
119 120 void tst_QHorizontalPercentBarSeries::setLabelsPosition()
120 121 {
121 122 QSignalSpy labelsPositionSpy(m_barseries,
122 123 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
123 124 QCOMPARE(m_barseries->labelsPosition(), QHorizontalPercentBarSeries::LabelsCenter);
124 125
125 126 m_barseries->setLabelsPosition(QHorizontalPercentBarSeries::LabelsInsideEnd);
126 127 TRY_COMPARE(labelsPositionSpy.count(), 1);
127 128 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
128 129 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
129 130 == QHorizontalPercentBarSeries::LabelsInsideEnd);
130 131 QCOMPARE(m_barseries->labelsPosition(), QHorizontalPercentBarSeries::LabelsInsideEnd);
131 132
132 133 m_barseries->setLabelsPosition(QHorizontalPercentBarSeries::LabelsInsideBase);
133 134 TRY_COMPARE(labelsPositionSpy.count(), 1);
134 135 arguments = labelsPositionSpy.takeFirst();
135 136 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
136 137 == QHorizontalPercentBarSeries::LabelsInsideBase);
137 138 QCOMPARE(m_barseries->labelsPosition(), QHorizontalPercentBarSeries::LabelsInsideBase);
138 139
139 140 m_barseries->setLabelsPosition(QHorizontalPercentBarSeries::LabelsOutsideEnd);
140 141 TRY_COMPARE(labelsPositionSpy.count(), 1);
141 142 arguments = labelsPositionSpy.takeFirst();
142 143 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
143 144 == QHorizontalPercentBarSeries::LabelsOutsideEnd);
144 145 QCOMPARE(m_barseries->labelsPosition(), QHorizontalPercentBarSeries::LabelsOutsideEnd);
145 146
146 147 m_barseries->setLabelsPosition(QHorizontalPercentBarSeries::LabelsCenter);
147 148 TRY_COMPARE(labelsPositionSpy.count(), 1);
148 149 arguments = labelsPositionSpy.takeFirst();
149 150 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
150 151 == QHorizontalPercentBarSeries::LabelsCenter);
151 152 QCOMPARE(m_barseries->labelsPosition(), QHorizontalPercentBarSeries::LabelsCenter);
152 153 }
153 154
154 155 void tst_QHorizontalPercentBarSeries::mouseclicked_data()
155 156 {
156 157
157 158 }
158 159
159 160 void tst_QHorizontalPercentBarSeries::mouseclicked()
160 161 {
161 162 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
162 163
163 164 QHorizontalPercentBarSeries* series = new QHorizontalPercentBarSeries();
164 165
165 166 QBarSet* set1 = new QBarSet(QString("set 1"));
166 167 *set1 << 10 << 10 << 10;
167 168 series->append(set1);
168 169
169 170 QBarSet* set2 = new QBarSet(QString("set 2"));
170 171 *set2 << 10 << 10 << 10;
171 172 series->append(set2);
172 173
173 174 QList<QBarSet*> barSets = series->barSets();
174 175
175 176 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
176 177
177 178 QChartView view(new QChart());
178 179 view.resize(400,300);
179 180 view.chart()->addSeries(series);
180 181 view.show();
181 182 QTest::qWaitForWindowShown(&view);
182 183
183 184 // Calculate expected layout for bars
184 185 QRectF plotArea = view.chart()->plotArea();
185 186 qreal width = plotArea.width();
186 187 qreal height = plotArea.height();
187 188 qreal rangeY = 3; // 3 values per set
188 189 qreal rangeX = 100; // From 0 to 100 because of scaling to 100%
189 190 qreal scaleY = (height / rangeY);
190 191 qreal scaleX = (width / rangeX);
191 192
192 193 qreal setCount = series->count();
193 194 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
194 195 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
195 196 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
196 197
197 198 QVector<QRectF> layout;
198 199
199 200 // 3 = count of values in set
200 201 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
201 202 for (int i = 0; i < 3; i++) {
202 203 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
203 204 qreal percentage = (100 / colSum);
204 205 qreal xPos = -scaleX * domainMinX + plotArea.left();
205 206 for (int set = 0; set < setCount; set++) {
206 207 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
207 208 qreal rectWidth = barSets.at(set)->at(i) * percentage * scaleX;
208 209 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
209 210 layout.append(rect);
210 211 xPos += rectWidth;
211 212 }
212 213 }
213 214
214 215 //====================================================================================
215 216 // barset 1, bar 0
216 217 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
217 218 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
218 219
219 220 QCOMPARE(seriesSpy.count(), 1);
220 221
221 222 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
222 223 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
223 224 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
224 225 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
225 226
226 227 //====================================================================================
227 228 // barset 1, bar 1
228 229 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
229 230 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
230 231
231 232 QCOMPARE(seriesSpy.count(), 1);
232 233
233 234 seriesSpyArg = seriesSpy.takeFirst();
234 235 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
235 236 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
236 237 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
237 238
238 239 //====================================================================================
239 240 // barset 1, bar 2
240 241 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
241 242 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
242 243
243 244 QCOMPARE(seriesSpy.count(), 1);
244 245
245 246 seriesSpyArg = seriesSpy.takeFirst();
246 247 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
247 248 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
248 249 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
249 250
250 251 //====================================================================================
251 252 // barset 2, bar 0
252 253 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
253 254 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
254 255
255 256 QCOMPARE(seriesSpy.count(), 1);
256 257
257 258 seriesSpyArg = seriesSpy.takeFirst();
258 259 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
259 260 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
260 261 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
261 262
262 263 //====================================================================================
263 264 // barset 2, bar 1
264 265 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
265 266 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
266 267
267 268 QCOMPARE(seriesSpy.count(), 1);
268 269
269 270 seriesSpyArg = seriesSpy.takeFirst();
270 271 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
271 272 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
272 273 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
273 274
274 275 //====================================================================================
275 276 // barset 2, bar 2
276 277 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
277 278 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
278 279
279 280 QCOMPARE(seriesSpy.count(), 1);
280 281
281 282 seriesSpyArg = seriesSpy.takeFirst();
282 283 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
283 284 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
284 285 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
285 286 }
286 287
287 288 void tst_QHorizontalPercentBarSeries::mousehovered_data()
288 289 {
289 290
290 291 }
291 292
292 293 void tst_QHorizontalPercentBarSeries::mousehovered()
293 294 {
294 295 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
295 296
296 297 QHorizontalPercentBarSeries* series = new QHorizontalPercentBarSeries();
297 298
298 299 QBarSet* set1 = new QBarSet(QString("set 1"));
299 300 *set1 << 10 << 10 << 10;
300 301 series->append(set1);
301 302
302 303 QBarSet* set2 = new QBarSet(QString("set 2"));
303 304 *set2 << 10 << 10 << 10;
304 305 series->append(set2);
305 306
306 307 QList<QBarSet*> barSets = series->barSets();
307 308
308 309 QSignalSpy seriesSpy(series,SIGNAL(hovered(bool,QBarSet*)));
309 310 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
310 311 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
311 312 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
312 313
313 314 QChartView view(new QChart());
314 315 view.resize(400,300);
315 316 view.chart()->addSeries(series);
316 317 view.show();
317 318 QTest::qWaitForWindowShown(&view);
318 319
319 320 //this is hack since view does not get events otherwise
320 321 view.setMouseTracking(true);
321 322
322 323 // Calculate expected layout for bars
323 324 QRectF plotArea = view.chart()->plotArea();
324 325 qreal width = plotArea.width();
325 326 qreal height = plotArea.height();
326 327 qreal rangeY = 3; // 3 values per set
327 328 qreal rangeX = 100; // From 0 to 100 because of scaling to 100%
328 329 qreal scaleY = (height / rangeY);
329 330 qreal scaleX = (width / rangeX);
330 331
331 332 qreal setCount = series->count();
332 333 qreal domainMinY = -0.5; // These come from internal domain used by barseries.
333 334 qreal domainMinX = 0; // No access to domain from outside, so use hard coded values.
334 335 qreal rectHeight = scaleY * series->barWidth(); // On horizontal chart barWidth of the barseries means height of the rect.
335 336
336 337 QVector<QRectF> layout;
337 338
338 339 // 3 = count of values in set
339 340 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
340 341 for (int i = 0; i < 3; i++) {
341 342 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
342 343 qreal percentage = (100 / colSum);
343 344 qreal xPos = -scaleX * domainMinX + plotArea.left();
344 345 for (int set = 0; set < setCount; set++) {
345 346 qreal yPos = (domainMinY +0.5 -i) * scaleY + plotArea.bottom() - rectHeight/2;
346 347 qreal rectWidth = barSets.at(set)->at(i) * percentage * scaleX;
347 348 QRectF rect(xPos, yPos - rectHeight, rectWidth, rectHeight);
348 349 layout.append(rect);
349 350 xPos += rectWidth;
350 351 }
351 352 }
352 353
353 354 //=======================================================================
354 355 // move mouse to left border
355 356 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(4).center().y()));
356 357 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
357 358 TRY_COMPARE(seriesSpy.count(), 0);
358 359 TRY_COMPARE(seriesIndexSpy.count(), 0);
359 360
360 361 //=======================================================================
361 362 // move mouse on top of set1
362 363 QTest::mouseMove(view.viewport(), layout.at(4).center().toPoint());
363 364 TRY_COMPARE(seriesSpy.count(), 1);
364 365 TRY_COMPARE(seriesIndexSpy.count(), 1);
365 366 TRY_COMPARE(setIndexSpy1.count(), 1);
366 367 TRY_COMPARE(setIndexSpy2.count(), 0);
367 368
368 369 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
369 370 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
370 371 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
371 372 QVERIFY(seriesSpyArg.at(0).toBool() == true);
372 373
373 374 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
374 375 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
375 376 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
376 377 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
377 378
378 379 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
379 380 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
380 381 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
381 382
382 383 //=======================================================================
383 384 // move mouse from top of set1 to top of set2
384 385 QTest::mouseMove(view.viewport(), layout.at(5).center().toPoint());
385 386 TRY_COMPARE(seriesSpy.count(), 2);
386 387 TRY_COMPARE(seriesIndexSpy.count(), 2);
387 388 TRY_COMPARE(setIndexSpy1.count(), 1);
388 389 TRY_COMPARE(setIndexSpy2.count(), 1);
389 390
390 391 // should leave set1
391 392 seriesSpyArg = seriesSpy.takeFirst();
392 393 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
393 394 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
394 395 QVERIFY(seriesSpyArg.at(0).toBool() == false);
395 396
396 397 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
397 398 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
398 399 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
399 400 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
400 401
401 402 setIndexSpyArg = setIndexSpy1.takeFirst();
402 403 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
403 404 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
404 405
405 406 // should enter set2
406 407 seriesSpyArg = seriesSpy.takeFirst();
407 408 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
408 409 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
409 410 QVERIFY(seriesSpyArg.at(0).toBool() == true);
410 411
411 412 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
412 413 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
413 414 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
414 415 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
415 416
416 417 setIndexSpyArg = setIndexSpy2.takeFirst();
417 418 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
418 419 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
419 420
420 421 //=======================================================================
421 422 // move mouse from top of set2 to background
422 423 QTest::mouseMove(view.viewport(), QPoint(layout.at(5).center().x(), 0));
423 424 TRY_COMPARE(seriesSpy.count(), 1);
424 425 TRY_COMPARE(seriesIndexSpy.count(), 1);
425 426 TRY_COMPARE(setIndexSpy1.count(), 0);
426 427 TRY_COMPARE(setIndexSpy2.count(), 1);
427 428
428 429 // should leave set2
429 430 seriesSpyArg = seriesSpy.takeFirst();
430 431 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
431 432 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
432 433 QVERIFY(seriesSpyArg.at(0).toBool() == false);
433 434
434 435 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
435 436 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
436 437 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
437 438 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
438 439
439 440 setIndexSpyArg = setIndexSpy2.takeFirst();
440 441 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
441 442 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
442 443
443 444 //=======================================================================
444 445 // move mouse on top of set1, bar0 to check the index (hover into set1)
445 446 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
446 447
447 448 TRY_COMPARE(seriesSpy.count(), 1);
448 449 TRY_COMPARE(seriesIndexSpy.count(), 1);
449 450 TRY_COMPARE(setIndexSpy1.count(), 1);
450 451 TRY_COMPARE(setIndexSpy2.count(), 0);
451 452
452 453 //should enter set1, bar0
453 454 seriesSpyArg = seriesSpy.takeFirst();
454 455 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
455 456 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
456 457 QVERIFY(seriesSpyArg.at(0).toBool() == true);
457 458
458 459 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
459 460 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
460 461 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
461 462 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
462 463 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
463 464 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
464 465
465 466 setIndexSpyArg = setIndexSpy1.takeFirst();
466 467 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
467 468 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
468 469 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
469 470 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
470 471
471 472 //=======================================================================
472 473 // move mouse on top of set2, bar0 to check the index (hover out set1,
473 474 // hover in set2)
474 475 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
475 476 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
476 477
477 478 TRY_COMPARE(seriesSpy.count(), 2);
478 479 TRY_COMPARE(seriesIndexSpy.count(), 2);
479 480 TRY_COMPARE(setIndexSpy1.count(), 1);
480 481 TRY_COMPARE(setIndexSpy2.count(), 1);
481 482
482 483 //should leave set1, bar0
483 484 seriesSpyArg = seriesSpy.takeFirst();
484 485 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
485 486 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
486 487 QVERIFY(seriesSpyArg.at(0).toBool() == false);
487 488
488 489 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
489 490 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
490 491 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
491 492 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
492 493 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
493 494 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
494 495
495 496 setIndexSpyArg = setIndexSpy1.takeFirst();
496 497 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
497 498 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
498 499 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
499 500 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
500 501
501 502 //should enter set2, bar0
502 503 seriesSpyArg = seriesSpy.takeFirst();
503 504 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
504 505 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
505 506 QVERIFY(seriesSpyArg.at(0).toBool() == true);
506 507
507 508 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
508 509 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
509 510 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
510 511 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
511 512 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
512 513 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
513 514
514 515 setIndexSpyArg = setIndexSpy2.takeFirst();
515 516 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
516 517 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
517 518 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
518 519 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
519 520
520 521 //=======================================================================
521 522 // move mouse on top of set1, bar1 to check the index (hover out set2,
522 523 // hover in set1)
523 524 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
524 525
525 526 TRY_COMPARE(seriesSpy.count(), 2);
526 527 TRY_COMPARE(seriesIndexSpy.count(), 2);
527 528 TRY_COMPARE(setIndexSpy1.count(), 1);
528 529 TRY_COMPARE(setIndexSpy2.count(), 1);
529 530
530 531 //should leave set2, bar0
531 532 seriesSpyArg = seriesSpy.takeFirst();
532 533 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
533 534 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
534 535 QVERIFY(seriesSpyArg.at(0).toBool() == false);
535 536
536 537 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
537 538 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
538 539 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
539 540 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
540 541 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
541 542 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
542 543
543 544 setIndexSpyArg = setIndexSpy2.takeFirst();
544 545 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
545 546 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
546 547 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
547 548 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
548 549
549 550 //should enter set1, bar1
550 551 seriesSpyArg = seriesSpy.takeFirst();
551 552 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
552 553 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
553 554 QVERIFY(seriesSpyArg.at(0).toBool() == true);
554 555
555 556 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
556 557 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
557 558 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
558 559 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
559 560 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
560 561 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
561 562
562 563 setIndexSpyArg = setIndexSpy1.takeFirst();
563 564 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
564 565 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
565 566 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
566 567 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
567 568
568 569 //=======================================================================
569 570 // move mouse between set1 and set2 (hover out set1)
570 571 QTest::mouseMove(view.viewport(), QPoint(layout.at(3).left(),
571 572 (layout.at(3).top() + layout.at(4).bottom()) / 2));
572 573
573 574 TRY_COMPARE(seriesSpy.count(), 1);
574 575 TRY_COMPARE(seriesIndexSpy.count(), 1);
575 576 TRY_COMPARE(setIndexSpy1.count(), 1);
576 577 TRY_COMPARE(setIndexSpy2.count(), 0);
577 578
578 579 //should leave set1, bar1
579 580 seriesSpyArg = seriesSpy.takeFirst();
580 581 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
581 582 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
582 583 QVERIFY(seriesSpyArg.at(0).toBool() == false);
583 584
584 585 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
585 586 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
586 587 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
587 588 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
588 589 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
589 590 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
590 591
591 592 setIndexSpyArg = setIndexSpy1.takeFirst();
592 593 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
593 594 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
594 595 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
595 596 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
596 597
597 598 //=======================================================================
598 599 // move mouse on top of set2, bar1 to check the index (hover in set2)
599 600 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
600 601
601 602 TRY_COMPARE(seriesSpy.count(), 1);
602 603 TRY_COMPARE(seriesIndexSpy.count(), 1);
603 604 TRY_COMPARE(setIndexSpy1.count(), 0);
604 605 TRY_COMPARE(setIndexSpy2.count(), 1);
605 606
606 607 //should enter set2, bar1
607 608 seriesSpyArg = seriesSpy.takeFirst();
608 609 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
609 610 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
610 611 QVERIFY(seriesSpyArg.at(0).toBool() == true);
611 612
612 613 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
613 614 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
614 615 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
615 616 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
616 617 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
617 618 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
618 619
619 620 setIndexSpyArg = setIndexSpy2.takeFirst();
620 621 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
621 622 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
622 623 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
623 624 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
624 625
625 626 //=======================================================================
626 627 // move mouse between set1 and set2 (hover out set2)
627 628 QTest::mouseMove(view.viewport(), QPoint(layout.at(3).left(),
628 629 (layout.at(3).top() + layout.at(4).bottom()) / 2));
629 630
630 631 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
631 632 TRY_COMPARE(seriesSpy.count(), 1);
632 633 TRY_COMPARE(seriesIndexSpy.count(), 1);
633 634 TRY_COMPARE(setIndexSpy1.count(), 0);
634 635 TRY_COMPARE(setIndexSpy2.count(), 1);
635 636
636 637 //should leave set1, bar1
637 638 seriesSpyArg = seriesSpy.takeFirst();
638 639 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
639 640 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
640 641 QVERIFY(seriesSpyArg.at(0).toBool() == false);
641 642
642 643 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
643 644 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
644 645 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
645 646 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
646 647 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
647 648 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
648 649
649 650 setIndexSpyArg = setIndexSpy2.takeFirst();
650 651 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
651 652 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
652 653 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
653 654 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
654 655 }
655 656
657 void tst_QHorizontalPercentBarSeries::zeroValuesInSeries()
658 {
659 QHorizontalPercentBarSeries *series = new QHorizontalPercentBarSeries();
660 QBarSet *set1 = new QBarSet(QString("set 1"));
661 *set1 << 100 << 0.0 << 10;
662 series->append(set1);
663
664 QBarSet *set2 = new QBarSet(QString("set 2"));
665 *set2 << 0.0 << 0.0 << 70;
666 series->append(set2);
667
668 QChartView view(new QChart());
669 view.chart()->addSeries(series);
670 view.chart()->createDefaultAxes();
671 view.show();
672
673 QTest::qWaitForWindowShown(&view);
674 }
675
656 676 QTEST_MAIN(tst_QHorizontalPercentBarSeries)
657 677
658 678 #include "tst_qhorizontalpercentbarseries.moc"
659 679
@@ -1,663 +1,683
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2014 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Enterprise Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Enterprise licenses may use this file in
11 11 ** accordance with the Qt Enterprise License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include <QtTest/QtTest>
22 22 #include <qpercentbarseries.h>
23 23 #include <qbarset.h>
24 24 #include <qchartview.h>
25 25 #include <qchart.h>
26 26 #include "tst_definitions.h"
27 27
28 28 QTCOMMERCIALCHART_USE_NAMESPACE
29 29
30 30 Q_DECLARE_METATYPE(QBarSet*)
31 31 Q_DECLARE_METATYPE(QAbstractBarSeries::LabelsPosition)
32 32
33 33 class tst_QPercentBarSeries : public QObject
34 34 {
35 35 Q_OBJECT
36 36
37 37 public slots:
38 38 void initTestCase();
39 39 void cleanupTestCase();
40 40 void init();
41 41 void cleanup();
42 42
43 43 private slots:
44 44 void qpercentbarseries_data();
45 45 void qpercentbarseries();
46 46 void type_data();
47 47 void type();
48 48 void setLabelsFormat();
49 49 void setLabelsPosition();
50 50 void mouseclicked_data();
51 51 void mouseclicked();
52 52 void mousehovered_data();
53 53 void mousehovered();
54 void zeroValuesInSeries();
54 55
55 56 private:
56 57 QPercentBarSeries* m_barseries;
57 58 };
58 59
59 60 void tst_QPercentBarSeries::initTestCase()
60 61 {
61 62 qRegisterMetaType<QBarSet*>("QBarSet*");
62 63 qRegisterMetaType<QAbstractBarSeries::LabelsPosition>("QAbstractBarSeries::LabelsPosition");
63 64 }
64 65
65 66 void tst_QPercentBarSeries::cleanupTestCase()
66 67 {
67 68 }
68 69
69 70 void tst_QPercentBarSeries::init()
70 71 {
71 72 m_barseries = new QPercentBarSeries();
72 73 }
73 74
74 75 void tst_QPercentBarSeries::cleanup()
75 76 {
76 77 delete m_barseries;
77 78 m_barseries = 0;
78 79 }
79 80
80 81 void tst_QPercentBarSeries::qpercentbarseries_data()
81 82 {
82 83 }
83 84
84 85 void tst_QPercentBarSeries::qpercentbarseries()
85 86 {
86 87 QPercentBarSeries *barseries = new QPercentBarSeries();
87 88 QVERIFY(barseries != 0);
88 89 }
89 90
90 91 void tst_QPercentBarSeries::type_data()
91 92 {
92 93
93 94 }
94 95
95 96 void tst_QPercentBarSeries::type()
96 97 {
97 98 QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypePercentBar);
98 99 }
99 100
100 101 void tst_QPercentBarSeries::mouseclicked_data()
101 102 {
102 103
103 104 }
104 105
105 106 void tst_QPercentBarSeries::setLabelsFormat()
106 107 {
107 108 QSignalSpy labelsFormatSpy(m_barseries, SIGNAL(labelsFormatChanged(QString)));
108 109 QCOMPARE(m_barseries->labelsFormat(), QString());
109 110
110 111 QString format("(@value)");
111 112 m_barseries->setLabelsFormat(format);
112 113 TRY_COMPARE(labelsFormatSpy.count(), 1);
113 114 QList<QVariant> arguments = labelsFormatSpy.takeFirst();
114 115 QVERIFY(arguments.at(0).toString() == format);
115 116 QCOMPARE(m_barseries->labelsFormat(), format);
116 117
117 118 m_barseries->setLabelsFormat(QString());
118 119 TRY_COMPARE(labelsFormatSpy.count(), 1);
119 120 arguments = labelsFormatSpy.takeFirst();
120 121 QVERIFY(arguments.at(0).toString() == QString());
121 122 QCOMPARE(m_barseries->labelsFormat(), QString());
122 123 }
123 124
124 125 void tst_QPercentBarSeries::setLabelsPosition()
125 126 {
126 127 QSignalSpy labelsPositionSpy(m_barseries,
127 128 SIGNAL(labelsPositionChanged(QAbstractBarSeries::LabelsPosition)));
128 129 QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsCenter);
129 130
130 131 m_barseries->setLabelsPosition(QPercentBarSeries::LabelsInsideEnd);
131 132 TRY_COMPARE(labelsPositionSpy.count(), 1);
132 133 QList<QVariant> arguments = labelsPositionSpy.takeFirst();
133 134 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
134 135 == QPercentBarSeries::LabelsInsideEnd);
135 136 QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsInsideEnd);
136 137
137 138 m_barseries->setLabelsPosition(QPercentBarSeries::LabelsInsideBase);
138 139 TRY_COMPARE(labelsPositionSpy.count(), 1);
139 140 arguments = labelsPositionSpy.takeFirst();
140 141 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
141 142 == QPercentBarSeries::LabelsInsideBase);
142 143 QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsInsideBase);
143 144
144 145 m_barseries->setLabelsPosition(QPercentBarSeries::LabelsOutsideEnd);
145 146 TRY_COMPARE(labelsPositionSpy.count(), 1);
146 147 arguments = labelsPositionSpy.takeFirst();
147 148 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
148 149 == QPercentBarSeries::LabelsOutsideEnd);
149 150 QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsOutsideEnd);
150 151
151 152 m_barseries->setLabelsPosition(QPercentBarSeries::LabelsCenter);
152 153 TRY_COMPARE(labelsPositionSpy.count(), 1);
153 154 arguments = labelsPositionSpy.takeFirst();
154 155 QVERIFY(arguments.at(0).value<QAbstractBarSeries::LabelsPosition>()
155 156 == QPercentBarSeries::LabelsCenter);
156 157 QCOMPARE(m_barseries->labelsPosition(), QPercentBarSeries::LabelsCenter);
157 158 }
158 159
159 160 void tst_QPercentBarSeries::mouseclicked()
160 161 {
161 162 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
162 163
163 164 QPercentBarSeries* series = new QPercentBarSeries();
164 165
165 166 QBarSet* set1 = new QBarSet(QString("set 1"));
166 167 *set1 << 10 << 10 << 10;
167 168 series->append(set1);
168 169
169 170 QBarSet* set2 = new QBarSet(QString("set 2"));
170 171 *set2 << 10 << 10 << 10;
171 172 series->append(set2);
172 173
173 174 QList<QBarSet*> barSets = series->barSets();
174 175
175 176 QSignalSpy seriesSpy(series,SIGNAL(clicked(int,QBarSet*)));
176 177
177 178 QChartView view(new QChart());
178 179 view.resize(400,300);
179 180 view.chart()->addSeries(series);
180 181 view.show();
181 182 QTest::qWaitForWindowShown(&view);
182 183
183 184 // Calculate expected layout for bars
184 185 QRectF plotArea = view.chart()->plotArea();
185 186 qreal width = plotArea.width();
186 187 qreal height = plotArea.height();
187 188 qreal rangeY = 100; // From 0 to 100 because range is scaled to 100%
188 189 qreal rangeX = 3; // 3 values per set
189 190 qreal scaleY = (height / rangeY);
190 191 qreal scaleX = (width / rangeX);
191 192
192 193 qreal setCount = series->count();
193 194 qreal domainMinY = 0; // These come from internal domain used by barseries.
194 195 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
195 196 qreal rectWidth = scaleX * series->barWidth();
196 197
197 198 QVector<QRectF> layout;
198 199
199 200 // 3 = count of values in set
200 201 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
201 202 for (int i = 0; i < 3; i++) {
202 203 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
203 204 qreal percentage = (100 / colSum);
204 205 qreal yPos = height + scaleY * domainMinY + plotArea.top();
205 206
206 207 for (int set = 0; set < setCount; set++) {
207 208 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
208 209 qreal rectHeigth = barSets.at(set)->at(i) * percentage * scaleY;
209 210
210 211 QRectF rect(xPos, yPos-rectHeigth, rectWidth, rectHeigth);
211 212 layout.append(rect);
212 213 yPos -= rectHeigth;
213 214 }
214 215 }
215 216
216 217 //====================================================================================
217 218 // barset 1, bar 0
218 219 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(0).center().toPoint());
219 220 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
220 221
221 222 QCOMPARE(seriesSpy.count(), 1);
222 223
223 224 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
224 225 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
225 226 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
226 227 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
227 228
228 229 //====================================================================================
229 230 // barset 1, bar 1
230 231 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(2).center().toPoint());
231 232 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
232 233
233 234 QCOMPARE(seriesSpy.count(), 1);
234 235
235 236 seriesSpyArg = seriesSpy.takeFirst();
236 237 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
237 238 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
238 239 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
239 240
240 241 //====================================================================================
241 242 // barset 1, bar 2
242 243 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(4).center().toPoint());
243 244 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
244 245
245 246 QCOMPARE(seriesSpy.count(), 1);
246 247
247 248 seriesSpyArg = seriesSpy.takeFirst();
248 249 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
249 250 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
250 251 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
251 252
252 253 //====================================================================================
253 254 // barset 2, bar 0
254 255 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(1).center().toPoint());
255 256 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
256 257
257 258 QCOMPARE(seriesSpy.count(), 1);
258 259
259 260 seriesSpyArg = seriesSpy.takeFirst();
260 261 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
261 262 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
262 263 QVERIFY(seriesSpyArg.at(0).toInt() == 0);
263 264
264 265 //====================================================================================
265 266 // barset 2, bar 1
266 267 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(3).center().toPoint());
267 268 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
268 269
269 270 QCOMPARE(seriesSpy.count(), 1);
270 271
271 272 seriesSpyArg = seriesSpy.takeFirst();
272 273 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
273 274 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
274 275 QVERIFY(seriesSpyArg.at(0).toInt() == 1);
275 276
276 277 //====================================================================================
277 278 // barset 2, bar 2
278 279 QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, layout.at(5).center().toPoint());
279 280 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
280 281
281 282 QCOMPARE(seriesSpy.count(), 1);
282 283
283 284 seriesSpyArg = seriesSpy.takeFirst();
284 285 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
285 286 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Int);
286 287 QVERIFY(seriesSpyArg.at(0).toInt() == 2);
287 288 }
288 289
289 290 void tst_QPercentBarSeries::mousehovered_data()
290 291 {
291 292
292 293 }
293 294
294 295 void tst_QPercentBarSeries::mousehovered()
295 296 {
296 297 SKIP_IF_CANNOT_TEST_MOUSE_EVENTS();
297 298
298 299 QPercentBarSeries* series = new QPercentBarSeries();
299 300
300 301 QBarSet* set1 = new QBarSet(QString("set 1"));
301 302 *set1 << 10 << 10 << 10;
302 303 series->append(set1);
303 304
304 305 QBarSet* set2 = new QBarSet(QString("set 2"));
305 306 *set2 << 10 << 10 << 10;
306 307 series->append(set2);
307 308
308 309 QList<QBarSet*> barSets = series->barSets();
309 310
310 311 QSignalSpy seriesSpy(series,SIGNAL(hovered(bool,QBarSet*)));
311 312 QSignalSpy seriesIndexSpy(series, SIGNAL(hovered(bool, int, QBarSet*)));
312 313 QSignalSpy setIndexSpy1(set1, SIGNAL(hovered(bool, int)));
313 314 QSignalSpy setIndexSpy2(set2, SIGNAL(hovered(bool, int)));
314 315
315 316 QChartView view(new QChart());
316 317 view.resize(400,300);
317 318 view.chart()->addSeries(series);
318 319 view.show();
319 320 QTest::qWaitForWindowShown(&view);
320 321
321 322 //this is hack since view does not get events otherwise
322 323 view.setMouseTracking(true);
323 324
324 325 // Calculate expected layout for bars
325 326 QRectF plotArea = view.chart()->plotArea();
326 327 qreal width = plotArea.width();
327 328 qreal height = plotArea.height();
328 329 qreal rangeY = 100; // From 0 to 100 because range is scaled to 100%
329 330 qreal rangeX = 3; // 3 values per set
330 331 qreal scaleY = (height / rangeY);
331 332 qreal scaleX = (width / rangeX);
332 333
333 334 qreal setCount = series->count();
334 335 qreal domainMinY = 0; // These come from internal domain used by barseries.
335 336 qreal domainMinX = -0.5; // No access to domain from outside, so use hard coded values.
336 337 qreal rectWidth = scaleX * series->barWidth();
337 338
338 339 QVector<QRectF> layout;
339 340
340 341 // 3 = count of values in set
341 342 // Note that rects in this vector will be interleaved (set1 bar0, set2 bar0, set1 bar1, set2 bar1, etc.)
342 343 for (int i = 0; i < 3; i++) {
343 344 qreal colSum = 20; // Sum of values in column (10 + 10 in our test case)
344 345 qreal percentage = (100 / colSum);
345 346 qreal yPos = height + scaleY * domainMinY + plotArea.top();
346 347
347 348 for (int set = 0; set < setCount; set++) {
348 349 qreal xPos = (i - domainMinX) * scaleX + plotArea.left() - rectWidth/2;
349 350 qreal rectHeight = barSets.at(set)->at(i) * percentage * scaleY;
350 351
351 352 QRectF rect(xPos, yPos-rectHeight, rectWidth, rectHeight);
352 353 layout.append(rect);
353 354 yPos -= rectHeight;
354 355 }
355 356 }
356 357
357 358 //=======================================================================
358 359 // move mouse to left border
359 360 QTest::mouseMove(view.viewport(), QPoint(0, layout.at(0).center().y()));
360 361 QCoreApplication::processEvents(QEventLoop::AllEvents, 10000);
361 362 TRY_COMPARE(seriesSpy.count(), 0);
362 363 TRY_COMPARE(seriesIndexSpy.count(), 0);
363 364
364 365 //=======================================================================
365 366 // move mouse on top of set1
366 367 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
367 368 TRY_COMPARE(seriesSpy.count(), 1);
368 369 TRY_COMPARE(seriesIndexSpy.count(), 1);
369 370 TRY_COMPARE(setIndexSpy1.count(), 1);
370 371 TRY_COMPARE(setIndexSpy2.count(), 0);
371 372
372 373 QList<QVariant> seriesSpyArg = seriesSpy.takeFirst();
373 374 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
374 375 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
375 376 QVERIFY(seriesSpyArg.at(0).toBool() == true);
376 377
377 378 QList<QVariant> seriesIndexSpyArg = seriesIndexSpy.takeFirst();
378 379 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
379 380 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
380 381 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
381 382
382 383 QList<QVariant> setIndexSpyArg = setIndexSpy1.takeFirst();
383 384 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
384 385 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
385 386
386 387 //=======================================================================
387 388 // move mouse from top of set1 to top of set2
388 389 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
389 390 TRY_COMPARE(seriesSpy.count(), 2);
390 391 TRY_COMPARE(seriesIndexSpy.count(), 2);
391 392 TRY_COMPARE(setIndexSpy1.count(), 1);
392 393 TRY_COMPARE(setIndexSpy2.count(), 1);
393 394
394 395 // should leave set1
395 396 seriesSpyArg = seriesSpy.takeFirst();
396 397 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
397 398 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
398 399 QVERIFY(seriesSpyArg.at(0).toBool() == false);
399 400
400 401 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
401 402 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
402 403 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
403 404 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
404 405
405 406 setIndexSpyArg = setIndexSpy1.takeFirst();
406 407 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
407 408 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
408 409
409 410 // should enter set2
410 411 seriesSpyArg = seriesSpy.takeFirst();
411 412 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
412 413 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
413 414 QVERIFY(seriesSpyArg.at(0).toBool() == true);
414 415
415 416 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
416 417 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
417 418 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
418 419 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
419 420
420 421 setIndexSpyArg = setIndexSpy2.takeFirst();
421 422 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
422 423 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
423 424
424 425 //=======================================================================
425 426 // move mouse from top of set2 to background
426 427 QTest::mouseMove(view.viewport(), QPoint(layout.at(1).center().x(), 0));
427 428 TRY_COMPARE(seriesSpy.count(), 1);
428 429 TRY_COMPARE(seriesIndexSpy.count(), 1);
429 430 TRY_COMPARE(setIndexSpy1.count(), 0);
430 431 TRY_COMPARE(setIndexSpy2.count(), 1);
431 432
432 433 // should leave set2
433 434 seriesSpyArg = seriesSpy.takeFirst();
434 435 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
435 436 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
436 437 QVERIFY(seriesSpyArg.at(0).toBool() == false);
437 438
438 439 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
439 440 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
440 441 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
441 442 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
442 443
443 444 setIndexSpyArg = setIndexSpy2.takeFirst();
444 445 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
445 446 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
446 447
447 448 //=======================================================================
448 449 // move mouse on top of set1, bar0 to check the index (hover into set1)
449 450 QTest::mouseMove(view.viewport(), layout.at(0).center().toPoint());
450 451
451 452 TRY_COMPARE(seriesSpy.count(), 1);
452 453 TRY_COMPARE(seriesIndexSpy.count(), 1);
453 454 TRY_COMPARE(setIndexSpy1.count(), 1);
454 455 TRY_COMPARE(setIndexSpy2.count(), 0);
455 456
456 457 //should enter set1, bar0
457 458 seriesSpyArg = seriesSpy.takeFirst();
458 459 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
459 460 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
460 461 QVERIFY(seriesSpyArg.at(0).toBool() == true);
461 462
462 463 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
463 464 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
464 465 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
465 466 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
466 467 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
467 468 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
468 469
469 470 setIndexSpyArg = setIndexSpy1.takeFirst();
470 471 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
471 472 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
472 473 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
473 474 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
474 475
475 476 //=======================================================================
476 477 // move mouse on top of set2, bar0 to check the index (hover out set1,
477 478 // hover in set2)
478 479 QTest::mouseMove(view.viewport(), layout.at(1).center().toPoint());
479 480
480 481 TRY_COMPARE(seriesSpy.count(), 2);
481 482 TRY_COMPARE(seriesIndexSpy.count(), 2);
482 483 TRY_COMPARE(setIndexSpy1.count(), 1);
483 484 TRY_COMPARE(setIndexSpy2.count(), 1);
484 485
485 486 //should leave set1, bar0
486 487 seriesSpyArg = seriesSpy.takeFirst();
487 488 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
488 489 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
489 490 QVERIFY(seriesSpyArg.at(0).toBool() == false);
490 491
491 492 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
492 493 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
493 494 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
494 495 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
495 496 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
496 497 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
497 498
498 499 setIndexSpyArg = setIndexSpy1.takeFirst();
499 500 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
500 501 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
501 502 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
502 503 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
503 504
504 505 //should enter set2, bar0
505 506 seriesSpyArg = seriesSpy.takeFirst();
506 507 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
507 508 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
508 509 QVERIFY(seriesSpyArg.at(0).toBool() == true);
509 510
510 511 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
511 512 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
512 513 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
513 514 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
514 515 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
515 516 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
516 517
517 518 setIndexSpyArg = setIndexSpy2.takeFirst();
518 519 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
519 520 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
520 521 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
521 522 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
522 523
523 524 //=======================================================================
524 525 // move mouse on top of set1, bar1 to check the index (hover out set2,
525 526 // hover in set1)
526 527 QTest::mouseMove(view.viewport(), layout.at(2).center().toPoint());
527 528
528 529 TRY_COMPARE(seriesSpy.count(), 2);
529 530 TRY_COMPARE(seriesIndexSpy.count(), 2);
530 531 TRY_COMPARE(setIndexSpy1.count(), 1);
531 532 TRY_COMPARE(setIndexSpy2.count(), 1);
532 533
533 534 //should leave set2, bar0
534 535 seriesSpyArg = seriesSpy.takeFirst();
535 536 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
536 537 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
537 538 QVERIFY(seriesSpyArg.at(0).toBool() == false);
538 539
539 540 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
540 541 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
541 542 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
542 543 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
543 544 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
544 545 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 0);
545 546
546 547 setIndexSpyArg = setIndexSpy2.takeFirst();
547 548 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
548 549 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
549 550 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
550 551 QVERIFY(setIndexSpyArg.at(1).toInt() == 0);
551 552
552 553 //should enter set1, bar1
553 554 seriesSpyArg = seriesSpy.takeFirst();
554 555 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
555 556 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
556 557 QVERIFY(seriesSpyArg.at(0).toBool() == true);
557 558
558 559 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
559 560 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
560 561 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
561 562 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
562 563 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
563 564 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
564 565
565 566 setIndexSpyArg = setIndexSpy1.takeFirst();
566 567 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
567 568 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
568 569 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
569 570 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
570 571
571 572 //=======================================================================
572 573 // move mouse between set1 and set2 (hover out set1)
573 574 QTest::mouseMove(view.viewport(), QPoint((layout.at(3).right() + layout.at(4).left()) /2,
574 575 layout.at(2).top()));
575 576
576 577 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
577 578 TRY_COMPARE(seriesSpy.count(), 1);
578 579 TRY_COMPARE(seriesIndexSpy.count(), 1);
579 580 TRY_COMPARE(setIndexSpy1.count(), 1);
580 581 TRY_COMPARE(setIndexSpy2.count(), 0);
581 582
582 583 //should leave set1, bar1
583 584 seriesSpyArg = seriesSpy.takeFirst();
584 585 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set1);
585 586 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
586 587 QVERIFY(seriesSpyArg.at(0).toBool() == false);
587 588
588 589 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
589 590 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set1);
590 591 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
591 592 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
592 593 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
593 594 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
594 595
595 596 setIndexSpyArg = setIndexSpy1.takeFirst();
596 597 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
597 598 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
598 599 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
599 600 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
600 601
601 602 //=======================================================================
602 603 // move mouse on top of set2, bar1 to check the index (hover in set2)
603 604 QTest::mouseMove(view.viewport(), layout.at(3).center().toPoint());
604 605
605 606 TRY_COMPARE(seriesSpy.count(), 1);
606 607 TRY_COMPARE(seriesIndexSpy.count(), 1);
607 608 TRY_COMPARE(setIndexSpy1.count(), 0);
608 609 TRY_COMPARE(setIndexSpy2.count(), 1);
609 610
610 611 //should enter set2, bar1
611 612 seriesSpyArg = seriesSpy.takeFirst();
612 613 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
613 614 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
614 615 QVERIFY(seriesSpyArg.at(0).toBool() == true);
615 616
616 617 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
617 618 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
618 619 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
619 620 QVERIFY(seriesIndexSpyArg.at(0).toBool() == true);
620 621 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
621 622 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
622 623
623 624 setIndexSpyArg = setIndexSpy2.takeFirst();
624 625 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
625 626 QVERIFY(setIndexSpyArg.at(0).toBool() == true);
626 627 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
627 628 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
628 629
629 630 //=======================================================================
630 631 // move mouse between set1 and set2 (hover out set2)
631 632 QTest::mouseMove(view.viewport(), QPoint((layout.at(3).right() + layout.at(4).left()) /2,
632 633 layout.at(2).top()));
633 634
634 635 QCoreApplication::processEvents(QEventLoop::AllEvents, 1000);
635 636 TRY_COMPARE(seriesSpy.count(), 1);
636 637 TRY_COMPARE(seriesIndexSpy.count(), 1);
637 638 TRY_COMPARE(setIndexSpy1.count(), 0);
638 639 TRY_COMPARE(setIndexSpy2.count(), 1);
639 640
640 641 //should leave set1, bar1
641 642 seriesSpyArg = seriesSpy.takeFirst();
642 643 QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(1)), set2);
643 644 QVERIFY(seriesSpyArg.at(0).type() == QVariant::Bool);
644 645 QVERIFY(seriesSpyArg.at(0).toBool() == false);
645 646
646 647 seriesIndexSpyArg = seriesIndexSpy.takeFirst();
647 648 QCOMPARE(qvariant_cast<QBarSet*>(seriesIndexSpyArg.at(2)), set2);
648 649 QVERIFY(seriesIndexSpyArg.at(0).type() == QVariant::Bool);
649 650 QVERIFY(seriesIndexSpyArg.at(0).toBool() == false);
650 651 QVERIFY(seriesIndexSpyArg.at(1).type() == QVariant::Int);
651 652 QVERIFY(seriesIndexSpyArg.at(1).toInt() == 1);
652 653
653 654 setIndexSpyArg = setIndexSpy2.takeFirst();
654 655 QVERIFY(setIndexSpyArg.at(0).type() == QVariant::Bool);
655 656 QVERIFY(setIndexSpyArg.at(0).toBool() == false);
656 657 QVERIFY(setIndexSpyArg.at(1).type() == QVariant::Int);
657 658 QVERIFY(setIndexSpyArg.at(1).toInt() == 1);
658 659 }
659 660
661 void tst_QPercentBarSeries::zeroValuesInSeries()
662 {
663 QPercentBarSeries *series = new QPercentBarSeries();
664 QBarSet *set1 = new QBarSet(QString("set 1"));
665 *set1 << 100 << 0.0 << 10;
666 series->append(set1);
667
668 QBarSet *set2 = new QBarSet(QString("set 2"));
669 *set2 << 0.0 << 0.0 << 70;
670 series->append(set2);
671
672 QChartView view(new QChart());
673 view.chart()->addSeries(series);
674 view.chart()->createDefaultAxes();
675 view.show();
676
677 QTest::qWaitForWindowShown(&view);
678 }
679
660 680 QTEST_MAIN(tst_QPercentBarSeries)
661 681
662 682 #include "tst_qpercentbarseries.moc"
663 683
General Comments 0
You need to be logged in to leave comments. Login now