on bc971cd help sign in
unity8/plugins/Unity/lenses.cpp
182 lines 5.3 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>
31// libunity-core
32#include <UnityCore/FilesystemLenses.h>
33#include <UnityCore/HomeLens.h>
35Lenses::Lenses(QObject *parent)
36 : QAbstractListModel(parent)
37 , m_unityLenses(std::make_shared<unity::dash::FilesystemLenses>())
38 , m_homeLens(std::make_shared<unity::dash::HomeLens>(QString::fromUtf8(dgettext("unity", "Home")).toStdString(),
39 QString::fromUtf8(dgettext("unity", "Home screen")).toStdString(),
40 QString::fromUtf8(dgettext("unity", "Search")).toStdString()))
41 , m_loaded(false)
43 m_roles[Lenses::RoleLens] = "lens";
44 m_roles[Lenses::RoleId] = "id";
45 m_roles[Lenses::RoleVisible] = "visible";
47 m_homeLens->AddLenses(m_unityLenses);
48 std::dynamic_pointer_cast<unity::dash::FilesystemLenses>(m_unityLenses)->lenses_loaded.connect(sigc::mem_fun(this, &Lenses::onLensesLoaded));
51QHash<int, QByteArray> Lenses::roleNames() const
53 return m_roles;
56int Lenses::rowCount(const QModelIndex& parent) const
58 Q_UNUSED(parent)
60 return m_lenses.count();
63QVariant Lenses::data(const QModelIndex& index, int role) const
65 Q_UNUSED(role)
67 if (!index.isValid()) {
68 return QVariant();
69 }
71 Lens* lens = m_lenses.at(index.row());
73 if (role == Lenses::RoleLens) {
74 return QVariant::fromValue(lens);
75 } else if (role == Lenses::RoleId) {
76 return QVariant::fromValue(lens->id());
77 } else if (role == Lenses::RoleVisible) {
78 return QVariant::fromValue(lens->visible());
79 } else {
80 return QVariant();
81 }
84QVariant Lenses::get(int row) const
86 return data(QAbstractListModel::index(row), 0);
89QVariant Lenses::get(const QString& lens_id) const
91 Q_FOREACH(Lens* lens, m_lenses) {
92 if (lens->id() == lens_id) {
93 return QVariant::fromValue(lens);
94 }
95 }
97 return QVariant();
100bool Lenses::loaded() const
102 return m_loaded;
105void Lenses::onLensAdded(const unity::dash::Lens::Ptr& lens)
107 int index = m_lenses.count();
108 beginInsertRows(QModelIndex(), index, index);
109 addUnityLens(lens);
110 endInsertRows();
113void Lenses::onLensesLoaded()
115 /* FIXME: this is temporary code that is required on mobile to order
116 the lenses according to the design.
117 */
118 QStringList staticLenses;
119 staticLenses << "mockmusic.lens" << "people.lens" << "home.lens" << "applications.lens" << "mockvideos.lens";
121 // not all the lenses are guaranteed to go into the model (only if their UnitCore counterparts exist);
122 // so build up a list of the valid ones, then add them later.
123 QList<unity::dash::Lens::Ptr> added_lenses;
125 // add statically ordered lenses
126 Q_FOREACH(QString lensId, staticLenses) {
127 if (lensId == "home.lens") {
128 added_lenses << m_homeLens;
129 } else {
130 unity::dash::Lens::Ptr lens = m_unityLenses->GetLens(lensId.toStdString());
131 if (lens != NULL) {
132 added_lenses << lens;
133 }
134 }
135 }
137 // add remaining lenses
138 unity::dash::Lenses::LensList lensesList = m_unityLenses->GetLenses();
139 for(auto it = lensesList.begin(); it != lensesList.end(); ++it) {
140 unity::dash::Lens::Ptr lens = (*it);
141 if (!staticLenses.contains(QString::fromStdString(lens->id))) {
142 added_lenses << lens;
143 }
144 }
146 if (added_lenses.count() > 0) {
147 int index = rowCount();
148 beginInsertRows(QModelIndex(), index, index+added_lenses.count()-1);
149 Q_FOREACH(unity::dash::Lens::Ptr lens, added_lenses) {
150 addUnityLens(lens);
151 }
152 endInsertRows();
153 }
155 m_loaded = true;
156 Q_EMIT loadedChanged(m_loaded);
158 // listen to dynamically added lenses
159 m_homeLens->lens_added.connect(sigc::mem_fun(this, &Lenses::onLensAdded));
162void Lenses::onLensPropertyChanged()
164 QModelIndex lensIndex = index(m_lenses.indexOf(qobject_cast<Lens*>(sender())));
165 Q_EMIT dataChanged(lensIndex, lensIndex);
168void Lenses::addUnityLens(const unity::dash::Lens::Ptr& unity_lens)
170 Lens* lens = new Lens(this);
171 lens->setUnityLens(unity_lens);
172 /* DOCME */
173 QObject::connect(lens, SIGNAL(visibleChanged(bool)), this, SLOT(onLensPropertyChanged()));
174 m_lenses.append(lens);
177void Lenses::removeUnityLens(int index)
179 Lens* lens = m_lenses.takeAt(index);
181 delete lens;