on ffde63f help sign in
unity8/tests/mocks/QMenuModel/unitymenumodel.cpp
245 lines 5.2 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors: Nick Dedekind <nick.dedekind@canonical.com>
17 */
19#include "unitymenumodel.h"
21enum MenuRoles {
22 LabelRole = Qt::DisplayRole + 1,
23 SensitiveRole,
24 IsSeparatorRole,
25 IconRole,
26 TypeRole,
27 ExtendedAttributesRole,
28 ActionRole,
29 ActionStateRole,
30 IsCheckRole,
31 IsRadioRole,
32 IsToggledRole
33};
35UnityMenuModel::UnityMenuModel(QObject *parent)
36: QAbstractListModel(parent)
40UnityMenuModel::~UnityMenuModel()
44QVariant UnityMenuModel::modelData() const
46 return m_modelData;
49void UnityMenuModel::setModelData(const QVariant& data)
51 beginResetModel();
53 m_modelData = data.toList();
54 Q_EMIT modelDataChanged();
56 endResetModel();
59void UnityMenuModel::insertRow(int row, const QVariant& data)
61 row = qMin(row, rowCount());
63 beginInsertRows(QModelIndex(), row, row);
65 m_modelData.insert(row, data);
67 endInsertRows();
70void UnityMenuModel::appendRow(const QVariant& data)
72 insertRow(rowCount(), data);
75void UnityMenuModel::removeRow(int row)
77 if (row < 0 || rowCount() <= row) {
78 return;
79 }
81 beginRemoveRows(QModelIndex(), row, row);
83 m_modelData.removeAt(row);
85 endRemoveRows();
88QByteArray UnityMenuModel::busName() const
90 return m_busName;
93void UnityMenuModel::setBusName(const QByteArray &busName)
95 this->m_busName = busName;
98QVariantMap UnityMenuModel::actions() const
100 return m_actions;
103void UnityMenuModel::setActions(const QVariantMap &actions)
105 this->m_actions = actions;
108QByteArray UnityMenuModel::menuObjectPath() const
110 return m_menuObjectPath;
113void UnityMenuModel::setMenuObjectPath(const QByteArray &path)
115 this->m_menuObjectPath = path;
118ActionStateParser* UnityMenuModel::actionStateParser() const
120 return nullptr;
123void UnityMenuModel::setActionStateParser(ActionStateParser*)
127QString UnityMenuModel::nameOwner() const
129 return QString("");
132int UnityMenuModel::rowCount(const QModelIndex&) const
134 return m_modelData.count();
137int UnityMenuModel::columnCount(const QModelIndex&) const
139 return 1;
142QVariant UnityMenuModel::data(const QModelIndex &index, int role) const
144 return rowData(index.row())[roleNames()[role]];
147QVariantMap UnityMenuModel::rowData(int row) const
149 if (m_modelData.count() <= row) {
150 return QVariantMap();
151 }
152 return m_modelData[row].toMap()["rowData"].toMap();
155QVariant UnityMenuModel::subMenuData(int row) const
157 return m_modelData[row].toMap()["submenu"];
160QModelIndex UnityMenuModel::index(int row, int column, const QModelIndex&) const
162 return createIndex(row, column);
165QModelIndex UnityMenuModel::parent(const QModelIndex &) const
167 return QModelIndex();
170QHash<int, QByteArray> UnityMenuModel::roleNames() const
172 QHash<int, QByteArray> names;
174 names[LabelRole] = "label";
175 names[SensitiveRole] = "sensitive";
176 names[IsSeparatorRole] = "isSeparator";
177 names[IconRole] = "icon";
178 names[TypeRole] = "type";
179 names[ExtendedAttributesRole] = "ext";
180 names[ActionRole] = "action";
181 names[ActionStateRole] = "actionState";
182 names[IsCheckRole] = "isCheck";
183 names[IsRadioRole] = "isRadio";
184 names[IsToggledRole] = "isToggled";
186 return names;
189QObject * UnityMenuModel::submenu(int position, QQmlComponent*)
191 if (position < 0 || m_modelData.count() < position) {
192 return nullptr;
193 }
195 while (submenus.count() <= position) {
196 submenus.append(nullptr);
197 }
199 QVariant submenuData = subMenuData(position);
200 if (submenuData.type() == (int)QMetaType::QVariantList) {
201 UnityMenuModel*& model = submenus[position];
202 if (!model) {
203 model = new UnityMenuModel(this);
204 }
205 if (model->modelData() != submenuData) {
206 model->setModelData(submenuData);
207 }
208 return model;
209 }
211 return nullptr;
214bool UnityMenuModel::loadExtendedAttributes(int, const QVariantMap &)
216 return false;
219QVariant UnityMenuModel::get(int row, const QByteArray &role)
221 static QHash<QByteArray, int> roles;
222 if (roles.isEmpty()) {
223 QHash<int, QByteArray> names = roleNames();
224 Q_FOREACH (int role, names.keys())
225 roles.insert(names[role], role);
226 }
228 return this->data(this->index(row, 0), roles[role]);
231void UnityMenuModel::activate(int, const QVariant&)
235void UnityMenuModel::changeState(int, const QVariant&)
239void UnityMenuModel::registerAction(UnityMenuAction*)
243void UnityMenuModel::unregisterAction(UnityMenuAction*)