##// END OF EJS Templates
Fixes missing layout updated on ticks,base and format calls
Michal Klocek -
r2333:158c8aa5716a
parent child
Show More
@@ -1,134 +1,137
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 #include <QDebug>
28 29
29 30 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 31
31 32 ChartLogValueAxisX::ChartLogValueAxisX(QLogValueAxis *axis, QGraphicsItem* item)
32 33 : HorizontalAxis(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 ChartLogValueAxisX::~ChartLogValueAxisX()
40 41 {
41 42 }
42 43
43 44 QVector<qreal> ChartLogValueAxisX::calculateLayout() const
44 45 {
45 46 QVector<qreal> points;
46 47
47 48 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
48 49 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
49 50 qreal leftEdge = logMin < logMax ? logMin : logMax;
50 51 qreal ceilEdge = ceil(leftEdge);
51 52 int tickCount = qAbs(qRound(logMax - logMin));
52 53 tickCount++;
53 54
54 55 points.resize(tickCount);
55 56 const QRectF &gridRect = gridGeometry();
56 57 const qreal deltaX = gridRect.width() / qAbs(logMax - logMin);
57 58 for (int i = 0; i < tickCount; ++i)
58 59 points[i] = (ceilEdge + i) * deltaX - leftEdge * deltaX + gridRect.left();
59 60
60 61 return points;
61 62 }
62 63
63 64 void ChartLogValueAxisX::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 HorizontalAxis::updateGeometry();
70 71 }
71 72
72 73 void ChartLogValueAxisX::handleBaseChanged(qreal base)
73 74 {
74 75 Q_UNUSED(base);
76 QGraphicsLayoutItem::updateGeometry();
75 77 if(presenter()) presenter()->layout()->invalidate();
76 78 }
77 79
78 80 void ChartLogValueAxisX::handleLabelFormatChanged(const QString &format)
79 81 {
80 82 Q_UNUSED(format);
83 QGraphicsLayoutItem::updateGeometry();
81 84 if(presenter()) presenter()->layout()->invalidate();
82 85 }
83 86
84 87 QSizeF ChartLogValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
85 88 {
86 89 Q_UNUSED(constraint)
87 90
88 91 QFontMetrics fn(font());
89 92 QSizeF sh;
90 93
91 94 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
92 95 QStringList ticksList;
93 96 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
94 97 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
95 98 int tickCount = qAbs(qRound(logMax - logMin));
96 99 tickCount++;
97 100
98 101 if (m_axis->max() > m_axis->min() && tickCount > 1)
99 102 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
100 103 else
101 104 ticksList.append(QString(" "));
102 105 qreal width = 0;
103 106 qreal height = 0;
104 107
105 108
106 109 switch (which) {
107 110 case Qt::MinimumSize:{
108 111 int count = qMax(ticksList.last().count(),ticksList.first().count());
109 112 width = fn.averageCharWidth() * count;
110 113 height = fn.height() + labelPadding();
111 114 width = qMax(width,base.width());
112 115 height += base.height();
113 116 sh = QSizeF(width,height);
114 117 break;
115 118 }
116 119 case Qt::PreferredSize: {
117 120 int count = qMax(ticksList.last().count(),ticksList.first().count());
118 121 width=fn.averageCharWidth() * count;
119 122 height=fn.height()+labelPadding();
120 123 width=qMax(width,base.width());
121 124 height+=base.height();
122 125 sh = QSizeF(width,height);
123 126 break;
124 127 }
125 128 default:
126 129 break;
127 130 }
128 131
129 132 return sh;
130 133 }
131 134
132 135 #include "moc_chartlogvalueaxisx_p.cpp"
133 136
134 137 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,131 +1,133
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
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30
31 31 ChartLogValueAxisY::ChartLogValueAxisY(QLogValueAxis *axis, QGraphicsItem* item)
32 32 : VerticalAxis(axis, item),
33 33 m_axis(axis)
34 34 {
35 35 QObject::connect(m_axis, SIGNAL(baseChanged(qreal)),this, SLOT(handleBaseChanged(qreal)));
36 36 QObject::connect(m_axis,SIGNAL(labelFormatChanged(QString)),this, SLOT(handleLabelFormatChanged(QString)));
37 37 }
38 38
39 39 ChartLogValueAxisY::~ChartLogValueAxisY()
40 40 {
41 41 }
42 42
43 43 QVector<qreal> ChartLogValueAxisY::calculateLayout() const
44 44 {
45 45 QVector<qreal> points;
46 46 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
47 47 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
48 48 qreal leftEdge = logMin < logMax ? logMin : logMax;
49 49 qreal ceilEdge = ceil(leftEdge);
50 50 int tickCount = qAbs(qRound(logMax - logMin));
51 51 tickCount++;
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 QGraphicsLayoutItem::updateGeometry();
75 76 if(presenter()) presenter()->layout()->invalidate();
76 77 }
77 78
78 79 void ChartLogValueAxisY::handleLabelFormatChanged(const QString &format)
79 80 {
80 81 Q_UNUSED(format);
82 QGraphicsLayoutItem::updateGeometry();
81 83 if(presenter()) presenter()->layout()->invalidate();
82 84 }
83 85
84 86 QSizeF ChartLogValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
85 87 {
86 88 Q_UNUSED(constraint)
87 89
88 90 QFontMetrics fn(font());
89 91 QSizeF sh;
90 92
91 93 QSizeF base = VerticalAxis::sizeHint(which, constraint);
92 94 QStringList ticksList;
93 95 qreal logMax = log10(m_axis->max()) / log10(m_axis->base());
94 96 qreal logMin = log10(m_axis->min()) / log10(m_axis->base());
95 97 int tickCount = qAbs(qRound(logMax - logMin));
96 98 tickCount++;
97 99 if (m_axis->max() > m_axis->min() && tickCount > 1)
98 100 ticksList = createLogValueLabels(m_axis->min(), m_axis->max(), m_axis->base(), tickCount, m_axis->labelFormat());
99 101 else
100 102 ticksList.append(QString(" "));
101 103 qreal width = 0;
102 104 qreal height = 0;
103 105
104 106 switch (which) {
105 107 case Qt::MinimumSize: {
106 108 width = fn.boundingRect("...").width() + labelPadding();
107 109 width += base.width();
108 110 height = fn.height();
109 111 height = qMax(height,base.height());
110 112 sh = QSizeF(width,height);
111 113 break;
112 114 }
113 115 case Qt::PreferredSize: {
114 116 int count = qMax(ticksList.first().count() , ticksList.last().count());
115 117 width = count*fn.averageCharWidth() + labelPadding() + 2; //two pixels of tolerance
116 118 width += base.width();
117 119 height = fn.height() * ticksList.count();
118 120 height = qMax(height,base.height());
119 121 sh = QSizeF(width,height);
120 122 break;
121 123 }
122 124 default:
123 125 break;
124 126 }
125 127
126 128 return sh;
127 129 }
128 130
129 131 #include "moc_chartlogvalueaxisy_p.cpp"
130 132
131 133 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,325 +1,325
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 "qlogvalueaxis.h"
22 22 #include "qlogvalueaxis_p.h"
23 23 #include "chartlogvalueaxisx_p.h"
24 24 #include "chartlogvalueaxisy_p.h"
25 25 #include "abstractdomain_p.h"
26 26 #include <float.h>
27 27 #include <cmath>
28 28
29 29 QTCOMMERCIALCHART_BEGIN_NAMESPACE
30 30 /*!
31 31 \class QLogValueAxis
32 32 \brief The QLogValueAxis class is used for manipulating chart's axis.
33 33 \mainclass
34 34 */
35 35
36 36 /*!
37 37 \qmlclass DateTimeAxis QLogValueAxis
38 38 \brief The DateTimeAxis element is used for manipulating chart's axes
39 39 \inherits AbstractAxis
40 40 */
41 41
42 42 /*!
43 43 \property QLogValueAxis::min
44 44 Defines the minimum value on the axis.
45 45 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
46 46 */
47 47 /*!
48 48 \qmlproperty real ValuesAxis::min
49 49 Defines the minimum value on the axis.
50 50 When setting this property the max is adjusted if necessary, to ensure that the range remains valid.
51 51 */
52 52
53 53 /*!
54 54 \property QLogValueAxis::max
55 55 Defines the maximum value on the axis.
56 56 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
57 57 */
58 58 /*!
59 59 \qmlproperty real ValuesAxis::max
60 60 Defines the maximum value on the axis.
61 61 When setting this property the min is adjusted if necessary, to ensure that the range remains valid.
62 62 */
63 63
64 64 /*!
65 65 \fn void QLogValueAxis::minChanged(qreal min)
66 66 Axis emits signal when \a min of axis has changed.
67 67 */
68 68 /*!
69 69 \qmlsignal ValuesAxis::onMinChanged(qreal min)
70 70 Axis emits signal when \a min of axis has changed.
71 71 */
72 72
73 73 /*!
74 74 \fn void QLogValueAxis::maxChanged(qreal max)
75 75 Axis emits signal when \a max of axis has changed.
76 76 */
77 77 /*!
78 78 \qmlsignal ValuesAxis::onMaxChanged(qreal max)
79 79 Axis emits signal when \a max of axis has changed.
80 80 */
81 81
82 82 /*!
83 83 \fn void QLogValueAxis::rangeChanged(qreal min, qreal max)
84 84 Axis emits signal when \a min or \a max of axis has changed.
85 85 */
86 86
87 87 /*!
88 88 Constructs an axis object which is a child of \a parent.
89 89 */
90 90 QLogValueAxis::QLogValueAxis(QObject *parent) :
91 91 QAbstractAxis(*new QLogValueAxisPrivate(this), parent)
92 92 {
93 93
94 94 }
95 95
96 96 /*!
97 97 \internal
98 98 */
99 99 QLogValueAxis::QLogValueAxis(QLogValueAxisPrivate &d, QObject *parent) : QAbstractAxis(d, parent)
100 100 {
101 101
102 102 }
103 103
104 104 /*!
105 105 Destroys the object
106 106 */
107 107 QLogValueAxis::~QLogValueAxis()
108 108 {
109 109 Q_D(QLogValueAxis);
110 110 if (d->m_chart)
111 111 d->m_chart->removeAxis(this);
112 112 }
113 113
114 114 void QLogValueAxis::setMin(qreal min)
115 115 {
116 116 Q_D(QLogValueAxis);
117 117 setRange(min, qMax(d->m_max, min));
118 118 }
119 119
120 120 qreal QLogValueAxis::min() const
121 121 {
122 122 Q_D(const QLogValueAxis);
123 123 return d->m_min;
124 124 }
125 125
126 126 void QLogValueAxis::setMax(qreal max)
127 127 {
128 128 Q_D(QLogValueAxis);
129 129 setRange(qMin(d->m_min, max), max);
130 130 }
131 131
132 132 qreal QLogValueAxis::max() const
133 133 {
134 134 Q_D(const QLogValueAxis);
135 135 return d->m_max;
136 136 }
137 137
138 138 /*!
139 139 Sets range from \a min to \a max on the axis.
140 140 If min is greater than max then this function returns without making any changes.
141 141 */
142 142 void QLogValueAxis::setRange(qreal min, qreal max)
143 143 {
144 144 Q_D(QLogValueAxis);
145 145 bool changed = false;
146 146
147 147 if (min > max)
148 148 return;
149 149
150 150 if (min > 0) {
151 151 if (!qFuzzyCompare(d->m_min, min)) {
152 152 d->m_min = min;
153 153 changed = true;
154 154 emit minChanged(min);
155 155 }
156 156
157 157 if (!qFuzzyCompare(d->m_max, max)) {
158 158 d->m_max = max;
159 159 changed = true;
160 160 emit maxChanged(max);
161 161 }
162 162
163 163 if (changed) {
164 164 emit rangeChanged(min, max);
165 165 emit d->rangeChanged(min,max);
166 166 }
167 167 }
168 168 }
169 169
170 170 void QLogValueAxis::setLabelFormat(const QString &format)
171 171 {
172 172 Q_D(QLogValueAxis);
173 173 d->m_format = format;
174 174 emit labelFormatChanged(format);
175 175 }
176 176
177 177 QString QLogValueAxis::labelFormat() const
178 178 {
179 179 Q_D(const QLogValueAxis);
180 180 return d->m_format;
181 181 }
182 182
183 183 void QLogValueAxis::setBase(qreal base)
184 184 {
185 185 // check if base is correct
186 186 if (qFuzzyCompare(base, 1))
187 187 return;
188 188
189 189 if (base > 0) {
190 190 Q_D(QLogValueAxis);
191 191 d->m_base = base;
192 192 emit baseChanged(base);
193 193 }
194 194 }
195 195
196 196 qreal QLogValueAxis::base() const
197 197 {
198 198 Q_D(const QLogValueAxis);
199 199 return d->m_base;
200 200 }
201 201
202 202 /*!
203 203 Returns the type of the axis
204 204 */
205 205 QAbstractAxis::AxisType QLogValueAxis::type() const
206 206 {
207 207 return AxisTypeLogValue;
208 208 }
209 209
210 210 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
211 211
212 212 QLogValueAxisPrivate::QLogValueAxisPrivate(QLogValueAxis *q)
213 213 : QAbstractAxisPrivate(q),
214 214 m_min(1),
215 215 m_max(1),
216 216 m_base(10),
217 217 m_format(QString::null)
218 218 {
219 219 }
220 220
221 221 QLogValueAxisPrivate::~QLogValueAxisPrivate()
222 222 {
223 223
224 224 }
225 225
226 226 void QLogValueAxisPrivate::setMin(const QVariant &min)
227 227 {
228 228 Q_Q(QLogValueAxis);
229 229 bool ok;
230 230 qreal value = min.toReal(&ok);
231 231 if (ok)
232 232 q->setMin(value);
233 233 }
234 234
235 235 void QLogValueAxisPrivate::setMax(const QVariant &max)
236 236 {
237 237
238 238 Q_Q(QLogValueAxis);
239 239 bool ok;
240 240 qreal value = max.toReal(&ok);
241 241 if (ok)
242 242 q->setMax(value);
243 243 }
244 244
245 245 void QLogValueAxisPrivate::setRange(const QVariant &min, const QVariant &max)
246 246 {
247 247 Q_Q(QLogValueAxis);
248 248 bool ok1;
249 249 bool ok2;
250 250 qreal value1 = min.toReal(&ok1);
251 251 qreal value2 = max.toReal(&ok2);
252 252 if (ok1 && ok2)
253 253 q->setRange(value1, value2);
254 254 }
255 255
256 256 void QLogValueAxisPrivate::setRange(qreal min, qreal max)
257 257 {
258 258 Q_Q(QLogValueAxis);
259 259 bool changed = false;
260 260
261 261 if (min > max)
262 262 return;
263 263
264 264 if (min > 0) {
265 265 if (!qFuzzyCompare(m_min, min)) {
266 266 m_min = min;
267 267 changed = true;
268 268 emit q->minChanged(min);
269 269 }
270 270
271 271 if (!qFuzzyCompare(m_max, max)) {
272 272 m_max = max;
273 273 changed = true;
274 274 emit q->maxChanged(max);
275 275 }
276 276
277 277 if (changed) {
278 emit q->rangeChanged(min, max);
279 278 emit rangeChanged(min,max);
279 emit q->rangeChanged(min, max);
280 280 }
281 281 }
282 282 }
283 283
284 284 void QLogValueAxisPrivate::initializeGraphics(QGraphicsItem* parent)
285 285 {
286 286 Q_Q(QLogValueAxis);
287 287 ChartAxis* axis(0);
288 288 if (orientation() == Qt::Vertical)
289 289 axis = new ChartLogValueAxisY(q,parent);
290 290 if (orientation() == Qt::Horizontal)
291 291 axis = new ChartLogValueAxisX(q,parent);
292 292
293 293 m_item.reset(axis);
294 294 QAbstractAxisPrivate::initializeGraphics(parent);
295 295 }
296 296
297 297
298 298 void QLogValueAxisPrivate::initializeDomain(AbstractDomain *domain)
299 299 {
300 300 if (orientation() == Qt::Vertical) {
301 301 if(!qFuzzyCompare(m_max, m_min)) {
302 302 domain->setRangeY(m_min, m_max);
303 303 }
304 304 else if ( domain->minY() > 0) {
305 305 setRange(domain->minY(), domain->maxY());
306 306 } else {
307 307 domain->setRangeY(m_min, domain->maxY());
308 308 }
309 309 }
310 310 if (orientation() == Qt::Horizontal) {
311 311 if(!qFuzzyCompare(m_max, m_min)) {
312 312 domain->setRangeX(m_min, m_max);
313 313 }
314 314 else if (domain->minX() > 0){
315 315 setRange(domain->minX(), domain->maxX());
316 316 } else {
317 317 domain->setRangeX(m_min, domain->maxX());
318 318 }
319 319 }
320 320 }
321 321
322 322 #include "moc_qlogvalueaxis.cpp"
323 323 #include "moc_qlogvalueaxis_p.cpp"
324 324
325 325 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,129 +1,131
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 "chartvalueaxisx_p.h"
22 22 #include "qabstractaxis.h"
23 23 #include "chartpresenter_p.h"
24 24 #include "qvalueaxis.h"
25 25 #include "chartlayout_p.h"
26 26 #include <QGraphicsLayout>
27 27 #include <QFontMetrics>
28 28 #include <qmath.h>
29 29 #include <QDebug>
30 30
31 31
32 32 QTCOMMERCIALCHART_BEGIN_NAMESPACE
33 33
34 34 ChartValueAxisX::ChartValueAxisX(QValueAxis *axis, QGraphicsItem* item )
35 35 : HorizontalAxis(axis, item),
36 36 m_axis(axis)
37 37 {
38 38 QObject::connect(m_axis,SIGNAL(tickCountChanged(int)),this, SLOT(handleTickCountChanged(int)));
39 39 QObject::connect(m_axis,SIGNAL(labelFormatChanged(QString)),this, SLOT(handleLabelFormatChanged(QString)));
40 40 }
41 41
42 42 ChartValueAxisX::~ChartValueAxisX()
43 43 {
44 44 }
45 45
46 46 QVector<qreal> ChartValueAxisX::calculateLayout() const
47 47 {
48 48 int tickCount = m_axis->tickCount();
49 49
50 50 Q_ASSERT(tickCount >= 2);
51 51
52 52 QVector<qreal> points;
53 53 points.resize(tickCount);
54 54
55 55 const QRectF &gridRect = gridGeometry();
56 56 const qreal deltaX = gridRect.width() / (tickCount - 1);
57 57 for (int i = 0; i < tickCount; ++i) {
58 58 points[i] = i * deltaX + gridRect.left();
59 59 }
60 60 return points;
61 61 }
62 62
63 63 void ChartValueAxisX::updateGeometry()
64 64 {
65 65 const QVector<qreal>& layout = ChartAxis::layout();
66 66 if (layout.isEmpty())
67 67 return;
68 68 setLabels(createValueLabels(min(),max(),layout.size(),m_axis->labelFormat()));
69 69 HorizontalAxis::updateGeometry();
70 70 }
71 71
72 72 void ChartValueAxisX::handleTickCountChanged(int tick)
73 73 {
74 74 Q_UNUSED(tick);
75 QGraphicsLayoutItem::updateGeometry();
75 76 if(presenter()) presenter()->layout()->invalidate();
76 77 }
77 78
78 79 void ChartValueAxisX::handleLabelFormatChanged(const QString &format)
79 80 {
80 81 Q_UNUSED(format);
82 QGraphicsLayoutItem::updateGeometry();
81 83 if(presenter()) presenter()->layout()->invalidate();
82 84 }
83 85
84 86 QSizeF ChartValueAxisX::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
85 87 {
86 88 Q_UNUSED(constraint)
87 89
88 90 QFontMetrics fn(font());
89 91 QSizeF sh;
90 92
91 93 QSizeF base = HorizontalAxis::sizeHint(which, constraint);
92 94 QStringList ticksList = createValueLabels(min(),max(),m_axis->tickCount(),m_axis->labelFormat());
93 95 qreal width = 0;
94 96 qreal height = 0;
95 97
96 98 int count = 1;
97 99
98 100 if(!ticksList.empty()) {
99 101 count = qMax(ticksList.last().count(),ticksList.first().count());
100 102 }
101 103
102 104 switch (which) {
103 105 case Qt::MinimumSize:{
104 106 count = qMin(count,5);
105 107 width = fn.averageCharWidth() * count;
106 108 height = fn.height() + labelPadding();
107 109 width = qMax(width,base.width());
108 110 height += base.height();
109 111 sh = QSizeF(width,height);
110 112 break;
111 113 }
112 114 case Qt::PreferredSize:{
113 115 width=fn.averageCharWidth() * count;
114 116 height=fn.height()+labelPadding();
115 117 width=qMax(width,base.width());
116 118 height+=base.height();
117 119 sh = QSizeF(width,height);
118 120 break;
119 121 }
120 122 default:
121 123 break;
122 124 }
123 125
124 126 return sh;
125 127 }
126 128
127 129 #include "moc_chartvalueaxisx_p.cpp"
128 130
129 131 QTCOMMERCIALCHART_END_NAMESPACE
@@ -1,128 +1,130
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 "chartvalueaxisy_p.h"
22 22 #include "qabstractaxis.h"
23 23 #include "chartpresenter_p.h"
24 24 #include "qvalueaxis.h"
25 25 #include "chartlayout_p.h"
26 26 #include <QGraphicsLayout>
27 27 #include <QFontMetrics>
28 28 #include <qmath.h>
29 29 #include <QDebug>
30 30
31 31 QTCOMMERCIALCHART_BEGIN_NAMESPACE
32 32
33 33 ChartValueAxisY::ChartValueAxisY(QValueAxis *axis, QGraphicsItem* item)
34 34 : VerticalAxis(axis, item),
35 35 m_axis(axis)
36 36 {
37 37 QObject::connect(m_axis,SIGNAL(tickCountChanged(int)),this, SLOT(handleTickCountChanged(int)));
38 38 QObject::connect(m_axis,SIGNAL(labelFormatChanged(QString)),this, SLOT(handleLabelFormatChanged(QString)));
39 39 }
40 40
41 41 ChartValueAxisY::~ChartValueAxisY()
42 42 {
43 43 }
44 44
45 45 QVector<qreal> ChartValueAxisY::calculateLayout() const
46 46 {
47 47 int tickCount = m_axis->tickCount();
48 48
49 49 Q_ASSERT(tickCount >= 2);
50 50
51 51 QVector<qreal> points;
52 52 points.resize(tickCount);
53 53
54 54 const QRectF &gridRect = gridGeometry();
55 55
56 56 const qreal deltaY = gridRect.height() / (tickCount - 1);
57 57 for (int i = 0; i < tickCount; ++i) {
58 58 points[i] = i * -deltaY + gridRect.bottom();
59 59 }
60 60
61 61 return points;
62 62 }
63 63
64 64 void ChartValueAxisY::updateGeometry()
65 65 {
66 66 const QVector<qreal> &layout = ChartAxis::layout();
67 67 if (layout.isEmpty())
68 68 return;
69 69 setLabels(createValueLabels(min(),max(),layout.size(),m_axis->labelFormat()));
70 70 VerticalAxis::updateGeometry();
71 71 }
72 72
73 73 void ChartValueAxisY::handleTickCountChanged(int tick)
74 74 {
75 75 Q_UNUSED(tick);
76 QGraphicsLayoutItem::updateGeometry();
76 77 if(presenter()) presenter()->layout()->invalidate();
77 78 }
78 79
79 80 void ChartValueAxisY::handleLabelFormatChanged(const QString &format)
80 81 {
81 82 Q_UNUSED(format);
83 QGraphicsLayoutItem::updateGeometry();
82 84 if(presenter()) presenter()->layout()->invalidate();
83 85 }
84 86
85 87 QSizeF ChartValueAxisY::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
86 88 {
87 89 Q_UNUSED(constraint)
88 90
89 91 QFontMetrics fn(font());
90 92 QSizeF sh;
91 93 QSizeF base = VerticalAxis::sizeHint(which, constraint);
92 94 QStringList ticksList = createValueLabels(min(),max(),m_axis->tickCount(),m_axis->labelFormat());
93 95 qreal width = 0;
94 96 qreal height = 0;
95 97
96 98 int count = 1;
97 99
98 100 if(!ticksList.empty()){
99 101 count = qMax(ticksList.last().count(),ticksList.first().count());
100 102 }
101 103
102 104 switch (which) {
103 105 case Qt::MinimumSize: {
104 106 width = fn.boundingRect("...").width() + labelPadding();
105 107 width += base.width();
106 108 height = fn.height();
107 109 height = qMax(height,base.height());
108 110 sh = QSizeF(width,height);
109 111 break;
110 112 }
111 113 case Qt::PreferredSize:
112 114 {
113 115 width = count*fn.averageCharWidth() + labelPadding() + 2; //two pixels of tolerance
114 116 width += base.width();
115 117 height = fn.height() * ticksList.count();
116 118 height = qMax(height,base.height());
117 119 sh = QSizeF(width,height);
118 120 break;
119 121 }
120 122 default:
121 123 break;
122 124 }
123 125 return sh;
124 126 }
125 127
126 128 #include "moc_chartvalueaxisy_p.cpp"
127 129
128 130 QTCOMMERCIALCHART_END_NAMESPACE
General Comments 0
You need to be logged in to leave comments. Login now