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, &UnityMenuModel::rowsInserted,
this, &UnityMenuModelEntry::onRowsInserted);
35 QObject::connect(m_parentModel, &UnityMenuModel::rowsRemoved,
this, &UnityMenuModelEntry::onRowsRemoved);
36 QObject::connect(m_parentModel, &UnityMenuModel::modelReset,
this, &UnityMenuModelEntry::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) {
58 disconnect(m_parentModel, 0,
this, 0);
68 disconnect(m_parentModel, 0,
this, 0);
75 UnityMenuModel* m_model;
76 UnityMenuModel* m_parentModel;
80 UnityMenuModelStack::UnityMenuModelStack(QObject* parent)
85 UnityMenuModelStack::~UnityMenuModelStack()
87 qDeleteAll(m_menuModels);
91 UnityMenuModel* UnityMenuModelStack::head()
const
93 return !m_menuModels.isEmpty() ? m_menuModels.first()->model() :
nullptr;
96 void UnityMenuModelStack::setHead(UnityMenuModel* model)
98 if (head() != model) {
99 qDeleteAll(m_menuModels);
100 m_menuModels.clear();
103 Q_EMIT headChanged(model);
107 UnityMenuModel* UnityMenuModelStack::tail()
const
109 return !m_menuModels.isEmpty() ? m_menuModels.last()->model() :
nullptr;
112 int UnityMenuModelStack::count()
const
114 return m_menuModels.count();
117 void UnityMenuModelStack::push(UnityMenuModel* model,
int index)
119 UnityMenuModelEntry* entry =
new UnityMenuModelEntry(model, tail(), index);
120 QObject::connect(entry, &UnityMenuModelEntry::remove,
this, &UnityMenuModelStack::onRemove);
122 m_menuModels << entry;
123 Q_EMIT tailChanged(model);
124 Q_EMIT countChanged(m_menuModels.count());
127 UnityMenuModel* UnityMenuModelStack::pop()
129 if (m_menuModels.isEmpty()) {
132 UnityMenuModelEntry* entry = m_menuModels.takeLast();
133 UnityMenuModel* model = entry->model();
134 entry->deleteLater();
136 Q_EMIT tailChanged(tail());
137 if (m_menuModels.isEmpty()) {
138 Q_EMIT headChanged(
nullptr);
140 Q_EMIT countChanged(m_menuModels.count());
145 void UnityMenuModelStack::onRemove()
147 UnityMenuModelEntry* removed = qobject_cast<UnityMenuModelEntry*>(sender());
148 if (!m_menuModels.contains(removed))
151 for (
int i = m_menuModels.count() -1; i >= 0; i--) {
152 UnityMenuModelEntry* entry = m_menuModels[i];
154 if (entry == removed) {
160 #include "unitymenumodelstack.moc"