on xenial help sign in
fixed problem destroying cached menu model.
commit0b65bb2b01fc050cd3c3ba65983b940e3ed9e79f authorNick Dedekind <nicholas.dedekind@gmail.com> dateTue 19 Aug 2014, 10:56 (12 years ago) parentf605473
3 files changed+60−26
plugins/Unity/Indicators/CachedUnityMenuModel.qml+32−24 plugins/Unity/Indicators/unitymenumodelcache.cpp+24−2 plugins/Unity/Indicators/unitymenumodelcache.h+4
plugins/Unity/Indicators/CachedUnityMenuModel.qml+32−24
@@ -18,43 +18,51 @@
1818 */
1919
2020import QtQuick 2.0
21−import QMenuModel 0.1 as QMenuModel
21+import QMenuModel 0.1
2222import Unity.Indicators 0.1 as Indicators
2323
2424// Make sure we don't duplicate models.
2525Item {
2626 id: cachedModel
27− property string busName: ""
28− property string actionsObjectPath: ""
29− property string menuObjectPath: ""
30−
27+ property string busName
28+ property string actionsObjectPath
29+ property string menuObjectPath
3130 readonly property bool ready: busName!=="" && actionsObjectPath!=="" && menuObjectPath!==""
3231
33− property QtObject model: {
34− if (!ready) {
35− return null;
32+ property UnityMenuModel model: {
33+ if (!ready) return null;
34+
35+ var newModel = Indicators.UnityMenuModelCache.model(menuObjectPath);
36+ if (!newModel) {
37+ newModel = modelComponent.createObject(null,
38+ {
39+ "busName": cachedModel.busName,
40+ "menuObjectPath": cachedModel.menuObjectPath,
41+ "actions": { "indicator": cachedModel.actionsObjectPath },
42+ });
43+ Indicators.UnityMenuModelCache.registerModel(newModel.menuObjectPath, newModel);
3644 }
45+ return newModel;
46+ }
47+
48+ QtObject {
49+ id: d
50+ property UnityMenuModel oldModel: null
51+ }
3752
38− var component = Indicators.UnityMenuModelCache.model(menuObjectPath);
39− if (!component) {
40− component = modelComponent.createObject(cachedModel);
41− return component;
53+ onModelChanged: {
54+ if (model !== d.oldModel) {
55+ if (d.oldModel) Indicators.UnityMenuModelCache.deref(d.oldModel);
56+ if (model) Indicators.UnityMenuModelCache.ref(model);
4257 }
43− return component;
58+ d.oldModel = model;
59+ }
60+ Component.onDestruction: {
61+ if (model) Indicators.UnityMenuModelCache.deref(model);
4462 }
4563
4664 Component {
4765 id: modelComponent
48−
49− QMenuModel.UnityMenuModel {
50− id: unityModel
51− busName: cachedModel.busName
52− actions: { "indicator": cachedModel.actionsObjectPath }
53− menuObjectPath: cachedModel.menuObjectPath
54−
55− Component.onCompleted: {
56− Indicators.UnityMenuModelCache.registerModel(cachedModel.menuObjectPath, unityModel);
57− }
58− }
66+ UnityMenuModel {}
5967 }
6068}
plugins/Unity/Indicators/unitymenumodelcache.cpp+24−2
@@ -20,6 +20,8 @@
2020#include "unitymenumodelcache.h"
2121#include <unitymenumodel.h>
2222
23+#include <QQmlEngine>
24+
2325UnityMenuModelCache::UnityMenuModelCache(QObject* parent)
2426 : QObject(parent)
2527{
@@ -27,9 +29,9 @@ UnityMenuModelCache::UnityMenuModelCache(QObject* parent)
2729
2830UnityMenuModelCache::~UnityMenuModelCache()
2931{
32+ qDeleteAll(m_menuModels);
3033}
3134
32−
3335UnityMenuModel* UnityMenuModelCache::model(const QString& path) const
3436{
3537 return m_menuModels.value(path, NULL);
@@ -37,6 +39,26 @@ UnityMenuModel* UnityMenuModelCache::model(const QString& path) const
3739
3840void UnityMenuModelCache::registerModel(const QString& path, UnityMenuModel* menuModel)
3941{
42+ QQmlEngine::setObjectOwnership(menuModel, QQmlEngine::CppOwnership);
4043 menuModel->setParent(this);
4144 m_menuModels[path] = menuModel;
42−}
45+}
46+
47+void UnityMenuModelCache::ref(UnityMenuModel* menuModel)
48+{
49+ m_refs[menuModel] = m_refs.value(menuModel, 0) + 1;
50+}
51+
52+void UnityMenuModelCache::deref(UnityMenuModel* menuModel)
53+{
54+ if (m_refs.contains(menuModel)) {
55+ if (m_refs[menuModel]-- == 0) {
56+ m_refs.remove(menuModel);
57+ QList<QString> keys = m_menuModels.keys(menuModel);
58+ Q_FOREACH(const QString& key, keys) {
59+ m_menuModels.remove(key);
60+ }
61+ delete menuModel;
62+ }
63+ }
64+}
plugins/Unity/Indicators/unitymenumodelcache.h+4
@@ -37,8 +37,12 @@ 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+
4043private:
4144 QHash<QString, UnityMenuModel*> m_menuModels;
45+ QHash<UnityMenuModel*, uint> m_refs;
4246};
4347
4448#endif // UNITYMENUMODELCACHE_H