telutils/dialpad/src/dialpadmultitaphandler.cpp
branchRCL_3
changeset 19 7d48bed6ce0c
equal deleted inserted replaced
18:594d59766373 19:7d48bed6ce0c
       
     1 /*!
       
     2 * Copyright (c) 2009 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: Dialpad multitap handler
       
    15 *
       
    16 */
       
    17 
       
    18 #include <QKeyEvent>
       
    19 #include <hbapplication.h>
       
    20 #include <hblineedit.h>
       
    21 #include "dialpadmultitaphandler.h"
       
    22 
       
    23 static const int DialpadMaxMultitapTime = 800; // ms
       
    24 static const int DialpadAsteriskMultitapCount = 4;
       
    25 static const int DialpadAsteriskMultitapChars[DialpadAsteriskMultitapCount] =
       
    26     {Qt::Key_Asterisk, Qt::Key_Plus, Qt::Key_P, Qt::Key_W};
       
    27 
       
    28 
       
    29 DialpadMultitapHandler::DialpadMultitapHandler(
       
    30     HbLineEdit& editor,
       
    31     QObject* parent) :
       
    32     QObject(parent),
       
    33     mEditor(editor),
       
    34     mAsteriskMultitapIndex(0),
       
    35     mGeneratedAsterisk(false)
       
    36 {
       
    37     // extra characters
       
    38     mExtraChar.insert(Qt::Key_Asterisk, '*');
       
    39     mExtraChar.insert(Qt::Key_Plus, '+');
       
    40     mExtraChar.insert(Qt::Key_P, 'p');
       
    41     mExtraChar.insert(Qt::Key_W, 'w');
       
    42 }
       
    43 
       
    44 DialpadMultitapHandler::~DialpadMultitapHandler()
       
    45 {
       
    46 }
       
    47 
       
    48 bool DialpadMultitapHandler::eventFilter(QObject *watched, QEvent *event)
       
    49 {
       
    50     if (watched!=&mEditor) {
       
    51         return false;
       
    52     }
       
    53 
       
    54     int type = event->type();
       
    55 
       
    56     if (type==QEvent::KeyPress) {
       
    57         QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
       
    58         if (isNumericKey(keyEvent->key()) &&
       
    59             keyEvent->text().isEmpty() ) {
       
    60             return true;
       
    61         }
       
    62     }
       
    63 
       
    64     bool keyEventEaten = false;
       
    65 
       
    66     if (type==QEvent::KeyPress) {
       
    67         QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
       
    68 
       
    69         if (keyEvent->key()==Qt::Key_Asterisk) {
       
    70             if ((mMultitapTimer.elapsed() <= DialpadMaxMultitapTime) &&
       
    71                 !mGeneratedAsterisk) {
       
    72                 // remove the last char by generated backspace
       
    73                 QKeyEvent backspaceEvent(QEvent::KeyPress,
       
    74                                          Qt::Key_Backspace,
       
    75                                          Qt::NoModifier, "");
       
    76                 HbApplication::sendEvent(&mEditor,&backspaceEvent);
       
    77 
       
    78                 // generate +,p,w,* character
       
    79                 mAsteriskMultitapIndex =
       
    80                     ++mAsteriskMultitapIndex % DialpadAsteriskMultitapCount;
       
    81 
       
    82                 int key = DialpadAsteriskMultitapChars[mAsteriskMultitapIndex];
       
    83 
       
    84                 // Allow + character only as a first char in editor
       
    85                 int cursorPosition = mEditor.cursorPosition();
       
    86                 if(cursorPosition != 0 && key == Qt::Key_Plus) {
       
    87                     mAsteriskMultitapIndex = ++mAsteriskMultitapIndex;
       
    88                     Q_ASSERT(mAsteriskMultitapIndex < DialpadAsteriskMultitapCount);
       
    89                     key = DialpadAsteriskMultitapChars[mAsteriskMultitapIndex];
       
    90                 }
       
    91                 
       
    92                 QKeyEvent generatedEvent(QEvent::KeyPress, key,
       
    93                                          Qt::KeypadModifier,
       
    94                                          mExtraChar.value(key));
       
    95                 if (key==Qt::Key_Asterisk) {
       
    96                     mGeneratedAsterisk = true;
       
    97                 }
       
    98 
       
    99                 HbApplication::sendEvent(&mEditor,&generatedEvent);
       
   100                 keyEventEaten = true;
       
   101                 mMultitapTimer.start();
       
   102             } else {
       
   103                 mMultitapTimer.start();
       
   104                 mAsteriskMultitapIndex = 0;
       
   105             }
       
   106 
       
   107             mGeneratedAsterisk = false;
       
   108         }
       
   109     }
       
   110 
       
   111     return keyEventEaten;
       
   112 }
       
   113 
       
   114 bool DialpadMultitapHandler::isNumericKey(int key)
       
   115 {
       
   116     if (key==Qt::Key_Yes || key==Qt::Key_Backspace) {
       
   117         return false;
       
   118     } else {
       
   119         return true;
       
   120     }
       
   121 }