~unity-team/unity8/trunk

« back to all changes in this revision

Viewing changes to tests/qmltests/plugins/Ubuntu/ChewieUI/fake_pluginmodel.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 2012 Canonical Ltd.
 
3
 *
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU Lesser General Public License as published by
 
6
 * the Free Software Foundation; version 3.
 
7
 *
 
8
 * This program is distributed in the hope that it will be useful,
 
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 * GNU Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authors:
 
17
 *      Renato Araujo Oliveira Filho <renato@canonical.com>
 
18
 */
 
19
 
 
20
#include "fake_pluginmodel.h"
 
21
#include "paths.h"
 
22
 
 
23
#include <QQmlContext>
 
24
#include <QQmlEngine>
 
25
#include <QDebug>
 
26
#include <QQmlComponent>
 
27
 
 
28
struct Indicator {
 
29
    Indicator(QString const& title, QString iconSource, QString label, QString description, QString componentSource)
 
30
    : title(title)
 
31
    , iconSource(iconSource)
 
32
    , label(label)
 
33
    , description(description)
 
34
    , componentSource(componentSource)
 
35
    , isValid(true)
 
36
    {}
 
37
 
 
38
    QString title;
 
39
    QString iconSource;
 
40
    QString label;
 
41
    QString description;
 
42
    QString componentSource;
 
43
    QMap<QString, QVariant> initialProperties;
 
44
    bool isValid;
 
45
};
 
46
 
 
47
const QList<Indicator> indicator_list = QList<Indicator>()
 
48
    << Indicator("Menu1",   sourceDirectory()+"Panel/graphics/Battery@18.png",      "",     "",     sourceDirectory()+"tests/qmltests/Panel/qml/fake_menu_plugin1.qml")
 
49
    << Indicator("Menu2",   sourceDirectory()+"Panel/graphics/Bluetooth@18.png",    "",     "",     sourceDirectory()+"tests/qmltests/Panel/qml/fake_menu_plugin2.qml")
 
50
    << Indicator("Menu3",   sourceDirectory()+"Panel/graphics/Clock@18.png",        "",     "",     sourceDirectory()+"tests/qmltests/Panel/qml/fake_menu_plugin3.qml")
 
51
    << Indicator("Menu4",   sourceDirectory()+"Panel/graphics/Location@18.png",     "",     "",     sourceDirectory()+"tests/qmltests/Panel/qml/fake_menu_plugin4.qml")
 
52
    << Indicator("Menu5",   sourceDirectory()+"Panel/graphics/Network@18.png",      "",     "",     sourceDirectory()+"tests/qmltests/Panel/qml/fake_menu_plugin5.qml");
 
53
 
 
54
 
 
55
PluginModel::PluginModel(QObject *parent)
 
56
    : QAbstractListModel(parent)
 
57
{
 
58
    m_widgetsMap = new WidgetsMap;
 
59
 
 
60
    QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
 
61
    QObject::connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SIGNAL(countChanged()));
 
62
    QObject::connect(this, SIGNAL(modelReset()), this, SIGNAL(countChanged()));
 
63
}
 
64
 
 
65
/*! \internal */
 
66
PluginModel::~PluginModel()
 
67
{
 
68
    delete m_widgetsMap;
 
69
}
 
70
 
 
71
/*!
 
72
    \qmlmethod PluginModel::get(int)
 
73
 
 
74
    Returns the item at index in the model. This allows the item data to be accessed from JavaScript:
 
75
    \b Note: methods should only be called after the Component has completed.
 
76
*/
 
77
QVariantMap PluginModel::get(int row) const
 
78
{
 
79
    QVariantMap result;
 
80
 
 
81
    QModelIndex index = this->index(row);
 
82
    if (index.isValid()) {
 
83
        QMap<int, QVariant> data = itemData(index);
 
84
        const QHash<int, QByteArray> roleNames = this->roleNames();
 
85
        Q_FOREACH(int i, roleNames.keys()) {
 
86
            result.insert(roleNames[i], data[i]);
 
87
        }
 
88
    }
 
89
    return result;
 
90
}
 
91
 
 
92
/*!
 
93
    \qmlproperty PluginModel::count
 
94
    The number of data entries in the model.
 
95
 
 
96
    \b Note: methods should only be called after the Component has completed.
 
97
*/
 
98
int PluginModel::count() const
 
99
{
 
100
    return rowCount();
 
101
}
 
102
 
 
103
/*!
 
104
    \qmlproperty WidgetsMap PluginModel::widgetsMap
 
105
    This property holds the map for all new widgets registered by dynamically loaded plugins.
 
106
*/
 
107
WidgetsMap* PluginModel::widgetsMap() const
 
108
{
 
109
    return m_widgetsMap;
 
110
}
 
111
 
 
112
/*!
 
113
    \qmlmethod PluginModel::load()
 
114
 
 
115
    Load all plugin information available on baseDir.
 
116
*/
 
117
void PluginModel::load()
 
118
{
 
119
 
 
120
}
 
121
 
 
122
/*!
 
123
    \qmlmethod PluginModel::unload()
 
124
 
 
125
    Unload all plugins.
 
126
*/
 
127
void PluginModel::unload()
 
128
{
 
129
    m_widgetsMap->clear();
 
130
}
 
131
 
 
132
/*! \internal */
 
133
QHash<int, QByteArray> PluginModel::roleNames() const
 
134
{
 
135
    static QHash<int, QByteArray> roles;
 
136
    if (roles.isEmpty()) {
 
137
        roles[Title] = "title";
 
138
        roles[IconSource] = "iconSource";
 
139
        roles[Label] = "label";
 
140
        roles[Description] = "description";
 
141
        roles[QMLComponent] = "component";
 
142
        roles[InitialProperties] = "initialProperties";
 
143
        roles[IsValid] = "isValid";
 
144
    }
 
145
    return roles;
 
146
}
 
147
 
 
148
/*! \internal */
 
149
int PluginModel::columnCount(const QModelIndex &) const
 
150
{
 
151
    return 1;
 
152
}
 
153
 
 
154
/*! \internal */
 
155
QVariant PluginModel::data(const QModelIndex &index, int role) const
 
156
{
 
157
    QVariant attribute;
 
158
 
 
159
    if ((index.row() >= 0) && (index.row() < indicator_list.size())) {
 
160
        Indicator indicator = (indicator_list[index.row()]);
 
161
 
 
162
        switch (role) {
 
163
        case Title:
 
164
            attribute = indicator.title;
 
165
            break;
 
166
        case IconSource:
 
167
            attribute = indicator.iconSource;
 
168
            break;
 
169
        case Label:
 
170
            attribute = indicator.label;
 
171
            break;
 
172
        case Description:
 
173
            attribute = indicator.description;
 
174
            break;
 
175
        case QMLComponent:
 
176
        {
 
177
            QQmlContext *context = QQmlEngine::contextForObject(this);
 
178
            if (context) {
 
179
                QQmlComponent* component = new QQmlComponent(context->parentContext()->engine(), QUrl(indicator.componentSource), NULL);
 
180
                attribute = QVariant::fromValue<QQmlComponent*>(component);
 
181
            }
 
182
        }   break;
 
183
        case InitialProperties:
 
184
            attribute = indicator.initialProperties;
 
185
            break;
 
186
        case IsValid:
 
187
            attribute = indicator.isValid;
 
188
            break;
 
189
        default:
 
190
            break;
 
191
        }
 
192
    }
 
193
    return attribute;
 
194
}
 
195
 
 
196
/*! \internal */
 
197
QModelIndex PluginModel::parent(const QModelIndex&) const
 
198
{
 
199
    return QModelIndex();
 
200
}
 
201
 
 
202
/*! \internal */
 
203
int PluginModel::rowCount(const QModelIndex &) const
 
204
{
 
205
    return indicator_list.size();
 
206
}