From 81766be447495bee1106bdccc28789707c344da2 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 18 Jul 2018 17:30:06 +0200 Subject: [PATCH] Allow user to open a link in a new tab. When a user or on a link, `QWebEngineView` try to open a new window using the `createWindow` method. By overloading it, we can create our own tab as we want. --- kiwix-desktop.pro | 5 ++++- kiwixapp.cpp | 13 +++++++++++-- kiwixapp.h | 8 +++++++- kiwixwebview.cpp | 16 ++++++++++------ kiwixwebview.h | 3 ++- ktabwidget.cpp | 27 +++++++++++++++++++++++++++ ktabwidget.h | 18 ++++++++++++++++++ mainwindow.cpp | 14 +++----------- mainwindow.h | 5 +++-- mainwindow.ui | 8 +++++++- 10 files changed, 92 insertions(+), 25 deletions(-) create mode 100644 ktabwidget.cpp create mode 100644 ktabwidget.h diff --git a/kiwix-desktop.pro b/kiwix-desktop.pro index 184c32a..b2217c5 100644 --- a/kiwix-desktop.pro +++ b/kiwix-desktop.pro @@ -38,7 +38,8 @@ SOURCES += \ kiwixrequestinterceptor.cpp \ kiwixwebview.cpp \ library.cpp \ - topwidget.cpp + topwidget.cpp \ + ktabwidget.cpp HEADERS += \ mainwindow.h \ @@ -49,6 +50,7 @@ HEADERS += \ kiwixwebview.h \ library.h \ topwidget.h \ + ktabwidget.h \ kconstants.h FORMS += \ @@ -60,6 +62,7 @@ isEmpty(PREFIX) { target.path = $$PREFIX/bin INSTALLS += target + static { PKGCONFIG_OPTION = "--static" } diff --git a/kiwixapp.cpp b/kiwixapp.cpp index f8a02ee..42b4a83 100644 --- a/kiwixapp.cpp +++ b/kiwixapp.cpp @@ -43,6 +43,7 @@ KiwixApp::KiwixApp(int& argc, char *argv[]) setFont(font); mainWindow = new MainWindow; mainWindow->show(); + tabWidget = mainWindow->getTabWidget(); errorDialog = new QErrorMessage(mainWindow); } @@ -60,12 +61,20 @@ KiwixApp *KiwixApp::instance() void KiwixApp::openZimFile(const QString &zimfile) { + QString zimId; try { - auto zimId = library.openBook(zimfile); - mainWindow->displayReader(library.getReader(zimId)); + zimId = library.openBook(zimfile); } catch (const std::exception& e) { showMessage("Cannot open " + zimfile + ": \n" + e.what()); + return; } + openUrl(QUrl("zim://"+zimId+"/")); +} + +void KiwixApp::openUrl(const QUrl &url, bool newTab) { + auto reader = library.getReader(url.host()); + Q_ASSERT(reader); + tabWidget->openUrl(reader, url, newTab); } void KiwixApp::showMessage(const QString &message) diff --git a/kiwixapp.h b/kiwixapp.h index 4f85ee5..79642a5 100644 --- a/kiwixapp.h +++ b/kiwixapp.h @@ -3,6 +3,7 @@ #include "library.h" #include "mainwindow.h" +#include "ktabwidget.h" #include "kiwixschemehandler.h" #include "kiwixrequestinterceptor.h" @@ -18,16 +19,21 @@ public: static KiwixApp* instance(); void openZimFile(const QString& zimfile); + void openUrl(const QUrl& url, bool newTab=true); + + void showMessage(const QString& message); KiwixSchemeHandler* getSchemeHandler() { return &schemeHandler; } KiwixRequestInterceptor* getRequestInterceptor() { return &requestIntercetor; } Library* getLibrary() { return &library; } MainWindow* getMainWindow() { return mainWindow; } + KTabWidget* getTabWidget() { return tabWidget; } + - void showMessage(const QString& message); private: Library library; MainWindow* mainWindow; + KTabWidget* tabWidget; QErrorMessage* errorDialog; KiwixSchemeHandler schemeHandler; diff --git a/kiwixwebview.cpp b/kiwixwebview.cpp index b847e26..b072598 100644 --- a/kiwixwebview.cpp +++ b/kiwixwebview.cpp @@ -16,11 +16,15 @@ KiwixWebView::KiwixWebView(QWidget *parent) KiwixWebView::~KiwixWebView() {} - -void KiwixWebView::initFromReader(std::shared_ptr reader) +QWebEngineView* KiwixWebView::createWindow(QWebEnginePage::WebWindowType type) { - std::string url("zim://"); - url += reader->getId(); - url += ".zim/"; - page()->setUrl(QUrl(QString::fromStdString(url))); + if ( type==QWebEnginePage::WebBrowserBackgroundTab + || type==QWebEnginePage::WebBrowserTab ) + { + auto tabWidget = KiwixApp::instance()->getTabWidget(); + return tabWidget->createNewTab(type==QWebEnginePage::WebBrowserTab); + } + return nullptr; +} + } diff --git a/kiwixwebview.h b/kiwixwebview.h index ae20166..6254469 100644 --- a/kiwixwebview.h +++ b/kiwixwebview.h @@ -12,7 +12,8 @@ public: KiwixWebView(QWidget *parent = Q_NULLPTR); virtual ~KiwixWebView(); - void initFromReader(std::shared_ptr reader); +protected: + virtual QWebEngineView* createWindow(QWebEnginePage::WebWindowType type); }; #endif // KIWIXWEBVIEW_H diff --git a/ktabwidget.cpp b/ktabwidget.cpp new file mode 100644 index 0000000..8f3e6bc --- /dev/null +++ b/ktabwidget.cpp @@ -0,0 +1,27 @@ +#include "ktabwidget.h" + +KTabWidget::KTabWidget(QWidget *parent) : + QTabWidget(parent) +{ + +} + +KiwixWebView* KTabWidget::createNewTab(bool setCurrent) +{ + KiwixWebView* webView = new KiwixWebView(); + // Ownership of webview is passed to the tabWidget + addTab(webView, ""); + if (setCurrent) { + setCurrentWidget(webView); + } + return webView; +} + +void KTabWidget::openUrl(std::shared_ptr reader, const QUrl& url, bool newTab) +{ + KiwixWebView* webView = nullptr; + if (newTab || !currentWidget()) { + webView = createNewTab(true); + } + webView->setUrl(url); +} diff --git a/ktabwidget.h b/ktabwidget.h new file mode 100644 index 0000000..fb8cd4e --- /dev/null +++ b/ktabwidget.h @@ -0,0 +1,18 @@ +#ifndef KTABWIDGET_H +#define KTABWIDGET_H + +#include +#include +#include +#include "kiwixwebview.h" + +class KTabWidget : public QTabWidget +{ +public: + KTabWidget(QWidget* parent=nullptr); + + KiwixWebView* createNewTab(bool setCurrent); + void openUrl(std::shared_ptr reader, const QUrl &url, bool newTab); +}; + +#endif // KTABWIDGET_H diff --git a/mainwindow.cpp b/mainwindow.cpp index d677ebf..511e618 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -23,16 +23,8 @@ MainWindow::~MainWindow() delete ui; } -void MainWindow::displayReader(std::shared_ptr reader) +KTabWidget* MainWindow::getTabWidget() { - auto webview = new KiwixWebView(); - std::string favicon_content; - std::string favicon_mimetype; - reader->getFavicon(favicon_content, favicon_mimetype); - QPixmap pixmap; - pixmap.loadFromData((const uchar*)favicon_content.data(), favicon_content.size()); - auto icon = QIcon(pixmap); - // Ownership of webview is passed to the tabWidget - ui->tabWidget->addTab(webview, icon, QString::fromStdString(reader->getTitle())); - webview->initFromReader(reader); + return ui->tabWidget; } + diff --git a/mainwindow.h b/mainwindow.h index e3abd5b..376bea6 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -3,6 +3,7 @@ #include #include "kiwixwebview.h" +#include "ktabwidget.h" namespace Ui { class MainWindow; @@ -16,11 +17,11 @@ public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); - void displayReader(std::shared_ptr reader); + KTabWidget* getTabWidget(); private: Ui::MainWindow *ui; - std::map, KiwixWebView*> webviews_map; + }; #endif // MAINWINDOW_H diff --git a/mainwindow.ui b/mainwindow.ui index 1c6c676..58d3fdf 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -19,7 +19,7 @@ - + QTabWidget::Rounded @@ -50,6 +50,12 @@ QToolBar
topwidget.h
+ + KTabWidget + QTabWidget +
ktabwidget.h
+ 1 +