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