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