bluetoothengine/btui/btcpplugin/btcpuisearchview.cpp
changeset 40 997690c3397a
parent 33 837dcc42fd6a
child 41 0b2439c3e397
equal deleted inserted replaced
37:91746b151f97 40:997690c3397a
     1 /*
     1 /*
     2  * Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
     2  * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     3  * All rights reserved.
     3  * All rights reserved.
     4  * This component and the accompanying materials are made available
     4  * This component and the accompanying materials are made available
     5  * under the terms of "Eclipse Public License v1.0""
     5  * under the terms of "Eclipse Public License v1.0""
     6  * which accompanies this distribution, and is available
     6  * which accompanies this distribution, and is available
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    23 #include <HbDataForm>
    23 #include <HbDataForm>
    24 #include <HbPushButton>
    24 #include <HbPushButton>
    25 #include <HbLabel>
    25 #include <HbLabel>
    26 #include <HbListView>
    26 #include <HbListView>
    27 #include <HbMenu>
    27 #include <HbMenu>
       
    28 #include <HbSelectionDialog>
    28 #include <QString>
    29 #include <QString>
    29 #include <QStringList>
    30 #include <QStringList>
    30 #include <QDebug>
    31 #include <QDebug>
    31 #include <bluetoothuitrace.h>
    32 #include <bluetoothuitrace.h>
    32 #include "btcpuimainview.h"
    33 #include "btcpuimainview.h"
    33 #include "btdelegatefactory.h"
    34 #include "btdelegatefactory.h"
    34 #include "btabstractdelegate.h"
    35 #include "btabstractdelegate.h"
    35 
    36 #include "btcpuisearchlistviewitem.h"
       
    37 #include "btuidevtypemap.h"
    36 
    38 
    37 
    39 
    38 // docml to load
    40 // docml to load
    39 const char* BTUI_SEARCHVIEW_DOCML = ":/docml/bt-search-view.docml";
    41 const char* BTUI_SEARCHVIEW_DOCML = ":/docml/bt-search-view.docml";
    40 
    42 
    44         QGraphicsItem *parent) :
    46         QGraphicsItem *parent) :
    45     BtCpUiBaseView(settingModel,deviceModel, parent), mAbstractDelegate(0), mBtuiModelSortFilter(0)
    47     BtCpUiBaseView(settingModel,deviceModel, parent), mAbstractDelegate(0), mBtuiModelSortFilter(0)
    46 {
    48 {
    47     bool ret(false);
    49     bool ret(false);
    48     
    50     
       
    51     mLastSelectionIndex = 0;
    49     mMainView = (BtCpUiMainView *) parent;
    52     mMainView = (BtCpUiMainView *) parent;
    50     
    53     
    51     mMainWindow = hbInstance->allMainWindows().first();
    54     mMainWindow = hbInstance->allMainWindows().first();
    52     
    55     
    53     mSoftKeyBackAction = new HbAction(Hb::BackNaviAction, this);
    56     mSoftKeyBackAction = new HbAction(Hb::BackNaviAction, this);
    56     // Create view for the application.
    59     // Create view for the application.
    57     // Set the name for the view. The name should be same as the view's
    60     // Set the name for the view. The name should be same as the view's
    58     // name in docml.
    61     // name in docml.
    59     setObjectName("bt_search_view");
    62     setObjectName("bt_search_view");
    60 
    63 
       
    64     mLoader = new HbDocumentLoader();
       
    65     // Pass the view to documentloader. Document loader uses this view
       
    66     // when docml is parsed, instead of creating new view.
    61     QObjectList objectList;
    67     QObjectList objectList;
    62     objectList.append(this);
    68     objectList.append(this);
    63     // Pass the view to documentloader. Document loader uses this view
       
    64     // when docml is parsed, instead of creating new view.
       
    65     mLoader = new HbDocumentLoader();
       
    66     mLoader->setObjectTree(objectList);
    69     mLoader->setObjectTree(objectList);
    67     
    70     
    68     // read view info from docml file
    71     // read view info from docml file
    69     bool ok = false;
    72     bool ok = false;
    70     mLoader->load( BTUI_SEARCHVIEW_DOCML, &ok );
    73     mLoader->load( BTUI_SEARCHVIEW_DOCML, &ok );
   117     
   120     
   118     // load tool bar actions
   121     // load tool bar actions
   119     mViewBy = static_cast<HbAction*>( mLoader->findObject( "viewByAction" ) );
   122     mViewBy = static_cast<HbAction*>( mLoader->findObject( "viewByAction" ) );
   120     BTUI_ASSERT_X( mViewBy, "bt-search-view", "view by action missing" ); 
   123     BTUI_ASSERT_X( mViewBy, "bt-search-view", "view by action missing" ); 
   121 //    TODO - Need to implement the View by
   124 //    TODO - Need to implement the View by
   122 //    ret = connect(viewByAction, SIGNAL(triggered()), this, SLOT(noOp()));
   125     ret = connect(mViewBy, SIGNAL(triggered()), this, SLOT(viewByDeviceTypeDialog()));
   123 //    BTUI_ASSERT_X( ret, "bt-search-view", "viewByAction can't connect" ); 
   126     BTUI_ASSERT_X( ret, "bt-search-view", "viewByAction can't connect" ); 
   124 
   127 
   125     mStop = static_cast<HbAction*>( mLoader->findObject( "stopAction" ) );
   128     mStop = static_cast<HbAction*>( mLoader->findObject( "stopAction" ) );
   126     BTUI_ASSERT_X( mStop, "bt-search-view", "stopAction missing" ); 
   129     BTUI_ASSERT_X( mStop, "bt-search-view", "stopAction missing" ); 
   127     ret = connect(mStop, SIGNAL(triggered()), this, SLOT(stopSearching()));
   130     ret = connect(mStop, SIGNAL(triggered()), this, SLOT(stopSearching()));
   128     BTUI_ASSERT_X( ret, "bt-search-view", "stopAction can't connect" ); 
   131     BTUI_ASSERT_X( ret, "bt-search-view", "stopAction can't connect" ); 
   148     BTUI_ASSERT_X( mConnect, "bt-search-view", "connectAction missing" ); 
   151     BTUI_ASSERT_X( mConnect, "bt-search-view", "connectAction missing" ); 
   149     mConnect->setText(hbTrId("txt_bt_menu_connect"));
   152     mConnect->setText(hbTrId("txt_bt_menu_connect"));
   150     
   153     
   151     ret = connect(mDeviceList, SIGNAL(activated(QModelIndex)), this, SLOT(deviceSelected(QModelIndex)));
   154     ret = connect(mDeviceList, SIGNAL(activated(QModelIndex)), this, SLOT(deviceSelected(QModelIndex)));
   152     BTUI_ASSERT_X( ret, "bt-search-view", "deviceSelected can't connect" ); 
   155     BTUI_ASSERT_X( ret, "bt-search-view", "deviceSelected can't connect" ); 
       
   156     
       
   157     // initialize device type list for "view by" option
       
   158     // Note:  this list needs to be in the same order as enum devTypeSelectionList
       
   159     mDevTypeList << hbTrId("txt_bt_list_audio_devices")
       
   160             << hbTrId("txt_bt_list_computers") 
       
   161             << hbTrId("txt_bt_list_input_devices") 
       
   162             << hbTrId("txt_bt_list_phones") 
       
   163             << hbTrId("txt_bt_list_other_devices");
   153 }
   164 }
   154 
   165 
   155 BtCpUiSearchView::~BtCpUiSearchView()
   166 BtCpUiSearchView::~BtCpUiSearchView()
   156 {
   167 {
       
   168     delete mLoader; // Also deletes all widgets that it constructed.
       
   169     
   157     setNavigationAction(0);
   170     setNavigationAction(0);
       
   171     disconnect( mSoftKeyBackAction );
   158     delete mSoftKeyBackAction;
   172     delete mSoftKeyBackAction;
   159     
   173     
   160     if(mAbstractDelegate) {
   174     if(mAbstractDelegate) {
   161         delete mAbstractDelegate;
   175         delete mAbstractDelegate;
   162         mAbstractDelegate = 0;
       
   163     }
   176     }
   164     
   177     
   165     if(mBtuiModelSortFilter) {
   178     if(mBtuiModelSortFilter) {
   166         delete mBtuiModelSortFilter;
   179         delete mBtuiModelSortFilter;
   167         mBtuiModelSortFilter = 0; 
       
   168     }
   180     }
   169 }
   181 }
   170 
   182 
   171 void BtCpUiSearchView::changeOrientation( Qt::Orientation orientation )
   183 void BtCpUiSearchView::changeOrientation( Qt::Orientation orientation )
   172 {
   184 {
   181         mLoader->load( BTUI_SEARCHVIEW_DOCML, "landscape_ui", &ok );
   193         mLoader->load( BTUI_SEARCHVIEW_DOCML, "landscape_ui", &ok );
   182         BTUI_ASSERT_X( ok, "bt-search-view", "Invalid docml file: landscape section problem" );
   194         BTUI_ASSERT_X( ok, "bt-search-view", "Invalid docml file: landscape section problem" );
   183     }
   195     }
   184 }
   196 }
   185 
   197 
       
   198 
       
   199 void BtCpUiSearchView::viewByDeviceTypeDialog()
       
   200 {
       
   201     HbSelectionDialog *query = new HbSelectionDialog;
       
   202     query->setStringItems(mDevTypeList, mLastSelectionIndex);
       
   203     query->setSelectionMode(HbAbstractItemView::MultiSelection);
       
   204 
       
   205     QList<QVariant> current;
       
   206     current.append(QVariant(0));
       
   207     query->setSelectedItems(current);
       
   208 
       
   209     query->setAttribute(Qt::WA_DeleteOnClose);
       
   210     
       
   211     // Set the heading for the dialog.
       
   212     HbLabel *headingLabel = new HbLabel(hbTrId("txt_bt_title_show"), query);
       
   213     query->setHeadingWidget(headingLabel);
       
   214 
       
   215 
       
   216     query->open(this,SLOT(viewByDialogClosed(HbAction*)));
       
   217 }
       
   218 /*!
       
   219    Callback for dialog closing
       
   220      ToDo:  this API might change in the future, see e-mails in Orbit-dev list, 
       
   221          e.g. 27.4.2010 Raju Abhilash 
       
   222  */
       
   223 void BtCpUiSearchView::viewByDialogClosed(HbAction* action)
       
   224 {
       
   225     HbSelectionDialog *dlg = (HbSelectionDialog*)(sender());
       
   226     bool first = true;
       
   227 
       
   228     if (action == dlg->actions().first()) {  // user pressed "Ok"
       
   229         // Get selected items.
       
   230         QList<QVariant> selections;
       
   231         selections = dlg->selectedItems();
       
   232 //        QString result;
       
   233         int devTypesWanted = 0;
       
   234         for (int i=0; i < selections.count(); i++) {
       
   235 //            result += mDevTypeList.at(selections.at(i).toInt()) + " ";
       
   236             if (first) {
       
   237                 // this will be used as default value when opening dialog again
       
   238                 // there is no way to specify multiple default values with HbSelectionDialog
       
   239                 mLastSelectionIndex = selections.at(i).toInt();
       
   240                 first = false;
       
   241             }
       
   242             switch (selections.at(i).toInt()) {
       
   243             case BtUiDevAudioDevice:
       
   244                 devTypesWanted |= BtuiDevProperty::AVDev;
       
   245                 break;
       
   246             case BtUiDevComputer:
       
   247                 devTypesWanted |= BtuiDevProperty::Computer;
       
   248                 break;
       
   249             case BtUiDevInputDevice:
       
   250                 devTypesWanted |= BtuiDevProperty::Peripheral;
       
   251                 break;
       
   252             case BtUiDevPhone:
       
   253                 devTypesWanted |= BtuiDevProperty::Phone;
       
   254                 break;
       
   255             case BtUiDevOtherDevice:
       
   256                 devTypesWanted |= (BtuiDevProperty::LANAccessDev |
       
   257                         BtuiDevProperty::Toy |
       
   258                         BtuiDevProperty::WearableDev |
       
   259                         BtuiDevProperty::ImagingDev |
       
   260                         BtuiDevProperty::HealthDev |
       
   261                         BtuiDevProperty::UncategorizedDev);
       
   262                 break;
       
   263             default:
       
   264                 // should never get here
       
   265                 BTUI_ASSERT_X(false, "BtCpUiSearchView::viewByDialogClosed()", 
       
   266                         "wrong device type in viewBy dialog!");
       
   267             }
       
   268         }
       
   269 //        qDebug() << result << "bits " << devTypesWanted;
       
   270         if (devTypesWanted) {
       
   271             mBtuiModelSortFilter->clearDeviceMajorFilters();
       
   272             mBtuiModelSortFilter->addDeviceMajorFilter(BtuiDevProperty::InRange,
       
   273                             BtuiModelSortFilter::AtLeastMatch);   // device must be in range
       
   274             mBtuiModelSortFilter->addDeviceMajorFilter(devTypesWanted,
       
   275                     BtuiModelSortFilter::RoughMatch);             // device can be any one of selected ones
       
   276         }
       
   277     }
       
   278 }
       
   279 
   186 void BtCpUiSearchView::stopSearching()
   280 void BtCpUiSearchView::stopSearching()
   187 {
   281 {
   188     // Stop delegate
   282     // Stop delegate
   189     
   283     
   190     // Change label and buttons to reflect status
   284     // Change label and buttons to reflect status
   194 
   288 
   195     // Stop delegate
   289     // Stop delegate
   196     mAbstractDelegate->cancel();
   290     mAbstractDelegate->cancel();
   197 }
   291 }
   198 
   292 
       
   293 void BtCpUiSearchView::startSearchDelegate ()
       
   294 {
       
   295     bool ret(false);
       
   296     
       
   297     if(mAbstractDelegate) {
       
   298         mAbstractDelegate->cancel();
       
   299         disconnect( mAbstractDelegate );
       
   300         delete mAbstractDelegate;
       
   301 		mAbstractDelegate = 0;
       
   302     }
       
   303     // Create the inquiry delegate.
       
   304     mAbstractDelegate = BtDelegateFactory::newDelegate(BtDelegate::Inquiry, mSettingModel, mDeviceModel );
       
   305     
       
   306     // Connect to the signal from the BtDelegateInquiry for completion of the search request
       
   307     ret = connect(mAbstractDelegate, SIGNAL(commandCompleted(int)), this, SLOT(searchDelegateCompleted(int)));
       
   308     BTUI_ASSERT_X( ret, "bt-search-view", "searchDelegateCompleted can't connect" );
       
   309     
       
   310     // Connect to the signal from the BtuiModel when the search has been completed.
       
   311     ret = connect(mDeviceModel, SIGNAL(deviceSearchCompleted(int)), this, SLOT(deviceSearchCompleted(int)));
       
   312     BTUI_ASSERT_X( ret, "bt-search-view", "deviceSearchCompleted can't connect" ); 
       
   313     
       
   314     mAbstractDelegate->exec(QVariant());
       
   315 }
       
   316 
   199 void BtCpUiSearchView::retrySearch()
   317 void BtCpUiSearchView::retrySearch()
   200 {
   318 {
   201     // Retry delegate
   319     // Retry delegate
   202 
   320 
   203     // Change label and buttons to reflect status
   321     // Change label and buttons to reflect status
   204     mLabelSearching->setPlainText(hbTrId("txt_bt_subhead_searching"));
   322     mLabelSearching->setPlainText(hbTrId("txt_bt_subhead_searching"));
   205     mRetry->setEnabled(false);
   323     mRetry->setEnabled(false);
   206     mStop->setEnabled(true);
   324     mStop->setEnabled(true);
   207     
   325     
       
   326     BtCpUiSearchListViewItem *prototype = new BtCpUiSearchListViewItem(mDeviceList);
       
   327     prototype->setModelSortFilter(mBtuiModelSortFilter);
       
   328     mDeviceList->setItemPrototype(prototype);
       
   329 
   208     // Make use of the delegate and start the search.
   330     // Make use of the delegate and start the search.
   209     mAbstractDelegate->cancel();
   331     startSearchDelegate ();
   210     mDeviceList->setModel(mBtuiModelSortFilter);
       
   211     mAbstractDelegate->exec(QVariant());
       
   212 }
   332 }
   213 
   333 
   214 void BtCpUiSearchView::setSoftkeyBack()
   334 void BtCpUiSearchView::setSoftkeyBack()
   215 {
   335 {
   216     if (navigationAction() != mSoftKeyBackAction) {
   336     if (navigationAction() != mSoftKeyBackAction) {
   229 {
   349 {
   230     Q_UNUSED(value);
   350     Q_UNUSED(value);
   231     Q_UNUSED(cmdId);  
   351     Q_UNUSED(cmdId);  
   232     
   352     
   233     setSoftkeyBack();
   353     setSoftkeyBack();
   234     
   354             
   235     bool ret(false);
       
   236     
       
   237     if(!mAbstractDelegate) {
       
   238         // Create the inquiry delegate.
       
   239         mAbstractDelegate = BtDelegateFactory::newDelegate(
       
   240                 BtDelegate::Inquiry, mSettingModel, mDeviceModel );
       
   241     }
       
   242     
       
   243     // Connect to the signal from the BtDelegateInquiry for completion of the search request
       
   244     ret = connect(mAbstractDelegate, SIGNAL(commandCompleted(int)), this, SLOT(searchDelegateCompleted(int)));
       
   245     BTUI_ASSERT_X( ret, "bt-search-view", "searchDelegateCompleted can't connect" );
       
   246     
       
   247     // Connect to the signal from the BtuiModel when the search has been completed.
       
   248     ret = connect(mDeviceModel, SIGNAL(deviceSearchCompleted(int)), this, SLOT(deviceSearchCompleted(int)));
       
   249     BTUI_ASSERT_X( ret, "bt-search-view", "deviceSearchCompleted can't connect" ); 
       
   250     
       
   251     if(!mBtuiModelSortFilter) {
   355     if(!mBtuiModelSortFilter) {
   252         mBtuiModelSortFilter = new BtuiModelSortFilter(this);
   356         mBtuiModelSortFilter = new BtuiModelSortFilter(this);
   253     }
   357     }
   254     mBtuiModelSortFilter->setSourceModel(mDeviceModel);
   358     mBtuiModelSortFilter->setSourceModel(mDeviceModel);
   255     mBtuiModelSortFilter->addDeviceMajorFilter(BtDeviceModel::InRange, BtuiModelSortFilter::AtLeastMatch);
   359     mBtuiModelSortFilter->addDeviceMajorFilter(
       
   360             BtuiDevProperty::InRange, BtuiModelSortFilter::AtLeastMatch);
   256     mDeviceList->setModel(mBtuiModelSortFilter);
   361     mDeviceList->setModel(mBtuiModelSortFilter);
   257 
   362 
       
   363     BtCpUiSearchListViewItem *prototype = new BtCpUiSearchListViewItem(mDeviceList);
       
   364     prototype->setModelSortFilter(mBtuiModelSortFilter);
       
   365     mDeviceList->setItemPrototype(prototype);
       
   366 
   258     // Make use of the delegate and start the search.
   367     // Make use of the delegate and start the search.
   259     mAbstractDelegate->exec(QVariant());
   368     startSearchDelegate ();
   260 }
   369 }
   261 
   370 
   262 void BtCpUiSearchView::deactivateView()
   371 void BtCpUiSearchView::deactivateView()
   263 {
   372 {
       
   373     if( mAbstractDelegate ) {
       
   374        disconnect( mAbstractDelegate ); 
       
   375     }
       
   376     disconnect( mDeviceModel );
   264 }
   377 }
   265 
   378 
   266 void BtCpUiSearchView::searchDelegateCompleted(int error)
   379 void BtCpUiSearchView::searchDelegateCompleted(int error)
   267 {
   380 {
   268     //TODO - handle error.
   381     //TODO - handle error.