Falkon Develop
Cross-platform Qt-based web browser
tabmodel.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2018 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program 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#include "tabmodel.h"
19#include "webtab.h"
20#include "tabwidget.h"
21#include "browserwindow.h"
22
23// TabModelMimeData
25 : QMimeData()
26{
27}
28
30{
31 return m_tab;
32}
33
35{
36 m_tab = tab;
37}
38
39bool TabModelMimeData::hasFormat(const QString &format) const
40{
41 return mimeType() == format;
42}
43
44QStringList TabModelMimeData::formats() const
45{
46 return {mimeType()};
47}
48
49// static
51{
52 return QSL("application/falkon.tabmodel.tab");
53}
54
55// TabModel
56TabModel::TabModel(BrowserWindow *window, QObject *parent)
57 : QAbstractListModel(parent)
58 , m_window(window)
59{
60 init();
61}
62
63QModelIndex TabModel::tabIndex(WebTab *tab) const
64{
65 const int idx = m_tabs.indexOf(tab);
66 if (idx < 0) {
67 return {};
68 }
69 return index(idx);
70}
71
72WebTab *TabModel::tab(const QModelIndex &index) const
73{
74 return m_tabs.value(index.row());
75}
76
77int TabModel::rowCount(const QModelIndex &parent) const
78{
79 if (parent.isValid()) {
80 return 0;
81 }
82 return m_tabs.count();
83}
84
85Qt::ItemFlags TabModel::flags(const QModelIndex &index) const
86{
87 if (!index.isValid()) {
88 return Qt::ItemIsDropEnabled;
89 }
90 return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
91}
92
93QVariant TabModel::data(const QModelIndex &index, int role) const
94{
95 if (index.row() < 0 || index.row() > m_tabs.count()) {
96 return {};
97 }
98
99 WebTab *t = tab(index);
100 if (!t) {
101 return {};
102 }
103
104 switch (role) {
105 case WebTabRole:
106 return QVariant::fromValue(t);
107
108 case TitleRole:
109 case Qt::DisplayRole:
110 return t->title();
111
112 case IconRole:
113 case Qt::DecorationRole:
114 return t->icon();
115
116 case PinnedRole:
117 return t->isPinned();
118
119 case RestoredRole:
120 return t->isRestored();
121
122 case CurrentTabRole:
123 return t->isCurrentTab();
124
125 case LoadingRole:
126 return t->isLoading();
127
128 case AudioPlayingRole:
129 return t->isPlaying();
130
131 case AudioMutedRole:
132 return t->isMuted();
133
135 return t->backgroundActivity();
136
137 default:
138 return {};
139 }
140}
141
142Qt::DropActions TabModel::supportedDropActions() const
143{
144 return Qt::MoveAction;
145}
146
147QStringList TabModel::mimeTypes() const
148{
150}
151
152QMimeData *TabModel::mimeData(const QModelIndexList &indexes) const
153{
154 if (indexes.isEmpty()) {
155 return nullptr;
156 }
157 auto *tab = indexes.at(0).data(WebTabRole).value<WebTab*>();
158 if (!tab) {
159 return nullptr;
160 }
161 auto *mimeData = new TabModelMimeData;
162 mimeData->setTab(tab);
163 return mimeData;
164}
165
166bool TabModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const
167{
168 Q_UNUSED(row)
169 if (action != Qt::MoveAction || parent.isValid() || column > 0 || !m_window) {
170 return false;
171 }
172 const auto *mimeData = qobject_cast<const TabModelMimeData*>(data);
173 if (!mimeData) {
174 return false;
175 }
176 return mimeData->tab();
177}
178
179bool TabModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
180{
181 if (!canDropMimeData(data, action, row, column, parent)) {
182 return false;
183 }
184
185 const auto *mimeData = static_cast<const TabModelMimeData*>(data);
186 WebTab *tab = mimeData->tab();
187
188 if (tab->browserWindow() == m_window) {
189 if (row < 0) {
190 row = tab->isPinned() ? m_window->tabWidget()->pinnedTabsCount() : m_window->tabWidget()->count();
191 }
192 tab->moveTab(row > mimeData->tab()->tabIndex() ? row - 1 : row);
193 } else {
194 if (row < 0) {
195 row = m_window->tabCount();
196 }
197 if (tab->browserWindow()) {
199 }
200 tab->setPinned(row < m_window->tabWidget()->pinnedTabsCount());
201 m_window->tabWidget()->insertView(row, tab, Qz::NT_SelectedTab);
202 }
203
204 return true;
205}
206
207void TabModel::init()
208{
209 for (int i = 0; i < m_window->tabCount(); ++i) {
210 tabInserted(i);
211 }
212
213 connect(m_window->tabWidget(), &TabWidget::tabInserted, this, &TabModel::tabInserted);
214 connect(m_window->tabWidget(), &TabWidget::tabRemoved, this, &TabModel::tabRemoved);
215 connect(m_window->tabWidget(), &TabWidget::tabMoved, this, &TabModel::tabMoved);
216
217 connect(m_window, &QObject::destroyed, this, [this]() {
218 beginResetModel();
219 m_window = nullptr;
220 m_tabs.clear();
221 endResetModel();
222 });
223}
224
225void TabModel::tabInserted(int index)
226{
227 WebTab *tab = m_window->tabWidget()->webTab(index);
228
229 beginInsertRows(QModelIndex(), index, index);
230 m_tabs.insert(index, tab);
231 endInsertRows();
232
233 auto emitDataChanged = [this](WebTab *tab, int role) {
234 const QModelIndex idx = tabIndex(tab);
235 Q_EMIT dataChanged(idx, idx, {role});
236 };
237
238 connect(tab, &WebTab::titleChanged, this, std::bind(emitDataChanged, tab, Qt::DisplayRole));
239 connect(tab, &WebTab::titleChanged, this, std::bind(emitDataChanged, tab, TitleRole));
240 connect(tab, &WebTab::iconChanged, this, std::bind(emitDataChanged, tab, Qt::DecorationRole));
241 connect(tab, &WebTab::iconChanged, this, std::bind(emitDataChanged, tab, IconRole));
242 connect(tab, &WebTab::pinnedChanged, this, std::bind(emitDataChanged, tab, PinnedRole));
243 connect(tab, &WebTab::restoredChanged, this, std::bind(emitDataChanged, tab, RestoredRole));
244 connect(tab, &WebTab::currentTabChanged, this, std::bind(emitDataChanged, tab, CurrentTabRole));
245 connect(tab, &WebTab::loadingChanged, this, std::bind(emitDataChanged, tab, LoadingRole));
246 connect(tab, &WebTab::playingChanged, this, std::bind(emitDataChanged, tab, AudioPlayingRole));
247 connect(tab, &WebTab::mutedChanged, this, std::bind(emitDataChanged, tab, AudioMutedRole));
248 connect(tab, &WebTab::backgroundActivityChanged, this, std::bind(emitDataChanged, tab, BackgroundActivityRole));
249}
250
251void TabModel::tabRemoved(int index)
252{
253 beginRemoveRows(QModelIndex(), index, index);
254 m_tabs.remove(index);
255 endRemoveRows();
256}
257
258void TabModel::tabMoved(int from, int to)
259{
260 if (!beginMoveRows(QModelIndex(), from, from, QModelIndex(), to > from ? to + 1 : to)) {
261 qWarning() << "Invalid beginMoveRows" << from << (to > from ? to + 1 : to);
262 return;
263 }
264 m_tabs.insert(to, m_tabs.takeAt(from));
265 endMoveRows();
266}
TabWidget * tabWidget() const
int tabCount() const
bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const override
Definition: tabmodel.cpp:166
QStringList mimeTypes() const override
Definition: tabmodel.cpp:147
Qt::DropActions supportedDropActions() const override
Definition: tabmodel.cpp:142
QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const override
Definition: tabmodel.cpp:93
WebTab * tab(const QModelIndex &index) const
Definition: tabmodel.cpp:72
Qt::ItemFlags flags(const QModelIndex &index) const override
Definition: tabmodel.cpp:85
bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override
Definition: tabmodel.cpp:179
QMimeData * mimeData(const QModelIndexList &indexes) const override
Definition: tabmodel.cpp:152
TabModel(BrowserWindow *window, QObject *parent=nullptr)
Definition: tabmodel.cpp:56
@ PinnedRole
Definition: tabmodel.h:57
@ AudioPlayingRole
Definition: tabmodel.h:61
@ LoadingRole
Definition: tabmodel.h:60
@ RestoredRole
Definition: tabmodel.h:58
@ CurrentTabRole
Definition: tabmodel.h:59
@ WebTabRole
Definition: tabmodel.h:54
@ IconRole
Definition: tabmodel.h:56
@ BackgroundActivityRole
Definition: tabmodel.h:63
@ TitleRole
Definition: tabmodel.h:55
@ AudioMutedRole
Definition: tabmodel.h:62
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Definition: tabmodel.cpp:77
QModelIndex tabIndex(WebTab *tab) const
Definition: tabmodel.cpp:63
QStringList formats() const override
Definition: tabmodel.cpp:44
static QString mimeType()
Definition: tabmodel.cpp:50
bool hasFormat(const QString &format) const override
Definition: tabmodel.cpp:39
void setTab(WebTab *tab)
Definition: tabmodel.cpp:34
WebTab * tab() const
Definition: tabmodel.cpp:29
WebTab * webTab(int index=-1) const
Definition: tabwidget.cpp:570
int insertView(int index, WebTab *tab, const Qz::NewTabPositionFlags &openFlags)
Definition: tabwidget.cpp:400
void tabInserted(int index)
void tabRemoved(int index)
int pinnedTabsCount() const
Definition: tabwidget.cpp:556
void tabMoved(int from, int to)
void detachTab(WebTab *tab)
Definition: tabwidget.cpp:685
Definition: webtab.h:40
void setPinned(bool state)
Definition: webtab.cpp:419
void mutedChanged(bool muted)
void titleChanged(const QString &title)
void currentTabChanged(bool current)
void restoredChanged(bool restored)
void iconChanged(const QIcon &icon)
void pinnedChanged(bool pinned)
bool isPinned() const
Definition: webtab.cpp:414
BrowserWindow * browserWindow() const
Definition: webtab.cpp:204
void moveTab(int to)
Definition: webtab.cpp:703
void backgroundActivityChanged(bool activity)
void loadingChanged(bool loading)
void playingChanged(bool playing)
@ NT_SelectedTab
Definition: qzcommon.h:97
t
Definition: i18n.py:27
i
Definition: i18n.py:23
#define QSL(x)
Definition: qzcommon.h:40