radioapp/radiowidgets/src/radiocarouselitem.cpp
branchGCC_SURGE
changeset 37 451b2e1545b2
parent 33 11b6825f0862
child 41 3a6b55c6390c
equal deleted inserted replaced
26:6bcf277166c1 37:451b2e1545b2
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 // System includes
       
    19 #include <HbStyleLoader>
       
    20 #include <HbTextItem>
       
    21 #include <HbRichTextItem>
       
    22 #include <HbIconItem>
       
    23 #include <HbTouchArea>
       
    24 #include <HbTapGesture>
       
    25 #include <QPainter>
       
    26 
       
    27 // User includes
       
    28 #include "radiocarouselitem.h"
       
    29 #include "radiocarouselitemobserver.h"
       
    30 #include "radiostation.h"
       
    31 #include "radiologger.h"
       
    32 
       
    33 const QLatin1String FILE_PATH_WIDGETML   ( ":/layout/radiocarouselitem.widgetml" );
       
    34 const QLatin1String FILE_PATH_CSS        ( ":/layout/radiocarouselitem.css" );
       
    35 const QLatin1String ICON_BUTTON          ( "star_button" );
       
    36 const QLatin1String GENRE_LABEL          ( "genre_label" );
       
    37 const QLatin1String FAVORITE_TOUCH_AREA  ( "favorite_touch_area" );
       
    38 const QLatin1String NAME_LABEL           ( "name_label" );
       
    39 const QLatin1String RT_LABEL             ( "rt_label" );
       
    40 const QLatin1String URL_LABEL            ( "url_label" );
       
    41 
       
    42 const QLatin1String SEEKING_TEXT        ( "txt_rad_list_tuning" );
       
    43 const QLatin1String CONNECT_HEADSET_TEXT( "txt_rad_list_connect_wireless_antenna_headset_with" );
       
    44 
       
    45 /*!
       
    46  *
       
    47  */
       
    48 static void registerAndCheck( const QString& file ) {
       
    49     bool registered = HbStyleLoader::registerFilePath( file );
       
    50     Q_ASSERT_X( registered, "RadioCarouselItem", "Failed to register CSS or WIDGETML!" );
       
    51 }
       
    52 
       
    53 /*!
       
    54  *
       
    55  */
       
    56 RadioCarouselItem::RadioCarouselItem( RadioCarouselItemObserver& observer, QGraphicsItem* parent, bool registerCss ) :
       
    57     HbWidget( parent ),
       
    58     mObserver( observer ),
       
    59     mFavoriteItem( NULL ),
       
    60     mGenreItem( NULL ),
       
    61     mFavoriteTouchArea( NULL ),
       
    62     mNameItem( NULL ),
       
    63     mRadiotextItem( NULL ),
       
    64     mUrlItem( NULL ),
       
    65     mAppearance( Default ),
       
    66     mOwnsCss( registerCss ),
       
    67     mFlags( DefaultFlags )
       
    68 {
       
    69     mStation.reset( new RadioStation() );
       
    70 
       
    71     if ( mOwnsCss ) {
       
    72         registerAndCheck( FILE_PATH_CSS );
       
    73         registerAndCheck( FILE_PATH_WIDGETML );
       
    74     }
       
    75 
       
    76     createPrimitives();
       
    77     updatePrimitives();
       
    78 
       
    79     updateFavoriteIcon( false );
       
    80 
       
    81     updateVisibilities();
       
    82 
       
    83     grabGesture( Qt::TapGesture );
       
    84 }
       
    85 
       
    86 /*!
       
    87  *
       
    88  */
       
    89 RadioCarouselItem::~RadioCarouselItem()
       
    90 {
       
    91     if ( mOwnsCss ) {
       
    92         HbStyleLoader::unregisterFilePath( FILE_PATH_CSS );
       
    93         HbStyleLoader::unregisterFilePath( FILE_PATH_WIDGETML );
       
    94     }
       
    95 }
       
    96 
       
    97 /*!
       
    98  *
       
    99  */
       
   100 void RadioCarouselItem::createPrimitives()
       
   101 {
       
   102     mFavoriteItem = new HbIconItem( this );
       
   103     HbStyle::setItemName( mFavoriteItem, ICON_BUTTON );
       
   104 
       
   105     mGenreItem = new HbTextItem( this );
       
   106     HbStyle::setItemName( mGenreItem, GENRE_LABEL );
       
   107 
       
   108     mNameItem = new HbTextItem( this );
       
   109     HbStyle::setItemName( mNameItem, NAME_LABEL );
       
   110 
       
   111     mRadiotextItem = new HbRichTextItem( this );
       
   112     HbStyle::setItemName( mRadiotextItem, RT_LABEL );
       
   113 
       
   114     mUrlItem = new HbTextItem( this );
       
   115     HbStyle::setItemName( mUrlItem, URL_LABEL );
       
   116 
       
   117     mFavoriteTouchArea = new HbTouchArea( this );
       
   118     HbStyle::setItemName( mFavoriteTouchArea, FAVORITE_TOUCH_AREA );
       
   119 
       
   120     // Matti testing needs these
       
   121     mFavoriteItem->setObjectName( ICON_BUTTON );
       
   122     mGenreItem->setObjectName( GENRE_LABEL );
       
   123     mNameItem->setObjectName( NAME_LABEL );
       
   124     mRadiotextItem->setObjectName( RT_LABEL );
       
   125     mUrlItem->setObjectName( URL_LABEL );
       
   126     mFavoriteTouchArea->setObjectName( FAVORITE_TOUCH_AREA );
       
   127 }
       
   128 
       
   129 /*!
       
   130  *
       
   131  */
       
   132 void RadioCarouselItem::drawOffScreen( QPainter& painter )
       
   133 {
       
   134     QStyleOptionGraphicsItem option;
       
   135 
       
   136     foreach ( QGraphicsItem* child, childItems() ) {
       
   137         QGraphicsWidget* childWidget = static_cast<QGraphicsWidget*>( child );
       
   138         option.exposedRect = childWidget->rect();
       
   139         painter.save();
       
   140         painter.translate( childWidget->pos() );
       
   141         childWidget->paint( &painter, &option, NULL );
       
   142         painter.restore();
       
   143     }
       
   144 }
       
   145 
       
   146 /*!
       
   147  *
       
   148  */
       
   149 void RadioCarouselItem::updatePrimitives()
       
   150 {
       
   151     update();
       
   152 }
       
   153 
       
   154 /*!
       
   155  * \reimp
       
   156  */
       
   157 void RadioCarouselItem::gestureEvent( QGestureEvent* event )
       
   158 {
       
   159     if ( HbTapGesture* gesture = qobject_cast<HbTapGesture*>( event->gesture( Qt::TapGesture ) ) ) {
       
   160         if ( gesture->state() == Qt::GestureFinished ) {
       
   161             const QPointF mappedHotSpot = event->mapToGraphicsScene( gesture->hotSpot() );
       
   162 
       
   163             if ( mFlags.testFlag( FavoriteTouchable ) &&
       
   164                     mFavoriteTouchArea->sceneBoundingRect().contains( mappedHotSpot ) ) {
       
   165 
       
   166                 mObserver.handleIconClicked( *mStation );
       
   167 
       
   168             } else if ( mFlags.testFlag( RadiotextTouchable ) &&
       
   169                     mRadiotextItem->sceneBoundingRect().contains( mappedHotSpot ) ) {
       
   170 
       
   171                 mObserver.handleRadiotextClicked( *mStation );
       
   172 
       
   173             } else if ( mFlags.testFlag( UrlTouchable ) &&
       
   174                     mUrlItem->sceneBoundingRect().contains( mappedHotSpot ) ) {
       
   175 
       
   176                 mObserver.handleUrlClicked( *mStation );
       
   177 
       
   178             }
       
   179         }
       
   180     }
       
   181 }
       
   182 
       
   183 /*!
       
   184  *
       
   185  */
       
   186 void RadioCarouselItem::setFlags( CarouselItemFlags flags )
       
   187 {
       
   188     mFlags |= flags;
       
   189     updateVisibilities();
       
   190 }
       
   191 
       
   192 /*!
       
   193  *
       
   194  */
       
   195 void RadioCarouselItem::clearFlags( CarouselItemFlags flags )
       
   196 {
       
   197     for ( int i = 1; i < LastFlagMarker; i = i << 1 ) {
       
   198         if ( flags.testFlag( static_cast<ItemFlag>( i ) ) ) {
       
   199             mFlags &= ~i;
       
   200         }
       
   201     }
       
   202     updateVisibilities();
       
   203 }
       
   204 
       
   205 /*!
       
   206  *
       
   207  */
       
   208 void RadioCarouselItem::updateVisibilities()
       
   209 {
       
   210     mFavoriteItem->setVisible( mFlags.testFlag( FavoriteVisible ) );
       
   211     mGenreItem->setVisible( mFlags.testFlag( GenreVisible ) );
       
   212     mRadiotextItem->setVisible( mFlags.testFlag( RadiotextVisible ) );
       
   213     mUrlItem->setVisible( mFlags.testFlag( UrlVisible ) );
       
   214 }
       
   215 
       
   216 /*!
       
   217  *
       
   218  */
       
   219 void RadioCarouselItem::setAppearance( Appearance appearance )
       
   220 {
       
   221     mAppearance = appearance;
       
   222 
       
   223     if ( mAppearance == ManualSeek ) {
       
   224         mFlags = ManualSeekFlags;
       
   225         mGenreItem->setText( "" );
       
   226         mRadiotextItem->setText( "" );
       
   227         mUrlItem->setText( "" );
       
   228         mNameItem->setText( mStation->frequencyString() );
       
   229         updateFavoriteIcon( false );
       
   230     } else {
       
   231         mFlags = DefaultFlags;
       
   232     }
       
   233 
       
   234     updateVisibilities();
       
   235 
       
   236     repolish();
       
   237 }
       
   238 
       
   239 /*!
       
   240  *
       
   241  */
       
   242 RadioCarouselItem::Appearance RadioCarouselItem::appearance() const
       
   243 {
       
   244     return mAppearance;
       
   245 }
       
   246 
       
   247 /*!
       
   248  *
       
   249  */
       
   250 void RadioCarouselItem::setSeekLayout( bool seekLayout )
       
   251 {
       
   252     if ( seekLayout ) {
       
   253         setAppearance( ManualSeek );
       
   254     } else {
       
   255         setAppearance( mStation->radioText().isEmpty() ? Default : Full );
       
   256     }
       
   257 }
       
   258 
       
   259 /*!
       
   260  *
       
   261  */
       
   262 void RadioCarouselItem::setStation( const RadioStation& station )
       
   263 {
       
   264     *mStation = station;
       
   265 
       
   266     updateLayout();
       
   267 
       
   268     update();
       
   269 }
       
   270 
       
   271 /*!
       
   272  *
       
   273  */
       
   274 uint RadioCarouselItem::frequency() const
       
   275 {
       
   276     return mStation->frequency();
       
   277 }
       
   278 
       
   279 /*!
       
   280  *
       
   281  */
       
   282 void RadioCarouselItem::update( const RadioStation* station )
       
   283 {
       
   284     if ( station ) {
       
   285         *mStation = *station;
       
   286         updateLayout();
       
   287     }
       
   288 
       
   289     if ( mStation->isValid() ) {
       
   290         mGenreItem->setText( mObserver.localizeGenre( mStation->genre() ) );
       
   291 
       
   292         const bool hasName = mStation->hasName();
       
   293         if ( hasName ) {
       
   294             mNameItem->setText( mStation->name() );
       
   295         } else {
       
   296             mNameItem->setText( mStation->frequencyString() );
       
   297         }
       
   298 
       
   299         if ( mStation->hasRadiotext() ) {
       
   300             mRadiotextItem->setText( mStation->radioText() );
       
   301         } else {
       
   302             if ( mStation->hasDynamicPs() ) {
       
   303                 mRadiotextItem->setText( mStation->dynamicPsText() );
       
   304             } else if ( hasName ) {
       
   305                 const QString loc = "%L1 Mhz"; //hbTrId( "txt_rad_list_l1_mhz_small" );
       
   306                 mRadiotextItem->setText( loc.arg( mStation->frequencyString() ) );
       
   307             } else {
       
   308                 mRadiotextItem->setText( "" );
       
   309             }
       
   310         }
       
   311 
       
   312         mUrlItem->setText( mStation->url() );
       
   313         if ( mStation->hasUrl() ) {
       
   314             HbStyle::setItemName( mUrlItem, URL_LABEL );
       
   315             setFlags( UrlVisible | UrlTouchable );
       
   316         } else {
       
   317             HbStyle::setItemName( mUrlItem, "" ); // Clear the name so the item disappears from layout
       
   318             clearFlags( UrlVisible | UrlTouchable );
       
   319         }
       
   320 
       
   321         updateFavoriteIcon( mStation->isFavorite() );
       
   322     } else {
       
   323         cleanRdsData();
       
   324     }
       
   325 }
       
   326 
       
   327 /*!
       
   328  *
       
   329  */
       
   330 void RadioCarouselItem::setFrequency( uint frequency )
       
   331 {
       
   332     LOG_FORMAT( "RadioCarouselItem::setFrequency: %d", frequency );
       
   333 
       
   334     mNameItem->setText( RadioStation::parseFrequency( frequency ) );
       
   335 
       
   336     if ( !mObserver.isInManualSeek() ) {
       
   337         *mStation = mObserver.findStation( frequency );
       
   338     }
       
   339 }
       
   340 
       
   341 /*!
       
   342  *
       
   343  */
       
   344 void RadioCarouselItem::cleanRdsData()
       
   345 {
       
   346     mGenreItem->setText( "" );
       
   347     mRadiotextItem->setText( "" );
       
   348     mUrlItem->setText( "" );
       
   349 }
       
   350 
       
   351 /*!
       
   352  *
       
   353  */
       
   354 void RadioCarouselItem::setRadioText( const QString& text )
       
   355 {
       
   356     mRadiotextItem->setText( text );
       
   357 }
       
   358 
       
   359 /*!
       
   360  *
       
   361  */
       
   362 void RadioCarouselItem::setItemVisibility( ItemVisibility visibility )
       
   363 {
       
   364     CarouselItemFlags flags = 0;
       
   365     if ( visibility == AllVisible ) {
       
   366         flags = DefaultFlags;
       
   367     } else if ( visibility == AllHidden ) {
       
   368 
       
   369     } else if ( visibility == IconVisible ) {
       
   370         flags = FavoriteVisible;
       
   371     }
       
   372 
       
   373     setFlags( flags );
       
   374 }
       
   375 
       
   376 /*!
       
   377  *
       
   378  */
       
   379 void RadioCarouselItem::setIconOpacity( qreal opacity )
       
   380 {
       
   381     mFavoriteItem->setOpacity( opacity );
       
   382 }
       
   383 
       
   384 /*!
       
   385  *
       
   386  */
       
   387 void RadioCarouselItem::updateFavoriteIcon( bool isFavorite )
       
   388 {
       
   389     if ( isFavorite ) {
       
   390         mFavoriteItem->setIcon( mObserver.favoriteIcon() );
       
   391     } else {
       
   392         mFavoriteItem->setIcon( mObserver.nonFavoriteIcon() );
       
   393     }
       
   394 }
       
   395 
       
   396 /*!
       
   397  *
       
   398  */
       
   399 void RadioCarouselItem::updateLayout()
       
   400 {
       
   401     setAppearance( mStation->hasName() || mStation->hasRadiotext() || mStation->hasUrl() ? Full : Default );
       
   402 }
       
   403