2018-01-30 14:53:18 +01:00
|
|
|
/* ============================================================
|
|
|
|
* Falkon - Qt web browser
|
|
|
|
* Copyright (C) 2018 David Rosca <nowrep@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "tabmodel.h"
|
|
|
|
#include "webtab.h"
|
|
|
|
#include "tabwidget.h"
|
|
|
|
#include "tabbedwebview.h"
|
|
|
|
#include "browserwindow.h"
|
|
|
|
|
2018-02-04 14:24:40 +01:00
|
|
|
// TabModelMimeData
|
|
|
|
TabModelMimeData::TabModelMimeData()
|
|
|
|
: QMimeData()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
WebTab *TabModelMimeData::tab() const
|
|
|
|
{
|
|
|
|
return m_tab;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabModelMimeData::setTab(WebTab *tab)
|
|
|
|
{
|
|
|
|
m_tab = tab;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TabModelMimeData::hasFormat(const QString &format) const
|
|
|
|
{
|
|
|
|
return mimeType() == format;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList TabModelMimeData::formats() const
|
|
|
|
{
|
|
|
|
return {mimeType()};
|
|
|
|
}
|
2018-01-30 18:55:18 +01:00
|
|
|
|
2018-02-04 14:24:40 +01:00
|
|
|
// static
|
|
|
|
QString TabModelMimeData::mimeType()
|
|
|
|
{
|
|
|
|
return QSL("application/falkon.tabmodel.tab");
|
|
|
|
}
|
|
|
|
|
|
|
|
// TabModel
|
2018-01-30 14:53:18 +01:00
|
|
|
TabModel::TabModel(BrowserWindow *window, QObject *parent)
|
|
|
|
: QAbstractListModel(parent)
|
|
|
|
, m_window(window)
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2018-01-31 10:54:35 +01:00
|
|
|
QModelIndex TabModel::tabIndex(WebTab *tab) const
|
2018-01-30 14:53:18 +01:00
|
|
|
{
|
2018-01-31 10:54:35 +01:00
|
|
|
const int idx = m_tabs.indexOf(tab);
|
|
|
|
if (idx < 0) {
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
return index(idx);
|
|
|
|
}
|
|
|
|
|
|
|
|
WebTab *TabModel::tab(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
return m_tabs.value(index.row());
|
2018-01-30 14:53:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int TabModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
if (parent.isValid()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-01-31 10:54:35 +01:00
|
|
|
return m_tabs.count();
|
2018-01-30 14:53:18 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 18:55:18 +01:00
|
|
|
Qt::ItemFlags TabModel::flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
if (!index.isValid()) {
|
|
|
|
return Qt::ItemIsDropEnabled;
|
|
|
|
}
|
|
|
|
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled;
|
|
|
|
}
|
|
|
|
|
2018-01-30 14:53:18 +01:00
|
|
|
QVariant TabModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2018-01-31 10:54:35 +01:00
|
|
|
if (index.row() < 0 || index.row() > m_tabs.count()) {
|
2018-01-30 14:53:18 +01:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2018-01-31 10:54:35 +01:00
|
|
|
WebTab *t = tab(index);
|
|
|
|
if (!t) {
|
2018-01-30 14:53:18 +01:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (role) {
|
|
|
|
case WebTabRole:
|
2018-01-31 10:54:35 +01:00
|
|
|
return QVariant::fromValue(t);
|
2018-01-30 14:53:18 +01:00
|
|
|
|
|
|
|
case TitleRole:
|
|
|
|
case Qt::DisplayRole:
|
2018-01-31 10:54:35 +01:00
|
|
|
return t->title();
|
2018-01-30 14:53:18 +01:00
|
|
|
|
|
|
|
case IconRole:
|
|
|
|
case Qt::DecorationRole:
|
2018-01-31 10:54:35 +01:00
|
|
|
return t->icon();
|
2018-01-30 14:53:18 +01:00
|
|
|
|
|
|
|
case PinnedRole:
|
2018-01-31 10:54:35 +01:00
|
|
|
return t->isPinned();
|
2018-01-30 14:53:18 +01:00
|
|
|
|
|
|
|
case RestoredRole:
|
2018-01-31 10:54:35 +01:00
|
|
|
return t->isRestored();
|
2018-01-30 15:51:24 +01:00
|
|
|
|
2018-01-31 11:45:47 +01:00
|
|
|
case CurrentTabRole:
|
|
|
|
return t->isCurrentTab();
|
|
|
|
|
|
|
|
case LoadingRole:
|
|
|
|
return t->isLoading();
|
|
|
|
|
2018-02-01 15:45:40 +01:00
|
|
|
case AudioPlayingRole:
|
|
|
|
return t->isPlaying();
|
|
|
|
|
|
|
|
case AudioMutedRole:
|
|
|
|
return t->isMuted();
|
|
|
|
|
2018-01-30 14:53:18 +01:00
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 18:55:18 +01:00
|
|
|
Qt::DropActions TabModel::supportedDropActions() const
|
|
|
|
{
|
|
|
|
return Qt::MoveAction;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList TabModel::mimeTypes() const
|
|
|
|
{
|
2018-02-04 14:24:40 +01:00
|
|
|
return {TabModelMimeData::mimeType()};
|
2018-01-30 18:55:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QMimeData *TabModel::mimeData(const QModelIndexList &indexes) const
|
|
|
|
{
|
2018-02-04 14:24:40 +01:00
|
|
|
if (indexes.isEmpty()) {
|
|
|
|
return nullptr;
|
2018-01-30 18:55:18 +01:00
|
|
|
}
|
2018-02-04 14:24:40 +01:00
|
|
|
WebTab *tab = indexes.at(0).data(WebTabRole).value<WebTab*>();
|
|
|
|
if (!tab) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
TabModelMimeData *mimeData = new TabModelMimeData;
|
|
|
|
mimeData->setTab(tab);
|
2018-01-30 18:55:18 +01:00
|
|
|
return mimeData;
|
|
|
|
}
|
|
|
|
|
2018-02-04 14:24:40 +01:00
|
|
|
bool TabModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const
|
2018-01-30 18:55:18 +01:00
|
|
|
{
|
2018-02-04 14:24:40 +01:00
|
|
|
Q_UNUSED(row)
|
|
|
|
if (action != Qt::MoveAction || parent.isValid() || column > 0 || !m_window) {
|
2018-01-30 18:55:18 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-02-04 14:24:40 +01:00
|
|
|
const TabModelMimeData *mimeData = qobject_cast<const TabModelMimeData*>(data);
|
|
|
|
if (!mimeData) {
|
|
|
|
return false;
|
2018-01-30 18:55:18 +01:00
|
|
|
}
|
2018-02-04 14:24:40 +01:00
|
|
|
return mimeData->tab();
|
|
|
|
}
|
2018-01-30 18:55:18 +01:00
|
|
|
|
2018-02-04 14:24:40 +01:00
|
|
|
bool TabModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
|
|
|
|
{
|
|
|
|
if (!canDropMimeData(data, action, row, column, parent)) {
|
2018-01-30 18:55:18 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-04 14:24:40 +01:00
|
|
|
const TabModelMimeData *mimeData = static_cast<const TabModelMimeData*>(data);
|
|
|
|
WebTab *tab = mimeData->tab();
|
|
|
|
|
|
|
|
if (tab->browserWindow() == m_window) {
|
|
|
|
if (tab->isPinned()) {
|
|
|
|
if (row < 0) {
|
|
|
|
row = m_window->tabWidget()->pinnedTabsCount();
|
|
|
|
}
|
|
|
|
if (row > m_window->tabWidget()->pinnedTabsCount()) {
|
|
|
|
tab->togglePinned();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (row < 0) {
|
|
|
|
row = m_window->tabWidget()->count();
|
|
|
|
}
|
|
|
|
if (row < m_window->tabWidget()->pinnedTabsCount()) {
|
|
|
|
tab->togglePinned();
|
|
|
|
row++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tab->moveTab(row > mimeData->tab()->tabIndex() ? row - 1 : row);
|
|
|
|
} else {
|
|
|
|
if (row < 0) {
|
|
|
|
row = m_window->tabCount();
|
|
|
|
}
|
|
|
|
if (tab->browserWindow()) {
|
|
|
|
tab->browserWindow()->tabWidget()->detachTab(tab);
|
|
|
|
}
|
|
|
|
tab->setPinned(row < m_window->tabWidget()->pinnedTabsCount());
|
|
|
|
m_window->tabWidget()->insertView(row, tab, Qz::NT_SelectedTab);
|
2018-01-30 18:55:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-30 14:53:18 +01:00
|
|
|
void TabModel::init()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < m_window->tabCount(); ++i) {
|
|
|
|
tabInserted(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
connect(m_window->tabWidget(), &TabWidget::tabInserted, this, &TabModel::tabInserted);
|
|
|
|
connect(m_window->tabWidget(), &TabWidget::tabRemoved, this, &TabModel::tabRemoved);
|
|
|
|
connect(m_window->tabWidget(), &TabWidget::tabMoved, this, &TabModel::tabMoved);
|
2018-01-30 15:51:24 +01:00
|
|
|
|
|
|
|
connect(m_window, &QObject::destroyed, this, [this]() {
|
|
|
|
beginResetModel();
|
|
|
|
m_window = nullptr;
|
2018-01-31 10:54:35 +01:00
|
|
|
m_tabs.clear();
|
2018-01-30 15:51:24 +01:00
|
|
|
endResetModel();
|
|
|
|
});
|
2018-01-30 14:53:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TabModel::tabInserted(int index)
|
|
|
|
{
|
|
|
|
WebTab *tab = m_window->weView(index)->webTab();
|
|
|
|
|
|
|
|
beginInsertRows(QModelIndex(), index, index);
|
|
|
|
m_tabs.insert(index, tab);
|
|
|
|
endInsertRows();
|
|
|
|
|
|
|
|
auto emitDataChanged = [this](WebTab *tab, int role) {
|
2018-01-31 10:54:35 +01:00
|
|
|
const QModelIndex idx = tabIndex(tab);
|
2018-01-30 14:53:18 +01:00
|
|
|
emit dataChanged(idx, idx, {role});
|
|
|
|
};
|
|
|
|
|
|
|
|
connect(tab, &WebTab::titleChanged, this, std::bind(emitDataChanged, tab, Qt::DisplayRole));
|
|
|
|
connect(tab, &WebTab::titleChanged, this, std::bind(emitDataChanged, tab, TitleRole));
|
|
|
|
connect(tab, &WebTab::iconChanged, this, std::bind(emitDataChanged, tab, Qt::DecorationRole));
|
|
|
|
connect(tab, &WebTab::iconChanged, this, std::bind(emitDataChanged, tab, IconRole));
|
|
|
|
connect(tab, &WebTab::pinnedChanged, this, std::bind(emitDataChanged, tab, PinnedRole));
|
|
|
|
connect(tab, &WebTab::restoredChanged, this, std::bind(emitDataChanged, tab, RestoredRole));
|
2018-01-31 11:45:47 +01:00
|
|
|
connect(tab, &WebTab::currentTabChanged, this, std::bind(emitDataChanged, tab, CurrentTabRole));
|
|
|
|
connect(tab, &WebTab::loadingChanged, this, std::bind(emitDataChanged, tab, LoadingRole));
|
2018-02-01 15:45:40 +01:00
|
|
|
connect(tab, &WebTab::playingChanged, this, std::bind(emitDataChanged, tab, AudioPlayingRole));
|
|
|
|
connect(tab, &WebTab::mutedChanged, this, std::bind(emitDataChanged, tab, AudioMutedRole));
|
2018-01-30 14:53:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void TabModel::tabRemoved(int index)
|
|
|
|
{
|
|
|
|
beginRemoveRows(QModelIndex(), index, index);
|
|
|
|
m_tabs.remove(index);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TabModel::tabMoved(int from, int to)
|
|
|
|
{
|
|
|
|
beginMoveRows(QModelIndex(), from, from, QModelIndex(), to > from ? to + 1 : to);
|
|
|
|
m_tabs.insert(to, m_tabs.takeAt(from));
|
|
|
|
endMoveRows();
|
|
|
|
}
|