##// END OF EJS Templates
Bugfix precision error when calculating layout in valueaxis
Michal Klocek -
r2144:c7788799f12f
parent child
Show More
@@ -1,117 +1,116
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartvalueaxisx_p.h"
21 #include "chartvalueaxisx_p.h"
22 #include "qabstractaxis.h"
22 #include "qabstractaxis.h"
23 #include "chartpresenter_p.h"
23 #include "chartpresenter_p.h"
24 #include "qvalueaxis.h"
24 #include "qvalueaxis.h"
25 #include "chartlayout_p.h"
25 #include "chartlayout_p.h"
26 #include <QGraphicsLayout>
26 #include <QGraphicsLayout>
27 #include <QFontMetrics>
27 #include <QFontMetrics>
28 #include <qmath.h>
28 #include <qmath.h>
29
29
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 ChartValueAxisX::ChartValueAxisX(QValueAxis *axis, ChartPresenter *presenter)
33 ChartValueAxisX::ChartValueAxisX(QValueAxis *axis, ChartPresenter *presenter)
34 : HorizontalAxis(axis, presenter),
34 : HorizontalAxis(axis, presenter),
35 m_tickCount(0), m_axis(axis)
35 m_tickCount(0), m_axis(axis)
36 {
36 {
37 }
37 }
38
38
39 ChartValueAxisX::~ChartValueAxisX()
39 ChartValueAxisX::~ChartValueAxisX()
40 {
40 {
41 }
41 }
42
42
43 QVector<qreal> ChartValueAxisX::calculateLayout() const
43 QVector<qreal> ChartValueAxisX::calculateLayout() const
44 {
44 {
45 Q_ASSERT(m_tickCount >= 2);
45 Q_ASSERT(m_tickCount >= 2);
46
46
47 QVector<qreal> points;
47 QVector<qreal> points;
48 points.resize(m_tickCount);
48 points.resize(m_tickCount);
49
49
50 const QRectF &gridRect = gridGeometry();
50 const QRectF &gridRect = gridGeometry();
51 const qreal deltaX = gridRect.width() / (m_tickCount - 1);
51 const qreal deltaX = gridRect.width() / (m_tickCount - 1);
52 for (int i = 0; i < m_tickCount; ++i) {
52 for (int i = 0; i < m_tickCount; ++i) {
53 int x = i * deltaX + gridRect.left();
53 points[i] = i * deltaX + gridRect.left();
54 points[i] = x;
55 }
54 }
56 return points;
55 return points;
57 }
56 }
58
57
59 void ChartValueAxisX::updateGeometry()
58 void ChartValueAxisX::updateGeometry()
60 {
59 {
61 const QVector<qreal>& layout = ChartAxis::layout();
60 const QVector<qreal>& layout = ChartAxis::layout();
62 if (layout.isEmpty())
61 if (layout.isEmpty())
63 return;
62 return;
64 setLabels(createValueLabels(layout.size()));
63 setLabels(createValueLabels(layout.size()));
65 HorizontalAxis::updateGeometry();
64 HorizontalAxis::updateGeometry();
66 }
65 }
67
66
68 void ChartValueAxisX::handleAxisUpdated()
67 void ChartValueAxisX::handleAxisUpdated()
69 {
68 {
70 if (m_tickCount != m_axis->tickCount()) {
69 if (m_tickCount != m_axis->tickCount()) {
71 m_tickCount = m_axis->tickCount();
70 m_tickCount = m_axis->tickCount();
72 presenter()->layout()->invalidate();
71 presenter()->layout()->invalidate();
73 }
72 }
74
73
75 ChartAxis::handleAxisUpdated();
74 ChartAxis::handleAxisUpdated();
76 }
75 }
77
76
78 QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
77 QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
79 {
78 {
80 Q_UNUSED(constraint)
79 Q_UNUSED(constraint)
81
80
82 QFontMetrics fn(font());
81 QFontMetrics fn(font());
83 QSizeF sh;
82 QSizeF sh;
84
83
85 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
84 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
86 QStringList ticksList = createValueLabels(m_tickCount);
85 QStringList ticksList = createValueLabels(m_tickCount);
87 qreal width = 0;
86 qreal width = 0;
88 qreal height = 0;
87 qreal height = 0;
89
88
90 switch (which) {
89 switch (which) {
91 case Qt::MinimumSize:{
90 case Qt::MinimumSize:{
92 int count = qMax(ticksList.last().count(),ticksList.first().count());
91 int count = qMax(ticksList.last().count(),ticksList.first().count());
93 count = qMin(count,5);
92 count = qMin(count,5);
94 width = fn.averageCharWidth() * count;
93 width = fn.averageCharWidth() * count;
95 height = fn.height() + labelPadding();
94 height = fn.height() + labelPadding();
96 width = qMax(width,base.width());
95 width = qMax(width,base.width());
97 height += base.height();
96 height += base.height();
98 sh = QSizeF(width,height);
97 sh = QSizeF(width,height);
99 break;
98 break;
100 }
99 }
101 case Qt::PreferredSize:{
100 case Qt::PreferredSize:{
102 int count = qMax(ticksList.last().count(),ticksList.first().count());
101 int count = qMax(ticksList.last().count(),ticksList.first().count());
103 width=fn.averageCharWidth() * count;
102 width=fn.averageCharWidth() * count;
104 height=fn.height()+labelPadding();
103 height=fn.height()+labelPadding();
105 width=qMax(width,base.width());
104 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 default:
109 default:
111 break;
110 break;
112 }
111 }
113
112
114 return sh;
113 return sh;
115 }
114 }
116
115
117 QTCOMMERCIALCHART_END_NAMESPACE
116 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,117 +1,116
1 /****************************************************************************
1 /****************************************************************************
2 **
2 **
3 ** Copyright (C) 2012 Digia Plc
3 ** Copyright (C) 2012 Digia Plc
4 ** All rights reserved.
4 ** All rights reserved.
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
5 ** For any questions to Digia, please use contact form at http://qt.digia.com
6 **
6 **
7 ** This file is part of the Qt Commercial Charts Add-on.
7 ** This file is part of the Qt Commercial Charts Add-on.
8 **
8 **
9 ** $QT_BEGIN_LICENSE$
9 ** $QT_BEGIN_LICENSE$
10 ** Licensees holding valid Qt Commercial licenses may use this file in
10 ** Licensees holding valid Qt Commercial licenses may use this file in
11 ** accordance with the Qt Commercial License Agreement provided with the
11 ** accordance with the Qt Commercial License Agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.
13 ** a written agreement between you and Digia.
14 **
14 **
15 ** If you have questions regarding the use of this file, please use
15 ** If you have questions regarding the use of this file, please use
16 ** contact form at http://qt.digia.com
16 ** contact form at http://qt.digia.com
17 ** $QT_END_LICENSE$
17 ** $QT_END_LICENSE$
18 **
18 **
19 ****************************************************************************/
19 ****************************************************************************/
20
20
21 #include "chartvalueaxisy_p.h"
21 #include "chartvalueaxisy_p.h"
22 #include "qabstractaxis.h"
22 #include "qabstractaxis.h"
23 #include "chartpresenter_p.h"
23 #include "chartpresenter_p.h"
24 #include "qvalueaxis.h"
24 #include "qvalueaxis.h"
25 #include "chartlayout_p.h"
25 #include "chartlayout_p.h"
26 #include <QGraphicsLayout>
26 #include <QGraphicsLayout>
27 #include <QFontMetrics>
27 #include <QFontMetrics>
28 #include <qmath.h>
28 #include <qmath.h>
29 #include <QDebug>
29 #include <QDebug>
30
30
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32
32
33 ChartValueAxisY::ChartValueAxisY(QValueAxis *axis, ChartPresenter *presenter)
33 ChartValueAxisY::ChartValueAxisY(QValueAxis *axis, ChartPresenter *presenter)
34 : VerticalAxis(axis, presenter),
34 : VerticalAxis(axis, presenter),
35 m_tickCount(0),
35 m_tickCount(0),
36 m_axis(axis)
36 m_axis(axis)
37 {
37 {
38 }
38 }
39
39
40 ChartValueAxisY::~ChartValueAxisY()
40 ChartValueAxisY::~ChartValueAxisY()
41 {
41 {
42 }
42 }
43
43
44 QVector<qreal> ChartValueAxisY::calculateLayout() const
44 QVector<qreal> ChartValueAxisY::calculateLayout() const
45 {
45 {
46 Q_ASSERT(m_tickCount >= 2);
46 Q_ASSERT(m_tickCount >= 2);
47
47
48 QVector<qreal> points;
48 QVector<qreal> points;
49 points.resize(m_tickCount);
49 points.resize(m_tickCount);
50
50
51 const QRectF &gridRect = gridGeometry();
51 const QRectF &gridRect = gridGeometry();
52
52
53 const qreal deltaY = gridRect.height() / (m_tickCount - 1);
53 const qreal deltaY = gridRect.height() / (m_tickCount - 1);
54 for (int i = 0; i < m_tickCount; ++i) {
54 for (int i = 0; i < m_tickCount; ++i) {
55 int y = i * -deltaY + gridRect.bottom();
55 points[i] = i * -deltaY + gridRect.bottom();
56 points[i] = y;
57 }
56 }
58
57
59 return points;
58 return points;
60 }
59 }
61
60
62 void ChartValueAxisY::updateGeometry()
61 void ChartValueAxisY::updateGeometry()
63 {
62 {
64 const QVector<qreal> &layout = ChartAxis::layout();
63 const QVector<qreal> &layout = ChartAxis::layout();
65 if (layout.isEmpty())
64 if (layout.isEmpty())
66 return;
65 return;
67 setLabels(createValueLabels(layout.size()));
66 setLabels(createValueLabels(layout.size()));
68 VerticalAxis::updateGeometry();
67 VerticalAxis::updateGeometry();
69 }
68 }
70
69
71 void ChartValueAxisY::handleAxisUpdated()
70 void ChartValueAxisY::handleAxisUpdated()
72 {
71 {
73 if (m_tickCount != m_axis->tickCount()) {
72 if (m_tickCount != m_axis->tickCount()) {
74 m_tickCount = m_axis->tickCount();
73 m_tickCount = m_axis->tickCount();
75 presenter()->layout()->invalidate();
74 presenter()->layout()->invalidate();
76 }
75 }
77
76
78 ChartAxis::handleAxisUpdated();
77 ChartAxis::handleAxisUpdated();
79 }
78 }
80
79
81 QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
80 QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
82 {
81 {
83 Q_UNUSED(constraint)
82 Q_UNUSED(constraint)
84
83
85 QFontMetrics fn(font());
84 QFontMetrics fn(font());
86 QSizeF sh;
85 QSizeF sh;
87 QSizeF base = VerticalAxis::sizeHint(which, constraint);
86 QSizeF base = VerticalAxis::sizeHint(which, constraint);
88 QStringList ticksList = createValueLabels(m_tickCount);
87 QStringList ticksList = createValueLabels(m_tickCount);
89 qreal width = 0;
88 qreal width = 0;
90 qreal height = 0;
89 qreal height = 0;
91
90
92 switch (which) {
91 switch (which) {
93 case Qt::MinimumSize: {
92 case Qt::MinimumSize: {
94 width = fn.boundingRect("...").width() + labelPadding();
93 width = fn.boundingRect("...").width() + labelPadding();
95 width += base.width();
94 width += base.width();
96 height = fn.height();
95 height = fn.height();
97 height = qMax(height,base.height());
96 height = qMax(height,base.height());
98 sh = QSizeF(width,height);
97 sh = QSizeF(width,height);
99 break;
98 break;
100 }
99 }
101 case Qt::PreferredSize:
100 case Qt::PreferredSize:
102 {
101 {
103 int count = qMax(ticksList.first().count() , ticksList.last().count());
102 int count = qMax(ticksList.first().count() , ticksList.last().count());
104 width = count*fn.averageCharWidth() + labelPadding() + 2; //two pixels of tolerance
103 width = count*fn.averageCharWidth() + labelPadding() + 2; //two pixels of tolerance
105 width += base.width();
104 width += base.width();
106 height = fn.height() * ticksList.count();
105 height = fn.height() * ticksList.count();
107 height = qMax(height,base.height());
106 height = qMax(height,base.height());
108 sh = QSizeF(width,height);
107 sh = QSizeF(width,height);
109 break;
108 break;
110 }
109 }
111 default:
110 default:
112 break;
111 break;
113 }
112 }
114 return sh;
113 return sh;
115 }
114 }
116
115
117 QTCOMMERCIALCHART_END_NAMESPACE
116 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now