2
* Copyright 2012 Canonical Ltd.
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.
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.
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/>.
17
* Renato Araujo Oliveira Filho <renato@canonical.com>
20
#include "fake_pluginmodel.h"
23
#include <QQmlContext>
26
#include <QQmlComponent>
29
Indicator(QString const& title, QString iconSource, QString label, QString description, QString componentSource)
31
, iconSource(iconSource)
33
, description(description)
34
, componentSource(componentSource)
42
QString componentSource;
43
QMap<QString, QVariant> initialProperties;
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");
55
PluginModel::PluginModel(QObject *parent)
56
: QAbstractListModel(parent)
58
m_widgetsMap = new WidgetsMap;
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()));
66
PluginModel::~PluginModel()
72
\qmlmethod PluginModel::get(int)
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.
77
QVariantMap PluginModel::get(int row) const
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]);
93
\qmlproperty PluginModel::count
94
The number of data entries in the model.
96
\b Note: methods should only be called after the Component has completed.
98
int PluginModel::count() const
104
\qmlproperty WidgetsMap PluginModel::widgetsMap
105
This property holds the map for all new widgets registered by dynamically loaded plugins.
107
WidgetsMap* PluginModel::widgetsMap() const
113
\qmlmethod PluginModel::load()
115
Load all plugin information available on baseDir.
117
void PluginModel::load()
123
\qmlmethod PluginModel::unload()
127
void PluginModel::unload()
129
m_widgetsMap->clear();
133
QHash<int, QByteArray> PluginModel::roleNames() const
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";
149
int PluginModel::columnCount(const QModelIndex &) const
155
QVariant PluginModel::data(const QModelIndex &index, int role) const
159
if ((index.row() >= 0) && (index.row() < indicator_list.size())) {
160
Indicator indicator = (indicator_list[index.row()]);
164
attribute = indicator.title;
167
attribute = indicator.iconSource;
170
attribute = indicator.label;
173
attribute = indicator.description;
177
QQmlContext *context = QQmlEngine::contextForObject(this);
179
QQmlComponent* component = new QQmlComponent(context->parentContext()->engine(), QUrl(indicator.componentSource), NULL);
180
attribute = QVariant::fromValue<QQmlComponent*>(component);
183
case InitialProperties:
184
attribute = indicator.initialProperties;
187
attribute = indicator.isValid;
197
QModelIndex PluginModel::parent(const QModelIndex&) const
199
return QModelIndex();
203
int PluginModel::rowCount(const QModelIndex &) const
205
return indicator_list.size();