From 9ba76d7ccdaafcbc7c25b9c4737bd22a2a87d1dd Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Wed, 3 Oct 2018 11:04:05 +0200 Subject: Subwindow: only keep tab history for restore tab action --- src/subwindow/tabwidget.cpp | 49 +++++++++++++++++++++++++++------------------ src/subwindow/tabwidget.h | 12 ++++++++++- 2 files changed, 41 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/subwindow/tabwidget.cpp b/src/subwindow/tabwidget.cpp index 6189b2c..25e27cf 100644 --- a/src/subwindow/tabwidget.cpp +++ b/src/subwindow/tabwidget.cpp @@ -14,6 +14,16 @@ #include #include #include +#include + +inline WebView *createViewFromInfo(TabWidget::TabInformation &tab, QWidget *parent) +{ + auto *view = new WebView(tab.profile, parent); + QDataStream stream(&tab.historyBuffer, QIODevice::ReadOnly); + stream >> *view->history(); + view->history()->goToItem(view->history()->itemAt(tab.historyIndex)); + return view; +} TabWidget::TabWidget(QWidget *parent) : QTabWidget(parent) @@ -72,31 +82,28 @@ QMenu *TabWidget::createTabMenu(QWidget *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(page->profile()), this); - view->setPage(page); - addTab(view); + TabInformation tab = m_closedTabs.takeLast(); + addTab(createViewFromInfo(tab, this)); } }); 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(page->profile()), this); - view->setPage(page); - this->addTab(view); - m_closedTabs.removeOne(page); + for(int i = 0; i < m_closedTabs.count(); ++i) { + auto *openAction = tabHistoryMenu->addAction(m_closedTabs.at(i).title); + + connect(openAction, &QAction::triggered, this, [this, i]() { + TabInformation tab = m_closedTabs.takeAt(i); + addTab(createViewFromInfo(tab, this)); }); } + tabHistoryMenu->addSeparator(); + auto *clearTabHistory = tabHistoryMenu->addAction(tr("Clear")); connect(clearTabHistory, &QAction::triggered, this, [this]() { - while(!m_closedTabs.isEmpty()) { - delete m_closedTabs.dequeue(); - } + m_closedTabs.clear(); }); }); @@ -137,14 +144,18 @@ int TabWidget::addTab(WebView *view) void TabWidget::deleteTab(int index) { // deleting the widget automatically removes the tab? - auto *w = widget(index); + WebView *w = qobject_cast(widget(index)); - QWebEnginePage *p = qobject_cast(w)->page(); - p->setParent(nullptr); - m_closedTabs.enqueue(p); + TabInformation tab; + tab.profile = w->profile(); + tab.title = w->title(); + tab.historyIndex = w->history()->currentItemIndex(); + QDataStream stream(&tab.historyBuffer, QIODevice::WriteOnly); + stream << *w->history(); + m_closedTabs.enqueue(tab); while(m_closedTabs.length() > 5) { - delete m_closedTabs.dequeue(); + m_closedTabs.dequeue(); } disconnect(w); diff --git a/src/subwindow/tabwidget.h b/src/subwindow/tabwidget.h index d1a16b8..48690e8 100644 --- a/src/subwindow/tabwidget.h +++ b/src/subwindow/tabwidget.h @@ -11,16 +11,26 @@ #include #include +#include class QAction; class QMenu; class WebView; +class WebProfile; class QWebEnginePage; class TabWidget : public QTabWidget { Q_OBJECT public: + struct TabInformation + { + WebProfile *profile; + QString title; + int historyIndex; + QByteArray historyBuffer; + }; + explicit TabWidget(QWidget *parent = nullptr); ~TabWidget() override; @@ -36,7 +46,7 @@ protected: private: QMenu *tabContextMenu; - QQueue m_closedTabs; + QQueue m_closedTabs; }; #endif // SMOLBOTE_TABWIDGET_H -- cgit v1.2.1