src/hbplugins/inputmethods/touchinput/hbinputqwertynumerictouchkeyboard.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 <hbinputsettingproxy.h>
       
    31 
       
    32 #include "hbinputqwertynumerictouchkeyboard.h"
       
    33 #include "hbinputqwertynumerictouchkeyboard_p.h"
       
    34 #include "hbinputvkbwidget_p.h"
       
    35 #include "hbinputbuttongroup.h"
       
    36 #include "hbinputbutton.h"
       
    37 #include "hbinputmodeindicator.h"
       
    38 
       
    39 const qreal HbKeyboardHeightInUnits = 17.3;
       
    40 const qreal HbKeyboardWidthInUnits = 95.5;
       
    41 
       
    42 const int HbVirtualQwertyNumberOfRows = 2;
       
    43 const int HbVirtualQwertyNumberOfColumns = 10;
       
    44 const int HbButtonKeyCodeTable[HbVirtualQwertyNumberOfRows * HbVirtualQwertyNumberOfColumns] =
       
    45 {
       
    46     HbInputButton::ButtonKeyCodeCharacter,
       
    47     HbInputButton::ButtonKeyCodeCharacter,
       
    48     HbInputButton::ButtonKeyCodeCharacter,
       
    49     HbInputButton::ButtonKeyCodeCharacter,
       
    50     HbInputButton::ButtonKeyCodeCharacter,
       
    51     HbInputButton::ButtonKeyCodeCharacter,
       
    52     HbInputButton::ButtonKeyCodeCharacter,
       
    53     HbInputButton::ButtonKeyCodeCharacter,
       
    54     HbInputButton::ButtonKeyCodeCharacter,
       
    55     HbInputButton::ButtonKeyCodeCharacter,
       
    56     HbInputButton::ButtonKeyCodeShift,
       
    57     HbInputButton::ButtonKeyCodeSymbol,
       
    58     HbInputButton::ButtonKeyCodeCharacter,
       
    59     HbInputButton::ButtonKeyCodeCharacter,
       
    60     HbInputButton::ButtonKeyCodeCharacter,
       
    61     HbInputButton::ButtonKeyCodeCharacter,
       
    62     HbInputButton::ButtonKeyCodeCharacter,
       
    63     HbInputButton::ButtonKeyCodeDelete,
       
    64     HbInputButton::ButtonKeyCodeSettings,
       
    65     HbInputButton::ButtonKeyCodeCustom
       
    66 };
       
    67 
       
    68 HbQwertyNumericKeyboardPrivate::HbQwertyNumericKeyboardPrivate()
       
    69 {
       
    70 }
       
    71 
       
    72 HbQwertyNumericKeyboardPrivate::~HbQwertyNumericKeyboardPrivate()
       
    73 {
       
    74 }
       
    75 
       
    76 void HbQwertyNumericKeyboardPrivate::init()
       
    77 {
       
    78     Q_Q(HbQwertyNumericKeyboard);
       
    79 
       
    80     HbInputVkbWidgetPrivate::init();
       
    81 
       
    82     HbInputButtonGroup *buttonGroup = static_cast<HbInputButtonGroup*>(q->contentItem());
       
    83     if (buttonGroup) {
       
    84         buttonGroup->setGridSize(QSize(HbVirtualQwertyNumberOfColumns, HbVirtualQwertyNumberOfRows));
       
    85 
       
    86         QList<HbInputButton*> buttons;
       
    87         for (int i = 0; i < HbVirtualQwertyNumberOfColumns * HbVirtualQwertyNumberOfRows; ++i) {
       
    88             HbInputButton *item = new HbInputButton(HbButtonKeyCodeTable[i], QPoint(i % HbVirtualQwertyNumberOfColumns, i / HbVirtualQwertyNumberOfColumns));
       
    89             buttons.append(item);
       
    90 
       
    91             if (HbButtonKeyCodeTable[i] == HbInputButton::ButtonKeyCodeSettings) {
       
    92                 mInputModeIndicator = new HbInputModeIndicator(item, q);
       
    93             } else if (HbButtonKeyCodeTable[i] == HbInputButton::ButtonKeyCodeShift ||
       
    94                        HbButtonKeyCodeTable[i] == HbInputButton::ButtonKeyCodeSymbol) {
       
    95                 item->setState(HbInputButton::ButtonStateDisabled);
       
    96             }
       
    97         }
       
    98         buttonGroup->setButtons(buttons);
       
    99         buttonGroup->setButtonPreviewEnabled(HbInputSettingProxy::instance()->isCharacterPreviewForQwertyEnabled());
       
   100 
       
   101         QObject::connect(buttonGroup, SIGNAL(buttonPressed(const QKeyEvent&)), q, SLOT(sendKeyPressEvent(const QKeyEvent&)));
       
   102         QObject::connect(buttonGroup, SIGNAL(buttonDoublePressed(const QKeyEvent&)), q, SLOT(sendKeyDoublePressEvent(const QKeyEvent&)));
       
   103         QObject::connect(buttonGroup, SIGNAL(buttonReleased(const QKeyEvent&)), q, SLOT(sendKeyReleaseEvent(const QKeyEvent&)));
       
   104         QObject::connect(buttonGroup, SIGNAL(buttonLongPressed(const QKeyEvent&)), q, SLOT(sendLongPressEvent(const QKeyEvent&)));
       
   105         QObject::connect(buttonGroup, SIGNAL(pressedButtonChanged(const QKeyEvent&, const QKeyEvent&)), q, SLOT(sendKeyChangeEvent(const QKeyEvent&, const QKeyEvent&)));
       
   106     }
       
   107     QObject::connect(q, SIGNAL(flickEvent(HbInputVkbWidget::HbFlickDirection)), buttonGroup, SLOT(cancelButtonPress()));
       
   108 }
       
   109 
       
   110 int HbQwertyNumericKeyboardPrivate::keyCode(int buttonId)
       
   111 {
       
   112     return HbButtonKeyCodeTable[buttonId];
       
   113 }
       
   114 
       
   115 void HbQwertyNumericKeyboardPrivate::applyEditorConstraints()
       
   116 {
       
   117     Q_Q(HbQwertyNumericKeyboard);
       
   118 
       
   119     HbInputFocusObject *focusedObject = mOwner->focusObject();
       
   120     if (!focusedObject) {
       
   121         return;
       
   122     }
       
   123 
       
   124     HbInputButtonGroup *buttonGroup = static_cast<HbInputButtonGroup*>(q->contentItem());
       
   125     if (buttonGroup) {
       
   126         QList<HbInputButton*> buttons = buttonGroup->buttons();
       
   127         for (int i = 0; i < buttons.count(); ++i) {
       
   128             if (keyCode(i) == HbInputButton::ButtonKeyCodeCharacter) {
       
   129                 HbInputButton *item = buttons.at(i);
       
   130 
       
   131                 HbInputButton::HbInputButtonState state = item->state();
       
   132                 QString data = item->text(HbInputButton::ButtonTextIndexPrimary);
       
   133                 if (data.isEmpty() || !focusedObject->characterAllowedInEditor(data.at(0))) {
       
   134                     state = HbInputButton::ButtonStateDisabled;
       
   135                 } else if (item->state() == HbInputButton::ButtonStateDisabled) {
       
   136                     state = HbInputButton::ButtonStateReleased;
       
   137                 }
       
   138                 item->setState(state);
       
   139             }
       
   140         }
       
   141         buttonGroup->setButtons(buttons);
       
   142     }
       
   143 }
       
   144 
       
   145 void HbQwertyNumericKeyboardPrivate::updateKeyCodes()
       
   146 {
       
   147     Q_Q(HbQwertyNumericKeyboard);
       
   148 
       
   149     QString characters;
       
   150     getCharacters(characters);
       
   151 
       
   152     HbInputButtonGroup *buttonGroup = static_cast<HbInputButtonGroup*>(q->contentItem());
       
   153     if (buttonGroup) {
       
   154         int key = 0;
       
   155         QList<HbInputButton*> buttons = buttonGroup->buttons();
       
   156         for (int i = 0; i < buttons.count(); ++i) {
       
   157             if (keyCode(i) == HbInputButton::ButtonKeyCodeCharacter) {
       
   158                 HbInputButton *item = buttons.at(i);
       
   159 
       
   160                 if (key < characters.count()) { 
       
   161                     item->setKeyCode(characters.at(key).unicode());
       
   162                 }
       
   163                 ++key;
       
   164             }
       
   165         }
       
   166     }
       
   167 }
       
   168 
       
   169 void HbQwertyNumericKeyboardPrivate::updateButtons()
       
   170 {
       
   171     Q_Q(HbQwertyNumericKeyboard);
       
   172 
       
   173     QString characters;
       
   174     getCharacters(characters);
       
   175 
       
   176     HbInputButtonGroup *buttonGroup = static_cast<HbInputButtonGroup*>(q->contentItem());
       
   177     if (buttonGroup) {
       
   178         int key = 0;
       
   179         QList<HbInputButton*> buttons = buttonGroup->buttons();
       
   180         for (int i = 0; i < buttons.count(); ++i) {
       
   181             if (keyCode(i) == HbInputButton::ButtonKeyCodeCharacter) {
       
   182                 HbInputButton *item = buttons.at(i);
       
   183 
       
   184                 if (key < characters.count()) {
       
   185                     item->setText(characters.at(key), HbInputButton::ButtonTextIndexPrimary);
       
   186                 } else {
       
   187                     item->setText(QString(), HbInputButton::ButtonTextIndexPrimary);
       
   188                 }
       
   189                 ++key;
       
   190             }
       
   191         }
       
   192         buttonGroup->setButtons(buttons);
       
   193     }
       
   194 }
       
   195 
       
   196 void HbQwertyNumericKeyboardPrivate::getCharacters(QString &characters)
       
   197 {
       
   198     characters = QString("1234567890");
       
   199 
       
   200     if (mKeymap) {
       
   201         const HbKeyboardMap* keyboardMap = mKeymap->keyboard(HbKeyboardSctLandscape);
       
   202         if (!keyboardMap) {
       
   203             return;
       
   204         }
       
   205 
       
   206         foreach (const HbMappedKey* mappedKey, keyboardMap->keys) {
       
   207             QString chars = mappedKey->characters(HbModifierNone);
       
   208             
       
   209             HbInputFocusObject *focusedObject = mOwner->focusObject();
       
   210             QString allowedChars;
       
   211             if (focusedObject) {
       
   212                 focusedObject->filterStringWithEditorFilter(chars, allowedChars);
       
   213             }
       
   214 
       
   215             foreach (QChar sctChar, allowedChars) {
       
   216                 if (!characters.contains(sctChar)) {
       
   217                     characters.append(sctChar);
       
   218                 }
       
   219             }
       
   220         }
       
   221     }
       
   222 }
       
   223 
       
   224 /*!
       
   225 Constructs the object. owner is the owning input method implementation. Keymap
       
   226 is key mapping data to be used to display button texts. Key mapping data can be
       
   227 changed later (for example when the input language changes) by calling
       
   228 setKeymap.
       
   229 */
       
   230 HbQwertyNumericKeyboard::HbQwertyNumericKeyboard(HbInputMethod *owner, const HbKeymap *keymap, QGraphicsItem *parent)
       
   231  : HbInputVkbWidget(*new HbQwertyNumericKeyboardPrivate, parent)
       
   232 {
       
   233     Q_D(HbQwertyNumericKeyboard);
       
   234     d->mOwner = owner;
       
   235     setKeymap(keymap);
       
   236 
       
   237     QObject::connect(HbInputSettingProxy::instance(), SIGNAL(characterPreviewStateForQwertyChanged(bool)), this, SLOT(updateButtonPreviewStatus(bool)));
       
   238 }
       
   239 
       
   240 /*!
       
   241 Constructs the object. owner is the owning input method implementation. Keymap
       
   242 is key mapping data to be used to display button texts. Key mapping data can be
       
   243 changed later (for example when the input language changes) by calling
       
   244 setKeymap.
       
   245 */
       
   246 HbQwertyNumericKeyboard::HbQwertyNumericKeyboard(HbQwertyNumericKeyboardPrivate &dd, HbInputMethod *owner,
       
   247                                                  const HbKeymap *keymap, QGraphicsItem* parent)
       
   248  : HbInputVkbWidget(dd, parent)
       
   249 {
       
   250     Q_D(HbQwertyNumericKeyboard);
       
   251     d->mOwner = owner;
       
   252     setKeymap(keymap);
       
   253 
       
   254     QObject::connect(HbInputSettingProxy::instance(), SIGNAL(characterPreviewStateForQwertyChanged(bool)), this, SLOT(updateButtonPreviewStatus(bool)));
       
   255 }
       
   256 
       
   257 /*!
       
   258 Destructs the object.
       
   259 */
       
   260 HbQwertyNumericKeyboard::~HbQwertyNumericKeyboard()
       
   261 {
       
   262 }
       
   263 
       
   264 /*!
       
   265 Returns keyboard type.
       
   266 */
       
   267 HbKeyboardType HbQwertyNumericKeyboard::keyboardType() const
       
   268 {
       
   269     return HbKeyboardVirtualQwerty;
       
   270 }
       
   271 
       
   272 /*!
       
   273 Returns preferred keyboard size. HbVkbHost uses this information when it opens the keyboard.
       
   274 */
       
   275 QSizeF HbQwertyNumericKeyboard::preferredKeyboardSize()
       
   276 {
       
   277     Q_D(HbQwertyNumericKeyboard);
       
   278 
       
   279     QSizeF result;
       
   280     qreal unitValue = HbDeviceProfile::profile(mainWindow()).unitValue();
       
   281 
       
   282     result.setHeight(HbKeyboardHeightInUnits * unitValue + d->mCloseHandleHeight);
       
   283     result.setWidth(HbKeyboardWidthInUnits * unitValue);
       
   284 
       
   285     return QSizeF(result);
       
   286 }
       
   287 
       
   288 /*!
       
   289 Updates button preview status.
       
   290 */
       
   291 void HbQwertyNumericKeyboard::updateButtonPreviewStatus(bool status)
       
   292 {
       
   293     HbInputButtonGroup *buttonGroup = static_cast<HbInputButtonGroup*>(contentItem());
       
   294     if (buttonGroup) {
       
   295         buttonGroup->setButtonPreviewEnabled(status);   
       
   296     }
       
   297 }
       
   298 
       
   299 // End of file