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
|
/****************************************************************************
* Copyright (C) 2012 Instituto Nokia de Tecnologia (INdT) *
* *
* This file may be used under the terms of the GNU Lesser *
* General Public License version 2.1 as published by the Free Software *
* Foundation and appearing in the file LICENSE.LGPL included in the *
* packaging of this file. Please review the following information to *
* ensure the GNU Lesser General Public License version 2.1 requirements *
* will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
****************************************************************************/
#include "TabsModel.h"
enum TabModelRole {
UrlRole = Qt::UserRole + 1,
ScreenshotRole
};
TabsModel::TabsModel(QObject* parent)
: QAbstractListModel(parent)
, m_currentWebViewIndex(-1)
{
connect(this, SIGNAL(rowsInserted(const QModelIndex&, int, int)), SIGNAL(countChanged()));
connect(this, SIGNAL(rowsRemoved(const QModelIndex&, int, int)), SIGNAL(countChanged()));
generateRolenames();
}
QVariant TabsModel::data(const QModelIndex& index, int role) const
{
if (!index.isValid() || index.row() >= m_list.size())
return QVariant();
const QPointer<QObject>& data = m_list.at(index.row());
switch (role) {
case UrlRole:
if (data)
return data->property("url");
break;
case ScreenshotRole:
static QString defaultScreenShot = QLatin1String("qrc:///mobile/grid/thumb_tabs_placeholder");
return QVariant::fromValue(defaultScreenShot);
}
return QVariant();
}
void TabsModel::append(QObject* webView)
{
QModelIndex index = QModelIndex();
beginInsertRows(index, rowCount(index), rowCount(index));
QPointer<QObject> item = webView;
connect(webView, SIGNAL(urlChanged()), SLOT(onUrlChanged()));
m_list.append(item);
endInsertRows();
setCurrentWebViewIndex(m_list.size() - 1);
}
void TabsModel::onUrlChanged()
{
QObject* webView = sender();
int i = 0;
for (TabsList::iterator it = m_list.begin(); it != m_list.end(); ++it, ++i) {
if (it->data() == webView) {
QModelIndex modelIndex = createIndex(i, 0);
emit dataChanged(modelIndex, modelIndex);
return;
}
}
}
void TabsModel::remove(int pos)
{
if (pos < 0 || pos >= m_list.size())
return;
beginRemoveRows(QModelIndex(), pos, pos);
QPointer<QObject> item = m_list.takeAt(pos);
endRemoveRows();
delete item;
QModelIndex start = createIndex(pos, 0), end = createIndex(m_list.size() - 1, 0);
emit dataChanged(start, end);
if (currentWebViewIndex() < 0)
return;
if (m_list.empty())
setCurrentWebViewIndex(-1);
else {
if (pos > 0)
setCurrentWebViewIndex(pos - 1);
else
emit currentWebViewChanged();
}
}
void TabsModel::generateRolenames()
{
QHash<int, QByteArray> roles;
roles[UrlRole] = QByteArray("url");
roles[ScreenshotRole] = QByteArray("screenshot");
setRoleNames(roles);
}
void TabsModel::setCurrentWebViewIndex(int pos)
{
if (m_currentWebViewIndex == pos || pos >= m_list.size())
return;
m_currentWebViewIndex = pos;
emit currentWebViewIndexChanged();
emit currentWebViewChanged();
}
QObject* TabsModel::currentWebView()
{
if (m_currentWebViewIndex < 0)
return 0;
return m_list[m_currentWebViewIndex].data();
}
int TabsModel::rowCount(const QModelIndex&) const
{
return m_list.count();
}
|