##// END OF EJS Templates
Fix axis sizehints when labels are angled...
Miikka Heikkinen -
r2412:38736d0999b4
parent child
Show More
@@ -1,143 +1,144
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 "chartlayout_p.h"
24 #include "chartlayout_p.h"
25 #include <QFontMetrics>
25 #include <QFontMetrics>
26 #include <QDebug>
26 #include <QDebug>
27 #include <qmath.h>
27 #include <qmath.h>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis, QGraphicsItem* item)
31 ChartBarCategoryAxisX::ChartBarCategoryAxisX(QBarCategoryAxis *axis, QGraphicsItem* item)
32 : HorizontalAxis(axis, item, true),
32 : HorizontalAxis(axis, item, true),
33 m_categoriesAxis(axis)
33 m_categoriesAxis(axis)
34 {
34 {
35 QObject::connect(m_categoriesAxis,SIGNAL(categoriesChanged()),this, SLOT(handleCategoriesChanged()));
35 QObject::connect(m_categoriesAxis,SIGNAL(categoriesChanged()),this, SLOT(handleCategoriesChanged()));
36 handleCategoriesChanged();
36 handleCategoriesChanged();
37 }
37 }
38
38
39 ChartBarCategoryAxisX::~ChartBarCategoryAxisX()
39 ChartBarCategoryAxisX::~ChartBarCategoryAxisX()
40 {
40 {
41 }
41 }
42
42
43 QVector<qreal> ChartBarCategoryAxisX::calculateLayout() const
43 QVector<qreal> ChartBarCategoryAxisX::calculateLayout() const
44 {
44 {
45 QVector<qreal> points;
45 QVector<qreal> points;
46 const QRectF& gridRect = gridGeometry();
46 const QRectF& gridRect = gridGeometry();
47 qreal range = max() - min();
47 qreal range = max() - min();
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 offset = int(offset * delta)%int(delta);
53 offset = int(offset * delta)%int(delta);
54
54
55 int count = qFloor(range);
55 int count = qFloor(range);
56 if(count < 1 ) return points;
56 if(count < 1 ) return points;
57
57
58 points.resize(count+2);
58 points.resize(count+2);
59
59
60 for (int i = 0; i < count+2; ++i) {
60 for (int i = 0; i < count+2; ++i) {
61 points[i] = offset + i * delta + gridRect.left();
61 points[i] = offset + i * delta + gridRect.left();
62 }
62 }
63
63
64 return points;
64 return points;
65 }
65 }
66
66
67 QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector<qreal>& layout) const
67 QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector<qreal>& layout) const
68 {
68 {
69 QStringList result ;
69 QStringList result ;
70 const QRectF &gridRect = gridGeometry();
70 const QRectF &gridRect = gridGeometry();
71 qreal d = (max() - min()) / gridRect.width();
71 qreal d = (max() - min()) / gridRect.width();
72
72
73 for (int i = 0; i < layout.count() - 1; ++i) {
73 for (int i = 0; i < layout.count() - 1; ++i) {
74 qreal x = qFloor((((layout[i] + layout[i + 1]) / 2 - gridRect.left()) * d + min() + 0.5));
74 qreal x = qFloor((((layout[i] + layout[i + 1]) / 2 - gridRect.left()) * d + min() + 0.5));
75 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
75 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
76 result << m_categoriesAxis->categories().at(x);
76 result << m_categoriesAxis->categories().at(x);
77 } else {
77 } else {
78 // No label for x coordinate
78 // No label for x coordinate
79 result << "";
79 result << "";
80 }
80 }
81 }
81 }
82 result << "";
82 result << "";
83 return result;
83 return result;
84 }
84 }
85
85
86
86
87 void ChartBarCategoryAxisX::updateGeometry()
87 void ChartBarCategoryAxisX::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 HorizontalAxis::updateGeometry();
93 HorizontalAxis::updateGeometry();
94 }
94 }
95
95
96 void ChartBarCategoryAxisX::handleCategoriesChanged()
96 void ChartBarCategoryAxisX::handleCategoriesChanged()
97 {
97 {
98 QGraphicsLayoutItem::updateGeometry();
98 QGraphicsLayoutItem::updateGeometry();
99 if(presenter()) presenter()->layout()->invalidate();
99 if(presenter()) presenter()->layout()->invalidate();
100 }
100 }
101
101
102 QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
102 QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
103 {
103 {
104 Q_UNUSED(constraint)
104 Q_UNUSED(constraint)
105
105
106 QFontMetrics fn(font());
106 QFontMetrics fn(font());
107 QSizeF sh;
107 QSizeF sh;
108 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
108 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
109 QStringList ticksList = m_categoriesAxis->categories();
109 QStringList ticksList = m_categoriesAxis->categories();
110
110
111 qreal width=0;
111 qreal width=0;
112 qreal height=0;
112 qreal height=0;
113
113
114 switch (which) {
114 switch (which) {
115 case Qt::MinimumSize:
115 case Qt::MinimumSize: {
116 width = fn.boundingRect("...").width();
116 QRectF boundingRect = labelBoundingRect(fn, "...");
117 height = fn.height()+labelPadding();
117 width = qMax(boundingRect.width(), base.width());
118 width=qMax(width,base.width());
118 height = boundingRect.height() + labelPadding();
119 height += base.height();
119 height += base.height();
120 sh = QSizeF(width,height);
120 sh = QSizeF(width, height);
121 break;
121 break;
122 }
122 case Qt::PreferredSize:{
123 case Qt::PreferredSize:{
123
124 int labelHeight = 0;
124 for (int i = 0; i < ticksList.size(); ++i)
125 foreach (const QString& s, ticksList) {
125 {
126 QRect rect = labelBoundingRect(fn, s);
126 QRectF rect = fn.boundingRect(ticksList.at(i));
127 labelHeight = qMax(rect.height(), labelHeight);
127 width += rect.width();
128 width += rect.width();
128 }
129 }
129 height = fn.height()+labelPadding();
130 height = labelHeight + labelPadding();
130 width = qMax(width,base.width());
131 height += base.height();
131 height += base.height();
132 sh = QSizeF(width,height);
132 width = qMax(width, base.width());
133 sh = QSizeF(width, height);
133 break;
134 break;
134 }
135 }
135 default:
136 default:
136 break;
137 break;
137 }
138 }
138 return sh;
139 return sh;
139 }
140 }
140
141
141 #include "moc_chartbarcategoryaxisx_p.cpp"
142 #include "moc_chartbarcategoryaxisx_p.cpp"
142
143
143 QTCOMMERCIALCHART_END_NAMESPACE
144 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,143 +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 "chartlayout_p.h"
24 #include "chartlayout_p.h"
25 #include <qmath.h>
25 #include <qmath.h>
26 #include <QFontMetrics>
26 #include <QFontMetrics>
27 #include <QDebug>
27 #include <QDebug>
28
28
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
30
31 ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis, QGraphicsItem* item)
31 ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis, QGraphicsItem* item)
32 : VerticalAxis(axis, item, true),
32 : VerticalAxis(axis, item, true),
33 m_categoriesAxis(axis)
33 m_categoriesAxis(axis)
34 {
34 {
35 QObject::connect( m_categoriesAxis,SIGNAL(categoriesChanged()),this, SLOT(handleCategoriesChanged()));
35 QObject::connect( m_categoriesAxis,SIGNAL(categoriesChanged()),this, SLOT(handleCategoriesChanged()));
36 handleCategoriesChanged();
36 handleCategoriesChanged();
37 }
37 }
38
38
39 ChartBarCategoryAxisY::~ChartBarCategoryAxisY()
39 ChartBarCategoryAxisY::~ChartBarCategoryAxisY()
40 {
40 {
41 }
41 }
42
42
43 QVector<qreal> ChartBarCategoryAxisY::calculateLayout() const
43 QVector<qreal> ChartBarCategoryAxisY::calculateLayout() const
44 {
44 {
45 QVector<qreal> points;
45 QVector<qreal> points;
46 const QRectF& gridRect = gridGeometry();
46 const QRectF& gridRect = gridGeometry();
47 qreal range = max() - min();
47 qreal range = max() - min();
48 const qreal delta = gridRect.height()/range;
48 const qreal delta = gridRect.height()/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 offset = int(offset * delta)%int(delta);
53 offset = int(offset * delta)%int(delta);
54
54
55 int count = qFloor(range);
55 int count = qFloor(range);
56 if(count < 1 ) return points;
56 if(count < 1 ) return points;
57
57
58 points.resize(count+2);
58 points.resize(count+2);
59
59
60 for (int i = 0; i < count+2; ++i) {
60 for (int i = 0; i < count+2; ++i) {
61 points[i] = gridRect.bottom() - i * delta -offset;
61 points[i] = gridRect.bottom() - i * delta -offset;
62 }
62 }
63
63
64 return points;
64 return points;
65 }
65 }
66
66
67 QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& layout) const
67 QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& layout) const
68 {
68 {
69 QStringList result;
69 QStringList result;
70 const QRectF &gridRect = gridGeometry();
70 const QRectF &gridRect = gridGeometry();
71 qreal d = (max() - min()) / gridRect.height();
71 qreal d = (max() - min()) / gridRect.height();
72
72
73 for (int i = 0; i < layout.count() - 1; ++i) {
73 for (int i = 0; i < layout.count() - 1; ++i) {
74 qreal x = qFloor(((gridRect.height() - (layout[i + 1] + layout[i]) / 2 + gridRect.top()) * d + min() + 0.5));
74 qreal x = qFloor(((gridRect.height() - (layout[i + 1] + layout[i]) / 2 + gridRect.top()) * d + min() + 0.5));
75 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
75 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
76 result << m_categoriesAxis->categories().at(x);
76 result << m_categoriesAxis->categories().at(x);
77 } else {
77 } else {
78 // No label for x coordinate
78 // No label for x coordinate
79 result << "";
79 result << "";
80 }
80 }
81 }
81 }
82 result << "";
82 result << "";
83 return result;
83 return result;
84 }
84 }
85
85
86 void ChartBarCategoryAxisY::updateGeometry()
86 void ChartBarCategoryAxisY::updateGeometry()
87 {
87 {
88 const QVector<qreal>& layout = ChartAxis::layout();
88 const QVector<qreal>& layout = ChartAxis::layout();
89 if (layout.isEmpty())
89 if (layout.isEmpty())
90 return;
90 return;
91 setLabels(createCategoryLabels(layout));
91 setLabels(createCategoryLabels(layout));
92 VerticalAxis::updateGeometry();
92 VerticalAxis::updateGeometry();
93 }
93 }
94
94
95 void ChartBarCategoryAxisY::handleCategoriesChanged()
95 void ChartBarCategoryAxisY::handleCategoriesChanged()
96 {
96 {
97 QGraphicsLayoutItem::updateGeometry();
97 QGraphicsLayoutItem::updateGeometry();
98 if(presenter()) presenter()->layout()->invalidate();
98 if(presenter()) presenter()->layout()->invalidate();
99 }
99 }
100
100
101 QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
101 QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
102 {
102 {
103 Q_UNUSED(constraint)
103 Q_UNUSED(constraint)
104
104
105 QFontMetrics fn(font());
105 QFontMetrics fn(font());
106 QSizeF sh;
106 QSizeF sh;
107 QSizeF base = VerticalAxis::sizeHint(which, constraint);
107 QSizeF base = VerticalAxis::sizeHint(which, constraint);
108 QStringList ticksList = m_categoriesAxis->categories();
108 QStringList ticksList = m_categoriesAxis->categories();
109 qreal width=0;
109 qreal width=0;
110 qreal height=0;
110 qreal height=0;
111
111
112 switch (which) {
112 switch (which) {
113 case Qt::MinimumSize:
113 case Qt::MinimumSize: {
114 width = fn.boundingRect("...").width() + labelPadding();
114 QRectF boundingRect = labelBoundingRect(fn, "...");
115 height = fn.height();
115 width = boundingRect.width() + labelPadding();
116 width+=base.width();
116 width += base.width();
117 if(base.width()>0) width+=labelPadding();
117 if (base.width() > 0)
118 height=qMax(height,base.height());
118 width += labelPadding();
119 sh = QSizeF(width,height);
119 height = qMax(boundingRect.height(), base.height());
120 sh = QSizeF(width, height);
120 break;
121 break;
122 }
121 case Qt::PreferredSize:{
123 case Qt::PreferredSize:{
122
124 int labelWidth = 0;
123 for (int i = 0; i < ticksList.size(); ++i)
125 foreach (const QString& s, ticksList) {
124 {
126 QRect rect = labelBoundingRect(fn, s);
125 QRectF rect = fn.boundingRect(ticksList.at(i));
127 labelWidth = qMax(rect.width(), labelWidth);
126 height+=rect.height();
128 height += rect.height();
127 width=qMax(rect.width()+labelPadding(),width); //one pixel torelance
128 }
129 }
129 height=qMax(height,base.height());
130 width = labelWidth + labelPadding() + 1;
130 width+=base.width();
131 width += base.width();
131 if(base.width()>0) width+=labelPadding();
132 if (base.width() > 0)
132 sh = QSizeF(width,height);
133 width += labelPadding();
134 height = qMax(height, base.height());
135 sh = QSizeF(width, height);
133 break;
136 break;
134 }
137 }
135 default:
138 default:
136 break;
139 break;
137 }
140 }
138 return sh;
141 return sh;
139 }
142 }
140
143
141 #include "moc_chartbarcategoryaxisy_p.cpp"
144 #include "moc_chartbarcategoryaxisy_p.cpp"
142
145
143 QTCOMMERCIALCHART_END_NAMESPACE
146 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,119 +1,121
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, QGraphicsItem* item)
31 ChartCategoryAxisX::ChartCategoryAxisX(QCategoryAxis *axis, QGraphicsItem* item)
32 : HorizontalAxis(axis, item, true),
32 : HorizontalAxis(axis, item, 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 //TODO :: ChartAxis::handleAxisUpdated();
78 //TODO :: 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 = HorizontalAxis::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 QRectF boundingRect = labelBoundingRect(fn, "...");
95 height = fn.height() + labelPadding();
95 width = qMax(boundingRect.width(), base.width());
96 width = qMax(width, base.width());
96 height = boundingRect.height() + labelPadding();
97 height += base.height();
97 height += base.height();
98 sh = QSizeF(width, height);
98 sh = QSizeF(width, height);
99 break;
99 break;
100 }
100 case Qt::PreferredSize: {
101 case Qt::PreferredSize: {
101
102 int labelHeight = 0;
102 for (int i = 0; i < ticksList.size(); ++i) {
103 foreach (const QString& s, ticksList) {
103 QRectF rect = fn.boundingRect(ticksList.at(i));
104 QRect rect = labelBoundingRect(fn, s);
105 labelHeight = qMax(rect.height(), labelHeight);
104 width += rect.width();
106 width += rect.width();
105 height = qMax(rect.height() + labelPadding(), height);
106 }
107 }
107 width = qMax(width, base.width());
108 height = labelHeight + labelPadding();
108 height += base.height();
109 height += base.height();
110 width = qMax(width, base.width());
109 sh = QSizeF(width, height);
111 sh = QSizeF(width, height);
110 break;
112 break;
111 }
113 }
112 default:
114 default:
113 break;
115 break;
114 }
116 }
115
117
116 return sh;
118 return sh;
117 }
119 }
118
120
119 QTCOMMERCIALCHART_END_NAMESPACE
121 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,118 +1,120
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, QGraphicsItem* item)
32 ChartCategoryAxisY::ChartCategoryAxisY(QCategoryAxis *axis, QGraphicsItem* item)
33 : VerticalAxis(axis, item, true),
33 : VerticalAxis(axis, item, 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 //TODO:: ChartAxis::handleAxisUpdated();
78 //TODO:: 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 = VerticalAxis::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 QRectF boundingRect = labelBoundingRect(fn, "...");
95 height = fn.height();
95 width = boundingRect.width() + labelPadding();
96 width += base.width();
96 width += base.width();
97 height = qMax(height, base.height());;
97 height = qMax(boundingRect.height(), base.height());
98 sh = QSizeF(width, height);
98 sh = QSizeF(width, height);
99 break;
99 break;
100 }
100 case Qt::PreferredSize: {
101 case Qt::PreferredSize: {
101
102 int labelWidth = 0;
102 for (int i = 0; i < ticksList.size(); ++i) {
103 foreach (const QString& s, ticksList) {
103 QRectF rect = fn.boundingRect(ticksList.at(i));
104 QRect rect = labelBoundingRect(fn, s);
104 width = qMax(rect.width() + labelPadding() + 1, width);
105 labelWidth = qMax(rect.width(), labelWidth);
105 height += rect.height();
106 height += rect.height();
106 }
107 }
107 height = qMax(height, base.height());
108 width = labelWidth + labelPadding() + 1;
108 width += base.width();
109 width += base.width();
110 height = qMax(height, base.height());
109 sh = QSizeF(width, height);
111 sh = QSizeF(width, height);
110 break;
112 break;
111 }
113 }
112 default:
114 default:
113 break;
115 break;
114 }
116 }
115 return sh;
117 return sh;
116 }
118 }
117
119
118 QTCOMMERCIALCHART_END_NAMESPACE
120 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,532 +1,546
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 "abstractdomain_p.h"
26 #include "abstractdomain_p.h"
27 #include <qmath.h>
27 #include <qmath.h>
28 #include <QDateTime>
28 #include <QDateTime>
29 #include <QValueAxis>
29 #include <QValueAxis>
30 #include <QLogValueAxis>
30 #include <QLogValueAxis>
31 #include <QGraphicsLayout>
31 #include <QGraphicsLayout>
32 #include <QFontMetrics>
32 #include <QFontMetrics>
33
33
34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
34 QTCOMMERCIALCHART_BEGIN_NAMESPACE
35
35
36 ChartAxis::ChartAxis(QAbstractAxis *axis, QGraphicsItem* item , bool intervalAxis)
36 ChartAxis::ChartAxis(QAbstractAxis *axis, QGraphicsItem* item , bool intervalAxis)
37 : ChartElement(item),
37 : ChartElement(item),
38 m_axis(axis),
38 m_axis(axis),
39 m_labelsAngle(axis->labelsAngle()),
39 m_labelsAngle(axis->labelsAngle()),
40 m_grid(new QGraphicsItemGroup(item)),
40 m_grid(new QGraphicsItemGroup(item)),
41 m_arrow(new QGraphicsItemGroup(item)),
41 m_arrow(new QGraphicsItemGroup(item)),
42 m_shades(new QGraphicsItemGroup(item)),
42 m_shades(new QGraphicsItemGroup(item)),
43 m_labels(new QGraphicsItemGroup(item)),
43 m_labels(new QGraphicsItemGroup(item)),
44 m_title(new QGraphicsSimpleTextItem(item)),
44 m_title(new QGraphicsSimpleTextItem(item)),
45 m_animation(0),
45 m_animation(0),
46 m_labelPadding(5),
46 m_labelPadding(5),
47 m_intervalAxis(intervalAxis)
47 m_intervalAxis(intervalAxis)
48 {
48 {
49 Q_ASSERT(item);
49 Q_ASSERT(item);
50 //initial initialization
50 //initial initialization
51 m_arrow->setHandlesChildEvents(false);
51 m_arrow->setHandlesChildEvents(false);
52 m_arrow->setZValue(ChartPresenter::AxisZValue);
52 m_arrow->setZValue(ChartPresenter::AxisZValue);
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 m_title->setZValue(ChartPresenter::GridZValue);
56 m_title->setZValue(ChartPresenter::GridZValue);
57 handleVisibleChanged(m_axis->isVisible());
57 handleVisibleChanged(m_axis->isVisible());
58 connectSlots();
58 connectSlots();
59
59
60 setFlag(QGraphicsItem::ItemHasNoContents,true);
60 setFlag(QGraphicsItem::ItemHasNoContents,true);
61 }
61 }
62
62
63 void ChartAxis::connectSlots()
63 void ChartAxis::connectSlots()
64 {
64 {
65 QObject::connect(m_axis,SIGNAL(visibleChanged(bool)),this,SLOT(handleVisibleChanged(bool)));
65 QObject::connect(m_axis,SIGNAL(visibleChanged(bool)),this,SLOT(handleVisibleChanged(bool)));
66 QObject::connect(m_axis,SIGNAL(lineVisibleChanged(bool)),this,SLOT(handleArrowVisibleChanged(bool)));
66 QObject::connect(m_axis,SIGNAL(lineVisibleChanged(bool)),this,SLOT(handleArrowVisibleChanged(bool)));
67 QObject::connect(m_axis,SIGNAL(gridVisibleChanged(bool)),this,SLOT(handleGridVisibleChanged(bool)));
67 QObject::connect(m_axis,SIGNAL(gridVisibleChanged(bool)),this,SLOT(handleGridVisibleChanged(bool)));
68 QObject::connect(m_axis,SIGNAL(labelsVisibleChanged(bool)),this,SLOT(handleLabelsVisibleChanged(bool)));
68 QObject::connect(m_axis,SIGNAL(labelsVisibleChanged(bool)),this,SLOT(handleLabelsVisibleChanged(bool)));
69 QObject::connect(m_axis,SIGNAL(shadesVisibleChanged(bool)),this,SLOT(handleShadesVisibleChanged(bool)));
69 QObject::connect(m_axis,SIGNAL(shadesVisibleChanged(bool)),this,SLOT(handleShadesVisibleChanged(bool)));
70 QObject::connect(m_axis,SIGNAL(labelsAngleChanged(int)),this,SLOT(handleLabelsAngleChanged(int)));
70 QObject::connect(m_axis,SIGNAL(labelsAngleChanged(int)),this,SLOT(handleLabelsAngleChanged(int)));
71 QObject::connect(m_axis,SIGNAL(linePenChanged(const QPen&)),this,SLOT(handleArrowPenChanged(const QPen&)));
71 QObject::connect(m_axis,SIGNAL(linePenChanged(const QPen&)),this,SLOT(handleArrowPenChanged(const QPen&)));
72 QObject::connect(m_axis,SIGNAL(labelsPenChanged(const QPen&)),this,SLOT(handleLabelsPenChanged(const QPen&)));
72 QObject::connect(m_axis,SIGNAL(labelsPenChanged(const QPen&)),this,SLOT(handleLabelsPenChanged(const QPen&)));
73 QObject::connect(m_axis,SIGNAL(labelsBrushChanged(const QBrush&)),this,SLOT(handleLabelsBrushChanged(const QBrush&)));
73 QObject::connect(m_axis,SIGNAL(labelsBrushChanged(const QBrush&)),this,SLOT(handleLabelsBrushChanged(const QBrush&)));
74 QObject::connect(m_axis,SIGNAL(labelsFontChanged(const QFont&)),this,SLOT(handleLabelsFontChanged(const QFont&)));
74 QObject::connect(m_axis,SIGNAL(labelsFontChanged(const QFont&)),this,SLOT(handleLabelsFontChanged(const QFont&)));
75 QObject::connect(m_axis,SIGNAL(gridLinePenChanged(const QPen&)),this,SLOT(handleGridPenChanged(const QPen&)));
75 QObject::connect(m_axis,SIGNAL(gridLinePenChanged(const QPen&)),this,SLOT(handleGridPenChanged(const QPen&)));
76 QObject::connect(m_axis,SIGNAL(shadesPenChanged(const QPen&)),this,SLOT(handleShadesPenChanged(const QPen&)));
76 QObject::connect(m_axis,SIGNAL(shadesPenChanged(const QPen&)),this,SLOT(handleShadesPenChanged(const QPen&)));
77 QObject::connect(m_axis,SIGNAL(shadesBrushChanged(const QBrush&)),this,SLOT(handleShadesBrushChanged(const QBrush&)));
77 QObject::connect(m_axis,SIGNAL(shadesBrushChanged(const QBrush&)),this,SLOT(handleShadesBrushChanged(const QBrush&)));
78 QObject::connect(m_axis,SIGNAL(titleTextChanged(const QString&)),this,SLOT(handleTitleTextChanged(const QString&)));
78 QObject::connect(m_axis,SIGNAL(titleTextChanged(const QString&)),this,SLOT(handleTitleTextChanged(const QString&)));
79 QObject::connect(m_axis,SIGNAL(titleFontChanged(const QFont&)),this,SLOT(handleTitleFontChanged(const QFont&)));
79 QObject::connect(m_axis,SIGNAL(titleFontChanged(const QFont&)),this,SLOT(handleTitleFontChanged(const QFont&)));
80 QObject::connect(m_axis,SIGNAL(titlePenChanged(const QPen&)),this,SLOT(handleTitlePenChanged(const QPen&)));
80 QObject::connect(m_axis,SIGNAL(titlePenChanged(const QPen&)),this,SLOT(handleTitlePenChanged(const QPen&)));
81 QObject::connect(m_axis,SIGNAL(titleBrushChanged(const QBrush&)),this,SLOT(handleTitleBrushChanged(const QBrush&)));
81 QObject::connect(m_axis,SIGNAL(titleBrushChanged(const QBrush&)),this,SLOT(handleTitleBrushChanged(const QBrush&)));
82 QObject::connect(m_axis,SIGNAL(titleVisibleChanged(bool)),this,SLOT(handleTitleVisibleChanged(bool)));
82 QObject::connect(m_axis,SIGNAL(titleVisibleChanged(bool)),this,SLOT(handleTitleVisibleChanged(bool)));
83 QObject::connect(m_axis->d_ptr.data(),SIGNAL(rangeChanged(qreal,qreal)),this,SLOT(handleRangeChanged(qreal,qreal)));
83 QObject::connect(m_axis->d_ptr.data(),SIGNAL(rangeChanged(qreal,qreal)),this,SLOT(handleRangeChanged(qreal,qreal)));
84 }
84 }
85
85
86 ChartAxis::~ChartAxis()
86 ChartAxis::~ChartAxis()
87 {
87 {
88 }
88 }
89
89
90 void ChartAxis::setAnimation(AxisAnimation *animation)
90 void ChartAxis::setAnimation(AxisAnimation *animation)
91 {
91 {
92 m_animation = animation;
92 m_animation = animation;
93 }
93 }
94
94
95 void ChartAxis::setLayout(QVector<qreal> &layout)
95 void ChartAxis::setLayout(QVector<qreal> &layout)
96 {
96 {
97 m_layoutVector = layout;
97 m_layoutVector = layout;
98 }
98 }
99
99
100 void ChartAxis::createItems(int count)
100 void ChartAxis::createItems(int count)
101 {
101 {
102 if (m_arrow->childItems().size() == 0){
102 if (m_arrow->childItems().size() == 0){
103 QGraphicsLineItem* arrow = new ArrowItem(this, this);
103 QGraphicsLineItem* arrow = new ArrowItem(this, this);
104 arrow->setPen(m_axis->linePen());
104 arrow->setPen(m_axis->linePen());
105 m_arrow->addToGroup(arrow);
105 m_arrow->addToGroup(arrow);
106 }
106 }
107
107
108 if (m_intervalAxis && m_grid->childItems().size() == 0) {
108 if (m_intervalAxis && m_grid->childItems().size() == 0) {
109 for (int i = 0 ; i < 2 ; i ++){
109 for (int i = 0 ; i < 2 ; i ++){
110 QGraphicsLineItem* item = new QGraphicsLineItem(this);
110 QGraphicsLineItem* item = new QGraphicsLineItem(this);
111 item->setPen(m_axis->gridLinePen());
111 item->setPen(m_axis->gridLinePen());
112 m_grid->addToGroup(item);
112 m_grid->addToGroup(item);
113 }
113 }
114 }
114 }
115
115
116 for (int i = 0; i < count; ++i) {
116 for (int i = 0; i < count; ++i) {
117 QGraphicsLineItem* arrow = new QGraphicsLineItem(this);
117 QGraphicsLineItem* arrow = new QGraphicsLineItem(this);
118 arrow->setPen(m_axis->linePen());
118 arrow->setPen(m_axis->linePen());
119 QGraphicsLineItem* grid = new QGraphicsLineItem(this);
119 QGraphicsLineItem* grid = new QGraphicsLineItem(this);
120 grid->setPen(m_axis->gridLinePen());
120 grid->setPen(m_axis->gridLinePen());
121 QGraphicsSimpleTextItem* label = new QGraphicsSimpleTextItem(this);
121 QGraphicsSimpleTextItem* label = new QGraphicsSimpleTextItem(this);
122 label->setFont(m_axis->labelsFont());
122 label->setFont(m_axis->labelsFont());
123 label->setPen(m_axis->labelsPen());
123 label->setPen(m_axis->labelsPen());
124 label->setBrush(m_axis->labelsBrush());
124 label->setBrush(m_axis->labelsBrush());
125 label->setRotation(m_labelsAngle);
125 label->setRotation(m_labelsAngle);
126 m_arrow->addToGroup(arrow);
126 m_arrow->addToGroup(arrow);
127 m_grid->addToGroup(grid);
127 m_grid->addToGroup(grid);
128 m_labels->addToGroup(label);
128 m_labels->addToGroup(label);
129
129
130 if ((m_grid->childItems().size()) % 2 && m_grid->childItems().size() > 2){
130 if ((m_grid->childItems().size()) % 2 && m_grid->childItems().size() > 2){
131 QGraphicsRectItem* shades = new QGraphicsRectItem(this);
131 QGraphicsRectItem* shades = new QGraphicsRectItem(this);
132 shades->setPen(m_axis->shadesPen());
132 shades->setPen(m_axis->shadesPen());
133 shades->setBrush(m_axis->shadesBrush());
133 shades->setBrush(m_axis->shadesBrush());
134 m_shades->addToGroup(shades);
134 m_shades->addToGroup(shades);
135 }
135 }
136 }
136 }
137
137
138 }
138 }
139
139
140 void ChartAxis::deleteItems(int count)
140 void ChartAxis::deleteItems(int count)
141 {
141 {
142 QList<QGraphicsItem *> lines = m_grid->childItems();
142 QList<QGraphicsItem *> lines = m_grid->childItems();
143 QList<QGraphicsItem *> labels = m_labels->childItems();
143 QList<QGraphicsItem *> labels = m_labels->childItems();
144 QList<QGraphicsItem *> shades = m_shades->childItems();
144 QList<QGraphicsItem *> shades = m_shades->childItems();
145 QList<QGraphicsItem *> axis = m_arrow->childItems();
145 QList<QGraphicsItem *> axis = m_arrow->childItems();
146
146
147 for (int i = 0; i < count; ++i) {
147 for (int i = 0; i < count; ++i) {
148 if (lines.size() % 2 && lines.size() > 1)
148 if (lines.size() % 2 && lines.size() > 1)
149 delete(shades.takeLast());
149 delete(shades.takeLast());
150 delete(lines.takeLast());
150 delete(lines.takeLast());
151 delete(labels.takeLast());
151 delete(labels.takeLast());
152 delete(axis.takeLast());
152 delete(axis.takeLast());
153 }
153 }
154 }
154 }
155
155
156 void ChartAxis::updateLayout(QVector<qreal> &layout)
156 void ChartAxis::updateLayout(QVector<qreal> &layout)
157 {
157 {
158 int diff = m_layoutVector.size() - layout.size();
158 int diff = m_layoutVector.size() - layout.size();
159
159
160 if (diff > 0)
160 if (diff > 0)
161 deleteItems(diff);
161 deleteItems(diff);
162 else if (diff < 0)
162 else if (diff < 0)
163 createItems(-diff);
163 createItems(-diff);
164
164
165 if (m_animation) {
165 if (m_animation) {
166 switch (presenter()->state()) {
166 switch (presenter()->state()) {
167 case ChartPresenter::ZoomInState:
167 case ChartPresenter::ZoomInState:
168 m_animation->setAnimationType(AxisAnimation::ZoomInAnimation);
168 m_animation->setAnimationType(AxisAnimation::ZoomInAnimation);
169 m_animation->setAnimationPoint(presenter()->statePoint());
169 m_animation->setAnimationPoint(presenter()->statePoint());
170 break;
170 break;
171 case ChartPresenter::ZoomOutState:
171 case ChartPresenter::ZoomOutState:
172 m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation);
172 m_animation->setAnimationType(AxisAnimation::ZoomOutAnimation);
173 m_animation->setAnimationPoint(presenter()->statePoint());
173 m_animation->setAnimationPoint(presenter()->statePoint());
174 break;
174 break;
175 case ChartPresenter::ScrollUpState:
175 case ChartPresenter::ScrollUpState:
176 case ChartPresenter::ScrollLeftState:
176 case ChartPresenter::ScrollLeftState:
177 m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation);
177 m_animation->setAnimationType(AxisAnimation::MoveBackwordAnimation);
178 break;
178 break;
179 case ChartPresenter::ScrollDownState:
179 case ChartPresenter::ScrollDownState:
180 case ChartPresenter::ScrollRightState:
180 case ChartPresenter::ScrollRightState:
181 m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation);
181 m_animation->setAnimationType(AxisAnimation::MoveForwardAnimation);
182 break;
182 break;
183 case ChartPresenter::ShowState:
183 case ChartPresenter::ShowState:
184 m_animation->setAnimationType(AxisAnimation::DefaultAnimation);
184 m_animation->setAnimationType(AxisAnimation::DefaultAnimation);
185 break;
185 break;
186 }
186 }
187 m_animation->setValues(m_layoutVector, layout);
187 m_animation->setValues(m_layoutVector, layout);
188 presenter()->startAnimation(m_animation);
188 presenter()->startAnimation(m_animation);
189 } else {
189 } else {
190 setLayout(layout);
190 setLayout(layout);
191 updateGeometry();
191 updateGeometry();
192 }
192 }
193 }
193 }
194
194
195 void ChartAxis::setLabelPadding(int padding)
195 void ChartAxis::setLabelPadding(int padding)
196 {
196 {
197 m_labelPadding = padding;
197 m_labelPadding = padding;
198 }
198 }
199
199
200 bool ChartAxis::isEmpty()
200 bool ChartAxis::isEmpty()
201 {
201 {
202 return m_axisRect.isEmpty() || m_gridRect.isEmpty() || qFuzzyCompare(min(),max());
202 return m_axisRect.isEmpty() || m_gridRect.isEmpty() || qFuzzyCompare(min(),max());
203 }
203 }
204
204
205 void ChartAxis::setGeometry(const QRectF &axis, const QRectF &grid)
205 void ChartAxis::setGeometry(const QRectF &axis, const QRectF &grid)
206 {
206 {
207 m_gridRect = grid;
207 m_gridRect = grid;
208 m_axisRect = axis;
208 m_axisRect = axis;
209
209
210 if (isEmpty())
210 if (isEmpty())
211 return;
211 return;
212
212
213 QVector<qreal> layout = calculateLayout();
213 QVector<qreal> layout = calculateLayout();
214 updateLayout(layout);
214 updateLayout(layout);
215 }
215 }
216
216
217 qreal ChartAxis::min() const
217 qreal ChartAxis::min() const
218 {
218 {
219 return m_axis->d_ptr->min();
219 return m_axis->d_ptr->min();
220 }
220 }
221
221
222 qreal ChartAxis::max() const
222 qreal ChartAxis::max() const
223 {
223 {
224 return m_axis->d_ptr->max();
224 return m_axis->d_ptr->max();
225 }
225 }
226
226
227 QFont ChartAxis::font() const
227 QFont ChartAxis::font() const
228 {
228 {
229 return m_axis->labelsFont();
229 return m_axis->labelsFont();
230 }
230 }
231
231
232 QFont ChartAxis::titleFont() const
232 QFont ChartAxis::titleFont() const
233 {
233 {
234 return m_axis->titleFont();
234 return m_axis->titleFont();
235 }
235 }
236
236
237 QString ChartAxis::titleText() const
237 QString ChartAxis::titleText() const
238 {
238 {
239 return m_axis->titleText();
239 return m_axis->titleText();
240 }
240 }
241
241
242 void ChartAxis::axisSelected()
242 void ChartAxis::axisSelected()
243 {
243 {
244 emit clicked();
244 emit clicked();
245 }
245 }
246
246
247 Qt::Orientation ChartAxis::orientation() const
247 Qt::Orientation ChartAxis::orientation() const
248 {
248 {
249 return m_axis->orientation();
249 return m_axis->orientation();
250 }
250 }
251
251
252 Qt::Alignment ChartAxis::alignment() const
252 Qt::Alignment ChartAxis::alignment() const
253 {
253 {
254 return m_axis->alignment();
254 return m_axis->alignment();
255 }
255 }
256
256
257 void ChartAxis::setLabels(const QStringList &labels)
257 void ChartAxis::setLabels(const QStringList &labels)
258 {
258 {
259 m_labelsList = labels;
259 m_labelsList = labels;
260 }
260 }
261
261
262 QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
262 QSizeF ChartAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
263 {
263 {
264 Q_UNUSED(which);
264 Q_UNUSED(which);
265 Q_UNUSED(constraint);
265 Q_UNUSED(constraint);
266 return QSizeF();
266 return QSizeF();
267 }
267 }
268
268
269 //handlers
269 //handlers
270
270
271 void ChartAxis::handleArrowVisibleChanged(bool visible)
271 void ChartAxis::handleArrowVisibleChanged(bool visible)
272 {
272 {
273 m_arrow->setVisible(visible);
273 m_arrow->setVisible(visible);
274 }
274 }
275
275
276 void ChartAxis::handleGridVisibleChanged(bool visible)
276 void ChartAxis::handleGridVisibleChanged(bool visible)
277 {
277 {
278 m_grid->setVisible(visible);
278 m_grid->setVisible(visible);
279 }
279 }
280
280
281 void ChartAxis::handleLabelsVisibleChanged(bool visible)
281 void ChartAxis::handleLabelsVisibleChanged(bool visible)
282 {
282 {
283 m_labels->setVisible(visible);
283 m_labels->setVisible(visible);
284 }
284 }
285
285
286 void ChartAxis::handleShadesVisibleChanged(bool visible)
286 void ChartAxis::handleShadesVisibleChanged(bool visible)
287 {
287 {
288 m_shades->setVisible(visible);
288 m_shades->setVisible(visible);
289 }
289 }
290
290
291 void ChartAxis::handleTitleVisibleChanged(bool visible)
291 void ChartAxis::handleTitleVisibleChanged(bool visible)
292 {
292 {
293 m_title->setVisible(visible);
293 m_title->setVisible(visible);
294 presenter()->layout()->invalidate();
294 presenter()->layout()->invalidate();
295 }
295 }
296
296
297 void ChartAxis::handleLabelsAngleChanged(int angle)
297 void ChartAxis::handleLabelsAngleChanged(int angle)
298 {
298 {
299 foreach (QGraphicsItem *item, m_labels->childItems())
299 foreach (QGraphicsItem *item, m_labels->childItems())
300 item->setRotation(angle);
300 item->setRotation(angle);
301
301
302 m_labelsAngle = angle;
302 m_labelsAngle = angle;
303 }
303 }
304
304
305 void ChartAxis::handleLabelsPenChanged(const QPen &pen)
305 void ChartAxis::handleLabelsPenChanged(const QPen &pen)
306 {
306 {
307 foreach (QGraphicsItem *item , m_labels->childItems())
307 foreach (QGraphicsItem *item , m_labels->childItems())
308 static_cast<QGraphicsSimpleTextItem *>(item)->setPen(pen);
308 static_cast<QGraphicsSimpleTextItem *>(item)->setPen(pen);
309 }
309 }
310
310
311 void ChartAxis::handleLabelsBrushChanged(const QBrush &brush)
311 void ChartAxis::handleLabelsBrushChanged(const QBrush &brush)
312 {
312 {
313 foreach (QGraphicsItem *item , m_labels->childItems())
313 foreach (QGraphicsItem *item , m_labels->childItems())
314 static_cast<QGraphicsSimpleTextItem *>(item)->setBrush(brush);
314 static_cast<QGraphicsSimpleTextItem *>(item)->setBrush(brush);
315 }
315 }
316
316
317 void ChartAxis::handleLabelsFontChanged(const QFont &font)
317 void ChartAxis::handleLabelsFontChanged(const QFont &font)
318 {
318 {
319 foreach (QGraphicsItem *item , m_labels->childItems())
319 foreach (QGraphicsItem *item , m_labels->childItems())
320 static_cast<QGraphicsSimpleTextItem *>(item)->setFont(font);
320 static_cast<QGraphicsSimpleTextItem *>(item)->setFont(font);
321 QGraphicsLayoutItem::updateGeometry();
321 QGraphicsLayoutItem::updateGeometry();
322 presenter()->layout()->invalidate();
322 presenter()->layout()->invalidate();
323 }
323 }
324
324
325 void ChartAxis::handleShadesBrushChanged(const QBrush &brush)
325 void ChartAxis::handleShadesBrushChanged(const QBrush &brush)
326 {
326 {
327 foreach (QGraphicsItem *item , m_shades->childItems())
327 foreach (QGraphicsItem *item , m_shades->childItems())
328 static_cast<QGraphicsRectItem *>(item)->setBrush(brush);
328 static_cast<QGraphicsRectItem *>(item)->setBrush(brush);
329 }
329 }
330
330
331 void ChartAxis::handleShadesPenChanged(const QPen &pen)
331 void ChartAxis::handleShadesPenChanged(const QPen &pen)
332 {
332 {
333 foreach (QGraphicsItem *item , m_shades->childItems())
333 foreach (QGraphicsItem *item , m_shades->childItems())
334 static_cast<QGraphicsRectItem *>(item)->setPen(pen);
334 static_cast<QGraphicsRectItem *>(item)->setPen(pen);
335 }
335 }
336
336
337 void ChartAxis::handleArrowPenChanged(const QPen &pen)
337 void ChartAxis::handleArrowPenChanged(const QPen &pen)
338 {
338 {
339 foreach (QGraphicsItem *item , m_arrow->childItems())
339 foreach (QGraphicsItem *item , m_arrow->childItems())
340 static_cast<QGraphicsLineItem *>(item)->setPen(pen);
340 static_cast<QGraphicsLineItem *>(item)->setPen(pen);
341 }
341 }
342
342
343 void ChartAxis::handleGridPenChanged(const QPen &pen)
343 void ChartAxis::handleGridPenChanged(const QPen &pen)
344 {
344 {
345 foreach (QGraphicsItem *item , m_grid->childItems())
345 foreach (QGraphicsItem *item , m_grid->childItems())
346 static_cast<QGraphicsLineItem *>(item)->setPen(pen);
346 static_cast<QGraphicsLineItem *>(item)->setPen(pen);
347 }
347 }
348
348
349 void ChartAxis::handleTitleTextChanged(const QString &title)
349 void ChartAxis::handleTitleTextChanged(const QString &title)
350 {
350 {
351 Q_UNUSED(title)
351 Q_UNUSED(title)
352 QGraphicsLayoutItem::updateGeometry();
352 QGraphicsLayoutItem::updateGeometry();
353 presenter()->layout()->invalidate();
353 presenter()->layout()->invalidate();
354 }
354 }
355
355
356
356
357 void ChartAxis::handleTitlePenChanged(const QPen &pen)
357 void ChartAxis::handleTitlePenChanged(const QPen &pen)
358 {
358 {
359 m_title->setPen(pen);
359 m_title->setPen(pen);
360 }
360 }
361
361
362 void ChartAxis::handleTitleBrushChanged(const QBrush &brush)
362 void ChartAxis::handleTitleBrushChanged(const QBrush &brush)
363 {
363 {
364 m_title->setBrush(brush);
364 m_title->setBrush(brush);
365 }
365 }
366
366
367 void ChartAxis::handleTitleFontChanged(const QFont &font)
367 void ChartAxis::handleTitleFontChanged(const QFont &font)
368 {
368 {
369 if(m_title->font() != font){
369 if(m_title->font() != font){
370 m_title->setFont(font);
370 m_title->setFont(font);
371 QGraphicsLayoutItem::updateGeometry();
371 QGraphicsLayoutItem::updateGeometry();
372 presenter()->layout()->invalidate();
372 presenter()->layout()->invalidate();
373 }
373 }
374 }
374 }
375
375
376 void ChartAxis::handleVisibleChanged(bool visible)
376 void ChartAxis::handleVisibleChanged(bool visible)
377 {
377 {
378 setVisible(visible);
378 setVisible(visible);
379 if(!visible) {
379 if(!visible) {
380 m_grid->setVisible(visible);
380 m_grid->setVisible(visible);
381 m_arrow->setVisible(visible);
381 m_arrow->setVisible(visible);
382 m_shades->setVisible(visible);
382 m_shades->setVisible(visible);
383 m_labels->setVisible(visible);
383 m_labels->setVisible(visible);
384 m_title->setVisible(visible);
384 m_title->setVisible(visible);
385 }else {
385 }else {
386 m_grid->setVisible(m_axis->isGridLineVisible());
386 m_grid->setVisible(m_axis->isGridLineVisible());
387 m_arrow->setVisible(m_axis->isLineVisible());
387 m_arrow->setVisible(m_axis->isLineVisible());
388 m_shades->setVisible(m_axis->shadesVisible());
388 m_shades->setVisible(m_axis->shadesVisible());
389 m_labels->setVisible(m_axis->labelsVisible());
389 m_labels->setVisible(m_axis->labelsVisible());
390 m_title->setVisible(m_axis->isTitleVisible());
390 m_title->setVisible(m_axis->isTitleVisible());
391 }
391 }
392
392
393 if(presenter()) presenter()->layout()->invalidate();
393 if(presenter()) presenter()->layout()->invalidate();
394 }
394 }
395
395
396 void ChartAxis::handleRangeChanged(qreal min, qreal max)
396 void ChartAxis::handleRangeChanged(qreal min, qreal max)
397 {
397 {
398 Q_UNUSED(min);
398 Q_UNUSED(min);
399 Q_UNUSED(max);
399 Q_UNUSED(max);
400
400
401 if (!isEmpty()) {
401 if (!isEmpty()) {
402
402
403 QVector<qreal> layout = calculateLayout();
403 QVector<qreal> layout = calculateLayout();
404 updateLayout(layout);
404 updateLayout(layout);
405 QSizeF before = effectiveSizeHint(Qt::PreferredSize);
405 QSizeF before = effectiveSizeHint(Qt::PreferredSize);
406 QSizeF after = sizeHint(Qt::PreferredSize);
406 QSizeF after = sizeHint(Qt::PreferredSize);
407
407
408 if (before != after) {
408 if (before != after) {
409 QGraphicsLayoutItem::updateGeometry();
409 QGraphicsLayoutItem::updateGeometry();
410 //we don't want to call invalidate on layout, since it will change minimum size of component,
410 //we don't want to call invalidate on layout, since it will change minimum size of component,
411 //which we would like to avoid since it causes nasty flips when scrolling or zooming,
411 //which we would like to avoid since it causes nasty flips when scrolling or zooming,
412 //instead recalculate layout and use plotArea for extra space.
412 //instead recalculate layout and use plotArea for extra space.
413 presenter()->layout()->setGeometry(presenter()->layout()->geometry());
413 presenter()->layout()->setGeometry(presenter()->layout()->geometry());
414 }
414 }
415 }
415 }
416
416
417 }
417 }
418
418
419 //helpers
419 //helpers
420
420
421 QStringList ChartAxis::createValueLabels(qreal min, qreal max, int ticks,const QString& format)
421 QStringList ChartAxis::createValueLabels(qreal min, qreal max, int ticks,const QString& format)
422 {
422 {
423 //TODO: Q_ASSERT(m_max > m_min);
423 //TODO: Q_ASSERT(m_max > m_min);
424 //TODO: Q_ASSERT(ticks > 1);
424 //TODO: Q_ASSERT(ticks > 1);
425
425
426 QStringList labels;
426 QStringList labels;
427
427
428 if(max <= min || ticks < 1){
428 if(max <= min || ticks < 1){
429 return labels;
429 return labels;
430 }
430 }
431
431
432 int n = qMax(int(-qFloor(log10((max - min) / (ticks - 1)))), 0);
432 int n = qMax(int(-qFloor(log10((max - min) / (ticks - 1)))), 0);
433 n++;
433 n++;
434
434
435 if (format.isNull()) {
435 if (format.isNull()) {
436 for (int i = 0; i < ticks; i++) {
436 for (int i = 0; i < ticks; i++) {
437 qreal value = min + (i * (max - min) / (ticks - 1));
437 qreal value = min + (i * (max - min) / (ticks - 1));
438 labels << QString::number(value, 'f', n);
438 labels << QString::number(value, 'f', n);
439 }
439 }
440 } else {
440 } else {
441 QByteArray array = format.toLatin1();
441 QByteArray array = format.toLatin1();
442 for (int i = 0; i < ticks; i++) {
442 for (int i = 0; i < ticks; i++) {
443 qreal value = min + (i * (max - min) / (ticks - 1));
443 qreal value = min + (i * (max - min) / (ticks - 1));
444 if (format.contains("d")
444 if (format.contains("d")
445 || format.contains("i")
445 || format.contains("i")
446 || format.contains("c"))
446 || format.contains("c"))
447 labels << QString().sprintf(array, (qint64)value);
447 labels << QString().sprintf(array, (qint64)value);
448 else if (format.contains("u")
448 else if (format.contains("u")
449 || format.contains("o")
449 || format.contains("o")
450 || format.contains("x", Qt::CaseInsensitive))
450 || format.contains("x", Qt::CaseInsensitive))
451 labels << QString().sprintf(array, (quint64)value);
451 labels << QString().sprintf(array, (quint64)value);
452 else if (format.contains("f", Qt::CaseInsensitive)
452 else if (format.contains("f", Qt::CaseInsensitive)
453 || format.contains("e", Qt::CaseInsensitive)
453 || format.contains("e", Qt::CaseInsensitive)
454 || format.contains("g", Qt::CaseInsensitive))
454 || format.contains("g", Qt::CaseInsensitive))
455 labels << QString().sprintf(array, value);
455 labels << QString().sprintf(array, value);
456 else
456 else
457 labels << QString();
457 labels << QString();
458 }
458 }
459 }
459 }
460
460
461 return labels;
461 return labels;
462 }
462 }
463
463
464 QStringList ChartAxis::createLogValueLabels(qreal min, qreal max, qreal base, int ticks, const QString& format)
464 QStringList ChartAxis::createLogValueLabels(qreal min, qreal max, qreal base, int ticks, const QString& format)
465 {
465 {
466 // Q_ASSERT(m_max > m_min);
466 // Q_ASSERT(m_max > m_min);
467 // Q_ASSERT(ticks > 1);
467 // Q_ASSERT(ticks > 1);
468
468
469 QStringList labels;
469 QStringList labels;
470
470
471 int n = 0;
471 int n = 0;
472 if (ticks > 1)
472 if (ticks > 1)
473 n = qMax(int(-qFloor(log10((max - min) / (ticks - 1)))), 0);
473 n = qMax(int(-qFloor(log10((max - min) / (ticks - 1)))), 0);
474 n++;
474 n++;
475
475
476 int firstTick;
476 int firstTick;
477 if (base > 1)
477 if (base > 1)
478 firstTick = ceil(log10(min) / log10(base));
478 firstTick = ceil(log10(min) / log10(base));
479 else
479 else
480 firstTick = ceil(log10(max) / log10(base));
480 firstTick = ceil(log10(max) / log10(base));
481
481
482 if (format.isNull()) {
482 if (format.isNull()) {
483 for (int i = firstTick; i < ticks + firstTick; i++) {
483 for (int i = firstTick; i < ticks + firstTick; i++) {
484 qreal value = qPow(base, i);
484 qreal value = qPow(base, i);
485 labels << QString::number(value, 'f', n);
485 labels << QString::number(value, 'f', n);
486 }
486 }
487 } else {
487 } else {
488 QByteArray array = format.toLatin1();
488 QByteArray array = format.toLatin1();
489 for (int i = firstTick; i < ticks + firstTick; i++) {
489 for (int i = firstTick; i < ticks + firstTick; i++) {
490 qreal value = qPow(base, i);
490 qreal value = qPow(base, i);
491 if (format.contains("d")
491 if (format.contains("d")
492 || format.contains("i")
492 || format.contains("i")
493 || format.contains("c"))
493 || format.contains("c"))
494 labels << QString().sprintf(array, (qint64)value);
494 labels << QString().sprintf(array, (qint64)value);
495 else if (format.contains("u")
495 else if (format.contains("u")
496 || format.contains("o")
496 || format.contains("o")
497 || format.contains("x", Qt::CaseInsensitive))
497 || format.contains("x", Qt::CaseInsensitive))
498 labels << QString().sprintf(array, (quint64)value);
498 labels << QString().sprintf(array, (quint64)value);
499 else if (format.contains("f", Qt::CaseInsensitive)
499 else if (format.contains("f", Qt::CaseInsensitive)
500 || format.contains("e", Qt::CaseInsensitive)
500 || format.contains("e", Qt::CaseInsensitive)
501 || format.contains("g", Qt::CaseInsensitive))
501 || format.contains("g", Qt::CaseInsensitive))
502 labels << QString().sprintf(array, value);
502 labels << QString().sprintf(array, value);
503 else
503 else
504 labels << QString();
504 labels << QString();
505 }
505 }
506 }
506 }
507
507
508 return labels;
508 return labels;
509 }
509 }
510
510
511 QStringList ChartAxis::createDateTimeLabels(qreal min, qreal max,int ticks,const QString& format)
511 QStringList ChartAxis::createDateTimeLabels(qreal min, qreal max,int ticks,const QString& format)
512 {
512 {
513 //TODO: Q_ASSERT(m_max > m_min);
513 //TODO: Q_ASSERT(m_max > m_min);
514 //TODO: Q_ASSERT(ticks > 1);
514 //TODO: Q_ASSERT(ticks > 1);
515 QStringList labels;
515 QStringList labels;
516
516
517 if(max <= min || ticks < 1) {
517 if(max <= min || ticks < 1) {
518 return labels;
518 return labels;
519 }
519 }
520
520
521 int n = qMax(int(-floor(log10((max - min) / (ticks - 1)))), 0);
521 int n = qMax(int(-floor(log10((max - min) / (ticks - 1)))), 0);
522 n++;
522 n++;
523 for (int i = 0; i < ticks; i++) {
523 for (int i = 0; i < ticks; i++) {
524 qreal value = min + (i * (max - min) / (ticks - 1));
524 qreal value = min + (i * (max - min) / (ticks - 1));
525 labels << QDateTime::fromMSecsSinceEpoch(value).toString(format);
525 labels << QDateTime::fromMSecsSinceEpoch(value).toString(format);
526 }
526 }
527 return labels;
527 return labels;
528 }
528 }
529
529
530 QRect ChartAxis::labelBoundingRect(const QFontMetrics &fn, const QString &label) const
531 {
532 QRect boundingRect = fn.boundingRect(label);
533
534 // Take label rotation into account
535 if (m_labelsAngle) {
536 QTransform transform;
537 transform.rotate(m_labelsAngle);
538 boundingRect = transform.mapRect(boundingRect);
539 }
540
541 return boundingRect;
542 }
543
530 #include "moc_chartaxis_p.cpp"
544 #include "moc_chartaxis_p.cpp"
531
545
532 QTCOMMERCIALCHART_END_NAMESPACE
546 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,196 +1,197
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, QGraphicsItem* item = 0, bool intervalAxis = false);
51 ChartAxis(QAbstractAxis *axis, QGraphicsItem* item = 0, bool intervalAxis = false);
52 ~ChartAxis();
52 ~ChartAxis();
53
53
54 QAbstractAxis* axis() const { return m_axis; }
54 QAbstractAxis* axis() const { return m_axis; }
55
55
56 void setLabelPadding(int padding);
56 void setLabelPadding(int padding);
57 int labelPadding() const { return m_labelPadding;};
57 int labelPadding() const { return m_labelPadding;};
58
58
59 QFont titleFont() const;
59 QFont titleFont() const;
60 QString titleText() const;
60 QString titleText() const;
61
61
62 void setLayout(QVector<qreal> &layout);
62 void setLayout(QVector<qreal> &layout);
63 QVector<qreal> layout() const { return m_layoutVector; }
63 QVector<qreal> layout() const { return m_layoutVector; }
64
64
65 void setAnimation(AxisAnimation *animation);
65 void setAnimation(AxisAnimation *animation);
66 ChartAnimation *animation() const { return m_animation; };
66 ChartAnimation *animation() const { return m_animation; };
67
67
68 Qt::Orientation orientation() const;
68 Qt::Orientation orientation() const;
69 Qt::Alignment alignment() const;
69 Qt::Alignment alignment() const;
70
70
71 void setGeometry(const QRectF &axis, const QRectF &grid);
71 void setGeometry(const QRectF &axis, const QRectF &grid);
72 QRectF axisGeometry() const { return m_axisRect; }
72 QRectF axisGeometry() const { return m_axisRect; }
73 QRectF gridGeometry() const { return m_gridRect; }
73 QRectF gridGeometry() const { return m_gridRect; }
74
74
75 void setLabels(const QStringList &labels);
75 void setLabels(const QStringList &labels);
76 QStringList labels() const { return m_labelsList; }
76 QStringList labels() const { return m_labelsList; }
77
77
78 //this flag indicates that axis is used to show intervals it means labels are in between ticks
78 //this flag indicates that axis is used to show intervals it means labels are in between ticks
79 bool intervalAxis() const { return m_intervalAxis; }
79 bool intervalAxis() const { return m_intervalAxis; }
80
80
81 virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
81 virtual QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const;
82
82
83
83
84 QRectF boundingRect() const{
84 QRectF boundingRect() const{
85 return QRectF();
85 return QRectF();
86 }
86 }
87
87
88 void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*)
88 void paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*)
89 {
89 {
90
90
91 }
91 }
92
92
93 //helpers
93 //helpers
94 static QStringList createValueLabels(qreal max, qreal min, int ticks, const QString &format);
94 static QStringList createValueLabels(qreal max, qreal min, int ticks, const QString &format);
95 static QStringList createLogValueLabels(qreal min, qreal max, qreal base, int ticks, const QString& format);
95 static QStringList createLogValueLabels(qreal min, qreal max, qreal base, int ticks, const QString& format);
96 static QStringList createDateTimeLabels(qreal max, qreal min, int ticks, const QString &format);
96 static QStringList createDateTimeLabels(qreal max, qreal min, int ticks, const QString &format);
97
97
98 protected:
98 protected:
99 void setGeometry(const QRectF &size) { Q_UNUSED(size);};
99 void setGeometry(const QRectF &size) { Q_UNUSED(size);};
100 virtual void updateGeometry() = 0;
100 virtual void updateGeometry() = 0;
101 virtual QVector<qreal> calculateLayout() const = 0;
101 virtual QVector<qreal> calculateLayout() const = 0;
102
102
103 QList<QGraphicsItem *> lineItems() { return m_grid->childItems(); };
103 QList<QGraphicsItem *> lineItems() { return m_grid->childItems(); };
104 QList<QGraphicsItem *> labelItems() { return m_labels->childItems();};
104 QList<QGraphicsItem *> labelItems() { return m_labels->childItems();};
105 QList<QGraphicsItem *> shadeItems() { return m_shades->childItems();};
105 QList<QGraphicsItem *> shadeItems() { return m_shades->childItems();};
106 QList<QGraphicsItem *> arrowItems() { return m_arrow->childItems();};
106 QList<QGraphicsItem *> arrowItems() { return m_arrow->childItems();};
107 QGraphicsSimpleTextItem* titleItem() const { return m_title.data();}
107 QGraphicsSimpleTextItem* titleItem() const { return m_title.data();}
108
108
109 QFont font() const;
109 QFont font() const;
110 qreal min() const;
110 qreal min() const;
111 qreal max() const;
111 qreal max() const;
112 QRect labelBoundingRect(const QFontMetrics &fn, const QString &label) const;
112
113
113 //handlers
114 //handlers
114 public Q_SLOTS:
115 public Q_SLOTS:
115 void handleVisibleChanged(bool visible);
116 void handleVisibleChanged(bool visible);
116 void handleArrowVisibleChanged(bool visible);
117 void handleArrowVisibleChanged(bool visible);
117 void handleGridVisibleChanged(bool visible);
118 void handleGridVisibleChanged(bool visible);
118 void handleLabelsVisibleChanged(bool visible);
119 void handleLabelsVisibleChanged(bool visible);
119 void handleShadesVisibleChanged(bool visible);
120 void handleShadesVisibleChanged(bool visible);
120 void handleLabelsAngleChanged(int angle);
121 void handleLabelsAngleChanged(int angle);
121 void handleShadesBrushChanged(const QBrush &brush);
122 void handleShadesBrushChanged(const QBrush &brush);
122 void handleShadesPenChanged(const QPen &pen);
123 void handleShadesPenChanged(const QPen &pen);
123 void handleArrowPenChanged(const QPen &pen);
124 void handleArrowPenChanged(const QPen &pen);
124 void handleGridPenChanged(const QPen &pen);
125 void handleGridPenChanged(const QPen &pen);
125 void handleLabelsPenChanged(const QPen &pen);
126 void handleLabelsPenChanged(const QPen &pen);
126 void handleLabelsBrushChanged(const QBrush &brush);
127 void handleLabelsBrushChanged(const QBrush &brush);
127 void handleLabelsFontChanged(const QFont &font);
128 void handleLabelsFontChanged(const QFont &font);
128 void handleTitlePenChanged(const QPen &pen);
129 void handleTitlePenChanged(const QPen &pen);
129 void handleTitleBrushChanged(const QBrush &brush);
130 void handleTitleBrushChanged(const QBrush &brush);
130 void handleTitleFontChanged(const QFont &font);
131 void handleTitleFontChanged(const QFont &font);
131 void handleTitleTextChanged(const QString &title);
132 void handleTitleTextChanged(const QString &title);
132 void handleTitleVisibleChanged(bool visible);
133 void handleTitleVisibleChanged(bool visible);
133 void handleRangeChanged(qreal min , qreal max);
134 void handleRangeChanged(qreal min , qreal max);
134
135
135 Q_SIGNALS:
136 Q_SIGNALS:
136 void clicked();
137 void clicked();
137
138
138 private:
139 private:
139 inline bool isEmpty();
140 inline bool isEmpty();
140 void createItems(int count);
141 void createItems(int count);
141 void deleteItems(int count);
142 void deleteItems(int count);
142 void updateLayout(QVector<qreal> &layout);
143 void updateLayout(QVector<qreal> &layout);
143 void axisSelected();
144 void axisSelected();
144 void connectSlots();
145 void connectSlots();
145
146
146 private:
147 private:
147 QAbstractAxis *m_axis;
148 QAbstractAxis *m_axis;
148 int m_labelsAngle;
149 int m_labelsAngle;
149 QRectF m_axisRect;
150 QRectF m_axisRect;
150 QRectF m_gridRect;
151 QRectF m_gridRect;
151 QScopedPointer<QGraphicsItemGroup> m_grid;
152 QScopedPointer<QGraphicsItemGroup> m_grid;
152 QScopedPointer<QGraphicsItemGroup> m_arrow;
153 QScopedPointer<QGraphicsItemGroup> m_arrow;
153 QScopedPointer<QGraphicsItemGroup> m_shades;
154 QScopedPointer<QGraphicsItemGroup> m_shades;
154 QScopedPointer<QGraphicsItemGroup> m_labels;
155 QScopedPointer<QGraphicsItemGroup> m_labels;
155 QScopedPointer<QGraphicsSimpleTextItem> m_title;
156 QScopedPointer<QGraphicsSimpleTextItem> m_title;
156 QVector<qreal> m_layoutVector;
157 QVector<qreal> m_layoutVector;
157 AxisAnimation *m_animation;
158 AxisAnimation *m_animation;
158 int m_labelPadding;
159 int m_labelPadding;
159 QStringList m_labelsList;
160 QStringList m_labelsList;
160 bool m_intervalAxis;
161 bool m_intervalAxis;
161
162
162 friend class AxisAnimation;
163 friend class AxisAnimation;
163 friend class ArrowItem;
164 friend class ArrowItem;
164
165
165 };
166 };
166
167
167 class ArrowItem: public QGraphicsLineItem
168 class ArrowItem: public QGraphicsLineItem
168 {
169 {
169
170
170 public:
171 public:
171 explicit ArrowItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {}
172 explicit ArrowItem(ChartAxis *axis, QGraphicsItem *parent = 0) : QGraphicsLineItem(parent), m_axis(axis) {}
172
173
173 protected:
174 protected:
174 void mousePressEvent(QGraphicsSceneMouseEvent *event) {
175 void mousePressEvent(QGraphicsSceneMouseEvent *event) {
175 Q_UNUSED(event)
176 Q_UNUSED(event)
176 m_axis->axisSelected();
177 m_axis->axisSelected();
177 }
178 }
178
179
179 QRectF boundingRect() const {
180 QRectF boundingRect() const {
180 return shape().boundingRect();
181 return shape().boundingRect();
181 }
182 }
182
183
183 QPainterPath shape() const {
184 QPainterPath shape() const {
184 QPainterPath path = QGraphicsLineItem::shape();
185 QPainterPath path = QGraphicsLineItem::shape();
185 QRectF rect = path.boundingRect();
186 QRectF rect = path.boundingRect();
186 path.addRect(rect.adjusted(0, 0, m_axis->orientation() != Qt::Horizontal ? 8 : 0, m_axis->orientation() != Qt::Vertical ? 8 : 0));
187 path.addRect(rect.adjusted(0, 0, m_axis->orientation() != Qt::Horizontal ? 8 : 0, m_axis->orientation() != Qt::Vertical ? 8 : 0));
187 return path;
188 return path;
188 }
189 }
189
190
190 private:
191 private:
191 ChartAxis *m_axis;
192 ChartAxis *m_axis;
192 };
193 };
193
194
194 QTCOMMERCIALCHART_END_NAMESPACE
195 QTCOMMERCIALCHART_END_NAMESPACE
195
196
196 #endif /* CHARTAXI_H */
197 #endif /* CHARTAXI_H */
@@ -1,128 +1,131
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 "chartdatetimeaxisx_p.h"
21 #include "chartdatetimeaxisx_p.h"
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "qdatetimeaxis.h"
23 #include "qdatetimeaxis.h"
24 #include "chartlayout_p.h"
24 #include "chartlayout_p.h"
25 #include <QGraphicsLayout>
25 #include <QGraphicsLayout>
26 #include <QDateTime>
26 #include <QDateTime>
27 #include <QFontMetrics>
27 #include <QFontMetrics>
28 #include <qmath.h>
28 #include <qmath.h>
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 ChartDateTimeAxisX::ChartDateTimeAxisX(QDateTimeAxis *axis, QGraphicsItem* item)
32 ChartDateTimeAxisX::ChartDateTimeAxisX(QDateTimeAxis *axis, QGraphicsItem* item)
33 : HorizontalAxis(axis, item),
33 : HorizontalAxis(axis, item),
34 m_axis(axis)
34 m_axis(axis)
35 {
35 {
36 QObject::connect(m_axis,SIGNAL(tickCountChanged(int)),this, SLOT(handleTickCountChanged(int)));
36 QObject::connect(m_axis,SIGNAL(tickCountChanged(int)),this, SLOT(handleTickCountChanged(int)));
37 QObject::connect(m_axis,SIGNAL(formatChanged(QString)),this, SLOT(handleFormatChanged(QString)));
37 QObject::connect(m_axis,SIGNAL(formatChanged(QString)),this, SLOT(handleFormatChanged(QString)));
38 }
38 }
39
39
40 ChartDateTimeAxisX::~ChartDateTimeAxisX()
40 ChartDateTimeAxisX::~ChartDateTimeAxisX()
41 {
41 {
42 }
42 }
43
43
44 QVector<qreal> ChartDateTimeAxisX::calculateLayout() const
44 QVector<qreal> ChartDateTimeAxisX::calculateLayout() const
45 {
45 {
46 int tickCount = m_axis->tickCount();
46 int tickCount = m_axis->tickCount();
47
47
48 Q_ASSERT(tickCount >= 2);
48 Q_ASSERT(tickCount >= 2);
49
49
50 QVector<qreal> points;
50 QVector<qreal> points;
51 points.resize(tickCount);
51 points.resize(tickCount);
52 const QRectF &gridRect = gridGeometry();
52 const QRectF &gridRect = gridGeometry();
53 const qreal deltaX = gridRect.width() / (tickCount - 1);
53 const qreal deltaX = gridRect.width() / (tickCount - 1);
54 for (int i = 0; i < tickCount; ++i) {
54 for (int i = 0; i < tickCount; ++i) {
55 int x = i * deltaX + gridRect.left();
55 int x = i * deltaX + gridRect.left();
56 points[i] = x;
56 points[i] = x;
57 }
57 }
58 return points;
58 return points;
59 }
59 }
60
60
61 void ChartDateTimeAxisX::updateGeometry()
61 void ChartDateTimeAxisX::updateGeometry()
62 {
62 {
63 const QVector<qreal>& layout = ChartAxis::layout();
63 const QVector<qreal>& layout = ChartAxis::layout();
64 if (layout.isEmpty())
64 if (layout.isEmpty())
65 return;
65 return;
66 setLabels(createDateTimeLabels(min(),max(), layout.size(),m_axis->format()));
66 setLabels(createDateTimeLabels(min(),max(), layout.size(),m_axis->format()));
67 HorizontalAxis::updateGeometry();
67 HorizontalAxis::updateGeometry();
68 }
68 }
69
69
70 void ChartDateTimeAxisX::handleTickCountChanged(int tick)
70 void ChartDateTimeAxisX::handleTickCountChanged(int tick)
71 {
71 {
72 Q_UNUSED(tick)
72 Q_UNUSED(tick)
73 QGraphicsLayoutItem::updateGeometry();
73 QGraphicsLayoutItem::updateGeometry();
74 if(presenter()) presenter()->layout()->invalidate();
74 if(presenter()) presenter()->layout()->invalidate();
75 }
75 }
76
76
77 void ChartDateTimeAxisX::handleFormatChanged(const QString &format)
77 void ChartDateTimeAxisX::handleFormatChanged(const QString &format)
78 {
78 {
79 Q_UNUSED(format);
79 Q_UNUSED(format);
80 QGraphicsLayoutItem::updateGeometry();
80 QGraphicsLayoutItem::updateGeometry();
81 if(presenter()) presenter()->layout()->invalidate();
81 if(presenter()) presenter()->layout()->invalidate();
82 }
82 }
83
83
84 QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
84 QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
85 {
85 {
86 Q_UNUSED(constraint)
86 Q_UNUSED(constraint)
87
87
88 QFontMetrics fn(font());
88 QFontMetrics fn(font());
89 QSizeF sh;
89 QSizeF sh;
90
90
91 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
91 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
92 QStringList ticksList = createDateTimeLabels(min(),max(),m_axis->tickCount(),m_axis->format());
92 QStringList ticksList = createDateTimeLabels(min(),max(),m_axis->tickCount(),m_axis->format());
93 qreal width = 0;
93 qreal width = 0;
94 qreal height = 0;
94 qreal height = 0;
95
95
96 if(ticksList.empty()){
96 if(ticksList.empty()){
97 return sh;
97 return sh;
98 }
98 }
99
99
100 switch (which) {
100 switch (which) {
101 case Qt::MinimumSize:{
101 case Qt::MinimumSize:{
102 int count = qMax(ticksList.last().count(),ticksList.first().count());
102 QRectF boundingRect = labelBoundingRect(fn, "...");
103 width = fn.averageCharWidth() * count;
103 width = qMax(boundingRect.width(), base.width());
104 height = fn.height() + labelPadding();
104 height = boundingRect.height() + labelPadding();
105 width = qMax(width,base.width());
106 height += base.height();
105 height += base.height();
107 sh = QSizeF(width,height);
106 sh = QSizeF(width, height);
108 break;
107 break;
109 }
108 }
110 case Qt::PreferredSize: {
109 case Qt::PreferredSize: {
111 int count = qMax(ticksList.last().count(),ticksList.first().count());
110 int labelHeight = 0;
112 width=fn.averageCharWidth() * count;
111 foreach (const QString& s, ticksList) {
113 height=fn.height()+labelPadding();
112 QRect rect = labelBoundingRect(fn, s);
114 width=qMax(width,base.width());
113 labelHeight = qMax(rect.height(), labelHeight);
115 height+=base.height();
114 width += rect.width();
116 sh = QSizeF(width,height);
115 }
116 height = labelHeight + labelPadding();
117 height += base.height();
118 width = qMax(width, base.width());
119 sh = QSizeF(width, height);
117 break;
120 break;
118 }
121 }
119 default:
122 default:
120 break;
123 break;
121 }
124 }
122
125
123 return sh;
126 return sh;
124 }
127 }
125
128
126 #include "moc_chartdatetimeaxisx_p.cpp"
129 #include "moc_chartdatetimeaxisx_p.cpp"
127
130
128 QTCOMMERCIALCHART_END_NAMESPACE
131 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,135 +1,133
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 "chartdatetimeaxisy_p.h"
21 #include "chartdatetimeaxisy_p.h"
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "qdatetimeaxis.h"
23 #include "qdatetimeaxis.h"
24 #include "chartlayout_p.h"
24 #include "chartlayout_p.h"
25 #include <QGraphicsLayout>
25 #include <QGraphicsLayout>
26 #include <QFontMetrics>
26 #include <QFontMetrics>
27 #include <QDateTime>
27 #include <QDateTime>
28 #include <qmath.h>
28 #include <qmath.h>
29
29
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31
31
32 ChartDateTimeAxisY::ChartDateTimeAxisY(QDateTimeAxis *axis, QGraphicsItem* item)
32 ChartDateTimeAxisY::ChartDateTimeAxisY(QDateTimeAxis *axis, QGraphicsItem* item)
33 : VerticalAxis(axis, item),
33 : VerticalAxis(axis, item),
34 m_axis(axis)
34 m_axis(axis)
35 {
35 {
36 QObject::connect(m_axis,SIGNAL(tickCountChanged(int)),this, SLOT(handleTickCountChanged(int)));
36 QObject::connect(m_axis,SIGNAL(tickCountChanged(int)),this, SLOT(handleTickCountChanged(int)));
37 QObject::connect(m_axis,SIGNAL(formatChanged(QString)),this, SLOT(handleFormatChanged(QString)));
37 QObject::connect(m_axis,SIGNAL(formatChanged(QString)),this, SLOT(handleFormatChanged(QString)));
38 }
38 }
39
39
40 ChartDateTimeAxisY::~ChartDateTimeAxisY()
40 ChartDateTimeAxisY::~ChartDateTimeAxisY()
41 {
41 {
42 }
42 }
43
43
44 QVector<qreal> ChartDateTimeAxisY::calculateLayout() const
44 QVector<qreal> ChartDateTimeAxisY::calculateLayout() const
45 {
45 {
46 int tickCount = m_axis->tickCount();
46 int tickCount = m_axis->tickCount();
47
47
48 Q_ASSERT(tickCount >= 2);
48 Q_ASSERT(tickCount >= 2);
49
49
50 QVector<qreal> points;
50 QVector<qreal> points;
51 points.resize(tickCount);
51 points.resize(tickCount);
52 const QRectF &gridRect = gridGeometry();
52 const QRectF &gridRect = gridGeometry();
53 const qreal deltaY = gridRect.height() / (tickCount - 1);
53 const qreal deltaY = gridRect.height() / (tickCount - 1);
54 for (int i = 0; i < tickCount; ++i) {
54 for (int i = 0; i < 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 ChartDateTimeAxisY::updateGeometry()
62 void ChartDateTimeAxisY::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(createDateTimeLabels(min(),max(), layout.size(),m_axis->format()));
67 setLabels(createDateTimeLabels(min(),max(), layout.size(),m_axis->format()));
68 VerticalAxis::updateGeometry();
68 VerticalAxis::updateGeometry();
69 }
69 }
70
70
71 void ChartDateTimeAxisY::handleTickCountChanged(int tick)
71 void ChartDateTimeAxisY::handleTickCountChanged(int tick)
72 {
72 {
73 Q_UNUSED(tick)
73 Q_UNUSED(tick)
74 QGraphicsLayoutItem::updateGeometry();
74 QGraphicsLayoutItem::updateGeometry();
75 if(presenter()) presenter()->layout()->invalidate();
75 if(presenter()) presenter()->layout()->invalidate();
76 }
76 }
77
77
78 void ChartDateTimeAxisY::handleFormatChanged(const QString &format)
78 void ChartDateTimeAxisY::handleFormatChanged(const QString &format)
79 {
79 {
80 Q_UNUSED(format);
80 Q_UNUSED(format);
81 QGraphicsLayoutItem::updateGeometry();
81 QGraphicsLayoutItem::updateGeometry();
82 if(presenter()) presenter()->layout()->invalidate();
82 if(presenter()) presenter()->layout()->invalidate();
83 }
83 }
84
84
85 QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
85 QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
86 {
86 {
87 Q_UNUSED(constraint)
87 Q_UNUSED(constraint)
88
88
89 QFontMetrics fn(font());
89 QFontMetrics fn(font());
90 QSizeF sh;
90 QSizeF sh;
91
91
92 QSizeF base = VerticalAxis::sizeHint(which, constraint);
92 QSizeF base = VerticalAxis::sizeHint(which, constraint);
93 QStringList ticksList = createDateTimeLabels(min(),max(),m_axis->tickCount(),m_axis->format());
93 QStringList ticksList = createDateTimeLabels(min(),max(),m_axis->tickCount(),m_axis->format());
94 qreal width = 0;
94 qreal width = 0;
95 qreal height = 0;
95 qreal height = 0;
96
96
97
97
98 if(ticksList.empty()){
98 if(ticksList.empty()){
99 return sh;
99 return sh;
100 }
100 }
101
101
102 int labelWidth = 0;
103
104 foreach(const QString& s, ticksList)
105 {
106 labelWidth=qMax(fn.width(s),labelWidth);
107 }
108
109 switch (which) {
102 switch (which) {
110 case Qt::MinimumSize: {
103 case Qt::MinimumSize: {
111 width = fn.boundingRect("...").width() + labelPadding();
104 QRectF boundingRect = labelBoundingRect(fn, "...");
105 width = boundingRect.width() + labelPadding();
112 width += base.width();
106 width += base.width();
113 height = fn.height();
107 height = qMax(boundingRect.height(), base.height());
114 height = qMax(height,base.height());
108 sh = QSizeF(width, height);
115 sh = QSizeF(width,height);
116 break;
109 break;
117 }
110 }
118 case Qt::PreferredSize: {
111 case Qt::PreferredSize: {
112 int labelWidth = 0;
113 foreach (const QString& s, ticksList) {
114 QRect rect = labelBoundingRect(fn, s);
115 labelWidth = qMax(rect.width(), labelWidth);
116 height += rect.height();
117 }
119 width = labelWidth + labelPadding() + 2; //two pixels of tolerance
118 width = labelWidth + labelPadding() + 2; //two pixels of tolerance
120 width += base.width();
119 width += base.width();
121 height = fn.height() * ticksList.count();
120 height = qMax(height, base.height());
122 height = qMax(height,base.height());
121 sh = QSizeF(width, height);
123 sh = QSizeF(width,height);
124 break;
122 break;
125 }
123 }
126 default:
124 default:
127 break;
125 break;
128 }
126 }
129
127
130 return sh;
128 return sh;
131 }
129 }
132
130
133 #include "moc_chartdatetimeaxisy_p.cpp"
131 #include "moc_chartdatetimeaxisy_p.cpp"
134
132
135 QTCOMMERCIALCHART_END_NAMESPACE
133 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,210 +1,207
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, QGraphicsItem* item , bool intervalAxis)
29 HorizontalAxis::HorizontalAxis(QAbstractAxis *axis, QGraphicsItem* item , bool intervalAxis)
30 : ChartAxis(axis, item, intervalAxis)
30 : ChartAxis(axis, item, 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 QGraphicsSimpleTextItem* title = titleItem();
51 QGraphicsSimpleTextItem* title = titleItem();
52
52
53 Q_ASSERT(labels.size() == labelList.size());
53 Q_ASSERT(labels.size() == labelList.size());
54 Q_ASSERT(layout.size() == labelList.size());
54 Q_ASSERT(layout.size() == labelList.size());
55
55
56 const QRectF &axisRect = axisGeometry();
56 const QRectF &axisRect = axisGeometry();
57 const QRectF &gridRect = gridGeometry();
57 const QRectF &gridRect = gridGeometry();
58
58
59 //arrow
59 //arrow
60 QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem *>(axis.at(0));
60 QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem *>(axis.at(0));
61
61
62 if (alignment() == Qt::AlignTop)
62 if (alignment() == Qt::AlignTop)
63 arrowItem->setLine(gridRect.left(), axisRect.bottom(), gridRect.right(), axisRect.bottom());
63 arrowItem->setLine(gridRect.left(), axisRect.bottom(), gridRect.right(), axisRect.bottom());
64 else if (alignment() == Qt::AlignBottom)
64 else if (alignment() == Qt::AlignBottom)
65 arrowItem->setLine(gridRect.left(), axisRect.top(), gridRect.right(), axisRect.top());
65 arrowItem->setLine(gridRect.left(), axisRect.top(), gridRect.right(), axisRect.top());
66
66
67 qreal width = 0;
67 qreal width = 0;
68 QFontMetrics fn(font());
68 QFontMetrics fn(font());
69
69
70 //title
70 //title
71
71
72 if (!titleText().isNull()) {
72 if (!titleText().isNull()) {
73 QFontMetrics fn(title->font());
73 QFontMetrics fn(title->font());
74
74
75 int size(0);
75 int size(0);
76
76
77 size = gridRect.width();
77 size = gridRect.width();
78 QString titleText = this->titleText();
78 QString titleText = this->titleText();
79
79
80 if (fn.boundingRect(titleText).width() > size) {
80 if (fn.boundingRect(titleText).width() > size) {
81 QString string = titleText + "...";
81 QString string = titleText + "...";
82 while (fn.boundingRect(string).width() > size && string.length() > 3)
82 while (fn.boundingRect(string).width() > size && string.length() > 3)
83 string.remove(string.length() - 4, 1);
83 string.remove(string.length() - 4, 1);
84 title->setText(string);
84 title->setText(string);
85 } else {
85 } else {
86 title->setText(titleText);
86 title->setText(titleText);
87 }
87 }
88
88
89 QPointF center = gridRect.center() - title->boundingRect().center();
89 QPointF center = gridRect.center() - title->boundingRect().center();
90 if (alignment() == Qt::AlignTop) {
90 if (alignment() == Qt::AlignTop) {
91 title->setPos(center.x(), axisRect.top());
91 title->setPos(center.x(), axisRect.top());
92 } else if (alignment() == Qt::AlignBottom) {
92 } else if (alignment() == Qt::AlignBottom) {
93 title->setPos(center.x(), axisRect.bottom() - title->boundingRect().height());
93 title->setPos(center.x(), axisRect.bottom() - title->boundingRect().height());
94 }
94 }
95 }
95 }
96
96
97 for (int i = 0; i < layout.size(); ++i) {
97 for (int i = 0; i < layout.size(); ++i) {
98
98
99 //items
99 //items
100 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i));
100 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem*>(lines.at(i));
101 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(axis.at(i + 1));
101 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem*>(axis.at(i + 1));
102 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labels.at(i));
102 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labels.at(i));
103
103
104 //grid line
104 //grid line
105 gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom());
105 gridItem->setLine(layout[i], gridRect.top(), layout[i], gridRect.bottom());
106
106
107 //label text wrapping
107 //label text wrapping
108 if(intervalAxis()&& i+1!=layout.size()) {
108 QString text = labelList.at(i);
109 //wrapping in case of interval axis
109 QRectF boundingRect = labelBoundingRect(fn, text);
110 const qreal delta = layout[i+1] - layout[i];
110 qreal size = axisRect.bottom() - axisRect.top() - labelPadding() - title->boundingRect().height();
111 QString text = labelList.at(i);
111 if (boundingRect.height() > size) {
112 if (fn.boundingRect(text).width() + 1 > delta )
112 QString label = text + "...";
113 {
113 while (boundingRect.height() >= size && label.length() > 3) {
114 QString label = text + "...";
115 while (fn.boundingRect(label).width() >= delta && label.length() > 3)
116 label.remove(label.length() - 4, 1);
114 label.remove(label.length() - 4, 1);
117 labelItem->setText(label);
115 boundingRect = labelBoundingRect(fn, label);
118 }
119 else {
120 labelItem->setText(text);
121 }
116 }
122 }else{
117 labelItem->setText(label);
123 labelItem->setText(labelList.at(i));
118 } else {
119 labelItem->setText(text);
124 }
120 }
125
121
126 //label transformation origin point
122 //label transformation origin point
127 const QRectF& rect = labelItem->boundingRect();
123 const QRectF& rect = labelItem->boundingRect();
128 QPointF center = rect.center();
124 QPointF center = rect.center();
129 labelItem->setTransformOriginPoint(center.x(), center.y());
125 labelItem->setTransformOriginPoint(center.x(), center.y());
126 int heightDiff = rect.height() - boundingRect.height();
130
127
131 //ticks and label position
128 //ticks and label position
132 if (alignment() == Qt::AlignTop) {
129 if (alignment() == Qt::AlignTop) {
133 labelItem->setPos(layout[i] - center.x(), axisRect.bottom() - rect.height() - labelPadding());
130 labelItem->setPos(layout[i] - center.x(), axisRect.bottom() - rect.height() + (heightDiff / 2) - labelPadding());
134 tickItem->setLine(layout[i], axisRect.bottom(), layout[i], axisRect.bottom() - labelPadding());
131 tickItem->setLine(layout[i], axisRect.bottom(), layout[i], axisRect.bottom() - labelPadding());
135 } else if (alignment() == Qt::AlignBottom) {
132 } else if (alignment() == Qt::AlignBottom) {
136 labelItem->setPos(layout[i] - center.x(), axisRect.top() + labelPadding());
133 labelItem->setPos(layout[i] - center.x(), axisRect.top() - (heightDiff / 2) + labelPadding());
137 tickItem->setLine(layout[i], axisRect.top(), layout[i], axisRect.top() + labelPadding());
134 tickItem->setLine(layout[i], axisRect.top(), layout[i], axisRect.top() + labelPadding());
138 }
135 }
139
136
140 //label in beetwen
137 //label in beetwen
141 if(intervalAxis()&& i+1!=layout.size()) {
138 if(intervalAxis()&& i+1!=layout.size()) {
142 const qreal delta = (layout[i+1] - layout[i])/2;
139 const qreal delta = (layout[i+1] - layout[i])/2;
143 labelItem->setPos(layout[i] + delta - center.x(), labelItem->pos().y());
140 labelItem->setPos(layout[i] + delta - center.x(), labelItem->pos().y());
144 }
141 }
145
142
146 //label overlap detection
143 //label overlap detection
147 if(labelItem->pos().x() < width ||
144 if(labelItem->pos().x() < width ||
148 labelItem->pos().x() < axisRect.left() ||
145 labelItem->pos().x() < axisRect.left() ||
149 labelItem->pos().x() + rect.width() -1 > axisRect.right()){
146 labelItem->pos().x() + boundingRect.width() -1 > axisRect.right()){
150 labelItem->setVisible(false);
147 labelItem->setVisible(false);
151 } else {
148 } else {
152 labelItem->setVisible(true);
149 labelItem->setVisible(true);
153 width=rect.width()+labelItem->pos().x();
150 width = boundingRect.width() + labelItem->pos().x();
154 }
151 }
155
152
156 //shades
153 //shades
157 if ((i + 1) % 2 && i > 1) {
154 if ((i + 1) % 2 && i > 1) {
158 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1));
155 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1));
159 rectItem->setRect(layout[i - 1], gridRect.top(), layout[i] - layout[i - 1], gridRect.height());
156 rectItem->setRect(layout[i - 1], gridRect.top(), layout[i] - layout[i - 1], gridRect.height());
160 }
157 }
161
158
162 // check if the grid line and the axis tick should be shown
159 // check if the grid line and the axis tick should be shown
163 qreal x = gridItem->line().p1().x();
160 qreal x = gridItem->line().p1().x();
164 if (x < gridRect.left() || x > gridRect.right()) {
161 if (x < gridRect.left() || x > gridRect.right()) {
165 gridItem->setVisible(false);
162 gridItem->setVisible(false);
166 tickItem->setVisible(false);
163 tickItem->setVisible(false);
167 }else{
164 }else{
168 gridItem->setVisible(true);
165 gridItem->setVisible(true);
169 tickItem->setVisible(true);
166 tickItem->setVisible(true);
170 }
167 }
171
168
172 }
169 }
173
170
174 //begin/end grid line in case labels between
171 //begin/end grid line in case labels between
175 if (intervalAxis()) {
172 if (intervalAxis()) {
176 QGraphicsLineItem *gridLine;
173 QGraphicsLineItem *gridLine;
177 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size()));
174 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size()));
178 gridLine->setLine(gridRect.right(), gridRect.top(), gridRect.right(), gridRect.bottom());
175 gridLine->setLine(gridRect.right(), gridRect.top(), gridRect.right(), gridRect.bottom());
179 gridLine->setVisible(true);
176 gridLine->setVisible(true);
180 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
177 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
181 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.left(), gridRect.bottom());
178 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.left(), gridRect.bottom());
182 gridLine->setVisible(true);
179 gridLine->setVisible(true);
183 }
180 }
184 }
181 }
185
182
186 QSizeF HorizontalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
183 QSizeF HorizontalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
187 {
184 {
188 Q_UNUSED(constraint);
185 Q_UNUSED(constraint);
189 QFontMetrics fn(titleFont());
186 QFontMetrics fn(titleFont());
190 QSizeF sh(0,0);
187 QSizeF sh(0,0);
191
188
192 if (titleText().isNull() || !titleItem()->isVisible())
189 if (titleText().isNull() || !titleItem()->isVisible())
193 return sh;
190 return sh;
194
191
195 switch (which) {
192 switch (which) {
196 case Qt::MinimumSize:
193 case Qt::MinimumSize:
197 sh = QSizeF(fn.boundingRect("...").width(), fn.height());
194 sh = QSizeF(fn.boundingRect("...").width(), fn.height());
198 break;
195 break;
199 case Qt::MaximumSize:
196 case Qt::MaximumSize:
200 case Qt::PreferredSize:
197 case Qt::PreferredSize:
201 sh = QSizeF(fn.boundingRect(axis()->titleText()).width(), fn.height());
198 sh = QSizeF(fn.boundingRect(axis()->titleText()).width(), fn.height());
202 break;
199 break;
203 default:
200 default:
204 break;
201 break;
205 }
202 }
206
203
207 return sh;
204 return sh;
208 }
205 }
209
206
210 QTCOMMERCIALCHART_END_NAMESPACE
207 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,134 +1,137
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 "chartlogvalueaxisx_p.h"
21 #include "chartlogvalueaxisx_p.h"
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "qlogvalueaxis.h"
23 #include "qlogvalueaxis.h"
24 #include "chartlayout_p.h"
24 #include "chartlayout_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 ChartLogValueAxisX::ChartLogValueAxisX(QLogValueAxis *axis, QGraphicsItem* item)
32 ChartLogValueAxisX::ChartLogValueAxisX(QLogValueAxis *axis, QGraphicsItem* item)
33 : HorizontalAxis(axis, item),
33 : HorizontalAxis(axis, item),
34 m_axis(axis)
34 m_axis(axis)
35 {
35 {
36 QObject::connect(m_axis,SIGNAL(baseChanged(qreal)),this, SLOT(handleBaseChanged(qreal)));
36 QObject::connect(m_axis,SIGNAL(baseChanged(qreal)),this, SLOT(handleBaseChanged(qreal)));
37 QObject::connect(m_axis,SIGNAL(labelFormatChanged(QString)),this, SLOT(handleLabelFormatChanged(QString)));
37 QObject::connect(m_axis,SIGNAL(labelFormatChanged(QString)),this, SLOT(handleLabelFormatChanged(QString)));
38 }
38 }
39
39
40 ChartLogValueAxisX::~ChartLogValueAxisX()
40 ChartLogValueAxisX::~ChartLogValueAxisX()
41 {
41 {
42 }
42 }
43
43
44 QVector<qreal> ChartLogValueAxisX::calculateLayout() const
44 QVector<qreal> ChartLogValueAxisX::calculateLayout() const
45 {
45 {
46 QVector<qreal> points;
46 QVector<qreal> points;
47
47
48 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
48 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
49 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
49 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
50 qreal leftEdge = logMin < logMax ? logMin : logMax;
50 qreal leftEdge = logMin < logMax ? logMin : logMax;
51 qreal ceilEdge = ceil(leftEdge);
51 qreal ceilEdge = ceil(leftEdge);
52 int tickCount = qAbs(ceil(logMax) - ceil(logMin));
52 int tickCount = qAbs(ceil(logMax) - ceil(logMin));
53
53
54 points.resize(tickCount);
54 points.resize(tickCount);
55 const QRectF &gridRect = gridGeometry();
55 const QRectF &gridRect = gridGeometry();
56 const qreal deltaX = gridRect.width() / qAbs(logMax - logMin);
56 const qreal deltaX = gridRect.width() / qAbs(logMax - logMin);
57 for (int i = 0; i < tickCount; ++i)
57 for (int i = 0; i < tickCount; ++i)
58 points[i] = (ceilEdge + i) * deltaX - leftEdge * deltaX + gridRect.left();
58 points[i] = (ceilEdge + i) * deltaX - leftEdge * deltaX + gridRect.left();
59
59
60 return points;
60 return points;
61 }
61 }
62
62
63 void ChartLogValueAxisX::updateGeometry()
63 void ChartLogValueAxisX::updateGeometry()
64 {
64 {
65 const QVector<qreal>& layout = ChartAxis::layout();
65 const QVector<qreal>& layout = ChartAxis::layout();
66 if (layout.isEmpty())
66 if (layout.isEmpty())
67 return;
67 return;
68 setLabels(createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), layout.size(), m_axis->labelFormat()));
68 setLabels(createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), layout.size(), m_axis->labelFormat()));
69 HorizontalAxis::updateGeometry();
69 HorizontalAxis::updateGeometry();
70 }
70 }
71
71
72 void ChartLogValueAxisX::handleBaseChanged(qreal base)
72 void ChartLogValueAxisX::handleBaseChanged(qreal base)
73 {
73 {
74 Q_UNUSED(base);
74 Q_UNUSED(base);
75 QGraphicsLayoutItem::updateGeometry();
75 QGraphicsLayoutItem::updateGeometry();
76 if(presenter()) presenter()->layout()->invalidate();
76 if(presenter()) presenter()->layout()->invalidate();
77 }
77 }
78
78
79 void ChartLogValueAxisX::handleLabelFormatChanged(const QString &format)
79 void ChartLogValueAxisX::handleLabelFormatChanged(const QString &format)
80 {
80 {
81 Q_UNUSED(format);
81 Q_UNUSED(format);
82 QGraphicsLayoutItem::updateGeometry();
82 QGraphicsLayoutItem::updateGeometry();
83 if(presenter()) presenter()->layout()->invalidate();
83 if(presenter()) presenter()->layout()->invalidate();
84 }
84 }
85
85
86 QSizeF ChartLogValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
86 QSizeF ChartLogValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
87 {
87 {
88 Q_UNUSED(constraint)
88 Q_UNUSED(constraint)
89
89
90 QFontMetrics fn(font());
90 QFontMetrics fn(font());
91 QSizeF sh;
91 QSizeF sh;
92
92
93 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
93 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
94 QStringList ticksList;
94 QStringList ticksList;
95 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
95 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
96 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
96 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
97 int tickCount = qAbs(ceil(logMax) - ceil(logMin));
97 int tickCount = qAbs(ceil(logMax) - ceil(logMin));
98 if (m_axis->max() > m_axis->min() && tickCount > 1)
98 if (m_axis->max() > m_axis->min() && tickCount > 1)
99 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
99 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
100 else
100 else
101 ticksList.append(QString(" "));
101 ticksList.append(QString(" "));
102 qreal width = 0;
102 qreal width = 0;
103 qreal height = 0;
103 qreal height = 0;
104
104
105
105
106 switch (which) {
106 switch (which) {
107 case Qt::MinimumSize:{
107 case Qt::MinimumSize:{
108 int count = qMax(ticksList.last().count(),ticksList.first().count());
108 QRectF boundingRect = labelBoundingRect(fn, "...");
109 width = fn.averageCharWidth() * count;
109 width = qMax(boundingRect.width(), base.width());
110 height = fn.height() + labelPadding();
110 height = boundingRect.height() + labelPadding();
111 width = qMax(width,base.width());
112 height += base.height();
111 height += base.height();
113 sh = QSizeF(width,height);
112 sh = QSizeF(width, height);
114 break;
113 break;
115 }
114 }
116 case Qt::PreferredSize: {
115 case Qt::PreferredSize: {
117 int count = qMax(ticksList.last().count(),ticksList.first().count());
116 int labelHeight = 0;
118 width=fn.averageCharWidth() * count;
117 foreach (const QString& s, ticksList) {
119 height=fn.height()+labelPadding();
118 QRect rect = labelBoundingRect(fn, s);
120 width=qMax(width,base.width());
119 labelHeight = qMax(rect.height(), labelHeight);
121 height+=base.height();
120 width += rect.width();
122 sh = QSizeF(width,height);
121 }
122 height = labelHeight + labelPadding();
123 height += base.height();
124 width = qMax(width, base.width());
125 sh = QSizeF(width, height);
123 break;
126 break;
124 }
127 }
125 default:
128 default:
126 break;
129 break;
127 }
130 }
128
131
129 return sh;
132 return sh;
130 }
133 }
131
134
132 #include "moc_chartlogvalueaxisx_p.cpp"
135 #include "moc_chartlogvalueaxisx_p.cpp"
133
136
134 QTCOMMERCIALCHART_END_NAMESPACE
137 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,139 +1,136
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartlogvalueaxisy_p.h"
21 #include "chartlogvalueaxisy_p.h"
22 #include "chartpresenter_p.h"
22 #include "chartpresenter_p.h"
23 #include "qlogvalueaxis.h"
23 #include "qlogvalueaxis.h"
24 #include "chartlayout_p.h"
24 #include "chartlayout_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 ChartLogValueAxisY::ChartLogValueAxisY(QLogValueAxis *axis, QGraphicsItem* item)
32 ChartLogValueAxisY::ChartLogValueAxisY(QLogValueAxis *axis, QGraphicsItem* item)
33 : VerticalAxis(axis, item),
33 : VerticalAxis(axis, item),
34 m_axis(axis)
34 m_axis(axis)
35 {
35 {
36 QObject::connect(m_axis, SIGNAL(baseChanged(qreal)),this, SLOT(handleBaseChanged(qreal)));
36 QObject::connect(m_axis, SIGNAL(baseChanged(qreal)),this, SLOT(handleBaseChanged(qreal)));
37 QObject::connect(m_axis,SIGNAL(labelFormatChanged(QString)),this, SLOT(handleLabelFormatChanged(QString)));
37 QObject::connect(m_axis,SIGNAL(labelFormatChanged(QString)),this, SLOT(handleLabelFormatChanged(QString)));
38 }
38 }
39
39
40 ChartLogValueAxisY::~ChartLogValueAxisY()
40 ChartLogValueAxisY::~ChartLogValueAxisY()
41 {
41 {
42 }
42 }
43
43
44 QVector<qreal> ChartLogValueAxisY::calculateLayout() const
44 QVector<qreal> ChartLogValueAxisY::calculateLayout() const
45 {
45 {
46 QVector<qreal> points;
46 QVector<qreal> points;
47 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
47 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
48 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
48 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
49 qreal leftEdge = logMin < logMax ? logMin : logMax;
49 qreal leftEdge = logMin < logMax ? logMin : logMax;
50 qreal ceilEdge = ceil(leftEdge);
50 qreal ceilEdge = ceil(leftEdge);
51 int tickCount = qAbs(ceil(logMax) - ceil(logMin));
51 int tickCount = qAbs(ceil(logMax) - ceil(logMin));
52
52
53 points.resize(tickCount);
53 points.resize(tickCount);
54 const QRectF &gridRect = gridGeometry();
54 const QRectF &gridRect = gridGeometry();
55 const qreal deltaY = gridRect.height() / qAbs(logMax - logMin);
55 const qreal deltaY = gridRect.height() / qAbs(logMax - logMin);
56 for (int i = 0; i < tickCount; ++i)
56 for (int i = 0; i < tickCount; ++i)
57 points[i] = (ceilEdge + i) * -deltaY - leftEdge * -deltaY + gridRect.bottom();
57 points[i] = (ceilEdge + i) * -deltaY - leftEdge * -deltaY + gridRect.bottom();
58
58
59 return points;
59 return points;
60 }
60 }
61
61
62
62
63 void ChartLogValueAxisY::updateGeometry()
63 void ChartLogValueAxisY::updateGeometry()
64 {
64 {
65 const QVector<qreal> &layout = ChartAxis::layout();
65 const QVector<qreal> &layout = ChartAxis::layout();
66 if (layout.isEmpty())
66 if (layout.isEmpty())
67 return;
67 return;
68 setLabels(createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), layout.size(), m_axis->labelFormat()));
68 setLabels(createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), layout.size(), m_axis->labelFormat()));
69 VerticalAxis::updateGeometry();
69 VerticalAxis::updateGeometry();
70 }
70 }
71
71
72 void ChartLogValueAxisY::handleBaseChanged(qreal base)
72 void ChartLogValueAxisY::handleBaseChanged(qreal base)
73 {
73 {
74 Q_UNUSED(base);
74 Q_UNUSED(base);
75 QGraphicsLayoutItem::updateGeometry();
75 QGraphicsLayoutItem::updateGeometry();
76 if(presenter()) presenter()->layout()->invalidate();
76 if(presenter()) presenter()->layout()->invalidate();
77 }
77 }
78
78
79 void ChartLogValueAxisY::handleLabelFormatChanged(const QString &format)
79 void ChartLogValueAxisY::handleLabelFormatChanged(const QString &format)
80 {
80 {
81 Q_UNUSED(format);
81 Q_UNUSED(format);
82 QGraphicsLayoutItem::updateGeometry();
82 QGraphicsLayoutItem::updateGeometry();
83 if(presenter()) presenter()->layout()->invalidate();
83 if(presenter()) presenter()->layout()->invalidate();
84 }
84 }
85
85
86 QSizeF ChartLogValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
86 QSizeF ChartLogValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
87 {
87 {
88 Q_UNUSED(constraint)
88 Q_UNUSED(constraint)
89
89
90 QFontMetrics fn(font());
90 QFontMetrics fn(font());
91 QSizeF sh;
91 QSizeF sh;
92
92
93 QSizeF base = VerticalAxis::sizeHint(which, constraint);
93 QSizeF base = VerticalAxis::sizeHint(which, constraint);
94 QStringList ticksList;
94 QStringList ticksList;
95 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
95 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
96 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
96 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
97 int tickCount = qAbs(ceil(logMax) - ceil(logMin));
97 int tickCount = qAbs(ceil(logMax) - ceil(logMin));
98 if (m_axis->max() > m_axis->min() && tickCount > 1)
98 if (m_axis->max() > m_axis->min() && tickCount > 1)
99 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
99 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
100 else
100 else
101 ticksList.append(QString(" "));
101 ticksList.append(QString(" "));
102 qreal width = 0;
102 qreal width = 0;
103 qreal height = 0;
103 qreal height = 0;
104
104
105
106 int labelWidth = 0;
107
108 foreach(const QString& s, ticksList)
109 {
110 labelWidth=qMax(fn.width(s),labelWidth);
111 }
112
113 switch (which) {
105 switch (which) {
114 case Qt::MinimumSize: {
106 case Qt::MinimumSize: {
115 width = fn.boundingRect("...").width() + labelPadding();
107 QRectF boundingRect = labelBoundingRect(fn, "...");
108 width = boundingRect.width() + labelPadding();
116 width += base.width();
109 width += base.width();
117 height = fn.height();
110 height = qMax(boundingRect.height(), base.height());
118 height = qMax(height,base.height());
111 sh = QSizeF(width, height);
119 sh = QSizeF(width,height);
120 break;
112 break;
121 }
113 }
122 case Qt::PreferredSize: {
114 case Qt::PreferredSize: {
115 int labelWidth = 0;
116 foreach (const QString& s, ticksList) {
117 QRect rect = labelBoundingRect(fn, s);
118 labelWidth = qMax(rect.width(), labelWidth);
119 height += rect.height();
120 }
123 width = labelWidth + labelPadding() + 2; //two pixels of tolerance
121 width = labelWidth + labelPadding() + 2; //two pixels of tolerance
124 width += base.width();
122 width += base.width();
125 height = fn.height() * ticksList.count();
123 height = qMax(height, base.height());
126 height = qMax(height,base.height());
124 sh = QSizeF(width, height);
127 sh = QSizeF(width,height);
128 break;
125 break;
129 }
126 }
130 default:
127 default:
131 break;
128 break;
132 }
129 }
133
130
134 return sh;
131 return sh;
135 }
132 }
136
133
137 #include "moc_chartlogvalueaxisy_p.cpp"
134 #include "moc_chartlogvalueaxisy_p.cpp"
138
135
139 QTCOMMERCIALCHART_END_NAMESPACE
136 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,133 +1,129
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 #include <QDebug>
29 #include <QDebug>
30
30
31
31
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33
33
34 ChartValueAxisX::ChartValueAxisX(QValueAxis *axis, QGraphicsItem* item )
34 ChartValueAxisX::ChartValueAxisX(QValueAxis *axis, QGraphicsItem* item )
35 : HorizontalAxis(axis, item),
35 : HorizontalAxis(axis, item),
36 m_axis(axis)
36 m_axis(axis)
37 {
37 {
38 QObject::connect(m_axis,SIGNAL(tickCountChanged(int)),this, SLOT(handleTickCountChanged(int)));
38 QObject::connect(m_axis,SIGNAL(tickCountChanged(int)),this, SLOT(handleTickCountChanged(int)));
39 QObject::connect(m_axis,SIGNAL(labelFormatChanged(QString)),this, SLOT(handleLabelFormatChanged(QString)));
39 QObject::connect(m_axis,SIGNAL(labelFormatChanged(QString)),this, SLOT(handleLabelFormatChanged(QString)));
40 }
40 }
41
41
42 ChartValueAxisX::~ChartValueAxisX()
42 ChartValueAxisX::~ChartValueAxisX()
43 {
43 {
44 }
44 }
45
45
46 QVector<qreal> ChartValueAxisX::calculateLayout() const
46 QVector<qreal> ChartValueAxisX::calculateLayout() const
47 {
47 {
48 int tickCount = m_axis->tickCount();
48 int tickCount = m_axis->tickCount();
49
49
50 Q_ASSERT(tickCount >= 2);
50 Q_ASSERT(tickCount >= 2);
51
51
52 QVector<qreal> points;
52 QVector<qreal> points;
53 points.resize(tickCount);
53 points.resize(tickCount);
54
54
55 const QRectF &gridRect = gridGeometry();
55 const QRectF &gridRect = gridGeometry();
56 const qreal deltaX = gridRect.width() / (tickCount - 1);
56 const qreal deltaX = gridRect.width() / (tickCount - 1);
57 for (int i = 0; i < tickCount; ++i) {
57 for (int i = 0; i < tickCount; ++i) {
58 points[i] = i * deltaX + gridRect.left();
58 points[i] = i * deltaX + gridRect.left();
59 }
59 }
60 return points;
60 return points;
61 }
61 }
62
62
63 void ChartValueAxisX::updateGeometry()
63 void ChartValueAxisX::updateGeometry()
64 {
64 {
65 const QVector<qreal>& layout = ChartAxis::layout();
65 const QVector<qreal>& layout = ChartAxis::layout();
66 if (layout.isEmpty())
66 if (layout.isEmpty())
67 return;
67 return;
68 setLabels(createValueLabels(min(),max(),layout.size(),m_axis->labelFormat()));
68 setLabels(createValueLabels(min(),max(),layout.size(),m_axis->labelFormat()));
69 HorizontalAxis::updateGeometry();
69 HorizontalAxis::updateGeometry();
70 }
70 }
71
71
72 void ChartValueAxisX::handleTickCountChanged(int tick)
72 void ChartValueAxisX::handleTickCountChanged(int tick)
73 {
73 {
74 Q_UNUSED(tick);
74 Q_UNUSED(tick);
75 QGraphicsLayoutItem::updateGeometry();
75 QGraphicsLayoutItem::updateGeometry();
76 if(presenter()) presenter()->layout()->invalidate();
76 if(presenter()) presenter()->layout()->invalidate();
77 }
77 }
78
78
79 void ChartValueAxisX::handleLabelFormatChanged(const QString &format)
79 void ChartValueAxisX::handleLabelFormatChanged(const QString &format)
80 {
80 {
81 Q_UNUSED(format);
81 Q_UNUSED(format);
82 QGraphicsLayoutItem::updateGeometry();
82 QGraphicsLayoutItem::updateGeometry();
83 if(presenter()) presenter()->layout()->invalidate();
83 if(presenter()) presenter()->layout()->invalidate();
84 }
84 }
85
85
86 QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
86 QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
87 {
87 {
88 Q_UNUSED(constraint)
88 Q_UNUSED(constraint)
89
89
90 QFontMetrics fn(font());
90 QFontMetrics fn(font());
91 QSizeF sh;
91 QSizeF sh;
92
92
93 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
93 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
94 QStringList ticksList = createValueLabels(min(),max(),m_axis->tickCount(),m_axis->labelFormat());
94 QStringList ticksList = createValueLabels(min(),max(),m_axis->tickCount(),m_axis->labelFormat());
95 qreal width = 0;
95 qreal width = 0;
96 qreal height = 0;
96 qreal height = 0;
97
97
98
98
99 switch (which) {
99 switch (which) {
100 case Qt::MinimumSize: {
100 case Qt::MinimumSize: {
101 if(!ticksList.empty()) {
101 QRectF boundingRect = labelBoundingRect(fn, "...");
102 foreach(QString label,ticksList) {
102 width = qMax(boundingRect.width(), base.width());
103 width = qMax(qreal(fn.boundingRect(label).width()),width);
103 height = boundingRect.height() + labelPadding();
104 }
105 }
106 height = fn.height() + labelPadding();
107 width = qMax(width,base.width());
108 height += base.height();
104 height += base.height();
109 sh = QSizeF(width,height);
105 sh = QSizeF(width, height);
110 break;
106 break;
111 }
107 }
112 case Qt::PreferredSize: {
108 case Qt::PreferredSize: {
113 if(!ticksList.empty()) {
109 int labelHeight = 0;
114 foreach(QString label,ticksList) {
110 foreach (const QString& s, ticksList) {
115 width+=fn.boundingRect(label).width();
111 QRect rect = labelBoundingRect(fn, s);
116 }
112 labelHeight = qMax(rect.height(), labelHeight);
113 width += rect.width();
117 }
114 }
118 height=fn.height()+labelPadding();
115 height = labelHeight + labelPadding();
119 width=qMax(width,base.width());
116 height += base.height();
120 height+=base.height();
117 width = qMax(width, base.width());
121 sh = QSizeF(width,height);
118 sh = QSizeF(width, height);
122 break;
119 break;
123 }
120 }
124 default:
121 default:
125 break;
122 break;
126 }
123 }
127
128 return sh;
124 return sh;
129 }
125 }
130
126
131 #include "moc_chartvalueaxisx_p.cpp"
127 #include "moc_chartvalueaxisx_p.cpp"
132
128
133 QTCOMMERCIALCHART_END_NAMESPACE
129 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,131 +1,128
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, QGraphicsItem* item)
33 ChartValueAxisY::ChartValueAxisY(QValueAxis *axis, QGraphicsItem* item)
34 : VerticalAxis(axis, item),
34 : VerticalAxis(axis, item),
35 m_axis(axis)
35 m_axis(axis)
36 {
36 {
37 QObject::connect(m_axis,SIGNAL(tickCountChanged(int)),this, SLOT(handleTickCountChanged(int)));
37 QObject::connect(m_axis,SIGNAL(tickCountChanged(int)),this, SLOT(handleTickCountChanged(int)));
38 QObject::connect(m_axis,SIGNAL(labelFormatChanged(QString)),this, SLOT(handleLabelFormatChanged(QString)));
38 QObject::connect(m_axis,SIGNAL(labelFormatChanged(QString)),this, SLOT(handleLabelFormatChanged(QString)));
39 }
39 }
40
40
41 ChartValueAxisY::~ChartValueAxisY()
41 ChartValueAxisY::~ChartValueAxisY()
42 {
42 {
43 }
43 }
44
44
45 QVector<qreal> ChartValueAxisY::calculateLayout() const
45 QVector<qreal> ChartValueAxisY::calculateLayout() const
46 {
46 {
47 int tickCount = m_axis->tickCount();
47 int tickCount = m_axis->tickCount();
48
48
49 Q_ASSERT(tickCount >= 2);
49 Q_ASSERT(tickCount >= 2);
50
50
51 QVector<qreal> points;
51 QVector<qreal> points;
52 points.resize(tickCount);
52 points.resize(tickCount);
53
53
54 const QRectF &gridRect = gridGeometry();
54 const QRectF &gridRect = gridGeometry();
55
55
56 const qreal deltaY = gridRect.height() / (tickCount - 1);
56 const qreal deltaY = gridRect.height() / (tickCount - 1);
57 for (int i = 0; i < tickCount; ++i) {
57 for (int i = 0; i < tickCount; ++i) {
58 points[i] = i * -deltaY + gridRect.bottom();
58 points[i] = i * -deltaY + gridRect.bottom();
59 }
59 }
60
60
61 return points;
61 return points;
62 }
62 }
63
63
64 void ChartValueAxisY::updateGeometry()
64 void ChartValueAxisY::updateGeometry()
65 {
65 {
66 const QVector<qreal> &layout = ChartAxis::layout();
66 const QVector<qreal> &layout = ChartAxis::layout();
67 if (layout.isEmpty())
67 if (layout.isEmpty())
68 return;
68 return;
69 setLabels(createValueLabels(min(),max(),layout.size(),m_axis->labelFormat()));
69 setLabels(createValueLabels(min(),max(),layout.size(),m_axis->labelFormat()));
70 VerticalAxis::updateGeometry();
70 VerticalAxis::updateGeometry();
71 }
71 }
72
72
73 void ChartValueAxisY::handleTickCountChanged(int tick)
73 void ChartValueAxisY::handleTickCountChanged(int tick)
74 {
74 {
75 Q_UNUSED(tick);
75 Q_UNUSED(tick);
76 QGraphicsLayoutItem::updateGeometry();
76 QGraphicsLayoutItem::updateGeometry();
77 if(presenter()) presenter()->layout()->invalidate();
77 if(presenter()) presenter()->layout()->invalidate();
78 }
78 }
79
79
80 void ChartValueAxisY::handleLabelFormatChanged(const QString &format)
80 void ChartValueAxisY::handleLabelFormatChanged(const QString &format)
81 {
81 {
82 Q_UNUSED(format);
82 Q_UNUSED(format);
83 QGraphicsLayoutItem::updateGeometry();
83 QGraphicsLayoutItem::updateGeometry();
84 if(presenter()) presenter()->layout()->invalidate();
84 if(presenter()) presenter()->layout()->invalidate();
85 }
85 }
86
86
87 QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
87 QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
88 {
88 {
89 Q_UNUSED(constraint)
89 Q_UNUSED(constraint)
90
90
91 QFontMetrics fn(font());
91 QFontMetrics fn(font());
92 QSizeF sh;
92 QSizeF sh;
93 QSizeF base = VerticalAxis::sizeHint(which, constraint);
93 QSizeF base = VerticalAxis::sizeHint(which, constraint);
94 QStringList ticksList = createValueLabels(min(),max(),m_axis->tickCount(),m_axis->labelFormat());
94 QStringList ticksList = createValueLabels(min(),max(),m_axis->tickCount(),m_axis->labelFormat());
95 qreal width = 0;
95 qreal width = 0;
96 qreal height = 0;
96 qreal height = 0;
97
97
98 int labelWidth = 0;
99
100 foreach(const QString& s, ticksList)
101 {
102 labelWidth=qMax(fn.width(s),labelWidth);
103 }
104
105 switch (which) {
98 switch (which) {
106 case Qt::MinimumSize: {
99 case Qt::MinimumSize: {
107 width = fn.boundingRect("...").width() + labelPadding();
100 QRectF boundingRect = labelBoundingRect(fn, "...");
101 width = boundingRect.width() + labelPadding();
108 width += base.width();
102 width += base.width();
109 height = fn.height();
103 height = qMax(boundingRect.height(), base.height());
110 height = qMax(height,base.height());
104 sh = QSizeF(width, height);
111 sh = QSizeF(width,height);
112 break;
105 break;
113 }
106 }
114 case Qt::PreferredSize:
107 case Qt::PreferredSize: {
115 {
108 int labelWidth = 0;
109 foreach (const QString& s, ticksList) {
110 QRect rect = labelBoundingRect(fn, s);
111 labelWidth = qMax(rect.width(), labelWidth);
112 height += rect.height();
113 }
116 width = labelWidth + labelPadding() + 2; //two pixels of tolerance
114 width = labelWidth + labelPadding() + 2; //two pixels of tolerance
117 width += base.width();
115 width += base.width();
118 height = fn.height() * ticksList.count();
116 height = qMax(height, base.height());
119 height = qMax(height,base.height());
117 sh = QSizeF(width, height);
120 sh = QSizeF(width,height);
121 break;
118 break;
122 }
119 }
123 default:
120 default:
124 break;
121 break;
125 }
122 }
126 return sh;
123 return sh;
127 }
124 }
128
125
129 #include "moc_chartvalueaxisy_p.cpp"
126 #include "moc_chartvalueaxisy_p.cpp"
130
127
131 QTCOMMERCIALCHART_END_NAMESPACE
128 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,212 +1,218
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 #include <QDebug>
24 #include <QDebug>
25
25
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
26 QTCOMMERCIALCHART_BEGIN_NAMESPACE
27
27
28 VerticalAxis::VerticalAxis(QAbstractAxis *axis, QGraphicsItem* item, bool intervalAxis)
28 VerticalAxis::VerticalAxis(QAbstractAxis *axis, QGraphicsItem* item, bool intervalAxis)
29 : ChartAxis(axis, item, intervalAxis)
29 : ChartAxis(axis, item, intervalAxis)
30 {
30 {
31
31
32 }
32 }
33
33
34 VerticalAxis::~VerticalAxis()
34 VerticalAxis::~VerticalAxis()
35 {
35 {
36
36
37 }
37 }
38
38
39 void VerticalAxis::updateGeometry()
39 void VerticalAxis::updateGeometry()
40 {
40 {
41 const QVector<qreal> &layout = ChartAxis::layout();
41 const QVector<qreal> &layout = ChartAxis::layout();
42
42
43 if (layout.isEmpty())
43 if (layout.isEmpty())
44 return;
44 return;
45
45
46 QStringList labelList = labels();
46 QStringList labelList = labels();
47
47
48 QList<QGraphicsItem *> lines = lineItems();
48 QList<QGraphicsItem *> lines = lineItems();
49 QList<QGraphicsItem *> labels = labelItems();
49 QList<QGraphicsItem *> labels = labelItems();
50 QList<QGraphicsItem *> shades = shadeItems();
50 QList<QGraphicsItem *> shades = shadeItems();
51 QList<QGraphicsItem *> axis = arrowItems();
51 QList<QGraphicsItem *> axis = arrowItems();
52 QGraphicsSimpleTextItem* title = titleItem();
52 QGraphicsSimpleTextItem* title = titleItem();
53
53
54 Q_ASSERT(labels.size() == labelList.size());
54 Q_ASSERT(labels.size() == labelList.size());
55 Q_ASSERT(layout.size() == labelList.size());
55 Q_ASSERT(layout.size() == labelList.size());
56
56
57 const QRectF &axisRect = axisGeometry();
57 const QRectF &axisRect = axisGeometry();
58 const QRectF &gridRect = gridGeometry();
58 const QRectF &gridRect = gridGeometry();
59
59
60 qreal height = axisRect.bottom();
60 qreal height = axisRect.bottom();
61
61
62
62
63 //arrow
63 //arrow
64 QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem*>(axis.at(0));
64 QGraphicsLineItem *arrowItem = static_cast<QGraphicsLineItem*>(axis.at(0));
65
65
66 //arrow position
66 //arrow position
67 if (alignment()==Qt::AlignLeft)
67 if (alignment()==Qt::AlignLeft)
68 arrowItem->setLine( axisRect.right() , gridRect.top(), axisRect.right(), gridRect.bottom());
68 arrowItem->setLine( axisRect.right() , gridRect.top(), axisRect.right(), gridRect.bottom());
69 else if(alignment()==Qt::AlignRight)
69 else if(alignment()==Qt::AlignRight)
70 arrowItem->setLine( axisRect.left() , gridRect.top(), axisRect.left(), gridRect.bottom());
70 arrowItem->setLine( axisRect.left() , gridRect.top(), axisRect.left(), gridRect.bottom());
71
71
72 QFontMetrics fn(font());
72 QFontMetrics fn(font());
73
73
74 //title
74 //title
75
75
76 if (!titleText().isNull()) {
76 if (!titleText().isNull()) {
77 QFontMetrics fn(title->font());
77 QFontMetrics fn(title->font());
78
78
79 int size(0);
79 int size(0);
80 size = gridRect.height();
80 size = gridRect.height();
81 QString titleText = this->titleText();
81 QString titleText = this->titleText();
82
82
83 if (fn.boundingRect(titleText).width() > size) {
83 if (fn.boundingRect(titleText).width() > size) {
84 QString string = titleText + "...";
84 QString string = titleText + "...";
85 while (fn.boundingRect(string).width() > size && string.length() > 3)
85 while (fn.boundingRect(string).width() > size && string.length() > 3)
86 string.remove(string.length() - 4, 1);
86 string.remove(string.length() - 4, 1);
87 title->setText(string);
87 title->setText(string);
88 }
88 }
89 else {
89 else {
90 title->setText(titleText);
90 title->setText(titleText);
91 }
91 }
92
92
93 QPointF center = gridRect.center() - title->boundingRect().center();
93 QPointF center = gridRect.center() - title->boundingRect().center();
94 if (alignment() == Qt::AlignLeft) {
94 if (alignment() == Qt::AlignLeft) {
95 title->setPos(axisRect.left() - title->boundingRect().width()/2 + title->boundingRect().height()/2 , center.y());
95 title->setPos(axisRect.left() - title->boundingRect().width()/2 + title->boundingRect().height()/2 , center.y());
96 }
96 }
97 else if (alignment() == Qt::AlignRight) {
97 else if (alignment() == Qt::AlignRight) {
98 title->setPos(axisRect.right()- title->boundingRect().width()/2 - title->boundingRect().height()/2, center.y());
98 title->setPos(axisRect.right()- title->boundingRect().width()/2 - title->boundingRect().height()/2, center.y());
99 }
99 }
100 title->setTransformOriginPoint(title->boundingRect().center());
100 title->setTransformOriginPoint(title->boundingRect().center());
101 title->setRotation(270);
101 title->setRotation(270);
102 }
102 }
103
103
104 for (int i = 0; i < layout.size(); ++i) {
104 for (int i = 0; i < layout.size(); ++i) {
105
105
106 //items
106 //items
107 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem *>(lines.at(i));
107 QGraphicsLineItem *gridItem = static_cast<QGraphicsLineItem *>(lines.at(i));
108 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem *>(axis.at(i + 1));
108 QGraphicsLineItem *tickItem = static_cast<QGraphicsLineItem *>(axis.at(i + 1));
109 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labels.at(i));
109 QGraphicsSimpleTextItem *labelItem = static_cast<QGraphicsSimpleTextItem *>(labels.at(i));
110
110
111 //grid line
111 //grid line
112 gridItem->setLine(gridRect.left() , layout[i], gridRect.right(), layout[i]);
112 gridItem->setLine(gridRect.left() , layout[i], gridRect.right(), layout[i]);
113
113
114 //label text wrapping
114 //label text wrapping
115 QString text = labelList.at(i);
115 QString text = labelList.at(i);
116 QRectF boundingRect = labelBoundingRect(fn, text);
117
116 qreal size = axisRect.right() - axisRect.left() - labelPadding() - title->boundingRect().height();
118 qreal size = axisRect.right() - axisRect.left() - labelPadding() - title->boundingRect().height();
117 if (fn.boundingRect(text).width() > size) {
119 if (boundingRect.width() > size) {
118 QString label = text + "...";
120 QString label = text + "...";
119 while (fn.boundingRect(label).width() > size && label.length() > 3)
121 while (boundingRect.width() > size && label.length() > 3) {
120 label.remove(label.length() - 4, 1);
122 label.remove(label.length() - 4, 1);
123 boundingRect = labelBoundingRect(fn, label);
124 }
121 labelItem->setText(label);
125 labelItem->setText(label);
122 } else {
126 } else {
123 labelItem->setText(text);
127 labelItem->setText(text);
124 }
128 }
129
125 //label transformation origin point
130 //label transformation origin point
126 const QRectF &rect = labelItem->boundingRect();
131 const QRectF &rect = labelItem->boundingRect();
127
132
128 QPointF center = rect.center();
133 QPointF center = rect.center();
129 labelItem->setTransformOriginPoint(center.x(), center.y());
134 labelItem->setTransformOriginPoint(center.x(), center.y());
135 int widthDiff = rect.width() - boundingRect.width();
130
136
131 //ticks and label position
137 //ticks and label position
132 if (alignment() == Qt::AlignLeft) {
138 if (alignment() == Qt::AlignLeft) {
133 labelItem->setPos(axisRect.right() - rect.width() - labelPadding() , layout[i] - center.y());
139 labelItem->setPos(axisRect.right() - rect.width() + (widthDiff / 2) - labelPadding(), layout[i] - center.y());
134 tickItem->setLine(axisRect.right() - labelPadding(), layout[i], axisRect.right(), layout[i]);
140 tickItem->setLine(axisRect.right() - labelPadding(), layout[i], axisRect.right(), layout[i]);
135 } else if (alignment() == Qt::AlignRight) {
141 } else if (alignment() == Qt::AlignRight) {
136 labelItem->setPos(axisRect.left() + labelPadding() , layout[i] - center.y());
142 labelItem->setPos(axisRect.left() + labelPadding() - (widthDiff / 2), layout[i] - center.y());
137 tickItem->setLine(axisRect.left(), layout[i], axisRect.left() + labelPadding(), layout[i]);
143 tickItem->setLine(axisRect.left(), layout[i], axisRect.left() + labelPadding(), layout[i]);
138 }
144 }
139
145
140 //label in beetwen
146 //label in beetwen
141 if(intervalAxis()&& i+1!=layout.size()) {
147 if(intervalAxis()&& i+1!=layout.size()) {
142 const qreal delta = (layout[i+1] - layout[i])/2;
148 const qreal delta = (layout[i+1] - layout[i])/2;
143 labelItem->setPos(labelItem->pos().x() , layout[i] + delta - center.y());
149 labelItem->setPos(labelItem->pos().x() , layout[i] + delta - center.y());
144 }
150 }
145
151
146 //label overlap detection
152 //label overlap detection
147 if(labelItem->pos().y() + rect.height() > height ||
153 if(labelItem->pos().y() + boundingRect.height() > height ||
148 labelItem->pos().y() + rect.height()/2 > gridRect.bottom() ||
154 labelItem->pos().y() + boundingRect.height()/2 > gridRect.bottom() ||
149 labelItem->pos().y() + rect.height()/2 < gridRect.top()) {
155 labelItem->pos().y() + boundingRect.height()/2 < gridRect.top()) {
150 labelItem->setVisible(false);
156 labelItem->setVisible(false);
151 }
157 }
152 else {
158 else {
153 labelItem->setVisible(true);
159 labelItem->setVisible(true);
154 height=labelItem->pos().y();
160 height=labelItem->pos().y();
155 }
161 }
156
162
157 //shades
163 //shades
158 if ((i + 1) % 2 && i > 1) {
164 if ((i + 1) % 2 && i > 1) {
159 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1));
165 QGraphicsRectItem *rectItem = static_cast<QGraphicsRectItem *>(shades.at(i / 2 - 1));
160 rectItem->setRect(gridRect.left(), layout[i], gridRect.width(), layout[i - 1] - layout[i]);
166 rectItem->setRect(gridRect.left(), layout[i], gridRect.width(), layout[i - 1] - layout[i]);
161 }
167 }
162
168
163 // check if the grid line and the axis tick should be shown
169 // check if the grid line and the axis tick should be shown
164 qreal y = gridItem->line().p1().y();
170 qreal y = gridItem->line().p1().y();
165 if ((y < gridRect.top() || y > gridRect.bottom()))
171 if ((y < gridRect.top() || y > gridRect.bottom()))
166 {
172 {
167 gridItem->setVisible(false);
173 gridItem->setVisible(false);
168 tickItem->setVisible(false);
174 tickItem->setVisible(false);
169 }else{
175 }else{
170 gridItem->setVisible(true);
176 gridItem->setVisible(true);
171 tickItem->setVisible(true);
177 tickItem->setVisible(true);
172 }
178 }
173
179
174 }
180 }
175 //begin/end grid line in case labels between
181 //begin/end grid line in case labels between
176 if (intervalAxis()) {
182 if (intervalAxis()) {
177 QGraphicsLineItem *gridLine;
183 QGraphicsLineItem *gridLine;
178 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size()));
184 gridLine = static_cast<QGraphicsLineItem *>(lines.at(layout.size()));
179 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.right(), gridRect.top());
185 gridLine->setLine(gridRect.left(), gridRect.top(), gridRect.right(), gridRect.top());
180 gridLine->setVisible(true);
186 gridLine->setVisible(true);
181 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
187 gridLine = static_cast<QGraphicsLineItem*>(lines.at(layout.size()+1));
182 gridLine->setLine(gridRect.left(), gridRect.bottom(), gridRect.right(), gridRect.bottom());
188 gridLine->setLine(gridRect.left(), gridRect.bottom(), gridRect.right(), gridRect.bottom());
183 gridLine->setVisible(true);
189 gridLine->setVisible(true);
184 }
190 }
185 }
191 }
186
192
187 QSizeF VerticalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
193 QSizeF VerticalAxis::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
188 {
194 {
189
195
190 Q_UNUSED(constraint);
196 Q_UNUSED(constraint);
191 QFontMetrics fn(titleFont());
197 QFontMetrics fn(titleFont());
192 QSizeF sh(0,0);
198 QSizeF sh(0,0);
193
199
194 if (titleText().isNull() || !titleItem()->isVisible())
200 if (titleText().isNull() || !titleItem()->isVisible())
195 return sh;
201 return sh;
196
202
197 switch (which) {
203 switch (which) {
198 case Qt::MinimumSize:
204 case Qt::MinimumSize:
199 sh = QSizeF(fn.height(), fn.boundingRect("...").width());
205 sh = QSizeF(fn.height(), fn.boundingRect("...").width());
200 break;
206 break;
201 case Qt::MaximumSize:
207 case Qt::MaximumSize:
202 case Qt::PreferredSize:
208 case Qt::PreferredSize:
203 sh = QSizeF(fn.height(), fn.boundingRect(axis()->titleText()).width());
209 sh = QSizeF(fn.height(), fn.boundingRect(axis()->titleText()).width());
204 break;
210 break;
205 default:
211 default:
206 break;
212 break;
207 }
213 }
208
214
209 return sh;
215 return sh;
210 }
216 }
211
217
212 QTCOMMERCIALCHART_END_NAMESPACE
218 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now