src/hbplugins/inputmethods/common/hbinputpredictionhandler.cpp
changeset 7 923ff622b8b9
parent 6 c3690ec91ef8
child 21 4633027730f5
child 34 ed14f46c0e55
equal deleted inserted replaced
6:c3690ec91ef8 7:923ff622b8b9
    44 HbInputPredictionHandlerPrivate::HbInputPredictionHandlerPrivate()
    44 HbInputPredictionHandlerPrivate::HbInputPredictionHandlerPrivate()
    45     :mEngine(0),
    45     :mEngine(0),
    46     mCandidates(0),
    46     mCandidates(0),
    47     mBestGuessLocation(0),
    47     mBestGuessLocation(0),
    48     mShowTail(true),
    48     mShowTail(true),
    49     mTailShowing(false),
       
    50     mAutoAddedSpace(true),
    49     mAutoAddedSpace(true),
    51     mCanContinuePrediction(true),
    50     mCanContinuePrediction(true),
    52     mShowTooltip(true),
    51     mShowTooltip(true),
    53     mSpellQueryDialog(0)
    52     mSpellQueryDialog(0)
    54 {
    53 {
    62     }
    61     }
    63 }
    62 }
    64 
    63 
    65 void HbInputPredictionHandlerPrivate::deleteOneCharacter()
    64 void HbInputPredictionHandlerPrivate::deleteOneCharacter()
    66 {
    65 {
       
    66     if (!mEngine && !mInputMethod->focusObject()) {
       
    67         return;
       
    68     }
    67     mShowTail = true;
    69     mShowTail = true;
    68     mShowTooltip = true;
    70     mShowTooltip = true;
    69     // A backspace in predictive means updating the engine for the delete key press
    71     // A backspace in predictive means updating the engine for the delete key press
    70     // and get the new candidate list from the engine.
    72     // and get the new candidate list from the engine.
    71     if ( mEngine->inputLength() >= 1 ) {
    73     if ( mEngine->inputLength() >= 1 ) {
    72         //Only autocomplition part should be deleted when autocompliton part is enable and user pressed a delete key
    74         int tailLength =  mInputMethod->focusObject()->preEditString().length() - mEngine->inputLength();
    73         if(false == mTailShowing) {
    75         //Only autocomplition part should be deleted when autocompliton part is shown and user pressed a delete key
       
    76         if(tailLength <= 0) { // no autocompletion part displayed
    74             mEngine->deleteKeyPress( this );
    77             mEngine->deleteKeyPress( this );
    75         }
    78         }
    76         //To prevent showing autocompletion part while deleting the characters using backspace key
    79         //To prevent showing autocompletion part while deleting the characters using backspace key
    77         mShowTail = false;
    80         mShowTail = false;
    78         mShowTooltip = false;
    81         mShowTooltip = false;
   216                     list.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, mEngine->inputLength(), taillength, gray));
   219                     list.append(QInputMethodEvent::Attribute(QInputMethodEvent::TextFormat, mEngine->inputLength(), taillength, gray));
   217                 }
   220                 }
   218 				list.append(QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, mEngine->inputLength(), 0, 0));
   221 				list.append(QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, mEngine->inputLength(), 0, 0));
   219                 QInputMethodEvent event(mCandidates->at(mBestGuessLocation), list);
   222                 QInputMethodEvent event(mCandidates->at(mBestGuessLocation), list);
   220                 focusedObject->sendEvent(event);
   223                 focusedObject->sendEvent(event);
   221                 mTailShowing = true;
       
   222             } else {
   224             } else {
   223 				list.append(QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, mCandidates->at(mBestGuessLocation).length(), 0, 0));
   225 				list.append(QInputMethodEvent::Attribute(QInputMethodEvent::Cursor, mCandidates->at(mBestGuessLocation).length(), 0, 0));
   224                 QInputMethodEvent event(mCandidates->at(mBestGuessLocation).left(mEngine->inputLength()), list);
   226                 QInputMethodEvent event(mCandidates->at(mBestGuessLocation).left(mEngine->inputLength()), list);
   225                 focusedObject->sendEvent(event);
   227                 focusedObject->sendEvent(event);
   226                 mTailShowing = false;
       
   227             }
   228             }
   228             if (mShowTooltip && mBestGuessLocation > 0 && mCandidates->at(0).mid(0, mEngine->inputLength()) \
   229             if (mShowTooltip && mBestGuessLocation > 0 && mCandidates->at(0).mid(0, mEngine->inputLength()) \
   229                 != mCandidates->at(mBestGuessLocation).mid(0, mEngine->inputLength())) {                
   230                 != mCandidates->at(mBestGuessLocation).mid(0, mEngine->inputLength())) {                
   230                     q->processExactWord(mCandidates->at(0));
   231                     q->processExactWord(mCandidates->at(0));
   231             } else {
   232             } else {
   271                 deleteOneCharacter();
   272                 deleteOneCharacter();
   272             }
   273             }
   273 		}
   274 		}
   274         ret = true;
   275         ret = true;
   275         break;
   276         break;
   276     case Qt::Key_Period: // TODO: better handling for punctuation
   277     
   277     case Qt::Key_Comma: { // Need to take fn, shift etc. in account
       
   278             HbModifier modifier = HbModifierNone;
       
   279             int currentTextCase = focusObject->editorInterface().textCase();
       
   280             if ( HbTextCaseUpper == currentTextCase || HbTextCaseAutomatic == currentTextCase ) {
       
   281                 modifier = HbModifierShiftPressed;
       
   282             }
       
   283             QString qc;
       
   284             const HbMappedKey* mappedKey = mKeymap->keyForKeycode(mInputMethod->inputState().keyboard(), event->key());
       
   285 
       
   286             if (mappedKey) {
       
   287                 if (modifier == HbModifierNone) {
       
   288                     qc = mappedKey->characters(HbModifierNone).left(1);
       
   289                 } else if (modifier == HbModifierShiftPressed) {
       
   290                     qc = mappedKey->characters(HbModifierShiftPressed).left(1);
       
   291                 }
       
   292             }
       
   293 
       
   294             if (mEngine->inputLength() == 0) {
       
   295                 QList<QInputMethodEvent::Attribute> list;
       
   296                 QInputMethodEvent event(QString(), list);
       
   297                 if (mAutoAddedSpace) {
       
   298                     int cursorPos = mInputMethod->focusObject()->inputMethodQuery(Qt::ImCursorPosition).toInt();
       
   299                     QString text = mInputMethod->focusObject()->inputMethodQuery(Qt::ImSurroundingText).toString();
       
   300                     if (cursorPos > 0 && text.at(cursorPos-1).isSpace()) {
       
   301                         event.setCommitString(qc, -1, 1);
       
   302                     } else {
       
   303                         event.setCommitString(qc);
       
   304                     }
       
   305                 } else {
       
   306                     event.setCommitString(qc);
       
   307                 }
       
   308                 mAutoAddedSpace = false;
       
   309                 q->sendAndUpdate(event);
       
   310             } else {
       
   311                 // Fix for input stopping after ,. keys in qwerty predictive
       
   312                 commitAndAppendCharacter(qc.at(0));
       
   313                 QString empty;
       
   314                 q->processExactWord(empty);
       
   315             }
       
   316             ret = true;
       
   317         }
       
   318         break;
       
   319     case HbInputButton::ButtonKeyCodeEnter:
   278     case HbInputButton::ButtonKeyCodeEnter:
   320     case HbInputButton::ButtonKeyCodeSpace:
   279     case HbInputButton::ButtonKeyCodeSpace:
   321     case Qt::Key_0: {//Space
   280     
       
   281         {
   322             // A space means we have to commit the candidates when we are in predictive mode.
   282             // A space means we have to commit the candidates when we are in predictive mode.
   323             QChar qc(event->key());
   283             QChar qc(event->key());
   324             if (qc == Qt::Key_Enter) {
   284             if (qc == Qt::Key_Enter) {
   325                 qc = QChar('\n');  // Editor expects normal line feed.
   285                 qc = QChar('\n');  // Editor expects normal line feed.
   326             } else if (qc == Qt::Key_0) {
   286             } 
   327                 qc = QChar(' ');
       
   328             }
       
   329             commitAndAppendCharacter(qc);
   287             commitAndAppendCharacter(qc);
   330             // if exact word popup functionality is on then we should inform exact word popup
   288             // if exact word popup functionality is on then we should inform exact word popup
   331             // about the space.//++TODO
   289             // about the space.//++TODO
   332             QString empty;
   290             QString empty;
   333             q->processExactWord(empty);
   291             q->processExactWord(empty);
   441         mEngine->clear();
   399         mEngine->clear();
   442     }
   400     }
   443     if (mCandidates) {
   401     if (mCandidates) {
   444         mCandidates->clear();
   402         mCandidates->clear();
   445     }
   403     }
   446 
       
   447     mTailShowing = false;
       
   448 }
   404 }
   449 
   405 
   450 void HbInputPredictionHandlerPrivate::commit()
   406 void HbInputPredictionHandlerPrivate::commit()
   451 {
   407 {
   452     if (mEngine && mEngine->inputLength() > 0 && mCandidates->count() > 0) {
   408     if (mEngine && mEngine->inputLength() > 0 && mCandidates->count() > 0) {
   509         mEngine->clear();
   465         mEngine->clear();
   510     }
   466     }
   511 
   467 
   512     //Enable the flag after commit
   468     //Enable the flag after commit
   513     mCanContinuePrediction = true;
   469     mCanContinuePrediction = true;
   514 	mTailShowing = false;
       
   515 }
   470 }
   516 
   471 
   517 /*!
   472 /*!
   518 This function accepts a QInputMethodEvent and commits the event to editor. This also clears all the key presses and
   473 This function accepts a QInputMethodEvent and commits the event to editor. This also clears all the key presses and
   519 candidates from prediction engine
   474 candidates from prediction engine
   538         mEngine->clear();
   493         mEngine->clear();
   539     }
   494     }
   540 
   495 
   541     //Enable the flag after commit
   496     //Enable the flag after commit
   542     mCanContinuePrediction = true;
   497     mCanContinuePrediction = true;
   543 	mTailShowing = false;
       
   544 
       
   545 }
   498 }
   546 
   499 
   547 void HbInputPredictionHandlerPrivate::commitExactWord()
   500 void HbInputPredictionHandlerPrivate::commitExactWord()
   548 {
   501 {
   549     if (mEngine && mEngine->inputLength() /*> 0 && mCandidates->count() > 0*/) {
   502     if (mEngine && mEngine->inputLength() /*> 0 && mCandidates->count() > 0*/) {