##// END OF EJS Templates
QLogValueAxis number of labels calculation fixed
Marek Rosa -
r2370:6a9743cf4397
parent child
Show More
@@ -1,137 +1,135
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 "chartlogvalueaxisx_p.h"
22 22 #include "chartpresenter_p.h"
23 23 #include "qlogvalueaxis.h"
24 24 #include "chartlayout_p.h"
25 25 #include <QGraphicsLayout>
26 26 #include <QFontMetrics>
27 27 #include <qmath.h>
28 28 #include <QDebug>
29 29
30 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
31 31
32 32 ChartLogValueAxisX::ChartLogValueAxisX(QLogValueAxis *axis, QGraphicsItem* item)
33 33 : HorizontalAxis(axis, item),
34 34 m_axis(axis)
35 35 {
36 36 QObject::connect(m_axis,SIGNAL(baseChanged(qreal)),this, SLOT(handleBaseChanged(qreal)));
37 37 QObject::connect(m_axis,SIGNAL(labelFormatChanged(QString)),this, SLOT(handleLabelFormatChanged(QString)));
38 38 }
39 39
40 40 ChartLogValueAxisX::~ChartLogValueAxisX()
41 41 {
42 42 }
43 43
44 44 QVector<qreal> ChartLogValueAxisX::calculateLayout() const
45 45 {
46 46 QVector<qreal> points;
47 47
48 48 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
49 49 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
50 50 qreal leftEdge = logMin < logMax ? logMin : logMax;
51 51 qreal ceilEdge = ceil(leftEdge);
52 52 int tickCount = qAbs(qRound(logMax - logMin));
53 53 tickCount++;
54 54
55 55 points.resize(tickCount);
56 56 const QRectF &gridRect = gridGeometry();
57 57 const qreal deltaX = gridRect.width() / qAbs(logMax - logMin);
58 58 for (int i = 0; i < tickCount; ++i)
59 59 points[i] = (ceilEdge + i) * deltaX - leftEdge * deltaX + gridRect.left();
60 60
61 61 return points;
62 62 }
63 63
64 64 void ChartLogValueAxisX::updateGeometry()
65 65 {
66 66 const QVector<qreal>& layout = ChartAxis::layout();
67 67 if (layout.isEmpty())
68 68 return;
69 69 setLabels(createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), layout.size(), m_axis->labelFormat()));
70 70 HorizontalAxis::updateGeometry();
71 71 }
72 72
73 73 void ChartLogValueAxisX::handleBaseChanged(qreal base)
74 74 {
75 75 Q_UNUSED(base);
76 76 QGraphicsLayoutItem::updateGeometry();
77 77 if(presenter()) presenter()->layout()->invalidate();
78 78 }
79 79
80 80 void ChartLogValueAxisX::handleLabelFormatChanged(const QString &format)
81 81 {
82 82 Q_UNUSED(format);
83 83 QGraphicsLayoutItem::updateGeometry();
84 84 if(presenter()) presenter()->layout()->invalidate();
85 85 }
86 86
87 87 QSizeF ChartLogValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
88 88 {
89 89 Q_UNUSED(constraint)
90 90
91 91 QFontMetrics fn(font());
92 92 QSizeF sh;
93 93
94 94 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
95 95 QStringList ticksList;
96 96 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
97 97 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
98 int tickCount = qAbs(qRound(logMax - logMin));
99 tickCount++;
100
98 int tickCount = qAbs(ceil(logMax) - ceil(logMin));
101 99 if (m_axis->max() > m_axis->min() && tickCount > 1)
102 100 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
103 101 else
104 102 ticksList.append(QString(" "));
105 103 qreal width = 0;
106 104 qreal height = 0;
107 105
108 106
109 107 switch (which) {
110 108 case Qt::MinimumSize:{
111 109 int count = qMax(ticksList.last().count(),ticksList.first().count());
112 110 width = fn.averageCharWidth() * count;
113 111 height = fn.height() + labelPadding();
114 112 width = qMax(width,base.width());
115 113 height += base.height();
116 114 sh = QSizeF(width,height);
117 115 break;
118 116 }
119 117 case Qt::PreferredSize: {
120 118 int count = qMax(ticksList.last().count(),ticksList.first().count());
121 119 width=fn.averageCharWidth() * count;
122 120 height=fn.height()+labelPadding();
123 121 width=qMax(width,base.width());
124 122 height+=base.height();
125 123 sh = QSizeF(width,height);
126 124 break;
127 125 }
128 126 default:
129 127 break;
130 128 }
131 129
132 130 return sh;
133 131 }
134 132
135 133 #include "moc_chartlogvalueaxisx_p.cpp"
136 134
137 135 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,133 +1,135
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 "chartlogvalueaxisy_p.h"
22 22 #include "chartpresenter_p.h"
23 23 #include "qlogvalueaxis.h"
24 24 #include "chartlayout_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 ChartLogValueAxisY::ChartLogValueAxisY(QLogValueAxis *axis, QGraphicsItem* item)
32 33 : VerticalAxis(axis, item),
33 34 m_axis(axis)
34 35 {
35 36 QObject::connect(m_axis, SIGNAL(baseChanged(qreal)),this, SLOT(handleBaseChanged(qreal)));
36 37 QObject::connect(m_axis,SIGNAL(labelFormatChanged(QString)),this, SLOT(handleLabelFormatChanged(QString)));
37 38 }
38 39
39 40 ChartLogValueAxisY::~ChartLogValueAxisY()
40 41 {
41 42 }
42 43
43 44 QVector<qreal> ChartLogValueAxisY::calculateLayout() const
44 45 {
45 46 QVector<qreal> points;
46 47 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
47 48 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
48 49 qreal leftEdge = logMin < logMax ? logMin : logMax;
49 50 qreal ceilEdge = ceil(leftEdge);
50 51 int tickCount = qAbs(qRound(logMax - logMin));
51 52 tickCount++;
52 53
53 54 points.resize(tickCount);
54 55 const QRectF &gridRect = gridGeometry();
55 56 const qreal deltaY = gridRect.height() / qAbs(logMax - logMin);
56 57 for (int i = 0; i < tickCount; ++i)
57 58 points[i] = (ceilEdge + i) * -deltaY - leftEdge * -deltaY + gridRect.bottom();
58 59
59 60 return points;
60 61 }
61 62
62 63
63 64 void ChartLogValueAxisY::updateGeometry()
64 65 {
65 66 const QVector<qreal> &layout = ChartAxis::layout();
66 67 if (layout.isEmpty())
67 68 return;
68 69 setLabels(createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), layout.size(), m_axis->labelFormat()));
69 70 VerticalAxis::updateGeometry();
70 71 }
71 72
72 73 void ChartLogValueAxisY::handleBaseChanged(qreal base)
73 74 {
74 75 Q_UNUSED(base);
75 76 QGraphicsLayoutItem::updateGeometry();
76 77 if(presenter()) presenter()->layout()->invalidate();
77 78 }
78 79
79 80 void ChartLogValueAxisY::handleLabelFormatChanged(const QString &format)
80 81 {
81 82 Q_UNUSED(format);
82 83 QGraphicsLayoutItem::updateGeometry();
83 84 if(presenter()) presenter()->layout()->invalidate();
84 85 }
85 86
86 87 QSizeF ChartLogValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
87 88 {
88 89 Q_UNUSED(constraint)
89 90
90 91 QFontMetrics fn(font());
91 92 QSizeF sh;
92 93
93 94 QSizeF base = VerticalAxis::sizeHint(which, constraint);
94 95 QStringList ticksList;
95 96 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
96 97 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
97 int tickCount = qAbs(qRound(logMax - logMin));
98 tickCount++;
98 int tickCount = qAbs(ceil(logMax) - ceil(logMin));
99 99 if (m_axis->max() > m_axis->min() && tickCount > 1)
100 100 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
101 101 else
102 102 ticksList.append(QString(" "));
103 qDebug() << ticksList;
104 qDebug() << tickCount;
103 105 qreal width = 0;
104 106 qreal height = 0;
105 107
106 108 switch (which) {
107 109 case Qt::MinimumSize: {
108 110 width = fn.boundingRect("...").width() + labelPadding();
109 111 width += base.width();
110 112 height = fn.height();
111 113 height = qMax(height,base.height());
112 114 sh = QSizeF(width,height);
113 115 break;
114 116 }
115 117 case Qt::PreferredSize: {
116 int count = qMax(ticksList.first().count() , ticksList.last().count());
118 int count = qMax(ticksList.first().count(), ticksList.last().count());
117 119 width = count*fn.averageCharWidth() + labelPadding() + 2; //two pixels of tolerance
118 120 width += base.width();
119 121 height = fn.height() * ticksList.count();
120 122 height = qMax(height,base.height());
121 123 sh = QSizeF(width,height);
122 124 break;
123 125 }
124 126 default:
125 127 break;
126 128 }
127 129
128 130 return sh;
129 131 }
130 132
131 133 #include "moc_chartlogvalueaxisy_p.cpp"
132 134
133 135 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now