src/hbplugins/inputmethods/touchinput/hbinputtouchkeypadbutton.cpp
changeset 5 627c4a0fd0e7
parent 3 11d3954df52a
child 6 c3690ec91ef8
equal deleted inserted replaced
3:11d3954df52a 5:627c4a0fd0e7
     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 #include <QGraphicsScene>
       
    26 #include <QGraphicsSceneMouseEvent>
       
    27 
       
    28 #include <hbframedrawer.h>
       
    29 #include <hbframedrawerpool_p.h>
       
    30 #include <hbframeitem.h>
       
    31 #include <QGraphicsSceneResizeEvent>
       
    32 #include <hbtextitem.h>
       
    33 #include <hbiconitem.h>
       
    34 #include <hbevent.h>
       
    35 #ifdef HB_EFFECTS
       
    36 #include <hbeffect.h>
       
    37 #endif
       
    38 #include <hbtapgesture.h>
       
    39 
       
    40 #include "hbinputtouchkeypadbutton.h"
       
    41 #include "hbinputvkbwidget.h"
       
    42 #include "hbinputvkbwidget_p.h"
       
    43 
       
    44 /// @cond
       
    45 
       
    46 /*!
       
    47 @proto
       
    48 @hbinput
       
    49 \class HbTouchKeypadButton
       
    50 \brief A button widget to be used in touch keypads.
       
    51 
       
    52 Expands HbPushButton functionality to suit touch keypad purposes. It handles virtual keyboard closing gesture
       
    53 that is initiated from within the button area and knows how to act as a sticky input button. Sticky buttons propagate
       
    54 mouse press state to neighboring button when a drag event crosses widget boundary. This is needed for example in virtual qwerty where
       
    55 user must be able to slide finger across the keyboard.  
       
    56 */
       
    57 
       
    58 const QString HbNormalBackground("qtg_fr_input_btn_keypad_normal");
       
    59 const QString HbNormalPressedBackground("qtg_fr_input_btn_keypad_pressed");
       
    60 const QString HbNormalInActiveBackground("qtg_fr_input_btn_keypad_disabled");
       
    61 const QString HbNormalLatchedBackground("qtg_fr_input_btn_keypad_latched");
       
    62 
       
    63 const QString HbFunctionBackground("qtg_fr_input_btn_function_normal");
       
    64 const QString HbFunctionPressedBackground("qtg_fr_input_btn_function_pressed");
       
    65 const QString HbFuncInActiveBackground("qtg_fr_input_btn_function_disabled");
       
    66 const QString HbFunctionLatchedBackground("qtg_fr_input_btn_function_latched");
       
    67 
       
    68 inline HbTouchKeypadButton* hbtouchkeypadbutton_cast(QGraphicsItem *item)
       
    69 {
       
    70     if( item->isWidget() && qobject_cast<HbTouchKeypadButton *>(static_cast<QGraphicsWidget*>(item)) ) {
       
    71         return static_cast<HbTouchKeypadButton *>(item);
       
    72     }
       
    73     return 0;
       
    74 }
       
    75 
       
    76 class HbTouchKeypadButtonPrivate
       
    77 {
       
    78 public:
       
    79     HbTouchKeypadButtonPrivate(HbInputVkbWidget* owner)
       
    80         : mOwner(owner),
       
    81           mFaded(false),
       
    82           mButtonType(HbTouchKeypadButton::HbTouchButtonNormal),
       
    83           mFrameIcon(0),
       
    84           mStickyKey(false),
       
    85           mLatch(false)
       
    86     {}
       
    87 
       
    88 public:
       
    89     HbInputVkbWidget* mOwner;
       
    90     bool mFaded;
       
    91     HbTouchKeypadButton::HbTouchButtonType mButtonType;
       
    92     HbFrameItem *mFrameIcon;
       
    93     bool mStickyKey;
       
    94     bool mLatch;
       
    95     int mKeyCode;
       
    96 };
       
    97 
       
    98 HbTouchKeypadButton::HbTouchKeypadButton(HbInputVkbWidget *owner,
       
    99                                          const QString &text,
       
   100                                          QGraphicsWidget *parent)
       
   101                                          : HbPushButton(text, parent), d_ptr(new HbTouchKeypadButtonPrivate(owner))
       
   102 {
       
   103     #ifdef HB_EFFECTS
       
   104         HbEffect::disable(this);
       
   105     #endif
       
   106 
       
   107     this->setToolTip(QString());
       
   108     setBackgroundAttributes(HbTouchKeypadButton::HbTouchButtonReleased);
       
   109     setProperty("buttonType", "normal");
       
   110 }
       
   111 
       
   112 HbTouchKeypadButton::HbTouchKeypadButton(HbInputVkbWidget *owner,
       
   113                                          const HbIcon &icon,
       
   114                                          const QString &text,
       
   115                                          QGraphicsItem *parent)
       
   116                                          : HbPushButton(icon, text, parent), d_ptr(new HbTouchKeypadButtonPrivate(owner))
       
   117 {
       
   118     #ifdef HB_EFFECTS
       
   119         HbEffect::disable(this);
       
   120     #endif
       
   121 
       
   122     this->setToolTip(QString());
       
   123     setBackgroundAttributes(HbTouchKeypadButton::HbTouchButtonReleased);
       
   124     setProperty("buttonType", "normal");
       
   125 }
       
   126 
       
   127 HbTouchKeypadButton::~HbTouchKeypadButton()
       
   128 {
       
   129     delete d_ptr;
       
   130 }
       
   131 
       
   132 void HbTouchKeypadButton::mousePressEvent(QGraphicsSceneMouseEvent *event)
       
   133 {
       
   134     Q_UNUSED(event)
       
   135 }
       
   136 
       
   137 void HbTouchKeypadButton::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
       
   138 {
       
   139     Q_UNUSED(event)
       
   140 }
       
   141 
       
   142 void HbTouchKeypadButton::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
       
   143 {
       
   144     Q_UNUSED(event)
       
   145 }
       
   146 
       
   147 void HbTouchKeypadButton::gestureEvent(QGestureEvent *event)
       
   148 {
       
   149     Q_D(HbTouchKeypadButton);
       
   150     if (HbTapGesture *tap = qobject_cast<HbTapGesture*>(event->gesture(Qt::TapGesture))) {
       
   151         switch(tap->state()) {
       
   152         case Qt::GestureStarted:
       
   153             if (d->mOwner && d->mOwner->d_func()) {
       
   154                 d->mOwner->d_func()->updateMouseHitItem(this, tap->scenePosition());
       
   155             }
       
   156             if (!(d->mButtonType == HbTouchButtonNormalInActive && text().isEmpty())) {
       
   157                 setBackgroundAttributes(HbTouchKeypadButton::HbTouchButtonPressed);
       
   158             }
       
   159             break;
       
   160         case Qt::GestureUpdated:
       
   161             // Handle tap-and-hold?
       
   162             break;
       
   163         case Qt::GestureFinished:
       
   164             if (!(d->mButtonType == HbTouchButtonNormalInActive && text().isEmpty())) {
       
   165                 if (d->mLatch) {
       
   166                     setBackgroundAttributes(HbTouchKeypadButton::HbTouchButtonLatched);
       
   167                 } else {
       
   168                     setBackgroundAttributes(HbTouchKeypadButton::HbTouchButtonReleased);
       
   169                 }
       
   170                 break;
       
   171         case Qt::GestureCanceled:
       
   172                 setBackgroundAttributes(HbTouchKeypadButton::HbTouchButtonReleased);
       
   173                 break;
       
   174         default:
       
   175                 break;
       
   176             }
       
   177         }
       
   178     }
       
   179     HbPushButton::gestureEvent(event);
       
   180 }
       
   181 
       
   182 void HbTouchKeypadButton::resizeEvent(QGraphicsSceneResizeEvent *event)
       
   183 {
       
   184     Q_D(HbTouchKeypadButton);
       
   185 
       
   186     HbPushButton::resizeEvent(event);
       
   187 
       
   188     // setting the draw rect for the frameitem in this button
       
   189     // get the new size, and use the new size to the frameitem
       
   190     if (d->mFrameIcon ) {
       
   191         QSizeF mySize = event->newSize();
       
   192         QRectF rect = QRectF(mySize.width()*0.1, mySize.height()*0.3, mySize.width()*0.8, mySize.height());
       
   193         d->mFrameIcon->setGeometry( rect );
       
   194     }
       
   195 }
       
   196 
       
   197 bool HbTouchKeypadButton::isFaded()
       
   198 {
       
   199     Q_D(HbTouchKeypadButton);
       
   200     return d->mFaded;
       
   201 }
       
   202 
       
   203 void HbTouchKeypadButton::setFade(bool fade)
       
   204 {
       
   205     Q_D(HbTouchKeypadButton);
       
   206     if (d->mFaded == fade) {
       
   207         return;
       
   208     }
       
   209 
       
   210     d->mFaded =  fade;
       
   211 
       
   212     // now set button's text, type and background attributes based on d->mFaded value
       
   213     if(d->mFaded) {
       
   214         if (d->mFrameIcon) {
       
   215             d->mFrameIcon->setOpacity(0.2);
       
   216         }
       
   217         if(HbTouchButtonNormal == getButtonType() ){
       
   218             setButtonType(HbTouchKeypadButton::HbTouchButtonNormalInActive);
       
   219         } else if(HbTouchButtonFunction == getButtonType()) {
       
   220             setButtonType(HbTouchKeypadButton::HbTouchButtonFnInActive);
       
   221         }
       
   222     } else { 
       
   223         if (d->mFrameIcon) {
       
   224             d->mFrameIcon->setOpacity(1.0);
       
   225         }
       
   226         if(HbTouchButtonNormalInActive == getButtonType()){
       
   227             setButtonType(HbTouchKeypadButton::HbTouchButtonNormal);
       
   228         } else if(HbTouchButtonFnInActive == getButtonType()) {
       
   229             setButtonType(HbTouchKeypadButton::HbTouchButtonFunction);
       
   230         }
       
   231     }
       
   232     setBackgroundAttributes(HbTouchKeypadButton::HbTouchButtonReleased);
       
   233 }
       
   234 
       
   235 void HbTouchKeypadButton::setButtonType(HbTouchButtonType buttonType)
       
   236 {
       
   237     Q_D(HbTouchKeypadButton);
       
   238     d->mButtonType = buttonType;
       
   239     if (buttonType == HbTouchButtonNormal ||
       
   240         buttonType == HbTouchButtonNormalInActive) {
       
   241         setProperty("buttonType", "normal");
       
   242     } else if (buttonType == HbTouchButtonFunction ||
       
   243                buttonType == HbTouchButtonFnInActive){
       
   244         setProperty("buttonType", "function");
       
   245     }
       
   246 }
       
   247 
       
   248 int HbTouchKeypadButton::getButtonType()
       
   249 {
       
   250     Q_D(HbTouchKeypadButton);
       
   251     return d->mButtonType;
       
   252 }
       
   253 
       
   254 HbFrameItem * HbTouchKeypadButton::getFrameIcon()
       
   255 {
       
   256     Q_D(HbTouchKeypadButton);
       
   257     return d->mFrameIcon;
       
   258 }
       
   259 
       
   260 void HbTouchKeypadButton::setBackgroundAttributes(HbTouchButtonState buttonState)
       
   261 {
       
   262     Q_D(HbTouchKeypadButton);
       
   263 
       
   264     if(d->mButtonType == HbTouchButtonNormal) {
       
   265         if(buttonState == HbTouchKeypadButton::HbTouchButtonPressed) {
       
   266             setBackground(HbNormalPressedBackground);
       
   267         } else if (buttonState == HbTouchKeypadButton::HbTouchButtonLatched) {
       
   268             setBackground(HbNormalLatchedBackground);
       
   269         } else {
       
   270             setBackground(HbNormalBackground);
       
   271         }
       
   272     } else if(d->mButtonType == HbTouchButtonFunction) {
       
   273         if(buttonState == HbTouchKeypadButton::HbTouchButtonPressed) {
       
   274             setBackground(HbFunctionPressedBackground);
       
   275         } else if (buttonState == HbTouchKeypadButton::HbTouchButtonLatched) {
       
   276             setBackground(HbFunctionLatchedBackground);
       
   277         } else{
       
   278             setBackground(HbFunctionBackground);
       
   279         }
       
   280     } else if(d->mButtonType == HbTouchButtonFnInActive){
       
   281         setBackground(HbFuncInActiveBackground);
       
   282     } else if(d->mButtonType == HbTouchButtonNormalInActive) {
       
   283         setBackground(HbNormalInActiveBackground);
       
   284     } else {
       
   285         setBackground(HbFuncInActiveBackground);
       
   286     }
       
   287 }
       
   288 
       
   289 void HbTouchKeypadButton::setBackground(const QString& backgroundFrameFilename)
       
   290 {
       
   291     HbFrameDrawer* drawer = frameBackground();
       
   292     if (!drawer || drawer->frameGraphicsName() != backgroundFrameFilename) {
       
   293         setFrameBackground(HbFrameDrawerPool::get(backgroundFrameFilename, HbFrameDrawer::NinePieces, size()));
       
   294         update();
       
   295     }
       
   296 }
       
   297 
       
   298 void HbTouchKeypadButton::setFrameIcon(const QString& frameIconFileName )
       
   299 {
       
   300     Q_D(HbTouchKeypadButton);
       
   301 
       
   302     if (!d->mFrameIcon ) {
       
   303         d->mFrameIcon = new HbFrameItem(this);
       
   304         HbFrameDrawer *framedrawer = new HbFrameDrawer(frameIconFileName, HbFrameDrawer::ThreePiecesHorizontal);
       
   305         d->mFrameIcon->setFrameDrawer(framedrawer);
       
   306     } else {
       
   307         d->mFrameIcon->frameDrawer().setFrameGraphicsName(frameIconFileName);
       
   308     }
       
   309 }
       
   310 
       
   311 int HbTouchKeypadButton::type() const
       
   312 {
       
   313     Q_D(const HbTouchKeypadButton);
       
   314 
       
   315     if (d->mButtonType == HbTouchButtonFunction ||
       
   316         d->mButtonType == HbTouchButtonFnInActive) {
       
   317         return Hb::ItemType_InputFunctionButton;
       
   318     } else if (d->mButtonType == HbTouchButtonNormal ||
       
   319         d->mButtonType == HbTouchButtonNormalInActive) {
       
   320         return Hb::ItemType_InputCharacterButton;
       
   321     } else {
       
   322         return Hb::ItemType_InputCharacterButton;
       
   323     }
       
   324 }
       
   325 
       
   326 void HbTouchKeypadButton::setAsStickyButton(bool isSticky)
       
   327 {
       
   328     Q_D(HbTouchKeypadButton);
       
   329     d->mStickyKey = isSticky;
       
   330 }
       
   331 
       
   332 bool HbTouchKeypadButton::isStickyButton() const
       
   333 {
       
   334     Q_D(const HbTouchKeypadButton);
       
   335     return d->mStickyKey;
       
   336 }
       
   337 
       
   338 void HbTouchKeypadButton::setLatch(bool enable)
       
   339 {
       
   340     Q_D(HbTouchKeypadButton);
       
   341 
       
   342     d->mLatch = enable;
       
   343     if (d->mLatch) {
       
   344         setProperty("state", "latched");
       
   345         setBackgroundAttributes(HbTouchKeypadButton::HbTouchButtonLatched);
       
   346     } else {
       
   347         setProperty("state", "normal");
       
   348         setBackgroundAttributes(HbTouchKeypadButton::HbTouchButtonReleased);
       
   349     }
       
   350 }
       
   351 
       
   352 bool HbTouchKeypadButton::isLatched() const
       
   353 {
       
   354     Q_D(const HbTouchKeypadButton);
       
   355     return d->mLatch;
       
   356 }
       
   357 
       
   358 int HbTouchKeypadButton::keyCode() const
       
   359 {
       
   360     Q_D(const HbTouchKeypadButton);
       
   361     return d->mKeyCode;
       
   362 }
       
   363 
       
   364 void HbTouchKeypadButton::setKeyCode(int code)
       
   365 {
       
   366     Q_D(HbTouchKeypadButton);
       
   367     d->mKeyCode = code;
       
   368 }
       
   369 
       
   370 void HbTouchKeypadButton::setText(const QString &text)
       
   371 {
       
   372     // Workaround for pushbutton feature
       
   373     if (!text.isNull()) {
       
   374         HbPushButton::setText(text);
       
   375     } else {
       
   376         HbPushButton::setText(QString(""));
       
   377     }
       
   378 }
       
   379 
       
   380 void HbTouchKeypadButton::setAdditionalText(const QString &additionalText)
       
   381 {
       
   382     if (!additionalText.isNull()) {
       
   383         HbPushButton::setAdditionalText(additionalText);
       
   384     } else {
       
   385         HbPushButton::setAdditionalText(QString(""));
       
   386     }
       
   387 }
       
   388 
       
   389 void HbTouchKeypadButton::changeEvent( QEvent *event )
       
   390 {
       
   391     if ( event->type() == HbEvent::ThemeChanged ) {
       
   392         updatePrimitives();
       
   393         setBackgroundAttributes(HbTouchKeypadButton::HbTouchButtonReleased);
       
   394     }
       
   395     HbPushButton::changeEvent(event);
       
   396 }
       
   397 
       
   398 void HbTouchKeypadButton::updatePrimitives()
       
   399 {
       
   400     Q_D(HbTouchKeypadButton);
       
   401     HbPushButton::updatePrimitives();
       
   402 
       
   403     if (d->mFrameIcon && d->mFaded) {
       
   404         d->mFrameIcon->setOpacity(0.2);
       
   405     }
       
   406 }
       
   407 
       
   408 QSizeF HbTouchKeypadButton::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
       
   409 {
       
   410     QSizeF sh;
       
   411     switch (which) {
       
   412         case Qt::MinimumSize:
       
   413             sh = QSizeF(50, 50);
       
   414             break;
       
   415         case Qt::PreferredSize:
       
   416             sh = HbAbstractButton::sizeHint(which, constraint);
       
   417             break;
       
   418         case Qt::MaximumSize:
       
   419             sh = QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
       
   420             break;
       
   421         default:
       
   422             sh = HbAbstractButton::sizeHint(which, constraint);
       
   423             break;
       
   424     }
       
   425     return sh;
       
   426 }
       
   427 
       
   428 QVariant HbTouchKeypadButton::itemChange( GraphicsItemChange change, const QVariant & value )
       
   429 {
       
   430 	// If the button is being hidden and it has the press background, 
       
   431 	// need to set it to released background. This fix is needed for the error:
       
   432 	// In ITU-T long press * key and then return back to alpha mode, the * key 
       
   433 	// has button pressed background.
       
   434 	if (QGraphicsItem::ItemVisibleHasChanged == change && !value.toBool()) { 
       
   435 		if (isDown()) {
       
   436 			setBackgroundAttributes(HbTouchKeypadButton::HbTouchButtonReleased);
       
   437 		}
       
   438 	}
       
   439 	return HbPushButton::itemChange(change, value);
       
   440 }
       
   441 
       
   442 void HbTouchKeypadButton::setInitialSize(const QSizeF& initialSize)
       
   443 {
       
   444     setPreferredSize(initialSize);
       
   445     QGraphicsItem* backgroundPrimitive = primitive(HbStyle::P_PushButton_background);
       
   446     if (backgroundPrimitive) {
       
   447         HbIconItem *iconItem = static_cast<HbIconItem*>(backgroundPrimitive);
       
   448         iconItem->setSize(initialSize);
       
   449     }
       
   450 }
       
   451 
       
   452 /// @endcond
       
   453 
       
   454 // End of file