Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qqmlpreviewhandler.cpp
Go to the documentation of this file.
1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant
4
7
8#include <QtCore/qtimer.h>
9#include <QtCore/qsettings.h>
10#include <QtCore/qlibraryinfo.h>
11
12#include <QtGui/qwindow.h>
13#include <QtGui/qguiapplication.h>
14#include <QtQuick/qquickwindow.h>
15#include <QtQuick/qquickitem.h>
16
17#include <private/qabstractanimation_p.h>
18#include <private/qhighdpiscaling_p.h>
19
21
23{
24 m_fpsTimer.setInterval(1000);
25 connect(&m_fpsTimer, &QTimer::timeout, this, &QQmlPreviewHandler::fpsTimerHit);
26}
27
28QQmlPreviewHandler::~QQmlPreviewHandler() = default;
29
31{
32 return m_currentRootItem;
33}
34
36{
37 m_currentRootItem = item;
38}
39
40void QQmlPreviewHandler::addEngine(QQmlEngine *qmlEngine)
41{
42 m_engines.append(qmlEngine);
43}
44
45void QQmlPreviewHandler::removeEngine(QQmlEngine *qmlEngine)
46{
47 m_engines.removeOne(qmlEngine);
48}
49
51{
52 QUnifiedTimer::instance()->setSpeedModifier(newFactor);
53}
54
55void QQmlPreviewHandler::zoomWindow(QQuickWindow *window, qreal zoomFactor,
56 QQmlPreviewPosition *position)
57{
58 if (!window)
59 return;
60 if (qFuzzyIsNull(zoomFactor)) {
61 emit error(QString::fromLatin1("Zooming with factor: %1 will result in nothing "
62 "so it will be ignored.")
63 .arg(zoomFactor));
64 return;
65 }
66
67 bool resetZoom = false;
68 if (zoomFactor < 0) {
69 resetZoom = true;
70 zoomFactor = 1.0;
71 }
72
73 window->setGeometry(window->geometry());
74
76 window->destroy();
77
78 for (QScreen *screen : QGuiApplication::screens())
79 QHighDpiScaling::setScreenFactor(screen, zoomFactor);
80 if (resetZoom)
81 QHighDpiScaling::updateHighDpiScaling();
82
83 window->show();
84 position->initLastSavedWindowPosition(window);
85}
86
87void QQmlPreviewHandler::disconnectWindow(QQuickWindow *window)
88{
89 disconnect(window, &QQuickWindow::beforeSynchronizing,
90 this, &QQmlPreviewHandler::beforeSynchronizing);
91 disconnect(window, &QQuickWindow::afterSynchronizing,
92 this, &QQmlPreviewHandler::afterSynchronizing);
93 disconnect(window, &QQuickWindow::beforeRendering,
94 this, &QQmlPreviewHandler::beforeRendering);
95 disconnect(window, &QQuickWindow::frameSwapped,
96 this, &QQmlPreviewHandler::frameSwapped);
97 m_fpsTimer.stop();
98 m_rendering = FrameTime();
99 m_synchronizing = FrameTime();
100}
101
102void QQmlPreviewHandler::connectWindow(QQuickWindow *window)
103{
104 connect(window, &QQuickWindow::beforeSynchronizing,
105 this, &QQmlPreviewHandler::beforeSynchronizing, Qt::DirectConnection);
106 connect(window, &QQuickWindow::afterSynchronizing,
107 this, &QQmlPreviewHandler::afterSynchronizing, Qt::DirectConnection);
108 connect(window, &QQuickWindow::beforeRendering,
109 this, &QQmlPreviewHandler::beforeRendering, Qt::DirectConnection);
110 connect(window, &QQuickWindow::frameSwapped,
111 this, &QQmlPreviewHandler::frameSwapped, Qt::DirectConnection);
112 m_fpsTimer.start();
113}
114
116{
117 connect(service, &QQmlPreviewServiceImpl::load, this, &QQmlPreviewHandler::load);
118 connect(service, &QQmlPreviewServiceImpl::animationSpeed,
119 this, &QQmlPreviewHandler::setAnimationSpeed);
120 connect(this, &QQmlPreviewHandler::error,
121 service, &QQmlPreviewServiceImpl::forwardError, Qt::DirectConnection);
122 connect(this, &QQmlPreviewHandler::fps,
123 service, &QQmlPreviewServiceImpl::forwardFps, Qt::DirectConnection);
124}
125
126void QQmlPreviewHandler::beforeSynchronizing()
127{
128 m_synchronizing.beginFrame();
129}
130
131void QQmlPreviewHandler::afterSynchronizing()
132{
133
134 if (m_rendering.elapsed >= 0)
135 m_rendering.endFrame();
136 m_synchronizing.recordFrame();
137 m_synchronizing.endFrame();
138}
139
140void QQmlPreviewHandler::beforeRendering()
141{
142 m_rendering.beginFrame();
143}
144
145void QQmlPreviewHandler::frameSwapped()
146{
147 m_rendering.recordFrame();
148}
149
151{
152 timer.start();
153}
154
156{
157 elapsed = timer.elapsed();
158}
159
161{
162 if (elapsed < min)
163 min = static_cast<quint16>(qMax(0ll, elapsed));
164 if (elapsed > max)
165 max = static_cast<quint16>(qMin(qint64(std::numeric_limits<quint16>::max()), elapsed));
166 total = static_cast<quint16>(qBound(0ll, qint64(std::numeric_limits<quint16>::max()),
167 elapsed + total));
168 ++number;
169 elapsed = -1;
170}
171
173{
174 min = std::numeric_limits<quint16>::max();
175 max = 0;
176 total = 0;
177 number = 0;
178}
179
180void QQmlPreviewHandler::fpsTimerHit()
181{
182 const FpsInfo info = {
183 m_synchronizing.number,
184 m_synchronizing.min,
185 m_synchronizing.max,
186 m_synchronizing.total,
187
188 m_rendering.number,
189 m_rendering.min,
190 m_rendering.max,
191 m_rendering.total
192 };
193
194 emit fps(info);
195
196 m_rendering.reset();
197 m_synchronizing.reset();
198}
199
200QQuickWindow *QQmlPreviewHandler::currentWindow() const
201{
202 return m_currentWindow.data();
203}
204
205void QQmlPreviewHandler::setCurrentWindow(QQuickWindow *window)
206{
207 if (window == m_currentWindow.data())
208 return;
209
210 if (m_currentWindow)
211 disconnectWindow(m_currentWindow.data());
212
213 m_currentWindow = window;
214
215 if (m_currentWindow)
216 connectWindow(m_currentWindow.data());
217}
218
219
220QT_END_NAMESPACE
221
222#include "moc_qqmlpreviewhandler.cpp"
QObject * parent
Definition qobject.h:74
\inmodule QtCore
Definition qobject.h:106
void disconnectWindow(QQuickWindow *window)
void setCurrentRootItem(QQuickItem *item)
void zoomWindow(QQuickWindow *window, qreal zoomFactor, QQmlPreviewPosition *position)
QQuickItem * currentRootItem() const
void setCurrentWindow(QQuickWindow *window)
void connectWindow(QQuickWindow *window)
void setAnimationSpeed(qreal newFactor)
virtual void removeEngine(QQmlEngine *engine)
virtual void addEngine(QQmlEngine *engine)
QQuickWindow * currentWindow() const
virtual void connectToService(QQmlPreviewServiceImpl *service)
void takePosition(QWindow *window, InitializeState state=PositionInitialized)
void initLastSavedWindowPosition(QWindow *window)
Combined button and popup list for selecting options.