##// END OF EJS Templates
BarChart brush and labels fix
Marek Rosa -
r2311:4de64fb010d7
parent child
Show More
@@ -1,216 +1,214
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 handleUpdatedBars();
126 }
127 }
127
128
128 void AbstractBarChartItem::handleLabelsVisibleChanged(bool visible)
129 void AbstractBarChartItem::handleLabelsVisibleChanged(bool visible)
129 {
130 {
130 foreach (QGraphicsSimpleTextItem *label, m_labels)
131 foreach (QGraphicsSimpleTextItem *label, m_labels)
131 label->setVisible(visible);
132 label->setVisible(visible);
132 update();
133 update();
133 }
134 }
134
135
135 void AbstractBarChartItem::handleDataStructureChanged()
136 void AbstractBarChartItem::handleDataStructureChanged()
136 {
137 {
137 foreach (QGraphicsItem *item, childItems())
138 foreach (QGraphicsItem *item, childItems())
138 delete item;
139 delete item;
139
140
140 m_bars.clear();
141 m_bars.clear();
141 m_labels.clear();
142 m_labels.clear();
142 m_layout.clear();
143 m_layout.clear();
143
144
144 bool labelsVisible = m_series->isLabelsVisible();
145
146 // Create new graphic items for bars
145 // Create new graphic items for bars
147 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
146 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
148 for (int s = 0; s < m_series->count(); s++) {
147 for (int s = 0; s < m_series->count(); s++) {
149 QBarSet *set = m_series->d_func()->barsetAt(s);
148 QBarSet *set = m_series->d_func()->barsetAt(s);
150
149
151 // Bars
150 // Bars
152 Bar *bar = new Bar(set, c, this);
151 Bar *bar = new Bar(set, c, this);
153 m_bars.append(bar);
152 m_bars.append(bar);
154 connect(bar, SIGNAL(clicked(int,QBarSet*)), m_series, SIGNAL(clicked(int,QBarSet*)));
153 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*)));
154 connect(bar, SIGNAL(hovered(bool,QBarSet*)), m_series, SIGNAL(hovered(bool,QBarSet*)));
156 connect(bar, SIGNAL(clicked(int,QBarSet*)), set, SIGNAL(clicked(int)));
155 connect(bar, SIGNAL(clicked(int,QBarSet*)), set, SIGNAL(clicked(int)));
157 connect(bar, SIGNAL(hovered(bool,QBarSet*)), set, SIGNAL(hovered(bool)));
156 connect(bar, SIGNAL(hovered(bool,QBarSet*)), set, SIGNAL(hovered(bool)));
158 m_layout.append(QRectF(0, 0, 0, 0));
157 m_layout.append(QRectF(0, 0, 0, 0));
159
158
160 // Labels
159 // Labels
161 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
160 m_labels.append(new QGraphicsSimpleTextItem(this));
162 label->setVisible(labelsVisible);
163 m_labels.append(label);
164 }
161 }
165 }
162 }
166
163
167 if(themeManager()) themeManager()->updateSeries(m_series);
164 if(themeManager()) themeManager()->updateSeries(m_series);
168 handleLayoutChanged();
165 handleLayoutChanged();
166 handleVisibleChanged();
169 }
167 }
170
168
171 void AbstractBarChartItem::handleVisibleChanged()
169 void AbstractBarChartItem::handleVisibleChanged()
172 {
170 {
173 bool visible = m_series->isVisible();
171 bool visible = m_series->isVisible();
174 if (visible)
172 if (visible)
175 handleLabelsVisibleChanged(m_series->isLabelsVisible());
173 handleLabelsVisibleChanged(m_series->isLabelsVisible());
176 else
174 else
177 handleLabelsVisibleChanged(visible);
175 handleLabelsVisibleChanged(visible);
178
176
179 foreach (QGraphicsItem *bar, m_bars)
177 foreach (QGraphicsItem *bar, m_bars)
180 bar->setVisible(visible);
178 bar->setVisible(visible);
181 }
179 }
182
180
183 void AbstractBarChartItem::handleOpacityChanged()
181 void AbstractBarChartItem::handleOpacityChanged()
184 {
182 {
185 foreach (QGraphicsItem *item, childItems())
183 foreach (QGraphicsItem *item, childItems())
186 item->setOpacity(m_series->opacity());
184 item->setOpacity(m_series->opacity());
187 }
185 }
188
186
189 void AbstractBarChartItem::handleUpdatedBars()
187 void AbstractBarChartItem::handleUpdatedBars()
190 {
188 {
191 // Handle changes in pen, brush, labels etc.
189 // Handle changes in pen, brush, labels etc.
192 int categoryCount = m_series->d_func()->categoryCount();
190 int categoryCount = m_series->d_func()->categoryCount();
193 int setCount = m_series->count();
191 int setCount = m_series->count();
194 int itemIndex(0);
192 int itemIndex(0);
195
193
196 for (int category = 0; category < categoryCount; category++) {
194 for (int category = 0; category < categoryCount; category++) {
197 for (int set = 0; set < setCount; set++) {
195 for (int set = 0; set < setCount; set++) {
198 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
196 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
199 Bar *bar = m_bars.at(itemIndex);
197 Bar *bar = m_bars.at(itemIndex);
200 bar->setPen(barSet->m_pen);
198 bar->setPen(barSet->m_pen);
201 bar->setBrush(barSet->m_brush);
199 bar->setBrush(barSet->m_brush);
202 bar->update();
200 bar->update();
203
201
204 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
202 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
205 label->setText(QString("%1").arg(barSet->value(category)));
203 label->setText(QString("%1").arg(barSet->value(category)));
206 label->setFont(barSet->m_labelFont);
204 label->setFont(barSet->m_labelFont);
207 label->setBrush(barSet->m_labelBrush);
205 label->setBrush(barSet->m_labelBrush);
208 label->update();
206 label->update();
209 itemIndex++;
207 itemIndex++;
210 }
208 }
211 }
209 }
212 }
210 }
213
211
214 #include "moc_abstractbarchartitem_p.cpp"
212 #include "moc_abstractbarchartitem_p.cpp"
215
213
216 QTCOMMERCIALCHART_END_NAMESPACE
214 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now