1
mirror of https://invent.kde.org/network/falkon.git synced 2024-09-21 09:42: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()
{
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<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)

View File

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

View File

@ -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);

View File

@ -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();