##// END OF EJS Templates
Refactors axis layout managment...
Michal Klocek -
r291:2b7ffa40a22f
parent child
Show More
@@ -0,0 +1,70
1 #include "axisanimationitem_p.h"
2 #include <QPropertyAnimation>
3
4 Q_DECLARE_METATYPE(QVector<qreal>)
5
6 QTCOMMERCIALCHART_BEGIN_NAMESPACE
7
8 const static int duration = 500;
9
10 AxisAnimationItem::AxisAnimationItem(AxisType type,QGraphicsItem* parent) :
11 AxisItem(type,parent)
12 {
13 }
14
15 AxisAnimationItem::~AxisAnimationItem()
16 {
17 }
18
19 void AxisAnimationItem::updateItems(QVector<qreal>& vector1)
20 {
21 QVector<qreal> vector0 = vector1;
22 calculateLayout(vector1);
23 if(vector1.count()==0) return;
24 vector0.resize(vector1.size());
25
26 AxisAnimator *animation = new AxisAnimator(this);
27 animation->setDuration(duration);
28 animation->setEasingCurve(QEasingCurve::InOutBack);
29 animation->setKeyValueAt(0.0, qVariantFromValue(vector0));
30 animation->setKeyValueAt(1.0, qVariantFromValue(vector1));
31 animation->start(QAbstractAnimation::DeleteWhenStopped);
32 }
33
34 void AxisAnimationItem::setLabelsAngle(int angle)
35 {
36 AxisItem::setLabelsAngle(angle);
37 }
38
39 AxisAnimator::AxisAnimator(AxisItem *axis): m_axis(axis)
40 {
41 }
42
43 AxisAnimator::~AxisAnimator()
44 {
45 }
46
47 QVariant AxisAnimator::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const
48 {
49 QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start);
50 QVector<qreal> endVecotr = qVariantValue<QVector<qreal> >(end);
51 QVector<qreal> result;
52 Q_ASSERT(startVector.count() == endVecotr.count());
53
54 for(int i =0 ;i< startVector.count();i++){
55 qreal value = startVector[i] + ((endVecotr[i]- startVector[i]) * progress);//qBound(0.0, progress, 1.0));
56 result << value;
57 }
58 return qVariantFromValue(result);
59 }
60
61
62 void AxisAnimator::updateCurrentValue (const QVariant & value )
63 {
64 QVector<qreal> vector = qVariantValue<QVector<qreal> >(value);
65 m_axis->applyLayout(vector);
66 }
67
68 #include "moc_axisanimationitem_p.cpp"
69
70 QTCOMMERCIALCHART_END_NAMESPACE
@@ -0,0 +1,44
1 #ifndef AXISANIMATIONITEM_H_
2 #define AXISANIMATIONITEM_H_
3
4 #include "domain_p.h"
5 #include "axisitem_p.h"
6 #include <QGraphicsItem>
7 #include <QVariantAnimation>
8
9 QTCOMMERCIALCHART_BEGIN_NAMESPACE
10
11 class QChartAxis;
12
13 class AxisAnimationItem : public AxisItem
14 {
15 Q_OBJECT
16
17 public:
18 AxisAnimationItem(AxisType type = X_AXIS,QGraphicsItem* parent = 0);
19 ~AxisAnimationItem();
20
21 void setLabelsAngle(int angle);
22
23 protected:
24 void updateItems(QVector<qreal>& vector);
25
26 };
27
28 class AxisAnimator: public QVariantAnimation
29 {
30 public:
31 AxisAnimator(AxisItem *axis);
32 virtual ~AxisAnimator();
33 protected:
34 virtual QVariant interpolated (const QVariant & from, const QVariant & to, qreal progress ) const;
35 virtual void updateCurrentValue (const QVariant & value );
36 private:
37 AxisItem* m_axis;
38 };
39
40 QTCOMMERCIALCHART_END_NAMESPACE
41
42
43
44 #endif /* AXISITEM_H_ */
@@ -1,316 +1,333
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 m_rect;
33 return QRectF();
34 }
34 }
35
35
36 void AxisItem::createItems(int count)
36 void AxisItem::createItems(int count)
37 {
37 {
38 m_axis.addToGroup(new QGraphicsLineItem(this));
38 if(m_axis.children().size()==0)
39 m_axis.addToGroup(new QGraphicsLineItem());
39 for (int i = 0; i < count; ++i) {
40 for (int i = 0; i < count; ++i) {
40 m_grid.addToGroup(new QGraphicsLineItem(this));
41 m_grid.addToGroup(new QGraphicsLineItem());
41 m_labels.addToGroup(new QGraphicsSimpleTextItem(this));
42 m_labels.addToGroup(new QGraphicsSimpleTextItem());
42 if(i%2) m_shades.addToGroup(new QGraphicsRectItem(this));
43 if(m_grid.childItems().size()%2) m_shades.addToGroup(new QGraphicsRectItem());
43 m_axis.addToGroup(new QGraphicsLineItem(this));
44 m_axis.addToGroup(new QGraphicsLineItem());
44 }
45 }
45 }
46 }
46
47
47 void AxisItem::clear()
48 void AxisItem::clear(int count)
48 {
49 {
49 foreach(QGraphicsItem* item , m_shades.childItems()) {
50 QList<QGraphicsItem *> lines = m_grid.childItems();
50 delete item;
51 QList<QGraphicsItem *> labels = m_labels.childItems();
51 }
52 QList<QGraphicsItem *> shades = m_shades.childItems();
52
53 QList<QGraphicsItem *> axis = m_axis.childItems();
53 foreach(QGraphicsItem* item , m_grid.childItems()) {
54 delete item;
55 }
56
57 foreach(QGraphicsItem* item , m_labels.childItems()) {
58 delete item;
59 }
60
54
61 foreach(QGraphicsItem* item , m_axis.childItems()) {
55 for (int i = 0; i < count; ++i) {
62 delete item;
56 delete(lines.takeLast());
57 delete(labels.takeLast());
58 if(lines.size()%2) delete(shades.takeLast());
59 delete(axis.takeLast());
63 }
60 }
64
61
65 m_thicksList.clear();
62 m_thicksList.clear();
66
63
67 }
64 }
68
65
69 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
66 void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
70 {
67 {
71
68 Q_UNUSED(painter);
69 Q_UNUSED(option);
70 Q_UNUSED(widget);
72 }
71 }
73
72
74 void AxisItem::updateItem(int count)
73 void AxisItem::updateItems(QVector<qreal>& vector)
75 {
74 {
76 if(count ==0) return;
75 calculateLayout(vector);
77
76 if(vector.count()==0) return;
78 QList<QGraphicsItem *> lines = m_grid.childItems();
77 applyLayout(vector);
79 QList<QGraphicsItem *> labels = m_labels.childItems();
80 QList<QGraphicsItem *> shades = m_shades.childItems();
81 QList<QGraphicsItem *> axis = m_axis.childItems();
82
83 switch (m_type)
84 {
85 case X_AXIS:
86 {
87 const qreal deltaX = m_rect.width() / (count-1);
88
89 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
90 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
91
92 for (int i = 0; i < count; ++i) {
93 int x = i * deltaX + m_rect.left();
94 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
95 lineItem->setLine(x, m_rect.top(), x, m_rect.bottom());
96 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
97 labelItem->setText(m_thicksList.at(i));
98 QPointF center = labelItem->boundingRect().center();
99 labelItem->setTransformOriginPoint(center.x(), center.y());
100 labelItem->setPos(x - center.x(), m_rect.bottom() + label_padding);
101 if(i%2){
102 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
103 rectItem->setRect(x,m_rect.top(),deltaX,m_rect.height());
104 }
105 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
106 lineItem->setLine(x,m_rect.bottom(),x,m_rect.bottom()+5);
107 }
108 }
109 break;
110
111 case Y_AXIS:
112 {
113 const qreal deltaY = m_rect.height()/ (count-1);
114
115 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
116 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
117
118 for (int i = 0; i < count; ++i) {
119 int y = i * -deltaY + m_rect.bottom();
120 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
121 lineItem->setLine(m_rect.left() , y, m_rect.right(), y);
122 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
123 labelItem->setText(m_thicksList.at(i));
124 QPointF center = labelItem->boundingRect().center();
125 labelItem->setTransformOriginPoint(center.x(), center.y());
126 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , y-center.y());
127 if(i%2){
128 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
129 rectItem->setRect(m_rect.left(),y,m_rect.width(),deltaY);
130 }
131 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
132 lineItem->setLine(m_rect.left()-5,y,m_rect.left(),y);
133 }
134 }
135 break;
136 default:
137 qDebug()<<"Unknown axis type";
138 break;
139 }
140 }
78 }
141
79
142 void AxisItem::handleAxisUpdate(QChartAxis* axis)
80 void AxisItem::handleAxisUpdate(QChartAxis* axis)
143 {
81 {
144 if(axis->isAxisVisible()) {
82 if(axis->isAxisVisible()) {
145 setAxisOpacity(100);
83 setAxisOpacity(100);
146 }
84 }
147 else {
85 else {
148 setAxisOpacity(0);
86 setAxisOpacity(0);
149 }
87 }
150
88
151 if(axis->isGridVisible()) {
89 if(axis->isGridVisible()) {
152 setGridOpacity(100);
90 setGridOpacity(100);
153 }
91 }
154 else {
92 else {
155 setGridOpacity(0);
93 setGridOpacity(0);
156 }
94 }
157
95
158 if(axis->isLabelsVisible())
96 if(axis->isLabelsVisible())
159 {
97 {
160 setLabelsOpacity(100);
98 setLabelsOpacity(100);
161 }
99 }
162 else {
100 else {
163 setLabelsOpacity(0);
101 setLabelsOpacity(0);
164 }
102 }
165
103
166 if(axis->isShadesVisible()) {
104 if(axis->isShadesVisible()) {
167 setShadesOpacity(axis->shadesOpacity());
105 setShadesOpacity(axis->shadesOpacity());
168 }
106 }
169 else {
107 else {
170 setShadesOpacity(0);
108 setShadesOpacity(0);
171 }
109 }
172
110
173 setLabelsAngle(axis->labelsAngle());
111 setLabelsAngle(axis->labelsAngle());
174 setAxisPen(axis->axisPen());
112 setAxisPen(axis->axisPen());
175 setLabelsPen(axis->labelsPen());
113 setLabelsPen(axis->labelsPen());
176 setLabelsBrush(axis->labelsBrush());
114 setLabelsBrush(axis->labelsBrush());
177 setLabelsFont(axis->labelFont());
115 setLabelsFont(axis->labelFont());
178 setGridPen(axis->gridPen());
116 setGridPen(axis->gridPen());
179 setShadesPen(axis->shadesPen());
117 setShadesPen(axis->shadesPen());
180 setShadesBrush(axis->shadesBrush());
118 setShadesBrush(axis->shadesBrush());
181 }
119 }
182
120
183 void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels)
121 void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels)
184 {
122 {
185 m_thicksList=labels;
123 int diff = m_thicksList.size() - labels.size();
186 QList<QGraphicsItem*> items = m_labels.childItems();
124
187 //if(items.size()!=m_thicksList.size()){
125 if(diff>0){
188 clear();
126 clear(diff);
189 m_thicksList=labels;
127 }else if(diff<0){
190 createItems(m_thicksList.size());
128 createItems(-diff);
191 updateItem(m_thicksList.size());
192 items = m_labels.childItems();
193 handleAxisUpdate(axis);
194 // }
195
196 Q_ASSERT(items.size()==m_thicksList.size());
197
198 int i=0;
199 foreach(QGraphicsItem* item, items){
200 static_cast<QGraphicsSimpleTextItem*>(item)->setText(m_thicksList.at(i));
201 i++;
202 }
129 }
203 update();
130 m_thicksList=labels;
131 m_layoutVector.resize(m_thicksList.size());
132 updateItems(m_layoutVector);
133 handleAxisUpdate(axis);
204 }
134 }
205
135
206 void AxisItem::handleGeometryChanged(const QRectF& rect)
136 void AxisItem::handleGeometryChanged(const QRectF& rect)
207 {
137 {
208 m_rect = rect;
138 m_rect = rect;
209 updateItem(m_thicksList.size());
139 updateItems(m_layoutVector);
210 update();
211 }
140 }
212
141
213 void AxisItem::setAxisOpacity(qreal opacity)
142 void AxisItem::setAxisOpacity(qreal opacity)
214 {
143 {
215 m_axis.setOpacity(opacity);
144 m_axis.setOpacity(opacity);
216 }
145 }
217
146
218 qreal AxisItem::axisOpacity() const
147 qreal AxisItem::axisOpacity() const
219 {
148 {
220 return m_axis.opacity();
149 return m_axis.opacity();
221 }
150 }
222
151
223 void AxisItem::setGridOpacity(qreal opacity)
152 void AxisItem::setGridOpacity(qreal opacity)
224 {
153 {
225 m_grid.setOpacity(opacity);
154 m_grid.setOpacity(opacity);
226 }
155 }
227
156
228 qreal AxisItem::gridOpacity() const
157 qreal AxisItem::gridOpacity() const
229 {
158 {
230 return m_grid.opacity();
159 return m_grid.opacity();
231 }
160 }
232
161
233 void AxisItem::setLabelsOpacity(qreal opacity)
162 void AxisItem::setLabelsOpacity(qreal opacity)
234 {
163 {
235 m_labels.setOpacity(opacity);
164 m_labels.setOpacity(opacity);
236 }
165 }
237
166
238 qreal AxisItem::labelsOpacity() const
167 qreal AxisItem::labelsOpacity() const
239 {
168 {
240 return m_labels.opacity();
169 return m_labels.opacity();
241 }
170 }
242
171
243 void AxisItem::setShadesOpacity(qreal opacity)
172 void AxisItem::setShadesOpacity(qreal opacity)
244 {
173 {
245 m_shades.setOpacity(opacity);
174 m_shades.setOpacity(opacity);
246 }
175 }
247
176
248 qreal AxisItem::shadesOpacity() const
177 qreal AxisItem::shadesOpacity() const
249 {
178 {
250 return m_shades.opacity();
179 return m_shades.opacity();
251 }
180 }
252
181
253 void AxisItem::setLabelsAngle(int angle)
182 void AxisItem::setLabelsAngle(int angle)
254 {
183 {
255 foreach(QGraphicsItem* item , m_labels.childItems()) {
184 foreach(QGraphicsItem* item , m_labels.childItems()) {
256 QPointF center = item->boundingRect().center();
185 QPointF center = item->boundingRect().center();
257 item->setRotation(angle);
186 item->setRotation(angle);
258 }
187 }
259
188
260 m_labelsAngle=angle;
189 m_labelsAngle=angle;
261 }
190 }
262
191
263 void AxisItem::setLabelsPen(const QPen& pen)
192 void AxisItem::setLabelsPen(const QPen& pen)
264 {
193 {
265 foreach(QGraphicsItem* item , m_labels.childItems()) {
194 foreach(QGraphicsItem* item , m_labels.childItems()) {
266 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
195 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
267 }
196 }
268 }
197 }
269
198
270 void AxisItem::setLabelsBrush(const QBrush& brush)
199 void AxisItem::setLabelsBrush(const QBrush& brush)
271 {
200 {
272 foreach(QGraphicsItem* item , m_labels.childItems()) {
201 foreach(QGraphicsItem* item , m_labels.childItems()) {
273 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
202 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
274 }
203 }
275 }
204 }
276
205
277 void AxisItem::setLabelsFont(const QFont& font)
206 void AxisItem::setLabelsFont(const QFont& font)
278 {
207 {
279 foreach(QGraphicsItem* item , m_labels.childItems()) {
208 foreach(QGraphicsItem* item , m_labels.childItems()) {
280 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
209 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
281 }
210 }
282 }
211 }
283
212
284 void AxisItem::setShadesBrush(const QBrush& brush)
213 void AxisItem::setShadesBrush(const QBrush& brush)
285 {
214 {
286 foreach(QGraphicsItem* item , m_shades.childItems()) {
215 foreach(QGraphicsItem* item , m_shades.childItems()) {
287 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
216 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
288 }
217 }
289 }
218 }
290
219
291 void AxisItem::setShadesPen(const QPen& pen)
220 void AxisItem::setShadesPen(const QPen& pen)
292 {
221 {
293 foreach(QGraphicsItem* item , m_shades.childItems()) {
222 foreach(QGraphicsItem* item , m_shades.childItems()) {
294 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
223 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
295 }
224 }
296 }
225 }
297
226
298 void AxisItem::setAxisPen(const QPen& pen)
227 void AxisItem::setAxisPen(const QPen& pen)
299 {
228 {
300 foreach(QGraphicsItem* item , m_axis.childItems()) {
229 foreach(QGraphicsItem* item , m_axis.childItems()) {
301 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
230 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
302 }
231 }
303 }
232 }
304
233
305 void AxisItem::setGridPen(const QPen& pen)
234 void AxisItem::setGridPen(const QPen& pen)
306 {
235 {
307 foreach(QGraphicsItem* item , m_grid.childItems()) {
236 foreach(QGraphicsItem* item , m_grid.childItems()) {
308 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
237 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
309 }
238 }
310 }
239 }
311
240
241 void AxisItem::calculateLayout(QVector<qreal>& points)
242 {
243 switch (m_type)
244 {
245 case X_AXIS:
246 {
247 const qreal deltaX = m_rect.width()/(m_thicksList.size()-1);
248 for (int i = 0; i < m_thicksList.size(); ++i) {
249 int x = i * deltaX + m_rect.left();
250 points[i]=x;
251 }
252 }
253 break;
254 case Y_AXIS:
255 {
256 const qreal deltaY = m_rect.height()/(m_thicksList.size()-1);
257 for (int i = 0; i < m_thicksList.size(); ++i) {
258 int y = i * -deltaY + m_rect.bottom();
259 points[i]=y;
260 }
261 }
262 break;
263 }
264 }
265
266 void AxisItem::applyLayout(const QVector<qreal>& points)
267 {
268 Q_ASSERT(points.size() == m_thicksList.size());
269
270 QList<QGraphicsItem *> lines = m_grid.childItems();
271 QList<QGraphicsItem *> labels = m_labels.childItems();
272 QList<QGraphicsItem *> shades = m_shades.childItems();
273 QList<QGraphicsItem *> axis = m_axis.childItems();
274
275 Q_ASSERT(labels.size() == m_thicksList.size());
276
277 switch (m_type)
278 {
279 case X_AXIS:
280 {
281 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
282 lineItem->setLine(m_rect.left(), m_rect.bottom(), m_rect.right(), m_rect.bottom());
283
284 for (int i = 0; i < points.size(); ++i) {
285 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
286 lineItem->setLine(points[i], m_rect.top(), points[i], m_rect.bottom());
287 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
288 labelItem->setText(m_thicksList.at(i));
289 QPointF center = labelItem->boundingRect().center();
290 labelItem->setTransformOriginPoint(center.x(), center.y());
291 labelItem->setPos(points[i] - center.x(), m_rect.bottom() + label_padding);
292 if(i%2){
293 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
294 rectItem->setRect(points[i],m_rect.top(),points[i+1]-points[i],m_rect.height());
295 }
296 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
297 lineItem->setLine(points[i],m_rect.bottom(),points[i],m_rect.bottom()+5);
298 }
299 }
300 break;
301
302 case Y_AXIS:
303 {
304 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
305 lineItem->setLine(m_rect.left() , m_rect.top(), m_rect.left(), m_rect.bottom());
306
307 for (int i = 0; i < points.size(); ++i) {
308 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(lines.at(i));
309 lineItem->setLine(m_rect.left() , points[i], m_rect.right(), points[i]);
310 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
311 labelItem->setText(m_thicksList.at(i));
312 QPointF center = labelItem->boundingRect().center();
313 labelItem->setTransformOriginPoint(center.x(), center.y());
314 labelItem->setPos(m_rect.left() - labelItem->boundingRect().width() - label_padding , points[i]-center.y());
315 if(i%2){
316 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2));
317 rectItem->setRect(m_rect.left(),points[i],m_rect.width(),points[i]-points[i+1]);
318 }
319 lineItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
320 lineItem->setLine(m_rect.left()-5,points[i],m_rect.left(),points[i]);
321 }
322 }
323 break;
324 default:
325 qDebug()<<"Unknown axis type";
326 break;
327 }
328 }
312
329
313 //TODO "nice numbers algorithm"
330 //TODO "nice numbers algorithm"
314 #include "moc_axisitem_p.cpp"
331 #include "moc_axisitem_p.cpp"
315
332
316 QTCOMMERCIALCHART_END_NAMESPACE
333 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,75 +1,78
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); //look and feel
53 void handleAxisUpdate(QChartAxis* axis); //look and feel
54 void handleLabelsChanged(QChartAxis* axis,const QStringList& labels); //labels from dataset
54 void handleLabelsChanged(QChartAxis* axis,const QStringList& labels); //labels from dataset
55 void handleGeometryChanged(const QRectF& size); // geometry from presenter
55 void handleGeometryChanged(const QRectF& size); // geometry from presenter
56 protected:
56 public:
57 void updateItem(int count);
57 virtual void updateItems(QVector<qreal>& points);
58 virtual void calculateLayout(QVector<qreal>& points);
59 virtual void applyLayout(const QVector<qreal>& points);
58 private:
60 private:
59 void clear();
61 void clear(int count);
60 void createItems(int count);
62 void createItems(int count);
61 private:
63 private:
62 AxisType m_type;
64 AxisType m_type;
63 QRectF m_rect;
65 QRectF m_rect;
64 int m_labelsAngle;
66 int m_labelsAngle;
65 QGraphicsItemGroup m_grid;
67 QGraphicsItemGroup m_grid;
66 QGraphicsItemGroup m_shades;
68 QGraphicsItemGroup m_shades;
67 QGraphicsItemGroup m_labels;
69 QGraphicsItemGroup m_labels;
68 QGraphicsItemGroup m_axis;
70 QGraphicsItemGroup m_axis;
69 QStringList m_thicksList;
71 QStringList m_thicksList;
72 QVector<qreal> m_layoutVector;
70
73
71 };
74 };
72
75
73 QTCOMMERCIALCHART_END_NAMESPACE
76 QTCOMMERCIALCHART_END_NAMESPACE
74
77
75 #endif /* AXISITEM_H_ */
78 #endif /* AXISITEM_H_ */
@@ -1,92 +1,94
1 !include( ../common.pri ):error( Couldn't find the common.pri file! )
1 !include( ../common.pri ):error( Couldn't find the common.pri file! )
2 TARGET = QtCommercialChart
2 TARGET = QtCommercialChart
3 DESTDIR = $$CHART_BUILD_LIB_DIR
3 DESTDIR = $$CHART_BUILD_LIB_DIR
4 TEMPLATE = lib
4 TEMPLATE = lib
5 QT += core \
5 QT += core \
6 gui
6 gui
7 CONFIG += debug_and_release
7 CONFIG += debug_and_release
8 CONFIG(debug, debug|release):TARGET = QtCommercialChartd
8 CONFIG(debug, debug|release):TARGET = QtCommercialChartd
9 SOURCES += \
9 SOURCES += \
10 axisitem.cpp \
10 axisitem.cpp \
11 axisanimationitem.cpp \
11 chartdataset.cpp \
12 chartdataset.cpp \
12 chartpresenter.cpp \
13 chartpresenter.cpp \
13 charttheme.cpp \
14 charttheme.cpp \
14 domain.cpp \
15 domain.cpp \
15 qchart.cpp \
16 qchart.cpp \
16 qchartaxis.cpp \
17 qchartaxis.cpp \
17 qchartseries.cpp \
18 qchartseries.cpp \
18 qchartview.cpp
19 qchartview.cpp
19 PRIVATE_HEADERS += \
20 PRIVATE_HEADERS += \
20 axisitem_p.h \
21 axisitem_p.h \
22 axisanimationitem_p.h \
21 chartdataset_p.h \
23 chartdataset_p.h \
22 chartitem_p.h \
24 chartitem_p.h \
23 chartpresenter_p.h \
25 chartpresenter_p.h \
24 charttheme_p.h \
26 charttheme_p.h \
25 domain_p.h
27 domain_p.h
26 PUBLIC_HEADERS += \
28 PUBLIC_HEADERS += \
27 qchart.h \
29 qchart.h \
28 qchartaxis.h \
30 qchartaxis.h \
29 qchartglobal.h \
31 qchartglobal.h \
30 qchartseries.h \
32 qchartseries.h \
31 qchartview.h \
33 qchartview.h \
32
34
33 include(linechart/linechart.pri)
35 include(linechart/linechart.pri)
34 include(barchart/barchart.pri)
36 include(barchart/barchart.pri)
35 include(piechart/piechart.pri)
37 include(piechart/piechart.pri)
36 include(scatterseries/scatter.pri)
38 include(scatterseries/scatter.pri)
37
39
38 THEMES += themes/chartthemeicy_p.h \
40 THEMES += themes/chartthemeicy_p.h \
39 themes/chartthemegrayscale_p.h \
41 themes/chartthemegrayscale_p.h \
40 themes/chartthemescientific_p.h \
42 themes/chartthemescientific_p.h \
41 themes/chartthemevanilla_p.h
43 themes/chartthemevanilla_p.h
42 HEADERS += $$PUBLIC_HEADERS
44 HEADERS += $$PUBLIC_HEADERS
43 HEADERS += $$PRIVATE_HEADERS
45 HEADERS += $$PRIVATE_HEADERS
44 HEADERS += $$THEMES
46 HEADERS += $$THEMES
45 INCLUDEPATH += linechart \
47 INCLUDEPATH += linechart \
46 barchart \
48 barchart \
47 themes \
49 themes \
48 .
50 .
49 OBJECTS_DIR = $$CHART_BUILD_DIR/lib
51 OBJECTS_DIR = $$CHART_BUILD_DIR/lib
50 MOC_DIR = $$CHART_BUILD_DIR/lib
52 MOC_DIR = $$CHART_BUILD_DIR/lib
51 UI_DIR = $$CHART_BUILD_DIR/lib
53 UI_DIR = $$CHART_BUILD_DIR/lib
52 RCC_DIR = $$CHART_BUILD_DIR/lib
54 RCC_DIR = $$CHART_BUILD_DIR/lib
53 DEFINES += QTCOMMERCIALCHART_LIBRARY
55 DEFINES += QTCOMMERCIALCHART_LIBRARY
54 public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart
56 public_headers.path = $$[QT_INSTALL_HEADERS]/QtCommercialChart
55 public_headers.files = $$PUBLIC_HEADERS
57 public_headers.files = $$PUBLIC_HEADERS
56 target.path = $$[QT_INSTALL_LIBS]
58 target.path = $$[QT_INSTALL_LIBS]
57 INSTALLS += target \
59 INSTALLS += target \
58 public_headers
60 public_headers
59 install_build_public_headers.name = bild_public_headers
61 install_build_public_headers.name = bild_public_headers
60 install_build_public_headers.output = $$CHART_BUILD_PUBLIC_HEADER_DIR/${QMAKE_FILE_BASE}.h
62 install_build_public_headers.output = $$CHART_BUILD_PUBLIC_HEADER_DIR/${QMAKE_FILE_BASE}.h
61 install_build_public_headers.input = PUBLIC_HEADERS
63 install_build_public_headers.input = PUBLIC_HEADERS
62 install_build_public_headers.commands = $$QMAKE_COPY \
64 install_build_public_headers.commands = $$QMAKE_COPY \
63 ${QMAKE_FILE_NAME} \
65 ${QMAKE_FILE_NAME} \
64 $$CHART_BUILD_PUBLIC_HEADER_DIR
66 $$CHART_BUILD_PUBLIC_HEADER_DIR
65 install_build_public_headers.CONFIG += target_predeps \
67 install_build_public_headers.CONFIG += target_predeps \
66 no_link
68 no_link
67 install_build_private_headers.name = bild_private_headers
69 install_build_private_headers.name = bild_private_headers
68 install_build_private_headers.output = $$CHART_BUILD_PRIVATE_HEADER_DIR/${QMAKE_FILE_BASE}.h
70 install_build_private_headers.output = $$CHART_BUILD_PRIVATE_HEADER_DIR/${QMAKE_FILE_BASE}.h
69 install_build_private_headers.input = PRIVATE_HEADERS
71 install_build_private_headers.input = PRIVATE_HEADERS
70 install_build_private_headers.commands = $$QMAKE_COPY \
72 install_build_private_headers.commands = $$QMAKE_COPY \
71 ${QMAKE_FILE_NAME} \
73 ${QMAKE_FILE_NAME} \
72 $$CHART_BUILD_PRIVATE_HEADER_DIR
74 $$CHART_BUILD_PRIVATE_HEADER_DIR
73 install_build_private_headers.CONFIG += target_predeps \
75 install_build_private_headers.CONFIG += target_predeps \
74 no_link
76 no_link
75 QMAKE_EXTRA_COMPILERS += install_build_public_headers install_build_private_headers
77 QMAKE_EXTRA_COMPILERS += install_build_public_headers install_build_private_headers
76 chartversion.target = qchartversion_p.h
78 chartversion.target = qchartversion_p.h
77 chartversion.commands = @echo \
79 chartversion.commands = @echo \
78 "build_time" \
80 "build_time" \
79 > \
81 > \
80 $$chartversion.target;
82 $$chartversion.target;
81 chartversion.depends = $$HEADERS \
83 chartversion.depends = $$HEADERS \
82 $$SOURCES
84 $$SOURCES
83 PRE_TARGETDEPS += qchartversion_p.h
85 PRE_TARGETDEPS += qchartversion_p.h
84 QMAKE_CLEAN += qchartversion_p.h
86 QMAKE_CLEAN += qchartversion_p.h
85 QMAKE_EXTRA_TARGETS += chartversion
87 QMAKE_EXTRA_TARGETS += chartversion
86 unix:QMAKE_DISTCLEAN += -r \
88 unix:QMAKE_DISTCLEAN += -r \
87 $$CHART_BUILD_HEADER_DIR \
89 $$CHART_BUILD_HEADER_DIR \
88 $$CHART_BUILD_LIB_DIR
90 $$CHART_BUILD_LIB_DIR
89 win32:QMAKE_DISTCLEAN += /Q \
91 win32:QMAKE_DISTCLEAN += /Q \
90 $$CHART_BUILD_HEADER_DIR \
92 $$CHART_BUILD_HEADER_DIR \
91 $$CHART_BUILD_LIB_DIR
93 $$CHART_BUILD_LIB_DIR
92
94
General Comments 0
You need to be logged in to leave comments. Login now