ginebra2/Charms/ExternalEventCharm.cpp
changeset 9 b39122337a00
child 16 3c88a81ff781
equal deleted inserted replaced
7:a1f515018ac1 9:b39122337a00
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
       
     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 Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not,
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:
       
    19 */
       
    20 
       
    21 #include "ExternalEventCharm.h"
       
    22 
       
    23 namespace GVA {
       
    24 
       
    25 const QString ExternalEventCharm::s_mouseClick = "MouseClick";
       
    26 static const int KLongPressThreshold = 30;
       
    27 
       
    28 inline QGraphicsScene *ExternalEventCharm::scene() {
       
    29     QGraphicsObject *item = static_cast<QGraphicsObject*>(m_object);
       
    30     return item->scene();
       
    31 }
       
    32 
       
    33 ExternalEventCharm::ExternalEventCharm(QGraphicsObject *object)
       
    34   : ObjectCharm(object),
       
    35     m_pressed(false)
       
    36 {
       
    37 }
       
    38 
       
    39 bool ExternalEventCharm::eventFilter(QObject *object, QEvent *event) {
       
    40     checkForExternalEvent(this, event);
       
    41 
       
    42     switch (event->type()) {
       
    43       case QEvent::Show:
       
    44       {
       
    45           scene()->installEventFilter(this);
       
    46           break;
       
    47       }
       
    48       case QEvent::Hide:
       
    49       {
       
    50           scene()->removeEventFilter(this);
       
    51           break;
       
    52       }
       
    53       default: break;
       
    54     }
       
    55 
       
    56     // standard event processing
       
    57     return object->eventFilter(object, event);
       
    58 }
       
    59 
       
    60 void ExternalEventCharm::checkForExternalEvent(QObject * o, QEvent * e)
       
    61 {
       
    62     Q_UNUSED(o);
       
    63     bool emitClick = false;
       
    64 
       
    65     switch (e->type()) {
       
    66         case QEvent::GraphicsSceneMousePress: {
       
    67             QGraphicsSceneMouseEvent * me = static_cast<QGraphicsSceneMouseEvent*>(e);
       
    68             m_pressPos = me->scenePos();
       
    69             m_pressed = true;
       
    70             break;
       
    71         }
       
    72         case QEvent::GraphicsSceneMouseRelease: {
       
    73             // See if we need to send a mouse-click signal.
       
    74             // TODO: When gesture handling is implemented this should be replaced.
       
    75             if(m_pressed) {
       
    76                 QGraphicsSceneMouseEvent * me = static_cast<QGraphicsSceneMouseEvent*>(e);
       
    77                 QPointF diff = m_pressPos - me->scenePos();
       
    78                 if(diff.manhattanLength() < KLongPressThreshold) {
       
    79                     emitClick = true;
       
    80                 }
       
    81                 m_pressed = false;
       
    82             }
       
    83             break;
       
    84         }
       
    85         case QEvent::GraphicsSceneMouseDoubleClick:
       
    86             m_pressed = false;
       
    87             break;
       
    88         default:
       
    89             // Ignore other events.
       
    90             return;
       
    91     }
       
    92 
       
    93     QGraphicsSceneMouseEvent * me = static_cast<QGraphicsSceneMouseEvent*>(e);
       
    94     QGraphicsObject *item = static_cast<QGraphicsObject*>(m_object);
       
    95     QRectF itemGeometry = item->sceneBoundingRect();
       
    96 
       
    97     if (!itemGeometry.contains(me->scenePos())) {
       
    98         if(emitClick)
       
    99             emit externalMouseEvent(me, s_mouseClick, "");
       
   100         else
       
   101             emitExternalEvent(e);
       
   102     }
       
   103 }
       
   104 
       
   105 void ExternalEventCharm::emitExternalEvent(QEvent * e)
       
   106 {
       
   107     QString description;
       
   108 
       
   109     QDebug stream(&description);
       
   110     stream << e;
       
   111 
       
   112     QString name = description;
       
   113     name.truncate(name.indexOf('('));
       
   114 
       
   115     emit externalMouseEvent(e, name, description.trimmed());
       
   116 }
       
   117 
       
   118 
       
   119 }   // end GVA namepace