##// END OF EJS Templates
barlabel visibility fix
sauimone -
r2309:178e3a519f4a
parent child
Show More
@@ -1,212 +1,216
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 "abstractbarchartitem_p.h"
21 #include "abstractbarchartitem_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 "qabstractbarseries.h"
25 #include "qabstractbarseries.h"
26 #include "qabstractbarseries_p.h"
26 #include "qabstractbarseries_p.h"
27 #include "qchart.h"
27 #include "qchart.h"
28 #include "chartpresenter_p.h"
28 #include "chartpresenter_p.h"
29 #include "charttheme_p.h"
29 #include "charttheme_p.h"
30 #include "abstractbaranimation_p.h"
30 #include "abstractbaranimation_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 AbstractBarChartItem::AbstractBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
36 AbstractBarChartItem::AbstractBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
37 ChartItem(series->d_func(),item),
37 ChartItem(series->d_func(),item),
38 m_animation(0),
38 m_animation(0),
39 m_series(series)
39 m_series(series)
40 {
40 {
41
41
42 setFlag(ItemClipsChildrenToShape);
42 setFlag(ItemClipsChildrenToShape);
43 connect(series->d_func(), SIGNAL(updatedLayout()), this, SLOT(handleLayoutChanged()));
43 connect(series->d_func(), SIGNAL(updatedLayout()), this, SLOT(handleLayoutChanged()));
44 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleUpdatedBars()));
44 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleUpdatedBars()));
45 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool)));
45 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool)));
46 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
46 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
47 connect(series, SIGNAL(visibleChanged()), this, SLOT(handleVisibleChanged()));
47 connect(series, SIGNAL(visibleChanged()), this, SLOT(handleVisibleChanged()));
48 connect(series, SIGNAL(opacityChanged()), this, SLOT(handleOpacityChanged()));
48 connect(series, SIGNAL(opacityChanged()), this, SLOT(handleOpacityChanged()));
49 setZValue(ChartPresenter::BarSeriesZValue);
49 setZValue(ChartPresenter::BarSeriesZValue);
50 handleDataStructureChanged();
50 handleDataStructureChanged();
51 handleVisibleChanged();
51 handleVisibleChanged();
52 handleUpdatedBars();
52 handleUpdatedBars();
53 }
53 }
54
54
55 AbstractBarChartItem::~AbstractBarChartItem()
55 AbstractBarChartItem::~AbstractBarChartItem()
56 {
56 {
57 }
57 }
58
58
59 void AbstractBarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
59 void AbstractBarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
60 {
60 {
61 Q_UNUSED(painter);
61 Q_UNUSED(painter);
62 Q_UNUSED(option);
62 Q_UNUSED(option);
63 Q_UNUSED(widget);
63 Q_UNUSED(widget);
64 }
64 }
65
65
66 QRectF AbstractBarChartItem::boundingRect() const
66 QRectF AbstractBarChartItem::boundingRect() const
67 {
67 {
68 return m_rect;
68 return m_rect;
69 }
69 }
70
70
71 void AbstractBarChartItem::applyLayout(const QVector<QRectF> &layout)
71 void AbstractBarChartItem::applyLayout(const QVector<QRectF> &layout)
72 {
72 {
73 if (m_animation) {
73 if (m_animation) {
74 m_animation->setup(m_layout, layout);
74 m_animation->setup(m_layout, layout);
75 presenter()->startAnimation(m_animation);
75 presenter()->startAnimation(m_animation);
76 } else {
76 } else {
77 setLayout(layout);
77 setLayout(layout);
78 update();
78 update();
79 }
79 }
80 }
80 }
81
81
82 void AbstractBarChartItem::setAnimation(AbstractBarAnimation *animation)
82 void AbstractBarChartItem::setAnimation(AbstractBarAnimation *animation)
83 {
83 {
84 m_animation = animation;
84 m_animation = animation;
85 }
85 }
86
86
87 void AbstractBarChartItem::setLayout(const QVector<QRectF> &layout)
87 void AbstractBarChartItem::setLayout(const QVector<QRectF> &layout)
88 {
88 {
89 if (layout.count() != m_bars.count())
89 if (layout.count() != m_bars.count())
90 return;
90 return;
91
91
92 m_layout = layout;
92 m_layout = layout;
93
93
94 for (int i = 0; i < m_bars.count(); i++) {
94 for (int i = 0; i < m_bars.count(); i++) {
95 m_bars.at(i)->setRect(layout.at(i));
95 m_bars.at(i)->setRect(layout.at(i));
96 QGraphicsSimpleTextItem *label = m_labels.at(i);
96 QGraphicsSimpleTextItem *label = m_labels.at(i);
97 label->setPos(layout.at(i).center() - label->boundingRect().center());
97 label->setPos(layout.at(i).center() - label->boundingRect().center());
98
98
99 }
99 }
100 }
100 }
101 //handlers
101 //handlers
102
102
103 void AbstractBarChartItem::handleDomainUpdated()
103 void AbstractBarChartItem::handleDomainUpdated()
104 {
104 {
105 m_domainMinX = domain()->minX();
105 m_domainMinX = domain()->minX();
106 m_domainMaxX = domain()->maxX();
106 m_domainMaxX = domain()->maxX();
107 m_domainMinY = domain()->minY();
107 m_domainMinY = domain()->minY();
108 m_domainMaxY = domain()->maxY();
108 m_domainMaxY = domain()->maxY();
109
109
110 QRectF rect(QPointF(0,0),domain()->size());
110 QRectF rect(QPointF(0,0),domain()->size());
111
111
112 if(m_rect != rect){
112 if(m_rect != rect){
113 prepareGeometryChange();
113 prepareGeometryChange();
114 m_rect = rect;
114 m_rect = rect;
115 }
115 }
116
116
117 handleLayoutChanged();
117 handleLayoutChanged();
118 }
118 }
119
119
120 void AbstractBarChartItem::handleLayoutChanged()
120 void AbstractBarChartItem::handleLayoutChanged()
121 {
121 {
122 if ((m_rect.width() <= 0) || (m_rect.height() <= 0))
122 if ((m_rect.width() <= 0) || (m_rect.height() <= 0))
123 return; // rect size zero.
123 return; // rect size zero.
124 QVector<QRectF> layout = calculateLayout();
124 QVector<QRectF> layout = calculateLayout();
125 applyLayout(layout);
125 applyLayout(layout);
126 }
126 }
127
127
128 void AbstractBarChartItem::handleLabelsVisibleChanged(bool visible)
128 void AbstractBarChartItem::handleLabelsVisibleChanged(bool visible)
129 {
129 {
130 foreach (QGraphicsSimpleTextItem *label, m_labels)
130 foreach (QGraphicsSimpleTextItem *label, m_labels)
131 label->setVisible(visible);
131 label->setVisible(visible);
132 update();
132 update();
133 }
133 }
134
134
135 void AbstractBarChartItem::handleDataStructureChanged()
135 void AbstractBarChartItem::handleDataStructureChanged()
136 {
136 {
137 foreach (QGraphicsItem *item, childItems())
137 foreach (QGraphicsItem *item, childItems())
138 delete item;
138 delete item;
139
139
140 m_bars.clear();
140 m_bars.clear();
141 m_labels.clear();
141 m_labels.clear();
142 m_layout.clear();
142 m_layout.clear();
143
143
144 bool labelsVisible = m_series->isLabelsVisible();
144 bool labelsVisible = m_series->isLabelsVisible();
145
145
146 // Create new graphic items for bars
146 // Create new graphic items for bars
147 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
147 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
148 for (int s = 0; s < m_series->count(); s++) {
148 for (int s = 0; s < m_series->count(); s++) {
149 QBarSet *set = m_series->d_func()->barsetAt(s);
149 QBarSet *set = m_series->d_func()->barsetAt(s);
150
150
151 // Bars
151 // Bars
152 Bar *bar = new Bar(set, c, this);
152 Bar *bar = new Bar(set, c, this);
153 m_bars.append(bar);
153 m_bars.append(bar);
154 connect(bar, SIGNAL(clicked(int,QBarSet*)), m_series, SIGNAL(clicked(int,QBarSet*)));
154 connect(bar, SIGNAL(clicked(int,QBarSet*)), m_series, SIGNAL(clicked(int,QBarSet*)));
155 connect(bar, SIGNAL(hovered(bool,QBarSet*)), m_series, SIGNAL(hovered(bool,QBarSet*)));
155 connect(bar, SIGNAL(hovered(bool,QBarSet*)), m_series, SIGNAL(hovered(bool,QBarSet*)));
156 connect(bar, SIGNAL(clicked(int,QBarSet*)), set, SIGNAL(clicked(int)));
156 connect(bar, SIGNAL(clicked(int,QBarSet*)), set, SIGNAL(clicked(int)));
157 connect(bar, SIGNAL(hovered(bool,QBarSet*)), set, SIGNAL(hovered(bool)));
157 connect(bar, SIGNAL(hovered(bool,QBarSet*)), set, SIGNAL(hovered(bool)));
158 m_layout.append(QRectF(0, 0, 0, 0));
158 m_layout.append(QRectF(0, 0, 0, 0));
159
159
160 // Labels
160 // Labels
161 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
161 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
162 label->setVisible(labelsVisible);
162 label->setVisible(labelsVisible);
163 m_labels.append(label);
163 m_labels.append(label);
164 }
164 }
165 }
165 }
166
166
167 if(themeManager()) themeManager()->updateSeries(m_series);
167 if(themeManager()) themeManager()->updateSeries(m_series);
168 handleLayoutChanged();
168 handleLayoutChanged();
169 }
169 }
170
170
171 void AbstractBarChartItem::handleVisibleChanged()
171 void AbstractBarChartItem::handleVisibleChanged()
172 {
172 {
173 bool visible = m_series->isVisible();
173 bool visible = m_series->isVisible();
174 handleLabelsVisibleChanged(visible);
174 if (visible)
175 foreach (QGraphicsItem *item, childItems())
175 handleLabelsVisibleChanged(m_series->isLabelsVisible());
176 item->setVisible(visible);
176 else
177 handleLabelsVisibleChanged(visible);
178
179 foreach (QGraphicsItem *bar, m_bars)
180 bar->setVisible(visible);
177 }
181 }
178
182
179 void AbstractBarChartItem::handleOpacityChanged()
183 void AbstractBarChartItem::handleOpacityChanged()
180 {
184 {
181 foreach (QGraphicsItem *item, childItems())
185 foreach (QGraphicsItem *item, childItems())
182 item->setOpacity(m_series->opacity());
186 item->setOpacity(m_series->opacity());
183 }
187 }
184
188
185 void AbstractBarChartItem::handleUpdatedBars()
189 void AbstractBarChartItem::handleUpdatedBars()
186 {
190 {
187 // Handle changes in pen, brush, labels etc.
191 // Handle changes in pen, brush, labels etc.
188 int categoryCount = m_series->d_func()->categoryCount();
192 int categoryCount = m_series->d_func()->categoryCount();
189 int setCount = m_series->count();
193 int setCount = m_series->count();
190 int itemIndex(0);
194 int itemIndex(0);
191
195
192 for (int category = 0; category < categoryCount; category++) {
196 for (int category = 0; category < categoryCount; category++) {
193 for (int set = 0; set < setCount; set++) {
197 for (int set = 0; set < setCount; set++) {
194 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
198 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
195 Bar *bar = m_bars.at(itemIndex);
199 Bar *bar = m_bars.at(itemIndex);
196 bar->setPen(barSet->m_pen);
200 bar->setPen(barSet->m_pen);
197 bar->setBrush(barSet->m_brush);
201 bar->setBrush(barSet->m_brush);
198 bar->update();
202 bar->update();
199
203
200 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
204 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
201 label->setText(QString("%1").arg(barSet->value(category)));
205 label->setText(QString("%1").arg(barSet->value(category)));
202 label->setFont(barSet->m_labelFont);
206 label->setFont(barSet->m_labelFont);
203 label->setBrush(barSet->m_labelBrush);
207 label->setBrush(barSet->m_labelBrush);
204 label->update();
208 label->update();
205 itemIndex++;
209 itemIndex++;
206 }
210 }
207 }
211 }
208 }
212 }
209
213
210 #include "moc_abstractbarchartitem_p.cpp"
214 #include "moc_abstractbarchartitem_p.cpp"
211
215
212 QTCOMMERCIALCHART_END_NAMESPACE
216 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now