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