VWebView: remove html in clipobard after copy image

When pasting in WeChat public account, if there is html with image data
in the clipboard, it could not recognize the image data.
This commit is contained in:
Le Tan 2018-01-01 11:46:35 +08:00
parent dbba8edad6
commit fffae253c5
2 changed files with 41 additions and 9 deletions

View File

@ -28,7 +28,8 @@ VWebView::VWebView(VFile *p_file, QWidget *p_parent)
m_file(p_file), m_file(p_file),
m_copyImageUrlActionHooked(false), m_copyImageUrlActionHooked(false),
m_needRemoveBackground(false), m_needRemoveBackground(false),
m_fixImgSrc(g_config->getFixImageSrcInWebWhenCopied()) m_fixImgSrc(g_config->getFixImageSrcInWebWhenCopied()),
m_afterCopyImage(false)
{ {
setAcceptDrops(false); setAcceptDrops(false);
@ -86,10 +87,10 @@ void VWebView::contextMenuEvent(QContextMenuEvent *p_event)
menu->insertAction(copyWithoutBgAct, copyAct); menu->insertAction(copyWithoutBgAct, copyAct);
} }
// We need to replace the "Copy Image" action, because the default one use // We need to replace the "Copy Image" action:
// the fully-encoded URL to fetch the image while Windows seems to not // - the default one use the fully-encoded URL to fetch the image while
// recognize it. // Windows seems to not recognize it.
#if defined(Q_OS_WIN) // - We need to remove the html to let it be recognized by some web pages.
QAction *defaultCopyImageAct = pageAction(QWebEnginePage::CopyImageToClipboard); QAction *defaultCopyImageAct = pageAction(QWebEnginePage::CopyImageToClipboard);
if (actions.contains(defaultCopyImageAct)) { if (actions.contains(defaultCopyImageAct)) {
QAction *copyImageAct = new QAction(defaultCopyImageAct->text(), menu); QAction *copyImageAct = new QAction(defaultCopyImageAct->text(), menu);
@ -99,7 +100,6 @@ void VWebView::contextMenuEvent(QContextMenuEvent *p_event)
menu->insertAction(defaultCopyImageAct, copyImageAct); menu->insertAction(defaultCopyImageAct, copyImageAct);
defaultCopyImageAct->setVisible(false); defaultCopyImageAct->setVisible(false);
} }
#endif
// Add Copy All without Background action. // Add Copy All without Background action.
QAction *copyAllWithoutBgAct = new QAction(tr("Copy &All without Background"), menu); QAction *copyAllWithoutBgAct = new QAction(tr("Copy &All without Background"), menu);
@ -123,8 +123,10 @@ void VWebView::handleEditAction()
void VWebView::copyImage() void VWebView::copyImage()
{ {
Q_ASSERT(m_copyImageUrlActionHooked); m_afterCopyImage = true;
#if defined(Q_OS_WIN)
Q_ASSERT(m_copyImageUrlActionHooked);
// triggerPageAction(QWebEnginePage::CopyImageUrlToClipboard) will not really // triggerPageAction(QWebEnginePage::CopyImageUrlToClipboard) will not really
// trigger the corresponding action. It just do the stuff directly. // trigger the corresponding action. It just do the stuff directly.
QAction *copyImageUrlAct = pageAction(QWebEnginePage::CopyImageUrlToClipboard); QAction *copyImageUrlAct = pageAction(QWebEnginePage::CopyImageUrlToClipboard);
@ -146,12 +148,14 @@ void VWebView::copyImage()
if (!imgPath.isEmpty()) { if (!imgPath.isEmpty()) {
QImage img(imgPath); QImage img(imgPath);
if (!img.isNull()) { if (!img.isNull()) {
m_afterCopyImage = false;
VClipboardUtils::setImageToClipboard(clipboard, img, QClipboard::Clipboard); VClipboardUtils::setImageToClipboard(clipboard, img, QClipboard::Clipboard);
qDebug() << "clipboard copy image via URL" << imgPath; qDebug() << "clipboard copy image via URL" << imgPath;
return; return;
} }
} }
} }
#endif
// Fall back. // Fall back.
triggerPageAction(QWebEnginePage::CopyImageToClipboard); triggerPageAction(QWebEnginePage::CopyImageToClipboard);
@ -255,6 +259,9 @@ void VWebView::handleClipboardChanged(QClipboard::Mode p_mode)
bool removeBackground = m_needRemoveBackground; bool removeBackground = m_needRemoveBackground;
m_needRemoveBackground = false; m_needRemoveBackground = false;
bool afterCopyImage = m_afterCopyImage;
m_afterCopyImage = false;
if (!hasFocus() if (!hasFocus()
|| p_mode != QClipboard::Clipboard) { || p_mode != QClipboard::Clipboard) {
return; return;
@ -266,7 +273,11 @@ void VWebView::handleClipboardChanged(QClipboard::Mode p_mode)
return; return;
} }
alterHtmlMimeData(clipboard, mimeData, removeBackground); if (afterCopyImage) {
removeHtmlFromImageData(clipboard, mimeData);
} else {
alterHtmlMimeData(clipboard, mimeData, removeBackground);
}
} }
bool VWebView::fixImgSrc(QString &p_html) bool VWebView::fixImgSrc(QString &p_html)
@ -323,7 +334,7 @@ void VWebView::alterHtmlMimeData(QClipboard *p_clipboard,
const QMimeData *p_mimeData, const QMimeData *p_mimeData,
bool p_removeBackground) bool p_removeBackground)
{ {
if (!p_mimeData->hasHtml()) { if (!p_mimeData->hasHtml() || p_mimeData->hasImage()) {
return; return;
} }
@ -357,3 +368,18 @@ void VWebView::alterHtmlMimeData(QClipboard *p_clipboard,
VClipboardUtils::setMimeDataToClipboard(p_clipboard, data, QClipboard::Clipboard); VClipboardUtils::setMimeDataToClipboard(p_clipboard, data, QClipboard::Clipboard);
qDebug() << "altered clipboard's Html"; qDebug() << "altered clipboard's Html";
} }
void VWebView::removeHtmlFromImageData(QClipboard *p_clipboard,
const QMimeData *p_mimeData)
{
if (!p_mimeData->hasImage()) {
return;
}
if (p_mimeData->hasHtml()) {
qDebug() << "remove html from image data" << p_mimeData->html();
QMimeData *data = new QMimeData();
data->setImageData(p_mimeData->imageData());
VClipboardUtils::setMimeDataToClipboard(p_clipboard, data, QClipboard::Clipboard);
}
}

View File

@ -44,6 +44,9 @@ private:
bool fixImgSrc(QString &p_html); bool fixImgSrc(QString &p_html);
void removeHtmlFromImageData(QClipboard *p_clipboard,
const QMimeData *p_mimeData);
VFile *m_file; VFile *m_file;
// Whether this view has hooked the Copy Image Url action. // Whether this view has hooked the Copy Image Url action.
@ -52,6 +55,9 @@ private:
bool m_needRemoveBackground; bool m_needRemoveBackground;
bool m_fixImgSrc; bool m_fixImgSrc;
// Whether it is after copy image action.
bool m_afterCopyImage;
}; };
#endif // VWEBVIEW_H #endif // VWEBVIEW_H