src/hbplugins/inputmethods/hardwareinput/hbhardwareinputpredictionqwertyhandler.cpp
changeset 0 16d8024aca5e
child 1 f7ac710697a9
equal deleted inserted replaced
-1:000000000000 0:16d8024aca5e
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (developer.feedback@nokia.com)
       
     6 **
       
     7 ** This file is part of the HbPlugins module of the UI Extensions for Mobile.
       
     8 **
       
     9 ** GNU Lesser General Public License Usage
       
    10 ** This file may be used under the terms of the GNU Lesser General Public
       
    11 ** License version 2.1 as published by the Free Software Foundation and
       
    12 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
       
    13 ** Please review the following information to ensure the GNU Lesser General
       
    14 ** Public License version 2.1 requirements will be met:
       
    15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    16 **
       
    17 ** In addition, as a special exception, Nokia gives you certain additional
       
    18 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    20 **
       
    21 ** If you have questions regarding the use of this file, please contact
       
    22 ** Nokia at developer.feedback@nokia.com.
       
    23 **
       
    24 ****************************************************************************/
       
    25 #include <QTimer>
       
    26 #include <hbinputmethod.h>
       
    27 #include <hbinputkeymapfactory.h>
       
    28 #include <hbinputsettingproxy.h>
       
    29 
       
    30 #include "hbinputpredictionhandler_p.h"
       
    31 #include "HbHardwareInputPredictionQwertyHandler.h"
       
    32 #include "hbinputabstractbase.h"
       
    33 
       
    34 class HbHardwareInputPredictionQwertyHandlerPrivate: public HbInputPredictionHandlerPrivate
       
    35 {
       
    36     Q_DECLARE_PUBLIC(HbHardwareInputPredictionQwertyHandler)
       
    37 public:
       
    38     HbHardwareInputPredictionQwertyHandlerPrivate();
       
    39     ~HbHardwareInputPredictionQwertyHandlerPrivate();
       
    40 
       
    41 public:
       
    42     bool buttonPressed(const QKeyEvent *event);
       
    43     bool buttonReleased(const QKeyEvent *event);
       
    44     void init();
       
    45     void _q_timeout();
       
    46 
       
    47 public:
       
    48     bool mExactPopupLaunched;
       
    49     HbInputShiftKeyState mShiftKeyState;
       
    50     HbFnState           mFnState;
       
    51     int                 mButton;
       
    52 };
       
    53 
       
    54 HbHardwareInputPredictionQwertyHandlerPrivate::HbHardwareInputPredictionQwertyHandlerPrivate()
       
    55 :   mExactPopupLaunched(false),
       
    56     mShiftKeyState(EKeyPressedNone),
       
    57     mFnState(HbFnOff),
       
    58     mButton(0)
       
    59 {
       
    60 }
       
    61 
       
    62 HbHardwareInputPredictionQwertyHandlerPrivate::~HbHardwareInputPredictionQwertyHandlerPrivate()
       
    63 {
       
    64 }
       
    65 
       
    66 void HbHardwareInputPredictionQwertyHandlerPrivate::init()
       
    67 {
       
    68 }
       
    69 
       
    70 void HbHardwareInputPredictionQwertyHandlerPrivate::_q_timeout()
       
    71 {
       
    72     qDebug("HbHardwareInputPredictionQwertyHandler::timeout called");
       
    73     mTimer->stop();
       
    74     if (mButton == Qt::Key_Shift ){
       
    75         mShiftKeyState = EShiftKeyPressed;
       
    76     } else {        
       
    77         mFnState = HbFnNext;
       
    78     }
       
    79 }
       
    80 
       
    81 
       
    82 bool HbHardwareInputPredictionQwertyHandlerPrivate::buttonReleased(const QKeyEvent *event)
       
    83 {
       
    84     Q_Q(HbHardwareInputPredictionQwertyHandler);
       
    85     HbInputFocusObject *focusObject = 0;
       
    86     focusObject = mInputMethod->focusObject();
       
    87     if (!focusObject) {
       
    88         qDebug("HbVirtualQwerty::virtualButtonClicked : no focused editor widget!");
       
    89         return false;
       
    90     }
       
    91     int key = event->key();
       
    92     
       
    93     // If the timer is not active and it is alpha mode, it is a long press
       
    94     // and handled in another function. So just return.
       
    95     if (mTimer->isActive()) {
       
    96         mTimer->stop();
       
    97         qDebug("Timer stoped");
       
    98     } 
       
    99 
       
   100     bool ret = true;
       
   101     switch(key) {
       
   102     case Qt::Key_Alt:  //Fn
       
   103         if (mFnState == HbFnOff) {
       
   104                 mFnState = HbFnNext;
       
   105             } else if (mFnState == HbFnNext) {
       
   106                 mFnState = HbFnOn;
       
   107             } else {
       
   108                 mFnState = HbFnOff;
       
   109             }
       
   110         break;
       
   111     case Qt::Key_Shift: {
       
   112         mShiftKeyState = EShiftKeyPressed;
       
   113             int currentTextCase = focusObject->editorInterface().textCase();
       
   114             switch(currentTextCase) {
       
   115             case HbTextCaseLower:
       
   116                 focusObject->editorInterface().setTextCase(HbTextCaseUpper);
       
   117                 focusObject->syncEditorInterface();
       
   118                 emit q->setModifiers(HbModifierShiftPressed);
       
   119                 break;
       
   120             case HbTextCaseUpper:
       
   121             case HbTextCaseAutomatic:
       
   122                 focusObject->editorInterface().setTextCase(HbTextCaseLower);
       
   123                 focusObject->syncEditorInterface();
       
   124                 emit q->setModifiers(HbModifierNone);
       
   125                 break;
       
   126             default:
       
   127                 break;
       
   128             }
       
   129         }
       
   130         break;
       
   131     case Qt::Key_Control: { 
       
   132         }
       
   133         break;
       
   134     case Qt::Key_Left:{
       
   135         if(mCandidates->count()) {
       
   136             if (mExactPopupLaunched) {
       
   137                 q->commitAndUpdate(mCandidates->at(1));
       
   138             } else {
       
   139                 q->commitAndUpdate(mCandidates->at(0));
       
   140             }
       
   141         }       
       
   142         HbInputPredictionHandlerPrivate::reset();
       
   143         // close exactword popup.
       
   144         mInputMethod->closeExactWordPopup();
       
   145         break;
       
   146         }
       
   147     case Qt::Key_Right:{
       
   148         q->actionHandler(HbInputModeHandler::HbInputModeActionCommit);
       
   149         break;
       
   150         }
       
   151     case Qt::Key_Up:{
       
   152          if (mExactPopupLaunched) {
       
   153                 q->commitAndUpdate(mCandidates->at(0));
       
   154             } else {
       
   155                 q->actionHandler(HbInputModeHandler::HbInputModeActionLaunchCandidatePopup);
       
   156          }
       
   157         HbInputPredictionHandlerPrivate::reset();   
       
   158         // close exactword popup.
       
   159         mInputMethod->closeExactWordPopup();
       
   160         break;
       
   161         }
       
   162     case Qt::Key_Down:{
       
   163         q->actionHandler(HbInputModeHandler::HbInputModeActionLaunchCandidatePopup);
       
   164         break;
       
   165 		}
       
   166 	case Qt::Key_Space:{
       
   167 		if (event->modifiers() & Qt::ControlModifier){
       
   168 			if (HbInputSettingProxy::instance()->predictiveInputStatus()) {
       
   169 					HbInputSettingProxy::instance()->setPredictiveInputStatus(0);
       
   170 				} else {
       
   171 					HbInputSettingProxy::instance()->setPredictiveInputStatus(1);
       
   172 				}
       
   173 			break;
       
   174 			}
       
   175         }
       
   176     default: {
       
   177             int currentTextCase = focusObject->editorInterface().textCase();
       
   178             mModifiers = Qt::NoModifier;
       
   179             if (mFnState == HbFnNext) {
       
   180                 mModifiers |= Qt::AltModifier;
       
   181                 mFnState = HbFnOff;
       
   182             } else if (mFnState == HbFnOn) {
       
   183                 mModifiers |= Qt::AltModifier;
       
   184             }
       
   185             // If shift is pressed, the shifted characters have to be input.
       
   186             if ( HbTextCaseUpper == currentTextCase || HbTextCaseAutomatic == currentTextCase ) {
       
   187                 mModifiers |= Qt::ShiftModifier;
       
   188             }
       
   189             // let's pass it to the base class.
       
   190             ret = q->HbInputPredictionHandler::filterEvent(event);
       
   191 
       
   192             // Reset the shift state.
       
   193             if (EShiftKeyPressed == mShiftKeyState) {
       
   194                 mShiftKeyState = EKeyPressedNone;
       
   195                 int textCase = HbTextCaseLower;
       
   196                 HbModifier modifier = HbModifierNone;
       
   197 
       
   198                 if (HbTextCaseLower == currentTextCase) {
       
   199                     textCase = HbTextCaseUpper;
       
   200                     modifier = HbModifierShiftPressed;
       
   201                 }
       
   202                 focusObject->editorInterface().setTextCase(textCase);
       
   203                 focusObject->syncEditorInterface();
       
   204                 emit q->setModifiers(modifier);
       
   205             }
       
   206             mInputMethod->updateState();
       
   207         }
       
   208         break;
       
   209     };
       
   210     return ret;
       
   211 }
       
   212 
       
   213 bool HbHardwareInputPredictionQwertyHandlerPrivate::buttonPressed(const QKeyEvent* event)
       
   214 {
       
   215     Q_UNUSED(event);
       
   216     if (!mTimer->isActive()){
       
   217         mTimer->start(HbLongPressTimerTimeout);
       
   218         qDebug("Timer started");
       
   219     }
       
   220     return true;
       
   221 }
       
   222 
       
   223 
       
   224 HbHardwareInputPredictionQwertyHandler::HbHardwareInputPredictionQwertyHandler(HbInputAbstractMethod *inputMethod)
       
   225     :HbInputPredictionHandler(* new HbHardwareInputPredictionQwertyHandlerPrivate, inputMethod)
       
   226 {
       
   227     Q_D(HbHardwareInputPredictionQwertyHandler);
       
   228     d->q_ptr = this;
       
   229     d->init();
       
   230 }
       
   231 
       
   232 HbHardwareInputPredictionQwertyHandler::~HbHardwareInputPredictionQwertyHandler()
       
   233 {
       
   234 }
       
   235 
       
   236 /*!
       
   237 lists different input mode bindings..
       
   238 */
       
   239 void HbHardwareInputPredictionQwertyHandler::listInputModes(QVector<HbInputModeProperties>& modes) const
       
   240 {
       
   241 	Q_UNUSED(modes);
       
   242     /*HbInputModeProperties binding;
       
   243     binding.iMode = HbInputModeLatinPredictive;
       
   244     QList<HbKeyboardType> availableKeyBoards;
       
   245     HbInputSettingProxy::instance()->availableHwKeyboard(availableKeyBoards);
       
   246     foreach(HbKeyboardType keyboardType, availableKeyBoards) {
       
   247         (keyboardType&HbQwertyKeyboardMask) ? binding.iKeyboard = keyboardType : binding.iKeyboard = HbKeyboardNone;
       
   248         if (binding.iKeyboard != HbKeyboardNone) {
       
   249             QVector<int> languages;
       
   250             HbKeyMapFactory::instance()->listLanguages(languages);
       
   251 
       
   252             foreach(int lang, languages) {
       
   253                 if(HbKeyMapFactory::instance()->isKeyboardDataAvailableForLanguage(lang,binding.iKeyboard)){
       
   254                     binding.iLanguage = (QLocale::Language)lang;        
       
   255                     modes.push_front(binding);
       
   256                 }
       
   257             }
       
   258         }
       
   259     }*/
       
   260 }
       
   261 
       
   262 /*!
       
   263 Action Handler.
       
   264 */
       
   265 bool HbHardwareInputPredictionQwertyHandler::actionHandler(HbInputModeAction action)
       
   266 {
       
   267     Q_D(HbHardwareInputPredictionQwertyHandler);
       
   268     bool ret = true;
       
   269     switch (action) {
       
   270         case HbInputModeActionFocusRecieved:
       
   271             HbInputPredictionHandler::actionHandler(HbInputModeActionSetCandidateList);
       
   272             HbInputPredictionHandler::actionHandler(HbInputModeActionSetKeypad);
       
   273             d->mTimer->stop();
       
   274             qDebug("Timer stoped");
       
   275             break;
       
   276         case HbInputModeActionFocusLost:
       
   277             HbInputPredictionHandler::actionHandler(HbInputModeActionFocusLost);
       
   278             if (d->mCandidates->count()) {
       
   279                 if (d->mExactPopupLaunched) {
       
   280                     commitAndUpdate(d->mCandidates->at(1));
       
   281                 } else {
       
   282                     commitAndUpdate(d->mCandidates->at(0));
       
   283                 }
       
   284             }        
       
   285             // close exactword popup.
       
   286             d->mInputMethod->closeExactWordPopup();
       
   287             break;
       
   288         default: ret = HbInputPredictionHandler::actionHandler(action);
       
   289     }
       
   290 
       
   291     return ret;
       
   292 }
       
   293 
       
   294 /*!
       
   295 filterEvent for key event.
       
   296 */
       
   297 bool HbHardwareInputPredictionQwertyHandler::filterEvent(const QKeyEvent * event)
       
   298 {
       
   299     Q_D(HbHardwareInputPredictionQwertyHandler);
       
   300 
       
   301     if (!event->isAutoRepeat()) {
       
   302         if ((event->type() == QEvent::KeyRelease) ) {
       
   303             return d->buttonReleased(event);
       
   304         } else {
       
   305             return d->buttonPressed(event);
       
   306         }
       
   307     }
       
   308 
       
   309     return true;
       
   310 }
       
   311 
       
   312 /*!
       
   313 this SLOT is called when a character on character previe pane is selected.
       
   314 */
       
   315 void HbHardwareInputPredictionQwertyHandler::charFromPreviewSelected(QString character)
       
   316 {
       
   317     if(character.size() > 0) {
       
   318         appendUnicodeCharacter(character[0]);
       
   319     }
       
   320 }
       
   321 
       
   322 /*!
       
   323  this function is called by HbPredictionHandler when HbPredictionHandler encounters a exact word.
       
   324 */
       
   325 void HbHardwareInputPredictionQwertyHandler::processExactWord(QString exactWord)
       
   326 {
       
   327     Q_D(HbHardwareInputPredictionQwertyHandler);
       
   328     if (exactWord.size()) {
       
   329         d->mInputMethod->launchExactWordPopup(exactWord);
       
   330         d->mExactPopupLaunched = true;
       
   331     } else {
       
   332         d->mInputMethod->closeExactWordPopup();
       
   333         d->mExactPopupLaunched = false;
       
   334     }
       
   335 }
       
   336 
       
   337 /*!
       
   338  this slot should be called when exact word popup is closed.
       
   339 */
       
   340 void HbHardwareInputPredictionQwertyHandler::exactWordPopupClosed()
       
   341 {
       
   342     commitExactWord();
       
   343 }
       
   344 void HbHardwareInputPredictionQwertyHandler::sctCharacterSelected(QChar character)
       
   345 {
       
   346     HbInputPredictionHandler::sctCharacterSelected(character);
       
   347 }
       
   348 
       
   349 /*!
       
   350 Returns true if preidciton engine is available and initialized.
       
   351 */
       
   352 bool HbHardwareInputPredictionQwertyHandler::isActive() const
       
   353 { 
       
   354     Q_D(const HbHardwareInputPredictionQwertyHandler);
       
   355     return d->mEngine != 0;
       
   356 }
       
   357 
       
   358 //EOF