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