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