diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-04-16 17:07:36 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-04-16 17:07:36 +0200 |
commit | d796821f8304306dbe088701724243b39e8eb358 (patch) | |
tree | 836c85aa421db5c495e2b36f6a02cea924b6d919 /src/mainwindow/widgets/tabwidget.cpp | |
parent | Branch of Qt5.11 features (diff) | |
download | smolbote-d796821f8304306dbe088701724243b39e8eb358.tar.xz |
Multiple subwindows interface
Subwindows are similar to tab groups.
- Rewrote Browser and MainWindow, so they should be somewhat cleaner now
- Moved AboutDialog to lib/about
What's broken:
- loading bar
- search box
- address bar bookmark suggestions
- plugins
Diffstat (limited to 'src/mainwindow/widgets/tabwidget.cpp')
-rw-r--r-- | src/mainwindow/widgets/tabwidget.cpp | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/src/mainwindow/widgets/tabwidget.cpp b/src/mainwindow/widgets/tabwidget.cpp new file mode 100644 index 0000000..21476cd --- /dev/null +++ b/src/mainwindow/widgets/tabwidget.cpp @@ -0,0 +1,121 @@ +/* + * 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 "tabwidget.h" +#include "webengine/webview.h" +#include <QAction> +#include <QContextMenuEvent> +#include <QMenu> +#include <QTabBar> + +TabWidget::TabWidget(QWidget *parent) + : QTabWidget(parent) +{ + setStyleSheet("QTabBar::tab { width: 200px; }"); + + setTabsClosable(true); + setTabBarAutoHide(true); + setElideMode(Qt::ElideRight); + setMovable(true); + + connect(this, &TabWidget::tabCloseRequested, this, &TabWidget::deleteTab); + + // general actions + closeTab_action = new QAction(this); + closeTab_action->setShortcut(QKeySequence("Ctrl+X")); + connect(closeTab_action, &QAction::triggered, this, [this]() { + this->deleteTab(this->currentIndex()); + }); + addAction(closeTab_action); + + leftTab_action = new QAction(this); + leftTab_action->setShortcut(QKeySequence("Ctrl+O")); + connect(leftTab_action, &QAction::triggered, this, [this]() { + this->setCurrentIndex(qMax(0, this->currentIndex() - 1)); + }); + addAction(leftTab_action); + + rightTab_action = new QAction(this); + rightTab_action->setShortcut(QKeySequence("Ctrl+P")); + connect(rightTab_action, &QAction::triggered, this, [this]() { + this->setCurrentIndex(qMin(this->currentIndex() + 1, this->count() - 1)); + }); + addAction(rightTab_action); + + // context menu + tabContextMenu = new QMenu(this); + auto *closeTab = tabContextMenu->addAction(tr("Close Tab")); + connect(closeTab, &QAction::triggered, this, [this]() { + deleteTab(this->tabBar()->tabAt(mapFromGlobal(tabContextMenu->pos()))); + }); + + auto *closeTabsLeft = tabContextMenu->addAction(tr("Close Tabs left")); + connect(closeTabsLeft, &QAction::triggered, this, [this]() { + int idx = this->tabBar()->tabAt(mapFromGlobal(tabContextMenu->pos())); + for(int i = idx - 1; i >= 0; --i) { + deleteTab(i); + } + }); + + auto *closeTabsRight = tabContextMenu->addAction(tr("Close Tabs right")); + connect(closeTabsRight, &QAction::triggered, this, [this]() { + int idx = this->tabBar()->tabAt(mapFromGlobal(tabContextMenu->pos())); + for(int i = count() - 1; i > idx; --i) { + deleteTab(i); + } + }); +} + +TabWidget::~TabWidget() +{ + for(int i = count() - 1; i >= 0; i--) { + delete widget(i); + } +} + +int TabWidget::addTab(WebView *view) +{ + Q_CHECK_PTR(view); + + int idx = QTabWidget::addTab(view, view->title()); + connect(view, &WebView::titleChanged, [this, view](const QString &title) { + int idx = this->indexOf(view); + Q_ASSERT(idx != -1); + + this->setTabText(idx, title); + }); + connect(view, &WebView::iconChanged, [this, view](const QIcon &icon) { + int idx = this->indexOf(view); + Q_ASSERT(idx != -1); + + this->setTabIcon(idx, icon); + }); + + return idx; +} + +void TabWidget::deleteTab(int index) +{ + // deleting the widget automatically removes the tab? + if(count() > 1) { + widget(index)->deleteLater(); + removeTab(index); + } else + parentWidget()->close(); +} + +void TabWidget::contextMenuEvent(QContextMenuEvent *event) +{ + // check if the context menu was called on a tab + int tabIndex = tabBar()->tabAt(event->pos()); + if(tabIndex < 0) { + return; + } + + tabContextMenu->exec(event->globalPos()); +} |