1
mirror of https://invent.kde.org/network/falkon.git synced 2024-09-21 17:52:10 +02:00

WebTab: Add option to prepend child tabs instead of appending

This commit is contained in:
David Rosca 2018-02-03 10:41:43 +01:00
parent 81e86d4a3c
commit e6b220a87a
No known key found for this signature in database
GPG Key ID: EBC3FC294452C6D8
4 changed files with 103 additions and 15 deletions

View File

@ -26,6 +26,12 @@ void WebTabTest::initTestCase()
void WebTabTest::cleanupTestCase() void WebTabTest::cleanupTestCase()
{ {
WebTab::setAddChildBehavior(WebTab::AppendChild);
}
void WebTabTest::init()
{
WebTab::setAddChildBehavior(WebTab::AppendChild);
} }
void WebTabTest::parentChildTabsTest() void WebTabTest::parentChildTabsTest()
@ -78,4 +84,58 @@ void WebTabTest::parentChildTabsTest()
QTest::qWait(10); 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<WebTab*>{&tab2});
QCOMPARE(tab2.parentTab(), &tab1);
QCOMPARE(tab2.childTabs(), QVector<WebTab*>{});
tab1.addChildTab(&tab3);
QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab3, &tab2}));
QCOMPARE(tab3.parentTab(), &tab1);
QCOMPARE(tab3.childTabs(), QVector<WebTab*>{});
tab1.addChildTab(&tab4, 1);
QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab3, &tab4, &tab2}));
QCOMPARE(tab4.parentTab(), &tab1);
QCOMPARE(tab4.childTabs(), QVector<WebTab*>{});
tab4.addChildTab(&tab5);
tab4.addChildTab(&tab6);
QCOMPARE(tab4.childTabs(), (QVector<WebTab*>{&tab6, &tab5}));
tab4.attach(mApp->getWindow());
tab4.detach();
QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab3, &tab6, &tab5, &tab2}));
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, &tab6, &tab5, &tab2}));
QCOMPARE(tab3.parentTab(), &tab1);
QCOMPARE(tab3.childTabs(), QVector<WebTab*>{&tab4});
QCOMPARE(tab4.parentTab(), &tab3);
tab3.addChildTab(&tab2);
QCOMPARE(tab3.childTabs(), (QVector<WebTab*>{&tab2, &tab4}));
QCOMPARE(tab1.childTabs(), (QVector<WebTab*>{&tab3, &tab6, &tab5}));
QTest::qWait(10);
}
FALKONTEST_MAIN(WebTabTest) FALKONTEST_MAIN(WebTabTest)

View File

@ -26,6 +26,8 @@ class WebTabTest : public QObject
private slots: private slots:
void initTestCase(); void initTestCase();
void cleanupTestCase(); void cleanupTestCase();
void init();
void parentChildTabsTest(); void parentChildTabsTest();
void prependChildTabsTest();
}; };

View File

@ -454,12 +454,12 @@ void WebTab::setParentTab(WebTab *tab)
m_parentTab = tab; m_parentTab = tab;
if (m_parentTab) { if (tab) {
m_parentTab->m_childTabs.append(this); m_parentTab = nullptr;
emit m_parentTab->childTabAdded(this, m_parentTab->m_childTabs.size() - 1); tab->addChildTab(this);
} else {
emit parentTabChanged(m_parentTab);
} }
emit parentTabChanged(m_parentTab);
} }
void WebTab::addChildTab(WebTab *tab, int index) void WebTab::addChildTab(WebTab *tab, int index)
@ -468,24 +468,28 @@ void WebTab::addChildTab(WebTab *tab, int index)
return; return;
} }
WebTab *tabParent = tab->m_parentTab; WebTab *oldParent = tab->m_parentTab;
tab->m_parentTab = this; tab->m_parentTab = this;
if (tabParent) { if (oldParent) {
const int index = tabParent->m_childTabs.indexOf(tab); const int index = oldParent->m_childTabs.indexOf(tab);
if (index >= 0) { if (index >= 0) {
tabParent->m_childTabs.removeAt(index); oldParent->m_childTabs.removeAt(index);
emit tabParent->childTabRemoved(tab, index); emit oldParent->childTabRemoved(tab, index);
} }
} }
if (index < 0 || index > m_childTabs.size()) { if (index < 0 || index > m_childTabs.size()) {
m_childTabs.append(tab); index = 0;
emit childTabAdded(tab, m_childTabs.size() - 1); if (addChildBehavior() == AppendChild) {
} else { index = m_childTabs.size();
m_childTabs.insert(index, tab); } else if (addChildBehavior() == PrependChild) {
emit childTabAdded(tab, index); index = 0;
}
} }
m_childTabs.insert(index, tab);
emit childTabAdded(tab, index);
emit tab->parentTabChanged(this); 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) void WebTab::resizeEvent(QResizeEvent *event)
{ {
QWidget::resizeEvent(event); QWidget::resizeEvent(event);

View File

@ -59,6 +59,11 @@ public:
friend FALKON_EXPORT QDataStream &operator>>(QDataStream &stream, SavedTab &tab); friend FALKON_EXPORT QDataStream &operator>>(QDataStream &stream, SavedTab &tab);
}; };
enum AddChildBehavior {
AppendChild = 0,
PrependChild
};
explicit WebTab(QWidget *parent = nullptr); explicit WebTab(QWidget *parent = nullptr);
TabbedWebView* webView() const; TabbedWebView* webView() const;
@ -117,6 +122,9 @@ public:
void tabActivated(); void tabActivated();
static AddChildBehavior addChildBehavior();
static void setAddChildBehavior(AddChildBehavior behavior);
private slots: private slots:
void showNotification(QWidget* notif); void showNotification(QWidget* notif);
void loadFinished(); void loadFinished();