aboutsummaryrefslogtreecommitdiff
path: root/src/mainwindow
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 /src/mainwindow
parentUpdated documentation (diff)
downloadsmolbote-a6e6823da845ca14d37d8914c80185257e8c0e62.tar.xz
Refactoring TabBar
- cleaned up code - moved to mainwindow/widgets - add Close tabs left/right to context menu
Diffstat (limited to 'src/mainwindow')
-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.h56
6 files changed, 238 insertions, 7 deletions
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/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 <QTabBar>
+
+class MainWindow;
+class WebView;
+class WebEngineProfile;
+class QMenu;
+class TabBar : public QTabBar
+{
+ Q_OBJECT
+
+public:
+ explicit TabBar(const QHash<QString, QString> &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<WebView *> m_views;
+
+ QMenu *tabContextMenu;
+
+ QAction *newTab_action;
+ QAction *closeTab_action;
+ QAction *leftTab_action;
+ QAction *rightTab_action;
+};
+
+#endif // SMOLBOTE_TABBAR_H