add QWebView to view html

This commit is contained in:
Le Tan 2019-07-26 22:01:18 +08:00
parent b020629b9f
commit c3a692d33d
11 changed files with 104 additions and 40 deletions

View File

@ -1,6 +1,7 @@
#include "vcopytextashtmldialog.h" #include "vcopytextashtmldialog.h"
#include <QtWidgets> #include <QtWidgets>
#include <QWebView>
#include <QClipboard> #include <QClipboard>
#include <QMimeData> #include <QMimeData>
#include <QApplication> #include <QApplication>
@ -30,8 +31,8 @@ void VCopyTextAsHtmlDialog::setupUI()
m_textEdit->setProperty("LineEdit", true); m_textEdit->setProperty("LineEdit", true);
m_htmlLabel = new QLabel(tr("HTML:")); m_htmlLabel = new QLabel(tr("HTML:"));
m_htmlViewer = VUtils::getTextBrowser(g_config->getBaseBackground()); m_htmlViewer = VUtils::getWebView(g_config->getBaseBackground());
// m_htmlViewer->setContextMenuPolicy(Qt::NoContextMenu); m_htmlViewer->setContextMenuPolicy(Qt::NoContextMenu);
m_htmlViewer->setMinimumSize(600, 400); m_htmlViewer->setMinimumSize(600, 400);
m_infoLabel = new QLabel(tr("Converting text to HTML ...")); m_infoLabel = new QLabel(tr("Converting text to HTML ..."));
@ -65,8 +66,7 @@ void VCopyTextAsHtmlDialog::setConvertedHtml(const QUrl &p_baseUrl,
const QString &p_html) const QString &p_html)
{ {
QString html = p_html; QString html = p_html;
m_htmlViewer->setHtml("<html><body>" + html + "</body></html>"); m_htmlViewer->setHtml("<html><body>" + html + "</body></html>", p_baseUrl);
m_htmlViewer->setSource(p_baseUrl);
setHtmlVisible(true); setHtmlVisible(true);
g_webUtils->alterHtmlAsTarget(p_baseUrl, html, m_copyTarget); g_webUtils->alterHtmlAsTarget(p_baseUrl, html, m_copyTarget);

View File

@ -6,7 +6,7 @@
class QPlainTextEdit; class QPlainTextEdit;
class QTextBrowser; class QWebView;
class QDialogButtonBox; class QDialogButtonBox;
class VWaitingWidget; class VWaitingWidget;
class QLabel; class QLabel;
@ -32,7 +32,7 @@ private:
QLabel *m_htmlLabel; QLabel *m_htmlLabel;
QTextBrowser *m_htmlViewer; QWebView *m_htmlViewer;
QLabel *m_infoLabel; QLabel *m_infoLabel;

View File

@ -1,6 +1,7 @@
#include "vtipsdialog.h" #include "vtipsdialog.h"
#include <QtWidgets> #include <QtWidgets>
#include <QWebView>
#include "vconfigmanager.h" #include "vconfigmanager.h"
#include "vmarkdownconverter.h" #include "vmarkdownconverter.h"
@ -22,8 +23,8 @@ VTipsDialog::VTipsDialog(const QString &p_tipFile,
void VTipsDialog::setupUI(const QString &p_actionText) void VTipsDialog::setupUI(const QString &p_actionText)
{ {
m_viewer = VUtils::getTextBrowser(g_config->getBaseBackground()); m_viewer = VUtils::getWebView(g_config->getBaseBackground());
// m_viewer->setContextMenuPolicy(Qt::NoContextMenu); m_viewer->setContextMenuPolicy(Qt::NoContextMenu);
m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok); m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok);
connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept); connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
@ -58,10 +59,9 @@ void VTipsDialog::readFile(const QString &p_tipFile)
QString html = mdConverter.generateHtml(content, QString html = mdConverter.generateHtml(content,
g_config->getMarkdownExtensions(), g_config->getMarkdownExtensions(),
toc); toc);
// html = VUtils::generateSimpleHtmlTemplate(html); html = VUtils::generateSimpleHtmlTemplate(html);
// Add a base URL to enable it to access local style files. // Add a base URL to enable it to access local style files.
m_viewer->setHtml(html); m_viewer->setHtml(html, QUrl("qrc:/resources"));
// m_viewer->setSource(QUrl("qrc:/resources"));
} }
void VTipsDialog::showEvent(QShowEvent *p_event) void VTipsDialog::showEvent(QShowEvent *p_event)

View File

@ -8,7 +8,7 @@
class QDialogButtonBox; class QDialogButtonBox;
class QPushButton; class QPushButton;
class QShowEvent; class QShowEvent;
class QTextBrowser; class QWebView;
typedef std::function<void()> TipsDialogFunc; typedef std::function<void()> TipsDialogFunc;
@ -29,7 +29,7 @@ private:
void readFile(const QString &p_tipFile); void readFile(const QString &p_tipFile);
QTextBrowser *m_viewer; QWebView *m_viewer;
QDialogButtonBox *m_btnBox; QDialogButtonBox *m_btnBox;

View File

@ -4,7 +4,7 @@
# #
#------------------------------------------------- #-------------------------------------------------
QT += core gui network svg printsupport QT += core gui network svg printsupport webkitwidgets
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
@ -155,7 +155,8 @@ SOURCES += main.cpp\
widgets/vcombobox.cpp \ widgets/vcombobox.cpp \
vtablehelper.cpp \ vtablehelper.cpp \
vtable.cpp \ vtable.cpp \
dialog/vinserttabledialog.cpp dialog/vinserttabledialog.cpp \
vpreviewpage.cpp
HEADERS += vmainwindow.h \ HEADERS += vmainwindow.h \
vdirectorytree.h \ vdirectorytree.h \
@ -295,7 +296,8 @@ HEADERS += vmainwindow.h \
widgets/vcombobox.h \ widgets/vcombobox.h \
vtablehelper.h \ vtablehelper.h \
vtable.h \ vtable.h \
dialog/vinserttabledialog.h dialog/vinserttabledialog.h \
vpreviewpage.h
RESOURCES += \ RESOURCES += \
vnote.qrc \ vnote.qrc \

View File

@ -31,12 +31,13 @@
#include <QSvgRenderer> #include <QSvgRenderer>
#include <QPainter> #include <QPainter>
#include <QTemporaryFile> #include <QTemporaryFile>
#include <QTextBrowser> #include <QWebView>
#include "vorphanfile.h" #include "vorphanfile.h"
#include "vnote.h" #include "vnote.h"
#include "vnotebook.h" #include "vnotebook.h"
#include "pegparser.h" #include "pegparser.h"
#include "vpreviewpage.h"
#include "widgets/vcombobox.h" #include "widgets/vcombobox.h"
extern VConfigManager *g_config; extern VConfigManager *g_config;
@ -1462,10 +1463,18 @@ void VUtils::setDynamicProperty(QWidget *p_widget, const char *p_prop, bool p_va
p_widget->style()->polish(p_widget); p_widget->style()->polish(p_widget);
} }
QTextBrowser *VUtils::getTextBrowser(const QColor &p_background, QWidget *p_parent) QWebView *VUtils::getWebView(const QColor &p_background, QWidget *p_parent)
{ {
QTextBrowser *browser = new QTextBrowser(p_parent); auto viewer = new QWebView(p_parent);
return browser; VPreviewPage *page = new VPreviewPage(viewer);
// Setting the background to Qt::transparent will force GrayScale antialiasing.
if (p_background.isValid() && p_background != Qt::transparent) {
page->setBackgroundColor(p_background);
}
viewer->setPage(page);
viewer->setZoomFactor(g_config->getWebZoomFactor());
return viewer;
} }
QString VUtils::getFileNameWithLocale(const QString &p_name, const QString &p_locale) QString VUtils::getFileNameWithLocale(const QString &p_name, const QString &p_locale)

View File

@ -23,7 +23,7 @@ class QAction;
class QTreeWidgetItem; class QTreeWidgetItem;
class QFormLayout; class QFormLayout;
class QTemporaryFile; class QTemporaryFile;
class QTextBrowser; class QWebView;
#if !defined(V_ASSERT) #if !defined(V_ASSERT)
#define V_ASSERT(cond) ((!(cond)) ? qt_assert(#cond, __FILE__, __LINE__) : qt_noop()) #define V_ASSERT(cond) ((!(cond)) ? qt_assert(#cond, __FILE__, __LINE__) : qt_noop())
@ -314,7 +314,7 @@ public:
// Create and return a QComboBox. // Create and return a QComboBox.
static QComboBox *getComboBox(QWidget *p_parent = nullptr); static QComboBox *getComboBox(QWidget *p_parent = nullptr);
static QTextBrowser *getTextBrowser(const QColor &p_background, QWidget *p_parent = nullptr); static QWebView *getWebView(const QColor &p_background, QWidget *p_parent = nullptr);
static void setDynamicProperty(QWidget *p_widget, const char *p_prop, bool p_val = true); static void setDynamicProperty(QWidget *p_widget, const char *p_prop, bool p_val = true);

47
src/vpreviewpage.cpp Normal file
View File

@ -0,0 +1,47 @@
#include "vpreviewpage.h"
#include <QDesktopServices>
#include <QNetworkRequest>
#include <QWebFrame>
#include "vmainwindow.h"
extern VMainWindow *g_mainWin;
VPreviewPage::VPreviewPage(QWidget *parent)
: QWebPage(parent)
{
}
bool VPreviewPage::acceptNavigationRequest(QWebFrame *p_frame,
const QNetworkRequest &p_request,
QWebPage::NavigationType p_type)
{
Q_UNUSED(p_frame);
Q_UNUSED(p_type);
auto url = p_request.url();
if (url.isLocalFile()) {
QString filePath = url.toLocalFile();
if (g_mainWin->tryOpenInternalFile(filePath)) {
return false;
}
} else if (p_frame) {
return true;
} else if (url.scheme() == "data") {
// Qt 5.12 will trigger this when calling QWebEngineView.setHtml().
return true;
}
QDesktopServices::openUrl(url);
return false;
}
void VPreviewPage::setBackgroundColor(const QColor &p_background)
{
auto pa = palette();
pa.setColor(QPalette::Window, p_background);
pa.setColor(QPalette::Base, p_background);
setPalette(pa);
}

21
src/vpreviewpage.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef VPREVIEWPAGE_H
#define VPREVIEWPAGE_H
#include <QWebPage>
#include <QColor>
class VPreviewPage : public QWebPage
{
Q_OBJECT
public:
explicit VPreviewPage(QWidget *parent = 0);
void setBackgroundColor(const QColor &p_background);
protected:
bool acceptNavigationRequest(QWebFrame *p_frame,
const QNetworkRequest &p_request,
QWebPage::NavigationType p_type) Q_DECL_OVERRIDE;
};
#endif // VPREVIEWPAGE_H

View File

@ -27,7 +27,7 @@ extern VWebUtils *g_webUtils;
static const QString c_ClipboardPropertyMark = "CopiedImageURLAltered"; static const QString c_ClipboardPropertyMark = "CopiedImageURLAltered";
VWebView::VWebView(VFile *p_file, QWidget *p_parent) VWebView::VWebView(VFile *p_file, QWidget *p_parent)
: QTextBrowser(p_parent), : QWebView(p_parent),
m_file(p_file), m_file(p_file),
m_copyImageUrlActionHooked(false), m_copyImageUrlActionHooked(false),
m_afterCopyImage(false), m_afterCopyImage(false),
@ -481,14 +481,3 @@ void VWebView::initPreviewTunnelMenu(QAction *p_before, QMenu *p_menu)
p_menu->insertMenu(p_before, subMenu); p_menu->insertMenu(p_before, subMenu);
} }
QString VWebView::selectedText() const
{
return QString();
}
void VWebView::setHtml(const QString &p_html, const QUrl &p_baseUrl)
{
QTextBrowser::setHtml(p_html);
setSource(p_baseUrl);
}

View File

@ -1,14 +1,14 @@
#ifndef VWEBVIEW_H #ifndef VWEBVIEW_H
#define VWEBVIEW_H #define VWEBVIEW_H
#include <QTextBrowser> #include <QWebView>
#include <QClipboard> #include <QClipboard>
#include <QRegExp> #include <QRegExp>
class VFile; class VFile;
class QMenu; class QMenu;
class VWebView : public QTextBrowser class VWebView : public QWebView
{ {
Q_OBJECT Q_OBJECT
public: public:
@ -17,10 +17,6 @@ public:
void setInPreview(bool p_preview); void setInPreview(bool p_preview);
QString selectedText() const;
void setHtml(const QString &p_html, const QUrl &p_baseUrl = QUrl());
signals: signals:
void editNote(); void editNote();