2
* Copyright 2013 Canonical Ltd.
4
* This file is part of webbrowser-app.
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.
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.
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/>.
19
#include "tabs-model.h"
22
#include <QtCore/QDebug>
23
#include <QtQuick/QQuickItem>
27
\brief List model that stores the list of currently open tabs.
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,
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.
37
TabsModel::TabsModel(QObject* parent)
38
: QAbstractListModel(parent)
43
TabsModel::~TabsModel()
47
QHash<int, QByteArray> TabsModel::roleNames() const
49
static QHash<int, QByteArray> roles;
50
if (roles.isEmpty()) {
52
roles[Title] = "title";
54
roles[Thumbnail] = "thumbnail";
55
roles[WebView] = "webview";
60
int TabsModel::rowCount(const QModelIndex& parent) const
63
return m_webviews.count();
66
QVariant TabsModel::data(const QModelIndex& index, int role) const
68
if (!index.isValid()) {
71
int row = index.row();
72
if (!checkValidTabIndex(row)) {
75
QQuickItem* webview = m_webviews.at(row);
78
return webview->property("url");
80
return webview->property("title");
82
return webview->property("icon");
84
// XXX: not implemented yet
87
return QVariant::fromValue(webview);
93
int TabsModel::currentIndex() const
95
return m_currentIndex;
98
void TabsModel::setCurrentIndex(int index)
100
if (index == m_currentIndex) {
103
if (!checkValidTabIndex(index)) {
106
m_currentIndex = index;
107
Q_EMIT currentIndexChanged();
108
Q_EMIT currentWebviewChanged();
111
QQuickItem* TabsModel::currentWebview() const
113
if (m_currentIndex >= 0) {
114
return m_webviews.at(m_currentIndex);
121
Add a tab to the model and return the corresponding index in the model.
123
It is the responsibility of the caller to instantiate the corresponding
126
int TabsModel::add(QQuickItem* webview)
129
qWarning() << "Invalid WebView";
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()));
139
Q_EMIT countChanged();
144
Given its index, remove a tab from the model, and return the corresponding
147
It is the responsibility of the caller to destroy the corresponding
150
QQuickItem* TabsModel::remove(int index)
152
if (!checkValidTabIndex(index)) {
155
beginRemoveRows(QModelIndex(), index, index);
156
QQuickItem* webview = m_webviews.takeAt(index);
157
webview->disconnect(this);
159
Q_EMIT countChanged();
160
bool currentWasLast = (m_currentIndex == m_webviews.count());
161
bool removedCurrent = (index == m_currentIndex);
162
if (currentWasLast) {
164
Q_EMIT currentIndexChanged();
166
if (removedCurrent) {
167
Q_EMIT currentWebviewChanged();
172
bool TabsModel::checkValidTabIndex(int index) const
174
if ((index < 0) || (index >= m_webviews.count())) {
175
qWarning() << "Invalid tab index:" << index;
181
void TabsModel::onDataChanged(QQuickItem* webview, int role)
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);
189
void TabsModel::onUrlChanged()
191
onDataChanged(qobject_cast<QQuickItem*>(sender()), Url);
194
void TabsModel::onTitleChanged()
196
onDataChanged(qobject_cast<QQuickItem*>(sender()), Title);
199
void TabsModel::onIconChanged()
201
onDataChanged(qobject_cast<QQuickItem*>(sender()), Icon);