##// END OF EJS Templates
Fixes categoryaxis sizeHints
Michal Klocek -
r2136:44641cd4faa0
parent child
Show More
1 NO CONTENT: modified file
NO CONTENT: modified file
@@ -1,118 +1,119
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, ChartPresenter *presenter)
31 ChartCategoryAxisX::ChartCategoryAxisX(QCategoryAxis *axis, ChartPresenter *presenter)
32 : HorizontalAxis(axis, presenter, true),
32 : HorizontalAxis(axis, presenter, 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 = m_axis->max() - m_axis->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)) - m_axis->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)) - m_axis->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 setLabels(m_axis->categoriesLabels() << "");
71 setLabels(m_axis->categoriesLabels() << "");
71 HorizontalAxis::updateGeometry();
72 HorizontalAxis::updateGeometry();
72 }
73 }
73
74
74 void ChartCategoryAxisX::handleAxisUpdated()
75 void ChartCategoryAxisX::handleAxisUpdated()
75 {
76 {
76 updateGeometry();
77 updateGeometry();
77 ChartAxis::handleAxisUpdated();
78 ChartAxis::handleAxisUpdated();
78 }
79 }
79
80
80 QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
81 QSizeF ChartCategoryAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
81 {
82 {
82 Q_UNUSED(constraint)
83 Q_UNUSED(constraint)
83
84
84 QFontMetrics fn(font());
85 QFontMetrics fn(font());
85 QSizeF sh;
86 QSizeF sh;
86 QSizeF base = ChartAxis::sizeHint(which, constraint);
87 QSizeF base = ChartAxis::sizeHint(which, constraint);
87 QStringList ticksList ; //TODO:
88 QStringList ticksList = m_axis->categoriesLabels();
88 qreal width = 0;
89 qreal width = 0;
89 qreal height = 0;
90 qreal height = 0;
90
91
91 switch (which) {
92 switch (which) {
92 case Qt::MinimumSize:
93 case Qt::MinimumSize:
93 width = fn.boundingRect("...").width();
94 width = fn.boundingRect("...").width();
94 height = fn.height() + labelPadding();
95 height = fn.height() + labelPadding();
95 width = qMax(width, base.width());
96 width = qMax(width, base.width());
96 height += base.height();
97 height += base.height();
97 sh = QSizeF(width, height);
98 sh = QSizeF(width, height);
98 break;
99 break;
99 case Qt::PreferredSize: {
100 case Qt::PreferredSize: {
100
101
101 for (int i = 0; i < ticksList.size(); ++i) {
102 for (int i = 0; i < ticksList.size(); ++i) {
102 QRectF rect = fn.boundingRect(ticksList.at(i));
103 QRectF rect = fn.boundingRect(ticksList.at(i));
103 width += rect.width();
104 width += rect.width();
104 height = qMax(rect.height() + labelPadding(), height);
105 height = qMax(rect.height() + labelPadding(), height);
105 }
106 }
106 width = qMax(width, base.width());
107 width = qMax(width, base.width());
107 height += base.height();
108 height += base.height();
108 sh = QSizeF(width, height);
109 sh = QSizeF(width, height);
109 break;
110 break;
110 }
111 }
111 default:
112 default:
112 break;
113 break;
113 }
114 }
114
115
115 return sh;
116 return sh;
116 }
117 }
117
118
118 QTCOMMERCIALCHART_END_NAMESPACE
119 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,118 +1,118
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
29
29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30
31
31 ChartCategoryAxisY::ChartCategoryAxisY(QCategoryAxis *axis, ChartPresenter *presenter)
32 ChartCategoryAxisY::ChartCategoryAxisY(QCategoryAxis *axis, ChartPresenter *presenter)
32 : VerticalAxis(axis, presenter, true),
33 : VerticalAxis(axis, presenter, true),
33 m_axis(axis)
34 m_axis(axis)
34 {
35 {
35 }
36 }
36
37
37 ChartCategoryAxisY::~ChartCategoryAxisY()
38 ChartCategoryAxisY::~ChartCategoryAxisY()
38 {
39 {
39 }
40 }
40
41
41 QVector<qreal> ChartCategoryAxisY::calculateLayout() const
42 QVector<qreal> ChartCategoryAxisY::calculateLayout() const
42 {
43 {
43 int tickCount = m_axis->categoriesLabels().count() + 1;
44 int tickCount = m_axis->categoriesLabels().count() + 1;
44 QVector<qreal> points;
45 QVector<qreal> points;
45
46
46 if (tickCount < 2)
47 if (tickCount < 2)
47 return points;
48 return points;
48
49
49 const QRectF &gridRect = gridGeometry();
50 const QRectF &gridRect = gridGeometry();
50 qreal range = m_axis->max() - m_axis->min();
51 qreal range = max() - min();
51 if (range > 0) {
52 if (range > 0) {
52 points.resize(tickCount);
53 points.resize(tickCount);
53 qreal scale = gridRect.height() / range;
54 qreal scale = gridRect.height() / range;
54 for (int i = 0; i < tickCount; ++i) {
55 for (int i = 0; i < tickCount; ++i) {
55 if (i < tickCount - 1) {
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 points[i] = y;
58 points[i] = y;
58 } else {
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 points[i] = y;
61 points[i] = y;
61 }
62 }
62 }
63 }
63 }
64 }
64
65
65 return points;
66 return points;
66 }
67 }
67
68
68 void ChartCategoryAxisY::updateGeometry()
69 void ChartCategoryAxisY::updateGeometry()
69 {
70 {
70 setLabels(m_axis->categoriesLabels() << "");
71 setLabels(m_axis->categoriesLabels() << "");
71 VerticalAxis::updateGeometry();
72 VerticalAxis::updateGeometry();
72 }
73 }
73
74
74 void ChartCategoryAxisY::handleAxisUpdated()
75 void ChartCategoryAxisY::handleAxisUpdated()
75 {
76 {
76 updateGeometry();
77 updateGeometry();
77 ChartAxis::handleAxisUpdated();
78 ChartAxis::handleAxisUpdated();
78 }
79 }
79
80
80 QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
81 QSizeF ChartCategoryAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
81 {
82 {
82 Q_UNUSED(constraint)
83 Q_UNUSED(constraint)
83
84
84 QFontMetrics fn(font());
85 QFontMetrics fn(font());
85 QSizeF sh;
86 QSizeF sh;
86 QSizeF base = ChartAxis::sizeHint(which, constraint);
87 QSizeF base = ChartAxis::sizeHint(which, constraint);
87 QStringList ticksList; //TODO::
88 QStringList ticksList = m_axis->categoriesLabels();
88 qreal width = 0;
89 qreal width = 0;
89 qreal height = 0;
90 qreal height = 0;
90
91
91 switch (which) {
92 switch (which) {
92 case Qt::MinimumSize:
93 case Qt::MinimumSize:
93 width = fn.boundingRect("...").width() + labelPadding();
94 width = fn.boundingRect("...").width() + labelPadding();
94 height = fn.height();
95 height = fn.height();
95 width = qMax(width, base.width());
96 width += base.width();
96 height += base.height();
97 height = qMax(height, base.height());;
97 sh = QSizeF(width, height);
98 sh = QSizeF(width, height);
98 break;
99 break;
99 case Qt::PreferredSize: {
100 case Qt::PreferredSize: {
100
101
101 for (int i = 0; i < ticksList.size(); ++i) {
102 for (int i = 0; i < ticksList.size(); ++i) {
102 QRectF rect = fn.boundingRect(ticksList.at(i));
103 QRectF rect = fn.boundingRect(ticksList.at(i));
104 width = qMax(rect.width() + labelPadding() + 1, width);
103 height += rect.height();
105 height += rect.height();
104 width = qMax(rect.width() + labelPadding(), width);
105 }
106 }
106 height = qMax(height, base.height());
107 height = qMax(height, base.height());
107 width += base.width();
108 width += base.width();
108 sh = QSizeF(width, height);
109 sh = QSizeF(width, height);
109 break;
110 break;
110 }
111 }
111 default:
112 default:
112 break;
113 break;
113 }
114 }
114
115 return sh;
115 return sh;
116 }
116 }
117
117
118 QTCOMMERCIALCHART_END_NAMESPACE
118 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now