phonebookui/Phonebook2/UIControls/src/CPbk2ThumbnailPopup.cpp
changeset 0 e686773b3f54
child 21 9da50d567e3c
equal deleted inserted replaced
-1:000000000000 0:e686773b3f54
       
     1 /*
       
     2 * Copyright (c) 2005-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "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 *
       
    14 * Description:  Phonebook 2 thumbnail popup.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include "CPbk2ThumbnailPopup.h"
       
    22 
       
    23 // Phonebook 2
       
    24 #include "CPbk2ThumbnailPopupControl.h"
       
    25 #include "CPbk2ThumbnailScaler.h"
       
    26 #include <CPbk2ImageManager.h>
       
    27 #include <CPbk2IconFactory.h>
       
    28 #include <CPbk2IconInfoContainer.h>
       
    29 #include <TPbk2AppIconId.h>
       
    30 #include <TPbk2ImageManagerParams.h>
       
    31 #include <Pbk2UIControls.rsg>
       
    32 
       
    33 // Virtual Phonebook
       
    34 #include <MVPbkBaseContact.h>
       
    35 #include <MVPbkFieldType.h>
       
    36 #include <CVPbkContactManager.h>
       
    37 #include <TVPbkFieldTypeMapping.h>
       
    38 
       
    39 // System includes
       
    40 #include <aknlayoutscalable_apps.cdl.h>
       
    41 #include <AknsUtils.h>
       
    42 #include <barsread.h>
       
    43 #include <layoutmetadata.cdl.h>
       
    44 #include <AknUtils.h>
       
    45 
       
    46 
       
    47 /// Unnamed namespace for local defintions
       
    48 namespace {
       
    49 
       
    50 // LOCAL CONSTANTS AND MACROS
       
    51 #ifdef _DEBUG
       
    52 
       
    53 enum TPanicCode
       
    54     {
       
    55     EPanicPostCond_Constructor = 1,
       
    56     EPanicInvariant_InvalidTimerState,
       
    57     EPanicPreCond_Pbk2ThumbnailGetComplete,
       
    58     EPanicPreCond_Pbk2ThumbnailGetFailed,
       
    59     EPanicPreCond_LoadContactL,
       
    60     EPanicPreCond_ShowPopupBitmap
       
    61     };
       
    62 
       
    63 void Panic(TPanicCode aReason)
       
    64     {
       
    65     _LIT(KPanicText, "CPbk2ThumbnailPopup");
       
    66     User::Panic(KPanicText, aReason);
       
    67     }
       
    68 
       
    69 #endif // _DEBUG
       
    70 
       
    71 } /// namespace
       
    72 
       
    73 /**
       
    74  * Observer for the thumbnail scaling process.
       
    75  */
       
    76 NONSHARABLE_CLASS(CPbk2ThumbnailScalerObserver) :
       
    77             public CBase,
       
    78             public MPbk2ThumbnailScalerObserver
       
    79     {
       
    80     public: // Construction and destruction
       
    81 
       
    82         /**
       
    83          * Creates a new instance of this class.
       
    84          *
       
    85          * @param aPopup    Popup that is used to show the bitmap.
       
    86          * @return  A new instance of this class.
       
    87          */
       
    88         static CPbk2ThumbnailScalerObserver* NewL(
       
    89                 CPbk2ThumbnailPopup& aPopup );
       
    90 
       
    91         /**
       
    92          * Standard C++ destructor.
       
    93          */
       
    94         ~CPbk2ThumbnailScalerObserver();
       
    95 
       
    96     private: // From MPbk2ThumbnailScalerObserver
       
    97         void ThumbnailScalingComplete(
       
    98                 TInt aError,
       
    99                 CFbsBitmap* aBitmap );
       
   100 
       
   101     private: // Implementation
       
   102         CPbk2ThumbnailScalerObserver(
       
   103                 CPbk2ThumbnailPopup& aPopup );
       
   104 
       
   105     private: // Data
       
   106         /// Ref: Popup that is used to show the bitmap
       
   107         CPbk2ThumbnailPopup& iPopup;
       
   108     };
       
   109 
       
   110 // --------------------------------------------------------------------------
       
   111 // CPbk2ThumbnailScalerObserver::CPbk2ThumbnailScalerObserver
       
   112 // --------------------------------------------------------------------------
       
   113 //
       
   114 CPbk2ThumbnailScalerObserver::CPbk2ThumbnailScalerObserver
       
   115         ( CPbk2ThumbnailPopup& aPopup ) :
       
   116             iPopup(aPopup)
       
   117     {
       
   118     }
       
   119 
       
   120 // --------------------------------------------------------------------------
       
   121 // CPbk2ThumbnailScalerObserver::~CPbk2ThumbnailScalerObserver
       
   122 // --------------------------------------------------------------------------
       
   123 //
       
   124 CPbk2ThumbnailScalerObserver::~CPbk2ThumbnailScalerObserver()
       
   125     {
       
   126     }
       
   127 
       
   128 // --------------------------------------------------------------------------
       
   129 // CPbk2ThumbnailScalerObserver::NewL
       
   130 // --------------------------------------------------------------------------
       
   131 //
       
   132 CPbk2ThumbnailScalerObserver* CPbk2ThumbnailScalerObserver::NewL
       
   133         ( CPbk2ThumbnailPopup& aPopup )
       
   134     {
       
   135     return new ( ELeave ) CPbk2ThumbnailScalerObserver( aPopup );
       
   136     }
       
   137 
       
   138 // --------------------------------------------------------------------------
       
   139 // CPbk2ThumbnailScalerObserver::ThumbnailScalingComplete
       
   140 // --------------------------------------------------------------------------
       
   141 //
       
   142 void CPbk2ThumbnailScalerObserver::ThumbnailScalingComplete
       
   143         (TInt aError, CFbsBitmap* aBitmap)
       
   144     {
       
   145     if (aError == KErrNone)
       
   146         {
       
   147         iPopup.ShowPopupBitmap(aBitmap);
       
   148         }
       
   149     else
       
   150         {
       
   151         delete aBitmap;
       
   152         }
       
   153     }
       
   154 
       
   155 // --------------------------------------------------------------------------
       
   156 // CPbk2ThumbnailPopup::CPbk2ThumbnailPopup
       
   157 // --------------------------------------------------------------------------
       
   158 //
       
   159 inline CPbk2ThumbnailPopup::CPbk2ThumbnailPopup
       
   160     (CVPbkContactManager& aContactManager)
       
   161     : iContactManager(aContactManager)
       
   162     {
       
   163     __ASSERT_DEBUG( !iThumbnailPopupControl,
       
   164         Panic(EPanicPostCond_Constructor));
       
   165 
       
   166     }
       
   167 
       
   168 // --------------------------------------------------------------------------
       
   169 // CPbk2ThumbnailPopup::~CPbk2ThumbnailPopup
       
   170 // --------------------------------------------------------------------------
       
   171 //
       
   172 CPbk2ThumbnailPopup::~CPbk2ThumbnailPopup()
       
   173     {
       
   174     delete iThumbnailScaler;
       
   175     delete iScalerObserver;
       
   176     delete iThumbOperation;
       
   177     delete iThumbnailPopupControl;
       
   178     delete iThumbnailBackg;
       
   179     delete iThumbnailBackgMask;
       
   180     delete iManager;
       
   181     }
       
   182 
       
   183 // --------------------------------------------------------------------------
       
   184 // CPbk2ThumbnailPopup::ConstructL
       
   185 // --------------------------------------------------------------------------
       
   186 //
       
   187 inline void CPbk2ThumbnailPopup::ConstructL()
       
   188     {
       
   189     }
       
   190 
       
   191 // --------------------------------------------------------------------------
       
   192 // CPbk2ThumbnailPopup::NewL
       
   193 // --------------------------------------------------------------------------
       
   194 //
       
   195 CPbk2ThumbnailPopup* CPbk2ThumbnailPopup::NewL
       
   196         ( CVPbkContactManager& aContactManager )
       
   197     {
       
   198     CPbk2ThumbnailPopup* self =
       
   199         new (ELeave) CPbk2ThumbnailPopup(aContactManager);
       
   200     CleanupStack::PushL( self );
       
   201     self->ConstructL();
       
   202     CleanupStack::Pop( self );
       
   203     return self;
       
   204     }
       
   205 
       
   206 // --------------------------------------------------------------------------
       
   207 // CPbk2ThumbnailPopup::Load
       
   208 // --------------------------------------------------------------------------
       
   209 //
       
   210 void CPbk2ThumbnailPopup::Load
       
   211         ( MVPbkBaseContact& aContact, MPbk2ThumbnailPopupObserver& aObserver,
       
   212         const CEikListBox* aListBox, const MVPbkFieldType* aFieldType )
       
   213     {
       
   214     iListBox = aListBox;
       
   215     iLoadObserver = &aObserver;
       
   216     TRAPD(err, DoLoadL(aContact, aFieldType));
       
   217     if ( err != KErrNone)
       
   218         {
       
   219         iLoadObserver->PopupLoadFailed( err );
       
   220         }
       
   221     }
       
   222 
       
   223 // --------------------------------------------------------------------------
       
   224 // CPbk2ThumbnailPopup::HideAndCancel
       
   225 // --------------------------------------------------------------------------
       
   226 //
       
   227 void CPbk2ThumbnailPopup::HideAndCancel()
       
   228     {
       
   229     HideControl();
       
   230 
       
   231     // Cancel thumbnail loading
       
   232     delete iThumbOperation;
       
   233     iThumbOperation = NULL;
       
   234     // Cancel thumbnail scaling
       
   235     delete iThumbnailScaler;
       
   236     iThumbnailScaler = NULL;
       
   237     }
       
   238 
       
   239 // --------------------------------------------------------------------------
       
   240 // CPbk2ThumbnailPopup::ShowPopupBitmap
       
   241 // --------------------------------------------------------------------------
       
   242 //
       
   243 void CPbk2ThumbnailPopup::ShowPopupBitmap(CFbsBitmap* aBitmap)
       
   244     {
       
   245     iBitmap=aBitmap;
       
   246     const TInt orientation( Layout_Meta_Data::IsLandscapeOrientation() ? 0 : 1 );
       
   247 
       
   248     /**
       
   249     * The thumbnail window requires size equal to the
       
   250     * thumbnail size + the space needed for bg graphics (see LAF documentation)
       
   251     */
       
   252     TAknLayoutRect thumbWindowLayoutRect, thumbBgLayoutRect, thumbLayoutRect;
       
   253 
       
   254     thumbWindowLayoutRect.LayoutRect( iListBox->View()->ViewRect(),
       
   255                                       AknLayoutScalable_Apps::popup_phob_thumbnail2_window( orientation ) );
       
   256 
       
   257     thumbBgLayoutRect.LayoutRect( thumbWindowLayoutRect.Rect(),
       
   258                                   AknLayoutScalable_Apps::bg_popup_preview_window_pane_cp01( orientation ) );
       
   259 
       
   260     thumbLayoutRect.LayoutRect( thumbWindowLayoutRect.Rect(),
       
   261                                 AknLayoutScalable_Apps::popup_phob_thumbnail2_window_g1( orientation ) );
       
   262 
       
   263     const TSize bgSpace( thumbBgLayoutRect.Rect().Size() - thumbLayoutRect.Rect().Size() );
       
   264     const TSize bgSize( aBitmap->SizeInPixels() + bgSpace );
       
   265 
       
   266     // scale the background bitmaps
       
   267     AknIconUtils::SetSize( iThumbnailBackg,
       
   268                            bgSize,
       
   269                            EAspectRatioNotPreserved );
       
   270 
       
   271     AknIconUtils::SetSize( iThumbnailBackgMask,
       
   272                            bgSize,
       
   273                            EAspectRatioNotPreserved );
       
   274 
       
   275     iThumbnailPopupControl->SetThumbnailBackg( iThumbnailBackg, iThumbnailBackgMask );
       
   276     iThumbnailPopupControl->SetBitmap( aBitmap );
       
   277     iThumbnailPopupControl->MakeControlVisible( ETrue );
       
   278 
       
   279     iLoadObserver->PopupLoadComplete();
       
   280     }
       
   281 
       
   282 // --------------------------------------------------------------------------
       
   283 // CPbk2ThumbnailPopup::Refresh
       
   284 // --------------------------------------------------------------------------
       
   285 //
       
   286 void CPbk2ThumbnailPopup::Refresh()
       
   287     {
       
   288     if (( iThumbnailPopupControl ) && (iBitmap))
       
   289         {
       
   290         iThumbnailPopupControl->MakeControlVisible( ETrue );
       
   291         }
       
   292     }
       
   293 
       
   294 // --------------------------------------------------------------------------
       
   295 // CPbk2ThumbnailPopup::SetThumbnailLocation
       
   296 // --------------------------------------------------------------------------
       
   297 //
       
   298 void CPbk2ThumbnailPopup::SetThumbnailLocation
       
   299         ( TPbk2ThumbnailLocation aThumbnailLocation )
       
   300     {
       
   301     iThumbnailLocation = aThumbnailLocation;
       
   302     }
       
   303 
       
   304 // --------------------------------------------------------------------------
       
   305 // CPbk2ThumbnailPopup::Pbk2ImageGetComplete
       
   306 // --------------------------------------------------------------------------
       
   307 //
       
   308 void CPbk2ThumbnailPopup::Pbk2ImageGetComplete
       
   309         (MPbk2ImageOperation& aOperation,
       
   310         CFbsBitmap* aBitmap)
       
   311     {
       
   312     __ASSERT_DEBUG(&aOperation==iThumbOperation && aBitmap
       
   313         && iThumbnailPopupControl && iThumbnailScaler,
       
   314         Panic(EPanicPreCond_Pbk2ThumbnailGetComplete));
       
   315 
       
   316     // Suppress the unreferenced parameter warning
       
   317     (void) aOperation;
       
   318 
       
   319     iThumbnailScaler->CreateThumbnail(aBitmap);
       
   320 
       
   321     delete iThumbOperation;
       
   322     iThumbOperation = NULL;
       
   323     }
       
   324 
       
   325 // --------------------------------------------------------------------------
       
   326 // CPbk2ThumbnailPopup::Pbk2ImageGetFailed
       
   327 // --------------------------------------------------------------------------
       
   328 //
       
   329 void CPbk2ThumbnailPopup::Pbk2ImageGetFailed
       
   330         (MPbk2ImageOperation& aOperation,
       
   331         TInt aError)
       
   332     {
       
   333     __ASSERT_DEBUG(iThumbOperation == &aOperation &&
       
   334                     aError != KErrNone &&
       
   335                     iLoadObserver,
       
   336             Panic(EPanicPreCond_Pbk2ThumbnailGetFailed));
       
   337 
       
   338     // Suppress the unreferenced parameter warnings
       
   339     (void) aError;
       
   340     (void) aOperation;
       
   341 
       
   342     HideControl();
       
   343     delete iThumbOperation;
       
   344     iThumbOperation = NULL;
       
   345 
       
   346     iLoadObserver->PopupLoadFailed(aError);
       
   347     }
       
   348 
       
   349 // --------------------------------------------------------------------------
       
   350 // CPbk2ThumbnailPopup::DoLoadL
       
   351 // --------------------------------------------------------------------------
       
   352 //
       
   353 void CPbk2ThumbnailPopup::DoLoadL
       
   354         (MVPbkBaseContact& aContact,
       
   355         const MVPbkFieldType* aFieldType)
       
   356     {
       
   357     HideAndCancel();
       
   358 
       
   359     if ( !aFieldType )
       
   360         {
       
   361         ReadFieldTypeL();
       
   362         }
       
   363     else
       
   364         {
       
   365         iFieldType = aFieldType;
       
   366         }
       
   367 
       
   368     LoadThumbnailBackgL();
       
   369     GetThumbnailL( aContact, *iFieldType );
       
   370     }
       
   371 
       
   372 // --------------------------------------------------------------------------
       
   373 // CPbk2ThumbnailPopup::HideControl
       
   374 // --------------------------------------------------------------------------
       
   375 //
       
   376 void CPbk2ThumbnailPopup::HideControl()
       
   377     {
       
   378     if (iThumbnailPopupControl)
       
   379         {
       
   380         // hide the control
       
   381         iThumbnailPopupControl->MakeControlVisible(EFalse);
       
   382         iThumbnailPopupControl->SetBitmap(NULL);
       
   383         iBitmap = NULL;
       
   384         }
       
   385     }
       
   386 
       
   387 // --------------------------------------------------------------------------
       
   388 // CPbk2ThumbnailPopup::GetThumbnailL
       
   389 // --------------------------------------------------------------------------
       
   390 //
       
   391 void CPbk2ThumbnailPopup::GetThumbnailL
       
   392             ( MVPbkBaseContact& aContact, const MVPbkFieldType& aFieldType )
       
   393     {
       
   394     if (!iManager)
       
   395         {
       
   396         iManager = CPbk2ImageManager::NewL(iContactManager);
       
   397         }
       
   398 
       
   399     if (iManager->HasImage(aContact, aFieldType))
       
   400         {
       
   401         if (!iThumbnailPopupControl )
       
   402             {
       
   403             iThumbnailPopupControl =
       
   404                 CPbk2ThumbnailPopupControl::NewL( iThumbnailLocation, iListBox );
       
   405             }
       
   406         if (!iScalerObserver)
       
   407             {
       
   408             iScalerObserver = CPbk2ThumbnailScalerObserver::NewL(*this);
       
   409             }
       
   410         if (!iThumbnailScaler)
       
   411             {
       
   412             iThumbnailScaler = CPbk2ThumbnailScaler::NewL(*iScalerObserver);
       
   413             }
       
   414 
       
   415         // start loading picture
       
   416         delete iThumbOperation;
       
   417         iThumbOperation = NULL;
       
   418 
       
   419         TPbk2ImageManagerParams params;
       
   420         SetupImageParams( params );
       
   421 
       
   422         iThumbOperation =
       
   423             iManager->GetImageAsyncL( &params, aContact, aFieldType, *this);
       
   424         }
       
   425     else if (iThumbnailPopupControl)
       
   426     	{
       
   427     	iThumbnailPopupControl->SetBitmap(NULL);
       
   428     	}
       
   429 
       
   430     }
       
   431 
       
   432 // --------------------------------------------------------------------------
       
   433 // CPbk2ThumbnailPopup::ReadFieldTypeL
       
   434 // --------------------------------------------------------------------------
       
   435 //
       
   436 void CPbk2ThumbnailPopup::ReadFieldTypeL()
       
   437     {
       
   438     TResourceReader reader;
       
   439     CCoeEnv::Static()->CreateResourceReaderLC
       
   440         ( reader, R_PBK2_THUMBNAIL_FIELD_TYPE );
       
   441     const TInt count = reader.ReadInt16();
       
   442 
       
   443     TVPbkFieldTypeMapping mapping(reader);
       
   444     iFieldType = mapping.FindMatch(iContactManager.FieldTypes());
       
   445     if (!iFieldType)
       
   446         {
       
   447         User::Leave(KErrNotFound);
       
   448         }
       
   449     CleanupStack::PopAndDestroy(); // CreateResourceReaderLC
       
   450     }
       
   451 
       
   452 // --------------------------------------------------------------------------
       
   453 // CPbk2ThumbnailPopup::LoadThumbnailBackgL
       
   454 // --------------------------------------------------------------------------
       
   455 //
       
   456 void CPbk2ThumbnailPopup::LoadThumbnailBackgL()
       
   457     {
       
   458     delete iThumbnailBackg;
       
   459     iThumbnailBackg = NULL;
       
   460     delete iThumbnailBackgMask;
       
   461     iThumbnailBackgMask = NULL;
       
   462 
       
   463     CPbk2IconInfoContainer* iconInfoContainer =
       
   464         CPbk2IconInfoContainer::NewL( R_PBK2_GRAF_BG_THUMBNAIL );
       
   465     CleanupStack::PushL( iconInfoContainer );
       
   466     CPbk2IconFactory* factory =
       
   467         CPbk2IconFactory::NewLC( *iconInfoContainer );
       
   468 
       
   469     factory->CreateIconL(
       
   470         TPbk2AppIconId( EPbk2qgn_graf_phob_thumbnail_backg ),
       
   471         *AknsUtils::SkinInstance(),
       
   472         iThumbnailBackg,
       
   473         iThumbnailBackgMask );
       
   474 
       
   475     CleanupStack::PopAndDestroy(2); // factory, iconInfoContainer
       
   476     }
       
   477 
       
   478 // --------------------------------------------------------------------------
       
   479 // CPbk2ThumbnailPopup::SetupImageParams
       
   480 // --------------------------------------------------------------------------
       
   481 //
       
   482 void CPbk2ThumbnailPopup::SetupImageParams
       
   483         ( TPbk2ImageManagerParams& aParams )
       
   484     {
       
   485     const TInt orientation( Layout_Meta_Data::IsLandscapeOrientation() ? 0 : 1 );
       
   486     TAknLayoutRect thumbWindowLayoutRect, thumbLayoutRect;
       
   487 
       
   488     thumbWindowLayoutRect.LayoutRect(
       
   489         iListBox->View()->ViewRect(),
       
   490         AknLayoutScalable_Apps::popup_phob_thumbnail2_window( orientation ) );
       
   491 
       
   492     thumbLayoutRect.LayoutRect(
       
   493         thumbWindowLayoutRect.Rect(),
       
   494         AknLayoutScalable_Apps::popup_phob_thumbnail2_window_g1( orientation ) );
       
   495 
       
   496     aParams.iSize = thumbLayoutRect.Rect().Size();
       
   497     aParams.iFlags = TPbk2ImageManagerParams::EScaleImage;
       
   498     }
       
   499 
       
   500 
       
   501 // End of File