src/hbplugins/inputmethods/common/hbinputbasichandler.cpp
changeset 0 16d8024aca5e
child 2 06ff229162e9
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 <hbinputkeymap.h>
       
    27 #include <hbinputpredictionfactory.h>
       
    28 
       
    29 #include "hbinputbasichandler_p.h"
       
    30 #include "hbinputpredictionengine.h"
       
    31 #include "hbinputabstractbase.h"
       
    32 
       
    33 HbInputBasicHandlerPrivate::HbInputBasicHandlerPrivate()
       
    34 :mAutoCompleter(0)
       
    35 {
       
    36 }
       
    37 
       
    38 HbInputBasicHandlerPrivate::~HbInputBasicHandlerPrivate()
       
    39 {
       
    40 }
       
    41 
       
    42 void HbInputBasicHandlerPrivate::init()
       
    43 {
       
    44 }
       
    45 
       
    46 void HbInputBasicHandlerPrivate::setUpAutoCompleter()
       
    47 {
       
    48     HbInputFocusObject *focusObject = 0;
       
    49     focusObject = mInputMethod->focusObject();
       
    50     if (!focusObject) {
       
    51         return;
       
    52     }
       
    53     // Check if this is auto completion field and set it up if it is.
       
    54     if (focusObject->editorInterface().constraints() & HbEditorConstraintAutoCompletingField) {
       
    55         if (!mAutoCompleter) {
       
    56             mAutoCompleter = HbPredictionFactory::instance()->createEngine(HbAutoCompleteVendorIdString);
       
    57         }
       
    58 
       
    59         mInputMethod->closeAutoCompletionPopup();
       
    60 
       
    61         if (mAutoCompleter) {
       
    62             mAutoCompleter->setExtraUserDictionary(mInputMethod->focusObject()->editorInterface().extraDictionaryId());
       
    63         }
       
    64     }
       
    65 }
       
    66 
       
    67 void HbInputBasicHandlerPrivate::refreshAutoCompleter()
       
    68 {
       
    69     HbInputFocusObject *focusObject = 0;
       
    70     focusObject = mInputMethod->focusObject();
       
    71 
       
    72     if (!focusObject) {
       
    73         return;
       
    74     }
       
    75 
       
    76     if (focusObject->editorInterface().constraints() & HbEditorConstraintAutoCompletingField &&
       
    77         mAutoCompleter) {
       
    78         mAutoCompleter->setWord(focusObject->editorSurroundingText());
       
    79         mInputMethod->launchAutoCompletionPopup(mAutoCompleter->candidateList());
       
    80     }
       
    81 }
       
    82 
       
    83 void HbInputBasicHandlerPrivate::deleteCharacterInAutoCompleter()
       
    84 {
       
    85      refreshAutoCompleter();
       
    86 }
       
    87 
       
    88 void HbInputBasicHandlerPrivate::addWordInAutoCompleter()
       
    89 {
       
    90     if (mAutoCompleter) {
       
    91         mAutoCompleter->commit();
       
    92     }
       
    93 }
       
    94 
       
    95 void HbInputBasicHandlerPrivate::autoCompletionPopupClosed(QString currentCandidate, int closingKey)
       
    96 {
       
    97     Q_UNUSED(closingKey);
       
    98     Q_Q(HbInputBasicHandler);
       
    99 
       
   100     HbInputFocusObject *focusObject = 0;
       
   101     focusObject = mInputMethod->focusObject();
       
   102     if (!focusObject) {
       
   103         return;
       
   104     }
       
   105 
       
   106     if (focusObject->editorInterface().constraints() & HbEditorConstraintAutoCompletingField) {
       
   107         if (mAutoCompleter) {
       
   108             int inputLength = mAutoCompleter->inputLength();
       
   109 
       
   110             mAutoCompleter->clear();
       
   111             QString current = currentCandidate;
       
   112             q->commitAndUpdate(current, -inputLength, inputLength);
       
   113             mAutoCompleter->addUsedWord(current);
       
   114         }
       
   115     }
       
   116 }
       
   117 
       
   118 
       
   119 HbInputBasicHandler::HbInputBasicHandler(HbInputAbstractMethod* inputMethod)
       
   120 :HbInputModeHandler(*new HbInputBasicHandlerPrivate(), inputMethod)
       
   121 {
       
   122     Q_D(HbInputBasicHandler);
       
   123     d->q_ptr = this;
       
   124     d->init();
       
   125 }
       
   126 
       
   127 HbInputBasicHandler::HbInputBasicHandler(HbInputBasicHandlerPrivate &dd, HbInputAbstractMethod* inputMethod)
       
   128 :HbInputModeHandler(dd, inputMethod)
       
   129 {
       
   130     Q_D(HbInputBasicHandler);
       
   131     d->q_ptr = this;
       
   132     d->init();
       
   133 }
       
   134 
       
   135 
       
   136 HbInputBasicHandler::~HbInputBasicHandler()
       
   137 {
       
   138 }
       
   139 
       
   140 /*!
       
   141     This function handles different keyevents.
       
   142 */
       
   143 bool HbInputBasicHandler::filterEvent(const QKeyEvent *event)
       
   144 {
       
   145     Q_D(HbInputBasicHandler);
       
   146     HbInputFocusObject *focusObject = 0;
       
   147     focusObject = d->mInputMethod->focusObject();
       
   148     if (!focusObject) {
       
   149         qDebug("HbInputBasicHandler::filterEvent no focusObject ... failed!!");
       
   150         return false;
       
   151     }
       
   152 
       
   153     bool ret = true;
       
   154     switch (event->key()) {
       
   155     case Qt::Key_Backspace:
       
   156     case Qt::Key_Delete: {
       
   157         QKeyEvent keyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier);
       
   158         sendAndUpdate(keyEvent);
       
   159         // pass event to auto completer.
       
   160         deleteCharacterInAutoCompleter();
       
   161         // return false since the event is sent forward
       
   162         ret = false;
       
   163         break;
       
   164     }
       
   165     case Qt::Key_Return:
       
   166     case Qt::Key_Enter:
       
   167     case Qt::Key_Space: {
       
   168         QChar qc(event->key());
       
   169         if (qc == Qt::Key_Enter || qc == Qt::Key_Return) {
       
   170             qc = QChar('\n');  // Editor expects normal line feed.
       
   171         }
       
   172         commitAndUpdate(qc);
       
   173         }
       
   174         break;
       
   175     case Qt::Key_Period:
       
   176     case Qt::Key_Comma: {
       
   177         QString qc(event->key());
       
   178         HbModifier modifier = HbModifierNone;
       
   179         int currentTextCase = focusObject->editorInterface().textCase();
       
   180         if ( HbTextCaseUpper == currentTextCase || HbTextCaseAutomatic == currentTextCase ) {
       
   181             modifier = HbModifierShiftPressed;
       
   182         }
       
   183         // If shift is pressed, the shifted characters should
       
   184         // be input.
       
   185         const HbMappedKey* mappedKey = d->mKeymap->keyForKeycode(d->mInputMethod->inputState().keyboard(), event->key());
       
   186         if (mappedKey) {
       
   187             qc = mappedKey->characters(modifier).left(1);
       
   188         }
       
   189         commitAndUpdate(qc);
       
   190         break;
       
   191     }
       
   192     default:
       
   193         ret = HbInputModeHandler::filterEvent(event);
       
   194         break;
       
   195     }
       
   196     return ret;
       
   197 }
       
   198 
       
   199 /*!
       
   200  Action Handler for latin basic.
       
   201 */
       
   202 bool HbInputBasicHandler::actionHandler(HbInputModeAction action)
       
   203 {
       
   204     return HbInputModeHandler::actionHandler(action);
       
   205 }
       
   206 
       
   207 /*!
       
   208 Commits the candidate upon closing of the candidate list. Now can the closing key be anything other than
       
   209 just selection. Need to evaluate this. When the candidate list is visible, the current implementation of the
       
   210 candidate list does not allow the virtual keypad to be clicked.
       
   211 */
       
   212 void HbInputBasicHandler::autoCompletionPopupClosed(QString currentCandidate, int closingKey)
       
   213 {
       
   214     Q_D(HbInputBasicHandler);
       
   215     d->autoCompletionPopupClosed(currentCandidate, closingKey);
       
   216 }
       
   217 
       
   218 /*!
       
   219  Sets up autocompleter
       
   220 */
       
   221 void HbInputBasicHandler::setUpAutoCompleter()
       
   222 {
       
   223     Q_D(HbInputBasicHandler);
       
   224     d->setUpAutoCompleter();
       
   225 }
       
   226 
       
   227 /*!
       
   228  Appends a character in to the autocompleter engine and shows the popup with the updated list.
       
   229 */
       
   230 void HbInputBasicHandler::refreshAutoCompleter()
       
   231 {
       
   232     Q_D(HbInputBasicHandler);
       
   233     d->refreshAutoCompleter();
       
   234 }
       
   235 
       
   236 /*!
       
   237  Deletes a character and shows the popup with the new list.
       
   238 */
       
   239 void HbInputBasicHandler::deleteCharacterInAutoCompleter()
       
   240 {
       
   241     Q_D(HbInputBasicHandler);
       
   242     d->deleteCharacterInAutoCompleter();
       
   243 }
       
   244 
       
   245 /*!
       
   246  Adds a word to the autocompleter engine.
       
   247 */
       
   248 void HbInputBasicHandler::addWordInAutoCompleter()
       
   249 {
       
   250     Q_D(HbInputBasicHandler);
       
   251     d->addWordInAutoCompleter();
       
   252 }
       
   253 
       
   254 /*!
       
   255 Call-back implementation to indicate that a character was selected from the SCT. With this, the character is committed to the
       
   256 editor and editor is again made to focus.
       
   257 */
       
   258 void HbInputBasicHandler::sctCharacterSelected(QString character)
       
   259 {       
       
   260     HbInputModeHandler::sctCharacterSelected(character);
       
   261     refreshAutoCompleter();
       
   262 }
       
   263 
       
   264 void HbInputBasicHandler::smileySelected(QString character)
       
   265 {       
       
   266     HbInputModeHandler::smileySelected(character);
       
   267     refreshAutoCompleter();
       
   268 }
       
   269 //EOF