on 551c708 help sign in
unity8/tests/mocks/QMenuModel/unitymenumodel.cpp
242 lines 5.1 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"
20#include <QDebug>
22enum MenuRoles {
23 LabelRole = Qt::DisplayRole + 1,
24 SensitiveRole,
25 IsSeparatorRole,
26 IconRole,
27 TypeRole,
28 ExtendedAttributesRole,
29 ActionRole,
30 ActionStateRole,
31 IsCheckRole,
32 IsRadioRole,
33 IsToggledRole
34};
36UnityMenuModel::UnityMenuModel(QObject *parent)
37: QAbstractListModel(parent)
41QVariant UnityMenuModel::modelData() const
43 return m_modelData;
46void UnityMenuModel::setModelData(const QVariant& data)
48 beginResetModel();
50 m_modelData.clear();
51 m_modelData = data.toList();
53 endResetModel();
57void UnityMenuModel::insertRow(int row, const QVariant& data)
59 row = qMin(row, rowCount());
61 beginInsertRows(QModelIndex(), row, row);
63 m_modelData.insert(row, data);
65 endInsertRows();
68void UnityMenuModel::appendRow(const QVariant& data)
70 insertRow(rowCount(), data);
73void UnityMenuModel::removeRow(int row)
75 if (row < 0 || rowCount() <= row) {
76 return;
77 }
79 beginRemoveRows(QModelIndex(), row, row);
81 m_modelData.removeAt(row);
83 endRemoveRows();
86UnityMenuModel::~UnityMenuModel()
90QByteArray UnityMenuModel::busName() const
92 return m_busName;
95void UnityMenuModel::setBusName(const QByteArray &busName)
97 this->m_busName = busName;
100QVariantMap UnityMenuModel::actions() const
102 return m_actions;
105void UnityMenuModel::setActions(const QVariantMap &actions)
107 this->m_actions = actions;
110QByteArray UnityMenuModel::menuObjectPath() const
112 return m_menuObjectPath;
115void UnityMenuModel::setMenuObjectPath(const QByteArray &path)
117 this->m_menuObjectPath = path;
120ActionStateParser* UnityMenuModel::actionStateParser() const
122 return NULL;
125void UnityMenuModel::setActionStateParser(ActionStateParser*)
129int UnityMenuModel::rowCount(const QModelIndex&) const
131 return m_modelData.count();
134int UnityMenuModel::columnCount(const QModelIndex&) const
136 return 1;
139QVariant UnityMenuModel::data(const QModelIndex &index, int role) const
141 return rowData(index.row())[roleNames()[role]];
144QVariantMap UnityMenuModel::rowData(int row) const
146 if (m_modelData.count() <= row) {
147 return QVariantMap();
148 }
149 return m_modelData[row].toMap()["rowData"].toMap();
152QVariant UnityMenuModel::subMenuData(int row) const
154 return m_modelData[row].toMap()["submenu"];
157QModelIndex UnityMenuModel::index(int row, int column, const QModelIndex&) const
159 return createIndex(row, column);
162QModelIndex UnityMenuModel::parent(const QModelIndex &) const
164 return QModelIndex();
167QHash<int, QByteArray> UnityMenuModel::roleNames() const
169 QHash<int, QByteArray> names;
171 names[LabelRole] = "label";
172 names[SensitiveRole] = "sensitive";
173 names[IsSeparatorRole] = "isSeparator";
174 names[IconRole] = "icon";
175 names[TypeRole] = "type";
176 names[ExtendedAttributesRole] = "ext";
177 names[ActionRole] = "action";
178 names[ActionStateRole] = "actionState";
179 names[IsCheckRole] = "isCheck";
180 names[IsRadioRole] = "isRadio";
181 names[IsToggledRole] = "isToggled";
183 return names;
186QObject * UnityMenuModel::submenu(int position, QQmlComponent*)
188 if (position < 0 || m_modelData.count() < position) {
189 return NULL;
190 }
192 while (submenus.count() <= position) {
193 submenus.append(NULL);
194 }
196 QVariant submenuData = subMenuData(position);
197 if (submenuData.type() == (int)QMetaType::QVariantList) {
198 UnityMenuModel*& model = submenus[position];
199 if (!model) {
200 model = new UnityMenuModel(this);
201 }
202 if (model->modelData() != submenuData) {
203 model->setModelData(submenuData);
204 }
205 return model;
206 }
208 return NULL;
211bool UnityMenuModel::loadExtendedAttributes(int, const QVariantMap &)
213 return false;
216QVariant UnityMenuModel::get(int row, const QByteArray &role)
218 static QHash<QByteArray, int> roles;
219 if (roles.isEmpty()) {
220 QHash<int, QByteArray> names = roleNames();
221 Q_FOREACH (int role, names.keys())
222 roles.insert(names[role], role);
223 }
225 return this->data(this->index(row, 0), roles[role]);
228void UnityMenuModel::activate(int, const QVariant&)
232void UnityMenuModel::changeState(int, const QVariant&)
236void UnityMenuModel::registerAction(UnityMenuAction*)
240void UnityMenuModel::unregisterAction(UnityMenuAction*)