aboutsummaryrefslogtreecommitdiff
path: root/src/widgets/webviewtabbar.cpp
blob: 59e5c69f0a00f36da8915210f595e5df6e6f2c73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "webviewtabbar.h"

WebViewTabBar::WebViewTabBar(QWidget *parent) :
    QTabBar(parent)
{
    setTabsClosable(true);
    connect(this, SIGNAL(tabCloseRequested(int)), this, SLOT(handleTabClose(int)));
    connect(this, SIGNAL(currentChanged(int)), this, SLOT(handleCurrentChanged(int)));
}

WebViewTabBar::~WebViewTabBar()
{
    // cleanup
    qDeleteAll(m_views);
    m_views.clear();
}

int WebViewTabBar::addTab(QWebEngineView *view)
{
    m_views.append(view);
    //connect(view, SIGNAL(titleChanged()), this, SLOT(updateTabText()));
    connect(view, &QWebEngineView::titleChanged, [this, view](const QString &title) {
       int index = m_views.indexOf(view);
       setTabText(index, title);
    });
    return QTabBar::addTab("New Tab");
}

QWebEngineView *WebViewTabBar::currentView()
{
    return m_views.at(currentIndex());
}

void WebViewTabBar::handleCurrentChanged(int index)
{
    emit(currentTabChanged(m_views.at(index)));
}

void WebViewTabBar::handleTabClose(int index)
{
    removeTab(index);
    m_views.at(index)->deleteLater();
    m_views.remove(index);
}

void WebViewTabBar::updateTabText(QWebEngineView *view, const QString &text)
{
    int index = m_views.indexOf(view);
    setTabText(index, text);
}