/*
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:
*
*/

#include <QGraphicsScene>
#include <QGraphicsSceneMouseEvent>
#include <QGesture>
#include <QTouchEvent>

#include <HbTapGesture>
#include <HbPanGesture>

#include "hswidgettoucharea.h"
#include "hswidgethost.h"
#include "hsscene.h"

HsWidgetTouchArea::HsWidgetTouchArea(HsWidgetHost *widgetHost)
  : HbTouchArea(widgetHost),
    mWidgetHost(widgetHost)
{
    grabGesture(Qt::TapAndHoldGesture);
}
  
HsWidgetTouchArea::~HsWidgetTouchArea()
{
}

bool HsWidgetTouchArea::sceneEvent(QEvent *event)
{
    HsScene *scene = HsScene::instance();
    switch (event->type()) {
        case QEvent::TouchBegin:
        case QEvent::GraphicsSceneMousePress:
            emit scene->widgetTapStarted(mWidgetHost);
            break;        
        case QEvent::TouchEnd:
            {
                ungrabGesture(Qt::PanGesture);
                ungrabMouse();
                QPointF scenePos = static_cast<QTouchEvent *>(event)->touchPoints().first().scenePos();
                emit scene->widgetMoveFinished(scenePos, mWidgetHost);
            }
            break;
        case QEvent::GraphicsSceneMouseRelease:
            {
                ungrabGesture(Qt::PanGesture);
                ungrabMouse();
                QPointF scenePos = static_cast<QGraphicsSceneMouseEvent *>(event)->scenePos();
                emit scene->widgetMoveFinished(scenePos, mWidgetHost);
            }
            break;    
        default:
            break;
    }

    return HbTouchArea::sceneEvent(event);
}

QPainterPath HsWidgetTouchArea::shape() const
{       
    return mWidgetHost->shape();
}

void HsWidgetTouchArea::gestureEvent(QGestureEvent *event)
{
    HsScene *scene = HsScene::instance();
 
    // Tap-and-hold gesture.
    QGesture *gesture = event->gesture(Qt::TapAndHoldGesture);
    if (gesture) {
        if (gesture->state() == Qt::GestureFinished) {
            grabGesture(Qt::PanGesture);
            grabMouse();
            emit scene->widgetTapAndHoldFinished(event, mWidgetHost);
        }
        return;
    }

    // Pan gesture.
    gesture = event->gesture(Qt::PanGesture);
    if (gesture) {
        QPointF scenePos = event->mapToGraphicsScene(gesture->hotSpot());
        switch (gesture->state()) {
            case Qt::GestureStarted:
            case Qt::GestureUpdated:
                emit scene->widgetMoveUpdated(scenePos, mWidgetHost);
                break;
            case Qt::GestureCanceled:
            case Qt::GestureFinished:
                ungrabGesture(Qt::PanGesture);
                emit scene->widgetMoveFinished(scenePos, mWidgetHost);
                break;
            default:
                break;
        }
        return;
    }
}
