on 0b01bee help sign in
unity8/plugins/Unity/lenses.cpp
191 lines 5.7 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();
47 //std::dynamic_pointer_cast<unity::dash::FilesystemLenses>(m_unityLenses)->lenses_loaded.connect(sigc::mem_fun(this, &Lenses::onLensesLoaded));
50QHash<int, QByteArray> Lenses::roleNames() const
52 return m_roles;
55int Lenses::rowCount(const QModelIndex& parent) const
57 Q_UNUSED(parent)
59 return m_lenses.count();
62QVariant Lenses::data(const QModelIndex& index, int role) const
64 Q_UNUSED(role)
66 if (!index.isValid()) {
67 return QVariant();
68 }
70 Lens* lens = m_lenses.at(index.row());
72 if (role == Lenses::RoleLens) {
73 return QVariant::fromValue(lens);
74 } else if (role == Lenses::RoleId) {
75 return QVariant::fromValue(lens->id());
76 } else if (role == Lenses::RoleVisible) {
77 return QVariant::fromValue(lens->visible());
78 } else {
79 return QVariant();
80 }
83QVariant Lenses::get(int row) const
85 return data(QAbstractListModel::index(row), 0);
88QVariant Lenses::get(const QString& lens_id) const
90 Q_FOREACH(Lens* lens, m_lenses) {
91 if (lens->id() == lens_id) {
92 return QVariant::fromValue(lens);
93 }
94 }
96 return QVariant();
99bool Lenses::loaded() const
101 return m_loaded;
104void Lenses::onScopeAdded(const unity::dash::Scope::Ptr& scope, int position)
106 int index = m_lenses.count();
107 beginInsertRows(QModelIndex(), index, index);
108 addUnityLens(scope);
109 endInsertRows();
111 // FIXME: do only once after all loaded?
112 m_loaded = true;
113 Q_EMIT loadedChanged(m_loaded);
116void Lenses::onScopeRemoved(const unity::dash::Scope::Ptr& scope)
118 //TODO pawel
121void Lenses::onScopesReordered(const unity::dash::Scopes::ScopeList& scopes)
123 //TODO pawel
126void Lenses::onLensesLoaded()
128 /* FIXME: this is temporary code that is required on mobile to order
129 the lenses according to the design.
130 */
131 /*QStringList staticLenses;
132 staticLenses << "mockmusic.lens" << "people.lens" << "home.lens" << "applications.lens" << "mockvideos.lens";
134 // not all the lenses are guaranteed to go into the model (only if their UnitCore counterparts exist);
135 // so build up a list of the valid ones, then add them later.
136 QList<unity::dash::Scope::Ptr> added_lenses;
138 // add statically ordered lenses
139 Q_FOREACH(QString lensId, staticLenses) {
140 unity::dash::Scope::Ptr lens = m_unityLenses->GetScope(lensId.toStdString());
141 if (lens != NULL) {
142 added_lenses << lens;
143 }
144 }
146 // add remaining lenses
147 unity::dash::Scopes::ScopeList lensesList = m_unityLenses->GetScopes();
148 for(auto it = lensesList.begin(); it != lensesList.end(); ++it) {
149 unity::dash::Scope::Ptr lens = (*it);
150 if (!staticLenses.contains(QString::fromStdString(lens->id))) {
151 added_lenses << lens;
152 }
153 }
155 if (added_lenses.count() > 0) {
156 int index = rowCount();
157 beginInsertRows(QModelIndex(), index, index+added_lenses.count()-1);
158 Q_FOREACH(unity::dash::Scope::Ptr lens, added_lenses) {
159 addUnityLens(lens);
160 }
161 endInsertRows();
162 }*/
164 m_loaded = true;
165 Q_EMIT loadedChanged(m_loaded);
167 // listen to dynamically added lenses
168 //m_homeLens->lens_added.connect(sigc::mem_fun(this, &Lenses::onLensAdded)); //FIXME
171void Lenses::onLensPropertyChanged()
173 QModelIndex lensIndex = index(m_lenses.indexOf(qobject_cast<Lens*>(sender())));
174 Q_EMIT dataChanged(lensIndex, lensIndex);
177void Lenses::addUnityLens(const unity::dash::Scope::Ptr& unity_lens)
179 Lens* lens = new Lens(this);
180 lens->setUnityLens(unity_lens);
181 /* DOCME */
182 QObject::connect(lens, SIGNAL(visibleChanged(bool)), this, SLOT(onLensPropertyChanged()));
183 m_lenses.append(lens);
186void Lenses::removeUnityLens(int index)
188 Lens* lens = m_lenses.takeAt(index);
190 delete lens;