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 `<title></title>` html tag has been
  found.
This commit is contained in:
Matthieu Gautier 2018-07-18 17:35:17 +02:00
parent 81766be447
commit cb21a6816e
2 changed files with 13 additions and 0 deletions

View File

@ -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<kiwix::Reader> 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);
}
}

View File

@ -13,6 +13,7 @@ public:
KiwixWebView* createNewTab(bool setCurrent);
void openUrl(std::shared_ptr<kiwix::Reader> reader, const QUrl &url, bool newTab);
void setTitleOf(KiwixWebView* webView, const QString& title);
};
#endif // KTABWIDGET_H