2 * Copyright (C) 2013 Canonical, Ltd.
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.
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.
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/>.
16 * Authors: Nick Dedekind <nick.dedekind@canonical.com>
19#include "unitymenumodel.h"
22 LabelRole = Qt::DisplayRole + 1,
27 ExtendedAttributesRole,
37UnityMenuModel::UnityMenuModel(QObject *parent)
38: QAbstractListModel(parent)
42UnityMenuModel::~UnityMenuModel()
46QVariant UnityMenuModel::modelData() const
51void UnityMenuModel::setModelData(const QVariant& data)
55 m_modelData = data.toList();
56 Q_EMIT modelDataChanged();
61void UnityMenuModel::insertRow(int row, const QVariant& data)
63 row = qMin(row, rowCount());
65 beginInsertRows(QModelIndex(), row, row);
67 m_modelData.insert(row, data);
72void UnityMenuModel::appendRow(const QVariant& data)
74 insertRow(rowCount(), data);
77void UnityMenuModel::removeRow(int row)
79 if (row < 0 || rowCount() <= row) {
83 beginRemoveRows(QModelIndex(), row, row);
85 m_modelData.removeAt(row);
90QByteArray UnityMenuModel::busName() const
95void UnityMenuModel::setBusName(const QByteArray &busName)
97 this->m_busName = busName;
100QVariantMap UnityMenuModel::actions() const
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
125void UnityMenuModel::setActionStateParser(ActionStateParser*)
129QString UnityMenuModel::nameOwner() const
134int UnityMenuModel::rowCount(const QModelIndex&) const
136 return m_modelData.count();
139int UnityMenuModel::columnCount(const QModelIndex&) const
144QVariant UnityMenuModel::data(const QModelIndex &index, int role) const
146 QVariantMap v = rowData(index.row());
147 QString roleName = roleNames()[role];
149 if (v.contains(roleName)) return v[roleName];
153 case LabelRole: return QString();
154 case SensitiveRole: return true;
155 case IsSeparatorRole: return false;
156 case IconRole: return QString();
157 case TypeRole: return QString();
158 case ExtendedAttributesRole: return QVariantMap();
159 case ActionRole: return QString();
160 case ActionStateRole: return QVariant();
161 case IsCheckRole: return false;
162 case IsRadioRole: return false;
163 case IsToggledRole: return false;
164 case ShortcutRole: return QString();
165 case HasSubmenuRole: return subMenuData(index.row()).isValid();
171QVariantMap UnityMenuModel::rowData(int row) const
173 if (m_modelData.count() <= row) {
174 return QVariantMap();
176 QVariantMap vRow = m_modelData.value(row, QVariantMap()).toMap();
177 return vRow["rowData"].toMap();
180QVariant UnityMenuModel::subMenuData(int row) const
182 QVariantMap v = m_modelData.value(row, QVariantMap()).toMap();
183 return v.value("submenu", QVariant());
186QModelIndex UnityMenuModel::index(int row, int column, const QModelIndex&) const
188 return createIndex(row, column);
191QModelIndex UnityMenuModel::parent(const QModelIndex &) const
193 return QModelIndex();
196QHash<int, QByteArray> UnityMenuModel::roleNames() const
198 QHash<int, QByteArray> names;
200 names[LabelRole] = "label";
201 names[SensitiveRole] = "sensitive";
202 names[IsSeparatorRole] = "isSeparator";
203 names[IconRole] = "icon";
204 names[TypeRole] = "type";
205 names[ExtendedAttributesRole] = "ext";
206 names[ActionRole] = "action";
207 names[ActionStateRole] = "actionState";
208 names[IsCheckRole] = "isCheck";
209 names[IsRadioRole] = "isRadio";
210 names[IsToggledRole] = "isToggled";
211 names[ShortcutRole] = "shortcut";
212 names[HasSubmenuRole] = "hasSubmenu";
217QObject * UnityMenuModel::submenu(int position, QQmlComponent*)
219 if (position < 0 || m_modelData.count() < position) {
223 while (submenus.count() <= position) {
224 submenus.append(nullptr);
227 QVariant submenuData = subMenuData(position);
228 if (submenuData.type() == (int)QMetaType::QVariantList) {
229 UnityMenuModel*& model = submenus[position];
231 model = new UnityMenuModel(this);
232 connect(model, &UnityMenuModel::activated, this, &UnityMenuModel::activated);
234 if (model->modelData() != submenuData) {
235 model->setModelData(submenuData);
243bool UnityMenuModel::loadExtendedAttributes(int, const QVariantMap &)
248QVariant UnityMenuModel::get(int row, const QByteArray &role)
250 static QHash<QByteArray, int> roles;
251 if (roles.isEmpty()) {
252 const QHash<int, QByteArray> names = roleNames();
253 for(auto it = names.begin(); it != names.end(); ++it)
254 roles.insert(it.value(), it.key());
257 return data(index(row, 0), roles[role]);
260void UnityMenuModel::activate(int row, const QVariant&)
262 QVariantMap vModelData = m_modelData.value(row, QVariantMap()).toMap();
263 QVariantMap rd = vModelData["rowData"].toMap();
265 bool isCheckable = rd[roleNames()[IsCheckRole]].toBool() || rd[roleNames()[IsRadioRole]].toBool();
267 rd[roleNames()[IsToggledRole]] = !rd[roleNames()[IsToggledRole]].toBool();
268 vModelData["rowData"] = rd;
269 m_modelData[row] = vModelData;
271 dataChanged(index(row, 0), index(row, 0), QVector<int>() << IsToggledRole);
273 Q_EMIT activated(rd[roleNames()[ActionRole]].toString());
276void UnityMenuModel::changeState(int, const QVariant&)
280void UnityMenuModel::registerAction(UnityMenuAction*)
284void UnityMenuModel::unregisterAction(UnityMenuAction*)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list