@@ -1,37 +1,35 | |||
|
1 | 1 | INCLUDEPATH += $$PWD |
|
2 | 2 | DEPENDPATH += $$PWD |
|
3 | 3 | |
|
4 | 4 | SOURCES += \ |
|
5 | 5 | $$PWD/bar.cpp \ |
|
6 | 6 | $$PWD/barchartitem.cpp \ |
|
7 | 7 | $$PWD/percentbarchartitem.cpp \ |
|
8 | 8 | $$PWD/groupedbarchartitem.cpp \ |
|
9 | 9 | $$PWD/qbarseries.cpp \ |
|
10 | 10 | $$PWD/qbarset.cpp \ |
|
11 | 11 | $$PWD/qpercentbarseries.cpp \ |
|
12 | 12 | $$PWD/qstackedbarseries.cpp \ |
|
13 | 13 | $$PWD/qgroupedbarseries.cpp \ |
|
14 | 14 | $$PWD/stackedbarchartitem.cpp \ |
|
15 | $$PWD/barlabel.cpp \ | |
|
16 | 15 | $$PWD/qbarmodelmapper.cpp |
|
17 | 16 | |
|
18 | 17 | PRIVATE_HEADERS += \ |
|
19 | 18 | $$PWD/bar_p.h \ |
|
20 | 19 | $$PWD/barchartitem_p.h \ |
|
21 | 20 | $$PWD/percentbarchartitem_p.h \ |
|
22 | 21 | $$PWD/stackedbarchartitem_p.h \ |
|
23 | 22 | $$PWD/groupedbarchartitem_p.h \ |
|
24 | $$PWD/barlabel_p.h \ | |
|
25 | 23 | $$PWD/qbarset_p.h \ |
|
26 | 24 | $$PWD/qbarseries_p.h \ |
|
27 | 25 | $$PWD/qstackedbarseries_p.h\ |
|
28 | 26 | $$PWD/qpercentbarseries_p.h \ |
|
29 | 27 | $$PWD/qgroupedbarseries_p.h \ |
|
30 | 28 | |
|
31 | 29 | PUBLIC_HEADERS += \ |
|
32 | 30 | $$PWD/qbarseries.h \ |
|
33 | 31 | $$PWD/qbarset.h \ |
|
34 | 32 | $$PWD/qpercentbarseries.h \ |
|
35 | 33 | $$PWD/qstackedbarseries.h \ |
|
36 | 34 | $$PWD/qgroupedbarseries.h \ |
|
37 | 35 | $$PWD/qbarmodelmapper.h |
@@ -1,210 +1,217 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 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 Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial 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 "barchartitem_p.h" |
|
22 | 22 | #include "bar_p.h" |
|
23 | #include "barlabel_p.h" | |
|
24 | 23 | #include "qbarset.h" |
|
25 | 24 | #include "qbarset_p.h" |
|
26 | 25 | #include "qbarseries.h" |
|
27 | 26 | #include "qbarseries_p.h" |
|
28 | 27 | #include "qchart.h" |
|
29 | 28 | #include "chartpresenter_p.h" |
|
30 | 29 | #include "chartanimator_p.h" |
|
31 | 30 | #include "chartdataset_p.h" |
|
32 | 31 | #include <QPainter> |
|
33 | 32 | |
|
34 | 33 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
35 | 34 | |
|
36 | 35 | BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) : |
|
37 | 36 | ChartItem(presenter), |
|
38 | 37 | m_layoutSet(false), |
|
39 | 38 | m_series(series) |
|
40 | 39 | { |
|
41 | 40 | setFlag(ItemClipsChildrenToShape); |
|
42 | 41 | connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged())); |
|
43 | 42 | connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleModelChanged())); |
|
43 | connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(labelsVisibleChanged(bool))); | |
|
44 | 44 | setZValue(ChartPresenter::BarSeriesZValue); |
|
45 | 45 | dataChanged(); |
|
46 | 46 | } |
|
47 | 47 | |
|
48 | 48 | BarChartItem::~BarChartItem() |
|
49 | 49 | { |
|
50 | 50 | } |
|
51 | 51 | |
|
52 | 52 | void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
53 | 53 | { |
|
54 | 54 | Q_UNUSED(painter); |
|
55 | 55 | Q_UNUSED(option); |
|
56 | 56 | Q_UNUSED(widget); |
|
57 | 57 | } |
|
58 | 58 | |
|
59 | 59 | QRectF BarChartItem::boundingRect() const |
|
60 | 60 | { |
|
61 | 61 | return m_rect; |
|
62 | 62 | } |
|
63 | 63 | |
|
64 | 64 | void BarChartItem::dataChanged() |
|
65 | 65 | { |
|
66 | 66 | foreach(QGraphicsItem *item, childItems()) { |
|
67 | 67 | delete item; |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | 70 | m_bars.clear(); |
|
71 | 71 | m_labels.clear(); |
|
72 | 72 | m_layout.clear(); |
|
73 | 73 | |
|
74 | bool labelsVisible = m_series->isLabelsVisible(); | |
|
75 | ||
|
74 | 76 | // Create new graphic items for bars |
|
75 | 77 | for (int c = 0; c < m_series->categoryCount(); c++) { |
|
76 | 78 | QString category = m_series->d_func()->categoryName(c); |
|
77 | 79 | for (int s = 0; s < m_series->barsetCount(); s++) { |
|
78 | 80 | QBarSet *set = m_series->d_func()->barsetAt(s); |
|
81 | ||
|
82 | // Bars | |
|
79 | 83 | Bar *bar = new Bar(set,category,this); |
|
80 | 84 | m_bars.append(bar); |
|
81 | 85 | connect(bar, SIGNAL(clicked(QString)), set, SIGNAL(clicked(QString))); |
|
82 | 86 | connect(bar, SIGNAL(clicked(QBarSet*,QString)), m_series, SIGNAL(clicked(QBarSet*,QString))); |
|
83 | 87 | connect(bar, SIGNAL(hovered(bool)), set, SIGNAL(hovered(bool))); |
|
84 | 88 | connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool))); |
|
85 | 89 | m_layout.append(QRectF(0, 0, 0, 0)); |
|
86 | } | |
|
87 | } | |
|
88 | 90 | |
|
89 | // Create labels | |
|
90 | for (int category = 0; category < m_series->categoryCount(); category++) { | |
|
91 | for (int s = 0; s < m_series->barsetCount(); s++) { | |
|
92 | QBarSet *set = m_series->d_func()->barsetAt(s); | |
|
93 | BarLabel *value = new BarLabel(*set, this); | |
|
94 | m_labels.append(value); | |
|
95 | connect(set->d_ptr.data(),SIGNAL(labelsVisibleChanged(bool)),value,SLOT(labelsVisibleChanged(bool))); | |
|
91 | // Labels | |
|
92 | QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this); | |
|
93 | label->setVisible(labelsVisible); | |
|
94 | m_labels.append(label); | |
|
96 | 95 | } |
|
97 |
} |
|
|
96 | } | |
|
98 | 97 | |
|
99 | 98 | // TODO: Is this the right place to call it? |
|
100 | 99 | // presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series)); |
|
101 | 100 | } |
|
102 | 101 | |
|
103 | 102 | QVector<QRectF> BarChartItem::calculateLayout() |
|
104 | 103 | { |
|
105 | 104 | QVector<QRectF> layout; |
|
106 | 105 | |
|
107 | 106 | // Use temporary qreals for accurancy |
|
108 | 107 | qreal categoryCount = m_series->categoryCount(); |
|
109 | 108 | qreal setCount = m_series->barsetCount(); |
|
110 | 109 | |
|
111 | 110 | // Domain: |
|
112 | 111 | qreal width = geometry().width(); |
|
113 | 112 | qreal height = geometry().height(); |
|
114 | 113 | qreal rangeY = m_domainMaxY - m_domainMinY; |
|
115 | 114 | qreal rangeX = m_domainMaxX - m_domainMinX; |
|
116 | 115 | qreal scaleY = (height / rangeY); |
|
117 | 116 | qreal scaleX = (width / rangeX); |
|
118 | 117 | qreal categoryWidth = width / categoryCount; |
|
119 | 118 | qreal barWidth = categoryWidth / setCount - categoryWidth * m_series->d_func()->barMargin(); |
|
120 | 119 | |
|
121 | 120 | int itemIndex(0); |
|
122 | 121 | for (int category = 0; category < categoryCount; category++) { |
|
123 | 122 | qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y(); |
|
124 | 123 | for (int set = 0; set < setCount; set++) { |
|
125 | 124 | QBarSet* barSet = m_series->d_func()->barsetAt(set); |
|
126 | 125 | qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2; |
|
127 | 126 | qreal barHeight = barSet->at(category).y() * scaleY; |
|
128 | 127 | |
|
129 | 128 | Bar* bar = m_bars.at(itemIndex); |
|
130 | 129 | QRectF rect(xPos, yPos - barHeight, barWidth, barHeight); |
|
131 | 130 | |
|
132 | 131 | layout.append(rect); |
|
133 | 132 | bar->setPen(barSet->pen()); |
|
134 | 133 | bar->setBrush(barSet->brush()); |
|
135 | 134 | |
|
136 |
|
|
|
135 | QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); | |
|
137 | 136 | |
|
138 | 137 | if (!qFuzzyIsNull(barSet->at(category).y())) { |
|
139 | 138 | label->setText(QString::number(barSet->at(category).y())); |
|
140 | 139 | } else { |
|
141 | 140 | label->setText(QString("")); |
|
142 | 141 | } |
|
143 | 142 | |
|
144 | 143 | label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) |
|
145 | 144 | ,yPos - barHeight/2 - label->boundingRect().height()/2); |
|
146 | 145 | label->setFont(barSet->labelFont()); |
|
147 | 146 | |
|
148 | 147 | itemIndex++; |
|
149 | 148 | } |
|
150 | 149 | } |
|
151 | 150 | |
|
152 | 151 | return layout; |
|
153 | 152 | } |
|
154 | 153 | |
|
155 | 154 | void BarChartItem::applyLayout(const QVector<QRectF> &layout) |
|
156 | 155 | { |
|
157 | 156 | if (animator()) |
|
158 | 157 | animator()->updateLayout(this, m_layout, layout); |
|
159 | 158 | else |
|
160 | 159 | setLayout(layout); |
|
161 | 160 | } |
|
162 | 161 | |
|
163 | 162 | void BarChartItem::setLayout(const QVector<QRectF> &layout) |
|
164 | 163 | { |
|
165 | 164 | m_layout = layout; |
|
166 | 165 | |
|
167 | 166 | for (int i=0; i < m_bars.count(); i++) |
|
168 | 167 | m_bars.at(i)->setRect(layout.at(i)); |
|
169 | 168 | |
|
170 | 169 | update(); |
|
171 | 170 | } |
|
172 | 171 | //handlers |
|
173 | 172 | |
|
174 | 173 | void BarChartItem::handleModelChanged() |
|
175 | 174 | { |
|
176 | 175 | // dataChanged(); |
|
177 | 176 | presenter()->resetAllElements(); |
|
178 | 177 | } |
|
179 | 178 | |
|
180 | 179 | void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY) |
|
181 | 180 | { |
|
182 | 181 | m_domainMinX = minX; |
|
183 | 182 | m_domainMaxX = maxX; |
|
184 | 183 | m_domainMinY = minY; |
|
185 | 184 | m_domainMaxY = maxY; |
|
186 | 185 | handleLayoutChanged(); |
|
187 | 186 | } |
|
188 | 187 | |
|
189 | 188 | void BarChartItem::handleGeometryChanged(const QRectF &rect) |
|
190 | 189 | { |
|
191 | 190 | prepareGeometryChange(); |
|
192 | 191 | m_rect = rect; |
|
193 | 192 | handleLayoutChanged(); |
|
194 | 193 | m_layoutSet = true; |
|
195 | 194 | } |
|
196 | 195 | |
|
197 | 196 | void BarChartItem::handleLayoutChanged() |
|
198 | 197 | { |
|
199 | 198 | if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) { |
|
200 | 199 | // rect size zero. |
|
201 | 200 | return; |
|
202 | 201 | } |
|
203 | 202 | QVector<QRectF> layout = calculateLayout(); |
|
204 | 203 | applyLayout(layout); |
|
205 | 204 | update(); |
|
206 | 205 | } |
|
207 | 206 | |
|
207 | void BarChartItem::labelsVisibleChanged(bool visible) | |
|
208 | { | |
|
209 | foreach (QGraphicsSimpleTextItem* label, m_labels) { | |
|
210 | label->setVisible(visible); | |
|
211 | } | |
|
212 | update(); | |
|
213 | } | |
|
214 | ||
|
208 | 215 | #include "moc_barchartitem_p.cpp" |
|
209 | 216 | |
|
210 | 217 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,84 +1,84 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 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 Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial 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 | #ifndef BARCHARTITEM_H |
|
22 | 22 | #define BARCHARTITEM_H |
|
23 | 23 | |
|
24 | 24 | #include "chartitem_p.h" |
|
25 | 25 | #include "qbarseries.h" |
|
26 | 26 | #include <QPen> |
|
27 | 27 | #include <QBrush> |
|
28 | 28 | |
|
29 | 29 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
30 | 30 | |
|
31 | 31 | class Bar; |
|
32 | class BarLabel; | |
|
33 | 32 | class QAxisCategories; |
|
34 | 33 | class QChart; |
|
35 | 34 | |
|
36 | 35 | //typedef QVector<QRectF> BarLayout; |
|
37 | 36 | |
|
38 | 37 | class BarChartItem : public ChartItem |
|
39 | 38 | { |
|
40 | 39 | Q_OBJECT |
|
41 | 40 | public: |
|
42 | 41 | BarChartItem(QBarSeries *series, ChartPresenter *presenter); |
|
43 | 42 | virtual ~BarChartItem(); |
|
44 | 43 | |
|
45 | 44 | public: |
|
46 | 45 | // From QGraphicsItem |
|
47 | 46 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
48 | 47 | QRectF boundingRect() const; |
|
49 | 48 | |
|
50 | 49 | virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes |
|
51 | 50 | |
|
52 | 51 | virtual QVector<QRectF> calculateLayout(); |
|
53 | 52 | void applyLayout(const QVector<QRectF> &layout); |
|
54 | 53 | void setLayout(const QVector<QRectF> &layout); |
|
55 | 54 | void updateLayout(const QVector<QRectF> &layout); |
|
56 | 55 | |
|
57 | 56 | QRectF geometry() const { return m_rect;} |
|
58 | 57 | |
|
59 | 58 | public Q_SLOTS: |
|
60 | 59 | void handleModelChanged(); |
|
61 | 60 | void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY); |
|
62 | 61 | void handleGeometryChanged(const QRectF &size); |
|
63 | 62 | void handleLayoutChanged(); |
|
63 | void labelsVisibleChanged(bool visible); | |
|
64 | 64 | |
|
65 | 65 | protected: |
|
66 | 66 | |
|
67 | 67 | qreal m_domainMinX; |
|
68 | 68 | qreal m_domainMaxX; |
|
69 | 69 | qreal m_domainMinY; |
|
70 | 70 | qreal m_domainMaxY; |
|
71 | 71 | |
|
72 | 72 | QRectF m_rect; |
|
73 | 73 | bool m_layoutSet; // True, if component has been laid out. |
|
74 | 74 | QVector<QRectF> m_layout; |
|
75 | 75 | |
|
76 | 76 | // Not owned. |
|
77 | 77 | QBarSeries *m_series; |
|
78 | 78 | QList<Bar *> m_bars; |
|
79 |
QList< |
|
|
79 | QList<QGraphicsSimpleTextItem *> m_labels; | |
|
80 | 80 | }; |
|
81 | 81 | |
|
82 | 82 | QTCOMMERCIALCHART_END_NAMESPACE |
|
83 | 83 | |
|
84 | 84 | #endif // BARCHARTITEM_H |
@@ -1,90 +1,89 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 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 Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial 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 "groupedbarchartitem_p.h" |
|
22 | 22 | #include "bar_p.h" |
|
23 | #include "barlabel_p.h" | |
|
24 | 23 | #include "qbarset_p.h" |
|
25 | 24 | #include "qbarseries_p.h" |
|
26 | 25 | #include "qbarset.h" |
|
27 | 26 | |
|
28 | 27 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
29 | 28 | |
|
30 | 29 | GroupedBarChartItem::GroupedBarChartItem(QBarSeries *series, ChartPresenter *presenter) : |
|
31 | 30 | BarChartItem(series, presenter) |
|
32 | 31 | { |
|
33 | 32 | } |
|
34 | 33 | |
|
35 | 34 | QVector<QRectF> GroupedBarChartItem::calculateLayout() |
|
36 | 35 | { |
|
37 | 36 | QVector<QRectF> layout; |
|
38 | 37 | |
|
39 | 38 | // Use temporary qreals for accurancy |
|
40 | 39 | qreal categoryCount = m_series->categoryCount(); |
|
41 | 40 | qreal setCount = m_series->barsetCount(); |
|
42 | 41 | |
|
43 | 42 | // Domain: |
|
44 | 43 | qreal width = geometry().width(); |
|
45 | 44 | qreal height = geometry().height(); |
|
46 | 45 | qreal rangeY = m_domainMaxY - m_domainMinY; |
|
47 | 46 | qreal rangeX = m_domainMaxX - m_domainMinX; |
|
48 | 47 | qreal scaleY = (height / rangeY); |
|
49 | 48 | qreal scaleX = (width / rangeX); |
|
50 | 49 | qreal categoryWidth = width / categoryCount; |
|
51 | 50 | qreal barWidth = categoryWidth / setCount - categoryWidth * m_series->d_func()->barMargin(); |
|
52 | 51 | |
|
53 | 52 | int itemIndex(0); |
|
54 | 53 | for (int category = 0; category < categoryCount; category++) { |
|
55 | 54 | qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y(); |
|
56 | 55 | for (int set = 0; set < setCount; set++) { |
|
57 | 56 | QBarSet* barSet = m_series->d_func()->barsetAt(set); |
|
58 | 57 | |
|
59 | 58 | qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left(); |
|
60 | 59 | xPos -= setCount*barWidth/2; |
|
61 | 60 | xPos += set*barWidth; |
|
62 | 61 | qreal barHeight = barSet->at(category).y() * scaleY; |
|
63 | 62 | Bar* bar = m_bars.at(itemIndex); |
|
64 | 63 | |
|
65 | 64 | QRectF rect(xPos, yPos - barHeight, barWidth, barHeight); |
|
66 | 65 | layout.append(rect); |
|
67 | 66 | bar->setPen(barSet->pen()); |
|
68 | 67 | bar->setBrush(barSet->brush()); |
|
69 | 68 | |
|
70 |
|
|
|
69 | QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); | |
|
71 | 70 | |
|
72 | 71 | if (!qFuzzyIsNull(barSet->at(category).y())) { |
|
73 | 72 | label->setText(QString::number(barSet->at(category).y())); |
|
74 | 73 | } else { |
|
75 | 74 | label->setText(QString("")); |
|
76 | 75 | } |
|
77 | 76 | |
|
78 | 77 | label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) |
|
79 | 78 | ,yPos - barHeight/2 - label->boundingRect().height()/2); |
|
80 | 79 | label->setFont(barSet->labelFont()); |
|
81 | 80 | |
|
82 | 81 | itemIndex++; |
|
83 | 82 | } |
|
84 | 83 | } |
|
85 | 84 | return layout; |
|
86 | 85 | } |
|
87 | 86 | |
|
88 | 87 | #include "moc_groupedbarchartitem_p.cpp" |
|
89 | 88 | |
|
90 | 89 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,94 +1,93 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 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 Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial 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 | #include "barlabel_p.h" | |
|
24 | 23 | #include "qbarseries_p.h" |
|
25 | 24 | #include "qbarset.h" |
|
26 | 25 | #include <QDebug> |
|
27 | 26 | |
|
28 | 27 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
29 | 28 | |
|
30 | 29 | PercentBarChartItem::PercentBarChartItem(QBarSeries *series, ChartPresenter *presenter) : |
|
31 | 30 | BarChartItem(series, presenter) |
|
32 | 31 | { |
|
33 | 32 | } |
|
34 | 33 | |
|
35 | 34 | QVector<QRectF> PercentBarChartItem::calculateLayout() |
|
36 | 35 | { |
|
37 | 36 | QVector<QRectF> layout; |
|
38 | 37 | |
|
39 | 38 | // Use temporary qreals for accurancy |
|
40 | 39 | qreal categoryCount = m_series->categoryCount(); |
|
41 | 40 | qreal setCount = m_series->barsetCount(); |
|
42 | 41 | |
|
43 | 42 | // Domain: |
|
44 | 43 | qreal width = geometry().width(); |
|
45 | 44 | qreal height = geometry().height(); |
|
46 | 45 | qreal rangeY = m_domainMaxY - m_domainMinY; |
|
47 | 46 | qreal rangeX = m_domainMaxX - m_domainMinX; |
|
48 | 47 | qreal scaleY = (height / rangeY); |
|
49 | 48 | qreal scaleX = (width / rangeX); |
|
50 | 49 | qreal categoryWidth = width / categoryCount; |
|
51 | 50 | qreal barWidth = categoryWidth / setCount - categoryWidth * m_series->d_func()->barMargin(); |
|
52 | 51 | |
|
53 | 52 | int itemIndex(0); |
|
54 | 53 | for (int category = 0; category < categoryCount; category++) { |
|
55 | 54 | qreal colSum = m_series->d_func()->categorySum(category); |
|
56 | 55 | qreal percentage = (100 / colSum); |
|
57 | 56 | qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y(); |
|
58 | 57 | for (int set=0; set < setCount; set++) { |
|
59 | 58 | QBarSet* barSet = m_series->d_func()->barsetAt(set); |
|
60 | 59 | |
|
61 | 60 | qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2; |
|
62 | 61 | |
|
63 | 62 | qreal barHeight = barSet->at(category).y() * percentage * scaleY; |
|
64 | 63 | Bar* bar = m_bars.at(itemIndex); |
|
65 | 64 | bar->setPen(barSet->pen()); |
|
66 | 65 | bar->setBrush(barSet->brush()); |
|
67 | 66 | QRectF rect(xPos, yPos-barHeight, barWidth, barHeight); |
|
68 | 67 | layout.append(rect); |
|
69 | 68 | |
|
70 |
|
|
|
69 | QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); | |
|
71 | 70 | |
|
72 | 71 | if (!qFuzzyIsNull(m_series->d_func()->valueAt(set,category))) { |
|
73 | 72 | int p = m_series->d_func()->percentageAt(set,category) * 100; |
|
74 | 73 | QString vString(QString::number(p)); |
|
75 | 74 | vString.truncate(3); |
|
76 | 75 | vString.append("%"); |
|
77 | 76 | label->setText(vString); |
|
78 | 77 | } else { |
|
79 | 78 | label->setText(QString("")); |
|
80 | 79 | } |
|
81 | 80 | |
|
82 | 81 | label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) |
|
83 | 82 | ,yPos - barHeight/2 - label->boundingRect().height()/2); |
|
84 | 83 | label->setFont(barSet->labelFont()); |
|
85 | 84 | itemIndex++; |
|
86 | 85 | yPos -= barHeight; |
|
87 | 86 | } |
|
88 | 87 | } |
|
89 | 88 | return layout; |
|
90 | 89 | } |
|
91 | 90 | |
|
92 | 91 | #include "moc_percentbarchartitem_p.cpp" |
|
93 | 92 | |
|
94 | 93 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,483 +1,492 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 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 Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial 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 "qbarseries.h" |
|
22 | 22 | #include "qbarseries_p.h" |
|
23 | 23 | #include "qbarset.h" |
|
24 | 24 | #include "qbarset_p.h" |
|
25 | 25 | #include "domain_p.h" |
|
26 | 26 | #include "legendmarker_p.h" |
|
27 | 27 | #include "chartdataset_p.h" |
|
28 | 28 | #include "charttheme_p.h" |
|
29 | 29 | #include "chartanimator_p.h" |
|
30 | 30 | |
|
31 | 31 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
32 | 32 | |
|
33 | 33 | /*! |
|
34 | 34 | \class QBarSeries |
|
35 | 35 | \brief part of QtCommercial chart API. |
|
36 | 36 | \mainclass |
|
37 | 37 | |
|
38 | 38 | QBarSeries represents a series of data shown as bars. The purpose of this class is to draw bars to |
|
39 | 39 | the position defined by data. Single bar is defined by QPointF, where x value is the x-coordinate of the bar |
|
40 | 40 | and y-value is the height of the bar. The category names are ignored with this series and x-axis |
|
41 | 41 | shows the x-values. |
|
42 | 42 | |
|
43 | 43 | See the \l {BarChart Example} {bar chart example} to learn how to create a simple bar chart. |
|
44 | 44 | \image examples_barchart.png |
|
45 | 45 | |
|
46 | 46 | \sa QBarSet, QStackedBarSeries, QPercentBarSeries |
|
47 | 47 | */ |
|
48 | 48 | |
|
49 | 49 | /*! |
|
50 | 50 | \fn void QBarSeries::clicked(QBarSet *barset, QString category) |
|
51 | 51 | |
|
52 | 52 | The signal is emitted if the user clicks with a mouse on top of QBarSet \a barset of category \a category |
|
53 | 53 | contained by the series. |
|
54 | 54 | */ |
|
55 | 55 | |
|
56 | 56 | /*! |
|
57 | 57 | \fn void QBarSeries::hovered(QBarSet* barset, bool status) |
|
58 | 58 | |
|
59 | 59 | The signal is emitted if mouse is hovered on top of series. |
|
60 | 60 | Parameter \a barset is the pointer of barset, where hover happened. |
|
61 | 61 | Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series. |
|
62 | 62 | */ |
|
63 | 63 | |
|
64 | 64 | /*! |
|
65 | 65 | Constructs empty QBarSeries. |
|
66 | 66 | QBarSeries is QObject which is a child of a \a parent. |
|
67 | 67 | */ |
|
68 | 68 | QBarSeries::QBarSeries(QObject *parent) : |
|
69 | 69 | QAbstractSeries(*new QBarSeriesPrivate(this),parent) |
|
70 | 70 | { |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | /*! |
|
74 | 74 | Destructs barseries and owned barsets. |
|
75 | 75 | */ |
|
76 | 76 | QBarSeries::~QBarSeries() |
|
77 | 77 | { |
|
78 | 78 | // NOTE: d_ptr destroyed by QObject |
|
79 | 79 | } |
|
80 | 80 | |
|
81 | 81 | /*! |
|
82 | 82 | \internal |
|
83 | 83 | */ |
|
84 | 84 | QBarSeries::QBarSeries(QBarSeriesPrivate &d, QObject *parent) : |
|
85 | 85 | QAbstractSeries(d,parent) |
|
86 | 86 | { |
|
87 | 87 | } |
|
88 | 88 | |
|
89 | 89 | /*! |
|
90 | 90 | Returns the type of series. Derived classes override this. |
|
91 | 91 | */ |
|
92 | 92 | QAbstractSeries::SeriesType QBarSeries::type() const |
|
93 | 93 | { |
|
94 | 94 | return QAbstractSeries::SeriesTypeBar; |
|
95 | 95 | } |
|
96 | 96 | |
|
97 | 97 | /*! |
|
98 | 98 | Sets the \a categories, which are used to to group the data. |
|
99 | 99 | */ |
|
100 | 100 | void QBarSeries::setCategories(QBarCategories categories) |
|
101 | 101 | { |
|
102 | 102 | Q_D(QBarSeries); |
|
103 | 103 | d->setCategories(categories); |
|
104 | 104 | emit d->categoriesUpdated(); |
|
105 | 105 | } |
|
106 | 106 | |
|
107 | 107 | /*! |
|
108 | 108 | 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. |
|
109 | 109 | Returns true, if appending succeeded. |
|
110 | 110 | |
|
111 | 111 | */ |
|
112 | 112 | bool QBarSeries::append(QBarSet *set) |
|
113 | 113 | { |
|
114 | 114 | Q_D(QBarSeries); |
|
115 | 115 | if ((d->m_barSets.contains(set)) || (set == 0)) { |
|
116 | 116 | // Fail if set is already in list or set is null. |
|
117 | 117 | return false; |
|
118 | 118 | } |
|
119 | 119 | d->m_barSets.append(set); |
|
120 | 120 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged())); |
|
121 | 121 | emit d->restructuredBars(); |
|
122 | 122 | return true; |
|
123 | 123 | } |
|
124 | 124 | |
|
125 | 125 | /*! |
|
126 | 126 | Removes a set of bars from series. Releases ownership of \a set. Doesn't delete \a set. |
|
127 | 127 | Returns true, if set was removed. |
|
128 | 128 | */ |
|
129 | 129 | bool QBarSeries::remove(QBarSet *set) |
|
130 | 130 | { |
|
131 | 131 | Q_D(QBarSeries); |
|
132 | 132 | if (!d->m_barSets.contains(set)) { |
|
133 | 133 | // Fail if set is not in list |
|
134 | 134 | return false; |
|
135 | 135 | } |
|
136 | 136 | d->m_barSets.removeOne(set); |
|
137 | 137 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged())); |
|
138 | 138 | emit d->restructuredBars(); |
|
139 | 139 | return true; |
|
140 | 140 | } |
|
141 | 141 | |
|
142 | 142 | /*! |
|
143 | 143 | Adds a list of barsets to series. Takes ownership of \a sets. |
|
144 | 144 | Returns true, if all sets were appended succesfully. If any of the sets is null or is already appended to series, |
|
145 | 145 | nothing is appended and function returns false. If any of the sets is in list more than once, nothing is appended |
|
146 | 146 | and function returns false. |
|
147 | 147 | */ |
|
148 | 148 | bool QBarSeries::append(QList<QBarSet* > sets) |
|
149 | 149 | { |
|
150 | 150 | Q_D(QBarSeries); |
|
151 | 151 | foreach (QBarSet* set, sets) { |
|
152 | 152 | if ((set == 0) || (d->m_barSets.contains(set))) { |
|
153 | 153 | // Fail if any of the sets is null or is already appended. |
|
154 | 154 | return false; |
|
155 | 155 | } |
|
156 | 156 | if (sets.count(set) != 1) { |
|
157 | 157 | // Also fail if same set is more than once in given list. |
|
158 | 158 | return false; |
|
159 | 159 | } |
|
160 | 160 | } |
|
161 | 161 | |
|
162 | 162 | foreach (QBarSet* set, sets) { |
|
163 | 163 | d->m_barSets.append(set); |
|
164 | 164 | QObject::connect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged())); |
|
165 | 165 | } |
|
166 | 166 | emit d->restructuredBars(); |
|
167 | 167 | return true; |
|
168 | 168 | } |
|
169 | 169 | |
|
170 | 170 | /*! |
|
171 | 171 | Removes a list of barsets from series. Releases ownership of \a sets. Doesn't delete \a sets. |
|
172 | 172 | */ |
|
173 | 173 | bool QBarSeries::remove(QList<QBarSet* > sets) |
|
174 | 174 | { |
|
175 | 175 | Q_D(QBarSeries); |
|
176 | 176 | |
|
177 | 177 | bool setsRemoved = false; |
|
178 | 178 | foreach (QBarSet* set, sets) { |
|
179 | 179 | if (d->m_barSets.contains(set)) { |
|
180 | 180 | d->m_barSets.removeOne(set); |
|
181 | 181 | QObject::disconnect(set->d_ptr.data(), SIGNAL(updatedBars()), d, SLOT(barsetChanged())); |
|
182 | 182 | setsRemoved = true; |
|
183 | 183 | } |
|
184 | 184 | } |
|
185 | 185 | |
|
186 | 186 | if (setsRemoved) { |
|
187 | 187 | emit d->restructuredBars(); |
|
188 | 188 | } |
|
189 | 189 | return setsRemoved; |
|
190 | 190 | } |
|
191 | 191 | |
|
192 | 192 | /*! |
|
193 | 193 | Returns number of sets in series. |
|
194 | 194 | */ |
|
195 | 195 | int QBarSeries::barsetCount() const |
|
196 | 196 | { |
|
197 | 197 | Q_D(const QBarSeries); |
|
198 | 198 | return d->m_barSets.count(); |
|
199 | 199 | } |
|
200 | 200 | |
|
201 | 201 | /*! |
|
202 | 202 | Returns number of categories in series |
|
203 | 203 | */ |
|
204 | 204 | int QBarSeries::categoryCount() const |
|
205 | 205 | { |
|
206 | 206 | Q_D(const QBarSeries); |
|
207 | 207 | return d->categoryCount(); |
|
208 | 208 | } |
|
209 | 209 | |
|
210 | 210 | /*! |
|
211 | 211 | Returns a list of sets in series. Keeps ownership of sets. |
|
212 | 212 | */ |
|
213 | 213 | QList<QBarSet*> QBarSeries::barSets() const |
|
214 | 214 | { |
|
215 | 215 | Q_D(const QBarSeries); |
|
216 | 216 | return d->m_barSets; |
|
217 | 217 | } |
|
218 | 218 | |
|
219 | 219 | /*! |
|
220 | 220 | Returns the bar categories of the series. |
|
221 | 221 | */ |
|
222 | 222 | QBarCategories QBarSeries::categories() const |
|
223 | 223 | { |
|
224 | 224 | Q_D(const QBarSeries); |
|
225 | 225 | return d->categories(); |
|
226 | 226 | } |
|
227 | 227 | |
|
228 | 228 | /*! |
|
229 | 229 | Sets the visibility of labels in series to \a visible |
|
230 | 230 | */ |
|
231 | 231 | void QBarSeries::setLabelsVisible(bool visible) |
|
232 | 232 | { |
|
233 | foreach (QBarSet* s, barSets()) { | |
|
234 |
|
|
|
233 | Q_D(QBarSeries); | |
|
234 | if (d->m_labelsVisible != visible) { | |
|
235 | d->m_labelsVisible = visible; | |
|
236 | emit d->updatedBars(); | |
|
235 | 237 | } |
|
236 | 238 | } |
|
237 | 239 | |
|
240 | bool QBarSeries::isLabelsVisible() const | |
|
241 | { | |
|
242 | Q_D(const QBarSeries); | |
|
243 | return d->m_labelsVisible; | |
|
244 | } | |
|
245 | ||
|
238 | 246 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
239 | 247 | |
|
240 | 248 | QBarSeriesPrivate::QBarSeriesPrivate(QBarSeries *q) : |
|
241 | 249 | QAbstractSeriesPrivate(q), |
|
242 | m_barMargin(0.05) // Default value is 5% of category width | |
|
250 | m_barMargin(0.05), // Default value is 5% of category width | |
|
251 | m_labelsVisible(false) | |
|
243 | 252 | { |
|
244 | 253 | } |
|
245 | 254 | |
|
246 | 255 | void QBarSeriesPrivate::setCategories(QBarCategories categories) |
|
247 | 256 | { |
|
248 | 257 | m_categories = categories; |
|
249 | 258 | } |
|
250 | 259 | |
|
251 | 260 | void QBarSeriesPrivate::insertCategory(int index, const QString category) |
|
252 | 261 | { |
|
253 | 262 | m_categories.insert(index, category); |
|
254 | 263 | emit categoriesUpdated(); |
|
255 | 264 | } |
|
256 | 265 | |
|
257 | 266 | void QBarSeriesPrivate::removeCategory(int index) |
|
258 | 267 | { |
|
259 | 268 | m_categories.removeAt(index); |
|
260 | 269 | emit categoriesUpdated(); |
|
261 | 270 | } |
|
262 | 271 | |
|
263 | 272 | int QBarSeriesPrivate::categoryCount() const |
|
264 | 273 | { |
|
265 | 274 | if (m_categories.count() > 0) { |
|
266 | 275 | return m_categories.count(); |
|
267 | 276 | } |
|
268 | 277 | |
|
269 | 278 | // No categories defined. return count of longest set. |
|
270 | 279 | int count = 0; |
|
271 | 280 | for (int i=0; i<m_barSets.count(); i++) { |
|
272 | 281 | if (m_barSets.at(i)->count() > count) { |
|
273 | 282 | count = m_barSets.at(i)->count(); |
|
274 | 283 | } |
|
275 | 284 | } |
|
276 | 285 | |
|
277 | 286 | return count; |
|
278 | 287 | } |
|
279 | 288 | |
|
280 | 289 | QBarCategories QBarSeriesPrivate::categories() const |
|
281 | 290 | { |
|
282 | 291 | if (m_categories.count() > 0) { |
|
283 | 292 | return m_categories; |
|
284 | 293 | } |
|
285 | 294 | |
|
286 | 295 | // No categories defined. retun list of indices. |
|
287 | 296 | QBarCategories categories; |
|
288 | 297 | |
|
289 | 298 | int count = categoryCount(); |
|
290 | 299 | for (int i = 0; i < count; i++) { |
|
291 | 300 | categories.append(QString::number(i)); |
|
292 | 301 | } |
|
293 | 302 | return categories; |
|
294 | 303 | } |
|
295 | 304 | |
|
296 | 305 | void QBarSeriesPrivate::setBarMargin(qreal margin) |
|
297 | 306 | { |
|
298 | 307 | if (margin > 1.0) { |
|
299 | 308 | margin = 1.0; |
|
300 | 309 | } else if (margin < 0.0) { |
|
301 | 310 | margin = 0.0; |
|
302 | 311 | } |
|
303 | 312 | |
|
304 | 313 | m_barMargin = margin; |
|
305 | 314 | emit updatedBars(); |
|
306 | 315 | } |
|
307 | 316 | |
|
308 | 317 | qreal QBarSeriesPrivate::barMargin() |
|
309 | 318 | { |
|
310 | 319 | return m_barMargin; |
|
311 | 320 | } |
|
312 | 321 | |
|
313 | 322 | QBarSet* QBarSeriesPrivate::barsetAt(int index) |
|
314 | 323 | { |
|
315 | 324 | return m_barSets.at(index); |
|
316 | 325 | } |
|
317 | 326 | |
|
318 | 327 | QString QBarSeriesPrivate::categoryName(int category) |
|
319 | 328 | { |
|
320 | 329 | if ((category >= 0) && (category < m_categories.count())) { |
|
321 | 330 | return m_categories.at(category); |
|
322 | 331 | } |
|
323 | 332 | |
|
324 | 333 | return QString::number(category); |
|
325 | 334 | } |
|
326 | 335 | |
|
327 | 336 | qreal QBarSeriesPrivate::min() |
|
328 | 337 | { |
|
329 | 338 | if (m_barSets.count() <= 0) { |
|
330 | 339 | return 0; |
|
331 | 340 | } |
|
332 | 341 | qreal min = INT_MAX; |
|
333 | 342 | |
|
334 | 343 | for (int i = 0; i < m_barSets.count(); i++) { |
|
335 | 344 | int categoryCount = m_barSets.at(i)->count(); |
|
336 | 345 | for (int j = 0; j < categoryCount; j++) { |
|
337 | 346 | qreal temp = m_barSets.at(i)->at(j).y(); |
|
338 | 347 | if (temp < min) |
|
339 | 348 | min = temp; |
|
340 | 349 | } |
|
341 | 350 | } |
|
342 | 351 | return min; |
|
343 | 352 | } |
|
344 | 353 | |
|
345 | 354 | qreal QBarSeriesPrivate::max() |
|
346 | 355 | { |
|
347 | 356 | if (m_barSets.count() <= 0) { |
|
348 | 357 | return 0; |
|
349 | 358 | } |
|
350 | 359 | qreal max = INT_MIN; |
|
351 | 360 | |
|
352 | 361 | for (int i = 0; i < m_barSets.count(); i++) { |
|
353 | 362 | int categoryCount = m_barSets.at(i)->count(); |
|
354 | 363 | for (int j = 0; j < categoryCount; j++) { |
|
355 | 364 | qreal temp = m_barSets.at(i)->at(j).y(); |
|
356 | 365 | if (temp > max) |
|
357 | 366 | max = temp; |
|
358 | 367 | } |
|
359 | 368 | } |
|
360 | 369 | |
|
361 | 370 | return max; |
|
362 | 371 | } |
|
363 | 372 | |
|
364 | 373 | qreal QBarSeriesPrivate::valueAt(int set, int category) |
|
365 | 374 | { |
|
366 | 375 | if ((set < 0) || (set >= m_barSets.count())) { |
|
367 | 376 | // No set, no value. |
|
368 | 377 | return 0; |
|
369 | 378 | } else if ((category < 0) || (category >= m_barSets.at(set)->count())) { |
|
370 | 379 | // No category, no value. |
|
371 | 380 | return 0; |
|
372 | 381 | } |
|
373 | 382 | |
|
374 | 383 | return m_barSets.at(set)->at(category).y(); |
|
375 | 384 | } |
|
376 | 385 | |
|
377 | 386 | qreal QBarSeriesPrivate::percentageAt(int set, int category) |
|
378 | 387 | { |
|
379 | 388 | if ((set < 0) || (set >= m_barSets.count())) { |
|
380 | 389 | // No set, no value. |
|
381 | 390 | return 0; |
|
382 | 391 | } else if ((category < 0) || (category >= m_barSets.at(set)->count())) { |
|
383 | 392 | // No category, no value. |
|
384 | 393 | return 0; |
|
385 | 394 | } |
|
386 | 395 | |
|
387 | 396 | qreal value = m_barSets.at(set)->at(category).y(); |
|
388 | 397 | qreal sum = categorySum(category); |
|
389 | 398 | if ( qFuzzyIsNull(sum) ) { |
|
390 | 399 | return 0; |
|
391 | 400 | } |
|
392 | 401 | |
|
393 | 402 | return value / sum; |
|
394 | 403 | } |
|
395 | 404 | |
|
396 | 405 | qreal QBarSeriesPrivate::categorySum(int category) |
|
397 | 406 | { |
|
398 | 407 | qreal sum(0); |
|
399 | 408 | int count = m_barSets.count(); // Count sets |
|
400 | 409 | for (int set = 0; set < count; set++) { |
|
401 | 410 | if (category < m_barSets.at(set)->count()) |
|
402 | 411 | sum += m_barSets.at(set)->at(category).y(); |
|
403 | 412 | } |
|
404 | 413 | return sum; |
|
405 | 414 | } |
|
406 | 415 | |
|
407 | 416 | qreal QBarSeriesPrivate::absoluteCategorySum(int category) |
|
408 | 417 | { |
|
409 | 418 | qreal sum(0); |
|
410 | 419 | int count = m_barSets.count(); // Count sets |
|
411 | 420 | for (int set = 0; set < count; set++) { |
|
412 | 421 | if (category < m_barSets.at(set)->count()) |
|
413 | 422 | sum += qAbs(m_barSets.at(set)->at(category).y()); |
|
414 | 423 | } |
|
415 | 424 | return sum; |
|
416 | 425 | } |
|
417 | 426 | |
|
418 | 427 | qreal QBarSeriesPrivate::maxCategorySum() |
|
419 | 428 | { |
|
420 | 429 | qreal max = INT_MIN; |
|
421 | 430 | int count = categoryCount(); |
|
422 | 431 | for (int i = 0; i < count; i++) { |
|
423 | 432 | qreal sum = categorySum(i); |
|
424 | 433 | if (sum > max) |
|
425 | 434 | max = sum; |
|
426 | 435 | } |
|
427 | 436 | return max; |
|
428 | 437 | } |
|
429 | 438 | |
|
430 | 439 | void QBarSeriesPrivate::barsetChanged() |
|
431 | 440 | { |
|
432 | 441 | emit updatedBars(); |
|
433 | 442 | } |
|
434 | 443 | |
|
435 | 444 | void QBarSeriesPrivate::scaleDomain(Domain& domain) |
|
436 | 445 | { |
|
437 | 446 | qreal minX(domain.minX()); |
|
438 | 447 | qreal minY(domain.minY()); |
|
439 | 448 | qreal maxX(domain.maxX()); |
|
440 | 449 | qreal maxY(domain.maxY()); |
|
441 | 450 | int tickXCount(domain.tickXCount()); |
|
442 | 451 | int tickYCount(domain.tickYCount()); |
|
443 | 452 | |
|
444 | 453 | qreal x = categoryCount(); |
|
445 | 454 | qreal y = max(); |
|
446 | 455 | minX = qMin(minX, x) - 0.5; |
|
447 | 456 | minY = qMin(minY, y); |
|
448 | 457 | maxX = qMax(maxX, x) - 0.5; |
|
449 | 458 | maxY = qMax(maxY, y); |
|
450 | 459 | tickXCount = x+1; |
|
451 | 460 | |
|
452 | 461 | domain.setRange(minX,maxX,minY,maxY,tickXCount,tickYCount); |
|
453 | 462 | } |
|
454 | 463 | |
|
455 | 464 | Chart* QBarSeriesPrivate::createGraphics(ChartPresenter* presenter) |
|
456 | 465 | { |
|
457 | 466 | Q_Q(QBarSeries); |
|
458 | 467 | |
|
459 | 468 | BarChartItem* bar = new BarChartItem(q,presenter); |
|
460 | 469 | if(presenter->animationOptions().testFlag(QChart::SeriesAnimations)) { |
|
461 | 470 | presenter->animator()->addAnimation(bar); |
|
462 | 471 | } |
|
463 | 472 | presenter->chartTheme()->decorate(q, presenter->dataSet()->seriesIndex(q)); |
|
464 | 473 | return bar; |
|
465 | 474 | |
|
466 | 475 | } |
|
467 | 476 | |
|
468 | 477 | QList<LegendMarker*> QBarSeriesPrivate::createLegendMarker(QLegend* legend) |
|
469 | 478 | { |
|
470 | 479 | Q_Q(QBarSeries); |
|
471 | 480 | QList<LegendMarker*> markers; |
|
472 | 481 | foreach(QBarSet* set, q->barSets()) { |
|
473 | 482 | BarLegendMarker* marker = new BarLegendMarker(q,set,legend); |
|
474 | 483 | markers << marker; |
|
475 | 484 | } |
|
476 | 485 | |
|
477 | 486 | return markers; |
|
478 | 487 | } |
|
479 | 488 | |
|
480 | 489 | #include "moc_qbarseries.cpp" |
|
481 | 490 | #include "moc_qbarseries_p.cpp" |
|
482 | 491 | |
|
483 | 492 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,74 +1,75 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 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 Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial 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 | #ifndef BARSERIES_H |
|
22 | 22 | #define BARSERIES_H |
|
23 | 23 | |
|
24 | 24 | #include <qabstractseries.h> |
|
25 | 25 | #include <QStringList> |
|
26 | 26 | |
|
27 | 27 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
28 | 28 | |
|
29 | 29 | typedef QStringList QBarCategories; |
|
30 | 30 | |
|
31 | 31 | class QBarSet; |
|
32 | 32 | class BarCategory; |
|
33 | 33 | class QBarSeriesPrivate; |
|
34 | 34 | |
|
35 | 35 | // Container for series |
|
36 | 36 | class QTCOMMERCIALCHART_EXPORT QBarSeries : public QAbstractSeries |
|
37 | 37 | { |
|
38 | 38 | Q_OBJECT |
|
39 | 39 | public: |
|
40 | 40 | explicit QBarSeries(QObject *parent = 0); |
|
41 | 41 | virtual ~QBarSeries(); |
|
42 | 42 | |
|
43 | 43 | QAbstractSeries::SeriesType type() const; |
|
44 | 44 | void setCategories(QBarCategories categories); |
|
45 | 45 | |
|
46 | 46 | bool append(QBarSet *set); |
|
47 | 47 | bool remove(QBarSet *set); |
|
48 | 48 | bool append(QList<QBarSet* > sets); |
|
49 | 49 | bool remove(QList<QBarSet* > sets); |
|
50 | 50 | int barsetCount() const; |
|
51 | 51 | int categoryCount() const; |
|
52 | 52 | QList<QBarSet*> barSets() const; |
|
53 | 53 | QBarCategories categories() const; |
|
54 | 54 | |
|
55 | 55 | void setLabelsVisible(bool visible = true); |
|
56 | bool isLabelsVisible() const; | |
|
56 | 57 | |
|
57 | 58 | protected: |
|
58 | 59 | explicit QBarSeries(QBarSeriesPrivate &d,QObject *parent = 0); |
|
59 | 60 | |
|
60 | 61 | Q_SIGNALS: |
|
61 | 62 | void clicked(QBarSet *barset, QString category); |
|
62 | 63 | void hovered(QBarSet* barset, bool status); |
|
63 | 64 | |
|
64 | 65 | protected: |
|
65 | 66 | Q_DECLARE_PRIVATE(QBarSeries) |
|
66 | 67 | friend class BarChartItem; |
|
67 | 68 | friend class PercentBarChartItem; |
|
68 | 69 | friend class StackedBarChartItem; |
|
69 | 70 | friend class GroupedBarChartItem; |
|
70 | 71 | }; |
|
71 | 72 | |
|
72 | 73 | QTCOMMERCIALCHART_END_NAMESPACE |
|
73 | 74 | |
|
74 | 75 | #endif // BARSERIES_H |
@@ -1,61 +1,64 | |||
|
1 | 1 | #ifndef QBARSERIES_P_H |
|
2 | 2 | #define QBARSERIES_P_H |
|
3 | 3 | |
|
4 | 4 | #include "qbarseries.h" |
|
5 | 5 | #include "qabstractseries_p.h" |
|
6 | 6 | #include <QStringList> |
|
7 | 7 | #include <QAbstractSeries> |
|
8 | 8 | |
|
9 | 9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
10 | 10 | |
|
11 | 11 | class QBarModelMapper; |
|
12 | 12 | |
|
13 | 13 | // Container for series |
|
14 | 14 | class QBarSeriesPrivate : public QAbstractSeriesPrivate |
|
15 | 15 | { |
|
16 | 16 | Q_OBJECT |
|
17 | 17 | public: |
|
18 | 18 | QBarSeriesPrivate(QBarSeries *parent); |
|
19 | 19 | void setCategories(QBarCategories categories); |
|
20 | 20 | void insertCategory(int index, const QString category); |
|
21 | 21 | void removeCategory(int index); |
|
22 | 22 | int categoryCount() const; |
|
23 | 23 | QBarCategories categories() const; |
|
24 | 24 | |
|
25 | 25 | void setBarMargin(qreal margin); |
|
26 | 26 | qreal barMargin(); |
|
27 | 27 | |
|
28 | 28 | void scaleDomain(Domain& domain); |
|
29 | 29 | Chart* createGraphics(ChartPresenter* presenter); |
|
30 | 30 | QList<LegendMarker*> createLegendMarker(QLegend* legend); |
|
31 | 31 | |
|
32 | 32 | QBarSet* barsetAt(int index); |
|
33 | 33 | QString categoryName(int category); |
|
34 | 34 | qreal min(); |
|
35 | 35 | qreal max(); |
|
36 | 36 | qreal valueAt(int set, int category); |
|
37 | 37 | qreal percentageAt(int set, int category); |
|
38 | 38 | qreal categorySum(int category); |
|
39 | 39 | qreal absoluteCategorySum(int category); |
|
40 | 40 | qreal maxCategorySum(); |
|
41 | 41 | |
|
42 | 42 | Q_SIGNALS: |
|
43 | 43 | void clicked(QBarSet *barset, QString category); |
|
44 | 44 | void updatedBars(); |
|
45 | 45 | void restructuredBars(); |
|
46 | 46 | void categoriesUpdated(); |
|
47 | void labelsVisibleChanged(bool visible); | |
|
47 | 48 | |
|
48 | 49 | private Q_SLOTS: |
|
49 | 50 | void barsetChanged(); |
|
50 | 51 | |
|
51 | 52 | protected: |
|
52 | 53 | QList<QBarSet *> m_barSets; |
|
53 | 54 | QBarCategories m_categories; |
|
54 | 55 | qreal m_barMargin; |
|
56 | bool m_labelsVisible; | |
|
57 | ||
|
55 | 58 | private: |
|
56 | 59 | Q_DECLARE_PUBLIC(QBarSeries) |
|
57 | 60 | }; |
|
58 | 61 | |
|
59 | 62 | QTCOMMERCIALCHART_END_NAMESPACE |
|
60 | 63 | |
|
61 | 64 | #endif // QBARSERIESPRIVATE_P_H |
@@ -1,363 +1,342 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 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 Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial 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 "qbarset.h" |
|
22 | 22 | #include "qbarset_p.h" |
|
23 | 23 | |
|
24 | 24 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
25 | 25 | |
|
26 | 26 | /*! |
|
27 | 27 | \class QBarSet |
|
28 | 28 | \brief part of QtCommercial chart API. |
|
29 | 29 | |
|
30 | 30 | QBarSet represents one set of bars. Set of bars contains one data value for each category. |
|
31 | 31 | First value of set is assumed to belong to first category, second to second category and so on. |
|
32 | 32 | If set has fewer values than there are categories, then the missing values are assumed to be |
|
33 | 33 | at the end of set. For missing values in middle of a set, numerical value of zero is used. |
|
34 | 34 | |
|
35 | 35 | \mainclass |
|
36 | 36 | |
|
37 | 37 | \sa QBarSeries, QGroupedBarSeries, QStackedBarSeries, QPercentBarSeries |
|
38 | 38 | */ |
|
39 | 39 | |
|
40 | 40 | /*! |
|
41 | 41 | \fn void QBarSet::clicked(QString category) |
|
42 | 42 | \brief signals that set has been clicked |
|
43 | 43 | Parameter \a category describes on which category was clicked |
|
44 | 44 | */ |
|
45 | 45 | |
|
46 | 46 | /*! |
|
47 | 47 | \fn void QBarSet::hovered(bool status) |
|
48 | 48 | \brief signals that mouse has hovered over the set. If \a status is true, then mouse was entered. If \a status is false, then mouse was left. |
|
49 | 49 | |
|
50 | 50 | The signal is emitted if mouse is hovered on top of set |
|
51 | 51 | Parameter \a status is true, if mouse entered on top of series, false if mouse left from top of series. |
|
52 | 52 | */ |
|
53 | 53 | |
|
54 | 54 | /*! |
|
55 | 55 | Constructs QBarSet with a name of \a name and with parent of \a parent |
|
56 | 56 | */ |
|
57 | 57 | QBarSet::QBarSet(const QString name, QObject *parent) |
|
58 | 58 | : QObject(parent) |
|
59 | 59 | ,d_ptr(new QBarSetPrivate(name,this)) |
|
60 | 60 | { |
|
61 | 61 | } |
|
62 | 62 | |
|
63 | 63 | /*! |
|
64 | 64 | Destroys the barset |
|
65 | 65 | */ |
|
66 | 66 | QBarSet::~QBarSet() |
|
67 | 67 | { |
|
68 | 68 | // NOTE: d_ptr destroyed by QObject |
|
69 | 69 | } |
|
70 | 70 | |
|
71 | 71 | /*! |
|
72 | 72 | Sets new \a name for set. |
|
73 | 73 | */ |
|
74 | 74 | void QBarSet::setName(const QString name) |
|
75 | 75 | { |
|
76 | 76 | d_ptr->m_name = name; |
|
77 | 77 | } |
|
78 | 78 | |
|
79 | 79 | /*! |
|
80 | 80 | Returns name of the set. |
|
81 | 81 | */ |
|
82 | 82 | QString QBarSet::name() const |
|
83 | 83 | { |
|
84 | 84 | return d_ptr->m_name; |
|
85 | 85 | } |
|
86 | 86 | |
|
87 | 87 | /*! |
|
88 | 88 | Appends a point to set. Parameter \a value x coordinate defines the |
|
89 | 89 | position in x-axis and y coordinate defines the height of bar. |
|
90 | 90 | Depending on presentation (QBarSeries, QGroupedBarSeries, QStackedBarSeries, QPercentBarSeries) |
|
91 | 91 | the x values are used or ignored. |
|
92 | 92 | */ |
|
93 | 93 | void QBarSet::append(const QPointF value) |
|
94 | 94 | { |
|
95 | 95 | d_ptr->m_values.append(value); |
|
96 | 96 | emit d_ptr->restructuredBars(); |
|
97 | 97 | } |
|
98 | 98 | |
|
99 | 99 | /*! |
|
100 | 100 | Appends a list of \a values to set. Works like append with single point. |
|
101 | 101 | \sa append() |
|
102 | 102 | */ |
|
103 | 103 | void QBarSet::append(const QList<QPointF> values) |
|
104 | 104 | { |
|
105 | 105 | for (int i=0; i<values.count(); i++) { |
|
106 | 106 | d_ptr->m_values.append(values.at(i)); |
|
107 | 107 | } |
|
108 | 108 | emit d_ptr->restructuredBars(); |
|
109 | 109 | } |
|
110 | 110 | |
|
111 | 111 | /*! |
|
112 | 112 | Appends new value \a value to the end of set. Internally the value is converted to QPointF, |
|
113 | 113 | with x coordinate being the index of appended value and y coordinate is the value. |
|
114 | 114 | */ |
|
115 | 115 | void QBarSet::append(const qreal value) |
|
116 | 116 | { |
|
117 | 117 | append(QPointF(d_ptr->m_values.count(), value)); |
|
118 | 118 | } |
|
119 | 119 | |
|
120 | 120 | /*! |
|
121 | 121 | Appends a list of reals to set. Works like append with single real value. The values in list |
|
122 | 122 | are converted to QPointF, where x coordinate is the index of point and y coordinate is the value. |
|
123 | 123 | \sa append() |
|
124 | 124 | */ |
|
125 | 125 | void QBarSet::append(const QList<qreal> values) |
|
126 | 126 | { |
|
127 | 127 | int index = d_ptr->m_values.count(); |
|
128 | 128 | for (int i=0; i<values.count(); i++) { |
|
129 | 129 | d_ptr->m_values.append(QPointF(index,values.at(i))); |
|
130 | 130 | index++; |
|
131 | 131 | } |
|
132 | 132 | emit d_ptr->restructuredBars(); |
|
133 | 133 | } |
|
134 | 134 | |
|
135 | 135 | /*! |
|
136 | 136 | Convinience operator. Same as append, with real \a value. |
|
137 | 137 | \sa append() |
|
138 | 138 | */ |
|
139 | 139 | QBarSet& QBarSet::operator << (const qreal &value) |
|
140 | 140 | { |
|
141 | 141 | append(value); |
|
142 | 142 | return *this; |
|
143 | 143 | } |
|
144 | 144 | |
|
145 | 145 | /*! |
|
146 | 146 | Convinience operator. Same as append, with QPointF \a value. |
|
147 | 147 | \sa append() |
|
148 | 148 | */ |
|
149 | 149 | QBarSet& QBarSet::operator << (const QPointF &value) |
|
150 | 150 | { |
|
151 | 151 | append(value); |
|
152 | 152 | return *this; |
|
153 | 153 | } |
|
154 | 154 | |
|
155 | 155 | /*! |
|
156 | 156 | Inserts new \a value on the \a index position. |
|
157 | 157 | The value that is currently at this postion is moved to postion index + 1 |
|
158 | 158 | \sa remove() |
|
159 | 159 | */ |
|
160 | 160 | void QBarSet::insert(const int index, const qreal value) |
|
161 | 161 | { |
|
162 | 162 | d_ptr->m_values.insert(index, QPointF(index, value)); |
|
163 | 163 | // emit d_ptr->updatedBars(); |
|
164 | 164 | } |
|
165 | 165 | |
|
166 | 166 | /*! |
|
167 | 167 | Removes the value specified by \a index |
|
168 | 168 | \sa insert() |
|
169 | 169 | */ |
|
170 | 170 | void QBarSet::remove(const int index) |
|
171 | 171 | { |
|
172 | 172 | d_ptr->m_values.removeAt(index); |
|
173 | 173 | // emit d_ptr->updatedBars(); |
|
174 | 174 | } |
|
175 | 175 | |
|
176 | 176 | /*! |
|
177 | 177 | Sets a new value \a value to set, indexed by \a index |
|
178 | 178 | */ |
|
179 | 179 | void QBarSet::replace(const int index, const qreal value) |
|
180 | 180 | { |
|
181 | 181 | d_ptr->m_values.replace(index,QPointF(index,value)); |
|
182 | 182 | emit d_ptr->updatedBars(); |
|
183 | 183 | } |
|
184 | 184 | |
|
185 | 185 | /*! |
|
186 | 186 | Returns value of set indexed by \a index. Note that all appended values are stored internally as QPointF. |
|
187 | 187 | The returned QPointF has x coordinate, which is index (if appended with qreal append) or the x value |
|
188 | 188 | of the QPointF (if appended with QPointF append). |
|
189 | 189 | If the index is out of bounds QPointF(0, 0.0) is returned. |
|
190 | 190 | */ |
|
191 | 191 | QPointF QBarSet::at(const int index) const |
|
192 | 192 | { |
|
193 | 193 | if (index < 0 || index >= d_ptr->m_values.count()) { |
|
194 | 194 | return QPointF(index, 0.0); |
|
195 | 195 | } |
|
196 | 196 | |
|
197 | 197 | return d_ptr->m_values.at(index); |
|
198 | 198 | } |
|
199 | 199 | |
|
200 | 200 | /*! |
|
201 | 201 | Returns value of set indexed by \a index. ote that all appended values are stored internally as QPointF. |
|
202 | 202 | The returned QPointF has x coordinate, which is index (if appended with qreal append) or the x value |
|
203 | 203 | of the QPointF (if appended with QPointF append). |
|
204 | 204 | */ |
|
205 | 205 | QPointF QBarSet::operator [](const int index) const |
|
206 | 206 | { |
|
207 | 207 | return d_ptr->m_values.at(index); |
|
208 | 208 | } |
|
209 | 209 | |
|
210 | 210 | /*! |
|
211 | 211 | Returns count of values in set. |
|
212 | 212 | */ |
|
213 | 213 | int QBarSet::count() const |
|
214 | 214 | { |
|
215 | 215 | return d_ptr->m_values.count(); |
|
216 | 216 | } |
|
217 | 217 | |
|
218 | 218 | /*! |
|
219 | 219 | Returns sum of all values in barset. The sum is sum of y coordinates in the QPointF representation. |
|
220 | 220 | */ |
|
221 | 221 | qreal QBarSet::sum() const |
|
222 | 222 | { |
|
223 | 223 | qreal total(0); |
|
224 | 224 | for (int i=0; i < d_ptr->m_values.count(); i++) { |
|
225 | 225 | //total += d_ptr->m_values.at(i); |
|
226 | 226 | total += d_ptr->m_values.at(i).y(); |
|
227 | 227 | } |
|
228 | 228 | return total; |
|
229 | 229 | } |
|
230 | 230 | |
|
231 | 231 | /*! |
|
232 | 232 | Sets pen for set. Bars of this set are drawn using \a pen |
|
233 | 233 | */ |
|
234 | 234 | void QBarSet::setPen(const QPen &pen) |
|
235 | 235 | { |
|
236 | 236 | if(d_ptr->m_pen!=pen){ |
|
237 | 237 | d_ptr->m_pen = pen; |
|
238 | 238 | emit d_ptr->updatedBars(); |
|
239 | 239 | } |
|
240 | 240 | } |
|
241 | 241 | |
|
242 | 242 | /*! |
|
243 | 243 | Returns pen of the set. |
|
244 | 244 | */ |
|
245 | 245 | QPen QBarSet::pen() const |
|
246 | 246 | { |
|
247 | 247 | return d_ptr->m_pen; |
|
248 | 248 | } |
|
249 | 249 | |
|
250 | 250 | /*! |
|
251 | 251 | Sets brush for the set. Bars of this set are drawn using \a brush |
|
252 | 252 | */ |
|
253 | 253 | void QBarSet::setBrush(const QBrush &brush) |
|
254 | 254 | { |
|
255 | 255 | if(d_ptr->m_brush!=brush){ |
|
256 | 256 | d_ptr->m_brush = brush; |
|
257 | 257 | emit d_ptr->updatedBars(); |
|
258 | 258 | } |
|
259 | 259 | } |
|
260 | 260 | |
|
261 | 261 | /*! |
|
262 | 262 | Returns brush of the set. |
|
263 | 263 | */ |
|
264 | 264 | QBrush QBarSet::brush() const |
|
265 | 265 | { |
|
266 | 266 | return d_ptr->m_brush; |
|
267 | 267 | } |
|
268 | 268 | |
|
269 | 269 | /*! |
|
270 | 270 | Sets \a pen of the values that are drawn on top of this barset |
|
271 | 271 | */ |
|
272 | 272 | void QBarSet::setLabelPen(const QPen &pen) |
|
273 | 273 | { |
|
274 | 274 | if(d_ptr->m_labelPen!=pen){ |
|
275 | 275 | d_ptr->m_labelPen = pen; |
|
276 | 276 | emit d_ptr->updatedBars(); |
|
277 | 277 | } |
|
278 | 278 | } |
|
279 | 279 | |
|
280 | 280 | /*! |
|
281 | 281 | Returns pen of the values that are drawn on top of this barset |
|
282 | 282 | */ |
|
283 | 283 | QPen QBarSet::labelPen() const |
|
284 | 284 | { |
|
285 | 285 | return d_ptr->m_labelPen; |
|
286 | 286 | } |
|
287 | 287 | |
|
288 | 288 | /*! |
|
289 | 289 | Sets \a brush of the values that are drawn on top of this barset |
|
290 | 290 | */ |
|
291 | 291 | void QBarSet::setLabelBrush(const QBrush &brush) |
|
292 | 292 | { |
|
293 | 293 | if(d_ptr->m_labelBrush!=brush){ |
|
294 | 294 | d_ptr->m_labelBrush = brush; |
|
295 | 295 | emit d_ptr->updatedBars(); |
|
296 | 296 | } |
|
297 | 297 | } |
|
298 | 298 | |
|
299 | 299 | /*! |
|
300 | 300 | Returns brush of the values that are drawn on top of this barset |
|
301 | 301 | */ |
|
302 | 302 | QBrush QBarSet::labelBrush() const |
|
303 | 303 | { |
|
304 | 304 | return d_ptr->m_labelBrush; |
|
305 | 305 | } |
|
306 | 306 | |
|
307 | 307 | /*! |
|
308 | 308 | Sets the \a font for values that are drawn on top of this barset |
|
309 | 309 | */ |
|
310 | 310 | void QBarSet::setLabelFont(const QFont &font) |
|
311 | 311 | { |
|
312 | 312 | if(d_ptr->m_labelFont!=font) { |
|
313 | 313 | d_ptr->m_labelFont = font; |
|
314 | 314 | emit d_ptr->updatedBars(); |
|
315 | 315 | } |
|
316 | 316 | |
|
317 | 317 | } |
|
318 | 318 | |
|
319 | 319 | /*! |
|
320 | 320 | Returns the pen for values that are drawn on top of this set |
|
321 | 321 | */ |
|
322 | 322 | QFont QBarSet::labelFont() const |
|
323 | 323 | { |
|
324 | 324 | return d_ptr->m_labelFont; |
|
325 | 325 | } |
|
326 | 326 | |
|
327 | /*! | |
|
328 | Sets visibility of bar labels. If \a visible is true, labels are drawn on top of barsets. | |
|
329 | */ | |
|
330 | ||
|
331 | void QBarSet::setLabelsVisible(bool visible) | |
|
332 | { | |
|
333 | if(d_ptr->m_labelsVisible!=visible) { | |
|
334 | d_ptr->m_labelsVisible = visible; | |
|
335 | emit d_ptr->labelsVisibleChanged(visible); | |
|
336 | } | |
|
337 | } | |
|
338 | ||
|
339 | /*! | |
|
340 | Returns the visibility of values | |
|
341 | */ | |
|
342 | bool QBarSet::labelsVisible() const | |
|
343 | { | |
|
344 | return d_ptr->m_labelsVisible; | |
|
345 | } | |
|
346 | ||
|
347 | 327 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
|
348 | 328 | |
|
349 | 329 | QBarSetPrivate::QBarSetPrivate(const QString name, QBarSet *parent) : QObject(parent), |
|
350 | 330 | q_ptr(parent), |
|
351 |
m_name(name) |
|
|
352 | m_labelsVisible(false) | |
|
331 | m_name(name) | |
|
353 | 332 | { |
|
354 | 333 | } |
|
355 | 334 | |
|
356 | 335 | QBarSetPrivate::~QBarSetPrivate() |
|
357 | 336 | { |
|
358 | 337 | } |
|
359 | 338 | |
|
360 | 339 | #include "moc_qbarset.cpp" |
|
361 | 340 | #include "moc_qbarset_p.cpp" |
|
362 | 341 | |
|
363 | 342 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,93 +1,90 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 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 Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial 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 | #ifndef QBARSET_H |
|
22 | 22 | #define QBARSET_H |
|
23 | 23 | |
|
24 | 24 | #include <qchartglobal.h> |
|
25 | 25 | #include <QPen> |
|
26 | 26 | #include <QBrush> |
|
27 | 27 | #include <QFont> |
|
28 | 28 | |
|
29 | 29 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
30 | 30 | class QBarSetPrivate; |
|
31 | 31 | |
|
32 | 32 | class QTCOMMERCIALCHART_EXPORT QBarSet : public QObject |
|
33 | 33 | { |
|
34 | 34 | Q_OBJECT |
|
35 | 35 | Q_PROPERTY(QString name READ name WRITE setName) |
|
36 | 36 | |
|
37 | 37 | public: |
|
38 | 38 | explicit QBarSet(const QString name, QObject *parent = 0); |
|
39 | 39 | virtual ~QBarSet(); |
|
40 | 40 | |
|
41 | 41 | void setName(const QString name); |
|
42 | 42 | QString name() const; |
|
43 | 43 | |
|
44 |
void append(const QPointF value); |
|
|
45 |
void append(const QList<QPointF> values); |
|
|
46 | void append(const qreal value); // TODO: change so, that count becomes x-value | |
|
47 |
void append(const QList<qreal> values); |
|
|
44 | void append(const QPointF value); | |
|
45 | void append(const QList<QPointF> values); | |
|
46 | void append(const qreal value); | |
|
47 | void append(const QList<qreal> values); | |
|
48 | 48 | |
|
49 | QBarSet& operator << (const qreal &value); // TODO: change implementations so, that count becomes x-value | |
|
50 |
QBarSet& operator << (const QPointF &value); |
|
|
49 | QBarSet& operator << (const qreal &value); | |
|
50 | QBarSet& operator << (const QPointF &value); | |
|
51 | 51 | |
|
52 | void insert(const int index, const qreal value); // TODO: internal reindexing (what happens, if there are points with real x values?) | |
|
53 | void remove(const int index); // TODO: internal reindexing (what happens, if there are points with real x values?) | |
|
52 | void insert(const int index, const qreal value); | |
|
53 | void remove(const int index); | |
|
54 | 54 | void replace(const int index, const qreal value); |
|
55 | 55 | QPointF at(const int index) const; |
|
56 | 56 | QPointF operator [] (const int index) const; |
|
57 | 57 | int count() const; |
|
58 | 58 | qreal sum() const; |
|
59 | 59 | |
|
60 | 60 | void setPen(const QPen &pen); |
|
61 | 61 | QPen pen() const; |
|
62 | 62 | |
|
63 | 63 | void setBrush(const QBrush &brush); |
|
64 | 64 | QBrush brush() const; |
|
65 | 65 | |
|
66 | 66 | void setLabelPen(const QPen &pen); |
|
67 | 67 | QPen labelPen() const; |
|
68 | 68 | |
|
69 | 69 | void setLabelBrush(const QBrush &brush); |
|
70 | 70 | QBrush labelBrush() const; |
|
71 | 71 | |
|
72 | 72 | void setLabelFont(const QFont &font); |
|
73 | 73 | QFont labelFont() const; |
|
74 | 74 | |
|
75 | void setLabelsVisible(bool visible = true); | |
|
76 | bool labelsVisible() const; | |
|
77 | ||
|
78 | 75 | Q_SIGNALS: |
|
79 | 76 | void clicked(QString category); |
|
80 | 77 | void hovered(bool status); |
|
81 | 78 | |
|
82 | 79 | private: |
|
83 | 80 | QScopedPointer<QBarSetPrivate> d_ptr; |
|
84 | 81 | Q_DISABLE_COPY(QBarSet) |
|
85 | 82 | friend class QBarSeries; |
|
86 | 83 | friend class BarLegendMarker; |
|
87 | 84 | friend class BarChartItem; |
|
88 | 85 | friend class QBarSeriesPrivate; |
|
89 | 86 | }; |
|
90 | 87 | |
|
91 | 88 | QTCOMMERCIALCHART_END_NAMESPACE |
|
92 | 89 | |
|
93 | 90 | #endif // QBARSET_H |
@@ -1,39 +1,40 | |||
|
1 | 1 | #ifndef QBARSET_P_H |
|
2 | 2 | #define QBARSET_P_H |
|
3 | 3 | |
|
4 | 4 | #include "qbarset.h" |
|
5 | 5 | #include <QMap> |
|
6 | #include <QPen> | |
|
7 | #include <QBrush> | |
|
8 | #include <QFont> | |
|
6 | 9 | |
|
7 | 10 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | 11 | |
|
9 | 12 | class QBarSetPrivate : public QObject |
|
10 | 13 | { |
|
11 | 14 | Q_OBJECT |
|
12 | 15 | |
|
13 | 16 | public: |
|
14 | 17 | QBarSetPrivate(const QString name, QBarSet *parent); |
|
15 | 18 | ~QBarSetPrivate(); |
|
16 | 19 | |
|
17 | 20 | Q_SIGNALS: |
|
18 | 21 | void clicked(QString category); |
|
19 | 22 | void restructuredBars(); |
|
20 | 23 | void updatedBars(); |
|
21 | void labelsVisibleChanged(bool visible); | |
|
22 | 24 | |
|
23 | 25 | public: |
|
24 | 26 | QBarSet * const q_ptr; |
|
25 | 27 | QString m_name; |
|
26 | 28 | QList<QPointF> m_values; |
|
27 | 29 | QPen m_pen; |
|
28 | 30 | QBrush m_brush; |
|
29 | 31 | QPen m_labelPen; |
|
30 | 32 | QBrush m_labelBrush; |
|
31 | 33 | QFont m_labelFont; |
|
32 | bool m_labelsVisible; | |
|
33 | 34 | |
|
34 | 35 | friend class QBarSet; |
|
35 | 36 | }; |
|
36 | 37 | |
|
37 | 38 | QTCOMMERCIALCHART_END_NAMESPACE |
|
38 | 39 | |
|
39 | 40 | #endif // QBARSETPRIVATE_P_H |
@@ -1,88 +1,87 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 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 Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial 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 "stackedbarchartitem_p.h" |
|
22 | 22 | #include "bar_p.h" |
|
23 | #include "barlabel_p.h" | |
|
24 | 23 | #include "qbarset_p.h" |
|
25 | 24 | #include "qbarseries_p.h" |
|
26 | 25 | #include "qbarset.h" |
|
27 | 26 | |
|
28 | 27 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
29 | 28 | |
|
30 | 29 | StackedBarChartItem::StackedBarChartItem(QBarSeries *series, ChartPresenter *presenter) : |
|
31 | 30 | BarChartItem(series, presenter) |
|
32 | 31 | { |
|
33 | 32 | } |
|
34 | 33 | |
|
35 | 34 | QVector<QRectF> StackedBarChartItem::calculateLayout() |
|
36 | 35 | { |
|
37 | 36 | QVector<QRectF> layout; |
|
38 | 37 | // Use temporary qreals for accurancy |
|
39 | 38 | qreal categoryCount = m_series->categoryCount(); |
|
40 | 39 | qreal setCount = m_series->barsetCount(); |
|
41 | 40 | |
|
42 | 41 | // Domain: |
|
43 | 42 | qreal width = geometry().width(); |
|
44 | 43 | qreal height = geometry().height(); |
|
45 | 44 | qreal rangeY = m_domainMaxY - m_domainMinY; |
|
46 | 45 | qreal rangeX = m_domainMaxX - m_domainMinX; |
|
47 | 46 | qreal scaleY = (height / rangeY); |
|
48 | 47 | qreal scaleX = (width / rangeX); |
|
49 | 48 | qreal categoryWidth = width / categoryCount; |
|
50 | 49 | qreal barWidth = categoryWidth / setCount - categoryWidth * m_series->d_func()->barMargin(); |
|
51 | 50 | |
|
52 | 51 | int itemIndex(0); |
|
53 | 52 | for (int category = 0; category < categoryCount; category++) { |
|
54 | 53 | qreal yPos = height + rangeY * m_domainMinY + geometry().topLeft().y(); |
|
55 | 54 | for (int set=0; set < m_series->barsetCount(); set++) { |
|
56 | 55 | QBarSet* barSet = m_series->d_func()->barsetAt(set); |
|
57 | 56 | |
|
58 | 57 | qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2; |
|
59 | 58 | |
|
60 | 59 | qreal barHeight = barSet->at(category).y() * scaleY; |
|
61 | 60 | Bar* bar = m_bars.at(itemIndex); |
|
62 | 61 | bar->setPen(barSet->pen()); |
|
63 | 62 | bar->setBrush(barSet->brush()); |
|
64 | 63 | QRectF rect(xPos, yPos-barHeight, barWidth, barHeight); |
|
65 | 64 | layout.append(rect); |
|
66 | 65 | |
|
67 |
|
|
|
66 | QGraphicsSimpleTextItem* label = m_labels.at(itemIndex); | |
|
68 | 67 | |
|
69 | 68 | if (!qFuzzyIsNull(barSet->at(category).y())) { |
|
70 | 69 | label->setText(QString::number(barSet->at(category).y())); |
|
71 | 70 | } else { |
|
72 | 71 | label->setText(QString("")); |
|
73 | 72 | } |
|
74 | 73 | |
|
75 | 74 | label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2) |
|
76 | 75 | ,yPos - barHeight/2 - label->boundingRect().height()/2); |
|
77 | 76 | label->setFont(barSet->labelFont()); |
|
78 | 77 | itemIndex++; |
|
79 | 78 | yPos -= barHeight; |
|
80 | 79 | } |
|
81 | 80 | } |
|
82 | 81 | |
|
83 | 82 | return layout; |
|
84 | 83 | } |
|
85 | 84 | |
|
86 | 85 | #include "moc_stackedbarchartitem_p.cpp" |
|
87 | 86 | |
|
88 | 87 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,677 +1,673 | |||
|
1 | 1 | /**************************************************************************** |
|
2 | 2 | ** |
|
3 | 3 | ** Copyright (C) 2012 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 Commercial Charts Add-on. |
|
8 | 8 | ** |
|
9 | 9 | ** $QT_BEGIN_LICENSE$ |
|
10 | 10 | ** Licensees holding valid Qt Commercial licenses may use this file in |
|
11 | 11 | ** accordance with the Qt Commercial 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 <qbarseries.h> |
|
23 | 23 | #include <qbarset.h> |
|
24 | 24 | #include <qchartview.h> |
|
25 | 25 | #include <qchart.h> |
|
26 | 26 | |
|
27 | 27 | QTCOMMERCIALCHART_USE_NAMESPACE |
|
28 | 28 | |
|
29 | 29 | Q_DECLARE_METATYPE(QBarSet*) |
|
30 | 30 | |
|
31 | 31 | class tst_QBarSeries : 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 qbarseries_data(); |
|
43 | 43 | void qbarseries(); |
|
44 | 44 | void type_data(); |
|
45 | 45 | void type(); |
|
46 | 46 | void setCategories_data(); |
|
47 | 47 | void setCategories(); |
|
48 | 48 | void append_data(); |
|
49 | 49 | void append(); |
|
50 | 50 | void remove_data(); |
|
51 | 51 | void remove(); |
|
52 | 52 | void appendList_data(); |
|
53 | 53 | void appendList(); |
|
54 | 54 | void removeList_data(); |
|
55 | 55 | void removeList(); |
|
56 | 56 | void barsetCount_data(); |
|
57 | 57 | void barsetCount(); |
|
58 | 58 | void categoryCount_data(); |
|
59 | 59 | void categoryCount(); |
|
60 | 60 | void barSets_data(); |
|
61 | 61 | void barSets(); |
|
62 | 62 | void categories_data(); |
|
63 | 63 | void categories(); |
|
64 | 64 | void setLabelsVisible_data(); |
|
65 | 65 | void setLabelsVisible(); |
|
66 | 66 | void mouseclicked_data(); |
|
67 | 67 | void mouseclicked(); |
|
68 | 68 | void mousehovered_data(); |
|
69 | 69 | void mousehovered(); |
|
70 | 70 | |
|
71 | 71 | private: |
|
72 | 72 | QBarSeries* m_barseries; |
|
73 | 73 | QBarSeries* m_barseries_with_sets; |
|
74 | 74 | |
|
75 | 75 | QList<QBarSet*> m_testSets; |
|
76 | 76 | |
|
77 | 77 | QBarCategories m_categories; |
|
78 | 78 | }; |
|
79 | 79 | |
|
80 | 80 | void tst_QBarSeries::initTestCase() |
|
81 | 81 | { |
|
82 | 82 | qRegisterMetaType<QBarSet*>("QBarSet*"); |
|
83 | 83 | } |
|
84 | 84 | |
|
85 | 85 | void tst_QBarSeries::cleanupTestCase() |
|
86 | 86 | { |
|
87 | 87 | } |
|
88 | 88 | |
|
89 | 89 | void tst_QBarSeries::init() |
|
90 | 90 | { |
|
91 | 91 | m_categories << "category0" << "category1" << "category2"; |
|
92 | 92 | m_barseries = new QBarSeries(); |
|
93 | 93 | m_barseries->setCategories(m_categories); |
|
94 | 94 | m_barseries_with_sets = new QBarSeries(); |
|
95 | 95 | m_barseries_with_sets->setCategories(m_categories); |
|
96 | 96 | |
|
97 | 97 | for (int i=0; i<5; i++) { |
|
98 | 98 | m_testSets.append(new QBarSet("testset")); |
|
99 | 99 | m_barseries_with_sets->append(m_testSets.at(i)); |
|
100 | 100 | } |
|
101 | 101 | } |
|
102 | 102 | |
|
103 | 103 | void tst_QBarSeries::cleanup() |
|
104 | 104 | { |
|
105 | 105 | foreach(QBarSet* s, m_testSets) { |
|
106 | 106 | m_barseries_with_sets->remove(s); |
|
107 | 107 | delete s; |
|
108 | 108 | } |
|
109 | 109 | m_testSets.clear(); |
|
110 | 110 | |
|
111 | 111 | delete m_barseries; |
|
112 | 112 | m_barseries = 0; |
|
113 | 113 | delete m_barseries_with_sets; |
|
114 | 114 | m_barseries_with_sets = 0; |
|
115 | 115 | m_categories.clear(); |
|
116 | 116 | } |
|
117 | 117 | |
|
118 | 118 | void tst_QBarSeries::qbarseries_data() |
|
119 | 119 | { |
|
120 | 120 | QTest::addColumn<QBarCategories> ("categories"); |
|
121 | 121 | QBarCategories c; |
|
122 | 122 | c << "category0" << "category1" << "category2"; |
|
123 | 123 | QTest::newRow("categories") << c; |
|
124 | 124 | } |
|
125 | 125 | |
|
126 | 126 | void tst_QBarSeries::qbarseries() |
|
127 | 127 | { |
|
128 | 128 | QFETCH(QBarCategories, categories); |
|
129 | 129 | QBarSeries *barseries = new QBarSeries(); |
|
130 | 130 | QVERIFY(barseries != 0); |
|
131 | 131 | barseries->setCategories(categories); |
|
132 | 132 | QBarCategories verifyCategories = barseries->categories(); |
|
133 | 133 | |
|
134 | 134 | QVERIFY(verifyCategories.count() == categories.count()); |
|
135 | 135 | for (int i=0; i<categories.count(); i++) { |
|
136 | 136 | QVERIFY(verifyCategories.at(i).compare(categories.at(i)) == 0); |
|
137 | 137 | } |
|
138 | 138 | } |
|
139 | 139 | |
|
140 | 140 | void tst_QBarSeries::type_data() |
|
141 | 141 | { |
|
142 | 142 | |
|
143 | 143 | } |
|
144 | 144 | |
|
145 | 145 | void tst_QBarSeries::type() |
|
146 | 146 | { |
|
147 | 147 | QVERIFY(m_barseries->type() == QAbstractSeries::SeriesTypeBar); |
|
148 | 148 | } |
|
149 | 149 | |
|
150 | 150 | void tst_QBarSeries::setCategories_data() |
|
151 | 151 | { |
|
152 | 152 | QTest::addColumn<QBarCategories> ("categories"); |
|
153 | 153 | QBarCategories categories; |
|
154 | 154 | categories << "c1" << "c2" << "c3" << "c4" << "c5" << "c6"; |
|
155 | 155 | QTest::newRow("cat") << categories; |
|
156 | 156 | } |
|
157 | 157 | |
|
158 | 158 | void tst_QBarSeries::setCategories() |
|
159 | 159 | { |
|
160 | 160 | QVERIFY(m_barseries->categories().count() == m_categories.count()); |
|
161 | 161 | |
|
162 | 162 | QFETCH(QBarCategories, categories); |
|
163 | 163 | m_barseries->setCategories(categories); |
|
164 | 164 | |
|
165 | 165 | QVERIFY(m_barseries->categories().count() == categories.count()); |
|
166 | 166 | for (int i=0; i<categories.count(); i++) { |
|
167 | 167 | QVERIFY(m_barseries->categories().at(i).compare(categories.at(i)) == 0); |
|
168 | 168 | } |
|
169 | 169 | } |
|
170 | 170 | |
|
171 | 171 | void tst_QBarSeries::append_data() |
|
172 | 172 | { |
|
173 | 173 | } |
|
174 | 174 | |
|
175 | 175 | void tst_QBarSeries::append() |
|
176 | 176 | { |
|
177 | 177 | QVERIFY(m_barseries->barsetCount() == 0); |
|
178 | 178 | |
|
179 | 179 | bool ret = false; |
|
180 | 180 | |
|
181 | 181 | // Try adding barset |
|
182 | 182 | QBarSet *barset = new QBarSet("testset"); |
|
183 | 183 | ret = m_barseries->append(barset); |
|
184 | 184 | |
|
185 | 185 | QVERIFY(ret == true); |
|
186 | 186 | QVERIFY(m_barseries->barsetCount() == 1); |
|
187 | 187 | |
|
188 | 188 | // Try adding another set |
|
189 | 189 | QBarSet *barset2 = new QBarSet("testset2"); |
|
190 | 190 | ret = m_barseries->append(barset2); |
|
191 | 191 | |
|
192 | 192 | QVERIFY(ret == true); |
|
193 | 193 | QVERIFY(m_barseries->barsetCount() == 2); |
|
194 | 194 | |
|
195 | 195 | // Try adding same set again |
|
196 | 196 | ret = m_barseries->append(barset2); |
|
197 | 197 | QVERIFY(ret == false); |
|
198 | 198 | QVERIFY(m_barseries->barsetCount() == 2); |
|
199 | 199 | |
|
200 | 200 | // Try adding null set |
|
201 | 201 | ret = m_barseries->append(0); |
|
202 | 202 | QVERIFY(ret == false); |
|
203 | 203 | QVERIFY(m_barseries->barsetCount() == 2); |
|
204 | 204 | |
|
205 | 205 | } |
|
206 | 206 | |
|
207 | 207 | void tst_QBarSeries::remove_data() |
|
208 | 208 | { |
|
209 | 209 | } |
|
210 | 210 | |
|
211 | 211 | void tst_QBarSeries::remove() |
|
212 | 212 | { |
|
213 | 213 | int count = m_testSets.count(); |
|
214 | 214 | QVERIFY(m_barseries_with_sets->barsetCount() == count); |
|
215 | 215 | |
|
216 | 216 | // Try to remove null pointer (should not remove, should not crash) |
|
217 | 217 | bool ret = false; |
|
218 | 218 | ret = m_barseries_with_sets->remove(0); |
|
219 | 219 | QVERIFY(ret == false); |
|
220 | 220 | QVERIFY(m_barseries_with_sets->barsetCount() == count); |
|
221 | 221 | |
|
222 | 222 | // Try to remove invalid pointer (should not remove, should not crash) |
|
223 | 223 | ret = m_barseries_with_sets->remove((QBarSet*) (m_testSets.at(0) + 1) ); |
|
224 | 224 | QVERIFY(ret == false); |
|
225 | 225 | QVERIFY(m_barseries_with_sets->barsetCount() == count); |
|
226 | 226 | |
|
227 | 227 | // remove some sets |
|
228 | 228 | ret = m_barseries_with_sets->remove(m_testSets.at(2)); |
|
229 | 229 | QVERIFY(ret == true); |
|
230 | 230 | ret = m_barseries_with_sets->remove(m_testSets.at(3)); |
|
231 | 231 | QVERIFY(ret == true); |
|
232 | 232 | ret = m_barseries_with_sets->remove(m_testSets.at(4)); |
|
233 | 233 | QVERIFY(ret == true); |
|
234 | 234 | |
|
235 | 235 | QVERIFY(m_barseries_with_sets->barsetCount() == 2); |
|
236 | 236 | |
|
237 | 237 | QList<QBarSet*> verifysets = m_barseries_with_sets->barSets(); |
|
238 | 238 | |
|
239 | 239 | QVERIFY(verifysets.at(0) == m_testSets.at(0)); |
|
240 | 240 | QVERIFY(verifysets.at(1) == m_testSets.at(1)); |
|
241 | 241 | |
|
242 | 242 | // Try removing all sets again (should be ok, even if some sets have already been removed) |
|
243 | 243 | ret = false; |
|
244 | 244 | for (int i=0; i<count; i++) { |
|
245 | 245 | ret |= m_barseries_with_sets->remove(m_testSets.at(i)); |
|
246 | 246 | } |
|
247 | 247 | |
|
248 | 248 | QVERIFY(ret == true); |
|
249 | 249 | QVERIFY(m_barseries_with_sets->barsetCount() == 0); |
|
250 | 250 | } |
|
251 | 251 | |
|
252 | 252 | void tst_QBarSeries::appendList_data() |
|
253 | 253 | { |
|
254 | 254 | |
|
255 | 255 | } |
|
256 | 256 | |
|
257 | 257 | void tst_QBarSeries::appendList() |
|
258 | 258 | { |
|
259 | 259 | int count = 5; |
|
260 | 260 | QVERIFY(m_barseries->barsetCount() == 0); |
|
261 | 261 | |
|
262 | 262 | QList<QBarSet*> sets; |
|
263 | 263 | for (int i=0; i<count; i++) { |
|
264 | 264 | sets.append(new QBarSet("testset")); |
|
265 | 265 | } |
|
266 | 266 | |
|
267 | 267 | // Append new sets (should succeed, count should match the count of sets) |
|
268 | 268 | bool ret = false; |
|
269 | 269 | ret = m_barseries->append(sets); |
|
270 | 270 | QVERIFY(ret == true); |
|
271 | 271 | QVERIFY(m_barseries->barsetCount() == count); |
|
272 | 272 | |
|
273 | 273 | // Append same sets again (should fail, count should remain same) |
|
274 | 274 | ret = m_barseries->append(sets); |
|
275 | 275 | QVERIFY(ret == false); |
|
276 | 276 | QVERIFY(m_barseries->barsetCount() == count); |
|
277 | 277 | |
|
278 | 278 | // Try append empty list (should succeed, but count should remain same) |
|
279 | 279 | QList<QBarSet*> invalidList; |
|
280 | 280 | ret = m_barseries->append(invalidList); |
|
281 | 281 | QVERIFY(ret == true); |
|
282 | 282 | QVERIFY(m_barseries->barsetCount() == count); |
|
283 | 283 | |
|
284 | 284 | // Try append list with one new and one existing set (should fail, count remains same) |
|
285 | 285 | invalidList.append(new QBarSet("ok set")); |
|
286 | 286 | invalidList.append(sets.at(0)); |
|
287 | 287 | ret = m_barseries->append(invalidList); |
|
288 | 288 | QVERIFY(ret == false); |
|
289 | 289 | QVERIFY(m_barseries->barsetCount() == count); |
|
290 | 290 | |
|
291 | 291 | // Try append list with null pointers (should fail, count remains same) |
|
292 | 292 | QList<QBarSet*> invalidList2; |
|
293 | 293 | invalidList2.append(0); |
|
294 | 294 | invalidList2.append(0); |
|
295 | 295 | invalidList2.append(0); |
|
296 | 296 | ret = m_barseries->append(invalidList2); |
|
297 | 297 | QVERIFY(ret == false); |
|
298 | 298 | QVERIFY(m_barseries->barsetCount() == count); |
|
299 | 299 | } |
|
300 | 300 | |
|
301 | 301 | void tst_QBarSeries::removeList_data() |
|
302 | 302 | { |
|
303 | 303 | |
|
304 | 304 | } |
|
305 | 305 | |
|
306 | 306 | void tst_QBarSeries::removeList() |
|
307 | 307 | { |
|
308 | 308 | int count = m_testSets.count(); |
|
309 | 309 | QVERIFY(m_barseries_with_sets->barsetCount() == count); |
|
310 | 310 | |
|
311 | 311 | // Try removing empty list of sets (should return false, since no barsets were removed) |
|
312 | 312 | bool ret = false; |
|
313 | 313 | QList<QBarSet*> invalidList; |
|
314 | 314 | ret = m_barseries_with_sets->remove(invalidList); |
|
315 | 315 | QVERIFY(ret == false); |
|
316 | 316 | QVERIFY(m_barseries_with_sets->barsetCount() == count); |
|
317 | 317 | |
|
318 | 318 | // Add some null pointers to list |
|
319 | 319 | invalidList.append(0); |
|
320 | 320 | invalidList.append(0); |
|
321 | 321 | invalidList.append(0); |
|
322 | 322 | |
|
323 | 323 | // Try removing null pointers from list (should return false, should not crash, should not remove anything) |
|
324 | 324 | ret = m_barseries_with_sets->remove(invalidList); |
|
325 | 325 | QVERIFY(ret == false); |
|
326 | 326 | QVERIFY(m_barseries_with_sets->barsetCount() == count); |
|
327 | 327 | |
|
328 | 328 | // remove all sets (should return true, since sets were removed) |
|
329 | 329 | ret = m_barseries_with_sets->remove(m_testSets); |
|
330 | 330 | QVERIFY(ret == true); |
|
331 | 331 | QVERIFY(m_barseries_with_sets->barsetCount() == 0); |
|
332 | 332 | |
|
333 | 333 | // Try removing invalid list again (should return false, since no barsets were removed) |
|
334 | 334 | ret = m_barseries_with_sets->remove(invalidList); |
|
335 | 335 | QVERIFY(ret == false); |
|
336 | 336 | QVERIFY(m_barseries_with_sets->barsetCount() == 0); |
|
337 | 337 | |
|
338 | 338 | // remove all sets again (should return false, since barsets were already removed) |
|
339 | 339 | ret = m_barseries_with_sets->remove(m_testSets); |
|
340 | 340 | QVERIFY(ret == false); |
|
341 | 341 | QVERIFY(m_barseries_with_sets->barsetCount() == 0); |
|
342 | 342 | } |
|
343 | 343 | |
|
344 | 344 | void tst_QBarSeries::barsetCount_data() |
|
345 | 345 | { |
|
346 | 346 | |
|
347 | 347 | } |
|
348 | 348 | |
|
349 | 349 | void tst_QBarSeries::barsetCount() |
|
350 | 350 | { |
|
351 | 351 | QVERIFY(m_barseries->barsetCount() == 0); |
|
352 | 352 | QVERIFY(m_barseries_with_sets->barsetCount() == m_testSets.count()); |
|
353 | 353 | } |
|
354 | 354 | |
|
355 | 355 | void tst_QBarSeries::categoryCount_data() |
|
356 | 356 | { |
|
357 | 357 | |
|
358 | 358 | } |
|
359 | 359 | |
|
360 | 360 | void tst_QBarSeries::categoryCount() |
|
361 | 361 | { |
|
362 | 362 | QVERIFY(m_barseries->categoryCount() == m_categories.count()); |
|
363 | 363 | QVERIFY(m_barseries_with_sets->categoryCount() == m_categories.count()); |
|
364 | 364 | } |
|
365 | 365 | |
|
366 | 366 | void tst_QBarSeries::barSets_data() |
|
367 | 367 | { |
|
368 | 368 | |
|
369 | 369 | } |
|
370 | 370 | |
|
371 | 371 | void tst_QBarSeries::barSets() |
|
372 | 372 | { |
|
373 | 373 | QVERIFY(m_barseries->barSets().count() == 0); |
|
374 | 374 | |
|
375 | 375 | QList<QBarSet*> sets = m_barseries_with_sets->barSets(); |
|
376 | 376 | QVERIFY(sets.count() == m_testSets.count()); |
|
377 | 377 | |
|
378 | 378 | for (int i=0; i<m_testSets.count(); i++) { |
|
379 | 379 | QVERIFY(sets.at(i) == m_testSets.at(i)); |
|
380 | 380 | } |
|
381 | 381 | } |
|
382 | 382 | |
|
383 | 383 | void tst_QBarSeries::categories_data() |
|
384 | 384 | { |
|
385 | 385 | |
|
386 | 386 | } |
|
387 | 387 | |
|
388 | 388 | void tst_QBarSeries::categories() |
|
389 | 389 | { |
|
390 | 390 | QBarCategories categories = m_barseries->categories(); |
|
391 | 391 | |
|
392 | 392 | QVERIFY(categories.count() == m_categories.count()); |
|
393 | 393 | for (int i=0; i<m_categories.count(); i++) { |
|
394 | 394 | QVERIFY(categories.at(i).compare(m_categories.at(i)) == 0); |
|
395 | 395 | } |
|
396 | 396 | } |
|
397 | 397 | |
|
398 | 398 | void tst_QBarSeries::setLabelsVisible_data() |
|
399 | 399 | { |
|
400 | 400 | |
|
401 | 401 | } |
|
402 | 402 | |
|
403 | 403 | void tst_QBarSeries::setLabelsVisible() |
|
404 | 404 | { |
|
405 | 405 | // labels should be invisible by default |
|
406 | foreach (QBarSet* s, m_testSets) { | |
|
407 |
|
|
|
408 | } | |
|
406 | QVERIFY(m_barseries->isLabelsVisible() == false); | |
|
407 | QVERIFY(m_barseries_with_sets->isLabelsVisible() == false); | |
|
409 | 408 | |
|
410 | 409 | // turn labels to visible |
|
411 | 410 | m_barseries_with_sets->setLabelsVisible(true); |
|
412 | foreach (QBarSet* s, m_testSets) { | |
|
413 |
|
|
|
414 | } | |
|
411 | // TODO: test the signal | |
|
412 | QVERIFY(m_barseries_with_sets->isLabelsVisible() == true); | |
|
415 | 413 | |
|
416 | 414 | // turn labels to invisible |
|
417 | 415 | m_barseries_with_sets->setLabelsVisible(false); |
|
418 | foreach (QBarSet* s, m_testSets) { | |
|
419 |
|
|
|
420 | } | |
|
416 | // TODO: test the signal | |
|
417 | QVERIFY(m_barseries_with_sets->isLabelsVisible() == false); | |
|
421 | 418 | |
|
422 | 419 | // without parameter, should turn labels to visible |
|
423 | 420 | m_barseries_with_sets->setLabelsVisible(); |
|
424 | foreach (QBarSet* s, m_testSets) { | |
|
425 |
|
|
|
426 | } | |
|
421 | // TODO: test the signal | |
|
422 | QVERIFY(m_barseries_with_sets->isLabelsVisible() == true); | |
|
427 | 423 | } |
|
428 | 424 | |
|
429 | 425 | void tst_QBarSeries::mouseclicked_data() |
|
430 | 426 | { |
|
431 | 427 | |
|
432 | 428 | } |
|
433 | 429 | |
|
434 | 430 | void tst_QBarSeries::mouseclicked() |
|
435 | 431 | { |
|
436 | 432 | QBarSeries* series = new QBarSeries(); |
|
437 | 433 | QBarCategories categories; |
|
438 | 434 | categories << "test1" << "test2" << "test3"; |
|
439 | 435 | series->setCategories(categories); |
|
440 | 436 | |
|
441 | 437 | QBarSet* set1 = new QBarSet(QString("set 1")); |
|
442 | 438 | *set1 << QPointF(0,10) << QPointF(1,10) << QPointF(2,10); |
|
443 | 439 | series->append(set1); |
|
444 | 440 | |
|
445 | 441 | QBarSet* set2 = new QBarSet(QString("set 2")); |
|
446 | 442 | *set2 << QPointF(0.3,10) << QPointF(1.3,10) << QPointF(2.3,10); |
|
447 | 443 | series->append(set2); |
|
448 | 444 | |
|
449 | 445 | QSignalSpy setSpy1(set1, SIGNAL(clicked(QString))); |
|
450 | 446 | QSignalSpy setSpy2(set2, SIGNAL(clicked(QString))); |
|
451 | 447 | QSignalSpy seriesSpy(series,SIGNAL(clicked(QBarSet*,QString))); |
|
452 | 448 | |
|
453 | 449 | QChartView view(new QChart()); |
|
454 | 450 | view.resize(400,300); |
|
455 | 451 | view.chart()->addSeries(series); |
|
456 | 452 | view.show(); |
|
457 | 453 | QTest::qWaitForWindowShown(&view); |
|
458 | 454 | |
|
459 | 455 | //==================================================================================== |
|
460 | 456 | // barset 1, category test1 |
|
461 | 457 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(105,180)); |
|
462 | 458 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
463 | 459 | |
|
464 | 460 | QCOMPARE(setSpy1.count(), 1); |
|
465 | 461 | QCOMPARE(setSpy2.count(), 0); |
|
466 | 462 | QCOMPARE(seriesSpy.count(), 1); |
|
467 | 463 | QList<QVariant> setSpyArg = setSpy1.takeFirst(); |
|
468 | 464 | QVERIFY(setSpyArg.at(0).type() == QVariant::String); |
|
469 | 465 | QVERIFY(setSpyArg.at(0).toString().compare(QString("test1")) == 0); |
|
470 | 466 | |
|
471 | 467 | QList<QVariant> seriesSpyArg = seriesSpy.takeFirst(); |
|
472 | 468 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1); |
|
473 | 469 | |
|
474 | 470 | //==================================================================================== |
|
475 | 471 | // barset 1, category test2 |
|
476 | 472 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(200,180)); |
|
477 | 473 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
478 | 474 | |
|
479 | 475 | QCOMPARE(setSpy1.count(), 1); |
|
480 | 476 | QCOMPARE(setSpy2.count(), 0); |
|
481 | 477 | QCOMPARE(seriesSpy.count(), 1); |
|
482 | 478 | setSpyArg = setSpy1.takeFirst(); |
|
483 | 479 | QVERIFY(setSpyArg.at(0).type() == QVariant::String); |
|
484 | 480 | QVERIFY(setSpyArg.at(0).toString().compare(QString("test2")) == 0); |
|
485 | 481 | |
|
486 | 482 | seriesSpyArg = seriesSpy.takeFirst(); |
|
487 | 483 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1); |
|
488 | 484 | |
|
489 | 485 | //==================================================================================== |
|
490 | 486 | // barset 1, category test3 |
|
491 | 487 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(295,180)); |
|
492 | 488 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
493 | 489 | |
|
494 | 490 | QCOMPARE(setSpy1.count(), 1); |
|
495 | 491 | QCOMPARE(setSpy2.count(), 0); |
|
496 | 492 | QCOMPARE(seriesSpy.count(), 1); |
|
497 | 493 | setSpyArg = setSpy1.takeFirst(); |
|
498 | 494 | QVERIFY(setSpyArg.at(0).type() == QVariant::String); |
|
499 | 495 | QVERIFY(setSpyArg.at(0).toString().compare(QString("test3")) == 0); |
|
500 | 496 | |
|
501 | 497 | seriesSpyArg = seriesSpy.takeFirst(); |
|
502 | 498 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1); |
|
503 | 499 | |
|
504 | 500 | //==================================================================================== |
|
505 | 501 | // barset 2, category test1 |
|
506 | 502 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(145,180)); |
|
507 | 503 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
508 | 504 | |
|
509 | 505 | QCOMPARE(setSpy1.count(), 0); |
|
510 | 506 | QCOMPARE(setSpy2.count(), 1); |
|
511 | 507 | QCOMPARE(seriesSpy.count(), 1); |
|
512 | 508 | setSpyArg = setSpy2.takeFirst(); |
|
513 | 509 | QVERIFY(setSpyArg.at(0).type() == QVariant::String); |
|
514 | 510 | QVERIFY(setSpyArg.at(0).toString().compare(QString("test1")) == 0); |
|
515 | 511 | |
|
516 | 512 | seriesSpyArg = seriesSpy.takeFirst(); |
|
517 | 513 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2); |
|
518 | 514 | |
|
519 | 515 | //==================================================================================== |
|
520 | 516 | // barset 2, category test2 |
|
521 | 517 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(240,180)); |
|
522 | 518 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
523 | 519 | |
|
524 | 520 | QCOMPARE(setSpy1.count(), 0); |
|
525 | 521 | QCOMPARE(setSpy2.count(), 1); |
|
526 | 522 | QCOMPARE(seriesSpy.count(), 1); |
|
527 | 523 | setSpyArg = setSpy2.takeFirst(); |
|
528 | 524 | QVERIFY(setSpyArg.at(0).type() == QVariant::String); |
|
529 | 525 | QVERIFY(setSpyArg.at(0).toString().compare(QString("test2")) == 0); |
|
530 | 526 | |
|
531 | 527 | seriesSpyArg = seriesSpy.takeFirst(); |
|
532 | 528 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2); |
|
533 | 529 | |
|
534 | 530 | //==================================================================================== |
|
535 | 531 | // barset 2, category test3 |
|
536 | 532 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(335,180)); |
|
537 | 533 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
538 | 534 | |
|
539 | 535 | QCOMPARE(setSpy1.count(), 0); |
|
540 | 536 | QCOMPARE(setSpy2.count(), 1); |
|
541 | 537 | QCOMPARE(seriesSpy.count(), 1); |
|
542 | 538 | setSpyArg = setSpy2.takeFirst(); |
|
543 | 539 | QVERIFY(setSpyArg.at(0).type() == QVariant::String); |
|
544 | 540 | QVERIFY(setSpyArg.at(0).toString().compare(QString("test3")) == 0); |
|
545 | 541 | |
|
546 | 542 | seriesSpyArg = seriesSpy.takeFirst(); |
|
547 | 543 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2); |
|
548 | 544 | |
|
549 | 545 | //==================================================================================== |
|
550 | 546 | // no event cases |
|
551 | 547 | QTest::mouseClick(view.viewport(), Qt::LeftButton, 0, QPoint(1,1)); // Outside of both sets |
|
552 | 548 | QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(1,1)); // Right mouse button outside and inside sets |
|
553 | 549 | QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(105,180)); // barset 1, category test1 |
|
554 | 550 | QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(200,180)); // barset 1, category test2 |
|
555 | 551 | QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(295,180)); // barset 1, category test3 |
|
556 | 552 | QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(145,180)); // barset 2, category test1 |
|
557 | 553 | QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(240,180)); // barset 2, category test2 |
|
558 | 554 | QTest::mouseClick(view.viewport(), Qt::RightButton, 0, QPoint(335,180)); // barset 2, category test3 |
|
559 | 555 | |
|
560 | 556 | QCoreApplication::processEvents(QEventLoop::AllEvents, 1000); |
|
561 | 557 | QCOMPARE(setSpy1.count(), 0); |
|
562 | 558 | QCOMPARE(setSpy2.count(), 0); |
|
563 | 559 | QCOMPARE(seriesSpy.count(), 0); |
|
564 | 560 | } |
|
565 | 561 | |
|
566 | 562 | void tst_QBarSeries::mousehovered_data() |
|
567 | 563 | { |
|
568 | 564 | |
|
569 | 565 | } |
|
570 | 566 | |
|
571 | 567 | void tst_QBarSeries::mousehovered() |
|
572 | 568 | { |
|
573 | 569 | QBarSeries* series = new QBarSeries(); |
|
574 | 570 | QBarCategories categories; |
|
575 | 571 | categories << "test1" << "test2" << "test3"; |
|
576 | 572 | series->setCategories(categories); |
|
577 | 573 | |
|
578 | 574 | QBarSet* set1 = new QBarSet(QString("set 1")); |
|
579 | 575 | *set1 << QPointF(0.1,10) << QPointF(1.1,10) << QPointF(2.1,10); |
|
580 | 576 | series->append(set1); |
|
581 | 577 | |
|
582 | 578 | QBarSet* set2 = new QBarSet(QString("set 2")); |
|
583 | 579 | *set2 << QPointF(0.3,10) << QPointF(1.3,10) << QPointF(2.3,10); |
|
584 | 580 | series->append(set2); |
|
585 | 581 | |
|
586 | 582 | QSignalSpy setSpy1(set1, SIGNAL(hovered(bool))); |
|
587 | 583 | QSignalSpy setSpy2(set2, SIGNAL(hovered(bool))); |
|
588 | 584 | QSignalSpy seriesSpy(series,SIGNAL(hovered(QBarSet*,bool))); |
|
589 | 585 | |
|
590 | 586 | QChartView view(new QChart()); |
|
591 | 587 | view.resize(400,300); |
|
592 | 588 | view.chart()->addSeries(series); |
|
593 | 589 | view.show(); |
|
594 | 590 | QTest::qWaitForWindowShown(&view); |
|
595 | 591 | |
|
596 | 592 | //======================================================================= |
|
597 | 593 | // move mouse to left border |
|
598 | 594 | QTest::mouseMove(view.viewport(), QPoint(0, 180)); |
|
599 | 595 | |
|
600 | 596 | QVERIFY(setSpy1.count() == 0); |
|
601 | 597 | QVERIFY(setSpy2.count() == 0); |
|
602 | 598 | QVERIFY(seriesSpy.count() == 0); |
|
603 | 599 | |
|
604 | 600 | //======================================================================= |
|
605 | 601 | // move mouse on top of set1 |
|
606 | 602 | QTest::mouseMove(view.viewport(), QPoint(105,180)); |
|
607 | 603 | |
|
608 | 604 | QVERIFY(setSpy1.count() == 1); |
|
609 | 605 | QVERIFY(setSpy2.count() == 0); |
|
610 | 606 | QVERIFY(seriesSpy.count() == 1); |
|
611 | 607 | |
|
612 | 608 | QList<QVariant> setSpyArg = setSpy1.takeFirst(); |
|
613 | 609 | QVERIFY(setSpyArg.at(0).type() == QVariant::Bool); |
|
614 | 610 | QVERIFY(setSpyArg.at(0).toBool() == true); |
|
615 | 611 | |
|
616 | 612 | QList<QVariant> seriesSpyArg = seriesSpy.takeFirst(); |
|
617 | 613 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1); |
|
618 | 614 | |
|
619 | 615 | //======================================================================= |
|
620 | 616 | // move mouse from top of set1 to top of set2 |
|
621 | 617 | QTest::mouseMove(view.viewport(), QPoint(145,180)); |
|
622 | 618 | |
|
623 | 619 | QVERIFY(setSpy1.count() == 1); |
|
624 | 620 | QVERIFY(setSpy2.count() == 1); |
|
625 | 621 | QVERIFY(seriesSpy.count() == 2); |
|
626 | 622 | |
|
627 | 623 | // should leave set1 |
|
628 | 624 | setSpyArg = setSpy1.takeFirst(); |
|
629 | 625 | QVERIFY(setSpyArg.at(0).type() == QVariant::Bool); |
|
630 | 626 | QVERIFY(setSpyArg.at(0).toBool() == false); |
|
631 | 627 | |
|
632 | 628 | // should enter set2 |
|
633 | 629 | setSpyArg = setSpy2.takeFirst(); |
|
634 | 630 | QVERIFY(setSpyArg.at(0).type() == QVariant::Bool); |
|
635 | 631 | QVERIFY(setSpyArg.at(0).toBool() == true); |
|
636 | 632 | |
|
637 | 633 | // should leave set1 |
|
638 | 634 | seriesSpyArg = seriesSpy.takeFirst(); |
|
639 | 635 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set1); |
|
640 | 636 | QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool); |
|
641 | 637 | QVERIFY(seriesSpyArg.at(1).toBool() == false); |
|
642 | 638 | |
|
643 | 639 | // should enter set2 |
|
644 | 640 | seriesSpyArg = seriesSpy.takeFirst(); |
|
645 | 641 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2); |
|
646 | 642 | QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool); |
|
647 | 643 | QVERIFY(seriesSpyArg.at(1).toBool() == true); |
|
648 | 644 | |
|
649 | 645 | //======================================================================= |
|
650 | 646 | // move mouse from top of set2 to background |
|
651 | 647 | QTest::mouseMove(view.viewport(), QPoint(175,180)); |
|
652 | 648 | |
|
653 | 649 | QVERIFY(setSpy1.count() == 0); |
|
654 | 650 | QVERIFY(setSpy2.count() == 1); |
|
655 | 651 | QVERIFY(seriesSpy.count() == 1); |
|
656 | 652 | |
|
657 | 653 | // should leave set2 (event via set) |
|
658 | 654 | setSpyArg = setSpy2.takeFirst(); |
|
659 | 655 | QVERIFY(setSpyArg.at(0).type() == QVariant::Bool); |
|
660 | 656 | QVERIFY(setSpyArg.at(0).toBool() == false); |
|
661 | 657 | |
|
662 | 658 | // should leave set2 (event via series) |
|
663 | 659 | seriesSpyArg = seriesSpy.takeFirst(); |
|
664 | 660 | QCOMPARE(qvariant_cast<QBarSet*>(seriesSpyArg.at(0)), set2); |
|
665 | 661 | QVERIFY(seriesSpyArg.at(1).type() == QVariant::Bool); |
|
666 | 662 | QVERIFY(seriesSpyArg.at(1).toBool() == false); |
|
667 | 663 | } |
|
668 | 664 | |
|
669 | 665 | /* |
|
670 | 666 | bool setModel(QAbstractItemModel *model); |
|
671 | 667 | void setModelMapping(int categories, int bottomBoundary, int topBoundary, Qt::Orientation orientation = Qt::Vertical); |
|
672 | 668 | void setModelMappingRange(int first, int count = -1); |
|
673 | 669 | */ |
|
674 | 670 | QTEST_MAIN(tst_QBarSeries) |
|
675 | 671 | |
|
676 | 672 | #include "tst_qbarseries.moc" |
|
677 | 673 |
|
1 | NO CONTENT: file was removed |
|
1 | NO CONTENT: file was removed |
General Comments 0
You need to be logged in to leave comments.
Login now