@@ -1,299 +1,300 | |||||
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 <QPainter> |
|
4 | #include <QPainter> | |
4 | #include <QDebug> |
|
5 | #include <QDebug> | |
5 |
|
6 | |||
6 | static int label_padding = 5; |
|
7 | static int label_padding = 5; | |
7 |
|
8 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
10 | |||
10 | AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) : |
|
11 | AxisItem::AxisItem(AxisType type,QGraphicsItem* parent) : | |
11 | ChartItem(parent), |
|
12 | ChartItem(parent), | |
12 | m_type(type), |
|
13 | m_type(type), | |
13 | m_labelsAngle(0), |
|
14 | m_labelsAngle(0), | |
14 | m_shadesEnabled(true), |
|
15 | m_shadesEnabled(true), | |
15 | m_grid(parent), |
|
16 | m_grid(parent), | |
16 | m_shades(parent), |
|
17 | m_shades(parent), | |
17 | m_labels(parent) |
|
18 | m_labels(parent), | |
|
19 | m_origin(0,0) | |||
18 | { |
|
20 | { | |
19 | //initial initialization |
|
21 | //initial initialization | |
20 |
m_shades.setZValue( |
|
22 | m_shades.setZValue(ChartPresenter::ShadesZValue); | |
21 |
m_grid.setZValue( |
|
23 | m_grid.setZValue(ChartPresenter::GridZValue); | |
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(int count) |
|
35 | void AxisItem::createItems(int count) | |
34 | { |
|
36 | { | |
35 | for (int i = 0; i < count; ++i) { |
|
37 | for (int i = 0; i < count; ++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 | m_thicksList.clear(); |
|
58 | m_thicksList.clear(); | |
57 |
|
59 | |||
58 | } |
|
60 | } | |
59 |
|
61 | |||
60 | void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
62 | void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
61 | { |
|
63 | { | |
62 |
|
64 | |||
63 | } |
|
65 | } | |
64 |
|
66 | |||
65 | void AxisItem::updateItem(int count) |
|
67 | void AxisItem::updateItem(int count) | |
66 | { |
|
68 | { | |
67 |
|
69 | |||
68 | QList<QGraphicsItem *> lines = m_grid.childItems(); |
|
70 | QList<QGraphicsItem *> lines = m_grid.childItems(); | |
69 | QList<QGraphicsItem *> labels = m_labels.childItems(); |
|
71 | QList<QGraphicsItem *> labels = m_labels.childItems(); | |
70 | QList<QGraphicsItem *> shades = m_shades.childItems(); |
|
72 | QList<QGraphicsItem *> shades = m_shades.childItems(); | |
71 |
|
73 | |||
72 | switch (m_type) |
|
74 | switch (m_type) | |
73 | { |
|
75 | { | |
74 | case X_AXIS: |
|
76 | case X_AXIS: | |
75 | { |
|
77 | { | |
76 | const qreal deltaX = m_rect.width() / (count-1); |
|
78 | const qreal deltaX = m_rect.width() / (count-1); | |
77 |
|
79 | |||
78 | m_axis.setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); |
|
80 | m_axis.setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom()); | |
79 |
|
81 | |||
80 | for (int i = 0; i < count; ++i) { |
|
82 | for (int i = 0; i < count; ++i) { | |
81 | int x = i * deltaX + m_rect.left(); |
|
83 | int x = i * deltaX + m_rect.left(); | |
82 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
84 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | |
83 | lineItem->setLine(x, m_rect.top(), x, m_rect.bottom()); |
|
85 | lineItem->setLine(x, m_rect.top(), x, m_rect.bottom()); | |
84 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); |
|
86 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); | |
85 | labelItem->setText(m_thicksList.at(i)); |
|
87 | labelItem->setText(m_thicksList.at(i)); | |
86 | QPointF center = labelItem->boundingRect().center(); |
|
88 | QPointF center = labelItem->boundingRect().center(); | |
87 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
89 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
88 | labelItem->setPos(x - center.x(), m_rect.bottom() + label_padding); |
|
90 | labelItem->setPos(x - center.x(), m_rect.bottom() + label_padding); | |
89 |
|
91 | |||
90 | if(i%2){ |
|
92 | if(i%2){ | |
91 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2)); |
|
93 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2)); | |
92 | rectItem->setRect(x,m_rect.top(),deltaX,m_rect.height()); |
|
94 | rectItem->setRect(x,m_rect.top(),deltaX,m_rect.height()); | |
93 | } |
|
95 | } | |
94 | } |
|
96 | } | |
95 | } |
|
97 | } | |
96 | break; |
|
98 | break; | |
97 |
|
99 | |||
98 | case Y_AXIS: |
|
100 | case Y_AXIS: | |
99 | { |
|
101 | { | |
100 | const qreal deltaY = m_rect.height()/ (count-1); |
|
102 | const qreal deltaY = m_rect.height()/ (count-1); | |
101 |
|
103 | |||
102 | m_axis.setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); |
|
104 | m_axis.setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom()); | |
103 |
|
105 | |||
104 | for (int i = 0; i < count; ++i) { |
|
106 | for (int i = 0; i < count; ++i) { | |
105 | int y = i * -deltaY + m_rect.bottom(); |
|
107 | int y = i * -deltaY + m_rect.bottom(); | |
106 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); |
|
108 | QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i)); | |
107 | lineItem->setLine(m_rect.left() , y, m_rect.right(), y); |
|
109 | lineItem->setLine(m_rect.left() , y, m_rect.right(), y); | |
108 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); |
|
110 | QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i)); | |
109 | labelItem->setText(m_thicksList.at(i)); |
|
111 | labelItem->setText(m_thicksList.at(i)); | |
110 | QPointF center = labelItem->boundingRect().center(); |
|
112 | QPointF center = labelItem->boundingRect().center(); | |
111 | labelItem->setTransformOriginPoint(center.x(), center.y()); |
|
113 | labelItem->setTransformOriginPoint(center.x(), center.y()); | |
112 | labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , y-center.y()); |
|
114 | labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , y-center.y()); | |
113 | if(i%2){ |
|
115 | if(i%2){ | |
114 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2)); |
|
116 | QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2)); | |
115 | rectItem->setRect(m_rect.left(),y,m_rect.width(),deltaY); |
|
117 | rectItem->setRect(m_rect.left(),y,m_rect.width(),deltaY); | |
116 | } |
|
118 | } | |
117 | } |
|
119 | } | |
118 | } |
|
120 | } | |
119 | break; |
|
121 | break; | |
120 | default: |
|
122 | default: | |
121 | qDebug()<<"Unknown axis type"; |
|
123 | qDebug()<<"Unknown axis type"; | |
122 | break; |
|
124 | break; | |
123 | } |
|
125 | } | |
124 | } |
|
126 | } | |
125 |
|
127 | |||
126 | void AxisItem::handleAxisUpdate(QChartAxis* axis) |
|
128 | void AxisItem::handleAxisUpdate(QChartAxis* axis) | |
127 | { |
|
129 | { | |
128 | if(axis->isAxisVisible()) { |
|
130 | if(axis->isAxisVisible()) { | |
129 | setAxisOpacity(100); |
|
131 | setAxisOpacity(100); | |
130 | } |
|
132 | } | |
131 | else { |
|
133 | else { | |
132 | setAxisOpacity(0); |
|
134 | setAxisOpacity(0); | |
133 | } |
|
135 | } | |
134 |
|
136 | |||
135 | if(axis->isGridVisible()) { |
|
137 | if(axis->isGridVisible()) { | |
136 | setGridOpacity(100); |
|
138 | setGridOpacity(100); | |
137 | } |
|
139 | } | |
138 | else { |
|
140 | else { | |
139 | setGridOpacity(0); |
|
141 | setGridOpacity(0); | |
140 | } |
|
142 | } | |
141 |
|
143 | |||
142 | if(axis->isLabelsVisible()) |
|
144 | if(axis->isLabelsVisible()) | |
143 | { |
|
145 | { | |
144 | setLabelsOpacity(100); |
|
146 | setLabelsOpacity(100); | |
145 | } |
|
147 | } | |
146 | else { |
|
148 | else { | |
147 | setLabelsOpacity(0); |
|
149 | setLabelsOpacity(0); | |
148 | } |
|
150 | } | |
149 |
|
151 | |||
150 | if(axis->isShadesVisible()) { |
|
152 | if(axis->isShadesVisible()) { | |
151 | setShadesOpacity(axis->shadesOpacity()); |
|
153 | setShadesOpacity(axis->shadesOpacity()); | |
152 | } |
|
154 | } | |
153 | else { |
|
155 | else { | |
154 | setShadesOpacity(0); |
|
156 | setShadesOpacity(0); | |
155 | } |
|
157 | } | |
156 |
|
158 | |||
157 | setLabelsAngle(axis->labelsAngle()); |
|
159 | setLabelsAngle(axis->labelsAngle()); | |
158 | setAxisPen(axis->axisPen()); |
|
160 | setAxisPen(axis->axisPen()); | |
159 | setLabelsPen(axis->labelsPen()); |
|
161 | setLabelsPen(axis->labelsPen()); | |
160 | setLabelsBrush(axis->labelsBrush()); |
|
162 | setLabelsBrush(axis->labelsBrush()); | |
161 | setLabelsFont(axis->labelFont()); |
|
163 | setLabelsFont(axis->labelFont()); | |
162 | setGridPen(axis->gridPen()); |
|
164 | setGridPen(axis->gridPen()); | |
163 | setShadesPen(axis->shadesPen()); |
|
165 | setShadesPen(axis->shadesPen()); | |
164 | setShadesBrush(axis->shadesBrush()); |
|
166 | setShadesBrush(axis->shadesBrush()); | |
165 | } |
|
167 | } | |
166 |
|
168 | |||
167 | void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels) |
|
169 | void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels) | |
168 | { |
|
170 | { | |
169 | m_thicksList=labels; |
|
171 | m_thicksList=labels; | |
170 | QList<QGraphicsItem*> items = m_labels.childItems(); |
|
172 | QList<QGraphicsItem*> items = m_labels.childItems(); | |
171 | if(items.size()!=m_thicksList.size()){ |
|
173 | if(items.size()!=m_thicksList.size()){ | |
172 | clear(); |
|
174 | clear(); | |
173 | m_thicksList=labels; |
|
175 | m_thicksList=labels; | |
174 | createItems(m_thicksList.size()); |
|
176 | createItems(m_thicksList.size()); | |
175 | updateItem(m_thicksList.size()); |
|
177 | updateItem(m_thicksList.size()); | |
176 | items = m_labels.childItems(); |
|
178 | items = m_labels.childItems(); | |
177 | handleAxisUpdate(axis); |
|
179 | handleAxisUpdate(axis); | |
178 | } |
|
180 | } | |
179 |
|
181 | |||
180 | Q_ASSERT(items.size()==m_thicksList.size()); |
|
182 | Q_ASSERT(items.size()==m_thicksList.size()); | |
181 |
|
183 | |||
182 | int i=0; |
|
184 | int i=0; | |
183 | foreach(QGraphicsItem* item, items){ |
|
185 | foreach(QGraphicsItem* item, items){ | |
184 | static_cast<QGraphicsSimpleTextItem*>(item)->setText(m_thicksList.at(i)); |
|
186 | static_cast<QGraphicsSimpleTextItem*>(item)->setText(m_thicksList.at(i)); | |
185 | i++; |
|
187 | i++; | |
186 | } |
|
188 | } | |
187 | update(); |
|
189 | update(); | |
188 | } |
|
190 | } | |
189 |
|
191 | |||
190 | void AxisItem::handleGeometryChanged(const QRectF& rect) |
|
192 | void AxisItem::handleGeometryChanged(const QRectF& rect) | |
191 | { |
|
193 | { | |
192 | m_rect = rect; |
|
194 | m_rect = rect; | |
193 | updateItem(m_thicksList.size()); |
|
195 | updateItem(m_thicksList.size()); | |
194 | update(); |
|
196 | update(); | |
195 | } |
|
197 | } | |
196 |
|
198 | |||
197 | void AxisItem::setAxisOpacity(qreal opacity) |
|
199 | void AxisItem::setAxisOpacity(qreal opacity) | |
198 | { |
|
200 | { | |
199 | m_axis.setOpacity(opacity); |
|
201 | m_axis.setOpacity(opacity); | |
200 | } |
|
202 | } | |
201 |
|
203 | |||
202 | qreal AxisItem::axisOpacity() const |
|
204 | qreal AxisItem::axisOpacity() const | |
203 | { |
|
205 | { | |
204 | return m_axis.opacity(); |
|
206 | return m_axis.opacity(); | |
205 | } |
|
207 | } | |
206 |
|
208 | |||
207 | void AxisItem::setGridOpacity(qreal opacity) |
|
209 | void AxisItem::setGridOpacity(qreal opacity) | |
208 | { |
|
210 | { | |
209 | m_grid.setOpacity(opacity); |
|
211 | m_grid.setOpacity(opacity); | |
210 | } |
|
212 | } | |
211 |
|
213 | |||
212 |
|
||||
213 | qreal AxisItem::gridOpacity() const |
|
214 | qreal AxisItem::gridOpacity() const | |
214 | { |
|
215 | { | |
215 | return m_grid.opacity(); |
|
216 | return m_grid.opacity(); | |
216 | } |
|
217 | } | |
217 |
|
218 | |||
218 | void AxisItem::setLabelsOpacity(qreal opacity) |
|
219 | void AxisItem::setLabelsOpacity(qreal opacity) | |
219 | { |
|
220 | { | |
220 | m_labels.setOpacity(opacity); |
|
221 | m_labels.setOpacity(opacity); | |
221 | } |
|
222 | } | |
222 |
|
223 | |||
223 | qreal AxisItem::labelsOpacity() const |
|
224 | qreal AxisItem::labelsOpacity() const | |
224 | { |
|
225 | { | |
225 | return m_labels.opacity(); |
|
226 | return m_labels.opacity(); | |
226 | } |
|
227 | } | |
227 |
|
228 | |||
228 | void AxisItem::setShadesOpacity(qreal opacity) |
|
229 | void AxisItem::setShadesOpacity(qreal opacity) | |
229 | { |
|
230 | { | |
230 | m_shades.setOpacity(opacity); |
|
231 | m_shades.setOpacity(opacity); | |
231 | } |
|
232 | } | |
232 |
|
233 | |||
233 | qreal AxisItem::shadesOpacity() const |
|
234 | qreal AxisItem::shadesOpacity() const | |
234 | { |
|
235 | { | |
235 | return m_shades.opacity(); |
|
236 | return m_shades.opacity(); | |
236 | } |
|
237 | } | |
237 |
|
238 | |||
238 | void AxisItem::setLabelsAngle(int angle) |
|
239 | void AxisItem::setLabelsAngle(int angle) | |
239 | { |
|
240 | { | |
240 | foreach(QGraphicsItem* item , m_labels.childItems()) { |
|
241 | foreach(QGraphicsItem* item , m_labels.childItems()) { | |
241 | QPointF center = item->boundingRect().center(); |
|
242 | QPointF center = item->boundingRect().center(); | |
242 | item->setRotation(angle); |
|
243 | item->setRotation(angle); | |
243 | } |
|
244 | } | |
244 |
|
245 | |||
245 | m_labelsAngle=angle; |
|
246 | m_labelsAngle=angle; | |
246 | } |
|
247 | } | |
247 |
|
248 | |||
248 | void AxisItem::setLabelsPen(const QPen& pen) |
|
249 | void AxisItem::setLabelsPen(const QPen& pen) | |
249 | { |
|
250 | { | |
250 | foreach(QGraphicsItem* item , m_labels.childItems()) { |
|
251 | foreach(QGraphicsItem* item , m_labels.childItems()) { | |
251 | static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen); |
|
252 | static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen); | |
252 | } |
|
253 | } | |
253 | } |
|
254 | } | |
254 |
|
255 | |||
255 | void AxisItem::setLabelsBrush(const QBrush& brush) |
|
256 | void AxisItem::setLabelsBrush(const QBrush& brush) | |
256 | { |
|
257 | { | |
257 | foreach(QGraphicsItem* item , m_labels.childItems()) { |
|
258 | foreach(QGraphicsItem* item , m_labels.childItems()) { | |
258 | static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush); |
|
259 | static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush); | |
259 | } |
|
260 | } | |
260 | } |
|
261 | } | |
261 |
|
262 | |||
262 | void AxisItem::setLabelsFont(const QFont& font) |
|
263 | void AxisItem::setLabelsFont(const QFont& font) | |
263 | { |
|
264 | { | |
264 | foreach(QGraphicsItem* item , m_labels.childItems()) { |
|
265 | foreach(QGraphicsItem* item , m_labels.childItems()) { | |
265 | static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font); |
|
266 | static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font); | |
266 | } |
|
267 | } | |
267 | } |
|
268 | } | |
268 |
|
269 | |||
269 | void AxisItem::setShadesBrush(const QBrush& brush) |
|
270 | void AxisItem::setShadesBrush(const QBrush& brush) | |
270 | { |
|
271 | { | |
271 | foreach(QGraphicsItem* item , m_shades.childItems()) { |
|
272 | foreach(QGraphicsItem* item , m_shades.childItems()) { | |
272 | static_cast<QGraphicsRectItem*>(item)->setBrush(brush); |
|
273 | static_cast<QGraphicsRectItem*>(item)->setBrush(brush); | |
273 | } |
|
274 | } | |
274 | } |
|
275 | } | |
275 |
|
276 | |||
276 | void AxisItem::setShadesPen(const QPen& pen) |
|
277 | void AxisItem::setShadesPen(const QPen& pen) | |
277 | { |
|
278 | { | |
278 | foreach(QGraphicsItem* item , m_shades.childItems()) { |
|
279 | foreach(QGraphicsItem* item , m_shades.childItems()) { | |
279 | static_cast<QGraphicsRectItem*>(item)->setPen(pen); |
|
280 | static_cast<QGraphicsRectItem*>(item)->setPen(pen); | |
280 | } |
|
281 | } | |
281 | } |
|
282 | } | |
282 |
|
283 | |||
283 | void AxisItem::setAxisPen(const QPen& pen) |
|
284 | void AxisItem::setAxisPen(const QPen& pen) | |
284 | { |
|
285 | { | |
285 | m_axis.setPen(pen); |
|
286 | m_axis.setPen(pen); | |
286 | } |
|
287 | } | |
287 |
|
288 | |||
288 | void AxisItem::setGridPen(const QPen& pen) |
|
289 | void AxisItem::setGridPen(const QPen& pen) | |
289 | { |
|
290 | { | |
290 | foreach(QGraphicsItem* item , m_grid.childItems()) { |
|
291 | foreach(QGraphicsItem* item , m_grid.childItems()) { | |
291 | static_cast<QGraphicsLineItem*>(item)->setPen(pen); |
|
292 | static_cast<QGraphicsLineItem*>(item)->setPen(pen); | |
292 | } |
|
293 | } | |
293 | } |
|
294 | } | |
294 |
|
295 | |||
295 |
|
296 | |||
296 | //TODO "nice numbers algorithm" |
|
297 | //TODO "nice numbers algorithm" | |
297 | #include "moc_axisitem_p.cpp" |
|
298 | #include "moc_axisitem_p.cpp" | |
298 |
|
299 | |||
299 | QTCOMMERCIALCHART_END_NAMESPACE |
|
300 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,76 +1,77 | |||||
1 | #ifndef AXISITEM_H_ |
|
1 | #ifndef AXISITEM_H_ | |
2 | #define AXISITEM_H_ |
|
2 | #define AXISITEM_H_ | |
3 |
|
3 | |||
4 | #include "domain_p.h" |
|
4 | #include "domain_p.h" | |
5 | #include "chartitem_p.h" |
|
5 | #include "chartitem_p.h" | |
6 | #include <QGraphicsItem> |
|
6 | #include <QGraphicsItem> | |
7 |
|
7 | |||
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
8 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
9 |
|
9 | |||
10 | class QChartAxis; |
|
10 | class QChartAxis; | |
11 |
|
11 | |||
12 | class AxisItem : public QObject, public ChartItem |
|
12 | class AxisItem : public QObject, public ChartItem | |
13 | { |
|
13 | { | |
14 | Q_OBJECT |
|
14 | Q_OBJECT | |
15 | public: |
|
15 | public: | |
16 | enum AxisType{X_AXIS,Y_AXIS}; |
|
16 | enum AxisType{X_AXIS,Y_AXIS}; | |
17 |
|
17 | |||
18 | AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0); |
|
18 | AxisItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0); | |
19 | ~AxisItem(); |
|
19 | ~AxisItem(); | |
20 |
|
20 | |||
21 | //from QGraphicsItem |
|
21 | //from QGraphicsItem | |
22 | QRectF boundingRect() const; |
|
22 | QRectF boundingRect() const; | |
23 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); |
|
23 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); | |
24 |
|
24 | |||
25 | AxisType axisType() const {return m_type;}; |
|
25 | AxisType axisType() const {return m_type;}; | |
26 |
|
26 | |||
27 | void setAxisOpacity(qreal opacity); |
|
27 | void setAxisOpacity(qreal opacity); | |
28 | qreal axisOpacity() const; |
|
28 | qreal axisOpacity() const; | |
29 |
|
29 | |||
30 | void setGridOpacity(qreal opacity); |
|
30 | void setGridOpacity(qreal opacity); | |
31 | qreal gridOpacity() const; |
|
31 | qreal gridOpacity() const; | |
32 |
|
32 | |||
33 | void setLabelsOpacity(qreal opacity); |
|
33 | void setLabelsOpacity(qreal opacity); | |
34 | qreal labelsOpacity() const; |
|
34 | qreal labelsOpacity() const; | |
35 |
|
35 | |||
36 | void setShadesOpacity(qreal opacity); |
|
36 | void setShadesOpacity(qreal opacity); | |
37 | qreal shadesOpacity() const; |
|
37 | qreal shadesOpacity() const; | |
38 |
|
38 | |||
39 | void setLabelsAngle(int angle); |
|
39 | void setLabelsAngle(int angle); | |
40 | int labelsAngle()const { return m_labelsAngle; } |
|
40 | int labelsAngle()const { return m_labelsAngle; } | |
41 |
|
41 | |||
42 | void setShadesBrush(const QBrush& brush); |
|
42 | void setShadesBrush(const QBrush& brush); | |
43 | void setShadesPen(const QPen& pen); |
|
43 | void setShadesPen(const QPen& pen); | |
44 |
|
44 | |||
45 | void setAxisPen(const QPen& pen); |
|
45 | void setAxisPen(const QPen& pen); | |
46 | void setGridPen(const QPen& pen); |
|
46 | void setGridPen(const QPen& pen); | |
47 |
|
47 | |||
48 | void setLabelsPen(const QPen& pen); |
|
48 | void setLabelsPen(const QPen& pen); | |
49 | void setLabelsBrush(const QBrush& brush); |
|
49 | void setLabelsBrush(const QBrush& brush); | |
50 | void setLabelsFont(const QFont& font); |
|
50 | void setLabelsFont(const QFont& font); | |
51 |
|
51 | |||
52 | public slots: |
|
52 | public slots: | |
53 | void handleAxisUpdate(QChartAxis* axis); |
|
53 | void handleAxisUpdate(QChartAxis* axis); //look and feel | |
54 | void handleLabelsChanged(QChartAxis* axis,const QStringList& labels); |
|
54 | void handleLabelsChanged(QChartAxis* axis,const QStringList& labels); //labels from dataset | |
55 | void handleGeometryChanged(const QRectF& size); |
|
55 | void handleGeometryChanged(const QRectF& size); // geometry from presenter | |
56 | protected: |
|
56 | protected: | |
57 | void updateItem(int count); |
|
57 | void updateItem(int count); | |
58 | private: |
|
58 | private: | |
59 | void clear(); |
|
59 | void clear(); | |
60 | void createItems(int count); |
|
60 | void createItems(int count); | |
61 | private: |
|
61 | private: | |
62 | AxisType m_type; |
|
62 | AxisType m_type; | |
63 | QRectF m_rect; |
|
63 | QRectF m_rect; | |
64 | int m_labelsAngle; |
|
64 | int m_labelsAngle; | |
65 | bool m_shadesEnabled; |
|
65 | bool m_shadesEnabled; | |
66 | QGraphicsItemGroup m_grid; |
|
66 | QGraphicsItemGroup m_grid; | |
67 | QGraphicsItemGroup m_shades; |
|
67 | QGraphicsItemGroup m_shades; | |
68 | QGraphicsItemGroup m_labels; |
|
68 | QGraphicsItemGroup m_labels; | |
69 | QGraphicsLineItem m_axis; |
|
69 | QGraphicsLineItem m_axis; | |
70 | QStringList m_thicksList; |
|
70 | QStringList m_thicksList; | |
|
71 | QPointF m_origin; | |||
71 |
|
72 | |||
72 | }; |
|
73 | }; | |
73 |
|
74 | |||
74 | QTCOMMERCIALCHART_END_NAMESPACE |
|
75 | QTCOMMERCIALCHART_END_NAMESPACE | |
75 |
|
76 | |||
76 | #endif /* AXISITEM_H_ */ |
|
77 | #endif /* AXISITEM_H_ */ |
@@ -1,61 +1,63 | |||||
1 | #ifndef CHARTPRESENTER_H_ |
|
1 | #ifndef CHARTPRESENTER_H_ | |
2 | #define CHARTPRESENTER_H_ |
|
2 | #define CHARTPRESENTER_H_ | |
3 |
|
3 | |||
4 | #include "qchartglobal.h" |
|
4 | #include "qchartglobal.h" | |
5 | #include "qchart.h" //becouse of QChart::ChartThemeId //TODO |
|
5 | #include "qchart.h" //becouse of QChart::ChartThemeId //TODO | |
6 | #include "qchartaxis.h" |
|
6 | #include "qchartaxis.h" | |
7 | #include <QRectF> |
|
7 | #include <QRectF> | |
8 |
|
8 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
10 | |||
11 | class ChartItem; |
|
11 | class ChartItem; | |
12 | class QChartSeries; |
|
12 | class QChartSeries; | |
13 | class ChartDataSet; |
|
13 | class ChartDataSet; | |
14 | //class QChart; |
|
14 | //class QChart; | |
15 | class Domain; |
|
15 | class Domain; | |
16 | class AxisItem; |
|
16 | class AxisItem; | |
17 | class ChartTheme; |
|
17 | class ChartTheme; | |
18 |
|
18 | |||
19 | class ChartPresenter: public QObject |
|
19 | class ChartPresenter: public QObject | |
20 | { |
|
20 | { | |
21 | Q_OBJECT |
|
21 | Q_OBJECT | |
22 | public: |
|
22 | public: | |
|
23 | enum ZValues { BackgroundZValue = -1 , ShadesZValue, GridZValue, AxisZValue , LineChartZValue }; | |||
|
24 | ||||
23 | ChartPresenter(QChart* chart,ChartDataSet *dataset); |
|
25 | ChartPresenter(QChart* chart,ChartDataSet *dataset); | |
24 | virtual ~ChartPresenter(); |
|
26 | virtual ~ChartPresenter(); | |
25 |
|
27 | |||
26 | void setMargin(int margin); |
|
28 | void setMargin(int margin); | |
27 | int margin() const; |
|
29 | int margin() const; | |
28 |
|
30 | |||
29 | QRectF geometry() const; |
|
31 | QRectF geometry() const; | |
30 |
|
32 | |||
31 | void setChartTheme(QChart::ChartTheme theme); |
|
33 | void setChartTheme(QChart::ChartTheme theme); | |
32 | QChart::ChartTheme chartTheme(); |
|
34 | QChart::ChartTheme chartTheme(); | |
33 |
|
35 | |||
34 | private: |
|
36 | private: | |
35 | void createConnections(); |
|
37 | void createConnections(); | |
36 |
|
38 | |||
37 | public slots: |
|
39 | public slots: | |
38 | void handleSeriesAdded(QChartSeries* series); |
|
40 | void handleSeriesAdded(QChartSeries* series); | |
39 | void handleSeriesRemoved(QChartSeries* series); |
|
41 | void handleSeriesRemoved(QChartSeries* series); | |
40 | void handleAxisAdded(QChartAxis* axis); |
|
42 | void handleAxisAdded(QChartAxis* axis); | |
41 | void handleAxisRemoved(QChartAxis* axis); |
|
43 | void handleAxisRemoved(QChartAxis* axis); | |
42 | void handleSeriesDomainChanged(QChartSeries* series, const Domain& domain); |
|
44 | void handleSeriesDomainChanged(QChartSeries* series, const Domain& domain); | |
43 | void handleAxisLabelsChanged(QChartAxis* axis, const QStringList& labels); |
|
45 | void handleAxisLabelsChanged(QChartAxis* axis, const QStringList& labels); | |
44 | void handleSeriesChanged(QChartSeries* series); |
|
46 | void handleSeriesChanged(QChartSeries* series); | |
45 | void handleGeometryChanged(); |
|
47 | void handleGeometryChanged(); | |
46 | signals: |
|
48 | signals: | |
47 | void geometryChanged(const QRectF& rect); |
|
49 | void geometryChanged(const QRectF& rect); | |
48 | private: |
|
50 | private: | |
49 | QMap<QChartSeries*,ChartItem*> m_chartItems; |
|
51 | QMap<QChartSeries*,ChartItem*> m_chartItems; | |
50 | QMap<QChartAxis*,AxisItem*> m_axisItems; |
|
52 | QMap<QChartAxis*,AxisItem*> m_axisItems; | |
51 | QChart* m_chart; |
|
53 | QChart* m_chart; | |
52 | ChartDataSet* m_dataset; |
|
54 | ChartDataSet* m_dataset; | |
53 | ChartTheme *m_chartTheme; |
|
55 | ChartTheme *m_chartTheme; | |
54 | int m_marginSize; |
|
56 | int m_marginSize; | |
55 | QRectF m_rect; |
|
57 | QRectF m_rect; | |
56 |
|
58 | |||
57 | }; |
|
59 | }; | |
58 |
|
60 | |||
59 | QTCOMMERCIALCHART_END_NAMESPACE |
|
61 | QTCOMMERCIALCHART_END_NAMESPACE | |
60 |
|
62 | |||
61 | #endif /* CHARTPRESENTER_H_ */ |
|
63 | #endif /* CHARTPRESENTER_H_ */ |
@@ -1,210 +1,210 | |||||
1 | #include "linechartitem_p.h" |
|
1 | #include "linechartitem_p.h" | |
2 | #include "qlinechartseries.h" |
|
2 | #include "qlinechartseries.h" | |
3 | #include "chartpresenter_p.h" |
|
3 | #include "chartpresenter_p.h" | |
4 | #include <QPainter> |
|
4 | #include <QPainter> | |
5 |
|
5 | |||
6 |
|
6 | |||
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
7 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
8 |
|
8 | |||
9 | //TODO: optimazie : remove points which are not visible |
|
9 | //TODO: optimazie : remove points which are not visible | |
10 |
|
10 | |||
11 | LineChartItem::LineChartItem(ChartPresenter* presenter, QLineChartSeries* series,QGraphicsItem *parent):ChartItem(parent), |
|
11 | LineChartItem::LineChartItem(ChartPresenter* presenter, QLineChartSeries* series,QGraphicsItem *parent):ChartItem(parent), | |
12 | m_presenter(presenter), |
|
12 | m_presenter(presenter), | |
13 | m_series(series), |
|
13 | m_series(series), | |
14 | m_dirtyData(false), |
|
14 | m_dirtyData(false), | |
15 | m_dirtyGeometry(false), |
|
15 | m_dirtyGeometry(false), | |
16 | m_dirtyDomain(false) |
|
16 | m_dirtyDomain(false) | |
17 | { |
|
17 | { | |
18 |
|
18 | setZValue(ChartPresenter::LineChartZValue); | ||
19 | } |
|
19 | } | |
20 |
|
20 | |||
21 | QRectF LineChartItem::boundingRect() const |
|
21 | QRectF LineChartItem::boundingRect() const | |
22 | { |
|
22 | { | |
23 | return m_rect; |
|
23 | return m_rect; | |
24 | } |
|
24 | } | |
25 |
|
25 | |||
26 | QPainterPath LineChartItem::shape() const |
|
26 | QPainterPath LineChartItem::shape() const | |
27 | { |
|
27 | { | |
28 | return m_path; |
|
28 | return m_path; | |
29 | } |
|
29 | } | |
30 |
|
30 | |||
31 |
|
31 | |||
32 | void LineChartItem::addPoints(const QVector<QPointF>& points) |
|
32 | void LineChartItem::addPoints(const QVector<QPointF>& points) | |
33 | { |
|
33 | { | |
34 | m_data = points; |
|
34 | m_data = points; | |
35 | for(int i=0; i<m_data.size();i++){ |
|
35 | for(int i=0; i<m_data.size();i++){ | |
36 | const QPointF& point =m_data[i]; |
|
36 | const QPointF& point =m_data[i]; | |
37 | QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this); |
|
37 | QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this); | |
38 | item->setPos(point.x()-1,point.y()-1);; |
|
38 | item->setPos(point.x()-1,point.y()-1);; | |
39 | if(!m_clipRect.contains(point) || !m_series->isPointsVisible()) item->setVisible(false); |
|
39 | if(!m_clipRect.contains(point) || !m_series->isPointsVisible()) item->setVisible(false); | |
40 | m_points << item; |
|
40 | m_points << item; | |
41 | } |
|
41 | } | |
42 | } |
|
42 | } | |
43 |
|
43 | |||
44 | void LineChartItem::addPoint(const QPointF& point) |
|
44 | void LineChartItem::addPoint(const QPointF& point) | |
45 | { |
|
45 | { | |
46 | m_data << point; |
|
46 | m_data << point; | |
47 | QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this); |
|
47 | QGraphicsRectItem* item = new QGraphicsRectItem(0,0,3,3,this); | |
48 | m_clipRect.contains(point); |
|
48 | m_clipRect.contains(point); | |
49 | item->setPos(point.x()-1,point.y()-1); |
|
49 | item->setPos(point.x()-1,point.y()-1); | |
50 | if(!m_clipRect.contains(point) || !m_series->isPointsVisible()) item->setVisible(false); |
|
50 | if(!m_clipRect.contains(point) || !m_series->isPointsVisible()) item->setVisible(false); | |
51 | m_points << item; |
|
51 | m_points << item; | |
52 | } |
|
52 | } | |
53 |
|
53 | |||
54 | void LineChartItem::removePoint(const QPointF& point) |
|
54 | void LineChartItem::removePoint(const QPointF& point) | |
55 | { |
|
55 | { | |
56 | Q_ASSERT(m_data.count() == m_points.count()); |
|
56 | Q_ASSERT(m_data.count() == m_points.count()); | |
57 | int index = m_data.lastIndexOf(point,0); |
|
57 | int index = m_data.lastIndexOf(point,0); | |
58 | m_data.remove(index); |
|
58 | m_data.remove(index); | |
59 | delete(m_points.takeAt(index)); |
|
59 | delete(m_points.takeAt(index)); | |
60 | } |
|
60 | } | |
61 |
|
61 | |||
62 | void LineChartItem::setPoint(const QPointF& oldPoint,const QPointF& newPoint) |
|
62 | void LineChartItem::setPoint(const QPointF& oldPoint,const QPointF& newPoint) | |
63 | { |
|
63 | { | |
64 | Q_ASSERT(m_data.count() == m_points.count()); |
|
64 | Q_ASSERT(m_data.count() == m_points.count()); | |
65 | int index = m_data.lastIndexOf(oldPoint,0); |
|
65 | int index = m_data.lastIndexOf(oldPoint,0); | |
66 |
|
66 | |||
67 | if(index > -1){ |
|
67 | if(index > -1){ | |
68 | m_data.replace(index,newPoint); |
|
68 | m_data.replace(index,newPoint); | |
69 | QGraphicsItem* item = m_points.at(index); |
|
69 | QGraphicsItem* item = m_points.at(index); | |
70 | item->setPos(newPoint.x()-1,newPoint.y()-1); |
|
70 | item->setPos(newPoint.x()-1,newPoint.y()-1); | |
71 | } |
|
71 | } | |
72 | } |
|
72 | } | |
73 |
|
73 | |||
74 | void LineChartItem::setPoint(int index,const QPointF& point) |
|
74 | void LineChartItem::setPoint(int index,const QPointF& point) | |
75 | { |
|
75 | { | |
76 | Q_ASSERT(m_data.count() == m_points.count()); |
|
76 | Q_ASSERT(m_data.count() == m_points.count()); | |
77 | Q_ASSERT(index>=0); |
|
77 | Q_ASSERT(index>=0); | |
78 |
|
78 | |||
79 | m_data.replace(index,point); |
|
79 | m_data.replace(index,point); | |
80 | QGraphicsItem* item = m_points.at(index); |
|
80 | QGraphicsItem* item = m_points.at(index); | |
81 | item->setPos(point.x()-1,point.y()-1); |
|
81 | item->setPos(point.x()-1,point.y()-1); | |
82 | } |
|
82 | } | |
83 |
|
83 | |||
84 | void LineChartItem::clear() |
|
84 | void LineChartItem::clear() | |
85 | { |
|
85 | { | |
86 | qDeleteAll(m_points); |
|
86 | qDeleteAll(m_points); | |
87 | m_points.clear(); |
|
87 | m_points.clear(); | |
88 | m_hash.clear(); |
|
88 | m_hash.clear(); | |
89 | m_path = QPainterPath(); |
|
89 | m_path = QPainterPath(); | |
90 | m_rect = QRect(); |
|
90 | m_rect = QRect(); | |
91 | m_data.clear(); |
|
91 | m_data.clear(); | |
92 | } |
|
92 | } | |
93 |
|
93 | |||
94 | void LineChartItem::clearView() |
|
94 | void LineChartItem::clearView() | |
95 | { |
|
95 | { | |
96 | qDeleteAll(m_points); |
|
96 | qDeleteAll(m_points); | |
97 | m_points.clear(); |
|
97 | m_points.clear(); | |
98 | m_path = QPainterPath(); |
|
98 | m_path = QPainterPath(); | |
99 | m_rect = QRect(); |
|
99 | m_rect = QRect(); | |
100 | m_data.clear(); |
|
100 | m_data.clear(); | |
101 | } |
|
101 | } | |
102 |
|
102 | |||
103 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
|
103 | void LineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) | |
104 | { |
|
104 | { | |
105 | Q_UNUSED(widget); |
|
105 | Q_UNUSED(widget); | |
106 | Q_UNUSED(option); |
|
106 | Q_UNUSED(option); | |
107 | painter->save(); |
|
107 | painter->save(); | |
108 | painter->setPen(m_pen); |
|
108 | painter->setPen(m_pen); | |
109 | painter->setClipRect(m_clipRect); |
|
109 | painter->setClipRect(m_clipRect); | |
110 | painter->drawPath(m_path); |
|
110 | painter->drawPath(m_path); | |
111 | painter->restore(); |
|
111 | painter->restore(); | |
112 | } |
|
112 | } | |
113 |
|
113 | |||
114 | void LineChartItem::calculatePoint(QPointF& point, int index, const QLineChartSeries* series,const QSizeF& size, const Domain& domain) const |
|
114 | void LineChartItem::calculatePoint(QPointF& point, int index, const QLineChartSeries* series,const QSizeF& size, const Domain& domain) const | |
115 | { |
|
115 | { | |
116 | const qreal deltaX = size.width()/domain.spanX(); |
|
116 | const qreal deltaX = size.width()/domain.spanX(); | |
117 | const qreal deltaY = size.height()/domain.spanY(); |
|
117 | const qreal deltaY = size.height()/domain.spanY(); | |
118 | qreal x = (series->x(index) - domain.m_minX)* deltaX; |
|
118 | qreal x = (series->x(index) - domain.m_minX)* deltaX; | |
119 | qreal y = (series->y(index) - domain.m_minY)*-deltaY + size.height(); |
|
119 | qreal y = (series->y(index) - domain.m_minY)*-deltaY + size.height(); | |
120 | point.setX(x); |
|
120 | point.setX(x); | |
121 | point.setY(y); |
|
121 | point.setY(y); | |
122 | } |
|
122 | } | |
123 |
|
123 | |||
124 |
|
124 | |||
125 | void LineChartItem::calculatePoints(QVector<QPointF>& points, QHash<int,int>& hash,const QLineChartSeries* series,const QSizeF& size, const Domain& domain) const |
|
125 | void LineChartItem::calculatePoints(QVector<QPointF>& points, QHash<int,int>& hash,const QLineChartSeries* series,const QSizeF& size, const Domain& domain) const | |
126 | { |
|
126 | { | |
127 | const qreal deltaX = size.width()/domain.spanX(); |
|
127 | const qreal deltaX = size.width()/domain.spanX(); | |
128 | const qreal deltaY = size.height()/domain.spanY(); |
|
128 | const qreal deltaY = size.height()/domain.spanY(); | |
129 |
|
129 | |||
130 | for (int i = 0; i < series->count(); ++i) { |
|
130 | for (int i = 0; i < series->count(); ++i) { | |
131 | qreal x = (series->x(i) - domain.m_minX)* deltaX; |
|
131 | qreal x = (series->x(i) - domain.m_minX)* deltaX; | |
132 | qreal y = (series->y(i) - domain.m_minY)*-deltaY + size.height(); |
|
132 | qreal y = (series->y(i) - domain.m_minY)*-deltaY + size.height(); | |
133 | hash[i] = points.size(); |
|
133 | hash[i] = points.size(); | |
134 | points << QPointF(x,y); |
|
134 | points << QPointF(x,y); | |
135 | } |
|
135 | } | |
136 | } |
|
136 | } | |
137 |
|
137 | |||
138 | void LineChartItem::updateDomain() |
|
138 | void LineChartItem::updateDomain() | |
139 | { |
|
139 | { | |
140 | clear(); |
|
140 | clear(); | |
141 | prepareGeometryChange(); |
|
141 | prepareGeometryChange(); | |
142 | calculatePoints(m_data,m_hash,m_series,m_size, m_domain); |
|
142 | calculatePoints(m_data,m_hash,m_series,m_size, m_domain); | |
143 | addPoints(m_data); |
|
143 | addPoints(m_data); | |
144 | } |
|
144 | } | |
145 |
|
145 | |||
146 | void LineChartItem::updateData() |
|
146 | void LineChartItem::updateData() | |
147 | { |
|
147 | { | |
148 | //for now the same |
|
148 | //for now the same | |
149 | updateDomain(); |
|
149 | updateDomain(); | |
150 | } |
|
150 | } | |
151 |
|
151 | |||
152 | void LineChartItem::updateGeometry() |
|
152 | void LineChartItem::updateGeometry() | |
153 | { |
|
153 | { | |
154 |
|
154 | |||
155 | if(m_data.size()==0) return; |
|
155 | if(m_data.size()==0) return; | |
156 |
|
156 | |||
157 | prepareGeometryChange(); |
|
157 | prepareGeometryChange(); | |
158 | QPainterPath path; |
|
158 | QPainterPath path; | |
159 | const QPointF& point = m_data.at(0); |
|
159 | const QPointF& point = m_data.at(0); | |
160 | path.moveTo(point); |
|
160 | path.moveTo(point); | |
161 |
|
161 | |||
162 | foreach( const QPointF& point , m_data) { |
|
162 | foreach( const QPointF& point , m_data) { | |
163 | path.lineTo(point); |
|
163 | path.lineTo(point); | |
164 | } |
|
164 | } | |
165 |
|
165 | |||
166 | m_path = path; |
|
166 | m_path = path; | |
167 | m_rect = path.boundingRect(); |
|
167 | m_rect = path.boundingRect(); | |
168 | } |
|
168 | } | |
169 |
|
169 | |||
170 | void LineChartItem::setPen(const QPen& pen) |
|
170 | void LineChartItem::setPen(const QPen& pen) | |
171 | { |
|
171 | { | |
172 | m_pen = pen; |
|
172 | m_pen = pen; | |
173 | } |
|
173 | } | |
174 |
|
174 | |||
175 | //handlers |
|
175 | //handlers | |
176 |
|
176 | |||
177 | void LineChartItem::handleModelChanged(int index) |
|
177 | void LineChartItem::handleModelChanged(int index) | |
178 | { |
|
178 | { | |
179 | Q_ASSERT(index<m_series->count()); |
|
179 | Q_ASSERT(index<m_series->count()); | |
180 | if(m_hash.contains(index)){ |
|
180 | if(m_hash.contains(index)){ | |
181 | int i = m_hash.value(index); |
|
181 | int i = m_hash.value(index); | |
182 | QPointF point; |
|
182 | QPointF point; | |
183 | calculatePoint(point,index,m_series,m_size,m_domain); |
|
183 | calculatePoint(point,index,m_series,m_size,m_domain); | |
184 | setPoint(i,point); |
|
184 | setPoint(i,point); | |
185 | } |
|
185 | } | |
186 | update(); |
|
186 | update(); | |
187 | } |
|
187 | } | |
188 |
|
188 | |||
189 | void LineChartItem::handleDomainChanged(const Domain& domain) |
|
189 | void LineChartItem::handleDomainChanged(const Domain& domain) | |
190 | { |
|
190 | { | |
191 | m_domain = domain; |
|
191 | m_domain = domain; | |
192 | updateDomain(); |
|
192 | updateDomain(); | |
193 | update(); |
|
193 | update(); | |
194 | } |
|
194 | } | |
195 |
|
195 | |||
196 | void LineChartItem::handleGeometryChanged(const QRectF& rect) |
|
196 | void LineChartItem::handleGeometryChanged(const QRectF& rect) | |
197 | { |
|
197 | { | |
198 | Q_ASSERT(rect.isValid()); |
|
198 | Q_ASSERT(rect.isValid()); | |
199 | m_size=rect.size(); |
|
199 | m_size=rect.size(); | |
200 | m_clipRect=rect.translated(-rect.topLeft()); |
|
200 | m_clipRect=rect.translated(-rect.topLeft()); | |
201 | updateDomain(); |
|
201 | updateDomain(); | |
202 | updateGeometry(); |
|
202 | updateGeometry(); | |
203 | setPos(rect.topLeft()); |
|
203 | setPos(rect.topLeft()); | |
204 | update(); |
|
204 | update(); | |
205 | } |
|
205 | } | |
206 |
|
206 | |||
207 |
|
207 | |||
208 | #include "moc_linechartitem_p.cpp" |
|
208 | #include "moc_linechartitem_p.cpp" | |
209 |
|
209 | |||
210 | QTCOMMERCIALCHART_END_NAMESPACE |
|
210 | QTCOMMERCIALCHART_END_NAMESPACE |
@@ -1,158 +1,161 | |||||
1 | #include "qchart.h" |
|
1 | #include "qchart.h" | |
2 | #include "qchartaxis.h" |
|
2 | #include "qchartaxis.h" | |
3 | #include "chartpresenter_p.h" |
|
3 | #include "chartpresenter_p.h" | |
4 | #include "chartdataset_p.h" |
|
4 | #include "chartdataset_p.h" | |
5 | #include <QGraphicsScene> |
|
5 | #include <QGraphicsScene> | |
6 | #include <QGraphicsSceneResizeEvent> |
|
6 | #include <QGraphicsSceneResizeEvent> | |
7 | #include <QDebug> |
|
7 | #include <QDebug> | |
8 |
|
8 | |||
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE |
|
9 | QTCOMMERCIALCHART_BEGIN_NAMESPACE | |
10 |
|
10 | |||
11 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), |
|
11 | QChart::QChart(QGraphicsItem *parent, Qt::WindowFlags wFlags) : QGraphicsWidget(parent,wFlags), | |
12 | m_backgroundItem(0), |
|
12 | m_backgroundItem(0), | |
13 | m_titleItem(0), |
|
13 | m_titleItem(0), | |
14 | m_dataset(new ChartDataSet(this)), |
|
14 | m_dataset(new ChartDataSet(this)), | |
15 | m_presenter(new ChartPresenter(this,m_dataset)) |
|
15 | m_presenter(new ChartPresenter(this,m_dataset)) | |
16 | { |
|
16 | { | |
17 | } |
|
17 | } | |
18 |
|
18 | |||
19 | QChart::~QChart() {} |
|
19 | QChart::~QChart() {} | |
20 |
|
20 | |||
21 | void QChart::addSeries(QChartSeries* series,QChartAxis* axisY) |
|
21 | void QChart::addSeries(QChartSeries* series,QChartAxis* axisY) | |
22 | { |
|
22 | { | |
23 | m_dataset->addSeries(series,axisY); |
|
23 | m_dataset->addSeries(series,axisY); | |
24 | } |
|
24 | } | |
25 |
|
25 | |||
26 | void QChart::removeSeries(QChartSeries* series) |
|
26 | void QChart::removeSeries(QChartSeries* series) | |
27 | { |
|
27 | { | |
28 | m_dataset->removeSeries(series); |
|
28 | m_dataset->removeSeries(series); | |
29 | } |
|
29 | } | |
30 |
|
30 | |||
31 | void QChart::removeAllSeries() |
|
31 | void QChart::removeAllSeries() | |
32 | { |
|
32 | { | |
33 | m_dataset->removeAllSeries(); |
|
33 | m_dataset->removeAllSeries(); | |
34 | } |
|
34 | } | |
35 |
|
35 | |||
36 | void QChart::setChartBackgroundBrush(const QBrush& brush) |
|
36 | void QChart::setChartBackgroundBrush(const QBrush& brush) | |
37 | { |
|
37 | { | |
38 | createChartBackgroundItem(); |
|
38 | createChartBackgroundItem(); | |
39 | m_backgroundItem->setBrush(brush); |
|
39 | m_backgroundItem->setBrush(brush); | |
40 | m_backgroundItem->update(); |
|
40 | m_backgroundItem->update(); | |
41 | } |
|
41 | } | |
42 |
|
42 | |||
43 | void QChart::setChartBackgroundPen(const QPen& pen) |
|
43 | void QChart::setChartBackgroundPen(const QPen& pen) | |
44 | { |
|
44 | { | |
45 | createChartBackgroundItem(); |
|
45 | createChartBackgroundItem(); | |
46 | m_backgroundItem->setPen(pen); |
|
46 | m_backgroundItem->setPen(pen); | |
47 | m_backgroundItem->update(); |
|
47 | m_backgroundItem->update(); | |
48 | } |
|
48 | } | |
49 |
|
49 | |||
50 | void QChart::setChartTitle(const QString& title) |
|
50 | void QChart::setChartTitle(const QString& title) | |
51 | { |
|
51 | { | |
52 | createChartTitleItem(); |
|
52 | createChartTitleItem(); | |
53 | m_titleItem->setPlainText(title); |
|
53 | m_titleItem->setPlainText(title); | |
54 | } |
|
54 | } | |
55 |
|
55 | |||
56 | void QChart::setChartTitleFont(const QFont& font) |
|
56 | void QChart::setChartTitleFont(const QFont& font) | |
57 | { |
|
57 | { | |
58 | createChartTitleItem(); |
|
58 | createChartTitleItem(); | |
59 | m_titleItem->setFont(font); |
|
59 | m_titleItem->setFont(font); | |
60 | } |
|
60 | } | |
61 |
|
61 | |||
62 | void QChart::createChartBackgroundItem() |
|
62 | void QChart::createChartBackgroundItem() | |
63 | { |
|
63 | { | |
64 | if(!m_backgroundItem) { |
|
64 | if(!m_backgroundItem) { | |
65 | m_backgroundItem = new QGraphicsRectItem(this); |
|
65 | m_backgroundItem = new QGraphicsRectItem(this); | |
66 |
m_backgroundItem->setZValue( |
|
66 | m_backgroundItem->setZValue(ChartPresenter::BackgroundZValue); | |
67 | } |
|
67 | } | |
68 | } |
|
68 | } | |
69 |
|
69 | |||
70 | void QChart::createChartTitleItem() |
|
70 | void QChart::createChartTitleItem() | |
71 | { |
|
71 | { | |
72 | if(!m_titleItem) m_titleItem = new QGraphicsTextItem(this); |
|
72 | if(!m_titleItem) { | |
|
73 | m_titleItem = new QGraphicsTextItem(this); | |||
|
74 | m_titleItem->setZValue(ChartPresenter::BackgroundZValue); | |||
|
75 | } | |||
73 | } |
|
76 | } | |
74 |
|
77 | |||
75 | int QChart::margin() const |
|
78 | int QChart::margin() const | |
76 | { |
|
79 | { | |
77 | return m_presenter->margin(); |
|
80 | return m_presenter->margin(); | |
78 | } |
|
81 | } | |
79 |
|
82 | |||
80 | void QChart::setMargin(int margin) |
|
83 | void QChart::setMargin(int margin) | |
81 | { |
|
84 | { | |
82 | m_presenter->setMargin(margin); |
|
85 | m_presenter->setMargin(margin); | |
83 | } |
|
86 | } | |
84 |
|
87 | |||
85 | void QChart::setChartTheme(QChart::ChartTheme theme) |
|
88 | void QChart::setChartTheme(QChart::ChartTheme theme) | |
86 | { |
|
89 | { | |
87 | m_presenter->setChartTheme(theme); |
|
90 | m_presenter->setChartTheme(theme); | |
88 | } |
|
91 | } | |
89 |
|
92 | |||
90 | QChart::ChartTheme QChart::chartTheme() const |
|
93 | QChart::ChartTheme QChart::chartTheme() const | |
91 | { |
|
94 | { | |
92 | return m_presenter->chartTheme(); |
|
95 | return m_presenter->chartTheme(); | |
93 | } |
|
96 | } | |
94 |
|
97 | |||
95 | void QChart::zoomIn() |
|
98 | void QChart::zoomIn() | |
96 | { |
|
99 | { | |
97 | if (!m_dataset->nextDomain()) { |
|
100 | if (!m_dataset->nextDomain()) { | |
98 | QRectF rect = m_presenter->geometry(); |
|
101 | QRectF rect = m_presenter->geometry(); | |
99 | rect.setWidth(rect.width()/2); |
|
102 | rect.setWidth(rect.width()/2); | |
100 | rect.setHeight(rect.height()/2); |
|
103 | rect.setHeight(rect.height()/2); | |
101 | rect.moveCenter(m_presenter->geometry().center()); |
|
104 | rect.moveCenter(m_presenter->geometry().center()); | |
102 | zoomIn(rect); |
|
105 | zoomIn(rect); | |
103 | } |
|
106 | } | |
104 | } |
|
107 | } | |
105 |
|
108 | |||
106 | void QChart::zoomIn(const QRectF& rect) |
|
109 | void QChart::zoomIn(const QRectF& rect) | |
107 | { |
|
110 | { | |
108 | if(!rect.isValid()) return; |
|
111 | if(!rect.isValid()) return; | |
109 | QRectF r = rect.normalized(); |
|
112 | QRectF r = rect.normalized(); | |
110 | int margin = m_presenter->margin(); |
|
113 | int margin = m_presenter->margin(); | |
111 | r.translate(-margin, -margin); |
|
114 | r.translate(-margin, -margin); | |
112 | m_dataset->addDomain(r,m_presenter->geometry()); |
|
115 | m_dataset->addDomain(r,m_presenter->geometry()); | |
113 | } |
|
116 | } | |
114 |
|
117 | |||
115 | void QChart::zoomOut() |
|
118 | void QChart::zoomOut() | |
116 | { |
|
119 | { | |
117 | m_dataset->previousDomain(); |
|
120 | m_dataset->previousDomain(); | |
118 | } |
|
121 | } | |
119 |
|
122 | |||
120 | void QChart::zoomReset() |
|
123 | void QChart::zoomReset() | |
121 | { |
|
124 | { | |
122 | m_dataset->clearDomains(); |
|
125 | m_dataset->clearDomains(); | |
123 | } |
|
126 | } | |
124 |
|
127 | |||
125 | QChartAxis* QChart::axisX() const |
|
128 | QChartAxis* QChart::axisX() const | |
126 | { |
|
129 | { | |
127 | return m_dataset->axisX(); |
|
130 | return m_dataset->axisX(); | |
128 | } |
|
131 | } | |
129 |
|
132 | |||
130 | QChartAxis* QChart::axisY() const |
|
133 | QChartAxis* QChart::axisY() const | |
131 | { |
|
134 | { | |
132 | return m_dataset->axisY(); |
|
135 | return m_dataset->axisY(); | |
133 | } |
|
136 | } | |
134 |
|
137 | |||
135 | void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) |
|
138 | void QChart::resizeEvent(QGraphicsSceneResizeEvent *event) | |
136 | { |
|
139 | { | |
137 |
|
140 | |||
138 | m_rect = QRectF(QPoint(0,0),event->newSize()); |
|
141 | m_rect = QRectF(QPoint(0,0),event->newSize()); | |
139 | QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); |
|
142 | QRectF rect = m_rect.adjusted(margin(),margin(), -margin(), -margin()); | |
140 |
|
143 | |||
141 | // recalculate title position |
|
144 | // recalculate title position | |
142 | if (m_titleItem) { |
|
145 | if (m_titleItem) { | |
143 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); |
|
146 | QPointF center = m_rect.center() -m_titleItem->boundingRect().center(); | |
144 | m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2); |
|
147 | m_titleItem->setPos(center.x(),m_rect.top()/2 + margin()/2); | |
145 | } |
|
148 | } | |
146 |
|
149 | |||
147 | //recalculate background gradient |
|
150 | //recalculate background gradient | |
148 | if (m_backgroundItem) { |
|
151 | if (m_backgroundItem) { | |
149 | m_backgroundItem->setRect(rect); |
|
152 | m_backgroundItem->setRect(rect); | |
150 | } |
|
153 | } | |
151 |
|
154 | |||
152 | QGraphicsWidget::resizeEvent(event); |
|
155 | QGraphicsWidget::resizeEvent(event); | |
153 | update(); |
|
156 | update(); | |
154 | } |
|
157 | } | |
155 |
|
158 | |||
156 | #include "moc_qchart.cpp" |
|
159 | #include "moc_qchart.cpp" | |
157 |
|
160 | |||
158 | QTCOMMERCIALCHART_END_NAMESPACE |
|
161 | QTCOMMERCIALCHART_END_NAMESPACE |
General Comments 0
You need to be logged in to leave comments.
Login now