qstmgesturelib/qstmutils.cpp
changeset 0 1450b09d0cfd
child 3 0954f5dd2cd0
equal deleted inserted replaced
-1:000000000000 0:1450b09d0cfd
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   Gesture recognition utilities
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "qstmutils.h"
       
    20 #include "qstmgesturedefs.h"
       
    21 
       
    22 using namespace qstmUiEventEngine;
       
    23 
       
    24 inline int QStm_Utils::longerEdge( const QSize& size )
       
    25 {
       
    26 	return MAX( size.height(), size.width() );
       
    27 }
       
    28 
       
    29 
       
    30 QRect QStm_Utils::toleranceRectMm( const QPoint& centerPoint, int size_mm )
       
    31 {
       
    32 	long toleranceLength = mm2Pixels(size_mm) / 2;
       
    33 	return toleranceRectPx(centerPoint, toleranceLength);
       
    34 }
       
    35 
       
    36 QRect QStm_Utils::toleranceRectPx( const QPoint& centerPoint, int size_px )
       
    37 {
       
    38 	QRect toleranceRect( QPoint(0,0), QSize(size_px, size_px) );
       
    39 	// grow by the tolerance length, while keeping the center point
       
    40 	toleranceRect.moveCenter(centerPoint);
       
    41 	return toleranceRect;
       
    42 }
       
    43 
       
    44 
       
    45 long QStm_Utils::mm2Pixels(long mm)
       
    46 {
       
    47 	//return Twips2Pixels(mm * KTwipsInMm);
       
    48 	return mm * QApplication::desktop()->height() / QApplication::desktop()->heightMM();
       
    49 }
       
    50 
       
    51 long QStm_Utils::inches2Pixels(double inches)
       
    52 	{
       
    53 	//return Twips2Pixels(inches * KTwipsInInch);
       
    54 	return inches * 25.4 * QApplication::desktop()->height() / QApplication::desktop()->heightMM();
       
    55 	}
       
    56 
       
    57 int QStm_Utils::distance(const QPoint& p1, const QPoint& p2)
       
    58 {
       
    59 /*
       
    60 	double d = dx * dx + dy * dy;
       
    61 	double dist ;
       
    62 	Math::Sqrt(dist, d) ;
       
    63 	return dist;
       
    64 */
       
    65 	QPoint diff = p1 - p2;
       
    66 	int x = diff.x();
       
    67 	int y = diff.y();
       
    68 	
       
    69 	if(x<0) x=-x;
       
    70 	if(y<0) y=-y;
       
    71 	if(x < y) {
       
    72 	  int t = x;
       
    73 	  x = y;
       
    74 	  y = t;        // ensures that x >= y
       
    75 	}
       
    76 	int dist = (y < ((13107 * x)>>15)) ?    // * (.4)
       
    77 			  (x + ((y * 6310)>>15)) :      // * (.192582403)
       
    78 			  (((x * 27926)>>15)            // * (.852245894)
       
    79 				 + ((y * 18414)>>15));      // * (.561967668)
       
    80 	return dist;
       
    81 }
       
    82 
       
    83 
       
    84 qreal QStm_Utils::distanceF(const QPoint& p1, const QPoint& p2)
       
    85 {
       
    86 	QPoint diff = p1 - p2;
       
    87 	double dist;
       
    88 	SQRT(dist, ((qreal)(diff.x() * diff.x() + diff.y() * diff.y()))) ;
       
    89 	return dist;
       
    90 }
       
    91