src/hbinput/inputwidgets/hbinputbutton.cpp
changeset 21 4633027730f5
parent 7 923ff622b8b9
child 30 80e4d18b72f5
equal deleted inserted replaced
7:923ff622b8b9 21:4633027730f5
    25 
    25 
    26 #include "hbinputbutton.h"
    26 #include "hbinputbutton.h"
    27 
    27 
    28 /// @cond
    28 /// @cond
    29 
    29 
       
    30 // Defines how much the button size should be increased from its original size.
       
    31 const qreal HbInputThresholdMultiplier = 0.25;
       
    32 
       
    33 // Threshold for touch point position comparison
       
    34 const qreal HbInputTouchThreshold = 5.0;
       
    35 
    30 class HbInputButtonPrivate
    36 class HbInputButtonPrivate
    31 {
    37 {
    32 public:
    38 public:
    33     HbInputButtonPrivate();
    39     HbInputButtonPrivate();
    34     HbInputButtonPrivate(int keyCode, const QPoint &position, const QSize &size);
    40     HbInputButtonPrivate(int keyCode, const QPoint &position, const QSize &size);
    38 
    44 
    39     void setDefaultGraphics(int keyCode);
    45     void setDefaultGraphics(int keyCode);
    40 
    46 
    41     HbInputButton::HbInputButtonType mType;
    47     HbInputButton::HbInputButtonType mType;
    42     HbInputButton::HbInputButtonState mState;
    48     HbInputButton::HbInputButtonState mState;
       
    49     HbInputButton::HbInputButtonState mPreviousState;
    43     QPoint mPosition;
    50     QPoint mPosition;
    44     QSize mSize;
    51     QSize mSize;
    45     int mKeyCode;
    52     int mKeyCode;
    46     bool mAutoRepeat;
    53     bool mAutoRepeat;
    47     QList<QString> mTexts;
    54     QList<QString> mTexts;
    48     QString mMappedCharacters;
    55     QString mMappedCharacters;
    49     QList<HbIcon> mIcons;
    56     QList<HbIcon> mIcons;
    50     QRectF mBoundingRect;
    57     QRectF mBoundingRect;
       
    58     QPointF mLastInteractionPoint;
    51 };
    59 };
    52 
    60 
    53 HbInputButtonPrivate::HbInputButtonPrivate()
    61 HbInputButtonPrivate::HbInputButtonPrivate()
    54     : mType(HbInputButton::ButtonTypeNormal), mState(HbInputButton::ButtonStateReleased),
    62     : mType(HbInputButton::ButtonTypeNormal), mState(HbInputButton::ButtonStateReleased),
    55       mPosition(0, 0), mSize(1, 1), mKeyCode(-1), mAutoRepeat(false)
    63       mPreviousState(HbInputButton::ButtonStateReleased), mPosition(0, 0), mSize(1, 1),
       
    64       mKeyCode(-1), mAutoRepeat(false), mLastInteractionPoint(-1, -1)
    56 {
    65 {
    57     for (int i = 0; i < HbInputButton::ButtonTextIndexCount; ++i) {
    66     for (int i = 0; i < HbInputButton::ButtonTextIndexCount; ++i) {
    58         mTexts.append("");
    67         mTexts.append("");
    59     }
    68     }
    60 
    69 
    63     }
    72     }
    64 }
    73 }
    65 
    74 
    66 HbInputButtonPrivate::HbInputButtonPrivate(int keyCode, const QPoint &position, const QSize &size)
    75 HbInputButtonPrivate::HbInputButtonPrivate(int keyCode, const QPoint &position, const QSize &size)
    67     : mType(HbInputButton::ButtonTypeNormal), mState(HbInputButton::ButtonStateReleased),
    76     : mType(HbInputButton::ButtonTypeNormal), mState(HbInputButton::ButtonStateReleased),
    68       mPosition(position), mSize(size), mKeyCode(keyCode), mAutoRepeat(false)
    77       mPreviousState(HbInputButton::ButtonStateReleased), mPosition(position),
       
    78       mSize(size), mKeyCode(keyCode), mAutoRepeat(false)
    69 {
    79 {
    70     for (int i = 0; i < HbInputButton::ButtonTextIndexCount; ++i) {
    80     for (int i = 0; i < HbInputButton::ButtonTextIndexCount; ++i) {
    71         mTexts.append("");
    81         mTexts.append("");
    72     }
    82     }
    73 
    83 
    95 }
   105 }
    96 
   106 
    97 HbInputButtonPrivate::HbInputButtonPrivate(HbInputButton::HbInputButtonType type, HbInputButton::HbInputButtonState state,
   107 HbInputButtonPrivate::HbInputButtonPrivate(HbInputButton::HbInputButtonType type, HbInputButton::HbInputButtonState state,
    98         const QPoint &position, const QSize &size, int keyCode, bool autoRepeat,
   108         const QPoint &position, const QSize &size, int keyCode, bool autoRepeat,
    99         const QList<QString> &texts, const QString &mappedCharacters, const QList<HbIcon> &icons)
   109         const QList<QString> &texts, const QString &mappedCharacters, const QList<HbIcon> &icons)
   100     : mType(type), mState(state), mPosition(position), mSize(size), mKeyCode(keyCode), mAutoRepeat(autoRepeat),
   110     : mType(type), mState(state), mPreviousState(state), mPosition(position), mSize(size),
   101       mMappedCharacters(mappedCharacters)
   111       mKeyCode(keyCode), mAutoRepeat(autoRepeat), mMappedCharacters(mappedCharacters)
   102 {
   112 {
   103     for (int i = 0; i < HbInputButton::ButtonTextIndexCount; ++i) {
   113     for (int i = 0; i < HbInputButton::ButtonTextIndexCount; ++i) {
   104         if (i < texts.count()) {
   114         if (i < texts.count()) {
   105             mTexts.append(texts.at(i));
   115             mTexts.append(texts.at(i));
   106         } else {
   116         } else {
   125 }
   135 }
   126 
   136 
   127 void HbInputButtonPrivate::setDefaultGraphics(int keyCode)
   137 void HbInputButtonPrivate::setDefaultGraphics(int keyCode)
   128 {
   138 {
   129     switch(keyCode) {
   139     switch(keyCode) {
   130         case HbInputButton::ButtonKeyCodeDelete:
   140     case HbInputButton::ButtonKeyCodeDelete:
   131             mIcons.replace(HbInputButton::ButtonTextIndexPrimary, HbIcon(HbInputButtonIconDelete));
   141         mIcons.replace(HbInputButton::ButtonTextIndexPrimary, HbIcon(HbInputButtonIconDelete));
   132             break;
   142         break;
   133         case HbInputButton::ButtonKeyCodeShift:
   143     case HbInputButton::ButtonKeyCodeShift:
   134             mIcons.replace(HbInputButton::ButtonTextIndexPrimary, HbIcon(HbInputButtonIconShift));
   144         mIcons.replace(HbInputButton::ButtonTextIndexPrimary, HbIcon(HbInputButtonIconShift));
   135             break;
   145         break;
   136         case HbInputButton::ButtonKeyCodeSymbol:
   146     case HbInputButton::ButtonKeyCodeSymbol:
   137             mIcons.replace(HbInputButton::ButtonTextIndexPrimary, HbIcon(HbInputButtonIconSymbol));
   147         mIcons.replace(HbInputButton::ButtonTextIndexPrimary, HbIcon(HbInputButtonIconSymbol));
   138             break;
   148         break;
   139         case HbInputButton::ButtonKeyCodeEnter:
   149     case HbInputButton::ButtonKeyCodeEnter:
   140             mIcons.replace(HbInputButton::ButtonTextIndexPrimary, HbIcon(HbInputButtonIconEnter));
   150         mIcons.replace(HbInputButton::ButtonTextIndexPrimary, HbIcon(HbInputButtonIconEnter));
   141             break;
   151         break;
   142         case HbInputButton::ButtonKeyCodeSpace:
   152     case HbInputButton::ButtonKeyCodeSpace:
   143             mIcons.replace(HbInputButton::ButtonTextIndexPrimary, HbIcon(HbInputButtonIconSpace));
   153         mIcons.replace(HbInputButton::ButtonTextIndexPrimary, HbIcon(HbInputButtonIconSpace));
   144             break;
   154         break;
   145         case HbInputButton::ButtonKeyCodeAlphabet:
   155     case HbInputButton::ButtonKeyCodeAlphabet:
   146             mIcons.replace(HbInputButton::ButtonTextIndexPrimary, HbIcon(HbInputButtonIconSymbol));
   156         mIcons.replace(HbInputButton::ButtonTextIndexPrimary, HbIcon(HbInputButtonIconSymbol));
   147             break;
   157         break;
   148         case HbInputButton::ButtonKeyCodeSmiley:
   158     case HbInputButton::ButtonKeyCodeSmiley:
   149             mIcons.replace(HbInputButton::ButtonTextIndexPrimary, HbIcon(HbInputButtonIconSmiley));
   159         mIcons.replace(HbInputButton::ButtonTextIndexPrimary, HbIcon(HbInputButtonIconSmiley));
   150             break;
   160         break;
   151         default:
   161     default:
   152             break;
   162         break;
   153     }
   163     }
   154 }
   164 }
   155 
   165 
   156 /// @endcond
   166 /// @endcond
       
   167 
       
   168 /*!
       
   169     \enum HbInputButton::HbInputButtonKeyCode
       
   170 
       
   171     This enum defines a set of predefined key codes for input button. ButtonKeyCodeCustom should
       
   172     always be the last item so that all values bigger than ButtonKeyCodeCustom can be interpreted
       
   173     as custom key codes
       
   174  */
       
   175 
       
   176 /*!
       
   177     \enum HbInputButton::HbInputButtonType
       
   178 
       
   179     This enum defines different button types. Button's graphics, text layout and functionality
       
   180     depend of the this type. ButtonTypeCount should always be the last value.
       
   181  */
       
   182 
       
   183 /*!
       
   184     \enum HbInputButton::HbInputButtonState
       
   185 
       
   186     This enum defines button states. State mostly affects the button's visual look in addition
       
   187     to what can be done with the button (i.e. no interaction with disabled buttons, released and
       
   188     latched buttons can be pressed). ButtonStateCount should always be the last value.
       
   189  */
       
   190 
       
   191 /*!
       
   192     \enum HbInputButton::HbInputButtonTextIndex
       
   193 
       
   194     This enum defines set of text indices that can be used when setting texts to different parts of
       
   195     a button using setText function. ButtonTextIndexCount should always be the last value.
       
   196  */
       
   197 
       
   198 /*!
       
   199     \enum HbInputButton::HbInputButtonIconIndex
       
   200 
       
   201     This enum defines set of icon indices that can be used when setting icons to different parts of
       
   202     a button using setIcon function. ButtonIconIndexCount should always be the last value.
       
   203  */
   157 
   204 
   158 /*!
   205 /*!
   159 Constructor
   206 Constructor
   160 */
   207 */
   161 HbInputButton::HbInputButton()
   208 HbInputButton::HbInputButton()
   208     return d->mType;
   255     return d->mType;
   209 }
   256 }
   210 
   257 
   211 /*!
   258 /*!
   212 Updates button's state.
   259 Updates button's state.
       
   260 State change sequence latched, pressed, released will result
       
   261 in a latched state. Otherwise the new state will be the given state.
   213 
   262 
   214 \sa state
   263 \sa state
   215 */
   264 */
   216 void HbInputButton::setState(HbInputButtonState state)
   265 void HbInputButton::setState(HbInputButtonState state)
   217 {
   266 {
   218     Q_D(HbInputButton);
   267     Q_D(HbInputButton);
   219 
   268 
   220     d->mState = state;
   269     if (d->mState == ButtonStatePressed &&
       
   270         d->mPreviousState == ButtonStateLatched &&
       
   271         state == ButtonStateReleased) {
       
   272         d->mState = d->mPreviousState;
       
   273     } else {
       
   274         d->mPreviousState = d->mState;
       
   275         d->mState = state;
       
   276     }
   221 }
   277 }
   222 
   278 
   223 /*!
   279 /*!
   224 Returns button's state.
   280 Returns button's state.
   225 
   281 
   515     Q_D(const HbInputButton);
   571     Q_D(const HbInputButton);
   516 
   572 
   517     return d->mBoundingRect;
   573     return d->mBoundingRect;
   518 }
   574 }
   519 
   575 
       
   576 /*!
       
   577 \brief Returns an active touch area for current button.
       
   578 
       
   579 Button contains bounding rectangle for actual size of the item, while touch area
       
   580 can actually extend over buttons physical boundaries. This is to make interacting
       
   581 with button easier.
       
   582 
       
   583 \return Active touch area as a rectangle, centered to its button.
       
   584 \sa setBoundingRect
       
   585 */
       
   586 QRectF HbInputButton::activeTouchArea() const
       
   587 {
       
   588     Q_D(const HbInputButton);
       
   589 
       
   590     QRectF threshold(d->mBoundingRect);
       
   591 
       
   592     qreal mod_w = d->mBoundingRect.width() * HbInputThresholdMultiplier;
       
   593     qreal mod_h = d->mBoundingRect.height() * HbInputThresholdMultiplier;
       
   594     threshold.adjust(-mod_w, -mod_h, mod_w, mod_h);
       
   595 
       
   596     return threshold;
       
   597 }
       
   598 
       
   599 /*!
       
   600 \brief Set position where last user interaction occurred.
       
   601 */
       
   602 void HbInputButton::setLastTriggeredPosition(const QPointF &position)
       
   603 {
       
   604     Q_D(HbInputButton);
       
   605 
       
   606     d->mLastInteractionPoint = position;
       
   607 }
       
   608 
       
   609 /*!
       
   610 \brief Clear last user interaction data.
       
   611 */
       
   612 void HbInputButton::clearLastTriggeredPosition()
       
   613 {
       
   614     Q_D(HbInputButton);
       
   615 
       
   616     d->mLastInteractionPoint = QPointF(-1, -1);
       
   617 }
       
   618 
       
   619 /*!
       
   620 Checks whether the new touch point position is close enough to the previous
       
   621 one that hit this button so that the new touch point can
       
   622 be interpreted as the same as the old one.
       
   623 */
       
   624 bool HbInputButton::wasTriggeredAt(const QPointF &position) const
       
   625 {
       
   626     Q_D(const HbInputButton);
       
   627 
       
   628     QRectF rect(d->mLastInteractionPoint.x() - HbInputTouchThreshold, d->mLastInteractionPoint.y() - HbInputTouchThreshold,
       
   629                 2 * HbInputTouchThreshold, 2 * HbInputTouchThreshold);
       
   630 
       
   631     return rect.contains(position);
       
   632 }
       
   633 
       
   634 
   520 // End of file
   635 // End of file