mirror of
https://gitee.com/vnotex/vnote.git
synced 2025-07-05 13:59:52 +08:00
add QWebView to view html
This commit is contained in:
parent
b020629b9f
commit
c3a692d33d
@ -1,6 +1,7 @@
|
||||
#include "vcopytextashtmldialog.h"
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QWebView>
|
||||
#include <QClipboard>
|
||||
#include <QMimeData>
|
||||
#include <QApplication>
|
||||
@ -30,8 +31,8 @@ void VCopyTextAsHtmlDialog::setupUI()
|
||||
m_textEdit->setProperty("LineEdit", true);
|
||||
|
||||
m_htmlLabel = new QLabel(tr("HTML:"));
|
||||
m_htmlViewer = VUtils::getTextBrowser(g_config->getBaseBackground());
|
||||
// m_htmlViewer->setContextMenuPolicy(Qt::NoContextMenu);
|
||||
m_htmlViewer = VUtils::getWebView(g_config->getBaseBackground());
|
||||
m_htmlViewer->setContextMenuPolicy(Qt::NoContextMenu);
|
||||
m_htmlViewer->setMinimumSize(600, 400);
|
||||
|
||||
m_infoLabel = new QLabel(tr("Converting text to HTML ..."));
|
||||
@ -65,8 +66,7 @@ void VCopyTextAsHtmlDialog::setConvertedHtml(const QUrl &p_baseUrl,
|
||||
const QString &p_html)
|
||||
{
|
||||
QString html = p_html;
|
||||
m_htmlViewer->setHtml("<html><body>" + html + "</body></html>");
|
||||
m_htmlViewer->setSource(p_baseUrl);
|
||||
m_htmlViewer->setHtml("<html><body>" + html + "</body></html>", p_baseUrl);
|
||||
setHtmlVisible(true);
|
||||
|
||||
g_webUtils->alterHtmlAsTarget(p_baseUrl, html, m_copyTarget);
|
||||
|
@ -6,7 +6,7 @@
|
||||
|
||||
|
||||
class QPlainTextEdit;
|
||||
class QTextBrowser;
|
||||
class QWebView;
|
||||
class QDialogButtonBox;
|
||||
class VWaitingWidget;
|
||||
class QLabel;
|
||||
@ -32,7 +32,7 @@ private:
|
||||
|
||||
QLabel *m_htmlLabel;
|
||||
|
||||
QTextBrowser *m_htmlViewer;
|
||||
QWebView *m_htmlViewer;
|
||||
|
||||
QLabel *m_infoLabel;
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
#include "vtipsdialog.h"
|
||||
|
||||
#include <QtWidgets>
|
||||
#include <QWebView>
|
||||
|
||||
#include "vconfigmanager.h"
|
||||
#include "vmarkdownconverter.h"
|
||||
@ -22,8 +23,8 @@ VTipsDialog::VTipsDialog(const QString &p_tipFile,
|
||||
|
||||
void VTipsDialog::setupUI(const QString &p_actionText)
|
||||
{
|
||||
m_viewer = VUtils::getTextBrowser(g_config->getBaseBackground());
|
||||
// m_viewer->setContextMenuPolicy(Qt::NoContextMenu);
|
||||
m_viewer = VUtils::getWebView(g_config->getBaseBackground());
|
||||
m_viewer->setContextMenuPolicy(Qt::NoContextMenu);
|
||||
|
||||
m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok);
|
||||
connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
|
||||
@ -58,10 +59,9 @@ void VTipsDialog::readFile(const QString &p_tipFile)
|
||||
QString html = mdConverter.generateHtml(content,
|
||||
g_config->getMarkdownExtensions(),
|
||||
toc);
|
||||
// html = VUtils::generateSimpleHtmlTemplate(html);
|
||||
html = VUtils::generateSimpleHtmlTemplate(html);
|
||||
// Add a base URL to enable it to access local style files.
|
||||
m_viewer->setHtml(html);
|
||||
// m_viewer->setSource(QUrl("qrc:/resources"));
|
||||
m_viewer->setHtml(html, QUrl("qrc:/resources"));
|
||||
}
|
||||
|
||||
void VTipsDialog::showEvent(QShowEvent *p_event)
|
||||
|
@ -8,7 +8,7 @@
|
||||
class QDialogButtonBox;
|
||||
class QPushButton;
|
||||
class QShowEvent;
|
||||
class QTextBrowser;
|
||||
class QWebView;
|
||||
|
||||
typedef std::function<void()> TipsDialogFunc;
|
||||
|
||||
@ -29,7 +29,7 @@ private:
|
||||
|
||||
void readFile(const QString &p_tipFile);
|
||||
|
||||
QTextBrowser *m_viewer;
|
||||
QWebView *m_viewer;
|
||||
|
||||
QDialogButtonBox *m_btnBox;
|
||||
|
||||
|
@ -4,7 +4,7 @@
|
||||
#
|
||||
#-------------------------------------------------
|
||||
|
||||
QT += core gui network svg printsupport
|
||||
QT += core gui network svg printsupport webkitwidgets
|
||||
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||
|
||||
@ -155,7 +155,8 @@ SOURCES += main.cpp\
|
||||
widgets/vcombobox.cpp \
|
||||
vtablehelper.cpp \
|
||||
vtable.cpp \
|
||||
dialog/vinserttabledialog.cpp
|
||||
dialog/vinserttabledialog.cpp \
|
||||
vpreviewpage.cpp
|
||||
|
||||
HEADERS += vmainwindow.h \
|
||||
vdirectorytree.h \
|
||||
@ -295,7 +296,8 @@ HEADERS += vmainwindow.h \
|
||||
widgets/vcombobox.h \
|
||||
vtablehelper.h \
|
||||
vtable.h \
|
||||
dialog/vinserttabledialog.h
|
||||
dialog/vinserttabledialog.h \
|
||||
vpreviewpage.h
|
||||
|
||||
RESOURCES += \
|
||||
vnote.qrc \
|
||||
|
@ -31,12 +31,13 @@
|
||||
#include <QSvgRenderer>
|
||||
#include <QPainter>
|
||||
#include <QTemporaryFile>
|
||||
#include <QTextBrowser>
|
||||
#include <QWebView>
|
||||
|
||||
#include "vorphanfile.h"
|
||||
#include "vnote.h"
|
||||
#include "vnotebook.h"
|
||||
#include "pegparser.h"
|
||||
#include "vpreviewpage.h"
|
||||
#include "widgets/vcombobox.h"
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
return browser;
|
||||
auto viewer = new QWebView(p_parent);
|
||||
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)
|
||||
|
@ -23,7 +23,7 @@ class QAction;
|
||||
class QTreeWidgetItem;
|
||||
class QFormLayout;
|
||||
class QTemporaryFile;
|
||||
class QTextBrowser;
|
||||
class QWebView;
|
||||
|
||||
#if !defined(V_ASSERT)
|
||||
#define V_ASSERT(cond) ((!(cond)) ? qt_assert(#cond, __FILE__, __LINE__) : qt_noop())
|
||||
@ -314,7 +314,7 @@ public:
|
||||
// Create and return a QComboBox.
|
||||
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);
|
||||
|
||||
|
47
src/vpreviewpage.cpp
Normal file
47
src/vpreviewpage.cpp
Normal 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
21
src/vpreviewpage.h
Normal 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
|
@ -27,7 +27,7 @@ extern VWebUtils *g_webUtils;
|
||||
static const QString c_ClipboardPropertyMark = "CopiedImageURLAltered";
|
||||
|
||||
VWebView::VWebView(VFile *p_file, QWidget *p_parent)
|
||||
: QTextBrowser(p_parent),
|
||||
: QWebView(p_parent),
|
||||
m_file(p_file),
|
||||
m_copyImageUrlActionHooked(false),
|
||||
m_afterCopyImage(false),
|
||||
@ -481,14 +481,3 @@ void VWebView::initPreviewTunnelMenu(QAction *p_before, QMenu *p_menu)
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -1,14 +1,14 @@
|
||||
#ifndef VWEBVIEW_H
|
||||
#define VWEBVIEW_H
|
||||
|
||||
#include <QTextBrowser>
|
||||
#include <QWebView>
|
||||
#include <QClipboard>
|
||||
#include <QRegExp>
|
||||
|
||||
class VFile;
|
||||
class QMenu;
|
||||
|
||||
class VWebView : public QTextBrowser
|
||||
class VWebView : public QWebView
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
@ -17,10 +17,6 @@ public:
|
||||
|
||||
void setInPreview(bool p_preview);
|
||||
|
||||
QString selectedText() const;
|
||||
|
||||
void setHtml(const QString &p_html, const QUrl &p_baseUrl = QUrl());
|
||||
|
||||
signals:
|
||||
void editNote();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user