##// END OF EJS Templates
removed m_layoutSet from barchartitem
sauimone -
r1247:0108313386d5
parent child
Show More
@@ -1,217 +1,215
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_layoutSet(false),
38 m_series(series)
37 m_series(series)
39 {
38 {
40 setFlag(ItemClipsChildrenToShape);
39 setFlag(ItemClipsChildrenToShape);
41 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
40 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleLayoutChanged()));
42 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleModelChanged()));
41 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleModelChanged()));
43 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(labelsVisibleChanged(bool)));
42 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(labelsVisibleChanged(bool)));
44 setZValue(ChartPresenter::BarSeriesZValue);
43 setZValue(ChartPresenter::BarSeriesZValue);
45 dataChanged();
44 dataChanged();
46 }
45 }
47
46
48 BarChartItem::~BarChartItem()
47 BarChartItem::~BarChartItem()
49 {
48 {
50 }
49 }
51
50
52 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
51 void BarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
53 {
52 {
54 Q_UNUSED(painter);
53 Q_UNUSED(painter);
55 Q_UNUSED(option);
54 Q_UNUSED(option);
56 Q_UNUSED(widget);
55 Q_UNUSED(widget);
57 }
56 }
58
57
59 QRectF BarChartItem::boundingRect() const
58 QRectF BarChartItem::boundingRect() const
60 {
59 {
61 return m_rect;
60 return m_rect;
62 }
61 }
63
62
64 void BarChartItem::dataChanged()
63 void BarChartItem::dataChanged()
65 {
64 {
66 foreach(QGraphicsItem *item, childItems()) {
65 foreach(QGraphicsItem *item, childItems()) {
67 delete item;
66 delete item;
68 }
67 }
69
68
70 m_bars.clear();
69 m_bars.clear();
71 m_labels.clear();
70 m_labels.clear();
72 m_layout.clear();
71 m_layout.clear();
73
72
74 bool labelsVisible = m_series->isLabelsVisible();
73 bool labelsVisible = m_series->isLabelsVisible();
75
74
76 // Create new graphic items for bars
75 // Create new graphic items for bars
77 for (int c = 0; c < m_series->categoryCount(); c++) {
76 for (int c = 0; c < m_series->categoryCount(); c++) {
78 QString category = m_series->d_func()->categoryName(c);
77 QString category = m_series->d_func()->categoryName(c);
79 for (int s = 0; s < m_series->barsetCount(); s++) {
78 for (int s = 0; s < m_series->barsetCount(); s++) {
80 QBarSet *set = m_series->d_func()->barsetAt(s);
79 QBarSet *set = m_series->d_func()->barsetAt(s);
81
80
82 // Bars
81 // Bars
83 Bar *bar = new Bar(set,category,this);
82 Bar *bar = new Bar(set,category,this);
84 m_bars.append(bar);
83 m_bars.append(bar);
85 connect(bar, SIGNAL(clicked(QString)), set, SIGNAL(clicked(QString)));
84 connect(bar, SIGNAL(clicked(QString)), set, SIGNAL(clicked(QString)));
86 connect(bar, SIGNAL(clicked(QBarSet*,QString)), m_series, SIGNAL(clicked(QBarSet*,QString)));
85 connect(bar, SIGNAL(clicked(QBarSet*,QString)), m_series, SIGNAL(clicked(QBarSet*,QString)));
87 connect(bar, SIGNAL(hovered(bool)), set, SIGNAL(hovered(bool)));
86 connect(bar, SIGNAL(hovered(bool)), set, SIGNAL(hovered(bool)));
88 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
87 connect(bar, SIGNAL(hovered(QBarSet*,bool)), m_series, SIGNAL(hovered(QBarSet*,bool)));
89 m_layout.append(QRectF(0, 0, 0, 0));
88 m_layout.append(QRectF(0, 0, 0, 0));
90
89
91 // Labels
90 // Labels
92 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
91 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
93 label->setVisible(labelsVisible);
92 label->setVisible(labelsVisible);
94 m_labels.append(label);
93 m_labels.append(label);
95 }
94 }
96 }
95 }
97
96
98 // TODO: Is this the right place to call it?
97 // TODO: Is this the right place to call it?
99 // presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
98 // presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series));
100 }
99 }
101
100
102 QVector<QRectF> BarChartItem::calculateLayout()
101 QVector<QRectF> BarChartItem::calculateLayout()
103 {
102 {
104 QVector<QRectF> layout;
103 QVector<QRectF> layout;
105
104
106 // Use temporary qreals for accurancy
105 // Use temporary qreals for accurancy
107 qreal categoryCount = m_series->categoryCount();
106 qreal categoryCount = m_series->categoryCount();
108 qreal setCount = m_series->barsetCount();
107 qreal setCount = m_series->barsetCount();
109
108
110 // Domain:
109 // Domain:
111 qreal width = geometry().width();
110 qreal width = geometry().width();
112 qreal height = geometry().height();
111 qreal height = geometry().height();
113 qreal rangeY = m_domainMaxY - m_domainMinY;
112 qreal rangeY = m_domainMaxY - m_domainMinY;
114 qreal rangeX = m_domainMaxX - m_domainMinX;
113 qreal rangeX = m_domainMaxX - m_domainMinX;
115 qreal scaleY = (height / rangeY);
114 qreal scaleY = (height / rangeY);
116 qreal scaleX = (width / rangeX);
115 qreal scaleX = (width / rangeX);
117 qreal categoryWidth = width / categoryCount;
116 qreal categoryWidth = width / categoryCount;
118 qreal barWidth = categoryWidth / setCount - categoryWidth * m_series->d_func()->barMargin();
117 qreal barWidth = categoryWidth / setCount - categoryWidth * m_series->d_func()->barMargin();
119
118
120 int itemIndex(0);
119 int itemIndex(0);
121 for (int category = 0; category < categoryCount; category++) {
120 for (int category = 0; category < categoryCount; category++) {
122 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
121 qreal yPos = height + scaleY * m_domainMinY + geometry().topLeft().y();
123 for (int set = 0; set < setCount; set++) {
122 for (int set = 0; set < setCount; set++) {
124 QBarSet* barSet = m_series->d_func()->barsetAt(set);
123 QBarSet* barSet = m_series->d_func()->barsetAt(set);
125 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
124 qreal xPos = (barSet->at(category).x() - m_domainMinX) * scaleX + m_rect.left() - barWidth/2;
126 qreal barHeight = barSet->at(category).y() * scaleY;
125 qreal barHeight = barSet->at(category).y() * scaleY;
127
126
128 Bar* bar = m_bars.at(itemIndex);
127 Bar* bar = m_bars.at(itemIndex);
129 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
128 QRectF rect(xPos, yPos - barHeight, barWidth, barHeight);
130
129
131 layout.append(rect);
130 layout.append(rect);
132 bar->setPen(barSet->pen());
131 bar->setPen(barSet->pen());
133 bar->setBrush(barSet->brush());
132 bar->setBrush(barSet->brush());
134
133
135 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
134 QGraphicsSimpleTextItem* label = m_labels.at(itemIndex);
136
135
137 if (!qFuzzyIsNull(barSet->at(category).y())) {
136 if (!qFuzzyIsNull(barSet->at(category).y())) {
138 label->setText(QString::number(barSet->at(category).y()));
137 label->setText(QString::number(barSet->at(category).y()));
139 } else {
138 } else {
140 label->setText(QString(""));
139 label->setText(QString(""));
141 }
140 }
142
141
143 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
142 label->setPos(xPos + (rect.width()/2 - label->boundingRect().width()/2)
144 ,yPos - barHeight/2 - label->boundingRect().height()/2);
143 ,yPos - barHeight/2 - label->boundingRect().height()/2);
145 label->setFont(barSet->labelFont());
144 label->setFont(barSet->labelFont());
146
145
147 itemIndex++;
146 itemIndex++;
148 }
147 }
149 }
148 }
150
149
151 return layout;
150 return layout;
152 }
151 }
153
152
154 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
153 void BarChartItem::applyLayout(const QVector<QRectF> &layout)
155 {
154 {
156 if (animator())
155 if (animator())
157 animator()->updateLayout(this, m_layout, layout);
156 animator()->updateLayout(this, m_layout, layout);
158 else
157 else
159 setLayout(layout);
158 setLayout(layout);
160 }
159 }
161
160
162 void BarChartItem::setLayout(const QVector<QRectF> &layout)
161 void BarChartItem::setLayout(const QVector<QRectF> &layout)
163 {
162 {
164 m_layout = layout;
163 m_layout = layout;
165
164
166 for (int i=0; i < m_bars.count(); i++)
165 for (int i=0; i < m_bars.count(); i++)
167 m_bars.at(i)->setRect(layout.at(i));
166 m_bars.at(i)->setRect(layout.at(i));
168
167
169 update();
168 update();
170 }
169 }
171 //handlers
170 //handlers
172
171
173 void BarChartItem::handleModelChanged()
172 void BarChartItem::handleModelChanged()
174 {
173 {
175 // dataChanged();
174 // dataChanged();
176 presenter()->resetAllElements();
175 presenter()->resetAllElements();
177 }
176 }
178
177
179 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
178 void BarChartItem::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY)
180 {
179 {
181 m_domainMinX = minX;
180 m_domainMinX = minX;
182 m_domainMaxX = maxX;
181 m_domainMaxX = maxX;
183 m_domainMinY = minY;
182 m_domainMinY = minY;
184 m_domainMaxY = maxY;
183 m_domainMaxY = maxY;
185 handleLayoutChanged();
184 handleLayoutChanged();
186 }
185 }
187
186
188 void BarChartItem::handleGeometryChanged(const QRectF &rect)
187 void BarChartItem::handleGeometryChanged(const QRectF &rect)
189 {
188 {
190 prepareGeometryChange();
189 prepareGeometryChange();
191 m_rect = rect;
190 m_rect = rect;
192 handleLayoutChanged();
191 handleLayoutChanged();
193 m_layoutSet = true;
194 }
192 }
195
193
196 void BarChartItem::handleLayoutChanged()
194 void BarChartItem::handleLayoutChanged()
197 {
195 {
198 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
196 if ((m_rect.width() <= 0) || (m_rect.height() <= 0)) {
199 // rect size zero.
197 // rect size zero.
200 return;
198 return;
201 }
199 }
202 QVector<QRectF> layout = calculateLayout();
200 QVector<QRectF> layout = calculateLayout();
203 applyLayout(layout);
201 applyLayout(layout);
204 update();
202 update();
205 }
203 }
206
204
207 void BarChartItem::labelsVisibleChanged(bool visible)
205 void BarChartItem::labelsVisibleChanged(bool visible)
208 {
206 {
209 foreach (QGraphicsSimpleTextItem* label, m_labels) {
207 foreach (QGraphicsSimpleTextItem* label, m_labels) {
210 label->setVisible(visible);
208 label->setVisible(visible);
211 }
209 }
212 update();
210 update();
213 }
211 }
214
212
215 #include "moc_barchartitem_p.cpp"
213 #include "moc_barchartitem_p.cpp"
216
214
217 QTCOMMERCIALCHART_END_NAMESPACE
215 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,84 +1,83
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 #ifndef BARCHARTITEM_H
21 #ifndef BARCHARTITEM_H
22 #define BARCHARTITEM_H
22 #define BARCHARTITEM_H
23
23
24 #include "chartitem_p.h"
24 #include "chartitem_p.h"
25 #include "qbarseries.h"
25 #include "qbarseries.h"
26 #include <QPen>
26 #include <QPen>
27 #include <QBrush>
27 #include <QBrush>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 class Bar;
31 class Bar;
32 class QAxisCategories;
32 class QAxisCategories;
33 class QChart;
33 class QChart;
34
34
35 //typedef QVector<QRectF> BarLayout;
35 //typedef QVector<QRectF> BarLayout;
36
36
37 class BarChartItem : public ChartItem
37 class BarChartItem : public ChartItem
38 {
38 {
39 Q_OBJECT
39 Q_OBJECT
40 public:
40 public:
41 BarChartItem(QBarSeries *series, ChartPresenter *presenter);
41 BarChartItem(QBarSeries *series, ChartPresenter *presenter);
42 virtual ~BarChartItem();
42 virtual ~BarChartItem();
43
43
44 public:
44 public:
45 // From QGraphicsItem
45 // From QGraphicsItem
46 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
46 void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
47 QRectF boundingRect() const;
47 QRectF boundingRect() const;
48
48
49 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
49 virtual void dataChanged(); // data of series has changed -> need to recalculate bar sizes
50
50
51 virtual QVector<QRectF> calculateLayout();
51 virtual QVector<QRectF> calculateLayout();
52 void applyLayout(const QVector<QRectF> &layout);
52 void applyLayout(const QVector<QRectF> &layout);
53 void setLayout(const QVector<QRectF> &layout);
53 void setLayout(const QVector<QRectF> &layout);
54 void updateLayout(const QVector<QRectF> &layout);
54 void updateLayout(const QVector<QRectF> &layout);
55
55
56 QRectF geometry() const { return m_rect;}
56 QRectF geometry() const { return m_rect;}
57
57
58 public Q_SLOTS:
58 public Q_SLOTS:
59 void handleModelChanged();
59 void handleModelChanged();
60 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
60 void handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY);
61 void handleGeometryChanged(const QRectF &size);
61 void handleGeometryChanged(const QRectF &size);
62 void handleLayoutChanged();
62 void handleLayoutChanged();
63 void labelsVisibleChanged(bool visible);
63 void labelsVisibleChanged(bool visible);
64
64
65 protected:
65 protected:
66
66
67 qreal m_domainMinX;
67 qreal m_domainMinX;
68 qreal m_domainMaxX;
68 qreal m_domainMaxX;
69 qreal m_domainMinY;
69 qreal m_domainMinY;
70 qreal m_domainMaxY;
70 qreal m_domainMaxY;
71
71
72 QRectF m_rect;
72 QRectF m_rect;
73 bool m_layoutSet; // True, if component has been laid out.
74 QVector<QRectF> m_layout;
73 QVector<QRectF> m_layout;
75
74
76 // Not owned.
75 // Not owned.
77 QBarSeries *m_series;
76 QBarSeries *m_series;
78 QList<Bar *> m_bars;
77 QList<Bar *> m_bars;
79 QList<QGraphicsSimpleTextItem *> m_labels;
78 QList<QGraphicsSimpleTextItem *> m_labels;
80 };
79 };
81
80
82 QTCOMMERCIALCHART_END_NAMESPACE
81 QTCOMMERCIALCHART_END_NAMESPACE
83
82
84 #endif // BARCHARTITEM_H
83 #endif // BARCHARTITEM_H
General Comments 0
You need to be logged in to leave comments. Login now