on xenial help sign in
use qml ownership for models
commit22591420876a501777070efc90f33ed9aeb59013 authorNick Dedekind <nicholas.dedekind@gmail.com> dateTue 19 Aug 2014, 16:23 (12 years ago) parent821f5d2
6 files changed+7−96
plugins/Unity/Indicators/CachedUnityMenuModel.qml−16 plugins/Unity/Indicators/unitymenumodelcache.cpp+6−21 plugins/Unity/Indicators/unitymenumodelcache.h−4 qml/Shell.qml−1 tests/qmltests/CMakeLists.txt+1−1 tests/qmltests/Panel/Indicators/tst_CachedUnityMenuModel.qml−53
plugins/Unity/Indicators/CachedUnityMenuModel.qml−16
@@ -45,22 +45,6 @@ Item {
4545 return newModel;
4646 }
4747
48− QtObject {
49− id: d
50− property UnityMenuModel oldModel: null
51− }
52−
53− onModelChanged: {
54− if (model !== d.oldModel) {
55− if (d.oldModel) Indicators.UnityMenuModelCache.deref(d.oldModel);
56− if (model) Indicators.UnityMenuModelCache.ref(model);
57− }
58− d.oldModel = model;
59− }
60− Component.onDestruction: {
61− if (model) Indicators.UnityMenuModelCache.deref(model);
62− }
63−
6448 Component {
6549 id: modelComponent
6650 UnityMenuModel {}
plugins/Unity/Indicators/unitymenumodelcache.cpp+6−21
@@ -30,7 +30,6 @@ UnityMenuModelCache::UnityMenuModelCache(QObject* parent)
3030
3131UnityMenuModelCache::~UnityMenuModelCache()
3232{
33− qDeleteAll(m_menuModels);
3433}
3534
3635UnityMenuModel* UnityMenuModelCache::model(const QString& path) const
@@ -40,31 +39,17 @@ UnityMenuModel* UnityMenuModelCache::model(const QString& path) const
4039
4140void UnityMenuModelCache::registerModel(const QString& path, UnityMenuModel* menuModel)
4241{
43− QQmlEngine::setObjectOwnership(menuModel, QQmlEngine::CppOwnership);
44− menuModel->setParent(this);
4542 m_menuModels[path] = menuModel;
46−}
4743
48−void UnityMenuModelCache::ref(UnityMenuModel* menuModel)
49−{
50− m_refs[menuModel] = m_refs.value(menuModel, 0) + 1;
51−}
52−
53−void UnityMenuModelCache::deref(UnityMenuModel* menuModel)
54−{
55− if (m_refs.contains(menuModel)) {
56− if (--m_refs[menuModel] == 0) {
57− m_refs.remove(menuModel);
58− QList<QString> keys = m_menuModels.keys(menuModel);
59− Q_FOREACH(const QString& key, keys) {
60− m_menuModels.remove(key);
61− }
62− menuModel->deleteLater();
44+ connect(menuModel, &QObject::destroyed, this, [menuModel, this](QObject*) {
45+ QList<QString> keys = m_menuModels.keys(menuModel);
46+ Q_FOREACH(const QString& key, keys) {
47+ m_menuModels.remove(key);
6348 }
64− }
49+ });
6550}
6651
6752bool UnityMenuModelCache::contains(UnityMenuModel* menuModel)
6853{
69− return m_refs.contains(menuModel);
54+ return !m_menuModels.keys(menuModel).isEmpty();
7055}
plugins/Unity/Indicators/unitymenumodelcache.h−4
@@ -37,14 +37,10 @@ public:
3737 Q_INVOKABLE UnityMenuModel* model(const QString& path) const;
3838 Q_INVOKABLE void registerModel(const QString& path, UnityMenuModel* menuModel);
3939
40− Q_INVOKABLE void ref(UnityMenuModel* menuModel);
41− Q_INVOKABLE void deref(UnityMenuModel* menuModel);
42−
4340 Q_INVOKABLE bool contains(UnityMenuModel* menuModel);
4441
4542private:
4643 QHash<QString, UnityMenuModel*> m_menuModels;
47− QHash<UnityMenuModel*, uint> m_refs;
4844};
4945
5046#endif // UNITYMENUMODELCACHE_H
qml/Shell.qml−1
@@ -648,5 +648,4 @@ Item {
648648 }
649649 }
650650 }
651−
652651}
tests/qmltests/CMakeLists.txt+1−1
@@ -72,7 +72,7 @@ add_qml_test(Panel Panel)
7272add_qml_test(Panel SearchIndicator)
7373add_qml_test(Panel/Indicators DefaultIndicatorWidget ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/tests/mocks/QMenuModel")
7474add_qml_test(Panel/Indicators DefaultIndicatorPage ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/tests/mocks/QMenuModel")
75−add_qml_test(Panel/Indicators CachedUnityMenuModel IMPORT_PATHS ${CMAKE_BINARY_DIR}/tests/mocks ${qmltest_DEFAULT_IMPORT_PATHS} ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/tests/mocks/QMenuModel")
75+add_qml_test(Panel/Indicators CachedUnityMenuModel IMPORT_PATHS ${CMAKE_BINARY_DIR}/plugins ${CMAKE_BINARY_DIR}/tests/utils/modules)
7676# These MenuItemFactory tests need the test/mocks/ to come before plugins/
7777add_qml_test(Panel/Indicators MenuItemFactory IMPORT_PATHS ${CMAKE_BINARY_DIR}/tests/mocks ${qmltest_DEFAULT_IMPORT_PATHS})
7878add_qml_test(Panel/Indicators MessageMenuItemFactory IMPORT_PATHS ${CMAKE_BINARY_DIR}/tests/mocks ${qmltest_DEFAULT_IMPORT_PATHS})
tests/qmltests/Panel/Indicators/tst_CachedUnityMenuModel.qml−53
@@ -50,9 +50,6 @@ Item {
5050 });
5151
5252 verify(cachedObject.model !== cachedObject2.model);
53−
54− cachedObject.destroy();
55− cachedObject2.destroy();
5653 }
5754
5855 function test_createSame() {
@@ -71,53 +68,6 @@ Item {
7168 });
7269
7370 verify(cachedObject.model === cachedObject2.model);
74−
75− cachedObject.destroy();
76− cachedObject2.destroy();
77− }
78−
79− function test_destroyAllDeletesModel() {
80− var cachedObject = model.createObject(root,
81− {
82− "busName": "com.canonical.test1",
83− "menuObjectPath": "com/canonical/test1",
84− "actionsObjectPath": "com/canonical/test1/actions"
85− });
86−
87− var cachedObject2 = model.createObject(root,
88− {
89− "busName": "com.canonical.test1",
90− "menuObjectPath": "com/canonical/test1",
91− "actionsObjectPath": "com/canonical/test1/actions"
92− });
93−
94− var menuModel = cachedObject.model;
95− cachedObject.destroy();
96− cachedObject2.destroy();
97− tryCompareFunction(function() { return Indicators.UnityMenuModelCache.contains(menuModel) }, false);
98− }
99−
100− function test_destroyPartialKeepsModel() {
101− var cachedObject = model.createObject(root,
102− {
103− "busName": "com.canonical.test1",
104− "menuObjectPath": "com/canonical/test1",
105− "actionsObjectPath": "com/canonical/test1/actions"
106− });
107−
108− var cachedObject2 = model.createObject(root,
109− {
110− "busName": "com.canonical.test1",
111− "menuObjectPath": "com/canonical/test1",
112− "actionsObjectPath": "com/canonical/test1/actions"
113− });
114−
115− var menuModel = cachedObject.model;
116− cachedObject.destroy();
117− wait(1);
118− compare(Indicators.UnityMenuModelCache.contains(menuModel), true);
119−
120− cachedObject2.destroy();
12171 }
12272
12373 // Tests that changing cached model data does not change the model path of others
@@ -141,9 +91,6 @@ Item {
14191 compare(cachedObject2.model.menuObjectPath, "com/canonical/test1");
14292
14393 verify(cachedObject.model !== cachedObject2.model);
144−
145− cachedObject.destroy();
146− cachedObject2.destroy();
14794 }
14895 }
14996}