summaryrefslogtreecommitdiffstats
path: root/src/Authoring/Studio/UI/PlayerContainerWnd.cpp
blob: e077cf9e3f39306c256ec36d9a5f204024ae37ce (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/****************************************************************************
**
** Copyright (C) 2002 NVIDIA Corporation.
** Copyright (C) 2017 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 "SceneView.h"
#include "Doc.h"
#include "StudioProjectSettings.h"
#include "Dispatch.h"
#include "HotKeys.h"
#include "MasterP.h"
#include "StudioApp.h"
#include "IStudioRenderer.h"
#include "PlatformStrings.h"
#include "PlayerContainerWnd.h"
#include "Qt3DSDMStudioSystem.h"
#include "Core.h"
#include "MainFrm.h"
#include "StudioUtils.h"

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

CPlayerContainerWnd::CPlayerContainerWnd(CSceneView *inSceneView)
    : QScrollArea(inSceneView)
    , m_SceneView(inSceneView)
    , m_PlayerWnd(NULL)
    , m_ViewMode(VIEW_SCENE)
{
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}

CPlayerContainerWnd::~CPlayerContainerWnd()
{
}

bool CPlayerContainerWnd::ShouldHideScrollBars()
{
    return m_ViewMode == VIEW_EDIT || g_StudioApp.IsAuthorZoom();
}

//==============================================================================
/**
 * SetPlayerWndPosition: Sets the position of the child player window
 *
 * Called when the view is scrolled to position the child player window
 * @param outLeftPresentationEdge   the left edge of the presentation, with the scroll position
 *                                  taken into consideration
 * @param outTopPresentionEdge      the top edge of the presentation, with the scroll
 *                                  position taken into consideration
 *
 */
//==============================================================================
void CPlayerContainerWnd::SetPlayerWndPosition(long &outLeftPresentationEdge,
                                               long &outTopPresentionEdge)
{
    long theHScrollPosition, theVScrollPosition;
    // Negate to adjust actual client positions
    theHScrollPosition = -horizontalScrollBar()->value();
    theVScrollPosition = -verticalScrollBar()->value();

    QRect theClientRect = rect();

    // Horizontal scrollbar does not exist
    if (m_ClientRect.width() < theClientRect.width()) {
        theHScrollPosition =
                theClientRect.left() + (theClientRect.width() / 2) - (m_ClientRect.width() / 2);
        outLeftPresentationEdge = theHScrollPosition;
    } else {
        // This stays a negated value
        outLeftPresentationEdge = theHScrollPosition;
    }

    // Vertical scrollbar does not exist
    if (m_ClientRect.height() < theClientRect.height()) {
        theVScrollPosition =
                theClientRect.top() + (theClientRect.height() / 2) - (m_ClientRect.height() / 2);
        outTopPresentionEdge = theVScrollPosition;
    } else {
        // This stays a negated value
        outTopPresentionEdge = theVScrollPosition;
    }

    // Move the child player window
    m_PlayerWnd->setGeometry(QRect(QPoint(theHScrollPosition, theVScrollPosition),
                                   m_ClientRect.size()));
}

//==============================================================================
/**
 *	SetScrollRanges: Sets the scroll ranges when the view is being resized
 */
//==============================================================================
void CPlayerContainerWnd::SetScrollRanges()
{

    long theScrollWidth = 0;
    long theScrollHeight = 0;

    if (ShouldHideScrollBars()) {
        horizontalScrollBar()->setRange(0, 0);
        verticalScrollBar()->setRange(0, 0);
        horizontalScrollBar()->setValue(0);
        verticalScrollBar()->setValue(0);
    } else {
        QSize theSize = GetEffectivePresentationSize();

        theScrollWidth = theSize.width();
        theScrollHeight = theSize.height();

        // Set scrollbar ranges
        horizontalScrollBar()->setRange(0, theScrollWidth - width());
        verticalScrollBar()->setRange(0, theScrollHeight - height());
        horizontalScrollBar()->setPageStep(width());
        verticalScrollBar()->setPageStep(height());
        horizontalScrollBar()->setVisible(true);
        verticalScrollBar()->setVisible(true);
    }
}

//==============================================================================
/**
 *	OnRulerGuideToggled:
 *	Handle scrollbar position when ruler, guide has been toggled
 */
//==============================================================================
void CPlayerContainerWnd::OnRulerGuideToggled()
{
    int scrollAmount = g_StudioApp.getRenderer().AreGuidesEnabled() ? 16 : -16;
    bool hasHorz = horizontalScrollBar()->isVisible();
    bool hasVert = verticalScrollBar()->isVisible();
    int hscrollPos = 0, vscrollPos = 0;
    if (hasHorz)
        hscrollPos = qMax(horizontalScrollBar()->value() + scrollAmount, 0);
    if (hasVert)
        vscrollPos = qMax(verticalScrollBar()->value() + scrollAmount, 0);
    horizontalScrollBar()->setValue(hscrollPos);
    verticalScrollBar()->setValue(vscrollPos);
    m_PlayerWnd->update();
}

qreal CPlayerContainerWnd::fixedDevicePixelRatio() const
{
    // Fix a problem on X11: https://bugreports.qt.io/browse/QTBUG-65570
    qreal ratio = devicePixelRatio();
    if (QWindow *w = window()->windowHandle()) {
        if (QScreen *s = w->screen())
            ratio = s->devicePixelRatio();
    }
    return ratio;
}

//==============================================================================
/**
 *	RecenterClient: Recenters the Client rect in the View's client area.
 */
//==============================================================================
void CPlayerContainerWnd::RecenterClient()
{
    QRect theViewClientRect = rect();
    QSize theClientSize;
    m_ClientRect = theViewClientRect;

    if (!ShouldHideScrollBars()) {
        theClientSize = GetEffectivePresentationSize();

        // Only center if we need to scroll
        if (theClientSize.width() > theViewClientRect.width()) {
            // compute the size of the client rectangle to display
            m_ClientRect.setLeft(
                        (theViewClientRect.width() / 2) - (theClientSize.width() / 2)
                        - (theClientSize.width() % 2));
            m_ClientRect.setRight((theViewClientRect.width() / 2) + (theClientSize.width() / 2));
        }

        if (theClientSize.height() > theViewClientRect.height()) {
            m_ClientRect.setTop(
                        (theViewClientRect.height() / 2) - (theClientSize.height() / 2)
                        - (theClientSize.height() % 2));
            m_ClientRect.setBottom((theViewClientRect.height() / 2) + (theClientSize.height() / 2));
        }
    }

    QRect glRect = m_ClientRect;
    glRect.setWidth(int(fixedDevicePixelRatio() * m_ClientRect.width()));
    glRect.setHeight(int(fixedDevicePixelRatio() * m_ClientRect.height()));
    g_StudioApp.getRenderer().SetViewRect(glRect);
}

void CPlayerContainerWnd::wheelEvent(QWheelEvent* event)
{
    const bool theCtrlKeyIsDown = event->modifiers() & Qt::ControlModifier;

    if (!theCtrlKeyIsDown && !IsDeploymentView()) {
        // Zoom when in edit camera view
        g_StudioApp.GetCore()->GetDispatch()->FireSceneMouseWheel(
                    SceneDragSenderType::Matte, event->delta(), STUDIO_TOOLMODE_CAMERA_ZOOM);
    } else {
        // Otherwise, scroll the view
        QScrollArea::wheelEvent(event);
    }
}

void CPlayerContainerWnd::scrollContentsBy(int, int)
{
    long x, y;
    SetPlayerWndPosition(x, y);
}

//==============================================================================
/**
 *	Set the view mode of the current scene view, whether we are in editing mode
 *	or deployment mode. For editing mode, we want to use the full scene area without
 *	any matte area.
 *	@param inViewMode	the view mode of this scene
 */
void CPlayerContainerWnd::SetViewMode(EViewMode inViewMode)
{
    m_ViewMode = inViewMode;
    m_SceneView->recheckSizingMode();

    // When viewmode changes, update scene camera view mode status
    // into renderer context
    g_StudioApp.getRenderer().setIsSceneCameraView(IsDeploymentView());
}

//==============================================================================
/**
 *	return the view mode of the current scene view, whether we are in editing mode
 *	or deployment mode. For editing mode, we want to use the full scene area without
 *	any matte area.
 *	@return  the current view mode
 */
CPlayerContainerWnd::EViewMode CPlayerContainerWnd::GetViewMode()
{
    return m_ViewMode;
}

//==============================================================================
/**
 *	Checks whether we are in deployment view mode.
 *	@return true if is in deployment view mode, else false
 */
bool CPlayerContainerWnd::IsDeploymentView()
{
    return m_ViewMode == VIEW_SCENE ? true : false;
}

void CPlayerContainerWnd::setToolMode(long inMode)
{
    if (m_PlayerWnd)
        m_PlayerWnd->setToolMode(inMode);
}

QSize CPlayerContainerWnd::GetEffectivePresentationSize() const
{
    QSize theSize = g_StudioApp.GetCore()->GetStudioProjectSettings()->getPresentationSize();

    // If we have guides, resize the window with enough space for the guides as well as the
    // presentation
    // This is a very dirty hack because we are of course hardcoding the size of the guides.
    // If the size of the guides never changes, the bet paid off.
    if (g_StudioApp.getRenderer().AreGuidesEnabled())
        theSize += QSize(32, 32);

    return theSize / fixedDevicePixelRatio();
}

//==============================================================================

void CPlayerContainerWnd::SetPlayerWnd(CPlayerWnd *inPlayerWnd)
{
    m_PlayerWnd = inPlayerWnd;
    viewport()->setBackgroundRole(QPalette::Dark);
    m_PlayerWnd->setContainerWnd(this);
    m_PlayerWnd->setParent(viewport());
    m_PlayerWnd->setVisible(true);
    m_PlayerWnd->resize(m_PlayerWnd->sizeHint());
}

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

    SetScrollRanges();
}