summaryrefslogtreecommitdiffstats
path: root/src/datavis3d/engine/maps3dcontroller_p.h
blob: d0c2e74e7644e6bf5c67a979f92597e189200c43 (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
/****************************************************************************
**
** 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 QtDataVis3D 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
**
****************************************************************************/

//
//  W A R N I N G
//  -------------
//
// This file is not part of the QtDataVis3D API.  It exists purely as an
// implementation detail.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.

#ifndef MAPS3DCONTROLLER_P_H
#define MAPS3DCONTROLLER_P_H

#include "datavis3dglobal_p.h"
#include "q3dmaps.h"
#include "maprenderitem_p.h"
#include <QOpenGLFunctions>
#include <QFont>

//#define DISPLAY_RENDER_SPEED

class QPoint;
class QSizeF;

QT_DATAVIS3D_BEGIN_NAMESPACE

class ShaderHelper;
class ObjectHelper;
class TextureHelper;
class Theme;
class Drawer;
class Maps3DRenderer;
class CameraHelper;
class QMapDataProxy;

class QT_DATAVIS3D_EXPORT Maps3DController : public QObject, public QOpenGLFunctions
{
    Q_OBJECT
public:
    enum SelectionType {
        SelectionNone = 0,
        SelectionBar,
        SelectionRow,
        SelectionColumn
    };

    enum MousePressType {
        MouseNone = 0,
        MouseOnScene,
        MouseOnOverview,
        MouseOnZoom,
        MouseRotating,
        MouseOnPinch
    };

public:
    Maps3DController(const QRect &rect);
    ~Maps3DController();

    void initializeOpenGL();
    virtual void synchDataToRenderer();
    void render(const GLuint defaultFboHandle = 0);

    // bar specifications; base thickness in x, y and z, enum to indicate which direction is increased with value
    // TODO: Start using thickness also in adjustment direction; use it as a relative value.
    // For example, in AdjustAll mode setting thickness to (0.1f, 1.0f, 0.5f) would apply value to
    // x at 10%, y at 100% and z at 50%. If a dimension is not included, given thickness states its absolute value.
    void setBarSpecs(const QVector3D &thickness = QVector3D(1.0f, 1.0f, 1.0f),
                     Q3DMaps::AdjustmentDirection direction = Q3DMaps::AdjustHeight);

    // bar type; bars (=cubes), pyramids, cones, cylinders, balls, etc.
    void setBarType(QDataVis::MeshStyle style, bool smooth = false);

    // override bar type with own mesh
    void setMeshFileName(const QString &objFileName);

    // Select preset camera placement
    void setCameraPreset(QDataVis::CameraPreset preset);

    // Set camera rotation if you don't want to use the presets (in horizontal (-180...180) and
    // vertical (0...90) angles and distance in percentage (10...500))
    void setCameraPosition(GLfloat horizontal, GLfloat vertical, GLint distance = 100);

    // Set theme (bar colors, shaders, window color, background colors, light intensity and text colors are affected)
    void setTheme(QDataVis::ColorTheme theme);

    // Set color if you don't want to use themes. Set uniform to false if you want the (height) color to change from bottom to top
    void setBarColor(QColor baseColor, QColor heightColor, bool uniform = true);

    // Set area specs
    void setAreaSpecs(const QRect &areaRect, const QImage &image);

    // Set area image
    void setImage(const QImage &image);

    // TODO: light placement API

    // Change selection mode; single bar, bar and row, bar and column, or all
    void setSelectionMode(QDataVis::SelectionMode mode);
    QDataVis::SelectionMode selectionMode();

    // Font size adjustment
    void setFontSize(float fontsize);
    float fontSize();

    // Set font
    void setFont(const QFont &font);
    QFont font();

    // Label transparency adjustment
    void setLabelTransparency(QDataVis::LabelTransparency transparency);
    QDataVis::LabelTransparency labelTransparency();

    // Adjust shadow quality
    QDataVis::ShadowQuality setShadowQuality(QDataVis::ShadowQuality quality);
    QDataVis::ShadowQuality shadowQuality();

    // Size
    const QSize size();
    const QRect boundingRect();
    void setBoundingRect(const QRect boundingRect);
    void setWidth(const int width);
    int width();
    void setHeight(const int height);
    int height();
    void setX(const int x);
    int x();
    void setY(const int y);
    int y();

#if defined(Q_OS_ANDROID)
    void mouseDoubleClickEvent(QMouseEvent *event);
    void touchEvent(QTouchEvent *event);
#endif
    void mousePressEvent(QMouseEvent *event, const QPoint &mousePos);
    void mouseReleaseEvent(QMouseEvent *event, const QPoint &mousePos);
    void mouseMoveEvent(QMouseEvent *event, const QPoint &mousePos);
    void wheelEvent(QWheelEvent *event);
    void resizeNotify();

    void loadBarMesh();
    void loadBackgroundMesh();
    void loadGridLineMesh();
    void loadLabelMesh();
    void initShaders(const QString &vertexShader, const QString &fragmentShader);
    void initSelectionShader();
    void initBackgroundShaders(const QString &vertexShader, const QString &fragmentShader);
    void initLabelShaders(const QString &vertexShader, const QString &fragmentShader);
    void initSelectionBuffer();
#if !defined(QT_OPENGL_ES_2)
    void initDepthShader();
    void initDepthBuffer();
#endif
    void updateTextures();
    void calculateSceneScalingFactors(const QRect &areaRect);
    void calculateHeightAdjustment(const QPair<GLfloat, GLfloat> &limits);
    void calculateTranslation(MapRenderItem &item);
    SelectionType isSelected(GLint bar, const QVector3D &selection);
    bool isValid(const MapRenderItem &item);

    // Sets the data proxy. Assumes ownership of the data proxy. Deletes old proxy.
    void setDataProxy(QMapDataProxy *proxy);
    QMapDataProxy *dataProxy();

    void handleLimitChange();

public slots:
    void handleArrayReset();
    void handleItemsAdded(int startIndex, int count);
    void handleItemsChanged(int startIndex, int count);
    void handleItemsRemoved(int startIndex, int count);
    void handleItemsInserted(int startIndex, int count);

private:
    void drawScene(const GLuint defaultFboHandle);

    Maps3DRenderer *m_renderer;
    CameraHelper *m_camera;

    ShaderHelper *m_barShader;
    ShaderHelper *m_depthShader;
    ShaderHelper *m_selectionShader;
    ShaderHelper *m_backgroundShader;
    ShaderHelper *m_labelShader;
    ObjectHelper *m_barObj;
    ObjectHelper *m_backgroundObj;
    ObjectHelper *m_gridLineObj;
    ObjectHelper *m_labelObj;
    QString m_objFile;
    MousePressType m_mousePressed;
    QPoint m_mousePos;
    GLint m_zoomLevel;
    GLfloat m_autoScaleAdjustment;
    GLfloat m_horizontalRotation;
    GLfloat m_verticalRotation;
    QVector3D m_barThickness;
    GLfloat m_heightNormalizer;
    GLfloat m_yAdjustment;
    GLfloat m_scaleFactor;
    Theme *m_theme;
    bool m_isInitialized;
    QDataVis::SelectionMode m_selectionMode;
    BarRenderItem *m_selectedBar; // points to renderitem array
    BarRenderItem *m_previouslySelectedBar; // points to renderitem array
    QString m_axisLabelX;
    QString m_axisLabelZ;
    QString m_axisLabelY;
    QRect m_sceneViewPort;
    QRect m_zoomViewPort;
    bool m_zoomActivated;
    TextureHelper *m_textureHelper;
    QDataVis::LabelTransparency m_labelTransparency;
    QFont m_font;
    Drawer *m_drawer;
    QSizeF m_areaSize;
    GLuint m_bgrTexture;
    GLuint m_depthTexture;
    GLuint m_selectionTexture;
    GLuint m_depthFrameBuffer;
    GLuint m_selectionFrameBuffer;
    GLuint m_selectionDepthBuffer;
    bool m_updateLabels;
    Q3DMaps::AdjustmentDirection m_adjustDirection;
    QDataVis::ShadowQuality m_shadowQuality;
    GLfloat m_shadowQualityToShader;
    bool m_bgrHasAlpha;
    QRect m_boundingRect;
    QMapDataProxy *m_data;
    bool m_valuesDirty;
    MapRenderItemArray m_renderItemArray;
};

QT_DATAVIS3D_END_NAMESPACE

#endif