From cb21a6816eebcf9081049e6c123b1272ec057dd9 Mon Sep 17 00:00:00 2001 From: Matthieu Gautier Date: Wed, 18 Jul 2018 17:35:17 +0200 Subject: [PATCH] Set the tab's text to the title of the webView. The `titleChanged` signal is emitted twice: - At beggining of loading with the Url of the page. - Once the parsing is done and the `` html tag has been found. --- ktabwidget.cpp | 12 ++++++++++++ ktabwidget.h | 1 + 2 files changed, 13 insertions(+) diff --git a/ktabwidget.cpp b/ktabwidget.cpp index 8f3e6bc..3a08263 100644 --- a/ktabwidget.cpp +++ b/ktabwidget.cpp @@ -9,6 +9,8 @@ KTabWidget::KTabWidget(QWidget *parent) : KiwixWebView* KTabWidget::createNewTab(bool setCurrent) { KiwixWebView* webView = new KiwixWebView(); + QObject::connect(webView, &KiwixWebView::titleChanged, this, + [=](const QString& str) { setTitleOf(webView, str); }); // Ownership of webview is passed to the tabWidget addTab(webView, ""); if (setCurrent) { @@ -25,3 +27,13 @@ void KTabWidget::openUrl(std::shared_ptr reader, const QUrl& url, } webView->setUrl(url); } + +void KTabWidget::setTitleOf(KiwixWebView* webView, const QString& title) +{ + if (title.startsWith("zim://")) { + auto url = QUrl(title); + setTabText(indexOf(webView), url.path()); + } else { + setTabText(indexOf(webView), title); + } +} diff --git a/ktabwidget.h b/ktabwidget.h index fb8cd4e..7c7d3bf 100644 --- a/ktabwidget.h +++ b/ktabwidget.h @@ -13,6 +13,7 @@ public: KiwixWebView* createNewTab(bool setCurrent); void openUrl(std::shared_ptr reader, const QUrl &url, bool newTab); + void setTitleOf(KiwixWebView* webView, const QString& title); }; #endif // KTABWIDGET_H