src/hbplugins/inputmethods/touchinput/hbinput12keytouchkeyboard.cpp
changeset 2 06ff229162e9
child 5 627c4a0fd0e7
equal deleted inserted replaced
1:f7ac710697a9 2:06ff229162e9
       
     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 <hbdeviceprofile.h>
       
    27 
       
    28 #include <hbinputmethod.h>
       
    29 #include <hbinputkeymap.h>
       
    30 #include <hbinpututils.h>
       
    31 #include <hbframedrawer.h>
       
    32 
       
    33 #include "hbinput12keytouchkeyboard.h"
       
    34 #include "hbinput12keytouchkeyboard_p.h"
       
    35 #include "hbinputbuttongroup.h"
       
    36 #include "hbinputbutton.h"
       
    37 #include "hbinputmodeindicator.h"
       
    38 
       
    39 const qreal HbKeyboardHeightInUnits = 37.8;
       
    40 const qreal HbKeyboardWidthInUnits = 53.8;
       
    41 
       
    42 const int HbVirtual12KeyNumberOfRows = 4;
       
    43 const int HbVirtual12KeyNumberOfColumns = 4;
       
    44 const int HbButtonKeyCodeTable[HbVirtual12KeyNumberOfRows * HbVirtual12KeyNumberOfColumns] =
       
    45 {
       
    46     HbInputButton::ButtonKeyCodeCharacter,
       
    47     HbInputButton::ButtonKeyCodeCharacter,
       
    48     HbInputButton::ButtonKeyCodeCharacter,
       
    49     HbInputButton::ButtonKeyCodeDelete,
       
    50     HbInputButton::ButtonKeyCodeCharacter,
       
    51     HbInputButton::ButtonKeyCodeCharacter,
       
    52     HbInputButton::ButtonKeyCodeCharacter,
       
    53     HbInputButton::ButtonKeyCodeSymbol,
       
    54     HbInputButton::ButtonKeyCodeCharacter,
       
    55     HbInputButton::ButtonKeyCodeCharacter,
       
    56     HbInputButton::ButtonKeyCodeCharacter,
       
    57     HbInputButton::ButtonKeyCodeSettings,
       
    58     HbInputButton::ButtonKeyCodeAsterisk,
       
    59     HbInputButton::ButtonKeyCodeCharacter,
       
    60     HbInputButton::ButtonKeyCodeShift,
       
    61     HbInputButton::ButtonKeyCodeCustom
       
    62 };
       
    63 
       
    64 Hb12KeyTouchKeyboardPrivate::Hb12KeyTouchKeyboardPrivate()
       
    65 {
       
    66 }
       
    67 
       
    68 Hb12KeyTouchKeyboardPrivate::~Hb12KeyTouchKeyboardPrivate()
       
    69 {
       
    70 }
       
    71 
       
    72 void Hb12KeyTouchKeyboardPrivate::init()
       
    73 {
       
    74     Q_Q(Hb12KeyTouchKeyboard);
       
    75 
       
    76     HbInputVkbWidgetPrivate::init();
       
    77 
       
    78     HbInputButtonGroup *buttonGroup = static_cast<HbInputButtonGroup*>(q->contentItem());
       
    79     if (buttonGroup) {
       
    80         buttonGroup->setGridSize(QSize(HbVirtual12KeyNumberOfColumns, HbVirtual12KeyNumberOfRows));
       
    81 
       
    82         QList<HbInputButton*> buttons;
       
    83         for (int i = 0; i < HbVirtual12KeyNumberOfColumns * HbVirtual12KeyNumberOfRows; ++i) {
       
    84             HbInputButton *item = new HbInputButton(HbButtonKeyCodeTable[i], QPoint(i % HbVirtual12KeyNumberOfColumns, i / HbVirtual12KeyNumberOfColumns));
       
    85             buttons.append(item);
       
    86 
       
    87             if (HbButtonKeyCodeTable[i] == HbInputButton::ButtonKeyCodeSettings) {
       
    88                 mInputModeIndicator = new HbInputModeIndicator(item, q);
       
    89             } else if (HbButtonKeyCodeTable[i] == HbInputButton::ButtonKeyCodeDelete) {
       
    90                 item->setIcon(HbIcon(HbInputButtonIconDelete2), HbInputButton::ButtonIconIndexPrimary);
       
    91             } else if (HbButtonKeyCodeTable[i] == HbInputButton::ButtonKeyCodeSymbol) {
       
    92                 item->setIcon(HbIcon(HbInputButtonIconSymbol2), HbInputButton::ButtonIconIndexPrimary);
       
    93             } else if (HbButtonKeyCodeTable[i] == HbInputButton::ButtonKeyCodeAsterisk) {
       
    94                 item->setText(QString("*"), HbInputButton::ButtonTextIndexPrimary);
       
    95                 item->setText(QString("+"), HbInputButton::ButtonTextIndexSecondaryFirstRow);
       
    96                 item->setType(HbInputButton::ButtonTypeNormal);
       
    97             } else if (HbButtonKeyCodeTable[i] == HbInputButton::ButtonKeyCodeShift) {
       
    98                 item->setText(QString(" "), HbInputButton::ButtonTextIndexSecondaryFirstRow);
       
    99                 item->setType(HbInputButton::ButtonTypeNormal);
       
   100             }
       
   101         }
       
   102         buttonGroup->setButtons(buttons);
       
   103 
       
   104         QObject::connect(buttonGroup, SIGNAL(buttonPressed(const QKeyEvent&)), q, SLOT(sendKeyPressEvent(const QKeyEvent&)));
       
   105         QObject::connect(buttonGroup, SIGNAL(buttonDoublePressed(const QKeyEvent&)), q, SLOT(sendKeyDoublePressEvent(const QKeyEvent&)));
       
   106         QObject::connect(buttonGroup, SIGNAL(buttonReleased(const QKeyEvent&)), q, SLOT(sendKeyReleaseEvent(const QKeyEvent&)));
       
   107         QObject::connect(buttonGroup, SIGNAL(buttonLongPressed(const QKeyEvent&)), q, SLOT(sendLongPressEvent(const QKeyEvent&)));
       
   108         QObject::connect(buttonGroup, SIGNAL(pressedButtonChanged(const QKeyEvent&, const QKeyEvent&)), q, SLOT(sendKeyChangeEvent(const QKeyEvent&, const QKeyEvent&)));
       
   109     }
       
   110 
       
   111     QObject::connect(q, SIGNAL(flickEvent(HbInputVkbWidget::HbFlickDirection)), buttonGroup, SLOT(cancelButtonPress()));
       
   112 }
       
   113 
       
   114 int Hb12KeyTouchKeyboardPrivate::keyCode(int buttonId)
       
   115 {
       
   116     return HbButtonKeyCodeTable[buttonId];
       
   117 }
       
   118 
       
   119 void Hb12KeyTouchKeyboardPrivate::applyEditorConstraints()
       
   120 {
       
   121     Q_Q(Hb12KeyTouchKeyboard);
       
   122 
       
   123     HbInputFocusObject *focusedObject = mOwner->focusObject();
       
   124     if (!focusedObject) {
       
   125         return;
       
   126     }
       
   127 
       
   128     HbInputButtonGroup *buttonGroup = static_cast<HbInputButtonGroup*>(q->contentItem());
       
   129     if (buttonGroup) {
       
   130         QList<HbInputButton*> buttons = buttonGroup->buttons();
       
   131         for (int i = 0; i < buttons.count(); ++i) {
       
   132             HbInputButton *item = buttons.at(i);
       
   133 
       
   134             HbInputButton::HbInputButtonState state = item->state();
       
   135             if (keyCode(i) == HbInputButton::ButtonKeyCodeCharacter) {
       
   136                 if (mMode == EModeNumeric) {
       
   137                     QString data = item->text(HbInputButton::ButtonTextIndexPrimary);
       
   138                     if (data.isEmpty() || !focusedObject->characterAllowedInEditor(data.at(0))) {
       
   139                         state = HbInputButton::ButtonStateDisabled;
       
   140                     } else if (item->state() == HbInputButton::ButtonStateDisabled) {
       
   141                         state = HbInputButton::ButtonStateReleased;
       
   142                     }
       
   143                 } else if (mMode == EModeAbc) {
       
   144                     if (item->text(HbInputButton::ButtonTextIndexSecondaryFirstRow).isEmpty() &&
       
   145                         item->icon(HbInputButton::ButtonIconIndexSecondaryFirstRow).isNull()) {
       
   146                         state = HbInputButton::ButtonStateDisabled;
       
   147                     }  else if (item->state() == HbInputButton::ButtonStateDisabled) {
       
   148                         state = HbInputButton::ButtonStateReleased;
       
   149                     }
       
   150                 }
       
   151             } else if (keyCode(i) == HbInputButton::ButtonKeyCodeSymbol) {
       
   152                 if (mMode == EModeNumeric && focusedObject->editorInterface().isNumericEditor()) {
       
   153                     state = HbInputButton::ButtonStateDisabled;
       
   154                 } else if (item->state() == HbInputButton::ButtonStateDisabled) {
       
   155                     state = HbInputButton::ButtonStateReleased;
       
   156                 }
       
   157             } else if (keyCode(i) == HbInputButton::ButtonKeyCodeAsterisk ||
       
   158                        keyCode(i) == HbInputButton::ButtonKeyCodeShift) {
       
   159                 QString sctCharacters;
       
   160                 const HbKeyboardMap *keyboardMap = mKeymap->keyboard(HbKeyboardSctPortrait);
       
   161                 if (keyboardMap) {
       
   162                     foreach (const HbMappedKey* mappedKey, keyboardMap->keys) {
       
   163                         focusedObject->filterStringWithEditorFilter(mappedKey->characters(HbModifierNone), sctCharacters);
       
   164                         if (sctCharacters.count()) {
       
   165                             break;
       
   166                         }
       
   167                     }
       
   168                 }
       
   169 
       
   170                 if (!sctCharacters.count() && mMode == EModeNumeric && focusedObject->editorInterface().isNumericEditor()) {
       
   171                     state = HbInputButton::ButtonStateDisabled;
       
   172                 } else if (item->state() == HbInputButton::ButtonStateDisabled) {
       
   173                     state = HbInputButton::ButtonStateReleased;
       
   174                 }
       
   175             }
       
   176             item->setState(state);
       
   177         }
       
   178         buttonGroup->setButtons(buttons);
       
   179     }
       
   180 }
       
   181 
       
   182 void Hb12KeyTouchKeyboardPrivate::updateButtons()
       
   183 {
       
   184     Q_Q(Hb12KeyTouchKeyboard);
       
   185 
       
   186     HbInputButtonGroup *buttonGroup = static_cast<HbInputButtonGroup*>(q->contentItem());
       
   187     if (buttonGroup) {
       
   188         int key = 0;
       
   189         QList<HbInputButton*> buttons = buttonGroup->buttons();
       
   190         for (int i = 0; i < buttons.count(); ++i) {
       
   191             HbInputButton *item = buttons.at(i);
       
   192             if (keyCode(i) == HbInputButton::ButtonKeyCodeCharacter) {
       
   193 
       
   194                 if (mMode == EModeNumeric) {
       
   195                     QChar numChr;
       
   196                     const HbKeyboardMap *keyboardMap = mKeymap->keyboard(HbKeyboardVirtual12Key);
       
   197                     if (keyboardMap && key < keyboardMap->keys.count()) {
       
   198                         numChr = HbInputUtils::findFirstNumberCharacterBoundToKey(keyboardMap->keys.at(key), mKeymap->language());
       
   199                     }
       
   200 
       
   201                     if (numChr > 0) {
       
   202                         item->setText(numChr, HbInputButton::ButtonTextIndexPrimary);
       
   203                     } else {
       
   204                         item->setText(QString(), HbInputButton::ButtonTextIndexPrimary);
       
   205                     }
       
   206                     item->setText(QString(), HbInputButton::ButtonTextIndexSecondaryFirstRow);
       
   207                     item->setText(QString(), HbInputButton::ButtonTextIndexSecondarySecondRow);
       
   208                     item->setIcon(HbIcon(), HbInputButton::ButtonIconIndexPrimary);
       
   209                     item->setIcon(HbIcon(), HbInputButton::ButtonIconIndexSecondaryFirstRow);
       
   210                     item->setIcon(HbIcon(), HbInputButton::ButtonIconIndexSecondarySecondRow);
       
   211                 } else if (mMode == EModeAbc) {
       
   212                     QString keydata;
       
   213                     QChar numChr;
       
   214                     const HbKeyboardMap *keyboardMap = mKeymap->keyboard(HbKeyboardVirtual12Key);
       
   215                     if (keyboardMap && key < keyboardMap->keys.count()) {
       
   216                         keydata = keyboardMap->keys.at(key)->characters(mModifiers);
       
   217                         numChr = HbInputUtils::findFirstNumberCharacterBoundToKey(keyboardMap->keys.at(key), mKeymap->language());
       
   218                     }
       
   219 
       
   220                     QString title("");
       
   221                     if (mOwner->focusObject()) {
       
   222                         QString allowedData;
       
   223                         mOwner->focusObject()->filterStringWithEditorFilter(keydata, allowedData);
       
   224                         title.append(allowedData.left(numberOfCharactersToShow(key)));
       
   225                     } else {
       
   226                         title.append(keydata.left(numberOfCharactersToShow(key)));
       
   227                     }
       
   228 
       
   229                     if (numChr == QChar('0')) {
       
   230                         item->setText(numChr, HbInputButton::ButtonTextIndexPrimary);
       
   231                         item->setIcon(HbIcon(HbInputButtonIconSpace2), HbInputButton::ButtonIconIndexSecondaryFirstRow);
       
   232                         // Set space as secondaty text so that the layout is correct if icon is not found. This can be removed when
       
   233                         // new space graphics are included in the main line.
       
   234                         item->setText(QString(" "), HbInputButton::ButtonTextIndexSecondaryFirstRow);
       
   235                     } else {
       
   236                         item->setText(title, HbInputButton::ButtonTextIndexSecondaryFirstRow);
       
   237                         item->setText(numChr, HbInputButton::ButtonTextIndexPrimary);
       
   238                     }
       
   239                 }
       
   240 
       
   241                 ++key;
       
   242             } else if (keyCode(i) == HbInputButton::ButtonKeyCodeShift) {
       
   243                 if (mMode == EModeNumeric) {
       
   244                     item->setText(QString("#"), HbInputButton::ButtonTextIndexSecondaryFirstRow);
       
   245                 } else if (mMode == EModeAbc) {
       
   246                     item->setText(QString(" "), HbInputButton::ButtonTextIndexSecondaryFirstRow);
       
   247                 }
       
   248             }
       
   249         }
       
   250         buttonGroup->setButtons(buttons);
       
   251     }
       
   252 }
       
   253 
       
   254 int Hb12KeyTouchKeyboardPrivate::numberOfCharactersToShow(int key)
       
   255 {
       
   256     QChar keyCode;
       
   257     const HbKeyboardMap *keyboardMap = mKeymap->keyboard(HbKeyboardVirtual12Key);
       
   258     if (keyboardMap && key < keyboardMap->keys.count()) {
       
   259         keyCode = keyboardMap->keys.at(key)->keycode;
       
   260     }
       
   261 
       
   262     if (keyCode == QChar('7') || keyCode == QChar('9')) {
       
   263         return 4;
       
   264     } else {
       
   265         return 3;
       
   266     }
       
   267 }
       
   268 
       
   269 /*!
       
   270 Constructs the object. owner is the owning input method implementation. Keymap
       
   271 is key mapping data to be used to display button texts. Key mapping data can be
       
   272 changed later (for example when the input language changes) by calling
       
   273 setKeymap.
       
   274 */
       
   275 Hb12KeyTouchKeyboard::Hb12KeyTouchKeyboard(HbInputMethod *owner, const HbKeymap *keymap, QGraphicsItem *parent)
       
   276  : HbInputVkbWidget(*new Hb12KeyTouchKeyboardPrivate, parent)
       
   277 {
       
   278     if (!owner) {
       
   279         return;
       
   280     }
       
   281     Q_D(Hb12KeyTouchKeyboard);
       
   282     d->mOwner = owner;
       
   283     setKeymap(keymap);
       
   284 }
       
   285 
       
   286 /*!
       
   287 Constructs the object. owner is the owning input method implementation. Keymap
       
   288 is key mapping data to be used to display button texts. Key mapping data can be
       
   289 changed later (for example when the input language changes) by calling
       
   290 setKeymap.
       
   291 */
       
   292 Hb12KeyTouchKeyboard::Hb12KeyTouchKeyboard(Hb12KeyTouchKeyboardPrivate &dd, HbInputMethod *owner,
       
   293                                            const HbKeymap *keymap, QGraphicsItem *parent)
       
   294  : HbInputVkbWidget(dd, parent)
       
   295 {
       
   296     if (!owner) {
       
   297         return;
       
   298     }
       
   299     Q_D(Hb12KeyTouchKeyboard);
       
   300     d->mOwner = owner;
       
   301     setKeymap(keymap);
       
   302 }
       
   303 
       
   304 /*!
       
   305 Destructs the object.
       
   306 */
       
   307 Hb12KeyTouchKeyboard::~Hb12KeyTouchKeyboard()
       
   308 {
       
   309 }
       
   310 
       
   311 /*!
       
   312 Returns keyboard type.
       
   313 */
       
   314 HbKeyboardType Hb12KeyTouchKeyboard::keyboardType() const
       
   315 {
       
   316     return HbKeyboardVirtual12Key;
       
   317 }
       
   318 
       
   319 /*!
       
   320 Returns preferred keyboard size. HbVkbHost uses this information when it opens the keyboard.
       
   321 */
       
   322 QSizeF Hb12KeyTouchKeyboard::preferredKeyboardSize()
       
   323 {
       
   324     Q_D(Hb12KeyTouchKeyboard);
       
   325 
       
   326     QSizeF result;
       
   327     qreal unitValue = HbDeviceProfile::profile(mainWindow()).unitValue();
       
   328 
       
   329     result.setHeight(HbKeyboardHeightInUnits * unitValue + d->mCloseHandleHeight);
       
   330     result.setWidth(HbKeyboardWidthInUnits * unitValue);
       
   331 
       
   332     return QSizeF(result);
       
   333 }
       
   334 
       
   335 // End of file