##// END OF EJS Templates
Fix QLogValueAxis update...
Titta Heikkala -
r2758:39be6d68b7b8
parent child
Show More
@@ -1,137 +1,137
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2014 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and Digia.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include <private/chartlogvalueaxisx_p.h>
20 20 #include <private/chartpresenter_p.h>
21 21 #include <QtCharts/QLogValueAxis>
22 22 #include <private/abstractchartlayout_p.h>
23 23 #include <QtWidgets/QGraphicsLayout>
24 24 #include <QtCore/QtMath>
25 25 #include <QtCore/QDebug>
26 26
27 27 QT_CHARTS_BEGIN_NAMESPACE
28 28
29 29 ChartLogValueAxisX::ChartLogValueAxisX(QLogValueAxis *axis, QGraphicsItem *item)
30 30 : HorizontalAxis(axis, item),
31 31 m_axis(axis)
32 32 {
33 33 QObject::connect(m_axis, SIGNAL(baseChanged(qreal)), this, SLOT(handleBaseChanged(qreal)));
34 34 QObject::connect(m_axis, SIGNAL(labelFormatChanged(QString)), this, SLOT(handleLabelFormatChanged(QString)));
35 35 }
36 36
37 37 ChartLogValueAxisX::~ChartLogValueAxisX()
38 38 {
39 39 }
40 40
41 41 QVector<qreal> ChartLogValueAxisX::calculateLayout() const
42 42 {
43 43 QVector<qreal> points;
44 44
45 45 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
46 46 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
47 47 qreal leftEdge = logMin < logMax ? logMin : logMax;
48 48 qreal ceilEdge = ceil(leftEdge);
49 49 int tickCount = qAbs(ceil(logMax) - ceil(logMin));
50 50
51 points.resize(tickCount);
51 points.resize(tickCount + 1);
52 52 const QRectF &gridRect = gridGeometry();
53 53 const qreal deltaX = gridRect.width() / qAbs(logMax - logMin);
54 for (int i = 0; i < tickCount; ++i)
54 for (int i = 0; i <= tickCount; ++i)
55 55 points[i] = (ceilEdge + qreal(i)) * deltaX - leftEdge * deltaX + gridRect.left();
56 56
57 57 return points;
58 58 }
59 59
60 60 void ChartLogValueAxisX::updateGeometry()
61 61 {
62 62 const QVector<qreal>& layout = ChartAxisElement::layout();
63 63 if (layout.isEmpty())
64 64 return;
65 65 setLabels(createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), layout.size(), m_axis->labelFormat()));
66 66 HorizontalAxis::updateGeometry();
67 67 }
68 68
69 69 void ChartLogValueAxisX::handleBaseChanged(qreal base)
70 70 {
71 71 Q_UNUSED(base);
72 72 QGraphicsLayoutItem::updateGeometry();
73 73 if(presenter()) presenter()->layout()->invalidate();
74 74 }
75 75
76 76 void ChartLogValueAxisX::handleLabelFormatChanged(const QString &format)
77 77 {
78 78 Q_UNUSED(format);
79 79 QGraphicsLayoutItem::updateGeometry();
80 80 if(presenter()) presenter()->layout()->invalidate();
81 81 }
82 82
83 83 QSizeF ChartLogValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
84 84 {
85 85 Q_UNUSED(constraint)
86 86
87 87 QSizeF sh;
88 88
89 89 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
90 90 QStringList ticksList;
91 91 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
92 92 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
93 93 int tickCount = qAbs(ceil(logMax) - ceil(logMin));
94 94 if (m_axis->max() > m_axis->min() && tickCount > 0)
95 95 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
96 96 else
97 97 ticksList.append(QStringLiteral(" "));
98 98 // Width of horizontal axis sizeHint indicates the maximum distance labels can extend past
99 99 // first and last ticks. Base width is irrelevant.
100 100 qreal width = 0;
101 101 qreal height = 0;
102 102
103 103 switch (which) {
104 104 case Qt::MinimumSize:{
105 105 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(),
106 106 QStringLiteral("..."),
107 107 axis()->labelsAngle());
108 108 width = boundingRect.width() / 2.0;
109 109 height = boundingRect.height() + labelPadding() + base.height() + 1.0;
110 110 sh = QSizeF(width, height);
111 111 break;
112 112 }
113 113 case Qt::PreferredSize: {
114 114 qreal labelHeight = 0.0;
115 115 qreal firstWidth = -1.0;
116 116 foreach (const QString& s, ticksList) {
117 117 QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
118 118 labelHeight = qMax(rect.height(), labelHeight);
119 119 width = rect.width();
120 120 if (firstWidth < 0.0)
121 121 firstWidth = width;
122 122 }
123 123 height = labelHeight + labelPadding() + base.height() + 1.0;
124 124 width = qMax(width, firstWidth) / 2.0;
125 125 sh = QSizeF(width, height);
126 126 break;
127 127 }
128 128 default:
129 129 break;
130 130 }
131 131
132 132 return sh;
133 133 }
134 134
135 135 #include "moc_chartlogvalueaxisx_p.cpp"
136 136
137 137 QT_CHARTS_END_NAMESPACE
@@ -1,137 +1,137
1 1 /****************************************************************************
2 2 **
3 3 ** Copyright (C) 2014 Digia Plc
4 4 ** All rights reserved.
5 5 ** For any questions to Digia, please use contact form at http://qt.io
6 6 **
7 7 ** This file is part of the Qt Charts module.
8 8 **
9 9 ** Licensees holding valid commercial license for Qt may use this file in
10 10 ** accordance with the Qt License Agreement provided with the Software
11 11 ** or, alternatively, in accordance with the terms contained in a written
12 12 ** agreement between you and Digia.
13 13 **
14 14 ** If you have questions regarding the use of this file, please use
15 15 ** contact form at http://qt.io
16 16 **
17 17 ****************************************************************************/
18 18
19 19 #include <private/chartlogvalueaxisy_p.h>
20 20 #include <private/chartpresenter_p.h>
21 21 #include <QtCharts/QLogValueAxis>
22 22 #include <private/abstractchartlayout_p.h>
23 23 #include <QtWidgets/QGraphicsLayout>
24 24 #include <QtCore/QtMath>
25 25 #include <QtCore/QDebug>
26 26
27 27 QT_CHARTS_BEGIN_NAMESPACE
28 28
29 29 ChartLogValueAxisY::ChartLogValueAxisY(QLogValueAxis *axis, QGraphicsItem *item)
30 30 : VerticalAxis(axis, item),
31 31 m_axis(axis)
32 32 {
33 33 QObject::connect(m_axis, SIGNAL(baseChanged(qreal)), this, SLOT(handleBaseChanged(qreal)));
34 34 QObject::connect(m_axis, SIGNAL(labelFormatChanged(QString)), this, SLOT(handleLabelFormatChanged(QString)));
35 35 }
36 36
37 37 ChartLogValueAxisY::~ChartLogValueAxisY()
38 38 {
39 39 }
40 40
41 41 QVector<qreal> ChartLogValueAxisY::calculateLayout() const
42 42 {
43 43 QVector<qreal> points;
44 44 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
45 45 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
46 46 qreal leftEdge = logMin < logMax ? logMin : logMax;
47 47 qreal ceilEdge = ceil(leftEdge);
48 48 int tickCount = qAbs(ceil(logMax) - ceil(logMin));
49 49
50 points.resize(tickCount);
50 points.resize(tickCount + 1);
51 51 const QRectF &gridRect = gridGeometry();
52 52 const qreal deltaY = gridRect.height() / qAbs(logMax - logMin);
53 for (int i = 0; i < tickCount; ++i)
53 for (int i = 0; i <= tickCount; ++i)
54 54 points[i] = (ceilEdge + qreal(i)) * -deltaY - leftEdge * -deltaY + gridRect.bottom();
55 55
56 56 return points;
57 57 }
58 58
59 59
60 60 void ChartLogValueAxisY::updateGeometry()
61 61 {
62 62 const QVector<qreal> &layout = ChartAxisElement::layout();
63 63 if (layout.isEmpty())
64 64 return;
65 65 setLabels(createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), layout.size(), m_axis->labelFormat()));
66 66 VerticalAxis::updateGeometry();
67 67 }
68 68
69 69 void ChartLogValueAxisY::handleBaseChanged(qreal base)
70 70 {
71 71 Q_UNUSED(base);
72 72 QGraphicsLayoutItem::updateGeometry();
73 73 if(presenter()) presenter()->layout()->invalidate();
74 74 }
75 75
76 76 void ChartLogValueAxisY::handleLabelFormatChanged(const QString &format)
77 77 {
78 78 Q_UNUSED(format);
79 79 QGraphicsLayoutItem::updateGeometry();
80 80 if(presenter()) presenter()->layout()->invalidate();
81 81 }
82 82
83 83 QSizeF ChartLogValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
84 84 {
85 85 Q_UNUSED(constraint)
86 86
87 87 QSizeF sh;
88 88
89 89 QSizeF base = VerticalAxis::sizeHint(which, constraint);
90 90 QStringList ticksList;
91 91 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
92 92 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
93 93 int tickCount = qAbs(ceil(logMax) - ceil(logMin));
94 94 if (m_axis->max() > m_axis->min() && tickCount > 0)
95 95 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
96 96 else
97 97 ticksList.append(QStringLiteral(" "));
98 98 qreal width = 0;
99 99 // Height of vertical axis sizeHint indicates the maximum distance labels can extend past
100 100 // first and last ticks. Base height is irrelevant.
101 101 qreal height = 0;
102 102
103 103 switch (which) {
104 104 case Qt::MinimumSize: {
105 105 QRectF boundingRect = ChartPresenter::textBoundingRect(axis()->labelsFont(),
106 106 QStringLiteral("..."),
107 107 axis()->labelsAngle());
108 108 width = boundingRect.width() + labelPadding() + base.width() + 1.0;
109 109 height = boundingRect.height() / 2.0;
110 110 sh = QSizeF(width, height);
111 111 break;
112 112 }
113 113 case Qt::PreferredSize: {
114 114 qreal labelWidth = 0.0;
115 115 qreal firstHeight = -1.0;
116 116 foreach (const QString& s, ticksList) {
117 117 QRectF rect = ChartPresenter::textBoundingRect(axis()->labelsFont(), s, axis()->labelsAngle());
118 118 labelWidth = qMax(rect.width(), labelWidth);
119 119 height = rect.height();
120 120 if (firstHeight < 0.0)
121 121 firstHeight = height;
122 122 }
123 123 width = labelWidth + labelPadding() + base.width() + 2.0; //two pixels of tolerance
124 124 height = qMax(height, firstHeight) / 2.0;
125 125 sh = QSizeF(width, height);
126 126 break;
127 127 }
128 128 default:
129 129 break;
130 130 }
131 131
132 132 return sh;
133 133 }
134 134
135 135 #include "moc_chartlogvalueaxisy_p.cpp"
136 136
137 137 QT_CHARTS_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now