1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

TabModel: Remove ParentTabRole and ChildTabsRole

This commit is contained in:
David Rosca 2018-01-31 10:54:35 +01:00
parent 2b994920ad
commit a8bda61f3e
No known key found for this signature in database
GPG Key ID: EBC3FC294452C6D8
3 changed files with 30 additions and 48 deletions

View File

@ -116,21 +116,6 @@ void TabModelTest::dataTest()
WebTab *tab1 = w->weView(1)->webTab();
QSignalSpy dataChangedSpy(&model, &TabModel::dataChanged);
tab1->setParentTab(tab0);
QCOMPARE(dataChangedSpy.count(), 2);
QCOMPARE(dataChangedSpy.at(0).at(0).value<QModelIndex>(), model.index(0, 0));
QCOMPARE(dataChangedSpy.at(0).at(1).value<QModelIndex>(), model.index(0, 0));
QCOMPARE(dataChangedSpy.at(0).at(2).value<QVector<int>>(), QVector<int>{TabModel::ChildTabsRole});
QCOMPARE(model.index(0, 0).data(TabModel::ChildTabsRole).value<QVector<WebTab*>>(), QVector<WebTab*>{tab1});
QCOMPARE(dataChangedSpy.at(1).at(0).value<QModelIndex>(), model.index(1, 0));
QCOMPARE(dataChangedSpy.at(1).at(1).value<QModelIndex>(), model.index(1, 0));
QCOMPARE(dataChangedSpy.at(1).at(2).value<QVector<int>>(), QVector<int>{TabModel::ParentTabRole});
QCOMPARE(model.index(1, 0).data(TabModel::ParentTabRole).value<WebTab*>(), tab0);
delete w;
}

View File

@ -30,9 +30,18 @@ TabModel::TabModel(BrowserWindow *window, QObject *parent)
init();
}
WebTab *TabModel::webTab(int row) const
QModelIndex TabModel::tabIndex(WebTab *tab) const
{
return m_tabs.value(row);
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());
}
int TabModel::rowCount(const QModelIndex &parent) const
@ -40,7 +49,7 @@ int TabModel::rowCount(const QModelIndex &parent) const
if (parent.isValid()) {
return 0;
}
return m_window ? m_window->tabCount() : 0;
return m_tabs.count();
}
Qt::ItemFlags TabModel::flags(const QModelIndex &index) const
@ -53,38 +62,32 @@ Qt::ItemFlags TabModel::flags(const QModelIndex &index) const
QVariant TabModel::data(const QModelIndex &index, int role) const
{
if (!m_window || index.row() < 0 || index.row() > m_window->tabCount()) {
if (index.row() < 0 || index.row() > m_tabs.count()) {
return QVariant();
}
WebTab *tab = webTab(index.row());
if (!tab) {
WebTab *t = tab(index);
if (!t) {
return QVariant();
}
switch (role) {
case WebTabRole:
return QVariant::fromValue(tab);
return QVariant::fromValue(t);
case TitleRole:
case Qt::DisplayRole:
return tab->title();
return t->title();
case IconRole:
case Qt::DecorationRole:
return tab->icon();
return t->icon();
case PinnedRole:
return tab->isPinned();
return t->isPinned();
case RestoredRole:
return tab->isRestored();
case ParentTabRole:
return QVariant::fromValue(tab->parentTab());
case ChildTabsRole:
return QVariant::fromValue(tab->childTabs());
return t->isRestored();
default:
return QVariant();
@ -134,11 +137,11 @@ bool TabModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int ro
QVector<WebTab*> tabs;
while (!stream.atEnd()) {
int index;
stream >> index;
WebTab *tab = webTab(index);
if (tab) {
tabs.append(tab);
int idx;
stream >> idx;
WebTab *t = tab(index(idx));
if (t) {
tabs.append(t);
}
}
@ -169,9 +172,7 @@ void TabModel::init()
connect(m_window, &QObject::destroyed, this, [this]() {
beginResetModel();
m_window = nullptr;
for (WebTab *tab : qAsConst(m_tabs)) {
tab->disconnect(this);
}
m_tabs.clear();
endResetModel();
});
}
@ -185,7 +186,7 @@ void TabModel::tabInserted(int index)
endInsertRows();
auto emitDataChanged = [this](WebTab *tab, int role) {
const QModelIndex idx = TabModel::index(m_tabs.indexOf(tab), 0);
const QModelIndex idx = tabIndex(tab);
emit dataChanged(idx, idx, {role});
};
@ -195,9 +196,6 @@ void TabModel::tabInserted(int index)
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));
connect(tab, &WebTab::parentTabChanged, this, std::bind(emitDataChanged, tab, ParentTabRole));
connect(tab, &WebTab::childTabAdded, this, std::bind(emitDataChanged, tab, ChildTabsRole));
connect(tab, &WebTab::childTabRemoved, this, std::bind(emitDataChanged, tab, ChildTabsRole));
}
void TabModel::tabRemoved(int index)

View File

@ -34,14 +34,13 @@ public:
TitleRole = Qt::UserRole + 2,
IconRole = Qt::UserRole + 3,
PinnedRole = Qt::UserRole + 4,
RestoredRole = Qt::UserRole + 5,
ParentTabRole = Qt::UserRole + 6,
ChildTabsRole = Qt::UserRole + 7
RestoredRole = Qt::UserRole + 5
};
explicit TabModel(BrowserWindow *window, QObject *parent = nullptr);
WebTab *webTab(int row) const;
QModelIndex tabIndex(WebTab *tab) const;
WebTab *tab(const QModelIndex &index) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
Qt::ItemFlags flags(const QModelIndex &index) const override;