aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-04-02 15:47:01 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2018-04-02 15:47:01 +0200
commita6e6823da845ca14d37d8914c80185257e8c0e62 (patch)
tree1c0126f5a78bc95a6f536d2b091cf762972943c9
parentUpdated documentation (diff)
downloadsmolbote-a6e6823da845ca14d37d8914c80185257e8c0e62.tar.xz
Refactoring TabBar
- cleaned up code - moved to mainwindow/widgets - add Close tabs left/right to context menu
-rw-r--r--lib/configuration/configuration.cpp12
-rw-r--r--src/CMakeLists.txt13
-rw-r--r--src/browser.cpp5
-rw-r--r--src/browser.h18
-rw-r--r--src/main.cpp2
-rw-r--r--src/mainwindow/mainwindow.cpp8
-rw-r--r--src/mainwindow/mainwindow.h6
-rw-r--r--src/mainwindow/widgets/navigationbar.cpp2
-rw-r--r--src/mainwindow/widgets/searchform.cpp2
-rw-r--r--src/mainwindow/widgets/tabbar.cpp171
-rw-r--r--src/mainwindow/widgets/tabbar.h (renamed from src/widgets/mainwindowtabbar.h)35
-rw-r--r--src/webengine/webengineprofile.cpp16
-rw-r--r--src/webengine/webengineprofile.h4
-rw-r--r--src/webengine/webview.cpp11
-rw-r--r--src/webengine/webview.h3
-rw-r--r--src/widgets/mainwindowmenubar.cpp8
-rw-r--r--src/widgets/mainwindowtabbar.cpp150
17 files changed, 255 insertions, 211 deletions
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<std::string>()->default_value("Ctrl+N"))
- ("browser.shortcuts.newTab", po::value<std::string>()->default_value("Ctrl+T"))
("browser.shortcuts.about", po::value<std::string>()->default_value("F1"))
("browser.shortcuts.quit", po::value<std::string>()->default_value("Ctrl+Q"))
@@ -54,10 +53,11 @@ Configuration::Configuration()
("addressbar.shortcuts.pageMenu", po::value<std::string>()->default_value("F2"))
("addressbar.shortcuts.toolsMenu", po::value<std::string>()->default_value("F10"))
- // tabs
- ("browser.shortcuts.tabClose", po::value<std::string>()->default_value("Ctrl+X"))
- ("browser.shortcuts.tabLeft", po::value<std::string>()->default_value("Ctrl+O"))
- ("browser.shortcuts.tabRight", po::value<std::string>()->default_value("Ctrl+P"))
+ // tab bar
+ ("tabbar.shortcuts.new", po::value<std::string>()->default_value("Ctrl+T"))
+ ("tabbar.shortcuts.close", po::value<std::string>()->default_value("Ctrl+X"))
+ ("tabbar.shortcuts.left", po::value<std::string>()->default_value("Ctrl+O"))
+ ("tabbar.shortcuts.right", po::value<std::string>()->default_value("Ctrl+P"))
// page
("browser.shortcuts.toggleSearchBox", po::value<std::string>()->default_value("F3"))
@@ -131,7 +131,7 @@ bool Configuration::parse(int argc, const char **argv)
QHash<QString, QString> Configuration::section(const std::string &prefix) const
{
QHash<QString, QString> 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<std::string>(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 <QWebEngineDownloadItem>
#include <QtConcurrent>
#include <bookmarks/bookmarkswidget.h>
#include <downloads/downloadswidget.h>
@@ -141,9 +144,11 @@ std::shared_ptr<WebEngineProfile> Browser::profile(const QString &storageName)
if(storageName.isEmpty()) {
// create off-the-record profile
_profile = std::make_shared<WebEngineProfile>(nullptr);
+ _profile->loadProfile(m_config->section("profile"), path + "/otr.ini");
} else {
// regular profile
_profile = std::make_shared<WebEngineProfile>(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 <QJsonObject>
#include <QVector>
#include <memory>
-#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<WebEngineProfile> profile(const QString &storageName);
- const QList<QString> profiles() const {
+ const QList<QString> profiles() const
+ {
return m_profiles.keys();
}
- const QVector<Plugin> plugins() const {
+ const QVector<Plugin> plugins() const
+ {
return m_plugins;
}
@@ -60,8 +62,8 @@ private:
QHash<QString, std::shared_ptr<WebEngineProfile>> m_profiles;
std::shared_ptr<WebEngineProfile> m_defaultProfile;
- UrlRequestInterceptor* m_urlRequestInterceptor = nullptr;
- CookieFilter* m_cookieInterceptor = nullptr;
+ UrlRequestInterceptor *m_urlRequestInterceptor = nullptr;
+ CookieFilter *m_cookieInterceptor = nullptr;
std::shared_ptr<BookmarksWidget> m_bookmarksManager;
std::shared_ptr<DownloadsWidget> 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 <iomanip>
#include <iostream>
-
+#include "configuration/configuration.h"
// startup time measuring
#ifdef QT_DEBUG
#include <QElapsedTimer>
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 <QDockWidget>
#include <QMessageBox>
#include <bookmarks/bookmarksview.h>
@@ -21,10 +23,10 @@
MainWindow::MainWindow(std::shared_ptr<Configuration> 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<Configuration> 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 <QMainWindow>
#include <QUrl>
#include <interfaces.h>
@@ -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 <QToolBar>
#include <QToolButton>
#include <QWebEngineHistory>
+#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 <QAction>
+#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 <QContextMenuEvent>
+#include <QMenu>
+
+TabBar::TabBar(const QHash<QString, QString> &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/widgets/mainwindowtabbar.h b/src/mainwindow/widgets/tabbar.h
index 823db1b..f4b7414 100644
--- a/src/widgets/mainwindowtabbar.h
+++ b/src/mainwindow/widgets/tabbar.h
@@ -6,23 +6,22 @@
* SPDX-License-Identifier: GPL-3.0
*/
-#ifndef WEBVIEWTABBAR_H
-#define WEBVIEWTABBAR_H
+#ifndef SMOLBOTE_TABBAR_H
+#define SMOLBOTE_TABBAR_H
-#include "webengine/webengineprofile.h"
-#include "webengine/webview.h"
#include <QTabBar>
-#include <memory>
-class Configuration;
class MainWindow;
-class MainWindowTabBar : public QTabBar
+class WebView;
+class WebEngineProfile;
+class QMenu;
+class TabBar : public QTabBar
{
Q_OBJECT
public:
- explicit MainWindowTabBar(const std::shared_ptr<Configuration> &config, MainWindow *parent = nullptr);
- ~MainWindowTabBar();
+ explicit TabBar(const QHash<QString, QString> &config, MainWindow *parent = nullptr);
+ ~TabBar() override;
void setProfile(WebEngineProfile *profile);
WebView *currentView();
@@ -35,21 +34,23 @@ public slots:
void removeTab(int index);
protected:
- void contextMenuEvent(QContextMenuEvent *event);
- QSize tabSizeHint(int index) const;
+ void contextMenuEvent(QContextMenuEvent *event) override;
+ QSize tabSizeHint(int index) const override;
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<WebView *> m_views;
- MainWindow *m_parent;
-};
-WebView *createWebView(const QUrl &url, WebEngineProfile *profile, MainWindow *parent);
+ QMenu *tabContextMenu;
+
+ QAction *newTab_action;
+ QAction *closeTab_action;
+ QAction *leftTab_action;
+ QAction *rightTab_action;
+};
-#endif // WEBVIEWTABBAR_H
+#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<QString, QString> 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 <QUrl>
#include <QWebEngineProfile>
-#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<QString, QString> 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 <QDialog>
#include <QStatusBar>
#include <QVBoxLayout>
+#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 <QInputDialog>
+#include <QShortcut>
+#include "configuration/configuration.h"
MainWindowMenuBar::MainWindowMenuBar(std::shared_ptr<Configuration> config, MainWindow *parent)
: QMenuBar(parent)
@@ -32,11 +34,7 @@ MainWindowMenuBar::MainWindowMenuBar(std::shared_ptr<Configuration> config, Main
});
newWindowAction->setShortcut(QKeySequence(config->value<std::string>("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<std::string>("browser.shortcuts.newTab").value().c_str()));
+ browserMenu->addAction(parent->findChild<QAction*>("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 <QContextMenuEvent>
-#include <QShortcut>
-#include <QMenu>
-
-MainWindowTabBar::MainWindowTabBar(const std::shared_ptr<Configuration> &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<std::string>("browser.shortcuts.tabClose").value())));
- connect(tabCloseShortcut, &QShortcut::activated, [this]() {
- this->removeTab(currentIndex());
- });
-
- QShortcut *tabLeftShortcut = new QShortcut(parent);
- tabLeftShortcut->setKey(QKeySequence(QString::fromStdString(config->value<std::string>("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<std::string>("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;
-}