##// 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 3 ** Copyright (C) 2012 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 6 **
7 7 ** This file is part of the Qt Commercial Charts Add-on.
8 8 **
9 9 ** $QT_BEGIN_LICENSE$
10 10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 11 ** accordance with the Qt Commercial License Agreement provided with the
12 12 ** Software or, alternatively, in accordance with the terms contained in
13 13 ** a written agreement between you and Digia.
14 14 **
15 15 ** If you have questions regarding the use of this file, please use
16 16 ** contact form at http://qt.digia.com
17 17 ** $QT_END_LICENSE$
18 18 **
19 19 ****************************************************************************/
20 20
21 21 #include "abstractbarchartitem_p.h"
22 22 #include "bar_p.h"
23 23 #include "qbarset.h"
24 24 #include "qbarset_p.h"
25 25 #include "qabstractbarseries.h"
26 26 #include "qabstractbarseries_p.h"
27 27 #include "qchart.h"
28 28 #include "chartpresenter_p.h"
29 29 #include "charttheme_p.h"
30 30 #include "abstractbaranimation_p.h"
31 31 #include "chartdataset_p.h"
32 32 #include <QPainter>
33 33
34 34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 35
36 36 AbstractBarChartItem::AbstractBarChartItem(QAbstractBarSeries *series, QGraphicsItem* item) :
37 37 ChartItem(series->d_func(),item),
38 38 m_animation(0),
39 39 m_series(series)
40 40 {
41 41
42 42 setFlag(ItemClipsChildrenToShape);
43 43 connect(series->d_func(), SIGNAL(updatedLayout()), this, SLOT(handleLayoutChanged()));
44 44 connect(series->d_func(), SIGNAL(updatedBars()), this, SLOT(handleUpdatedBars()));
45 45 connect(series->d_func(), SIGNAL(labelsVisibleChanged(bool)), this, SLOT(handleLabelsVisibleChanged(bool)));
46 46 connect(series->d_func(), SIGNAL(restructuredBars()), this, SLOT(handleDataStructureChanged()));
47 47 connect(series, SIGNAL(visibleChanged()), this, SLOT(handleVisibleChanged()));
48 48 connect(series, SIGNAL(opacityChanged()), this, SLOT(handleOpacityChanged()));
49 49 setZValue(ChartPresenter::BarSeriesZValue);
50 50 handleDataStructureChanged();
51 51 handleVisibleChanged();
52 52 handleUpdatedBars();
53 53 }
54 54
55 55 AbstractBarChartItem::~AbstractBarChartItem()
56 56 {
57 57 }
58 58
59 59 void AbstractBarChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
60 60 {
61 61 Q_UNUSED(painter);
62 62 Q_UNUSED(option);
63 63 Q_UNUSED(widget);
64 64 }
65 65
66 66 QRectF AbstractBarChartItem::boundingRect() const
67 67 {
68 68 return m_rect;
69 69 }
70 70
71 71 void AbstractBarChartItem::applyLayout(const QVector<QRectF> &layout)
72 72 {
73 73 if (m_animation) {
74 74 m_animation->setup(m_layout, layout);
75 75 presenter()->startAnimation(m_animation);
76 76 } else {
77 77 setLayout(layout);
78 78 update();
79 79 }
80 80 }
81 81
82 82 void AbstractBarChartItem::setAnimation(AbstractBarAnimation *animation)
83 83 {
84 84 m_animation = animation;
85 85 }
86 86
87 87 void AbstractBarChartItem::setLayout(const QVector<QRectF> &layout)
88 88 {
89 89 if (layout.count() != m_bars.count())
90 90 return;
91 91
92 92 m_layout = layout;
93 93
94 94 for (int i = 0; i < m_bars.count(); i++) {
95 95 m_bars.at(i)->setRect(layout.at(i));
96 96 QGraphicsSimpleTextItem *label = m_labels.at(i);
97 97 label->setPos(layout.at(i).center() - label->boundingRect().center());
98 98
99 99 }
100 100 }
101 101 //handlers
102 102
103 103 void AbstractBarChartItem::handleDomainUpdated()
104 104 {
105 105 m_domainMinX = domain()->minX();
106 106 m_domainMaxX = domain()->maxX();
107 107 m_domainMinY = domain()->minY();
108 108 m_domainMaxY = domain()->maxY();
109 109
110 110 QRectF rect(QPointF(0,0),domain()->size());
111 111
112 112 if(m_rect != rect){
113 113 prepareGeometryChange();
114 114 m_rect = rect;
115 115 }
116 116
117 117 handleLayoutChanged();
118 118 }
119 119
120 120 void AbstractBarChartItem::handleLayoutChanged()
121 121 {
122 122 if ((m_rect.width() <= 0) || (m_rect.height() <= 0))
123 123 return; // rect size zero.
124 124 QVector<QRectF> layout = calculateLayout();
125 125 applyLayout(layout);
126 handleUpdatedBars();
126 127 }
127 128
128 129 void AbstractBarChartItem::handleLabelsVisibleChanged(bool visible)
129 130 {
130 131 foreach (QGraphicsSimpleTextItem *label, m_labels)
131 132 label->setVisible(visible);
132 133 update();
133 134 }
134 135
135 136 void AbstractBarChartItem::handleDataStructureChanged()
136 137 {
137 138 foreach (QGraphicsItem *item, childItems())
138 139 delete item;
139 140
140 141 m_bars.clear();
141 142 m_labels.clear();
142 143 m_layout.clear();
143 144
144 bool labelsVisible = m_series->isLabelsVisible();
145
146 145 // Create new graphic items for bars
147 146 for (int c = 0; c < m_series->d_func()->categoryCount(); c++) {
148 147 for (int s = 0; s < m_series->count(); s++) {
149 148 QBarSet *set = m_series->d_func()->barsetAt(s);
150 149
151 150 // Bars
152 151 Bar *bar = new Bar(set, c, this);
153 152 m_bars.append(bar);
154 153 connect(bar, SIGNAL(clicked(int,QBarSet*)), m_series, SIGNAL(clicked(int,QBarSet*)));
155 154 connect(bar, SIGNAL(hovered(bool,QBarSet*)), m_series, SIGNAL(hovered(bool,QBarSet*)));
156 155 connect(bar, SIGNAL(clicked(int,QBarSet*)), set, SIGNAL(clicked(int)));
157 156 connect(bar, SIGNAL(hovered(bool,QBarSet*)), set, SIGNAL(hovered(bool)));
158 157 m_layout.append(QRectF(0, 0, 0, 0));
159 158
160 159 // Labels
161 QGraphicsSimpleTextItem *label = new QGraphicsSimpleTextItem(this);
162 label->setVisible(labelsVisible);
163 m_labels.append(label);
160 m_labels.append(new QGraphicsSimpleTextItem(this));
164 161 }
165 162 }
166 163
167 164 if(themeManager()) themeManager()->updateSeries(m_series);
168 165 handleLayoutChanged();
166 handleVisibleChanged();
169 167 }
170 168
171 169 void AbstractBarChartItem::handleVisibleChanged()
172 170 {
173 171 bool visible = m_series->isVisible();
174 172 if (visible)
175 173 handleLabelsVisibleChanged(m_series->isLabelsVisible());
176 174 else
177 175 handleLabelsVisibleChanged(visible);
178 176
179 177 foreach (QGraphicsItem *bar, m_bars)
180 178 bar->setVisible(visible);
181 179 }
182 180
183 181 void AbstractBarChartItem::handleOpacityChanged()
184 182 {
185 183 foreach (QGraphicsItem *item, childItems())
186 184 item->setOpacity(m_series->opacity());
187 185 }
188 186
189 187 void AbstractBarChartItem::handleUpdatedBars()
190 188 {
191 189 // Handle changes in pen, brush, labels etc.
192 190 int categoryCount = m_series->d_func()->categoryCount();
193 191 int setCount = m_series->count();
194 192 int itemIndex(0);
195 193
196 194 for (int category = 0; category < categoryCount; category++) {
197 195 for (int set = 0; set < setCount; set++) {
198 196 QBarSetPrivate *barSet = m_series->d_func()->barsetAt(set)->d_ptr.data();
199 197 Bar *bar = m_bars.at(itemIndex);
200 198 bar->setPen(barSet->m_pen);
201 199 bar->setBrush(barSet->m_brush);
202 200 bar->update();
203 201
204 202 QGraphicsSimpleTextItem *label = m_labels.at(itemIndex);
205 203 label->setText(QString("%1").arg(barSet->value(category)));
206 204 label->setFont(barSet->m_labelFont);
207 205 label->setBrush(barSet->m_labelBrush);
208 206 label->update();
209 207 itemIndex++;
210 208 }
211 209 }
212 210 }
213 211
214 212 #include "moc_abstractbarchartitem_p.cpp"
215 213
216 214 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now