##// END OF EJS Templates
Remove setter for labelinBeetwen flag from axis
Michal Klocek -
r2118:7942a7e7245e
parent child
Show More
@@ -1,148 +1,148
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartbarcategoryaxisx_p.h"
21 #include "chartbarcategoryaxisx_p.h"
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "qbarcategoryaxis_p.h"
23 #include "qbarcategoryaxis_p.h"
24 #include <QFontMetrics>
24 #include <QFontMetrics>
25 #include <QDebug>
25 #include <QDebug>
26 #include <qmath.h>
26 #include <qmath.h>
27
27
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29
29
30 ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis,ChartPresenter *presenter) : HorizontalAxis(axis,presenter),
30 ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis,ChartPresenter *presenter) : HorizontalAxis(axis,presenter,true),
31 m_categoriesAxis(axis)
31 m_categoriesAxis(axis)
32 {
32 {
33 setLabelBetweenTicks(true);
33
34 }
34 }
35
35
36 ChartBarCategoryAxisX::~ChartBarCategoryAxisX()
36 ChartBarCategoryAxisX::~ChartBarCategoryAxisX()
37 {
37 {
38 }
38 }
39
39
40 QVector<qreal> ChartBarCategoryAxisX::calculateLayout() const
40 QVector<qreal> ChartBarCategoryAxisX::calculateLayout() const
41 {
41 {
42 int count = m_categoriesAxis->d_ptr->count();
42 int count = m_categoriesAxis->d_ptr->count();
43
43
44 Q_ASSERT(count>=1);
44 Q_ASSERT(count>=1);
45
45
46 QVector<qreal> points;
46 QVector<qreal> points;
47 points.resize(count+2);
47 points.resize(count+2);
48
48
49 const QRectF& gridRect = gridGeometry();
49 const QRectF& gridRect = gridGeometry();
50
50
51 const qreal delta = gridRect.width()/(count);
51 const qreal delta = gridRect.width()/(count);
52 qreal offset =-min()-0.5;
52 qreal offset =-min()-0.5;
53
53
54 if(delta<1) return points;
54 if(delta<1) return points;
55
55
56 if(offset<0) {
56 if(offset<0) {
57 offset = int(offset * gridRect.width()/(max() - min()))%int(delta) + delta;
57 offset = int(offset * gridRect.width()/(max() - min()))%int(delta) + delta;
58 }
58 }
59 else {
59 else {
60 offset = int(offset * gridRect.width()/(max() - min()))%int(delta);
60 offset = int(offset * gridRect.width()/(max() - min()))%int(delta);
61 }
61 }
62
62
63 for (int i = -1; i < count+1; ++i) {
63 for (int i = -1; i < count+1; ++i) {
64 qreal x = offset + i * delta + gridRect.left();
64 qreal x = offset + i * delta + gridRect.left();
65 points[i+1] = x;
65 points[i+1] = x;
66 }
66 }
67 return points;
67 return points;
68 }
68 }
69
69
70 QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector<qreal>& layout) const
70 QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector<qreal>& layout) const
71 {
71 {
72 QStringList result ;
72 QStringList result ;
73 const QRectF& gridRect = gridGeometry();
73 const QRectF& gridRect = gridGeometry();
74
74
75 qreal d = (max() - min())/gridRect.width();
75 qreal d = (max() - min())/gridRect.width();
76 for (int i = 0;i < layout.count()-1; ++i) {
76 for (int i = 0;i < layout.count()-1; ++i) {
77 qreal x = qFloor((((layout[i] + layout[i+1])/2-gridRect.left())*d + min()+0.5));
77 qreal x = qFloor((((layout[i] + layout[i+1])/2-gridRect.left())*d + min()+0.5));
78 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
78 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
79 result << m_categoriesAxis->categories().at(x);
79 result << m_categoriesAxis->categories().at(x);
80 }
80 }
81 else {
81 else {
82 // No label for x coordinate
82 // No label for x coordinate
83 result << "";
83 result << "";
84 }
84 }
85 }
85 }
86 result << "";
86 result << "";
87 return result;
87 return result;
88 }
88 }
89
89
90
90
91 void ChartBarCategoryAxisX::updateGeometry()
91 void ChartBarCategoryAxisX::updateGeometry()
92 {
92 {
93 const QVector<qreal>& layout = ChartAxis::layout();
93 const QVector<qreal>& layout = ChartAxis::layout();
94 if(layout.isEmpty()) return;
94 if(layout.isEmpty()) return;
95 setLabels(createCategoryLabels(layout));
95 setLabels(createCategoryLabels(layout));
96 HorizontalAxis::updateGeometry();
96 HorizontalAxis::updateGeometry();
97 }
97 }
98
98
99 void ChartBarCategoryAxisX::handleAxisUpdated()
99 void ChartBarCategoryAxisX::handleAxisUpdated()
100 {
100 {
101 if(m_categoriesAxis->categories()!=m_categories)
101 if(m_categoriesAxis->categories()!=m_categories)
102 {
102 {
103 m_categories=m_categoriesAxis->categories();
103 m_categories=m_categoriesAxis->categories();
104 if(ChartAxis::layout().count()==m_categoriesAxis->d_ptr->count()+2) updateGeometry();
104 if(ChartAxis::layout().count()==m_categoriesAxis->d_ptr->count()+2) updateGeometry();
105 }
105 }
106 ChartAxis::handleAxisUpdated();
106 ChartAxis::handleAxisUpdated();
107 }
107 }
108
108
109 QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
109 QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
110 {
110 {
111 Q_UNUSED(constraint)
111 Q_UNUSED(constraint)
112
112
113 QFontMetrics fn(font());
113 QFontMetrics fn(font());
114 QSizeF sh;
114 QSizeF sh;
115 QSizeF base = ChartAxis::sizeHint(which, constraint);
115 QSizeF base = ChartAxis::sizeHint(which, constraint);
116 QStringList ticksList = createCategoryLabels(ChartAxis::layout());
116 QStringList ticksList = createCategoryLabels(ChartAxis::layout());
117 qreal width=0;
117 qreal width=0;
118 qreal height=0;
118 qreal height=0;
119
119
120 switch (which) {
120 switch (which) {
121 case Qt::MinimumSize:
121 case Qt::MinimumSize:
122 width = fn.boundingRect("...").width();
122 width = fn.boundingRect("...").width();
123 height = fn.height()+labelPadding();
123 height = fn.height()+labelPadding();
124 width=qMax(width,base.width());
124 width=qMax(width,base.width());
125 height+=base.height();
125 height+=base.height();
126 sh = QSizeF(width,height);
126 sh = QSizeF(width,height);
127 break;
127 break;
128 case Qt::PreferredSize:{
128 case Qt::PreferredSize:{
129
129
130 for (int i = 0; i < ticksList.size(); ++i)
130 for (int i = 0; i < ticksList.size(); ++i)
131 {
131 {
132 QRectF rect = fn.boundingRect(ticksList.at(i));
132 QRectF rect = fn.boundingRect(ticksList.at(i));
133 width+=rect.width();
133 width+=rect.width();
134 height=qMax(rect.height()+labelPadding(),height);
134 height=qMax(rect.height()+labelPadding(),height);
135 }
135 }
136 width=qMax(width,base.width());
136 width=qMax(width,base.width());
137 height+=base.height();
137 height+=base.height();
138 sh = QSizeF(width,height);
138 sh = QSizeF(width,height);
139 break;
139 break;
140 }
140 }
141 default:
141 default:
142 break;
142 break;
143 }
143 }
144
144
145 return sh;
145 return sh;
146 }
146 }
147
147
148 QTCOMMERCIALCHART_END_NAMESPACE
148 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,148 +1,147
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartbarcategoryaxisy_p.h"
21 #include "chartbarcategoryaxisy_p.h"
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "qbarcategoryaxis_p.h"
23 #include "qbarcategoryaxis_p.h"
24 #include <qmath.h>
24 #include <qmath.h>
25 #include <QFontMetrics>
25 #include <QFontMetrics>
26
26
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27 QTCOMMERCIALCHART_BEGIN_NAMESPACE
28
28
29 ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis,ChartPresenter *presenter) : VerticalAxis(axis,presenter),
29 ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis,ChartPresenter *presenter) : VerticalAxis(axis,presenter,true),
30 m_categoriesAxis(axis)
30 m_categoriesAxis(axis)
31 {
31 {
32 setLabelBetweenTicks(true);
33 }
32 }
34
33
35 ChartBarCategoryAxisY::~ChartBarCategoryAxisY()
34 ChartBarCategoryAxisY::~ChartBarCategoryAxisY()
36 {
35 {
37 }
36 }
38
37
39 QVector<qreal> ChartBarCategoryAxisY::calculateLayout() const
38 QVector<qreal> ChartBarCategoryAxisY::calculateLayout() const
40 {
39 {
41 int count = m_categoriesAxis->d_ptr->count();
40 int count = m_categoriesAxis->d_ptr->count();
42
41
43 Q_ASSERT(count>=1);
42 Q_ASSERT(count>=1);
44
43
45 QVector<qreal> points;
44 QVector<qreal> points;
46 points.resize(count+2);
45 points.resize(count+2);
47
46
48 const QRectF& gridRect = gridGeometry();
47 const QRectF& gridRect = gridGeometry();
49
48
50 const qreal delta = gridRect.height()/(count);
49 const qreal delta = gridRect.height()/(count);
51 qreal offset = - min() - 0.5;
50 qreal offset = - min() - 0.5;
52
51
53 if(delta<1) return points;
52 if(delta<1) return points;
54
53
55 if(offset<0) {
54 if(offset<0) {
56 offset = int(offset * gridRect.height()/(max() - min()))%int(delta) + delta;
55 offset = int(offset * gridRect.height()/(max() - min()))%int(delta) + delta;
57 }
56 }
58 else {
57 else {
59 offset = int(offset * gridRect.height()/(max() - min()))%int(delta);
58 offset = int(offset * gridRect.height()/(max() - min()))%int(delta);
60 }
59 }
61
60
62 for (int i = -1; i < count +1; ++i) {
61 for (int i = -1; i < count +1; ++i) {
63 int y = gridRect.bottom() - i * delta - offset;
62 int y = gridRect.bottom() - i * delta - offset;
64 points[i+1] = y;
63 points[i+1] = y;
65 }
64 }
66 return points;
65 return points;
67 }
66 }
68
67
69 QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& layout) const
68 QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& layout) const
70 {
69 {
71 QStringList result;
70 QStringList result;
72 const QRectF& gridRect = gridGeometry();
71 const QRectF& gridRect = gridGeometry();
73 qreal d = (max() - min())/gridRect.height();
72 qreal d = (max() - min())/gridRect.height();
74 for (int i = 0;i < layout.count()-1; ++i) {
73 for (int i = 0;i < layout.count()-1; ++i) {
75 qreal x = qFloor(((gridRect.height()- (layout[i+1] + layout[i])/2 + gridRect.top())*d + min()+0.5));
74 qreal x = qFloor(((gridRect.height()- (layout[i+1] + layout[i])/2 + gridRect.top())*d + min()+0.5));
76 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
75 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
77 result << m_categoriesAxis->categories().at(x);
76 result << m_categoriesAxis->categories().at(x);
78 }
77 }
79 else {
78 else {
80 // No label for x coordinate
79 // No label for x coordinate
81 result << "";
80 result << "";
82 }
81 }
83 }
82 }
84 result << "";
83 result << "";
85 return result;
84 return result;
86 }
85 }
87
86
88 void ChartBarCategoryAxisY::updateGeometry()
87 void ChartBarCategoryAxisY::updateGeometry()
89 {
88 {
90 const QVector<qreal>& layout = ChartAxis::layout();
89 const QVector<qreal>& layout = ChartAxis::layout();
91 if(layout.isEmpty()) return;
90 if(layout.isEmpty()) return;
92 setLabels(createCategoryLabels(layout));
91 setLabels(createCategoryLabels(layout));
93 VerticalAxis::updateGeometry();
92 VerticalAxis::updateGeometry();
94 }
93 }
95
94
96 void ChartBarCategoryAxisY::handleAxisUpdated()
95 void ChartBarCategoryAxisY::handleAxisUpdated()
97 {
96 {
98
97
99 if(m_categoriesAxis->categories()!=m_categories)
98 if(m_categoriesAxis->categories()!=m_categories)
100 {
99 {
101 m_categories=m_categoriesAxis->categories();
100 m_categories=m_categoriesAxis->categories();
102 if(ChartAxis::layout().count()==m_categoriesAxis->d_ptr->count()+2) {
101 if(ChartAxis::layout().count()==m_categoriesAxis->d_ptr->count()+2) {
103 updateGeometry();
102 updateGeometry();
104 }
103 }
105 }
104 }
106 ChartAxis::handleAxisUpdated();
105 ChartAxis::handleAxisUpdated();
107 }
106 }
108
107
109 QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
108 QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
110 {
109 {
111 Q_UNUSED(constraint)
110 Q_UNUSED(constraint)
112
111
113 QFontMetrics fn(font());
112 QFontMetrics fn(font());
114 QSizeF sh;
113 QSizeF sh;
115 QSizeF base = ChartAxis::sizeHint(which, constraint);
114 QSizeF base = ChartAxis::sizeHint(which, constraint);
116 QStringList ticksList = createCategoryLabels(ChartAxis::layout());
115 QStringList ticksList = createCategoryLabels(ChartAxis::layout());
117 qreal width=0;
116 qreal width=0;
118 qreal height=0;
117 qreal height=0;
119
118
120 switch (which) {
119 switch (which) {
121 case Qt::MinimumSize:
120 case Qt::MinimumSize:
122 width = fn.boundingRect("...").width() + labelPadding();
121 width = fn.boundingRect("...").width() + labelPadding();
123 height = fn.height();
122 height = fn.height();
124 width+=base.width();
123 width+=base.width();
125 height=qMax(height,base.height());
124 height=qMax(height,base.height());
126 sh = QSizeF(width,height);
125 sh = QSizeF(width,height);
127 break;
126 break;
128 case Qt::PreferredSize:{
127 case Qt::PreferredSize:{
129
128
130 for (int i = 0; i < ticksList.size(); ++i)
129 for (int i = 0; i < ticksList.size(); ++i)
131 {
130 {
132 QRectF rect = fn.boundingRect(ticksList.at(i));
131 QRectF rect = fn.boundingRect(ticksList.at(i));
133 height+=rect.height();
132 height+=rect.height();
134 width=qMax(rect.width()+labelPadding(),width);
133 width=qMax(rect.width()+labelPadding(),width);
135 }
134 }
136 height=qMax(height,base.height());
135 height=qMax(height,base.height());
137 width+=base.width();
136 width+=base.width();
138 sh = QSizeF(width,height);
137 sh = QSizeF(width,height);
139 break;
138 break;
140 }
139 }
141 default:
140 default:
142 break;
141 break;
143 }
142 }
144
143
145 return sh;
144 return sh;
146 }
145 }
147
146
148 QTCOMMERCIALCHART_END_NAMESPACE
147 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,118 +1,117
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartcategoryaxisx_p.h"
21 #include "chartcategoryaxisx_p.h"
22 #include "qcategoryaxis.h"
22 #include "qcategoryaxis.h"
23 #include "qabstractaxis.h"
23 #include "qabstractaxis.h"
24 #include "chartpresenter_p.h"
24 #include "chartpresenter_p.h"
25 #include <QGraphicsLayout>
25 #include <QGraphicsLayout>
26 #include <QFontMetrics>
26 #include <QFontMetrics>
27 #include <qmath.h>
27 #include <qmath.h>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 ChartCategoryAxisX::ChartCategoryAxisX(QCategoryAxis *axis,ChartPresenter *presenter) : HorizontalAxis(axis,presenter),
31 ChartCategoryAxisX::ChartCategoryAxisX(QCategoryAxis *axis,ChartPresenter *presenter) : HorizontalAxis(axis,presenter,true),
32 m_axis(axis)
32 m_axis(axis)
33 {
33 {
34 setLabelBetweenTicks(true);
35 }
34 }
36
35
37 ChartCategoryAxisX::~ChartCategoryAxisX()
36 ChartCategoryAxisX::~ChartCategoryAxisX()
38 {
37 {
39 }
38 }
40
39
41 QVector<qreal> ChartCategoryAxisX::calculateLayout() const
40 QVector<qreal> ChartCategoryAxisX::calculateLayout() const
42 {
41 {
43 int tickCount = m_axis->categoriesLabels().count() + 1;
42 int tickCount = m_axis->categoriesLabels().count() + 1;
44 QVector<qreal> points;
43 QVector<qreal> points;
45
44
46 if (tickCount < 2)
45 if (tickCount < 2)
47 return points;
46 return points;
48
47
49 const QRectF& gridRect = gridGeometry();
48 const QRectF& gridRect = gridGeometry();
50 qreal range = m_axis->max() - m_axis->min();
49 qreal range = m_axis->max() - m_axis->min();
51 if (range > 0) {
50 if (range > 0) {
52 points.resize(tickCount);
51 points.resize(tickCount);
53 qreal scale = gridRect.width() / range;
52 qreal scale = gridRect.width() / range;
54 for (int i = 0; i < tickCount; ++i)
53 for (int i = 0; i < tickCount; ++i)
55 if (i < tickCount - 1) {
54 if (i < tickCount - 1) {
56 int x = (m_axis->startValue(m_axis->categoriesLabels().at(i)) - m_axis->min()) * scale + gridRect.left();
55 int x = (m_axis->startValue(m_axis->categoriesLabels().at(i)) - m_axis->min()) * scale + gridRect.left();
57 points[i] = x;
56 points[i] = x;
58 } else {
57 } else {
59 int x = (m_axis->endValue(m_axis->categoriesLabels().at(i - 1)) - m_axis->min()) * scale + gridRect.left();
58 int x = (m_axis->endValue(m_axis->categoriesLabels().at(i - 1)) - m_axis->min()) * scale + gridRect.left();
60 points[i] = x;
59 points[i] = x;
61 }
60 }
62 }
61 }
63
62
64 return points;
63 return points;
65 }
64 }
66
65
67 void ChartCategoryAxisX::updateGeometry()
66 void ChartCategoryAxisX::updateGeometry()
68 {
67 {
69 setLabels(m_axis->categoriesLabels()<<"");
68 setLabels(m_axis->categoriesLabels()<<"");
70 HorizontalAxis::updateGeometry();
69 HorizontalAxis::updateGeometry();
71 }
70 }
72
71
73 void ChartCategoryAxisX::handleAxisUpdated()
72 void ChartCategoryAxisX::handleAxisUpdated()
74 {
73 {
75 updateGeometry();
74 updateGeometry();
76 ChartAxis::handleAxisUpdated();
75 ChartAxis::handleAxisUpdated();
77 }
76 }
78
77
79 QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
78 QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
80 {
79 {
81 Q_UNUSED(constraint)
80 Q_UNUSED(constraint)
82
81
83 QFontMetrics fn(font());
82 QFontMetrics fn(font());
84 QSizeF sh;
83 QSizeF sh;
85 QSizeF base = ChartAxis::sizeHint(which, constraint);
84 QSizeF base = ChartAxis::sizeHint(which, constraint);
86 QStringList ticksList ; //TODO:
85 QStringList ticksList ; //TODO:
87 qreal width=0;
86 qreal width=0;
88 qreal height=0;
87 qreal height=0;
89
88
90 switch (which) {
89 switch (which) {
91 case Qt::MinimumSize:
90 case Qt::MinimumSize:
92 width = fn.boundingRect("...").width();
91 width = fn.boundingRect("...").width();
93 height = fn.height() + labelPadding();
92 height = fn.height() + labelPadding();
94 width=qMax(width,base.width());
93 width=qMax(width,base.width());
95 height+=base.height();
94 height+=base.height();
96 sh = QSizeF(width,height);
95 sh = QSizeF(width,height);
97 break;
96 break;
98 case Qt::PreferredSize: {
97 case Qt::PreferredSize: {
99
98
100 for (int i = 0; i < ticksList.size(); ++i)
99 for (int i = 0; i < ticksList.size(); ++i)
101 {
100 {
102 QRectF rect = fn.boundingRect(ticksList.at(i));
101 QRectF rect = fn.boundingRect(ticksList.at(i));
103 width+=rect.width();
102 width+=rect.width();
104 height=qMax(rect.height()+labelPadding(),height);
103 height=qMax(rect.height()+labelPadding(),height);
105 }
104 }
106 width=qMax(width,base.width());
105 width=qMax(width,base.width());
107 height+=base.height();
106 height+=base.height();
108 sh = QSizeF(width,height);
107 sh = QSizeF(width,height);
109 break;
108 break;
110 }
109 }
111 default:
110 default:
112 break;
111 break;
113 }
112 }
114
113
115 return sh;
114 return sh;
116 }
115 }
117
116
118 QTCOMMERCIALCHART_END_NAMESPACE
117 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,118 +1,117
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartcategoryaxisy_p.h"
21 #include "chartcategoryaxisy_p.h"
22 #include "qcategoryaxis.h"
22 #include "qcategoryaxis.h"
23 #include "qabstractaxis.h"
23 #include "qabstractaxis.h"
24 #include "chartpresenter_p.h"
24 #include "chartpresenter_p.h"
25 #include <QGraphicsLayout>
25 #include <QGraphicsLayout>
26 #include <QFontMetrics>
26 #include <QFontMetrics>
27 #include <qmath.h>
27 #include <qmath.h>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 ChartCategoryAxisY::ChartCategoryAxisY(QCategoryAxis *axis,ChartPresenter *presenter) : VerticalAxis(axis,presenter),
31 ChartCategoryAxisY::ChartCategoryAxisY(QCategoryAxis *axis,ChartPresenter *presenter) : VerticalAxis(axis,presenter,true),
32 m_axis(axis)
32 m_axis(axis)
33 {
33 {
34 setLabelBetweenTicks(true);
35 }
34 }
36
35
37 ChartCategoryAxisY::~ChartCategoryAxisY()
36 ChartCategoryAxisY::~ChartCategoryAxisY()
38 {
37 {
39 }
38 }
40
39
41 QVector<qreal> ChartCategoryAxisY::calculateLayout() const
40 QVector<qreal> ChartCategoryAxisY::calculateLayout() const
42 {
41 {
43 int tickCount = m_axis->categoriesLabels().count() + 1;
42 int tickCount = m_axis->categoriesLabels().count() + 1;
44 QVector<qreal> points;
43 QVector<qreal> points;
45
44
46 if (tickCount < 2)
45 if (tickCount < 2)
47 return points;
46 return points;
48
47
49 const QRectF& gridRect = gridGeometry();
48 const QRectF& gridRect = gridGeometry();
50 qreal range = m_axis->max() - m_axis->min();
49 qreal range = m_axis->max() - m_axis->min();
51 if (range > 0) {
50 if (range > 0) {
52 points.resize(tickCount);
51 points.resize(tickCount);
53 qreal scale = gridRect.height() / range;
52 qreal scale = gridRect.height() / range;
54 for (int i = 0; i < tickCount; ++i)
53 for (int i = 0; i < tickCount; ++i)
55 if (i < tickCount - 1) {
54 if (i < tickCount - 1) {
56 int y = -(m_axis->startValue(m_axis->categoriesLabels().at(i)) - m_axis->min()) * scale + gridRect.bottom();
55 int y = -(m_axis->startValue(m_axis->categoriesLabels().at(i)) - m_axis->min()) * scale + gridRect.bottom();
57 points[i] = y;
56 points[i] = y;
58 } else {
57 } else {
59 int y = -(m_axis->endValue(m_axis->categoriesLabels().at(i - 1)) - m_axis->min()) * scale + gridRect.bottom();
58 int y = -(m_axis->endValue(m_axis->categoriesLabels().at(i - 1)) - m_axis->min()) * scale + gridRect.bottom();
60 points[i] = y;
59 points[i] = y;
61 }
60 }
62 }
61 }
63
62
64 return points;
63 return points;
65 }
64 }
66
65
67 void ChartCategoryAxisY::updateGeometry()
66 void ChartCategoryAxisY::updateGeometry()
68 {
67 {
69 setLabels(m_axis->categoriesLabels()<<"");
68 setLabels(m_axis->categoriesLabels()<<"");
70 VerticalAxis::updateGeometry();
69 VerticalAxis::updateGeometry();
71 }
70 }
72
71
73 void ChartCategoryAxisY::handleAxisUpdated()
72 void ChartCategoryAxisY::handleAxisUpdated()
74 {
73 {
75 updateGeometry();
74 updateGeometry();
76 ChartAxis::handleAxisUpdated();
75 ChartAxis::handleAxisUpdated();
77 }
76 }
78
77
79 QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
78 QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
80 {
79 {
81 Q_UNUSED(constraint)
80 Q_UNUSED(constraint)
82
81
83 QFontMetrics fn(font());
82 QFontMetrics fn(font());
84 QSizeF sh;
83 QSizeF sh;
85 QSizeF base = ChartAxis::sizeHint(which, constraint);
84 QSizeF base = ChartAxis::sizeHint(which, constraint);
86 QStringList ticksList; //TODO::
85 QStringList ticksList; //TODO::
87 qreal width=0;
86 qreal width=0;
88 qreal height=0;
87 qreal height=0;
89
88
90 switch (which) {
89 switch (which) {
91 case Qt::MinimumSize:
90 case Qt::MinimumSize:
92 width = fn.boundingRect("...").width()+labelPadding();
91 width = fn.boundingRect("...").width()+labelPadding();
93 height = fn.height();
92 height = fn.height();
94 width=qMax(width,base.width());
93 width=qMax(width,base.width());
95 height+=base.height();
94 height+=base.height();
96 sh = QSizeF(width,height);
95 sh = QSizeF(width,height);
97 break;
96 break;
98 case Qt::PreferredSize:{
97 case Qt::PreferredSize:{
99
98
100 for (int i = 0; i < ticksList.size(); ++i)
99 for (int i = 0; i < ticksList.size(); ++i)
101 {
100 {
102 QRectF rect = fn.boundingRect(ticksList.at(i));
101 QRectF rect = fn.boundingRect(ticksList.at(i));
103 height+=rect.height();
102 height+=rect.height();
104 width=qMax(rect.width()+labelPadding(),width);
103 width=qMax(rect.width()+labelPadding(),width);
105 }
104 }
106 height=qMax(height,base.height());
105 height=qMax(height,base.height());
107 width+=base.width();
106 width+=base.width();
108 sh = QSizeF(width,height);
107 sh = QSizeF(width,height);
109 break;
108 break;
110 }
109 }
111 default:
110 default:
112 break;
111 break;
113 }
112 }
114
113
115 return sh;
114 return sh;
116 }
115 }
117
116
118 QTCOMMERCIALCHART_END_NAMESPACE
117 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,527 +1,522
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartaxis_p.h"
21 #include "chartaxis_p.h"
22 #include "qabstractaxis.h"
22 #include "qabstractaxis.h"
23 #include "qabstractaxis_p.h"
23 #include "qabstractaxis_p.h"
24 #include "chartpresenter_p.h"
24 #include "chartpresenter_p.h"
25 #include "chartlayout_p.h"
25 #include "chartlayout_p.h"
26 #include "domain_p.h"
26 #include "domain_p.h"
27 #include <qmath.h>
27 #include <qmath.h>
28 #include <QDateTime>
28 #include <QDateTime>
29 #include <QValueAxis>
29 #include <QValueAxis>
30 #include <QGraphicsLayout>
30 #include <QGraphicsLayout>
31 #include <QFontMetrics>
31 #include <QFontMetrics>
32
32
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34
34
35 ChartAxis::ChartAxis(QAbstractAxis *axis,ChartPresenter *presenter) : ChartElement(presenter),
35 ChartAxis::ChartAxis(QAbstractAxis *axis,ChartPresenter *presenter, bool intervalAxis) : ChartElement(presenter),
36 m_chartAxis(axis),
36 m_chartAxis(axis),
37 m_labelsAngle(0),
37 m_labelsAngle(0),
38 m_grid(new QGraphicsItemGroup(presenter->rootItem())),
38 m_grid(new QGraphicsItemGroup(presenter->rootItem())),
39 m_shades(new QGraphicsItemGroup(presenter->rootItem())),
39 m_shades(new QGraphicsItemGroup(presenter->rootItem())),
40 m_labels(new QGraphicsItemGroup(presenter->rootItem())),
40 m_labels(new QGraphicsItemGroup(presenter->rootItem())),
41 m_arrow(new QGraphicsItemGroup(presenter->rootItem())),
41 m_arrow(new QGraphicsItemGroup(presenter->rootItem())),
42 m_title(new QGraphicsSimpleTextItem(presenter->rootItem())),
42 m_title(new QGraphicsSimpleTextItem(presenter->rootItem())),
43 m_min(0),
43 m_min(0),
44 m_max(0),
44 m_max(0),
45 m_animation(0),
45 m_animation(0),
46 m_labelPadding(5),
46 m_labelPadding(5),
47 m_labelBetween(false)
47 m_intervalAxis(intervalAxis)
48 {
48 {
49 //initial initialization
49 //initial initialization
50 m_arrow->setZValue(ChartPresenter::AxisZValue);
50 m_arrow->setZValue(ChartPresenter::AxisZValue);
51 m_arrow->setHandlesChildEvents(false);
51 m_arrow->setHandlesChildEvents(false);
52 m_labels->setZValue(ChartPresenter::AxisZValue);
52 m_labels->setZValue(ChartPresenter::AxisZValue);
53 m_shades->setZValue(ChartPresenter::ShadesZValue);
53 m_shades->setZValue(ChartPresenter::ShadesZValue);
54 m_grid->setZValue(ChartPresenter::GridZValue);
54 m_grid->setZValue(ChartPresenter::GridZValue);
55
55
56 QObject::connect(m_chartAxis->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
56 QObject::connect(m_chartAxis->d_ptr.data(),SIGNAL(updated()),this,SLOT(handleAxisUpdated()));
57
57
58 QGraphicsSimpleTextItem item;
58 QGraphicsSimpleTextItem item;
59 m_font = item.font();
59 m_font = item.font();
60
60
61 }
61 }
62
62
63 ChartAxis::~ChartAxis()
63 ChartAxis::~ChartAxis()
64 {
64 {
65 }
65 }
66
66
67 void ChartAxis::setAnimation(AxisAnimation* animation)
67 void ChartAxis::setAnimation(AxisAnimation* animation)
68 {
68 {
69 m_animation=animation;
69 m_animation=animation;
70 }
70 }
71
71
72 void ChartAxis::setLayout(QVector<qreal> &layout)
72 void ChartAxis::setLayout(QVector<qreal> &layout)
73 {
73 {
74 m_layoutVector=layout;
74 m_layoutVector=layout;
75 }
75 }
76
76
77 void ChartAxis::createItems(int count)
77 void ChartAxis::createItems(int count)
78 {
78 {
79 if (m_arrow->children().size() == 0)
79 if (m_arrow->children().size() == 0)
80 m_arrow->addToGroup(new AxisItem(this,presenter()->rootItem()));
80 m_arrow->addToGroup(new AxisItem(this,presenter()->rootItem()));
81 if (m_grid->children().size() == 0){
81 if (m_intervalAxis && m_grid->children().size() == 0){
82 for(int i = 0 ; i < 2 ;i ++)
82 for(int i = 0 ; i < 2 ;i ++)
83 m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
83 m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
84 }
84 }
85 for (int i = 0; i < count; ++i) {
85 for (int i = 0; i < count; ++i) {
86 m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
86 m_grid->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
87 m_labels->addToGroup(new QGraphicsSimpleTextItem(presenter()->rootItem()));
87 m_labels->addToGroup(new QGraphicsSimpleTextItem(presenter()->rootItem()));
88 m_arrow->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
88 m_arrow->addToGroup(new QGraphicsLineItem(presenter()->rootItem()));
89 if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2)
89 if ((m_grid->childItems().size())%2 && m_grid->childItems().size()>2)
90 m_shades->addToGroup(new QGraphicsRectItem(presenter()->rootItem()));
90 m_shades->addToGroup(new QGraphicsRectItem(presenter()->rootItem()));
91 }
91 }
92 }
92 }
93
93
94 void ChartAxis::deleteItems(int count)
94 void ChartAxis::deleteItems(int count)
95 {
95 {
96 QList<QGraphicsItem *> lines = m_grid->childItems();
96 QList<QGraphicsItem *> lines = m_grid->childItems();
97 QList<QGraphicsItem *> labels = m_labels->childItems();
97 QList<QGraphicsItem *> labels = m_labels->childItems();
98 QList<QGraphicsItem *> shades = m_shades->childItems();
98 QList<QGraphicsItem *> shades = m_shades->childItems();
99 QList<QGraphicsItem *> axis = m_arrow->childItems();
99 QList<QGraphicsItem *> axis = m_arrow->childItems();
100
100
101 for (int i = 0; i < count; ++i) {
101 for (int i = 0; i < count; ++i) {
102 if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast());
102 if (lines.size()%2 && lines.size() > 1) delete(shades.takeLast());
103 delete(lines.takeLast());
103 delete(lines.takeLast());
104 delete(labels.takeLast());
104 delete(labels.takeLast());
105 delete(axis.takeLast());
105 delete(axis.takeLast());
106 }
106 }
107 }
107 }
108
108
109 void ChartAxis::updateLayout(QVector<qreal> &layout)
109 void ChartAxis::updateLayout(QVector<qreal> &layout)
110 {
110 {
111 int diff = m_layoutVector.size() - layout.size();
111 int diff = m_layoutVector.size() - layout.size();
112
112
113 if (diff>0) {
113 if (diff>0) {
114 deleteItems(diff);
114 deleteItems(diff);
115 }
115 }
116 else if (diff<0) {
116 else if (diff<0) {
117 createItems(-diff);
117 createItems(-diff);
118 }
118 }
119
119
120 if(diff<0) handleAxisUpdated();
120 if(diff<0) handleAxisUpdated();
121
121
122 if (m_animation) {
122 if (m_animation) {
123 switch(presenter()->state()){
123 switch(presenter()->state()){
124 case ChartPresenter::ZoomInState:
124 case ChartPresenter::ZoomInState:
125 m_animation->setAnimationType(AxisAnimation::ZoomInAnimation);
125 m_animation->setAnimationType(AxisAnimation::ZoomInAnimation);
126 m_animation->setAnimationPoint(presenter()->statePoint());
126 m_animation->setAnimationPoint(presenter()->statePoint());
127 break;
127 break;
128 case ChartPresenter::ZoomOutState:
128 case ChartPresenter::ZoomOutState:
129 m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation);
129 m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation);
130 m_animation->setAnimationPoint(presenter()->statePoint());
130 m_animation->setAnimationPoint(presenter()->statePoint());
131 break;
131 break;
132 case ChartPresenter::ScrollUpState:
132 case ChartPresenter::ScrollUpState:
133 case ChartPresenter::ScrollLeftState:
133 case ChartPresenter::ScrollLeftState:
134 m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation);
134 m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation);
135 break;
135 break;
136 case ChartPresenter::ScrollDownState:
136 case ChartPresenter::ScrollDownState:
137 case ChartPresenter::ScrollRightState:
137 case ChartPresenter::ScrollRightState:
138 m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation);
138 m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation);
139 break;
139 break;
140 case ChartPresenter::ShowState:
140 case ChartPresenter::ShowState:
141 m_animation->setAnimationType(AxisAnimation::DefaultAnimation);
141 m_animation->setAnimationType(AxisAnimation::DefaultAnimation);
142 break;
142 break;
143 }
143 }
144 m_animation->setValues(m_layoutVector,layout);
144 m_animation->setValues(m_layoutVector,layout);
145 presenter()->startAnimation(m_animation);
145 presenter()->startAnimation(m_animation);
146 }
146 }
147 else {
147 else {
148 setLayout(layout);
148 setLayout(layout);
149 updateGeometry();
149 updateGeometry();
150 }
150 }
151 }
151 }
152
152
153 void ChartAxis::setArrowOpacity(qreal opacity)
153 void ChartAxis::setArrowOpacity(qreal opacity)
154 {
154 {
155 m_arrow->setOpacity(opacity);
155 m_arrow->setOpacity(opacity);
156 }
156 }
157
157
158 qreal ChartAxis::arrowOpacity() const
158 qreal ChartAxis::arrowOpacity() const
159 {
159 {
160 return m_arrow->opacity();
160 return m_arrow->opacity();
161 }
161 }
162
162
163 void ChartAxis::setArrowVisibility(bool visible)
163 void ChartAxis::setArrowVisibility(bool visible)
164 {
164 {
165 m_arrow->setOpacity(visible);
165 m_arrow->setOpacity(visible);
166 }
166 }
167
167
168 void ChartAxis::setGridOpacity(qreal opacity)
168 void ChartAxis::setGridOpacity(qreal opacity)
169 {
169 {
170 m_grid->setOpacity(opacity);
170 m_grid->setOpacity(opacity);
171 }
171 }
172
172
173 qreal ChartAxis::gridOpacity() const
173 qreal ChartAxis::gridOpacity() const
174 {
174 {
175 return m_grid->opacity();
175 return m_grid->opacity();
176 }
176 }
177
177
178 void ChartAxis::setGridVisibility(bool visible)
178 void ChartAxis::setGridVisibility(bool visible)
179 {
179 {
180 m_grid->setOpacity(visible);
180 m_grid->setOpacity(visible);
181 }
181 }
182
182
183 void ChartAxis::setLabelsOpacity(qreal opacity)
183 void ChartAxis::setLabelsOpacity(qreal opacity)
184 {
184 {
185 m_labels->setOpacity(opacity);
185 m_labels->setOpacity(opacity);
186 }
186 }
187
187
188 qreal ChartAxis::labelsOpacity() const
188 qreal ChartAxis::labelsOpacity() const
189 {
189 {
190 return m_labels->opacity();
190 return m_labels->opacity();
191 }
191 }
192
192
193 void ChartAxis::setLabelsVisibility(bool visible)
193 void ChartAxis::setLabelsVisibility(bool visible)
194 {
194 {
195 m_labels->setOpacity(visible);
195 m_labels->setOpacity(visible);
196 }
196 }
197
197
198 void ChartAxis::setShadesOpacity(qreal opacity)
198 void ChartAxis::setShadesOpacity(qreal opacity)
199 {
199 {
200 m_shades->setOpacity(opacity);
200 m_shades->setOpacity(opacity);
201 }
201 }
202
202
203 qreal ChartAxis::shadesOpacity() const
203 qreal ChartAxis::shadesOpacity() const
204 {
204 {
205 return m_shades->opacity();
205 return m_shades->opacity();
206 }
206 }
207
207
208 void ChartAxis::setShadesVisibility(bool visible)
208 void ChartAxis::setShadesVisibility(bool visible)
209 {
209 {
210 m_shades->setVisible(visible);
210 m_shades->setVisible(visible);
211 }
211 }
212
212
213 void ChartAxis::setLabelsAngle(int angle)
213 void ChartAxis::setLabelsAngle(int angle)
214 {
214 {
215 foreach(QGraphicsItem* item , m_labels->childItems()) {
215 foreach(QGraphicsItem* item , m_labels->childItems()) {
216 item->setRotation(angle);
216 item->setRotation(angle);
217 }
217 }
218
218
219 m_labelsAngle=angle;
219 m_labelsAngle=angle;
220 }
220 }
221
221
222 void ChartAxis::setLabelsPen(const QPen &pen)
222 void ChartAxis::setLabelsPen(const QPen &pen)
223 {
223 {
224 foreach(QGraphicsItem* item , m_labels->childItems()) {
224 foreach(QGraphicsItem* item , m_labels->childItems()) {
225 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
225 static_cast<QGraphicsSimpleTextItem*>(item)->setPen(pen);
226 }
226 }
227 }
227 }
228
228
229 void ChartAxis::setLabelsBrush(const QBrush &brush)
229 void ChartAxis::setLabelsBrush(const QBrush &brush)
230 {
230 {
231 foreach(QGraphicsItem* item , m_labels->childItems()) {
231 foreach(QGraphicsItem* item , m_labels->childItems()) {
232 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
232 static_cast<QGraphicsSimpleTextItem*>(item)->setBrush(brush);
233 }
233 }
234 }
234 }
235
235
236 void ChartAxis::setLabelsFont(const QFont &font)
236 void ChartAxis::setLabelsFont(const QFont &font)
237 {
237 {
238 foreach(QGraphicsItem* item , m_labels->childItems()) {
238 foreach(QGraphicsItem* item , m_labels->childItems()) {
239 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
239 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
240 }
240 }
241 if(m_font!=font) {
241 if(m_font!=font) {
242 m_font = font;
242 m_font = font;
243 foreach(QGraphicsItem* item , m_labels->childItems()) {
243 foreach(QGraphicsItem* item , m_labels->childItems()) {
244 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
244 static_cast<QGraphicsSimpleTextItem*>(item)->setFont(font);
245 }
245 }
246 QGraphicsLayoutItem::updateGeometry();
246 QGraphicsLayoutItem::updateGeometry();
247 presenter()->layout()->invalidate();
247 presenter()->layout()->invalidate();
248
248
249 }
249 }
250 }
250 }
251
251
252 void ChartAxis::setShadesBrush(const QBrush &brush)
252 void ChartAxis::setShadesBrush(const QBrush &brush)
253 {
253 {
254 foreach(QGraphicsItem* item , m_shades->childItems()) {
254 foreach(QGraphicsItem* item , m_shades->childItems()) {
255 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
255 static_cast<QGraphicsRectItem*>(item)->setBrush(brush);
256 }
256 }
257 }
257 }
258
258
259 void ChartAxis::setShadesPen(const QPen &pen)
259 void ChartAxis::setShadesPen(const QPen &pen)
260 {
260 {
261 foreach(QGraphicsItem* item , m_shades->childItems()) {
261 foreach(QGraphicsItem* item , m_shades->childItems()) {
262 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
262 static_cast<QGraphicsRectItem*>(item)->setPen(pen);
263 }
263 }
264 }
264 }
265
265
266 void ChartAxis::setArrowPen(const QPen &pen)
266 void ChartAxis::setArrowPen(const QPen &pen)
267 {
267 {
268 foreach(QGraphicsItem* item , m_arrow->childItems()) {
268 foreach(QGraphicsItem* item , m_arrow->childItems()) {
269 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
269 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
270 }
270 }
271 }
271 }
272
272
273 void ChartAxis::setGridPen(const QPen &pen)
273 void ChartAxis::setGridPen(const QPen &pen)
274 {
274 {
275 foreach(QGraphicsItem* item , m_grid->childItems()) {
275 foreach(QGraphicsItem* item , m_grid->childItems()) {
276 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
276 static_cast<QGraphicsLineItem*>(item)->setPen(pen);
277 }
277 }
278 }
278 }
279
279
280 void ChartAxis::setLabelPadding(int padding)
280 void ChartAxis::setLabelPadding(int padding)
281 {
281 {
282 m_labelPadding=padding;
282 m_labelPadding=padding;
283 }
283 }
284
284
285 bool ChartAxis::isEmpty()
285 bool ChartAxis::isEmpty()
286 {
286 {
287 return !m_axisRect.isValid() || m_gridRect.isEmpty() || qFuzzyIsNull(m_min - m_max);
287 return !m_axisRect.isValid() || m_gridRect.isEmpty() || qFuzzyIsNull(m_min - m_max);
288 }
288 }
289
289
290 void ChartAxis::handleDomainUpdated()
290 void ChartAxis::handleDomainUpdated()
291 {
291 {
292 Domain* domain = qobject_cast<Domain*>(sender());
292 Domain* domain = qobject_cast<Domain*>(sender());
293 qreal min(0);
293 qreal min(0);
294 qreal max(0);
294 qreal max(0);
295
295
296 if(m_chartAxis->orientation()==Qt::Horizontal) {
296 if(m_chartAxis->orientation()==Qt::Horizontal) {
297 min = domain->minX();
297 min = domain->minX();
298 max = domain->maxX();
298 max = domain->maxX();
299 }
299 }
300 else if (m_chartAxis->orientation()==Qt::Vertical)
300 else if (m_chartAxis->orientation()==Qt::Vertical)
301 {
301 {
302 min = domain->minY();
302 min = domain->minY();
303 max = domain->maxY();
303 max = domain->maxY();
304 }
304 }
305
305
306 if (!qFuzzyIsNull(m_min - min) || !qFuzzyIsNull(m_max - max))
306 if (!qFuzzyIsNull(m_min - min) || !qFuzzyIsNull(m_max - max))
307 {
307 {
308 m_min = min;
308 m_min = min;
309 m_max = max;
309 m_max = max;
310
310
311 if (!isEmpty()) {
311 if (!isEmpty()) {
312
312
313 QVector<qreal> layout = calculateLayout();
313 QVector<qreal> layout = calculateLayout();
314 updateLayout(layout);
314 updateLayout(layout);
315 QSizeF before = effectiveSizeHint(Qt::MinimumSize);
315 QSizeF before = effectiveSizeHint(Qt::MinimumSize);
316
316
317 QSizeF after= sizeHint(Qt::MinimumSize);
317 QSizeF after= sizeHint(Qt::MinimumSize);
318
318
319 if(before!=after) {
319 if(before!=after) {
320 QGraphicsLayoutItem::updateGeometry();
320 QGraphicsLayoutItem::updateGeometry();
321 //we don't want to call invalidate on layout, since it will change minimum size of component,
321 //we don't want to call invalidate on layout, since it will change minimum size of component,
322 //which we would like to avoid since it causes nasty flips when scrolling or zooming,
322 //which we would like to avoid since it causes nasty flips when scrolling or zooming,
323 //instead recalculate layout and use plotArea for extra space.
323 //instead recalculate layout and use plotArea for extra space.
324 presenter()->layout()->setGeometry(presenter()->layout()->geometry());
324 presenter()->layout()->setGeometry(presenter()->layout()->geometry());
325 }
325 }
326 }
326 }
327 }
327 }
328 }
328 }
329
329
330 void ChartAxis::handleAxisUpdated()
330 void ChartAxis::handleAxisUpdated()
331 {
331 {
332 if(isEmpty()) return;
332 if(isEmpty()) return;
333
333
334
334
335 bool visible = m_chartAxis->isVisible();
335 bool visible = m_chartAxis->isVisible();
336
336
337 setArrowVisibility(visible && m_chartAxis->isLineVisible());
337 setArrowVisibility(visible && m_chartAxis->isLineVisible());
338 setGridVisibility(visible && m_chartAxis->isGridLineVisible());
338 setGridVisibility(visible && m_chartAxis->isGridLineVisible());
339 setLabelsVisibility(visible && m_chartAxis->labelsVisible());
339 setLabelsVisibility(visible && m_chartAxis->labelsVisible());
340 setShadesVisibility(visible && m_chartAxis->shadesVisible());
340 setShadesVisibility(visible && m_chartAxis->shadesVisible());
341 setLabelsAngle(m_chartAxis->labelsAngle());
341 setLabelsAngle(m_chartAxis->labelsAngle());
342 setArrowPen(m_chartAxis->linePen());
342 setArrowPen(m_chartAxis->linePen());
343 setLabelsPen(m_chartAxis->labelsPen());
343 setLabelsPen(m_chartAxis->labelsPen());
344 setLabelsBrush(m_chartAxis->labelsBrush());
344 setLabelsBrush(m_chartAxis->labelsBrush());
345 setLabelsFont(m_chartAxis->labelsFont());
345 setLabelsFont(m_chartAxis->labelsFont());
346 setGridPen(m_chartAxis->gridLinePen());
346 setGridPen(m_chartAxis->gridLinePen());
347 setShadesPen(m_chartAxis->shadesPen());
347 setShadesPen(m_chartAxis->shadesPen());
348 setShadesBrush(m_chartAxis->shadesBrush());
348 setShadesBrush(m_chartAxis->shadesBrush());
349 setTitleText(m_chartAxis->title());
349 setTitleText(m_chartAxis->title());
350 }
350 }
351
351
352 void ChartAxis::setTitleText(const QString& title)
352 void ChartAxis::setTitleText(const QString& title)
353 {
353 {
354 if(m_titleText!=title) {
354 if(m_titleText!=title) {
355 m_titleText = title;
355 m_titleText = title;
356 m_axisRect = QRect();
356 m_axisRect = QRect();
357 QGraphicsLayoutItem::updateGeometry();
357 QGraphicsLayoutItem::updateGeometry();
358 presenter()->layout()->invalidate();
358 presenter()->layout()->invalidate();
359 }
359 }
360 }
360 }
361
361
362 void ChartAxis::hide()
362 void ChartAxis::hide()
363 {
363 {
364 setArrowVisibility(false);
364 setArrowVisibility(false);
365 setGridVisibility(false);
365 setGridVisibility(false);
366 setLabelsVisibility(false);
366 setLabelsVisibility(false);
367 setShadesVisibility(false);
367 setShadesVisibility(false);
368 }
368 }
369
369
370 void ChartAxis::setGeometry(const QRectF &axis, const QRectF &grid)
370 void ChartAxis::setGeometry(const QRectF &axis, const QRectF &grid)
371 {
371 {
372 m_gridRect = grid;
372 m_gridRect = grid;
373 m_axisRect = axis;
373 m_axisRect = axis;
374
374
375 if (isEmpty()) return;
375 if (isEmpty()) return;
376
376
377 if(!m_titleText.isNull()) {
377 if(!m_titleText.isNull()) {
378 QFontMetrics fn(m_title->font());
378 QFontMetrics fn(m_title->font());
379
379
380 int size(0);
380 int size(0);
381
381
382 if(orientation()==Qt::Horizontal)
382 if(orientation()==Qt::Horizontal)
383 size = grid.width();
383 size = grid.width();
384 else if(orientation()==Qt::Vertical)
384 else if(orientation()==Qt::Vertical)
385 size = grid.height();
385 size = grid.height();
386
386
387 if (fn.boundingRect(m_titleText).width() > size)
387 if (fn.boundingRect(m_titleText).width() > size)
388 {
388 {
389 QString string = m_titleText + "...";
389 QString string = m_titleText + "...";
390 while (fn.boundingRect(string).width() > size && string.length() > 3)
390 while (fn.boundingRect(string).width() > size && string.length() > 3)
391 string.remove(string.length() - 4, 1);
391 string.remove(string.length() - 4, 1);
392 m_title->setText(string);
392 m_title->setText(string);
393 }
393 }
394 else
394 else
395 m_title->setText(m_titleText);
395 m_title->setText(m_titleText);
396
396
397 QPointF center = grid.center() - m_title->boundingRect().center();
397 QPointF center = grid.center() - m_title->boundingRect().center();
398 if(orientation()==Qt::Horizontal) {
398 if(orientation()==Qt::Horizontal) {
399 m_title->setPos(center.x(),m_axisRect.bottom()-m_title->boundingRect().height());
399 m_title->setPos(center.x(),m_axisRect.bottom()-m_title->boundingRect().height());
400 }
400 }
401 else if(orientation()==Qt::Vertical) {
401 else if(orientation()==Qt::Vertical) {
402 m_title->setTransformOriginPoint(m_title->boundingRect().center());
402 m_title->setTransformOriginPoint(m_title->boundingRect().center());
403 m_title->setRotation(270);
403 m_title->setRotation(270);
404 m_title->setPos(m_axisRect.left()- m_title->boundingRect().width()/2+m_title->boundingRect().height()/2,center.y());
404 m_title->setPos(m_axisRect.left()- m_title->boundingRect().width()/2+m_title->boundingRect().height()/2,center.y());
405 }
405 }
406 }
406 }
407
407
408 QVector<qreal> layout = calculateLayout();
408 QVector<qreal> layout = calculateLayout();
409 updateLayout(layout);
409 updateLayout(layout);
410
410
411 }
411 }
412
412
413 void ChartAxis::axisSelected()
413 void ChartAxis::axisSelected()
414 {
414 {
415 //TODO: axis clicked;
415 //TODO: axis clicked;
416 }
416 }
417
417
418 void ChartAxis::setLabelBetweenTicks(bool enabled)
419 {
420 m_labelBetween=enabled;
421 }
422
423 Qt::Orientation ChartAxis::orientation() const
418 Qt::Orientation ChartAxis::orientation() const
424 {
419 {
425 return m_chartAxis->orientation();
420 return m_chartAxis->orientation();
426 }
421 }
427
422
428 Qt::Alignment ChartAxis::alignment() const
423 Qt::Alignment ChartAxis::alignment() const
429 {
424 {
430 return m_chartAxis->alignment();
425 return m_chartAxis->alignment();
431 }
426 }
432
427
433 bool ChartAxis::isVisible()
428 bool ChartAxis::isVisible()
434 {
429 {
435 return m_chartAxis->isVisible();
430 return m_chartAxis->isVisible();
436 }
431 }
437
432
438 void ChartAxis::setLabels(const QStringList& labels)
433 void ChartAxis::setLabels(const QStringList& labels)
439 {
434 {
440 m_labelsList=labels;
435 m_labelsList=labels;
441 }
436 }
442
437
443 QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
438 QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF& constraint) const
444 {
439 {
445
440
446 Q_UNUSED(constraint);
441 Q_UNUSED(constraint);
447 QFontMetrics fn(m_title->font());
442 QFontMetrics fn(m_title->font());
448 QSizeF sh;
443 QSizeF sh;
449
444
450 if(m_titleText.isNull()) return sh;
445 if(m_titleText.isNull()) return sh;
451
446
452 switch(which) {
447 switch(which) {
453 case Qt::MinimumSize:
448 case Qt::MinimumSize:
454 if(orientation()==Qt::Horizontal) {
449 if(orientation()==Qt::Horizontal) {
455 sh = QSizeF(fn.boundingRect ("...").width(),fn.height());
450 sh = QSizeF(fn.boundingRect ("...").width(),fn.height());
456 }
451 }
457 else if(orientation()==Qt::Vertical) {
452 else if(orientation()==Qt::Vertical) {
458 sh = QSizeF(fn.height(),fn.boundingRect ("...").width());
453 sh = QSizeF(fn.height(),fn.boundingRect ("...").width());
459 }
454 }
460
455
461 break;
456 break;
462 case Qt::MaximumSize:
457 case Qt::MaximumSize:
463 case Qt::PreferredSize:
458 case Qt::PreferredSize:
464 if(orientation()==Qt::Horizontal) {
459 if(orientation()==Qt::Horizontal) {
465 sh = QSizeF(fn.boundingRect(m_chartAxis->title()).width(),fn.height());
460 sh = QSizeF(fn.boundingRect(m_chartAxis->title()).width(),fn.height());
466 }
461 }
467 else if(orientation()==Qt::Vertical) {
462 else if(orientation()==Qt::Vertical) {
468 sh = QSizeF(fn.height(),fn.boundingRect(m_chartAxis->title()).width());
463 sh = QSizeF(fn.height(),fn.boundingRect(m_chartAxis->title()).width());
469 }
464 }
470
465
471 break;
466 break;
472 default:
467 default:
473 break;
468 break;
474 }
469 }
475
470
476 return sh;
471 return sh;
477 }
472 }
478
473
479 QStringList ChartAxis::createValueLabels(int ticks) const
474 QStringList ChartAxis::createValueLabels(int ticks) const
480 {
475 {
481 Q_ASSERT(m_max>m_min);
476 Q_ASSERT(m_max>m_min);
482 Q_ASSERT(ticks>1);
477 Q_ASSERT(ticks>1);
483
478
484 QStringList labels;
479 QStringList labels;
485
480
486 int n = qMax(int(-qFloor(log10((m_max-m_min)/(ticks-1)))),0);
481 int n = qMax(int(-qFloor(log10((m_max-m_min)/(ticks-1)))),0);
487 n++;
482 n++;
488
483
489 QValueAxis *axis = qobject_cast<QValueAxis *>(m_chartAxis);
484 QValueAxis *axis = qobject_cast<QValueAxis *>(m_chartAxis);
490
485
491 QString format = axis->labelFormat();
486 QString format = axis->labelFormat();
492
487
493 if(format.isNull()) {
488 if(format.isNull()) {
494 for (int i=0; i< ticks; i++) {
489 for (int i=0; i< ticks; i++) {
495 qreal value = m_min + (i * (m_max - m_min)/ (ticks-1));
490 qreal value = m_min + (i * (m_max - m_min)/ (ticks-1));
496 labels << QString::number(value,'f',n);
491 labels << QString::number(value,'f',n);
497 }
492 }
498 }
493 }
499 else {
494 else {
500 QByteArray array = format.toAscii();
495 QByteArray array = format.toAscii();
501 for (int i=0; i< ticks; i++) {
496 for (int i=0; i< ticks; i++) {
502 qreal value = m_min + (i * (m_max - m_min)/ (ticks-1));
497 qreal value = m_min + (i * (m_max - m_min)/ (ticks-1));
503 labels << QString().sprintf(array, value);
498 labels << QString().sprintf(array, value);
504 }
499 }
505 }
500 }
506
501
507 return labels;
502 return labels;
508 }
503 }
509
504
510 QStringList ChartAxis::createDateTimeLabels(const QString& format,int ticks) const
505 QStringList ChartAxis::createDateTimeLabels(const QString& format,int ticks) const
511 {
506 {
512 Q_ASSERT(m_max>m_min);
507 Q_ASSERT(m_max>m_min);
513 Q_ASSERT(ticks>1);
508 Q_ASSERT(ticks>1);
514 QStringList labels;
509 QStringList labels;
515 int n = qMax(int(-floor(log10((m_max-m_min)/(ticks-1)))),0);
510 int n = qMax(int(-floor(log10((m_max-m_min)/(ticks-1)))),0);
516 n++;
511 n++;
517 for (int i=0; i< ticks; i++) {
512 for (int i=0; i< ticks; i++) {
518 qreal value = m_min + (i * (m_max - m_min)/ (ticks-1));
513 qreal value = m_min + (i * (m_max - m_min)/ (ticks-1));
519 labels << QDateTime::fromMSecsSinceEpoch(value).toString(format);
514 labels << QDateTime::fromMSecsSinceEpoch(value).toString(format);
520 }
515 }
521
516
522 return labels;
517 return labels;
523 }
518 }
524
519
525 #include "moc_chartaxis_p.cpp"
520 #include "moc_chartaxis_p.cpp"
526
521
527 QTCOMMERCIALCHART_END_NAMESPACE
522 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,195 +1,195
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef CHARTAXIS_H
30 #ifndef CHARTAXIS_H
31 #define CHARTAXIS_H
31 #define CHARTAXIS_H
32
32
33 #include "qchartglobal.h"
33 #include "qchartglobal.h"
34 #include "chartelement_p.h"
34 #include "chartelement_p.h"
35 #include "axisanimation_p.h"
35 #include "axisanimation_p.h"
36 #include <QGraphicsItem>
36 #include <QGraphicsItem>
37 #include <QGraphicsLayoutItem>
37 #include <QGraphicsLayoutItem>
38 #include <QFont>
38 #include <QFont>
39
39
40 QTCOMMERCIALCHART_BEGIN_NAMESPACE
40 QTCOMMERCIALCHART_BEGIN_NAMESPACE
41
41
42 class QAbstractAxis;
42 class QAbstractAxis;
43 class ChartPresenter;
43 class ChartPresenter;
44
44
45 class ChartAxis : public ChartElement, public QGraphicsLayoutItem
45 class ChartAxis : public ChartElement, public QGraphicsLayoutItem
46 {
46 {
47 Q_OBJECT
47 Q_OBJECT
48 Q_INTERFACES(QGraphicsLayoutItem)
48 Q_INTERFACES(QGraphicsLayoutItem)
49 public:
49 public:
50
50
51 ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter);
51 ChartAxis(QAbstractAxis *axis, ChartPresenter *presenter, bool intervalAxis = false);
52 ~ChartAxis();
52 ~ChartAxis();
53
53
54 void setArrowOpacity(qreal opacity);
54 void setArrowOpacity(qreal opacity);
55 qreal arrowOpacity() const;
55 qreal arrowOpacity() const;
56 void setArrowVisibility(bool visible);
56 void setArrowVisibility(bool visible);
57
57
58 void setGridOpacity(qreal opacity);
58 void setGridOpacity(qreal opacity);
59 qreal gridOpacity() const;
59 qreal gridOpacity() const;
60 void setGridVisibility(bool visible);
60 void setGridVisibility(bool visible);
61
61
62 void setLabelsOpacity(qreal opacity);
62 void setLabelsOpacity(qreal opacity);
63 qreal labelsOpacity() const;
63 qreal labelsOpacity() const;
64 void setLabelsVisibility(bool visible);
64 void setLabelsVisibility(bool visible);
65
65
66 void setShadesOpacity(qreal opacity);
66 void setShadesOpacity(qreal opacity);
67 qreal shadesOpacity() const;
67 qreal shadesOpacity() const;
68 void setShadesVisibility(bool visible);
68 void setShadesVisibility(bool visible);
69
69
70 void setLabelsAngle(int angle);
70 void setLabelsAngle(int angle);
71 int labelsAngle()const { return m_labelsAngle; }
71 int labelsAngle()const { return m_labelsAngle; }
72
72
73 void setShadesBrush(const QBrush &brush);
73 void setShadesBrush(const QBrush &brush);
74 void setShadesPen(const QPen &pen);
74 void setShadesPen(const QPen &pen);
75
75
76 void setArrowPen(const QPen &pen);
76 void setArrowPen(const QPen &pen);
77 void setGridPen(const QPen &pen);
77 void setGridPen(const QPen &pen);
78
78
79 void setLabelsPen(const QPen &pen);
79 void setLabelsPen(const QPen &pen);
80 void setLabelsBrush(const QBrush &brush);
80 void setLabelsBrush(const QBrush &brush);
81 void setLabelsFont(const QFont &font);
81 void setLabelsFont(const QFont &font);
82 void setLabelPadding(int padding);
82 void setLabelPadding(int padding);
83 int labelPadding() const { return m_labelPadding;};
83 int labelPadding() const { return m_labelPadding;};
84
84
85 void setTitlePen(const QPen &pen);
85 void setTitlePen(const QPen &pen);
86 void setTitleBrush(const QBrush &brush);
86 void setTitleBrush(const QBrush &brush);
87 void setTitleFont(const QFont &font);
87 void setTitleFont(const QFont &font);
88 void setTitleText(const QString &title);
88 void setTitleText(const QString &title);
89
89
90 void setLayout(QVector<qreal> &layout);
90 void setLayout(QVector<qreal> &layout);
91 QVector<qreal> layout() const { return m_layoutVector; }
91 QVector<qreal> layout() const { return m_layoutVector; }
92
92
93 void setAnimation(AxisAnimation *animation);
93 void setAnimation(AxisAnimation *animation);
94 ChartAnimation *animation() const { return m_animation; };
94 ChartAnimation *animation() const { return m_animation; };
95
95
96 Qt::Orientation orientation() const;
96 Qt::Orientation orientation() const;
97 Qt::Alignment alignment() const;
97 Qt::Alignment alignment() const;
98
98
99 bool isVisible();
99 bool isVisible();
100 void hide();
100 void hide();
101
101
102 void setGeometry(const QRectF &axis, const QRectF &grid);
102 void setGeometry(const QRectF &axis, const QRectF &grid);
103 QRectF axisGeometry() const { return m_axisRect; }
103 QRectF axisGeometry() const { return m_axisRect; }
104 QRectF gridGeometry() const { return m_gridRect; }
104 QRectF gridGeometry() const { return m_gridRect; }
105
105
106 void setLabels(const QStringList& labels);
106 void setLabels(const QStringList& labels);
107 QStringList labels() const { return m_labelsList; }
107 QStringList labels() const { return m_labelsList; }
108
108
109 void setLabelBetweenTicks(bool enabled);
109 //this flag indicates that axis is used to show intervals it means labels are in between ticks
110 bool labelBetweenTicks() const { return m_labelBetween; }
110 bool intervalAxis() const { return m_intervalAxis; }
111
111
112 virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
112 virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
113
113
114 protected:
114 protected:
115 void setGeometry(const QRectF &size){ Q_UNUSED(size);};
115 void setGeometry(const QRectF &size){ Q_UNUSED(size);};
116 virtual void updateGeometry() = 0;
116 virtual void updateGeometry() = 0;
117 virtual QVector<qreal> calculateLayout() const = 0;
117 virtual QVector<qreal> calculateLayout() const = 0;
118
118
119 QList<QGraphicsItem *> lineItems() { return m_grid->childItems(); };
119 QList<QGraphicsItem *> lineItems() { return m_grid->childItems(); };
120 QList<QGraphicsItem *> labelItems() { return m_labels->childItems();};
120 QList<QGraphicsItem *> labelItems() { return m_labels->childItems();};
121 QList<QGraphicsItem *> shadeItems() { return m_shades->childItems();};
121 QList<QGraphicsItem *> shadeItems() { return m_shades->childItems();};
122 QList<QGraphicsItem *> arrowItems() { return m_arrow->childItems();};
122 QList<QGraphicsItem *> arrowItems() { return m_arrow->childItems();};
123
123
124 QFont font() const { return m_font; }
124 QFont font() const { return m_font; }
125 qreal min() const {return m_min; }
125 qreal min() const {return m_min; }
126 qreal max() const {return m_max; }
126 qreal max() const {return m_max; }
127 QStringList createValueLabels(int ticks) const;
127 QStringList createValueLabels(int ticks) const;
128 QStringList createDateTimeLabels(const QString& format,int ticks) const;
128 QStringList createDateTimeLabels(const QString& format,int ticks) const;
129
129
130 public Q_SLOTS:
130 public Q_SLOTS:
131 virtual void handleAxisUpdated();
131 virtual void handleAxisUpdated();
132 virtual void handleDomainUpdated();
132 virtual void handleDomainUpdated();
133
133
134 private:
134 private:
135 inline bool isEmpty();
135 inline bool isEmpty();
136 void createItems(int count);
136 void createItems(int count);
137 void deleteItems(int count);
137 void deleteItems(int count);
138 void updateLayout(QVector<qreal> &layout);
138 void updateLayout(QVector<qreal> &layout);
139 void axisSelected();
139 void axisSelected();
140
140
141 private:
141 private:
142 QAbstractAxis *m_chartAxis;
142 QAbstractAxis *m_chartAxis;
143 int m_labelsAngle;
143 int m_labelsAngle;
144 QRectF m_axisRect;
144 QRectF m_axisRect;
145 QRectF m_gridRect;
145 QRectF m_gridRect;
146 QScopedPointer<QGraphicsItemGroup> m_grid;
146 QScopedPointer<QGraphicsItemGroup> m_grid;
147 QScopedPointer<QGraphicsItemGroup> m_shades;
147 QScopedPointer<QGraphicsItemGroup> m_shades;
148 QScopedPointer<QGraphicsItemGroup> m_labels;
148 QScopedPointer<QGraphicsItemGroup> m_labels;
149 QScopedPointer<QGraphicsItemGroup> m_arrow;
149 QScopedPointer<QGraphicsItemGroup> m_arrow;
150 QGraphicsSimpleTextItem *m_title;
150 QGraphicsSimpleTextItem *m_title;
151 QVector<qreal> m_layoutVector;
151 QVector<qreal> m_layoutVector;
152 qreal m_min;
152 qreal m_min;
153 qreal m_max;
153 qreal m_max;
154 AxisAnimation *m_animation;
154 AxisAnimation *m_animation;
155 QFont m_font;
155 QFont m_font;
156 QString m_titleText;
156 QString m_titleText;
157 int m_labelPadding;
157 int m_labelPadding;
158 QStringList m_labelsList;
158 QStringList m_labelsList;
159 bool m_labelBetween;
159 bool m_intervalAxis;
160
160
161 friend class AxisAnimation;
161 friend class AxisAnimation;
162 friend class AxisItem;
162 friend class AxisItem;
163
163
164 };
164 };
165
165
166 class AxisItem: public QGraphicsLineItem
166 class AxisItem: public QGraphicsLineItem
167 {
167 {
168
168
169 public:
169 public:
170 explicit AxisItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {}
170 explicit AxisItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {}
171
171
172 protected:
172 protected:
173 void mousePressEvent(QGraphicsSceneMouseEvent *event) {
173 void mousePressEvent(QGraphicsSceneMouseEvent *event) {
174 Q_UNUSED(event)
174 Q_UNUSED(event)
175 m_axis->axisSelected();
175 m_axis->axisSelected();
176 }
176 }
177
177
178 QRectF boundingRect() const {
178 QRectF boundingRect() const {
179 return shape().boundingRect();
179 return shape().boundingRect();
180 }
180 }
181
181
182 QPainterPath shape() const {
182 QPainterPath shape() const {
183 QPainterPath path = QGraphicsLineItem::shape();
183 QPainterPath path = QGraphicsLineItem::shape();
184 QRectF rect = path.boundingRect();
184 QRectF rect = path.boundingRect();
185 path.addRect(rect.adjusted(0, 0, m_axis->orientation() != Qt::Horizontal ? 8 : 0, m_axis->orientation() != Qt::Vertical ? 8 : 0));
185 path.addRect(rect.adjusted(0, 0, m_axis->orientation() != Qt::Horizontal ? 8 : 0, m_axis->orientation() != Qt::Vertical ? 8 : 0));
186 return path;
186 return path;
187 }
187 }
188
188
189 private:
189 private:
190 ChartAxis *m_axis;
190 ChartAxis *m_axis;
191 };
191 };
192
192
193 QTCOMMERCIALCHART_END_NAMESPACE
193 QTCOMMERCIALCHART_END_NAMESPACE
194
194
195 #endif /* CHARTAXI_H */
195 #endif /* CHARTAXI_H */
@@ -1,136 +1,136
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "horizontalaxis_p.h"
21 #include "horizontalaxis_p.h"
22 #include "qabstractaxis.h"
22 #include "qabstractaxis.h"
23 #include <QFontMetrics>
23 #include <QFontMetrics>
24 #include <QDebug>
24 #include <QDebug>
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
27
28 HorizontalAxis::HorizontalAxis(QAbstractAxis *axis, ChartPresenter *presenter):ChartAxis(axis,presenter)
28 HorizontalAxis::HorizontalAxis(QAbstractAxis *axis, ChartPresenter *presenter,bool intervalAxis):ChartAxis(axis,presenter,intervalAxis)
29 {
29 {
30 }
30 }
31
31
32 HorizontalAxis::~HorizontalAxis()
32 HorizontalAxis::~HorizontalAxis()
33 {
33 {
34 }
34 }
35
35
36 void HorizontalAxis::updateGeometry()
36 void HorizontalAxis::updateGeometry()
37 {
37 {
38 const QVector<qreal>& layout = ChartAxis::layout();
38 const QVector<qreal>& layout = ChartAxis::layout();
39
39
40 if(layout.isEmpty()) return;
40 if(layout.isEmpty()) return;
41
41
42 QStringList ticksList = labels();
42 QStringList ticksList = labels();
43
43
44 QList<QGraphicsItem *> lines = lineItems();
44 QList<QGraphicsItem *> lines = lineItems();
45 QList<QGraphicsItem *> labels = labelItems();
45 QList<QGraphicsItem *> labels = labelItems();
46 QList<QGraphicsItem *> shades = shadeItems();
46 QList<QGraphicsItem *> shades = shadeItems();
47 QList<QGraphicsItem *> axis = arrowItems();
47 QList<QGraphicsItem *> axis = arrowItems();
48
48
49 Q_ASSERT(labels.size() == ticksList.size());
49 Q_ASSERT(labels.size() == ticksList.size());
50 Q_ASSERT(layout.size() == ticksList.size());
50 Q_ASSERT(layout.size() == ticksList.size());
51
51
52 const QRectF& axisRect = axisGeometry();
52 const QRectF& axisRect = axisGeometry();
53 const QRectF& gridRect = gridGeometry();
53 const QRectF& gridRect = gridGeometry();
54
54
55 //arrow
55 //arrow
56 QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem*>(axis.at(0));
56 QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem*>(axis.at(0));
57
57
58 if (alignment()==Qt::AlignTop)
58 if (alignment()==Qt::AlignTop)
59 arrowItem->setLine(gridRect.left(), axisRect.bottom(), gridRect.right(), axisRect.bottom());
59 arrowItem->setLine(gridRect.left(), axisRect.bottom(), gridRect.right(), axisRect.bottom());
60 else if(alignment()==Qt::AlignBottom)
60 else if(alignment()==Qt::AlignBottom)
61 arrowItem->setLine(gridRect.left(), axisRect.top(), gridRect.right(), axisRect.top());
61 arrowItem->setLine(gridRect.left(), axisRect.top(), gridRect.right(), axisRect.top());
62
62
63 qreal width = 0;
63 qreal width = 0;
64
64
65 for (int i = 0; i < layout.size(); ++i) {
65 for (int i = 0; i < layout.size(); ++i) {
66
66
67 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i));
67 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i));
68 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
68 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
69 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
69 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
70
70
71 //grid line
71 //grid line
72 gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom());
72 gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom());
73
73
74 //label text
74 //label text
75 labelItem->setText(ticksList.at(i));
75 labelItem->setText(ticksList.at(i));
76 const QRectF& rect = labelItem->boundingRect();
76 const QRectF& rect = labelItem->boundingRect();
77 QPointF center = rect.center();
77 QPointF center = rect.center();
78 labelItem->setTransformOriginPoint(center.x(), center.y());
78 labelItem->setTransformOriginPoint(center.x(), center.y());
79
79
80 //ticks and label position
80 //ticks and label position
81 if (alignment()==Qt::AlignTop){
81 if (alignment()==Qt::AlignTop){
82 labelItem->setPos(layout[i] - center.x(), axisRect.bottom() - rect.height() - labelPadding());
82 labelItem->setPos(layout[i] - center.x(), axisRect.bottom() - rect.height() - labelPadding());
83 tickItem->setLine(layout[i],axisRect.bottom(),layout[i],axisRect.bottom() - labelPadding());
83 tickItem->setLine(layout[i],axisRect.bottom(),layout[i],axisRect.bottom() - labelPadding());
84 }else if(alignment()==Qt::AlignBottom){
84 }else if(alignment()==Qt::AlignBottom){
85 labelItem->setPos(layout[i] - center.x(), axisRect.top() + labelPadding());
85 labelItem->setPos(layout[i] - center.x(), axisRect.top() + labelPadding());
86 tickItem->setLine(layout[i],axisRect.top(),layout[i],axisRect.top() + labelPadding());
86 tickItem->setLine(layout[i],axisRect.top(),layout[i],axisRect.top() + labelPadding());
87 }
87 }
88
88
89 if(labelBetweenTicks()&& i+1!=layout.size()) {
89 if(intervalAxis()&& i+1!=layout.size()) {
90 const qreal delta = (layout[i+1] - layout[i])/2;
90 const qreal delta = (layout[i+1] - layout[i])/2;
91 labelItem->setPos(layout[i] + delta - center.x(), labelItem->pos().y());
91 labelItem->setPos(layout[i] + delta - center.x(), labelItem->pos().y());
92 }
92 }
93
93
94 //overlap detection
94 //overlap detection
95 if(labelItem->pos().x() <= width ||
95 if(labelItem->pos().x() <= width ||
96 labelItem->pos().x() < axisRect.left() ||
96 labelItem->pos().x() < axisRect.left() ||
97 labelItem->pos().x() + rect.width() > axisRect.right()) {
97 labelItem->pos().x() + rect.width() > axisRect.right()) {
98 labelItem->setVisible(false);
98 labelItem->setVisible(false);
99 gridItem->setVisible(false);
99 gridItem->setVisible(false);
100 tickItem->setVisible(false);
100 tickItem->setVisible(false);
101 }
101 }
102 else {
102 else {
103 labelItem->setVisible(true);
103 labelItem->setVisible(true);
104 gridItem->setVisible(true);
104 gridItem->setVisible(true);
105 tickItem->setVisible(true);
105 tickItem->setVisible(true);
106 width=rect.width()+labelItem->pos().x();
106 width=rect.width()+labelItem->pos().x();
107 }
107 }
108
108
109 //shades
109 //shades
110 if ((i+1)%2 && i>1) {
110 if ((i+1)%2 && i>1) {
111 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
111 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
112 rectItem->setRect(layout[i-1],gridRect.top(),layout[i]-layout[i-1],gridRect.height());
112 rectItem->setRect(layout[i-1],gridRect.top(),layout[i]-layout[i-1],gridRect.height());
113 }
113 }
114
114
115 // check if the grid line and the axis tick should be shown
115 // check if the grid line and the axis tick should be shown
116 qreal x = gridItem->line().p1().x();
116 qreal x = gridItem->line().p1().x();
117 if (x < gridRect.left() || x > gridRect.right()) {
117 if (x < gridRect.left() || x > gridRect.right()) {
118 gridItem->setVisible(false);
118 gridItem->setVisible(false);
119 tickItem->setVisible(false);
119 tickItem->setVisible(false);
120 if( labelBetweenTicks() && ( labelItem->pos().x() < gridRect.left() || labelItem->pos().x() + rect.width() > gridRect.right()))
120 if( intervalAxis() && ( labelItem->pos().x() < gridRect.left() || labelItem->pos().x() + rect.width() > gridRect.right()))
121 labelItem->setVisible(false);
121 labelItem->setVisible(false);
122 }
122 }
123
123
124 }
124 }
125
125
126 //begin/end grid line in case labels between
126 //begin/end grid line in case labels between
127 if(labelBetweenTicks()) {
127 if(intervalAxis()) {
128 QGraphicsLineItem *gridLine;
128 QGraphicsLineItem *gridLine;
129 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()));
129 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()));
130 gridLine->setLine(gridRect.right(), gridRect.top(), gridRect.right(), gridRect.bottom());
130 gridLine->setLine(gridRect.right(), gridRect.top(), gridRect.right(), gridRect.bottom());
131 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
131 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
132 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.left(), gridRect.bottom());
132 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.left(), gridRect.bottom());
133 }
133 }
134 }
134 }
135
135
136 QTCOMMERCIALCHART_END_NAMESPACE
136 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,49 +1,49
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef HORIZONTALAXIS_P_H_
30 #ifndef HORIZONTALAXIS_P_H_
31 #define HORIZONTALAXIS_P_H_
31 #define HORIZONTALAXIS_P_H_
32
32
33 #include "chartaxis_p.h"
33 #include "chartaxis_p.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class HorizontalAxis : public ChartAxis
37 class HorizontalAxis : public ChartAxis
38 {
38 {
39 public:
39 public:
40 HorizontalAxis(QAbstractAxis *axis, ChartPresenter *presenter);
40 HorizontalAxis(QAbstractAxis *axis, ChartPresenter *presenter,bool intervalAxis = false);
41 ~HorizontalAxis();
41 ~HorizontalAxis();
42 protected:
42 protected:
43 void updateGeometry();
43 void updateGeometry();
44
44
45 };
45 };
46
46
47 QTCOMMERCIALCHART_END_NAMESPACE
47 QTCOMMERCIALCHART_END_NAMESPACE
48
48
49 #endif
49 #endif
@@ -1,146 +1,146
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "verticalaxis_p.h"
21 #include "verticalaxis_p.h"
22 #include "qabstractaxis.h"
22 #include "qabstractaxis.h"
23 #include <QFontMetrics>
23 #include <QFontMetrics>
24
24
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
25 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26
26
27 VerticalAxis::VerticalAxis(QAbstractAxis *axis, ChartPresenter *presenter):ChartAxis(axis,presenter)
27 VerticalAxis::VerticalAxis(QAbstractAxis *axis, ChartPresenter *presenter, bool intervalAxis):ChartAxis(axis,presenter,intervalAxis)
28 {
28 {
29
29
30 }
30 }
31
31
32 VerticalAxis::~VerticalAxis()
32 VerticalAxis::~VerticalAxis()
33 {
33 {
34
34
35 }
35 }
36
36
37 void VerticalAxis::updateGeometry()
37 void VerticalAxis::updateGeometry()
38 {
38 {
39 const QVector<qreal> &layout = ChartAxis::layout();
39 const QVector<qreal> &layout = ChartAxis::layout();
40
40
41 if(layout.isEmpty()) return;
41 if(layout.isEmpty()) return;
42
42
43 QStringList labelList = labels();
43 QStringList labelList = labels();
44
44
45 QList<QGraphicsItem *> lines = lineItems();
45 QList<QGraphicsItem *> lines = lineItems();
46 QList<QGraphicsItem *> labels = labelItems();
46 QList<QGraphicsItem *> labels = labelItems();
47 QList<QGraphicsItem *> shades = shadeItems();
47 QList<QGraphicsItem *> shades = shadeItems();
48 QList<QGraphicsItem *> axis = arrowItems();
48 QList<QGraphicsItem *> axis = arrowItems();
49
49
50 Q_ASSERT(labels.size() == labelList.size());
50 Q_ASSERT(labels.size() == labelList.size());
51 Q_ASSERT(layout.size() == labelList.size());
51 Q_ASSERT(layout.size() == labelList.size());
52
52
53 const QRectF& axisRect = axisGeometry();
53 const QRectF& axisRect = axisGeometry();
54 const QRectF& gridRect = gridGeometry();
54 const QRectF& gridRect = gridGeometry();
55
55
56 qreal height = axisRect.bottom();
56 qreal height = axisRect.bottom();
57
57
58 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
58 QGraphicsLineItem *lineItem = static_cast<QGraphicsLineItem*>(axis.at(0));
59
59
60 if (alignment()==Qt::AlignLeft)
60 if (alignment()==Qt::AlignLeft)
61 lineItem->setLine( axisRect.right() , gridRect.top(), axisRect.right(), gridRect.bottom());
61 lineItem->setLine( axisRect.right() , gridRect.top(), axisRect.right(), gridRect.bottom());
62 else if(alignment()==Qt::AlignRight)
62 else if(alignment()==Qt::AlignRight)
63 lineItem->setLine( axisRect.left() , gridRect.top(), axisRect.left(), gridRect.bottom());
63 lineItem->setLine( axisRect.left() , gridRect.top(), axisRect.left(), gridRect.bottom());
64
64
65 QFontMetrics fn(font());
65 QFontMetrics fn(font());
66
66
67 for (int i = 0; i < layout.size(); ++i) {
67 for (int i = 0; i < layout.size(); ++i) {
68
68
69 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i));
69 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i));
70 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
70 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(axis.at(i+1));
71 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
71 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem*>(labels.at(i));
72
72
73 //grid line
73 //grid line
74 gridItem->setLine( gridRect.left() , layout[i], gridRect.right(), layout[i]);
74 gridItem->setLine( gridRect.left() , layout[i], gridRect.right(), layout[i]);
75
75
76 //label text
76 //label text
77 QString text = labelList.at(i);
77 QString text = labelList.at(i);
78 if (fn.boundingRect(text).width() > axisRect.right() - axisRect.left() - labelPadding() )
78 if (fn.boundingRect(text).width() > axisRect.right() - axisRect.left() - labelPadding() )
79 {
79 {
80 QString label = text + "...";
80 QString label = text + "...";
81 while (fn.boundingRect(label).width() > axisRect.right() - axisRect.left() - labelPadding() && label.length() > 3)
81 while (fn.boundingRect(label).width() > axisRect.right() - axisRect.left() - labelPadding() && label.length() > 3)
82 label.remove(label.length() - 4, 1);
82 label.remove(label.length() - 4, 1);
83 labelItem->setText(label);
83 labelItem->setText(label);
84 }
84 }
85 else {
85 else {
86 labelItem->setText(text);
86 labelItem->setText(text);
87 }
87 }
88 const QRectF& rect = labelItem->boundingRect();
88 const QRectF& rect = labelItem->boundingRect();
89 QPointF center = rect.center();
89 QPointF center = rect.center();
90 labelItem->setTransformOriginPoint(center.x(), center.y());
90 labelItem->setTransformOriginPoint(center.x(), center.y());
91
91
92 //ticks and label position
92 //ticks and label position
93 if (alignment()==Qt::AlignLeft) {
93 if (alignment()==Qt::AlignLeft) {
94 labelItem->setPos( axisRect.right() - rect.width() - labelPadding() , layout[i]-center.y());
94 labelItem->setPos( axisRect.right() - rect.width() - labelPadding() , layout[i]-center.y());
95 tickItem->setLine( axisRect.right()- labelPadding(),layout[i], axisRect.right(),layout[i]);
95 tickItem->setLine( axisRect.right()- labelPadding(),layout[i], axisRect.right(),layout[i]);
96 }
96 }
97 else if(alignment()==Qt::AlignRight) {
97 else if(alignment()==Qt::AlignRight) {
98 labelItem->setPos( axisRect.left() + labelPadding() , layout[i]-center.y());
98 labelItem->setPos( axisRect.left() + labelPadding() , layout[i]-center.y());
99 tickItem->setLine( axisRect.left(),layout[i], axisRect.left()+ labelPadding(),layout[i]);
99 tickItem->setLine( axisRect.left(),layout[i], axisRect.left()+ labelPadding(),layout[i]);
100 }
100 }
101 if(labelBetweenTicks()&& i+1!=layout.size()) {
101 if(intervalAxis()&& i+1!=layout.size()) {
102 const qreal delta = (layout[i+1] - layout[i])/2;
102 const qreal delta = (layout[i+1] - layout[i])/2;
103 labelItem->setPos(labelItem->pos().x() , layout[i] + delta - center.y());
103 labelItem->setPos(labelItem->pos().x() , layout[i] + delta - center.y());
104 }
104 }
105
105
106 //overlap detection
106 //overlap detection
107 if(labelItem->pos().y() + rect.height() > height ||
107 if(labelItem->pos().y() + rect.height() > height ||
108 labelItem->pos().y() + rect.height() > axisRect.bottom() ||
108 labelItem->pos().y() + rect.height() > axisRect.bottom() ||
109 labelItem->pos().y() < axisRect.top()) {
109 labelItem->pos().y() < axisRect.top()) {
110 labelItem->setVisible(false);
110 labelItem->setVisible(false);
111 gridItem->setVisible(false);
111 gridItem->setVisible(false);
112 tickItem->setVisible(false);
112 tickItem->setVisible(false);
113 }
113 }
114 else {
114 else {
115 labelItem->setVisible(true);
115 labelItem->setVisible(true);
116 gridItem->setVisible(true);
116 gridItem->setVisible(true);
117 height=labelItem->pos().y();
117 height=labelItem->pos().y();
118 }
118 }
119
119
120 //shades
120 //shades
121 if ((i+1)%2 && i>1) {
121 if ((i+1)%2 && i>1) {
122 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
122 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem*>(shades.at(i/2-1));
123 rectItem->setRect( gridRect.left(),layout[i], gridRect.width(),layout[i-1]-layout[i]);
123 rectItem->setRect( gridRect.left(),layout[i], gridRect.width(),layout[i-1]-layout[i]);
124 }
124 }
125
125
126 // check if the grid line and the axis tick should be shown
126 // check if the grid line and the axis tick should be shown
127 qreal y = gridItem->line().p1().y();
127 qreal y = gridItem->line().p1().y();
128 if (y < gridRect.top() || y > gridRect.bottom()) {
128 if (y < gridRect.top() || y > gridRect.bottom()) {
129 gridItem->setVisible(false);
129 gridItem->setVisible(false);
130 tickItem->setVisible(false);
130 tickItem->setVisible(false);
131 if( labelBetweenTicks() && ( labelItem->pos().y() < gridRect.top() || labelItem->pos().y() + rect.height() > gridRect.bottom()))
131 if( intervalAxis() && ( labelItem->pos().y() < gridRect.top() || labelItem->pos().y() + rect.height() > gridRect.bottom()))
132 labelItem->setVisible(false);
132 labelItem->setVisible(false);
133 }
133 }
134
134
135 }
135 }
136 //begin/end grid line in case labels between
136 //begin/end grid line in case labels between
137 if(labelBetweenTicks()) {
137 if(intervalAxis()) {
138 QGraphicsLineItem *gridLine;
138 QGraphicsLineItem *gridLine;
139 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()));
139 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()));
140 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.right(), gridRect.top());
140 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.right(), gridRect.top());
141 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
141 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
142 gridLine->setLine(gridRect.left(), gridRect.bottom(), gridRect.right(), gridRect.bottom());
142 gridLine->setLine(gridRect.left(), gridRect.bottom(), gridRect.right(), gridRect.bottom());
143 }
143 }
144 }
144 }
145
145
146 QTCOMMERCIALCHART_END_NAMESPACE
146 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,49 +1,49
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 // W A R N I N G
21 // W A R N I N G
22 // -------------
22 // -------------
23 //
23 //
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
24 // This file is not part of the QtCommercial Chart API. It exists purely as an
25 // implementation detail. This header file may change from version to
25 // implementation detail. This header file may change from version to
26 // version without notice, or even be removed.
26 // version without notice, or even be removed.
27 //
27 //
28 // We mean it.
28 // We mean it.
29
29
30 #ifndef VERTICALAXIS_P_H_
30 #ifndef VERTICALAXIS_P_H_
31 #define VERTICALAXIS_P_H_
31 #define VERTICALAXIS_P_H_
32
32
33 #include "chartaxis_p.h"
33 #include "chartaxis_p.h"
34
34
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35 QTCOMMERCIALCHART_BEGIN_NAMESPACE
36
36
37 class VerticalAxis : public ChartAxis
37 class VerticalAxis : public ChartAxis
38 {
38 {
39 public:
39 public:
40 VerticalAxis(QAbstractAxis *axis, ChartPresenter *presenter);
40 VerticalAxis(QAbstractAxis *axis, ChartPresenter *presenter, bool intervalAxis = false);
41 ~VerticalAxis();
41 ~VerticalAxis();
42 protected:
42 protected:
43 void updateGeometry();
43 void updateGeometry();
44
44
45 };
45 };
46
46
47 QTCOMMERCIALCHART_END_NAMESPACE
47 QTCOMMERCIALCHART_END_NAMESPACE
48
48
49 #endif /* VERTICALAXIS_P_H_ */
49 #endif /* VERTICALAXIS_P_H_ */
General Comments 0
You need to be logged in to leave comments. Login now