aboutsummaryrefslogtreecommitdiff
path: root/src/widgets/webviewtabbar.cpp
blob: d3747ccbaa4b0872877df5351d2a5331a6208d59 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/** LICENSE ********************************************************************
 ** 
 ** smolbote: yet another qute browser
 ** Copyright (C) 2017  Xian Nox
 ** 
 ** 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 "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(QWebEngineProfile *profile, const QUrl &url)
{
    QWebEngineView *view = new QWebEngineView(0);
    QWebEnginePage *page = new QWebEnginePage(profile);
    view->setPage(page);
    page->load(url);
    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);
    });
    connect(view, &QWebEngineView::iconChanged, [this, view](const QIcon &icon) {
        int index = m_views.indexOf(view);
        setTabIcon(index, icon);
    });

    return QTabBar::addTab("New Tab");
}

void WebViewTabBar::setProfile(QWebEngineProfile *profile)
{
    for(QWebEngineView *view : m_views) {
        QWebEnginePage *page = new QWebEnginePage(profile);
        page->load(view->url());
        view->setPage(page);
    }
}

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