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