on 5fbee09 help sign in
unity8/plugins/Unity/lenses.cpp
190 lines 5.5 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>
31Lenses::Lenses(QObject *parent)
32 : QAbstractListModel(parent)
33 , m_unityLenses(std::make_shared<unity::dash::GSettingsScopes>())
34/* , m_homeLens(std::make_shared<unity::dash::HomeLens>(QString::fromUtf8(dgettext("unity", "Home")).toStdString(),
35 QString::fromUtf8(dgettext("unity", "Home screen")).toStdString(),
36 QString::fromUtf8(dgettext("unity", "Search")).toStdString()))*/
37 , m_loaded(false)
39 m_roles[Lenses::RoleLens] = "lens";
40 m_roles[Lenses::RoleId] = "id";
41 m_roles[Lenses::RoleVisible] = "visible";
43 m_unityLenses->scope_added.connect(sigc::mem_fun(this, &Lenses::onScopeAdded));
44 m_unityLenses->scope_removed.connect(sigc::mem_fun(this, &Lenses::onScopeRemoved));
45 m_unityLenses->scopes_reordered.connect(sigc::mem_fun(this, &Lenses::onScopesReordered));
46 m_unityLenses->LoadScopes();
49QHash<int, QByteArray> Lenses::roleNames() const
51 return m_roles;
54int Lenses::rowCount(const QModelIndex& parent) const
56 Q_UNUSED(parent)
58 return m_lenses.count();
61QVariant Lenses::data(const QModelIndex& index, int role) const
63 Q_UNUSED(role)
65 if (!index.isValid()) {
66 return QVariant();
67 }
69 Lens* lens = m_lenses.at(index.row());
71 if (role == Lenses::RoleLens) {
72 return QVariant::fromValue(lens);
73 } else if (role == Lenses::RoleId) {
74 return QVariant::fromValue(lens->id());
75 } else if (role == Lenses::RoleVisible) {
76 return QVariant::fromValue(lens->visible());
77 } else {
78 return QVariant();
79 }
82QVariant Lenses::get(int row) const
84 return data(QAbstractListModel::index(row), 0);
87QVariant Lenses::get(const QString& lens_id) const
89 Q_FOREACH(Lens* lens, m_lenses) {
90 if (lens->id() == lens_id) {
91 return QVariant::fromValue(lens);
92 }
93 }
95 return QVariant();
98bool Lenses::loaded() const
100 return m_loaded;
103void Lenses::onScopeAdded(const unity::dash::Scope::Ptr& scope, int position)
105 int index = m_lenses.count();
106 beginInsertRows(QModelIndex(), index, index);
107 addUnityLens(scope);
108 endInsertRows();
110 // FIXME: do only once after all loaded?
111 m_loaded = true;
112 Q_EMIT loadedChanged(m_loaded);
115void Lenses::onScopeRemoved(const unity::dash::Scope::Ptr& scope)
117 //TODO
120void Lenses::onScopesReordered(const unity::dash::Scopes::ScopeList& scopes)
122 //TODO
125void Lenses::loadMocks()
127 /* FIXME: this is temporary code that is required on mobile to order
128 the lenses according to the design.
129 */
130 QStringList staticLenses;
131 staticLenses << "mockmusic.lens" << "people.lens" << "home.lens" << "applications.lens" << "mockvideos.lens";
133 // not all the lenses are guaranteed to go into the model (only if their UnitCore counterparts exist);
134 // so build up a list of the valid ones, then add them later.
135 QList<unity::dash::Scope::Ptr> added_lenses;
137 // add statically ordered lenses
138 Q_FOREACH(QString lensId, staticLenses) {
139 unity::dash::Scope::Ptr lens = m_unityLenses->GetScope(lensId.toStdString());
140 if (lens != NULL) {
141 added_lenses << lens;
142 }
143 }
145 // add remaining lenses
146 unity::dash::Scopes::ScopeList lensesList = m_unityLenses->GetScopes();
147 for(auto it = lensesList.begin(); it != lensesList.end(); ++it) {
148 unity::dash::Scope::Ptr lens = (*it);
149 if (!staticLenses.contains(QString::fromStdString(lens->id))) {
150 added_lenses << lens;
151 }
152 }
154 if (added_lenses.count() > 0) {
155 int index = rowCount();
156 beginInsertRows(QModelIndex(), index, index+added_lenses.count()-1);
157 Q_FOREACH(unity::dash::Scope::Ptr lens, added_lenses) {
158 addUnityLens(lens);
159 }
160 endInsertRows();
161 }
163 m_loaded = true;
164 Q_EMIT loadedChanged(m_loaded);
166 // listen to dynamically added lenses
167 //m_homeLens->lens_added.connect(sigc::mem_fun(this, &Lenses::onLensAdded)); //FIXME
170void Lenses::onLensPropertyChanged()
172 QModelIndex lensIndex = index(m_lenses.indexOf(qobject_cast<Lens*>(sender())));
173 Q_EMIT dataChanged(lensIndex, lensIndex);
176void Lenses::addUnityLens(const unity::dash::Scope::Ptr& unity_lens)
178 Lens* lens = new Lens(this);
179 lens->setUnityLens(unity_lens);
180 /* DOCME */
181 QObject::connect(lens, SIGNAL(visibleChanged(bool)), this, SLOT(onLensPropertyChanged()));
182 m_lenses.append(lens);
185void Lenses::removeUnityLens(int index)
187 Lens* lens = m_lenses.takeAt(index);
189 delete lens;