140 lines
3.3 KiB
C++
Executable File
140 lines
3.3 KiB
C++
Executable File
/**
|
|
* @RDE_HEADER@
|
|
* Handle HTML output for thp
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
*/
|
|
#include "thp_html_widget.h"
|
|
#include "ui_thp_html_widget.h"
|
|
|
|
#include <QUrl>
|
|
#include <QWebEnginePage>
|
|
#include <NGLLogger/NGLLoggerBootstrap.h>
|
|
|
|
|
|
namespace THP
|
|
{
|
|
|
|
class MyQWebEnginePage : public QWebEnginePage
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
explicit MyQWebEnginePage(NGL::INGLLogger &logger, QObject *parent = nullptr)
|
|
: QWebEnginePage(parent)
|
|
, m_logger(logger)
|
|
{}
|
|
MyQWebEnginePage(NGL::INGLLogger &logger, QWebEngineProfile *profile, QObject *parent = nullptr)
|
|
: QWebEnginePage(profile, parent)
|
|
, m_logger(logger)
|
|
{}
|
|
|
|
bool needsToBeOpenedExternally(const QUrl &url)
|
|
{
|
|
const QString lpath = url.toString();
|
|
const QStringRef ext = lpath.splitRef(QLatin1Char('.')).last();
|
|
const QStringRef ext2Chars = ext.left(2);
|
|
if (ext == QLatin1String("html") &&
|
|
lpath.midRef(lpath.lastIndexOf(QLatin1Char('/')) + 1).startsWith(QLatin1String("combined_")))
|
|
return true;
|
|
// PDF
|
|
if (ext == QLatin1String("pdf"))
|
|
return true;
|
|
// A/V playlist
|
|
if (ext == QLatin1String("pls"))
|
|
return true;
|
|
// Situation
|
|
if (ext == QLatin1String("json"))
|
|
return true;
|
|
// Word/Excel/PowerPoint
|
|
if (ext2Chars == QLatin1String("do") ||
|
|
ext2Chars == QLatin1String("xl") ||
|
|
ext2Chars == QLatin1String("pp"))
|
|
return true;
|
|
return false;
|
|
}
|
|
|
|
bool acceptNavigationRequest(const QUrl &url,
|
|
QWebEnginePage::NavigationType type,
|
|
bool isMainFrame) override
|
|
{
|
|
Q_UNUSED(isMainFrame)
|
|
if (type == QWebEnginePage::NavigationTypeLinkClicked)
|
|
{
|
|
if (needsToBeOpenedExternally(url))
|
|
{
|
|
emit linkClicked(url);
|
|
DEBUG_LOG(m_logger) << "acceptNavigationRequest denied for " << url.toString();
|
|
return false;
|
|
}
|
|
}
|
|
DEBUG_LOG(m_logger) << "acceptNavigationRequest allowed for " << url.toString();
|
|
return true;
|
|
}
|
|
|
|
Q_SIGNALS:
|
|
void linkClicked(const QUrl &url);
|
|
private:
|
|
NGL::INGLLogger &m_logger;
|
|
|
|
};
|
|
|
|
HtmlWidget::HtmlWidget(QWidget *parent)
|
|
: QWidget(parent)
|
|
, ui(new Ui::HtmlWidget)
|
|
, m_logger(NGL::NGLLoggerBootstrap::getNGLRootLogger().getNGLLogger("THP.HtmlWidget"))
|
|
{
|
|
ui->setupUi(this);
|
|
auto webEnginePage = new MyQWebEnginePage(m_logger, this);
|
|
ui->html->setPage(webEnginePage);
|
|
connect(webEnginePage, &MyQWebEnginePage::linkClicked, this, &HtmlWidget::linkClicked);
|
|
connect(ui->html, &QWebEngineView::loadFinished, this, &HtmlWidget::loadFinished);
|
|
connect(ui->html, &QWebEngineView::urlChanged, this, &HtmlWidget::urlChanged);
|
|
connect(ui->html, &QWebEngineView::titleChanged, this, &HtmlWidget::titleChanged);
|
|
}
|
|
|
|
HtmlWidget::~HtmlWidget()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void HtmlWidget::back()
|
|
{
|
|
ui->html->back();
|
|
}
|
|
|
|
QString HtmlWidget::title() const
|
|
{
|
|
return ui->html->title();
|
|
}
|
|
|
|
QUrl HtmlWidget::url() const
|
|
{
|
|
return ui->html->url();
|
|
}
|
|
|
|
bool HtmlWidget::showHTML(const QUrl &url)
|
|
{
|
|
if (!url.isValid())
|
|
{
|
|
WARNING_LOG(m_logger) << "Invalid url: " << url.toString();
|
|
return false;
|
|
}
|
|
ui->html->load(url);
|
|
return true;
|
|
}
|
|
|
|
void HtmlWidget::setZoomFactor(double newZoomFactor)
|
|
{
|
|
ui->html->setZoomFactor(newZoomFactor);
|
|
}
|
|
|
|
void HtmlWidget::print(QPrinter *printer) const
|
|
{
|
|
ui->html->page()->print(printer, [](bool) {});
|
|
}
|
|
|
|
} // namespace THP
|
|
|
|
#include "thp_html_widget.moc"
|