qwebviewselectionsuppressor.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the examples of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** No Commercial Usage
  11. ** This file contains pre-release code and may not be distributed.
  12. ** You may use this file in accordance with the terms and conditions
  13. ** contained in the Technology Preview License Agreement accompanying
  14. ** this package.
  15. **
  16. ** GNU Lesser General Public License Usage
  17. ** Alternatively, this file may be used under the terms of the GNU Lesser
  18. ** General Public License version 2.1 as published by the Free Software
  19. ** Foundation and appearing in the file LICENSE.LGPL included in the
  20. ** packaging of this file. Please review the following information to
  21. ** ensure the GNU Lesser General Public License version 2.1 requirements
  22. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  23. **
  24. ** In addition, as a special exception, Nokia gives you certain additional
  25. ** rights. These rights are described in the Nokia Qt LGPL Exception
  26. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  27. **
  28. ** If you have questions regarding the use of this file, please contact
  29. ** Nokia at qt-info@nokia.com.
  30. **
  31. **
  32. **
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #ifndef QWEBVIEWSELECTIONSUPPRESSOR_H
  42. #define QWEBVIEWSELECTIONSUPPRESSOR_H
  43. #include <QtWebKit/qwebview.h>
  44. #include <QtGui/qevent.h>
  45. class QWebViewSelectionSuppressor : public QObject
  46. {
  47. Q_OBJECT
  48. public:
  49. QWebViewSelectionSuppressor(QWebView *v)
  50. : QObject(v), view(v), enabled(false), mousePressed(false)
  51. {
  52. Q_ASSERT(view);
  53. enable();
  54. }
  55. inline void enable()
  56. {
  57. if (enabled)
  58. return;
  59. view->installEventFilter(this);
  60. enabled = true;
  61. }
  62. inline void disable()
  63. {
  64. if (!enabled)
  65. return;
  66. view->removeEventFilter(this);
  67. enabled = false;
  68. }
  69. inline bool isEnabled() const
  70. {
  71. return enabled;
  72. }
  73. protected:
  74. inline bool eventFilter(QObject *, QEvent *e);
  75. private:
  76. QWebView *view;
  77. bool enabled;
  78. bool mousePressed;
  79. };
  80. bool QWebViewSelectionSuppressor::eventFilter(QObject *, QEvent *e)
  81. {
  82. switch (e->type()) {
  83. case QEvent::MouseButtonPress:
  84. if (static_cast<QMouseEvent *>(e)->button() == Qt::LeftButton)
  85. mousePressed = true;
  86. break;
  87. case QEvent::MouseButtonRelease:
  88. if (static_cast<QMouseEvent *>(e)->button() == Qt::LeftButton)
  89. mousePressed = false;
  90. break;
  91. case QEvent::MouseMove:
  92. if (mousePressed)
  93. return true;
  94. break;
  95. default:
  96. break;
  97. }
  98. return false;
  99. }
  100. #endif