summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/Palettes/scenecamera/scenecamerascrollarea.cpp
blob: 035e242c1cdc4b3ff31fb0d064f42ece089308c0 (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
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt 3D Studio.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "Qt3DSCommonPrecompile.h"
#include "scenecamerascrollarea.h"
#include "scenecameraglwidget.h"
#include "Core.h"

#include <QtWidgets/qscrollbar.h>
#include <QtGui/qevent.h>

SceneCameraScrollArea::SceneCameraScrollArea(QWidget *parent)
    : QAbstractScrollArea(parent)
{
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);

    m_glWidget = new SceneCameraGlWidget(this);
}

SceneCameraScrollArea::~SceneCameraScrollArea()
{
}

void SceneCameraScrollArea::setZoom(qreal zoom, const QPoint &zoomPoint)
{
    // Calculate the actual presentation point
    qreal oldH = (horizontalScrollBar()->value() + zoomPoint.x()) / m_zoom;
    qreal oldV = (verticalScrollBar()->value() + zoomPoint.y()) / m_zoom;

    m_zoom = zoom;

    recalculateScrollRanges();

    // Move the scrollbars so that the actual presentation point stays in the same location
    horizontalScrollBar()->setValue(qRound(oldH * m_zoom - zoomPoint.x()));
    verticalScrollBar()->setValue(qRound(oldV * m_zoom - zoomPoint.y()));

    recalculateOffsets();

    Q_EMIT needUpdate();
}

void SceneCameraScrollArea::setPresentationSize(const QSize &size)
{
    if (m_presentationSize != size) {
        m_presentationSize = size;
        recalculateScrollRanges();
        recalculateOffsets();
    }
}

void SceneCameraScrollArea::recalculateScrollRanges()
{
    const QSizeF presSize = zoomedPresentationSize();

    const QSize viewSize = viewport()->size();
    horizontalScrollBar()->setRange(0, int(presSize.width() - viewSize.width()));
    verticalScrollBar()->setRange(0, int(presSize.height() - viewSize.height()));
    horizontalScrollBar()->setPageStep(viewSize.width());
    verticalScrollBar()->setPageStep(viewSize.height());
}

void SceneCameraScrollArea::recalculateOffsets()
{
    // Texture offset vector contains normalized rect of the viewable area of the texture
    const QSize viewSize = viewport()->size();
    const qreal fullWidth = qreal(horizontalScrollBar()->maximum() + viewSize.width());
    const qreal fullHeight = qreal(verticalScrollBar()->maximum() + viewSize.height());
    QVector4D textureOffset(
                float(horizontalScrollBar()->value() / fullWidth),
                float((verticalScrollBar()->maximum() - verticalScrollBar()->value()) / fullHeight),
                float(viewSize.width() / fullWidth), float(viewSize.height() / fullHeight));

    m_glWidget->setTextureOffset(textureOffset);

    // The geometry offset is adjusted to keep aspect ratio when view area is larger than
    // zoomed width/height. Since the geometry of the quad is in range [-1, 1], the width/height of
    // the offset is just a direct multiplier to the coordinate.
    // XY contain the subpixel offset to ensure we don't get artifacts depending on pixel alignment.
    const QSizeF presSize = zoomedPresentationSize();
    float subPixelX = 0.0f;
    float subPixelY = 0.0f;
    qreal normWidth = 1.0;
    qreal normHeight = 1.0;
    if (presSize.width() < fullWidth) {
        qreal diffX = (fullWidth - qRound(presSize.width())) / 2.0;
        subPixelX = float((diffX - qRound(diffX)) / fullWidth);
        normWidth = presSize.width() / fullWidth;
    }
    if (presSize.height() < fullHeight) {
        qreal diffY = (fullHeight - qRound(presSize.height())) / 2.0;
        subPixelY = float((diffY - qRound(diffY)) / fullHeight);
        normHeight = presSize.height() / fullHeight;
    }

    QVector4D geometryOffset(subPixelX, subPixelY, float(normWidth), float(normHeight));
    m_glWidget->setGeometryOffset(geometryOffset);
}

void SceneCameraScrollArea::scrollContentsBy(int, int)
{
    recalculateOffsets();
    Q_EMIT needUpdate();
}

void SceneCameraScrollArea::showEvent(QShowEvent *event)
{
    QAbstractScrollArea::showEvent(event);

    recalculateScrollRanges();
    recalculateOffsets();
    resizeGlWidget();
}

void SceneCameraScrollArea::resizeGlWidget()
{
    m_glWidget->resize(viewport()->size());
}

QSizeF SceneCameraScrollArea::zoomedPresentationSize()
{
    // Multiply QSize components separately to avoid rounding to integers
    QSizeF size = QSizeF(m_presentationSize.width() * m_zoom,
                         m_presentationSize.height() * m_zoom);
    return size;
}

void SceneCameraScrollArea::resizeEvent(QResizeEvent *event)
{
    QAbstractScrollArea::resizeEvent(event);

    recalculateScrollRanges();
    recalculateOffsets();
    resizeGlWidget();
}