~osomon/ubuntu-ui-extras/revert-changed-tests

« back to all changes in this revision

Viewing changes to modules/Ubuntu/Components/Extras/plugin/browser/tabs-model.cpp

  • Committer: Ugo Riboni
  • Date: 2013-06-25 13:51:18 UTC
  • Revision ID: ugo.riboni@canonical.com-20130625135118-scs2wpej1kyg3fc1
Merge changes to Browser.qml related to tabs support, except autopilot tests.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2013 Canonical Ltd.
 
3
 *
 
4
 * This file is part of webbrowser-app.
 
5
 *
 
6
 * webbrowser-app is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; version 3.
 
9
 *
 
10
 * webbrowser-app is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
#include "tabs-model.h"
 
20
 
 
21
// Qt
 
22
#include <QtCore/QDebug>
 
23
#include <QtQuick/QQuickItem>
 
24
 
 
25
/*!
 
26
    \class TabsModel
 
27
    \brief List model that stores the list of currently open tabs.
 
28
 
 
29
    TabsModel is a list model that stores the list of currently open tabs.
 
30
    Each tab holds a pointer to a WebView and associated metadata (URL, title,
 
31
    icon, thumbnail).
 
32
 
 
33
    The model doesn’t own the WebView, so it is the responsibility of whoever
 
34
    adds a tab to instantiate the corresponding WebView, and to destroy it after
 
35
    it’s removed from the model.
 
36
*/
 
37
TabsModel::TabsModel(QObject* parent)
 
38
    : QAbstractListModel(parent)
 
39
    , m_currentIndex(-1)
 
40
{
 
41
}
 
42
 
 
43
TabsModel::~TabsModel()
 
44
{
 
45
}
 
46
 
 
47
QHash<int, QByteArray> TabsModel::roleNames() const
 
48
{
 
49
    static QHash<int, QByteArray> roles;
 
50
    if (roles.isEmpty()) {
 
51
        roles[Url] = "url";
 
52
        roles[Title] = "title";
 
53
        roles[Icon] = "icon";
 
54
        roles[Thumbnail] = "thumbnail";
 
55
        roles[WebView] = "webview";
 
56
    }
 
57
    return roles;
 
58
}
 
59
 
 
60
int TabsModel::rowCount(const QModelIndex& parent) const
 
61
{
 
62
    Q_UNUSED(parent);
 
63
    return m_webviews.count();
 
64
}
 
65
 
 
66
QVariant TabsModel::data(const QModelIndex& index, int role) const
 
67
{
 
68
    if (!index.isValid()) {
 
69
        return QVariant();
 
70
    }
 
71
    int row = index.row();
 
72
    if (!checkValidTabIndex(row)) {
 
73
        return QVariant();
 
74
    }
 
75
    QQuickItem* webview = m_webviews.at(row);
 
76
    switch (role) {
 
77
    case Url:
 
78
        return webview->property("url");
 
79
    case Title:
 
80
        return webview->property("title");
 
81
    case Icon:
 
82
        return webview->property("icon");
 
83
    case Thumbnail:
 
84
        // XXX: not implemented yet
 
85
        return QVariant();
 
86
    case WebView:
 
87
        return QVariant::fromValue(webview);
 
88
    default:
 
89
        return QVariant();
 
90
    }
 
91
}
 
92
 
 
93
int TabsModel::currentIndex() const
 
94
{
 
95
    return m_currentIndex;
 
96
}
 
97
 
 
98
void TabsModel::setCurrentIndex(int index)
 
99
{
 
100
    if (index == m_currentIndex) {
 
101
        return;
 
102
    }
 
103
    if (!checkValidTabIndex(index)) {
 
104
        return;
 
105
    }
 
106
    m_currentIndex = index;
 
107
    Q_EMIT currentIndexChanged();
 
108
    Q_EMIT currentWebviewChanged();
 
109
}
 
110
 
 
111
QQuickItem* TabsModel::currentWebview() const
 
112
{
 
113
    if (m_currentIndex >= 0) {
 
114
        return m_webviews.at(m_currentIndex);
 
115
    } else {
 
116
        return 0;
 
117
    }
 
118
}
 
119
 
 
120
/*!
 
121
    Add a tab to the model and return the corresponding index in the model.
 
122
 
 
123
    It is the responsibility of the caller to instantiate the corresponding
 
124
    WebView beforehand.
 
125
*/
 
126
int TabsModel::add(QQuickItem* webview)
 
127
{
 
128
    if (webview == 0) {
 
129
        qWarning() << "Invalid WebView";
 
130
        return -1;
 
131
    }
 
132
    int index = m_webviews.count();
 
133
    beginInsertRows(QModelIndex(), index, index);
 
134
    m_webviews.append(webview);
 
135
    connect(webview, SIGNAL(urlChanged()), SLOT(onUrlChanged()));
 
136
    connect(webview, SIGNAL(titleChanged()), SLOT(onTitleChanged()));
 
137
    connect(webview, SIGNAL(iconChanged()), SLOT(onIconChanged()));
 
138
    endInsertRows();
 
139
    Q_EMIT countChanged();
 
140
    return index;
 
141
}
 
142
 
 
143
/*!
 
144
    Given its index, remove a tab from the model, and return the corresponding
 
145
    WebView.
 
146
 
 
147
    It is the responsibility of the caller to destroy the corresponding
 
148
    WebView afterwards.
 
149
*/
 
150
QQuickItem* TabsModel::remove(int index)
 
151
{
 
152
    if (!checkValidTabIndex(index)) {
 
153
        return 0;
 
154
    }
 
155
    beginRemoveRows(QModelIndex(), index, index);
 
156
    QQuickItem* webview = m_webviews.takeAt(index);
 
157
    webview->disconnect(this);
 
158
    endRemoveRows();
 
159
    Q_EMIT countChanged();
 
160
    bool currentWasLast = (m_currentIndex == m_webviews.count());
 
161
    bool removedCurrent = (index == m_currentIndex);
 
162
    if (currentWasLast) {
 
163
        --m_currentIndex;
 
164
        Q_EMIT currentIndexChanged();
 
165
    }
 
166
    if (removedCurrent) {
 
167
        Q_EMIT currentWebviewChanged();
 
168
    }
 
169
    return webview;
 
170
}
 
171
 
 
172
bool TabsModel::checkValidTabIndex(int index) const
 
173
{
 
174
    if ((index < 0) || (index >= m_webviews.count())) {
 
175
        qWarning() << "Invalid tab index:" << index;
 
176
        return false;
 
177
    }
 
178
    return true;
 
179
}
 
180
 
 
181
void TabsModel::onDataChanged(QQuickItem* webview, int role)
 
182
{
 
183
    int index = m_webviews.indexOf(webview);
 
184
    if (checkValidTabIndex(index)) {
 
185
        Q_EMIT dataChanged(this->index(index, 0), this->index(index, 0), QVector<int>() << role);
 
186
    }
 
187
}
 
188
 
 
189
void TabsModel::onUrlChanged()
 
190
{
 
191
    onDataChanged(qobject_cast<QQuickItem*>(sender()), Url);
 
192
}
 
193
 
 
194
void TabsModel::onTitleChanged()
 
195
{
 
196
    onDataChanged(qobject_cast<QQuickItem*>(sender()), Title);
 
197
}
 
198
 
 
199
void TabsModel::onIconChanged()
 
200
{
 
201
    onDataChanged(qobject_cast<QQuickItem*>(sender()), Icon);
 
202
}