messagingapp/msgui/msgaudiofetcher/src/msgaudiofetcherdialog.cpp
changeset 70 a15d9966050f
equal deleted inserted replaced
61:8ba0afbb4637 70:a15d9966050f
       
     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:
       
    15  *     The source file for messaging's audio fetcher dialog.
       
    16  *
       
    17  */
       
    18 
       
    19 // SYSTEM INCLUDES
       
    20 #include <hbaction.h>
       
    21 #include <HbStyleLoader>
       
    22 #include <HbTextItem>
       
    23 #include <HbColorScheme>
       
    24 #include <HbListView>
       
    25 #include <QModelIndex>
       
    26 #include <QStandardItemModel>
       
    27 
       
    28 // USER INCLUDES
       
    29 #include "msgaudiofetcherdialog.h"
       
    30 #include "msgaudiofetchermodel.h"
       
    31 #include "msgaudiopreview.h"
       
    32 
       
    33 #define LOC_SELECT_SELECT hbTrId("txt_common_button_select")
       
    34 #define LOC_SELECT_CANCEL hbTrId("txt_common_button_cancel")
       
    35 #define LOC_SELECT_SOUND  hbTrId("txt_messaging_title_select_a_sound")
       
    36 
       
    37 const QString LIST_ITEM_TITLE("qtc_list_item_title_normal");
       
    38 
       
    39 MsgAudioFetcherDialog::MsgAudioFetcherDialog(QGraphicsItem *parent) :
       
    40 	HbDialog(parent),
       
    41 	mListView(NULL),
       
    42 	mFetcherModel(NULL),
       
    43 	mAudioPreview(NULL),
       
    44 	mSelected(false)
       
    45 {
       
    46     setDismissPolicy(HbDialog::NoDismiss);
       
    47     setFrameType(HbDialog::Strong);
       
    48     setModal(true);
       
    49     
       
    50     initActions();
       
    51     initMainWidget();  
       
    52     
       
    53     connect(
       
    54             this, 
       
    55             SIGNAL(aboutToClose()), 
       
    56             this, 
       
    57             SLOT(reset()));
       
    58     
       
    59     //TODO: Goal was to put in viewReady, but connect is
       
    60     //not working since it only works for views not dialogs
       
    61     doDelayedConstruction();
       
    62 }
       
    63 
       
    64 MsgAudioFetcherDialog::~MsgAudioFetcherDialog()
       
    65 {
       
    66     //do nothing
       
    67 }
       
    68 
       
    69 void MsgAudioFetcherDialog::initMainWidget()
       
    70 {
       
    71 	//set heading
       
    72 	HbTextItem* heading = new HbTextItem(
       
    73 	        LOC_SELECT_SOUND, 
       
    74 	        this);
       
    75 	QColor color = HbColorScheme::color( LIST_ITEM_TITLE );
       
    76 	heading->setTextColor( color );
       
    77 	heading->setAlignment(Qt::AlignLeft);
       
    78 	setHeadingWidget(heading);	
       
    79 }
       
    80 
       
    81 void MsgAudioFetcherDialog::initActions()
       
    82 {
       
    83     mLeftAction = new HbAction(this);
       
    84     mLeftAction->setText(LOC_SELECT_SELECT);
       
    85     addAction(mLeftAction);
       
    86     mLeftAction->setEnabled(false);
       
    87 
       
    88     HbAction* rightAction = new HbAction(this);
       
    89     rightAction->setText(LOC_SELECT_CANCEL);
       
    90     rightAction->setEnabled(true);
       
    91     addAction(rightAction);    
       
    92     
       
    93     connect(mLeftAction,
       
    94                 SIGNAL(triggered()),
       
    95                 this,
       
    96                 SLOT(onSelectAction()));
       
    97     
       
    98     connect(rightAction,
       
    99                 SIGNAL(triggered()),
       
   100                 this,
       
   101                 SLOT(onCancelAction()));    
       
   102 }
       
   103 
       
   104 QString MsgAudioFetcherDialog::getCurrentItemPath()
       
   105 {
       
   106     if (mSeletedItem.isValid())
       
   107     {
       
   108         QString temppath = 
       
   109                     mFetcherModel->data(
       
   110                             mSeletedItem, 
       
   111                             Qt::UserRole).toString();
       
   112         //return the path
       
   113         return QDir::toNativeSeparators(temppath);
       
   114     }
       
   115     return QString();
       
   116 }
       
   117 
       
   118 void MsgAudioFetcherDialog::doDelayedConstruction()
       
   119 {
       
   120     //set the content list
       
   121     mListView = new HbListView(this);
       
   122     Q_ASSERT(mListView);
       
   123     mListView->setSelectionMode(
       
   124             HbAbstractItemView::SingleSelection);
       
   125     mFetcherModel = new MsgAudioFetcherModel(this);
       
   126     mListView->setModel(mFetcherModel);
       
   127     mListView->setUniformItemSizes(true);
       
   128     setContentWidget(mListView);
       
   129     connect(mListView, SIGNAL(activated(QModelIndex)),
       
   130             this, SLOT(on_list_activated(QModelIndex)));
       
   131     
       
   132     //initializes the audio preview
       
   133     mAudioPreview = new MsgAudioPreview(this);
       
   134 }
       
   135 
       
   136 void MsgAudioFetcherDialog::onSelectAction()
       
   137 {
       
   138 	//stop the playing file
       
   139     if (mAudioPreview->IsPlaying()) {
       
   140             mAudioPreview->Stop();        
       
   141         }
       
   142     
       
   143     //emit the selected sound file
       
   144     QString filePath = getCurrentItemPath();
       
   145     emit audioSelected(filePath);
       
   146     
       
   147     //close the dialog
       
   148     close();
       
   149 }
       
   150 
       
   151 void MsgAudioFetcherDialog::onCancelAction()
       
   152 {    
       
   153     //stop the playing file and return
       
   154     if (mAudioPreview->IsPlaying()) {
       
   155                 mAudioPreview->Stop();        
       
   156             }
       
   157     close();
       
   158     return;
       
   159 }
       
   160 
       
   161 void MsgAudioFetcherDialog::reset()
       
   162 {
       
   163     mSelected = false;
       
   164     mLeftAction->setEnabled(false);
       
   165     
       
   166     //list scroll to top
       
   167     mListView->scrollTo(
       
   168             mListView->model(
       
   169                     )->index(0,0));
       
   170     
       
   171     //deselect the selected item
       
   172     mListView->selectionModel(
       
   173             )->select(
       
   174                     mSeletedItem, 
       
   175                     QItemSelectionModel::Deselect);    
       
   176 }
       
   177 
       
   178 void MsgAudioFetcherDialog::on_list_activated(
       
   179         const QModelIndex &index)
       
   180 {
       
   181     //stop previewing when clicking another item.
       
   182     if (mAudioPreview->IsPlaying()) {
       
   183         mAudioPreview->SetAttr(getCurrentItemPath());
       
   184         mAudioPreview->Play();        
       
   185     }
       
   186 
       
   187     //enable the action select
       
   188     if(!mSelected)
       
   189     {
       
   190         mLeftAction->setEnabled(true);        
       
   191         mSelected = true;
       
   192     }     
       
   193     
       
   194     //save the latest selected
       
   195     mSeletedItem = index;
       
   196     
       
   197     //start play the item
       
   198     mAudioPreview->SetAttr(getCurrentItemPath());
       
   199     mAudioPreview->Play();
       
   200     
       
   201     return;
       
   202 }
       
   203 //End of File