From 67973d704d4b13505cde4b60b375353786aabd33 Mon Sep 17 00:00:00 2001 From: David Rosca Date: Wed, 31 Jan 2018 10:12:09 +0100 Subject: [PATCH] WebTab: Add addChildTab method Also fix detaching from parent/child tabs and add autotest. --- autotests/CMakeLists.txt | 1 + autotests/webtabtest.cpp | 75 +++++++++++++++++++++++++++++++++++++++ autotests/webtabtest.h | 31 ++++++++++++++++ src/lib/webtab/webtab.cpp | 46 ++++++++++++++++++------ src/lib/webtab/webtab.h | 5 +-- 5 files changed, 145 insertions(+), 13 deletions(-) create mode 100644 autotests/webtabtest.cpp create mode 100644 autotests/webtabtest.h diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index a2198ea63..3378153d5 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -21,6 +21,7 @@ falkon_tests( updatertest locationbartest webviewtest + webtabtest ) set(falkon_autotests_SRCS ${CMAKE_SOURCE_DIR}/tests/modeltest/modeltest.cpp) diff --git a/autotests/webtabtest.cpp b/autotests/webtabtest.cpp new file mode 100644 index 000000000..0f18fd191 --- /dev/null +++ b/autotests/webtabtest.cpp @@ -0,0 +1,75 @@ +/* ============================================================ +* Falkon - Qt web browser +* Copyright (C) 2018 David Rosca +* +* 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 . +* ============================================================ */ +#include "webtabtest.h" +#include "autotests.h" +#include "webtab.h" +#include "mainapplication.h" + +void WebTabTest::initTestCase() +{ +} + +void WebTabTest::cleanupTestCase() +{ +} + +void WebTabTest::parentChildTabsTest() +{ + WebTab tab1(mApp->getWindow()); + WebTab tab2(mApp->getWindow()); + WebTab tab3(mApp->getWindow()); + WebTab tab4(mApp->getWindow()); + WebTab tab5(mApp->getWindow()); + WebTab tab6(mApp->getWindow()); + + tab1.addChildTab(&tab2); + QCOMPARE(tab1.childTabs(), QVector{&tab2}); + QCOMPARE(tab2.parentTab(), &tab1); + QCOMPARE(tab2.childTabs(), QVector{}); + + tab1.addChildTab(&tab3); + QCOMPARE(tab1.childTabs(), (QVector{&tab2, &tab3})); + QCOMPARE(tab3.parentTab(), &tab1); + QCOMPARE(tab3.childTabs(), QVector{}); + + tab1.addChildTab(&tab4, 1); + QCOMPARE(tab1.childTabs(), (QVector{&tab2, &tab4, &tab3})); + QCOMPARE(tab4.parentTab(), &tab1); + QCOMPARE(tab4.childTabs(), QVector{}); + + tab4.addChildTab(&tab5); + tab4.addChildTab(&tab6); + + tab4.attach(mApp->getWindow()); + tab4.detach(); + + QCOMPARE(tab1.childTabs(), (QVector{&tab2, &tab5, &tab6, &tab3})); + QCOMPARE(tab4.parentTab(), nullptr); + QCOMPARE(tab4.childTabs(), QVector{}); + + tab3.addChildTab(&tab4); + tab3.setParentTab(nullptr); + tab1.addChildTab(&tab3, 0); + + QCOMPARE(tab1.childTabs(), (QVector{&tab3, &tab2, &tab5, &tab6})); + QCOMPARE(tab3.parentTab(), &tab1); + QCOMPARE(tab3.childTabs(), QVector{&tab4}); + QCOMPARE(tab4.parentTab(), &tab3); +} + +FALKONTEST_MAIN(WebTabTest) diff --git a/autotests/webtabtest.h b/autotests/webtabtest.h new file mode 100644 index 000000000..5568f7b65 --- /dev/null +++ b/autotests/webtabtest.h @@ -0,0 +1,31 @@ +/* ============================================================ +* Falkon - Qt web browser +* Copyright (C) 2018 David Rosca +* +* 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 . +* ============================================================ */ +#pragma once + +#include + +class WebTabTest : public QObject +{ + Q_OBJECT + +private slots: + void initTestCase(); + void cleanupTestCase(); + + void parentChildTabsTest(); +}; diff --git a/src/lib/webtab/webtab.cpp b/src/lib/webtab/webtab.cpp index 40ecd7780..8df4203b8 100644 --- a/src/lib/webtab/webtab.cpp +++ b/src/lib/webtab/webtab.cpp @@ -279,15 +279,17 @@ void WebTab::detach() Q_ASSERT(m_window); Q_ASSERT(m_tabBar); - // Remove parent tab - if (m_parentTab) { - m_parentTab->m_childTabs.removeOne(this); - emit m_parentTab->childTabRemoved(this); - } - // Remove from child tabs - const auto childTabs = m_childTabs; - for (WebTab *child : childTabs) { + // Remove parent tab and reparent children + WebTab *parentTab = m_parentTab; + const int parentIndex = parentTab ? parentTab->m_childTabs.indexOf(this) : -1; + setParentTab(nullptr); + int i = 0; + while (!m_childTabs.isEmpty()) { + WebTab *child = m_childTabs.at(0); child->setParentTab(nullptr); + if (parentTab) { + parentTab->addChildTab(child, parentIndex + i++); + } } // Remove icon from tab @@ -407,20 +409,42 @@ void WebTab::setParentTab(WebTab *tab) } if (m_parentTab) { - m_parentTab->m_childTabs.removeOne(this); - emit m_parentTab->childTabRemoved(this); + const int index = m_parentTab->m_childTabs.indexOf(this); + if (index >= 0) { + m_parentTab->m_childTabs.removeAt(index); + emit m_parentTab->childTabRemoved(this, index); + } } m_parentTab = tab; if (m_parentTab) { m_parentTab->m_childTabs.append(this); - emit m_parentTab->childTabAdded(this); + emit m_parentTab->childTabAdded(this, m_parentTab->m_childTabs.size() - 1); } emit parentTabChanged(m_parentTab); } +void WebTab::addChildTab(WebTab *tab, int index) +{ + if (tab->parentTab()) { + tab->setParentTab(nullptr); + } + + tab->m_parentTab = this; + + if (index < 0 || index > m_childTabs.size()) { + m_childTabs.append(tab); + emit childTabAdded(tab, m_childTabs.size() - 1); + } else { + m_childTabs.insert(index, tab); + emit childTabAdded(tab, index); + } + + emit tab->parentTabChanged(this); +} + QVector WebTab::childTabs() const { return m_childTabs; diff --git a/src/lib/webtab/webtab.h b/src/lib/webtab/webtab.h index 17001abde..8c9fd5dec 100644 --- a/src/lib/webtab/webtab.h +++ b/src/lib/webtab/webtab.h @@ -66,6 +66,7 @@ public: WebTab *parentTab() const; void setParentTab(WebTab *tab); + void addChildTab(WebTab *tab, int index = -1); QVector childTabs() const; @@ -122,8 +123,8 @@ signals: void pinnedChanged(bool pinned); void restoredChanged(bool restored); void parentTabChanged(WebTab *tab); - void childTabAdded(WebTab *tab); - void childTabRemoved(WebTab *tab); + void childTabAdded(WebTab *tab, int index); + void childTabRemoved(WebTab *tab, int index); private: void titleWasChanged(const QString &title);