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