2
* Copyright (C) 2011 Canonical, Ltd.
5
* Florian Boucault <florian.boucault@canonical.com>
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.
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.
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/>.
28
#include <QtCore/QStringList>
29
#include <QtGui/QKeySequence>
32
#include <UnityCore/FilesystemLenses.h>
33
#include <UnityCore/HomeLens.h>
35
Lenses::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()))
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));
51
QHash<int, QByteArray> Lenses::roleNames() const
56
int Lenses::rowCount(const QModelIndex& parent) const
60
return m_lenses.count();
63
QVariant Lenses::data(const QModelIndex& index, int role) const
67
if (!index.isValid()) {
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());
84
QVariant Lenses::get(int row) const
86
return data(QAbstractListModel::index(row), 0);
89
QVariant 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);
100
bool Lenses::loaded() const
105
void Lenses::onLensAdded(const unity::dash::Lens::Ptr& lens)
107
int index = m_lenses.count();
108
beginInsertRows(QModelIndex(), index, index);
113
void Lenses::onLensesLoaded()
115
/* FIXME: this is temporary code that is required on mobile to order
116
the lenses according to the design.
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;
130
unity::dash::Lens::Ptr lens = m_unityLenses->GetLens(lensId.toStdString());
132
added_lenses << lens;
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;
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) {
156
Q_EMIT loadedChanged(m_loaded);
158
// listen to dynamically added lenses
159
m_homeLens->lens_added.connect(sigc::mem_fun(this, &Lenses::onLensAdded));
162
void Lenses::onLensPropertyChanged()
164
QModelIndex lensIndex = index(m_lenses.indexOf(qobject_cast<Lens*>(sender())));
165
Q_EMIT dataChanged(lensIndex, lensIndex);
168
void Lenses::addUnityLens(const unity::dash::Lens::Ptr& unity_lens)
170
Lens* lens = new Lens(this);
171
lens->setUnityLens(unity_lens);
173
QObject::connect(lens, SIGNAL(visibleChanged(bool)), this, SLOT(onLensPropertyChanged()));
174
m_lenses.append(lens);
177
void Lenses::removeUnityLens(int index)
179
Lens* lens = m_lenses.takeAt(index);