From a6e6823da845ca14d37d8914c80185257e8c0e62 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 2 Apr 2018 15:47:01 +0200 Subject: Refactoring TabBar - cleaned up code - moved to mainwindow/widgets - add Close tabs left/right to context menu --- lib/configuration/configuration.cpp | 12 +-- src/CMakeLists.txt | 13 ++- src/browser.cpp | 5 + src/browser.h | 18 ++-- src/main.cpp | 2 +- src/mainwindow/mainwindow.cpp | 8 +- src/mainwindow/mainwindow.h | 6 +- src/mainwindow/widgets/navigationbar.cpp | 2 + src/mainwindow/widgets/searchform.cpp | 2 + src/mainwindow/widgets/tabbar.cpp | 171 +++++++++++++++++++++++++++++++ src/mainwindow/widgets/tabbar.h | 56 ++++++++++ src/webengine/webengineprofile.cpp | 16 +-- src/webengine/webengineprofile.h | 4 +- src/webengine/webview.cpp | 11 ++ src/webengine/webview.h | 3 + src/widgets/mainwindowmenubar.cpp | 8 +- src/widgets/mainwindowtabbar.cpp | 150 --------------------------- src/widgets/mainwindowtabbar.h | 55 ---------- 18 files changed, 293 insertions(+), 249 deletions(-) create mode 100644 src/mainwindow/widgets/tabbar.cpp create mode 100644 src/mainwindow/widgets/tabbar.h delete mode 100644 src/widgets/mainwindowtabbar.cpp delete mode 100644 src/widgets/mainwindowtabbar.h diff --git a/lib/configuration/configuration.cpp b/lib/configuration/configuration.cpp index fa01951..959dfc2 100644 --- a/lib/configuration/configuration.cpp +++ b/lib/configuration/configuration.cpp @@ -38,7 +38,6 @@ Configuration::Configuration() // browser menu ("browser.shortcuts.newWindow", po::value()->default_value("Ctrl+N")) - ("browser.shortcuts.newTab", po::value()->default_value("Ctrl+T")) ("browser.shortcuts.about", po::value()->default_value("F1")) ("browser.shortcuts.quit", po::value()->default_value("Ctrl+Q")) @@ -54,10 +53,11 @@ Configuration::Configuration() ("addressbar.shortcuts.pageMenu", po::value()->default_value("F2")) ("addressbar.shortcuts.toolsMenu", po::value()->default_value("F10")) - // tabs - ("browser.shortcuts.tabClose", po::value()->default_value("Ctrl+X")) - ("browser.shortcuts.tabLeft", po::value()->default_value("Ctrl+O")) - ("browser.shortcuts.tabRight", po::value()->default_value("Ctrl+P")) + // tab bar + ("tabbar.shortcuts.new", po::value()->default_value("Ctrl+T")) + ("tabbar.shortcuts.close", po::value()->default_value("Ctrl+X")) + ("tabbar.shortcuts.left", po::value()->default_value("Ctrl+O")) + ("tabbar.shortcuts.right", po::value()->default_value("Ctrl+P")) // page ("browser.shortcuts.toggleSearchBox", po::value()->default_value("F3")) @@ -131,7 +131,7 @@ bool Configuration::parse(int argc, const char **argv) QHash Configuration::section(const std::string &prefix) const { QHash v; - for(auto s : desc.options()) { + for(auto &s : desc.options()) { if(boost::starts_with(s->long_name(), prefix)) { v[s->long_name().c_str()] = QString::fromStdString(value(s->long_name().c_str()).value()); } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c3b96e2..6f231b4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -21,6 +21,8 @@ add_executable(poi singleapplication.h browser.cpp browser.h + commandline.cpp + commandline.h ../data/resources.qrc # main window @@ -34,6 +36,8 @@ add_executable(poi mainwindow/widgets/searchform.cpp mainwindow/widgets/searchform.h mainwindow/widgets/searchform.ui + mainwindow/widgets/tabbar.cpp + mainwindow/widgets/tabbar.h # address bar addressbar/completer.cpp @@ -44,8 +48,6 @@ add_executable(poi # todo: move all to mainwindow widgets/mainwindowmenubar.cpp widgets/mainwindowmenubar.h - widgets/mainwindowtabbar.cpp - widgets/mainwindowtabbar.h # webengine webengine/cookiefilter.cpp @@ -58,6 +60,10 @@ add_executable(poi webengine/webpage.h webengine/webview.cpp webengine/webview.h + webengine/widgets/pagetoolsmenu.cpp + webengine/widgets/pagetoolsmenu.h + webengine/widgets/pagemenu.cpp + webengine/widgets/pagemenu.h # forms forms/aboutdialog.cpp @@ -69,7 +75,8 @@ add_executable(poi #forms/cookiesform.ui # plugin interfaces - ../plugins/interfaces.h commandline.cpp commandline.h webengine/widgets/pagetoolsmenu.cpp webengine/widgets/pagetoolsmenu.h webengine/widgets/pagemenu.cpp webengine/widgets/pagemenu.h) + ../plugins/interfaces.h +) target_include_directories(poi PRIVATE ../lib ../plugins diff --git a/src/browser.cpp b/src/browser.cpp index d1e763f..e462280 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -7,8 +7,11 @@ */ #include "browser.h" +#include "configuration/configuration.h" #include "mainwindow/mainwindow.h" #include "webengine/urlinterceptor.h" +#include "webengine/webengineprofile.h" +#include #include #include #include @@ -141,9 +144,11 @@ std::shared_ptr Browser::profile(const QString &storageName) if(storageName.isEmpty()) { // create off-the-record profile _profile = std::make_shared(nullptr); + _profile->loadProfile(m_config->section("profile"), path + "/otr.ini"); } else { // regular profile _profile = std::make_shared(storageName, nullptr); + _profile->loadProfile(m_config->section("profile"), path + "/" + storageName + "/profile.ini"); _profile->setPersistentStoragePath(path + "/storage"); _profile->setCachePath(path + "/cache"); } diff --git a/src/browser.h b/src/browser.h index 497050d..1f86327 100644 --- a/src/browser.h +++ b/src/browser.h @@ -9,18 +9,18 @@ #ifndef BROWSER_H #define BROWSER_H -#include "configuration.h" #include "singleapplication.h" -#include "webengine/webengineprofile.h" +#include #include #include -#include "webengine/cookiefilter.h" -#include "../plugins/interfaces.h" +class Configuration; class MainWindow; class BookmarksWidget; class DownloadsWidget; class UrlRequestInterceptor; +class WebEngineProfile; +class CookieFilter; class Browser : public SingleApplication { Q_OBJECT @@ -39,11 +39,13 @@ public: std::shared_ptr profile(const QString &storageName); - const QList profiles() const { + const QList profiles() const + { return m_profiles.keys(); } - const QVector plugins() const { + const QVector plugins() const + { return m_plugins; } @@ -60,8 +62,8 @@ private: QHash> m_profiles; std::shared_ptr m_defaultProfile; - UrlRequestInterceptor* m_urlRequestInterceptor = nullptr; - CookieFilter* m_cookieInterceptor = nullptr; + UrlRequestInterceptor *m_urlRequestInterceptor = nullptr; + CookieFilter *m_cookieInterceptor = nullptr; std::shared_ptr m_bookmarksManager; std::shared_ptr m_downloadManager; }; diff --git a/src/main.cpp b/src/main.cpp index 8f0d26d..75691f9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,7 +12,7 @@ #include "version.h" #include #include - +#include "configuration/configuration.h" // startup time measuring #ifdef QT_DEBUG #include diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp index db059a2..99a80b8 100644 --- a/src/mainwindow/mainwindow.cpp +++ b/src/mainwindow/mainwindow.cpp @@ -8,10 +8,12 @@ #include "mainwindow.h" #include "addressbar/urllineedit.h" +#include "configuration/configuration.h" #include "forms/aboutdialog.h" #include "mainwindow/widgets/searchform.h" #include "ui_mainwindow.h" #include "widgets/mainwindowmenubar.h" +#include "mainwindow/widgets/tabbar.h" #include #include #include @@ -21,10 +23,10 @@ MainWindow::MainWindow(std::shared_ptr config, QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) - , tabBar(new MainWindowTabBar(config, this)) - , menuBar(new MainWindowMenuBar(config, this)) + , tabBar(new TabBar(config->section("tabbar"), this)) , m_addressBar(new UrlLineEdit(config->section("addressbar"), this)) , m_progressBar(new LoadingBar(this)) + , menuBar(new MainWindowMenuBar(config, this)) { Q_ASSERT(config); m_config = config; @@ -72,7 +74,7 @@ MainWindow::MainWindow(std::shared_ptr config, QWidget *parent) url.replace("$term", t); tabBar->currentView()->load(QUrl::fromUserInput(url)); }); - connect(tabBar, &MainWindowTabBar::currentTabChanged, this, &MainWindow::handleTabChanged); + connect(tabBar, &TabBar::currentTabChanged, this, &MainWindow::handleTabChanged); // loading bar ui->statusBar->addPermanentWidget(m_progressBar); diff --git a/src/mainwindow/mainwindow.h b/src/mainwindow/mainwindow.h index 84b408b..8b205ab 100644 --- a/src/mainwindow/mainwindow.h +++ b/src/mainwindow/mainwindow.h @@ -13,7 +13,6 @@ #include "webengine/webengineprofile.h" #include "widgets/loadingbar.h" #include "widgets/navigationbar.h" -#include "widgets/mainwindowtabbar.h" #include #include #include @@ -24,12 +23,11 @@ namespace Ui class MainWindow; } +class TabBar; class SearchForm; - class Configuration; class BookmarksWidget; class DownloadsWidget; - class MainWindowMenuBar; class UrlLineEdit; @@ -77,7 +75,7 @@ private: Ui::MainWindow *ui; SearchForm *m_searchBox; - MainWindowTabBar *tabBar; + TabBar *tabBar; WebView *m_currentView; MainWindowMenuBar *menuBar; diff --git a/src/mainwindow/widgets/navigationbar.cpp b/src/mainwindow/widgets/navigationbar.cpp index e488d77..0652159 100644 --- a/src/mainwindow/widgets/navigationbar.cpp +++ b/src/mainwindow/widgets/navigationbar.cpp @@ -15,6 +15,8 @@ #include #include #include +#include "webengine/webview.h" +#include "configuration/configuration.h" NavigationBar::NavigationBar(MainWindow *parent) : QObject(parent) diff --git a/src/mainwindow/widgets/searchform.cpp b/src/mainwindow/widgets/searchform.cpp index 1afebf9..8ac1bc2 100644 --- a/src/mainwindow/widgets/searchform.cpp +++ b/src/mainwindow/widgets/searchform.cpp @@ -10,6 +10,8 @@ #include "mainwindow/mainwindow.h" #include "ui_searchform.h" #include +#include "webengine/webview.h" +#include "configuration/configuration.h" SearchForm::SearchForm(MainWindow *parentWindow, QWidget *parent) : QWidget(parent) diff --git a/src/mainwindow/widgets/tabbar.cpp b/src/mainwindow/widgets/tabbar.cpp new file mode 100644 index 0000000..ef32ef0 --- /dev/null +++ b/src/mainwindow/widgets/tabbar.cpp @@ -0,0 +1,171 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/smolbote.hg + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "tabbar.h" +#include "mainwindow/mainwindow.h" +#include "webengine/webengineprofile.h" +#include "webengine/webview.h" +#include +#include + +TabBar::TabBar(const QHash &config, MainWindow *parent) + : QTabBar(parent) +{ + Q_CHECK_PTR(parent); + + setElideMode(Qt::ElideRight); + setTabsClosable(true); + setMovable(true); + setContextMenuPolicy(Qt::DefaultContextMenu); + + connect(this, &TabBar::tabCloseRequested, this, &TabBar::removeTab); + connect(this, &TabBar::currentChanged, this, &TabBar::handleCurrentChanged); + connect(this, &TabBar::tabMoved, this, &TabBar::updateVectorArrangement); + + newTab_action = new QAction(tr("New Tab"), parent); + newTab_action->setObjectName("newTab_action"); + newTab_action->setShortcut(QKeySequence(config["tabbar.shortcuts.new"])); + connect(newTab_action, &QAction::triggered, parent, [parent, this]() { + parent->newTab(parent->profile()->newtab()); + }); + parent->addAction(newTab_action); + + closeTab_action = new QAction(tr("Close Tab"), parent); + closeTab_action->setObjectName("closeTab_action"); + closeTab_action->setShortcut(QKeySequence(config["tabbar.shortcuts.close"])); + connect(closeTab_action, &QAction::triggered, this, [this]() { + removeTab(currentIndex()); + }); + parent->addAction(closeTab_action); + + leftTab_action = new QAction(tr("Left Tab"), parent); + leftTab_action->setObjectName("leftTab_action"); + leftTab_action->setShortcut(QKeySequence(config["tabbar.shortcuts.left"])); + connect(leftTab_action, &QAction::triggered, this, [this]() { + setCurrentIndex(currentIndex() - 1); + }); + parent->addAction(leftTab_action); + + rightTab_action = new QAction(tr("Right Tab"), parent); + rightTab_action->setObjectName("rightTab_action"); + rightTab_action->setShortcut(QKeySequence(config["tabbar.shortcuts.right"])); + connect(rightTab_action, &QAction::triggered, this, [this]() { + setCurrentIndex(currentIndex() + 1); + }); + parent->addAction(rightTab_action); + + // tab context menu + { + tabContextMenu = new QMenu(this); + + auto *closeTab = tabContextMenu->addAction(tr("Close Tab")); + connect(closeTab, &QAction::triggered, this, [this]() { + removeTab(tabAt(mapFromGlobal(tabContextMenu->pos()))); + }); + + auto *closeTabsLeft = tabContextMenu->addAction(tr("Close Tabs left")); + connect(closeTabsLeft, &QAction::triggered, this, [this]() { + int idx = tabAt(mapFromGlobal(tabContextMenu->pos())); + for(int i = idx - 1; i >= 0; --i) { + removeTab(i); + } + }); + + auto *closeTabsRight = tabContextMenu->addAction(tr("Close Tabs right")); + connect(closeTabsRight, &QAction::triggered, this, [this]() { + int idx = tabAt(mapFromGlobal(tabContextMenu->pos())); + for(int i = count() - 1; i > idx; --i) { + removeTab(i); + } + }); + } +} + +TabBar::~TabBar() +{ + // cleanup + qDeleteAll(m_views); + m_views.clear(); +} + +void TabBar::setProfile(WebEngineProfile *profile) +{ + Q_CHECK_PTR(profile); + + for(auto view : qAsConst(m_views)) { + auto *page = new WebPage(profile); + page->load(view->url()); + view->page()->deleteLater(); + view->setPage(page); + } +} + +WebView *TabBar::currentView() +{ + return m_views.at(currentIndex()); +} + +int TabBar::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); + setTabToolTip(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 TabBar::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 TabBar::contextMenuEvent(QContextMenuEvent *event) +{ + // check if the context menu was called on a tab + int tabIndex = tabAt(event->pos()); + if(tabIndex < 0) { + return; + } + + tabContextMenu->exec(event->globalPos()); +} + +QSize TabBar::tabSizeHint(int index) const +{ + Q_UNUSED(index) + return QSize(200, this->height()); +} + +void TabBar::handleCurrentChanged(int index) +{ + if(index < 0) { + newTab_action->trigger(); + return; + } + + emit currentTabChanged(m_views.at(index)); +} + +void TabBar::updateVectorArrangement(int from, int to) +{ + m_views.move(from, to); +} diff --git a/src/mainwindow/widgets/tabbar.h b/src/mainwindow/widgets/tabbar.h new file mode 100644 index 0000000..f4b7414 --- /dev/null +++ b/src/mainwindow/widgets/tabbar.h @@ -0,0 +1,56 @@ +/* + * This file is part of smolbote. It's copyrighted by the contributors recorded + * in the version control history of the file, available from its original + * location: https://neueland.iserlohn-fortress.net/smolbote.hg + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#ifndef SMOLBOTE_TABBAR_H +#define SMOLBOTE_TABBAR_H + +#include + +class MainWindow; +class WebView; +class WebEngineProfile; +class QMenu; +class TabBar : public QTabBar +{ + Q_OBJECT + +public: + explicit TabBar(const QHash &config, MainWindow *parent = nullptr); + ~TabBar() override; + + void setProfile(WebEngineProfile *profile); + WebView *currentView(); + +signals: + void currentTabChanged(WebView *view); + +public slots: + int addTab(WebView *view); + void removeTab(int index); + +protected: + void contextMenuEvent(QContextMenuEvent *event) override; + QSize tabSizeHint(int index) const override; + +private slots: + void handleCurrentChanged(int index); + void updateVectorArrangement(int from, int to); + +private: + // store all views in a vector since tabs can only store a QVariant, and that can't easily take a pointer + QVector m_views; + + QMenu *tabContextMenu; + + QAction *newTab_action; + QAction *closeTab_action; + QAction *leftTab_action; + QAction *rightTab_action; +}; + +#endif // SMOLBOTE_TABBAR_H diff --git a/src/webengine/webengineprofile.cpp b/src/webengine/webengineprofile.cpp index 263dcb5..55af9f2 100644 --- a/src/webengine/webengineprofile.cpp +++ b/src/webengine/webengineprofile.cpp @@ -51,25 +51,17 @@ QUrl WebEngineProfile::newtab() const return m_newtab; } -void WebEngineProfile::loadProfile(const QString &path) +void WebEngineProfile::loadProfile(QHash conf, const QString &path) { m_configPath = path; - // check if config file exists - if(!QFileInfo::exists(m_configPath)) { -#ifdef QT_DEBUG - qDebug("No config for profile '%s': %s", qUtf8Printable(m_name), qUtf8Printable(m_configPath)); -#endif - return; - } - #ifdef QT_DEBUG qDebug("Reading config for profile '%s': %s", qUtf8Printable(m_name), qUtf8Printable(m_configPath)); #endif QSettings config(m_configPath, QSettings::IniFormat); - m_homepage = config.value("homepage", m_homepage).toUrl(); - m_newtab = config.value("newtab", m_newtab).toUrl(); + m_homepage = config.value("homepage", conf["profile.homepage"]).toUrl(); + m_newtab = config.value("newtab", conf["profile.newtab"]).toUrl(); config.beginGroup("http"); setHttpUserAgent(config.value("userAgent", httpUserAgent()).toString()); @@ -84,7 +76,7 @@ void WebEngineProfile::loadProfile(const QString &path) setHttpCacheType(QWebEngineProfile::NoCache); } } - setHttpCacheMaximumSize(config.value("cacheSize").toInt()); + setHttpCacheMaximumSize(config.value("cacheSize", httpCacheMaximumSize()).toInt()); config.endGroup(); // http config.beginGroup("policy"); diff --git a/src/webengine/webengineprofile.h b/src/webengine/webengineprofile.h index 6a0d4e2..76295d3 100644 --- a/src/webengine/webengineprofile.h +++ b/src/webengine/webengineprofile.h @@ -9,9 +9,9 @@ #ifndef SMOLBOTE_WEBENGINEPROFILE_H #define SMOLBOTE_WEBENGINEPROFILE_H +#include "cookiefilter.h" #include #include -#include "cookiefilter.h" class WebEngineProfile : public QWebEngineProfile { @@ -27,7 +27,7 @@ public: QUrl newtab() const; public slots: - void loadProfile(const QString &path); + void loadProfile(QHash conf, const QString &path); void saveProfile(const QString &path = QString()); private: diff --git a/src/webengine/webview.cpp b/src/webengine/webview.cpp index 7ca3280..520ce50 100644 --- a/src/webengine/webview.cpp +++ b/src/webengine/webview.cpp @@ -13,6 +13,7 @@ #include #include #include +#include "mainwindow/widgets/tabbar.h" WebView::WebView(MainWindow *parentMainWindow, QWidget *parent) : QWebEngineView(parent) @@ -127,3 +128,13 @@ void WebView::triggerViewAction(WebView::ViewAction action) break; } } + +WebView *createWebView(const QUrl &url, WebEngineProfile *profile, MainWindow *parent) +{ + auto *view = new WebView(parent); + auto *page = new WebPage(profile); + view->setPage(page); + page->load(url); + + return view; +} diff --git a/src/webengine/webview.h b/src/webengine/webview.h index 1fab1db..0750aee 100644 --- a/src/webengine/webview.h +++ b/src/webengine/webview.h @@ -14,6 +14,7 @@ class QMenu; class MainWindow; +class WebEngineProfile; class WebView : public QWebEngineView { Q_OBJECT @@ -62,4 +63,6 @@ private: QWebEnginePage *m_devToolsPage; }; +WebView *createWebView(const QUrl &url, WebEngineProfile *profile, MainWindow *parent); + #endif // SMOLBOTE_WEBVIEW_H diff --git a/src/widgets/mainwindowmenubar.cpp b/src/widgets/mainwindowmenubar.cpp index 4aa6cc8..44a6eba 100644 --- a/src/widgets/mainwindowmenubar.cpp +++ b/src/widgets/mainwindowmenubar.cpp @@ -11,6 +11,8 @@ #include "downloads/downloadswidget.h" #include "mainwindow/mainwindow.h" #include +#include +#include "configuration/configuration.h" MainWindowMenuBar::MainWindowMenuBar(std::shared_ptr config, MainWindow *parent) : QMenuBar(parent) @@ -32,11 +34,7 @@ MainWindowMenuBar::MainWindowMenuBar(std::shared_ptr config, Main }); newWindowAction->setShortcut(QKeySequence(config->value("browser.shortcuts.newWindow").value().c_str())); - QAction *newTabAction = browserMenu->addAction(tr("New Tab")); - connect(newTabAction, &QAction::triggered, parent, [parent]() { - parent->newTab(); - }); - newTabAction->setShortcut(QKeySequence(config->value("browser.shortcuts.newTab").value().c_str())); + browserMenu->addAction(parent->findChild("newTab_action")); browserMenu->addSeparator(); //browserMenu->addAction(tr("Settings"), parent, &MainWindow::showSettingsDialog); diff --git a/src/widgets/mainwindowtabbar.cpp b/src/widgets/mainwindowtabbar.cpp deleted file mode 100644 index 114b0e3..0000000 --- a/src/widgets/mainwindowtabbar.cpp +++ /dev/null @@ -1,150 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/smolbote.hg - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "mainwindowtabbar.h" -#include "configuration.h" -#include "mainwindow/mainwindow.h" -#include -#include -#include - -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)) { - WebPage *page = new WebPage(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(), m_parent)); - 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, MainWindow *parent) -{ - WebView *view = new WebView(parent); - WebPage *page = new WebPage(profile); - view->setPage(page); - page->load(url); - - return view; -} diff --git a/src/widgets/mainwindowtabbar.h b/src/widgets/mainwindowtabbar.h deleted file mode 100644 index 823db1b..0000000 --- a/src/widgets/mainwindowtabbar.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/smolbote.hg - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef WEBVIEWTABBAR_H -#define WEBVIEWTABBAR_H - -#include "webengine/webengineprofile.h" -#include "webengine/webview.h" -#include -#include - -class Configuration; -class MainWindow; -class MainWindowTabBar : public QTabBar -{ - Q_OBJECT - -public: - explicit MainWindowTabBar(const std::shared_ptr &config, MainWindow *parent = nullptr); - ~MainWindowTabBar(); - - void setProfile(WebEngineProfile *profile); - WebView *currentView(); - -signals: - void currentTabChanged(WebView *view); - -public slots: - int addTab(WebView *view); - void removeTab(int index); - -protected: - void contextMenuEvent(QContextMenuEvent *event); - QSize tabSizeHint(int index) const; - -private slots: - void handleCurrentChanged(int index); - - void updateTabText(WebView *view, const QString &text); - void updateVectorArrangement(int from, int to); - -private: - // store all views in a vector since tabs can only store a QVariant, and that can't easily take a pointer - QVector m_views; - MainWindow *m_parent; -}; - -WebView *createWebView(const QUrl &url, WebEngineProfile *profile, MainWindow *parent); - -#endif // WEBVIEWTABBAR_H -- cgit v1.2.1