##// END OF EJS Templates
minor. compilation fix
Michal Klocek -
r332:744bdb6b6f83
parent child
Show More
@@ -1,335 +1,335
1 #include "axisitem_p.h"
1 #include "axisitem_p.h"
2 #include "qchartaxis.h"
2 #include "qchartaxis.h"
3 #include "chartpresenter_p.h"
3 #include "chartpresenter_p.h"
4 #include <QPainter>
4 #include <QPainter>
5 #include <QDebug>
5 #include <QDebug>
6
6
7 static int label_padding = 5;
7 static int label_padding = 5;
8
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
10
11 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) :
11 AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) :
12 ChartItem(parent),
12 ChartItem(parent),
13 m_type(type),
13 m_type(type),
14 m_labelsAngle(0),
14 m_labelsAngle(0),
15 m_grid(parent),
15 m_grid(parent),
16 m_shades(parent),
16 m_shades(parent),
17 m_labels(parent),
17 m_labels(parent),
18 m_axis(parent)
18 m_axis(parent)
19 {
19 {
20 //initial initialization
20 //initial initialization
21 m_axis.setZValue(ChartPresenter::AxisZValue);
21 m_axis.setZValue(ChartPresenter::AxisZValue);
22 m_shades.setZValue(ChartPresenter::ShadesZValue);
22 m_shades.setZValue(ChartPresenter::ShadesZValue);
23 m_grid.setZValue(ChartPresenter::GridZValue);
23 m_grid.setZValue(ChartPresenter::GridZValue);
24 setFlags(QGraphicsItem::ItemHasNoContents);
24 setFlags(QGraphicsItem::ItemHasNoContents);
25 }
25 }
26
26
27 AxisItem::~AxisItem()
27 AxisItem::~AxisItem()
28 {
28 {
29 }
29 }
30
30
31 QRectF AxisItem::boundingRect() const
31 QRectF AxisItem::boundingRect() const
32 {
32 {
33 return QRectF();
33 return QRectF();
34 }
34 }
35
35
36 void AxisItem::createItems(int count)
36 void AxisItem::createItems(int count)
37 {
37 {
38 if(m_axis.children().size()==0)
38 if(m_axis.children().size()==0)
39 m_axis.addToGroup(new QGraphicsLineItem());
39 m_axis.addToGroup(new QGraphicsLineItem());
40 for (int i = 0; i < count; ++i) {
40 for (int i = 0; i < count; ++i) {
41 m_grid.addToGroup(new QGraphicsLineItem());
41 m_grid.addToGroup(new QGraphicsLineItem());
42 m_labels.addToGroup(new QGraphicsSimpleTextItem());
42 m_labels.addToGroup(new QGraphicsSimpleTextItem());
43 if(m_grid.childItems().size()%2) m_shades.addToGroup(new QGraphicsRectItem());
43 if(m_grid.childItems().size()%2) m_shades.addToGroup(new QGraphicsRectItem());
44 m_axis.addToGroup(new QGraphicsLineItem());
44 m_axis.addToGroup(new QGraphicsLineItem());
45 }
45 }
46 }
46 }
47
47
48 void AxisItem::clear(int count)
48 void AxisItem::clear(int count)
49 {
49 {
50 QList<QGraphicsItem *> lines = m_grid.childItems();
50 QList<QGraphicsItem *> lines = m_grid.childItems();
51 QList<QGraphicsItem *> labels = m_labels.childItems();
51 QList<QGraphicsItem *> labels = m_labels.childItems();
52 QList<QGraphicsItem *> shades = m_shades.childItems();
52 QList<QGraphicsItem *> shades = m_shades.childItems();
53 QList<QGraphicsItem *> axis = m_axis.childItems();
53 QList<QGraphicsItem *> axis = m_axis.childItems();
54
54
55 for (int i = 0; i < count; ++i) {
55 for (int i = 0; i < count; ++i) {
56 delete(lines.takeLast());
56 delete(lines.takeLast());
57 delete(labels.takeLast());
57 delete(labels.takeLast());
58 if(lines.size()%2) delete(shades.takeLast());
58 if(lines.size()%2) delete(shades.takeLast());
59 delete(axis.takeLast());
59 delete(axis.takeLast());
60 }
60 }
61
61
62 m_thicksList.clear();
62 m_thicksList.clear();
63
63
64 }
64 }
65
65
66 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
66 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
67 {
67 {
68 Q_UNUSED(painter);
68 Q_UNUSED(painter);
69 Q_UNUSED(option);
69 Q_UNUSED(option);
70 Q_UNUSED(widget);
70 Q_UNUSED(widget);
71 }
71 }
72
72
73 void AxisItem::updateItems(QVector<qreal>& vector)
73 void AxisItem::updateItems(QVector<qreal>& vector)
74 {
74 {
75 calculateLayout(vector);
75 calculateLayout(vector);
76 if(vector.count()==0) return;
76 if(vector.count()==0) return;
77 applyLayout(vector);
77 applyLayout(vector);
78 }
78 }
79
79
80 void AxisItem::handleAxisUpdate(QChartAxis* axis)
80 void AxisItem::handleAxisUpdate(QChartAxis* axis)
81 {
81 {
82 if(m_layoutVector.count()==0) return;
82 if(m_layoutVector.count()==0) return;
83
83
84 if(axis->isAxisVisible()) {
84 if(axis->isAxisVisible()) {
85 setAxisOpacity(100);
85 setAxisOpacity(100);
86 }
86 }
87 else {
87 else {
88 setAxisOpacity(0);
88 setAxisOpacity(0);
89 }
89 }
90
90
91 if(axis->isGridVisible()) {
91 if(axis->isGridVisible()) {
92 setGridOpacity(100);
92 setGridOpacity(100);
93 }
93 }
94 else {
94 else {
95 setGridOpacity(0);
95 setGridOpacity(0);
96 }
96 }
97
97
98 if(axis->isLabelsVisible())
98 if(axis->isLabelsVisible())
99 {
99 {
100 setLabelsOpacity(100);
100 setLabelsOpacity(100);
101 }
101 }
102 else {
102 else {
103 setLabelsOpacity(0);
103 setLabelsOpacity(0);
104 }
104 }
105
105
106 if(axis->isShadesVisible()) {
106 if(axis->isShadesVisible()) {
107 setShadesOpacity(axis->shadesOpacity());
107 setShadesOpacity(axis->shadesOpacity());
108 }
108 }
109 else {
109 else {
110 setShadesOpacity(0);
110 setShadesOpacity(0);
111 }
111 }
112
112
113 setLabelsAngle(axis->labelsAngle());
113 setLabelsAngle(axis->labelsAngle());
114 setAxisPen(axis->axisPen());
114 setAxisPen(axis->axisPen());
115 setLabelsPen(axis->labelsPen());
115 setLabelsPen(axis->labelsPen());
116 setLabelsBrush(axis->labelsBrush());
116 setLabelsBrush(axis->labelsBrush());
117 setLabelsFont(axis->labelFont());
117 setLabelsFont(axis->labelsFont());
118 setGridPen(axis->gridPen());
118 setGridPen(axis->gridPen());
119 setShadesPen(axis->shadesPen());
119 setShadesPen(axis->shadesPen());
120 setShadesBrush(axis->shadesBrush());
120 setShadesBrush(axis->shadesBrush());
121 }
121 }
122
122
123 void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels)
123 void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels)
124 {
124 {
125 int diff = m_thicksList.size() - labels.size();
125 int diff = m_thicksList.size() - labels.size();
126
126
127 if(diff>0){
127 if(diff>0){
128 clear(diff);
128 clear(diff);
129 }else if(diff<0){
129 }else if(diff<0){
130 createItems(-diff);
130 createItems(-diff);
131 }
131 }
132 m_thicksList=labels;
132 m_thicksList=labels;
133 m_layoutVector.resize(m_thicksList.size());
133 m_layoutVector.resize(m_thicksList.size());
134 updateItems(m_layoutVector);
134 updateItems(m_layoutVector);
135 if(diff!=0) handleAxisUpdate(axis);
135 if(diff!=0) handleAxisUpdate(axis);
136 }
136 }
137
137
138 void AxisItem::handleGeometryChanged(const QRectF& rect)
138 void AxisItem::handleGeometryChanged(const QRectF& rect)
139 {
139 {
140 m_rect = rect;
140 m_rect = rect;
141 updateItems(m_layoutVector);
141 updateItems(m_layoutVector);
142 }
142 }
143
143
144 void AxisItem::setAxisOpacity(qreal opacity)
144 void AxisItem::setAxisOpacity(qreal opacity)
145 {
145 {
146 m_axis.setOpacity(opacity);
146 m_axis.setOpacity(opacity);
147 }
147 }
148
148
149 qreal AxisItem::axisOpacity() const
149 qreal AxisItem::axisOpacity() const
150 {
150 {
151 return m_axis.opacity();
151 return m_axis.opacity();
152 }
152 }
153
153
154 void AxisItem::setGridOpacity(qreal opacity)
154 void AxisItem::setGridOpacity(qreal opacity)
155 {
155 {
156 m_grid.setOpacity(opacity);
156 m_grid.setOpacity(opacity);
157 }
157 }
158
158
159 qreal AxisItem::gridOpacity() const
159 qreal AxisItem::gridOpacity() const
160 {
160 {
161 return m_grid.opacity();
161 return m_grid.opacity();
162 }
162 }
163
163
164 void AxisItem::setLabelsOpacity(qreal opacity)
164 void AxisItem::setLabelsOpacity(qreal opacity)
165 {
165 {
166 m_labels.setOpacity(opacity);
166 m_labels.setOpacity(opacity);
167 }
167 }
168
168
169 qreal AxisItem::labelsOpacity() const
169 qreal AxisItem::labelsOpacity() const
170 {
170 {
171 return m_labels.opacity();
171 return m_labels.opacity();
172 }
172 }
173
173
174 void AxisItem::setShadesOpacity(qreal opacity)
174 void AxisItem::setShadesOpacity(qreal opacity)
175 {
175 {
176 m_shades.setOpacity(opacity);
176 m_shades.setOpacity(opacity);
177 }
177 }
178
178
179 qreal AxisItem::shadesOpacity() const
179 qreal AxisItem::shadesOpacity() const
180 {
180 {
181 return m_shades.opacity();
181 return m_shades.opacity();
182 }
182 }
183
183
184 void AxisItem::setLabelsAngle(int angle)
184 void AxisItem::setLabelsAngle(int angle)
185 {
185 {
186 foreach(QGraphicsItem* item , m_labels.childItems()) {
186 foreach(QGraphicsItem* item , m_labels.childItems()) {
187 QPointF center = item->boundingRect().center();
187 QPointF center = item->boundingRect().center();
188 item->setRotation(angle);
188 item->setRotation(angle);
189 }
189 }
190
190
191 m_labelsAngle=angle;
191 m_labelsAngle=angle;
192 }
192 }
193
193
194 void AxisItem::setLabelsPen(const QPen& pen)
194 void AxisItem::setLabelsPen(const QPen& pen)
195 {
195 {
196 foreach(QGraphicsItem* item , m_labels.childItems()) {
196 foreach(QGraphicsItem* item , m_labels.childItems()) {
197 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
197 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
198 }
198 }
199 }
199 }
200
200
201 void AxisItem::setLabelsBrush(const QBrush& brush)
201 void AxisItem::setLabelsBrush(const QBrush& brush)
202 {
202 {
203 foreach(QGraphicsItem* item , m_labels.childItems()) {
203 foreach(QGraphicsItem* item , m_labels.childItems()) {
204 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
204 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
205 }
205 }
206 }
206 }
207
207
208 void AxisItem::setLabelsFont(const QFont& font)
208 void AxisItem::setLabelsFont(const QFont& font)
209 {
209 {
210 foreach(QGraphicsItem* item , m_labels.childItems()) {
210 foreach(QGraphicsItem* item , m_labels.childItems()) {
211 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
211 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
212 }
212 }
213 }
213 }
214
214
215 void AxisItem::setShadesBrush(const QBrush& brush)
215 void AxisItem::setShadesBrush(const QBrush& brush)
216 {
216 {
217 foreach(QGraphicsItem* item , m_shades.childItems()) {
217 foreach(QGraphicsItem* item , m_shades.childItems()) {
218 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
218 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
219 }
219 }
220 }
220 }
221
221
222 void AxisItem::setShadesPen(const QPen& pen)
222 void AxisItem::setShadesPen(const QPen& pen)
223 {
223 {
224 foreach(QGraphicsItem* item , m_shades.childItems()) {
224 foreach(QGraphicsItem* item , m_shades.childItems()) {
225 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
225 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
226 }
226 }
227 }
227 }
228
228
229 void AxisItem::setAxisPen(const QPen& pen)
229 void AxisItem::setAxisPen(const QPen& pen)
230 {
230 {
231 foreach(QGraphicsItem* item , m_axis.childItems()) {
231 foreach(QGraphicsItem* item , m_axis.childItems()) {
232 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
232 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
233 }
233 }
234 }
234 }
235
235
236 void AxisItem::setGridPen(const QPen& pen)
236 void AxisItem::setGridPen(const QPen& pen)
237 {
237 {
238 foreach(QGraphicsItem* item , m_grid.childItems()) {
238 foreach(QGraphicsItem* item , m_grid.childItems()) {
239 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
239 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
240 }
240 }
241 }
241 }
242
242
243 void AxisItem::calculateLayout(QVector<qreal>& points)
243 void AxisItem::calculateLayout(QVector<qreal>& points)
244 {
244 {
245 switch (m_type)
245 switch (m_type)
246 {
246 {
247 case X_AXIS:
247 case X_AXIS:
248 {
248 {
249 const qreal deltaX = m_rect.width()/(m_thicksList.size()-1);
249 const qreal deltaX = m_rect.width()/(m_thicksList.size()-1);
250 for (int i = 0; i < m_thicksList.size(); ++i) {
250 for (int i = 0; i < m_thicksList.size(); ++i) {
251 int x = i * deltaX + m_rect.left();
251 int x = i * deltaX + m_rect.left();
252 points[i]=x;
252 points[i]=x;
253 }
253 }
254 }
254 }
255 break;
255 break;
256 case Y_AXIS:
256 case Y_AXIS:
257 {
257 {
258 const qreal deltaY = m_rect.height()/(m_thicksList.size()-1);
258 const qreal deltaY = m_rect.height()/(m_thicksList.size()-1);
259 for (int i = 0; i < m_thicksList.size(); ++i) {
259 for (int i = 0; i < m_thicksList.size(); ++i) {
260 int y = i * -deltaY + m_rect.bottom();
260 int y = i * -deltaY + m_rect.bottom();
261 points[i]=y;
261 points[i]=y;
262 }
262 }
263 }
263 }
264 break;
264 break;
265 }
265 }
266 }
266 }
267
267
268 void AxisItem::applyLayout(const QVector<qreal>& points)
268 void AxisItem::applyLayout(const QVector<qreal>& points)
269 {
269 {
270 Q_ASSERT(points.size() == m_thicksList.size());
270 Q_ASSERT(points.size() == m_thicksList.size());
271
271
272 QList<QGraphicsItem *> lines = m_grid.childItems();
272 QList<QGraphicsItem *> lines = m_grid.childItems();
273 QList<QGraphicsItem *> labels = m_labels.childItems();
273 QList<QGraphicsItem *> labels = m_labels.childItems();
274 QList<QGraphicsItem *> shades = m_shades.childItems();
274 QList<QGraphicsItem *> shades = m_shades.childItems();
275 QList<QGraphicsItem *> axis = m_axis.childItems();
275 QList<QGraphicsItem *> axis = m_axis.childItems();
276
276
277 Q_ASSERT(labels.size() == m_thicksList.size());
277 Q_ASSERT(labels.size() == m_thicksList.size());
278
278
279 switch (m_type)
279 switch (m_type)
280 {
280 {
281 case X_AXIS:
281 case X_AXIS:
282 {
282 {
283 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
283 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
284 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
284 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
285
285
286 for (int i = 0; i < points.size(); ++i) {
286 for (int i = 0; i < points.size(); ++i) {
287 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
287 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
288 lineItem->setLine(points[i], m_rect.top(), points[i], m_rect.bottom());
288 lineItem->setLine(points[i], m_rect.top(), points[i], m_rect.bottom());
289 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
289 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
290 labelItem->setText(m_thicksList.at(i));
290 labelItem->setText(m_thicksList.at(i));
291 QPointF center = labelItem->boundingRect().center();
291 QPointF center = labelItem->boundingRect().center();
292 labelItem->setTransformOriginPoint(center.x(), center.y());
292 labelItem->setTransformOriginPoint(center.x(), center.y());
293 labelItem->setPos(points[i] - center.x(), m_rect.bottom() + label_padding);
293 labelItem->setPos(points[i] - center.x(), m_rect.bottom() + label_padding);
294 if(i%2){
294 if(i%2){
295 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
295 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
296 rectItem->setRect(points[i],m_rect.top(),points[i+1]-points[i],m_rect.height());
296 rectItem->setRect(points[i],m_rect.top(),points[i+1]-points[i],m_rect.height());
297 }
297 }
298 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
298 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
299 lineItem->setLine(points[i],m_rect.bottom(),points[i],m_rect.bottom()+5);
299 lineItem->setLine(points[i],m_rect.bottom(),points[i],m_rect.bottom()+5);
300 }
300 }
301 }
301 }
302 break;
302 break;
303
303
304 case Y_AXIS:
304 case Y_AXIS:
305 {
305 {
306 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
306 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
307 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
307 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
308
308
309 for (int i = 0; i < points.size(); ++i) {
309 for (int i = 0; i < points.size(); ++i) {
310 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
310 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
311 lineItem->setLine(m_rect.left() , points[i], m_rect.right(), points[i]);
311 lineItem->setLine(m_rect.left() , points[i], m_rect.right(), points[i]);
312 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
312 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
313 labelItem->setText(m_thicksList.at(i));
313 labelItem->setText(m_thicksList.at(i));
314 QPointF center = labelItem->boundingRect().center();
314 QPointF center = labelItem->boundingRect().center();
315 labelItem->setTransformOriginPoint(center.x(), center.y());
315 labelItem->setTransformOriginPoint(center.x(), center.y());
316 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , points[i]-center.y());
316 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , points[i]-center.y());
317 if(i%2){
317 if(i%2){
318 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
318 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
319 rectItem->setRect(m_rect.left(),points[i],m_rect.width(),points[i]-points[i+1]);
319 rectItem->setRect(m_rect.left(),points[i],m_rect.width(),points[i]-points[i+1]);
320 }
320 }
321 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
321 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
322 lineItem->setLine(m_rect.left()-5,points[i],m_rect.left(),points[i]);
322 lineItem->setLine(m_rect.left()-5,points[i],m_rect.left(),points[i]);
323 }
323 }
324 }
324 }
325 break;
325 break;
326 default:
326 default:
327 qDebug()<<"Unknown axis type";
327 qDebug()<<"Unknown axis type";
328 break;
328 break;
329 }
329 }
330 }
330 }
331
331
332 //TODO "nice numbers algorithm"
332 //TODO "nice numbers algorithm"
333 #include "moc_axisitem_p.cpp"
333 #include "moc_axisitem_p.cpp"
334
334
335 QTCOMMERCIALCHART_END_NAMESPACE
335 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now