##// END OF EJS Templates
barchart animation mechanics working. still some todo
sauimone -
r681:223051b363f0
parent child
Show More
@@ -3,7 +3,9
3 #include <QParallelAnimationGroup>
3 #include <QParallelAnimationGroup>
4 #include <QTimer>
4 #include <QTimer>
5
5
6 Q_DECLARE_METATYPE(QVector<QSizeF>)
6 Q_DECLARE_METATYPE(QVector<QRectF>)
7 //Q_DECLARE_METATYPE(BarLayout) // TODO?
8
7
9
8 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9
11
@@ -17,31 +19,35 BarAnimation::~BarAnimation()
17 {
19 {
18 }
20 }
19
21
20 void BarAnimation::updateValues(const BarLayout& /*layout*/)
22 void BarAnimation::updateValues(const QVector<QRectF>& layout)
21 {
23 {
22 // TODO:
24 // TODO?:
23 qDebug() << "BarAnimation::updateValues";
25 // qDebug() << "BarAnimation::updateValues" << layout.count();
24 }
26 }
25
27
26
28
27 QVariant BarAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const
29 QVariant BarAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const
28 {
30 {
29 QVector<QSizeF> startVector = qVariantValue<QVector<QSizeF> > (from);
31 QVector<QRectF> startVector = qVariantValue<QVector<QRectF> > (from);
30 QVector<QSizeF> endVector = qVariantValue<QVector<QSizeF> > (to);
32 QVector<QRectF> endVector = qVariantValue<QVector<QRectF> > (to);
31 QVector<QSizeF> result;
33 QVector<QRectF> result;
32
34
33 Q_ASSERT(startVector.count() == endVector.count()) ;
35 Q_ASSERT(startVector.count() == endVector.count()) ;
34
36
35 for(int i =0 ;i< startVector.count();i++){
37 for(int i =0 ;i< startVector.count();i++){
36 QSizeF value = startVector[i] + ((endVector[i]- endVector[i]) * progress);
38 //QRectF value = startVector[i] + ((endVector[i] - startVector[i]) * progress);
37 result << value;
39 QPointF topLeft = startVector[i].topLeft() + ((endVector[i].topLeft() - startVector[i].topLeft()) * progress);
40 QSizeF size = startVector[i].size() + ((endVector[i].size() - startVector[i].size()) * progress);
41 QRectF value(topLeft,size);
42 result << value;
38 }
43 }
39 return qVariantFromValue(result);
44 return qVariantFromValue(result);
40 }
45 }
41
46
42 void BarAnimation::updateCurrentValue(const QVariant &)
47 void BarAnimation::updateCurrentValue(const QVariant &value)
43 {
48 {
44 // TODO?
49 QVector<QRectF> layout = qVariantValue<QVector<QRectF> >(value);
50 m_item->setLayout(layout);
45 }
51 }
46
52
47 #include "moc_baranimation_p.cpp"
53 #include "moc_baranimation_p.cpp"
@@ -18,7 +18,7 public:
18 BarAnimation(BarChartItem *item);
18 BarAnimation(BarChartItem *item);
19 ~BarAnimation();
19 ~BarAnimation();
20
20
21 void updateValues(const BarLayout& layout);
21 void updateValues(const QVector<QRectF>& layout);
22
22
23 public: // from QVariantAnimation
23 public: // from QVariantAnimation
24 virtual QVariant interpolated (const QVariant & from, const QVariant & to, qreal progress ) const;
24 virtual QVariant interpolated (const QVariant & from, const QVariant & to, qreal progress ) const;
@@ -13,6 +13,7
13
13
14 Q_DECLARE_METATYPE(QVector<QPointF>)
14 Q_DECLARE_METATYPE(QVector<QPointF>)
15 Q_DECLARE_METATYPE(QVector<qreal>)
15 Q_DECLARE_METATYPE(QVector<qreal>)
16 Q_DECLARE_METATYPE(QVector<QRectF>)
16
17
17 QTCOMMERCIALCHART_BEGIN_NAMESPACE
18 QTCOMMERCIALCHART_BEGIN_NAMESPACE
18
19
@@ -267,12 +268,16 void ChartAnimator::updateLayout(PieChartItem* item, QPieSlice *slice, const Pie
267 animation->updateValue(slice, sliceData);
268 animation->updateValue(slice, sliceData);
268 }
269 }
269
270
270 void ChartAnimator::updateLayout(BarChartItem* item, const BarLayout &layout)
271 void ChartAnimator::updateLayout(BarChartItem* item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout)
271 {
272 {
272 qDebug() << "ChartAnimator::updateLayout";
273 qDebug() << "ChartAnimator::updateLayout";
273 BarAnimation* animation = static_cast<BarAnimation*>(m_animations.value(item));
274 BarAnimation* animation = static_cast<BarAnimation*>(m_animations.value(item));
274 Q_ASSERT(animation);
275 Q_ASSERT(animation);
275 animation->updateValues(layout);
276 // animation->updateValues(layout);
277 animation->setDuration(duration);
278 animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout));
279 animation->setKeyValueAt(1.0, qVariantFromValue(newLayout));
280 QTimer::singleShot(0,animation,SLOT(start()));
276 }
281 }
277
282
278
283
@@ -43,7 +43,7 public:
43 void updateLayout(PieChartItem* item, const PieLayout &layout);
43 void updateLayout(PieChartItem* item, const PieLayout &layout);
44 void updateLayout(PieChartItem* item, QPieSlice *slice, const PieSliceData &sliceData);
44 void updateLayout(PieChartItem* item, QPieSlice *slice, const PieSliceData &sliceData);
45
45
46 void updateLayout(BarChartItem* item, const BarLayout &layout);
46 void updateLayout(BarChartItem* item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout);
47
47
48 void setState(State state,const QPointF& point = QPointF());
48 void setState(State state,const QPointF& point = QPointF());
49
49
@@ -20,7 +20,7 BarChartItem::BarChartItem(QBarSeries *series, ChartPresenter *presenter) :
20 mSeries(series)
20 mSeries(series)
21 {
21 {
22 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
22 connect(series,SIGNAL(showToolTip(QPoint,QString)),this,SLOT(showToolTip(QPoint,QString)));
23 connect(series, SIGNAL(updatedBars()), this, SLOT(layoutChanged()));
23 connect(series, SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
24 //TODO: connect(series,SIGNAL("position or size has changed"), this, SLOT(handleLayoutChanged()));
24 //TODO: connect(series,SIGNAL("position or size has changed"), this, SLOT(handleLayoutChanged()));
25 connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int)));
25 connect(series, SIGNAL(restructuredBar(int)), this, SLOT(handleModelChanged(int)));
26 setZValue(ChartPresenter::BarSeriesZValue);
26 setZValue(ChartPresenter::BarSeriesZValue);
@@ -59,6 +59,7 void BarChartItem::dataChanged()
59
59
60 mBars.clear();
60 mBars.clear();
61 mFloatingValues.clear();
61 mFloatingValues.clear();
62 mLayout.clear();
62
63
63 // Create new graphic items for bars
64 // Create new graphic items for bars
64 for (int c=0; c<mSeries->categoryCount(); c++) {
65 for (int c=0; c<mSeries->categoryCount(); c++) {
@@ -72,6 +73,7 void BarChartItem::dataChanged()
72 connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString)));
73 connect(bar,SIGNAL(rightClicked(QString)),set,SIGNAL(rightClicked(QString)));
73 connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint)));
74 connect(bar,SIGNAL(hoverEntered(QPoint)),set,SLOT(barHoverEnterEvent(QPoint)));
74 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent()));
75 connect(bar,SIGNAL(hoverLeaved()),set,SLOT(barHoverLeaveEvent()));
76 mLayout.append(QRectF(0,0,0,0));
75 }
77 }
76 }
78 }
77
79
@@ -86,9 +88,11 void BarChartItem::dataChanged()
86 }
88 }
87 }
89 }
88 }
90 }
89
91 /*
90 void BarChartItem::layoutChanged()
92 void BarChartItem::layoutChanged()
91 {
93 {
94 qDebug() << "Deprecated BarChartItem::layoutChanged called. aborting";
95 return;
92 // Scale bars to new layout
96 // Scale bars to new layout
93 // Layout for bars:
97 // Layout for bars:
94 if (mSeries->barsetCount() <= 0) {
98 if (mSeries->barsetCount() <= 0) {
@@ -118,6 +122,8 void BarChartItem::layoutChanged()
118 qreal categoryWidth = width/categoryCount;
122 qreal categoryWidth = width/categoryCount;
119 qreal barWidth = categoryWidth / (setCount+1);
123 qreal barWidth = categoryWidth / (setCount+1);
120
124
125 BarLayout layout;
126
121 int itemIndex(0);
127 int itemIndex(0);
122 for (int category=0; category < categoryCount; category++) {
128 for (int category=0; category < categoryCount; category++) {
123 qreal xPos = categoryWidth * category + barWidth/2;
129 qreal xPos = categoryWidth * category + barWidth/2;
@@ -126,11 +132,18 void BarChartItem::layoutChanged()
126 qreal barHeight = mSeries->valueAt(set,category) * scale;
132 qreal barHeight = mSeries->valueAt(set,category) * scale;
127 Bar* bar = mBars.at(itemIndex);
133 Bar* bar = mBars.at(itemIndex);
128
134
135 QRectF rect(xPos,yPos-barHeight,mBarWidth,barHeight);
136 layout.insert(bar,rect);
129 // TODO: width settable per bar?
137 // TODO: width settable per bar?
130 bar->setRect(xPos, yPos-barHeight,barWidth, barHeight);
138 bar->setRect(xPos, yPos-barHeight,barWidth, barHeight);
131 bar->setPen(mSeries->barsetAt(set)->pen());
139 bar->setPen(mSeries->barsetAt(set)->pen());
132 bar->setBrush(mSeries->barsetAt(set)->brush());
140 bar->setBrush(mSeries->barsetAt(set)->brush());
133
141
142 // bar->resize(mBarWidth, barHeight);
143 // layout.insert(bar,QSizeF(mBarWidth,barHeight));
144 bar->setPen(mSeries->barsetAt(set)->pen());
145 bar->setBrush(mSeries->barsetAt(set)->brush());
146 // bar->setPos(xPos, yPos-barHeight);
134 itemIndex++;
147 itemIndex++;
135 xPos += barWidth;
148 xPos += barWidth;
136 }
149 }
@@ -160,32 +173,106 void BarChartItem::layoutChanged()
160 xPos += barWidth;
173 xPos += barWidth;
161 }
174 }
162 }
175 }
163 update();
176 // update();
164 }
177 }
165
178 */
166 BarLayout BarChartItem::calculateLayout()
179 QVector<QRectF> BarChartItem::calculateLayout()
167 {
180 {
181 // layoutChanged();
182 /*
168 BarLayout layout;
183 BarLayout layout;
169 foreach(Bar* bar, mBars) {
184 foreach(Bar* bar, mBars) {
170 layout.insert(bar,bar->boundingRect());
185 layout.insert(bar,bar->boundingRect());
171 }
186 }
187 */
188 QVector<QRectF> layout;
189
190 // Use temporary qreals for accurancy (we might get some compiler warnings... :)
191 int categoryCount = mSeries->categoryCount();
192 int setCount = mSeries->barsetCount();
193
194 qreal tW = mWidth;
195 qreal tH = mHeight;
196 qreal tM = mSeries->max();
197
198 // Domain:
199 if (mDomainMaxY > tM) {
200 tM = mDomainMaxY;
201 }
202
203 qreal scale = (tH/tM);
204 qreal tC = categoryCount + 1;
205 qreal categoryWidth = tW/tC;
206 mBarWidth = categoryWidth / (setCount+1);
207
208 int itemIndex(0);
209 for (int category=0; category < categoryCount; category++) {
210 qreal xPos = categoryWidth * category + categoryWidth /2 + mBarWidth/2;
211 qreal yPos = mHeight;
212 for (int set = 0; set < setCount; set++) {
213 qreal barHeight = mSeries->valueAt(set,category) * scale;
214 Bar* bar = mBars.at(itemIndex);
215
216 QRectF rect(xPos,yPos-barHeight,mBarWidth,barHeight);
217 //layout.insert(bar,rect);
218 layout.append(rect);
219 // TODO: width settable per bar?
220 // bar->resize(mBarWidth, barHeight);
221 // layout.insert(bar,QSizeF(mBarWidth,barHeight));
222 bar->setPen(mSeries->barsetAt(set)->pen());
223 bar->setBrush(mSeries->barsetAt(set)->brush());
224 // bar->setPos(xPos, yPos-barHeight);
225 itemIndex++;
226 xPos += mBarWidth;
227 }
228 }
229
230 // Position floating values
231 itemIndex = 0;
232 for (int category=0; category < mSeries->categoryCount(); category++) {
233 qreal xPos = categoryWidth * category + categoryWidth/2 + mBarWidth;
234 qreal yPos = mHeight;
235 for (int set=0; set < mSeries->barsetCount(); set++) {
236 qreal barHeight = mSeries->valueAt(set,category) * scale;
237 BarValue* value = mFloatingValues.at(itemIndex);
238
239 QBarSet* barSet = mSeries->barsetAt(set);
240 value->resize(100,50); // TODO: proper layout for this.
241 value->setPos(xPos, yPos-barHeight/2);
242 value->setPen(barSet->floatingValuePen());
243
244 if (mSeries->valueAt(set,category) != 0) {
245 value->setValueString(QString::number(mSeries->valueAt(set,category)));
246 } else {
247 value->setValueString(QString(""));
248 }
249
250 itemIndex++;
251 xPos += mBarWidth;
252 }
253 }
172
254
173 return layout;
255 return layout;
174 }
256 }
175
257
176 void BarChartItem::applyLayout(const BarLayout &layout)
258 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
177 {
259 {
178 if (animator())
260 if (animator())
179 animator()->updateLayout(this, layout);
261 animator()->updateLayout(this, mLayout, layout);
180 else
262 else
181 setLayout(layout);
263 setLayout(layout);
182 }
264 }
183
265
184 void BarChartItem::setLayout(const BarLayout &layout)
266 void BarChartItem::setLayout(const QVector<QRectF> &layout)
185 {
267 {
186 foreach (Bar *bar, layout.keys()) {
268 mLayout = layout;
187 bar->setRect(layout.value(bar));
269
270 for (int i=0; i<mBars.count(); i++) {
271 //mBars.at(i)->setSize(layout.at(i).size());
272 //mBars.at(i)->setPos(layout.at(i).topLeft());
273 mBars.at(i)->setRect(layout.at(i));
188 }
274 }
275
189 update();
276 update();
190 }
277 }
191
278
@@ -218,7 +305,7 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal
218 mDomainMaxX = maxX;
305 mDomainMaxX = maxX;
219 mDomainMinY = minY;
306 mDomainMinY = minY;
220 mDomainMaxY = maxY;
307 mDomainMaxY = maxY;
221 layoutChanged();
308 handleLayoutChanged();
222
309
223 /*
310 /*
224 int count = mSeries->categoryCount();
311 int count = mSeries->categoryCount();
@@ -249,7 +336,8 void BarChartItem::handleGeometryChanged(const QRectF& rect)
249
336
250 void BarChartItem::handleLayoutChanged()
337 void BarChartItem::handleLayoutChanged()
251 {
338 {
252 BarLayout layout = calculateLayout();
339 qDebug() << "BarChartItem::handleLayoutChanged";
340 QVector<QRectF> layout = calculateLayout();
253 applyLayout(layout);
341 applyLayout(layout);
254 update();
342 update();
255 }
343 }
@@ -14,7 +14,7 class BarValue;
14 class QChartAxisCategories;
14 class QChartAxisCategories;
15 class QChart;
15 class QChart;
16
16
17 typedef QHash<Bar*, QRectF> BarLayout;
17 //typedef QVector<QRectF> BarLayout;
18
18
19 class BarChartItem : public ChartItem
19 class BarChartItem : public ChartItem
20 {
20 {
@@ -34,12 +34,13 public:
34 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
34 // TODO: Consider the domain for layoutChanged. May be use case, may not be. If it is, then the derived classes need to implement it
35 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
35 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
36 private slots:
36 private slots:
37 virtual void layoutChanged(); // layout has changed -> need to recalculate bar sizes
37 //virtual void layoutChanged(); // layout has changed -> need to recalculate bar sizes
38
38
39 public:
39 public:
40 BarLayout calculateLayout();
40 QVector<QRectF> calculateLayout();
41 void applyLayout(const BarLayout &layout);
41 void applyLayout(const QVector<QRectF> &layout);
42 void setLayout(const BarLayout &layout);
42 void setLayout(const QVector<QRectF> &layout);
43 void updateLayout(const QVector<QRectF> &layout);
43
44
44 QRectF geometry() const { return m_rect;}
45 QRectF geometry() const { return m_rect;}
45
46
@@ -65,6 +66,7 protected:
65
66
66 QRectF m_rect;
67 QRectF m_rect;
67 bool mLayoutSet; // True, if component has been laid out.
68 bool mLayoutSet; // True, if component has been laid out.
69 QVector<QRectF> mLayout;
68
70
69 // Not owned.
71 // Not owned.
70 QBarSeries* mSeries;
72 QBarSeries* mSeries;
@@ -51,10 +51,12 QBarSeries::QBarSeries(QStringList categories, QObject *parent)
51 */
51 */
52 void QBarSeries::addBarSet(QBarSet *set)
52 void QBarSeries::addBarSet(QBarSet *set)
53 {
53 {
54 qDebug() << "add bar set";
54 mModel->addBarSet(set);
55 mModel->addBarSet(set);
55 connect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString)));
56 connect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString)));
56 connect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString)));
57 connect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString)));
57 connect(set, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
58 connect(set, SIGNAL(valueChanged()), this, SLOT(barsetChanged()));
59 emit updatedBars();
58 }
60 }
59
61
60 /*!
62 /*!
@@ -67,6 +69,7 void QBarSeries::removeBarSet(QBarSet *set)
67 disconnect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString)));
69 disconnect(set,SIGNAL(clicked(QString)),this,SLOT(barsetClicked(QString)));
68 disconnect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString)));
70 disconnect(set,SIGNAL(rightClicked(QString)),this,SLOT(barsetRightClicked(QString)));
69 mModel->removeBarSet(set);
71 mModel->removeBarSet(set);
72 emit updatedBars();
70 }
73 }
71
74
72 void QBarSeries::insertBarSet(int i, QBarSet *set)
75 void QBarSeries::insertBarSet(int i, QBarSet *set)
General Comments 0
You need to be logged in to leave comments. Login now