1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
#include <QtCore/QDebug>
#include "registry.h"
#include "watchletplugininterface.h"
#include "watchletsmodel.h"
using namespace sowatch;
WatchletsModel::WatchletsModel(QObject *parent) :
QAbstractListModel(parent)
{
QHash<int, QByteArray> roles = roleNames();
roles[TitleRole] = QByteArray("title");
roles[IconRole] = QByteArray("icon");
roles[ObjectRole] = QByteArray("object");
setRoleNames(roles);
}
QString WatchletsModel::watchModel() const
{
return _watchModel;
}
void WatchletsModel::setWatchModel(const QString &s)
{
_watchModel = s;
}
int WatchletsModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : _list.count();
}
QVariant WatchletsModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid()) return QVariant();
const int row = index.row();
if (row >= _list.size()) {
qWarning() << "Could not seek to" << row;
return QVariant();
}
const Watchlet *watchlet = _list.at(row);
const WatchletInfo& info = _info.at(row);
switch (role) {
case Qt::DisplayRole:
return QVariant::fromValue(info.name);
case IconRole:
return QVariant::fromValue(info.icon);
case ObjectRole:
return QVariant::fromValue(const_cast<sowatch::Watchlet*>(watchlet));
}
return QVariant();
}
int WatchletsModel::size() const
{
return _list.size();
}
Watchlet * WatchletsModel::at(int position) const
{
Q_ASSERT(position < _list.size());
return const_cast<sowatch::Watchlet*>(_list.at(position));
}
void WatchletsModel::add(Watchlet *w)
{
insert(_list.size(), w);
}
void WatchletsModel::insert(int position, Watchlet *w)
{
Q_ASSERT(position >= 0 && position <= _list.size());
beginInsertRows(QModelIndex(), position, position);
_list.insert(position, w);
_info.insert(position, getInfoForWatchlet(w));
endInsertRows();
emit modelChanged();
}
void WatchletsModel::move(const Watchlet *w, int to)
{
move(_list.indexOf(const_cast<Watchlet*>(w)), to);
}
void WatchletsModel::move(int position, int to)
{
Q_ASSERT(position >= 0 && position < _list.size());
Q_ASSERT(to >= 0 && to < _list.size());
beginMoveRows(QModelIndex(), position, position, QModelIndex(), to);
_list.move(position, to);
_info.move(position, to);
endMoveRows();
emit modelChanged();
}
void WatchletsModel::remove(const Watchlet *w)
{
remove(_list.indexOf(const_cast<Watchlet*>(w)));
}
void WatchletsModel::remove(int position)
{
Q_ASSERT(position >= 0 && position < _list.size());
beginRemoveRows(QModelIndex(), position, position);
_list.removeAt(position);
_info.removeAt(position);
endRemoveRows();
emit modelChanged();
}
WatchletsModel::WatchletInfo WatchletsModel::getInfoForWatchlet(const Watchlet *w)
{
QString id = w->id();
WatchletPluginInterface *plugin = Registry::registry()->getWatchletPlugin(id);
if (plugin) {
return plugin->describeWatchlet(id, _watchModel);
} else {
return WatchletInfo();
}
}
|