minversemousearea.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2011 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 Qt Components project.
  8. **
  9. ** $QT_BEGIN_LICENSE:BSD$
  10. ** You may use this file under the terms of the BSD license as follows:
  11. **
  12. ** "Redistribution and use in source and binary forms, with or without
  13. ** modification, are permitted provided that the following conditions are
  14. ** met:
  15. ** * Redistributions of source code must retain the above copyright
  16. ** notice, this list of conditions and the following disclaimer.
  17. ** * Redistributions in binary form must reproduce the above copyright
  18. ** notice, this list of conditions and the following disclaimer in
  19. ** the documentation and/or other materials provided with the
  20. ** distribution.
  21. ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
  22. ** the names of its contributors may be used to endorse or promote
  23. ** products derived from this software without specific prior written
  24. ** permission.
  25. **
  26. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  29. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  30. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  31. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  32. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  33. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  34. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  37. ** $QT_END_LICENSE$
  38. **
  39. ****************************************************************************/
  40. #include "minversemousearea.h"
  41. #include <QGraphicsScene>
  42. #include <QGraphicsSceneMouseEvent>
  43. // Threshold in QDeclarativeFlickable is 20
  44. static const int FlickThresholdSquare = 400;
  45. MInverseMouseArea::MInverseMouseArea(QDeclarativeItem *parent)
  46. : QDeclarativeItem(parent),
  47. m_pressed(false),
  48. m_enabled(true)
  49. {
  50. }
  51. MInverseMouseArea::~MInverseMouseArea()
  52. {
  53. if (scene())
  54. scene()->removeEventFilter(this);
  55. }
  56. bool MInverseMouseArea::isEnabled() const
  57. {
  58. return m_enabled;
  59. }
  60. void MInverseMouseArea::setEnabled(bool enabled)
  61. {
  62. if (m_enabled != enabled) {
  63. if (!enabled)
  64. m_pressed = false;
  65. m_enabled = enabled;
  66. emit enabledChanged();
  67. }
  68. }
  69. QVariant MInverseMouseArea::itemChange(GraphicsItemChange change, const QVariant &value)
  70. {
  71. switch (change) {
  72. case QGraphicsItem::ItemSceneChange: {
  73. QGraphicsScene *oldScene = scene();
  74. if (oldScene)
  75. oldScene->removeEventFilter(this);
  76. m_pressed = false;
  77. if (value.canConvert<QGraphicsScene *>()) {
  78. QGraphicsScene *newScene = value.value<QGraphicsScene *>();
  79. if (newScene)
  80. newScene->installEventFilter(this);
  81. }
  82. break;
  83. }
  84. case QGraphicsItem::ItemVisibleHasChanged: {
  85. if (!isVisible())
  86. m_pressed = false;
  87. break;
  88. }
  89. default:
  90. break;
  91. }
  92. return QDeclarativeItem::itemChange(change, value);
  93. }
  94. bool MInverseMouseArea::isClickedOnSoftwareInputPanel(QGraphicsSceneMouseEvent *event) const
  95. {
  96. QGraphicsItem * item = scene()->itemAt(event->scenePos());
  97. while(item != NULL) {
  98. QDeclarativeItem * declItem = dynamic_cast<QDeclarativeItem *>(item);
  99. if(declItem != NULL && declItem->objectName() == "softwareInputPanel")
  100. return true;
  101. item = item->parentItem();
  102. }
  103. return false;
  104. }
  105. bool MInverseMouseArea::eventFilter(QObject *obj, QEvent *ev)
  106. {
  107. Q_UNUSED(obj);
  108. if (!m_enabled || !isVisible())
  109. return false;
  110. switch (ev->type()) {
  111. case QEvent::GraphicsSceneMousePress: {
  112. QGraphicsSceneMouseEvent *me = static_cast<QGraphicsSceneMouseEvent *>(ev);
  113. QPointF mappedPos = mapToRootItem(me->scenePos());
  114. m_pressed = !contains(mapFromScene(me->scenePos())) && !isClickedOnSoftwareInputPanel(me);
  115. if (m_pressed)
  116. emit pressedOutside(mappedPos.x(), mappedPos.y());
  117. break;
  118. }
  119. case QEvent::GraphicsSceneMouseMove: {
  120. if (m_pressed) {
  121. QGraphicsSceneMouseEvent *me = static_cast<QGraphicsSceneMouseEvent *>(ev);
  122. const QPointF &dist = me->scenePos() - me->buttonDownScenePos(Qt::LeftButton);
  123. if (dist.x() * dist.x() + dist.y() * dist.y() > FlickThresholdSquare)
  124. m_pressed = false;
  125. }
  126. break;
  127. }
  128. case QEvent::GraphicsSceneMouseRelease: {
  129. QGraphicsSceneMouseEvent *me = static_cast<QGraphicsSceneMouseEvent *>(ev);
  130. QPointF mappedPos = mapToRootItem(me->scenePos());
  131. if (m_pressed) {
  132. m_pressed = false;
  133. emit clickedOutside(mappedPos.x(), mappedPos.y());
  134. }
  135. break;
  136. }
  137. default:
  138. break;
  139. }
  140. return false;
  141. }
  142. QPointF MInverseMouseArea::mapToRootItem(QPointF pos) {
  143. QPointF mappedPos = pos;
  144. QDeclarativeItem *rootItem = parentItem();
  145. while (rootItem->parentItem()) {
  146. if (rootItem->objectName() == "windowContent") {
  147. break;
  148. }
  149. rootItem = rootItem->parentItem();
  150. }
  151. if (rootItem) {
  152. mappedPos = rootItem->mapFromScene(pos);
  153. }
  154. return mappedPos;
  155. }