src/hbplugins/inputmethods/touchinput/hbinputbasic12keyhandler.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 
       
    26 #include "virtual12key.h"
       
    27 #include <QTimer>
       
    28 #include <hbinputmethod.h>
       
    29 #include <hbinputkeymapfactory.h>
       
    30 #include <hbinputpredictionengine.h>
       
    31 #include <hbinputsettingproxy.h>
       
    32 #include <hbinputpredictionfactory.h>
       
    33 #include "hbinputabstractbase.h"
       
    34 #include "hbinputbasic12keyhandler.h"
       
    35 #include "hbinputbasichandler_p.h"
       
    36 
       
    37 class HbInputBasic12KeyHandlerPrivate: public HbInputBasicHandlerPrivate
       
    38 {
       
    39     Q_DECLARE_PUBLIC(HbInputBasic12KeyHandler)
       
    40 
       
    41 public:
       
    42     HbInputBasic12KeyHandlerPrivate();
       
    43     ~HbInputBasic12KeyHandlerPrivate();
       
    44 
       
    45     void handleAlphaEvent(int buttonId);
       
    46     bool buttonPressed(const QKeyEvent *keyEvent);
       
    47     bool buttonReleased(const QKeyEvent *keyEvent);
       
    48     bool actionHandler(HbInputModeHandler::HbInputModeAction action);
       
    49 
       
    50     void _q_timeout();
       
    51 
       
    52 public:
       
    53     int mLastKey;
       
    54     QChar mCurrentChar;
       
    55     int mNumChr;
       
    56     int mDownKey;
       
    57     HbInputFocusObject *mCurrentlyFocused;
       
    58     bool mLongPressHappened;
       
    59 	bool mShiftKeyDoubleTapped;
       
    60 };
       
    61 
       
    62 HbInputBasic12KeyHandlerPrivate::HbInputBasic12KeyHandlerPrivate()
       
    63 :    mLastKey(0),
       
    64     mCurrentChar(0),
       
    65     mNumChr(0),
       
    66     mDownKey(0),
       
    67     mCurrentlyFocused(0),
       
    68 	mLongPressHappened(false),
       
    69 	mShiftKeyDoubleTapped(false)
       
    70 {
       
    71 }
       
    72 
       
    73 HbInputBasic12KeyHandlerPrivate::~HbInputBasic12KeyHandlerPrivate()
       
    74 {
       
    75 }
       
    76 
       
    77 // handles the key press events.
       
    78 void HbInputBasic12KeyHandlerPrivate::handleAlphaEvent(int buttonId)
       
    79 {
       
    80     Q_Q(HbInputBasic12KeyHandler);
       
    81     HbInputFocusObject *focusObject = 0;
       
    82     focusObject = mInputMethod->focusObject();
       
    83     if (!focusObject) {
       
    84         return;
       
    85     }
       
    86 
       
    87     mCurrentChar = q->getNthCharacterInKey(mNumChr, buttonId);
       
    88 
       
    89 
       
    90     if (mCurrentChar != 0) {
       
    91         QString str;
       
    92         str += mCurrentChar;
       
    93 
       
    94         QList<QInputMethodEvent::Attribute> list;
       
    95         QInputMethodEvent event(str, list);
       
    96         focusObject->sendEvent(event);
       
    97     }
       
    98 }
       
    99 
       
   100 bool HbInputBasic12KeyHandlerPrivate::buttonPressed(const QKeyEvent *keyEvent)
       
   101 {
       
   102     Q_UNUSED(keyEvent);
       
   103     mLongPressHappened = false;
       
   104     HbInputFocusObject *focusObject = 0;
       
   105     focusObject = mInputMethod->focusObject();
       
   106     if (!focusObject) {
       
   107         return false;
       
   108     }
       
   109 	int buttonId = keyEvent->key();
       
   110 	// mark a shift key double tap. This would be handled when the release event is received.
       
   111 	if ( (buttonId == Qt::Key_Shift) && (mLastKey == buttonId) && mTimer->isActive() ) {
       
   112 		mShiftKeyDoubleTapped = true;		
       
   113 	}
       
   114     if (mInputMethod) {
       
   115         if (mLastKey != buttonId)
       
   116         {
       
   117             if(mCurrentChar != 0) {
       
   118                 if (!focusObject->characterAllowedInEditor(mCurrentChar)) {
       
   119 					focusObject->sendCommitString(QString());			
       
   120                 } else {
       
   121                     if (isEnterCharacter(mCurrentChar)) {
       
   122                         focusObject->sendPreEditString(QString("")); // Make sure the enter character is cleared.
       
   123                         mCurrentChar = QChar('\n');                  // Convert enter character to line feed.
       
   124                     }
       
   125                     QChar commitChar(mCurrentChar);
       
   126                     mCurrentChar = 0;
       
   127                     focusObject->filterAndCommitCharacter(commitChar);
       
   128                 }			
       
   129             }			
       
   130         }
       
   131 
       
   132 		mDownKey = buttonId;
       
   133         mTimer->stop();
       
   134 		mTimer->start(HbMultiTapTimerTimeout);
       
   135 	}
       
   136     return false;
       
   137 }
       
   138 
       
   139 /*!
       
   140 Handles the key release events from the VKB. Launches the SCT with key release event of
       
   141 asterisk.
       
   142 */
       
   143 bool HbInputBasic12KeyHandlerPrivate::buttonReleased(const QKeyEvent *keyEvent)
       
   144 {
       
   145     HbInputVkbWidget::HbFlickDirection flickDir = static_cast<HbVirtual12Key*>(mInputMethod)->flickDirection();
       
   146 	if (mInputMethod && flickDir!=HbInputVkbWidget::HbFlickDirectionDown) {
       
   147 		Q_Q(HbInputBasic12KeyHandler);
       
   148 		int buttonId = keyEvent->key();
       
   149 		HbInputFocusObject *focusObject = 0;
       
   150 		focusObject = mInputMethod->focusObject();
       
   151 		if (!focusObject || !mDownKey) {
       
   152 			return false;
       
   153 		}
       
   154 		mDownKey = 0;
       
   155 		if ( mLongPressHappened ){
       
   156 			return false;
       
   157 		}
       
   158 		if(mTimer->isActive() && mLastKey != buttonId) {
       
   159 			mNumChr = 0;
       
   160 
       
   161 
       
   162 			// For QLineEdit it works fine. For HbLineEdit, need to set the state 
       
   163 			// to lower by calling activateState().
       
   164 			// This is needed for the scenario - When automatic text case is true
       
   165 			// click a button and before the multitap timer expires click on
       
   166 			// another button.
       
   167 			// Need to check for shift key : In empty editor, click on editor
       
   168 			// press shift, multitap on a button. The char is entered in upper case.
       
   169 			// It should be entered in lower case.
       
   170             if (Qt::Key_Shift != mLastKey) {
       
   171 			    mInputMethod->updateState();
       
   172             }
       
   173 			refreshAutoCompleter();
       
   174 		}
       
   175 
       
   176 		if (buttonId == Qt::Key_Return) {
       
   177 			mInputMethod->closeKeypad();
       
   178 			mLastKey = buttonId;
       
   179 			return true;
       
   180 		} else if (buttonId == Qt::Key_Shift) {
       
   181             // single tap of shift key toggles prediction status in case insensitive languages
       
   182             if (!HbInputSettingProxy::instance()->globalInputLanguage().isCaseSensitiveLanguage() && 
       
   183                 // when the language does not support prediction in that case we should not update the state and prediction
       
   184                 HbPredictionFactory::instance()->predictionEngineForLanguage(mInputMethod->inputState().language())) {
       
   185                 HbInputSettingProxy::instance()->togglePrediction();
       
   186             } else {
       
   187                 // For single key press, change the text input case. If the second shift key press is 
       
   188 				// received within long key press time out interval, then activate the next state
       
   189                 if (mShiftKeyDoubleTapped){
       
   190                     mShiftKeyDoubleTapped = false;
       
   191                     mTimer->stop();
       
   192                     if( HbInputSettingProxy::instance()->globalInputLanguage() == mInputMethod->inputState().language() ||
       
   193                         HbInputSettingProxy::instance()->globalSecondaryInputLanguage() == mInputMethod->inputState().language() ){
       
   194                         // in latin variants , double tap of shift key toggles the prediction status	
       
   195                         // revert back to the old case as this is a double tap 
       
   196                         // (the case was changed on the single tap)
       
   197                         updateTextCase();
       
   198                         // when the language does not support prediction in that case we should not update the state and prediction
       
   199                         if(HbPredictionFactory::instance()->predictionEngineForLanguage(mInputMethod->inputState().language())) {
       
   200                             HbInputSettingProxy::instance()->togglePrediction();
       
   201                         }
       
   202                     } else {
       
   203 					    // if the global language is different from the input mode language, we should 
       
   204                         // go back to the root state
       
   205                         // e.g. double tap of hash/shift key is used to change 
       
   206                         // to chinese input mode from latin input mode
       
   207                         HbInputState rootState;
       
   208                         mInputMethod->editorRootState(rootState);
       
   209                         mInputMethod->activateState(rootState); 		
       
   210                     }
       
   211                 } else {
       
   212                     updateTextCase();
       
   213                     mTimer->start(HbLongPressTimerTimeout);
       
   214                 }
       
   215             }	
       
   216 			mLastKey = buttonId;
       
   217 			mCurrentChar = 0;
       
   218             return true;
       
   219 		}
       
   220 
       
   221 		// Let's see if we can get the handler for this button in the base class.
       
   222 		if (q->HbInputBasicHandler::filterEvent(keyEvent)) {
       
   223 			mCurrentChar = 0;
       
   224 			return true;
       
   225 		}
       
   226 		mLastKey = buttonId;
       
   227 		handleAlphaEvent(buttonId);
       
   228 
       
   229 		// it was a long press on sct swith button. so just return form here.
       
   230 		if (!mTimer->isActive() && buttonId == Qt::Key_Control) {
       
   231 			return true;
       
   232 		}
       
   233 		if (buttonId == Qt::Key_Asterisk || buttonId == Qt::Key_Control) {
       
   234 			//Same asterisk key is used for launching candidate list (long key press)
       
   235 			//and also for SCT. So, do not launch SCT if candidate list is already launched.            
       
   236 			mInputMethod->switchMode(buttonId);
       
   237 			return true;
       
   238 		}
       
   239 	}
       
   240 	return false;
       
   241 }
       
   242 
       
   243 void HbInputBasic12KeyHandlerPrivate::_q_timeout()
       
   244 {
       
   245     Q_Q(HbInputBasic12KeyHandler);
       
   246     mTimer->stop();
       
   247     mNumChr = 0;
       
   248 
       
   249     HbInputFocusObject *focusedObject = 0;
       
   250     focusedObject = mInputMethod->focusObject();
       
   251     if (!focusedObject) {
       
   252         qDebug("HbInputBasic12KeyHandler::timeout focusObject == 0");
       
   253         return;
       
   254     }
       
   255 
       
   256     if (isEnterCharacter(mCurrentChar)) {
       
   257         focusedObject->sendPreEditString(QString("")); // Make sure the enter character is cleared.
       
   258         mCurrentChar = QChar('\n');                    // Convert enter character to line feed.
       
   259     }
       
   260 
       
   261     //Long key press number key is applicable to all keys
       
   262     if (mDownKey ) {       
       
   263         //switch to numeric mode for long key press of Hash key	
       
   264         if (mDownKey == Qt::Key_Shift) {
       
   265             mInputMethod->switchMode(Qt::Key_Shift);				
       
   266         } else if (mDownKey == Qt::Key_Control ) {
       
   267             mInputMethod->selectSpecialCharacterTableMode();
       
   268             mLongPressHappened = true;
       
   269         } else if (mDownKey == Qt::Key_Asterisk) {
       
   270 				//switch to sct mode for long tap of * key
       
   271 				mInputMethod->switchMode(mDownKey);
       
   272 		} else if (mDownKey != Qt::Key_Delete) {
       
   273             q->commitFirstMappedNumber(mDownKey);
       
   274             mLongPressHappened = true;
       
   275         }
       
   276  	mDownKey = 0;        
       
   277     } else if(mLastKey != Qt::Key_Shift){
       
   278         if (!focusedObject->characterAllowedInEditor(mCurrentChar)) {
       
   279             focusedObject->sendCommitString(QString());
       
   280         } else {
       
   281             if ( mCurrentChar != 0){
       
   282                 focusedObject->filterAndCommitCharacter(mCurrentChar);
       
   283             }
       
   284             mInputMethod->updateState();
       
   285             // pass this character to autocompleter.
       
   286             refreshAutoCompleter();
       
   287         }
       
   288     }
       
   289 
       
   290     mCurrentChar = 0;
       
   291     return;	
       
   292 }
       
   293 
       
   294 bool HbInputBasic12KeyHandlerPrivate::actionHandler(HbInputModeHandler::HbInputModeAction action)
       
   295 {
       
   296     HbInputFocusObject *focusObject = 0;
       
   297     focusObject = mInputMethod->focusObject();
       
   298     if (!focusObject) {
       
   299         return false;
       
   300     }
       
   301 
       
   302     bool ret = true;
       
   303     switch (action) {
       
   304     case HbInputModeHandler::HbInputModeActionReset:
       
   305         mTimer->stop();
       
   306         if (mCurrentChar != 0) {
       
   307             focusObject->filterAndCommitCharacter(mCurrentChar);
       
   308             mCurrentChar = 0;
       
   309         }
       
   310         mLastKey = 0;
       
   311         mNumChr = 0;
       
   312         mDownKey = 0;
       
   313         mShiftKeyDoubleTapped = false;
       
   314     break;        
       
   315 	case HbInputModeHandler::HbInputModeActionCancelButtonPress:
       
   316         mLastKey = 0;
       
   317         mNumChr = 0;
       
   318         mDownKey = 0;
       
   319         mShiftKeyDoubleTapped = false;
       
   320         if (mTimer->isActive()) {
       
   321             mTimer->stop();
       
   322         }
       
   323     break;
       
   324     case HbInputModeHandler::HbInputModeActionDeleteAndCommit:{
       
   325         mTimer->stop();
       
   326         mNumChr = 0;
       
   327         QString empty;
       
   328         if(mInputMethod) {
       
   329             if (mCurrentChar != 0) {
       
   330                 mCurrentChar = 0;
       
   331             } else if (focusObject->editorCursorPosition()){
       
   332                 QList<QInputMethodEvent::Attribute> list;
       
   333                 QInputMethodEvent event(QString(), list);
       
   334                 event.setCommitString(empty, -1, 1);
       
   335                 focusObject->sendEvent(event);
       
   336 				mInputMethod->updateState();            
       
   337 			}
       
   338         } else {
       
   339             // Close event was originated from a button press, remove the uncommitted character.
       
   340             focusObject->sendCommitString(empty);
       
   341         }
       
   342     }
       
   343     break;
       
   344     case HbInputModeHandler::HbInputModeActionFocusRecieved:
       
   345         mTimer->stop();
       
   346         mNumChr = 0;        
       
   347         // set up auto completer
       
   348         setUpAutoCompleter();
       
   349     break;
       
   350     case HbInputModeHandler::HbInputModeActionCommit:
       
   351     case HbInputModeHandler::HbInputModeActionFocusLost:
       
   352         if (mTimer->isActive()) {
       
   353             mTimer->stop();
       
   354             if (mCurrentChar != 0) {
       
   355                 if (isEnterCharacter(mCurrentChar)) {
       
   356                     focusObject->sendPreEditString(QString("")); // Make sure the enter character is cleared.
       
   357                     mCurrentChar = QChar('\n');                    // Convert enter character to line feed.
       
   358                 }
       
   359                 focusObject->filterAndCommitCharacter(mCurrentChar);
       
   360                 mCurrentChar = 0;
       
   361             }
       
   362             mLastKey = 0;
       
   363             mNumChr = 0;
       
   364             mInputMethod->updateState();
       
   365         }
       
   366 		// We should add the commit autocompleting text when focus lost happens
       
   367 		if(HbInputModeHandler::HbInputModeActionFocusLost == action){
       
   368 			if (mCurrentlyFocused != focusObject) {
       
   369 				mCurrentlyFocused = focusObject;
       
   370 				if (mAutoCompleter) {
       
   371 					mAutoCompleter->commit();
       
   372 				}
       
   373 			}
       
   374 		}
       
   375         break;
       
   376     default: {
       
   377         ret = false;
       
   378     }
       
   379     break;
       
   380     };
       
   381 
       
   382     return ret;
       
   383 }
       
   384 
       
   385 
       
   386 HbInputBasic12KeyHandler::HbInputBasic12KeyHandler(HbInputAbstractMethod* inputMethod)
       
   387 :HbInputBasicHandler( *new HbInputBasic12KeyHandlerPrivate, inputMethod)
       
   388 {
       
   389     Q_D(HbInputBasic12KeyHandler);
       
   390     d->q_ptr = this;
       
   391 }
       
   392 
       
   393 HbInputBasic12KeyHandler::~HbInputBasic12KeyHandler()
       
   394 {
       
   395 }
       
   396 
       
   397 /*!
       
   398  filterEvent function for handling different keyevents.
       
   399 */
       
   400 bool HbInputBasic12KeyHandler::filterEvent(const QKeyEvent * event)
       
   401 {
       
   402     Q_D(HbInputBasic12KeyHandler);
       
   403 
       
   404     if (event->type() == QEvent::KeyRelease) {
       
   405         return d->buttonReleased(event);
       
   406     } else {
       
   407         return d->buttonPressed(event);
       
   408     }
       
   409 }
       
   410 
       
   411 /*!
       
   412  returns true if in inline edit.
       
   413 */
       
   414 bool HbInputBasic12KeyHandler::isComposing() const
       
   415 {
       
   416     Q_D(const HbInputBasic12KeyHandler);
       
   417     return d->mTimer->isActive();
       
   418 }
       
   419 
       
   420 /*!
       
   421  list different input modes.
       
   422 */
       
   423 void HbInputBasic12KeyHandler::listInputModes(QVector<HbInputModeProperties>& modes) const
       
   424 {
       
   425     HbInputModeProperties binding;
       
   426     binding.iMode = HbInputModeDefault;
       
   427     binding.iKeyboard = HbKeyboardVirtual12Key;
       
   428 
       
   429     QList<HbInputLanguage> languages = HbKeymapFactory::availableLanguages();
       
   430     foreach (HbInputLanguage language, languages) {
       
   431         binding.iLanguage = language;
       
   432         modes.push_back(binding);
       
   433     }
       
   434 }
       
   435 
       
   436 /*!
       
   437 Action Handler
       
   438 */
       
   439 bool HbInputBasic12KeyHandler::actionHandler(HbInputModeAction action)
       
   440 {
       
   441     Q_D(HbInputBasic12KeyHandler);
       
   442     if (!d->actionHandler(action)) {
       
   443         // no action taken so let's pass it to the base class.
       
   444         return HbInputBasicHandler::actionHandler(action);
       
   445     }
       
   446     return false;
       
   447 }
       
   448 
       
   449 void HbInputBasic12KeyHandler::cursorPositionChanged(int oldPos, int newPos)
       
   450 {
       
   451     Q_D(HbInputBasic12KeyHandler);
       
   452 
       
   453     if (d->mTimer->isActive() && d->mCurrentChar != 0) {
       
   454         QString commitString(d->mCurrentChar);
       
   455         d->mCurrentChar = 0;
       
   456         commitAndUpdate(commitString, oldPos - newPos, 0);
       
   457         d->actionHandler(HbInputModeActionReset);
       
   458     }
       
   459     HbInputModeHandler::cursorPositionChanged(oldPos,newPos);
       
   460 }
       
   461 
       
   462 // EOF