mpviewplugins/mpmediawallviewplugin/src/mptracklistwidget.cpp
branchRCL_3
changeset 52 14979e23cb5e
equal deleted inserted replaced
50:26a1709b9fec 52:14979e23cb5e
       
     1 /*
       
     2 * Copyright (c) 2010 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: Track List Widget for Music Player Media Wall.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <QGraphicsSceneMouseEvent>
       
    20 
       
    21 #ifndef UNIT_TESTING
       
    22 #include <hbinstance.h>
       
    23 #else
       
    24 #include "hbinstancestub.h"
       
    25 #endif
       
    26 
       
    27 #include <hblistview.h>
       
    28 #include <hbframeitem.h>
       
    29 #include <hbmainwindow.h>
       
    30 #include <hbstyleloader.h>
       
    31 
       
    32 
       
    33 #include "mptracklistwidget.h"
       
    34 
       
    35 
       
    36 const int swipeAngleTolerance = 30; // angle is from 0 to 360
       
    37 
       
    38 /*!
       
    39     \class MpTrackListWidget
       
    40     \brief Widget with a list of tracks for Media Wall.
       
    41     
       
    42     This widget provides a list with custom style and a background.
       
    43 
       
    44 */
       
    45 
       
    46 /*!
       
    47     \fn void closed()
       
    48 
       
    49     This signal is emitted when the track list is closed by the user with a 
       
    50     left swipe gesture.
       
    51  */
       
    52 
       
    53 
       
    54 /*!
       
    55    Creates the MpTrackListWidget. 
       
    56  */
       
    57 MpTrackListWidget::MpTrackListWidget( QGraphicsItem *parent ) : HbWidget( parent ) 
       
    58 {
       
    59     // Register the custorm css path for the list items.
       
    60     HbStyleLoader::registerFilePath(":/css/mpcustomlistitem.css");
       
    61     HbStyleLoader::registerFilePath(":/css/mpcustomlistitem.hblistviewitem.widgetml");    
       
    62     mList = new HbListView( this );
       
    63     // set layout name that matches the custom list item layout.
       
    64     mList->setLayoutName("mpmwtracklist");
       
    65     
       
    66     //grab the gesture for close.
       
    67     grabGesture(Qt::SwipeGesture);
       
    68     
       
    69     mFrameItem = new HbFrameItem( this );
       
    70     mFrameItem->frameDrawer().setFrameType( HbFrameDrawer::NinePieces );
       
    71     mFrameItem->frameDrawer().setFrameGraphicsName( "qtg_fr_multimedia_trans" );  
       
    72     mFrameItem->setZValue(-1);
       
    73 }
       
    74 
       
    75 /*!
       
    76  Destructs the track list widget.
       
    77  */
       
    78 MpTrackListWidget::~MpTrackListWidget()
       
    79 {
       
    80 }
       
    81 
       
    82 /*!
       
    83     Returns the HbListView instance.
       
    84  */
       
    85 HbListView *MpTrackListWidget::list()
       
    86 {
       
    87     return mList;
       
    88 } 
       
    89 
       
    90 /*!
       
    91     \reimp
       
    92  */
       
    93 void MpTrackListWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
       
    94 {
       
    95     mFrameItem->setGeometry( rect() );
       
    96     qreal margin = 0.0;
       
    97     style()->parameter(QString("var(hb-param-margin-gene-middle-vertical)"), margin);
       
    98     mList->setGeometry( rect().adjusted( margin, margin, -margin, -margin ) );
       
    99     HbWidget::resizeEvent( event );
       
   100 }
       
   101 
       
   102 /*!
       
   103     \reimp
       
   104  */
       
   105 void MpTrackListWidget::mousePressEvent( QGraphicsSceneMouseEvent *event )
       
   106 {
       
   107     if ( event->button() == Qt::LeftButton ) {
       
   108         event->accept();
       
   109     }
       
   110     else {
       
   111         event->ignore();
       
   112     }
       
   113 }
       
   114 
       
   115 /*!
       
   116     \reimp
       
   117  */
       
   118 void MpTrackListWidget::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
       
   119 {
       
   120     if ( event->button() == Qt::LeftButton ) {
       
   121         event->accept();
       
   122     }
       
   123     else {
       
   124         event->ignore();
       
   125     }
       
   126 }
       
   127 
       
   128 /*!
       
   129     \reimp
       
   130  */
       
   131 void MpTrackListWidget::gestureEvent(QGestureEvent *event)
       
   132 {
       
   133     QGesture* gesture = event->gesture(Qt::SwipeGesture);
       
   134     if (gesture) {
       
   135         QSwipeGesture* swipe = static_cast<QSwipeGesture *>(gesture);
       
   136         if (swipe->state() == Qt::GestureFinished && 
       
   137                 swipeAngleToDirection (swipe->swipeAngle()) == QSwipeGesture::Left ) {
       
   138             //Left gesture is the direction in wich the track list slides to close.
       
   139             emit closed();
       
   140             event->accept(Qt::SwipeGesture);
       
   141         }
       
   142     }    
       
   143 }
       
   144 
       
   145 /*!
       
   146     Maps swipe \a angle to QSwipeGesture::SwipeDirection based on a tolerance 
       
   147     parameter and orientation. This funtions helps to identify a swipe even 
       
   148     if it is not sharp movement from 180 to 0 degrees on the righ swipe 
       
   149     gesture for instance. Since gesture events are mesured on device 
       
   150     cordinates this also helps to correct the gesture to local cordinates 
       
   151     bases on device orientation, wich is done using QT transformations, 
       
   152     meaning local cordinates and device cordinates are not always aligned.
       
   153  */
       
   154 QSwipeGesture::SwipeDirection MpTrackListWidget::swipeAngleToDirection(
       
   155         int angle )
       
   156 {
       
   157     int delta = swipeAngleTolerance;
       
   158     if ( hbInstance->allMainWindows()[0]->orientation() == Qt::Horizontal ) {
       
   159     //correction for transformation on rotation.
       
   160 #ifdef __WINS__ //wincw with forced rotation is to the right.
       
   161     angle += ( angle < 90 ) ? 270 : -90; 
       
   162 #else//currently hardware rotations is to the left.
       
   163     angle += 90;
       
   164     if ( angle > 360 ) {
       
   165         angle -= 360;
       
   166     }
       
   167 #endif    
       
   168       
       
   169     }
       
   170     QSwipeGesture::SwipeDirection direction( QSwipeGesture::NoDirection );
       
   171     if ( ( angle > 90 - delta) && ( angle < 90 + delta ) ) {
       
   172         direction = QSwipeGesture::Up;
       
   173     } else if ( ( angle > 270 - delta ) && (angle < 270 + delta ) ) {
       
   174         direction = QSwipeGesture::Down;
       
   175     } else if ( ( ( angle >= 0 ) && ( angle < delta ) ) 
       
   176             || ( ( angle > 360 - delta ) && ( angle <= 360 ) ) ) {
       
   177         direction = QSwipeGesture::Right;
       
   178     } else if ( ( angle > 180 - delta ) && ( angle < 180 + delta ) ) {
       
   179         direction = QSwipeGesture::Left;
       
   180     }
       
   181     return direction;    
       
   182 }
       
   183 
       
   184 //EOF