From 765f33e796dbc1e8c5fefd77c9cf4cdf58a99a83 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 11 Jun 2017 16:00:31 +0200 Subject: Fixed crash when closing last tab --- src/mainwindow.cpp | 18 +++++++++++------- src/widgets/webviewtabbar.cpp | 30 ++++++++++++++++++++++++++---- src/widgets/webviewtabbar.h | 6 ++++-- 3 files changed, 41 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 851ee9e..861f7cf 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -35,7 +35,7 @@ MainWindow::MainWindow(QUrl defaultUrl, QWidget *parent) : ui(new Ui::MainWindow), navigationToolBar(new QToolBar(tr("Navigation"), this)), tabToolBar(new QToolBar(tr("Tab bar"), this)), - tabBar(new WebViewTabBar(this)), + tabBar(new WebViewTabBar(nullptr, this)), urlLineEdit(new UrlLineEdit(navigationToolBar)), progressBar(new LoadingBar(this)) { @@ -91,6 +91,7 @@ MainWindow::MainWindow(QUrl defaultUrl, QWidget *parent) : // Load profile m_profile = qApp->profile(sSettings->value("browser.profile.default").toString()); + tabBar->setProfile(m_profile); // loading bar ui->statusBar->addPermanentWidget(progressBar); @@ -148,9 +149,9 @@ void MainWindow::addTabbedDock(Qt::DockWidgetArea area, QDockWidget *widget) void MainWindow::newTab(const QUrl &url) { if(!url.isEmpty()) { - tabBar->addTab(m_profile, url); + tabBar->addTab(url); } else { - tabBar->addTab(m_profile, sSettings->value("general.newtab").toUrl()); + tabBar->addTab(sSettings->value("general.newtab").toUrl()); } } @@ -202,11 +203,14 @@ void MainWindow::handleTabChanged(WebView *view) { Q_ASSERT(view); - // clear the parent of the central widget so it doesn't get deleted - centralWidget()->setParent(0); + // centralWidget can be a nullptr + if(centralWidget()) { + // clear the parent of the central widget so it doesn't get deleted + centralWidget()->setParent(0); - // disconnect signals - disconnect(centralWidget()); + // disconnect signals + disconnect(centralWidget()); + } // set new central widget setCentralWidget(view); diff --git a/src/widgets/webviewtabbar.cpp b/src/widgets/webviewtabbar.cpp index 429b103..d57e095 100644 --- a/src/widgets/webviewtabbar.cpp +++ b/src/widgets/webviewtabbar.cpp @@ -22,12 +22,15 @@ #include "browser.h" #include -WebViewTabBar::WebViewTabBar(QWidget *parent) : +WebViewTabBar::WebViewTabBar(QWebEngineProfile *profile, QWidget *parent) : QTabBar(parent) { + m_profile = profile; + setElideMode(Qt::ElideRight); setTabsClosable(true); setMovable(true); + setContextMenuPolicy(Qt::DefaultContextMenu); connect(this, SIGNAL(tabCloseRequested(int)), this, SLOT(removeTab(int))); connect(this, SIGNAL(currentChanged(int)), this, SLOT(handleCurrentChanged(int))); connect(this, SIGNAL(tabMoved(int,int)), this, SLOT(updateVectorArrangement(int,int))); @@ -73,15 +76,14 @@ QSignalMapper *WebViewTabBar::signalMapper() return m_signalMapper; } -int WebViewTabBar::addTab(QWebEngineProfile *profile, const QUrl &url) +int WebViewTabBar::addTab(const QUrl &url) { WebView *view = new WebView(0); - QWebEnginePage *page = new QWebEnginePage(profile); + QWebEnginePage *page = new QWebEnginePage(m_profile); view->setPage(page); page->load(url); m_views.append(view); - //connect(view, SIGNAL(titleChanged()), this, SLOT(updateTabText())); connect(view, &QWebEngineView::titleChanged, [this, view](const QString &title) { int index = m_views.indexOf(view); setTabText(index, title); @@ -96,6 +98,7 @@ int WebViewTabBar::addTab(QWebEngineProfile *profile, const QUrl &url) void WebViewTabBar::setProfile(QWebEngineProfile *profile) { + m_profile = profile; for(auto view : qAsConst(m_views)) { QWebEnginePage *page = new QWebEnginePage(profile); page->load(view->url()); @@ -108,6 +111,21 @@ WebView *WebViewTabBar::currentView() return m_views.at(currentIndex()); } +void WebViewTabBar::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 WebViewTabBar::tabSizeHint(int index) const { Q_UNUSED(index) @@ -116,6 +134,10 @@ QSize WebViewTabBar::tabSizeHint(int index) const void WebViewTabBar::handleCurrentChanged(int index) { + if(index < 0) { + addTab(QUrl::fromUserInput(sSettings->value("general.newtab").toString())); + return; + } emit currentTabChanged(m_views.at(index)); } diff --git a/src/widgets/webviewtabbar.h b/src/widgets/webviewtabbar.h index bf39efe..d1ccf81 100644 --- a/src/widgets/webviewtabbar.h +++ b/src/widgets/webviewtabbar.h @@ -30,7 +30,7 @@ class WebViewTabBar : public QTabBar Q_OBJECT public: - explicit WebViewTabBar(QWidget *parent = 0); + explicit WebViewTabBar(QWebEngineProfile *profile, QWidget *parent = 0); ~WebViewTabBar(); void setProfile(QWebEngineProfile *profile); @@ -42,12 +42,13 @@ signals: void currentTabChanged(WebView *view); public slots: - int addTab(QWebEngineProfile *profile, const QUrl &url); + int addTab(const QUrl &url); void removeTab(int index); void webAction(int action); protected: + void contextMenuEvent(QContextMenuEvent *event); QSize tabSizeHint(int index) const; private slots: @@ -61,6 +62,7 @@ private: QVector m_views; QSignalMapper *m_signalMapper; + QWebEngineProfile *m_profile; }; #endif // WEBVIEWTABBAR_H -- cgit v1.2.1