src/hbwidgets/widgets/hbpushbutton.cpp
changeset 0 16d8024aca5e
child 1 f7ac710697a9
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 HbWidgets 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 "hbpushbutton.h"
       
    27 #include "hbpushbutton_p.h"
       
    28 #include "hbstyle.h"
       
    29 #include "hbstyleoptionpushbutton.h"
       
    30 #include "hbgesturefilter.h"
       
    31 #include "hbgesture.h"
       
    32 #include "hbframedrawerpool_p.h"
       
    33 #include "hbnamespace.h"
       
    34 #ifdef HB_EFFECTS
       
    35 #include "hbeffect.h"
       
    36 #include "hbeffectinternal_p.h"
       
    37 #endif
       
    38 
       
    39 #include <QGraphicsSceneMouseEvent>
       
    40 #include <QTimer>
       
    41 #include <QGraphicsItem>
       
    42 #include <QDebug>
       
    43 
       
    44 
       
    45 /*!
       
    46     @beta
       
    47     @hbwidgets
       
    48     \class HbPushButton
       
    49 
       
    50     \brief The HbPushButton widget provides a button with command.
       
    51     
       
    52     \image html hbpushbutton.png A pushbutton with text ,icon and tooltip provided . 
       
    53     
       
    54     A push button enable users to perform important commands easily.
       
    55     A push button has a rectangular shape and typically displays a text label describing its command.
       
    56     Push Button can appear in any container except toolbars where ToolButton HbToolButton is used and palettes.
       
    57     A simple push button can be created as shown in the example below.
       
    58     \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,16}
       
    59 
       
    60     A Push buttons display a textual label. Pushbutton also
       
    61     can have icon.These can be set using the constructors and changed later using setText(),setIcon( ).
       
    62     In addition to this a push button also displays additional text label.
       
    63     Additional text can be set using setAdditionalText().
       
    64     Aligning of icon , text and additional text can be changed using setStretched()true/false property.
       
    65     Default with setStretched()=false it will place icon and text vertically.
       
    66 
       
    67     \image html buttonIconTextV.png A pushbutton with icon ,text.
       
    68 
       
    69     and along with additional text will place icon horizontally to both texts ,where both text comes vertically aligned.    
       
    70     
       
    71     \image html buttonIconTextAdditionalTextV.png A pushbutton with icon ,text and additional text.
       
    72 
       
    73     For setStretched()=true, it will stretch layout to have icon and text horizontally,side by side.
       
    74 
       
    75     \image html buttonIconTextH.png A pushbutton with icon ,text.
       
    76 
       
    77     and along with additional text will place all of them to Horizontally in one line .
       
    78     
       
    79     \image html buttonIconTextAdditionalTextH.png A pushbutton with custom icon ,text and additional text.
       
    80 
       
    81     Pushbutton can have both text and additoinal text placed vertically,this can be used in case Dialer button or 
       
    82     Virtual key pad buttons with stretched = false.Pushbutton doesn't supports both text in one line 
       
    83     stretched = true is not valid.
       
    84     Pushbutton will not have stretch layout by default.
       
    85        
       
    86     \image html buttonTextAdditional.png A pushbutton with additional text and text.
       
    87 
       
    88     Pushbutton can have different states like normal, pressed, disabled, latched and focused.
       
    89     A push button emits the signal clicked() when it is activated by tapping it.
       
    90     Push buttons also provide less commonly used signals, for example, pressed() and
       
    91     released() and emits longPress() on long press.
       
    92 
       
    93     Alignment of the text within the text and additionaltext items can be set using 
       
    94     setTextAlignment() and setAdditionalText(), default text alignment is Qt::AlignHCenter | Qt::AlignVCenter
       
    95 
       
    96     The push button is derived from HbWidget so it can be added to any QGraphicsLayout
       
    97     instance for layouting.
       
    98     \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,17}
       
    99 
       
   100     Push button can also be used as toggle button , following is code snippet
       
   101      \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,30}
       
   102 
       
   103     and onOffButton as 
       
   104      \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,29}
       
   105 
       
   106     Push button will come up with additional text following is code snippet.
       
   107     1. Push button with icon , text and additionalText
       
   108      \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,35}
       
   109 
       
   110     2. Pushbutton with additionalText and text.
       
   111      \snippet{ultimatecodesnippet/ultimatecodesnippet.cpp,36}
       
   112 
       
   113     \sa HbAbstractButton , HbToolButton
       
   114 */
       
   115     
       
   116 
       
   117 /*!
       
   118     \reimp
       
   119     \fn int HbPushButton::type() const
       
   120  */
       
   121 
       
   122 /*!
       
   123     \fn void HbPushButton::longPress( QPointF )
       
   124     This signal is emitted in case of longpress.
       
   125  */
       
   126 
       
   127 HbPushButtonPrivate::HbPushButtonPrivate() :
       
   128     textItem(0),
       
   129     additionalTextItem(0),
       
   130     touchArea(0),
       
   131     iconItem(0),
       
   132     frameItem(0),
       
   133     focusItem(0),
       
   134     orientation(Qt::Vertical),
       
   135     backgroundFrameDrawer(0),
       
   136     gestureFilter(0),
       
   137     gestureLongpressed(0),
       
   138     longPressTimer(0),
       
   139     textAlignment(Qt::AlignHCenter | Qt::AlignVCenter ),
       
   140     additionalTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter ),
       
   141     stretched(false),
       
   142     navigationKeyPress(false),
       
   143     hasTextAlignment(false),
       
   144     hasAdditionalTextAlignment(false)
       
   145 {
       
   146 #ifdef HB_EFFECTS
       
   147      //HbEffectInternal::add(HB_PUSHBUTTON_TYPE,"pushbutton_pressed.fxml", "pressed");
       
   148      HbEffectInternal::add(HB_PUSHBUTTON_TYPE,"pushbutton_released", "released");
       
   149 #endif
       
   150   
       
   151 }
       
   152 
       
   153 /*
       
   154   Destructor.
       
   155  */
       
   156 HbPushButtonPrivate::~HbPushButtonPrivate()
       
   157 {
       
   158     if(gestureFilter){
       
   159         delete gestureFilter;
       
   160         gestureFilter = NULL;
       
   161     }
       
   162 }
       
   163 
       
   164 /*
       
   165   createPrimitives.
       
   166  */
       
   167 void HbPushButtonPrivate::createPrimitives()
       
   168 {
       
   169     Q_Q(HbPushButton);
       
   170 
       
   171     if ( !frameItem ) {
       
   172         frameItem = q->style()->createPrimitive( HbStyle::P_PushButton_background, q );
       
   173     }
       
   174     if ( !text.isNull() ) {
       
   175         if ( !textItem ) {
       
   176             textItem = q->style()->createPrimitive( HbStyle::P_PushButton_text, q );
       
   177         }
       
   178     } else {
       
   179         if( textItem ){
       
   180             delete textItem;
       
   181         }
       
   182         textItem = 0;
       
   183     }
       
   184     if ( !additionalText.isNull() ) {
       
   185         if ( !additionalTextItem ) {
       
   186             additionalTextItem = q->style()->createPrimitive( HbStyle::P_PushButton_additionaltext, q );
       
   187         }
       
   188     } else {
       
   189         if( additionalTextItem ){
       
   190             delete additionalTextItem;
       
   191         }
       
   192         additionalTextItem = 0;
       
   193     }
       
   194 
       
   195     if ( !icon.isNull() ) {
       
   196         if ( !iconItem ) {
       
   197             iconItem = q->style()->createPrimitive( HbStyle::P_PushButton_icon, q);
       
   198         }
       
   199     } else {
       
   200         if( iconItem ){
       
   201             delete iconItem;
       
   202         }
       
   203         iconItem = 0;
       
   204     }
       
   205 
       
   206     if( !touchArea ) {
       
   207         touchArea = q->style()->createPrimitive( HbStyle::P_PushButton_toucharea, q );
       
   208     }
       
   209 
       
   210     if( !focusItem ) {
       
   211     focusItem = q->style()->createPrimitive(HbStyle::P_PushButton_focus, q);
       
   212     //focus primitive is will not be visible by default, 
       
   213     //only in case of keyboard focus events it will be Visible.
       
   214     focusItem->setVisible( false );
       
   215     }
       
   216 }
       
   217 
       
   218 /*
       
   219   initGesture.
       
   220  */
       
   221 void HbPushButtonPrivate::initGesture()
       
   222 {
       
   223     Q_Q(HbPushButton);
       
   224     // Create gesture filter
       
   225     gestureFilter = new HbGestureSceneFilter( Qt::LeftButton, q );
       
   226     // Add gestures for longpress
       
   227     gestureLongpressed = new HbGesture( HbGesture::longpress,5 );
       
   228     gestureFilter->addGesture( gestureLongpressed );
       
   229 
       
   230     // scene event filter for gesture can be installed
       
   231     // if widget is already added to scene
       
   232     if (q->scene()) {
       
   233         q->installSceneEventFilter(gestureFilter);
       
   234         if (touchArea) {
       
   235             touchArea->installSceneEventFilter(gestureFilter);
       
   236         }
       
   237     }
       
   238 
       
   239 }
       
   240 
       
   241 void HbPushButtonPrivate::_q_handleLongPress(QPointF point)
       
   242 {
       
   243     Q_Q(HbPushButton);
       
   244     if(!longPress){
       
   245         longPress = true;
       
   246         emit q->longPress( point );
       
   247     }
       
   248 }
       
   249 
       
   250 void HbPushButtonPrivate::_q_handleLongKeyPress( )
       
   251 {
       
   252     Q_Q(HbPushButton);
       
   253     if(!longPress){
       
   254         longPress = true;
       
   255         emit q->longPress( q->pos() );
       
   256     }
       
   257 }
       
   258 
       
   259 void HbPushButtonPrivate::initialize()
       
   260 {
       
   261     Q_Q(HbPushButton);
       
   262     q_ptr = q;
       
   263     createPrimitives();
       
   264     initGesture();
       
   265 
       
   266 }
       
   267 
       
   268 /*!
       
   269     @beta
       
   270     Constructs a new HbPushButton with \a parent.
       
   271  */
       
   272 HbPushButton::HbPushButton( QGraphicsItem *parent )
       
   273     : HbAbstractButton( *new HbPushButtonPrivate, parent )
       
   274 {
       
   275     Q_D(HbPushButton);
       
   276     d->initialize();
       
   277     connect( d->gestureLongpressed, SIGNAL(longPress(QPointF)),
       
   278         this, SLOT(_q_handleLongPress(QPointF)));
       
   279     setProperty("state", "normal");
       
   280      
       
   281 }
       
   282 
       
   283 /*!
       
   284     @beta
       
   285     Constructs a new HbPushButton with \a text and \a parent.
       
   286  */
       
   287 HbPushButton::HbPushButton(const QString &text, QGraphicsItem *parent)
       
   288     : HbAbstractButton(*new HbPushButtonPrivate, parent)
       
   289 {
       
   290     Q_D(HbPushButton);
       
   291     d->text = text;
       
   292     d->initialize();
       
   293     connect( d->gestureLongpressed, SIGNAL(longPress(QPointF)),
       
   294         this, SLOT(_q_handleLongPress(QPointF)) );
       
   295     setProperty("state", "normal");
       
   296      
       
   297 
       
   298 }
       
   299 
       
   300 /*!
       
   301     @beta
       
   302     Constructs a new HbPushButton with \a icon, \a text and \a parent.
       
   303  */
       
   304 HbPushButton::HbPushButton( const HbIcon &icon, const QString &text, QGraphicsItem *parent )
       
   305     : HbAbstractButton( *new HbPushButtonPrivate, parent )
       
   306 {
       
   307     Q_D(HbPushButton);
       
   308     d->icon = icon;
       
   309     d->text = text;
       
   310     d->initialize();
       
   311     connect( d->gestureLongpressed, SIGNAL(longPress(QPointF)),
       
   312         this, SLOT(_q_handleLongPress(QPointF)) );
       
   313     setProperty("state", "normal");
       
   314 
       
   315 }
       
   316 
       
   317 /*!
       
   318     \deprecated HbPushButton::HbPushButton(const HbIcon&, const QString&, Qt::Orientation, QGraphicsItem*)
       
   319         is deprecated.
       
   320     Constructs a new HbPushButton with \a icon, \a text \a orientation and \a parent.
       
   321  */
       
   322 HbPushButton::HbPushButton( const HbIcon &icon, const QString &text,
       
   323     Qt::Orientation orientation, QGraphicsItem *parent )
       
   324     : HbAbstractButton( *new HbPushButtonPrivate, parent )
       
   325 {
       
   326     Q_D(HbPushButton);
       
   327     d->icon = icon;
       
   328     d->text = text;
       
   329     d->initialize();
       
   330     d->orientation = orientation;
       
   331     connect( d->gestureLongpressed, SIGNAL(longPress(QPointF)),
       
   332         this, SLOT(_q_handleLongPress(QPointF)) );
       
   333     setProperty("state", "normal");
       
   334             
       
   335 }
       
   336 
       
   337 /*!
       
   338     Destructs the push button.
       
   339  */
       
   340 HbPushButton::~HbPushButton()
       
   341 {
       
   342     Q_D(HbPushButton);
       
   343     if( d->gestureFilter ) {
       
   344         removeSceneEventFilter( d->gestureFilter );
       
   345     }
       
   346 }
       
   347 
       
   348 /*!
       
   349     @beta
       
   350     Sets the \a background of the button.
       
   351 
       
   352     \note The background can contain different images for pressed and released states.
       
   353 
       
   354     \sa background()
       
   355  */
       
   356 void  HbPushButton::setBackground( const HbIcon &background )
       
   357 {
       
   358     Q_D(HbPushButton);
       
   359     if( !d->frameItem ) {
       
   360        d->createPrimitives();
       
   361     }
       
   362     if ( d->background != background ) {
       
   363         d->background = background;
       
   364         d->backgroundFrameDrawer = 0;
       
   365         HbStyleOptionPushButton buttonOption;
       
   366         initStyleOption( &buttonOption );
       
   367         style()->updatePrimitive( d->frameItem, HbStyle::P_PushButton_background, &buttonOption );
       
   368     }
       
   369 }
       
   370 
       
   371 /*!
       
   372     @beta
       
   373     Returns the background of the button.
       
   374 
       
   375     \sa setBackground()
       
   376  */
       
   377 HbIcon HbPushButton::background() const
       
   378 {
       
   379     Q_D(const HbPushButton);
       
   380     return d->background;
       
   381 }
       
   382 
       
   383 /*!
       
   384     @beta
       
   385     Sets the \a framebackground of the button.
       
   386 
       
   387     \note The background can contain different types of frame drawers(Nine piece,
       
   388     three piece or single).Ownership is transferred to the Pushbutton
       
   389 
       
   390     \sa frameBackground()
       
   391  */
       
   392 void  HbPushButton::setFrameBackground( HbFrameDrawer *backgroundFrameDrawer )
       
   393 {
       
   394     Q_D(HbPushButton);
       
   395     if (d->backgroundFrameDrawer != backgroundFrameDrawer) {
       
   396         if( !d->frameItem ) {
       
   397             d->createPrimitives();
       
   398         }
       
   399         d->backgroundFrameDrawer=backgroundFrameDrawer;
       
   400         d->background=HbIcon();
       
   401         HbStyleOptionPushButton buttonOption;
       
   402         initStyleOption( &buttonOption );
       
   403         style()->updatePrimitive( d->frameItem, HbStyle::P_PushButton_background, &buttonOption );
       
   404     }
       
   405 }
       
   406 
       
   407 /*!
       
   408     @beta
       
   409     Returns the background of the button.
       
   410 
       
   411     \sa setBackground()
       
   412  */
       
   413 HbFrameDrawer* HbPushButton::frameBackground() const
       
   414 {
       
   415     Q_D(const HbPushButton);
       
   416     return ( d->backgroundFrameDrawer );
       
   417 }
       
   418 
       
   419 
       
   420 /*!
       
   421     @beta
       
   422     Sets the \a text shown on the button.
       
   423 
       
   424     \sa text()
       
   425  */
       
   426 void HbPushButton::setText( const QString &text )
       
   427 {
       
   428     Q_D(HbPushButton);
       
   429     if ( d->text != text ) {
       
   430         bool doPolish = text.isEmpty() || d->text.isEmpty();
       
   431         d->text = text;
       
   432         d->createPrimitives();
       
   433         //updatePrimitives();
       
   434         if( d->textItem ){
       
   435             HbStyleOptionPushButton buttonOption;
       
   436             initStyleOption(&buttonOption);
       
   437             style()->updatePrimitive( d->textItem, HbStyle::P_PushButton_text, &buttonOption);
       
   438         }
       
   439         if(doPolish) {
       
   440             repolish();
       
   441         }
       
   442     }
       
   443 }
       
   444 
       
   445 
       
   446 /*!
       
   447     @beta
       
   448     Returns the text shown on the button.
       
   449 
       
   450     \sa setText()
       
   451  */
       
   452 QString HbPushButton::text() const
       
   453 {
       
   454     Q_D(const HbPushButton);
       
   455     return d->text;
       
   456 }
       
   457 
       
   458 
       
   459 /*!
       
   460     @beta
       
   461     Sets the \a additional text shown on the button.
       
   462     Additional text is only shown in case 
       
   463     1. with icon and text in Qt::Horizontal alignment.
       
   464     2. with additionalText and text in Qt::Vertical alignment.
       
   465 
       
   466     \sa additionalText()
       
   467  */
       
   468 void HbPushButton::setAdditionalText( const QString &additionalText )
       
   469 {
       
   470     Q_D(HbPushButton);
       
   471     if ( d->additionalText != additionalText ) {
       
   472         bool doPolish = additionalText.isEmpty() || d->additionalText.isEmpty();
       
   473         d->additionalText = additionalText;
       
   474         d->createPrimitives();
       
   475         // updatePrimitives();
       
   476         if(d->additionalTextItem){
       
   477             HbStyleOptionPushButton buttonOption;
       
   478             initStyleOption(&buttonOption);
       
   479             style()->updatePrimitive( d->additionalTextItem, HbStyle::P_PushButton_additionaltext, &buttonOption);
       
   480         }
       
   481         if( doPolish ) {
       
   482             repolish();
       
   483         }
       
   484     }
       
   485 }
       
   486 
       
   487 /*!
       
   488     @beta
       
   489     Returns the additional text shown on the button.
       
   490 
       
   491     \sa setAdditionalText()
       
   492  */
       
   493 QString HbPushButton::additionalText() const
       
   494 {
       
   495     Q_D(const HbPushButton);
       
   496     return d->additionalText;
       
   497 }
       
   498 
       
   499 /*!
       
   500     @beta
       
   501     Sets the \a icon shown on the button.
       
   502 
       
   503     \note The icon can contain images for different modes.
       
   504 
       
   505     \sa icon()
       
   506  */
       
   507 void HbPushButton::setIcon(const HbIcon &icon)
       
   508 {
       
   509     Q_D(HbPushButton);
       
   510 
       
   511     if ( d->icon != icon ) {
       
   512         bool doPolish = icon.isNull() || d->icon.isNull();
       
   513         d->icon = icon;
       
   514         d->createPrimitives();
       
   515         //updatePrimitives();
       
   516         if(d->iconItem ){
       
   517             HbStyleOptionPushButton buttonOption;
       
   518             initStyleOption( &buttonOption );
       
   519             style()->updatePrimitive( d->iconItem, HbStyle::P_PushButton_icon, &buttonOption );
       
   520 
       
   521         }
       
   522         if( doPolish ){
       
   523             repolish();
       
   524         }
       
   525     }
       
   526 }
       
   527 
       
   528 /*!
       
   529     @beta
       
   530     Returns the icon shown on the button.
       
   531     \sa setIcon()
       
   532  */
       
   533 
       
   534 HbIcon HbPushButton::icon() const
       
   535 {
       
   536     Q_D(const HbPushButton);
       
   537     return d->icon;
       
   538 }
       
   539 
       
   540 
       
   541 /*!
       
   542     \deprecated HbPushButton::setOrientation( Qt::Orientation )
       
   543         is deprecated. Please use HbPushButton::setStretched(bool) instead 
       
   544 
       
   545     Set the orientation.Default orientation is Qt::Vertical.
       
   546     In case of icon ,text and additional text it has to be set to Qt::Horizontal.
       
   547     In case of additionalText and text it has to be set to Qt:Vertical.
       
   548     \sa orientation()
       
   549 */
       
   550 void HbPushButton::setOrientation( Qt::Orientation orientation )
       
   551 {
       
   552     Q_D(HbPushButton);
       
   553     if( d->orientation != orientation ) {
       
   554         d->orientation = orientation;
       
   555         // calling setStretchedMode internaly this api is going to be depricated.
       
   556         bool stretched = 
       
   557             (d->orientation == Qt::Vertical)?(false):(true);
       
   558         setStretched( stretched );        
       
   559     }
       
   560     qDebug()<<"This api is deprecated use HbPushButton::setStretched( bool stretched )";
       
   561 }
       
   562 
       
   563 /*!
       
   564     \deprecated HbPushButton::orientation() const
       
   565         is deprecated.Please use bool HbPushButton::isStretched() const instead 
       
   566 
       
   567     Returns orientation of button
       
   568     \sa setOrientation ()
       
   569 */
       
   570 Qt::Orientation HbPushButton::orientation() const
       
   571 {
       
   572     Q_D(const HbPushButton);
       
   573     qDebug()<<"This api is deprecated use bool HbPushButton::isStretched() const";
       
   574     return d->orientation;    
       
   575 }
       
   576 
       
   577 /*!
       
   578     Set the text alignment for primarytext  and additional text.
       
   579     The default alignment is Qt::AlignHCenter | Qt::AlignVCenter.
       
   580     \sa textAlignment()
       
   581 */
       
   582 void HbPushButton::setTextAlignment( Qt::Alignment alignment )
       
   583 {
       
   584     Q_D(HbPushButton); 
       
   585 
       
   586     //HbWidgetBase* textItem = static_cast<HbWidgetBase*>(d->textItem);		
       
   587     if(!d->textItem){
       
   588         //need to create text item if user call alignment api before setText.
       
   589         d->textItem = style()->createPrimitive( HbStyle::P_PushButton_text, this);
       
   590     }
       
   591     //HbWidgetBasePrivate *textItem_p = HbWidgetBasePrivate::d_ptr(textItem);	
       
   592     // check for textitem and api protection flag
       
   593     if(alignment != d->textAlignment ){
       
   594         //take the alignment 
       
   595         d->textAlignment = alignment;
       
   596         //set the api protection flag 
       
   597         //HbWidgetBasePrivate::d_ptr(textItem)->setApiProtectionFlag(HbWidgetBasePrivate::AC_TextAlign,true);
       
   598         d->hasTextAlignment = true;
       
   599         HbStyleOptionPushButton buttonOption;            
       
   600         initStyleOption( &buttonOption );
       
   601         style()->updatePrimitive( 
       
   602         d->textItem, HbStyle::P_PushButton_text, &buttonOption );
       
   603     }
       
   604     d->hasTextAlignment = true;
       
   605 }
       
   606 
       
   607 /*!
       
   608     Returns the text alignment for primary text and additional text
       
   609     \sa setTextAlignment()
       
   610 */
       
   611 Qt::Alignment HbPushButton::textAlignment( ) const
       
   612 {
       
   613     Q_D(const HbPushButton);
       
   614     return d->textAlignment;
       
   615 }
       
   616 
       
   617 
       
   618 /*!
       
   619     Set the text alignment for additional text.
       
   620     The default alignment is Qt::AlignHCenter | Qt::AlignVCenter.
       
   621     \sa additionalTextAlignment()
       
   622 */
       
   623 void HbPushButton::setAdditionalTextAlignment( Qt::Alignment alignment )
       
   624 {
       
   625     Q_D(HbPushButton); 
       
   626 
       
   627     //HbWidgetBase* additionalTextItem = static_cast<HbWidgetBase*>(d->additionalTextItem);	
       
   628     if(!d->additionalTextItem){
       
   629         //need to create text item if user call alignment api before setAdditionalText.
       
   630         d->additionalTextItem = style()->createPrimitive( HbStyle::P_PushButton_additionaltext, this);
       
   631     }
       
   632     // check for textitem and api protection flag
       
   633     if(alignment != d->additionalTextAlignment ){
       
   634         //take the alignment 
       
   635         d->additionalTextAlignment = alignment;
       
   636         //set the api protection flag 
       
   637         //HbWidgetBasePrivate::d_ptr(additionalTextItem)->setApiProtectionFlag(HbWidgetBasePrivate::AC_TextAlign,true);
       
   638         d->hasAdditionalTextAlignment = true;
       
   639         HbStyleOptionPushButton buttonOption;            
       
   640         initStyleOption( &buttonOption );
       
   641         style()->updatePrimitive( 
       
   642         d->additionalTextItem, HbStyle::P_PushButton_additionaltext, &buttonOption );
       
   643     }
       
   644 }
       
   645 
       
   646 /*!
       
   647     Returns the text alignment for additional text
       
   648     \sa setAdditionalTextAlignment()
       
   649 */
       
   650 Qt::Alignment HbPushButton::additionalTextAlignment( ) const
       
   651 {
       
   652     Q_D(const HbPushButton);
       
   653     return d->additionalTextAlignment;
       
   654 }
       
   655 
       
   656 /*!
       
   657     Sets the stretched layout for button by default it set to true. setStretched true/false.
       
   658     For the case icon and text with value false will appear icon and text vertically aligned.
       
   659     and with value true will appear icon and text horizontally aligned.
       
   660     For the case icon , text and additional text with value false will appear like 
       
   661     both text will be vertically aligned and icon will be horizontally.
       
   662     and with value true icon , text and additionaltext will come horizontally aligned in one line
       
   663     For the case text and additonaltext(dialer button case ) only value false is applicable and both texts will 
       
   664     appeare vertically 
       
   665 
       
   666     \sa isStretched()
       
   667 */
       
   668 void HbPushButton::setStretched( bool stretched )
       
   669 {
       
   670     Q_D(HbPushButton);
       
   671     if( d->stretched != stretched ) {
       
   672         d->stretched = stretched;
       
   673         repolish();
       
   674     }
       
   675 }
       
   676 
       
   677 /*!
       
   678     Returns the whether button has streteched layout or not.
       
   679     \sa setStretched ()
       
   680 */
       
   681 bool HbPushButton::isStretched() const
       
   682 {
       
   683     Q_D(const HbPushButton);
       
   684     return d->stretched;
       
   685 }
       
   686 
       
   687 /*!
       
   688     \reimp
       
   689  */
       
   690 QGraphicsItem* HbPushButton::primitive( HbStyle::Primitive primitive ) const
       
   691 {
       
   692     Q_D(const HbPushButton);
       
   693 
       
   694     switch (primitive) {
       
   695         case HbStyle::P_PushButton_background:
       
   696             return d->frameItem;
       
   697         case HbStyle::P_PushButton_icon:
       
   698             return d->iconItem;
       
   699         case HbStyle::P_PushButton_text:
       
   700             return d->textItem;
       
   701         case HbStyle::P_PushButton_toucharea:
       
   702             return d->touchArea;
       
   703         case HbStyle::P_PushButton_additionaltext:
       
   704             return d->additionalTextItem;
       
   705         case HbStyle::P_PushButton_focus:
       
   706             return d->focusItem;
       
   707         default:
       
   708             return 0;
       
   709     }
       
   710 }
       
   711 
       
   712 /*!
       
   713     \reimp
       
   714  */
       
   715 void HbPushButton::recreatePrimitives()
       
   716 {
       
   717    Q_D(HbPushButton);
       
   718    HbWidget::recreatePrimitives();
       
   719 
       
   720    delete d->frameItem;
       
   721    d->frameItem = 0;
       
   722    delete d->iconItem;
       
   723    d->iconItem = 0;
       
   724    delete d->textItem;
       
   725    d->textItem = 0;
       
   726    delete d->touchArea;
       
   727    d->touchArea = 0;
       
   728    delete d->additionalTextItem;
       
   729    d->additionalTextItem = 0;
       
   730    delete d->focusItem;
       
   731    d->focusItem = 0;
       
   732    
       
   733    d->createPrimitives();
       
   734    setFrameBackground(0);
       
   735 }
       
   736 /*!
       
   737     \reimp
       
   738  */
       
   739 void HbPushButton::updatePrimitives()
       
   740 {
       
   741     Q_D(HbPushButton);
       
   742     HbWidget::updatePrimitives();
       
   743 
       
   744     HbStyleOptionPushButton buttonOption;
       
   745     initStyleOption(&buttonOption);
       
   746     if ( d->textItem ) {
       
   747         style()->updatePrimitive( d->textItem, HbStyle::P_PushButton_text, &buttonOption );
       
   748     }
       
   749     if ( d->touchArea ) {
       
   750         style()->updatePrimitive( d->touchArea, HbStyle::P_PushButton_toucharea, &buttonOption );
       
   751     }	
       
   752     if( d->additionalTextItem ){
       
   753         style()->updatePrimitive(
       
   754             d->additionalTextItem,HbStyle::P_PushButton_additionaltext,&buttonOption );
       
   755     }
       
   756     if ( d->iconItem) {
       
   757         style()->updatePrimitive( d->iconItem, HbStyle::P_PushButton_icon, &buttonOption );
       
   758     }
       
   759     if ( d->frameItem ){
       
   760         style()->updatePrimitive( d->frameItem, HbStyle::P_PushButton_background, &buttonOption );
       
   761     }
       
   762     // update will happen only for keyevents when focusItem will be visible.
       
   763     if( d->focusItem && hasFocus() && d->focusItem->isVisible() ) {
       
   764         style()->updatePrimitive( d->focusItem,HbStyle::P_PushButton_focus, &buttonOption );
       
   765     }
       
   766 }
       
   767 
       
   768 /*!
       
   769     \internal
       
   770  */
       
   771 HbPushButton::HbPushButton(HbPushButtonPrivate &dd, QGraphicsItem * parent) :
       
   772     HbAbstractButton(dd, parent)
       
   773 {
       
   774     Q_D(HbPushButton);
       
   775     d->initGesture();
       
   776     connect( d->gestureLongpressed, SIGNAL(longPress(QPointF)),
       
   777         this, SLOT(_q_handleLongPress(QPointF)) );
       
   778 }
       
   779 
       
   780 /*!
       
   781     Initializes \a option with the values from this HbPushButton. 
       
   782     This method is useful for subclasses when they need a HbStyleOptionPushButton,
       
   783     but don't want to fill in all the information themselves.
       
   784  */
       
   785 void HbPushButton::initStyleOption(HbStyleOptionPushButton *option) const
       
   786 {
       
   787     Q_D(const HbPushButton);
       
   788 
       
   789     HbAbstractButton::initStyleOption(option);
       
   790 
       
   791     Q_ASSERT(option);
       
   792     option->background = d->background;
       
   793     option->text = d->text;
       
   794     option->additionalText = d->additionalText;
       
   795     option->icon = d->icon;
       
   796     option->isCheckable = d->checkable;
       
   797     option->textAlignment = d->textAlignment;
       
   798     option->additionalTextAlignment = d->additionalTextAlignment;
       
   799     option->hasTextAlignment = d->hasTextAlignment;
       
   800     option->hasAdditionalTextAlignment = d->hasAdditionalTextAlignment;
       
   801     if(option->backgroundFrameDrawer) {
       
   802         HbFrameDrawerPool::release(option->backgroundFrameDrawer);
       
   803         option->backgroundFrameDrawer = 0;
       
   804     }
       
   805     option->backgroundFrameDrawer=( d->backgroundFrameDrawer );
       
   806 }
       
   807 
       
   808 /*!
       
   809     \reimp
       
   810  */
       
   811 void HbPushButton::keyPressEvent(QKeyEvent *event)
       
   812 {
       
   813     Q_D(HbPushButton);
       
   814 
       
   815     switch (event->key()) {
       
   816         case Qt::Key_Select:
       
   817         case Qt::Key_Enter:
       
   818         case Qt::Key_Return:{
       
   819                 if (!event->isAutoRepeat() && !d->autoRepeat && !d->longPressTimer) {
       
   820                     d->longPressTimer = new QTimer();
       
   821                     d->longPressTimer->setInterval(300);
       
   822                     connect( d->longPressTimer, SIGNAL(timeout()), this, SLOT(_q_handleLongKeyPress()) );
       
   823                     d->longPressTimer->start();
       
   824                 }else{
       
   825                     #ifdef HB_EFFECTS
       
   826                         HbEffect::start( this, HB_PUSHBUTTON_TYPE, "pressed" );
       
   827                     #endif
       
   828                 }
       
   829             }
       
   830             break;
       
   831         case Qt::Key_Up:
       
   832         case Qt::Key_Left:
       
   833         case Qt::Key_Right:
       
   834         case Qt::Key_Down:{
       
   835                 d->navigationKeyPress = true;
       
   836             }
       
   837             break;       
       
   838     }
       
   839     HbAbstractButton::keyPressEvent(event);
       
   840 }
       
   841 
       
   842 
       
   843 /*!
       
   844     \reimp
       
   845  */
       
   846 void HbPushButton::keyReleaseEvent(QKeyEvent *event)
       
   847 {
       
   848     Q_D(HbPushButton);
       
   849     switch(event->key()){
       
   850         case Qt::Key_Select:
       
   851         case Qt::Key_Enter:
       
   852         case Qt::Key_Return:{
       
   853                 #ifdef HB_EFFECTS
       
   854                     HbEffect::start( this, HB_PUSHBUTTON_TYPE, "released" );
       
   855                 #endif                
       
   856                 if( d->longPressTimer && d->longPressTimer->isActive() ) {                    
       
   857                     d->longPressTimer->stop();
       
   858                     delete d->longPressTimer;
       
   859                     d->longPressTimer = 0;                    
       
   860                 } 
       
   861             }
       
   862             break;
       
   863             case Qt::Key_Up:
       
   864             case Qt::Key_Left:
       
   865             case Qt::Key_Right:
       
   866             case Qt::Key_Down:{
       
   867                     d->navigationKeyPress = false;
       
   868                 }
       
   869             break;       
       
   870     }
       
   871     HbAbstractButton::keyReleaseEvent( event );
       
   872 }
       
   873 
       
   874 /*!
       
   875     \reimp
       
   876  */
       
   877 void HbPushButton::mousePressEvent( QGraphicsSceneMouseEvent *event )
       
   878 {
       
   879     Q_D(HbPushButton);
       
   880     HbAbstractButton::mousePressEvent(event);
       
   881     HbStyleOptionPushButton buttonOption;
       
   882     initStyleOption( &buttonOption );
       
   883     if( d->frameItem ){
       
   884         style()->updatePrimitive( d->frameItem, HbStyle::P_PushButton_background, &buttonOption );
       
   885     }
       
   886 #ifdef HB_EFFECTS
       
   887     if ( hitButton(event->pos()) ){
       
   888         HbEffect::start( this, HB_PUSHBUTTON_TYPE, "pressed" );
       
   889     }
       
   890 #endif
       
   891     setProperty("state", "pressed");    
       
   892 }
       
   893 
       
   894 /*!
       
   895     \reimp
       
   896  */
       
   897 void HbPushButton::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
       
   898 {
       
   899     Q_D(HbPushButton);
       
   900     HbAbstractButton::mouseReleaseEvent(event);
       
   901     HbStyleOptionPushButton buttonOption;
       
   902     initStyleOption( &buttonOption );     
       
   903     if( d->frameItem ){
       
   904         style()->updatePrimitive( d->frameItem, HbStyle::P_PushButton_background, &buttonOption );
       
   905     }
       
   906 #ifdef HB_EFFECTS
       
   907     if ( hitButton(event->pos()) ){
       
   908         HbEffect::start( this, HB_PUSHBUTTON_TYPE, "released" );
       
   909     }
       
   910 #endif
       
   911     setProperty("state", "normal"); 
       
   912 }
       
   913 
       
   914 /*!
       
   915     \reimp
       
   916  */
       
   917 void HbPushButton::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
       
   918 {
       
   919     Q_D(HbPushButton);
       
   920     HbAbstractButton::mouseMoveEvent(event);
       
   921     HbStyleOptionPushButton buttonOption;
       
   922     initStyleOption( &buttonOption );
       
   923     if( d->frameItem ){
       
   924         style()->updatePrimitive( d->frameItem, HbStyle::P_PushButton_background, &buttonOption );
       
   925     }
       
   926     if (d->down) {
       
   927         setProperty("state", "pressed"); 
       
   928     } else {
       
   929         setProperty("state", "normal"); 
       
   930     }
       
   931 }
       
   932 
       
   933 
       
   934 /*!
       
   935     \reimp
       
   936  */
       
   937 void HbPushButton::resizeEvent(QGraphicsSceneResizeEvent *event)
       
   938 {
       
   939     HbAbstractButton::resizeEvent(event);
       
   940 }
       
   941 /*!
       
   942   reimp
       
   943 
       
   944 */
       
   945 void HbPushButton::polish( HbStyleParameters& params )
       
   946 {
       
   947     Q_D( HbPushButton );
       
   948     if(d->stretched && (d->textItem && d->additionalTextItem && !d->iconItem)){
       
   949         d->stretched = false;
       
   950         qWarning()<<"Warning::Invalid Layout: Text and additonalText horizontal appearance not supported";
       
   951     }
       
   952     HbAbstractButton::polish( params );
       
   953 }
       
   954 
       
   955 
       
   956 /*!
       
   957     \reimp
       
   958  */
       
   959 void HbPushButton::focusInEvent(QFocusEvent *event)
       
   960 {
       
   961     Q_D(HbPushButton);
       
   962     if((event->reason() == Qt::TabFocusReason || event->reason() == Qt::BacktabFocusReason) 
       
   963         && ( d->navigationKeyPress ) ) {
       
   964         if(d->focusItem) {
       
   965             d->focusItem->setVisible(true);
       
   966             if( hasFocus()){
       
   967                 HbStyleOptionPushButton buttonOption;
       
   968                 initStyleOption( &buttonOption );
       
   969                 style()->updatePrimitive( d->focusItem,HbStyle::P_PushButton_focus, &buttonOption );
       
   970             }
       
   971         } else {
       
   972             d->focusItem = style()->createPrimitive(HbStyle::P_PushButton_focus, this);
       
   973         }
       
   974     }
       
   975     HbAbstractButton::focusInEvent(event);
       
   976 }
       
   977 
       
   978 /*!
       
   979     \reimp
       
   980  */
       
   981 void HbPushButton::focusOutEvent(QFocusEvent *event)
       
   982 {
       
   983     Q_D(HbPushButton);
       
   984     if(d->focusItem) {
       
   985         d->focusItem->setVisible(false);
       
   986     }
       
   987     HbAbstractButton::focusOutEvent(event);
       
   988 }
       
   989 
       
   990 /*!
       
   991     \reimp
       
   992  */
       
   993 QVariant HbPushButton::itemChange(GraphicsItemChange change, const QVariant &value)
       
   994 {
       
   995     Q_D(HbPushButton);
       
   996 
       
   997     switch ( change ) {
       
   998         case ItemEnabledHasChanged:
       
   999         case ItemVisibleChange: {
       
  1000                 updatePrimitives( );
       
  1001             }
       
  1002             break;
       
  1003         case ItemSceneHasChanged: {
       
  1004                 // scene event filter for gestures can be installed
       
  1005                 // once the widget has been added to the scene
       
  1006                 if(!value.isNull() && d->gestureFilter) {
       
  1007                     installSceneEventFilter(d->gestureFilter);
       
  1008                     if (d->touchArea) {
       
  1009                         d->touchArea->installSceneEventFilter(d->gestureFilter);
       
  1010                     }
       
  1011                 }
       
  1012                 updatePrimitives();
       
  1013             }
       
  1014             break;
       
  1015         default:
       
  1016             break;
       
  1017     }
       
  1018     return HbAbstractButton::itemChange( change, value );
       
  1019 }
       
  1020 
       
  1021 /*!
       
  1022   Overloaded hit detection to include touch area
       
  1023  */
       
  1024 bool HbPushButton::hitButton(const QPointF &pos) const
       
  1025 {
       
  1026     Q_D( const HbPushButton );
       
  1027     QRectF compRect = d->touchArea->boundingRect( );
       
  1028     compRect.translate( d->touchArea->pos() );
       
  1029     return compRect.contains( pos );
       
  1030 }
       
  1031 
       
  1032 #include "moc_hbpushbutton.cpp"
       
  1033