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