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