##// END OF EJS Templates
Fix int-qreal rounding errors in axislayout calculations...
Miikka Heikkinen -
r2447:ba792163ba0e
parent child
Show More
@@ -1,142 +1,142
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 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)
50 if (delta < 2)
51 return points;
51 return points;
52
52
53 qreal adjustedMin = min() + 0.5;
53 qreal adjustedMin = min() + 0.5;
54 qreal offset = (ceil(adjustedMin) - adjustedMin) * delta;
54 qreal offset = (ceil(adjustedMin) - adjustedMin) * delta;
55
55
56 int count = qFloor(range);
56 int count = qFloor(range);
57 if (count < 1)
57 if (count < 1)
58 return points;
58 return points;
59
59
60 points.resize(count + 2);
60 points.resize(count + 2);
61
61
62 for (int i = 0; i < count + 2; ++i)
62 for (int i = 0; i < count + 2; ++i)
63 points[i] = offset + (i * delta) + gridRect.left();
63 points[i] = offset + (qreal(i) * delta) + gridRect.left();
64
64
65 return points;
65 return points;
66 }
66 }
67
67
68 QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector<qreal>& layout) const
68 QStringList ChartBarCategoryAxisX::createCategoryLabels(const QVector<qreal>& layout) const
69 {
69 {
70 QStringList result ;
70 QStringList result ;
71 const QRectF &gridRect = gridGeometry();
71 const QRectF &gridRect = gridGeometry();
72 qreal d = (max() - min()) / gridRect.width();
72 qreal d = (max() - min()) / gridRect.width();
73
73
74 for (int i = 0; i < layout.count() - 1; ++i) {
74 for (int i = 0; i < layout.count() - 1; ++i) {
75 qreal x = qFloor((((layout[i] + layout[i + 1]) / 2 - gridRect.left()) * d + min() + 0.5));
75 qreal x = qFloor((((layout[i] + layout[i + 1]) / 2 - gridRect.left()) * d + min() + 0.5));
76 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
76 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
77 result << m_categoriesAxis->categories().at(x);
77 result << m_categoriesAxis->categories().at(x);
78 } else {
78 } else {
79 // No label for x coordinate
79 // No label for x coordinate
80 result << "";
80 result << "";
81 }
81 }
82 }
82 }
83 result << "";
83 result << "";
84 return result;
84 return result;
85 }
85 }
86
86
87
87
88 void ChartBarCategoryAxisX::updateGeometry()
88 void ChartBarCategoryAxisX::updateGeometry()
89 {
89 {
90 const QVector<qreal>& layout = ChartAxis::layout();
90 const QVector<qreal>& layout = ChartAxis::layout();
91 if (layout.isEmpty())
91 if (layout.isEmpty())
92 return;
92 return;
93 setLabels(createCategoryLabels(layout));
93 setLabels(createCategoryLabels(layout));
94 HorizontalAxis::updateGeometry();
94 HorizontalAxis::updateGeometry();
95 }
95 }
96
96
97 void ChartBarCategoryAxisX::handleCategoriesChanged()
97 void ChartBarCategoryAxisX::handleCategoriesChanged()
98 {
98 {
99 QGraphicsLayoutItem::updateGeometry();
99 QGraphicsLayoutItem::updateGeometry();
100 if(presenter()) presenter()->layout()->invalidate();
100 if(presenter()) presenter()->layout()->invalidate();
101 }
101 }
102
102
103 QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
103 QSizeF ChartBarCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
104 {
104 {
105 Q_UNUSED(constraint)
105 Q_UNUSED(constraint)
106
106
107 QFontMetrics fn(font());
107 QFontMetrics fn(font());
108 QSizeF sh;
108 QSizeF sh;
109 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
109 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
110 QStringList ticksList = m_categoriesAxis->categories();
110 QStringList ticksList = m_categoriesAxis->categories();
111
111
112 qreal width = 0; // Width is irrelevant for X axes with interval labels
112 qreal width = 0; // Width is irrelevant for X axes with interval labels
113 qreal height = 0;
113 qreal height = 0;
114
114
115 switch (which) {
115 switch (which) {
116 case Qt::MinimumSize: {
116 case Qt::MinimumSize: {
117 QRectF boundingRect = labelBoundingRect(fn, "...");
117 QRectF boundingRect = labelBoundingRect(fn, "...");
118 height = boundingRect.height() + labelPadding();
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 }
123 case Qt::PreferredSize:{
123 case Qt::PreferredSize:{
124 int labelHeight = 0;
124 int labelHeight = 0;
125 foreach (const QString& s, ticksList) {
125 foreach (const QString& s, ticksList) {
126 QRect rect = labelBoundingRect(fn, s);
126 QRect rect = labelBoundingRect(fn, s);
127 labelHeight = qMax(rect.height(), labelHeight);
127 labelHeight = qMax(rect.height(), labelHeight);
128 }
128 }
129 height = labelHeight + labelPadding();
129 height = labelHeight + labelPadding();
130 height += base.height();
130 height += base.height();
131 sh = QSizeF(width, height);
131 sh = QSizeF(width, height);
132 break;
132 break;
133 }
133 }
134 default:
134 default:
135 break;
135 break;
136 }
136 }
137 return sh;
137 return sh;
138 }
138 }
139
139
140 #include "moc_chartbarcategoryaxisx_p.cpp"
140 #include "moc_chartbarcategoryaxisx_p.cpp"
141
141
142 QTCOMMERCIALCHART_END_NAMESPACE
142 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,144 +1,144
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 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)
50 if (delta < 2)
51 return points;
51 return points;
52
52
53 qreal adjustedMin = min() + 0.5;
53 qreal adjustedMin = min() + 0.5;
54 qreal offset = (ceil(adjustedMin) - adjustedMin) * delta;
54 qreal offset = (ceil(adjustedMin) - adjustedMin) * delta;
55
55
56 int count = qFloor(range);
56 int count = qFloor(range);
57 if (count < 1)
57 if (count < 1)
58 return points;
58 return points;
59
59
60 points.resize(count + 2);
60 points.resize(count + 2);
61
61
62 for (int i = 0; i < count + 2; ++i)
62 for (int i = 0; i < count + 2; ++i)
63 points[i] = gridRect.bottom() - (i * delta) - offset;
63 points[i] = gridRect.bottom() - (qreal(i) * delta) - offset;
64
64
65 return points;
65 return points;
66 }
66 }
67
67
68 QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& layout) const
68 QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& layout) const
69 {
69 {
70 QStringList result;
70 QStringList result;
71 const QRectF &gridRect = gridGeometry();
71 const QRectF &gridRect = gridGeometry();
72 qreal d = (max() - min()) / gridRect.height();
72 qreal d = (max() - min()) / gridRect.height();
73
73
74 for (int i = 0; i < layout.count() - 1; ++i) {
74 for (int i = 0; i < layout.count() - 1; ++i) {
75 qreal x = qFloor(((gridRect.height() - (layout[i + 1] + layout[i]) / 2 + gridRect.top()) * d + min() + 0.5));
75 qreal x = qFloor(((gridRect.height() - (layout[i + 1] + layout[i]) / 2 + gridRect.top()) * d + min() + 0.5));
76 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
76 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
77 result << m_categoriesAxis->categories().at(x);
77 result << m_categoriesAxis->categories().at(x);
78 } else {
78 } else {
79 // No label for x coordinate
79 // No label for x coordinate
80 result << "";
80 result << "";
81 }
81 }
82 }
82 }
83 result << "";
83 result << "";
84 return result;
84 return result;
85 }
85 }
86
86
87 void ChartBarCategoryAxisY::updateGeometry()
87 void ChartBarCategoryAxisY::updateGeometry()
88 {
88 {
89 const QVector<qreal>& layout = ChartAxis::layout();
89 const QVector<qreal>& layout = ChartAxis::layout();
90 if (layout.isEmpty())
90 if (layout.isEmpty())
91 return;
91 return;
92 setLabels(createCategoryLabels(layout));
92 setLabels(createCategoryLabels(layout));
93 VerticalAxis::updateGeometry();
93 VerticalAxis::updateGeometry();
94 }
94 }
95
95
96 void ChartBarCategoryAxisY::handleCategoriesChanged()
96 void ChartBarCategoryAxisY::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 ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
102 QSizeF ChartBarCategoryAxisY::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 = VerticalAxis::sizeHint(which, constraint);
108 QSizeF base = VerticalAxis::sizeHint(which, constraint);
109 QStringList ticksList = m_categoriesAxis->categories();
109 QStringList ticksList = m_categoriesAxis->categories();
110 qreal width = 0;
110 qreal width = 0;
111 qreal height = 0; // Height is irrelevant for Y axes with interval labels
111 qreal height = 0; // Height is irrelevant for Y axes with interval labels
112
112
113 switch (which) {
113 switch (which) {
114 case Qt::MinimumSize: {
114 case Qt::MinimumSize: {
115 QRectF boundingRect = labelBoundingRect(fn, "...");
115 QRectF boundingRect = labelBoundingRect(fn, "...");
116 width = boundingRect.width() + labelPadding();
116 width = boundingRect.width() + labelPadding();
117 width += base.width();
117 width += base.width();
118 if (base.width() > 0)
118 if (base.width() > 0)
119 width += labelPadding();
119 width += labelPadding();
120 sh = QSizeF(width, height);
120 sh = QSizeF(width, height);
121 break;
121 break;
122 }
122 }
123 case Qt::PreferredSize:{
123 case Qt::PreferredSize:{
124 int labelWidth = 0;
124 int labelWidth = 0;
125 foreach (const QString& s, ticksList) {
125 foreach (const QString& s, ticksList) {
126 QRect rect = labelBoundingRect(fn, s);
126 QRect rect = labelBoundingRect(fn, s);
127 labelWidth = qMax(rect.width(), labelWidth);
127 labelWidth = qMax(rect.width(), labelWidth);
128 }
128 }
129 width = labelWidth + labelPadding() + 1;
129 width = labelWidth + labelPadding() + 1;
130 width += base.width();
130 width += base.width();
131 if (base.width() > 0)
131 if (base.width() > 0)
132 width += labelPadding();
132 width += labelPadding();
133 sh = QSizeF(width, height);
133 sh = QSizeF(width, height);
134 break;
134 break;
135 }
135 }
136 default:
136 default:
137 break;
137 break;
138 }
138 }
139 return sh;
139 return sh;
140 }
140 }
141
141
142 #include "moc_chartbarcategoryaxisy_p.cpp"
142 #include "moc_chartbarcategoryaxisy_p.cpp"
143
143
144 QTCOMMERCIALCHART_END_NAMESPACE
144 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,135 +1,133
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 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() / (qreal(tickCount) - 1.0);
54 for (int i = 0; i < tickCount; ++i) {
54 for (int i = 0; i < tickCount; ++i)
55 int x = i * deltaX + gridRect.left();
55 points[i] = qreal(i) * deltaX + gridRect.left();
56 points[i] = x;
57 }
58 return points;
56 return points;
59 }
57 }
60
58
61 void ChartDateTimeAxisX::updateGeometry()
59 void ChartDateTimeAxisX::updateGeometry()
62 {
60 {
63 const QVector<qreal>& layout = ChartAxis::layout();
61 const QVector<qreal>& layout = ChartAxis::layout();
64 if (layout.isEmpty())
62 if (layout.isEmpty())
65 return;
63 return;
66 setLabels(createDateTimeLabels(min(),max(), layout.size(),m_axis->format()));
64 setLabels(createDateTimeLabels(min(),max(), layout.size(),m_axis->format()));
67 HorizontalAxis::updateGeometry();
65 HorizontalAxis::updateGeometry();
68 }
66 }
69
67
70 void ChartDateTimeAxisX::handleTickCountChanged(int tick)
68 void ChartDateTimeAxisX::handleTickCountChanged(int tick)
71 {
69 {
72 Q_UNUSED(tick)
70 Q_UNUSED(tick)
73 QGraphicsLayoutItem::updateGeometry();
71 QGraphicsLayoutItem::updateGeometry();
74 if(presenter()) presenter()->layout()->invalidate();
72 if(presenter()) presenter()->layout()->invalidate();
75 }
73 }
76
74
77 void ChartDateTimeAxisX::handleFormatChanged(const QString &format)
75 void ChartDateTimeAxisX::handleFormatChanged(const QString &format)
78 {
76 {
79 Q_UNUSED(format);
77 Q_UNUSED(format);
80 QGraphicsLayoutItem::updateGeometry();
78 QGraphicsLayoutItem::updateGeometry();
81 if(presenter()) presenter()->layout()->invalidate();
79 if(presenter()) presenter()->layout()->invalidate();
82 }
80 }
83
81
84 QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
82 QSizeF ChartDateTimeAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
85 {
83 {
86 Q_UNUSED(constraint)
84 Q_UNUSED(constraint)
87
85
88 QFontMetrics fn(font());
86 QFontMetrics fn(font());
89 QSizeF sh;
87 QSizeF sh;
90
88
91 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
89 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
92 QStringList ticksList = createDateTimeLabels(min(),max(),m_axis->tickCount(),m_axis->format());
90 QStringList ticksList = createDateTimeLabels(min(),max(),m_axis->tickCount(),m_axis->format());
93 // Width of horizontal axis sizeHint indicates the maximum distance labels can extend past
91 // Width of horizontal axis sizeHint indicates the maximum distance labels can extend past
94 // first and last ticks. Base width is irrelevant.
92 // first and last ticks. Base width is irrelevant.
95 qreal width = 0;
93 qreal width = 0;
96 qreal height = 0;
94 qreal height = 0;
97
95
98 if (ticksList.empty())
96 if (ticksList.empty())
99 return sh;
97 return sh;
100
98
101 switch (which) {
99 switch (which) {
102 case Qt::MinimumSize:{
100 case Qt::MinimumSize:{
103 QRectF boundingRect = labelBoundingRect(fn, "...");
101 QRectF boundingRect = labelBoundingRect(fn, "...");
104 width = boundingRect.width() / 2.0;
102 width = boundingRect.width() / 2.0;
105 height = boundingRect.height() + labelPadding();
103 height = boundingRect.height() + labelPadding();
106 height += base.height();
104 height += base.height();
107 sh = QSizeF(width, height);
105 sh = QSizeF(width, height);
108 break;
106 break;
109 }
107 }
110 case Qt::PreferredSize: {
108 case Qt::PreferredSize: {
111 int labelHeight = 0;
109 int labelHeight = 0;
112 int firstWidth = -1;
110 int firstWidth = -1;
113 foreach (const QString& s, ticksList) {
111 foreach (const QString& s, ticksList) {
114 QRect rect = labelBoundingRect(fn, s);
112 QRect rect = labelBoundingRect(fn, s);
115 labelHeight = qMax(rect.height(), labelHeight);
113 labelHeight = qMax(rect.height(), labelHeight);
116 width = rect.width();
114 width = rect.width();
117 if (firstWidth < 0)
115 if (firstWidth < 0)
118 firstWidth = width;
116 firstWidth = width;
119 }
117 }
120 height = labelHeight + labelPadding();
118 height = labelHeight + labelPadding();
121 height += base.height();
119 height += base.height();
122 width = qMax(width, qreal(firstWidth)) / 2.0;
120 width = qMax(width, qreal(firstWidth)) / 2.0;
123 sh = QSizeF(width, height);
121 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_chartdatetimeaxisx_p.cpp"
131 #include "moc_chartdatetimeaxisx_p.cpp"
134
132
135 QTCOMMERCIALCHART_END_NAMESPACE
133 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,136 +1,134
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 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() / (qreal(tickCount) - 1.0);
54 for (int i = 0; i < tickCount; ++i) {
54 for (int i = 0; i < tickCount; ++i)
55 int y = i * -deltaY + gridRect.bottom();
55 points[i] = qreal(i) * -deltaY + gridRect.bottom();
56 points[i] = y;
57 }
58
56
59 return points;
57 return points;
60 }
58 }
61
59
62 void ChartDateTimeAxisY::updateGeometry()
60 void ChartDateTimeAxisY::updateGeometry()
63 {
61 {
64 const QVector<qreal> &layout = ChartAxis::layout();
62 const QVector<qreal> &layout = ChartAxis::layout();
65 if (layout.isEmpty())
63 if (layout.isEmpty())
66 return;
64 return;
67 setLabels(createDateTimeLabels(min(),max(), layout.size(),m_axis->format()));
65 setLabels(createDateTimeLabels(min(),max(), layout.size(),m_axis->format()));
68 VerticalAxis::updateGeometry();
66 VerticalAxis::updateGeometry();
69 }
67 }
70
68
71 void ChartDateTimeAxisY::handleTickCountChanged(int tick)
69 void ChartDateTimeAxisY::handleTickCountChanged(int tick)
72 {
70 {
73 Q_UNUSED(tick)
71 Q_UNUSED(tick)
74 QGraphicsLayoutItem::updateGeometry();
72 QGraphicsLayoutItem::updateGeometry();
75 if(presenter()) presenter()->layout()->invalidate();
73 if(presenter()) presenter()->layout()->invalidate();
76 }
74 }
77
75
78 void ChartDateTimeAxisY::handleFormatChanged(const QString &format)
76 void ChartDateTimeAxisY::handleFormatChanged(const QString &format)
79 {
77 {
80 Q_UNUSED(format);
78 Q_UNUSED(format);
81 QGraphicsLayoutItem::updateGeometry();
79 QGraphicsLayoutItem::updateGeometry();
82 if(presenter()) presenter()->layout()->invalidate();
80 if(presenter()) presenter()->layout()->invalidate();
83 }
81 }
84
82
85 QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
83 QSizeF ChartDateTimeAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
86 {
84 {
87 Q_UNUSED(constraint)
85 Q_UNUSED(constraint)
88
86
89 QFontMetrics fn(font());
87 QFontMetrics fn(font());
90 QSizeF sh;
88 QSizeF sh;
91
89
92 QSizeF base = VerticalAxis::sizeHint(which, constraint);
90 QSizeF base = VerticalAxis::sizeHint(which, constraint);
93 QStringList ticksList = createDateTimeLabels(min(),max(),m_axis->tickCount(),m_axis->format());
91 QStringList ticksList = createDateTimeLabels(min(),max(),m_axis->tickCount(),m_axis->format());
94 qreal width = 0;
92 qreal width = 0;
95 // Height of vertical axis sizeHint indicates the maximum distance labels can extend past
93 // Height of vertical axis sizeHint indicates the maximum distance labels can extend past
96 // first and last ticks. Base height is irrelevant.
94 // first and last ticks. Base height is irrelevant.
97 qreal height = 0;
95 qreal height = 0;
98
96
99 if (ticksList.empty())
97 if (ticksList.empty())
100 return sh;
98 return sh;
101
99
102 switch (which) {
100 switch (which) {
103 case Qt::MinimumSize: {
101 case Qt::MinimumSize: {
104 QRectF boundingRect = labelBoundingRect(fn, "...");
102 QRectF boundingRect = labelBoundingRect(fn, "...");
105 width = boundingRect.width() + labelPadding();
103 width = boundingRect.width() + labelPadding();
106 width += base.width();
104 width += base.width();
107 height = boundingRect.height() / 2.0;
105 height = boundingRect.height() / 2.0;
108 sh = QSizeF(width, height);
106 sh = QSizeF(width, height);
109 break;
107 break;
110 }
108 }
111 case Qt::PreferredSize: {
109 case Qt::PreferredSize: {
112 int labelWidth = 0;
110 int labelWidth = 0;
113 int firstHeight = -1;
111 int firstHeight = -1;
114 foreach (const QString& s, ticksList) {
112 foreach (const QString& s, ticksList) {
115 QRect rect = labelBoundingRect(fn, s);
113 QRect rect = labelBoundingRect(fn, s);
116 labelWidth = qMax(rect.width(), labelWidth);
114 labelWidth = qMax(rect.width(), labelWidth);
117 height = rect.height();
115 height = rect.height();
118 if (firstHeight < 0)
116 if (firstHeight < 0)
119 firstHeight = height;
117 firstHeight = height;
120 }
118 }
121 width = labelWidth + labelPadding() + 2; //two pixels of tolerance
119 width = labelWidth + labelPadding() + 2; //two pixels of tolerance
122 width += base.width();
120 width += base.width();
123 height = qMax(height, qreal(firstHeight)) / 2.0;
121 height = qMax(height, qreal(firstHeight)) / 2.0;
124 sh = QSizeF(width, height);
122 sh = QSizeF(width, height);
125 break;
123 break;
126 }
124 }
127 default:
125 default:
128 break;
126 break;
129 }
127 }
130
128
131 return sh;
129 return sh;
132 }
130 }
133
131
134 #include "moc_chartdatetimeaxisy_p.cpp"
132 #include "moc_chartdatetimeaxisy_p.cpp"
135
133
136 QTCOMMERCIALCHART_END_NAMESPACE
134 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,133 +1,132
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 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() / (qreal(tickCount) - 1.0);
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] = qreal(i) * deltaX + gridRect.left();
59 }
60 return points;
59 return points;
61 }
60 }
62
61
63 void ChartValueAxisX::updateGeometry()
62 void ChartValueAxisX::updateGeometry()
64 {
63 {
65 const QVector<qreal>& layout = ChartAxis::layout();
64 const QVector<qreal>& layout = ChartAxis::layout();
66 if (layout.isEmpty())
65 if (layout.isEmpty())
67 return;
66 return;
68 setLabels(createValueLabels(min(),max(),layout.size(),m_axis->labelFormat()));
67 setLabels(createValueLabels(min(),max(),layout.size(),m_axis->labelFormat()));
69 HorizontalAxis::updateGeometry();
68 HorizontalAxis::updateGeometry();
70 }
69 }
71
70
72 void ChartValueAxisX::handleTickCountChanged(int tick)
71 void ChartValueAxisX::handleTickCountChanged(int tick)
73 {
72 {
74 Q_UNUSED(tick);
73 Q_UNUSED(tick);
75 QGraphicsLayoutItem::updateGeometry();
74 QGraphicsLayoutItem::updateGeometry();
76 if(presenter()) presenter()->layout()->invalidate();
75 if(presenter()) presenter()->layout()->invalidate();
77 }
76 }
78
77
79 void ChartValueAxisX::handleLabelFormatChanged(const QString &format)
78 void ChartValueAxisX::handleLabelFormatChanged(const QString &format)
80 {
79 {
81 Q_UNUSED(format);
80 Q_UNUSED(format);
82 QGraphicsLayoutItem::updateGeometry();
81 QGraphicsLayoutItem::updateGeometry();
83 if(presenter()) presenter()->layout()->invalidate();
82 if(presenter()) presenter()->layout()->invalidate();
84 }
83 }
85
84
86 QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
85 QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
87 {
86 {
88 Q_UNUSED(constraint)
87 Q_UNUSED(constraint)
89
88
90 QFontMetrics fn(font());
89 QFontMetrics fn(font());
91 QSizeF sh;
90 QSizeF sh;
92
91
93 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
92 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
94 QStringList ticksList = createValueLabels(min(),max(),m_axis->tickCount(),m_axis->labelFormat());
93 QStringList ticksList = createValueLabels(min(),max(),m_axis->tickCount(),m_axis->labelFormat());
95 // Width of horizontal axis sizeHint indicates the maximum distance labels can extend past
94 // Width of horizontal axis sizeHint indicates the maximum distance labels can extend past
96 // first and last ticks. Base width is irrelevant.
95 // first and last ticks. Base width is irrelevant.
97 qreal width = 0;
96 qreal width = 0;
98 qreal height = 0;
97 qreal height = 0;
99
98
100 switch (which) {
99 switch (which) {
101 case Qt::MinimumSize: {
100 case Qt::MinimumSize: {
102 QRectF boundingRect = labelBoundingRect(fn, "...");
101 QRectF boundingRect = labelBoundingRect(fn, "...");
103 width = boundingRect.width() / 2.0;
102 width = boundingRect.width() / 2.0;
104 height = boundingRect.height() + labelPadding();
103 height = boundingRect.height() + labelPadding();
105 height += base.height();
104 height += base.height();
106 sh = QSizeF(width, height);
105 sh = QSizeF(width, height);
107 break;
106 break;
108 }
107 }
109 case Qt::PreferredSize: {
108 case Qt::PreferredSize: {
110 int labelHeight = 0;
109 int labelHeight = 0;
111 int firstWidth = -1;
110 int firstWidth = -1;
112 foreach (const QString& s, ticksList) {
111 foreach (const QString& s, ticksList) {
113 QRect rect = labelBoundingRect(fn, s);
112 QRect rect = labelBoundingRect(fn, s);
114 labelHeight = qMax(rect.height(), labelHeight);
113 labelHeight = qMax(rect.height(), labelHeight);
115 width = rect.width();
114 width = rect.width();
116 if (firstWidth < 0)
115 if (firstWidth < 0)
117 firstWidth = width;
116 firstWidth = width;
118 }
117 }
119 height = labelHeight + labelPadding();
118 height = labelHeight + labelPadding();
120 height += base.height();
119 height += base.height();
121 width = qMax(width, qreal(firstWidth)) / 2.0;
120 width = qMax(width, qreal(firstWidth)) / 2.0;
122 sh = QSizeF(width, height);
121 sh = QSizeF(width, height);
123 break;
122 break;
124 }
123 }
125 default:
124 default:
126 break;
125 break;
127 }
126 }
128 return sh;
127 return sh;
129 }
128 }
130
129
131 #include "moc_chartvalueaxisx_p.cpp"
130 #include "moc_chartvalueaxisx_p.cpp"
132
131
133 QTCOMMERCIALCHART_END_NAMESPACE
132 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,133 +1,132
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2013 Digia Plc
3 ** Copyright (C) 2013 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() / (qreal(tickCount) - 1.0);
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] = qreal(i) * -deltaY + gridRect.bottom();
59 }
60
59
61 return points;
60 return points;
62 }
61 }
63
62
64 void ChartValueAxisY::updateGeometry()
63 void ChartValueAxisY::updateGeometry()
65 {
64 {
66 const QVector<qreal> &layout = ChartAxis::layout();
65 const QVector<qreal> &layout = ChartAxis::layout();
67 if (layout.isEmpty())
66 if (layout.isEmpty())
68 return;
67 return;
69 setLabels(createValueLabels(min(),max(),layout.size(),m_axis->labelFormat()));
68 setLabels(createValueLabels(min(),max(),layout.size(),m_axis->labelFormat()));
70 VerticalAxis::updateGeometry();
69 VerticalAxis::updateGeometry();
71 }
70 }
72
71
73 void ChartValueAxisY::handleTickCountChanged(int tick)
72 void ChartValueAxisY::handleTickCountChanged(int tick)
74 {
73 {
75 Q_UNUSED(tick);
74 Q_UNUSED(tick);
76 QGraphicsLayoutItem::updateGeometry();
75 QGraphicsLayoutItem::updateGeometry();
77 if(presenter()) presenter()->layout()->invalidate();
76 if(presenter()) presenter()->layout()->invalidate();
78 }
77 }
79
78
80 void ChartValueAxisY::handleLabelFormatChanged(const QString &format)
79 void ChartValueAxisY::handleLabelFormatChanged(const QString &format)
81 {
80 {
82 Q_UNUSED(format);
81 Q_UNUSED(format);
83 QGraphicsLayoutItem::updateGeometry();
82 QGraphicsLayoutItem::updateGeometry();
84 if(presenter()) presenter()->layout()->invalidate();
83 if(presenter()) presenter()->layout()->invalidate();
85 }
84 }
86
85
87 QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
86 QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
88 {
87 {
89 Q_UNUSED(constraint)
88 Q_UNUSED(constraint)
90
89
91 QFontMetrics fn(font());
90 QFontMetrics fn(font());
92 QSizeF sh;
91 QSizeF sh;
93 QSizeF base = VerticalAxis::sizeHint(which, constraint);
92 QSizeF base = VerticalAxis::sizeHint(which, constraint);
94 QStringList ticksList = createValueLabels(min(),max(),m_axis->tickCount(),m_axis->labelFormat());
93 QStringList ticksList = createValueLabels(min(),max(),m_axis->tickCount(),m_axis->labelFormat());
95 qreal width = 0;
94 qreal width = 0;
96 // Height of vertical axis sizeHint indicates the maximum distance labels can extend past
95 // Height of vertical axis sizeHint indicates the maximum distance labels can extend past
97 // first and last ticks. Base height is irrelevant.
96 // first and last ticks. Base height is irrelevant.
98 qreal height = 0;
97 qreal height = 0;
99
98
100 switch (which) {
99 switch (which) {
101 case Qt::MinimumSize: {
100 case Qt::MinimumSize: {
102 QRectF boundingRect = labelBoundingRect(fn, "...");
101 QRectF boundingRect = labelBoundingRect(fn, "...");
103 width = boundingRect.width() + labelPadding();
102 width = boundingRect.width() + labelPadding();
104 width += base.width();
103 width += base.width();
105 height = boundingRect.height() / 2.0;
104 height = boundingRect.height() / 2.0;
106 sh = QSizeF(width, height);
105 sh = QSizeF(width, height);
107 break;
106 break;
108 }
107 }
109 case Qt::PreferredSize: {
108 case Qt::PreferredSize: {
110 int labelWidth = 0;
109 int labelWidth = 0;
111 int firstHeight = -1;
110 int firstHeight = -1;
112 foreach (const QString& s, ticksList) {
111 foreach (const QString& s, ticksList) {
113 QRect rect = labelBoundingRect(fn, s);
112 QRect rect = labelBoundingRect(fn, s);
114 labelWidth = qMax(rect.width(), labelWidth);
113 labelWidth = qMax(rect.width(), labelWidth);
115 height = rect.height();
114 height = rect.height();
116 if (firstHeight < 0)
115 if (firstHeight < 0)
117 firstHeight = height;
116 firstHeight = height;
118 }
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 = qMax(height, qreal(firstHeight)) / 2.0;
120 height = qMax(height, qreal(firstHeight)) / 2.0;
122 sh = QSizeF(width, height);
121 sh = QSizeF(width, height);
123 break;
122 break;
124 }
123 }
125 default:
124 default:
126 break;
125 break;
127 }
126 }
128 return sh;
127 return sh;
129 }
128 }
130
129
131 #include "moc_chartvalueaxisy_p.cpp"
130 #include "moc_chartvalueaxisy_p.cpp"
132
131
133 QTCOMMERCIALCHART_END_NAMESPACE
132 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now