summaryrefslogtreecommitdiffstats
path: root/src/datavisualization/data/qsurfacedataproxy.cpp
blob: 98998f1027ff4c422670b2689029bdceaf26545d (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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/****************************************************************************
**
** Copyright (C) 2013 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 "qsurfacedataproxy.h"
#include "qsurfacedataproxy_p.h"

QT_DATAVISUALIZATION_BEGIN_NAMESPACE

// Default ranges correspond value axis defaults
const qreal defaultMinValue = 0.0;
const qreal defaultMaxValue = 10.0;

/*!
 * \class QSurfaceDataProxy
 * \inmodule QtDataVisualization
 * \brief Proxy class for Q3DSurface.
 * \since 1.0.0
 *
 * QSurfaceDataProxy takes care of surface related data handling. The QSurfaceDataProxy handles the data
 * in rows and for this it provides two auxiliary typedefs. QSurfaceDataArray is a QList for
 * controlling the rows. For rows there is a QVector typedef QSurfaceDataRow which takes in qreal
 * values. See Q3DSurface documentation and basic sample code there how to feed the data for the
 * QSurfaceDataProxy.
 *
 * All rows must have same number of values.
 *
 * \note Surfaces with less than two rows or columns are not considered valid surfaces and will
 * not get rendered.
 *
 * \note The way row and column values are handled is subject to change after technology preview.
 *
 * QSurfaceDataProxy supports the following format tags for QAbstractDataProxy::setItemLabelFormat():
 * \table
 *   \row
 *     \li @xTitle    \li Title from X axis
 *   \row
 *     \li @yTitle    \li Title from Y axis
 *   \row
 *     \li @zTitle    \li Title from Z axis
 *   \row
 *     \li @xLabel    \li Item value formatted using the same format as the X axis attached to the graph uses,
 *                            see \l{Q3DValueAxis::setLabelFormat()} for more information.
 *   \row
 *     \li @yLabel    \li Item value formatted using the same format as the Y axis attached to the graph uses,
 *                            see \l{Q3DValueAxis::setLabelFormat()} for more information.
 *   \row
 *     \li @zLabel    \li Item value formatted using the same format as the Z axis attached to the graph uses,
 *                            see \l{Q3DValueAxis::setLabelFormat()} for more information.
 * \endtable
 *
 */

/*!
 * \qmltype SurfaceDataProxy
 * \instantiates QSurfaceDataProxy
 * \inherits AbstractDataProxy
 *
 * DOCUMENTATION GOES HERE TOO
 */

/*!
 * \qmlproperty int SurfaceDataProxy::rowCount
 * Number of the rows in the array.
 */

/*!
 * \qmlproperty int SurfaceDataProxy::columnCount
 * Number of the columns in the array.
 */

/*!
 * \qmlproperty qreal QSurfaceDataProxy::minValueRows
 *
 * The minimum value of the range in rows. For instance if function z value varies between -8.0
 * and 8.0 set this property to -8.0.
 *
 * \note The way row and column values are handled is subject to change after technology preview.
 */

/*!
 * \qmlproperty qreal QSurfaceDataProxy::maxValueRows
 *
 * The maximum value of the range in rows. For instance if function z value varies between -8.0
 * and 8.0 set this property to 8.0.
 *
 * \note The way row and column values are handled is subject to change after technology preview.
 */

/*!
 * \qmlproperty qreal QSurfaceDataProxy::minValueColumns
 *
 * The minimum value of the range in columns. For instance if function x value varies between -8.0
 * and 8.0 set this property to -8.0.
 *
 * \note The way row and column values are handled is subject to change after technology preview.
 */

/*!
 * \qmlproperty qreal QSurfaceDataProxy::maxValueColumns
 *
 * The maximum value of the range in columns. For instance if function x value varies between -8.0
 * and 8.0 set this property to 8.0.
 *
 * \note The way row and column values are handled is subject to change after technology preview.
 */

/*!
 * Constructs QSurfaceDataProxy with the given \a parent.
 */
QSurfaceDataProxy::QSurfaceDataProxy(QObject *parent) :
    QAbstractDataProxy(new QSurfaceDataProxyPrivate(this), parent)
{
}

/*!
 * \internal
 */
QSurfaceDataProxy::QSurfaceDataProxy(QSurfaceDataProxyPrivate *d, QObject *parent) :
    QAbstractDataProxy(d, parent)
{
}

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

/*!
 * Takes ownership of the \a newArray. Clears the existing array and if the \a newArray is
 * different than the existing array. If it's the same array, this just triggers arrayReset()
 * signal.
 * Passing null array deletes the old array and creates a new empty array.
 * All rows in \a newArray must be of same length.
 * Row and column ranges are reset to defaults, unless \a newArray is the same as the old array.
 * In that case, old row and column ranges are kept.
 */
void QSurfaceDataProxy::resetArray(QSurfaceDataArray *newArray)
{
    if (dptr()->m_dataArray != newArray) {
        dptr()->resetArray(newArray, defaultMinValue, defaultMaxValue, defaultMinValue,
                           defaultMaxValue);
    }
    emit arrayReset();
}

/*!
 * Takes ownership of the \a newArray. Clears the existing array and if the \a newArray is
 * different than the existing array. If it's the same array, this just triggers arrayReset()
 * signal and updates row/column values.
 * Passing null array deletes the old array and creates a new empty array.
 * All rows in \a newArray must be of same length.
 * Row and column ranges are set to values defined by the rest of the parameters: \a minValueRows,
 * \a maxValueRows, \a minValueColumns, and \a maxValueColumns.
 */
void QSurfaceDataProxy::resetArray(QSurfaceDataArray *newArray, qreal minValueRows,
                                   qreal maxValueRows, qreal minValueColumns,
                                   qreal maxValueColumns)
{
    dptr()->resetArray(newArray, minValueRows, maxValueRows, minValueColumns, maxValueColumns);
    emit arrayReset();
}

/*!
 * \return pointer to the data array.
 */
const QSurfaceDataArray *QSurfaceDataProxy::array() const
{
    return dptrc()->m_dataArray;
}

/*!
 * \property QSurfaceDataProxy::rowCount
 *
 * \return number of rows in the data.
 */
int QSurfaceDataProxy::rowCount() const
{
    return dptrc()->m_dataArray->size();
}

/*!
 * \property QSurfaceDataProxy::columnCount
 *
 * \return number of items in the columns.
 */
int QSurfaceDataProxy::columnCount() const
{
    if (dptrc()->m_dataArray->size() > 0)
        return dptrc()->m_dataArray->at(0)->size();
    else
        return 0;
}

/*!
 * \return pointer to the item at \a index. It is guaranteed to be valid only until next call that
 * modifies data.
 */
const QSurfaceDataItem *QSurfaceDataProxy::itemAt(int index) const
{
    return &dptrc()->m_dataArray->at(index)->at(2);
}

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

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

/*!
 * \fn void QSurfaceDataProxy::arrayReset()
 *
 * Emitted when data array is reset.
 */

/*!
 * Sets the value range for rows from \a min to \a max. For instance if function z value varies between -8.0
 * and 8.0 set \a min to -8.0 and \a max to 8.0.
 * When setting the range, the max is adjusted if necessary, to ensure that the range remains valid.
 */
void QSurfaceDataProxy::setValueRangeRows(qreal min, qreal max)
{
    dptr()->setValueRangeRows(min, max);
}

/*!
 * Sets the value range for columns from \a min to \a max. For instance if function x value varies between -8.0
 * and 8.0 set \a min to -8.0 and \a max to 8.0.
 * When setting the range, the max is adjusted if necessary, to ensure that the range remains valid.
 */
void QSurfaceDataProxy::setValueRangeColumns(qreal min, qreal max)
{
    dptr()->setValueRangeColumns(min, max);
}

/*!
 * \property QSurfaceDataProxy::minValueRows
 *
 * Defines the minimum value of the range for rows. For instance if function z value varies between -8.0
 * and 8.0 set this property to -8.0.
 * When setting this property the max is adjusted if necessary, to ensure that the range remains
 * valid.
 */
void QSurfaceDataProxy::setMinValueRows(qreal min)
{
    dptr()->setMinValueRows(min);
}

qreal QSurfaceDataProxy::minValueRows() const
{
    return dptrc()->m_minValueRows;
}

/*!
 * \property QSurfaceDataProxy::maxValueRows
 *
 * Defines the maximum value of the range for rows. For instance if function z value varies between -8.0
 * and 8.0 set this property to 8.0.
 * When setting this property the min is adjusted if necessary, to ensure that the range remains
 * valid.
 */
void QSurfaceDataProxy::setMaxValueRows(qreal max)
{
    dptr()->setMaxValueRows(max);
}

qreal QSurfaceDataProxy::maxValueRows() const
{
    return dptrc()->m_maxValueRows;
}

/*!
 * \property QSurfaceDataProxy::minValueColumns
 *
 * Defines the minimum value of the range for columns. For instance if function x value varies between -8.0
 * and 8.0 set this property to -8.0.
 * When setting this property the min is adjusted if necessary, to ensure that the range remains
 * valid.
 */
void QSurfaceDataProxy::setMinValueColumns(qreal min)
{
    dptr()->setMinValueColumns(min);
}

qreal QSurfaceDataProxy::minValueColumns() const
{
    return dptrc()->m_minValueColumns;
}

/*!
 * \property QSurfaceDataProxy::maxValueColumns
 *
 * Defines the maximum value of the range for columns. For instance if function x value varies between -8.0
 * and 8.0 set this property to 8.0.
 * When setting this property the min is adjusted if necessary, to ensure that the range remains
 * valid.
 */
void QSurfaceDataProxy::setMaxValueColumns(qreal max)
{
    dptr()->setMaxValueColumns(max);
}

qreal QSurfaceDataProxy::maxValueColumns() const
{
    return dptrc()->m_maxValueColumns;
}


//
//  QSurfaceDataProxyPrivate
//

QSurfaceDataProxyPrivate::QSurfaceDataProxyPrivate(QSurfaceDataProxy *q)
    : QAbstractDataProxyPrivate(q, QAbstractDataProxy::DataTypeSurface),
      m_dataArray(new QSurfaceDataArray),
      m_minValueRows(defaultMinValue),
      m_maxValueRows(defaultMaxValue),
      m_minValueColumns(defaultMinValue),
      m_maxValueColumns(defaultMaxValue)
{
    m_itemLabelFormat = QStringLiteral("@yLabel (@xLabel, @zLabel)");
}

QSurfaceDataProxyPrivate::~QSurfaceDataProxyPrivate()
{
    clearArray();
}

void QSurfaceDataProxyPrivate::resetArray(QSurfaceDataArray *newArray, qreal minValueRows,
                                          qreal maxValueRows, qreal minValueColumns,
                                          qreal maxValueColumns)
{
    if (!newArray)
        newArray = new QSurfaceDataArray;

    if (newArray != m_dataArray) {
        clearArray();
        m_dataArray = newArray;
    }

    setValueRangeRows(minValueRows, maxValueRows);
    setValueRangeColumns(minValueColumns, maxValueColumns);
}

void QSurfaceDataProxyPrivate::setValueRangeRows(qreal min, qreal max)
{
    bool changed = false;
    if (m_minValueRows != min) {
        m_minValueRows = min;
        changed = true;
    }
    if (m_maxValueRows != max || min >= max) {
        if (min >= max) {
            m_maxValueRows = min + 1.0;
            qWarning() << "Warning: Tried to set invalid range for value range."
                          " Range automatically adjusted to a valid one:"
                       << min << "-" << max << "-->" << m_minValueRows << "-" << m_maxValueRows;
        } else {
            m_maxValueRows = max;
        }
        changed = true;
    }

    if (changed)
        emit qptr()->valueRangeRowsChanged(min, max);
}

void QSurfaceDataProxyPrivate::setValueRangeColumns(qreal min, qreal max)
{
    bool changed = false;
    if (m_minValueColumns != min) {
        m_minValueColumns = min;
        changed = true;
    }
    if (m_maxValueColumns != max || min >= max) {
        if (min >= max) {
            m_maxValueColumns = min + 1.0;
            qWarning() << "Warning: Tried to set invalid range for value range."
                          " Range automatically adjusted to a valid one:"
                       << min << "-" << max << "-->" << m_minValueColumns << "-" << m_maxValueColumns;
        } else {
            m_maxValueColumns = max;
        }
        changed = true;
    }

    if (changed)
        emit qptr()->valueRangeColumnsChanged(min, max);
}

void QSurfaceDataProxyPrivate::setMinValueRows(qreal min)
{
    if (min != m_minValueRows) {
        if (min >= m_maxValueRows) {
            qreal oldMax = m_maxValueRows;
            m_maxValueRows = min + 1.0;
            qWarning() << "Warning: Tried to set minimum to equal or larger than maximum for"
                          " value range. Maximum automatically adjusted to a valid one:"
                       << oldMax <<  "-->" << m_maxValueRows;
        }
        m_minValueRows = min;
        emit qptr()->valueRangeRowsChanged(m_minValueRows, m_maxValueRows);
    }
}

void QSurfaceDataProxyPrivate::setMaxValueRows(qreal max)
{
    if (m_maxValueRows != max) {
        if (max <= m_minValueRows) {
            qreal oldMin = m_minValueRows;
            m_minValueRows = max - 1.0;
            qWarning() << "Warning: Tried to set maximum to equal or smaller than minimum for"
                          " value range. Minimum automatically adjusted to a valid one:"
                       << oldMin <<  "-->" << m_minValueRows;
        }
        m_maxValueRows = max;
        emit qptr()->valueRangeRowsChanged(m_minValueRows, m_maxValueRows);
    }
}

void QSurfaceDataProxyPrivate::setMinValueColumns(qreal min)
{
    if (min != m_minValueColumns) {
        if (min >= m_maxValueColumns) {
            qreal oldMax = m_maxValueColumns;
            m_maxValueColumns = min + 1.0;
            qWarning() << "Warning: Tried to set minimum to equal or larger than maximum for"
                          " value range. Maximum automatically adjusted to a valid one:"
                       << oldMax <<  "-->" << m_maxValueColumns;
        }
        m_minValueColumns = min;
        emit qptr()->valueRangeColumnsChanged(m_minValueColumns, m_maxValueRows);
    }
}

void QSurfaceDataProxyPrivate::setMaxValueColumns(qreal max)
{
    if (m_maxValueColumns != max) {
        if (max <= m_minValueColumns) {
            qreal oldMin = m_minValueColumns;
            m_minValueColumns = max - 1.0;
            qWarning() << "Warning: Tried to set maximum to equal or smaller than minimum for"
                          " value range. Minimum automatically adjusted to a valid one:"
                       << oldMin <<  "-->" << m_minValueColumns;
        }
        m_maxValueColumns = max;
        emit qptr()->valueRangeColumnsChanged(m_minValueColumns, m_maxValueColumns);
    }
}

QSurfaceDataProxy *QSurfaceDataProxyPrivate::qptr()
{
    return static_cast<QSurfaceDataProxy *>(q_ptr);
}

void QSurfaceDataProxyPrivate::limitValues(QVector3D &minValues, QVector3D &maxValues)
{
    qreal min = 0.0;
    qreal max = 0.0;

    int rows = m_dataArray->size();
    int columns = 0;
    if (rows)
        columns = m_dataArray->at(0)->size();

    if (rows && columns) {
        min = m_dataArray->at(0)->at(0);
        max = m_dataArray->at(0)->at(0);
    }

    for (int i = 0; i < rows; i++) {
        QSurfaceDataRow *row = m_dataArray->at(i);
        if (row) {
            for (int j = 0; j < columns; j++) {
                qreal itemValue = m_dataArray->at(i)->at(j);
                if (min > itemValue)
                    min = itemValue;
                if (max < itemValue)
                    max = itemValue;
            }
        }
    }

    minValues.setX(m_minValueColumns);
    minValues.setY(min);
    minValues.setZ(m_minValueRows);
    maxValues.setX(m_maxValueColumns);
    maxValues.setY(max);
    maxValues.setZ(m_maxValueRows);
}

void QSurfaceDataProxyPrivate::clearRow(int rowIndex)
{
    if (m_dataArray->at(rowIndex)) {
        delete m_dataArray->at(rowIndex);
        (*m_dataArray)[rowIndex] = 0;
    }
}

void QSurfaceDataProxyPrivate::clearArray()
{
    for (int i = 0; i < m_dataArray->size(); i++)
        clearRow(i);
    m_dataArray->clear();
    delete m_dataArray;
}

QT_DATAVISUALIZATION_END_NAMESPACE