summaryrefslogtreecommitdiffstats
path: root/src/datavisualization/axis/qlogvalue3daxisformatter.cpp
blob: 17ae447673f517c46b0f532d35f6d4597351526c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc
** All rights reserved.
** For any questions to Digia, please use contact form at http://qt.digia.com
**
** This file is part of the QtDataVisualization module.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.
**
** If you have questions regarding the use of this file, please use
** contact form at http://qt.digia.com
**
****************************************************************************/

#include "qlogvalue3daxisformatter_p.h"
#include "qvalue3daxis_p.h"
#include <QtCore/qmath.h>

QT_BEGIN_NAMESPACE_DATAVISUALIZATION

/*!
 * \class QLogValue3DAxisFormatter
 * \inmodule QtDataVisualization
 * \brief QLogValue3DAxisFormatter implements logarithmic value axis formatter.
 * \since Qt Data Visualization 1.1
 *
 * This class provides formatting rules for a logarithmic QValue3DAxis.
 *
 * When a QLogValue3DAxisFormatter is attached to a QValue3DAxis, the axis range
 * cannot include negative values or the zero.
 *
 * \sa QValue3DAxisFormatter
 */

/*!
 * \qmltype LogValueAxis3DFormatter
 * \inqmlmodule QtDataVisualization
 * \since QtDataVisualization 1.1
 * \ingroup datavisualization_qml
 * \instantiates QLogValue3DAxisFormatter
 * \brief LogValueAxis3DFormatter implements logarithmic value axis formatter.
 *
 * This type provides formatting rules for a logarithmic ValueAxis3D.
 * When a LogValueAxis3DFormatter is attached to a ValueAxis3D, the axis range
 * cannot include negative values or the zero.
 */

/*!
 * \qmlproperty real LogValueAxis3DFormatter::base
 *
 * The \a base of the logarithm used to map axis values. If the base is non-zero, the parent axis
 * segment count will be automatically adjusted whenever the axis formatter is recalculated.
 * If you want the range to be divided into equal segments like normal value axis, set this
 * property value to zero.
 *
 * The \a base has to be zero or positive value and not equal to one.
 * Defaults to ten.
 *
 * \sa ValueAxis3D::segmentCount
 */

/*!
 * \qmlproperty bool LogValueAxis3DFormatter::autoSubGrid
 *
 * If this property value is set to \c true, the parent axis sub-segment count is adjusted
 * automatically according to the base property value. The number of subsegments is set to
 * base value minus one, rounded down.
 * Defaults to \c true.
 *
 * \sa base, ValueAxis3D::subSegmentCount
 */

/*!
 * \qmlproperty bool LogValueAxis3DFormatter::showMaxLabel
 *
 * When the base property is non-zero, the segments that get automatically generated and depending
 * on the range, the last segment is often smaller than the other segments. In extreme cases this
 * can lead to overlapping labels on the last two grid lines. By setting this property to \c false,
 * you can suppress showing the max label for the axis in cases where the segments do not exactly
 * fit the axis.
 * Defaults to \c true.
 *
 * \sa base, AbstractAxis3D::labels
 */

/*!
 * \internal
 */
QLogValue3DAxisFormatter::QLogValue3DAxisFormatter(QLogValue3DAxisFormatterPrivate *d,
                                                   QObject *parent) :
    QValue3DAxisFormatter(d, parent)
{
}

/*!
 * Constructs a new QLogValue3DAxisFormatter instance with optional \a parent.
 */
QLogValue3DAxisFormatter::QLogValue3DAxisFormatter(QObject *parent) :
    QValue3DAxisFormatter(new QLogValue3DAxisFormatterPrivate(this), parent)
{
}

/*!
 * Destroys QLogValue3DAxisFormatter.
 */
QLogValue3DAxisFormatter::~QLogValue3DAxisFormatter()
{
}

/*!
 * \property QLogValue3DAxisFormatter::base
 *
 * The \a base of the logarithm used to map axis values. If the base is non-zero, the parent axis
 * segment count will be automatically adjusted whenever the axis formatter is recalculated.
 * If you want the range to be divided into equal segments like normal value axis, set this
 * property value to zero.
 *
 * The \a base has to be zero or positive value and not equal to one.
 * Defaults to ten.
 *
 * \sa QValue3DAxis::segmentCount
 */
void QLogValue3DAxisFormatter::setBase(qreal base)
{
    if (base < 0.0f || base == 1.0f) {
        qWarning() << "Warning: The logarithm base must be greater than 0 and not equal to 1,"
                   << "attempted:" << base;
        return;
    }
    if (dptr()->m_base != base) {
        dptr()->m_base = base;
        markDirty(true);
        emit baseChanged(base);
    }
}

qreal QLogValue3DAxisFormatter::base() const
{
    return dptrc()->m_base;
}

/*!
 * \property QLogValue3DAxisFormatter::autoSubGrid
 *
 * If this property value is set to \c true, the parent axis sub-segment count is adjusted
 * automatically according to the base property value. The number of subsegments is set to
 * base value minus one, rounded up.
 * Defaults to \c true.
 *
 * \sa base, QValue3DAxis::subSegmentCount
 */
void QLogValue3DAxisFormatter::setAutoSubGrid(bool enabled)
{
    if (dptr()->m_autoSubGrid != enabled) {
        dptr()->m_autoSubGrid = enabled;
        markDirty(false);
        emit autoSubGridChanged(enabled);
    }
}

bool QLogValue3DAxisFormatter::autoSubGrid() const
{
    return dptrc()->m_autoSubGrid;
}

/*!
 * \property QLogValue3DAxisFormatter::showMaxLabel
 *
 * When the base property is non-zero, the segments get automatically generated, and depending
 * on the range, the last segment is often smaller than the other segments. In extreme cases this
 * can lead to overlapping labels on the last two grid lines. By setting this property to \c false,
 * you can suppress showing the max label for the axis in cases where the segments do not exactly
 * fit the axis.
 * Defaults to \c true.
 *
 * \sa base, QAbstract3DAxis::labels
 */
void QLogValue3DAxisFormatter::setShowMaxLabel(bool enabled)
{
    if (dptr()->m_showMaxLabel != enabled) {
        dptr()->m_showMaxLabel = enabled;
        markDirty(true);
        emit showMaxLabelChanged(enabled);
    }
}

bool QLogValue3DAxisFormatter::showMaxLabel() const
{
    return dptrc()->m_showMaxLabel;
}

/*!
 * \internal
 */
bool QLogValue3DAxisFormatter::allowNegatives() const
{
    return false;
}

/*!
 * \internal
 */
bool QLogValue3DAxisFormatter::allowZero() const
{
    return false;
}

/*!
 * \internal
 */
QValue3DAxisFormatter *QLogValue3DAxisFormatter::createNewInstance() const
{
    return new QLogValue3DAxisFormatter();
}

/*!
 * \internal
 */
void QLogValue3DAxisFormatter::recalculate()
{
    dptr()->recalculate();
}

/*!
 * \internal
 */
float QLogValue3DAxisFormatter::positionAt(float value) const
{
    return dptrc()->positionAt(value);
}

/*!
 * \internal
 */
float QLogValue3DAxisFormatter::valueAt(float position) const
{
    return dptrc()->valueAt(position);
}

/*!
 * \internal
 */
void QLogValue3DAxisFormatter::populateCopy(QValue3DAxisFormatter &copy) const
{
    QValue3DAxisFormatter::populateCopy(copy);
    dptrc()->populateCopy(copy);
}

/*!
 * \internal
 */
QString QLogValue3DAxisFormatter::labelForIndex(int index) const
{
    return dptrc()->labelForIndex(index);
}

/*!
 * \internal
 */
QLogValue3DAxisFormatterPrivate *QLogValue3DAxisFormatter::dptr()
{
    return static_cast<QLogValue3DAxisFormatterPrivate *>(d_ptr.data());
}

/*!
 * \internal
 */
const QLogValue3DAxisFormatterPrivate *QLogValue3DAxisFormatter::dptrc() const
{
    return static_cast<const QLogValue3DAxisFormatterPrivate *>(d_ptr.data());
}

// QLogValue3DAxisFormatterPrivate
QLogValue3DAxisFormatterPrivate::QLogValue3DAxisFormatterPrivate(QLogValue3DAxisFormatter *q)
    : QValue3DAxisFormatterPrivate(q),
      m_base(10.0f),
      m_logMin(0.0f),
      m_logMax(0.0f),
      m_logRangeNormalizer(0.0f),
      m_autoSubGrid(true),
      m_showMaxLabel(true),
      m_evenSegments(true)
{
}

QLogValue3DAxisFormatterPrivate::~QLogValue3DAxisFormatterPrivate()
{
}

void QLogValue3DAxisFormatterPrivate::recalculate()
{
    // When doing position/value mappings, base doesn't matter, so just use natural logarithm
    m_logMin = qLn(qreal(m_min));
    m_logMax = qLn(qreal(m_max));
    m_logRangeNormalizer = m_logMax - m_logMin;

    if (m_base > 0.0) {
        // Update parent axis segment counts
        qreal logMin = qLn(qreal(m_min)) / qLn(m_base);
        qreal logMax = qLn(qreal(m_max)) / qLn(m_base);
        qreal logRangeNormalizer = logMax - logMin;

        int segmentCount = qCeil(logRangeNormalizer);

        m_axis->setSegmentCount(segmentCount);
        if (m_autoSubGrid) {
            int subSegmentCount = qCeil(m_base) - 1;
            if (subSegmentCount < 1)
                subSegmentCount = 1;
            m_axis->setSubSegmentCount(subSegmentCount);
        }

        resetArrays();

        // Calculate segment positions
        for (int i = 0; i < segmentCount; i++) {
            float gridValue = float(i) / float(logRangeNormalizer);
            m_gridPositions[i] = gridValue;
            m_labelPositions[i] = gridValue;
            m_labelValues[i] = qPow(m_base, qreal(i) + logMin);
        }
        // Ensure max value doesn't suffer from any rounding errors
        m_gridPositions[segmentCount] = 1.0f;
        m_labelPositions[segmentCount] = 1.0f;
        m_labelValues[segmentCount] = qreal(m_max);
        float lastDiff = 1.0f - m_labelPositions.at(segmentCount - 1);
        float firstDiff = m_labelPositions.at(1) - m_labelPositions.at(0);
        m_evenSegments = qFuzzyCompare(lastDiff, firstDiff);
    } else {
        // Grid lines and label positions are the same as the parent class, so call parent impl
        // first to populate those
        QValue3DAxisFormatterPrivate::doRecalculate();

        // Label value array needs to be repopulated
        qreal segmentStep = 1.0 / qreal(m_axis->segmentCount());
        for (int i = 0; i < m_labelPositions.size(); i++)
            m_labelValues[i] = qExp(segmentStep * qreal(i) * m_logRangeNormalizer + m_logMin);

        m_evenSegments = true;
    }


    // Subgrid line positions are logarithmically spaced
    int subGridCount = m_axis->subSegmentCount() - 1;
    if (subGridCount > 0) {
        float firstSegmentRange = valueAt(m_gridPositions.at(1)) - m_min;
        float subSegmentStep = firstSegmentRange / float(subGridCount + 1);

        // Since the logarithm has the same curvature across whole axis range, we can just calculate
        // subgrid positions for the first segment and replicate them to other segments.
        QVector<float> actualSubSegmentSteps(subGridCount);

        for (int i = 0; i < subGridCount; i++) {
            float currentSubPosition = positionAt(m_min + ((i + 1) * subSegmentStep));
            actualSubSegmentSteps[i] = currentSubPosition;
        }

        int segmentCount = m_axis->segmentCount();
        for (int i = 0; i < segmentCount; i++) {
            for (int j = 0; j < subGridCount; j++) {
                float position = m_gridPositions.at(i) + actualSubSegmentSteps.at(j);
                if (position > 1.0f)
                    position = 1.0f;
                m_subGridPositions[i * subGridCount + j] = position;
            }
        }
    }
}

void QLogValue3DAxisFormatterPrivate::populateCopy(QValue3DAxisFormatter &copy) const
{
    QLogValue3DAxisFormatter *logFormatter = static_cast<QLogValue3DAxisFormatter *>(&copy);
    QLogValue3DAxisFormatterPrivate *priv = logFormatter->dptr();

    priv->m_base = m_base;
    priv->m_logMin = m_logMin;
    priv->m_logMax = m_logMax;
    priv->m_logRangeNormalizer = m_logRangeNormalizer;
}

float QLogValue3DAxisFormatterPrivate::positionAt(float value) const
{
    qreal logValue = qLn(qreal(value));
    float retval = float((logValue - m_logMin) / m_logRangeNormalizer);

    return retval;
}

float QLogValue3DAxisFormatterPrivate::valueAt(float position) const
{
    qreal logValue = (qreal(position) * m_logRangeNormalizer) + m_logMin;
    return float(qExp(logValue));
}

QString QLogValue3DAxisFormatterPrivate::labelForIndex(int index) const
{
    if (index == m_gridPositions.size() - 1 && !m_evenSegments && !m_showMaxLabel)
        return QString();
    else
        return QValue3DAxisFormatterPrivate::labelForIndex(index);
}

QT_END_NAMESPACE_DATAVISUALIZATION