7#include <QtCore/qtimer.h>
8#include <QtCore/qsettings.h>
9#include <QtCore/qlibraryinfo.h>
11#include <QtGui/qwindow.h>
12#include <QtGui/qguiapplication.h>
13#include <QtQuick/qquickwindow.h>
14#include <QtQuick/qquickitem.h>
15#include <QtQml/qqmlcomponent.h>
17#include <private/qhighdpiscaling_p.h>
18#include <private/qqmlmetatype_p.h>
19#include <private/qquickpixmap_p.h>
20#include <private/qquickview_p.h>
21#include <private/qv4compileddata_p.h>
25struct QuitLockDisabler
27 const bool quitLockEnabled;
29 Q_NODISCARD_CTOR QuitLockDisabler()
30 : quitLockEnabled(QCoreApplication::isQuitLockEnabled())
32 QCoreApplication::setQuitLockEnabled(
false);
37 QCoreApplication::setQuitLockEnabled(quitLockEnabled);
43 m_dummyItem.reset(
new QQuickItem);
49 const QString platformName = QGuiApplication::platformName();
50 m_supportsMultipleWindows = (platformName == QStringLiteral(
"windows")
51 || platformName == QStringLiteral(
"cocoa")
52 || platformName == QStringLiteral(
"xcb")
53 || platformName == QStringLiteral(
"wayland"));
55 QCoreApplication::instance()->installEventFilter(
this);
57 m_fpsTimer.setInterval(1000);
58 connect(&m_fpsTimer, &QTimer::timeout,
this, &QQmlPreviewHandler::fpsTimerHit);
68 const QWindowList windows = QGuiApplication::allWindows();
69 for (QWindow *window : windows)
75 if (m_currentWindow && (event->type() == QEvent::Move) &&
76 qobject_cast<QQuickWindow*>(obj) == m_currentWindow) {
77 m_lastPosition.takePosition(m_currentWindow);
80 return QObject::eventFilter(obj, event);
85 return m_currentRootItem;
90 m_engines.append(qmlEngine);
95 const bool found = m_engines.removeOne(qmlEngine);
97 for (QObject *obj : m_createdObjects)
98 if (obj && ::qmlEngine(obj) == qmlEngine)
100 m_createdObjects.removeAll(
nullptr);
105 QSharedPointer<QuitLockDisabler> disabler(
new QuitLockDisabler);
108 m_component.reset(
nullptr);
109 QQuickPixmap::purgeCache();
111 const int numEngines = m_engines.size();
112 if (numEngines > 1) {
113 emit error(QString::fromLatin1(
"%1 QML engines available. We cannot decide which one "
114 "should load the component.").arg(numEngines));
116 }
else if (numEngines == 0) {
117 emit error(QLatin1String(
"No QML engines found."));
120 m_lastPosition.loadWindowPositionSettings(url);
122 QQmlEngine *engine = m_engines.front();
123 engine->clearSingletons();
124 engine->clearComponentCache();
125 m_component.reset(
new QQmlComponent(engine, url,
this));
127 auto onStatusChanged = [disabler,
this](QQmlComponent::Status status) {
129 case QQmlComponent::Null:
130 case QQmlComponent::Loading:
132 case QQmlComponent::Ready:
135 case QQmlComponent::Error:
136 emit error(m_component->errorString());
143 disconnect(m_component.data(), &QQmlComponent::statusChanged,
this,
nullptr);
147 if (onStatusChanged(m_component->status()))
148 connect(m_component.data(), &QQmlComponent::statusChanged,
this, onStatusChanged);
154 if (
const auto cu = QQmlMetaType::obtainCompilationUnit(url))
155 QQmlMetaType::unregisterInternalCompositeType(cu);
160 if (m_component.isNull() || !m_component->isReady())
161 emit error(QLatin1String(
"Component is not ready."));
163 QuitLockDisabler disabler;
171 m_zoomFactor = newFactor;
172 QTimer::singleShot(0,
this, &QQmlPreviewHandler::doZoom);
177 if (!m_currentWindow)
179 if (qFuzzyIsNull(m_zoomFactor)) {
180 emit error(QString::fromLatin1(
"Zooming with factor: %1 will result in nothing "
181 "so it will be ignored.").arg(m_zoomFactor));
185 bool resetZoom =
false;
186 if (m_zoomFactor < 0) {
191 m_currentWindow->setGeometry(m_currentWindow->geometry());
193 m_lastPosition.takePosition(m_currentWindow, QQmlPreviewPosition::InitializePosition);
194 m_currentWindow->destroy();
196 for (QScreen *screen : QGuiApplication::screens())
197 QHighDpiScaling::setScreenFactor(screen, m_zoomFactor);
199 QHighDpiScaling::updateHighDpiScaling();
201 m_currentWindow->show();
202 m_lastPosition.initLastSavedWindowPosition(m_currentWindow);
207 qDeleteAll(m_createdObjects);
208 m_createdObjects.clear();
209 setCurrentWindow(
nullptr);
218 return flags | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint
219 | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint;
222 return flags | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
230 if (QWindow *window = qobject_cast<QWindow *>(object)) {
231 setCurrentWindow(qobject_cast<QQuickWindow *>(window));
232 for (QWindow *otherWindow : QGuiApplication::allWindows()) {
233 if (QQuickWindow *quickWindow = qobject_cast<QQuickWindow *>(otherWindow)) {
234 if (quickWindow == m_currentWindow)
236 quickWindow->setVisible(
false);
237 quickWindow->setFlags(quickWindow->flags() & ~Qt::WindowStaysOnTopHint);
240 }
else if (QQuickItem *item = qobject_cast<QQuickItem *>(object)) {
241 setCurrentWindow(
nullptr);
242 for (QWindow *window : QGuiApplication::allWindows()) {
243 if (QQuickWindow *quickWindow = qobject_cast<QQuickWindow *>(window)) {
244 if (m_currentWindow !=
nullptr) {
245 emit error(QLatin1String(
"Multiple QQuickWindows available. We cannot "
246 "decide which one to use."));
249 setCurrentWindow(quickWindow);
251 window->setVisible(
false);
252 window->setFlag(Qt::WindowStaysOnTopHint,
false);
256 if (m_currentWindow ==
nullptr) {
257 setCurrentWindow(
new QQuickWindow);
258 m_createdObjects.append(m_currentWindow.data());
261 for (QQuickItem *oldItem : m_currentWindow->contentItem()->childItems())
262 oldItem->setParentItem(m_dummyItem.data());
266 if (QQuickView *view = qobject_cast<QQuickView *>(m_currentWindow))
267 QQuickViewPrivate::get(view)->setRootObject(item);
269 item->setParentItem(m_currentWindow->contentItem());
271 m_currentWindow->resize(item->size().toSize());
273 m_currentRootItem = item;
275 emit error(QLatin1String(
"Created object is neither a QWindow nor a QQuickItem."));
278 if (m_currentWindow) {
279 m_lastPosition.initLastSavedWindowPosition(m_currentWindow);
280 m_currentWindow->setFlags(fixFlags(m_currentWindow->flags()) | Qt::WindowStaysOnTopHint);
281 m_currentWindow->setVisible(
true);
287 if (window == m_currentWindow.data())
290 if (m_currentWindow) {
291 disconnect(m_currentWindow.data(), &QQuickWindow::beforeSynchronizing,
292 this, &QQmlPreviewHandler::beforeSynchronizing);
293 disconnect(m_currentWindow.data(), &QQuickWindow::afterSynchronizing,
294 this, &QQmlPreviewHandler::afterSynchronizing);
295 disconnect(m_currentWindow.data(), &QQuickWindow::beforeRendering,
296 this, &QQmlPreviewHandler::beforeRendering);
297 disconnect(m_currentWindow.data(), &QQuickWindow::frameSwapped,
298 this, &QQmlPreviewHandler::frameSwapped);
300 m_rendering = FrameTime();
301 m_synchronizing = FrameTime();
304 m_currentWindow = window;
306 if (m_currentWindow) {
307 connect(m_currentWindow.data(), &QQuickWindow::beforeSynchronizing,
308 this, &QQmlPreviewHandler::beforeSynchronizing, Qt::DirectConnection);
309 connect(m_currentWindow.data(), &QQuickWindow::afterSynchronizing,
310 this, &QQmlPreviewHandler::afterSynchronizing, Qt::DirectConnection);
311 connect(m_currentWindow.data(), &QQuickWindow::beforeRendering,
312 this, &QQmlPreviewHandler::beforeRendering, Qt::DirectConnection);
313 connect(m_currentWindow.data(), &QQuickWindow::frameSwapped,
314 this, &QQmlPreviewHandler::frameSwapped, Qt::DirectConnection);
321 m_synchronizing.beginFrame();
327 if (m_rendering.elapsed >= 0)
328 m_rendering.endFrame();
329 m_synchronizing.recordFrame();
330 m_synchronizing.endFrame();
335 m_rendering.beginFrame();
340 m_rendering.recordFrame();
350 elapsed = timer.elapsed();
356 min =
static_cast<quint16>(qMax(0ll, elapsed));
358 max =
static_cast<quint16>(qMin(qint64(std::numeric_limits<quint16>::max()), elapsed));
359 total =
static_cast<quint16>(qBound(0ll, qint64(std::numeric_limits<quint16>::max()),
367 min = std::numeric_limits<quint16>::max();
376 m_synchronizing.number,
379 m_synchronizing.total,
390 m_synchronizing.reset();
395 if (!m_supportsMultipleWindows)
397 QObject *object = m_component->create();
398 m_createdObjects.append(object);
404#include "moc_qqmlpreviewhandler.cpp"
void dropCU(const QUrl &url)
void zoom(qreal newFactor)
QQuickItem * currentRootItem()
bool eventFilter(QObject *obj, QEvent *event) override
Filters events if this object has been installed as an event filter for the watched object.
void removeEngine(QQmlEngine *engine)
void addEngine(QQmlEngine *engine)
void loadUrl(const QUrl &url)
static void closeAllWindows()
Qt::WindowFlags fixFlags(Qt::WindowFlags flags)