on 38257fa help sign in
unity8/plugins/Unity/lenses.cpp
184 lines 5.0 kB history raw
1/*
2 * Copyright (C) 2011 Canonical, Ltd.
3 *
4 * Authors:
5 * Florian Boucault <florian.boucault@canonical.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 3.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
20// Self
21#include "lenses.h"
23// Local
24#include "lens.h"
26// Qt
27#include <QDebug>
28#include <QtCore/QStringList>
29#include <QtGui/QKeySequence>
31Scopes::Scopes(QObject *parent)
32 : QAbstractListModel(parent)
33 , m_unityScopes(std::make_shared<unity::dash::GSettingsScopes>())
34 , m_loaded(false)
36 m_roles[Scopes::RoleLens] = "lens";
37 m_roles[Scopes::RoleId] = "id";
38 m_roles[Scopes::RoleVisible] = "visible";
40 m_unityScopes->scope_added.connect(sigc::mem_fun(this, &Scopes::onScopeAdded));
41 m_unityScopes->scope_removed.connect(sigc::mem_fun(this, &Scopes::onScopeRemoved));
42 m_unityScopes->scopes_reordered.connect(sigc::mem_fun(this, &Scopes::onScopesReordered));
43 m_unityScopes->LoadScopes();
46QHash<int, QByteArray> Scopes::roleNames() const
48 return m_roles;
51int Scopes::rowCount(const QModelIndex& parent) const
53 Q_UNUSED(parent)
55 return m_scopes.count();
58QVariant Scopes::data(const QModelIndex& index, int role) const
60 Q_UNUSED(role)
62 if (!index.isValid()) {
63 return QVariant();
64 }
66 Scope* scope = m_scopes.at(index.row());
68 if (role == Scopes::RoleLens) {
69 return QVariant::fromValue(scope);
70 } else if (role == Scopes::RoleId) {
71 return QVariant::fromValue(scope->id());
72 } else if (role == Scopes::RoleVisible) {
73 return QVariant::fromValue(scope->visible());
74 } else {
75 return QVariant();
76 }
79QVariant Scopes::get(int row) const
81 return data(QAbstractListModel::index(row), 0);
84QVariant Scopes::get(const QString& scope_id) const
86 Q_FOREACH(Scope* scope, m_scopes) {
87 if (scope->id() == scope_id) {
88 return QVariant::fromValue(scope);
89 }
90 }
92 return QVariant();
95bool Scopes::loaded() const
97 return m_loaded;
100void Scopes::onScopeAdded(const unity::dash::Scope::Ptr& scope, int position)
102 int index = m_scopes.count();
103 beginInsertRows(QModelIndex(), index, index);
104 addUnityScope(scope);
105 endInsertRows();
107 // FIXME: do only once after all loaded?
108 m_loaded = true;
109 Q_EMIT loadedChanged(m_loaded);
112void Scopes::onScopeRemoved(const unity::dash::Scope::Ptr& scope)
114 //TODO
117void Scopes::onScopesReordered(const unity::dash::Scopes::ScopeList& scopes)
119 //TODO
122void Scopes::loadMocks()
124 /* FIXME: this is temporary code that is required on mobile to order
125 the lenses according to the design.
126 */
127 QStringList staticScopes;
128 staticScopes << "mockmusic.lens" << "people.lens" << "home.lens" << "applications.lens" << "mockvideos.lens";
130 // not all the lenses are guaranteed to go into the model (only if their UnitCore counterparts exist);
131 // so build up a list of the valid ones, then add them later.
132 QList<unity::dash::Scope::Ptr> added_scopes;
134 // add statically ordered lenses
135 Q_FOREACH(QString lensId, staticScopes) {
136 unity::dash::Scope::Ptr lens = m_unityScopes->GetScope(lensId.toStdString());
137 if (lens != NULL) {
138 added_scopes << lens;
139 }
140 }
142 // add remaining lenses
143 unity::dash::Scopes::ScopeList scopesList = m_unityScopes->GetScopes();
144 for(auto it = scopesList.begin(); it != scopesList.end(); ++it) {
145 unity::dash::Scope::Ptr scope = (*it);
146 if (!staticScopes.contains(QString::fromStdString(scope->id))) {
147 added_scopes << scope;
148 }
149 }
151 if (added_scopes.count() > 0) {
152 int index = rowCount();
153 beginInsertRows(QModelIndex(), index, index+added_scopes.count()-1);
154 Q_FOREACH(unity::dash::Scope::Ptr scope, added_scopes) {
155 addUnityScope(scope);
156 }
157 endInsertRows();
158 }
160 m_loaded = true;
161 Q_EMIT loadedChanged(m_loaded);
164void Scopes::onScopePropertyChanged()
166 QModelIndex scopeIndex = index(m_scopes.indexOf(qobject_cast<Scope*>(sender())));
167 Q_EMIT dataChanged(scopeIndex, scopeIndex);
170void Scopes::addUnityScope(const unity::dash::Scope::Ptr& unity_scope)
172 Scope* scope = new Scope(this);
173 scope->setUnityScope(unity_scope);
174 /* DOCME */
175 QObject::connect(scope, SIGNAL(visibleChanged(bool)), this, SLOT(onLensPropertyChanged()));
176 m_scopes.append(scope);
179void Scopes::removeUnityScope(int index)
181 Scope* scope = m_scopes.takeAt(index);
183 delete scope;