diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-09-22 13:07:33 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-09-22 13:07:33 +0200 |
commit | 9c12d813966e0fdc4542a7001ffa135722840de0 (patch) | |
tree | 3e2e382a52d7c1fc6eecfdd7ac1200747d5de3a4 | |
parent | Add updater notes to documentation (diff) | |
download | smolbote-9c12d813966e0fdc4542a7001ffa135722840de0.tar.xz |
SubWindow: add 'open last closed tab/tabs' actions
-rw-r--r-- | src/subwindow/subwindow.cpp | 1 | ||||
-rw-r--r-- | src/subwindow/tabwidget.cpp | 57 | ||||
-rw-r--r-- | src/subwindow/tabwidget.h | 5 |
3 files changed, 63 insertions, 0 deletions
diff --git a/src/subwindow/subwindow.cpp b/src/subwindow/subwindow.cpp index 6cbb172..0e59c6b 100644 --- a/src/subwindow/subwindow.cpp +++ b/src/subwindow/subwindow.cpp @@ -69,6 +69,7 @@ SubWindow::SubWindow(const std::unique_ptr<Configuration> &config, QWidget *pare auto index = addTab(WebProfile::defaultProfile()->newtab()); tabWidget->setCurrentIndex(index); }); + newTab_button->setMenu(tabWidget->createTabMenu(newTab_button)); tabWidget->setCornerWidget(newTab_button, Qt::TopRightCorner); // general actions diff --git a/src/subwindow/tabwidget.cpp b/src/subwindow/tabwidget.cpp index 5583bcd..27ff1f3 100644 --- a/src/subwindow/tabwidget.cpp +++ b/src/subwindow/tabwidget.cpp @@ -65,6 +65,54 @@ TabWidget::~TabWidget() } } +QMenu *TabWidget::createTabMenu(QWidget *parent) +{ + auto *menu = new QMenu(parent); + + auto *reopenTabAction = menu->addAction(tr("Reopen last tab")); + connect(reopenTabAction, &QAction::triggered, this, [this]() { + if(!m_closedTabs.isEmpty()) { + QWebEnginePage *page = m_closedTabs.takeLast(); + auto *view = new WebView(qobject_cast<WebProfile *>(page->profile()), this); + view->setPage(page); + addTab(view); + } + }); + + auto *tabHistoryMenu = menu->addMenu(tr("Tab History")); + connect(tabHistoryMenu, &QMenu::aboutToShow, this, [this, tabHistoryMenu]() { + tabHistoryMenu->clear(); + for(QWebEnginePage *page : m_closedTabs) { + auto *openAction = tabHistoryMenu->addAction(page->title()); + connect(openAction, &QAction::triggered, this, [page, this]() { + auto *view = new WebView(qobject_cast<WebProfile *>(page->profile()), this); + view->setPage(page); + this->addTab(view); + m_closedTabs.removeOne(page); + }); + } + tabHistoryMenu->addSeparator(); + auto *clearTabHistory = tabHistoryMenu->addAction(tr("Clear")); + connect(clearTabHistory, &QAction::triggered, this, [this]() { + while(!m_closedTabs.isEmpty()) { + delete m_closedTabs.dequeue(); + } + }); + }); + + connect(menu, &QMenu::aboutToShow, this, [this, reopenTabAction, tabHistoryMenu]() { + if(m_closedTabs.isEmpty()) { + reopenTabAction->setEnabled(false); + tabHistoryMenu->setEnabled(false); + } else { + reopenTabAction->setEnabled(true); + tabHistoryMenu->setEnabled(true); + } + }); + + return menu; +} + int TabWidget::addTab(WebView *view) { Q_ASSERT_X(view != nullptr, "TabWidget::addTab", "Tried to add null view"); @@ -90,6 +138,15 @@ void TabWidget::deleteTab(int index) { // deleting the widget automatically removes the tab? auto *w = widget(index); + + QWebEnginePage *p = qobject_cast<WebView *>(w)->page(); + p->setParent(nullptr); + m_closedTabs.enqueue(p); + + while(m_closedTabs.length() > 5) { + delete m_closedTabs.dequeue(); + } + disconnect(w); delete w; diff --git a/src/subwindow/tabwidget.h b/src/subwindow/tabwidget.h index 793e041..a62d120 100644 --- a/src/subwindow/tabwidget.h +++ b/src/subwindow/tabwidget.h @@ -10,10 +10,12 @@ #define SMOLBOTE_TABWIDGET_H #include <QTabWidget> +#include <QQueue> class QAction; class QMenu; class WebView; +class QWebEnginePage; class TabWidget : public QTabWidget { Q_OBJECT @@ -22,6 +24,8 @@ public: explicit TabWidget(QWidget *parent = nullptr); ~TabWidget() override; + QMenu *createTabMenu(QWidget *parent = nullptr); + public slots: int addTab(WebView *view); void deleteTab(int index); @@ -32,6 +36,7 @@ protected: private: QMenu *tabContextMenu; + QQueue<QWebEnginePage *> m_closedTabs; }; #endif // SMOLBOTE_TABWIDGET_H |