imageeditor/plugins/FramePlugin/src/ImageEditorFrameControl.cpp
changeset 1 edfc90759b9f
equal deleted inserted replaced
0:57d4cdd99204 1:edfc90759b9f
       
     1 /*
       
     2 * Copyright (c) 2010 Ixonos Plc.
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the "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 * Ixonos Plc
       
    14 *
       
    15 * Description:  
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 //  INCLUDES
       
    22 #include <fbs.h>
       
    23 #include <f32file.h>
       
    24 #include <badesca.h>
       
    25 #include <gdi.h>
       
    26 #include <eikenv.h>
       
    27 #include <bitdev.h>
       
    28 #include <bautils.h>
       
    29 
       
    30 #include <aknview.h>
       
    31 #include <aknutils.h>
       
    32 #include <ConeResLoader.h>
       
    33 
       
    34 #ifdef RD_TACTILE_FEEDBACK 
       
    35 #include <touchfeedback.h>
       
    36 #endif /* RD_TACTILE_FEEDBACK  */
       
    37 
       
    38 #include "ImageEditorUI.hrh"
       
    39 #include "ImageEditorPluginBase.hrh"
       
    40 #include "Frame.hrh"
       
    41 #include "PluginInfo.h"
       
    42 #include "ImageEditorError.h"
       
    43 #include "ResolutionUtil.h"
       
    44 #include <frame.rsg>
       
    45 
       
    46 #include "ImageEditorFrameControl.h"
       
    47 #include "ImageEditorFramePlugin.h"
       
    48 #include "ImageEditorFramePlugin.pan"
       
    49 
       
    50 //#include "platform_security_literals.hrh"
       
    51 
       
    52 
       
    53 //	CONSTANTS
       
    54 const TInt KCurrentFrameIndex           = 1;
       
    55 const TInt KFrameChangeThreshold        = 30;
       
    56 const TInt KDirectionChangeThreshold    = 7;
       
    57 
       
    58 _LIT (KPgnResourceFile, "frame.rsc");
       
    59 _LIT (KFrameWild, "*.mbm");
       
    60 
       
    61 //=============================================================================
       
    62 CImageEditorFrameControl * CImageEditorFrameControl::NewL (
       
    63 	const TRect &		aRect,
       
    64 	CCoeControl	*		aParent
       
    65 	)
       
    66 {
       
    67     CImageEditorFrameControl * self = new (ELeave) CImageEditorFrameControl;
       
    68     CleanupStack::PushL (self);
       
    69     self->ConstructL (aRect, aParent);
       
    70     CleanupStack::Pop ();   // self
       
    71     return self;
       
    72 }
       
    73 
       
    74 //=============================================================================
       
    75 CImageEditorFrameControl::CImageEditorFrameControl () :
       
    76 iState (EFrameControlStateMin)
       
    77 {
       
    78     
       
    79 }
       
    80 
       
    81 //=============================================================================
       
    82 CImageEditorFrameControl::~CImageEditorFrameControl ()
       
    83 {
       
    84     iMultiplicities.Close();
       
    85 
       
    86     if (iFrameFileArray)
       
    87         {
       
    88         iFrameFileArray->Reset();
       
    89         delete iFrameFileArray;
       
    90         iFrameFileArray = NULL;
       
    91         }
       
    92     iEditorView = NULL;
       
    93     iItem = NULL;
       
    94 }
       
    95 
       
    96 //=============================================================================
       
    97 void CImageEditorFrameControl::ConstructL (
       
    98 	const TRect &		/*aRect*/,
       
    99 	CCoeControl	*		aParent
       
   100 	)
       
   101 {
       
   102 
       
   103 	//	Set parent window
       
   104 	SetContainerWindowL (*aParent);
       
   105    	
       
   106    	iFrameFileArray = new (ELeave) CDesCArrayFlat (8);
       
   107 
       
   108 #ifdef RD_TACTILE_FEEDBACK 
       
   109     iTouchFeedBack = MTouchFeedback::Instance();
       
   110 #endif /* RD_TACTILE_FEEDBACK  */
       
   111     
       
   112 	//	Activate control
       
   113     ActivateL();
       
   114     
       
   115     EnableDragEvents();
       
   116 }
       
   117 
       
   118 //=============================================================================
       
   119 void CImageEditorFrameControl::SetView (CAknView * aView)
       
   120 {
       
   121     iEditorView = aView;
       
   122 }
       
   123 
       
   124 //=============================================================================
       
   125 void CImageEditorFrameControl::SetSelectedUiItemL (CPluginInfo * aItem)
       
   126 {
       
   127     iItem = aItem;
       
   128 }
       
   129 
       
   130 //=============================================================================
       
   131 void CImageEditorFrameControl::PrepareL ()
       
   132 {
       
   133 	iState = EFrameControlStateInitializing;
       
   134 
       
   135     //	Find frames
       
   136     FindFramesL();
       
   137 
       
   138 	//	Select the first frame to be drawn
       
   139     iCurrentFrameIndex = 0;
       
   140     SetNaviPaneTextL();
       
   141     
       
   142     //	Load the first frame synchronously to avoid timing problems
       
   143     SelectFrameL();
       
   144     
       
   145     iState = EFrameControlStateIdle;
       
   146 }
       
   147 
       
   148 //=============================================================================
       
   149 TKeyResponse CImageEditorFrameControl::OfferKeyEventL (
       
   150     const TKeyEvent &   aKeyEvent,
       
   151     TEventCode          aType
       
   152     )
       
   153 {
       
   154     TKeyResponse response = EKeyWasNotConsumed;
       
   155 
       
   156     //  If busy, do not handle anything
       
   157     if ( iState < EFrameControlStateIdle )
       
   158     {
       
   159         response = EKeyWasConsumed;
       
   160     }
       
   161     else if (aType != EEventKey  )
       
   162     {
       
   163         switch (aKeyEvent.iScanCode)
       
   164 		{   
       
   165 		    // Just consume these keys (No EEventKey type event delivered for 
       
   166 		    // these keys so this must be done here)
       
   167 		    case EStdKeyRightShift:
       
   168 		    case EStdKeyLeftShift:
       
   169 			{
       
   170 				response = EKeyWasConsumed;
       
   171                 break;
       
   172 			}
       
   173 		}
       
   174     }
       
   175     //  We handle only event keys
       
   176     else if (EEventKey == aType)
       
   177     {
       
   178 
       
   179 		TBool rotated = CResolutionUtil::Self()->GetLandscape();
       
   180 
       
   181 		switch (aKeyEvent.iCode)
       
   182 		{
       
   183 
       
   184 			// Just consume these keys
       
   185 		    case 0x31: // 1
       
   186 		    case 0x33: // 3
       
   187 		    case 0x35: // 5
       
   188             case 0x37: // 7
       
   189 		    case 0x39: // 9
       
   190 		    case 0x2a: // *
       
   191 		    case 0x23: // #
       
   192 			case EKeyDownArrow:
       
   193 			case EKeyUpArrow:
       
   194 			case EStdKeyIncVolume: // zoom in key
       
   195 			case EStdKeyDecVolume: // zoom out key
       
   196 			{
       
   197 				response = EKeyWasConsumed;
       
   198                 break;
       
   199 			}
       
   200 
       
   201 		    case 0x32: // 2
       
   202 			{
       
   203 				if (rotated)
       
   204 				{
       
   205                 	//  Switch to next frame
       
   206 	                NaviLeftL();					
       
   207 				}
       
   208 				response = EKeyWasConsumed;	
       
   209                 break;
       
   210 			}
       
   211 			case 0x38: // 8
       
   212 			{
       
   213 				if (rotated)
       
   214 				{
       
   215                 	//  Switch to next frame
       
   216 	                NaviRightL();					
       
   217 				}
       
   218 				response = EKeyWasConsumed;	
       
   219                 break;
       
   220 			}
       
   221 
       
   222    		    case 0x34:   		    
       
   223    		    {
       
   224   				if (!rotated)
       
   225 				{
       
   226 		            //  Switch to previous frame
       
   227 		            NaviLeftL();
       
   228 				}
       
   229 				response = EKeyWasConsumed;
       
   230                 break;
       
   231 			}
       
   232 
       
   233 		    case 0x36:
       
   234 			{
       
   235 				if (!rotated)
       
   236 				{
       
   237                 	//  Switch to next frame
       
   238 	                NaviRightL();					
       
   239 				}
       
   240 				response = EKeyWasConsumed;	
       
   241                 break;
       
   242 			}
       
   243 
       
   244 			
       
   245 			case EKeyRightArrow:
       
   246 			{
       
   247 				NaviRightL();					
       
   248 				response = EKeyWasConsumed;	
       
   249                 break;			
       
   250 			}	           
       
   251 
       
   252             case EKeyLeftArrow:
       
   253             {
       
   254             	NaviLeftL();					
       
   255 				response = EKeyWasConsumed;	
       
   256                 break;			
       
   257             }
       
   258             
       
   259             case EKeyOK:
       
   260             case EKeyEnter:
       
   261             {
       
   262 				iEditorView->HandleCommandL (EImageEditorApplyPlugin);
       
   263                 response = EKeyWasConsumed;
       
   264                 break;
       
   265             }
       
   266 
       
   267 			default:
       
   268 			{
       
   269 				break;
       
   270 			}
       
   271 		}
       
   272 	}
       
   273     return response;
       
   274 }
       
   275 
       
   276 //=============================================================================
       
   277 void CImageEditorFrameControl::SizeChanged()
       
   278 {
       
   279 
       
   280 }
       
   281 
       
   282 //=============================================================================
       
   283 TDesC & CImageEditorFrameControl::GetParam ()
       
   284 {
       
   285 	return iParameter;
       
   286 }
       
   287 
       
   288 //=============================================================================
       
   289 void CImageEditorFrameControl::HandlePluginCommandL (const TInt aCommand)
       
   290 {
       
   291     switch (aCommand) 
       
   292     {
       
   293         case EPgnSoftkeyIdCancel:
       
   294         {
       
   295             iEditorView->HandleCommandL (EImageEditorCancelPlugin);
       
   296             break;
       
   297         }
       
   298         case EPgnSoftkeyIdOk:
       
   299         {
       
   300             iEditorView->HandleCommandL (EImageEditorApplyPlugin);
       
   301             break;
       
   302         }
       
   303         default:
       
   304         {
       
   305             break;
       
   306         }
       
   307     }
       
   308 }
       
   309 
       
   310 //=============================================================================
       
   311 TInt CImageEditorFrameControl::GetSoftkeyIndexL()
       
   312 {
       
   313     return 0;
       
   314 }
       
   315 
       
   316 //=============================================================================
       
   317 TPtrC CImageEditorFrameControl::GetNaviPaneTextL (
       
   318     TBool& aLeftNaviPaneScrollButtonVisibile, 
       
   319     TBool& aRightNaviPaneScrollButtonVisible )
       
   320 {
       
   321     if ( iFrameCount > 1)
       
   322     {
       
   323         aLeftNaviPaneScrollButtonVisibile = ETrue;
       
   324         aRightNaviPaneScrollButtonVisible = ETrue;
       
   325     }
       
   326     else
       
   327     {
       
   328         aLeftNaviPaneScrollButtonVisibile = EFalse;
       
   329         aRightNaviPaneScrollButtonVisible = EFalse;
       
   330     }
       
   331     return iNaviPaneText;
       
   332 }
       
   333 
       
   334 //=============================================================================
       
   335 void CImageEditorFrameControl::Draw (const TRect & aRect) const
       
   336 {
       
   337     CPreviewControlBase::DrawPreviewImage (aRect);
       
   338 }
       
   339 
       
   340 //=============================================================================
       
   341 void CImageEditorFrameControl::NaviRightL()
       
   342 {
       
   343     LOG(KFramePluginLogFile, "CImageEditorFrameControl::NaviRightL()");
       
   344 
       
   345 	// Switch to the next frame bitmap
       
   346     iCurrentFrameIndex++;  
       
   347     if (iCurrentFrameIndex > iFrameCount - 1 )
       
   348     {
       
   349         // loop back to first
       
   350         iCurrentFrameIndex = 0;
       
   351     }
       
   352     SetNaviPaneTextL();
       
   353     SelectFrameL();
       
   354 }
       
   355 
       
   356 //=============================================================================
       
   357 void CImageEditorFrameControl::NaviLeftL()
       
   358 {
       
   359     LOG(KFramePluginLogFile, "CImageEditorFrameControl::NaviLeftL()");
       
   360 
       
   361 	// Switch to the previous frame bitmap
       
   362     iCurrentFrameIndex--;
       
   363     if ( iCurrentFrameIndex < 0 )
       
   364     {
       
   365         // loop to last
       
   366         iCurrentFrameIndex = iFrameCount - 1;
       
   367     }
       
   368     SetNaviPaneTextL();
       
   369     SelectFrameL();
       
   370 }
       
   371 
       
   372 //=============================================================================
       
   373 void CImageEditorFrameControl::FindFramesL()
       
   374 {
       
   375     LOG(KFramePluginLogFile, "CImageEditorFrameControl::FindFrames()");
       
   376 
       
   377     //	Read resource
       
   378 	TFileName resourcefile;
       
   379 	resourcefile.Append(KPgnResourcePath);
       
   380 	resourcefile.Append(KPgnResourceFile);
       
   381 
       
   382     User::LeaveIfError( CompleteWithAppPath( resourcefile ) );
       
   383 
       
   384     RConeResourceLoader resLoader( *ControlEnv() );
       
   385     resLoader.OpenL ( resourcefile );
       
   386     CleanupClosePushL(resLoader);
       
   387 
       
   388     CDesCArrayFlat* array = ControlEnv()->ReadDesCArrayResourceL(R_FRAME_DIRS);
       
   389     CleanupStack::PushL(array);
       
   390 
       
   391     TFileName pathList;
       
   392     _LIT(KPathSeparator, ";");
       
   393 
       
   394     for (TInt k = 0; k < array->Count(); ++k)
       
   395         {
       
   396         TPtrC path = (*array)[k];
       
   397         LOGFMT(KFramePluginLogFile, "\tSearch path: %S", &path);
       
   398         pathList.Append(path);
       
   399         pathList.Append(KPathSeparator);
       
   400         }
       
   401 
       
   402 	//  Create a file finder
       
   403     TFindFile fileFinder (ControlEnv()->FsSession()); 
       
   404     CDir * fileList = 0; 
       
   405 
       
   406     //  Find files by wild card and directory
       
   407     TInt err = fileFinder.FindWildByPath (
       
   408 		KFrameWild, 
       
   409 		&pathList, 
       
   410 		fileList
       
   411 		);
       
   412 
       
   413     User::LeaveIfError(err);
       
   414 
       
   415     CleanupStack::PopAndDestroy(2); // resLoader, array
       
   416 
       
   417 	//	Go through all drives
       
   418     while (err == KErrNone)
       
   419     {
       
   420         CleanupStack::PushL (fileList);
       
   421 
       
   422 		//	Check all plug-in candidates
       
   423         for (TInt i = 0; i < fileList->Count(); ++i) 
       
   424         {
       
   425 
       
   426             //	Create a full file name for a frame file
       
   427             TParse fullentry;
       
   428             TPtrC name = (*fileList)[i].iName;
       
   429             const TDesC* related = &(fileFinder.File());
       
   430             fullentry.Set ( name, related, 0 );
       
   431             TPtrC fullname (fullentry.FullName());
       
   432 
       
   433             //  Store frame file names
       
   434             iFrameFileArray->AppendL (fullname);
       
   435 
       
   436             //  Check the amount of frames 
       
   437             TInt err_bmp = KErrNone;
       
   438             TInt err_bmp_mask = KErrNone;
       
   439             TInt j = 0;
       
   440             while (KErrNone == err_bmp && KErrNone == err_bmp_mask)
       
   441             {
       
   442                 CFbsBitmap * tmp = new (ELeave) CFbsBitmap;
       
   443                 CleanupStack::PushL (tmp);
       
   444                 err_bmp = tmp->Load (fullname, j);
       
   445                 CleanupStack::PopAndDestroy(); // tmp
       
   446 
       
   447                 tmp = new (ELeave) CFbsBitmap;
       
   448                 CleanupStack::PushL (tmp);
       
   449                 err_bmp_mask = tmp->Load (fullname, j + 1);
       
   450                 CleanupStack::PopAndDestroy(); // tmp
       
   451 
       
   452                 if (KErrNone == err_bmp && KErrNone == err_bmp_mask)
       
   453                 {
       
   454                     ++iFrameCount;
       
   455                 }
       
   456                 j += 2;
       
   457             }
       
   458             User::LeaveIfError( iMultiplicities.Append (j - 1) );
       
   459         }
       
   460     
       
   461 		CleanupStack::PopAndDestroy(); /// fileList
       
   462 
       
   463 		//	Try once again
       
   464         err = fileFinder.FindWild (fileList);
       
   465 
       
   466     }
       
   467 
       
   468 }
       
   469 
       
   470 //=============================================================================
       
   471 void CImageEditorFrameControl::SelectFrameL ()
       
   472 {
       
   473     LOG(KFramePluginLogFile, "CImageEditorFrameControl::SelectFrameL ()");
       
   474 
       
   475     //  Select frame
       
   476     TInt    file = 0;
       
   477     TInt    frame = 0;
       
   478     TInt    index = 0;
       
   479     TBool   bContinue = ETrue;
       
   480     for (TInt i = 0 ; (i < iFrameFileArray->Count()) && bContinue; ++i)
       
   481     {
       
   482         for (TInt j = 0 ; j < iMultiplicities[i]; j += 2)
       
   483         {
       
   484             if (index == iCurrentFrameIndex)
       
   485             {
       
   486                 file = i;
       
   487                 frame = j;
       
   488                 bContinue = EFalse;
       
   489                 break;
       
   490             }
       
   491             ++index;
       
   492         }
       
   493     }
       
   494 
       
   495 	iParameter.Copy ( _L("file \""));
       
   496 	iParameter.Append ( (*iFrameFileArray)[file] );
       
   497 	iParameter.Append ( _L("\" frame ") );
       
   498 	iParameter.AppendNum ( frame );
       
   499 	iParameter.Append ( _L(" mask ") );
       
   500 	iParameter.AppendNum ( frame + 1 );
       
   501 	iParameter.Append ( _L(" load"));
       
   502 
       
   503 	iEditorView->HandleCommandL (EImageEditorCmdRender);
       
   504    
       
   505 }
       
   506 
       
   507 //=============================================================================
       
   508 void CImageEditorFrameControl::SetNaviPaneTextL()
       
   509 {
       
   510     // Update the navi pane text with the frame index and count
       
   511     // (make sure this is not called before the view exists)
       
   512     if ( iEditorView && iItem )
       
   513     {
       
   514         // .loc file parameter format strings
       
   515         _LIT(KParameter1, "%0N");
       
   516         _LIT(KParameter2, "%1N");
       
   517 
       
   518         // Getting string from resources
       
   519         TPtrC ptr = iItem->Parameters()[KCurrentFrameIndex]; 
       
   520 
       
   521         // Finding format patterns
       
   522         TInt pos1 = ptr.Find(KParameter1);
       
   523         TInt pos2 = ptr.Find(KParameter2);
       
   524         
       
   525         if(pos1 == KErrNotFound || pos2 == KErrNotFound)
       
   526         {
       
   527             User::Leave(KSIEEInternal);
       
   528         }
       
   529 
       
   530         // Clear navi pane 
       
   531         iNaviPaneText.Zero();
       
   532 
       
   533         // Add new text and replace format strings with current frame
       
   534         // index and frame count
       
   535 	    iNaviPaneText.Append(ptr.Left(pos1));
       
   536         iNaviPaneText.AppendNum (  iCurrentFrameIndex + 1 );
       
   537         iNaviPaneText.Append (ptr.Mid(pos1 + 3, pos2 - (pos1 + 3)));
       
   538         iNaviPaneText.AppendNum ( iFrameCount );
       
   539 	    iNaviPaneText.Append (ptr.Mid(pos2 + 3));
       
   540         
       
   541         AknTextUtils::LanguageSpecificNumberConversion  (  iNaviPaneText );
       
   542         
       
   543         iEditorView->HandleCommandL (EImageEditorUpdateNavipane);
       
   544     }
       
   545 }
       
   546 
       
   547 //=============================================================================
       
   548 void CImageEditorFrameControl::HandlePointerEventL(
       
   549                                         const TPointerEvent &aPointerEvent )
       
   550     {        
       
   551     if( AknLayoutUtils::PenEnabled() )
       
   552 		{	
       
   553 			
       
   554 		switch( aPointerEvent.iType )
       
   555 			{
       
   556 			case TPointerEvent::EButton1Down:
       
   557 				{
       
   558                 // Initialize change values
       
   559                 iPreviousChange = ENoDirection;
       
   560 			    iOneDirectionalChange = ETrue;
       
   561 		        // Store positions
       
   562 		        iPointerPosition = aPointerEvent.iPosition;
       
   563                 iInitialPointerPosition = iPointerPosition;
       
   564                 
       
   565 #ifdef RD_TACTILE_FEEDBACK
       
   566 				if ( iTouchFeedBack )
       
   567 					{
       
   568 					iTouchFeedBack->InstantFeedback( ETouchFeedbackBasic );
       
   569 					RDebug::Printf( "ImageEditor::ImageEditorFrameControl: ETouchFeedback" );
       
   570 					}
       
   571 #endif /* RD_TACTILE_FEEDBACK  */
       
   572                 
       
   573 				break;
       
   574 				}
       
   575 			case TPointerEvent::EDrag:
       
   576 				{
       
   577 				// Moving direction is from right to left
       
   578 				// KDirectionChangeThreshold is here instead of 0, because 
       
   579 				// touch screen tends to give drag events though finger is
       
   580 				// kept still on the screen.
       
   581 				if( ( iPointerPosition.iX - aPointerEvent.iPosition.iX ) > 
       
   582 				                                KDirectionChangeThreshold )
       
   583 				    {
       
   584 				    if( iPreviousChange == ENextFrame )
       
   585 				        {
       
   586 				        iOneDirectionalChange = EFalse;
       
   587 				        }
       
   588 				    iPreviousChange = EPreviousFrame;
       
   589 				    }				   
       
   590 				// Moving direction is from left to right    
       
   591 				else if(( aPointerEvent.iPosition.iX - iPointerPosition.iX ) > 
       
   592 				                                KDirectionChangeThreshold )
       
   593 				    {
       
   594 				    if (iPreviousChange == EPreviousFrame)
       
   595 				        {
       
   596 				        iOneDirectionalChange = EFalse;
       
   597 				        }
       
   598 				    iPreviousChange = ENextFrame;
       
   599 				    }
       
   600 				
       
   601 				// store current pen position    
       
   602 			    iPointerPosition = aPointerEvent.iPosition;
       
   603 				break;		
       
   604 				}
       
   605 			case TPointerEvent::EButton1Up:
       
   606 			    {			    
       
   607 			    iFinalPointerPosition = aPointerEvent.iPosition;			    
       
   608 			    // pen has been moved only one direction (x-wise) 
       
   609 			    // after button down event
       
   610 			    if( iOneDirectionalChange )
       
   611 			        {
       
   612 			        TInt xChange = iFinalPointerPosition.iX - 
       
   613 			                       iInitialPointerPosition.iX;
       
   614                     TInt yChange = iFinalPointerPosition.iY - 
       
   615 			                       iInitialPointerPosition.iY;
       
   616 			                       
       
   617 			        // check if threshold value has been exceeded and that
       
   618 			        // x directional change is bigger than y directional
       
   619 			        if (  xChange > KFrameChangeThreshold  &&
       
   620 			              Abs( xChange ) > Abs ( yChange  ) )
       
   621     			        {
       
   622     			        NaviLeftL();
       
   623     			        }
       
   624 			        else if( xChange < ( -KFrameChangeThreshold ) &&
       
   625 			                 Abs( xChange ) > Abs( yChange ) )
       
   626     			        {
       
   627     			        NaviRightL();
       
   628     			        }
       
   629 			        }
       
   630 			        			    			        
       
   631 			    break;
       
   632 			    }
       
   633 			}
       
   634 		}
       
   635     }
       
   636 // End of File