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 --- src/widgets/mainwindowmenubar.cpp | 8 +- src/widgets/mainwindowtabbar.cpp | 150 -------------------------------------- src/widgets/mainwindowtabbar.h | 55 -------------- 3 files changed, 3 insertions(+), 210 deletions(-) delete mode 100644 src/widgets/mainwindowtabbar.cpp delete mode 100644 src/widgets/mainwindowtabbar.h (limited to 'src/widgets') 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