From e6b220a87a19d54085ba2674cb23d6ff46fff7cd Mon Sep 17 00:00:00 2001 From: David Rosca Date: Sat, 3 Feb 2018 10:41:43 +0100 Subject: [PATCH] WebTab: Add option to prepend child tabs instead of appending --- autotests/webtabtest.cpp | 60 +++++++++++++++++++++++++++++++++++++++ autotests/webtabtest.h | 2 ++ src/lib/webtab/webtab.cpp | 48 +++++++++++++++++++++---------- src/lib/webtab/webtab.h | 8 ++++++ 4 files changed, 103 insertions(+), 15 deletions(-) diff --git a/autotests/webtabtest.cpp b/autotests/webtabtest.cpp index b5a55de76..64c143922 100644 --- a/autotests/webtabtest.cpp +++ b/autotests/webtabtest.cpp @@ -26,6 +26,12 @@ void WebTabTest::initTestCase() void WebTabTest::cleanupTestCase() { + WebTab::setAddChildBehavior(WebTab::AppendChild); +} + +void WebTabTest::init() +{ + WebTab::setAddChildBehavior(WebTab::AppendChild); } void WebTabTest::parentChildTabsTest() @@ -78,4 +84,58 @@ void WebTabTest::parentChildTabsTest() QTest::qWait(10); } +void WebTabTest::prependChildTabsTest() +{ + WebTab::setAddChildBehavior(WebTab::PrependChild); + + WebTab tab1; + WebTab tab2; + WebTab tab3; + WebTab tab4; + WebTab tab5; + WebTab tab6; + + tab1.addChildTab(&tab2); + QCOMPARE(tab1.childTabs(), QVector{&tab2}); + QCOMPARE(tab2.parentTab(), &tab1); + QCOMPARE(tab2.childTabs(), QVector{}); + + tab1.addChildTab(&tab3); + QCOMPARE(tab1.childTabs(), (QVector{&tab3, &tab2})); + QCOMPARE(tab3.parentTab(), &tab1); + QCOMPARE(tab3.childTabs(), QVector{}); + + tab1.addChildTab(&tab4, 1); + QCOMPARE(tab1.childTabs(), (QVector{&tab3, &tab4, &tab2})); + QCOMPARE(tab4.parentTab(), &tab1); + QCOMPARE(tab4.childTabs(), QVector{}); + + tab4.addChildTab(&tab5); + tab4.addChildTab(&tab6); + + QCOMPARE(tab4.childTabs(), (QVector{&tab6, &tab5})); + + tab4.attach(mApp->getWindow()); + tab4.detach(); + + QCOMPARE(tab1.childTabs(), (QVector{&tab3, &tab6, &tab5, &tab2})); + QCOMPARE(tab4.parentTab(), nullptr); + QCOMPARE(tab4.childTabs(), QVector{}); + + tab3.addChildTab(&tab4); + tab3.setParentTab(nullptr); + tab1.addChildTab(&tab3, 0); + + QCOMPARE(tab1.childTabs(), (QVector{&tab3, &tab6, &tab5, &tab2})); + QCOMPARE(tab3.parentTab(), &tab1); + QCOMPARE(tab3.childTabs(), QVector{&tab4}); + QCOMPARE(tab4.parentTab(), &tab3); + + tab3.addChildTab(&tab2); + QCOMPARE(tab3.childTabs(), (QVector{&tab2, &tab4})); + QCOMPARE(tab1.childTabs(), (QVector{&tab3, &tab6, &tab5})); + + QTest::qWait(10); +} + FALKONTEST_MAIN(WebTabTest) diff --git a/autotests/webtabtest.h b/autotests/webtabtest.h index 5568f7b65..6b28eafa1 100644 --- a/autotests/webtabtest.h +++ b/autotests/webtabtest.h @@ -26,6 +26,8 @@ class WebTabTest : public QObject private slots: void initTestCase(); void cleanupTestCase(); + void init(); void parentChildTabsTest(); + void prependChildTabsTest(); }; diff --git a/src/lib/webtab/webtab.cpp b/src/lib/webtab/webtab.cpp index 42a54eab7..921ba2c37 100644 --- a/src/lib/webtab/webtab.cpp +++ b/src/lib/webtab/webtab.cpp @@ -454,12 +454,12 @@ void WebTab::setParentTab(WebTab *tab) m_parentTab = tab; - if (m_parentTab) { - m_parentTab->m_childTabs.append(this); - emit m_parentTab->childTabAdded(this, m_parentTab->m_childTabs.size() - 1); + if (tab) { + m_parentTab = nullptr; + tab->addChildTab(this); + } else { + emit parentTabChanged(m_parentTab); } - - emit parentTabChanged(m_parentTab); } void WebTab::addChildTab(WebTab *tab, int index) @@ -468,24 +468,28 @@ void WebTab::addChildTab(WebTab *tab, int index) return; } - WebTab *tabParent = tab->m_parentTab; + WebTab *oldParent = tab->m_parentTab; tab->m_parentTab = this; - if (tabParent) { - const int index = tabParent->m_childTabs.indexOf(tab); + if (oldParent) { + const int index = oldParent->m_childTabs.indexOf(tab); if (index >= 0) { - tabParent->m_childTabs.removeAt(index); - emit tabParent->childTabRemoved(tab, index); + oldParent->m_childTabs.removeAt(index); + emit oldParent->childTabRemoved(tab, index); } } 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); + index = 0; + if (addChildBehavior() == AppendChild) { + index = m_childTabs.size(); + } else if (addChildBehavior() == PrependChild) { + index = 0; + } } + m_childTabs.insert(index, tab); + emit childTabAdded(tab, index); + emit tab->parentTabChanged(this); } @@ -591,6 +595,20 @@ void WebTab::tabActivated() }); } +static WebTab::AddChildBehavior s_addChildBehavior = WebTab::AppendChild; + +// static +WebTab::AddChildBehavior WebTab::addChildBehavior() +{ + return s_addChildBehavior; +} + +// static +void WebTab::setAddChildBehavior(AddChildBehavior behavior) +{ + s_addChildBehavior = behavior; +} + void WebTab::resizeEvent(QResizeEvent *event) { QWidget::resizeEvent(event); diff --git a/src/lib/webtab/webtab.h b/src/lib/webtab/webtab.h index db0428cdf..7bf657e98 100644 --- a/src/lib/webtab/webtab.h +++ b/src/lib/webtab/webtab.h @@ -59,6 +59,11 @@ public: friend FALKON_EXPORT QDataStream &operator>>(QDataStream &stream, SavedTab &tab); }; + enum AddChildBehavior { + AppendChild = 0, + PrependChild + }; + explicit WebTab(QWidget *parent = nullptr); TabbedWebView* webView() const; @@ -117,6 +122,9 @@ public: void tabActivated(); + static AddChildBehavior addChildBehavior(); + static void setAddChildBehavior(AddChildBehavior behavior); + private slots: void showNotification(QWidget* notif); void loadFinished();