Changeset View
Changeset View
Standalone View
Standalone View
src/pagepool.cpp
Show All 32 Lines | |||||
33 | 33 | | |||
34 | void PagePool::setCachePages(bool cache) | 34 | void PagePool::setCachePages(bool cache) | ||
35 | { | 35 | { | ||
36 | if (cache == m_cachePages) { | 36 | if (cache == m_cachePages) { | ||
37 | return; | 37 | return; | ||
38 | } | 38 | } | ||
39 | 39 | | |||
40 | if (cache) { | 40 | if (cache) { | ||
41 | for (auto *c : m_componentForUrl.values()) { | 41 | clear(); | ||
42 | c->deleteLater(); | | |||
43 | } | | |||
44 | m_componentForUrl.clear(); | | |||
45 | | ||||
46 | for (auto *i : m_itemForUrl.values()) { | | |||
47 | // items that had been deparented are safe to delete | | |||
48 | if (!i->parentItem()) { | | |||
49 | i->deleteLater(); | | |||
50 | } | | |||
51 | QQmlEngine::setObjectOwnership(i, QQmlEngine::JavaScriptOwnership); | | |||
52 | } | | |||
53 | m_itemForUrl.clear(); | | |||
54 | } | 42 | } | ||
55 | 43 | | |||
56 | m_cachePages = cache; | 44 | m_cachePages = cache; | ||
57 | emit cachePagesChanged(); | 45 | emit cachePagesChanged(); | ||
58 | } | 46 | } | ||
59 | 47 | | |||
60 | bool PagePool::cachePages() const | 48 | bool PagePool::cachePages() const | ||
61 | { | 49 | { | ||
Show All 9 Lines | 58 | QQuickItem *PagePool::loadPageWithProperties( | |||
71 | const QString &url, const QVariantMap &properties, QJSValue callback) | 59 | const QString &url, const QVariantMap &properties, QJSValue callback) | ||
72 | { | 60 | { | ||
73 | Q_ASSERT(qmlEngine(this)); | 61 | Q_ASSERT(qmlEngine(this)); | ||
74 | QQmlContext *ctx = QQmlEngine::contextForObject(this); | 62 | QQmlContext *ctx = QQmlEngine::contextForObject(this); | ||
75 | Q_ASSERT(ctx); | 63 | Q_ASSERT(ctx); | ||
76 | 64 | | |||
77 | const QUrl actualUrl = resolvedUrl(url); | 65 | const QUrl actualUrl = resolvedUrl(url); | ||
78 | 66 | | |||
79 | QQuickItem *foundItem = nullptr; | 67 | auto found = m_itemForUrl.find(actualUrl); | ||
80 | if (actualUrl == m_lastLoadedUrl && m_lastLoadedItem) { | 68 | if (found != m_itemForUrl.end()) { | ||
81 | foundItem = m_lastLoadedItem; | 69 | m_lastLoadedUrl = found.key(); | ||
82 | } else if (m_itemForUrl.contains(actualUrl)) { | 70 | m_lastLoadedItem = found.value(); | ||
83 | foundItem = m_itemForUrl[actualUrl]; | | |||
84 | } | | |||
85 | 71 | | |||
86 | if (foundItem) { | | |||
87 | if (callback.isCallable()) { | 72 | if (callback.isCallable()) { | ||
88 | QJSValueList args = {qmlEngine(this)->newQObject(foundItem)}; | 73 | QJSValueList args = {qmlEngine(this)->newQObject(found.value())}; | ||
89 | callback.call(args); | 74 | callback.call(args); | ||
90 | m_lastLoadedUrl = actualUrl; | | |||
91 | emit lastLoadedUrlChanged(); | 75 | emit lastLoadedUrlChanged(); | ||
76 | emit lastLoadedItemChanged(); | ||||
92 | // We could return the item, but for api coherence return null | 77 | // We could return the item, but for api coherence return null | ||
93 | return nullptr; | 78 | return nullptr; | ||
79 | | ||||
94 | } else { | 80 | } else { | ||
95 | m_lastLoadedUrl = actualUrl; | | |||
96 | emit lastLoadedUrlChanged(); | 81 | emit lastLoadedUrlChanged(); | ||
97 | return foundItem; | 82 | emit lastLoadedItemChanged(); | ||
83 | return found.value(); | ||||
98 | } | 84 | } | ||
99 | } | 85 | } | ||
100 | 86 | | |||
101 | QQmlComponent *component = m_componentForUrl.value(actualUrl); | 87 | QQmlComponent *component = m_componentForUrl.value(actualUrl); | ||
102 | 88 | | |||
103 | if (!component) { | 89 | if (!component) { | ||
104 | component = new QQmlComponent(qmlEngine(this), actualUrl, QQmlComponent::PreferSynchronous); | 90 | component = new QQmlComponent(qmlEngine(this), actualUrl, QQmlComponent::PreferSynchronous); | ||
105 | } | 91 | } | ||
▲ Show 20 Lines • Show All 121 Lines • ▼ Show 20 Line(s) | 212 | { | |||
227 | return url.isLocalFile() || url.scheme().isEmpty() || url.scheme() == QStringLiteral("qrc"); | 213 | return url.isLocalFile() || url.scheme().isEmpty() || url.scheme() == QStringLiteral("qrc"); | ||
228 | } | 214 | } | ||
229 | 215 | | |||
230 | QUrl PagePool::urlForPage(QQuickItem *item) const | 216 | QUrl PagePool::urlForPage(QQuickItem *item) const | ||
231 | { | 217 | { | ||
232 | return m_urlForItem.value(item); | 218 | return m_urlForItem.value(item); | ||
233 | } | 219 | } | ||
234 | 220 | | |||
221 | QQuickItem *PagePool::pageForUrl(const QUrl &url) const | ||||
222 | { | ||||
223 | return m_itemForUrl.value(resolvedUrl(url.toString()), nullptr); | ||||
224 | } | ||||
225 | | ||||
235 | bool PagePool::contains(const QVariant &page) const | 226 | bool PagePool::contains(const QVariant &page) const | ||
236 | { | 227 | { | ||
237 | if (page.canConvert<QQuickItem *>()) { | 228 | if (page.canConvert<QQuickItem *>()) { | ||
238 | return m_urlForItem.contains(page.value<QQuickItem *>()); | 229 | return m_urlForItem.contains(page.value<QQuickItem *>()); | ||
239 | } else if (page.canConvert<QString>()) { | 230 | } else if (page.canConvert<QString>()) { | ||
240 | const QUrl actualUrl = resolvedUrl(page.value<QString>()); | 231 | const QUrl actualUrl = resolvedUrl(page.value<QString>()); | ||
241 | return m_itemForUrl.contains(actualUrl); | 232 | return m_itemForUrl.contains(actualUrl); | ||
242 | } else { | 233 | } else { | ||
Show All 34 Lines | 239 | { | |||
277 | 268 | | |||
278 | m_itemForUrl.remove(url); | 269 | m_itemForUrl.remove(url); | ||
279 | m_urlForItem.remove(item); | 270 | m_urlForItem.remove(item); | ||
280 | item->deleteLater(); | 271 | item->deleteLater(); | ||
281 | } | 272 | } | ||
282 | 273 | | |||
283 | void PagePool::clear() | 274 | void PagePool::clear() | ||
284 | { | 275 | { | ||
285 | for (const auto& url : m_urlForItem) { | 276 | for (auto *c : m_componentForUrl.values()) { | ||
286 | deletePage(url); | 277 | c->deleteLater(); | ||
287 | } | 278 | } | ||
279 | m_componentForUrl.clear(); | ||||
280 | | ||||
281 | for (auto *i : m_itemForUrl.values()) { | ||||
282 | // items that had been deparented are safe to delete | ||||
283 | if (!i->parentItem()) { | ||||
284 | i->deleteLater(); | ||||
285 | } | ||||
286 | QQmlEngine::setObjectOwnership(i, QQmlEngine::JavaScriptOwnership); | ||||
287 | } | ||||
288 | m_itemForUrl.clear(); | ||||
289 | | ||||
290 | m_urlForItem.clear(); | ||||
288 | m_lastLoadedUrl = QUrl(); | 291 | m_lastLoadedUrl = QUrl(); | ||
289 | m_lastLoadedItem = nullptr; | 292 | m_lastLoadedItem = nullptr; | ||
290 | 293 | | |||
291 | emit lastLoadedUrlChanged(); | 294 | emit lastLoadedUrlChanged(); | ||
292 | emit lastLoadedItemChanged(); | 295 | emit lastLoadedItemChanged(); | ||
293 | } | 296 | } | ||
294 | 297 | | |||
295 | #include "moc_pagepool.cpp" | 298 | #include "moc_pagepool.cpp" |