~unity-team/unity8/trunk

« back to all changes in this revision

Viewing changes to plugins/Unity/lenses.cpp

  • Committer: Michał Sawicz
  • Date: 2013-06-05 22:03:08 UTC
  • Revision ID: michal.sawicz@canonical.com-20130605220308-yny8fv3futtr04fg
Inital unity8 commit.

Previous history can be found at https://code.launchpad.net/~unity-team/unity/phablet

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 */
 
19
 
 
20
// Self
 
21
#include "lenses.h"
 
22
 
 
23
// Local
 
24
#include "lens.h"
 
25
 
 
26
// Qt
 
27
#include <QDebug>
 
28
#include <QtCore/QStringList>
 
29
#include <QtGui/QKeySequence>
 
30
 
 
31
// libunity-core
 
32
#include <UnityCore/FilesystemLenses.h>
 
33
#include <UnityCore/HomeLens.h>
 
34
 
 
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()))
 
41
    , m_loaded(false)
 
42
{
 
43
    m_roles[Lenses::RoleLens] = "lens";
 
44
    m_roles[Lenses::RoleId] = "id";
 
45
    m_roles[Lenses::RoleVisible] = "visible";
 
46
 
 
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));
 
49
}
 
50
 
 
51
QHash<int, QByteArray> Lenses::roleNames() const
 
52
{
 
53
    return m_roles;
 
54
}
 
55
 
 
56
int Lenses::rowCount(const QModelIndex& parent) const
 
57
{
 
58
    Q_UNUSED(parent)
 
59
 
 
60
    return m_lenses.count();
 
61
}
 
62
 
 
63
QVariant Lenses::data(const QModelIndex& index, int role) const
 
64
{
 
65
    Q_UNUSED(role)
 
66
 
 
67
    if (!index.isValid()) {
 
68
        return QVariant();
 
69
    }
 
70
 
 
71
    Lens* lens = m_lenses.at(index.row());
 
72
 
 
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
    }
 
82
}
 
83
 
 
84
QVariant Lenses::get(int row) const
 
85
{
 
86
    return data(QAbstractListModel::index(row), 0);
 
87
}
 
88
 
 
89
QVariant Lenses::get(const QString& lens_id) const
 
90
{
 
91
    Q_FOREACH(Lens* lens, m_lenses) {
 
92
        if (lens->id() == lens_id) {
 
93
            return QVariant::fromValue(lens);
 
94
        }
 
95
    }
 
96
 
 
97
    return QVariant();
 
98
}
 
99
 
 
100
bool Lenses::loaded() const
 
101
{
 
102
    return m_loaded;
 
103
}
 
104
 
 
105
void Lenses::onLensAdded(const unity::dash::Lens::Ptr& lens)
 
106
{
 
107
    int index = m_lenses.count();
 
108
    beginInsertRows(QModelIndex(), index, index);
 
109
    addUnityLens(lens);
 
110
    endInsertRows();
 
111
}
 
112
 
 
113
void Lenses::onLensesLoaded()
 
114
{
 
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";
 
120
 
 
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;
 
124
 
 
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
    }
 
136
 
 
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
    }
 
145
 
 
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
    }
 
154
 
 
155
    m_loaded = true;
 
156
    Q_EMIT loadedChanged(m_loaded);
 
157
 
 
158
    // listen to dynamically added lenses
 
159
    m_homeLens->lens_added.connect(sigc::mem_fun(this, &Lenses::onLensAdded));
 
160
}
 
161
 
 
162
void Lenses::onLensPropertyChanged()
 
163
{
 
164
    QModelIndex lensIndex = index(m_lenses.indexOf(qobject_cast<Lens*>(sender())));
 
165
    Q_EMIT dataChanged(lensIndex, lensIndex);
 
166
}
 
167
 
 
168
void Lenses::addUnityLens(const unity::dash::Lens::Ptr& unity_lens)
 
169
{
 
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);
 
175
}
 
176
 
 
177
void Lenses::removeUnityLens(int index)
 
178
{
 
179
    Lens* lens = m_lenses.takeAt(index);
 
180
 
 
181
    delete lens;
 
182
}