[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [falkon] /: WebTab: Add addChildTab method
From:       David Rosca <null () kde ! org>
Date:       2018-01-31 20:10:36
Message-ID: E1egyiK-0002Fo-NC () code ! kde ! org
[Download RAW message or body]

Git commit 67973d704d4b13505cde4b60b375353786aabd33 by David Rosca.
Committed on 31/01/2018 at 19:58.
Pushed by drosca into branch 'master'.

WebTab: Add addChildTab method

Also fix detaching from parent/child tabs and add autotest.

M  +1    -0    autotests/CMakeLists.txt
A  +75   -0    autotests/webtabtest.cpp     [License: GPL (v3+)]
A  +31   -0    autotests/webtabtest.h     [License: GPL (v3+)]
M  +35   -11   src/lib/webtab/webtab.cpp
M  +3    -2    src/lib/webtab/webtab.h

https://commits.kde.org/falkon/67973d704d4b13505cde4b60b375353786aabd33

diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt
index a2198ea6..3378153d 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 00000000..0f18fd19
--- /dev/null
+++ b/autotests/webtabtest.cpp
@@ -0,0 +1,75 @@
+/* ============================================================
+* 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 "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<WebTab*>{&tab2});
+    QCOMPARE(tab2.parentTab(), &tab1);
+    QCOMPARE(tab2.childTabs(), QVector<WebTab*>{});
+
+    tab1.addChildTab(&tab3);
+    QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab2, &tab3}));
+    QCOMPARE(tab3.parentTab(), &tab1);
+    QCOMPARE(tab3.childTabs(), QVector<WebTab*>{});
+
+    tab1.addChildTab(&tab4, 1);
+    QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab2, &tab4, &tab3}));
+    QCOMPARE(tab4.parentTab(), &tab1);
+    QCOMPARE(tab4.childTabs(), QVector<WebTab*>{});
+
+    tab4.addChildTab(&tab5);
+    tab4.addChildTab(&tab6);
+
+    tab4.attach(mApp->getWindow());
+    tab4.detach();
+
+    QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab2, &tab5, &tab6, &tab3}));
+    QCOMPARE(tab4.parentTab(), nullptr);
+    QCOMPARE(tab4.childTabs(), QVector<WebTab*>{});
+
+    tab3.addChildTab(&tab4);
+    tab3.setParentTab(nullptr);
+    tab1.addChildTab(&tab3, 0);
+
+    QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab3, &tab2, &tab5, &tab6}));
+    QCOMPARE(tab3.parentTab(), &tab1);
+    QCOMPARE(tab3.childTabs(), QVector<WebTab*>{&tab4});
+    QCOMPARE(tab4.parentTab(), &tab3);
+}
+
+FALKONTEST_MAIN(WebTabTest)
diff --git a/autotests/webtabtest.h b/autotests/webtabtest.h
new file mode 100644
index 00000000..5568f7b6
--- /dev/null
+++ b/autotests/webtabtest.h
@@ -0,0 +1,31 @@
+/* ============================================================
+* 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/>.
+* ============================================================ */
+#pragma once
+
+#include <QObject>
+
+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 40ecd778..8df4203b 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*> WebTab::childTabs() const
 {
     return m_childTabs;
diff --git a/src/lib/webtab/webtab.h b/src/lib/webtab/webtab.h
index 17001abd..8c9fd5de 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<WebTab*> 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);

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic