@@ -19,6 +19,65 @@
1919
2020#include "unitymenumodelstack.h"
2121
22+#include <QDebug>
23+#include <unitymenumodel.h>
24+
25+class UnityMenuModelEntry : public QObject {
26+ Q_OBJECT
27+public:
28+ UnityMenuModelEntry(UnityMenuModel* model, UnityMenuModel* parentModel, int index)
29+ : m_model(model),
30+ m_parentModel(parentModel),
31+ m_index(index)
32+ {
33+ if (m_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()));
37+ }
38+ }
39+
40+ UnityMenuModel* model() const { return m_model; }
41+
42+private Q_SLOTS:
43+ void onRowsInserted(const QModelIndex&, int start, int end)
44+ {
45+ int delta = end-start + 1;
46+ if (start <= m_index) {
47+ m_index += delta;
48+ }
49+ }
50+
51+ void onRowsRemoved(const QModelIndex&, int start, int end)
52+ {
53+ int delta = end-start + 1;
54+ if (start <= m_index) {
55+ if (start + delta >= m_index) {
56+ // in the range removed
57+ Q_EMIT remove();
58+ disconnect(m_parentModel, 0, this, 0);
59+ } else {
60+ m_index -= delta;
61+ }
62+ }
63+ }
64+
65+Q_SIGNALS:
66+ void remove();
67+
68+private Q_SLOTS:
69+ void onModelReset()
70+ {
71+ Q_EMIT remove();
72+ disconnect(m_parentModel, 0, this, 0);
73+ }
74+
75+private:
76+ UnityMenuModel* m_model;
77+ UnityMenuModel* m_parentModel;
78+ int m_index;
79+};
80+
2281UnityMenuModelStack::UnityMenuModelStack(QObject* parent)
2382 : QObject(parent)
2483{
@@ -26,30 +85,37 @@ UnityMenuModelStack::UnityMenuModelStack(QObject* parent)
2685
2786UnityMenuModelStack::~UnityMenuModelStack()
2887{
88+ qDeleteAll(m_menuModels);
89+ m_menuModels.clear();
2990}
3091
3192UnityMenuModel* UnityMenuModelStack::head() const
3293{
33− return !m_menuModels.isEmpty() ? m_menuModels.first() : NULL;
94+ return !m_menuModels.isEmpty() ? m_menuModels.first()->model() : NULL;
3495}
3596
3697void UnityMenuModelStack::setHead(UnityMenuModel* model)
3798{
3899 if (head() != model) {
100+ qDeleteAll(m_menuModels);
39101 m_menuModels.clear();
40− push(model);
102+
103+ push(model, 0);
41104 Q_EMIT headChanged(model);
42105 }
43106}
44107
45108UnityMenuModel* UnityMenuModelStack::tail() const
46109{
47− return !m_menuModels.isEmpty() ? m_menuModels.last() : NULL;
110+ return !m_menuModels.isEmpty() ? m_menuModels.last()->model() : NULL;
48111}
49112
50−void UnityMenuModelStack::push(UnityMenuModel* model)
113+void UnityMenuModelStack::push(UnityMenuModel* model, int index)
51114{
52− m_menuModels << model;
115+ UnityMenuModelEntry* entry = new UnityMenuModelEntry(model, tail(), index);
116+ QObject::connect(entry, SIGNAL(remove()), SLOT(onRemove()));
117+
118+ m_menuModels << entry;
53119 Q_EMIT tailChanged(model);
54120}
55121
@@ -58,7 +124,9 @@ UnityMenuModel* UnityMenuModelStack::pop()
58124 if (m_menuModels.isEmpty()) {
59125 return NULL;
60126 }
61− UnityMenuModel* model = m_menuModels.takeLast();
127+ UnityMenuModelEntry* entry = m_menuModels.takeLast();
128+ UnityMenuModel* model = entry->model();
129+ entry->deleteLater();
62130
63131 Q_EMIT tailChanged(tail());
64132 if (m_menuModels.isEmpty()) {
@@ -66,4 +134,21 @@ UnityMenuModel* UnityMenuModelStack::pop()
66134 }
67135
68136 return model;
69−}
137+}
138+
139+void UnityMenuModelStack::onRemove()
140+{
141+ UnityMenuModelEntry* removed = qobject_cast<UnityMenuModelEntry*>(sender());
142+ if (!m_menuModels.contains(removed))
143+ return;
144+
145+ for (int i = m_menuModels.count() -1; i >= 0; i--) {
146+ UnityMenuModelEntry* entry = m_menuModels[i];
147+ pop();
148+ if (entry == removed) {
149+ break;
150+ }
151+ }
152+}
153+
154+#include "unitymenumodelstack.moc"