57void QQmlInPlacePreviewHandler::connectToService(QQmlPreviewServiceImpl *service)
59 QQmlPreviewHandler::connectToService(service);
60 connect(
this, &QQmlInPlacePreviewHandler::hotReloadFailure, service,
61 &QQmlPreviewServiceImpl::forwardHotReloadFailure, Qt::DirectConnection);
62 connect(service, &QQmlPreviewServiceImpl::drop,
this, [
this](
const QUrl &url) {
63 if (!m_droppedUrls.contains(url))
64 m_droppedUrls.push_back(url);
66 connect(service, &QQmlPreviewServiceImpl::rerun,
this, [
this]() {
67 emit error(QLatin1String(
"You cannot rerun if in-place updates are enabled."));
69 connect(service, &QQmlPreviewServiceImpl::zoom,
this, [
this](qreal zoomFactor) {
70 QQmlPreviewPosition position;
71 zoomWindow(currentWindow(), zoomFactor, &position);
91 QMetaObject::invokeMethod(engine, [engine, url, receiver]() {
92 QV4::ExecutionEngine *v4 = engine->handle();
93 const QQmlRefPointer<QV4::ExecutableCompilationUnit> unit = v4->compilationUnitForUrl(url);
97 const std::vector<QObject *> objects =
98 v4->memoryManager->findObjectsForCompilationUnits({ unit->baseCompilationUnit() });
99 QPointer<QObject> found;
100 for (QObject *object : objects) {
101 QQmlData *ddata = QQmlData::get(object);
102 if (ddata && ddata->compilationUnit == unit && ddata->cuObjectIndex == 0) {
103 if (std::exchange(found, object))
109 QMetaObject::invokeMethod(receiver, [receiver, currentRoot = std::move(found)]() {
110 if (QQuickItem *rootItem = qobject_cast<QQuickItem *>(currentRoot)) {
111 receiver->setCurrentRootItem(rootItem);
112 receiver->setCurrentWindow(findCurrentWindow());
116 if (QQuickWindow *window = qobject_cast<QQuickWindow *>(currentRoot)) {
117 receiver->setCurrentWindow(window);
118 receiver->setCurrentRootItem(
nullptr);
122 receiver->setCurrentRootItem(
nullptr);
123 receiver->setCurrentWindow(findCurrentWindow());
131 QV4::ExecutionEngine *v4,
132 const QList<QQmlRefPointer<QV4::CompiledData::CompilationUnit>> &droppedUnits)
134 for (
const auto &oldCU : droppedUnits) {
135 if (
const auto newCU = QQmlMetaType::obtainCompilationUnit(oldCU->finalUrl()))
136 QQmlPreview::redirectResolvedTypeReferences(v4, oldCU, newCU);
143 const auto componentUpdateIt =
144 std::find_if(inplaceUpdate->pendingComponentUpdates.begin(),
145 inplaceUpdate->pendingComponentUpdates.end(),
147 return engineUpdate.component.get() == component;
150 if (componentUpdateIt == inplaceUpdate->pendingComponentUpdates.end())
153 componentUpdate =
std::move(*componentUpdateIt);
154 inplaceUpdate->pendingComponentUpdates.erase(componentUpdateIt);
156 switch (component->status()) {
157 case QQmlComponent::Null:
158 case QQmlComponent::Loading:
159 Q_UNREACHABLE_RETURN();
160 case QQmlComponent::Ready:
162 case QQmlComponent::Error:
163 inplaceUpdate->emitError(component->errorString());
167 QV4::ExecutionEngine *v4 = component->engine()->handle();
169 if (
const auto oldExecCU = componentUpdate.oldUnit) {
170 const auto newExecCU = QQmlComponentPrivate::get(component)->compilationUnit();
171 std::vector<QObject *> objects = v4->memoryManager->findObjectsForCompilationUnits(
172 { oldExecCU->baseCompilationUnit() });
174 const QQmlPreview::PatchResult result =
175 QQmlPreview::applyDiff(objects, oldExecCU, newExecCU);
176 if (result == QQmlPreview::PatchResult::Failed)
177 inplaceUpdate->emitReloadFailure(
"Could not apply diff");
179 componentUpdate.newUnit = newExecCU;
180 componentUpdate.patchResult = result;
181 inplaceUpdate->processedComponentUpdates.push_back(
std::move(componentUpdate));
186 if (inplaceUpdate->pendingComponentUpdates.empty()) {
187 updateResolvedTypeReferences(v4, inplaceUpdate->droppedUnits);
188 for (
const ComponentUpdate &update : inplaceUpdate->processedComponentUpdates) {
191 QQmlPreview::refreshBindings(
193 update.patchResult == QQmlPreview::PatchResult::PatchedInPlace ? update.newUnit
201 QV4::ExecutionEngine *v4 = inplaceUpdate->engine->handle();
202 std::vector<QQmlComponent *> components;
203 for (
const QUrl &url : urls) {
206 if (!v4->typeLoader()->removeFromCache(url))
210 QQmlRefPointer<QV4::ExecutableCompilationUnit> oldUnit = v4->compilationUnitForUrl(url);
214 inplaceUpdate->pendingComponentUpdates.push_back({
215 std::make_unique<QQmlComponent>(inplaceUpdate->engine, url),
218 QQmlPreview::PatchResult::Failed,
222 components.push_back(inplaceUpdate->pendingComponentUpdates.back().component.get());
225 for (QQmlComponent *component : components) {
226 switch (component->status()) {
227 case QQmlComponent::Null:
228 case QQmlComponent::Loading:
229 QObject::connect(component, &QQmlComponent::statusChanged, component,
230 [component, inplaceUpdate]() {
231 switch (component->status()) {
232 case QQmlComponent::Ready:
233 case QQmlComponent::Error:
234 updateInplace(component, inplaceUpdate);
241 case QQmlComponent::Ready:
242 case QQmlComponent::Error:
243 updateInplace(component, inplaceUpdate);
249void QQmlInPlacePreviewHandler::load(
const QUrl &url)
254 const QList<QQmlEngine *> seenEngines = engines();
255 const QList<QUrl> urls = std::exchange(m_droppedUrls, {});
257 QList<QQmlRefPointer<QV4::CompiledData::CompilationUnit>> droppedUnits;
258 for (
const QUrl &droppedUrl : urls) {
259 while (
const auto cu = QQmlMetaType::obtainCompilationUnit(droppedUrl)) {
260 droppedUnits.append(cu);
261 QQmlMetaType::deepClearCompositeType(cu);
264 for (QQmlEngine *engine : seenEngines) {
265 std::shared_ptr<InplaceUpdate> inplaceUpdate =
266 std::make_shared<InplaceUpdate>(
this, engine);
267 inplaceUpdate->droppedUnits = droppedUnits;
271 QMetaObject::invokeMethod(engine, [update = std::move(inplaceUpdate), urls]() {
272 updateEngine(update, urls);
280 if (seenEngines.size() == 1) {
281 findCurrentRootObject(seenEngines[0], url,
this);
283 setCurrentRootItem(
nullptr);
284 setCurrentWindow(findCurrentWindow());