/* * This file is part of smolbote. It's copyrighted by the contributors recorded * in the version control history of the file, available from its original * location: https://neueland.iserlohn-fortress.net/smolbote.hg * * SPDX-License-Identifier: GPL-3.0 */ #include "webview.h" #include "webpage.h" #include "webprofile.h" #include "widgets/pagemenu.h" #include "widgets/pagetoolsmenu.h" #include #include #include #include "mainwindow/window.h" WebView::WebView(WebProfile *profile, QWidget *parent) : QWebEngineView(parent) { Q_CHECK_PTR(profile); m_profile = profile; setPage(new WebPage(profile, this)); m_parentWindow = qobject_cast(parent); // load status and progress connect(this, &QWebEngineView::loadStarted, this, [this]() { m_loaded = false; }); connect(this, &QWebEngineView::loadFinished, this, [this]() { m_loaded = true; }); connect(this, &QWebEngineView::loadProgress, this, [this](int progress) { m_loadProgress = progress; if(m_loadProgress == 100) { m_loaded = true; emit loaded(); } }); m_pageMenu = new PageMenu(this); m_toolsMenu = new PageToolsMenu(this); } WebView::~WebView() { delete m_pageMenu; } QMenu *WebView::pageMenu() { Q_CHECK_PTR(m_pageMenu); return m_pageMenu; } void WebView::setPage(QWebEnginePage *page) { Q_CHECK_PTR(page); // make sure the page gets cleaned up if we replace it by taking ownership page->setParent(this); connect(page, &QWebEnginePage::linkHovered, this, &WebView::handleLinkHovered); QWebEngineView::setPage(page); } bool WebView::isLoaded() const { return m_loaded; } int WebView::loadProgress() const { return m_loadProgress; } void WebView::search(const QString& term) { const QString searchUrl = m_profile->search().arg(QString(QUrl::toPercentEncoding(term))); load(searchUrl); } WebView *WebView::createWindow(QWebEnginePage::WebWindowType type) { if(m_parentWindow == nullptr) { qDebug("WebView::createWindow: parent window not found, returning nullptr"); return nullptr; } // parent Window has been found WebView *view = new WebView(m_profile, m_parentWindow); switch(type) { case QWebEnginePage::WebBrowserWindow: // a complete web browser window m_parentWindow->addTab(view); break; case QWebEnginePage::WebBrowserTab: // a web browser tab m_parentWindow->swapToTab(m_parentWindow->addTab(view)); break; case QWebEnginePage::WebDialog: // a window without decorations m_parentWindow->addTab(view); break; case QWebEnginePage::WebBrowserBackgroundTab: // a web browser tab, but don't swap to it m_parentWindow->addTab(view); break; } return view; } void WebView::handleLinkHovered(const QString &url) { // TODO: tooltip Q_UNUSED(url); } void WebView::triggerViewAction(WebView::ViewAction action) { switch(action) { case GoHome: load(m_profile->homepage()); case BookmarkPage: emit newBookmark(this->title(), this->url()); break; } }