examples/Multimedia/ImageConv/src/CImageAppUi.cpp

00001 // Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 // CImageAppUi
00015 //
00016 
00017 #include <eikenv.h>
00018 #include <eikfile.rsg>
00019 #include <eikmenub.h>
00020 #include <eiklabel.h>
00021 
00022 #include <imageconv.rsg>
00023 #include "ImageConv.hrh"
00024 
00025 #include "CImageAppUi.h"
00026 #include "CImageAppView.h"
00027 
00028 
00029 _LIT(KCancelled, "Cancelled");
00030 _LIT(KLoading,"Loading...");
00031 _LIT(KSaving,"Saving...");
00032 _LIT(KDrive,"\\");
00033 #if defined(__WINS__)
00034 _LIT(KSamplesDir,"\\samples\\");
00035 #endif
00036 
00037 #if defined(__EPOC32__)
00038 _LIT(KSamplesDir,"Z:\\samples\\");
00039 #endif
00040 
00041 void CImageAppUi::ConstructL()
00042     {
00043         CEikAppUi::ConstructL();
00044 
00045         // Construct view
00046     iAppView = new(ELeave) CImageAppView;
00047     iAppView->ConstructL(ClientRect());
00048         AddToStackL(iAppView);
00049 
00050         // Bitmap
00051         iBitmap = new(ELeave) CFbsBitmap();
00052         iBitmap->Create(TSize(0,0), EColor256);
00053         iAppView->SetBitmap(iBitmap);
00054 
00055         // Image handler
00056         iImageHandler = new (ELeave) CImageHandler(*iBitmap, iEikonEnv->FsSession(), *this);
00057         iImageHandler->ConstructL();
00058 
00059         // Show menu bar
00060         iEikonEnv->AppUiFactory()->MenuBar()->TryDisplayMenuBarL();
00061         }
00062 
00063 CImageAppUi::~CImageAppUi()
00064         {
00065         delete iBitmap;
00066         delete iImageHandler;
00067         if (iAppView)
00068                 {
00069                 RemoveFromStack(iAppView);
00070                 delete iAppView;
00071                 }
00072         }
00073 
00074 // Handle key events
00075 // ESCAPE cancels loading or saving
00076 TKeyResponse CImageAppUi::HandleKeyEventL(const TKeyEvent& aKeyEvent,TEventCode /*aType*/)
00077         {
00078         TKeyResponse ret = EKeyWasConsumed;
00079 
00080         switch (aKeyEvent.iCode)
00081                 {
00082         case EKeyEscape:
00083                 iImageHandler->Cancel();
00084                 CEikonEnv::Static()->BusyMsgCancel();
00085                 CEikonEnv::Static()->InfoMsg(KCancelled);
00086                 break;
00087         default:
00088                 ret = EKeyWasNotConsumed;
00089                 break;
00090                 }
00091         return ret;
00092         }
00093 
00094 // Handle commands
00095 void CImageAppUi::HandleCommandL(TInt aCommand)
00096         {
00097         switch (aCommand)
00098                 {
00099         // File | Open
00100         case EImageCmdOpenFile:
00101                 OpenFileL();
00102                 break;
00103         // File | Save As
00104         case EImageCmdSaveAs:
00105                 SaveAsL();
00106                 break;
00107         // Image | Zoom in
00108         case EImageCmdZoomIn:
00109                 iImageHandler->ZoomFrame(ETrue);
00110                 break;
00111         // Image | Zoom out
00112         case EImageCmdZoomOut:
00113                 iImageHandler->ZoomFrame(EFalse);
00114                 break;
00115         // Image | Rotate clockwise
00116         case EImageCmdRotateClockwise:
00117                 iImageHandler->FrameRotate(ETrue);
00118                 break;
00119         // Image | Rotate anti-clockwise
00120         case EImageCmdRotateAntiClockwise:
00121                 iImageHandler->FrameRotate(EFalse);
00122                 break;
00123         // Image | Mirror
00124         case EImageCmdMirror:
00125                 iImageHandler->Mirror();
00126                 break;
00127         // Image | Flip
00128         case EImageCmdFlip:
00129                 iImageHandler->Flip();
00130                 break;
00131         // Image | Refresh
00132         case EImageCmdRefresh:
00133                 iImageHandler->LoadFileL(iLoadFileName);
00134                 break;
00135         // Image | Select frame
00136         case EImageCmdExtractFrame:
00137                 ExtractFrameL();
00138                 break;
00139         // File | Close
00140         case EEikCmdExit:
00141                 Exit();
00142                 break;
00143                 }
00144         }
00145 
00146 // Dim out image menu until image loaded
00147 void CImageAppUi::DynInitMenuBarL(TInt /*aResourceId*/, CEikMenuBar* aMenuBar)
00148         {
00149         if (iBitmap->SizeInPixels().iWidth == 0)
00150                 {
00151                 // Get title index for Image menu pane
00152                 const CEikMenuBar::CTitleArray& titles = *(aMenuBar->MenuTitleArray());
00153                 for (TInt i=0; i < titles.Count(); i++)
00154                         if (titles[i]->iData.iMenuPaneResourceId == R_IMAGE_VIEW_MENU)
00155                                 {
00156                                 aMenuBar->SetTitleDimmed(i, ETrue);
00157                                 break;
00158                                 }
00159                 }
00160         }
00161 
00162 // Open File dialog
00163 void CImageAppUi::OpenFileL()
00164         {
00165         
00166         #if defined(__WINS__)
00167         TDriveUnit sysDrive (RFs::GetSystemDrive());
00168         TDriveName sysDriveName (sysDrive.Name());      
00169         TFileName fileName(sysDriveName);
00170         fileName.Append(KSamplesDir);
00171         #else
00172         TFileName fileName(KSamplesDir);
00173         #endif
00174         
00175         if (iLoadFileName.Length() < 4)
00176                 {
00177                 iLoadFileName = fileName;
00178                 }
00179         
00180         // Select a file
00181         CEikDialog* dialog = new(ELeave) CEikFileOpenDialog(&iLoadFileName);
00182         if (!dialog->ExecuteLD(R_EIK_DIALOG_FILE_OPEN)) return;
00183         CEikonEnv::Static()->BusyMsgL(KLoading);
00184         // Load the selected image file
00185         iImageHandler->LoadFileL(iLoadFileName, 0);
00186         }
00187 
00188 // Select frame dialog
00189 void CImageAppUi::ExtractFrameL()
00190         {
00191         // Get number of frames
00192         TInt frameCount = iImageHandler->FrameCount();
00193         // Select a frame
00194         TInt selectedFrame = 0;
00195         CEikDialog* dialog = new(ELeave) CImageCurrentFrameDialog(selectedFrame, frameCount);
00196         if (!dialog->ExecuteLD(R_IMAGE_FRAME_DIALOG)) return;
00197         CEikonEnv::Static()->BusyMsgL(KLoading);
00198         // Load the selected frame
00199         iImageHandler->LoadFileL(iLoadFileName, selectedFrame);
00200         }
00201 
00202 // Save as... dialog
00203 void CImageAppUi::SaveAsL()
00204         {
00205         TDriveUnit sysDrive (RFs::GetSystemDrive());
00206         TDriveName sysDriveName (sysDrive.Name());      
00207         TFileName fileName(sysDriveName);
00208         fileName.Append(KDrive);
00209         
00210         // Do the dialog
00211         if (iSaveFileName.Length() < 4)
00212                 {
00213                 iSaveFileName = fileName;       
00214                 }
00215                 
00216         CEikDialog* dialog = new(ELeave) CImageSaveAsDialog(&iSaveFileName, iSaveInfo);
00217         if (!dialog->ExecuteLD(R_IMAGE_FILE_SAVEAS_DIALOG)) return;
00218         CEikonEnv::Static()->BusyMsgL(KSaving);
00219         // Save the image
00220         iImageHandler->SaveFileL(iSaveFileName, iSaveInfo.iImageType, iSaveInfo.iImageSubType );
00221         }
00222 
00223 // Image operation complete callback
00224 // Leaves will be caught by CONE error handling
00225 void CImageAppUi::ImageOperationCompleteL(TInt aError)
00226         {
00227         CEikonEnv::Static()->BusyMsgCancel();
00228         User::LeaveIfError(aError);
00229         iAppView->Clear();
00230         iAppView->DrawBitmapNow();
00231         }
00232 
00233 //
00234 // CImageCurrentFrameDialog: select frame dialog
00235 //
00236 
00237 CImageCurrentFrameDialog::CImageCurrentFrameDialog(TInt& aCurrentFrame,TInt aNumberOfFrames):
00238         iCurrentFrame(aCurrentFrame),
00239         iNumberOfFrames(aNumberOfFrames)
00240         {}
00241 
00242 // Initialise dialog with current settings
00243 void CImageCurrentFrameDialog::PreLayoutDynInitL()
00244         {
00245         CEikNumberEditor* frames = static_cast<CEikNumberEditor*>(Control(EImageIdNumberOfFrames));
00246         frames -> SetNumber(0);
00247         frames -> SetMinimumAndMaximum(0,iNumberOfFrames - 1);
00248         }
00249 
00250 // Store dialog result
00251 TBool CImageCurrentFrameDialog::OkToExitL(TInt /*aButtonId*/)
00252         {
00253         CEikNumberEditor* frames = static_cast<CEikNumberEditor*>(Control(EImageIdNumberOfFrames));
00254         iCurrentFrame = frames -> Number();
00255         return ETrue;
00256         }
00257 
00258 
00259 //
00260 // CImageSaveAsDialog: save as dialog
00261 //
00262 
00263 CImageSaveAsDialog::CImageSaveAsDialog(TDes* aFileName, TFileSaveDialogOptions& aSaveInfo):
00264         CEikFileSaveAsDialog(aFileName),
00265         iSaveInfo(aSaveInfo)
00266         {}
00267 
00268 // Set dialog with image types
00269 void CImageSaveAsDialog::PreLayoutDynInitL()
00270         {
00271         CImageEncoder::GetImageTypesL(iImageTypes);
00272         CEikChoiceList* types = static_cast<CEikChoiceList*>(Control(EImageIdSaveAsFormat));
00273         CDesC16ArrayFlat* listItems = new (ELeave) CDesC16ArrayFlat(4);
00274         for (int i=0; i<iImageTypes.Count(); i++)
00275                 listItems->AppendL(iImageTypes[i]->Description());
00276         types->SetArrayL(listItems);
00277         SetImageSubTypesL();
00278         CEikFileSaveAsDialog::PreLayoutDynInitL();
00279         }
00280 
00281 // Set image sub-types list
00282 void CImageSaveAsDialog::SetImageSubTypesL()
00283         {
00284         // Extract image format type chosen
00285         CEikChoiceList* subtypes = static_cast<CEikChoiceList*>(Control(EImageIdSubtype)); 
00286         CEikChoiceList* types = static_cast<CEikChoiceList*>(Control(EImageIdSaveAsFormat));
00287         TUid type = iImageTypes[types->CurrentItem()]->ImageType();             // Image type
00288         // Get list of available image sub-types
00289         TRAPD(err, CImageEncoder::GetImageSubTypesL (type, iImageSubTypes));
00290         if (err == KErrNotFound) // no sub-types
00291                 {
00292                 subtypes->SetArrayL(R_NO_SUB_TYPES);
00293                 subtypes->SetCurrentItem(0);
00294                 iSubtypeSet = EFalse;
00295                 return;
00296                 }
00297         User::LeaveIfError(err); // leave for other errors
00298         // Fill choicelist with sub-types
00299         CDesC16ArrayFlat* listItems = new (ELeave) CDesC16ArrayFlat(4);
00300         for (int i=0; i<iImageSubTypes.Count(); i++)
00301                 listItems->AppendL(iImageSubTypes[i]->Description());
00302         subtypes->SetArrayL(listItems);
00303         subtypes->SetCurrentItem(0);
00304         iSubtypeSet = ETrue;
00305         }
00306 
00307 // Update any image sub-types
00308 void CImageSaveAsDialog::HandleControlStateChangeL(TInt aControlId)
00309         {
00310         if (aControlId == EImageIdSaveAsFormat) SetImageSubTypesL();
00311         }
00312 
00313 // Need to clean up RImageTypeDescriptionArray resources before exiting
00314 CImageSaveAsDialog::~CImageSaveAsDialog()
00315         {
00316         iImageSubTypes.ResetAndDestroy();
00317         iImageSubTypes.Close();
00318         iImageTypes.ResetAndDestroy();
00319         iImageTypes.Close();
00320         }
00321 
00322 // Store selected type
00323 TBool CImageSaveAsDialog::OkToExitL(TInt aButtonId)
00324         {
00325         // Extract type chosen
00326         CEikChoiceList* types = static_cast<CEikChoiceList*>(Control(EImageIdSaveAsFormat));
00327         TInt index = types->CurrentItem();
00328         iSaveInfo.iImageType = iImageTypes[index]->ImageType();         // Image type
00329         if (iSubtypeSet)
00330                 {
00331                 CEikChoiceList* subtypes = static_cast<CEikChoiceList*>(Control(EImageIdSubtype));
00332                 iSaveInfo.iImageSubType = iImageSubTypes[subtypes->CurrentItem()]->SubType();// Image sub-type
00333                 }
00334         else
00335                 iSaveInfo.iImageSubType = KNullUid;
00336 
00337         return CEikFileSaveAsDialog::OkToExitL(aButtonId);
00338         }

Generated on Thu Jan 21 10:32:59 2010 for TB10.1 Example Applications by  doxygen 1.5.3