00001
00002
00003
00004
00005
00006
00007 #include "ImageConverterApp.h"
00008 #include "ImageConverterAppUi.h"
00009 #include "ImageConverterContainer.h"
00010 #include "ImageConverterEngine.h"
00011 #include "imageconverter.hrh"
00012 #include "encoderselectiondialog.h"
00013 #include "showinfodialog.h"
00014 #include <ImageConverter.rsg>
00015
00016
00017 #include <aknmessagequerydialog.h>
00018 #include <hlplch.h>
00019 #include <avkon.hrh>
00020 #include <avkon.rsg>
00021 #include <caknfileselectiondialog.h>
00022 #include <akncommondialogs.h>
00023 #include <imageconversion.h>
00024 #include <eikmenup.h>
00025 #include <aknglobalnote.h>
00026 #include <pathinfo.h>
00027 #include <aknquerydialog.h>
00028
00029 const TInt KScaleUpFactor = 150;
00030 const TInt KScaleDownFactor = 50;
00031 const TInt KMaxInfoMsgLength = 50;
00032
00033 const TInt KMaxInfoDescriptorLength = 80;
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 void CleanupRArray( TAny* object )
00045 {
00046 ((RImageTypeDescriptionArray*)object)->ResetAndDestroy();
00047 }
00048
00049
00050
00051
00052
00053
00054
00055
00056 void CImageConverterAppUi::ConstructL()
00057 {
00058 #ifdef __TUI_SUPPORTED__
00059 BaseConstructL(EAknEnableSkin | EAknTouchCompatible);
00060 #else
00061 BaseConstructL(EAknEnableSkin);
00062 #endif
00063
00064
00065 SearchOptionsButtonPosition();
00066
00067 iImageLoaded = EFalse;
00068 iConverter = CImageConverterEngine::NewL( this );
00069
00070 iAppContainer = new (ELeave) CImageConverterContainer;
00071 iAppContainer->SetMopParent( this );
00072 iAppContainer->ConstructL( ClientRect() );
00073 AddToStackL( iAppContainer );
00074
00075
00076 ReadImageDirectoryL();
00077 ReadImage(0);
00078 }
00079
00080
00081
00082
00083
00084
00085
00086 CImageConverterAppUi::~CImageConverterAppUi()
00087 {
00088 if (iAppContainer)
00089 {
00090 RemoveFromStack( iAppContainer );
00091 delete iAppContainer;
00092 }
00093
00094 if (iConverter)
00095 {
00096 iConverter->Cancel();
00097 delete iConverter;
00098 }
00099
00100 delete iSaveAs;
00101 delete iInfoDialog;
00102 delete iInfoStrings;
00103
00104 iFiles.Close();
00105 }
00106
00107
00108
00109
00110
00111
00112
00113 void CImageConverterAppUi::DynInitMenuPaneL(
00114 TInt aResourceId,CEikMenuPane* aMenuPane)
00115 {
00116 if( aResourceId == R_IMAGECONVERTER_MENU )
00117 {
00118 if( !iImageLoaded )
00119 {
00120
00121 aMenuPane->SetItemDimmed( EImageConverterCmdSaveAs, true );
00122 aMenuPane->SetItemDimmed( EImageConverterCmdInfo, true );
00123 aMenuPane->SetItemDimmed( EImageConverterCmdRotate, true );
00124 aMenuPane->SetItemDimmed( EImageConverterCmdZoomOut, true );
00125 aMenuPane->SetItemDimmed( EImageConverterCmdZoomIn, true );
00126 }
00127 else
00128 {
00129 aMenuPane->SetItemDimmed( EImageConverterCmdSaveAs, false );
00130 aMenuPane->SetItemDimmed( EImageConverterCmdInfo, false );
00131 aMenuPane->SetItemDimmed( EImageConverterCmdRotate, false );
00132 aMenuPane->SetItemDimmed( EImageConverterCmdZoomOut, false );
00133 if (iConverter && iConverter->CanDownScaleMore())
00134 {
00135 aMenuPane->SetItemDimmed( EImageConverterCmdZoomIn, false );
00136 }
00137 else
00138 {
00139 aMenuPane->SetItemDimmed( EImageConverterCmdZoomIn, true );
00140 }
00141 }
00142 }
00143 }
00144
00145 void CImageConverterAppUi::DoEvent(EPointerEvents aEvent)
00146 {
00147 switch (aEvent)
00148 {
00149 case EZoomIn:
00150 {
00151 iAppContainer->SetTitleText(ETitleZoomIn,EFalse);
00152 if (iConverter->Scale( KScaleUpFactor )==KErrNone)
00153 {
00154 iImageLoaded = false;
00155 }
00156 break;
00157 }
00158 case EZoomOut:
00159 {
00160 iAppContainer->ResetTitleText(EFalse);
00161 if (iConverter->Scale( KScaleDownFactor )==KErrNone)
00162 {
00163 iImageLoaded = false;
00164 }
00165 break;
00166 }
00167 case ENextImage:
00168 {
00169 ReadImage(1);
00170 break;
00171 }
00172 case EPrevImage:
00173 {
00174 ReadImage(-1);
00175 break;
00176 }
00177 case ERotateRight:
00178 {
00179 iImageLoaded = false;
00180 iAppContainer->SetTitleText(ETitleRotateRight,EFalse);
00181 iConverter->Rotate( CImageConverterEngine::EClockwise90 );
00182 break;
00183 }
00184 case ERotateLeft:
00185 {
00186 iImageLoaded = false;
00187 iAppContainer->SetTitleText(ETitleRotateLeft,EFalse);
00188 iConverter->Rotate( CImageConverterEngine::EAntiClockwise90 );
00189 break;
00190 }
00191 default:
00192 {
00193 break;
00194 }
00195 };
00196 }
00197
00198 TState CImageConverterAppUi::EngineState()
00199 {
00200 return iConverter->EngineState();
00201 }
00202
00203
00204
00205
00206
00207
00208
00209 TKeyResponse CImageConverterAppUi::HandleKeyEventL(
00210 const TKeyEvent& aKeyEvent,TEventCode aType)
00211 {
00212
00213
00214 if( iImageLoaded && (aType == EEventKey) )
00215 {
00216 if( aKeyEvent.iCode == EKeyLeftArrow )
00217 {
00218 iAppContainer->ChangePrevPictureL();
00219 return EKeyWasConsumed;
00220 }
00221 else if( aKeyEvent.iCode == EKeyRightArrow )
00222 {
00223 iAppContainer->ChangeNextPictureL();
00224 return EKeyWasConsumed;
00225 }
00226 else if( aKeyEvent.iCode == EKeyUpArrow )
00227 {
00228 iAppContainer->SetTitleText(ETitleZoomIn,ETrue);
00229 if (iConverter->Scale( KScaleUpFactor )==KErrNone)
00230 {
00231 iImageLoaded = false;
00232 }
00233 return EKeyWasConsumed;
00234 }
00235 else if( aKeyEvent.iCode == EKeyDownArrow )
00236 {
00237 iAppContainer->ResetTitleText(ETrue);
00238 if (iConverter->Scale( KScaleDownFactor )==KErrNone)
00239 {
00240 iImageLoaded = false;
00241 }
00242 return EKeyWasConsumed;
00243 }
00244 }
00245 else if ( iImageLoaded && aType == EEventKeyDown)
00246 {
00247 if (aKeyEvent.iScanCode == '1' || aKeyEvent.iScanCode == '*')
00248 {
00249 iAppContainer->SetTitleText(ETitleRotateLeft,ETrue);
00250 iImageLoaded = false;
00251 iConverter->Rotate( CImageConverterEngine::EAntiClockwise90 );
00252 return EKeyWasConsumed;
00253 }
00254 else if (aKeyEvent.iScanCode == '3' || aKeyEvent.iScanCode == EStdKeyHash)
00255 {
00256 iAppContainer->SetTitleText(ETitleRotateRight,ETrue);
00257 iImageLoaded = false;
00258 iConverter->Rotate( CImageConverterEngine::EClockwise90 );
00259 return EKeyWasConsumed;
00260 }
00261 }
00262 return EKeyWasNotConsumed;
00263 }
00264
00265
00266
00267
00268
00269
00270 void CImageConverterAppUi::HandleCommandL(TInt aCommand)
00271 {
00272 switch ( aCommand )
00273 {
00274 case EAknSoftkeyExit:
00275 case EEikCmdExit:
00276 {
00277 Exit();
00278 break;
00279 }
00280 case EImageConverterCmdOpen:
00281 {
00282 HandleOpenL();
00283 break;
00284 }
00285 case EImageConverterCmdSaveAs:
00286 {
00287 HandleSaveAsL();
00288 break;
00289 }
00290 case EImageConverterCmdInfo:
00291 {
00292 HandleInfoL();
00293 break;
00294 }
00295 case EImageConverterCmdOrientation:
00296 {
00297 if(Orientation() == CAknAppUiBase::EAppUiOrientationLandscape)
00298 {
00299 SetOrientationL(CAknAppUiBase::EAppUiOrientationPortrait);
00300 iAppContainer->SetPictureScreenCenter(ETrue);
00301 }
00302 else
00303 {
00304 SetOrientationL(CAknAppUiBase::EAppUiOrientationLandscape);
00305 iAppContainer->SetPictureScreenCenter(ETrue);
00306 }
00307 break;
00308 }
00309 case EImageConverterCmdRotateLeft:
00310 {
00311 iAppContainer->SetTitleText(ETitleRotateLeft,ETrue);
00312 iImageLoaded = false;
00313 iConverter->Rotate( CImageConverterEngine::EAntiClockwise90 );
00314 break;
00315 }
00316 case EImageConverterCmdRotateRight:
00317 {
00318 iAppContainer->SetTitleText(ETitleRotateRight,ETrue);
00319 iImageLoaded = false;
00320 iConverter->Rotate( CImageConverterEngine::EClockwise90 );
00321 break;
00322 }
00323 case EImageConverterCmdZoomIn:
00324 {
00325 iAppContainer->SetTitleText(ETitleZoomIn,ETrue);
00326 if (iConverter->Scale( KScaleUpFactor )==KErrNone)
00327 {
00328 iImageLoaded = false;
00329 }
00330 break;
00331 }
00332 case EImageConverterCmdZoomOut:
00333 {
00334 iAppContainer->ResetTitleText(ETrue);
00335 if (iConverter->Scale( KScaleDownFactor )==KErrNone)
00336 {
00337 iImageLoaded = false;
00338 }
00339 break;
00340 }
00341 case EImageConverterCmdNext:
00342 {
00343 iAppContainer->ChangeNextPictureL();
00344 break;
00345 }
00346 case EImageConverterCmdPrev:
00347 {
00348 iAppContainer->ChangePrevPictureL();
00349 break;
00350 }
00351 case EImageConverterCmdHelp:
00352 {
00353 CArrayFix<TCoeHelpContext>* buf = CCoeAppUi::AppHelpContextL ( );
00354 HlpLauncher::LaunchHelpApplicationL (iEikonEnv->WsSession ( ), buf );
00355 break;
00356 }
00357 case EImageConverterCmdAbout:
00358 {
00359 CAknMessageQueryDialog* dlg = new (ELeave)CAknMessageQueryDialog();
00360 dlg->PrepareLC (R_ABOUT_QUERY_DIALOG );
00361 HBufC* title = iEikonEnv->AllocReadResourceLC (R_ABOUT_DIALOG_TITLE );
00362 dlg->QueryHeading()->SetTextL (*title );
00363 CleanupStack::PopAndDestroy ( );
00364 HBufC* msg = iEikonEnv->AllocReadResourceLC (R_ABOUT_DIALOG_TEXT );
00365 dlg->SetMessageTextL (*msg );
00366 CleanupStack::PopAndDestroy ( );
00367 dlg->RunLD ( );
00368 break;
00369 }
00370 default:
00371 break;
00372 }
00373 }
00374
00375
00376 CArrayFix<TCoeHelpContext>* CImageConverterAppUi::HelpContextL ( ) const
00377 {
00378 CArrayFixFlat<TCoeHelpContext>* array = new(ELeave)CArrayFixFlat<TCoeHelpContext>(1);
00379 CleanupStack::PushL (array );
00380 _LIT(KGeneral_Information, "General Information about the application");
00381 array->AppendL (TCoeHelpContext (KUidHelpFile, KGeneral_Information ) );
00382 CleanupStack::Pop (array );
00383 return array;
00384 }
00385
00386
00387 void CImageConverterAppUi::ReadImageDirectoryL()
00388 {
00389 RFs& rfs = iEikonEnv->FsSession();
00390 CDir* fileList = NULL;
00391
00392 iFiles.Reset();
00393
00394 TFileName path = PathInfo::PhoneMemoryRootPath();
00395 path.Append(PathInfo::ImagesPath());
00396
00397 User::LeaveIfError(rfs.GetDir(path,KEntryAttNormal,ESortByName,fileList));
00398
00399 for( TInt i = 0; i < fileList->Count(); i++ )
00400 {
00401
00402 if (!fileList->operator[](i).IsDir())
00403 {
00404 TFileName filename;
00405 filename.Append(path);
00406 filename.Append(fileList->operator[](i).iName);
00407 filename.LowerCase();
00408
00409 if ( filename.Find(_L(".jpg"))!=KErrNotFound ||
00410 filename.Find(_L(".jpeg"))!=KErrNotFound ||
00411 filename.Find(_L(".bmp"))!=KErrNotFound ||
00412 filename.Find(_L(".gif"))!=KErrNotFound )
00413 {
00414 iFiles.Append(filename);
00415 }
00416 }
00417 }
00418
00419 delete fileList;
00420 }
00421
00422
00423
00424 TInt CImageConverterAppUi::ImageIndex()
00425 {
00426 return iOpenFileIndex;
00427 }
00428
00429 TInt CImageConverterAppUi::ImageCount()
00430 {
00431 return iFiles.Count();
00432 }
00433
00434 void CImageConverterAppUi::ImageName(TFileName& aFilename)
00435 {
00436 aFilename.Copy(iFiles[iOpenFileIndex]);
00437 if (aFilename.Find(_L("\\")) > -1)
00438 {
00439 aFilename.Delete(0,aFilename.LocateReverse('\\')+1);
00440 }
00441 }
00442
00443 void CImageConverterAppUi::PlainImageName(TFileName& aFilename)
00444 {
00445 aFilename.Copy(iFiles[iOpenFileIndex]);
00446 if (aFilename.Find(_L("\\")) > -1)
00447 {
00448 aFilename.Delete(0,aFilename.LocateReverse('\\')+1);
00449 }
00450 if (aFilename.Find(_L(".")) > -1)
00451 {
00452 aFilename.SetLength(aFilename.Find(_L(".")));
00453 }
00454 }
00455
00456 void CImageConverterAppUi::ReadImage(TInt aDirection)
00457 {
00458 TFileName fileToOpen;
00459
00460 if (iFiles.Count()==0)
00461 {
00462 return;
00463 }
00464
00465 iOpenFileIndex = iOpenFileIndex + aDirection;
00466 if (iOpenFileIndex < 0)
00467 {
00468 iOpenFileIndex = iFiles.Count()-1;
00469 }
00470 else if(iOpenFileIndex > iFiles.Count()-1)
00471 {
00472 iOpenFileIndex = 0;
00473 }
00474
00475 fileToOpen.Append(iFiles[iOpenFileIndex]);
00476
00477 iImageLoaded = false;
00478 iAppContainer->SetBitmap(NULL);
00479
00480
00481
00482
00483 TRAPD( err, iConverter->StartToDecodeL(fileToOpen) );
00484
00485
00486 if( err != KErrNone )
00487 {
00488 _LIT( KMsg, "Error when starting to decode:%d" );
00489 TBuf<KMaxInfoMsgLength> buf;
00490 buf.Format( KMsg, err );
00491 ShowMessage(buf);
00492 }
00493 }
00494
00495
00496
00497
00498
00499
00500
00501
00502 void CImageConverterAppUi::HandleOpenL( )
00503 {
00504 TFileName fileToOpen;
00505
00506 _LIT( KFileSelectionTitle, "Select image to open" );
00507
00508
00509 if( !AknCommonDialogs::RunSelectDlgLD(fileToOpen,
00510 R_MEMORY_SELECTION_DIALOG, KFileSelectionTitle) )
00511 {
00512 return;
00513 }
00514
00515 iImageLoaded = false;
00516 iAppContainer->SetBitmap(NULL);
00517
00518
00519
00520
00521 TRAPD( err, iConverter->StartToDecodeL(fileToOpen) );
00522
00523
00524 if( err != KErrNone )
00525 {
00526 _LIT( KMsg, "Error when starting to decode:%d" );
00527 TBuf<KMaxInfoMsgLength> buf;
00528 buf.Format( KMsg, err );
00529 ShowMessage(buf);
00530 }
00531 else
00532 {
00533 iFiles.Append(fileToOpen);
00534 iOpenFileIndex = iFiles.Count()-1;
00535 }
00536 }
00537
00538
00539
00540
00541
00542
00543 void CImageConverterAppUi::HandleSaveAsL()
00544 {
00545 TInt selectedIdx = 0;
00546
00547
00548 RImageTypeDescriptionArray imageTypes;
00549 iConverter->GetEncoderImageTypesL( imageTypes);
00550
00551
00552
00553
00554 CleanupStack::PushL( TCleanupItem(CleanupRArray, &imageTypes) );
00555
00556
00557 TFileName path = PathInfo::PhoneMemoryRootPath();
00558 path.Append(PathInfo::ImagesPath());
00559
00560 TFileName fileToSave;
00561 PlainImageName(fileToSave);
00562
00563
00564 CDesCArrayFlat* array = new (ELeave) CDesCArrayFlat(imageTypes.Count());
00565 CleanupStack::PushL(array);
00566 for( TInt i=0; i<imageTypes.Count(); i++ )
00567 {
00568 TBuf<KMaxInfoDescriptorLength> desc;
00569 desc.Append( imageTypes[i]->Description() );
00570 array->AppendL( desc );
00571 }
00572
00573
00574 TRAPD(err,
00575 iSaveAs = new(ELeave)CAknListQueryDialog(&selectedIdx);
00576 iSaveAs->PrepareLC(R_SELECTION_QUERY);
00577 iSaveAs->SetItemTextArray(array);
00578 iSaveAs->SetOwnershipType(ELbmDoesNotOwnItemArray);
00579 if(iSaveAs->RunLD())
00580 {
00581
00582 CAknTextQueryDialog* dlg = CAknTextQueryDialog::NewL(fileToSave,CAknQueryDialog::ENoTone);
00583 dlg->PrepareLC(R_QUERY_DIALOG);
00584 dlg->SetPromptL(_L("Save As"));
00585 if(dlg->RunLD())
00586 {
00587 fileToSave.Append(_L("."));
00588 fileToSave.Append(imageTypes[selectedIdx]->Description());
00589
00590 path.Append(fileToSave);
00591 iConverter->StartToEncodeL(path,
00592 imageTypes[selectedIdx]->ImageType(),
00593 imageTypes[selectedIdx]->SubType());
00594 }
00595 }
00596 );
00597 iSaveAs = NULL;
00598 CleanupStack::PopAndDestroy();
00599
00600 if (err)
00601 {
00602 User::Leave(err);
00603 }
00604
00605
00606 CleanupStack::PopAndDestroy(&imageTypes);
00607 }
00608
00609
00610
00611
00612
00613
00614 void CImageConverterAppUi::HandleInfoL()
00615 {
00616
00617 delete iInfoStrings;
00618 iInfoStrings = NULL;
00619 iInfoStrings = iConverter->GetImageInfoL();
00620
00621 if (iInfoStrings)
00622 {
00623
00624 iInfoDialog = new(ELeave) CShowInfoDialog( iInfoStrings );
00625 iInfoDialog->ExecuteLD( R_IMAGECONVERTER_SHOWINFO_DIALOG );
00626 iInfoDialog = NULL;
00627 }
00628 }
00629
00630 TBool CImageConverterAppUi::IsAnimating()
00631 {
00632 return iAppContainer->IsAnimating();
00633 }
00634
00635 TBool CImageConverterAppUi::IsEngineBusy()
00636 {
00637 return iConverter->IsActive();
00638 }
00639
00640
00641
00642
00643
00644
00645
00646 void CImageConverterAppUi::NotifyCompletion( TInt aErr, const TDesC& aMsg )
00647 {
00648 if( aErr == KErrNone )
00649 {
00650
00651
00652 iAppContainer->SetBitmap(iConverter->GetBitmap());
00653 iImageLoaded = true;
00654 }
00655 else
00656 {
00657 iAppContainer->SetBitmap(NULL);
00658 iImageLoaded = false;
00659 }
00660
00661 iAppContainer->DrawToBackBuffer();
00662 iAppContainer->DrawNow();
00663
00664 if (aErr && aErr != KErrNotReady)
00665 {
00666 ShowMessage(aMsg);
00667 }
00668 }
00669
00670
00671
00672
00673
00674
00675 void CImageConverterAppUi::ShowMessage(const TDesC& aMsg) const
00676 {
00677 if (aMsg.Length())
00678 {
00679
00680 TRAPD(err, ShowMessageL(aMsg));
00681 }
00682 }
00683
00684
00685
00686
00687
00688 void CImageConverterAppUi::ShowMessageL(const TDesC& aMsg) const
00689 {
00690 CAknGlobalNote* note = CAknGlobalNote::NewLC();
00691 note->ShowNoteL(EAknGlobalConfirmationNote, aMsg);
00692 CleanupStack::PopAndDestroy(note);
00693 }
00694
00695 TBool CImageConverterAppUi::IsOptionsButtonOnTop()
00696 {
00697 return iOptionButtonOnTop;
00698 }
00699
00700 void CImageConverterAppUi::SearchOptionsButtonPosition()
00701 {
00702 iOptionButtonOnTop = EFalse;
00703 CEikButtonGroupContainer* cba = Cba();
00704 if(!cba)
00705 {
00706 return;
00707 }
00708 CCoeControl* options = cba->ControlOrNull(EAknSoftkeyOptions);
00709 CCoeControl* exit = cba->ControlOrNull(EAknSoftkeyExit);
00710 if( options && exit )
00711 {
00712 if( options->Position().iY < exit->Position().iY )
00713 {
00714 iOptionButtonOnTop = ETrue;
00715 }
00716 }
00717 }
00718
00719 void CImageConverterAppUi::HandleResourceChangeL( TInt aType )
00720 {
00721 CAknAppUi::HandleResourceChangeL( aType );
00722
00723 if ( aType==KEikDynamicLayoutVariantSwitch )
00724 {
00725 SearchOptionsButtonPosition();
00726
00727 TRect rect;
00728 AknLayoutUtils::LayoutMetricsRect(
00729 AknLayoutUtils::EMainPane,rect);
00730 if (iSaveAs)
00731 {
00732 iSaveAs->SetRect(rect);
00733 }
00734 if (iInfoDialog)
00735 {
00736 iInfoDialog->SetRect(rect);
00737 }
00738 }
00739 }
00740
00741
00742
00743