imageeditor/plugins/CropPlugin/src/ImageEditorCropPlugin.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 #include <eikenv.h>
       
    22 #include <aknenv.h>
       
    23 #include <coneresloader.h>
       
    24 #include <barsread.h>
       
    25 #include <bautils.h>
       
    26 
       
    27 #include <aknutils.h>
       
    28 
       
    29 #include <crop.rsg>
       
    30 #include "ImageEditorCropPlugin.h"
       
    31 #include "ImageEditorCropControl.h"
       
    32 #include "ImageEditorPluginBaseDefs.h"
       
    33 #include "DrawUtils.h"
       
    34 #include "ResolutionUtil.h"
       
    35 #include "definitions.def"
       
    36 
       
    37 //#include "platform_security_literals.hrh"
       
    38 
       
    39 
       
    40 //	CONSTANTS
       
    41 _LIT (KPgnResourceFile, "crop.rsc");
       
    42 
       
    43 const TInt KManualCropQueryIndex = 3;
       
    44 
       
    45 
       
    46 //=============================================================================
       
    47 EXPORT_C CImageEditorPluginBase * CreateImageEditorPlugin ()
       
    48 {
       
    49 	CImageEditorCropPlugin * plugin = new (ELeave) CImageEditorCropPlugin;
       
    50 	CleanupStack::PushL(plugin);
       
    51 	plugin->ConstructL();
       
    52 	CleanupStack::Pop(); // plugin
       
    53     return plugin;
       
    54 }
       
    55 
       
    56 //=============================================================================
       
    57 CImageEditorCropPlugin::CImageEditorCropPlugin ()
       
    58 {
       
    59 
       
    60 }
       
    61 
       
    62 //=============================================================================
       
    63 void CImageEditorCropPlugin::ConstructL ()
       
    64 {
       
    65     CImageEditorPluginBase::ConstructL (KPgnResourcePath, KPgnResourceFile);
       
    66 }
       
    67 
       
    68 //=============================================================================
       
    69 CImageEditorCropPlugin::~CImageEditorCropPlugin ()
       
    70 {
       
    71     iSysPars = NULL;
       
    72     ReleasePlugin();
       
    73 }
       
    74 
       
    75 //=============================================================================
       
    76 TInt CImageEditorCropPlugin::SetProperty (
       
    77 	TInt		aPropertyId, 
       
    78 	TDesC &		aPropertyValue
       
    79 	)
       
    80 {
       
    81 
       
    82     TLex	parser;
       
    83 
       
    84 	//	Copy data
       
    85 	switch (aPropertyId) 
       
    86 	{
       
    87 		case KCapSystemParameters:
       
    88 		{
       
    89 			parser.Assign (aPropertyValue);
       
    90             TInt tempval = 0;
       
    91             parser.Val (tempval);
       
    92             iSysPars = (const CSystemParameters *)tempval;
       
    93 			return KErrNone;
       
    94 		}
       
    95 		default:
       
    96 		{
       
    97             return CImageEditorPluginBase::SetProperty (aPropertyId, aPropertyValue);
       
    98 		}
       
    99 	}
       
   100 }
       
   101 
       
   102 //=============================================================================
       
   103 TInt CImageEditorCropPlugin::GetProperty (
       
   104 	TInt		aPropertyId, 
       
   105 	TDes &		aPropertyValue
       
   106 	)
       
   107 {
       
   108 	//	Clean buffer
       
   109 	aPropertyValue.Zero();
       
   110 
       
   111 	//	Copy data
       
   112 	switch (aPropertyId) 
       
   113 	{
       
   114 		case KCapParamStruct:
       
   115 		{
       
   116 			//	Control found
       
   117 			if (iControl)
       
   118 			{
       
   119 				aPropertyValue.Copy ( ((CImageEditorCropControl *)iControl)->GetParam() );
       
   120 				return KErrNone;
       
   121 			}
       
   122 
       
   123 			//	Control not found, return KErrNotReady
       
   124 			else
       
   125 			{
       
   126 				return KErrNotReady;
       
   127 			}
       
   128 		}
       
   129         case KCapReadyToRender:
       
   130 		{
       
   131             TBool readyToRender = EFalse;
       
   132             if (iControl)
       
   133                 {
       
   134                 readyToRender = ((CImageEditorCropControl *)iControl)->IsReadyToRender();
       
   135                 }
       
   136 			aPropertyValue.AppendNum ((TInt)readyToRender);
       
   137 			return KErrNone;
       
   138 		}
       
   139 		default:
       
   140 		{
       
   141 			return CImageEditorPluginBase::GetProperty (aPropertyId, aPropertyValue);
       
   142 		}
       
   143 	}
       
   144 }
       
   145 
       
   146 //=============================================================================
       
   147 TInt CImageEditorCropPlugin::InitPluginL (
       
   148 	const TRect &		aRect,
       
   149 	CCoeControl *		aParent,
       
   150 	CCoeControl *&		aPluginControl
       
   151 	)
       
   152 {
       
   153     //  Delete previous control
       
   154 	ReleasePlugin();
       
   155 
       
   156 	//	Query dialog texts
       
   157 	CDesCArray * queries = new (ELeave) CDesCArraySeg (8);
       
   158 	CleanupStack::PushL (queries);
       
   159 
       
   160     //  Aspect ratio definition array
       
   161     RArray<TInt> aspect_ratios;
       
   162 
       
   163 	//	Get pointer to the parameter descriptor array
       
   164     TBuf<256>  readbuf;  
       
   165 	TLex       parser;
       
   166 	User::LeaveIfError ( CImageEditorPluginBase::GetProperty (KCapPluginParamNames, readbuf) );
       
   167 	parser.Assign (readbuf);
       
   168 	TInt tempval = 0;
       
   169 	parser.Val ( tempval );
       
   170 	CDesCArray * pars = (CDesCArraySeg *)tempval;
       
   171     
       
   172     //  Append manual crop query
       
   173 	queries->AppendL( (*pars)[KManualCropQueryIndex] );
       
   174    
       
   175     ReadWallPaperCropDefinitionsL(*queries, aspect_ratios);
       
   176 
       
   177 #ifdef __SHOW_OPTIONAL_ASPECT_RATIOS__    
       
   178     ReadCropDefinitionsL (*queries, aspect_ratios);
       
   179 #endif
       
   180 
       
   181     //  Create new control
       
   182     iControl = CImageEditorCropControl::NewL (aRect, aParent);
       
   183 
       
   184     //  Set system parameters
       
   185     ((CImageEditorCropControl*)iControl)->SetSystemParameters (iSysPars);
       
   186 
       
   187 	//	Show selection dialog
       
   188     TPtrC query = (*pars)[0];
       
   189     TInt index = SDrawUtils::LaunchListQueryDialogL (queries, query);
       
   190 
       
   191     //  If canceled, return with NULL pointer and error value
       
   192     TInt ret = KErrNone;
       
   193     if (index <= -1)
       
   194     {
       
   195     	// Relasing created plugin if canceled
       
   196     	ReleasePlugin();
       
   197         ret = KErrNotSupported;
       
   198     }
       
   199     else
       
   200     {
       
   201         //  Compute and set crop mode and aspect ratio
       
   202         float ar = 1.0F;
       
   203         if (index != 0)
       
   204         {
       
   205             TInt index2 = (index - 1) * 2;
       
   206             ar = (float)aspect_ratios[index2] / (float)aspect_ratios[index2 + 1];
       
   207         }
       
   208 
       
   209         ((CImageEditorCropControl*)iControl)->SetCropModeL (index, ar);
       
   210     }
       
   211 
       
   212     if (queries)
       
   213     {
       
   214         for (TInt i = 0; i < queries->Count(); ++i)
       
   215         {
       
   216             queries->Delete(i);
       
   217         }
       
   218         queries->Reset();
       
   219     }
       
   220     aspect_ratios.Reset();
       
   221     CleanupStack::PopAndDestroy(); // queries
       
   222     aPluginControl = iControl;
       
   223     return ret;
       
   224 }
       
   225 
       
   226 //=============================================================================
       
   227 void CImageEditorCropPlugin::ProcessImageL (CEditorImage * /*aImage*/ )
       
   228 {
       
   229 
       
   230 }
       
   231 
       
   232 //=============================================================================
       
   233 void CImageEditorCropPlugin::ReleasePlugin ()
       
   234 {
       
   235 	delete iControl;
       
   236 	iControl = 0;
       
   237 }
       
   238 
       
   239 //=============================================================================
       
   240 void CImageEditorCropPlugin::ReadWallPaperCropDefinitionsL (
       
   241     CDesCArray &  aCropTexts,
       
   242     RArray<TInt> &  aCropRatios
       
   243     ) const
       
   244 {
       
   245     // find out screen resolution
       
   246     TRect screenRect;
       
   247     CResolutionUtil::Self()->GetScreenRect(screenRect);
       
   248     TInt screenWidth = screenRect.Width();
       
   249     TInt screenHeight = screenRect.Height();
       
   250      
       
   251     //	Read resource
       
   252 	TFileName resourcefile;
       
   253 	resourcefile.Append(KPgnResourcePath);
       
   254 	resourcefile.Append(KPgnResourceFile);
       
   255     User::LeaveIfError( CompleteWithAppPath( resourcefile ) );
       
   256 
       
   257     //  Read aspect ratio definitions from resource file
       
   258     //  (RConeResourceLoader selects the language using BaflUtils::NearestLanguageFile)
       
   259     CEikonEnv *env = CEikonEnv::Static();
       
   260     RConeResourceLoader resLoader ( *env );
       
   261     CleanupClosePushL ( resLoader );
       
   262 	resLoader.OpenL ( resourcefile );
       
   263     
       
   264     // To find out if in landscape or in portrait mode    
       
   265     TInt shortSize;
       
   266     TInt longSize;    
       
   267     if ( screenHeight > screenWidth )
       
   268         {
       
   269         shortSize = screenWidth;
       
   270         longSize = screenHeight;
       
   271         }
       
   272     else
       
   273         {
       
   274         longSize = screenWidth;
       
   275         shortSize = screenHeight;
       
   276         }
       
   277         
       
   278 #ifndef __SHOW_TWO_WALLPAPER_ASPECT_RATIOS__
       
   279 
       
   280     // If only this one wallpaper option is shown crop ratio is set as portrait
       
   281     HBufC * wallpaperText = 
       
   282                     env->AllocReadResourceL( R_CROP_ASPECT_RATIO_WALLPAPER );
       
   283     CleanupStack::PushL( wallpaperText );
       
   284     aCropTexts.AppendL( wallpaperText->Des() );
       
   285     CleanupStack::PopAndDestroy();
       
   286     aCropRatios.Append( shortSize );
       
   287     aCropRatios.Append( longSize );
       
   288 
       
   289 #else        
       
   290         
       
   291     HBufC * wallpaperText2 = 
       
   292                     env->AllocReadResourceL( R_CROP_ASPECT_RATIO_WALLPAPER_PRT );
       
   293     CleanupStack::PushL( wallpaperText2 );
       
   294     aCropTexts.AppendL( wallpaperText2->Des() );
       
   295     CleanupStack::PopAndDestroy();    
       
   296     aCropRatios.Append( shortSize );
       
   297     aCropRatios.Append( longSize );
       
   298     
       
   299     
       
   300     HBufC * wallpaperText3 = 
       
   301                     env->AllocReadResourceL( R_CROP_ASPECT_RATIO_WALLPAPER_LNDS );
       
   302     CleanupStack::PushL( wallpaperText3 );
       
   303     aCropTexts.AppendL( wallpaperText3->Des() );
       
   304     CleanupStack::PopAndDestroy();    
       
   305     aCropRatios.Append( longSize );
       
   306     aCropRatios.Append( shortSize );
       
   307 
       
   308 #endif
       
   309    
       
   310     CleanupStack::PopAndDestroy(1); //resLoader
       
   311 }
       
   312 
       
   313 
       
   314 //=============================================================================
       
   315 void CImageEditorCropPlugin::ReadCropDefinitionsL (
       
   316     CDesCArray &  aCropTexts,
       
   317     RArray<TInt> &  aCropRatios
       
   318     ) const
       
   319 {
       
   320 
       
   321     //	Read resource
       
   322 	TFileName resourcefile;
       
   323 	resourcefile.Append(KPgnResourcePath);
       
   324 	resourcefile.Append(KPgnResourceFile);
       
   325     User::LeaveIfError( CompleteWithAppPath( resourcefile ) );
       
   326 
       
   327     //  Read aspect ratio definitions from resource file
       
   328     //  (RConeResourceLoader selects the language using BaflUtils::NearestLanguageFile)
       
   329     CEikonEnv *env = CEikonEnv::Static();
       
   330     RConeResourceLoader resLoader ( *env );
       
   331     CleanupClosePushL ( resLoader );
       
   332 	resLoader.OpenL ( resourcefile );
       
   333 
       
   334     HBufC8 * res8 = env->AllocReadResourceAsDes8LC (R_CROP_MODES);    
       
   335     TResourceReader resource;
       
   336     resource.SetBuffer (res8);
       
   337     
       
   338     TInt16 arraycount = (TInt16)resource.ReadInt16();
       
   339 	if (arraycount > 0)
       
   340 	{
       
   341 		//	Read extra parameters to array
       
   342 		for (TInt i = 0; i < arraycount; ++i)
       
   343 		{
       
   344             //  Read the crop ratio text
       
   345             HBufC * crop_text = resource.ReadHBufCL();
       
   346             CleanupStack::PushL(crop_text);
       
   347             aCropTexts.AppendL ( crop_text->Des() );
       
   348             CleanupStack::PopAndDestroy();
       
   349 
       
   350             //  Add width
       
   351             aCropRatios.Append ( resource.ReadInt32() );
       
   352             
       
   353             //  Add height
       
   354             aCropRatios.Append ( resource.ReadInt32() );
       
   355 		}
       
   356     }
       
   357     
       
   358     CleanupStack::PopAndDestroy(2); // res8, resLoader
       
   359 }
       
   360 
       
   361 // End of File