radioapp/radiowidgets/src/radiocarouselitem.cpp
branchRCL_3
changeset 19 cce62ebc198e
equal deleted inserted replaced
18:1a6714c53019 19:cce62ebc198e
       
     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     mLandscapeMode( false ),
       
    68     mFlags( DefaultFlags )
       
    69 {
       
    70     mStation.reset( new RadioStation() );
       
    71 
       
    72     if ( mOwnsCss ) {
       
    73         registerAndCheck( FILE_PATH_CSS );
       
    74         registerAndCheck( FILE_PATH_WIDGETML );
       
    75     }
       
    76 
       
    77     createPrimitives();
       
    78     updatePrimitives();
       
    79 
       
    80     updateFavoriteIcon( false );
       
    81 
       
    82     updateVisibilities();
       
    83 
       
    84     grabGesture( Qt::TapGesture );
       
    85 }
       
    86 
       
    87 /*!
       
    88  *
       
    89  */
       
    90 RadioCarouselItem::~RadioCarouselItem()
       
    91 {
       
    92     if ( mOwnsCss ) {
       
    93         HbStyleLoader::unregisterFilePath( FILE_PATH_CSS );
       
    94         HbStyleLoader::unregisterFilePath( FILE_PATH_WIDGETML );
       
    95     }
       
    96 }
       
    97 
       
    98 /*!
       
    99  *
       
   100  */
       
   101 void RadioCarouselItem::createPrimitives()
       
   102 {
       
   103     mFavoriteItem = new HbIconItem( this );
       
   104     HbStyle::setItemName( mFavoriteItem, ICON_BUTTON );
       
   105 
       
   106     mGenreItem = new HbTextItem( this );
       
   107     HbStyle::setItemName( mGenreItem, GENRE_LABEL );
       
   108 
       
   109     mNameItem = new HbTextItem( this );
       
   110     HbStyle::setItemName( mNameItem, NAME_LABEL );
       
   111 
       
   112     mRadiotextItem = new HbRichTextItem( this );
       
   113     HbStyle::setItemName( mRadiotextItem, RT_LABEL );
       
   114 
       
   115     mUrlItem = new HbTextItem( this );
       
   116     HbStyle::setItemName( mUrlItem, URL_LABEL );
       
   117 
       
   118     mFavoriteTouchArea = new HbTouchArea( this );
       
   119     HbStyle::setItemName( mFavoriteTouchArea, FAVORITE_TOUCH_AREA );
       
   120 
       
   121     // Matti testing needs these
       
   122     mFavoriteItem->setObjectName( ICON_BUTTON );
       
   123     mGenreItem->setObjectName( GENRE_LABEL );
       
   124     mNameItem->setObjectName( NAME_LABEL );
       
   125     mRadiotextItem->setObjectName( RT_LABEL );
       
   126     mUrlItem->setObjectName( URL_LABEL );
       
   127     mFavoriteTouchArea->setObjectName( FAVORITE_TOUCH_AREA );
       
   128 }
       
   129 
       
   130 /*!
       
   131  *
       
   132  */
       
   133 void RadioCarouselItem::drawOffScreen( QPainter& painter )
       
   134 {
       
   135     QStyleOptionGraphicsItem option;
       
   136 
       
   137     foreach ( QGraphicsItem* child, childItems() ) {
       
   138         QGraphicsWidget* childWidget = static_cast<QGraphicsWidget*>( child );
       
   139         option.exposedRect = childWidget->rect();
       
   140         painter.save();
       
   141         painter.translate( childWidget->pos() );
       
   142         childWidget->paint( &painter, &option, NULL );
       
   143         painter.restore();
       
   144     }
       
   145 }
       
   146 
       
   147 /*!
       
   148  *
       
   149  */
       
   150 void RadioCarouselItem::updatePrimitives()
       
   151 {
       
   152     update();
       
   153 }
       
   154 
       
   155 /*!
       
   156  * \reimp
       
   157  */
       
   158 void RadioCarouselItem::gestureEvent( QGestureEvent* event )
       
   159 {
       
   160     if ( HbTapGesture* gesture = qobject_cast<HbTapGesture*>( event->gesture( Qt::TapGesture ) ) ) {
       
   161         if ( gesture->state() == Qt::GestureFinished ) {
       
   162             const QPointF mappedHotSpot = event->mapToGraphicsScene( gesture->hotSpot() );
       
   163 
       
   164             if ( mFlags.testFlag( FavoriteTouchable ) &&
       
   165                     mFavoriteTouchArea->sceneBoundingRect().contains( mappedHotSpot ) ) {
       
   166 
       
   167                 mObserver.handleIconClicked( *mStation );
       
   168 
       
   169             } else if ( mFlags.testFlag( RadiotextTouchable ) &&
       
   170                     mRadiotextItem->sceneBoundingRect().contains( mappedHotSpot ) ) {
       
   171 
       
   172                 mObserver.handleRadiotextClicked( *mStation );
       
   173 
       
   174             } else if ( mFlags.testFlag( UrlTouchable ) &&
       
   175                     mUrlItem->sceneBoundingRect().contains( mappedHotSpot ) ) {
       
   176 
       
   177                 mObserver.handleUrlClicked( *mStation );
       
   178 
       
   179             }
       
   180         }
       
   181     }
       
   182 }
       
   183 
       
   184 /*!
       
   185  *
       
   186  */
       
   187 void RadioCarouselItem::setFlags( CarouselItemFlags flags )
       
   188 {
       
   189     mFlags |= flags;
       
   190     updateVisibilities();
       
   191 }
       
   192 
       
   193 /*!
       
   194  *
       
   195  */
       
   196 void RadioCarouselItem::clearFlags( CarouselItemFlags flags )
       
   197 {
       
   198     for ( int i = 1; i < LastFlagMarker; i = i << 1 ) {
       
   199         if ( flags.testFlag( static_cast<ItemFlag>( i ) ) ) {
       
   200             mFlags &= ~i;
       
   201         }
       
   202     }
       
   203     updateVisibilities();
       
   204 }
       
   205 
       
   206 /*!
       
   207  *
       
   208  */
       
   209 void RadioCarouselItem::updateVisibilities()
       
   210 {
       
   211     mFavoriteItem->setVisible( mFlags.testFlag( FavoriteVisible ) );
       
   212     mGenreItem->setVisible( mFlags.testFlag( GenreVisible ) );
       
   213     mRadiotextItem->setVisible( mFlags.testFlag( RadiotextVisible ) );
       
   214     mUrlItem->setVisible( mFlags.testFlag( UrlVisible ) );
       
   215 }
       
   216 
       
   217 /*!
       
   218  *
       
   219  */
       
   220 void RadioCarouselItem::setAppearance( Appearance appearance )
       
   221 {
       
   222     mAppearance = appearance;
       
   223 
       
   224     if ( mAppearance == ManualSeek ) {
       
   225         mFlags = ManualSeekFlags;
       
   226         mGenreItem->setText( "" );
       
   227         mRadiotextItem->setText( "" );
       
   228         mUrlItem->setText( "" );
       
   229         mNameItem->setText( mStation->frequencyString() );
       
   230         updateFavoriteIcon( false );
       
   231     } else {
       
   232         mFlags = DefaultFlags;
       
   233     }
       
   234 
       
   235     updateVisibilities();
       
   236 
       
   237     repolish();
       
   238 }
       
   239 
       
   240 /*!
       
   241  *
       
   242  */
       
   243 RadioCarouselItem::Appearance RadioCarouselItem::appearance() const
       
   244 {
       
   245     return mAppearance;
       
   246 }
       
   247 
       
   248 /*!
       
   249  *
       
   250  */
       
   251 void RadioCarouselItem::setLandscape( bool landscape )
       
   252 {
       
   253     mLandscapeMode = landscape;
       
   254 }
       
   255 
       
   256 /*!
       
   257  *
       
   258  */
       
   259 bool RadioCarouselItem::landscape() const
       
   260 {
       
   261     return mLandscapeMode;
       
   262 }
       
   263 
       
   264 /*!
       
   265  *
       
   266  */
       
   267 void RadioCarouselItem::setSeekLayout( bool seekLayout )
       
   268 {
       
   269     if ( seekLayout ) {
       
   270         setAppearance( ManualSeek );
       
   271     } else {
       
   272         setAppearance( mStation->radioText().isEmpty() ? Default : Full );
       
   273     }
       
   274 }
       
   275 
       
   276 /*!
       
   277  *
       
   278  */
       
   279 void RadioCarouselItem::setStation( const RadioStation& station )
       
   280 {
       
   281     *mStation = station;
       
   282 
       
   283     updateLayout();
       
   284 
       
   285     update();
       
   286 }
       
   287 
       
   288 /*!
       
   289  *
       
   290  */
       
   291 uint RadioCarouselItem::frequency() const
       
   292 {
       
   293     return mStation->frequency();
       
   294 }
       
   295 
       
   296 /*!
       
   297  *
       
   298  */
       
   299 void RadioCarouselItem::update( const RadioStation* station )
       
   300 {
       
   301     if ( station ) {
       
   302         *mStation = *station;
       
   303         updateLayout();
       
   304     }
       
   305 
       
   306     if ( mStation->isValid() ) {
       
   307         mGenreItem->setText( mObserver.localizeGenre( mStation->genre() ) );
       
   308 
       
   309         const bool hasName = mStation->hasName();
       
   310         if ( hasName ) {
       
   311             mNameItem->setText( mStation->name() );
       
   312         } else {
       
   313             mNameItem->setText( mStation->frequencyString() );
       
   314         }
       
   315 
       
   316         if ( mStation->hasRadiotext() ) {
       
   317             mRadiotextItem->setText( mStation->radioText() );
       
   318         } else {
       
   319             if ( mStation->hasDynamicPs() ) {
       
   320                 mRadiotextItem->setText( mStation->dynamicPsText() );
       
   321             } else if ( hasName ) {
       
   322                 const QString loc = "%L1 Mhz"; //hbTrId( "txt_rad_list_l1_mhz_small" );
       
   323                 mRadiotextItem->setText( loc.arg( mStation->frequencyString() ) );
       
   324             } else {
       
   325                 mRadiotextItem->setText( "" );
       
   326             }
       
   327         }
       
   328 
       
   329         mUrlItem->setText( mStation->url() );
       
   330         if ( mStation->hasUrl() ) {
       
   331             HbStyle::setItemName( mUrlItem, URL_LABEL );
       
   332             setFlags( UrlVisible | UrlTouchable );
       
   333         } else {
       
   334             HbStyle::setItemName( mUrlItem, "" ); // Clear the name so the item disappears from layout
       
   335             clearFlags( UrlVisible | UrlTouchable );
       
   336         }
       
   337 
       
   338         updateFavoriteIcon( mStation->isFavorite() );
       
   339     } else {
       
   340         cleanRdsData();
       
   341     }
       
   342 }
       
   343 
       
   344 /*!
       
   345  *
       
   346  */
       
   347 void RadioCarouselItem::setFrequency( uint frequency )
       
   348 {
       
   349     LOG_FORMAT( "RadioCarouselItem::setFrequency: %d", frequency );
       
   350 
       
   351     mNameItem->setText( RadioStation::parseFrequency( frequency ) );
       
   352 
       
   353     if ( !mObserver.isInManualSeek() ) {
       
   354         *mStation = mObserver.findStation( frequency );
       
   355     }
       
   356 }
       
   357 
       
   358 /*!
       
   359  *
       
   360  */
       
   361 void RadioCarouselItem::cleanRdsData()
       
   362 {
       
   363     mGenreItem->setText( "" );
       
   364     mRadiotextItem->setText( "" );
       
   365     mUrlItem->setText( "" );
       
   366 }
       
   367 
       
   368 /*!
       
   369  *
       
   370  */
       
   371 void RadioCarouselItem::setRadioText( const QString& text )
       
   372 {
       
   373     mRadiotextItem->setText( text );
       
   374 }
       
   375 
       
   376 /*!
       
   377  *
       
   378  */
       
   379 void RadioCarouselItem::setItemVisibility( ItemVisibility visibility )
       
   380 {
       
   381     CarouselItemFlags flags = 0;
       
   382     if ( visibility == AllVisible ) {
       
   383         flags = DefaultFlags;
       
   384     } else if ( visibility == AllHidden ) {
       
   385 
       
   386     } else if ( visibility == IconVisible ) {
       
   387         flags = FavoriteVisible;
       
   388     }
       
   389 
       
   390     setFlags( flags );
       
   391 }
       
   392 
       
   393 /*!
       
   394  *
       
   395  */
       
   396 void RadioCarouselItem::setIconOpacity( qreal opacity )
       
   397 {
       
   398     mFavoriteItem->setOpacity( opacity );
       
   399 }
       
   400 
       
   401 /*!
       
   402  *
       
   403  */
       
   404 void RadioCarouselItem::updateFavoriteIcon( bool isFavorite )
       
   405 {
       
   406     if ( isFavorite ) {
       
   407         mFavoriteItem->setIcon( mObserver.favoriteIcon() );
       
   408     } else {
       
   409         mFavoriteItem->setIcon( mObserver.nonFavoriteIcon() );
       
   410     }
       
   411 }
       
   412 
       
   413 /*!
       
   414  *
       
   415  */
       
   416 void RadioCarouselItem::updateLayout()
       
   417 {
       
   418     setAppearance( mStation->hasSentRds() ? Full : Default );
       
   419 }