summaryrefslogtreecommitdiffstats
path: root/src/datavisualization/engine/q3dscene.cpp
blob: 49c08bbc1a824b72924a407bd7048577348d0a05 (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
/****************************************************************************
**
** 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 <qmath.h>

#include "datavisualizationglobal_p.h"

#include "q3dscene.h"
#include "q3dscene_p.h"
#include "q3dcamera.h"
#include "q3dlight.h"

QT_DATAVISUALIZATION_BEGIN_NAMESPACE

Q3DScene::Q3DScene(QObject *parent) :
    d_ptr(new Q3DScenePrivate(this))
{
}

Q3DScene *Q3DScene::clone(QObject *parent)
{
    Q3DScene *cloneScene = new Q3DScene(parent);
    cloneScene->setViewport(viewport());
    cloneScene->setMainViewport(mainViewport());
    cloneScene->setSliceViewport(sliceViewport());

    if (d_ptr->m_camera) {
        Q3DCamera *cloneCamera = new Q3DCamera();
        cloneCamera->copyValuesFrom(*d_ptr->m_camera);
        cloneScene->setCamera(cloneCamera);
    }

    if (d_ptr->m_light) {
        Q3DLight *cloneLight = new Q3DLight();
        cloneLight->copyValuesFrom(*d_ptr->m_light);
        cloneScene->setLight(cloneLight);
    }

    cloneScene->setSlicingActivated(isSlicingActivated());
    cloneScene->setUnderSideCameraEnabled(isUnderSideCameraEnabled());

    return cloneScene;
}

Q3DScene::~Q3DScene()
{
}

QRect Q3DScene::viewport() const
{
    return d_ptr->m_viewport;
}

void Q3DScene::setViewport(const QRect &viewport)
{
    d_ptr->m_viewport = viewport;
    d_ptr->m_viewport.setX(0);
    d_ptr->m_viewport.setY(0);
}

void Q3DScene::setViewportSize(int width, int height)
{
    d_ptr->m_viewport.setWidth(width);
    d_ptr->m_viewport.setHeight(height);
}

QRect Q3DScene::mainViewport() const
{
    return d_ptr->m_mainViewport;
}

void Q3DScene::setMainViewport(const QRect &mainViewPort)
{
    d_ptr->m_mainViewport = mainViewPort;
}

bool Q3DScene::isInputInsideMainView(const QPoint &point)
{
    int x = point.x();
    int y = point.y();
    int areaMinX = d_ptr->m_mainViewport.x();
    int areaMaxX = d_ptr->m_mainViewport.x() + d_ptr->m_mainViewport.width();
    int areaMaxY = d_ptr->m_viewport.height() - d_ptr->m_mainViewport.y();
    int areaMinY = d_ptr->m_viewport.height() - (d_ptr->m_mainViewport.y() + d_ptr->m_mainViewport.height());

    return ( x > areaMinX && x < areaMaxX && y > areaMinY && y < areaMaxY );
}

bool Q3DScene::isInputInsideSliceView(const QPoint &point)
{
    int x = point.x();
    int y = point.y();
    int areaMinX = d_ptr->m_sliceViewport.x();
    int areaMaxX = d_ptr->m_sliceViewport.x() + d_ptr->m_sliceViewport.width();
    int areaMaxY = d_ptr->m_viewport.height() - d_ptr->m_sliceViewport.y();
    int areaMinY = d_ptr->m_viewport.height() - (d_ptr->m_sliceViewport.y() + d_ptr->m_sliceViewport.height());

    return ( x > areaMinX && x < areaMaxX && y > areaMinY && y < areaMaxY );
}

QRect Q3DScene::sliceViewport() const
{
    return d_ptr->m_sliceViewport;
}

void Q3DScene::setSliceViewport(const QRect &sliceViewPort)
{
    d_ptr->m_sliceViewport = sliceViewPort;
}

// TODO: Refactor the current way of building the scene...
//       The scene should have clear ownership of camera, light and other future building blocks of the scene.

Q3DCamera *Q3DScene::camera() const
{
    return d_ptr->m_camera;
}

void Q3DScene::setCamera(Q3DCamera *camera)
{
    if (d_ptr->m_camera)
        d_ptr->m_camera->setParentScene(0);

    d_ptr->m_camera = camera;
    d_ptr->m_camera->setParentScene(this);
}

Q3DLight *Q3DScene::light() const
{
    return d_ptr->m_light;
}

void Q3DScene::setLight(Q3DLight *light)
{
    if (d_ptr->m_light)
        d_ptr->m_light->setParentScene(0);

    d_ptr->m_light = light;
    d_ptr->m_light->setParentScene(this);
}

bool Q3DScene::isUnderSideCameraEnabled() const
{
    return d_ptr->m_isUnderSideCameraEnabled;
}

void Q3DScene::setUnderSideCameraEnabled(bool isEnabled)
{
    d_ptr->m_isUnderSideCameraEnabled = isEnabled;
}

bool Q3DScene::isSlicingActivated() const
{
    return d_ptr->m_isSlicingActivated;
}

void Q3DScene::setSlicingActivated(bool isSlicing)
{
    if (isSlicing != d_ptr->m_isSlicingActivated)
        d_ptr->m_isSlicingActivated = isSlicing;
}

void Q3DScene::setLightPositionRelativeToCamera(const QVector3D &relativePosition, GLfloat fixedRotation, GLfloat distanceModifier)
{
    d_ptr->m_light->setPosition(d_ptr->m_camera->calculatePositionRelativeToCamera(relativePosition, fixedRotation, distanceModifier));
}


Q3DScenePrivate::Q3DScenePrivate(Q3DScene *q) :
    q_ptr(q),
    m_camera(0),
    m_light(0),
    m_isUnderSideCameraEnabled(false),
    m_isSlicingActivated(false)
{
}

Q3DScenePrivate::~Q3DScenePrivate()
{
    delete m_camera;
    delete m_light;
}

QT_DATAVISUALIZATION_END_NAMESPACE