bluetoothengine/btui/btcpplugin/btcpuimainview.cpp
changeset 42 b72428996822
parent 31 a0ea99b6fa53
child 47 9e2a905b887f
--- a/bluetoothengine/btui/btcpplugin/btcpuimainview.cpp	Fri May 28 17:03:06 2010 +0300
+++ b/bluetoothengine/btui/btcpplugin/btcpuimainview.cpp	Mon Jul 12 18:51:05 2010 +0300
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
+* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
@@ -33,13 +33,15 @@
 #include <hbmenu.h>
 #include <hbaction.h>
 #include <hbcombobox.h>
+#include <hbgroupbox.h>
 #include "btcpuisearchview.h"
 #include "btcpuideviceview.h"
 #include <bluetoothuitrace.h>
 #include <btdelegatefactory.h>
 #include <btabstractdelegate.h>
 #include "btqtconstants.h"
-
+#include "btcpuimainlistviewitem.h"
+#include "btuidevtypemap.h"
 
 // docml to load
 const char* BTUI_MAINVIEW_DOCML = ":/docml/bt-main-view.docml";
@@ -57,7 +59,7 @@
       mAbstractDelegate(0), mMainFilterModel(0)
 {
     bool ret(false);
-    
+
     mMainWindow = hbInstance->allMainWindows().first();
     mMainView = this;
     
@@ -66,11 +68,11 @@
     // name in docml.
     setObjectName("view");
 
+    mLoader = new HbDocumentLoader();
+    // Pass the view to documentloader. Document loader uses this view
+    // when docml is parsed, instead of creating new view.
     QObjectList objectList;
     objectList.append(this);
-    // Pass the view to documentloader. Document loader uses this view
-    // when docml is parsed, instead of creating new view.
-    mLoader = new HbDocumentLoader();
     mLoader->setObjectTree(objectList);
 
     bool ok = false;
@@ -103,6 +105,14 @@
     mVisibilityMode=0;
     mVisibilityMode = qobject_cast<HbComboBox *>( mLoader->findWidget( "combobox" ) );
     BTUI_ASSERT_X( mVisibilityMode != 0, "bt-main-view", "visibility combobox not found" );
+    // add new item for temporary visibility
+    // NOTE:  translation (at least default english) gives string "(p)Visible for 5 min", 
+    // if setting 1 min --> "(s)Visible for 1 min", ie p=plural, s=singular, but these should
+    // not be shown to the user!
+    // ToDo:  change this to use translation once it starts working
+    QString tempVis(hbTrId("txt_bt_setlabel_visibility_val_visible_for_l1_min", 5));  
+    //QString tempVis(hbTrId("Visible for 5 min"));  
+    mVisibilityMode->addItem(tempVis, Qt::DisplayRole);
         
     mDeviceList=0;
     mDeviceList = qobject_cast<HbListView *>( mLoader->findWidget( "listView" ) );
@@ -120,7 +130,22 @@
     HbAction *discoverAction = static_cast<HbAction*>( mLoader->findObject( "discoverAction" ) );
     BTUI_ASSERT_X( discoverAction, "bt-main-view", "discover action missing" ); 
     ret = connect(discoverAction, SIGNAL(triggered()), this, SLOT(goToDiscoveryView()));
-    BTUI_ASSERT_X( ret, "bt-main-view", "orientation toggle can't connect" ); 
+    BTUI_ASSERT_X( ret, "bt-main-view", "discover action can't connect" ); 
+
+    // load tool bar actions
+    mAllAction = static_cast<HbAction*>( mLoader->findObject( "allAction" ) );
+    BTUI_ASSERT_X( mAllAction, "bt-main-view", "All action missing" ); 
+    ret = connect(mAllAction, SIGNAL(triggered()), this, SLOT(allActionTriggered()));
+    BTUI_ASSERT_X( ret, "bt-main-view", "all action can't connect" ); 
+
+    // load tool bar actions
+    mPairAction = static_cast<HbAction*>( mLoader->findObject( "pairedAction" ) );
+    BTUI_ASSERT_X( mPairAction, "bt-main-view", "Pair action missing" ); 
+    ret = connect(mPairAction, SIGNAL(triggered()), this, SLOT(pairActionTriggered()));
+    BTUI_ASSERT_X( ret, "bt-main-view", "pair action can't connect" ); 
+
+    mGroupBox = qobject_cast<HbGroupBox *>( mLoader->findWidget( "groupBox" ) );
+    BTUI_ASSERT_X( mGroupBox != 0, "bt-main-view", "Group Box not found" ); 
     
     //*********************Testing device view START****************************//
     HbAction *removePairedDevices = static_cast<HbAction*>( mLoader->findObject( "removePairedDevices" ) );
@@ -158,14 +183,14 @@
     mMainFilterModel = new BtuiModelSortFilter(this);
     
     mMainFilterModel->setSourceModel( mDeviceModel );
-    // filter to match only InRegistry devices
-    mMainFilterModel->addDeviceMajorFilter(
-            BtDeviceModel::InRegistry, 
-            BtuiModelSortFilter::AtLeastMatch);
+    updateDeviceListFilter(BtuiPaired);
+	    // List view item
+    BtCpUiMainListViewItem *prototype = new BtCpUiMainListViewItem(mDeviceList);
+    prototype->setModelSortFilter(mMainFilterModel);
+    mDeviceList->setItemPrototype(prototype);
 
     mDeviceList->setModel(mMainFilterModel);
 
-    
 }
 
 /*!
@@ -174,13 +199,8 @@
 BtCpUiMainView::~BtCpUiMainView()
 {
     delete mLoader; // Also deletes all widgets that it constructed.
-    
     mMainWindow->removeView(mSearchView);
-    delete mSearchView;
-    
     mMainWindow->removeView(mDeviceView);
-    delete mDeviceView;
-        
 	if (mAbstractDelegate) {
         delete mAbstractDelegate;
     }
@@ -189,10 +209,10 @@
 /*! 
     from base class, initialize the view
  */
-void BtCpUiMainView::activateView(const QVariant& value, int cmdId )
+void BtCpUiMainView::activateView(const QVariant& value, bool fromBackButton )
 {
     Q_UNUSED(value);
-    Q_UNUSED(cmdId);
+    Q_UNUSED(fromBackButton);
     
 }
 
@@ -204,14 +224,9 @@
 
 }
 
-void BtCpUiMainView::itemActivated(QModelIndex index)
-{
-    Q_UNUSED(index);
-}
-
 void BtCpUiMainView::goToDiscoveryView()
 {
-    changeView( SearchView, false, 0 );
+    changeView( SearchView, false );
 }
 
 void BtCpUiMainView::goToDeviceView(const QModelIndex& modelIndex)
@@ -219,7 +234,7 @@
     //the QModelIndex of the selected device should be given as parameter here 
     QVariant params;
     params.setValue(modelIndex);
-    changeView( DeviceView, false, 0, params );
+    changeView( DeviceView, false, params );
 }
 
 Qt::Orientation BtCpUiMainView::orientation()
@@ -243,8 +258,8 @@
 
 void BtCpUiMainView::setPrevBtLocalName()
 {
-    //Should we notify user this as Error...?
-    HbNotificationDialog::launchDialog(hbTrId("Error"));
+    //ToDo: Should we notify user this as Error...?
+    //HbNotificationDialog::launchDialog(hbTrId("Error"));
     QModelIndex index = mSettingModel->index( BtSettingModel::LocalBtNameRow,0 );
     
     mDeviceNameEdit->setText( mSettingModel->data(
@@ -276,12 +291,12 @@
     
     VisibilityMode mode = indexToVisibilityMode(index);
     list.append(QVariant((int)mode));
-    if(BtTemporary == VisibilityMode(mode)) {
+    if( BtTemporary == VisibilityMode(mode) ) {
         //Right now hardcoded to 5 Mins.
         list.append(QVariant(5));
     }
     //Error handling has to be done.    
-    if (!mAbstractDelegate) {
+    if ( !mAbstractDelegate ) {
         mAbstractDelegate = BtDelegateFactory::newDelegate(BtDelegate::Visibility, 
                 mSettingModel, mDeviceModel); 
         connect( mAbstractDelegate, SIGNAL(commandCompleted(int)), this, SLOT(visibilityDelegateCompleted(int)) );
@@ -295,24 +310,49 @@
 
 void BtCpUiMainView::setPrevVisibilityMode()
 {
-    bool ret(false);
-    
-    //Should we notify this error to user..?
-    HbNotificationDialog::launchDialog(hbTrId("Error"));
+   
     QModelIndex index = mSettingModel->index( BtSettingModel::VisibilityRow, 0 );
     
-    ret = disconnect(mVisibilityMode, SIGNAL(currentIndexChanged (int)), 
-                    this, SLOT(visibilityChanged (int)));
-    BTUI_ASSERT_X( ret, "BtCpUiMainView::setPrevVisibilityMode", "can't disconnect signal" );
-    
     mVisibilityMode->setCurrentIndex ( visibilityModeToIndex((VisibilityMode)
                 mSettingModel->data(index,BtSettingModel::SettingValueRole).toInt()) );
     
-    //Handle Visibility Change User Interaction
-    ret = connect(mVisibilityMode, SIGNAL(currentIndexChanged (int)), 
-            this, SLOT(visibilityChanged (int)));
-    BTUI_ASSERT_X( ret, "BtCpUiMainView::setPrevVisibilityMode", "can't connect signal" );
+}
+
+
+void BtCpUiMainView::allActionTriggered()
+{
+    updateDeviceListFilter(BtuiAll);
+}
+
+void BtCpUiMainView::pairActionTriggered()
+{
+    updateDeviceListFilter(BtuiPaired);
+}
 
+void BtCpUiMainView::updateDeviceListFilter(BtCpUiMainView::filterType filter)
+{
+    mMainFilterModel->clearDeviceMajorFilters();
+    
+    switch (filter) {
+        case BtuiAll:
+            mGroupBox->setHeading(hbTrId("txt_bt_subhead_bluetooth_all_devices"));
+            mPairAction->setEnabled(true);
+            mAllAction->setEnabled(false);
+            mMainFilterModel->addDeviceMajorFilter(
+                    BtuiDevProperty::InRegistry, 
+                    BtuiModelSortFilter::AtLeastMatch);
+
+            break;
+        case BtuiPaired:
+            mGroupBox->setHeading(hbTrId("txt_bt_subhead_bluetooth_paired_devices"));
+            mPairAction->setEnabled(false);
+            mAllAction->setEnabled(true);
+            mMainFilterModel->addDeviceMajorFilter(
+                    BtuiDevProperty::InRegistry | BtuiDevProperty::Bonded, 
+                    BtuiModelSortFilter::AtLeastMatch);
+
+            break;
+    }
 }
 
 
@@ -351,21 +391,14 @@
     }
 }
 
-void BtCpUiMainView::commandCompleted( int cmdId, int err, const QString &diagnostic )
-{
-    Q_UNUSED(cmdId);
-    Q_UNUSED(err);
-    Q_UNUSED(diagnostic);
-
-}
-
 /*!
     Slot for receiving notification of local setting changes from the model.
     Identify the setting changed and update the corresponding UI item.
  */
 void BtCpUiMainView::updateSettingItems(const QModelIndex &topLeft, const QModelIndex &bottomRight)
 {   
-
+    bool val(false);
+    
     // update only the part of the view specified by the model's row(s)
     for (int i=topLeft.row(); i <= bottomRight.row(); i++) {
         QModelIndex index = mSettingModel->index( i, 0);
@@ -376,8 +409,17 @@
                     mSettingModel->data(index,BtSettingModel::settingDisplayRole).toString() );
             break;
         case BtSettingModel::PowerStateRow:
-            mPowerButton->setText( mSettingModel->data(index,
-                    BtSettingModel::settingDisplayRole).toString() );
+            val = mSettingModel->data(index, BtSettingModel::SettingValueRole).toBool();
+            if (val) {
+                HbIcon icon("qtg_mono_bluetooth");
+                icon.setIconName("qtg_mono_bluetooth");
+                mPowerButton->setIcon(icon);  
+            }
+            else {
+                HbIcon icon("qtg_mono_bluetooth_off");
+                icon.setIconName("qtg_mono_bluetooth_off");
+                mPowerButton->setIcon(icon);
+            }
             break;
         case BtSettingModel::VisibilityRow:
             mVisibilityMode->setCurrentIndex ( visibilityModeToIndex((VisibilityMode)
@@ -393,15 +435,24 @@
  */
 void BtCpUiMainView::changePowerState()
 {
+    QModelIndex index = mSettingModel->index(BtSettingModel::PowerStateRow, 0);
+    PowerStateQtValue powerState = (PowerStateQtValue)mSettingModel->data(index, Qt::EditRole).toInt();
+    BTUI_ASSERT_X(((powerState == BtPowerOn) || (powerState == BtPowerOff)), 
+            "BtCpUiMainView::changePowerState()", "incorrect qt power state");
+
+    if (powerState == BtPowerOff) {
+        powerState = BtPowerOn;
+    }
+    else {
+        powerState = BtPowerOff;
+    } 
     
-    QModelIndex index = mSettingModel->index(BtSettingModel::PowerStateRow, 0);
-    QVariant powerState = mSettingModel->data(index, Qt::EditRole);
     if (!mAbstractDelegate)//if there is no other delegate running
     { 
         mAbstractDelegate = BtDelegateFactory::newDelegate(BtDelegate::ManagePower, 
                 mSettingModel, mDeviceModel ); 
         connect( mAbstractDelegate, SIGNAL(commandCompleted(int)), this, SLOT(powerDelegateCompleted(int)) );
-        mAbstractDelegate->exec(powerState);
+        mAbstractDelegate->exec(QVariant((int)powerState));
     }
    
 }
@@ -424,7 +475,7 @@
  */
 VisibilityMode BtCpUiMainView::indexToVisibilityMode(int index)
 {
-    VisibilityMode mode; 
+    VisibilityMode mode = BtVisibilityUnknown;
     switch(index) {
     case UiRowBtHidden:  
         mode = BtHidden;
@@ -436,7 +487,7 @@
         mode = BtTemporary;
         break;
     default:
-        mode = BtUnknown;
+        BTUI_ASSERT_X(false, "BtCpUiMainView::indexToVisibilityMode", "invalid mode");
     }
     return mode;
 }
@@ -446,7 +497,7 @@
  */
 int BtCpUiMainView::visibilityModeToIndex(VisibilityMode mode)
 {
-    int uiRow;
+    int uiRow = UiRowBtUnknown;
     switch(mode) {
     case BtHidden:  
         uiRow = UiRowBtHidden;
@@ -458,15 +509,12 @@
         uiRow = UiRowBtTemporary;
         break;
     default:
-        uiRow = -1;  // error
+        BTUI_ASSERT_X(false, "BtCpUiMainView::visibilityModeToIndex", "invalid mode");
     }
     return uiRow;
 }
-//////////////////////
-//
-// from view manager
-// 
-//////////////////////
+
+
 
 /*!
     Create views(main view, device view and search view).
@@ -495,12 +543,11 @@
 
 /*!
     Switch between the views.  
-    Parameter cmdId is used for automatically executing command.
     Parameter "value" is optional except for GadgetView, 
-    which needs the BT address (QString)
+    which needs the QModelIndex of device
  */
 void BtCpUiMainView::changeView(int targetViewId, bool fromBackButton, 
-        int cmdId, const QVariant& value )
+        const QVariant& value )
 {
     mCurrentView->deactivateView();
 
@@ -509,14 +556,8 @@
     // the previous viewId of target view should not be changed. 
     // Otherwise, loop happens between two views.
     if(!fromBackButton) {
-        if ((targetViewId == DeviceView) && (mCurrentViewId == SearchView)) {
-            // we don't want to return to search view after e.g. pairing a new device
-            mPreviousViewIds[ targetViewId ] = MainView;  
-        } 
-        else {
-            // normal case:  return to previous view
-            mPreviousViewIds[ targetViewId ] = mCurrentViewId;
-        }
+        // normal case:  return to previous view
+        mPreviousViewIds[ targetViewId ] = mCurrentViewId;
     }
 
     // set the new current view 
@@ -525,7 +566,7 @@
     mMainWindow->setCurrentView( mCurrentView );
 
     // do preparation or some actions when new view is activated 
-    mCurrentView->activateView( value, cmdId );
+    mCurrentView->activateView( value, fromBackButton );
 }
  
 
@@ -536,7 +577,7 @@
     QVariant params;
     params.setValue(index);
     
-    changeView( DeviceView, false, 0, params );
+    changeView( DeviceView, false, params );
 }
 
 BtCpUiBaseView * BtCpUiMainView::idToView(int targetViewId)
@@ -554,24 +595,9 @@
     return 0;
 }
 
-/*
-   Jump to previous view.  This function is used when back button is pressed.
- */
-void BtCpUiMainView::switchToPreviousViewReally()
-{  
-//    // jump to previous view of current view.
-//    if( (mCurrentViewId >= 0) && (mCurrentViewId < LastView)) {
-//        changeView( mPreviousViewIds[mCurrentViewId], true, 0 );
-//    } 
-//    else {
-//        BTUI_ASSERT_X(false, "BtCpUiMainView::switchToPreviousViewReally", "invalid view id");
-//    }
-}
-
-
 void BtCpUiMainView::setSoftkeyBack()
 {
-
+    // not needed in main view
 }
 
 /*!
@@ -581,12 +607,8 @@
  */
 void BtCpUiMainView::switchToPreviousView()
 {
-    // jump to previous view of current view.
-    if( (mCurrentViewId >= 0) && (mCurrentViewId < LastView)) {
-        changeView( mPreviousViewIds[mCurrentViewId], true, 0 );
-    } 
-    else {
-        BTUI_ASSERT_X(false, "BtCpUiMainView::switchToPreviousView", "invalid view id");
-    }      
+    BTUI_ASSERT_X( (mCurrentViewId >= 0) && (mCurrentViewId < LastView), 
+            "BtCpUiMainView::switchToPreviousView", "invalid view id");
+    changeView( mPreviousViewIds[mCurrentViewId], true );
 }