@@ -19,6 +19,65 @@
20#include "unitymenumodelstack.h"
20#include "unitymenumodelstack.h"
23+#include <unitymenumodel.h>
25+class UnityMenuModelEntry : public QObject {
28+ UnityMenuModelEntry(UnityMenuModel* model, UnityMenuModel* parentModel, int index)
30+ m_parentModel(parentModel),
34+ QObject::connect(m_parentModel, SIGNAL(rowsInserted(QModelIndex, int, int)), this, SLOT(onRowsInserted(QModelIndex, int, int)));
35+ QObject::connect(m_parentModel, SIGNAL(rowsRemoved(QModelIndex, int, int)), this, SLOT(countChanged(QModelIndex, int, int)));
36+ QObject::connect(m_parentModel, SIGNAL(modelReset()), this, SLOT(onModelReset()));
40+ UnityMenuModel* model() const { return m_model; }
43+ void onRowsInserted(const QModelIndex&, int start, int end)
45+ int delta = end-start + 1;
46+ if (start <= m_index) {
51+ void onRowsRemoved(const QModelIndex&, int start, int end)
53+ int delta = end-start + 1;
54+ if (start <= m_index) {
55+ if (start + delta >= m_index) {
56+ // in the range removed
58+ disconnect(m_parentModel, 0, this, 0);
72+ disconnect(m_parentModel, 0, this, 0);
76+ UnityMenuModel* m_model;
77+ UnityMenuModel* m_parentModel;
22UnityMenuModelStack::UnityMenuModelStack(QObject* parent)
81UnityMenuModelStack::UnityMenuModelStack(QObject* parent)
23 : QObject(parent)
82 : QObject(parent)
@@ -26,30 +85,37 @@ UnityMenuModelStack::UnityMenuModelStack(QObject* parent)
27UnityMenuModelStack::~UnityMenuModelStack()
86UnityMenuModelStack::~UnityMenuModelStack()
88+ qDeleteAll(m_menuModels);
89+ m_menuModels.clear();
31UnityMenuModel* UnityMenuModelStack::head() const
92UnityMenuModel* UnityMenuModelStack::head() const
33− return !m_menuModels.isEmpty() ? m_menuModels.first() : NULL;
94+ return !m_menuModels.isEmpty() ? m_menuModels.first()->model() : NULL;
36void UnityMenuModelStack::setHead(UnityMenuModel* model)
97void UnityMenuModelStack::setHead(UnityMenuModel* model)
38 if (head() != model) {
99 if (head() != model) {
100+ qDeleteAll(m_menuModels);
39 m_menuModels.clear();
101 m_menuModels.clear();
40− push(model);
103+ push(model, 0);
41 Q_EMIT headChanged(model);
104 Q_EMIT headChanged(model);
45UnityMenuModel* UnityMenuModelStack::tail() const
108UnityMenuModel* UnityMenuModelStack::tail() const
47− return !m_menuModels.isEmpty() ? m_menuModels.last() : NULL;
110+ return !m_menuModels.isEmpty() ? m_menuModels.last()->model() : NULL;
50−void UnityMenuModelStack::push(UnityMenuModel* model)
113+void UnityMenuModelStack::push(UnityMenuModel* model, int index)
115+ UnityMenuModelEntry* entry = new UnityMenuModelEntry(model, tail(), index);
116+ QObject::connect(entry, SIGNAL(remove()), SLOT(onRemove()));
52− m_menuModels << model;
118+ m_menuModels << entry;
53 Q_EMIT tailChanged(model);
119 Q_EMIT tailChanged(model);
@@ -58,7 +124,9 @@ UnityMenuModel* UnityMenuModelStack::pop()
58 if (m_menuModels.isEmpty()) {
124 if (m_menuModels.isEmpty()) {
59 return NULL;
125 return NULL;
61− UnityMenuModel* model = m_menuModels.takeLast();
127+ UnityMenuModelEntry* entry = m_menuModels.takeLast();
128+ UnityMenuModel* model = entry->model();
129+ entry->deleteLater();
63 Q_EMIT tailChanged(tail());
131 Q_EMIT tailChanged(tail());
64 if (m_menuModels.isEmpty()) {
132 if (m_menuModels.isEmpty()) {
@@ -66,4 +134,21 @@ UnityMenuModel* UnityMenuModelStack::pop()
68 return model;
136 return model;
139+void UnityMenuModelStack::onRemove()
141+ UnityMenuModelEntry* removed = qobject_cast<UnityMenuModelEntry*>(sender());
142+ if (!m_menuModels.contains(removed))
145+ for (int i = m_menuModels.count() -1; i >= 0; i--) {
146+ UnityMenuModelEntry* entry = m_menuModels[i];
148+ if (entry == removed) {
154+#include "unitymenumodelstack.moc"