controlpanelui/src/tonefetcher/src/tonefetcherwidget.cpp
changeset 19 36aa4756ee82
child 17 4a9568303383
child 21 2883a5458389
equal deleted inserted replaced
12:624337f114fe 19:36aa4756ee82
       
     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  *     The source file for tone fetcher widget.
       
    16  *     
       
    17  */
       
    18 #include "tonefetcherwidget.h"
       
    19 #include "hbabstractviewitem.h"
       
    20 #include "hbstyle.h"
       
    21 #include "hbabstractitemview.h"
       
    22 #include <hblistview.h>
       
    23 #include <hbmenu.h>
       
    24 #include <hbaction.h>
       
    25 #include <QModelIndex>
       
    26 #include <QGraphicsLinearLayout>
       
    27 #include <QDirModel>
       
    28 #include <QTime>
       
    29 #include <QFileInfo>
       
    30 #include <QString>
       
    31 #include <QStandardItemModel>
       
    32 #include <XQUtils>
       
    33 #include <hblabel.h>
       
    34 #include "tonefetcherview.h"
       
    35 #include "tonefetchermodel.h"
       
    36 #include <hbmessagebox.h>
       
    37 
       
    38 ToneFetcherWidget::ToneFetcherWidget( ToneFetcherView *serviceView  ) 
       
    39     : HbWidget(this),
       
    40       mLabel(0),
       
    41       mListView(0),
       
    42       mLayout(0),
       
    43       mToneModel(0),
       
    44       mServiceView(serviceView),         
       
    45       mServiceEngine(0)
       
    46       
       
    47 {
       
    48     mSelected = false;
       
    49     init();
       
    50     connect(mServiceEngine, SIGNAL(mdeSessionOpened()), 
       
    51             this, SLOT(mdeSessionOpened()));
       
    52     connect(mServiceEngine, SIGNAL(mdeSessionError(int)),
       
    53             this, SLOT(mdeSessionError(int)));
       
    54     connect(mServiceEngine, SIGNAL(queryComplete(QStringList, QStringList)), 
       
    55             this, SLOT(queryComplete(QStringList, QStringList)));
       
    56     connect(mServiceEngine, SIGNAL(queryError(int)), 
       
    57             this, SLOT(queryError(int)));
       
    58     connect(mServiceEngine, 
       
    59            SIGNAL(notifyPreviewEvent(ToneServiceEngine::TPreviewEvent, int)), 
       
    60            this, SLOT(previewEvent(ToneServiceEngine::TPreviewEvent, int)));
       
    61     connect( mServiceEngine, SIGNAL(notifyObjectChanged()),
       
    62             this, SLOT(onObjectChanged()));
       
    63 }
       
    64 
       
    65 ToneFetcherWidget::~ToneFetcherWidget()
       
    66 {
       
    67     delete mToneModel;    
       
    68 }
       
    69 
       
    70 void ToneFetcherWidget::on_list_activated(const QModelIndex &index)
       
    71 {  
       
    72     QModelIndexList modelIndexList = mListView->selectionModel()->selectedIndexes();
       
    73     
       
    74     //stop previewing when clicking another item.
       
    75     if (mServiceEngine->IsPlaying()) {
       
    76         mServiceEngine->preview(getCurrentItemPath());
       
    77     }
       
    78     /*
       
    79      * when one item is selected, reselecting it will deselect it. selecting another 
       
    80      * will also deselect it, while the other is selected.
       
    81      */
       
    82     if (mSelected){
       
    83         if(mOldSeletedItem != index) {
       
    84             mListView->selectionModel()->select(index, QItemSelectionModel::Select);
       
    85             mOldSeletedItem = index;
       
    86             emit triggerToolBar(true);
       
    87         } else {
       
    88             mListView->selectionModel()->select(index, QItemSelectionModel::Deselect);
       
    89             mSelected = false;
       
    90             emit triggerToolBar(false);
       
    91         }        
       
    92         return;
       
    93     }
       
    94     if (modelIndexList.count() > 0) {
       
    95         for (QModelIndexList::const_iterator it = modelIndexList.begin(); it != modelIndexList.end(); ++it) {
       
    96             if ((*it) == index) {
       
    97                 mSelected = true;
       
    98                 mOldSeletedItem = index;
       
    99                 emit triggerToolBar(true);
       
   100             }            
       
   101         }
       
   102         
       
   103     }   
       
   104     
       
   105 }
       
   106 
       
   107 void ToneFetcherWidget::init()
       
   108 {
       
   109     mLayout = new QGraphicsLinearLayout(this);
       
   110     mLayout->setOrientation(Qt::Vertical);
       
   111     setLayout(mLayout);
       
   112 
       
   113     mLabel = new HbLabel(this);
       
   114     mLabel->setPlainText(hbTrId("Select tone"));
       
   115     mLayout->addItem(mLabel);
       
   116     
       
   117     mListView = new HbListView(this);
       
   118     mListView->setObjectName("list");
       
   119     mLayout->addItem(mListView);
       
   120     mListView->setSelectionMode(HbAbstractItemView::SingleSelection);
       
   121      
       
   122     mServiceEngine = new ToneFetcherEngine(this);     
       
   123     mToneModel = new ToneFetcherModel(this);
       
   124     addRomFiles();
       
   125     
       
   126     connect(mListView, SIGNAL(activated(QModelIndex)),
       
   127         this, SLOT(on_list_activated(QModelIndex )));
       
   128 }
       
   129 
       
   130 void ToneFetcherWidget::mdeSessionOpened()
       
   131 {
       
   132     mServiceEngine->getTone();
       
   133 }
       
   134 
       
   135 void ToneFetcherWidget::queryComplete(const QStringList &nameList, const QStringList &uriList)
       
   136 {
       
   137     QStandardItem *fileName = 0;
       
   138     QStandardItem *filePath = 0; 
       
   139     for (int i = 0; i < nameList.size(); ++i) { 
       
   140         QString tr1 = nameList.at(i);
       
   141         tr1 = uriList.at(i);
       
   142         fileName = new QStandardItem(nameList.at(i));
       
   143         filePath = new QStandardItem(uriList.at(i));
       
   144         mToneModel->insertInOrder(fileName, filePath);       
       
   145     }   
       
   146     mLabel->setPlainText(QString::number(mSimpleSoundList.size() + mDigitalSoundList.size() + nameList.size()) + " tones");
       
   147     mListView->setModel(mToneModel);
       
   148     mToneModel->refresh();
       
   149 }
       
   150 
       
   151 void ToneFetcherWidget::queryError(int error)
       
   152 {
       
   153     Q_UNUSED(error);
       
   154         
       
   155 }
       
   156 
       
   157 void ToneFetcherWidget::mdeSessionError(int error)
       
   158 {
       
   159     Q_UNUSED(error);
       
   160 }
       
   161 
       
   162 QString ToneFetcherWidget::getCurrentItemPath() 
       
   163 {
       
   164     QModelIndexList modelIndexList = mListView->selectionModel()->selectedIndexes();
       
   165     if (modelIndexList.count() > 0) {
       
   166         QModelIndex index = modelIndexList.at(0);
       
   167         return mToneModel->data(index, Qt::UserRole).toString();
       
   168     }
       
   169     return QString();
       
   170 }
       
   171 
       
   172 void ToneFetcherWidget::playOrPause() 
       
   173 {
       
   174     mServiceEngine->preview(getCurrentItemPath());
       
   175 }
       
   176 
       
   177 void ToneFetcherWidget::previewEvent(ToneFetcherEngine::TPreviewEvent event, int errorId) 
       
   178 {
       
   179     Q_UNUSED(errorId);
       
   180     if (event == ToneFetcherEngine::EAudioPreviewComplete) {
       
   181         //reserved
       
   182     } else {
       
   183         HbMessageBox::information(QString(hbTrId("Preview Error")));
       
   184     }
       
   185 }
       
   186 
       
   187 void ToneFetcherWidget::onObjectChanged()
       
   188 {
       
   189     mToneModel->clearAll();
       
   190     mDigitalSoundList.clear();
       
   191     mSimpleSoundList.clear();
       
   192     addRomFiles();
       
   193     mServiceEngine->getTone();    
       
   194 }
       
   195 
       
   196 void ToneFetcherWidget::addRomFiles() 
       
   197 {
       
   198     QStandardItem *fileName = 0;
       
   199     QStandardItem *filePath = 0;
       
   200     QDir digitalSoundPath(XQUtils::romRootPath() + XQUtils::digitalSoundsPath());
       
   201     mDigitalSoundList = digitalSoundPath.entryInfoList();  
       
   202            
       
   203     QDir simpleSoundPath(XQUtils::romRootPath() + XQUtils::simpleSoundsPath());
       
   204     mSimpleSoundList = simpleSoundPath.entryInfoList();
       
   205 
       
   206     for (int i = 0; i < mDigitalSoundList.size(); ++i) {
       
   207         QFileInfo fileInfo = mDigitalSoundList.at(i);        
       
   208         fileName = new QStandardItem(fileInfo.fileName());
       
   209         filePath = new QStandardItem(fileInfo.absoluteFilePath());
       
   210         mToneModel->insertInOrder(fileName, filePath);
       
   211     }
       
   212       
       
   213     for (int i = 0; i < mSimpleSoundList.size(); ++i) {
       
   214         QFileInfo fileInfo = mSimpleSoundList.at(i);       
       
   215         fileName = new QStandardItem(fileInfo.fileName());
       
   216         filePath = new QStandardItem(fileInfo.absoluteFilePath());
       
   217         mToneModel->insertInOrder(fileName, filePath);  
       
   218     }
       
   219 }
       
   220 //End of File