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