/src/ui/web_event_delegate.cpp

https://github.com/linuxdeepin/deepin-appstore · C++ · 127 lines · 90 code · 16 blank · 21 comment · 3 complexity · 44345a394600c551bc8cbd7412b069f1 MD5 · raw file

  1. /*
  2. * Copyright (C) 2017 ~ 2018 Deepin Technology Co., Ltd.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "ui/web_event_delegate.h"
  18. #include <QDebug>
  19. #include <qcef_web_page.h>
  20. #include <QtGui/QDesktopServices>
  21. namespace dstore {
  22. namespace {
  23. enum MenuIds {
  24. // Normal navigation.
  25. MenuBack = QCefContextMenu::MENU_ID_USER_FIRST,
  26. MenuForward,
  27. MenuReload,
  28. MenuStop,
  29. // Editable.
  30. MenuUndo,
  31. MenuRedo,
  32. MenuCut,
  33. MenuCopy,
  34. MenuPaste,
  35. MenuDelete,
  36. MenuSelectAll,
  37. // Link.
  38. MenuOpenLinkInNewTab,
  39. MenuCopyLinkAddress,
  40. };
  41. } // namespace
  42. WebEventDelegate::WebEventDelegate(QObject* parent) : QObject(parent) {
  43. this->setObjectName("WebEventDelegate");
  44. }
  45. WebEventDelegate::~WebEventDelegate() {
  46. }
  47. bool WebEventDelegate::onBeforeBrowse(const QUrl& url, bool is_redirect) {
  48. return QCefBrowserEventDelegate::onBeforeBrowse(url, is_redirect);
  49. }
  50. void WebEventDelegate::onBeforeContextMenu(
  51. QCefWebPage* web_page,
  52. QCefContextMenu* menu,
  53. const QCefContextMenuParams& params) {
  54. QCefBrowserEventDelegate::onBeforeContextMenu(web_page, menu, params);
  55. auto type_flags = params.getTypeFlags();
  56. if (type_flags & QCEF_CM_FLAG_EDITABLE) {
  57. // Editable menu.
  58. auto state = params.getEditStateFlags();
  59. menu->addItem(MenuIds::MenuUndo, QObject::tr("Undo"),
  60. state & QCEF_CM_EDITFLAG_CAN_UNDO,
  61. [](QCefWebPage* page) {
  62. page->undo();
  63. });
  64. menu->addItem(MenuIds::MenuRedo, QObject::tr("Redo"),
  65. state & QCEF_CM_EDITFLAG_CAN_REDO,
  66. [](QCefWebPage* page) {
  67. page->redo();
  68. });
  69. menu->addSeparator();
  70. menu->addItem(MenuIds::MenuCut, QObject::tr("Cut"),
  71. state & QCEF_CM_EDITFLAG_CAN_CUT,
  72. [](QCefWebPage* page) {
  73. page->cut();
  74. });
  75. menu->addItem(MenuIds::MenuCopy, QObject::tr("Copy"),
  76. state & QCEF_CM_EDITFLAG_CAN_COPY,
  77. [](QCefWebPage* page) {
  78. page->copy();
  79. });
  80. menu->addItem(MenuIds::MenuPaste, QObject::tr("Paste"),
  81. state & QCEF_CM_EDITFLAG_CAN_PASTE,
  82. [](QCefWebPage* page) {
  83. page->paste();
  84. });
  85. menu->addItem(MenuIds::MenuDelete, QObject::tr("Delete"),
  86. state & QCEF_CM_EDITFLAG_CAN_DELETE,
  87. [](QCefWebPage* page) {
  88. page->doDelete();
  89. });
  90. menu->addSeparator();
  91. menu->addItem(MenuIds::MenuSelectAll, QObject::tr("Select all"),
  92. state & QCEF_CM_EDITFLAG_CAN_SELECT_ALL,
  93. [](QCefWebPage* page) {
  94. page->selectAll();
  95. });
  96. } else if (type_flags & QCEF_CM_FLAG_SELECTION) {
  97. // Support copy text
  98. auto state = params.getEditStateFlags();
  99. menu->addItem(MenuIds::MenuCopy, QObject::tr("Copy"),
  100. state & QCEF_CM_EDITFLAG_CAN_COPY,
  101. [](QCefWebPage* page) {
  102. page->copy();
  103. });
  104. }
  105. }
  106. bool WebEventDelegate::onBeforePopup(const QUrl& url,
  107. QCefWindowOpenDisposition disposition) {
  108. QDesktopServices::openUrl(url);
  109. return QCefBrowserEventDelegate::onBeforePopup(url, disposition);
  110. }
  111. } // namespace dstore