calendarui/regionalplugins/KoreanLunar/src/CalenExtraRowFormatter.cpp
branchRCL_3
changeset 31 97232defd20e
equal deleted inserted replaced
30:bd7edf625bdd 31:97232defd20e
       
     1 /*
       
     2 * Copyright (c) 2002-2004 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 : Class looking after alarm fields for forms.
       
    15 *
       
    16 */
       
    17 
       
    18 //debug
       
    19 #include "calendarui_debug.h"
       
    20 #include "CalenExtraRowFormatter.h"
       
    21 
       
    22 #include <calenkoreanlunarplugindata.rsg>
       
    23 #include <aknbiditextutils.h>
       
    24 #include <badesca.h>
       
    25 #include <eikenv.h>
       
    26 #include <StringLoader.h>
       
    27 
       
    28 // CONSTANTS
       
    29 _LIT( KSeparatorFmt, "%0U" );
       
    30 const TInt KExtraRowLength = 1000;
       
    31 
       
    32 // ======== MEMBER FUNCTIONS ========
       
    33 
       
    34 
       
    35 // ---------------------------------------------------------------------------
       
    36 // CollapseDuplicatesL()
       
    37 // ---------------------------------------------------------------------------
       
    38 //
       
    39 void CollapseDuplicatesL( TDes& aStr, TInt aPos, const TDesC& aSub )
       
    40     {
       
    41     TRACE_ENTRY_POINT;
       
    42 
       
    43     const TInt sublen = aSub.Length();
       
    44     if ( aStr.Length() == 0 || sublen == 0 )
       
    45         {
       
    46         return;
       
    47         }
       
    48 
       
    49     TPtrC remaining = aStr.Mid( aPos );
       
    50     TInt fstInRemaining = remaining.Find( aSub );
       
    51 
       
    52     if ( fstInRemaining >= 0 )
       
    53         {
       
    54         TInt restPos = fstInRemaining + sublen;
       
    55         TPtrC rest = remaining.Mid( restPos );
       
    56         TInt sndInRest = rest.Find( aSub );
       
    57 
       
    58         // 1) two substrings found in sequence
       
    59         if ( sndInRest == 0 )
       
    60             {
       
    61             // replace second substring with empty string
       
    62             TInt fst = aPos + fstInRemaining;
       
    63             TInt snd = aPos + restPos + sndInRest;
       
    64             aStr.Replace( snd, sublen, KNullDesC);
       
    65             // continue collapsing from first
       
    66             CollapseDuplicatesL( aStr, fst, aSub );
       
    67             }
       
    68         // 2) substring found later in string
       
    69         else if ( sndInRest > 0 )
       
    70             {
       
    71             // continue collapsing from this second substring
       
    72             TInt snd = aPos + restPos + sndInRest;
       
    73             CollapseDuplicatesL( aStr, snd, aSub );
       
    74             }
       
    75         // 3) No second substring found -> nothing to collapse
       
    76         else
       
    77             {
       
    78             TRACE_EXIT_POINT;
       
    79             return;
       
    80             }
       
    81         }
       
    82     // No substring found
       
    83     else
       
    84         {
       
    85         TRACE_EXIT_POINT;
       
    86         return;
       
    87         }
       
    88     }
       
    89 
       
    90 // ---------------------------------------------------------------------------
       
    91 // RemoveLeadingAndTrailingL()
       
    92 // ---------------------------------------------------------------------------
       
    93 //
       
    94 void RemoveLeadingAndTrailingL( TDes& aStr, const TDesC& aSub )
       
    95     {
       
    96     TRACE_ENTRY_POINT;
       
    97 
       
    98     // Trailing
       
    99     const TInt sublen = aSub.Length();
       
   100     if ( aStr.Right( sublen ).Find( aSub ) == 0 )
       
   101         {
       
   102         aStr.Replace( aStr.Length() - sublen, sublen, KNullDesC );
       
   103         }
       
   104 
       
   105     // Leading
       
   106     if ( aStr.Left( sublen ).Find( aSub ) == 0 )
       
   107         {
       
   108         aStr.Replace( 0, sublen, KNullDesC );
       
   109         }
       
   110 
       
   111     TRACE_EXIT_POINT;
       
   112     }
       
   113 
       
   114 // ---------------------------------------------------------------------------
       
   115 // CCalenExtraRowFormatter::NewL()
       
   116 // ---------------------------------------------------------------------------
       
   117 //
       
   118 CCalenExtraRowFormatter* CCalenExtraRowFormatter::NewL()
       
   119     {
       
   120     TRACE_ENTRY_POINT;
       
   121 
       
   122     CCalenExtraRowFormatter* self = new (ELeave) CCalenExtraRowFormatter;
       
   123     CleanupStack::PushL( self );
       
   124     self->ConstructL();
       
   125     CleanupStack::Pop( self );
       
   126 
       
   127     TRACE_EXIT_POINT;
       
   128     return self;
       
   129     }
       
   130 
       
   131 // ---------------------------------------------------------------------------
       
   132 // CCalenExtraRowFormatter::~CCalenExtraRowFormatter()
       
   133 // ---------------------------------------------------------------------------
       
   134 //
       
   135 CCalenExtraRowFormatter::~CCalenExtraRowFormatter()
       
   136     {
       
   137     TRACE_ENTRY_POINT;
       
   138     TRACE_EXIT_POINT;
       
   139     }
       
   140 
       
   141 // ---------------------------------------------------------------------------
       
   142 // CCalenExtraRowFormatter::CCalenExtraRowFormatter()
       
   143 // ---------------------------------------------------------------------------
       
   144 //
       
   145 CCalenExtraRowFormatter::CCalenExtraRowFormatter()
       
   146     {
       
   147     TRACE_ENTRY_POINT;
       
   148     TRACE_EXIT_POINT;
       
   149     }
       
   150 
       
   151 // ---------------------------------------------------------------------------
       
   152 // CCalenExtraRowFormatter::ConstructL()
       
   153 // ---------------------------------------------------------------------------
       
   154 //
       
   155 void CCalenExtraRowFormatter::ConstructL()
       
   156     {
       
   157     TRACE_ENTRY_POINT;
       
   158     TRACE_EXIT_POINT;
       
   159     }
       
   160 
       
   161 // ---------------------------------------------------------------------------
       
   162 // CCalenExtraRowFormatter::FormatExtraRowInformationL()
       
   163 // ---------------------------------------------------------------------------
       
   164 //
       
   165 TPtrC CCalenExtraRowFormatter::FormatExtraRowInformationL(
       
   166     CCalenLunarLocalizedInfo& aLocInfo,
       
   167     RArray<CCalenLunarLocalizedInfo::TField>& aPrioritizedFields,
       
   168     TInt aMaxWidth,
       
   169     const CFont& aFont
       
   170 #ifdef RD_CALENDAR_PREVIEW
       
   171     ,TBool aTwoLines
       
   172 #endif // RD_CALENDAR_PREVIEW
       
   173     )
       
   174     {
       
   175     TRACE_ENTRY_POINT;
       
   176 
       
   177     if ( aPrioritizedFields.Count() == 0)
       
   178         {
       
   179         iText = KNullDesC;
       
   180 
       
   181         TRACE_EXIT_POINT;
       
   182         return iText;
       
   183         }
       
   184 
       
   185     // Initialize substring labels
       
   186     RArray<CCalenLunarLocalizedInfo::TField> subLabels;
       
   187     CleanupClosePushL( subLabels );
       
   188     subLabels.AppendL( CCalenLunarLocalizedInfo::ELunarMonthAndDay );
       
   189     subLabels.AppendL( CCalenLunarLocalizedInfo::ELunarFestival );
       
   190     subLabels.AppendL( CCalenLunarLocalizedInfo::ESolarFestival );
       
   191     subLabels.AppendL( CCalenLunarLocalizedInfo::ESolarTerm );
       
   192 
       
   193     // ASSERT that all prioritized fields can be found from subLabels
       
   194     for ( TInt i=0; i < aPrioritizedFields.Count(); i++)
       
   195         {
       
   196         ASSERT( subLabels.Find( aPrioritizedFields[i] ) >= 0 );
       
   197         }
       
   198 
       
   199     TBool fits = EFalse;
       
   200 
       
   201     do
       
   202         {
       
   203         // Initialize substring array
       
   204         CPtrCArray* subs = new (ELeave) CPtrCArray(10);
       
   205         CleanupStack::PushL( subs );
       
   206         for ( TInt i = 0; i < subLabels.Count(); i++)
       
   207             {
       
   208             subs->AppendL( TPtrC( KNullDesC ) );
       
   209             }
       
   210 
       
   211         // Set wanted fields to substring array
       
   212         for ( TInt i = 0; i < aPrioritizedFields.Count(); i++)
       
   213             {
       
   214             CCalenLunarLocalizedInfo::TField field = aPrioritizedFields[i];
       
   215             TInt subIx = subLabels.Find( field );
       
   216             // Replace
       
   217             subs->Delete(subIx);
       
   218             RDebug::Print( _L("A sub count  %d"), subs->Count() );
       
   219             subs->InsertL(subIx, TPtrC( aLocInfo.GetField( field ) ) );
       
   220             RDebug::Print( _L("B sub count %d"), subs->Count() );
       
   221             RDebug::Print( _L("B field %S"), &(subs->At(subIx)) );
       
   222             }
       
   223 
       
   224         // Format all fields to extra row
       
   225         HBufC* extraRowFmt = StringLoader::LoadLC( R_KOR_CALE_EXTRA_ROW_LUNAR ); 
       
   226 
       
   227         RDebug::RawPrint( *extraRowFmt );
       
   228 
       
   229         TBuf<KExtraRowLength> fmt = *extraRowFmt;
       
   230         for ( TInt i=0; i < subLabels.Count(); i++ )
       
   231             {
       
   232             RDebug::Print( _L("Before Format") );
       
   233             RDebug::RawPrint( fmt );
       
   234             StringLoader::Format( iText,
       
   235                                   fmt,
       
   236                                   i + 1, // %0U is a separator
       
   237                                   subs->At( i ) );
       
   238             fmt = iText;
       
   239             RDebug::Print( _L("After Format") );
       
   240             RDebug::RawPrint( fmt );
       
   241             }
       
   242 
       
   243         // Now we have something like "Year of Dog%0U%0U6/11%0U%0U"
       
   244         // First We need to remove multiple occurences of %0U
       
   245         CollapseDuplicatesL( iText, 0, KSeparatorFmt );
       
   246         RDebug::Print( _L("After collapse") );
       
   247         RDebug::RawPrint( iText );
       
   248 
       
   249         // Remove leading and trailing %0U
       
   250         // By now, we are sure that there is max 1 %0U in the beginning
       
   251         // and in the end of string.
       
   252         RemoveLeadingAndTrailingL( iText, KSeparatorFmt );
       
   253         RDebug::Print( _L("After leading and trailing removal") );
       
   254         RDebug::RawPrint( iText );
       
   255 
       
   256         // If there are now separators anymore, then do not fill them
       
   257         TBool hasSeparators = iText.Find( KSeparatorFmt ) >= 0;
       
   258 
       
   259         if ( hasSeparators )
       
   260             {
       
   261             // fill in separators
       
   262             HBufC* separator = StringLoader::LoadLC( R_CALE_LUNAR_SEPARATOR );
       
   263             fmt = iText;
       
   264             StringLoader::Format( iText,
       
   265                                   fmt,
       
   266                                   0, // %0U is a separator
       
   267                                   *separator );
       
   268 
       
   269             RDebug::Print( _L("After separator insert") );
       
   270             RDebug::RawPrint( iText );
       
   271             CleanupStack::PopAndDestroy( separator );
       
   272             }
       
   273 
       
   274         CleanupStack::PopAndDestroy( extraRowFmt );
       
   275         CleanupStack::PopAndDestroy( subs );
       
   276 
       
   277         do{
       
   278         
       
   279         fits = TryToFitL( iText, aMaxWidth, aFont
       
   280 #ifdef RD_CALENDAR_PREVIEW
       
   281                           , aTwoLines
       
   282 #endif // RD_CALENDAR_PREVIEW
       
   283                           );
       
   284         
       
   285         if( !fits )
       
   286         	{
       
   287         	iText.SetLength( iText.Length()-1 );
       
   288         	}
       
   289         }while( !fits );
       
   290         if ( ! fits )
       
   291             {
       
   292             iText = KNullDesC;
       
   293             TInt last = aPrioritizedFields.Count() - 1;
       
   294             if ( last >= 0 )
       
   295                 {
       
   296                 aPrioritizedFields.Remove( last );
       
   297                 }
       
   298             }
       
   299 
       
   300         } while ( ! fits && aPrioritizedFields.Count() );
       
   301 
       
   302     CleanupStack::PopAndDestroy( &subLabels );
       
   303 
       
   304     TRACE_EXIT_POINT;
       
   305     return iText;
       
   306     }
       
   307 
       
   308 // ---------------------------------------------------------------------------
       
   309 // CCalenExtraRowFormatter::TryToFitL()
       
   310 // ---------------------------------------------------------------------------
       
   311 //
       
   312 TBool CCalenExtraRowFormatter::TryToFitL( const TDesC& aStr, TInt aMaxWidth, const CFont& aFont
       
   313 #ifdef RD_CALENDAR_PREVIEW
       
   314                                           , TBool aTwoLines
       
   315 #endif // RD_CALENDAR_PREVIEW
       
   316                                           )
       
   317     {
       
   318     TRACE_ENTRY_POINT;
       
   319 #ifdef RD_CALENDAR_PREVIEW
       
   320     TBool result(EFalse);
       
   321     if(aTwoLines)
       
   322         {
       
   323         CArrayFixFlat<TPtrC>* textLines = new(ELeave)CArrayFixFlat<TPtrC>( 3 );
       
   324         CleanupStack::PushL( textLines );
       
   325 
       
   326         CArrayFixFlat<TInt>* lineWidths = new( ELeave )CArrayFixFlat<TInt>( 1 );
       
   327         CleanupStack::PushL( lineWidths );
       
   328 
       
   329         lineWidths->AppendL( aMaxWidth );
       
   330 
       
   331         HBufC* visualText = AknBidiTextUtils::ConvertToVisualAndWrapToArrayWholeTextL(
       
   332             aStr,
       
   333             *lineWidths,
       
   334             aFont,
       
   335             *textLines);
       
   336 
       
   337         result = (textLines->Count() <= 2);
       
   338 
       
   339         CleanupStack::PopAndDestroy( lineWidths );
       
   340         CleanupStack::PopAndDestroy( textLines );
       
   341         delete visualText;
       
   342         }
       
   343     else
       
   344         {
       
   345         CFont::TMeasureTextInput::TFlags logicalOrder = static_cast<CFont::TMeasureTextInput::TFlags>(0);
       
   346         TInt textW = AknBidiTextUtils::MeasureTextBoundsWidth( aFont, aStr, logicalOrder );
       
   347         result = (textW <= aMaxWidth);
       
   348         }
       
   349     TRACE_EXIT_POINT;
       
   350     return result;
       
   351 #else
       
   352     CFont::TMeasureTextInput::TFlags logicalOrder = static_cast<CFont::TMeasureTextInput::TFlags>(0);
       
   353     TInt textW = AknBidiTextUtils::MeasureTextBoundsWidth( aFont, aStr, logicalOrder );
       
   354     TRACE_EXIT_POINT;
       
   355     return ( textW <= aMaxWidth );
       
   356 #endif
       
   357     }
       
   358 
       
   359 // End of file