1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the Qt Data Visualization module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 or (at your option) any later version
20** approved by the KDE Free Qt Foundation. The licenses are as published by
21** the Free Software Foundation and appearing in the file LICENSE.GPL3
22** included in the packaging of this file. Please review the following
23** information to ensure the GNU General Public License requirements will
24** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25**
26** $QT_END_LICENSE$
27**
28****************************************************************************/
29
30#include "scatterdatamodifier.h"
31
32#include <QtDataVisualization/QScatterDataProxy>
33#include <QtDataVisualization/QValue3DAxis>
34#include <QtDataVisualization/Q3DScene>
35#include <QtDataVisualization/Q3DCamera>
36#include <QtDataVisualization/QScatter3DSeries>
37#include <QtDataVisualization/Q3DTheme>
38#include <QtCore/qmath.h>
39#include <QtCore/QTextStream>
40#include <QtCore/QDebug>
41
42using namespace QtDataVisualization;
43
44ScatterDataModifier::ScatterDataModifier(Q3DScatter *scatter)
45 : m_graph(scatter),
46 m_inputHandler(new CustomInputHandler())
47{
48 m_graph->activeTheme()->setType(Q3DTheme::ThemeDigia);
49 m_graph->setShadowQuality(QAbstract3DGraph::ShadowQualityMedium);
50 m_graph->scene()->activeCamera()->setCameraPreset(Q3DCamera::CameraPresetFront);
51
52 m_graph->setAxisX(new QValue3DAxis);
53 m_graph->setAxisY(new QValue3DAxis);
54 m_graph->setAxisZ(new QValue3DAxis);
55
56 m_graph->axisX()->setRange(min: -10.0f, max: 10.0f);
57 m_graph->axisY()->setRange(min: -5.0f, max: 5.0f);
58 m_graph->axisZ()->setRange(min: -5.0f, max: 5.0f);
59
60 QScatter3DSeries *series = new QScatter3DSeries;
61 series->setItemLabelFormat(QStringLiteral("@xLabel, @yLabel, @zLabel"));
62 series->setMesh(QAbstract3DSeries::MeshCube);
63 series->setItemSize(0.15f);
64 m_graph->addSeries(series);
65
66 //! [2]
67 m_animationCameraX = new QPropertyAnimation(m_graph->scene()->activeCamera(), "xRotation");
68 m_animationCameraX->setDuration(20000);
69 m_animationCameraX->setStartValue(QVariant::fromValue(value: 0.0f));
70 m_animationCameraX->setEndValue(QVariant::fromValue(value: 360.0f));
71 m_animationCameraX->setLoopCount(-1);
72 //! [2]
73
74 //! [3]
75 QPropertyAnimation *upAnimation = new QPropertyAnimation(m_graph->scene()->activeCamera(), "yRotation");
76 upAnimation->setDuration(9000);
77 upAnimation->setStartValue(QVariant::fromValue(value: 5.0f));
78 upAnimation->setEndValue(QVariant::fromValue(value: 45.0f));
79
80 QPropertyAnimation *downAnimation = new QPropertyAnimation(m_graph->scene()->activeCamera(), "yRotation");
81 downAnimation->setDuration(9000);
82 downAnimation->setStartValue(QVariant::fromValue(value: 45.0f));
83 downAnimation->setEndValue(QVariant::fromValue(value: 5.0f));
84
85 m_animationCameraY = new QSequentialAnimationGroup();
86 m_animationCameraY->setLoopCount(-1);
87 m_animationCameraY->addAnimation(animation: upAnimation);
88 m_animationCameraY->addAnimation(animation: downAnimation);
89 //! [3]
90
91 m_animationCameraX->start();
92 m_animationCameraY->start();
93
94 // Give ownership of the handler to the graph and make it the active handler
95 //! [0]
96 m_graph->setActiveInputHandler(m_inputHandler);
97 //! [0]
98
99 //! [1]
100 m_selectionTimer = new QTimer(this);
101 m_selectionTimer->setInterval(10);
102 m_selectionTimer->setSingleShot(false);
103 QObject::connect(sender: m_selectionTimer, signal: &QTimer::timeout, receiver: this,
104 slot: &ScatterDataModifier::triggerSelection);
105 m_selectionTimer->start();
106 //! [1]
107}
108
109ScatterDataModifier::~ScatterDataModifier()
110{
111 delete m_graph;
112}
113
114void ScatterDataModifier::start()
115{
116 addData();
117}
118
119void ScatterDataModifier::addData()
120{
121 QVector<QVector3D> itemList;
122
123 // Read data items from the file to QVector
124 QTextStream stream;
125 QFile dataFile(":/data/data.txt");
126 if (dataFile.open(flags: QIODevice::ReadOnly | QIODevice::Text)) {
127 stream.setDevice(&dataFile);
128 while (!stream.atEnd()) {
129 QString line = stream.readLine();
130 if (line.startsWith(s: "#")) // Ignore comments
131 continue;
132 QStringList strList = line.split(sep: ",", behavior: Qt::SkipEmptyParts);
133 // Each line has three data items: xPos, yPos and zPos value
134 if (strList.size() < 3) {
135 qWarning() << "Invalid row read from data:" << line;
136 continue;
137 }
138 itemList.append(t: QVector3D(
139 strList.at(i: 0).trimmed().toFloat(),
140 strList.at(i: 1).trimmed().toFloat(),
141 strList.at(i: 2).trimmed().toFloat()));
142 }
143 } else {
144 qWarning() << "Unable to open data file:" << dataFile.fileName();
145 }
146
147 // Add data from the QVector to datamodel
148 QScatterDataArray *dataArray = new QScatterDataArray;
149 dataArray->resize(asize: itemList.count());
150 QScatterDataItem *ptrToDataArray = &dataArray->first();
151 for (int i = 0; i < itemList.count(); i++) {
152 ptrToDataArray->setPosition(itemList.at(i));
153 ptrToDataArray++;
154 }
155
156 m_graph->seriesList().at(i: 0)->dataProxy()->resetArray(newArray: dataArray);
157}
158
159void ScatterDataModifier::toggleCameraAnimation()
160{
161 if (m_animationCameraX->state() != QAbstractAnimation::Paused) {
162 m_animationCameraX->pause();
163 m_animationCameraY->pause();
164 } else {
165 m_animationCameraX->resume();
166 m_animationCameraY->resume();
167 }
168}
169
170void ScatterDataModifier::triggerSelection()
171{
172 m_graph->scene()->setSelectionQueryPosition(m_inputHandler->inputPosition());
173}
174
175void ScatterDataModifier::shadowQualityUpdatedByVisual(QAbstract3DGraph::ShadowQuality sq)
176{
177 int quality = int(sq);
178 emit shadowQualityChanged(quality); // connected to a checkbox in main.cpp
179}
180
181void ScatterDataModifier::changeShadowQuality(int quality)
182{
183 QAbstract3DGraph::ShadowQuality sq = QAbstract3DGraph::ShadowQuality(quality);
184 m_graph->setShadowQuality(sq);
185}
186

source code of qtdatavis3d/examples/datavisualization/custominput/scatterdatamodifier.cpp