diff --git a/templateparser/src/templateextracthtmlelementwebengineview.cpp b/templateparser/src/templateextracthtmlelementwebengineview.cpp index c57b217d..9d83f2c4 100644 --- a/templateparser/src/templateextracthtmlelementwebengineview.cpp +++ b/templateparser/src/templateextracthtmlelementwebengineview.cpp @@ -1,119 +1,118 @@ /* Copyright (C) 2017-2019 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "templateextracthtmlelementwebengineview.h" #include "templateparser_debug.h" #include "templatewebenginepage.h" #include template struct InvokeWrapper { R *receiver; void (C::*memberFun)(Arg); void operator()(Arg result) { (receiver->*memberFun)(result); } }; template InvokeWrapper invoke(R *receiver, void (C::*memberFun)(Arg)) { InvokeWrapper wrapper = {receiver, memberFun}; return wrapper; } using namespace TemplateParser; -TemplateExtractHtmlElementWebEngineView::TemplateExtractHtmlElementWebEngineView(QWidget *parent) - : QWebEngineView(parent) +TemplateExtractHtmlElementWebEngineView::TemplateExtractHtmlElementWebEngineView(QObject *parent) + : QObject(parent) { mPage = new TemplateWebEnginePage(this); - setPage(mPage); connect(mPage, &TemplateWebEnginePage::loadFinished, this, &TemplateExtractHtmlElementWebEngineView::slotLoadFinished); } TemplateExtractHtmlElementWebEngineView::~TemplateExtractHtmlElementWebEngineView() { } void TemplateExtractHtmlElementWebEngineView::clear() { mBodyElement.clear(); mHeaderElement.clear(); mHtmlElement.clear(); } void TemplateExtractHtmlElementWebEngineView::setHtmlContent(const QString &html) { clear(); mHtmlElement = html; - setHtml(html); + mPage->setHtml(html); } QString extractHeaderBodyScript() { const QString source = QStringLiteral("(function() {" "var res = {" " body: document.getElementsByTagName('body')[0].innerHTML," " header: document.getElementsByTagName('head')[0].innerHTML" "};" "return res;" "})()"); return source; } void TemplateExtractHtmlElementWebEngineView::slotLoadFinished(bool success) { if (success) { mPage->runJavaScript(extractHeaderBodyScript(), (QWebEngineScript::UserWorld + 2), invoke(this, &TemplateExtractHtmlElementWebEngineView::handleHtmlInfo)); } else { Q_EMIT loadContentDone(false); } } void TemplateExtractHtmlElementWebEngineView::handleHtmlInfo(const QVariant &result) { if (result.isValid()) { const QVariantMap map = result.toMap(); mBodyElement = map.value(QStringLiteral("body")).toString(); mHeaderElement = map.value(QStringLiteral("header")).toString(); Q_EMIT loadContentDone(true); } else { qCWarning(TEMPLATEPARSER_LOG) << "Impossible to get value"; Q_EMIT loadContentDone(false); } } QString TemplateExtractHtmlElementWebEngineView::htmlElement() const { return mHtmlElement; } QString TemplateExtractHtmlElementWebEngineView::headerElement() const { return mHeaderElement; } QString TemplateExtractHtmlElementWebEngineView::bodyElement() const { return mBodyElement; } diff --git a/templateparser/src/templateextracthtmlelementwebengineview.h b/templateparser/src/templateextracthtmlelementwebengineview.h index 853d3221..c379cdee 100644 --- a/templateparser/src/templateextracthtmlelementwebengineview.h +++ b/templateparser/src/templateextracthtmlelementwebengineview.h @@ -1,57 +1,57 @@ /* Copyright (C) 2017-2019 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef TEMPLATEEXTRACTHTMLELEMENTWEBENGINEVIEW_H #define TEMPLATEEXTRACTHTMLELEMENTWEBENGINEVIEW_H #include #include "templateparser_private_export.h" namespace TemplateParser { class TemplateWebEnginePage; -class TEMPLATEPARSER_TESTS_EXPORT TemplateExtractHtmlElementWebEngineView : public QWebEngineView +class TEMPLATEPARSER_TESTS_EXPORT TemplateExtractHtmlElementWebEngineView : public QObject { Q_OBJECT public: - explicit TemplateExtractHtmlElementWebEngineView(QWidget *parent = nullptr); + explicit TemplateExtractHtmlElementWebEngineView(QObject *parent = nullptr); ~TemplateExtractHtmlElementWebEngineView(); QString bodyElement() const; QString headerElement() const; QString htmlElement() const; void setHtmlContent(const QString &html); Q_SIGNALS: void loadContentDone(bool success); private: void clear(); void slotLoadFinished(bool success); void handleHtmlInfo(const QVariant &result); QString mBodyElement; QString mHeaderElement; QString mHtmlElement; TemplateWebEnginePage *mPage = nullptr; }; } #endif // TEMPLATEEXTRACTHTMLELEMENTWEBENGINEVIEW_H diff --git a/templateparser/src/templatewebengineview.cpp b/templateparser/src/templatewebengineview.cpp index f831b694..c1160e35 100644 --- a/templateparser/src/templatewebengineview.cpp +++ b/templateparser/src/templatewebengineview.cpp @@ -1,80 +1,79 @@ /* Copyright (C) 2017-2019 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "templatewebengineview.h" #include "templatewebenginepage.h" #include "templateparser_debug.h" using namespace TemplateParser; template struct InvokeWrapper { R *receiver; void (C::*memberFun)(Arg); void operator()(Arg result) { (receiver->*memberFun)(result); } }; template InvokeWrapper invoke(R *receiver, void (C::*memberFun)(Arg)) { InvokeWrapper wrapper = {receiver, memberFun}; return wrapper; } -TemplateWebEngineView::TemplateWebEngineView(QWidget *parent) - : QWebEngineView(parent) +TemplateWebEngineView::TemplateWebEngineView(QObject *parent) + : QObject(parent) { mPage = new TemplateWebEnginePage(this); - setPage(mPage); connect(mPage, &TemplateWebEnginePage::loadFinished, this, &TemplateWebEngineView::slotLoadFinished); } TemplateWebEngineView::~TemplateWebEngineView() { } void TemplateWebEngineView::setHtmlContent(const QString &html) { mExtractedPlainText.clear(); - setHtml(html); + mPage->setHtml(html); } void TemplateWebEngineView::slotLoadFinished(bool ok) { if (ok) { mPage->toPlainText(invoke(this, &TemplateWebEngineView::setPlainText)); } else { qCWarning(TEMPLATEPARSER_LOG) << "Loading page failed"; Q_EMIT loadContentDone(false); } } void TemplateWebEngineView::setPlainText(const QString &plainText) { mExtractedPlainText = plainText; Q_EMIT loadContentDone(true); } QString TemplateWebEngineView::plainText() const { return mExtractedPlainText; } diff --git a/templateparser/src/templatewebengineview.h b/templateparser/src/templatewebengineview.h index fc77eab5..31f8ea82 100644 --- a/templateparser/src/templatewebengineview.h +++ b/templateparser/src/templatewebengineview.h @@ -1,50 +1,50 @@ /* Copyright (C) 2017-2019 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef TEMPLATEWEBENGINEVIEW_H #define TEMPLATEWEBENGINEVIEW_H #include #include "templateparser_export.h" namespace TemplateParser { class TemplateWebEnginePage; -class TEMPLATEPARSER_EXPORT TemplateWebEngineView : public QWebEngineView +class TEMPLATEPARSER_EXPORT TemplateWebEngineView : public QObject { Q_OBJECT public: - explicit TemplateWebEngineView(QWidget *parent = nullptr); + explicit TemplateWebEngineView(QObject *parent = nullptr); ~TemplateWebEngineView(); void setHtmlContent(const QString &html); QString plainText() const; Q_SIGNALS: void loadContentDone(bool success); private: void slotLoadFinished(bool ok); void setPlainText(const QString &plainText); QString mExtractedPlainText; TemplateWebEnginePage *mPage = nullptr; }; } #endif // TEMPLATEWEBENGINEVIEW_H