vwebview.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. #include "vwebview.h"
  2. #include <QMenu>
  3. #include <QPoint>
  4. #include <QContextMenuEvent>
  5. #include <QWebEnginePage>
  6. #include <QAction>
  7. #include <QList>
  8. #include <QClipboard>
  9. #include <QMimeData>
  10. #include <QApplication>
  11. #include <QImage>
  12. #include <QFileInfo>
  13. #include "vfile.h"
  14. #include "utils/vclipboardutils.h"
  15. #include "utils/viconutils.h"
  16. #include "vconfigmanager.h"
  17. #include "utils/vwebutils.h"
  18. #include "utils/vutils.h"
  19. extern VConfigManager *g_config;
  20. extern VWebUtils *g_webUtils;
  21. // We set the property of the clipboard to mark that the URL copied in the
  22. // clipboard has been altered.
  23. static const QString c_ClipboardPropertyMark = "CopiedImageURLAltered";
  24. VWebView::VWebView(VFile *p_file, QWidget *p_parent)
  25. : QWebEngineView(p_parent),
  26. m_file(p_file),
  27. m_copyImageUrlActionHooked(false),
  28. m_afterCopyImage(false),
  29. m_inPreview(false)
  30. {
  31. setAcceptDrops(false);
  32. connect(QApplication::clipboard(), &QClipboard::changed,
  33. this, &VWebView::handleClipboardChanged);
  34. }
  35. void VWebView::contextMenuEvent(QContextMenuEvent *p_event)
  36. {
  37. QMenu *menu = page()->createStandardContextMenu();
  38. menu->setToolTipsVisible(true);
  39. const QList<QAction *> actions = menu->actions();
  40. #if defined(Q_OS_WIN)
  41. if (!m_copyImageUrlActionHooked) {
  42. // "Copy Image URL" action will put the encoded URL to the clipboard as text
  43. // and the URL as URLs. If the URL contains Chinese, OneNote or Word could not
  44. // recognize it.
  45. // We need to change it to only-space-encoded text.
  46. QAction *copyImageUrlAct = pageAction(QWebEnginePage::CopyImageUrlToClipboard);
  47. if (actions.contains(copyImageUrlAct)) {
  48. connect(copyImageUrlAct, &QAction::triggered,
  49. this, &VWebView::handleCopyImageUrlAction);
  50. m_copyImageUrlActionHooked = true;
  51. qDebug() << "hooked CopyImageUrl action" << copyImageUrlAct;
  52. }
  53. }
  54. #endif
  55. if (!hasSelection()
  56. && !m_inPreview
  57. && m_file
  58. && m_file->isModifiable()) {
  59. QAction *editAct= new QAction(VIconUtils::menuIcon(":/resources/icons/edit_note.svg"),
  60. tr("&Edit"), menu);
  61. editAct->setToolTip(tr("Edit current note"));
  62. connect(editAct, &QAction::triggered,
  63. this, &VWebView::handleEditAction);
  64. menu->insertAction(actions.isEmpty() ? NULL : actions[0], editAct);
  65. // actions does not contain editAction.
  66. if (!actions.isEmpty()) {
  67. menu->insertSeparator(actions[0]);
  68. }
  69. }
  70. // Add Copy As menu.
  71. QAction *copyAct = pageAction(QWebEnginePage::Copy);
  72. if (actions.contains(copyAct) && !m_inPreview) {
  73. initCopyAsMenu(copyAct, menu);
  74. }
  75. // We need to replace the "Copy Image" action:
  76. // - the default one use the fully-encoded URL to fetch the image while
  77. // Windows seems to not recognize it.
  78. // - We need to remove the html to let it be recognized by some web pages.
  79. QAction *defaultCopyImageAct = pageAction(QWebEnginePage::CopyImageToClipboard);
  80. if (actions.contains(defaultCopyImageAct)) {
  81. QAction *copyImageAct = new QAction(defaultCopyImageAct->text(), menu);
  82. copyImageAct->setToolTip(defaultCopyImageAct->toolTip());
  83. connect(copyImageAct, &QAction::triggered,
  84. this, &VWebView::copyImage);
  85. menu->insertAction(defaultCopyImageAct, copyImageAct);
  86. defaultCopyImageAct->setVisible(false);
  87. }
  88. if (!hasSelection() && !m_inPreview) {
  89. QAction *savePageAct = new QAction(QWebEnginePage::tr("Save &Page"), menu);
  90. connect(savePageAct, &QAction::triggered,
  91. this, &VWebView::requestSavePage);
  92. menu->addAction(savePageAct);
  93. }
  94. // Add Copy All As menu.
  95. if (!m_inPreview) {
  96. initCopyAllAsMenu(menu);
  97. }
  98. hideUnusedActions(menu);
  99. p_event->accept();
  100. bool valid = false;
  101. for (auto act : menu->actions()) {
  102. if (act->isVisible()) {
  103. valid = true;
  104. break;
  105. }
  106. }
  107. if (valid) {
  108. menu->exec(p_event->globalPos());
  109. }
  110. delete menu;
  111. }
  112. void VWebView::handleEditAction()
  113. {
  114. emit editNote();
  115. }
  116. void VWebView::copyImage()
  117. {
  118. #if defined(Q_OS_WIN)
  119. Q_ASSERT(m_copyImageUrlActionHooked);
  120. // triggerPageAction(QWebEnginePage::CopyImageUrlToClipboard) will not really
  121. // trigger the corresponding action. It just do the stuff directly.
  122. QAction *copyImageUrlAct = pageAction(QWebEnginePage::CopyImageUrlToClipboard);
  123. copyImageUrlAct->trigger();
  124. QCoreApplication::processEvents();
  125. QClipboard *clipboard = QApplication::clipboard();
  126. if (clipboard->property(c_ClipboardPropertyMark.toLatin1()).toBool()) {
  127. const QMimeData *mimeData = clipboard->mimeData();
  128. QString imgPath;
  129. if (mimeData->hasUrls()) {
  130. QList<QUrl> urls = mimeData->urls();
  131. if (!urls.isEmpty() && urls[0].isLocalFile()) {
  132. imgPath = urls[0].toLocalFile();
  133. }
  134. }
  135. if (!imgPath.isEmpty()) {
  136. QImage img = VUtils::imageFromFile(imgPath);
  137. if (!img.isNull()) {
  138. m_afterCopyImage = false;
  139. VClipboardUtils::setImageToClipboard(clipboard, img, QClipboard::Clipboard);
  140. qDebug() << "clipboard copy image via URL" << imgPath;
  141. return;
  142. }
  143. }
  144. }
  145. #endif
  146. m_afterCopyImage = true;
  147. // Fall back.
  148. triggerPageAction(QWebEnginePage::CopyImageToClipboard);
  149. }
  150. void VWebView::handleCopyImageUrlAction()
  151. {
  152. // To avoid failure of setting clipboard mime data.
  153. QCoreApplication::processEvents();
  154. QClipboard *clipboard = QApplication::clipboard();
  155. const QMimeData *mimeData = clipboard->mimeData();
  156. clipboard->setProperty(c_ClipboardPropertyMark.toLatin1(), false);
  157. if (clipboard->ownsClipboard()
  158. && mimeData->hasText()
  159. && mimeData->hasUrls()) {
  160. QString text = mimeData->text();
  161. QList<QUrl> urls = mimeData->urls();
  162. if (urls.size() == 1
  163. && urls[0].isLocalFile()
  164. && urls[0].toEncoded() == text) {
  165. QString spaceOnlyText = urls[0].toString(QUrl::EncodeSpaces);
  166. if (spaceOnlyText != text) {
  167. // Set new mime data.
  168. QMimeData *data = new QMimeData();
  169. data->setUrls(urls);
  170. data->setText(spaceOnlyText);
  171. VClipboardUtils::setMimeDataToClipboard(clipboard, data, QClipboard::Clipboard);
  172. clipboard->setProperty(c_ClipboardPropertyMark.toLatin1(), true);
  173. qDebug() << "clipboard copy image URL altered" << spaceOnlyText;
  174. }
  175. }
  176. }
  177. }
  178. void VWebView::hideUnusedActions(QMenu *p_menu)
  179. {
  180. QList<QAction *> unusedActions;
  181. // QWebEnginePage uses different actions of Back/Forward/Reload.
  182. // [Woboq](https://code.woboq.org/qt5/qtwebengine/src/webenginewidgets/api/qwebenginepage.cpp.html#1652)
  183. // We tell these three actions by name.
  184. const QStringList actionNames({QWebEnginePage::tr("&Back"),
  185. QWebEnginePage::tr("&Forward"),
  186. QWebEnginePage::tr("&Reload")});
  187. const QList<QAction *> actions = p_menu->actions();
  188. for (auto it : actions) {
  189. if (actionNames.contains(it->text())) {
  190. unusedActions.append(it);
  191. }
  192. }
  193. // ViewSource.
  194. QAction *act = pageAction(QWebEnginePage::ViewSource);
  195. unusedActions.append(act);
  196. // DownloadImageToDisk.
  197. act = pageAction(QWebEnginePage::DownloadImageToDisk);
  198. unusedActions.append(act);
  199. // DownloadLinkToDisk.
  200. act = pageAction(QWebEnginePage::DownloadLinkToDisk);
  201. unusedActions.append(act);
  202. for (auto it : unusedActions) {
  203. if (it) {
  204. it->setVisible(false);
  205. }
  206. }
  207. }
  208. bool VWebView::removeStyles(QString &p_html)
  209. {
  210. bool changed = false;
  211. const QStringList &styles = g_config->getStylesToRemoveWhenCopied();
  212. if (styles.isEmpty()) {
  213. return changed;
  214. }
  215. QRegExp tagReg("(<[^>]+\\sstyle=[^>]*>)");
  216. int pos = 0;
  217. while (pos < p_html.size()) {
  218. int idx = p_html.indexOf(tagReg, pos);
  219. if (idx == -1) {
  220. break;
  221. }
  222. QString styleStr = tagReg.cap(1);
  223. QString alteredStyleStr = styleStr;
  224. QString regPatt("(\\s|\")%1:[^;]+;");
  225. for (auto const & sty : styles) {
  226. QRegExp reg(regPatt.arg(sty));
  227. alteredStyleStr.replace(reg, "\\1");
  228. }
  229. pos = idx + tagReg.matchedLength();
  230. if (styleStr != alteredStyleStr) {
  231. pos = pos + alteredStyleStr.size() - styleStr.size();
  232. p_html.replace(idx, tagReg.matchedLength(), alteredStyleStr);
  233. changed = true;
  234. }
  235. }
  236. return changed;
  237. }
  238. void VWebView::handleClipboardChanged(QClipboard::Mode p_mode)
  239. {
  240. if (!hasFocus()
  241. || p_mode != QClipboard::Clipboard) {
  242. return;
  243. }
  244. QClipboard *clipboard = QApplication::clipboard();
  245. if (!clipboard->ownsClipboard()) {
  246. return;
  247. }
  248. const QMimeData *mimeData = clipboard->mimeData();
  249. QString copyTarget = m_copyTarget;
  250. m_copyTarget.clear();
  251. if (m_afterCopyImage) {
  252. m_afterCopyImage = false;
  253. removeHtmlFromImageData(clipboard, mimeData);
  254. } else {
  255. alterHtmlMimeData(clipboard, mimeData, copyTarget);
  256. }
  257. }
  258. void VWebView::alterHtmlMimeData(QClipboard *p_clipboard,
  259. const QMimeData *p_mimeData,
  260. const QString &p_copyTarget)
  261. {
  262. if (!p_mimeData->hasHtml()
  263. || p_mimeData->hasImage()
  264. || p_copyTarget.isEmpty()) {
  265. return;
  266. }
  267. QString html = p_mimeData->html();
  268. if (!g_webUtils->alterHtmlAsTarget(url(), html, p_copyTarget)) {
  269. return;
  270. }
  271. // Set new mime data.
  272. QMimeData *data = VClipboardUtils::cloneMimeData(p_mimeData);
  273. data->setHtml(html);
  274. VClipboardUtils::setMimeDataToClipboard(p_clipboard, data, QClipboard::Clipboard);
  275. qDebug() << "altered clipboard's Html";
  276. }
  277. void VWebView::removeHtmlFromImageData(QClipboard *p_clipboard,
  278. const QMimeData *p_mimeData)
  279. {
  280. if (!p_mimeData->hasImage()) {
  281. return;
  282. }
  283. if (p_mimeData->hasHtml()) {
  284. qDebug() << "remove html from image data" << p_mimeData->html();
  285. QMimeData *data = new QMimeData();
  286. data->setImageData(p_mimeData->imageData());
  287. VClipboardUtils::setMimeDataToClipboard(p_clipboard, data, QClipboard::Clipboard);
  288. }
  289. }
  290. void VWebView::initCopyAsMenu(QAction *p_after, QMenu *p_menu)
  291. {
  292. QStringList targets = g_webUtils->getCopyTargetsName();
  293. if (targets.isEmpty()) {
  294. return;
  295. }
  296. QMenu *subMenu = new QMenu(tr("Copy As"), p_menu);
  297. subMenu->setToolTipsVisible(true);
  298. for (auto const & target : targets) {
  299. QAction *act = new QAction(target, subMenu);
  300. act->setData(target);
  301. act->setToolTip(tr("Copy selected content using rules specified by target %1").arg(target));
  302. subMenu->addAction(act);
  303. }
  304. connect(subMenu, &QMenu::triggered,
  305. this, &VWebView::handleCopyAsAction);
  306. QAction *menuAct = p_menu->insertMenu(p_after, subMenu);
  307. p_menu->removeAction(p_after);
  308. p_menu->insertAction(menuAct, p_after);
  309. }
  310. void VWebView::handleCopyAsAction(QAction *p_act)
  311. {
  312. if (!p_act) {
  313. return;
  314. }
  315. m_copyTarget = p_act->data().toString();
  316. triggerPageAction(QWebEnginePage::Copy);
  317. }
  318. void VWebView::initCopyAllAsMenu(QMenu *p_menu)
  319. {
  320. QStringList targets = g_webUtils->getCopyTargetsName();
  321. if (targets.isEmpty()) {
  322. return;
  323. }
  324. QMenu *subMenu = new QMenu(tr("Copy All As"), p_menu);
  325. subMenu->setToolTipsVisible(true);
  326. for (auto const & target : targets) {
  327. QAction *act = new QAction(target, subMenu);
  328. act->setData(target);
  329. act->setToolTip(tr("Copy all content using rules specified by target %1").arg(target));
  330. subMenu->addAction(act);
  331. }
  332. connect(subMenu, &QMenu::triggered,
  333. this, &VWebView::handleCopyAllAsAction);
  334. p_menu->addSeparator();
  335. p_menu->addMenu(subMenu);
  336. }
  337. void VWebView::handleCopyAllAsAction(QAction *p_act)
  338. {
  339. if (!p_act) {
  340. return;
  341. }
  342. triggerPageAction(QWebEnginePage::SelectAll);
  343. m_copyTarget = p_act->data().toString();
  344. triggerPageAction(QWebEnginePage::Copy);
  345. triggerPageAction(QWebEnginePage::Unselect);
  346. }