From db73ab6dfdefd50c8f46f64423572757630723d9 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Fri, 29 Dec 2017 18:25:04 +0800 Subject: [PATCH] VWebView: fix img src after copied Add config web/fix_img_src_when_copied to control the behavior. --- src/dialog/vtipsdialog.cpp | 1 + src/dialog/vupdater.cpp | 1 + src/resources/vnote.ini | 3 ++ src/vconfigmanager.cpp | 3 ++ src/vconfigmanager.h | 10 ++++++ src/vwebview.cpp | 66 ++++++++++++++++++++++++++++++++++++-- src/vwebview.h | 4 +++ 7 files changed, 86 insertions(+), 2 deletions(-) diff --git a/src/dialog/vtipsdialog.cpp b/src/dialog/vtipsdialog.cpp index 247c0db8..cc1adbe5 100644 --- a/src/dialog/vtipsdialog.cpp +++ b/src/dialog/vtipsdialog.cpp @@ -24,6 +24,7 @@ VTipsDialog::VTipsDialog(const QString &p_tipFile, void VTipsDialog::setupUI(const QString &p_actionText) { m_viewer = VUtils::getWebEngineView(); + m_viewer->setContextMenuPolicy(Qt::NoContextMenu); m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok); connect(m_btnBox, &QDialogButtonBox::accepted, this, &QDialog::accept); diff --git a/src/dialog/vupdater.cpp b/src/dialog/vupdater.cpp index c8369feb..f0301628 100644 --- a/src/dialog/vupdater.cpp +++ b/src/dialog/vupdater.cpp @@ -36,6 +36,7 @@ void VUpdater::setupUI() m_proBar->setTextVisible(false); m_descriptionWV = VUtils::getWebEngineView(); + m_descriptionWV->setContextMenuPolicy(Qt::NoContextMenu); m_descriptionWV->setHtml(VUtils::generateSimpleHtmlTemplate(VNote::s_sloganTemplate)); m_btnBox = new QDialogButtonBox(QDialogButtonBox::Ok); diff --git a/src/resources/vnote.ini b/src/resources/vnote.ini index 5b54d301..f36127ff 100644 --- a/src/resources/vnote.ini +++ b/src/resources/vnote.ini @@ -190,6 +190,9 @@ custom_colors=White:#EEEEEE,Green:#CCE8CF,Wheat:#DFC7B2,LightGrey:#D3D3D3 ; Location and configuration for Mathjax mathjax_javascript=https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-MML-AM_CHTML +; Fix local relative image source when copied +fix_img_src_when_copied=true + [shortcuts] ; Define shortcuts here, with each item in the form "operation=keysequence". ; Leave keysequence empty to disable the shortcut of an operation. diff --git a/src/vconfigmanager.cpp b/src/vconfigmanager.cpp index 310aea7c..60065c20 100644 --- a/src/vconfigmanager.cpp +++ b/src/vconfigmanager.cpp @@ -284,6 +284,9 @@ void VConfigManager::initialize() m_closeBeforeExternalEditor = getConfigFromSettings("global", "close_before_external_editor").toBool(); + + m_fixImageSrcInWebWhenCopied = getConfigFromSettings("web", + "fix_img_src_when_copied").toBool(); } void VConfigManager::initSettings() diff --git a/src/vconfigmanager.h b/src/vconfigmanager.h index 4a4dd87f..53355c34 100644 --- a/src/vconfigmanager.h +++ b/src/vconfigmanager.h @@ -426,6 +426,8 @@ public: bool getCloseBeforeExternalEditor() const; + bool getFixImageSrcInWebWhenCopied() const; + private: // Look up a config from user and default settings. QVariant getConfigFromSettings(const QString §ion, const QString &key) const; @@ -816,6 +818,9 @@ private: // Whether user has reset the configurations. bool m_hasReset; + // Whether fix the local relative image src in read mode when copied. + bool m_fixImageSrcInWebWhenCopied; + // The name of the config file in each directory, obsolete. // Use c_dirConfigFile instead. static const QString c_obsoleteDirConfigFile; @@ -1983,4 +1988,9 @@ inline bool VConfigManager::getCloseBeforeExternalEditor() const return m_closeBeforeExternalEditor; } +inline bool VConfigManager::getFixImageSrcInWebWhenCopied() const +{ + return m_fixImageSrcInWebWhenCopied; +} + #endif // VCONFIGMANAGER_H diff --git a/src/vwebview.cpp b/src/vwebview.cpp index 814c9eaa..d1fc48d1 100644 --- a/src/vwebview.cpp +++ b/src/vwebview.cpp @@ -11,9 +11,13 @@ #include #include #include +#include #include "vfile.h" #include "utils/vclipboardutils.h" #include "utils/viconutils.h" +#include "vconfigmanager.h" + +extern VConfigManager *g_config; // We set the property of the clipboard to mark that the URL copied in the // clipboard has been altered. @@ -23,7 +27,8 @@ VWebView::VWebView(VFile *p_file, QWidget *p_parent) : QWebEngineView(p_parent), m_file(p_file), m_copyImageUrlActionHooked(false), - m_needRemoveBackground(false) + m_needRemoveBackground(false), + m_fixImgSrc(g_config->getFixImageSrcInWebWhenCopied()) { setAcceptDrops(false); @@ -246,6 +251,56 @@ void VWebView::handleClipboardChanged(QClipboard::Mode p_mode) alterHtmlMimeData(clipboard, mimeData, removeBackground); } +bool VWebView::fixImgSrc(QString &p_html) +{ + bool changed = false; + +#if defined(Q_OS_WIN) + QUrl::ComponentFormattingOption strOpt = QUrl::EncodeSpaces; +#else + QUrl::ComponentFormattingOption strOpt = QUrl::FullyEncoded; +#endif + + QRegExp reg("(html(); + // Add surrounded tags. if (!html.startsWith("")) { altered = true; html = QString("%1").arg(html); } + // Remove background color. if (p_removeBackground && removeBackgroundColor(html)) { altered = true; } + // Fix local relative images. + if (m_fixImgSrc && fixImgSrc(html)) { + altered = true; + } + if (!altered) { return; } @@ -275,5 +337,5 @@ void VWebView::alterHtmlMimeData(QClipboard *p_clipboard, data->setHtml(html); VClipboardUtils::setMimeDataToClipboard(p_clipboard, data, QClipboard::Clipboard); - qDebug() << "altered clipboard's Html"; + qDebug() << "altered clipboard's Html" << html; } diff --git a/src/vwebview.h b/src/vwebview.h index c6e8ea36..c7ea3663 100644 --- a/src/vwebview.h +++ b/src/vwebview.h @@ -40,12 +40,16 @@ private: const QMimeData *p_mimeData, bool p_removeBackground); + bool fixImgSrc(QString &p_html); + VFile *m_file; // Whether this view has hooked the Copy Image Url action. bool m_copyImageUrlActionHooked; bool m_needRemoveBackground; + + bool m_fixImgSrc; }; #endif // VWEBVIEW_H