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/qabstractanimation_p.h>
18#include <private/qhighdpiscaling_p.h>
19#include <private/qqmlmetatype_p.h>
20#include <private/qquickpixmap_p.h>
21#include <private/qquickview_p.h>
22#include <private/qv4compileddata_p.h>
26struct QuitLockDisabler
28 const bool quitLockEnabled;
30 Q_NODISCARD_CTOR QuitLockDisabler()
31 : quitLockEnabled(QCoreApplication::isQuitLockEnabled())
33 QCoreApplication::setQuitLockEnabled(
false);
38 QCoreApplication::setQuitLockEnabled(quitLockEnabled);
44 m_dummyItem.reset(
new QQuickItem);
50 const QString platformName = QGuiApplication::platformName();
51 m_supportsMultipleWindows = (platformName == QStringLiteral(
"windows")
52 || platformName == QStringLiteral(
"cocoa")
53 || platformName == QStringLiteral(
"xcb")
54 || platformName == QStringLiteral(
"wayland"));
56 QCoreApplication::instance()->installEventFilter(
this);
58 m_fpsTimer.setInterval(1000);
69 const QWindowList windows = QGuiApplication::allWindows();
70 for (QWindow *window : windows)
76 if (m_currentWindow && (event->type() == QEvent::Move) &&
77 qobject_cast<QQuickWindow*>(obj) == m_currentWindow) {
78 m_lastPosition.takePosition(m_currentWindow);
81 return QObject::eventFilter(obj, event);
86 return m_currentRootItem;
91 m_engines.append(qmlEngine);
96 const bool found = m_engines.removeOne(qmlEngine);
98 for (QObject *obj : m_createdObjects)
99 if (obj && ::qmlEngine(obj) == qmlEngine)
101 m_createdObjects.removeAll(
nullptr);
106 QSharedPointer<QuitLockDisabler> disabler(
new QuitLockDisabler);
109 m_component.reset(
nullptr);
110 QQuickPixmap::purgeCache();
112 const int numEngines = m_engines.size();
113 if (numEngines > 1) {
114 emit error(QString::fromLatin1(
"%1 QML engines available. We cannot decide which one "
115 "should load the component.").arg(numEngines));
117 }
else if (numEngines == 0) {
118 emit error(QLatin1String(
"No QML engines found."));
121 m_lastPosition.loadWindowPositionSettings(url);
123 QQmlEngine *engine = m_engines.front();
124 engine->clearSingletons();
125 engine->clearComponentCache();
126 m_component.reset(
new QQmlComponent(engine, url,
this));
128 auto onStatusChanged = [disabler,
this](QQmlComponent::Status status) {
130 case QQmlComponent::Null:
131 case QQmlComponent::Loading:
133 case QQmlComponent::Ready:
136 case QQmlComponent::Error:
137 emit error(m_component->errorString());
144 disconnect(m_component.data(), &QQmlComponent::statusChanged,
this,
nullptr);
148 if (onStatusChanged(m_component->status()))
149 connect(m_component.data(), &QQmlComponent::statusChanged,
this, onStatusChanged);
156 while (
const auto cu = QQmlMetaType::obtainCompilationUnit(url))
157 QQmlMetaType::unregisterInternalCompositeType(cu);
162 if (m_component.isNull() || !m_component->isReady())
163 emit error(QLatin1String(
"Component is not ready."));
165 QuitLockDisabler disabler;
173 m_zoomFactor = newFactor;
174 QTimer::singleShot(0,
this, &QQmlPreviewHandler::doZoom);
179 QUnifiedTimer::instance()->setSpeedModifier(newFactor);
184 if (!m_currentWindow)
186 if (qFuzzyIsNull(m_zoomFactor)) {
187 emit error(QString::fromLatin1(
"Zooming with factor: %1 will result in nothing "
188 "so it will be ignored.").arg(m_zoomFactor));
192 bool resetZoom =
false;
193 if (m_zoomFactor < 0) {
198 m_currentWindow->setGeometry(m_currentWindow->geometry());
200 m_lastPosition.takePosition(m_currentWindow, QQmlPreviewPosition::InitializePosition);
201 m_currentWindow->destroy();
203 for (QScreen *screen : QGuiApplication::screens())
204 QHighDpiScaling::setScreenFactor(screen, m_zoomFactor);
206 QHighDpiScaling::updateHighDpiScaling();
208 m_currentWindow->show();
209 m_lastPosition.initLastSavedWindowPosition(m_currentWindow);
214 qDeleteAll(m_createdObjects);
215 m_createdObjects.clear();
216 setCurrentWindow(
nullptr);
225 return flags | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint
226 | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint;
229 return flags | Qt::WindowTitleHint | Qt::WindowSystemMenuHint | Qt::WindowCloseButtonHint;
237 if (QWindow *window = qobject_cast<QWindow *>(object)) {
238 setCurrentWindow(qobject_cast<QQuickWindow *>(window));
239 for (QWindow *otherWindow : QGuiApplication::allWindows()) {
240 if (QQuickWindow *quickWindow = qobject_cast<QQuickWindow *>(otherWindow)) {
241 if (quickWindow == m_currentWindow)
243 quickWindow->setVisible(
false);
244 quickWindow->setFlags(quickWindow->flags() & ~Qt::WindowStaysOnTopHint);
247 }
else if (QQuickItem *item = qobject_cast<QQuickItem *>(object)) {
248 setCurrentWindow(
nullptr);
249 for (QWindow *window : QGuiApplication::allWindows()) {
250 if (QQuickWindow *quickWindow = qobject_cast<QQuickWindow *>(window)) {
251 if (m_currentWindow !=
nullptr) {
252 emit error(QLatin1String(
"Multiple QQuickWindows available. We cannot "
253 "decide which one to use."));
256 setCurrentWindow(quickWindow);
258 window->setVisible(
false);
259 window->setFlag(Qt::WindowStaysOnTopHint,
false);
263 if (m_currentWindow ==
nullptr) {
264 setCurrentWindow(
new QQuickWindow);
265 m_createdObjects.append(m_currentWindow.data());
268 for (QQuickItem *oldItem : m_currentWindow->contentItem()->childItems())
269 oldItem->setParentItem(m_dummyItem.data());
273 if (QQuickView *view = qobject_cast<QQuickView *>(m_currentWindow))
274 QQuickViewPrivate::get(view)->setRootObject(item);
276 item->setParentItem(m_currentWindow->contentItem());
278 m_currentWindow->resize(item->size().toSize());
280 m_currentRootItem = item;
282 emit error(QLatin1String(
"Created object is neither a QWindow nor a QQuickItem."));
285 if (m_currentWindow) {
286 m_lastPosition.initLastSavedWindowPosition(m_currentWindow);
287 m_currentWindow->setFlags(fixFlags(m_currentWindow->flags()) | Qt::WindowStaysOnTopHint);
288 m_currentWindow->setVisible(
true);
294 if (window == m_currentWindow.data())
297 if (m_currentWindow) {
298 disconnect(m_currentWindow.data(), &QQuickWindow::beforeSynchronizing,
300 disconnect(m_currentWindow.data(), &QQuickWindow::afterSynchronizing,
302 disconnect(m_currentWindow.data(), &QQuickWindow::beforeRendering,
304 disconnect(m_currentWindow.data(), &QQuickWindow::frameSwapped,
307 m_rendering = FrameTime();
308 m_synchronizing = FrameTime();
311 m_currentWindow = window;
313 if (m_currentWindow) {
314 connect(m_currentWindow.data(), &QQuickWindow::beforeSynchronizing,
316 connect(m_currentWindow.data(), &QQuickWindow::afterSynchronizing,
318 connect(m_currentWindow.data(), &QQuickWindow::beforeRendering,
320 connect(m_currentWindow.data(), &QQuickWindow::frameSwapped,
328 m_synchronizing.beginFrame();
334 if (m_rendering.elapsed >= 0)
335 m_rendering.endFrame();
336 m_synchronizing.recordFrame();
337 m_synchronizing.endFrame();
342 m_rendering.beginFrame();
347 m_rendering.recordFrame();
357 elapsed = timer.elapsed();
363 min =
static_cast<quint16>(qMax(0ll, elapsed));
365 max =
static_cast<quint16>(qMin(qint64(std::numeric_limits<quint16>::max()), elapsed));
366 total =
static_cast<quint16>(qBound(0ll, qint64(std::numeric_limits<quint16>::max()),
374 min = std::numeric_limits<quint16>::max();
383 m_synchronizing.number,
386 m_synchronizing.total,
397 m_synchronizing.reset();
402 if (!m_supportsMultipleWindows)
404 QObject *object = m_component->create();
405 m_createdObjects.append(object);
411#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 setAnimationSpeed(qreal newFactor)
void removeEngine(QQmlEngine *engine)
void addEngine(QQmlEngine *engine)
void loadUrl(const QUrl &url)
static void closeAllWindows()
Qt::WindowFlags fixFlags(Qt::WindowFlags flags)