ginebra2/WebTouchNavigation.cpp
branchGCC_SURGE
changeset 8 2e16851ffecd
parent 5 0f2326c2a325
child 16 3c88a81ff781
equal deleted inserted replaced
2:bf4420e9fa4d 8:2e16851ffecd
     1 /*
     1 /*
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     4 *
     5 * under the terms of "Eclipse Public License v1.0"
     5 * This program is free software: you can redistribute it and/or modify
     6 * which accompanies this distribution, and is available
     6 * it under the terms of the GNU Lesser General Public License as published by
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 * the Free Software Foundation, version 2.1 of the License.
     8 *
     8 *
     9 * Initial Contributors:
     9 * This program is distributed in the hope that it will be useful,
    10 * Nokia Corporation - initial contribution.
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11 *
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12 * Contributors:
    12 * GNU Lesser General Public License for more details.
    13 *
    13 *
    14 * Description: 
    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:
    15 *
    19 *
    16 */
    20 */
    17 
       
    18 
    21 
    19 #include <QTimer>
    22 #include <QTimer>
    20 #include <QGraphicsWebView>
    23 #include <QGraphicsWebView>
    21 #include <QWebPage>
    24 #include <QWebPage>
    22 #include <QWebFrame>
    25 #include <QWebFrame>
    50     hide();
    53     hide();
    51 }
    54 }
    52 
    55 
    53 
    56 
    54 WebTouchNavigation::WebTouchNavigation(QGraphicsWebView* view) :
    57 WebTouchNavigation::WebTouchNavigation(QGraphicsWebView* view) :
    55 		 m_view(view),
    58          m_view(view),
    56 		 m_scrollTimer(0)
    59          m_scrollTimer(0)
    57 
    60 
    58 {
    61 {
    59 	m_webPage = m_view->page();
    62     m_webPage = m_view->page();
    60 	m_kinetic = new KineticHelper(this);
    63     m_kinetic = new KineticHelper(this);
    61 	m_decelEdit = new DecelEdit(this);
    64     m_decelEdit = new DecelEdit(this);
    62 }
    65 }
    63 
    66 
    64 WebTouchNavigation::~WebTouchNavigation()
    67 WebTouchNavigation::~WebTouchNavigation()
    65 {
    68 {
    66 	disconnect(m_scrollTimer, SIGNAL(timeout()),this,SLOT(pan()));
    69     disconnect(m_scrollTimer, SIGNAL(timeout()),this,SLOT(pan()));
    67 	delete m_scrollTimer;
    70     delete m_scrollTimer;
    68 	    
    71 
    69 	delete m_kinetic;
    72     delete m_kinetic;
    70 }
    73 }
    71 
    74 
    72 
    75 
    73 void WebTouchNavigation::handleQStmGesture(QStm_Gesture* gesture)
    76 void WebTouchNavigation::handleQStmGesture(QStm_Gesture* gesture)
    74 {
    77 {
    75 	QStm_GestureType type = gesture->getGestureStmType();
    78     QStm_GestureType type = gesture->getGestureStmType();
    76 	
    79 
    77 	switch (type) {
    80     switch (type) {
    78 	    case QStmTapGestureType:
    81         case QStmTapGestureType:
    79 	    {
    82         {
    80 	    	doTap(gesture);
    83             doTap(gesture);
    81 	    	break;
    84             break;
    82 	    }
    85         }
    83 	    case QStmPanGestureType:
    86         case QStmPanGestureType:
    84 	    {
    87         {
    85 	    	doPan(gesture);
    88             doPan(gesture);
    86 	    	break;
    89             break;
    87 	    }
    90         }
    88 	    case QStmFlickGestureType:
    91         case QStmFlickGestureType:
    89 	    {
    92         {
    90 	    	doFlick(gesture);
    93             doFlick(gesture);
    91 	    	break;
    94             break;
    92 	    }
    95         }
    93 	    case QStmDoubleTapGestureType:
    96         case QStmDoubleTapGestureType:
    94 	    {
    97         {
    95 	        m_decelEdit->show();
    98             m_decelEdit->show();
    96 	    	break;
    99             break;
    97 	    }
   100         }
    98 	    case QStmTouchGestureType:
   101         case QStmTouchGestureType:
    99 	    {
   102         {
   100 	    	doTouch(gesture);
   103             doTouch(gesture);
   101 	    	break;
   104             break;
   102 	    }
   105         }
   103 	}
   106     }
   104 }
   107 }
   105 
   108 
   106 void WebTouchNavigation::doTouch(QStm_Gesture* gesture)
   109 void WebTouchNavigation::doTouch(QStm_Gesture* gesture)
   107 {
   110 {
   108 	stopScrolling();
   111     stopScrolling();
   109 	if (m_kinetic->isScrolling()) {
   112     if (m_kinetic->isScrolling()) {
   110 	    m_kinetic->stopScrolling();
   113         m_kinetic->stopScrolling();
   111 	}
   114     }
   112 }
   115 }
   113 
   116 
   114 void WebTouchNavigation::stopScrolling()
   117 void WebTouchNavigation::stopScrolling()
   115 {
   118 {
   116 	if (m_scrollTimer && m_scrollTimer->isActive()) {
   119     if (m_scrollTimer && m_scrollTimer->isActive()) {
   117 	    m_scrollTimer->stop();
   120         m_scrollTimer->stop();
   118 	    m_scrollDelta = QPoint(0,0);
   121         m_scrollDelta = QPoint(0,0);
   119 	}
   122     }
   120 }
   123 }
   121 
   124 
   122 
   125 
   123 void WebTouchNavigation::doFlick(QStm_Gesture* gesture)
   126 void WebTouchNavigation::doFlick(QStm_Gesture* gesture)
   124 {
   127 {
   125 	
   128 
   126 	QPointF pos = mapFromGlobal(gesture->position());
   129     QPointF pos = mapFromGlobal(gesture->position());
   127 	m_kineticSpeed = gesture->getSpeedVec();
   130     m_kineticSpeed = gesture->getSpeedVec();
   128 	QStm_GestureDirection direction = static_cast<QStm_GestureDirection>(gesture->getDirection()); 
   131     QStm_GestureDirection direction = static_cast<QStm_GestureDirection>(gesture->getDirection());
   129 	
   132 
   130 	if (direction == ENorth || direction == ESouth) {
   133     if (direction == ENorth || direction == ESouth) {
   131 		m_kineticSpeed.setY(0.0);
   134         m_kineticSpeed.setY(0.0);
   132 	}
   135     }
   133 	else if (direction == EEast || direction == EWest) {
   136     else if (direction == EEast || direction == EWest) {
   134 		m_kineticSpeed.setX(0.0);
   137         m_kineticSpeed.setX(0.0);
   135 	}
   138     }
   136 	
   139 
   137 	m_kineticSpeed *= -1.0;
   140     m_kineticSpeed *= -1.0;
   138 	m_frame = m_webPage->frameAt(pos.toPoint());
   141     m_frame = m_webPage->frameAt(pos.toPoint());
   139 	m_kinetic->startScrolling();
   142     m_kinetic->startScrolling();
   140 }
   143 }
   141 
   144 
   142 void WebTouchNavigation::doPan(QStm_Gesture* gesture)
   145 void WebTouchNavigation::doPan(QStm_Gesture* gesture)
   143 {
   146 {
   144 	if (gesture->gestureState() == Qt::GestureFinished) {
   147     if (gesture->gestureState() == Qt::GestureFinished) {
   145 		stopScrolling();
   148         stopScrolling();
   146 	}
   149     }
   147 	else {
   150     else {
   148 	  m_scrollDelta += gesture->getLengthAndDirection();
   151       m_scrollDelta += gesture->getLengthAndDirection();
   149 	  Q_ASSERT(m_scrollDelta.x() != 0 || m_scrollDelta.y() != 0);
   152       Q_ASSERT(m_scrollDelta.x() != 0 || m_scrollDelta.y() != 0);
   150 	  QPointF pos = mapFromGlobal(gesture->position());
   153       QPointF pos = mapFromGlobal(gesture->position());
   151 	  m_frame = m_webPage->frameAt(pos.toPoint());
   154       m_frame = m_webPage->frameAt(pos.toPoint());
   152       if(!m_scrollTimer) {
   155       if (!m_scrollTimer) {
   153           m_scrollTimer = new QTimer(this);
   156           m_scrollTimer = new QTimer(this);
   154           m_scrollTimer->setSingleShot(false);
   157           m_scrollTimer->setSingleShot(false);
   155           QObject::connect(m_scrollTimer, SIGNAL(timeout()), this, SLOT(pan()));
   158           QObject::connect(m_scrollTimer, SIGNAL(timeout()), this, SLOT(pan()));
   156       }
   159       }
   157       
   160 
   158       if (!m_scrollTimer->isActive()) {
   161       if (!m_scrollTimer->isActive()) {
   159           m_scrollTimer->stop();
   162           m_scrollTimer->stop();
   160           m_scrollTimer->start(SCROLL_TIMEOUT);
   163           m_scrollTimer->start(SCROLL_TIMEOUT);
   161       }
   164       }
   162 	}
   165     }
   163 }
   166 }
   164 
   167 
   165 void WebTouchNavigation::pan()
   168 void WebTouchNavigation::pan()
   166 {
   169 {
   167 	if (m_scrollDelta.x() != 0 || m_scrollDelta.y() != 0) {
   170     if (m_scrollDelta.x() != 0 || m_scrollDelta.y() != 0) {
   168 	    m_frame->scroll(-m_scrollDelta.x(), m_scrollDelta.y());
   171         m_frame->scroll(-m_scrollDelta.x(), m_scrollDelta.y());
   169 	    m_scrollDelta = QPoint(0,0);
   172         m_scrollDelta = QPoint(0,0);
   170 	}
   173     }
   171 }
   174 }
   172 
   175 
   173 void WebTouchNavigation::doTap(QStm_Gesture* gesture)
   176 void WebTouchNavigation::doTap(QStm_Gesture* gesture)
   174 {
   177 {
   175 	QPoint gpos = gesture->position(); 
   178     QPoint gpos = gesture->position();
   176 	QPoint pos = mapFromGlobal(gpos).toPoint();
   179     QPoint pos = mapFromGlobal(gpos).toPoint();
   177 	Qt::MouseButtons buttons = Qt::LeftButton;        
   180     Qt::MouseButtons buttons = Qt::LeftButton;
   178 	buttons &= ~Qt::RightButton;
   181     buttons &= ~Qt::RightButton;
   179 	buttons &= ~Qt::MidButton; 
   182     buttons &= ~Qt::MidButton;
   180 	buttons &= Qt::MouseButtonMask;
   183     buttons &= Qt::MouseButtonMask;
   181 	
   184 
   182 	QMouseEvent mdown (QEvent::MouseButtonPress, pos, gpos, Qt::LeftButton, buttons, Qt::NoModifier);
   185     QMouseEvent mdown (QEvent::MouseButtonPress, pos, gpos, Qt::LeftButton, buttons, Qt::NoModifier);
   183 	QMouseEvent mup (QEvent::MouseButtonRelease, pos, gpos, Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
   186     QMouseEvent mup (QEvent::MouseButtonRelease, pos, gpos, Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
   184 	
   187 
   185 	m_webPage->event(&mdown);
   188     m_webPage->event(&mdown);
   186 	m_webPage->event(&mup);
   189     m_webPage->event(&mup);
   187 }
   190 }
   188 
   191 
   189 
   192 
   190 
   193 
   191 
   194 
   192 QPointF WebTouchNavigation::mapFromGlobal(const QPointF& gpos)
   195 QPointF WebTouchNavigation::mapFromGlobal(const QPointF& gpos)
   193 {
   196 {
   194 	QGraphicsScene* scene = m_view->scene();
   197     QGraphicsScene* scene = m_view->scene();
   195 	QList<QGraphicsView*> gvList = scene->views();
   198     QList<QGraphicsView*> gvList = scene->views();
   196 	QList<QGraphicsView*>::iterator it;
   199     QList<QGraphicsView*>::iterator it;
   197 	
   200 
   198 	for(it = gvList.begin(); it != gvList.end(); it++) {
   201     for (it = gvList.begin(); it != gvList.end(); it++) {
   199 	    if (static_cast<QGraphicsView*>(*it)->hasFocus()) {
   202         if (static_cast<QGraphicsView*>(*it)->hasFocus()) {
   200 	        QWidget* viewport = static_cast<QGraphicsView*>(*it)->viewport();
   203             QWidget* viewport = static_cast<QGraphicsView*>(*it)->viewport();
   201 	        return m_view->mapFromScene(viewport->mapFromGlobal(gpos.toPoint()));
   204             return m_view->mapFromScene(viewport->mapFromGlobal(gpos.toPoint()));
   202         }
   205         }
   203 	}
   206     }
   204 
   207 
   205 	return QPointF(0.0, 0.0);	
   208     return QPointF(0.0, 0.0);
   206 }
   209 }
   207 
   210 
   208 
   211 
   209 void WebTouchNavigation::scrollTo(QPoint& pos)
   212 void WebTouchNavigation::scrollTo(QPoint& pos)
   210 {
   213 {
   217     return m_frame->scrollPosition();
   220     return m_frame->scrollPosition();
   218 }
   221 }
   219 
   222 
   220 QPoint WebTouchNavigation::getInitialPosition()
   223 QPoint WebTouchNavigation::getInitialPosition()
   221 {
   224 {
   222     return m_frame->scrollPosition();    
   225     return m_frame->scrollPosition();
   223 }
   226 }
   224 
   227 
   225 QPointF WebTouchNavigation::getInitialSpeed()
   228 QPointF WebTouchNavigation::getInitialSpeed()
   226 {
   229 {
   227     return m_kineticSpeed;    
   230     return m_kineticSpeed;
   228 }
   231 }
   229 
   232 
   230 
   233 
   231 
   234 
   232 }
   235 }
   233 
   236