##// END OF EJS Templates
barchart animation blink fix
sauimone -
r1389:e0ea9f9bcb2b
parent child
Show More
@@ -1,212 +1,212
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 23 #include "qbarset.h"
24 24 #include "qbarset_p.h"
25 25 #include "qbarseries.h"
26 26 #include "qbarseries_p.h"
27 27 #include "qchart.h"
28 28 #include "chartpresenter_p.h"
29 29 #include "chartanimator_p.h"
30 30 #include "chartdataset_p.h"
31 31 #include <QPainter>
32 32
33 33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 34
35 35 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
36 36 ChartItem(presenter),
37 37 m_series(series)
38 38 {
39 39 setFlag(ItemClipsChildrenToShape);
40 40 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
41 41 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool)));
42 42 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
43 43 connect(series, SIGNAL(visibleChanged()), this, SLOT(handleLayoutChanged()));
44 44 setZValue(ChartPresenter::BarSeriesZValue);
45 45 handleDataStructureChanged();
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::handleDataStructureChanged()
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 74 bool labelsVisible = m_series->isLabelsVisible();
75 75
76 76 // Create new graphic items for bars
77 77 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
78 78 for (int s = 0; s < m_series->barsetCount(); s++) {
79 79 QBarSet *set = m_series->d_func()->barsetAt(s);
80 80
81 81 // Bars
82 82 Bar *bar = new Bar(set,c,this);
83 83 m_bars.append(bar);
84 84 connect(bar, SIGNAL(clicked(QBarSet*,int)), m_series, SIGNAL(clicked(QBarSet*,int)));
85 85 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
86 86 m_layout.append(QRectF(0, 0, 0, 0));
87 87
88 88 // Labels
89 89 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
90 90 label->setVisible(labelsVisible);
91 91 m_labels.append(label);
92 92 }
93 93 }
94 94
95 95 // TODO: Is this the right place to call it?
96 96 presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
97 97 handleLayoutChanged();
98 98 }
99 99
100 100 QVector<QRectF> BarChartItem::calculateLayout()
101 101 {
102 102 QVector<QRectF> layout;
103 103
104 104 // Use temporary qreals for accuracy
105 105 qreal categoryCount = m_series->d_func()->categoryCount();
106 106 qreal setCount = m_series->barsetCount();
107 107 bool barsVisible = m_series->isVisible();
108 108
109 109 // Domain:
110 110 qreal width = geometry().width();
111 111 qreal height = geometry().height();
112 112 qreal rangeY = m_domainMaxY - m_domainMinY;
113 113 qreal rangeX = m_domainMaxX - m_domainMinX;
114 114 qreal scaleY = (height / rangeY);
115 115 qreal scaleX = (width / rangeX);
116 116 qreal barWidth = scaleX - scaleX * m_series->d_func()->barMargin();
117 117
118 118 int itemIndex(0);
119 119 for (int category = 0; category < categoryCount; category++) {
120 120 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
121 121 for (int set = 0; set < setCount; set++) {
122 122 QBarSet* barSet = m_series->d_func()->barsetAt(set);
123 123 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
124 124 qreal barHeight = barSet->at(category).y() * scaleY;
125 125
126 126 Bar* bar = m_bars.at(itemIndex);
127 127 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
128 128
129 129 layout.append(rect);
130 130 bar->setPen(barSet->pen());
131 131 bar->setBrush(barSet->brush());
132 132 bar->setVisible(barsVisible);
133 133
134 134 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
135 135
136 136 if (!qFuzzyIsNull(barSet->at(category).y())) {
137 137 label->setText(QString::number(barSet->at(category).y()));
138 138 } else {
139 139 label->setText(QString(""));
140 140 }
141 141
142 142 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
143 143 ,yPos - barHeight/2 - label->boundingRect().height()/2);
144 144 label->setFont(barSet->labelFont());
145 145 label->setBrush(barSet->labelBrush());
146 146
147 147 itemIndex++;
148 148 }
149 149 }
150 150
151 151 return layout;
152 152 }
153 153
154 154 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
155 155 {
156 156 if (animator()) {
157 157 animator()->updateLayout(this, m_layout, layout);
158 158 } else {
159 159 setLayout(layout);
160 update();
160 161 }
161 162 }
162 163
163 164 void BarChartItem::setLayout(const QVector<QRectF> &layout)
164 165 {
165 166 m_layout = layout;
166 167
167 168 for (int i=0; i < m_bars.count(); i++) {
168 169 m_bars.at(i)->setRect(layout.at(i));
169 170 }
170 171
171 172 update();
172 173 }
173 174 //handlers
174 175
175 176 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
176 177 {
177 178 m_domainMinX = minX;
178 179 m_domainMaxX = maxX;
179 180 m_domainMinY = minY;
180 181 m_domainMaxY = maxY;
181 182 handleLayoutChanged();
182 183 }
183 184
184 185 void BarChartItem::handleGeometryChanged(const QRectF &rect)
185 186 {
186 187 prepareGeometryChange();
187 188 m_rect = rect;
188 189 handleLayoutChanged();
189 190 }
190 191
191 192 void BarChartItem::handleLayoutChanged()
192 193 {
193 194 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
194 195 // rect size zero.
195 196 return;
196 197 }
197 198 QVector<QRectF> layout = calculateLayout();
198 199 applyLayout(layout);
199 update();
200 200 }
201 201
202 202 void BarChartItem::handleLabelsVisibleChanged(bool visible)
203 203 {
204 204 foreach (QGraphicsSimpleTextItem* label, m_labels) {
205 205 label->setVisible(visible);
206 206 }
207 207 update();
208 208 }
209 209
210 210 #include "moc_barchartitem_p.cpp"
211 211
212 212 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now