photosgallery/viewframework/commandhandlers/commoncommandhandlers/src/glxmediaselectionpopup.cpp
branchRCL_3
changeset 32 78ad99c24f08
parent 25 191387a8b767
child 56 b023a8d2866a
--- a/photosgallery/viewframework/commandhandlers/commoncommandhandlers/src/glxmediaselectionpopup.cpp	Tue May 11 16:13:40 2010 +0300
+++ b/photosgallery/viewframework/commandhandlers/commoncommandhandlers/src/glxmediaselectionpopup.cpp	Tue May 25 12:42:31 2010 +0300
@@ -67,7 +67,8 @@
 //   
 EXPORT_C CGlxMediaListAdaptor::CGlxMediaListAdaptor(
         const MGlxMediaList* aMediaList, TBool aMultiSelection) :
-    iMediaList(aMediaList), iMultiSelection(aMultiSelection)
+    iMediaList(aMediaList), iMultiSelection(aMultiSelection),
+			iStaticItemSelected(EFalse)
     {
     }
 
@@ -153,174 +154,158 @@
     }
 
 // ---------------------------------------------------------------------------
-// CGlxSingleGraphicPopupMenuStyleListBox::IsVisible
+// CGlxMediaListAdaptor::IsStaticItemSelected
 // ---------------------------------------------------------------------------
-// 
-TBool CGlxSingleGraphicPopupMenuStyleListBox::IsVisible(TInt aItemIndex)
-    {
-    TRACER("CGlxSingleGraphicPopupMenuStyleListBox::IsVisible");
-    return (TopItemIndex() <= aItemIndex && aItemIndex <= BottomItemIndex());
-    }
+TBool CGlxMediaListAdaptor::IsStaticItemSelected()
+	{
+	TRACER("CGlxMediaListAdaptor::IsStaticItemSelected");
+	return iStaticItemSelected;
+	}
 
 // ---------------------------------------------------------------------------
-// CGlxSingleGraphicPopupMenuStyleListBox::OfferKeyEventL()
+// CGlxMediaListAdaptor::SetStaticItemSelected
 // ---------------------------------------------------------------------------
-//   
-EXPORT_C TKeyResponse CGlxSingleGraphicPopupMenuStyleListBox::OfferKeyEventL(
-        const TKeyEvent& aKeyEvent, TEventCode aType)
-    {
-    TRACER("CGlxSingleGraphicPopupMenuStyleListBox::OfferKeyEventL");
-    __ASSERT_DEBUG(dynamic_cast< CGlxMediaListAdaptor*>(Model()->ItemTextArray()), Panic(EGlxPanicCastFailed));
-
-    if (aKeyEvent.iCode == EKeyOK && iView)
-        {
-        CGlxMediaListAdaptor* mediaListAdaptor =
-                static_cast<CGlxMediaListAdaptor*> (Model()->ItemTextArray());
+void CGlxMediaListAdaptor::SetStaticItemSelected(TBool aSelected)
+	{
+	TRACER("CGlxMediaListAdaptor::SetStaticItemSelected");
+	iStaticItemSelected = aSelected;
+	}
 
-        if (mediaListAdaptor->MultiSelectionEnabled()
-                && View()->CurrentItemIndex() >= 0)
-            {
-            const TGlxMedia& item = mediaListAdaptor->MediaList()->Item(
-                    View()->CurrentItemIndex());
-            // Reset the observer to populist
-            SetListBoxObserver(iPopupList);
-            if (item.IsStatic())
-                {
-                iListBoxFlags &= (~EMultipleSelection); // turn off multiple selection
-                }
-            else
-                {
-                iListBoxFlags |= EMultipleSelection; // turn on multiple selection
-                }
-            }
-        }
+//---------------------------------------------------------------------------
+// CGlxSingleGraphicPopupMenuStyleList::NewL
+//---------------------------------------------------------------------------
+CGlxSingleGraphicPopupMenuStyleList* CGlxSingleGraphicPopupMenuStyleList::NewL(
+		CEikListBox* aListBox, TInt aCbaResource,
+		AknPopupLayouts::TAknPopupLayouts aType)
+	{
+	TRACER("CGlxSingleGraphicPopupMenuStyleList::NewL");
 
-    return CAknSingleGraphicPopupMenuStyleListBox::OfferKeyEventL(aKeyEvent,
-            aType);
-    }
+	CGlxSingleGraphicPopupMenuStyleList* self =
+		new (ELeave) CGlxSingleGraphicPopupMenuStyleList;
+	CleanupStack::PushL(self);
+	self->ConstructL(aListBox, aCbaResource, aType);
+	CleanupStack::Pop(self);
+	return self;
+	}
 
 // ---------------------------------------------------------------------------
-// CGlxSingleGraphicPopupMenuStyleListBox::HandleListBoxEventL()
+// CGlxSingleGraphicPopupMenuStyleList::HandleListBoxEventL()
 // ---------------------------------------------------------------------------
-// 
-void CGlxSingleGraphicPopupMenuStyleListBox::HandleListBoxEventL(
-        CEikListBox *aListBox, TListBoxEvent aEventType)
+//
+void CGlxSingleGraphicPopupMenuStyleList::HandleListBoxEventL(
+		CEikListBox *aListBox, TListBoxEvent aEventType)
     {
-    TRACER("CGlxSingleGraphicPopupMenuStyleListBox::HandleListBoxEventL");
+    TRACER("CGlxSingleGraphicPopupMenuStyleList::HandleListBoxEventL");
+
+    CAknSingleGraphicPopupMenuStyleListBox* listBox =
+			static_cast<CAknSingleGraphicPopupMenuStyleListBox*> (aListBox);
+
+    CGlxMediaListAdaptor
+			* mediaListAdaptor =
+					static_cast<CGlxMediaListAdaptor*> (listBox->Model()->ItemTextArray());
+
+    TBool staticItemSelected = EFalse;
+    TInt currItemIndx = listBox->View()->CurrentItemIndex();
+
     switch (aEventType)
         {
         case EEventItemClicked:
         case EEventItemSingleClicked:
             {
-            CGlxMediaListAdaptor
-                    * mediaListAdaptor =
-                            static_cast<CGlxMediaListAdaptor*> (Model()->ItemTextArray());
-            if (mediaListAdaptor->MultiSelectionEnabled()
-                    && View()->CurrentItemIndex() >= 0)
+
+            if (mediaListAdaptor->MultiSelectionEnabled() && currItemIndx >= 0)
                 {
                 const TGlxMedia& item = mediaListAdaptor->MediaList()->Item(
-                        View()->CurrentItemIndex());
-                if (item.IsStatic())
+						currItemIndx);
+
+                //Check if media item is user-defined or static
+                staticItemSelected = item.IsStatic();
+                mediaListAdaptor->SetStaticItemSelected(staticItemSelected);
+                if (!staticItemSelected)
                     {
-                    // when the user marks a non-static item and then clicks on
-                    // a static item without moving highlight, the control comes here.
-                    iListBoxFlags &= (~EMultipleSelection); // turn off multiple selection
+					//Mark or UnMark the user-defined item
+                    TBool isMarked = listBox->View()->ItemIsSelected(currItemIndx);
+                    (isMarked == (TBool) ETrue) ? (listBox->View()->DeselectItem(
+							currItemIndx))
+							: (listBox->View()->SelectItemL(currItemIndx));
                     }
-                else
-                    {
-                    iListBoxFlags |= EMultipleSelection; // turn on multiple selection
 
-                    TBool isMarked = View()->ItemIsSelected(
-                            View()->CurrentItemIndex());
-                    (isMarked == (TBool) ETrue)
-                                                ? (View()->DeselectItem(
-                                                       View()->CurrentItemIndex()))
-                                                   : (View()->SelectItemL(
-                                                           View()->CurrentItemIndex()));
-                    }
+                //Show Command Set based on selected items
+                TInt selectCount = listBox->View()->SelectionIndexes()->Count();
+				CEikButtonGroupContainer* cbaContainer = ButtonGroupContainer();
+				//Show 'OK' only if a static item or more than
+				//1 user-defined item is selected
+				if(staticItemSelected || selectCount)
+					{
+					cbaContainer->SetCommandSetL(R_GLX_SOFTKEYS_OK_CANCEL);
+					}
+				else
+					{
+					cbaContainer->SetCommandSetL(R_AVKON_SOFTKEYS_CANCEL);
+					}
+				cbaContainer->DrawDeferred();
+
                 }
-            aListBox->DrawDeferred();
+            listBox->DrawDeferred();
+
+            //Forward for default processing, if static item is selected(i.e. For Tags)
+			//or if List Box's Multiple selection is Disabled(i.e. For Albums)
+			if (staticItemSelected
+					|| (!mediaListAdaptor->MultiSelectionEnabled()))
+				{
+				CAknPopupList::HandleListBoxEventL( aListBox, aEventType);
+				}
+
+			//After Scrolling, then Select "New Tag" i.e Static item is selected
+			if (staticItemSelected)
+				{
+				ProcessCommandL(EAknSoftkeyOk);
+				}
+
             break;
             }
         default:
-            break;
+        	{
+        	CAknPopupList::HandleListBoxEventL( aListBox, aEventType);
+        	break;
+        	}
         }
     }
 
 // ---------------------------------------------------------------------------
-// CGlxSingleGraphicPopupMenuStyleListBox::HandlePointerEventL()
+// CGlxSingleGraphicPopupMenuStyleList::HandlePointerEventL()
 // ---------------------------------------------------------------------------
-//  
-void CGlxSingleGraphicPopupMenuStyleListBox::HandlePointerEventL(
-        const TPointerEvent& aPointerEvent)
+//
+void CGlxSingleGraphicPopupMenuStyleList::HandlePointerEventL(
+		const TPointerEvent& aPointerEvent)
     {
-    TRACER("CGlxSingleGraphicPopupMenuStyleListBox::HandlePointerEventL");
-    CGlxMediaListAdaptor* mediaListAdaptor =
-            static_cast<CGlxMediaListAdaptor*> (Model()->ItemTextArray());
-    if (mediaListAdaptor->MultiSelectionEnabled())
-        {
-        // Need to handle the case when the highlight is on one item
-        // and the user clicks on another media item. The notification 
-        // of changed item index is received very late. In order to
-        // handle this, checking if the index has changed.
-        TInt changedItemIndex = -1;
-        TInt itemIndex = View()->CurrentItemIndex();
-        TBool isItemChanged = View()->XYPosToItemIndex(
-                aPointerEvent.iPosition, changedItemIndex);
-        if (isItemChanged)
-            {
-            itemIndex = changedItemIndex;
-            }
-        const TGlxMedia& item =
-                mediaListAdaptor->MediaList()->Item(itemIndex);
-        if (item.IsStatic())
-            {
-            // A static item is not markable. Turn off multiple selection
-            // Set the listbox observer to default that is CAknPopupList 
-            // object, which will process it further.
-            iListBoxFlags &= (~EMultipleSelection);
-            SetListBoxObserver(iPopupList);
-            }
-        else
-            {
-            // Markable object. The event is handled in HandleListBoxEventL()
-            // Set the listbox observer to the current object.
-            SetListBoxObserver(this);
-            }
+	TRACER("CGlxSingleGraphicPopupMenuStyleList::HandlePointerEventL");
+
+    CAknSingleGraphicPopupMenuStyleListBox* listBox =
+			static_cast<CAknSingleGraphicPopupMenuStyleListBox*> (ListBox());
+	CGlxMediaListAdaptor
+			* mediaListAdaptor =
+					static_cast<CGlxMediaListAdaptor*> (listBox->Model()->ItemTextArray());
 
-        CAknSingleGraphicPopupMenuStyleListBox::HandlePointerEventL(
-                aPointerEvent);
-
-        const CListBoxView::CSelectionIndexArray* selectionIndices =
-                View()->SelectionIndexes();
-        CEikButtonGroupContainer* cbaContainer =
-                iPopupList->ButtonGroupContainer();
-        if (selectionIndices->Count() || item.IsStatic())
-            {
-            cbaContainer->SetCommandSetL(R_GLX_SOFTKEYS_OK_CANCEL);
-            }
-        else
-            {
-            cbaContainer->SetCommandSetL(R_AVKON_SOFTKEYS_CANCEL);
-            }
-        cbaContainer->DrawDeferred();
+	//Check for MultipleSelection is Disbaled
+    if ( !(mediaListAdaptor->MultiSelectionEnabled()) )
+    	{
+		//checking if the index has changed & has valid item available
+		TInt changedItemIndex = KErrNotFound;
+        TBool isItemAvailable = listBox->View()->XYPosToItemIndex(
+				aPointerEvent.iPosition, changedItemIndex);
+        if(isItemAvailable)
+			{
+			//Check if Selected item is static
+			const TGlxMedia& item = mediaListAdaptor->MediaList()->Item(
+					changedItemIndex);
+			mediaListAdaptor->SetStaticItemSelected( item.IsStatic() );
+			}
         }
-    else
-        {
-        CAknSingleGraphicPopupMenuStyleListBox::HandlePointerEventL(
-                aPointerEvent);
-        }
-    }
 
-// ---------------------------------------------------------------------------
-// CGlxSingleGraphicPopupMenuStyleListBox::SetPopupList()
-// ---------------------------------------------------------------------------
-// 
-void CGlxSingleGraphicPopupMenuStyleListBox::SetPopupList(
-        CAknPopupList* aPopupList)
-    {
-    TRACER("CGlxSingleGraphicPopupMenuStyleListBox::SetPopupList");
-    iPopupList = aPopupList;
+    //Forward for default processing
+    CAknPopupList::HandlePointerEventL( aPointerEvent);
+
     }
 
 // ---------------------------------------------------------------------------
@@ -411,11 +396,13 @@
     if (aAccepted)
         {
 
-        if (iMediaList->Item(iListBox->CurrentItemIndex()).IsStatic())
+		//Check if a static item is selected
+		if (iMediaListAdaptor->IsStaticItemSelected())
             {
 			TFileName uiutilitiesrscfile;
-			uiutilitiesrscfile.Append(CGlxResourceUtilities::GetUiUtilitiesResourceFilenameL());
-			CGlxCommandHandlerNewMedia* commandHandlerNewMedia =
+			uiutilitiesrscfile.Append(
+					CGlxResourceUtilities::GetUiUtilitiesResourceFilenameL());
+            CGlxCommandHandlerNewMedia* commandHandlerNewMedia =
 			CGlxCommandHandlerNewMedia::NewL(this,uiutilitiesrscfile);
             TGlxMediaId newMediaId;
             TInt error = commandHandlerNewMedia->ExecuteLD(newMediaId);
@@ -595,7 +582,7 @@
         {
         for (TInt i = 0; i < aItemIndexes.Count(); i++)
             {
-            if (iListBox->IsVisible(aItemIndexes[i]))
+            if (IsListBoxItemVisible(aItemIndexes[i]))
                 {
                 iListBox->RedrawItem(aItemIndexes[i]);
                 }
@@ -618,8 +605,8 @@
             iListBox->HandleItemAdditionL();
             iHandleItemAdditionRequired = EFalse;
             }
-        if (iListBox->IsVisible(aItemIndex))
-            {
+        if (IsListBoxItemVisible(aItemIndex))
+			{
             iListBox->RedrawItem(aItemIndex);
             }
         }
@@ -841,10 +828,10 @@
     {
     TRACER("CGlxMediaSelectionPopup::ConstructPopupListL");
     // create the list box
-    iListBox = new (ELeave) CGlxSingleGraphicPopupMenuStyleListBox;
+    iListBox = new (ELeave) CAknSingleGraphicPopupMenuStyleListBox;
 
     // create the popup list
-    iPopupList = CAknPopupList::NewL(iListBox, R_AVKON_SOFTKEYS_CANCEL);
+    iPopupList = CGlxSingleGraphicPopupMenuStyleList::NewL( iListBox, R_AVKON_SOFTKEYS_CANCEL) ;
 
     // set the title of the popup
     __ASSERT_DEBUG(iSelectMediaPopupTitle, Panic(EGlxPanicNullPointer));
@@ -853,7 +840,7 @@
 
     iListBox ->ConstructL(iPopupList,
             aMultiSelection
-                            ? EAknListBoxMultiselectionList
+                            ?  EAknListBoxMultiselectionList
                                : EAknListBoxMenuList);
     iListBox->CreateScrollBarFrameL();
     iListBox->ScrollBarFrame()->SetScrollBarVisibilityL(
@@ -863,7 +850,16 @@
     iListBox->View()->CalcBottomItemIndex();
     iListBox->Model()->SetOwnershipType(ELbmDoesNotOwnItemArray);
 
-    // Store the pointer of AknPopupList
-    iListBox->SetPopupList(iPopupList);
+    //set 'iPopupList' as List Box observer
+    iListBox->SetListBoxObserver(iPopupList);
     }
 
+//-----------------------------------------------------------------------------
+// CGlxMediaSelectionPopup::IsListBoxItemVisible
+//-----------------------------------------------------------------------------
+TBool CGlxMediaSelectionPopup::IsListBoxItemVisible(TInt aIndex)
+	{
+	TRACER("CGlxMediaSelectionPopup::IsListBoxItemVisible");
+	return iListBox && (iListBox->TopItemIndex() <= aIndex &&
+			aIndex <= iListBox->BottomItemIndex());
+	}