##// END OF EJS Templates
barchart animation blink fix
sauimone -
r1389:e0ea9f9bcb2b
parent child
Show More
@@ -1,212 +1,212
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "barchartitem_p.h"
21 #include "barchartitem_p.h"
22 #include "bar_p.h"
22 #include "bar_p.h"
23 #include "qbarset.h"
23 #include "qbarset.h"
24 #include "qbarset_p.h"
24 #include "qbarset_p.h"
25 #include "qbarseries.h"
25 #include "qbarseries.h"
26 #include "qbarseries_p.h"
26 #include "qbarseries_p.h"
27 #include "qchart.h"
27 #include "qchart.h"
28 #include "chartpresenter_p.h"
28 #include "chartpresenter_p.h"
29 #include "chartanimator_p.h"
29 #include "chartanimator_p.h"
30 #include "chartdataset_p.h"
30 #include "chartdataset_p.h"
31 #include <QPainter>
31 #include <QPainter>
32
32
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34
34
35 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
35 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
36 ChartItem(presenter),
36 ChartItem(presenter),
37 m_series(series)
37 m_series(series)
38 {
38 {
39 setFlag(ItemClipsChildrenToShape);
39 setFlag(ItemClipsChildrenToShape);
40 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
40 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
41 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool)));
41 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool)));
42 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
42 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
43 connect(series, SIGNAL(visibleChanged()), this, SLOT(handleLayoutChanged()));
43 connect(series, SIGNAL(visibleChanged()), this, SLOT(handleLayoutChanged()));
44 setZValue(ChartPresenter::BarSeriesZValue);
44 setZValue(ChartPresenter::BarSeriesZValue);
45 handleDataStructureChanged();
45 handleDataStructureChanged();
46 }
46 }
47
47
48 BarChartItem::~BarChartItem()
48 BarChartItem::~BarChartItem()
49 {
49 {
50 }
50 }
51
51
52 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
52 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
53 {
53 {
54 Q_UNUSED(painter);
54 Q_UNUSED(painter);
55 Q_UNUSED(option);
55 Q_UNUSED(option);
56 Q_UNUSED(widget);
56 Q_UNUSED(widget);
57 }
57 }
58
58
59 QRectF BarChartItem::boundingRect() const
59 QRectF BarChartItem::boundingRect() const
60 {
60 {
61 return m_rect;
61 return m_rect;
62 }
62 }
63
63
64 void BarChartItem::handleDataStructureChanged()
64 void BarChartItem::handleDataStructureChanged()
65 {
65 {
66 foreach(QGraphicsItem *item, childItems()) {
66 foreach(QGraphicsItem *item, childItems()) {
67 delete item;
67 delete item;
68 }
68 }
69
69
70 m_bars.clear();
70 m_bars.clear();
71 m_labels.clear();
71 m_labels.clear();
72 m_layout.clear();
72 m_layout.clear();
73
73
74 bool labelsVisible = m_series->isLabelsVisible();
74 bool labelsVisible = m_series->isLabelsVisible();
75
75
76 // Create new graphic items for bars
76 // Create new graphic items for bars
77 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
77 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
78 for (int s = 0; s < m_series->barsetCount(); s++) {
78 for (int s = 0; s < m_series->barsetCount(); s++) {
79 QBarSet *set = m_series->d_func()->barsetAt(s);
79 QBarSet *set = m_series->d_func()->barsetAt(s);
80
80
81 // Bars
81 // Bars
82 Bar *bar = new Bar(set,c,this);
82 Bar *bar = new Bar(set,c,this);
83 m_bars.append(bar);
83 m_bars.append(bar);
84 connect(bar, SIGNAL(clicked(QBarSet*,int)), m_series, SIGNAL(clicked(QBarSet*,int)));
84 connect(bar, SIGNAL(clicked(QBarSet*,int)), m_series, SIGNAL(clicked(QBarSet*,int)));
85 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
85 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
86 m_layout.append(QRectF(0, 0, 0, 0));
86 m_layout.append(QRectF(0, 0, 0, 0));
87
87
88 // Labels
88 // Labels
89 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
89 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
90 label->setVisible(labelsVisible);
90 label->setVisible(labelsVisible);
91 m_labels.append(label);
91 m_labels.append(label);
92 }
92 }
93 }
93 }
94
94
95 // TODO: Is this the right place to call it?
95 // TODO: Is this the right place to call it?
96 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
96 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
97 handleLayoutChanged();
97 handleLayoutChanged();
98 }
98 }
99
99
100 QVector<QRectF> BarChartItem::calculateLayout()
100 QVector<QRectF> BarChartItem::calculateLayout()
101 {
101 {
102 QVector<QRectF> layout;
102 QVector<QRectF> layout;
103
103
104 // Use temporary qreals for accuracy
104 // Use temporary qreals for accuracy
105 qreal categoryCount = m_series->d_func()->categoryCount();
105 qreal categoryCount = m_series->d_func()->categoryCount();
106 qreal setCount = m_series->barsetCount();
106 qreal setCount = m_series->barsetCount();
107 bool barsVisible = m_series->isVisible();
107 bool barsVisible = m_series->isVisible();
108
108
109 // Domain:
109 // Domain:
110 qreal width = geometry().width();
110 qreal width = geometry().width();
111 qreal height = geometry().height();
111 qreal height = geometry().height();
112 qreal rangeY = m_domainMaxY - m_domainMinY;
112 qreal rangeY = m_domainMaxY - m_domainMinY;
113 qreal rangeX = m_domainMaxX - m_domainMinX;
113 qreal rangeX = m_domainMaxX - m_domainMinX;
114 qreal scaleY = (height / rangeY);
114 qreal scaleY = (height / rangeY);
115 qreal scaleX = (width / rangeX);
115 qreal scaleX = (width / rangeX);
116 qreal barWidth = scaleX - scaleX * m_series->d_func()->barMargin();
116 qreal barWidth = scaleX - scaleX * m_series->d_func()->barMargin();
117
117
118 int itemIndex(0);
118 int itemIndex(0);
119 for (int category = 0; category < categoryCount; category++) {
119 for (int category = 0; category < categoryCount; category++) {
120 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
120 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
121 for (int set = 0; set < setCount; set++) {
121 for (int set = 0; set < setCount; set++) {
122 QBarSet* barSet = m_series->d_func()->barsetAt(set);
122 QBarSet* barSet = m_series->d_func()->barsetAt(set);
123 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
123 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
124 qreal barHeight = barSet->at(category).y() * scaleY;
124 qreal barHeight = barSet->at(category).y() * scaleY;
125
125
126 Bar* bar = m_bars.at(itemIndex);
126 Bar* bar = m_bars.at(itemIndex);
127 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
127 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
128
128
129 layout.append(rect);
129 layout.append(rect);
130 bar->setPen(barSet->pen());
130 bar->setPen(barSet->pen());
131 bar->setBrush(barSet->brush());
131 bar->setBrush(barSet->brush());
132 bar->setVisible(barsVisible);
132 bar->setVisible(barsVisible);
133
133
134 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
134 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
135
135
136 if (!qFuzzyIsNull(barSet->at(category).y())) {
136 if (!qFuzzyIsNull(barSet->at(category).y())) {
137 label->setText(QString::number(barSet->at(category).y()));
137 label->setText(QString::number(barSet->at(category).y()));
138 } else {
138 } else {
139 label->setText(QString(""));
139 label->setText(QString(""));
140 }
140 }
141
141
142 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
142 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
143 ,yPos - barHeight/2 - label->boundingRect().height()/2);
143 ,yPos - barHeight/2 - label->boundingRect().height()/2);
144 label->setFont(barSet->labelFont());
144 label->setFont(barSet->labelFont());
145 label->setBrush(barSet->labelBrush());
145 label->setBrush(barSet->labelBrush());
146
146
147 itemIndex++;
147 itemIndex++;
148 }
148 }
149 }
149 }
150
150
151 return layout;
151 return layout;
152 }
152 }
153
153
154 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
154 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
155 {
155 {
156 if (animator()) {
156 if (animator()) {
157 animator()->updateLayout(this, m_layout, layout);
157 animator()->updateLayout(this, m_layout, layout);
158 } else {
158 } else {
159 setLayout(layout);
159 setLayout(layout);
160 update();
160 }
161 }
161 }
162 }
162
163
163 void BarChartItem::setLayout(const QVector<QRectF> &layout)
164 void BarChartItem::setLayout(const QVector<QRectF> &layout)
164 {
165 {
165 m_layout = layout;
166 m_layout = layout;
166
167
167 for (int i=0; i < m_bars.count(); i++) {
168 for (int i=0; i < m_bars.count(); i++) {
168 m_bars.at(i)->setRect(layout.at(i));
169 m_bars.at(i)->setRect(layout.at(i));
169 }
170 }
170
171
171 update();
172 update();
172 }
173 }
173 //handlers
174 //handlers
174
175
175 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
176 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
176 {
177 {
177 m_domainMinX = minX;
178 m_domainMinX = minX;
178 m_domainMaxX = maxX;
179 m_domainMaxX = maxX;
179 m_domainMinY = minY;
180 m_domainMinY = minY;
180 m_domainMaxY = maxY;
181 m_domainMaxY = maxY;
181 handleLayoutChanged();
182 handleLayoutChanged();
182 }
183 }
183
184
184 void BarChartItem::handleGeometryChanged(const QRectF &rect)
185 void BarChartItem::handleGeometryChanged(const QRectF &rect)
185 {
186 {
186 prepareGeometryChange();
187 prepareGeometryChange();
187 m_rect = rect;
188 m_rect = rect;
188 handleLayoutChanged();
189 handleLayoutChanged();
189 }
190 }
190
191
191 void BarChartItem::handleLayoutChanged()
192 void BarChartItem::handleLayoutChanged()
192 {
193 {
193 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
194 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
194 // rect size zero.
195 // rect size zero.
195 return;
196 return;
196 }
197 }
197 QVector<QRectF> layout = calculateLayout();
198 QVector<QRectF> layout = calculateLayout();
198 applyLayout(layout);
199 applyLayout(layout);
199 update();
200 }
200 }
201
201
202 void BarChartItem::handleLabelsVisibleChanged(bool visible)
202 void BarChartItem::handleLabelsVisibleChanged(bool visible)
203 {
203 {
204 foreach (QGraphicsSimpleTextItem* label, m_labels) {
204 foreach (QGraphicsSimpleTextItem* label, m_labels) {
205 label->setVisible(visible);
205 label->setVisible(visible);
206 }
206 }
207 update();
207 update();
208 }
208 }
209
209
210 #include "moc_barchartitem_p.cpp"
210 #include "moc_barchartitem_p.cpp"
211
211
212 QTCOMMERCIALCHART_END_NAMESPACE
212 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now