on fe2b269 help sign in
unity8/tests/mocks/Unity/Launcher/MockLauncherModel.cpp
176 lines 5.9 kB history raw
1/*
2 * Copyright 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 Lesser 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 Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authors:
17 * Michael Zanetti <michael.zanetti@canonical.com>
18 */
20#include "MockLauncherModel.h"
21#include "MockLauncherItem.h"
23MockLauncherModel::MockLauncherModel(QObject* parent): LauncherModelInterface(parent)
25 MockLauncherItem *item = new MockLauncherItem("phone-app", "/usr/share/applications/phone-app.desktop", "Phone", "phone-app", this);
26 item->setProgress(0);
27 m_list.append(item);
28 item = new MockLauncherItem("camera-app", "/usr/share/applications/camera-app.desktop", "Camera", "camera", this);
29 item->setProgress(10);
30 m_list.append(item);
31 item = new MockLauncherItem("gallery-app", "/usr/share/applications/gallery-app.desktop", "Gallery", "gallery", this);
32 item->setProgress(50);
33 m_list.append(item);
34 item = new MockLauncherItem("facebook-webapp", "/usr/share/applications/facebook-webapp.desktop", "Facebook", "facebook", this);
35 item->setProgress(150);
36 m_list.append(item);
37 item = new MockLauncherItem("webbrowser-app", "/usr/share/applications/webbrowser-app.desktop", "Browser", "browser", this);
38 item->setCount(1);
39 m_list.append(item);
40 item = new MockLauncherItem("twitter-webapp", "/usr/share/applications/twitter-webapp.desktop", "Twitter", "twitter", this);
41 item->setCount(12);
42 m_list.append(item);
43 item = new MockLauncherItem("gmail-webapp", "/usr/share/applications/gmail-webapp.desktop", "GMail", "gmail", this);
44 item->setCount(123);
45 m_list.append(item);
46 item = new MockLauncherItem("ubuntu-weather-app", "/usr/share/applications/ubuntu-weather-app.desktop", "Weather", "weather", this);
47 item->setCount(1234567890);
48 m_list.append(item);
49 item = new MockLauncherItem("notes-app", "/usr/share/applications/notes-app.desktop", "Notepad", "notepad", this);
50 item->setProgress(50);
51 item->setCount(5);
52 m_list.append(item);
53 item = new MockLauncherItem("calendar-app", "/usr/share/applications/calendar-app.desktop","Calendar", "calendar", this);
54 m_list.append(item);
57MockLauncherModel::~MockLauncherModel()
61int MockLauncherModel::rowCount(const QModelIndex& parent) const
63 Q_UNUSED(parent)
64 return m_list.count();
67QVariant MockLauncherModel::data(const QModelIndex& index, int role) const
69 LauncherItemInterface *item = m_list.at(index.row());
70 switch(role)
71 {
72 case RoleDesktopFile:
73 return item->desktopFile();
74 case RoleName:
75 return item->name();
76 case RoleIcon:
77 return item->icon();
78 case RolePinned:
79 return item->pinned();
80 case RoleRunning:
81 return item->running();
82 case RoleRecent:
83 return item->recent();
84 case RoleProgress:
85 return item->progress();
86 case RoleCount:
87 return item->count();
88 }
90 return QVariant();
93unity::shell::launcher::LauncherItemInterface *MockLauncherModel::get(int index) const
95 if (index < 0 || index >= m_list.count())
96 {
97 return 0;
98 }
99 return m_list.at(index);
102void MockLauncherModel::move(int oldIndex, int newIndex)
104 // Make sure its not moved outside the lists
105 if (newIndex < 0) {
106 newIndex = 0;
107 }
108 if (newIndex >= m_list.count()) {
109 newIndex = m_list.count()-1;
110 }
112 // Nothing to do?
113 if (oldIndex == newIndex) {
114 return;
115 }
117 // QList's and QAbstractItemModel's move implementation differ when moving an item up the list :/
118 // While QList needs the index in the resulting list, beginMoveRows expects it to be in the current list
119 // adjust the model's index by +1 in case we're moving upwards
120 int newModelIndex = newIndex > oldIndex ? newIndex+1 : newIndex;
122 beginMoveRows(QModelIndex(), oldIndex, oldIndex, QModelIndex(), newModelIndex);
123 m_list.move(oldIndex, newIndex);
124 endMoveRows();
126 pin(m_list.at(newIndex)->appId());
129void MockLauncherModel::pin(const QString &appId, int index)
131 int currentIndex = findApp(appId);
133 if (currentIndex >= 0) {
134 if (index == -1 || index == currentIndex) {
135 m_list.at(currentIndex)->setPinned(true);
136 QModelIndex modelIndex = this->index(currentIndex);
137 Q_EMIT dataChanged(modelIndex, modelIndex);
138 } else {
139 move(currentIndex, index);
140 }
141 } else {
142 beginInsertRows(QModelIndex(), index, index);
143 m_list.insert(index, new MockLauncherItem(appId,
144 appId + ".desktop",
145 appId,
146 appId + ".png"));
147 m_list.at(index)->setPinned(true);
148 endInsertRows();
149 }
152void MockLauncherModel::requestRemove(const QString &appId)
154 int index = findApp(appId);
155 if (index >= 0) {
156 beginRemoveRows(QModelIndex(), index, 0);
157 m_list.takeAt(index)->deleteLater();
158 endRemoveRows();
159 }
162void MockLauncherModel::quickListActionInvoked(const QString &appId, int actionIndex)
164 Q_EMIT quickListTriggered(appId, actionIndex);
167int MockLauncherModel::findApp(const QString &appId)
169 for (int i = 0; i < m_list.count(); ++i) {
170 MockLauncherItem *item = m_list.at(i);
171 if (item->appId() == appId) {
172 return i;
173 }
174 }
175 return -1;