##// END OF EJS Templates
Fixes categoryaxis sizeHints
Michal Klocek -
r2136:44641cd4faa0
parent child
Show More
@@ -1,146 +1,146
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 "chartbarcategoryaxisy_p.h"
22 22 #include "chartpresenter_p.h"
23 23 #include "qbarcategoryaxis_p.h"
24 24 #include <qmath.h>
25 25 #include <QFontMetrics>
26 26 #include <QDebug>
27 27
28 28 QTCOMMERCIALCHART_BEGIN_NAMESPACE
29 29
30 30 ChartBarCategoryAxisY::ChartBarCategoryAxisY(QBarCategoryAxis *axis, ChartPresenter *presenter)
31 31 : VerticalAxis(axis, presenter, true),
32 32 m_categoriesAxis(axis)
33 33 {
34 34 }
35 35
36 36 ChartBarCategoryAxisY::~ChartBarCategoryAxisY()
37 37 {
38 38 }
39 39
40 40 QVector<qreal> ChartBarCategoryAxisY::calculateLayout() const
41 41 {
42 42 int count = m_categoriesAxis->d_ptr->count();
43 43
44 44 Q_ASSERT(count >= 1);
45 45
46 46 QVector<qreal> points;
47 47 points.resize(count + 2);
48 48
49 49 const QRectF &gridRect = gridGeometry();
50 50
51 51 const qreal delta = gridRect.height() / (count);
52 52 qreal offset = - min() - 0.5;
53 53
54 54 if (delta < 1)
55 55 return points;
56 56
57 57 if (offset < 0)
58 58 offset = int(offset * gridRect.height() / (max() - min())) % int(delta) + delta;
59 59 else
60 60 offset = int(offset * gridRect.height() / (max() - min())) % int(delta);
61 61
62 62 for (int i = -1; i < count + 1; ++i) {
63 63 int y = gridRect.bottom() - i * delta - offset;
64 64 points[i + 1] = y;
65 65 }
66 66 return points;
67 67 }
68 68
69 69 QStringList ChartBarCategoryAxisY::createCategoryLabels(const QVector<qreal>& layout) const
70 70 {
71 71 QStringList result;
72 72 const QRectF &gridRect = gridGeometry();
73 73 qreal d = (max() - min()) / gridRect.height();
74 74 for (int i = 0; i < layout.count() - 1; ++i) {
75 75 qreal x = qFloor(((gridRect.height() - (layout[i + 1] + layout[i]) / 2 + gridRect.top()) * d + min() + 0.5));
76 76 if ((x < m_categoriesAxis->categories().count()) && (x >= 0)) {
77 77 result << m_categoriesAxis->categories().at(x);
78 78 } else {
79 79 // No label for x coordinate
80 80 result << "";
81 81 }
82 82 }
83 83 result << "";
84 84 return result;
85 85 }
86 86
87 87 void ChartBarCategoryAxisY::updateGeometry()
88 88 {
89 89 const QVector<qreal>& layout = ChartAxis::layout();
90 90 if (layout.isEmpty())
91 91 return;
92 92 setLabels(createCategoryLabels(layout));
93 93 VerticalAxis::updateGeometry();
94 94 }
95 95
96 96 void ChartBarCategoryAxisY::handleAxisUpdated()
97 97 {
98 98
99 99 if (m_categoriesAxis->categories() != m_categories) {
100 100 m_categories = m_categoriesAxis->categories();
101 101 if (ChartAxis::layout().count() == m_categoriesAxis->d_ptr->count() + 2)
102 102 updateGeometry();
103 103 }
104 104 ChartAxis::handleAxisUpdated();
105 105 }
106 106
107 107 QSizeF ChartBarCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
108 108 {
109 109 Q_UNUSED(constraint)
110 110
111 111 QFontMetrics fn(font());
112 112 QSizeF sh;
113 113 QSizeF base = ChartAxis::sizeHint(which, constraint);
114 114 QStringList ticksList = createCategoryLabels(ChartAxis::layout());
115 115
116 116 qreal width=0;
117 117 qreal height=0;
118 118
119 119 switch (which) {
120 120 case Qt::MinimumSize:
121 121 width = fn.boundingRect("...").width() + labelPadding();
122 122 height = fn.height();
123 123 width+=base.width();
124 124 height=qMax(height,base.height());
125 125 sh = QSizeF(width,height);
126 126 break;
127 127 case Qt::PreferredSize:{
128 128
129 129 for (int i = 0; i < ticksList.size(); ++i)
130 130 {
131 131 QRectF rect = fn.boundingRect(ticksList.at(i));
132 132 height+=rect.height();
133 width=qMax(rect.width()+labelPadding() +1 ,width); //one pixel torelance
133 width=qMax(rect.width()+labelPadding() + 1 ,width); //one pixel torelance
134 134 }
135 135 height=qMax(height,base.height());
136 136 width+=base.width();
137 137 sh = QSizeF(width,height);
138 138 break;
139 139 }
140 140 default:
141 141 break;
142 142 }
143 143 return sh;
144 144 }
145 145
146 146 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,118 +1,119
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 "chartcategoryaxisx_p.h"
22 22 #include "qcategoryaxis.h"
23 23 #include "qabstractaxis.h"
24 24 #include "chartpresenter_p.h"
25 25 #include <QGraphicsLayout>
26 26 #include <QFontMetrics>
27 27 #include <qmath.h>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 ChartCategoryAxisX::ChartCategoryAxisX(QCategoryAxis *axis, ChartPresenter *presenter)
32 32 : HorizontalAxis(axis, presenter, true),
33 33 m_axis(axis)
34 34 {
35 35 }
36 36
37 37 ChartCategoryAxisX::~ChartCategoryAxisX()
38 38 {
39 39 }
40 40
41 41 QVector<qreal> ChartCategoryAxisX::calculateLayout() const
42 42 {
43 43 int tickCount = m_axis->categoriesLabels().count() + 1;
44 44 QVector<qreal> points;
45 45
46 46 if (tickCount < 2)
47 47 return points;
48 48
49 49 const QRectF &gridRect = gridGeometry();
50 qreal range = m_axis->max() - m_axis->min();
50 qreal range = max() - min();
51 51 if (range > 0) {
52 52 points.resize(tickCount);
53 53 qreal scale = gridRect.width() / range;
54 54 for (int i = 0; i < tickCount; ++i) {
55 55 if (i < tickCount - 1) {
56 int x = (m_axis->startValue(m_axis->categoriesLabels().at(i)) - m_axis->min()) * scale + gridRect.left();
56 int x = (m_axis->startValue(m_axis->categoriesLabels().at(i)) - min()) * scale + gridRect.left();
57 57 points[i] = x;
58 58 } else {
59 int x = (m_axis->endValue(m_axis->categoriesLabels().at(i - 1)) - m_axis->min()) * scale + gridRect.left();
59 int x = (m_axis->endValue(m_axis->categoriesLabels().at(i - 1)) - min()) * scale + gridRect.left();
60 60 points[i] = x;
61 61 }
62 62 }
63 63 }
64 64
65 65 return points;
66 66 }
67 67
68 68 void ChartCategoryAxisX::updateGeometry()
69 69 {
70 //TODO: this is not optimal when many categories :( , create only visible lables
70 71 setLabels(m_axis->categoriesLabels() << "");
71 72 HorizontalAxis::updateGeometry();
72 73 }
73 74
74 75 void ChartCategoryAxisX::handleAxisUpdated()
75 76 {
76 77 updateGeometry();
77 78 ChartAxis::handleAxisUpdated();
78 79 }
79 80
80 81 QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
81 82 {
82 83 Q_UNUSED(constraint)
83 84
84 85 QFontMetrics fn(font());
85 86 QSizeF sh;
86 87 QSizeF base = ChartAxis::sizeHint(which, constraint);
87 QStringList ticksList ; //TODO:
88 QStringList ticksList = m_axis->categoriesLabels();
88 89 qreal width = 0;
89 90 qreal height = 0;
90 91
91 92 switch (which) {
92 93 case Qt::MinimumSize:
93 94 width = fn.boundingRect("...").width();
94 95 height = fn.height() + labelPadding();
95 96 width = qMax(width, base.width());
96 97 height += base.height();
97 98 sh = QSizeF(width, height);
98 99 break;
99 100 case Qt::PreferredSize: {
100 101
101 102 for (int i = 0; i < ticksList.size(); ++i) {
102 103 QRectF rect = fn.boundingRect(ticksList.at(i));
103 104 width += rect.width();
104 105 height = qMax(rect.height() + labelPadding(), height);
105 106 }
106 107 width = qMax(width, base.width());
107 108 height += base.height();
108 109 sh = QSizeF(width, height);
109 110 break;
110 111 }
111 112 default:
112 113 break;
113 114 }
114 115
115 116 return sh;
116 117 }
117 118
118 119 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,118 +1,118
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 "chartcategoryaxisy_p.h"
22 22 #include "qcategoryaxis.h"
23 23 #include "qabstractaxis.h"
24 24 #include "chartpresenter_p.h"
25 25 #include <QGraphicsLayout>
26 26 #include <QFontMetrics>
27 27 #include <qmath.h>
28 #include <QDebug>
28 29
29 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 31
31 32 ChartCategoryAxisY::ChartCategoryAxisY(QCategoryAxis *axis, ChartPresenter *presenter)
32 33 : VerticalAxis(axis, presenter, true),
33 34 m_axis(axis)
34 35 {
35 36 }
36 37
37 38 ChartCategoryAxisY::~ChartCategoryAxisY()
38 39 {
39 40 }
40 41
41 42 QVector<qreal> ChartCategoryAxisY::calculateLayout() const
42 43 {
43 44 int tickCount = m_axis->categoriesLabels().count() + 1;
44 45 QVector<qreal> points;
45 46
46 47 if (tickCount < 2)
47 48 return points;
48 49
49 50 const QRectF &gridRect = gridGeometry();
50 qreal range = m_axis->max() - m_axis->min();
51 qreal range = max() - min();
51 52 if (range > 0) {
52 53 points.resize(tickCount);
53 54 qreal scale = gridRect.height() / range;
54 55 for (int i = 0; i < tickCount; ++i) {
55 56 if (i < tickCount - 1) {
56 int y = -(m_axis->startValue(m_axis->categoriesLabels().at(i)) - m_axis->min()) * scale + gridRect.bottom();
57 int y = -(m_axis->startValue(m_axis->categoriesLabels().at(i)) - min()) * scale + gridRect.bottom();
57 58 points[i] = y;
58 59 } else {
59 int y = -(m_axis->endValue(m_axis->categoriesLabels().at(i - 1)) - m_axis->min()) * scale + gridRect.bottom();
60 int y = -(m_axis->endValue(m_axis->categoriesLabels().at(i - 1)) - min()) * scale + gridRect.bottom();
60 61 points[i] = y;
61 62 }
62 63 }
63 64 }
64 65
65 66 return points;
66 67 }
67 68
68 69 void ChartCategoryAxisY::updateGeometry()
69 70 {
70 71 setLabels(m_axis->categoriesLabels() << "");
71 72 VerticalAxis::updateGeometry();
72 73 }
73 74
74 75 void ChartCategoryAxisY::handleAxisUpdated()
75 76 {
76 77 updateGeometry();
77 78 ChartAxis::handleAxisUpdated();
78 79 }
79 80
80 81 QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
81 82 {
82 83 Q_UNUSED(constraint)
83 84
84 85 QFontMetrics fn(font());
85 86 QSizeF sh;
86 87 QSizeF base = ChartAxis::sizeHint(which, constraint);
87 QStringList ticksList; //TODO::
88 QStringList ticksList = m_axis->categoriesLabels();
88 89 qreal width = 0;
89 90 qreal height = 0;
90 91
91 92 switch (which) {
92 93 case Qt::MinimumSize:
93 94 width = fn.boundingRect("...").width() + labelPadding();
94 95 height = fn.height();
95 width = qMax(width, base.width());
96 height += base.height();
96 width += base.width();
97 height = qMax(height, base.height());;
97 98 sh = QSizeF(width, height);
98 99 break;
99 100 case Qt::PreferredSize: {
100 101
101 102 for (int i = 0; i < ticksList.size(); ++i) {
102 103 QRectF rect = fn.boundingRect(ticksList.at(i));
104 width = qMax(rect.width() + labelPadding() + 1, width);
103 105 height += rect.height();
104 width = qMax(rect.width() + labelPadding(), width);
105 106 }
106 107 height = qMax(height, base.height());
107 108 width += base.width();
108 109 sh = QSizeF(width, height);
109 110 break;
110 111 }
111 112 default:
112 113 break;
113 114 }
114
115 115 return sh;
116 116 }
117 117
118 118 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now