summaryrefslogtreecommitdiffstats
path: root/examples/datavisualization/customitems/customitemgraph.cpp
blob: 0a757e7d76edd44385e20724e5674e8b66bf1cb5 (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
/****************************************************************************
**
** 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 "customitemgraph.h"

#include <QtDataVisualization/Q3DTheme>
#include <QtDataVisualization/QCustom3DItem>
#include <QtGui/QImage>

using namespace QtDataVisualization;

CustomItemGraph::CustomItemGraph(Q3DSurface *surface, QLabel *label)
    : m_graph(surface),
      m_textField(label)
{
    QImage layerOneHMap(":/maps/layer_1.png");
    QHeightMapSurfaceDataProxy *layerOneProxy = new QHeightMapSurfaceDataProxy(layerOneHMap);
    QSurface3DSeries *layerOneSeries = new QSurface3DSeries(layerOneProxy);
    layerOneSeries->setItemLabelFormat(QStringLiteral("(@xLabel, @zLabel): @yLabel"));
    layerOneProxy->setValueRanges(34.0f, 40.0f, 18.0f, 24.0f);
    layerOneSeries->setDrawMode(QSurface3DSeries::DrawSurface);
    layerOneSeries->setFlatShadingEnabled(false);

    QImage layerTwoHMap(":/maps/layer_2.png");
    QHeightMapSurfaceDataProxy *layerTwoProxy = new QHeightMapSurfaceDataProxy(layerTwoHMap);
    QSurface3DSeries *layerTwoSeries = new QSurface3DSeries(layerTwoProxy);
    layerTwoSeries->setItemLabelFormat(QStringLiteral("(@xLabel, @zLabel): @yLabel"));
    layerTwoProxy->setValueRanges(34.0f, 40.0f, 18.0f, 24.0f);
    layerTwoSeries->setDrawMode(QSurface3DSeries::DrawSurface);
    layerTwoSeries->setFlatShadingEnabled(false);

    QImage layerThreeHMap(":/maps/layer_3.png");
    QHeightMapSurfaceDataProxy *layerThreeProxy = new QHeightMapSurfaceDataProxy(layerThreeHMap);
    QSurface3DSeries *layerThreeSeries = new QSurface3DSeries(layerThreeProxy);
    layerThreeSeries->setItemLabelFormat(QStringLiteral("(@xLabel, @zLabel): @yLabel"));
    layerThreeProxy->setValueRanges(34.0f, 40.0f, 18.0f, 24.0f);
    layerThreeSeries->setDrawMode(QSurface3DSeries::DrawSurface);
    layerThreeSeries->setFlatShadingEnabled(false);

    m_graph->axisX()->setLabelFormat("%.1f N");
    m_graph->axisZ()->setLabelFormat("%.1f E");
    m_graph->axisX()->setRange(34.0f, 40.0f);
    m_graph->axisY()->setRange(0.0f, 200.0f);
    m_graph->axisZ()->setRange(18.0f, 24.0f);

    m_graph->axisX()->setTitle(QStringLiteral("Latitude"));
    m_graph->axisY()->setTitle(QStringLiteral("Height"));
    m_graph->axisZ()->setTitle(QStringLiteral("Longitude"));

    m_graph->addSeries(layerOneSeries);
    m_graph->addSeries(layerTwoSeries);
    m_graph->addSeries(layerThreeSeries);

    QLinearGradient grOne;
    grOne.setColorAt(0.0, Qt::black);
    grOne.setColorAt(0.38, Qt::darkYellow);
    grOne.setColorAt(0.39, Qt::darkGreen);
    grOne.setColorAt(0.5, Qt::darkGray);
    grOne.setColorAt(1.0, Qt::gray);
    m_graph->seriesList().at(0)->setBaseGradient(grOne);
    m_graph->seriesList().at(0)->setColorStyle(Q3DTheme::ColorStyleRangeGradient);

    QLinearGradient grTwo;
    grTwo.setColorAt(0.385, Qt::blue);
    grTwo.setColorAt(0.395, Qt::white);
    m_graph->seriesList().at(1)->setBaseGradient(grTwo);
    m_graph->seriesList().at(1)->setColorStyle(Q3DTheme::ColorStyleRangeGradient);

    QLinearGradient grThree;
    grThree.setColorAt(0.0, Qt::white);
    grThree.setColorAt(0.05, Qt::black);
    m_graph->seriesList().at(2)->setBaseGradient(grThree);
    m_graph->seriesList().at(2)->setColorStyle(Q3DTheme::ColorStyleRangeGradient);

    m_graph->scene()->activeCamera()->setCameraPreset(Q3DCamera::CameraPresetFront);

    connect(m_graph, &QAbstract3DGraph::elementSelected,
            this, &CustomItemGraph::handleElementSelected);
}

CustomItemGraph::~CustomItemGraph()
{
    delete m_graph;
}

void CustomItemGraph::toggleItemOne(bool show)
{
    //! [1]
    QVector3D positionOne = QVector3D(39.0f, 77.0f, 19.2f);
    //! [1]
    if (show) {
        //! [0]
        QImage color = QImage(2, 2, QImage::Format_ARGB32);
        color.fill(Qt::red);
        //! [0]
        //! [2]
        QCustom3DItem *item = new QCustom3DItem(":/items/oilrig.obj", positionOne,
                                                QVector3D(0.025f, 0.025f, 0.025f),
                                                QQuaternion::fromAxisAndAngle(0.0f, 1.0f, 0.0f, 45.0f),
                                                color);
        //! [2]
        //! [3]
        m_graph->addCustomItem(item);
        //! [3]
    } else {
        //! [4]
        m_graph->removeCustomItemAt(positionOne);
        //! [4]
    }
}

void CustomItemGraph::toggleItemTwo(bool show)
{
    QVector3D positionTwo = QVector3D(34.5f, 77.0f, 23.4f);
    if (show) {
        QImage color = QImage(2, 2, QImage::Format_ARGB32);
        color.fill(Qt::red);
        QCustom3DItem *item = new QCustom3DItem();
        item->setMeshFile(":/items/oilrig.obj");
        item->setPosition(positionTwo);
        item->setScaling(QVector3D(0.025f, 0.025f, 0.025f));
        item->setRotation(QQuaternion::fromAxisAndAngle(0.0f, 1.0f, 0.0f, 25.0f));
        item->setTextureImage(color);
        m_graph->addCustomItem(item);
    } else {
        m_graph->removeCustomItemAt(positionTwo);
    }
}

void CustomItemGraph::toggleItemThree(bool show)
{
    QVector3D positionThree = QVector3D(34.5f, 86.0f, 19.1f);
    if (show) {
        QImage color = QImage(2, 2, QImage::Format_ARGB32);
        color.fill(Qt::darkMagenta);
        QCustom3DItem *item = new QCustom3DItem();
        item->setMeshFile(":/items/refinery.obj");
        item->setPosition(positionThree);
        item->setScaling(QVector3D(0.04f, 0.04f, 0.04f));
        item->setRotation(QQuaternion::fromAxisAndAngle(0.0f, 1.0f, 0.0f, 75.0f));
        item->setTextureImage(color);
        m_graph->addCustomItem(item);
    } else {
        m_graph->removeCustomItemAt(positionThree);
    }
}

void CustomItemGraph::toggleSeeThrough(bool seethrough)
{
    if (seethrough) {
        m_graph->seriesList().at(0)->setDrawMode(QSurface3DSeries::DrawWireframe);
        m_graph->seriesList().at(1)->setDrawMode(QSurface3DSeries::DrawWireframe);
    } else {
        m_graph->seriesList().at(0)->setDrawMode(QSurface3DSeries::DrawSurface);
        m_graph->seriesList().at(1)->setDrawMode(QSurface3DSeries::DrawSurface);
    }
}

void CustomItemGraph::toggleOilHighlight(bool highlight)
{
    if (highlight) {
        QLinearGradient grThree;
        grThree.setColorAt(0.0, Qt::black);
        grThree.setColorAt(0.05, Qt::red);
        m_graph->seriesList().at(2)->setBaseGradient(grThree);
    } else {
        QLinearGradient grThree;
        grThree.setColorAt(0.0, Qt::white);
        grThree.setColorAt(0.05, Qt::black);
        m_graph->seriesList().at(2)->setBaseGradient(grThree);
    }
}

void CustomItemGraph::toggleShadows(bool shadows)
{
    if (shadows)
        m_graph->setShadowQuality(QAbstract3DGraph::ShadowQualityMedium);
    else
        m_graph->setShadowQuality(QAbstract3DGraph::ShadowQualityNone);
}

void CustomItemGraph::handleElementSelected(QAbstract3DGraph::ElementType type)
{
    if (type == QAbstract3DGraph::ElementCustomItem) {
        int index = m_graph->selectedCustomItemIndex();
        QCustom3DItem *item = m_graph->selectedCustomItem();
        QString text;
        text.setNum(index);
        text.append(": ");
        QStringList split = item->meshFile().split("/");
        text.append(split.last());
        m_textField->setText(text);
    } else if (type == QAbstract3DGraph::ElementSeries) {
        m_textField->setText("Surface");
    } else if (type > QAbstract3DGraph::ElementSeries
               && type < QAbstract3DGraph::ElementCustomItem) {
        m_textField->setText("Axis");
    } else {
        m_textField->setText("Nothing");
    }
}