/* * 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 "mainwindow/subwindow.h" #include "webpage.h" #include "webprofile.h" #include "widgets/pagetoolsmenu.h" #include #include #include #include #include #include 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; }); #if defined(QTBUG_65223_WORKAROUND) connect(this, &QWebEngineView::loadProgress, this, [this](int progress) { if(progress == 100) { emit loadFinished(true); } }); #endif m_toolsMenu = new PageToolsMenu(this); } void WebView::setProfile(WebProfile *profile) { m_profile = profile; const auto url = this->url(); setPage(new WebPage(profile, this)); this->load(url); } bool WebView::isLoaded() const { return m_loaded; } 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 auto index = m_parentWindow->addTab(); WebView *view = m_parentWindow->view(index); switch(type) { case QWebEnginePage::WebBrowserWindow: // a complete web browser window break; case QWebEnginePage::WebBrowserTab: // a web browser tab m_parentWindow->setCurrentTab(index); break; case QWebEnginePage::WebDialog: // a window without decorations break; case QWebEnginePage::WebBrowserBackgroundTab: // a web browser tab, but don't swap to it break; } return view; } void WebView::contextMenuEvent(QContextMenuEvent *event) { QMenu *menu = page()->createStandardContextMenu(); // zoom widget menu->addSeparator(); { auto *zoomSlider = new QSlider(Qt::Horizontal); zoomSlider->setMinimum(5); zoomSlider->setMaximum(50); zoomSlider->setValue(10); auto *zoomAction = menu->addAction(tr("Zoom: %1x").arg(zoomFactor())); connect(zoomAction, &QAction::triggered, this, [zoomSlider]() { zoomSlider->setValue(10); }); connect(zoomSlider, &QSlider::valueChanged, this, [this, zoomAction](int value) { zoomAction->setText(tr("Zoom: %1x").arg(static_cast(value) / 10)); setZoomFactor(static_cast(value) / 10); }); auto *zoomWidgetAction = new QWidgetAction(this); zoomWidgetAction->setDefaultWidget(zoomSlider); menu->addAction(zoomWidgetAction); } menu->popup(event->globalPos()); } void WebView::triggerViewAction(WebView::ViewAction action) { switch(action) { case GoHome: load(m_profile->homepage()); break; case BookmarkPage: emit newBookmark(this->title(), this->url()); break; } }