/*******************************************************************************
**
** 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 .
**
******************************************************************************/
#include "mainwindowtabbar.h"
#include
#include
#include
#include
#include
#include "mainwindow.h"
MainWindowTabBar::MainWindowTabBar(const std::shared_ptr &config, MainWindow *parent) :
QTabBar(parent)
{
Q_CHECK_PTR(parent);
m_parent = parent;
setElideMode(Qt::ElideRight);
setTabsClosable(true);
setMovable(true);
setContextMenuPolicy(Qt::DefaultContextMenu);
connect(this, &MainWindowTabBar::tabCloseRequested, this, &MainWindowTabBar::removeTab);
connect(this, &MainWindowTabBar::currentChanged, this, &MainWindowTabBar::handleCurrentChanged);
connect(this, &MainWindowTabBar::tabMoved, this, &MainWindowTabBar::updateVectorArrangement);
QShortcut *tabCloseShortcut = new QShortcut(parent);
tabCloseShortcut->setKey(QKeySequence(QString::fromStdString(config->value("browser.shortcuts.tabClose").value())));
connect(tabCloseShortcut, &QShortcut::activated, [this]() {
this->removeTab(currentIndex());
});
QShortcut *tabLeftShortcut = new QShortcut(parent);
tabLeftShortcut->setKey(QKeySequence(QString::fromStdString(config->value("browser.shortcuts.tabLeft").value())));
connect(tabLeftShortcut, &QShortcut::activated, [this]() {
this->setCurrentIndex(currentIndex()-1);
});
QShortcut *tabRightShortcut = new QShortcut(parent);
tabRightShortcut->setKey(QKeySequence(QString::fromStdString(config->value("browser.shortcuts.tabRight").value())));
connect(tabRightShortcut, &QShortcut::activated, [this]() {
this->setCurrentIndex(currentIndex()+1);
});
}
MainWindowTabBar::~MainWindowTabBar()
{
// cleanup
qDeleteAll(m_views);
m_views.clear();
}
int MainWindowTabBar::addTab(WebView *view)
{
m_views.append(view);
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 MainWindowTabBar::setProfile(WebEngineProfile *profile)
{
Q_CHECK_PTR(profile);
for(auto view : qAsConst(m_views)) {
QWebEnginePage *page = new QWebEnginePage(profile);
page->load(view->url());
view->setPage(page);
}
}
WebView *MainWindowTabBar::currentView()
{
return m_views.at(currentIndex());
}
void MainWindowTabBar::contextMenuEvent(QContextMenuEvent *event)
{
int tabIndex = tabAt(event->pos());
if(tabIndex < 0) {
return;
}
QMenu menu(this);
QAction* closeAction = menu.addAction(tr("Close tab"));
connect(closeAction, &QAction::triggered, this, [tabIndex, this]() {
removeTab(tabIndex);
});
menu.exec(event->globalPos());
}
QSize MainWindowTabBar::tabSizeHint(int index) const
{
Q_UNUSED(index)
return QSize(200, this->height());
}
void MainWindowTabBar::handleCurrentChanged(int index)
{
if(index < 0) {
addTab(createWebView(m_parent->profile()->homepage(), m_parent->profile()));
return;
}
emit currentTabChanged(m_views.at(index));
}
void MainWindowTabBar::removeTab(int index)
{
// remove the tab data from the index
m_views.at(index)->deleteLater();
m_views.remove(index);
// remove the tab from the QTabBar
// this emits the currentTabChanged signal, so it should be done after the view is removed from the index
QTabBar::removeTab(index);
}
void MainWindowTabBar::updateTabText(WebView *view, const QString &text)
{
int index = m_views.indexOf(view);
setTabText(index, text);
}
void MainWindowTabBar::updateVectorArrangement(int from, int to)
{
m_views.move(from, to);
}
WebView *createWebView(const QUrl &url, WebEngineProfile *profile)
{
WebView *view = new WebView(nullptr);
QWebEnginePage *page = new QWebEnginePage(profile);
view->setPage(page);
page->load(url);
return view;
}